Files
bholman cb40fed3b9 Introduce Coco 3 Plus for MiSTer
Add original-firmware Sound & Speech, four configurable Multi-Pak sockets,
bounded per-slot ROM allocation and a nine-page menu organized around devices
and media. Repair inherited clock/reset/CPU-memory/video paths and add explicit
conditional external-I/O timing contracts. Preserve native Main startup-file
ordering, saved layouts, stock-core separation and the coco3plus media profile.

Provide native Windows tool downloads, pinned ToolShed ROM assembly, fresh
Quartus builds, functional and fitted timing checks, reviewed release packaging
and passwordless SSH installation with backups. Retain original component
notices, document contribution copyright and keep SSC ROM inputs outside Git.

Validation: Quartus17.0.2 Build602 seed5, zero errors; 15 functional jobs,
368 both-corner timing scopes and eight tooling-integrity checks pass.
The renamed build reaches Disk BASIC, PRINT1+1 returns2 and SSC loads ready.
Physical cold power cycling and conditional board-delay assumptions remain
separate. Full original upstream histories are preserved before this single
consolidated implementation commit.
2026-11-18 16:00:45 -07:00

130 lines
5.0 KiB
Systemverilog

// 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])}<lengths[read_slot]);
if(power_reset)begin
p_download<=0;p_wr<=0;p_file_index<=0;p_address<=0;p_data<=0;
valid<=0;fault<=0;active<=0;sampled_valid<=0;count<=0;malformed<=0;
used<=0;
for(n=0;n<4;n=n+1)begin lengths[n]<=0;masks[n]<=0;owned[n]<=0;end
end else begin
p_download<=download;p_wr<=wr;p_file_index<=file_index;
p_address<=address;p_data<=data;
used<=used&~released;
for(n=0;n<4;n=n+1)if(!enabled[n])begin
valid[n]<=0;fault[n]<=0;owned[n]<=0;lengths[n]<=0;
end
if(active&&!enabled[slot])begin
active<=0;used<=used&~released;
end else if(p_download&&selected&&enabled[target]&&!active)begin
active<=1;slot<=target;saved_index<=index;count<=p_wr?1:0;
malformed<=p_wr&&p_address!=0;valid[target]<=0;fault[target]<=0;
used<=start_used;owned[target]<=0;
if(p_wr&&p_address==0)begin
available_block=find_free(start_used);
if(available_block==16)malformed<=1;
else begin
blocks[{target,3'd0}]<=available_block[3:0];
used<=start_used|(16'b1<<available_block);
owned[target]<=16'b1<<available_block;
memory[{available_block[3:0],14'd0}]<=p_data;
end
end
end else if(active&&p_download&&p_wr)begin
if(index!=saved_index||p_address!={7'd0,count}||count>=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<<available_block);
owned[slot]<=owned[slot]|(16'b1<<available_block);
memory[{available_block[3:0],14'd0}]<=p_data;
end
end else memory[{blocks[{slot,p_address[16:14]}],p_address[13:0]}]<=p_data;
end
if(count<131073)count<=count+1'd1;
end
if(active&&enabled[slot]&&!p_download)begin
active<=0;
if(!malformed&&count!=0&&count<=131072)begin
lengths[slot]<=count;next_mask=count-1'd1;
next_mask=next_mask|(next_mask>>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