| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- `timescale 1ns/1ps
- module prod2u_p4_temperature_top_integration_tb;
- reg clk = 1'b0;
- reg host_rst_n = 1'b0;
- reg host_to_dut = 1'b1;
- wire dut_to_host;
- wire host_rx_valid;
- wire [7:0] host_rx_data;
- reg force_sda_low = 1'b0;
- tri1 temp_scl;
- tri1 temp_sda;
- reg [7:0] request_frame [0:23];
- reg [7:0] captured [0:127];
- integer request_length;
- integer captured_count = 0;
- integer read_index = 0;
- integer index;
- integer guard;
- integer accepted_requests = 0;
- integer host_bytes_launched = 0;
- integer dut_bytes_received = 0;
- reg [7:0] last_accepted_command = 8'd0;
- reg [15:0] crc;
- reg early_i2c_seen = 1'b0;
- assign temp_sda = force_sda_low ? 1'b0 : 1'bz;
- 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(10000),
- .HOST_UART_CLKS_PER_BIT(4),
- .POR_CYCLES(4),
- .INTER_BYTE_TIMEOUT_MS(8),
- .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(),
- .fan_pwm_out(),
- .solenoid_out(),
- .motor_en_n(),
- .x_step(),
- .x_dir(),
- .y_step(),
- .y_dir(),
- .z_step(),
- .z_dir(),
- .x_limit_raw(1'b0),
- .y_limit_raw(1'b0),
- .temp_scl(temp_scl),
- .temp_sda(temp_sda)
- );
- 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
- if (!dut.temperature_powerup_ready && dut.temperature_i2c_request_valid)
- early_i2c_seen <= 1'b1;
- if (dut.request_valid && dut.request_ready) begin
- accepted_requests <= accepted_requests + 1;
- last_accepted_command <= dut.request_payload[7:0];
- end
- if (dut.uart_rx_valid && dut.uart_rx_ready)
- dut_bytes_received <= dut_bytes_received + 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_host_command;
- input [7:0] command;
- begin
- for (index = 0; index < 24; index = index + 1)
- request_frame[index] = 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] = 8'h05;
- request_frame[13] = 8'd4;
- request_frame[14] = command;
- request_length = 20;
- crc = 16'hffff;
- for (index = 2; index < 18; index = index + 1)
- crc = crc16_byte(crc, request_frame[index]);
- request_frame[18] = crc[7:0];
- request_frame[19] = crc[15:8];
- end
- endtask
- task send_request;
- integer byte_index;
- integer bit_index;
- begin
- for (byte_index = 0; byte_index < request_length; byte_index = byte_index + 1) begin
- @(negedge clk);
- host_to_dut = 1'b0;
- repeat (4) @(posedge clk);
- for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
- @(negedge clk);
- host_to_dut = request_frame[byte_index][bit_index];
- repeat (4) @(posedge clk);
- end
- @(negedge clk);
- host_to_dut = 1'b1;
- repeat (4) @(posedge clk);
- host_bytes_launched = host_bytes_launched + 1;
- end
- end
- endtask
- task wait_bytes;
- input integer count;
- begin
- guard = 0;
- while (captured_count < read_index + count && guard < 200000) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (captured_count < read_index + count) begin
- $display(
- "TOP_TEMPERATURE_UART_TIMEOUT have=%0d need=%0d host_bytes=%0d dut_bytes=%0d accepted=%0d last_cmd=%02x crc_err=%0d timeout_err=%0d format_err=%0d req_valid=%0d req_ready=%0d core_state=%0d inflight=%0d read_state=%0d read_done=%0d bus_error=%0d i2c_state=%0d i2c_valid=%0d i2c_error=%0d frame_valid=%0d tx_owner=%0d",
- captured_count,
- read_index + count,
- host_bytes_launched,
- dut_bytes_received,
- accepted_requests,
- last_accepted_command,
- dut.parser_crc_error_count,
- dut.parser_timeout_count,
- dut.parser_format_error_count,
- dut.request_valid,
- dut.request_ready,
- dut.u_core.state,
- dut.u_core.command_inflight,
- dut.u_temperature_read.state,
- dut.temperature_read_done,
- dut.temperature_bus_error,
- dut.u_temperature_i2c.state,
- dut.temperature_i2c_response_valid,
- dut.temperature_i2c_error,
- dut.frame_valid,
- dut.u_tx_frames.owner
- );
- $fatal;
- end
- end
- endtask
- task check_crc;
- input integer frame_length;
- begin
- crc = 16'hffff;
- for (index = read_index + 2; index < read_index + frame_length - 2; index = index + 1)
- crc = crc16_byte(crc, captured[index]);
- if (captured[read_index + frame_length - 2] !== crc[7:0] ||
- captured[read_index + frame_length - 1] !== crc[15:8]) begin
- $display("TOP_TEMPERATURE_CRC_MISMATCH");
- $fatal;
- end
- end
- endtask
- task expect_all_ff_temperature;
- begin
- wait_bytes(35);
- if (captured[read_index] !== 8'ha5 || captured[read_index + 1] !== 8'h5a ||
- captured[read_index + 2] !== 8'h02 || captured[read_index + 3] !== 8'h04 ||
- captured[read_index + 4] !== 8'h10 || captured[read_index + 5] !== 8'h1b)
- $fatal;
- for (index = 0; index < 27; index = index + 1)
- if (captured[read_index + 6 + index] !== 8'hff) $fatal;
- check_crc(35);
- read_index = read_index + 35;
- end
- endtask
- task expect_ack;
- input [7:0] command;
- input [7:0] status;
- input expected_ok;
- begin
- wait_bytes(24);
- if (captured[read_index + 4] !== 8'h06 ||
- captured[read_index + 13] !== 8'd8 ||
- captured[read_index + 14] !== {7'd0, expected_ok} ||
- captured[read_index + 15] !== command ||
- captured[read_index + 16] !== status) begin
- $display("TOP_TEMPERATURE_ACK_MISMATCH");
- $fatal;
- end
- check_crc(24);
- read_index = read_index + 24;
- end
- endtask
- initial begin
- repeat (4) @(negedge clk);
- host_rst_n = 1'b1;
- wait (dut.sys_rst_n === 1'b1);
- build_host_command(8'h28);
- send_request();
- expect_all_ff_temperature();
- if (early_i2c_seen) begin
- $display("EARLY_GET_ACCESSED_I2C");
- $fatal;
- end
- wait (dut.temperature_powerup_ready === 1'b1);
- force_sda_low = 1'b1;
- build_host_command(8'h28);
- send_request();
- expect_ack(8'h28, 8'h0a, 1'b0);
- force_sda_low = 1'b0;
- repeat (8) @(negedge clk);
- if (dut.temperature_scl_drive_low || dut.temperature_sda_drive_low) begin
- $display("TOP_TEMPERATURE_BUS_NOT_RELEASED_AFTER_ERROR");
- $fatal;
- end
- build_host_command(8'h24);
- send_request();
- expect_ack(8'h24, 8'h00, 1'b1);
- $display("PASS: prod2u_p4_temperature_top_integration_tb");
- $finish;
- end
- endmodule
|