door_motion_controller_tb.v 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. `timescale 1ns/1ps
  2. // Smoke: three axes already on close limit → measure-open with forced rises →
  3. // startup_complete; then mid-percent request on X uses planner.
  4. module door_motion_controller_tb;
  5. reg clk = 1'b0;
  6. reg rst_n = 1'b0;
  7. reg tick_500us = 1'b0;
  8. reg limit_sync_valid = 1'b0;
  9. reg [2:0] open_limit = 3'b000;
  10. reg [2:0] close_limit = 3'b111;
  11. reg [2:0] gated_step = 3'b000;
  12. reg [2:0] gated_step_rise = 3'b000;
  13. reg motor_request_valid = 1'b0;
  14. reg [7:0] percent_x = 8'd0;
  15. reg [7:0] percent_y = 8'd0;
  16. reg [7:0] percent_z = 8'd0;
  17. reg watchdog_stop = 1'b0;
  18. wire [2:0] step;
  19. wire [2:0] dir_close;
  20. wire startup_complete;
  21. wire [2:0] calibrated_mask;
  22. wire [2:0] fault_mask;
  23. reg status_capture = 1'b0;
  24. reg [7:0] status_rd_addr = 8'd0;
  25. wire [7:0] status_rd_data;
  26. always #1 clk = ~clk;
  27. door_motion_controller #(
  28. .MAX_CAL_STEPS(32),
  29. .MIN_TRAVEL(2),
  30. .ENDPOINT_GUARD_EXTRA(8),
  31. .DIR_SETUP_TICKS(1),
  32. .PERCENT_MAX(100)
  33. ) dut (
  34. .clk(clk), .rst_n(rst_n),
  35. .tick_500us(tick_500us),
  36. .limit_sync_valid(limit_sync_valid),
  37. .open_limit(open_limit), .close_limit(close_limit),
  38. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  39. .motor_request_valid(motor_request_valid),
  40. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  41. .watchdog_stop(watchdog_stop), .watchdog_tripped(watchdog_stop),
  42. .step(step), .dir_close(dir_close),
  43. .startup_complete(startup_complete),
  44. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  45. .status_capture(status_capture),
  46. .status_rd_addr(status_rd_addr), .status_rd_data(status_rd_data)
  47. );
  48. task pulse_tick;
  49. begin
  50. @(negedge clk); tick_500us = 1'b1;
  51. @(negedge clk); tick_500us = 1'b0;
  52. end
  53. endtask
  54. task force_rise;
  55. input [2:0] mask;
  56. begin
  57. @(negedge clk);
  58. gated_step = mask;
  59. gated_step_rise = mask;
  60. @(negedge clk);
  61. gated_step_rise = 3'b000;
  62. gated_step = 3'b000;
  63. end
  64. endtask
  65. integer i;
  66. initial begin
  67. #500000 $fatal(1, "door_motion_controller_tb timeout");
  68. end
  69. initial begin
  70. repeat (2) @(posedge clk);
  71. rst_n = 1'b1;
  72. @(negedge clk);
  73. limit_sync_valid = 1'b1;
  74. // Already on close → jump to measure-open; need DIR setup then rises.
  75. repeat (4) pulse_tick();
  76. // Measure-open: 4 rises then hit open_limit same cycle as 4th rise.
  77. for (i = 0; i < 3; i = i + 1) begin
  78. force_rise(3'b111);
  79. pulse_tick();
  80. end
  81. @(negedge clk);
  82. open_limit = 3'b111;
  83. close_limit = 3'b000;
  84. gated_step = 3'b111;
  85. gated_step_rise = 3'b111;
  86. @(negedge clk);
  87. gated_step = 3'b000;
  88. gated_step_rise = 3'b000;
  89. repeat (8) @(negedge clk);
  90. if (!startup_complete)
  91. $fatal(1, "startup_complete not set after measure-open");
  92. if (calibrated_mask !== 3'b111)
  93. $fatal(1, "calibrated_mask=%b", calibrated_mask);
  94. if (fault_mask !== 3'b000)
  95. $fatal(1, "unexpected fault_mask=%b", fault_mask);
  96. // Snapshot byte0: cal=111, fault=000, startup=1, wd=0 → 0x47
  97. @(negedge clk);
  98. status_capture = 1'b1;
  99. @(negedge clk);
  100. status_capture = 1'b0;
  101. status_rd_addr = 8'd0;
  102. @(negedge clk);
  103. if (status_rd_data !== 8'h47)
  104. $fatal(1, "status byte0=%h expect 47", status_rd_data);
  105. // Mid request 50% on X only (travel=4 → target clamp half-up = 2)
  106. open_limit = 3'b000;
  107. @(negedge clk);
  108. percent_x = 8'd50;
  109. percent_y = 8'd0;
  110. percent_z = 8'd0;
  111. motor_request_valid = 1'b1;
  112. @(negedge clk);
  113. motor_request_valid = 1'b0;
  114. // Wait planner + DIR setup + a few steps toward close
  115. for (i = 0; i < 80; i = i + 1) begin
  116. pulse_tick();
  117. if (step[0]) begin
  118. @(negedge clk);
  119. gated_step[0] = 1'b1;
  120. gated_step_rise[0] = 1'b1;
  121. @(negedge clk);
  122. gated_step[0] = 1'b0;
  123. gated_step_rise[0] = 1'b0;
  124. end
  125. end
  126. if (dir_close[0] !== 1'b1)
  127. $fatal(1, "X mid 50%% from open should move close");
  128. $display("door_motion_controller_tb PASS");
  129. $finish;
  130. end
  131. endmodule