| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- `timescale 1ns/1ps
- // Startup + motor engine: LS hard-stop during close truncates axis before step budget.
- module startup_motor_stop_tb;
- localparam integer AXIS_COUNT = 3;
- localparam integer CLOSE_STEPS = 16;
- localparam integer BACKOFF_STEPS = 4;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg tick_500us = 1'b0;
- reg [AXIS_COUNT-1:0] limit_ls = {AXIS_COUNT{1'b0}};
- reg [AXIS_COUNT-1:0] limit_hs = {AXIS_COUNT{1'b0}};
- reg limit_valid = 1'b0;
- wire startup_done;
- wire motor_start;
- wire [AXIS_COUNT-1:0] motor_active_mask;
- wire [AXIS_COUNT-1:0] motor_dir_mask;
- wire [AXIS_COUNT-1:0] motor_stop_mask;
- wire [4:0] motor_step_count;
- wire motor_busy;
- wire motor_done;
- wire [AXIS_COUNT-1:0] step;
- integer x_rises;
- reg prev_x;
- always #1 clk = ~clk;
- always @(posedge clk) begin
- prev_x <= step[0];
- if (!prev_x && step[0]) x_rises <= x_rises + 1;
- end
- startup_close_seek #(
- .CLOSE_STEPS(CLOSE_STEPS),
- .BACKOFF_STEPS(BACKOFF_STEPS),
- .AXIS_COUNT(AXIS_COUNT),
- .CLOSE_DIR_MASK({AXIS_COUNT{1'b1}})
- ) u_startup (
- .clk(clk), .rst_n(rst_n),
- .limit_sync_valid(limit_valid),
- .close_limit(limit_ls), .open_limit(limit_hs),
- .startup_done(startup_done), .motor_start(motor_start),
- .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
- .motor_step_count(motor_step_count), .motor_stop_mask(motor_stop_mask),
- .motor_done(motor_done)
- );
- door_motor_engine #(
- .AXIS_COUNT(AXIS_COUNT),
- .MAX_STEPS(CLOSE_STEPS),
- .DIR_SETUP_TICKS(1)
- ) u_motor (
- .clk(clk), .rst_n(rst_n), .start(motor_start),
- .active_mask(motor_active_mask), .dir_mask(motor_dir_mask),
- .step_count(motor_step_count),
- .tick_500us(tick_500us), .abort(1'b0),
- .stop_mask(motor_stop_mask),
- .busy(motor_busy), .done(motor_done), .step(step), .dir()
- );
- task pulse_tick;
- begin
- @(negedge clk);
- tick_500us = 1'b1;
- @(negedge clk);
- tick_500us = 1'b0;
- end
- endtask
- task reset_dut;
- begin
- rst_n = 1'b0;
- limit_valid = 1'b0;
- x_rises = 0;
- prev_x = 1'b0;
- repeat (4) @(posedge clk);
- rst_n = 1'b1;
- repeat (4) @(posedge clk);
- limit_valid = 1'b1;
- @(posedge clk);
- end
- endtask
- initial begin
- #2000000 $fatal(1, "startup_motor_stop_tb timeout");
- end
- initial begin
- // All LS high -> no steps.
- limit_ls = 3'b111;
- limit_hs = 3'b000;
- reset_dut();
- repeat (32) pulse_tick();
- if (!startup_done) $fatal(1, "expected startup_done when all LS high");
- if (x_rises !== 0) $fatal(1, "unexpected X steps with all LS high");
- // X hits LS during close before consuming full budget.
- limit_ls = 3'b000;
- limit_hs = 3'b000;
- reset_dut();
- while (x_rises < 1) pulse_tick();
- limit_ls[0] = 1'b1;
- while (!startup_done) pulse_tick();
- if (x_rises >= CLOSE_STEPS)
- $fatal(1, "X reached full close budget after LS stop, x=%0d", x_rises);
- $display("startup_motor_stop_tb PASS");
- $finish;
- end
- endmodule
|