prod2u_p4_normal_v3_top_startup_tb.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. `timescale 1ns/1ps
  2. // Top-level startup: all LS already high -> startup_done without motion; then host 450-step jog.
  3. module prod2u_p4_normal_v3_top_startup_tb;
  4. reg sys_clk = 1'b0;
  5. reg host_uart_rx = 1'b1;
  6. wire host_uart_tx;
  7. wire [7:0] ptc_pwm_out;
  8. wire [3:0] fan_pwm_out;
  9. wire motor_en_n;
  10. wire x_step, x_dir, y_step, y_dir, z_step, z_dir;
  11. reg x_limit_ls = 1'b1;
  12. reg x_limit_hs = 1'b0;
  13. reg y_limit_ls = 1'b1;
  14. reg y_limit_hs = 1'b0;
  15. reg z_limit_ls = 1'b1;
  16. reg z_limit_hs = 1'b0;
  17. tri temp_scl;
  18. tri temp_sda;
  19. localparam integer FAST_TICK_DIV = 10;
  20. localparam integer HOST_STEPS = 450;
  21. reg [31:0] fast_tick_div;
  22. reg fast_tick;
  23. integer startup_x_steps, startup_y_steps, startup_z_steps;
  24. integer host_x_steps, host_y_steps;
  25. reg prev_x_step, prev_y_step, prev_z_step;
  26. reg startup_phase;
  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. initial begin
  40. fast_tick = 1'b0;
  41. fast_tick_div = 0;
  42. force dut.tick_500us = fast_tick;
  43. end
  44. always @(posedge sys_clk) begin
  45. if (fast_tick_div + 1 >= FAST_TICK_DIV) begin
  46. fast_tick_div <= 0;
  47. fast_tick <= 1'b1;
  48. end else begin
  49. fast_tick_div <= fast_tick_div + 1'b1;
  50. fast_tick <= 1'b0;
  51. end
  52. end
  53. always @(posedge sys_clk) begin
  54. prev_x_step <= x_step;
  55. prev_y_step <= y_step;
  56. if (startup_phase) begin
  57. if (!prev_x_step && x_step) startup_x_steps <= startup_x_steps + 1;
  58. if (!prev_y_step && y_step) startup_y_steps <= startup_y_steps + 1;
  59. if (!prev_z_step && z_step) startup_z_steps <= startup_z_steps + 1;
  60. end else begin
  61. if (!prev_x_step && x_step) host_x_steps <= host_x_steps + 1;
  62. if (!prev_y_step && y_step) host_y_steps <= host_y_steps + 1;
  63. end
  64. end
  65. task wait_for_startup_done;
  66. begin
  67. while (!dut.startup_done) @(posedge sys_clk);
  68. end
  69. endtask
  70. task drive_host_motor;
  71. begin
  72. @(negedge sys_clk);
  73. force dut.host_motor_start = 1'b1;
  74. force dut.host_motor_active_mask = 3'b011;
  75. force dut.host_motor_dir_mask = 3'b000;
  76. @(posedge sys_clk);
  77. @(negedge sys_clk);
  78. release dut.host_motor_start;
  79. release dut.host_motor_active_mask;
  80. release dut.host_motor_dir_mask;
  81. end
  82. endtask
  83. initial begin
  84. startup_x_steps = 0;
  85. startup_y_steps = 0;
  86. startup_z_steps = 0;
  87. host_x_steps = 0;
  88. host_y_steps = 0;
  89. startup_phase = 1'b1;
  90. prev_x_step = 1'b0;
  91. prev_y_step = 1'b0;
  92. prev_z_step = 1'b0;
  93. repeat (1100) @(posedge sys_clk);
  94. wait_for_startup_done();
  95. if (startup_x_steps !== 0)
  96. $fatal(1, "x_step during startup with all LS high: %0d", startup_x_steps);
  97. if (startup_y_steps !== 0)
  98. $fatal(1, "y_step during startup with all LS high: %0d", startup_y_steps);
  99. if (startup_z_steps !== 0)
  100. $fatal(1, "z_step during startup with all LS high: %0d", startup_z_steps);
  101. startup_phase = 1'b0;
  102. drive_host_motor();
  103. while (!dut.u_motor_engine.done) @(posedge sys_clk);
  104. if (host_x_steps !== HOST_STEPS)
  105. $fatal(1, "expected host x_step=%0d, got %0d", HOST_STEPS, host_x_steps);
  106. if (host_y_steps !== HOST_STEPS)
  107. $fatal(1, "expected host y_step=%0d, got %0d", HOST_STEPS, host_y_steps);
  108. if (!dut.startup_done)
  109. $fatal(1, "startup_done not sticky after host motor");
  110. $display("prod2u_p4_normal_v3_top_startup_tb PASS");
  111. $finish;
  112. end
  113. initial begin
  114. #100000000 $fatal(1, "top startup test timeout");
  115. end
  116. endmodule