| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- `timescale 1ns/1ps
- module door_motor_engine_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'b01;
- reg tick_500us = 1'b0;
- reg abort = 1'b0;
- wire busy;
- wire done;
- wire [1:0] step;
- wire [1:0] dir;
- integer x_rises;
- integer y_rises;
- reg previous_x_step;
- reg previous_y_step;
- always #1 clk = ~clk;
- initial begin
- #10000;
- $fatal(1, "motor production-boundary timeout x=%0d y=%0d busy=%b step=%b",
- x_rises, y_rises, busy, step);
- end
- always @(posedge clk) begin
- if (step[0] && !previous_x_step) x_rises = x_rises + 1;
- if (step[1] && !previous_y_step) y_rises = y_rises + 1;
- previous_x_step = step[0];
- previous_y_step = step[1];
- end
- // Production-boundary simulation: Formal uses a small parameterized proof,
- // while this bench checks the real 450 rising-edge contract.
- 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),
- .busy(busy), .done(done), .step(step), .dir(dir)
- );
- task pulse_tick;
- begin
- @(negedge clk);
- tick_500us = 1'b1;
- @(negedge clk);
- tick_500us = 1'b0;
- end
- endtask
- initial begin
- x_rises = 0;
- y_rises = 0;
- previous_x_step = 1'b0;
- previous_y_step = 1'b0;
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- start = 1'b1;
- @(negedge clk);
- start = 1'b0;
- repeat (902) pulse_tick();
- @(posedge clk);
- @(negedge clk);
- if (x_rises !== 450 || y_rises !== 450) $fatal(1, "step count mismatch x=%0d y=%0d", x_rises, y_rises);
- if (dir !== dir_mask) $fatal(1, "direction mismatch");
- if (busy !== 1'b0 || step !== 2'b00) $fatal(1, "motor not idle after completion");
- if (x_rises < 1 || y_rises < 1) $fatal(1, "axes did not overlap");
- active_mask = 2'b01;
- @(negedge clk);
- start = 1'b1;
- @(negedge clk);
- start = 1'b0;
- repeat (3) pulse_tick();
- @(negedge clk);
- abort = 1'b1;
- @(negedge clk);
- abort = 1'b0;
- @(posedge clk);
- @(negedge clk);
- if (busy !== 1'b0 || step !== 2'b00) $fatal(1, "abort did not stop motor immediately");
- $display("door_motor_engine_tb PASS");
- $finish;
- end
- endmodule
|