Files
CoCo3_Plus_MiSTer/tests/video_sdram_tb.sv
T
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

199 lines
8.3 KiB
Systemverilog

`timescale 1ns/1ps
// Logical SDRAM command/data model. Not an electrical SDRAM timing model.
module video_read_model #(parameter RETURN_DELAY_NS=0, COMMAND_DELAY_NS=0)(input clk, input [2:0] command,
input [12:0] address, input [1:0] bank, inout [15:0] dq,
output integer reads=0, output integer refreshes=0);
reg [12:0] rows[0:3];
reg [15:0] pipe[0:3];
reg [3:0] valid=0;
reg [15:0] drive=16'hzzzz;
reg [23:0] word_address;
integer i;
wire [2:0] pin_command;
wire [12:0] pin_address;
wire [1:0] pin_bank;
assign #(COMMAND_DELAY_NS) pin_command=command;
assign #(COMMAND_DELAY_NS) pin_address=address;
assign #(COMMAND_DELAY_NS) pin_bank=bank;
assign dq=drive;
function [15:0] contents(input [23:0] a);
contents=a[15:0] ^ {a[23:16],a[23:16]} ^ 16'h96a5;
endfunction
always @(negedge clk) begin
drive <= #(RETURN_DELAY_NS) (valid[0]?pipe[0]:16'hzzzz);
for(i=0;i<3;i=i+1) pipe[i]=pipe[i+1];
valid=valid>>1;
if(pin_command===3'b011) rows[pin_bank]=pin_address;
if(pin_command===3'b001) refreshes=refreshes+1;
if(pin_command===3'b101) begin
word_address={pin_bank,rows[pin_bank],pin_address[8:0]};
// Sequential burst length two wraps within the aligned pair.
pipe[2]=contents(word_address);pipe[3]=contents(word_address ^ 24'd1);
valid[2]=1;valid[3]=1;reads=reads+1;
end
end
endmodule
module video_channel #(parameter CANDIDATE=0)(
input clk,reset,hborder, input [24:0] base,
input [6:0] offset, input coco1, input [3:0] hres,
input wide,contention, output [8:0] buffer_address,
output [15:0] buffer_data, output buffer_write,
output [4:0] fetch_state, output integer cpu_reads=0,
output integer memory_reads,refreshes);
wire [24:0] vid_address;
wire request,ack,ready;
wire [15:0] dout,dq;
wire [12:0] row;
wire [1:0] bank;
wire ras,cas,we,cpu_ack,cpu_ready;
reg cpu_request=0;
integer phase=0;
// Independent competing CPU requests; each held until acknowledgement.
always @(posedge clk) begin
if(reset) begin cpu_request<=0;phase<=0;end
else begin
phase<=phase+1;
if(cpu_ack) cpu_request<=0;
else if(contention && phase%23==0) cpu_request<=1;
if(cpu_ready) cpu_reads<=cpu_reads+1;
end
end
coco_mem_fetch fetch(.fast_clk(clk),.RESET_N(!reset),
.SDRAM_VID_REQ(request),.SDRAM_VID_ADDR(vid_address),
.SDRAM_VID_ACK(ack),.SDRAM_VID_READY(ready),.SDRAM_DOUT(dout),
.BUFF_ADD(buffer_address),.BUFF_DATA_O(buffer_data),.BUFFER_WRITE(buffer_write),
.RAM_ADDRESS(base),.HBORDER(hborder),.HOR_OFFSET(offset),
.COCO1(coco1),.HRES(hres),.CT_320_160(wide));
assign fetch_state=fetch.state;
generate if(CANDIDATE) begin
sdram_candidate memory(.init(reset),.clk(clk),.SDRAM_DQ(dq),.SDRAM_A(row),
.SDRAM_BA(bank),.SDRAM_nRAS(ras),.SDRAM_nCAS(cas),.SDRAM_nWE(we),
.sdram_dout(dout),.sdram_cpu_addr(25'h1023456),.sdram_cpu_din(8'd0),
.sdram_cpu_req(cpu_request),.sdram_cpu_rnw(1'b1),.sdram_cpu_ack(cpu_ack),
.sdram_cpu_ready(cpu_ready),.sdram_vid_addr(vid_address),.sdram_vid_req(request),
.sdram_vid_ack(ack),.sdram_vid_ready(ready));
end else begin
sdram_reference memory(.init(reset),.clk(clk),.SDRAM_DQ(dq),.SDRAM_A(row),
.SDRAM_BA(bank),.SDRAM_nRAS(ras),.SDRAM_nCAS(cas),.SDRAM_nWE(we),
.sdram_dout(dout),.sdram_cpu_addr(25'h1023456),.sdram_cpu_din(8'd0),
.sdram_cpu_req(cpu_request),.sdram_cpu_rnw(1'b1),.sdram_cpu_ack(cpu_ack),
.sdram_cpu_ready(cpu_ready),.sdram_vid_addr(vid_address),.sdram_vid_req(request),
.sdram_vid_ack(ack),.sdram_vid_ready(ready));
end endgenerate
`ifndef REFERENCE_RETURN_DELAY
`define REFERENCE_RETURN_DELAY 0
`endif
`ifndef CANDIDATE_RETURN_DELAY
`define CANDIDATE_RETURN_DELAY 0
`endif
// The functional comparison tests the payload/handshake at the declared return
// latency. Physical min/max windows are checked separately by external-I/O STA.
video_read_model #(.RETURN_DELAY_NS(CANDIDATE ? `CANDIDATE_RETURN_DELAY : `REFERENCE_RETURN_DELAY)) model(clk,{ras,cas,we},row,bank,dq,memory_reads,refreshes);
endmodule
module video_sdram_tb;
reg clk=0,reset=1,hborder=0;
always #5 clk=~clk;
reg [24:0] base=0;
reg [6:0] offset=0;
reg coco1=0,wide=1,contention=0;
reg [3:0] hres=0;
wire [8:0] address[0:1];
wire [15:0] data[0:1];
wire [1:0] write_buffer;
wire [4:0] state[0:1];
wire [31:0] cpu_reads[0:1],memory_reads[0:1],refreshes[0:1];
genvar g;
generate for(g=0;g<2;g=g+1) begin
video_channel #(.CANDIDATE(g)) channel(clk,reset,hborder,base,offset,coco1,hres,
wide,contention,address[g],data[g],write_buffer[g],state[g],
cpu_reads[g],memory_reads[g],refreshes[g]);
end endgenerate
reg checking=0;
integer count[0:1];
reg [15:0] reference_data[0:511],candidate_data[0:511];
integer k,limit,scenario=0,total_words=0;
reg [8:0] word_offset;
reg [24:0] expected_byte;
reg [23:0] expected_word;
reg [15:0] expected_data;
integer beat_index;
always @(posedge clk) begin
if(checking) for(k=0;k<2;k=k+1) if(write_buffer[k]) begin
if(address[k]!==count[k]) $fatal(1,"buffer index channel=%0d scenario=%0d count=%0d actual=%h",k,scenario,count[k],address[k]);
if(count[k]>=512) $fatal(1,"buffer overrun");
if(k==0) reference_data[count[k]]=data[k];else candidate_data[count[k]]=data[k];
// Check the visible line against an address-derived oracle, independent of
// the DUT FSM. Extra upstream guard words are compared separately below.
if(count[k]<limit) begin
// Predict the pair transactions from the starting word alignment. On an
// odd start the first pair contributes one word, subsequent pairs two.
// Apply the upstream window mask to each transaction, not each beat:
// those differ at a 256-byte wrap when the base itself is odd-word aligned.
if(base[1]^offset[0]) begin
word_offset=(count[k]==0)?offset:(offset+1+((count[k]-1)&~1));
beat_index=(count[k]==0)?0:((count[k]-1)&1);
end else begin
word_offset=offset+(count[k]&~1);beat_index=count[k]&1;
end
if(coco1 || !hres[3]) word_offset=word_offset & 9'h07f;
expected_byte=base+{word_offset,1'b0};
expected_word=expected_byte[24:1] ^ beat_index;
expected_data=expected_word[15:0] ^ {expected_word[23:16],expected_word[23:16]} ^ 16'h96a5;
if(data[k]!==expected_data) $fatal(1,"video data channel=%0d scenario=%0d word=%0d expected=%h got=%h",k,scenario,count[k],expected_data,data[k]);
end
count[k]=count[k]+1;
end
end
task line(input [24:0] a,input [6:0] o,input c,input [3:0] h,input w,input busy_cpu);
integer timeout,j;
begin
@(negedge clk);#1;hborder=0;checking=0;
base=a;offset=o;coco1=c;hres=h;wide=w;contention=busy_cpu;
repeat(25) @(negedge clk);
#1;count[0]=0;count[1]=0;limit=w?320:160;checking=1;hborder=1;
timeout=0;
while((state[0]!==10 || state[1]!==10) && timeout<12000) begin
@(negedge clk);timeout=timeout+1;
end
if(timeout==12000) $fatal(1,"line fetch timeout scenario=%0d",scenario);
repeat(20) @(negedge clk);
#1;checking=0;
if(count[0]<limit || count[0]!==count[1]) $fatal(1,"line length mismatch %0d %0d",count[0],count[1]);
for(j=0;j<count[0];j=j+1) if(reference_data[j]!==candidate_data[j]) $fatal(1,"baseline/candidate mismatch word=%0d",j);
total_words=total_words+count[1];scenario=scenario+1;
end
endtask
integer mode,off,trial;
initial begin
count[0]=0;count[1]=0;
repeat(8) @(negedge clk);#1;reset=0;
repeat(26000) @(negedge clk);
// Address carry boundaries, all mode bits, odd/even starts, 256/1024-byte
// wrapping and both fetch widths; CPU activity exercises arbitration.
for(mode=0;mode<32;mode=mode+1)
for(off=0;off<4;off=off+1)
line((mode%4==0)?25'h1fffffe:((mode%4==1)?25'h07ffffe:25'h00003fe),
(off<2)?off:(126+off-2),mode[4],mode[3:0],mode[0],mode[1]);
for(trial=0;trial<48;trial=trial+1)
line($random & 25'h1fffffe,$random,$random,$random,$random,1);
if(cpu_reads[0]<10 || cpu_reads[1]<10 || refreshes[0]<3 || refreshes[1]<3)
$fatal(1,"contention/refresh not exercised");
$display("PASS %0d lines / %0d buffered words: baseline equivalence, address-derived data, mode/offset/wrap, CPU contention, refresh",scenario,total_words);
// Abort a line at several phases. Reset the actual fetch/controller and
// retain the production-length SDRAM startup, then fetch anew.
for(trial=0;trial<4;trial=trial+1) begin
@(negedge clk);#1;hborder=0;
repeat(30) @(negedge clk);#1;hborder=1;
repeat(3+trial*7) @(negedge clk);#1;reset=1;hborder=0;
repeat(4) @(negedge clk);#1;reset=0;
repeat(26000) @(negedge clk);
line(25'h01ffffe,trial[6:0],0,8,1,1);
end
$display("PASS four active-line reset/reinitialization phases; %0d total lines",scenario);
$finish;
end
initial begin #30000000;$fatal(1,"video regression timeout");end
endmodule