door_motor_engine_tb.v 2.6 KB

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