| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- `timescale 1ns/1ps
- // frame_tx with LENGTH_BYTES=1: single LENGTH byte on the wire; the CRC
- // still covers TYPE, LENGTH and PAYLOAD. Expected bytes below are the
- // final 8-bit protocol frames from the independent Python codec
- // (tools/crc16_ccitt_table.frame_bytes), hand-embedded.
- module frame_tx_len1_tb;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg start = 1'b0;
- reg [7:0] frame_type = 8'h82;
- reg [7:0] payload_length = 8'd27;
- wire busy;
- wire byte_valid;
- wire [7:0] byte_data;
- reg byte_ready = 1'b1;
- wire [15:0] payload_addr;
- // Two-byte NACK payload ROM stand-in: addr0 -> request TYPE 01,
- // addr1 -> reason 04; every other address reads FF.
- wire [7:0] payload_data = (payload_addr == 16'd0) ? 8'h01 :
- (payload_addr == 16'd1) ? 8'h04 : 8'hFF;
- wire done;
- // Independent TX CRC model: bit-serial like the production
- // crc16_serial (not its code, but the same shift/XOR definition),
- // stepped only while crc_data_valid_o is high and pulsed done one
- // cycle after the eighth bit. crc_busy_i mirrors the model so the
- // DUT's WAIT states pace bytes exactly like the real path.
- wire crc_start_o, crc_data_valid_o;
- wire [7:0] crc_data_o;
- reg crc_busy_r = 1'b0;
- reg crc_done_pulse_r = 1'b0;
- reg [15:0] crc_model_value_r = 16'hFFFF;
- reg [7:0] crc_shift = 8'd0;
- reg [3:0] crc_bit = 4'd0;
- wire crc_done_pulse = crc_done_pulse_r;
- wire [15:0] crc_model_value = crc_model_value_r;
- wire crc_busy_model = crc_busy_r;
- integer count;
- reg [7:0] captured [0:63];
- always #1 clk = ~clk;
- frame_tx #(.PAYLOAD_IDX_BITS(8), .EXTERNAL_CRC(1), .LENGTH_BYTES(1)) dut (
- .clk(clk), .rst_n(rst_n), .start(start), .frame_type(frame_type),
- .payload_length({8'd0, payload_length}), .busy(busy), .byte_valid(byte_valid),
- .byte_data(byte_data), .byte_ready(byte_ready), .payload_addr(payload_addr),
- .payload_data(payload_data), .done(done),
- .crc_start_o(crc_start_o), .crc_data_valid_o(crc_data_valid_o), .crc_data_o(crc_data_o),
- .crc_busy_i(crc_busy_model), .crc_data_done_i(crc_done_pulse), .crc_value_i(crc_model_value)
- );
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- crc_model_value_r <= 16'hFFFF;
- crc_busy_r <= 1'b0;
- crc_done_pulse_r <= 1'b0;
- crc_shift <= 8'd0;
- crc_bit <= 4'd0;
- end else begin
- crc_done_pulse_r <= 1'b0;
- if (crc_start_o) begin
- crc_model_value_r <= 16'hFFFF;
- crc_busy_r <= 1'b0;
- end else if (!crc_busy_r) begin
- if (crc_data_valid_o) begin
- crc_shift <= crc_data_o;
- crc_bit <= 4'd0;
- crc_busy_r <= 1'b1;
- end
- end else begin
- // MSB-first bit select over the latched byte.
- crc_model_value_r <= (crc_model_value_r[15] ^ crc_shift[7 - crc_bit[2:0]]) ?
- ({crc_model_value_r[14:0], 1'b0} ^ 16'h1021) :
- {crc_model_value_r[14:0], 1'b0};
- if (crc_bit == 4'd7) begin
- crc_bit <= 4'd0;
- crc_busy_r <= 1'b0;
- crc_done_pulse_r <= 1'b1;
- end else begin
- crc_bit <= crc_bit + 1'b1;
- end
- end
- end
- end
- // Sample on the rising edge like a real downstream ready consumer:
- // byte_valid/byte_data are launched by the DUT on this edge.
- always @(posedge clk) begin
- if (byte_valid && byte_ready) begin
- captured[count] = byte_data;
- count = count + 1;
- end
- end
- task run_frame;
- input [7:0] ftype;
- input [7:0] flen;
- begin
- count = 0;
- frame_type = ftype;
- payload_length = flen;
- @(negedge clk);
- start = 1'b1;
- @(negedge clk);
- start = 1'b0;
- wait (done);
- @(negedge clk);
- end
- endtask
- initial begin
- count = 0;
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- repeat (2) @(posedge clk);
- // GET_ACK with a 27-byte payload sourced from the shared ROM
- // (addr0 -> 01, addr1 -> 04, rest FF): the independently computed
- // wire image is A5 5A 82 1B 01 04 FF..FF 54 B9.
- run_frame(8'h82, 8'd27);
- if (count !== 33) $fatal(1, "TX byte count mismatch: %0d", count);
- if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
- captured[2] !== 8'h82 || captured[3] !== 8'h1B)
- $fatal(1, "TX header mismatch %h %h %h %h",
- captured[0], captured[1], captured[2], captured[3]);
- if (captured[4] !== 8'h01 || captured[5] !== 8'h04 || captured[30] !== 8'hFF)
- $fatal(1, "TX payload source mismatch %h %h %h",
- captured[4], captured[5], captured[30]);
- if (captured[31] !== 8'h54 || captured[32] !== 8'hB9)
- $fatal(1, "TX CRC mismatch: %h %h", captured[31], captured[32]);
- // SET_ACK, zero length: A5 5A 81 00 A6 35 (golden vector).
- run_frame(8'h81, 8'd0);
- if (count !== 6) $fatal(1, "SET_ACK byte count mismatch: %0d", count);
- if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
- captured[2] !== 8'h81 || captured[3] !== 8'h00)
- $fatal(1, "SET_ACK header mismatch");
- if (captured[4] !== 8'hA6 || captured[5] !== 8'h35)
- $fatal(1, "SET_ACK CRC mismatch: %h %h", captured[4], captured[5]);
- // Queue-full NACK: A5 5A C0 02 01 04 B1 2A (golden vector).
- run_frame(8'hC0, 8'd2);
- if (count !== 8) $fatal(1, "NACK byte count mismatch: %0d", count);
- if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
- captured[2] !== 8'hC0 || captured[3] !== 8'h02)
- $fatal(1, "NACK header mismatch");
- if (captured[4] !== 8'h01 || captured[5] !== 8'h04)
- $fatal(1, "NACK payload mismatch: %h %h", captured[4], captured[5]);
- if (captured[6] !== 8'hB1 || captured[7] !== 8'h2A)
- $fatal(1, "NACK CRC mismatch: %h %h", captured[6], captured[7]);
- $display("frame_tx_len1_tb PASS");
- $finish;
- end
- initial begin
- #200000;
- $fatal(1, "TX timeout state=%0d busy=%b valid=%b count=%0d",
- dut.state, busy, byte_valid, count);
- end
- endmodule
|