| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- `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 [7:0] frame_length;
- reg [7:0] payload_read_addr = 8'd0;
- wire [7:0] payload_read_data;
- wire payload_wr_en;
- wire [7:0] payload_wr_addr;
- wire [7:0] payload_wr_data;
- integer i;
- always #1 clk = ~clk;
- // Production boundary uses 11-byte SET_ALL max payload.
- frame_rx #(.MAX_PAYLOAD(11), .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(11), .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;
- repeat (10) @(posedge clk);
- end
- endtask
- task ack_frame;
- begin
- frame_ready = 1'b1;
- @(posedge clk);
- frame_ready = 1'b0;
- end
- endtask
- task get_frame;
- begin
- 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, "valid GET frame missing");
- if (frame_type !== 8'h02 || frame_length !== 8'd0) $fatal(1, "frame metadata mismatch");
- ack_frame();
- end
- endtask
- initial begin
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- get_frame();
- // Bad CRC must not publish (atomicity of frame accept).
- push(8'hA5); push(8'h5A); push(8'h02); push(8'h00); push(8'h6D); push(8'h7A);
- repeat (4) @(posedge clk);
- if (frame_valid) $fatal(1, "bad CRC published");
- // Golden SET_ALL all-zero 11-byte vector.
- // A55A010B00000000000000000000005873
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h0B);
- for (i = 0; i < 11; i = i + 1) push(8'h00);
- push(8'h58); push(8'h73);
- repeat (4) @(posedge clk);
- if (!frame_valid) $fatal(1, "golden SET_ALL not published");
- if (frame_type !== 8'h01 || frame_length !== 8'd11)
- $fatal(1, "SET_ALL metadata mismatch type=%h len=%0d", frame_type, frame_length);
- for (i = 0; i < 11; 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 golden: A55A010B0A141E280019326401020087D5
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h0B);
- 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);
- push(8'h87); push(8'hD5);
- repeat (4) @(posedge clk);
- if (!frame_valid) $fatal(1, "mixed golden SET_ALL not published");
- if (frame_length !== 8'd11) $fatal(1, "mixed golden length");
- ack_frame();
- // A complete oversized frame remains one trusted request. Only the
- // first MAX_PAYLOAD bytes enter RAM; embedded A5 5A cannot resync it.
- // CRC covers TYPE=01, LENGTH=34 and the complete generated payload.
- 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);
- repeat (4) @(posedge clk);
- if (!frame_valid) $fatal(1, "trusted oversized frame metadata missing");
- if (frame_type !== 8'h01 || frame_length !== 8'd34)
- $fatal(1, "oversized metadata mismatch type=%h len=%0d", frame_type, frame_length);
- ack_frame();
- // The same complete oversized request with a bad CRC is silent.
- 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'hED); push(8'h69);
- repeat (4) @(posedge clk);
- if (frame_valid) $fatal(1, "bad-CRC oversized frame published");
- 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
|