prod2u_p4_normal_v3_top_startup_tb.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. `timescale 1ns/1ps
  2. `include "src/generated/prod2u_door_temp_params.vh"
  3. // Top startup: axes begin on close limit, measure-open until open limit after
  4. // >=2 gated STEP rises, then sticky startup_complete.
  5. module prod2u_p4_normal_v3_top_startup_tb;
  6. reg sys_clk = 1'b0;
  7. reg host_uart_rx = 1'b1;
  8. wire host_uart_tx;
  9. wire [7:0] ptc_pwm_out;
  10. wire [3:0] fan_pwm_out;
  11. wire motor_en_n;
  12. wire x_step, x_dir, y_step, y_dir, z_step, z_dir;
  13. reg x_limit_ls = 1'b1;
  14. reg x_limit_hs = 1'b0;
  15. reg y_limit_ls = 1'b1;
  16. reg y_limit_hs = 1'b0;
  17. reg z_limit_ls = 1'b1;
  18. reg z_limit_hs = 1'b0;
  19. wire temp_scl, temp_sda;
  20. integer x_rises;
  21. integer y_rises;
  22. integer z_rises;
  23. always #20 sys_clk = ~sys_clk;
  24. prod2u_p4_normal_v3_top dut (
  25. .sys_clk(sys_clk), .host_uart_rx(host_uart_rx), .host_uart_tx(host_uart_tx),
  26. .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out), .motor_en_n(motor_en_n),
  27. .x_step(x_step), .x_dir(x_dir), .y_step(y_step), .y_dir(y_dir),
  28. .z_step(z_step), .z_dir(z_dir),
  29. .x_limit_ls(x_limit_ls), .x_limit_hs(x_limit_hs),
  30. .y_limit_ls(y_limit_ls), .y_limit_hs(y_limit_hs),
  31. .z_limit_ls(z_limit_ls), .z_limit_hs(z_limit_hs),
  32. .temp_scl(temp_scl), .temp_sda(temp_sda)
  33. );
  34. always @(posedge x_step) x_rises = x_rises + 1;
  35. always @(posedge y_step) y_rises = y_rises + 1;
  36. always @(posedge z_step) z_rises = z_rises + 1;
  37. initial begin
  38. #1_000_000_000 $fatal(1, "top_startup_tb timeout sc=%b rises=%0d/%0d/%0d",
  39. dut.startup_complete, x_rises, y_rises, z_rises);
  40. end
  41. // Serial X→Y→Z calibration: hit open only after that axis's measure-open rises.
  42. task automatic cal_hit_open;
  43. input integer ax;
  44. integer base_x, base_y, base_z;
  45. integer k;
  46. begin
  47. base_x = x_rises; base_y = y_rises; base_z = z_rises;
  48. begin : wait_ax
  49. for (k = 0; k < 5_000_000; k = k + 1) begin
  50. @(posedge sys_clk);
  51. if (ax == 0 && (x_rises - base_x) >= 2) disable wait_ax;
  52. if (ax == 1 && (y_rises - base_y) >= 2) disable wait_ax;
  53. if (ax == 2 && (z_rises - base_z) >= 2) disable wait_ax;
  54. end
  55. $fatal(1, "insufficient measure rises axis=%0d x=%0d y=%0d z=%0d",
  56. ax, x_rises, y_rises, z_rises);
  57. end
  58. if (ax == 0) begin x_limit_hs = 1'b1; x_limit_ls = 1'b0; end
  59. if (ax == 1) begin y_limit_hs = 1'b1; y_limit_ls = 1'b0; end
  60. if (ax == 2) begin z_limit_hs = 1'b1; z_limit_ls = 1'b0; end
  61. repeat (2000) @(posedge sys_clk);
  62. end
  63. endtask
  64. initial begin
  65. x_rises = 0; y_rises = 0; z_rises = 0;
  66. cal_hit_open(0);
  67. cal_hit_open(1);
  68. cal_hit_open(2);
  69. begin : wait_sc
  70. integer k;
  71. for (k = 0; k < 2_000_000; k = k + 1) begin
  72. @(posedge sys_clk);
  73. if (dut.startup_complete) disable wait_sc;
  74. end
  75. $fatal(1, "startup_complete not reached");
  76. end
  77. if (motor_en_n !== 1'b1)
  78. $fatal(1, "EN must remain permanent_high");
  79. repeat (100) @(posedge sys_clk);
  80. if (!dut.startup_complete)
  81. $fatal(1, "startup_complete not sticky");
  82. $display("prod2u_p4_normal_v3_top_startup_tb PASS");
  83. $finish;
  84. end
  85. endmodule