prod2u_p4_normal_v3_top_startup_tb.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. `timescale 1ns/1ps
  2. module prod2u_p4_normal_v3_top_startup_tb;
  3. reg sys_clk = 1'b0;
  4. reg host_uart_rx = 1'b1;
  5. wire host_uart_tx;
  6. wire [7:0] ptc_pwm_out;
  7. wire [3:0] fan_pwm_out;
  8. wire motor_en_n;
  9. wire x_step;
  10. wire x_dir;
  11. wire y_step;
  12. wire y_dir;
  13. reg x_limit_raw = 1'b0;
  14. reg y_limit_raw = 1'b0;
  15. tri temp_scl;
  16. tri temp_sda;
  17. localparam integer FAST_TICK_DIV = 10;
  18. reg [31:0] fast_tick_div;
  19. reg fast_tick;
  20. integer startup_x_steps;
  21. integer startup_y_steps;
  22. integer host_x_steps;
  23. integer host_y_steps;
  24. reg prev_x_step;
  25. reg prev_y_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), .x_step(x_step), .x_dir(x_dir), .y_step(y_step), .y_dir(y_dir),
  32. .x_limit_raw(x_limit_raw), .y_limit_raw(y_limit_raw),
  33. .temp_scl(temp_scl), .temp_sda(temp_sda)
  34. );
  35. // Accelerate the 500us tick so the motor engine completes in simulation time.
  36. initial begin
  37. fast_tick = 1'b0;
  38. fast_tick_div = 0;
  39. force dut.tick_500us = fast_tick;
  40. end
  41. always @(posedge sys_clk) begin
  42. if (fast_tick_div + 1 >= FAST_TICK_DIV) begin
  43. fast_tick_div <= 0;
  44. fast_tick <= 1'b1;
  45. end else begin
  46. fast_tick_div <= fast_tick_div + 1'b1;
  47. fast_tick <= 1'b0;
  48. end
  49. end
  50. always @(posedge sys_clk) begin
  51. prev_x_step <= x_step;
  52. prev_y_step <= y_step;
  53. if (startup_phase) begin
  54. if (!prev_x_step && x_step) startup_x_steps <= startup_x_steps + 1;
  55. if (!prev_y_step && y_step) startup_y_steps <= startup_y_steps + 1;
  56. end else begin
  57. if (!prev_x_step && x_step) host_x_steps <= host_x_steps + 1;
  58. if (!prev_y_step && y_step) host_y_steps <= host_y_steps + 1;
  59. end
  60. end
  61. task wait_for_startup_done;
  62. begin
  63. while (!dut.startup_done) @(posedge sys_clk);
  64. end
  65. endtask
  66. task drive_host_motor;
  67. begin
  68. @(negedge sys_clk);
  69. force dut.host_motor_start = 1'b1;
  70. force dut.host_motor_active_mask = 2'b11;
  71. force dut.host_motor_dir_mask = 2'b11;
  72. @(posedge sys_clk);
  73. @(negedge sys_clk);
  74. release dut.host_motor_start;
  75. release dut.host_motor_active_mask;
  76. release dut.host_motor_dir_mask;
  77. end
  78. endtask
  79. initial begin
  80. startup_x_steps = 0;
  81. startup_y_steps = 0;
  82. host_x_steps = 0;
  83. host_y_steps = 0;
  84. startup_phase = 1'b1;
  85. prev_x_step = 1'b0;
  86. prev_y_step = 1'b0;
  87. // Scenario: both raw limits are already high at power-on.
  88. x_limit_raw = 1'b1;
  89. y_limit_raw = 1'b1;
  90. // Wait for power-on reset and synchronizer valid.
  91. repeat (1100) @(posedge sys_clk);
  92. wait_for_startup_done();
  93. if (startup_x_steps !== 0)
  94. $fatal(1, "x_step rising edges during startup with both limits high: %0d", startup_x_steps);
  95. if (startup_y_steps !== 0)
  96. $fatal(1, "y_step rising edges during startup with both limits high: %0d", startup_y_steps);
  97. // Hand over to host-motor phase and count steps independently.
  98. startup_phase = 1'b0;
  99. drive_host_motor();
  100. // Wait for the host motor to finish 450 steps on each axis.
  101. while (!dut.u_motor_engine.done) @(posedge sys_clk);
  102. if (host_x_steps !== 450)
  103. $fatal(1, "expected host x_step=450, got %0d", host_x_steps);
  104. if (host_y_steps !== 450)
  105. $fatal(1, "expected host y_step=450, got %0d", host_y_steps);
  106. // Startup pulses must not satisfy the 450-step host motor assertion.
  107. if (startup_x_steps == 450)
  108. $fatal(1, "startup x_step count must not equal host 450-step assertion");
  109. if (startup_y_steps == 450)
  110. $fatal(1, "startup y_step count must not equal host 450-step assertion");
  111. $display("prod2u_p4_normal_v3_top_startup_tb PASS");
  112. $finish;
  113. end
  114. initial begin
  115. #100000000 $fatal(1, "top startup test timeout");
  116. end
  117. endmodule