frame_tx_uart_tx_tb.v 2.7 KB

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