sync chunk 11/12

This commit is contained in:
chunk-sync
2026-09-11 16:18:36 +08:00
parent c315cef09e
commit c2c1bb2648
922 changed files with 102283 additions and 6793 deletions
@@ -0,0 +1,208 @@
`timescale 1 ns / 1 ps
//------------------------------------------------------------------------------
// csi040_ctrl testbench
//
// testbench BRAM DUT SPI 线
// 32 bit SPI
// [opcode][addr high][addr low][data]
//
// ASIC SO direct read
// payload byte read_response
//------------------------------------------------------------------------------
module csi040_ctrl_tb;
localparam CLK_PERIOD = 10;
// DUT 线 ASIC
reg clk;
reg rst_n;
reg [12:0] bram_addr;
reg [31:0] bram_din;
wire [31:0] bram_dout;
reg bram_en;
reg [3:0] bram_we;
wire busy;
wire spi_done;
wire error_busy;
wire asic_rst_n;
wire asic_trig;
wire asic_csn;
wire asic_sck;
wire asic_si;
reg asic_so;
// SPI monitor CSN shift_in SI bit
integer bit_cnt;
integer frame_bits;
reg [31:0] shift_in;
reg [31:0] frame_in;
reg [7:0] read_response;
// 使 0x02/0x03 opcode仿
// SPI
csi040_ctrl#
(
.SPI_CLK_DIV ( 2 ),
.SPI_OPCODE_WRITE ( 8'h02 ),
.SPI_OPCODE_READ ( 8'h03 )
)
dut
(
.clk ( clk ),
.rst_n ( rst_n ),
.bram_addr ( bram_addr ),
.bram_clk ( clk ),
.bram_din ( bram_din ),
.bram_dout ( bram_dout ),
.bram_en ( bram_en ),
.bram_rst ( 1'b0 ),
.bram_we ( bram_we ),
.busy ( busy ),
.spi_done ( spi_done ),
.error_busy ( error_busy ),
.asic_rst_n ( asic_rst_n ),
.asic_trig ( asic_trig ),
.asic_csn ( asic_csn ),
.asic_sck ( asic_sck ),
.asic_si ( asic_si ),
.asic_so ( asic_so )
);
// 100 MHz
initial begin
clk = 1'b0;
forever #(CLK_PERIOD/2) clk = ~clk;
end
// write_bram shadow
// check_next_frame CSN monitor SPI bit
initial begin
rst_n = 1'b0;
bram_addr = 0;
bram_din = 0;
bram_en = 1'b0;
bram_we = 4'h0;
asic_so = 1'b0;
read_response = 8'h28;
#(CLK_PERIOD*8);
rst_n = 1'b1;
// CONTROL ASIC register 0x0002 <= 0x10
write_bram(13'h0020, 32'h00000010);
check_next_frame(32'h02000210);
// CH_ENABLE 16 bit ASIC register 0x0007/0x0008
write_bram(13'h0024, 32'h0000A55A);
check_next_frame(32'h0200075A);
check_next_frame(32'h020008A5);
// ch_index=3CH_CUR_A offset=1
// ASIC = 0x0010 + 3*0x10 + 1 = 0x0041
write_bram(13'h0040, 32'h00000003);
write_bram(13'h0048, 32'h0000007F);
check_next_frame(32'h0200417F);
// DCS key register 0x0009 3
write_bram(13'h002C, 32'h00000001);
check_next_frame(32'h02000955);
check_next_frame(32'h020009B3);
check_next_frame(32'h0200090A);
// Direct Part ID register 0x011D 1
// ASIC 0x28 BRAM SPI_RDATA
write_bram(13'h0008, 32'h0000011D);
write_bram(13'h0010, 32'h00000100);
check_next_frame(32'h03011D00);
read_bram(13'h0014);
if (bram_dout[7:0] !== 8'h28) begin
$display("ERROR read data expected 28 got %h", bram_dout[7:0]);
$stop;
end
#(CLK_PERIOD*20);
$finish;
end
// CSN SPI
always @(negedge asic_csn) begin
bit_cnt <= 0;
shift_in <= 0;
end
// CSN
always @(posedge asic_csn) begin
frame_in <= shift_in;
frame_bits <= bit_cnt;
end
// SPI monitorCSN SCK 沿 MOSI/SI
always @(posedge asic_sck) begin
if (!asic_csn) begin
shift_in <= {shift_in[30:0], asic_si};
bit_cnt <= bit_cnt + 1;
end
end
// ASIC 32 bit bit 24..31
// 沿 SO DUT 沿
always @(negedge asic_sck) begin
if (!asic_csn && (bit_cnt >= 24) && (bit_cnt < 32))
asic_so <= read_response[31-bit_cnt];
else
asic_so <= 1'b0;
end
// BRAM helper DUT busy-error
task write_bram;
input [12:0] addr;
input [31:0] data;
begin
wait(busy == 1'b0);
@(negedge clk);
bram_addr = addr;
bram_din = data;
bram_en = 1'b1;
bram_we = 4'hF;
@(posedge clk);
@(negedge clk);
bram_en = 1'b0;
bram_we = 4'h0;
end
endtask
// BRAM helperbram_en bram_we 0 沿 bram_dout
task read_bram;
input [12:0] addr;
begin
@(negedge clk);
bram_addr = addr;
bram_din = 0;
bram_en = 1'b1;
bram_we = 4'h0;
@(posedge clk);
@(negedge clk);
bram_en = 1'b0;
end
endtask
// SPI 32 bit
task check_next_frame;
input [31:0] expected;
begin
@(posedge asic_csn);
#(CLK_PERIOD/2);
if (frame_bits != 32) begin
$display("ERROR frame expected 32 bits got %0d bits", frame_bits);
$stop;
end
if (frame_in !== expected) begin
$display("ERROR frame expected %h got %h", expected, frame_in);
$stop;
end
end
endtask
endmodule