SummerCart64/fw/rtl/n64/n64_flashram.sv
Mateusz Faderewski 421d0438f3
[SC64][FW][SW] Controller rewrite to remove task subsystem + minor bug fixes (#64)
This PR completely removes `task.c / task.h` from `sw/controller` STM32
code.
Additionally, these changes were implemented:
- Updated IPL3
- Added new diagnostic data (voltage and temperature) readout commands
for USB and N64
- Fixed some issues with FlashRAM save type
- Joybus timings were relaxed to accommodate communication with
unsynchronized master controller (like _Datel Game Killer_, thanks
@RWeick)
- N64 embedded test program now waits for release of button press to
proceed
- Fixed issue where, in rare circumstances, I2C peripheral in STM32
would get locked-up on power-up
- LED blinking behavior on SD card access was changed
- LED blink duration on save writeback has been extended
- Minor fixes through the entire of hardware abstraction layer for STM32
code
- Primer now correctly detects issues with I2C bus during first time
programming
- `primer.py` script gives more meaningful error messages
- Fixed bug where RTC time was always written on N64FlashcartMenu boot
- sc64deployer now displays "Diagnostic data" instead of "MCU stack
usage"
2024-01-29 14:23:18 +01:00

153 lines
5.3 KiB
Systemverilog

module n64_flashram (
input clk,
input reset,
n64_reg_bus.flashram reg_bus,
n64_scb.flashram n64_scb
);
localparam [31:0] FLASH_TYPE_ID = 32'h1111_8001;
localparam [31:0] FLASH_MODEL_ID = 32'h0032_00F1;
typedef enum bit [7:0] {
CMD_STATUS_MODE = 8'hD2,
CMD_READID_MODE = 8'hE1,
CMD_READ_MODE = 8'hF0,
CMD_ERASE_SECTOR = 8'h4B,
CMD_ERASE_CHIP = 8'h3C,
CMD_BUFFER_MODE = 8'hB4,
CMD_ERASE_START = 8'h78,
CMD_WRITE_START = 8'hA5
} e_cmd;
typedef enum bit [1:0] {
STATE_STATUS,
STATE_ID,
STATE_READ,
STATE_BUFFER
} e_state;
typedef enum bit [1:0] {
WRITE_BUSY,
ERASE_BUSY,
WRITE_DONE,
ERASE_DONE
} e_status_bits;
e_state state;
logic [3:0] status;
logic [7:0] cmd;
logic erase_enabled;
always_comb begin
n64_scb.flashram_read_mode = (state == STATE_READ);
reg_bus.rdata = 16'd0;
if (state == STATE_ID) begin
case (reg_bus.address[2:1])
0: reg_bus.rdata = FLASH_TYPE_ID[31:16];
1: reg_bus.rdata = FLASH_TYPE_ID[15:0];
2: reg_bus.rdata = FLASH_MODEL_ID[31:16];
3: reg_bus.rdata = FLASH_MODEL_ID[15:0];
endcase
end else if (reg_bus.address[1]) begin
reg_bus.rdata = {12'd0, status};
end
end
always_ff @(posedge clk) begin
if (reset) begin
state <= STATE_STATUS;
status <= 4'b0000;
erase_enabled <= 1'b0;
n64_scb.flashram_pending <= 1'b0;
end else begin
if (n64_scb.flashram_done) begin
n64_scb.flashram_pending <= 1'b0;
if (n64_scb.flashram_write_or_erase) begin
status[ERASE_BUSY] <= 1'b0;
status[ERASE_DONE] <= 1'b1;
end else begin
status[WRITE_BUSY] <= 1'b0;
status[WRITE_DONE] <= 1'b1;
end
end
if (reg_bus.write && !n64_scb.flashram_pending) begin
if (reg_bus.address[16]) begin
if (!reg_bus.address[1]) begin
cmd <= reg_bus.wdata[15:8];
end else begin
erase_enabled <= 1'b0;
case (cmd)
CMD_STATUS_MODE: begin
state <= STATE_STATUS;
end
CMD_READID_MODE: begin
state <= STATE_ID;
end
CMD_READ_MODE: begin
state <= STATE_READ;
end
CMD_ERASE_SECTOR: begin
state <= STATE_STATUS;
erase_enabled <= 1'b1;
n64_scb.flashram_page <= reg_bus.wdata[9:0];
n64_scb.flashram_sector_or_all <= 1'b0;
end
CMD_ERASE_CHIP: begin
state <= STATE_STATUS;
erase_enabled <= 1'b1;
n64_scb.flashram_page <= 10'd0;
n64_scb.flashram_sector_or_all <= 1'b1;
end
CMD_BUFFER_MODE: begin
state <= STATE_BUFFER;
end
CMD_ERASE_START: begin
state <= STATE_STATUS;
if (erase_enabled) begin
status[ERASE_BUSY] <= 1'b1;
status[ERASE_DONE] <= 1'b0;
n64_scb.flashram_pending <= 1'b1;
n64_scb.flashram_write_or_erase <= 1'b1;
end
end
CMD_WRITE_START: begin
state <= STATE_STATUS;
status[WRITE_BUSY] <= 1'b1;
status[WRITE_DONE] <= 1'b0;
n64_scb.flashram_page <= reg_bus.wdata[9:0];
n64_scb.flashram_pending <= 1'b1;
n64_scb.flashram_write_or_erase <= 1'b0;
n64_scb.flashram_sector_or_all <= 1'b0;
end
endcase
end
end else begin
if (reg_bus.address[1] && state == STATE_STATUS) begin
status[ERASE_DONE] <= 1'b0;
status[WRITE_DONE] <= 1'b0;
end
end
end
end
end
always_comb begin
n64_scb.flashram_write = reg_bus.write && !reg_bus.address[16] && state == STATE_BUFFER;
n64_scb.flashram_address = reg_bus.address[6:1];
n64_scb.flashram_wdata = reg_bus.wdata;
end
endmodule