door_axis_independence_tb.v 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. `timescale 1ns/1ps
  2. // axis_independence_tb: X both-limits fault must not stall Y/Z motion.
  3. module door_axis_independence_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'b111;
  10. reg [2:0] gated_step = 3'b000;
  11. reg [2:0] gated_step_rise = 3'b000;
  12. reg motor_request_valid = 1'b0;
  13. reg [7:0] percent_x = 8'd0, percent_y = 8'd0, percent_z = 8'd0;
  14. wire [2:0] step, dir_close;
  15. wire startup_complete;
  16. wire [2:0] calibrated_mask, fault_mask;
  17. always #1 clk = ~clk;
  18. door_motion_controller #(
  19. .MAX_CAL_STEPS(16), .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(motor_request_valid),
  28. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  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. task automatic pulse_tick;
  36. begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end
  37. endtask
  38. task automatic force_rise;
  39. input integer ax; reg [2:0] m;
  40. begin
  41. m = 3'b001 << ax;
  42. @(negedge clk); gated_step = gated_step | m; gated_step_rise = m;
  43. @(negedge clk); gated_step_rise = 3'b0; gated_step = gated_step & ~m;
  44. end
  45. endtask
  46. task automatic wait_step;
  47. input integer ax; input want; integer t;
  48. begin
  49. for (t = 0; t < 8000; t = t + 1) begin
  50. if (step[ax] === want) return;
  51. @(negedge clk);
  52. end
  53. $fatal(1, "timeout step[%0d]", ax);
  54. end
  55. endtask
  56. task automatic cal_ok;
  57. input integer ax; input integer travel; integer r; integer t;
  58. begin
  59. close_limit[ax] = 1'b1; open_limit[ax] = 1'b0;
  60. repeat (4) pulse_tick();
  61. for (r = 0; r < travel; r = r + 1) begin
  62. wait_step(ax, 1); force_rise(ax); wait_step(ax, 0);
  63. end
  64. @(negedge clk); open_limit[ax] = 1'b1; close_limit[ax] = 1'b0;
  65. for (t = 0; t < 5000; t = t + 1) begin
  66. if (calibrated_mask[ax]) return;
  67. @(negedge clk);
  68. end
  69. $fatal(1, "cal %0d", ax);
  70. end
  71. endtask
  72. initial begin
  73. #12_000_000 $fatal(1, "timeout");
  74. end
  75. initial begin
  76. integer t, rises_y, rises_z, started;
  77. repeat (2) @(posedge clk); rst_n = 1'b1;
  78. @(negedge clk); limit_sync_valid = 1'b1;
  79. cal_ok(0, 3); cal_ok(1, 4); cal_ok(2, 5);
  80. for (t = 0; t < 5000 && !startup_complete; t = t + 1) @(negedge clk);
  81. if (!startup_complete) $fatal(1, "no startup");
  82. open_limit = 3'b0; close_limit = 3'b0;
  83. @(negedge clk);
  84. percent_x = 8'd50; percent_y = 8'd80; percent_z = 8'd80;
  85. motor_request_valid = 1'b1; @(negedge clk); motor_request_valid = 1'b0;
  86. started = 0;
  87. for (t = 0; t < 8000; t = t + 1) begin
  88. pulse_tick();
  89. if (step[1]) force_rise(1);
  90. if (step[2]) force_rise(2);
  91. if (dut.mov_y && dut.mov_z) begin
  92. started = 1;
  93. @(negedge clk);
  94. open_limit[0] = 1'b1;
  95. close_limit[0] = 1'b1;
  96. t = 8000;
  97. end
  98. @(negedge clk);
  99. end
  100. if (!started) $fatal(1, "Y/Z never both moving");
  101. for (t = 0; t < 2000 && !fault_mask[0]; t = t + 1) @(negedge clk);
  102. if (!fault_mask[0]) $fatal(1, "X not faulted");
  103. rises_y = 0; rises_z = 0;
  104. for (t = 0; t < 30000; t = t + 1) begin
  105. pulse_tick();
  106. if (step[1]) begin force_rise(1); rises_y = rises_y + 1; end
  107. if (step[2]) begin force_rise(2); rises_z = rises_z + 1; end
  108. if (rises_y >= 2 && rises_z >= 2) begin
  109. $display("door_axis_independence_tb PASS y=%0d z=%0d", rises_y, rises_z);
  110. $finish;
  111. end
  112. @(negedge clk);
  113. end
  114. $fatal(1, "Y/Z stalled y=%0d z=%0d", rises_y, rises_z);
  115. end
  116. endmodule