frame_tx_uart_tx_tb.v 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. `timescale 1ns/1ps
  2. module frame_tx_uart_tx_tb;
  3. reg sys_clk = 1'b0;
  4. reg host_uart_rx = 1'b1;
  5. reg x_limit_raw = 1'b0;
  6. reg y_limit_raw = 1'b0;
  7. tri1 temp_scl;
  8. tri1 temp_sda;
  9. wire host_uart_tx;
  10. wire [7:0] ptc_pwm_out;
  11. wire [3:0] fan_pwm_out;
  12. wire motor_en_n;
  13. wire x_step;
  14. wire x_dir;
  15. wire y_step;
  16. wire y_dir;
  17. reg [7:0] captured [0:32];
  18. reg [7:0] value;
  19. integer byte_index;
  20. integer bit_index;
  21. always #1 sys_clk = ~sys_clk;
  22. prod2u_p4_normal_v3_top dut (
  23. .sys_clk(sys_clk), .host_uart_rx(host_uart_rx), .host_uart_tx(host_uart_tx),
  24. .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out),
  25. .motor_en_n(motor_en_n),
  26. .x_step(x_step), .x_dir(x_dir), .y_step(y_step), .y_dir(y_dir),
  27. .x_limit_raw(x_limit_raw),
  28. .y_limit_raw(y_limit_raw), .temp_scl(temp_scl), .temp_sda(temp_sda)
  29. );
  30. task decode_byte;
  31. output [7:0] decoded;
  32. begin
  33. @(negedge host_uart_tx);
  34. repeat (217 + 108) @(posedge sys_clk);
  35. for (bit_index = 0; bit_index < 8; bit_index = bit_index + 1) begin
  36. decoded[bit_index] = host_uart_tx;
  37. repeat (217) @(posedge sys_clk);
  38. end
  39. if (host_uart_tx !== 1'b1) $fatal(1, "UART stop bit is low");
  40. end
  41. endtask
  42. initial begin
  43. #3000;
  44. force dut.temperature_rd_data = 8'hFF;
  45. force dut.u_frame_tx.start = 1'b1;
  46. repeat (2) @(posedge sys_clk);
  47. release dut.u_frame_tx.start;
  48. for (byte_index = 0; byte_index < 33; byte_index = byte_index + 1) begin
  49. decode_byte(value);
  50. captured[byte_index] = value;
  51. end
  52. if (captured[0] !== 8'hA5 || captured[1] !== 8'h5A ||
  53. captured[2] !== 8'h82 || captured[3] !== 8'h1B) begin
  54. $fatal(1, "UART frame header mismatch: %h %h %h %h",
  55. captured[0], captured[1], captured[2], captured[3]);
  56. end
  57. if (captured[4] !== 8'hFF || captured[30] !== 8'hFF) begin
  58. $fatal(1, "UART payload mismatch");
  59. end
  60. if (captured[31] !== 8'hA7 || captured[32] !== 8'h45) begin
  61. $fatal(1, "UART CRC mismatch: %h %h", captured[31], captured[32]);
  62. end
  63. release dut.temperature_rd_data;
  64. repeat (217 * 12) begin
  65. @(posedge sys_clk);
  66. if (host_uart_tx !== 1'b1) $fatal(1, "unexpected 34th UART byte");
  67. end
  68. $display("frame_tx_uart_tx_tb PASS");
  69. $finish;
  70. end
  71. initial begin
  72. #2500000;
  73. $fatal(1, "UART frame timeout");
  74. end
  75. endmodule