door_motor_engine_tb.v 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. `timescale 1ns/1ps
  2. module door_motor_engine_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg start = 1'b0;
  6. reg [1:0] active_mask = 2'b11;
  7. reg [1:0] dir_mask = 2'b01;
  8. reg tick_500us = 1'b0;
  9. reg abort = 1'b0;
  10. wire busy;
  11. wire done;
  12. wire [1:0] step;
  13. wire [1:0] dir;
  14. integer x_rises;
  15. integer y_rises;
  16. reg previous_x_step;
  17. reg previous_y_step;
  18. always #1 clk = ~clk;
  19. initial begin
  20. #10000;
  21. $fatal(1, "motor production-boundary timeout x=%0d y=%0d busy=%b step=%b",
  22. x_rises, y_rises, busy, step);
  23. end
  24. always @(posedge clk) begin
  25. if (step[0] && !previous_x_step) x_rises = x_rises + 1;
  26. if (step[1] && !previous_y_step) y_rises = y_rises + 1;
  27. previous_x_step = step[0];
  28. previous_y_step = step[1];
  29. end
  30. // Production-boundary simulation: Formal uses a small parameterized proof,
  31. // while this bench checks the real 450 rising-edge contract.
  32. door_motor_engine #(
  33. .FIXED_STEPS(450), .DIR_SETUP_TICKS(2)
  34. ) dut (
  35. .clk(clk), .rst_n(rst_n), .start(start),
  36. .active_mask(active_mask), .dir_mask(dir_mask), .tick_500us(tick_500us), .abort(abort),
  37. .busy(busy), .done(done), .step(step), .dir(dir)
  38. );
  39. task pulse_tick;
  40. begin
  41. @(negedge clk);
  42. tick_500us = 1'b1;
  43. @(negedge clk);
  44. tick_500us = 1'b0;
  45. end
  46. endtask
  47. initial begin
  48. x_rises = 0;
  49. y_rises = 0;
  50. previous_x_step = 1'b0;
  51. previous_y_step = 1'b0;
  52. repeat (3) @(posedge clk);
  53. rst_n = 1'b1;
  54. @(negedge clk);
  55. start = 1'b1;
  56. @(negedge clk);
  57. start = 1'b0;
  58. repeat (902) pulse_tick();
  59. @(posedge clk);
  60. @(negedge clk);
  61. if (x_rises !== 450 || y_rises !== 450) $fatal(1, "step count mismatch x=%0d y=%0d", x_rises, y_rises);
  62. if (dir !== dir_mask) $fatal(1, "direction mismatch");
  63. if (busy !== 1'b0 || step !== 2'b00) $fatal(1, "motor not idle after completion");
  64. if (x_rises < 1 || y_rises < 1) $fatal(1, "axes did not overlap");
  65. active_mask = 2'b01;
  66. @(negedge clk);
  67. start = 1'b1;
  68. @(negedge clk);
  69. start = 1'b0;
  70. repeat (3) pulse_tick();
  71. @(negedge clk);
  72. abort = 1'b1;
  73. @(negedge clk);
  74. abort = 1'b0;
  75. @(posedge clk);
  76. @(negedge clk);
  77. if (busy !== 1'b0 || step !== 2'b00) $fatal(1, "abort did not stop motor immediately");
  78. $display("door_motor_engine_tb PASS");
  79. $finish;
  80. end
  81. endmodule