`timescale 1ns/1ps module frame_rx_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg byte_valid = 1'b0; reg [7:0] byte_data = 8'd0; reg accept_enable = 1'b1; reg frame_ready = 1'b0; wire frame_valid; wire [7:0] frame_type; wire [15:0] frame_length; reg [15:0] payload_read_addr = 16'd0; wire [7:0] payload_read_data; wire payload_wr_en; wire [15:0] payload_wr_addr; wire [7:0] payload_wr_data; integer i; always #1 clk = ~clk; // 6400 SET_ALL is 32 bytes; the request RAM keeps that production size. frame_rx #(.MAX_PAYLOAD(32), .INTER_BYTE_TIMEOUT_CYCLES(16)) dut ( .clk(clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data), .accept_enable(accept_enable), .frame_valid(frame_valid), .frame_ready(frame_ready), .frame_type(frame_type), .frame_length(frame_length), .payload_read_addr(payload_read_addr), .payload_read_data(payload_read_data), .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr), .payload_wr_data(payload_wr_data) ); payload_ram #(.DEPTH(32), .ADDR_WIDTH(8)) payload_store ( .clk(clk), .wr_en(payload_wr_en), .wr_addr(payload_wr_addr), .wr_data(payload_wr_data), .rd_addr(payload_read_addr), .rd_data(payload_read_data) ); task push; input [7:0] value; begin @(negedge clk); byte_data = value; byte_valid = 1'b1; @(negedge clk); byte_valid = 1'b0; // Give the serial CRC time to absorb the byte before the next // one arrives (matches production 921600 byte spacing). repeat (10) @(posedge clk); $display("push %02h t=%0t", value, $time); end endtask task ack_frame; begin frame_ready = 1'b1; @(posedge clk); frame_ready = 1'b0; end endtask task expect_frame; input [7:0] etype; input [15:0] elen; begin repeat (4) @(posedge clk); $display("expect t=%0t valid=%b type=%h len=%0d state=%0d idx=%0d crc=%h", $time, frame_valid, frame_type, frame_length, dut.state, dut.payload_index, dut.crc_value); if (!frame_valid) $fatal(1, "valid frame missing"); if (frame_type !== etype || frame_length !== elen) $fatal(1, "frame metadata mismatch type=%h len=%0d", frame_type, frame_length); end endtask initial begin repeat (3) @(posedge clk); rst_n = 1'b1; // Fixed contract vector: GET_ALL A5 5A 02 00 00 FC A2. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h00); push(8'hFC); push(8'hA2); expect_frame(8'h02, 16'd0); ack_frame(); // Bad CRC (contract vector with corrupted CRC low byte) stays silent. push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h00); push(8'hFC); push(8'hA3); repeat (4) @(posedge clk); if (frame_valid) $fatal(1, "bad CRC published"); // SET_ALL, 32 zero bytes (16-bit length header 20 00, CRC 04 5D). push(8'hA5); push(8'h5A); push(8'h01); push(8'h20); push(8'h00); for (i = 0; i < 32; i = i + 1) push(8'h00); push(8'h04); push(8'h5D); expect_frame(8'h01, 16'd32); for (i = 0; i < 32; i = i + 1) begin payload_read_addr = i[15:0]; @(posedge clk); @(negedge clk); if (payload_read_data !== 8'h00) $fatal(1, "SET_ALL payload[%0d]=%h", i, payload_read_data); end ack_frame(); // Mixed values: 0A 14 1E 28 / 00 19 32 64 / 01 02 00 + zeros. push(8'hA5); push(8'h5A); push(8'h01); push(8'h20); push(8'h00); push(8'h0A); push(8'h14); push(8'h1E); push(8'h28); push(8'h00); push(8'h19); push(8'h32); push(8'h64); push(8'h01); push(8'h02); push(8'h00); for (i = 11; i < 32; i = i + 1) push(8'h00); push(8'h40); push(8'h33); expect_frame(8'h01, 16'd32); ack_frame(); // A complete oversized frame (declared 34 bytes over a 32-byte RAM) // remains one trusted request: consumed in full, CRC checked, with // embedded A5 5A unable to resync. CRC 42 3B from the independent // Python codec. push(8'hA5); push(8'h5A); push(8'h01); push(8'h22); push(8'h00); for (i = 0; i < 34; i = i + 1) begin if (i == 3) push(8'hA5); else if (i == 4) push(8'h5A); else push(i[7:0]); end push(8'h42); push(8'h3B); expect_frame(8'h01, 16'd34); ack_frame(); // The same complete oversized request with a bad CRC is silent and // the next real frame still parses (length extent is trusted). push(8'hA5); push(8'h5A); push(8'h01); push(8'h22); push(8'h00); for (i = 0; i < 34; i = i + 1) begin if (i == 3) push(8'hA5); else if (i == 4) push(8'h5A); else push(i[7:0]); end push(8'h42); push(8'h3C); repeat (4) @(posedge clk); if (frame_valid) $fatal(1, "bad-CRC oversized frame published"); push(8'hA5); push(8'h5A); push(8'h05); push(8'h00); push(8'h00); push(8'h6C); push(8'h27); expect_frame(8'h05, 16'd0); ack_frame(); accept_enable = 1'b0; push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h00); push(8'hFC); push(8'hA2); repeat (4) @(posedge clk); if (frame_valid) $fatal(1, "busy input was accepted"); $display("frame_rx_tb PASS"); $finish; end endmodule