door_serial_calibration_tb.v 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. `timescale 1ns/1ps
  2. // serial_calibration_tb: X→Y→Z; mutual exclusion; X fail → Y/Z continue; startup after profile.
  3. module door_serial_calibration_tb;
  4. reg clk = 1'b0;
  5. reg rst_n = 1'b0;
  6. reg tick_500us = 1'b0;
  7. reg limit_sync_valid = 1'b0;
  8. reg [2:0] open_limit = 3'b000;
  9. reg [2:0] close_limit = 3'b000; // X starts seek-close (not already on close)
  10. reg [2:0] gated_step = 3'b000;
  11. reg [2:0] gated_step_rise = 3'b000;
  12. wire [2:0] step, dir_close;
  13. wire startup_complete;
  14. wire [2:0] calibrated_mask, fault_mask;
  15. wire [2:0] cal_en = dut.cal_enable;
  16. integer max_concurrent;
  17. always #1 clk = ~clk;
  18. door_motion_controller #(
  19. .MAX_CAL_STEPS(8), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(4),
  20. .DIR_SETUP_TICKS(1), .PERCENT_MAX(100),
  21. .MOTION_SERVICE_CLKS(4), .PHASE_FACTOR(64), .CAL_RAMP_UNIT(2)
  22. ) dut (
  23. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  24. .limit_sync_valid(limit_sync_valid),
  25. .open_limit(open_limit), .close_limit(close_limit),
  26. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  27. .motor_request_valid(1'b0),
  28. .percent_x(8'd0), .percent_y(8'd0), .percent_z(8'd0),
  29. .watchdog_stop(1'b0), .watchdog_tripped(1'b0),
  30. .step(step), .dir_close(dir_close),
  31. .startup_complete(startup_complete),
  32. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  33. .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data()
  34. );
  35. always @(posedge clk) if (rst_n) begin
  36. if (((dut.in_cal_x && dut.mov_x) + (dut.in_cal_y && dut.mov_y) +
  37. (dut.in_cal_z && dut.mov_z)) > 1)
  38. $fatal(1, "concurrent cal motion");
  39. if ((cal_en[0]+cal_en[1]+cal_en[2]) > 1)
  40. $fatal(1, "multi enable %b", cal_en);
  41. end
  42. task automatic pulse_tick;
  43. begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end
  44. endtask
  45. task automatic force_rise;
  46. input integer ax; reg [2:0] m;
  47. begin
  48. m = 3'b001 << ax;
  49. @(negedge clk); gated_step = gated_step | m; gated_step_rise = m;
  50. @(negedge clk); gated_step_rise = 3'b0; gated_step = gated_step & ~m;
  51. end
  52. endtask
  53. task automatic wait_step;
  54. input integer ax; input want; integer t;
  55. begin
  56. for (t = 0; t < 20000; t = t + 1) begin
  57. if (step[ax] === want) return;
  58. @(negedge clk);
  59. end
  60. $fatal(1, "timeout step[%0d]==%0d state=%0d en=%b", ax, want,
  61. (ax==0)?dut.u_axis_x.status_state:(ax==1)?dut.u_axis_y.status_state:dut.u_axis_z.status_state,
  62. cal_en);
  63. end
  64. endtask
  65. task automatic cal_measure;
  66. input integer ax; input integer travel; integer r;
  67. begin
  68. close_limit[ax] = 1'b1; open_limit[ax] = 1'b0;
  69. repeat (6) pulse_tick();
  70. for (r = 0; r < travel; r = r + 1) begin
  71. wait_step(ax, 1); force_rise(ax); wait_step(ax, 0);
  72. end
  73. @(negedge clk); open_limit[ax] = 1'b1; close_limit[ax] = 1'b0;
  74. begin : w integer t; for (t = 0; t < 8000; t = t + 1) begin
  75. if (calibrated_mask[ax] || fault_mask[ax]) disable w;
  76. @(negedge clk);
  77. end end
  78. if (!calibrated_mask[ax]) $fatal(1, "cal fail ax=%0d", ax);
  79. end
  80. endtask
  81. initial begin
  82. #12_000_000 $fatal(1, "timeout");
  83. end
  84. initial begin
  85. integer r, t;
  86. max_concurrent = 0;
  87. repeat (2) @(posedge clk); rst_n = 1'b1;
  88. @(negedge clk); limit_sync_valid = 1'b1;
  89. // X seek-close timeout (close never asserts)
  90. repeat (6) pulse_tick();
  91. begin : xfail
  92. integer r, t;
  93. for (r = 0; r < 40 && !fault_mask[0]; r = r + 1) begin
  94. for (t = 0; t < 20000 && !step[0] && !fault_mask[0]; t = t + 1)
  95. @(negedge clk);
  96. if (fault_mask[0]) disable xfail;
  97. if (step[0]) begin
  98. force_rise(0);
  99. for (t = 0; t < 20000 && step[0] && !fault_mask[0]; t = t + 1)
  100. @(negedge clk);
  101. end else
  102. pulse_tick();
  103. end
  104. end
  105. if (!fault_mask[0]) $fatal(1, "X not faulted");
  106. begin : wait_y_en
  107. integer t;
  108. for (t = 0; t < 200; t = t + 1) begin
  109. if (cal_en[1]) disable wait_y_en;
  110. @(negedge clk);
  111. end
  112. $fatal(1, "Y not enabled after X fault en=%b term=%b/%b/%b",
  113. cal_en, dut.term_x, dut.term_y, dut.term_z);
  114. end
  115. cal_measure(1, 3);
  116. cal_measure(2, 5);
  117. for (t = 0; t < 8000 && !startup_complete; t = t + 1) @(negedge clk);
  118. if (!startup_complete) $fatal(1, "no startup_complete");
  119. if (calibrated_mask !== 3'b110) $fatal(1, "mask=%b", calibrated_mask);
  120. if (dut.u_axis_y.status_travel !== 13'd3) $fatal(1, "Y travel");
  121. if (dut.u_axis_z.status_travel !== 13'd5) $fatal(1, "Z travel");
  122. $display("door_serial_calibration_tb PASS");
  123. $finish;
  124. end
  125. endmodule