door_motor_engine_tb.v 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 inlet_dir = 1'b0;
  7. reg outlet_dir = 1'b1;
  8. wire busy;
  9. wire done;
  10. wire x_step;
  11. wire x_dir;
  12. wire y_step;
  13. wire y_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. always @(posedge clk) begin
  20. if (x_step && !previous_x_step) x_rises = x_rises + 1;
  21. if (y_step && !previous_y_step) y_rises = y_rises + 1;
  22. previous_x_step = x_step;
  23. previous_y_step = y_step;
  24. end
  25. door_motor_engine #(
  26. .FIXED_STEPS(3), .STEP_HALF_CYCLES(2), .DIR_SETUP_CYCLES(2)
  27. ) dut (
  28. .clk(clk), .rst_n(rst_n), .start(start),
  29. .inlet_dir(inlet_dir), .outlet_dir(outlet_dir),
  30. .busy(busy), .done(done), .x_step(x_step), .x_dir(x_dir),
  31. .y_step(y_step), .y_dir(y_dir)
  32. );
  33. initial begin
  34. x_rises = 0;
  35. y_rises = 0;
  36. previous_x_step = 1'b0;
  37. previous_y_step = 1'b0;
  38. repeat (3) @(posedge clk);
  39. rst_n = 1'b1;
  40. @(negedge clk);
  41. start = 1'b1;
  42. @(negedge clk);
  43. start = 1'b0;
  44. wait (done);
  45. if (x_rises !== 3 || y_rises !== 3) $fatal(1, "step count mismatch x=%0d y=%0d", x_rises, y_rises);
  46. if (x_dir !== inlet_dir || y_dir !== outlet_dir) $fatal(1, "direction mismatch");
  47. if (busy || x_step || y_step) $fatal(1, "motor not idle after completion");
  48. $display("door_motor_engine_tb PASS");
  49. $finish;
  50. end
  51. endmodule