// Original Coco 3 Plus contributions: Copyright (c) 2026 Brian K. Holman. // Original component notices/terms retained; see COPYRIGHT.md and LICENSING.md. // SPDX-License-Identifier: BSD-3-Clause // Single-chip PIC7040 RF/ROM map and peripheral bus adapter. See LICENSE. // ROM is volatile, sequentially loaded 0..4095 after rom_begin. No ROM bytes // are built into this source. A malformed load invalidates the image. module ssc_tms7000_memory(input wire clk,reset, input wire rom_begin,rom_write,input wire [11:0] rom_address,input wire [7:0] rom_data, output reg rom_loaded,load_error,output reg initialized, input wire request,write,peripheral,input wire [15:0] address,input wire [7:0] write_data, output reg ready,output reg [7:0] read_data, output reg pf_request,pf_write,output reg [7:0] pf_address,pf_write_data, input wire pf_ready,input wire [7:0] pf_read_data); localparam [2:0] IDLE=0,EXECUTE=1,READ_RETURN=2,PF_WAIT=3,PF_RELEASE=4,WAIT_RELEASE=5; reg [2:0] state; reg [7:0] rf[0:127],rom[0:4095]; reg [7:0] rf_read,rom_read; reg [6:0] clear_address; reg [15:0] access_address; reg [7:0] access_data; reg access_write,access_peripheral,loading; reg [11:0] expected_address; wire rf_store=initialized && !rom_begin && state==EXECUTE && access_write && access_address<128 && !access_peripheral; wire rom_store=!reset && !rom_begin && rom_write && loading && !load_error && rom_address==expected_address; always @(posedge clk)begin rf_read<=rf[access_address[6:0]];rom_read<=rom[access_address[11:0]]; if(!reset && !initialized)rf[clear_address]<=0; else if(!reset && rf_store)rf[access_address[6:0]]<=access_data; if(rom_store)rom[rom_address]<=rom_data; end always @(posedge clk)begin if(reset)begin initialized<=0;clear_address<=0;state<=IDLE;ready<=0;read_data<=0; access_address<=0;access_data<=0;access_write<=0;access_peripheral<=0; pf_request<=0;pf_write<=0;pf_address<=0;pf_write_data<=0; rom_loaded<=0;load_error<=0;loading<=0;expected_address<=0; end else begin if(!initialized)begin if(clear_address==127)initialized<=1;else clear_address<=clear_address+7'd1; end if(rom_begin)begin rom_loaded<=0;load_error<=0;loading<=1;expected_address<=0;end else if(rom_write)begin if(!loading || load_error || rom_address!=expected_address)begin rom_loaded<=0;load_error<=1;loading<=0;end else if(expected_address==4095)begin rom_loaded<=1;loading<=0;end else expected_address<=expected_address+12'd1; end if(rom_begin)begin state<=IDLE;ready<=0;pf_request<=0;end else case(state) IDLE:if(initialized && request)begin access_address<=address;access_data<=write_data;access_write<=write;access_peripheral<=peripheral; state<=EXECUTE; end EXECUTE:begin if(access_peripheral || (access_address>=16'h100 && access_address<16'h10c))begin pf_request<=1;pf_write<=access_write;pf_address<=access_address[7:0];pf_write_data<=access_data;state<=PF_WAIT; end else state<=READ_RETURN; end READ_RETURN:begin read_data<=access_address<128?rf_read:access_address<256?8'h00: access_address>=16'hf000 ? rom_read:8'hff; ready<=1;state<=WAIT_RELEASE; end PF_WAIT:if(pf_ready)begin read_data<=pf_read_data;pf_request<=0;state<=PF_RELEASE;end PF_RELEASE:if(!pf_ready)begin ready<=1;state<=WAIT_RELEASE;end WAIT_RELEASE:if(!request)begin ready<=0;state<=IDLE;end default:state<=IDLE; endcase end end endmodule