door_planner_grant_once_tb.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. `timescale 1ns/1ps
  2. // Shared planner arbiter: each axis request granted once; X done must not
  3. // steal a second grant before Y.
  4. module door_planner_grant_once_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), .MIN_TRAVEL(2),
  29. .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100)
  30. ) dut (
  31. .clk(clk), .rst_n(rst_n),
  32. .tick_500us(tick_500us),
  33. .limit_sync_valid(limit_sync_valid),
  34. .open_limit(open_limit), .close_limit(close_limit),
  35. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  36. .motor_request_valid(motor_request_valid),
  37. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  38. .watchdog_stop(watchdog_stop), .watchdog_tripped(1'b0),
  39. .step(step), .dir_close(dir_close),
  40. .startup_complete(startup_complete),
  41. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  42. .status_capture(status_capture),
  43. .status_rd_addr(status_rd_addr), .status_rd_data(status_rd_data)
  44. );
  45. integer starts_x, starts_y, starts_z;
  46. always @(posedge clk) begin
  47. if (rst_n && dut.grant_start) begin
  48. if (dut.grant_axis == 2'd0) starts_x = starts_x + 1;
  49. else if (dut.grant_axis == 2'd1) starts_y = starts_y + 1;
  50. else if (dut.grant_axis == 2'd2) starts_z = starts_z + 1;
  51. end
  52. end
  53. task pulse_tick;
  54. begin
  55. @(negedge clk); tick_500us = 1'b1;
  56. @(negedge clk); tick_500us = 1'b0;
  57. end
  58. endtask
  59. task force_rise;
  60. input [2:0] mask;
  61. begin
  62. @(negedge clk);
  63. gated_step = mask; gated_step_rise = mask;
  64. @(negedge clk);
  65. gated_step_rise = 3'b000; gated_step = 3'b000;
  66. end
  67. endtask
  68. integer i;
  69. initial begin
  70. #500000 $fatal(1, "door_planner_grant_once_tb timeout");
  71. end
  72. initial begin
  73. starts_x = 0; starts_y = 0; starts_z = 0;
  74. repeat (2) @(posedge clk);
  75. rst_n = 1'b1;
  76. @(negedge clk);
  77. limit_sync_valid = 1'b1;
  78. repeat (4) pulse_tick();
  79. for (i = 0; i < 3; i = i + 1) begin
  80. force_rise(3'b111);
  81. pulse_tick();
  82. end
  83. @(negedge clk);
  84. open_limit = 3'b111; close_limit = 3'b000;
  85. gated_step = 3'b111; gated_step_rise = 3'b111;
  86. @(negedge clk);
  87. gated_step = 3'b000; gated_step_rise = 3'b000;
  88. repeat (8) @(negedge clk);
  89. if (!startup_complete) $fatal(1, "no startup");
  90. open_limit = 3'b000;
  91. starts_x = 0; starts_y = 0; starts_z = 0;
  92. @(negedge clk);
  93. percent_x = 8'd25;
  94. percent_y = 8'd50;
  95. percent_z = 8'd75;
  96. motor_request_valid = 1'b1;
  97. @(negedge clk);
  98. motor_request_valid = 1'b0;
  99. // Planner iterates up to travel*percent — with travel=4, tiny.
  100. for (i = 0; i < 80; i = i + 1)
  101. pulse_tick();
  102. if (starts_x !== 1)
  103. $fatal(1, "X starts=%0d expect 1", starts_x);
  104. if (starts_y !== 1)
  105. $fatal(1, "Y starts=%0d expect 1", starts_y);
  106. if (starts_z !== 1)
  107. $fatal(1, "Z starts=%0d expect 1", starts_z);
  108. // Endpoint must not use planner
  109. starts_x = 0; starts_y = 0; starts_z = 0;
  110. @(negedge clk);
  111. percent_x = 8'd0;
  112. percent_y = 8'd100;
  113. percent_z = 8'd0;
  114. motor_request_valid = 1'b1;
  115. @(negedge clk);
  116. motor_request_valid = 1'b0;
  117. for (i = 0; i < 20; i = i + 1)
  118. pulse_tick();
  119. if ((starts_x + starts_y + starts_z) !== 0)
  120. $fatal(1, "endpoint targets must not grant planner");
  121. $display("door_planner_grant_once_tb PASS");
  122. $finish;
  123. end
  124. endmodule