`timescale 1ns/1ps module frame_tx_uart_tx_tb; reg sys_clk = 1'b0; reg host_uart_rx = 1'b1; reg x_limit_ls = 1'b0; reg x_limit_hs = 1'b0; reg y_limit_ls = 1'b0; reg y_limit_hs = 1'b0; reg z_limit_ls = 1'b0; reg z_limit_hs = 1'b0; wire z_step; wire z_dir; tri1 temp_scl; tri1 temp_sda; wire host_uart_tx; wire [7:0] ptc_pwm_out; wire [3:0] fan_pwm_out; wire motor_en_n; wire x_step; wire x_dir; wire y_step; wire y_dir; reg [7:0] captured [0:32]; reg [7:0] value; integer byte_index; integer bit_index; always #1 sys_clk = ~sys_clk; prod2u_p4_normal_v3_top dut ( .sys_clk(sys_clk), .host_uart_rx(host_uart_rx), .host_uart_tx(host_uart_tx), .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_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_ls(x_limit_ls), .x_limit_hs(x_limit_hs), .y_limit_ls(y_limit_ls), .y_limit_hs(y_limit_hs), .z_limit_ls(z_limit_ls), .z_limit_hs(z_limit_hs), .temp_scl(temp_scl), .temp_sda(temp_sda) ); task decode_byte; output [7:0] decoded; begin @(negedge host_uart_tx); repeat (217 + 108) @(posedge sys_clk); for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin decoded[bit_index] = host_uart_tx; repeat (217) @(posedge sys_clk); end if (host_uart_tx !== 1'b1) $fatal(1, "UART stop bit is low"); end endtask initial begin #3000; force dut.temperature_rd_data = 8'hFF; force dut.u_frame_tx.start = 1'b1; repeat (2) @(posedge sys_clk); release dut.u_frame_tx.start; for (byte_index = 0; byte_index < 33; byte_index = byte_index + 1) begin decode_byte(value); captured[byte_index] = value; end if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A || captured[2] !== 8'h82 || captured[3] !== 8'h1B) begin $fatal(1, "UART frame header mismatch: %h %h %h %h", captured[0], captured[1], captured[2], captured[3]); end if (captured[4] !== 8'hFF || captured[30] !== 8'hFF) begin $fatal(1, "UART payload mismatch"); end if (captured[31] !== 8'hA7 || captured[32] !== 8'h45) begin $fatal(1, "UART CRC mismatch: %h %h", captured[31], captured[32]); end release dut.temperature_rd_data; repeat (217 * 12) begin @(posedge sys_clk); if (host_uart_tx !== 1'b1) $fatal(1, "unexpected 34th UART byte"); end $display("frame_tx_uart_tx_tb PASS"); $finish; end initial begin #2500000; $fatal(1, "UART frame timeout"); end endmodule