| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- `timescale 1ns/1ps
- module door_motor_engine_stop_tb;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg start = 1'b0;
- reg [1:0] active_mask = 2'b11;
- reg [1:0] dir_mask = 2'b11;
- reg tick_500us = 1'b0;
- reg abort = 1'b0;
- reg [1:0] stop_mask = 2'b00;
- wire busy;
- wire done;
- wire [1:0] step;
- wire [1:0] dir;
- integer x_rises;
- integer y_rises;
- integer edge_count;
- reg prev_x;
- reg prev_y;
- always #1 clk = ~clk;
- door_motor_engine #(
- .FIXED_STEPS(450), .DIR_SETUP_TICKS(2)
- ) dut (
- .clk(clk), .rst_n(rst_n), .start(start),
- .active_mask(active_mask), .dir_mask(dir_mask),
- .tick_500us(tick_500us), .abort(abort),
- .stop_mask(stop_mask),
- .busy(busy), .done(done), .step(step), .dir(dir)
- );
- always @(posedge clk) begin
- prev_x <= step[0];
- prev_y <= step[1];
- if (step[0] && !prev_x) x_rises <= x_rises + 1;
- if (step[1] && !prev_y) y_rises <= y_rises + 1;
- end
- task pulse_tick;
- begin
- @(negedge clk);
- tick_500us = 1'b1;
- @(negedge clk);
- tick_500us = 1'b0;
- end
- endtask
- task reset;
- begin
- rst_n = 1'b0;
- start = 1'b0;
- stop_mask = 2'b00;
- abort = 1'b0;
- active_mask = 2'b11;
- x_rises = 0;
- y_rises = 0;
- prev_x = 1'b0;
- prev_y = 1'b0;
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- @(posedge clk);
- end
- endtask
- task start_motor;
- begin
- @(negedge clk);
- start = 1'b1;
- @(negedge clk);
- start = 1'b0;
- end
- endtask
- task wait_for_done;
- begin
- while (!done) pulse_tick();
- @(posedge clk);
- end
- endtask
- initial begin
- #10000000 $fatal(1, "door_motor_engine_stop_tb timeout");
- end
- initial begin
- // Scenario 1: X stopped while STEP is low. No new X rising edge; final STEP=0.
- reset();
- start_motor();
- // Wait for the first X high pulse to complete and step to become low.
- while (step[0] !== 1'b1) pulse_tick();
- while (step[0] !== 1'b0) pulse_tick();
- stop_mask = 2'b01;
- edge_count = x_rises;
- // Allow many ticks; X should not rise again.
- repeat (30) pulse_tick();
- if (x_rises !== edge_count)
- $fatal(1, "scenario1: new X rising edge after stop low, x_rises=%0d expected %0d", x_rises, edge_count);
- if (y_rises <= edge_count)
- $fatal(1, "scenario1: Y did not continue");
- wait_for_done();
- if (step !== 2'b00)
- $fatal(1, "scenario1: final step not zero");
- // Scenario 2: X stopped while STEP is high. Current pulse must complete, no narrow pulse.
- reset();
- start_motor();
- while (step[0] !== 1'b1) pulse_tick();
- stop_mask = 2'b01;
- edge_count = x_rises;
- // Wait for the current high phase to finish and observe no new X rising edge.
- while (step[0] !== 1'b0) pulse_tick();
- if (x_rises !== edge_count + 1)
- $fatal(1, "scenario2: X pulse not counted as completed");
- edge_count = x_rises;
- repeat (20) pulse_tick();
- if (x_rises !== edge_count)
- $fatal(1, "scenario2: new X rising edge after high-phase stop");
- // Scenario 3: X/Y run; X stopped first, Y reaches 450.
- reset();
- start_motor();
- stop_mask = 2'b01; // stop X immediately (during setup)
- wait_for_done();
- if (y_rises !== 450)
- $fatal(1, "scenario3: Y did not reach 450, got %0d", y_rises);
- if (x_rises !== 0)
- $fatal(1, "scenario3: X should not step, got %0d", x_rises);
- // Scenario 4: X and Y stopped at different times.
- reset();
- start_motor();
- while (x_rises < 3) pulse_tick();
- stop_mask = 2'b01; // stop X
- edge_count = x_rises;
- repeat (30) pulse_tick(); // Y continues
- if (x_rises !== edge_count)
- $fatal(1, "scenario4: X moved after stop");
- if (y_rises <= edge_count)
- $fatal(1, "scenario4: Y did not continue");
- stop_mask = 2'b11; // stop Y too
- wait_for_done();
- if (x_rises !== edge_count)
- $fatal(1, "scenario4: X moved after Y stop");
- // Scenario 5: stop asserted during DIR setup. Zero steps on that axis.
- reset();
- start_motor();
- stop_mask = 2'b01;
- wait_for_done();
- if (x_rises !== 0)
- $fatal(1, "scenario5: X stepped during setup stop");
- if (y_rises !== 450)
- $fatal(1, "scenario5: Y did not reach 450");
- // Scenario 6: stop at the 450th pulse boundary. No 451st rising edge.
- reset();
- start_motor();
- // Run until 449 complete pulses on both axes (step low after 449th pulse).
- while (x_rises < 449 || y_rises < 449) pulse_tick();
- while (step[0] !== 1'b0 || step[1] !== 1'b0) pulse_tick();
- // Stop X at the low boundary before the 450th high phase.
- stop_mask = 2'b01;
- wait_for_done();
- if (x_rises > 449)
- $fatal(1, "scenario6: X produced more than 449 rising edges");
- if (y_rises !== 450)
- $fatal(1, "scenario6: Y did not finish 450, got %0d", y_rises);
- // Scenario 7: host mode (stop_mask=0). Original 450-step behavior unchanged.
- reset();
- start_motor();
- stop_mask = 2'b00;
- wait_for_done();
- if (x_rises !== 450 || y_rises !== 450)
- $fatal(1, "scenario7: host mode count mismatch x=%0d y=%0d", x_rises, y_rises);
- $display("door_motor_engine_stop_tb PASS");
- $finish;
- end
- endmodule
|