| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- `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 [7:0] payload_read_addr = 8'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.
- // Final 8-bit protocol: single LENGTH byte on the wire.
- frame_rx #(.MAX_PAYLOAD(32), .INTER_BYTE_TIMEOUT_CYCLES(16), .LENGTH_BYTES(1)) dut (
- .clk(clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data),
- .byte_error(1'b0),
- .accept_enable(accept_enable), .frame_valid(frame_valid), .frame_ready(frame_ready),
- .frame_type(frame_type), .frame_length(frame_length),
- .payload_read_addr({8'd0, 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),
- .crc_start_o(), .crc_data_valid_o(), .crc_data_o(),
- .crc_busy_i(1'b0), .crc_data_done_i(1'b0), .crc_value_i(16'd0)
- );
- reg [7:0] ram [0:255];
- always @(posedge clk) begin
- if (payload_wr_en) ram[payload_wr_addr[7:0]] <= payload_wr_data;
- end
- assign payload_read_data = ram[payload_read_addr[7:0]];
- 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 6D 7B.
- push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
- push(8'h6D); push(8'h7B);
- 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'h6C); push(8'h7B);
- repeat (4) @(posedge clk);
- if (frame_valid) $fatal(1, "bad CRC published");
- // SET_ALL, 32 zero bytes (8-bit length header 20, CRC 0D 8C).
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
- for (i = 0; i < 32; i = i + 1) push(8'h00);
- push(8'h0D); push(8'h8C);
- expect_frame(8'h01, 16'd32);
- for (i = 0; i < 32; i = i + 1) begin
- payload_read_addr = i[7: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 (CRC 49 E2).
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
- 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'h49); push(8'hE2);
- 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 value 0x69EC from the
- // independent Python codec; wire CRC is u16le, so EC then 69.
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
- 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'hEC); push(8'h69);
- 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);
- 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'hEC); push(8'h96);
- 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'hFA); push(8'hE2);
- 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'h6D); push(8'h7B);
- repeat (4) @(posedge clk);
- if (frame_valid) $fatal(1, "busy input was accepted");
- $display("frame_rx_tb PASS");
- $finish;
- end
- endmodule
|