door_motor_engine_tb.v 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. `timescale 1ns/1ps
  2. // 3-axis motor engine: 450 rising edges at tick_500us; start ignored while busy.
  3. module door_motor_engine_tb;
  4. localparam integer AXIS_COUNT = 3;
  5. localparam integer HOST_STEPS = 450;
  6. localparam integer DIR_SETUP = 2;
  7. reg clk = 1'b0;
  8. reg rst_n = 1'b0;
  9. reg start = 1'b0;
  10. reg [AXIS_COUNT-1:0] active_mask = {AXIS_COUNT{1'b1}};
  11. reg [AXIS_COUNT-1:0] dir_mask = 3'b101;
  12. reg [12:0] step_count = HOST_STEPS[12:0];
  13. reg tick_500us = 1'b0;
  14. reg abort = 1'b0;
  15. reg [AXIS_COUNT-1:0] stop_mask = {AXIS_COUNT{1'b0}};
  16. wire busy;
  17. wire done;
  18. wire [AXIS_COUNT-1:0] step;
  19. wire [AXIS_COUNT-1:0] dir;
  20. integer x_rises, y_rises, z_rises;
  21. reg prev_x, prev_y, prev_z;
  22. always #1 clk = ~clk;
  23. initial begin
  24. #10000000;
  25. $fatal(1, "door_motor_engine_tb timeout x=%0d y=%0d z=%0d busy=%b",
  26. x_rises, y_rises, z_rises, busy);
  27. end
  28. always @(posedge clk) begin
  29. if (step[0] && !prev_x) x_rises = x_rises + 1;
  30. if (step[1] && !prev_y) y_rises = y_rises + 1;
  31. if (step[2] && !prev_z) z_rises = z_rises + 1;
  32. prev_x = step[0];
  33. prev_y = step[1];
  34. prev_z = step[2];
  35. end
  36. door_motor_engine #(
  37. .AXIS_COUNT(AXIS_COUNT),
  38. .MAX_STEPS(HOST_STEPS),
  39. .DIR_SETUP_TICKS(DIR_SETUP)
  40. ) dut (
  41. .clk(clk), .rst_n(rst_n), .start(start),
  42. .active_mask(active_mask), .dir_mask(dir_mask), .step_count(step_count),
  43. .tick_500us(tick_500us), .abort(abort), .stop_mask(stop_mask),
  44. .busy(busy), .done(done), .step(step), .dir(dir)
  45. );
  46. task pulse_tick;
  47. begin
  48. @(negedge clk);
  49. tick_500us = 1'b1;
  50. @(negedge clk);
  51. tick_500us = 1'b0;
  52. end
  53. endtask
  54. initial begin
  55. x_rises = 0; y_rises = 0; z_rises = 0;
  56. prev_x = 1'b0; prev_y = 1'b0; prev_z = 1'b0;
  57. repeat (3) @(posedge clk);
  58. rst_n = 1'b1;
  59. // All three axes overlap with mixed DIR.
  60. active_mask = 3'b111;
  61. dir_mask = 3'b101;
  62. step_count = HOST_STEPS[12:0];
  63. @(negedge clk); start = 1'b1;
  64. @(negedge clk); start = 1'b0;
  65. repeat (4) pulse_tick();
  66. if (dir !== 3'b101) $fatal(1, "mixed DIR not latched dir=%b", dir);
  67. while (busy) pulse_tick();
  68. @(posedge clk);
  69. if (x_rises !== HOST_STEPS || y_rises !== HOST_STEPS || z_rises !== HOST_STEPS)
  70. $fatal(1, "step count mismatch x=%0d y=%0d z=%0d", x_rises, y_rises, z_rises);
  71. // Busy ignores a new start (mask/dir must not relatch).
  72. x_rises = 0; y_rises = 0; z_rises = 0;
  73. prev_x = 1'b0; prev_y = 1'b0; prev_z = 1'b0;
  74. active_mask = 3'b111;
  75. dir_mask = 3'b000;
  76. @(negedge clk); start = 1'b1;
  77. @(negedge clk); start = 1'b0;
  78. repeat (20) pulse_tick();
  79. if (!busy) $fatal(1, "motor should still be busy");
  80. active_mask = 3'b100;
  81. dir_mask = 3'b100;
  82. @(negedge clk); start = 1'b1;
  83. @(negedge clk); start = 1'b0;
  84. repeat (4) pulse_tick();
  85. if (dir !== 3'b000) $fatal(1, "busy must ignore new start dir=%b", dir);
  86. while (busy) pulse_tick();
  87. @(posedge clk);
  88. if (x_rises !== HOST_STEPS || y_rises !== HOST_STEPS || z_rises !== HOST_STEPS)
  89. $fatal(1, "first run count x=%0d y=%0d z=%0d", x_rises, y_rises, z_rises);
  90. // Abort clears motion immediately.
  91. active_mask = 3'b001;
  92. dir_mask = 3'b000;
  93. @(negedge clk); start = 1'b1;
  94. @(negedge clk); start = 1'b0;
  95. repeat (3) pulse_tick();
  96. @(negedge clk); abort = 1'b1;
  97. @(negedge clk); abort = 1'b0;
  98. @(posedge clk);
  99. if (busy !== 1'b0 || step !== 3'b000)
  100. $fatal(1, "abort did not stop motor immediately");
  101. $display("door_motor_engine_tb PASS");
  102. $finish;
  103. end
  104. endmodule