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

110 lines
5.6 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
// MiSTer HPS file/bus/audio integration. Same57MHz domain; CPU commits on the
// negative edge, cartridge logic on the positive edge. Both half paths are timed.
module ssc_mister_bridge(input wire clk,reset,cpu_reset,mode,
input wire cpu_commit,cpu_rnw,input wire [15:0] cpu_address,input wire [7:0] cpu_data,
input wire ioctl_download,ioctl_wr,input wire [7:0] ioctl_index,
input wire [24:0] ioctl_address,input wire [7:0] ioctl_data,
input wire cartridge_operational,cartridge_fault,
input wire [7:0] cartridge_read_data,input wire signed [15:0] cartridge_audio,
input wire sound_enable,input wire [1:0] sound_select,
input wire [7:0] orchestra_left,orchestra_right,input wire [15:0] legacy_left,legacy_right,
output wire cartridge_reset,host_read,host_write,output wire [15:0] host_address,
output wire [7:0] host_data,output wire read_valid,output wire [7:0] read_data,
output wire palette_owner,rom_files_valid,load_fault,
output wire pic_begin,pic_write,output wire [11:0] pic_address,output wire [7:0] pic_data,
output wire speech_begin,speech_write,output wire [10:0] speech_address,output wire [7:0] speech_data,
output wire [15:0] audio_left,audio_right);
reg download_d,mode_d;
reg [5:0] file_kind;
reg [12:0] count;
reg malformed,pic_valid,speech_valid,pic_bad,speech_bad;
reg pipe_write;
reg [5:0] pipe_kind;
reg [24:0] pipe_address;
reg [7:0] pipe_data;
wire selected=ioctl_index[5:0]==4||ioctl_index[5:0]==5;
wire start=ioctl_download&&!download_d&&selected;
wire finish=!ioctl_download&&download_d;
wire transferring=ioctl_download&&(selected||file_kind==4||file_kind==5);
wire valid_length=(file_kind==4&&count==4096)||(file_kind==5&&count==2048);
assign pic_begin=start&&ioctl_index[5:0]==4;
assign speech_begin=start&&ioctl_index[5:0]==5;
assign pic_write=pipe_write&&pipe_kind==4;
assign speech_write=pipe_write&&pipe_kind==5;
assign pic_address=pipe_address[11:0];assign speech_address=pipe_address[10:0];
assign pic_data=pipe_data;assign speech_data=pipe_data;
assign rom_files_valid=pic_valid&&speech_valid&&!transferring;
assign load_fault=pic_bad||speech_bad||cartridge_fault;
assign palette_owner=!mode;
assign cartridge_reset=cpu_reset||transferring||(mode!=mode_d);
always @(posedge clk)begin
if(reset)begin
download_d<=0;mode_d<=0;file_kind<=0;count<=0;malformed<=0;
pic_valid<=0;speech_valid<=0;pic_bad<=0;speech_bad<=0;
pipe_write<=0;pipe_kind<=0;pipe_address<=0;pipe_data<=0;
end else begin
download_d<=ioctl_download;mode_d<=mode;
pipe_write<=ioctl_download&&ioctl_wr&&selected;
pipe_kind<=ioctl_index[5:0];pipe_address<=ioctl_address;pipe_data<=ioctl_data;
if(start)begin
file_kind<=ioctl_index[5:0];count<=ioctl_wr?13'd1:13'd0;
malformed<=ioctl_wr&&ioctl_address!=0;
if(ioctl_index[5:0]==4)begin pic_valid<=0;pic_bad<=0;end
else begin speech_valid<=0;speech_bad<=0;end
end else if(ioctl_download&&ioctl_wr&&(file_kind==4||file_kind==5))begin
if(ioctl_index[5:0]!=file_kind||ioctl_address!={12'd0,count})malformed<=1;
if(count<4097)count<=count+13'd1;else malformed<=1;
end
if(finish)begin
if(file_kind==4)begin pic_valid<=!malformed&&valid_length;pic_bad<=malformed||!valid_length;end
else if(file_kind==5)begin speech_valid<=!malformed&&valid_length;speech_bad<=malformed||!valid_length;end
file_kind<=0;
end
end
end
// Toggle handshake captures an old CPU bus at its real negative-edge commit.
// It clears after the following positive-edge consume, leaving read data stable
// before a possible back-to-back CPU read on the next negative edge.
reg write_toggle=0,consumed_toggle=0;
reg [15:0] write_address;
reg [7:0] write_data;
reg cpu_mode=0;
reg [7:0] status_sample=8'h7f;
wire write_pending=write_toggle!=consumed_toggle;
always @(negedge clk)begin
// CPU and this snapshot consume OLD values on the same negative edge.
// The CPU therefore sees a full-cycle-stable status through its read mux;
// this adds at most one57MHz tick of status visibility, no CPU wait state.
cpu_mode<=mode;
if(reset||cpu_reset||!rom_files_valid)status_sample<=8'h7f;
else status_sample<=cartridge_read_data;
if(reset||cpu_reset)begin write_toggle<=0;write_address<=0;write_data<=0;end
else if(cpu_commit&&!cpu_rnw)begin
write_address<=cpu_address;write_data<=cpu_data;write_toggle<=!write_toggle;
end
end
always @(posedge clk)if(reset)consumed_toggle<=0;else consumed_toggle<=write_toggle;
assign host_address=write_address;
assign host_data=write_data;
assign host_write=write_pending&&!cpu_reset&&rom_files_valid&&mode;
assign host_read=1'b0;
assign read_valid=cpu_mode&&cpu_rnw&&(cpu_address==16'hff7d||cpu_address==16'hff7e);
assign read_data=!read_valid ? 8'd0 : cpu_address==16'hff7d ? 8'hff :
status_sample;
// In cartridge mux position replace the stale internal-DAC contribution.
// Orchestra remains independent stereo. Its reset midpoint is128/0x8080.
wire select_cartridge=mode&&sound_enable&&sound_select==2;
wire signed [17:0] contribution=rom_files_valid&&cartridge_operational ?
{{2{cartridge_audio[15]}},cartridge_audio} : 18'sd0;
wire signed [17:0] sum_left=$signed({2'd0,orchestra_left,orchestra_left})-18'sd128+contribution;
wire signed [17:0] sum_right=$signed({2'd0,orchestra_right,orchestra_right})-18'sd128+contribution;
function [15:0] saturate(input signed [17:0] value);
begin saturate=value<0 ? 16'd0 : value>18'sd65535 ? 16'hffff : value[15:0];end
endfunction
assign audio_left=select_cartridge?saturate(sum_left):legacy_left;
assign audio_right=select_cartridge?saturate(sum_right):legacy_right;
endmodule