| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- `timescale 1ns/1ps
- module prod2u_p4_normal_v3_rx_tb;
- localparam [7:0] HOST_CMD = 8'h05;
- localparam [7:0] HOST_BATCH_CMD = 8'h07;
- localparam [7:0] MOTION_CMD = 8'h11;
- reg clk;
- reg rst_n;
- reg rx_valid;
- wire rx_ready;
- reg [7:0] rx_data;
- wire request_valid;
- reg request_ready;
- wire [7:0] request_type;
- wire [5:0] request_length;
- wire [271:0] request_payload;
- wire [15:0] request_crc16;
- wire fast_jog_valid;
- wire [1:0] fast_jog_axis;
- wire fast_jog_dir;
- wire [15:0] fast_jog_steps;
- wire [15:0] crc_error_count;
- wire [15:0] timeout_count;
- wire [15:0] format_error_count;
- reg [7:0] frame [0:49];
- reg [271:0] expected_payload;
- reg [15:0] count_before;
- reg [7:0] held_request_type;
- reg [5:0] held_request_length;
- reg [271:0] held_request_payload;
- reg [15:0] held_request_crc16;
- reg [15:0] held_crc_error_count;
- reg [15:0] held_timeout_count;
- reg [15:0] held_format_error_count;
- reg check_held_request;
- integer frame_length;
- integer i;
- integer wait_cycles;
- reg timeout_probe_valid;
- reg [7:0] timeout_probe_data;
- wire timeout1_probe_ready;
- wire timeout3_probe_ready;
- wire [15:0] timeout1_probe_count;
- wire [15:0] timeout3_probe_count;
- prod2u_p4_normal_v3_rx #(
- .CLK_HZ(10000),
- .INTER_BYTE_TIMEOUT_MS(2)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .rx_valid(rx_valid),
- .rx_ready(rx_ready),
- .rx_data(rx_data),
- .request_valid(request_valid),
- .request_ready(request_ready),
- .request_type(request_type),
- .request_length(request_length),
- .request_payload(request_payload),
- .request_crc16(request_crc16),
- .fast_jog_valid(fast_jog_valid),
- .fast_jog_axis(fast_jog_axis),
- .fast_jog_dir(fast_jog_dir),
- .fast_jog_steps(fast_jog_steps),
- .crc_error_count(crc_error_count),
- .timeout_count(timeout_count),
- .format_error_count(format_error_count)
- );
- prod2u_p4_normal_v3_rx #(
- .CLK_HZ(1000),
- .INTER_BYTE_TIMEOUT_MS(1)
- ) timeout1_dut (
- .clk(clk),
- .rst_n(rst_n),
- .rx_valid(timeout_probe_valid),
- .rx_ready(timeout1_probe_ready),
- .rx_data(timeout_probe_data),
- .request_valid(),
- .request_ready(1'b1),
- .request_type(),
- .request_length(),
- .request_payload(),
- .request_crc16(),
- .fast_jog_valid(),
- .fast_jog_axis(),
- .fast_jog_dir(),
- .fast_jog_steps(),
- .crc_error_count(),
- .timeout_count(timeout1_probe_count),
- .format_error_count()
- );
- prod2u_p4_normal_v3_rx #(
- .CLK_HZ(1000),
- .INTER_BYTE_TIMEOUT_MS(3)
- ) timeout3_dut (
- .clk(clk),
- .rst_n(rst_n),
- .rx_valid(timeout_probe_valid),
- .rx_ready(timeout3_probe_ready),
- .rx_data(timeout_probe_data),
- .request_valid(),
- .request_ready(1'b1),
- .request_type(),
- .request_length(),
- .request_payload(),
- .request_crc16(),
- .fast_jog_valid(),
- .fast_jog_axis(),
- .fast_jog_dir(),
- .fast_jog_steps(),
- .crc_error_count(),
- .timeout_count(timeout3_probe_count),
- .format_error_count()
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- always @(posedge clk) begin
- if (check_held_request &&
- (!request_valid || request_type !== held_request_type ||
- request_length !== held_request_length ||
- request_payload !== held_request_payload ||
- request_crc16 !== held_request_crc16)) begin
- $display(
- "held request changed valid=%0d type=%02x/%02x len=%0d/%0d crc=%04x/%04x payload_match=%0d",
- request_valid,
- request_type,
- held_request_type,
- request_length,
- held_request_length,
- request_crc16,
- held_request_crc16,
- request_payload === held_request_payload
- );
- $fatal;
- end
- end
- function [15:0] crc16_byte;
- input [15:0] crc_in;
- input [7:0] data;
- reg [15:0] value;
- integer bit_index;
- begin
- value = crc_in ^ {data, 8'h00};
- for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
- if (value[15]) begin
- value = (value << 1) ^ 16'h1021;
- end else begin
- value = value << 1;
- end
- end
- crc16_byte = value;
- end
- endfunction
- task begin_frame;
- input [7:0] frame_type;
- input [7:0] payload_length;
- begin
- for (i = 0; i < 50; i = i + 1) begin
- frame[i] = 8'h00;
- end
- frame[0] = 8'ha5;
- frame[1] = 8'h5a;
- frame[2] = 8'h02;
- frame[3] = 8'h04;
- frame[4] = frame_type;
- frame[13] = payload_length;
- frame_length = 16 + payload_length;
- end
- endtask
- task finish_frame;
- reg [15:0] crc;
- integer last_body_byte;
- begin
- crc = 16'hffff;
- last_body_byte = frame_length - 3;
- for (i = 2; i <= last_body_byte; i = i + 1) begin
- crc = crc16_byte(crc, frame[i]);
- end
- frame[frame_length - 2] = crc[7:0];
- frame[frame_length - 1] = crc[15:8];
- end
- endtask
- task build_snapshot;
- begin
- begin_frame(HOST_BATCH_CMD, 8'd34);
- frame[14] = 8'h2a;
- frame[15] = 8'h00;
- for (i = 0; i < 24; i = i + 1) begin
- frame[16 + i] = i[7:0];
- end
- frame[40] = 8'd30;
- frame[41] = 8'd40;
- frame[42] = 8'd50;
- frame[43] = 8'd60;
- frame[44] = 8'd100;
- frame[45] = 8'd0;
- frame[46] = 8'd100;
- frame[47] = 8'd0;
- finish_frame();
- end
- endtask
- task build_host_command;
- input [7:0] command;
- begin
- begin_frame(HOST_CMD, 8'd4);
- frame[14] = command;
- finish_frame();
- end
- endtask
- task build_motion_command;
- input [7:0] opcode;
- begin
- begin_frame(MOTION_CMD, 8'd8);
- frame[14] = opcode;
- frame[15] = 8'd1;
- frame[16] = 8'd0;
- frame[17] = 8'd3;
- frame[18] = 8'ha0;
- frame[19] = 8'h00;
- finish_frame();
- end
- endtask
- task send_byte;
- input [7:0] value;
- begin
- while (!rx_ready) begin
- @(negedge clk);
- end
- rx_data = value;
- rx_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- rx_valid = 1'b0;
- end
- endtask
- task send_built_frame;
- begin
- for (i = 0; i < frame_length; i = i + 1) begin
- send_byte(frame[i]);
- end
- end
- endtask
- task expect_request;
- input [7:0] expected_type;
- input [5:0] expected_length;
- input [271:0] expected_bytes;
- begin
- wait_cycles = 0;
- while (!request_valid && wait_cycles < 20) begin
- @(negedge clk);
- wait_cycles = wait_cycles + 1;
- end
- if (!request_valid) begin
- $display("request timeout type=%02x", expected_type);
- $fatal;
- end
- if (request_type !== expected_type ||
- request_length !== expected_length ||
- request_payload !== expected_bytes ||
- request_crc16 !== {frame[frame_length - 1], frame[frame_length - 2]}) begin
- $display(
- "request mismatch type=%02x/%02x length=%0d/%0d",
- request_type,
- expected_type,
- request_length,
- expected_length
- );
- $fatal;
- end
- @(posedge clk);
- #1;
- if (request_valid) begin
- $display("request_valid did not clear after ready handshake");
- $fatal;
- end
- end
- endtask
- task expect_format_rejection;
- begin
- count_before = format_error_count;
- send_built_frame();
- repeat (3) @(negedge clk);
- if (request_valid || format_error_count !== count_before + 16'd1) begin
- $display(
- "format rejection mismatch count=%0d/%0d valid=%0d",
- format_error_count,
- count_before + 16'd1,
- request_valid
- );
- $fatal;
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- rx_valid = 1'b0;
- rx_data = 8'h00;
- request_ready = 1'b1;
- check_held_request = 1'b0;
- timeout_probe_valid = 1'b0;
- timeout_probe_data = 8'h00;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- if ($bits(timeout1_dut.timeout_counter) != 1 ||
- $bits(timeout3_dut.timeout_counter) != 2) begin
- $display(
- "timeout counter widths mismatch one=%0d three=%0d",
- $bits(timeout1_dut.timeout_counter),
- $bits(timeout3_dut.timeout_counter)
- );
- $fatal;
- end
- // A parser with timeout N expires on the Nth idle cycle after a byte.
- timeout_probe_data = 8'ha5;
- timeout_probe_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- timeout_probe_valid = 1'b0;
- @(posedge clk);
- #1;
- if (timeout1_probe_count !== 16'd1 || timeout3_probe_count !== 16'd0 ||
- timeout3_dut.timeout_counter !== 2'd1) begin
- $display(
- "timeout first-idle mismatch t1=%0d t3=%0d counter3=%0d",
- timeout1_probe_count,
- timeout3_probe_count,
- timeout3_dut.timeout_counter
- );
- $fatal;
- end
- @(posedge clk);
- #1;
- if (timeout3_probe_count !== 16'd0 || timeout3_dut.timeout_counter !== 2'd2) begin
- $display(
- "timeout second-idle mismatch t3=%0d counter3=%0d",
- timeout3_probe_count,
- timeout3_dut.timeout_counter
- );
- $fatal;
- end
- @(posedge clk);
- #1;
- if (timeout3_probe_count !== 16'd1 || timeout3_dut.timeout_counter !== 2'd0) begin
- $display(
- "timeout third-idle mismatch t3=%0d counter3=%0d",
- timeout3_probe_count,
- timeout3_dut.timeout_counter
- );
- $fatal;
- end
- // The first complete command is accepted immediately after reset.
- build_snapshot();
- expected_payload = 272'd0;
- for (i = 0; i < 34; i = i + 1) begin
- expected_payload[i * 8 +: 8] = frame[14 + i];
- end
- send_built_frame();
- expect_request(HOST_BATCH_CMD, 6'd34, expected_payload);
- build_host_command(8'h29);
- expected_payload = 272'd0;
- expected_payload[7:0] = 8'h29;
- send_built_frame();
- expect_request(HOST_CMD, 6'd4, expected_payload);
- build_host_command(8'h26);
- expected_payload = 272'd0;
- expected_payload[7:0] = 8'h26;
- send_built_frame();
- expect_request(HOST_CMD, 6'd4, expected_payload);
- build_motion_command(8'h35);
- send_built_frame();
- if (!fast_jog_valid || fast_jog_axis !== 2'd1 ||
- fast_jog_dir !== 1'b0 || fast_jog_steps !== 16'd160 ||
- request_valid) begin
- $display("legal JOG did not use the fast exit");
- $fatal;
- end
- @(posedge clk);
- #1;
- if (fast_jog_valid) begin
- $display("fast JOG event was not one cycle");
- $fatal;
- end
- build_host_command(8'h24);
- frame[2] = 8'h03;
- expect_format_rejection();
- build_host_command(8'h24);
- frame[3] = 8'h03;
- expect_format_rejection();
- build_host_command(8'h24);
- frame[4] = 8'h06;
- expect_format_rejection();
- build_host_command(8'h24);
- frame[5] = 8'h01;
- expect_format_rejection();
- build_host_command(8'h24);
- frame[9] = 8'h01;
- expect_format_rejection();
- build_snapshot();
- frame[13] = 8'd33;
- expect_format_rejection();
- build_host_command(8'h24);
- frame[13] = 8'd8;
- expect_format_rejection();
- build_motion_command(8'h37);
- frame[13] = 8'd4;
- expect_format_rejection();
- build_host_command(8'h26);
- frame[frame_length - 1] = frame[frame_length - 1] ^ 8'h01;
- count_before = crc_error_count;
- send_built_frame();
- repeat (3) @(negedge clk);
- if (request_valid || crc_error_count !== count_before + 16'd1) begin
- $display("CRC rejection mismatch count=%0d valid=%0d", crc_error_count, request_valid);
- $fatal;
- end
- // A truncated frame expires without producing a request.
- build_snapshot();
- for (i = 0; i < 6; i = i + 1) begin
- send_byte(frame[i]);
- end
- repeat (19) @(negedge clk);
- if (timeout_count !== 16'd0) begin
- $display("timeout asserted before boundary count=%0d", timeout_count);
- $fatal;
- end
- @(negedge clk);
- if (timeout_count !== 16'd1 || request_valid) begin
- $display("timeout mismatch count=%0d valid=%0d", timeout_count, request_valid);
- $fatal;
- end
- // The one-entry ordinary slot stays stable while RX continues parsing.
- request_ready = 1'b0;
- build_motion_command(8'h37);
- send_built_frame();
- wait_cycles = 0;
- while (!request_valid && wait_cycles < 20) begin
- @(negedge clk);
- wait_cycles = wait_cycles + 1;
- end
- if (!request_valid) begin
- $display("ordinary request was not held under backpressure");
- $fatal;
- end
- held_request_type = request_type;
- held_request_length = request_length;
- held_request_payload = request_payload;
- held_request_crc16 = request_crc16;
- held_crc_error_count = crc_error_count;
- held_timeout_count = timeout_count;
- held_format_error_count = format_error_count;
- check_held_request = 1'b1;
- repeat (8) begin
- @(posedge clk);
- #1;
- if (!rx_ready || crc_error_count !== held_crc_error_count ||
- timeout_count !== held_timeout_count ||
- format_error_count !== held_format_error_count) begin
- $display("held request blocked RX or changed parser counters");
- $fatal;
- end
- end
- build_host_command(8'h29);
- send_built_frame();
- repeat (2) @(posedge clk);
- #1;
- if (!request_valid || request_type !== held_request_type ||
- request_length !== held_request_length ||
- request_payload !== held_request_payload ||
- request_crc16 !== held_request_crc16) begin
- $display("ordinary overflow replaced the held request");
- $fatal;
- end
- check_held_request = 1'b0;
- request_ready = 1'b1;
- @(posedge clk);
- #1;
- if (request_valid || !rx_ready) begin
- $display("request handshake did not retire the held slot");
- $fatal;
- end
- build_host_command(8'h26);
- expected_payload = 272'd0;
- expected_payload[7:0] = 8'h26;
- send_built_frame();
- expect_request(HOST_CMD, 6'd4, expected_payload);
- $display("PASS: prod2u_p4_normal_v3_rx_tb");
- $finish;
- end
- endmodule
|