`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; 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; reg previous_x_step; reg previous_y_step; always #1 clk = ~clk; initial begin #10000000; $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), .stop_mask(stop_mask), .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; // Close DIR=1 on X, open DIR=0 on Y; both axes 450 rising edges, overlapping. active_mask = 2'b11; dir_mask = 2'b01; @(negedge clk); start = 1'b1; @(negedge clk); start = 1'b0; // Sample DIR during motion (setup + first edges). repeat (4) pulse_tick(); if (dir !== 2'b01) $fatal(1, "mixed DIR not latched during motion dir=%b", dir); repeat (898) 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 after completion"); 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"); // Open both (DIR=0) still produces exactly 450 edges each. x_rises = 0; y_rises = 0; previous_x_step = 1'b0; previous_y_step = 1'b0; active_mask = 2'b11; dir_mask = 2'b00; @(negedge clk); start = 1'b1; @(negedge clk); start = 1'b0; repeat (4) pulse_tick(); if (dir !== 2'b00) $fatal(1, "open DIR not latched dir=%b", dir); while (busy) pulse_tick(); @(posedge clk); if (x_rises !== 450 || y_rises !== 450) $fatal(1, "open DIR step count mismatch x=%0d y=%0d", x_rises, y_rises); // Override: new start while busy must relatch DIR/mask and restart the 450 count. x_rises = 0; y_rises = 0; previous_x_step = 1'b0; previous_y_step = 1'b0; active_mask = 2'b11; dir_mask = 2'b00; @(negedge clk); start = 1'b1; @(negedge clk); start = 1'b0; repeat (20) pulse_tick(); if (!busy) $fatal(1, "motor should still be busy before override"); active_mask = 2'b01; dir_mask = 2'b01; x_rises = 0; y_rises = 0; @(negedge clk); start = 1'b1; @(negedge clk); start = 1'b0; repeat (4) pulse_tick(); if (dir !== 2'b01) $fatal(1, "override DIR not latched dir=%b", dir); while (busy) pulse_tick(); @(posedge clk); if (x_rises !== 450) $fatal(1, "override X steps=%0d", x_rises); if (y_rises !== 0) $fatal(1, "override must drop Y, y_steps=%0d", y_rises); active_mask = 2'b01; dir_mask = 2'b00; @(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