| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- `timescale 1ns/1ps
- // frame_rx with LENGTH_BYTES=1: single LENGTH byte, frame_length is
- // {8'h00, length_byte}, CRC covers TYPE then the single LENGTH byte.
- // Vectors below are the final 8-bit protocol frames from the independent
- // Python codec (tools/crc16_ccitt_table.frame_bytes), hand-embedded.
- module frame_rx_len1_tb;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg byte_valid = 1'b0;
- reg [7:0] byte_data = 8'd0;
- reg byte_error = 1'b0;
- 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 = 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;
- integer writes;
- // External-CRC handshake observation (RX accumulator is separate).
- wire crc_start_o, crc_data_valid_o;
- wire [7:0] crc_data_o;
- // crc_done_pulse follows one cycle behind the absorbed byte, like the
- // serial CRC's pipelined data_done, so the DUT observes a stable crc
- // accumulator for the state it is comparing against.
- reg crc_pending = 1'b0;
- reg crc_done_pulse_r = 1'b0;
- reg [15:0] crc_model_value_r = 16'hFFFF;
- wire crc_done_pulse = crc_done_pulse_r;
- wire [15:0] crc_model_value = crc_model_value_r;
- wire crc_absorb_now = crc_data_valid_o && !crc_start_o;
- always #1 clk = ~clk;
- frame_rx #(
- .MAX_PAYLOAD(32), .INTER_BYTE_TIMEOUT_CYCLES(32),
- .EXTERNAL_CRC(1), .UART_ERRORS_ENABLED(1), .LENGTH_BYTES(1)
- ) dut (
- .clk(clk), .rst_n(rst_n), .byte_valid(byte_valid), .byte_data(byte_data),
- .byte_error(byte_error),
- .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_8}), .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_start_o), .crc_data_valid_o(crc_data_valid_o), .crc_data_o(crc_data_o),
- .crc_busy_i(1'b0), .crc_data_done_i(crc_done_pulse), .crc_value_i(crc_model_value)
- );
- // Independent CRC-16/CCITT-FALSE model inside the testbench (nibble
- // formulation, not the DUT's bit-serial form): only fed from the
- // observed crc_data_valid_o/crc_data_o handshake.
- reg [15:0] crc_table [0:255];
- integer t_init, t_bit;
- initial begin
- for (t_init = 0; t_init < 256; t_init = t_init + 1) begin
- crc_table[t_init] = {t_init[7:0], 8'd0};
- for (t_bit = 0; t_bit < 8; t_bit = t_bit + 1)
- crc_table[t_init] = crc_table[t_init][15] ?
- (crc_table[t_init] << 1) ^ 16'h1021 : crc_table[t_init] << 1;
- end
- end
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- crc_model_value_r <= 16'hFFFF;
- crc_done_pulse_r <= 1'b0;
- crc_pending <= 1'b0;
- end else begin
- crc_done_pulse_r <= crc_pending && !crc_absorb_now;
- crc_pending <= 1'b0;
- if (crc_start_o)
- crc_model_value_r <= 16'hFFFF;
- else if (crc_data_valid_o) begin
- // Table-driven MSB-first CRC-16/CCITT-FALSE step over one
- // byte: index = old_crc[15:8] ^ byte, then shift out the
- // consumed top byte and fold the table word in.
- crc_model_value_r <= {crc_model_value_r[7:0], 8'd0} ^
- crc_table[(crc_model_value_r[15:8] ^ crc_data_o)];
- crc_pending <= 1'b1;
- end
- end
- end
- payload_ram #(.DEPTH(32), .ADDR_WIDTH(8)) payload_store (
- .clk(clk), .wr_en(payload_wr_en), .wr_addr(payload_wr_addr[7:0]),
- .wr_data(payload_wr_data),
- .rd_addr(payload_read_addr_8), .rd_data(payload_read_data)
- );
- // Sample on the falling edge: payload_wr_en is a registered pulse
- // that is stable through the whole low half-cycle.
- always @(negedge clk) if (payload_wr_en) begin
- writes = writes + 1;
- if (payload_wr_addr >= 32) $fatal(1, "request RAM overflow at %0d", payload_wr_addr);
- end
- task push;
- input [7:0] value;
- begin
- @(negedge clk);
- byte_data = value;
- byte_valid = 1'b1;
- byte_error = 1'b0;
- @(negedge clk);
- byte_valid = 1'b0;
- repeat (10) @(posedge clk);
- end
- endtask
- task push_err;
- begin
- @(negedge clk);
- byte_data = 8'h00;
- byte_valid = 1'b0;
- byte_error = 1'b1;
- @(negedge clk);
- byte_error = 1'b0;
- repeat (10) @(posedge clk);
- end
- endtask
- task ack_frame;
- begin
- frame_ready = 1'b1;
- @(posedge clk);
- @(negedge clk);
- frame_ready = 1'b0;
- end
- endtask
- // expect_frame checks metadata, samples the running write counter
- // for the payload assertions, then accepts the frame. The write
- // counter is reset at the start of the *next* case instead of here so
- // an assertion failure cannot hide which run produced the count.
- integer writes_at_accept;
- task expect_frame;
- input [7:0] etype;
- input [15:0] elen;
- begin
- repeat (4) @(posedge clk);
- 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);
- writes_at_accept = writes;
- end
- endtask
- task expect_silent;
- begin
- repeat (4) @(posedge clk);
- if (frame_valid) $fatal(1, "corrupt frame published");
- end
- endtask
- initial begin
- writes = 0;
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- // GET_ALL, 8-bit length: A5 5A 02 00 6D 7B (golden vector).
- writes = 0;
- 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 (corrupted CRC high byte) stays silent.
- push(8'hA5); push(8'h5A); push(8'h02); push(8'h00);
- push(8'h6D); push(8'h7C);
- expect_silent();
- // A later legal frame still parses (recovery).
- 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();
- // SET_ALL 32 zero bytes: A5 5A 01 20 + 32x00 + 0D 8C.
- writes = 0;
- 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);
- if (writes_at_accept !== 32) $fatal(1, "payload writes=%0d", writes_at_accept);
- for (i = 0; i < 32; i = i + 1) begin
- payload_read_addr_8 = 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 11-byte payload (0A 14 1E 28 00 19 32 64 01 02 00 + zeros,
- // CRC 49 E2).
- writes = 0;
- 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);
- if (writes_at_accept !== 32) $fatal(1, "payload writes=%0d", writes_at_accept);
- ack_frame();
- // A declared length beyond the 32-byte RAM (34) is trusted and
- // consumed in full: CRC checked over all 34 payload bytes, embedded
- // A5 5A (bytes 3,4) unable to resync; CRC EC 69.
- writes = 0;
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
- push(8'h00); push(8'h01); push(8'h02);
- push(8'hA5); push(8'h5A);
- for (i = 5; i < 34; i = i + 1) push(i[7:0]);
- push(8'hEC); push(8'h69);
- expect_frame(8'h01, 16'd34);
- if (writes_at_accept !== 32) $fatal(1, "payload writes=%0d", writes_at_accept);
- ack_frame();
- // Same oversized request with corrupted CRC is silent, then the
- // next real frame still parses.
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h22);
- push(8'h00); push(8'h01); push(8'h02);
- push(8'hA5); push(8'h5A);
- for (i = 5; i < 34; i = i + 1) push(i[7:0]);
- push(8'hEC); push(8'h96);
- expect_silent();
- 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();
- // A UART error byte anywhere in the frame poisons it.
- push(8'hA5); push(8'h5A); push(8'h01); push(8'h20);
- push(8'h00); push(8'h00); push_err();
- for (i = 3; i < 32; i = i + 1) push(8'h00);
- push(8'h0D); push(8'h8C);
- expect_silent();
- 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();
- // A half frame is abandoned only after the inter-byte timeout;
- // the parser then returns to hunting and parses the next frame.
- push(8'hA5); push(8'h5A); push(8'h01);
- repeat (64) @(posedge clk);
- if (frame_valid) $fatal(1, "truncated frame published");
- 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();
- // While accept is withdrawn the parser drops everything.
- 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");
- accept_enable = 1'b1;
- 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();
- $display("frame_rx_len1_tb PASS");
- $finish;
- end
- initial begin
- #5000000;
- $fatal(1, "frame_rx_len1 deadline");
- end
- endmodule
|