// 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 // Four private ROM address spaces, up to128 KiB each, sharing256 KiB of16 KiB // blocks. Four full128 KiB memories exceed this core's fitted M10K budget. // Same synchronous read edge as the previous COCO_ROM_CART. Loading one slot // never overwrites another or the built-in disk ROM. Partial/oversized streams // cannot expose stale bytes as a successfully loaded cartridge. module bh_mpi_rom( input wire clk,power_reset,input wire [1:0] read_slot, input wire [16:0] read_address,output wire [7:0] read_data, input wire download,wr,input wire [7:0] file_index, input wire [24:0] address,input wire [7:0] data, output wire loading,output reg [3:0] valid=0,fault=0, output reg loaded_pulse=0,input wire [3:0] enabled); reg [7:0] memory[0:262143]; reg [3:0] blocks[0:31]; reg [15:0] owned[0:3]; reg [15:0] used=0; // HPS changes its bus on the opposite edge. Capture raw fields first; all // allocation/validation/RAM write logic then has a full clock period rather // than decoding HPS index and driving256 M10Ks in half a period. reg p_download=0,p_wr=0; reg [7:0] p_file_index=0,p_data=0; reg [24:0] p_address=0; reg [17:0] lengths[0:3]; reg [16:0] masks[0:3]; wire [5:0] index=p_file_index[5:0]; wire selected=index==1||index==6||index==7||index==8||index==9; wire [1:0] target=index==6?0:index==7?1:index==9?3:2; reg active=0; reg [1:0] slot; reg [5:0] saved_index; reg [17:0] count; reg malformed; reg [7:0] sampled; reg sampled_valid=0; reg [16:0] next_mask; reg [15:0] start_used; reg [15:0] released; reg [4:0] available_block; integer n; integer k; wire [16:0] offset=read_address&masks[read_slot]; function [4:0] find_free(input [15:0] occupancy); integer b; begin find_free=16; for(b=15;b>=0;b=b-1)if(!occupancy[b])find_free=b; end endfunction always_comb begin start_used=0;released=0; for(k=0;k<4;k=k+1)begin if(k!=target&&enabled[k])start_used=start_used|owned[k]; if(!enabled[k])released=released|owned[k]; end end wire raw_selected=file_index[5:0]==1||file_index[5:0]==6|| file_index[5:0]==7||file_index[5:0]==8||file_index[5:0]==9; wire [1:0] raw_target=file_index[5:0]==6?0:file_index[5:0]==7?1:file_index[5:0]==9?3:2; assign loading=(download&&raw_selected&&enabled[raw_target])|| (p_download&&selected&&enabled[target])||active; assign read_data=sampled_valid?sampled:8'hff; always @(posedge clk)begin loaded_pulse<=0; sampled<=memory[{blocks[{read_slot,offset[16:14]}],offset[13:0]}]; sampled_valid<=valid[read_slot]&& ({1'b0,(read_address&masks[read_slot])}=131072) malformed<=1; else if(!malformed)begin if(p_address[13:0]==0)begin available_block=find_free(used&~released); if(available_block==16)malformed<=1; else begin blocks[{slot,p_address[16:14]}]<=available_block[3:0]; used<=(used&~released)|(16'b1<>1);next_mask=next_mask|(next_mask>>2); next_mask=next_mask|(next_mask>>4);next_mask=next_mask|(next_mask>>8); next_mask=next_mask|(next_mask>>16); masks[slot]<=next_mask;valid[slot]<=1;loaded_pulse<=1; end else begin valid[slot]<=0;fault[slot]<=1;lengths[slot]<=0; used<=(used&~released)&~owned[slot];owned[slot]<=0; end end end end endmodule