| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- `timescale 1ns/1ps
- module prod2u_p4_temperature_short_tx_tb;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg start = 1'b0;
- reg force_ff = 1'b0;
- wire rd_req;
- wire [4:0] rd_addr;
- reg rd_valid = 1'b0;
- reg [7:0] rd_data = 8'd0;
- wire tx_valid;
- reg tx_ready = 1'b1;
- wire [7:0] tx_data;
- wire busy;
- wire done;
- reg [7:0] memory [0:26];
- reg [7:0] captured [0:34];
- reg [15:0] expected_crc;
- reg [7:0] stalled_data;
- integer capture_count;
- integer read_count;
- integer index;
- integer wait_cycles;
- prod2u_p4_temperature_short_tx dut (
- .clk(clk),
- .rst_n(rst_n),
- .start(start),
- .force_ff(force_ff),
- .rd_req(rd_req),
- .rd_addr(rd_addr),
- .rd_valid(rd_valid),
- .rd_data(rd_data),
- .tx_valid(tx_valid),
- .tx_ready(tx_ready),
- .tx_data(tx_data),
- .busy(busy),
- .done(done)
- );
- always #5 clk = ~clk;
- always @(posedge clk) begin
- if (!rst_n) begin
- rd_valid <= 1'b0;
- rd_data <= 8'd0;
- read_count <= 0;
- end else begin
- rd_valid <= rd_req;
- if (rd_req) begin
- if (rd_addr > 5'd26 || rd_addr !== read_count[4:0]) begin
- $display(
- "STORE_READ_ORDER_MISMATCH count=%0d addr=%0d",
- read_count,
- rd_addr
- );
- $fatal;
- end
- rd_data <= memory[rd_addr];
- read_count <= read_count + 1;
- end
- end
- end
- function [15:0] crc16_byte;
- input [15:0] crc_in;
- input [7:0] value_in;
- reg [15:0] value;
- integer bit_index;
- begin
- value = crc_in ^ {value_in, 8'h00};
- for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
- if (value[15]) value = (value << 1) ^ 16'h1021;
- else value = value << 1;
- end
- crc16_byte = value;
- end
- endfunction
- task pulse_start;
- input next_force_ff;
- begin
- @(negedge clk);
- force_ff = next_force_ff;
- start = 1'b1;
- @(posedge clk);
- #1;
- start = 1'b0;
- end
- endtask
- task capture_frame;
- input expected_force_ff;
- begin
- capture_count = 0;
- wait_cycles = 0;
- while (tx_valid !== 1'b1 && wait_cycles < 100) begin
- @(posedge clk);
- #1;
- wait_cycles = wait_cycles + 1;
- end
- if (!tx_valid) begin
- $display("TX_VALID_TIMEOUT force_ff=%0d", expected_force_ff);
- $fatal;
- end
- @(negedge clk);
- tx_ready = 1'b0;
- stalled_data = tx_data;
- repeat (4) begin
- @(posedge clk);
- #1;
- if (!tx_valid || tx_data !== stalled_data) begin
- $display(
- "TX_CHANGED_UNDER_BACKPRESSURE valid=%0d data=%02x expected=%02x",
- tx_valid,
- tx_data,
- stalled_data
- );
- $fatal;
- end
- end
- @(negedge clk);
- tx_ready = 1'b1;
- wait_cycles = 0;
- while (capture_count < 35 && wait_cycles < 500) begin
- @(posedge clk);
- if (tx_valid && tx_ready) begin
- captured[capture_count] = tx_data;
- capture_count = capture_count + 1;
- end
- #1;
- wait_cycles = wait_cycles + 1;
- end
- if (capture_count !== 35) begin
- $display("TX_FRAME_TIMEOUT captured=%0d", capture_count);
- $fatal;
- end
- if (captured[0] !== 8'ha5 || captured[1] !== 8'h5a ||
- captured[2] !== 8'h02 || captured[3] !== 8'h04 ||
- captured[4] !== 8'h10 || captured[5] !== 8'h1b) begin
- $display("SHORT_HEADER_MISMATCH");
- $fatal;
- end
- for (index = 0; index < 27; index = index + 1) begin
- if (captured[6 + index] !==
- (expected_force_ff ? 8'hff : index[7:0])) begin
- $display(
- "SHORT_PAYLOAD_MISMATCH index=%0d data=%02x expected=%02x",
- index,
- captured[6 + index],
- (expected_force_ff ? 8'hff : index[7:0])
- );
- $fatal;
- end
- end
- expected_crc = 16'hffff;
- for (index = 2; index < 33; index = index + 1) begin
- expected_crc = crc16_byte(expected_crc, captured[index]);
- end
- if (captured[33] !== expected_crc[7:0] ||
- captured[34] !== expected_crc[15:8]) begin
- $display(
- "SHORT_CRC_MISMATCH got=%02x%02x expected_lo_hi=%02x%02x",
- captured[33],
- captured[34],
- expected_crc[7:0],
- expected_crc[15:8]
- );
- $fatal;
- end
- wait_cycles = 0;
- while (done !== 1'b1 && wait_cycles < 5) begin
- @(posedge clk);
- #1;
- wait_cycles = wait_cycles + 1;
- end
- if (!done || busy) begin
- $display("TX_DONE_MISMATCH done=%0d busy=%0d", done, busy);
- $fatal;
- end
- end
- endtask
- initial begin
- for (index = 0; index < 27; index = index + 1) begin
- memory[index] = index[7:0];
- end
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- pulse_start(1'b0);
- if (!busy) begin
- $display("TX_BUSY_NOT_ASSERTED_AFTER_START");
- $fatal;
- end
- capture_frame(1'b0);
- if (read_count !== 27) begin
- $display("STORE_READ_COUNT_MISMATCH count=%0d", read_count);
- $fatal;
- end
- if ({captured[34], captured[33]} !== 16'hc91f) begin
- $display("GOLDEN_CRC_MISMATCH crc=%02x%02x", captured[33], captured[34]);
- $fatal;
- end
- @(negedge clk);
- rst_n = 1'b0;
- repeat (2) @(negedge clk);
- rst_n = 1'b1;
- pulse_start(1'b1);
- capture_frame(1'b1);
- if (read_count !== 0) begin
- $display("FORCE_FF_READ_STORE count=%0d", read_count);
- $fatal;
- end
- if ({captured[34], captured[33]} !== 16'hc41b) begin
- $display("ALL_FF_CRC_MISMATCH crc=%02x%02x", captured[33], captured[34]);
- $fatal;
- end
- $display("PASS: prod2u_p4_temperature_short_tx_tb");
- $finish;
- end
- endmodule
|