frame_tx_uart_tx_tb.v 2.9 KB

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