Skip to content

Commit

Permalink
Make cart module use analogue pocket SRAM instead of FPGA BRAM
Browse files Browse the repository at this point in the history
  • Loading branch information
budude2 committed Jul 25, 2024
1 parent 5eccd7e commit d33e703
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 43 deletions.
9 changes: 6 additions & 3 deletions src/ap_core.qsf
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# ========================
set_global_assignment -name ORIGINAL_QUARTUS_VERSION 18.1.1
set_global_assignment -name PROJECT_CREATION_TIME_DATE "21:31:36 JANUARY 22, 2020"
set_global_assignment -name LAST_QUARTUS_VERSION "23.1std.0 Lite Edition"
set_global_assignment -name LAST_QUARTUS_VERSION "23.1std.1 Lite Edition"
set_global_assignment -name PROJECT_OUTPUT_DIRECTORY output_files
set_global_assignment -name PRE_FLOW_SCRIPT_FILE "quartus_sh:apf/build_id_gen.tcl"
set_global_assignment -name SMART_RECOMPILE ON
Expand Down Expand Up @@ -312,7 +312,7 @@ set_global_assignment -name PHYSICAL_SYNTHESIS_COMBO_LOGIC ON
set_global_assignment -name PHYSICAL_SYNTHESIS_REGISTER_DUPLICATION ON
set_global_assignment -name PHYSICAL_SYNTHESIS_REGISTER_RETIMING ON
set_global_assignment -name PHYSICAL_SYNTHESIS_ASYNCHRONOUS_SIGNAL_PIPELINING ON
set_global_assignment -name FITTER_EFFORT "AUTO FIT"
set_global_assignment -name FITTER_EFFORT "FAST FIT"
set_global_assignment -name ROUTER_LCELL_INSERTION_AND_LOGIC_DUPLICATION ON

# Assembler Assignments
Expand Down Expand Up @@ -741,6 +741,8 @@ set_instance_assignment -name WEAK_PULL_UP_RESISTOR ON -to bridge_1wire
set_instance_assignment -name WEAK_PULL_UP_RESISTOR ON -to bridge_spiclk
set_instance_assignment -name WEAK_PULL_UP_RESISTOR ON -to bridge_spimiso
set_instance_assignment -name WEAK_PULL_UP_RESISTOR ON -to bridge_spimosi
set_global_assignment -name NUM_PARALLEL_PROCESSORS ALL
set_global_assignment -name SYSTEMVERILOG_FILE gb/sram_128k_x1_x16.sv
set_global_assignment -name SYSTEMVERILOG_FILE gb/rtc_loader.sv
set_global_assignment -name SYSTEMVERILOG_FILE gb/rumbler.sv
set_global_assignment -name SYSTEMVERILOG_FILE gb/save_handler.sv
Expand Down Expand Up @@ -815,5 +817,6 @@ set_global_assignment -name SIP_FILE core/mf_pllbase.sip
set_global_assignment -name SOURCE_FILE db/ap_core.cmp.rdb
set_global_assignment -name SIGNALTAP_FILE stp1.stp
set_global_assignment -name QIP_FILE gb/rtc_ram/rtc_ram.qip
set_global_assignment -name NUM_PARALLEL_PROCESSORS ALL
set_global_assignment -name ALM_REGISTER_PACKING_EFFORT MEDIUM
set_global_assignment -name FINAL_PLACEMENT_OPTIMIZATION NEVER
set_instance_assignment -name PARTITION_HIERARCHY root_partition -to | -section_id Top
6 changes: 3 additions & 3 deletions src/apf/build_id.mif
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ DATA_RADIX = HEX;
CONTENT
BEGIN

0E0 : 20240617;
0E1 : 00215826;
0E2 : 5986fad0;
0E0 : 20240725;
0E1 : 00181442;
0E2 : 53904e6c;

END;
24 changes: 16 additions & 8 deletions src/core/core_top.sv
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,6 @@ assign cram1_we_n = 1;
assign cram1_ub_n = 1;
assign cram1_lb_n = 1;

assign sram_a = 'h0;
assign sram_dq = {16{1'bZ}};
assign sram_oe_n = 1;
assign sram_we_n = 1;
assign sram_ub_n = 1;
assign sram_lb_n = 1;

assign dbg_tx = 1'bZ;
assign user1 = 1'bZ;
assign aux_scl = 1'bZ;
Expand Down Expand Up @@ -624,6 +617,13 @@ always_ff @(posedge clk_74a) begin
else if (dataslot_allcomplete) ioctl_download <= 0;
end

reg ioctl_upload = 0;

always_ff @(posedge clk_74a) begin
if (dataslot_requestread) ioctl_upload <= 1;
else if (dataslot_allcomplete) ioctl_upload <= 0;
end

logic [14:0] cart_addr;
logic [22:0] mbc_addr;
logic cart_a15, cart_rd, cart_wr, cart_oe, nCS;
Expand Down Expand Up @@ -813,7 +813,15 @@ cart_top cart
.Savestate_CRAMWriteData ( 0 ),
.Savestate_CRAMReadData ( ),

.rumbling ( rumbling )
.rumbling ( rumbling ),

// SRAM External Interface
.sram_addr ( sram_a ), //! Address Out
.sram_dq ( sram_dq ), //! Data In/Out
.sram_oe_n ( sram_oe_n ), //! Output Enable
.sram_we_n ( sram_we_n ), //! Write Enable
.sram_ub_n ( sram_ub_n ), //! Upper Byte Mask
.sram_lb_n ( sram_lb_n ) //! Lower Byte Mask
);

reg [127:0] palette = 128'h828214517356305A5F1A3B4900000000;
Expand Down
67 changes: 38 additions & 29 deletions src/gb/cart.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module cart_top (
output isSGB_game,

input ioctl_download,
input ioctl_upload,
input ioctl_wr,
input [24:0] ioctl_addr,
input [15:0] ioctl_dout,
Expand Down Expand Up @@ -70,7 +71,14 @@ module cart_top (
input Savestate_CRAMRWrEn,
input [7:0] Savestate_CRAMWriteData,
output [7:0] Savestate_CRAMReadData,
output rumbling
output rumbling,

output [16:0] sram_addr,
inout [15:0] sram_dq,
output sram_oe_n,
output sram_we_n,
output sram_ub_n,
output sram_lb_n
);
///////////////////////////////////////////////////

Expand Down Expand Up @@ -408,7 +416,7 @@ assign cram_rd = cart_rd & is_cram_addr;
assign cram_wr = sleep_savestate ? Savestate_CRAMRWrEn : mbc_cram_wr || (cart_wr & is_cram_addr & mbc_ram_enable);

wire [16:0] cram_addr = sleep_savestate ? Savestate_CRAMAddr[16:0] : mbc_cram_addr;
wire [7:0] cram_di = sleep_savestate ? Savestate_CRAMWriteData : mbc_cram_wr ? mbc_cram_wr_do : cart_di;
wire [7:0] cram_di = sleep_savestate ? Savestate_CRAMWriteData : mbc_cram_wr ? mbc_cram_wr_do : cart_di;

// RAM size
assign ram_mask_file = // 0 - no ram
Expand All @@ -421,33 +429,34 @@ assign ram_mask_file = // 0 - no ram

assign has_save = mbc_battery && (cart_ram_size > 0 || mbc2 || mbc7 || tama);

// Up to 8kb * 16banks of Cart Ram (128kb)
dpram #(16) cram_l (
.clock_a (clk_sys),
.address_a (cram_addr[16:1]),
.wren_a (cram_wr & ~cram_addr[0]),
.data_a (cram_di),
.q_a (cram_q_l),

.clock_b (clk_sys),
.address_b (bk_addr[15:0]),
.wren_b (bk_wr),
.data_b (bk_data[7:0]),
.q_b (bk_q[7:0])
);

dpram #(16) cram_h (
.clock_a (clk_sys),
.address_a (cram_addr[16:1]),
.wren_a (cram_wr & cram_addr[0]),
.data_a (cram_di),
.q_a (cram_q_h),

.clock_b (clk_sys),
.address_b (bk_addr[15:0]),
.wren_b (bk_wr),
.data_b (bk_data[15:8]),
.q_b (bk_q[15:8])
wire bk_en = ioctl_download || ioctl_upload;
wire [15:0] q, cram_q_o;

assign cram_q_h = cram_q_o[15:8];
assign cram_q_l = cram_q_o[7:0];
assign bk_q = cram_q_o;

sram u_cram
(
// Clock and Reset
.clk ( clk_sys ), //! Input Clock
.reset ( reset ), //! Reset

// Single Port Internal Bus Interface
.we ( bk_en ? bk_wr : cram_wr ), //! Write Enable
.ub ( cram_wr & cram_addr[0] ),
.lb ( cram_wr & ~cram_addr[0] ),
.addr ( bk_en ? bk_addr : cram_addr[16:1] ), //! Address In
.d ( bk_en ? bk_data : {cram_di, cram_di} ), //! Data In
.q ( cram_q_o ), //! Data Out

// SRAM External Interface
.sram_addr ( sram_addr ), //! Address Out
.sram_dq ( sram_dq ), //! Data In/Out
.sram_oe_n ( sram_oe_n ), //! Output Enable
.sram_we_n ( sram_we_n ), //! Write Enable
.sram_ub_n ( sram_ub_n ), //! Upper Byte Mask
.sram_lb_n ( sram_lb_n ) //! Lower Byte Mask
);

endmodule
78 changes: 78 additions & 0 deletions src/gb/sram_128k_x1_x16.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//------------------------------------------------------------------------------
// SPDX-License-Identifier: MIT
// SPDX-FileType: SOURCE
// SPDX-FileCopyrightText: (c) 2023, OpenGateware authors and contributors
//------------------------------------------------------------------------------
//
// SRAM Controller
//
// Copyright (c) 2023, Marcus Andrade <marcus@opengateware.org>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//------------------------------------------------------------------------------

`default_nettype none

module sram
(
// Clock and Reset
input wire clk, //! Input Clock
input wire reset, //! Reset
// Single Port Internal Bus Interface
input wire we, //! Write Enable
input wire ub,
input wire lb,
input wire [16:0] addr, //! Address In
input wire [15:0] d, //! Data In
output reg [15:0] q, //! Data Out
// SRAM External Interface
output reg [16:0] sram_addr, //! Address Out
inout reg [15:0] sram_dq, //! Data In/Out
output reg sram_oe_n, //! Output Enable
output reg sram_we_n, //! Write Enable
output reg sram_ub_n, //! Upper Byte Mask
output reg sram_lb_n //! Lower Byte Mask
);

always @(posedge clk) begin : RW_SRAM
if(reset) begin
{sram_lb_n, sram_ub_n} <= 2'b11; // Mask Low/High Byte
sram_addr <= {17{1'bX}}; // Set Address as "Don't Care"
sram_dq <= {16{1'bZ}}; // Set Data Bus as High Impedance (Tristate)
end
else begin
sram_addr <= {17{1'bX}}; // Set Address as "Don't Care"
sram_dq <= {16{1'bZ}}; // Set Data Bus as High Impedance (Tristate)
if(we) begin
{sram_oe_n, sram_we_n} <= 2'b10; // Output Disabled/Write Enabled
{sram_lb_n, sram_ub_n} <= {~lb, ~ub};
sram_addr <= addr; // Set Address
sram_dq <= d; // Write Data
end
else begin
{sram_oe_n, sram_we_n} <= 2'b01; // Write Disabled/Output Enabled
{sram_lb_n, sram_ub_n} <= 2'b00; // Mask Low/High Byte
sram_addr <= addr; // Set Address
q <= sram_dq; // Read Data
end
end
end

endmodule

0 comments on commit d33e703

Please sign in to comment.