| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- `timescale 1ns/1ps
- module prod2u_p4_normal_v3_top_tb;
- reg clk = 1'b0;
- reg host_rst_n = 1'b0;
- reg host_tx_valid = 1'b0;
- reg [7:0] host_tx_data = 8'd0;
- wire host_tx_ready;
- wire host_to_dut;
- wire dut_to_host;
- wire host_rx_valid;
- wire [7:0] host_rx_data;
- reg x_limit_raw = 1'b0;
- reg y_limit_raw = 1'b0;
- wire [23:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire solenoid_out;
- wire motor_en_n;
- wire x_step;
- wire x_dir;
- wire y_step;
- wire y_dir;
- wire z_step;
- wire z_dir;
- reg [7:0] request_frame [0:49];
- reg [7:0] captured [0:511];
- integer request_length;
- integer captured_count = 0;
- integer read_index = 0;
- integer i;
- integer guard;
- integer failures = 0;
- integer x_rises = 0;
- reg [15:0] crc;
- reg [7:0] rx_frame_type;
- reg [7:0] rx_payload_len;
- reg [143:0] rx_payload;
- reg [191:0] held_ptc;
- reg [31:0] held_fan;
- reg [7:0] held_solenoid;
- reg [15:0] held_hash;
- reg held_disabled;
- reg held_timeout;
- uart_tx_8n1 #(.CLKS_PER_BIT(4)) u_host_tx (
- .clk(clk), .rst_n(host_rst_n), .tx_valid(host_tx_valid),
- .tx_data(host_tx_data), .tx_ready(host_tx_ready),
- .tx_o(host_to_dut), .busy()
- );
- uart_rx_8n1 #(.CLKS_PER_BIT(4)) u_host_rx (
- .clk(clk), .rst_n(host_rst_n), .rx_i(dut_to_host),
- .rx_valid(host_rx_valid), .rx_data(host_rx_data), .rx_ready(1'b1),
- .framing_error(), .break_detected(), .overflow()
- );
- prod2u_p4_normal_v3_top #(
- .CLK_HZ(4000000),
- .HOST_UART_CLKS_PER_BIT(4),
- .POR_CYCLES(4),
- .INTER_BYTE_TIMEOUT_MS(2),
- .PTC_STEP_CLKS(1),
- .FAN_STEP_CLKS(1),
- .SOLENOID_ACTIVE_HIGH(1)
- ) dut (
- .sys_clk(clk), .host_uart_rx(host_to_dut), .host_uart_tx(dut_to_host),
- .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out),
- .solenoid_out(solenoid_out), .motor_en_n(motor_en_n),
- .x_step(x_step), .x_dir(x_dir), .y_step(y_step), .y_dir(y_dir),
- .z_step(z_step), .z_dir(z_dir),
- .x_limit_raw(x_limit_raw), .y_limit_raw(y_limit_raw)
- );
- always #5 clk = ~clk;
- always @(posedge clk) begin
- if (host_rx_valid) begin
- captured[captured_count] <= host_rx_data;
- captured_count <= captured_count + 1;
- end
- end
- always @(posedge x_step) begin
- x_rises = x_rises + 1;
- 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]) value = (value << 1) ^ 16'h1021;
- else value = value << 1;
- end
- crc16_byte = value;
- end
- endfunction
- task build_command;
- input [7:0] frame_type;
- input integer payload_length;
- input [7:0] command;
- begin
- for (i = 0; i < 50; i = i + 1) request_frame[i] = 8'd0;
- request_frame[0] = 8'ha5;
- request_frame[1] = 8'h5a;
- request_frame[2] = 8'h02;
- request_frame[3] = 8'h04;
- request_frame[4] = frame_type;
- request_frame[13] = payload_length[7:0];
- request_frame[14] = command;
- request_length = 16 + payload_length;
- crc = 16'hffff;
- for (i = 2; i < 14 + payload_length; i = i + 1) begin
- crc = crc16_byte(crc, request_frame[i]);
- end
- request_frame[14 + payload_length] = crc[7:0];
- request_frame[15 + payload_length] = crc[15:8];
- end
- endtask
- task send_request;
- begin
- for (i = 0; i < request_length; i = i + 1) begin
- while (!host_tx_ready) @(negedge clk);
- host_tx_data = request_frame[i];
- host_tx_valid = 1'b1;
- @(negedge clk);
- host_tx_valid = 1'b0;
- end
- end
- endtask
- task build_jog;
- input [1:0] axis;
- input direction;
- input [1:0] speed_code;
- input [15:0] steps;
- begin
- build_command(8'h11, 8, 8'h35);
- request_frame[15] = {6'd0, axis};
- request_frame[16] = {7'd0, direction};
- request_frame[17] = {6'd0, speed_code};
- request_frame[18] = steps[7:0];
- request_frame[19] = steps[15:8];
- crc = 16'hffff;
- for (i = 2; i < 22; i = i + 1) begin
- crc = crc16_byte(crc, request_frame[i]);
- end
- request_frame[22] = crc[7:0];
- request_frame[23] = crc[15:8];
- end
- endtask
- task read_frame;
- integer frame_start;
- integer frame_bytes;
- integer scan_guard;
- begin
- frame_start = -1;
- scan_guard = 0;
- while (frame_start < 0 && scan_guard < 200000) begin
- while (captured_count < read_index + 2 && scan_guard < 200000) begin
- @(negedge clk);
- scan_guard = scan_guard + 1;
- end
- if (captured[read_index] == 8'ha5 && captured[read_index + 1] == 8'h5a) begin
- frame_start = read_index;
- end else begin
- read_index = read_index + 1;
- end
- end
- if (frame_start < 0 || captured[frame_start] !== 8'ha5 ||
- captured[frame_start + 1] !== 8'h5a) begin
- $display("TOP_FRAME_TIMEOUT captured=%0d read_index=%0d sys_rst_n=%0d frame_valid=%0d frame_ready=%0d serializer_busy=%0d serializer_done=%0d serializer_tx_valid=%0d serializer_byte=%02x uart_ready=%0d uart_busy=%0d uart_state=%0d uart_tx=%0d",
- captured_count, read_index, dut.sys_rst_n,
- dut.frame_valid, dut.frame_ready,
- dut.u_tx_frames.serializer_busy,
- dut.u_tx_frames.serializer_done,
- dut.u_tx_frames.tx_valid,
- dut.u_tx_frames.tx_data,
- dut.tx_stream_ready, dut.u_uart_tx.busy,
- dut.u_uart_tx.state, dut_to_host);
- $fatal;
- end
- while (captured_count < frame_start + 14) @(negedge clk);
- rx_frame_type = captured[frame_start + 4];
- rx_payload_len = captured[frame_start + 13];
- frame_bytes = 16 + rx_payload_len;
- while (captured_count < frame_start + frame_bytes) @(negedge clk);
- rx_payload = 144'd0;
- for (i = 0; i < rx_payload_len; i = i + 1) begin
- rx_payload[i * 8 +: 8] = captured[frame_start + 14 + i];
- end
- crc = 16'hffff;
- for (i = frame_start + 2; i < frame_start + 14 + rx_payload_len; i = i + 1) begin
- crc = crc16_byte(crc, captured[i]);
- end
- if (captured[frame_start + 14 + rx_payload_len] !== crc[7:0] ||
- captured[frame_start + 15 + rx_payload_len] !== crc[15:8]) $fatal;
- read_index = frame_start + frame_bytes;
- end
- endtask
- task expect_ack_without_trace;
- input [7:0] expected_command;
- input [7:0] expected_status;
- input expected_ok;
- reg first_frame;
- reg ack_seen;
- begin
- first_frame = 1'b1;
- ack_seen = 1'b0;
- guard = 0;
- while (!ack_seen && guard < 32) begin
- read_frame();
- if (first_frame && rx_frame_type !== 8'h06) begin
- $display("ACK_ONLY_FIRST_FRAME_FAIL command=%02x frame=%02x",
- expected_command, rx_frame_type);
- failures = failures + 1;
- end
- first_frame = 1'b0;
- if (rx_frame_type == 8'h0f) begin
- $display("ACK_ONLY_TRACE_FORBIDDEN command=%02x event=%02x seq=%0d",
- expected_command, rx_payload[39:32], rx_payload[31:0]);
- failures = failures + 1;
- end else if (rx_frame_type == 8'h06) begin
- ack_seen = 1'b1;
- if (rx_payload_len !== 8'd8 ||
- rx_payload[7:0] !== {7'd0, expected_ok} ||
- rx_payload[15:8] !== expected_command ||
- rx_payload[23:16] !== expected_status) begin
- $display("ACK_ONLY_ACK_PAYLOAD_FAIL command=%02x payload=%h",
- expected_command, rx_payload[63:0]);
- failures = failures + 1;
- end
- end else begin
- $display("ACK_ONLY_UNEXPECTED_BEFORE_ACK command=%02x frame=%02x",
- expected_command, rx_frame_type);
- failures = failures + 1;
- end
- guard = guard + 1;
- end
- if (!ack_seen) begin
- $display("ACK_ONLY_ACK_TIMEOUT command=%02x", expected_command);
- $fatal;
- end
- end
- endtask
- task expect_motion_status_without_trace;
- reg first_frame;
- reg status_seen;
- begin
- first_frame = 1'b1;
- status_seen = 1'b0;
- guard = 0;
- while (!status_seen && guard < 32) begin
- read_frame();
- if (first_frame && rx_frame_type !== 8'h12) begin
- $display("ACK_ONLY_STATUS_ORDER_FAIL frame=%02x", rx_frame_type);
- failures = failures + 1;
- end
- first_frame = 1'b0;
- if (rx_frame_type == 8'h0f) begin
- $display("ACK_ONLY_STATUS_TRACE_FORBIDDEN event=%02x seq=%0d",
- rx_payload[39:32], rx_payload[31:0]);
- failures = failures + 1;
- end else if (rx_frame_type == 8'h12) begin
- status_seen = 1'b1;
- if (rx_payload_len !== 8'd10) begin
- $display("ACK_ONLY_STATUS_LENGTH_FAIL len=%0d", rx_payload_len);
- failures = failures + 1;
- end
- end else begin
- $display("ACK_ONLY_UNEXPECTED_BEFORE_STATUS frame=%02x", rx_frame_type);
- failures = failures + 1;
- end
- guard = guard + 1;
- end
- if (!status_seen) begin
- $display("ACK_ONLY_STATUS_TIMEOUT");
- $fatal;
- end
- end
- endtask
- task expect_transaction_quiet;
- input [7:0] command;
- begin
- repeat (5000) @(negedge clk);
- while (captured_count >= read_index + 16) begin
- read_frame();
- if (rx_frame_type == 8'h0f) begin
- $display("ACK_ONLY_LATE_TRACE_FORBIDDEN command=%02x event=%02x seq=%0d",
- command, rx_payload[39:32], rx_payload[31:0]);
- end else begin
- $display("ACK_ONLY_LATE_FRAME_FORBIDDEN command=%02x frame=%02x",
- command, rx_frame_type);
- end
- failures = failures + 1;
- end
- if (captured_count !== read_index) begin
- $display("ACK_ONLY_LATE_PARTIAL_FRAME command=%02x bytes=%0d",
- command, captured_count - read_index);
- failures = failures + 1;
- read_index = captured_count;
- end
- end
- endtask
- initial begin
- repeat (3) @(negedge clk);
- host_rst_n = 1'b1;
- wait (dut.sys_rst_n === 1'b1);
- // There is no unsolicited BOOT or Trace frame. The first received
- // frame must be the ACK for the first host request below.
- repeat (200) @(negedge clk);
- if (captured_count != 0) begin
- $display("ACK_ONLY_UNSOLICITED_FRAME bytes=%0d first=%02x",
- captured_count, captured[0]);
- failures = failures + 1;
- end
- // Motor movement is independent of disabled/watchdog and LS inputs.
- x_limit_raw = 1'b1;
- y_limit_raw = 1'b1;
- build_jog(2'd0, 1'b1, 2'd3, 16'd1);
- send_request();
- expect_ack_without_trace(8'h35, 8'h00, 1'b1);
- if (x_rises != 1 || motor_en_n !== 1'b0 || !dut.disabled) begin
- $display("NO_GATES_TOP_FAIL rises=%0d en_n=%0d disabled=%0d",
- x_rises, motor_en_n, dut.disabled);
- failures = failures + 1;
- end
- wait (x_step === 1'b0);
- expect_transaction_quiet(8'h35);
- build_command(8'h05, 4, 8'h24);
- send_request();
- expect_ack_without_trace(8'h24, 8'h00, 1'b1);
- expect_transaction_quiet(8'h24);
- held_ptc = dut.ptc_percent;
- held_fan = dut.fan_percent;
- held_solenoid = dut.solenoid_percent;
- held_hash = dut.state_hash16;
- held_disabled = dut.disabled;
- held_timeout = dut.watchdog_timeout;
- build_command(8'h05, 4, 8'h29);
- send_request();
- expect_ack_without_trace(8'h29, 8'h01, 1'b0);
- expect_transaction_quiet(8'h29);
- if (dut.ptc_percent !== held_ptc || dut.fan_percent !== held_fan ||
- dut.solenoid_percent !== held_solenoid || dut.state_hash16 !== held_hash ||
- dut.disabled !== held_disabled || dut.watchdog_timeout !== held_timeout) $fatal;
- build_command(8'h11, 8, 8'h37);
- send_request();
- expect_ack_without_trace(8'h37, 8'h00, 1'b1);
- expect_motion_status_without_trace();
- expect_transaction_quiet(8'h37);
- held_ptc = dut.ptc_percent;
- held_fan = dut.fan_percent;
- held_solenoid = dut.solenoid_percent;
- held_hash = dut.state_hash16;
- held_disabled = dut.disabled;
- held_timeout = dut.watchdog_timeout;
- build_command(8'h11, 8, 8'h36);
- send_request();
- expect_ack_without_trace(8'h36, 8'h01, 1'b0);
- expect_transaction_quiet(8'h36);
- if (dut.ptc_percent !== held_ptc || dut.fan_percent !== held_fan ||
- dut.solenoid_percent !== held_solenoid || dut.state_hash16 !== held_hash ||
- dut.disabled !== held_disabled || dut.watchdog_timeout !== held_timeout ||
- x_step || y_step || z_step) begin
- $display("ACK_ONLY_MARK_UNSUPPORTED_SIDE_EFFECT");
- failures = failures + 1;
- end
- build_command(8'h05, 4, 8'h26);
- send_request();
- expect_ack_without_trace(8'h26, 8'h00, 1'b1);
- expect_transaction_quiet(8'h26);
- if (motor_en_n || x_step || y_step || z_step || solenoid_out) $fatal;
- if (!dut.disabled) $fatal;
- if (failures != 0) begin
- $display("ACK_ONLY_CONTRACT_RED failures=%0d", failures);
- $fatal;
- end
- $display("PASS: prod2u_p4_normal_v3_top_tb");
- $finish;
- end
- endmodule
|