| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- `timescale 1ns/1ps
- // 3-axis startup close-seek with accelerated ticks and reduced step budgets.
- module startup_close_seek_tb;
- localparam integer AXIS_COUNT = 3;
- localparam integer CLOSE_STEPS = 12;
- localparam integer BACKOFF_STEPS = 4;
- localparam integer DIR_SETUP = 1;
- 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_sync_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 [12:0] motor_step_count;
- wire motor_busy;
- wire motor_done;
- wire [AXIS_COUNT-1:0] step;
- wire [AXIS_COUNT-1:0] dir;
- integer start_count;
- always #1 clk = ~clk;
- startup_close_seek #(
- .CLOSE_STEPS(CLOSE_STEPS),
- .BACKOFF_STEPS(BACKOFF_STEPS),
- .AXIS_COUNT(AXIS_COUNT),
- .CLOSE_DIR_MASK({AXIS_COUNT{1'b1}})
- ) dut (
- .clk(clk), .rst_n(rst_n),
- .limit_sync_valid(limit_sync_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(DIR_SETUP)
- ) 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[12:0]),
- .tick_500us(tick_500us), .abort(1'b0),
- .stop_mask(motor_stop_mask),
- .busy(motor_busy), .done(motor_done), .step(step), .dir(dir)
- );
- task pulse_tick;
- begin
- @(negedge clk);
- tick_500us = 1'b1;
- @(negedge clk);
- tick_500us = 1'b0;
- end
- endtask
- task wait_sync_valid;
- begin
- repeat (3) @(posedge clk);
- limit_sync_valid = 1'b1;
- end
- endtask
- task wait_startup_done;
- begin
- while (!startup_done) pulse_tick();
- @(posedge clk);
- end
- endtask
- initial begin
- #10000000 $fatal(1, "startup_close_seek_tb timeout");
- end
- always @(posedge clk) begin
- if (motor_start) start_count <= start_count + 1;
- if (!limit_sync_valid && motor_start)
- $fatal(1, "motor_start asserted before limit_sync_valid");
- end
- initial begin
- start_count = 0;
- // Scenario 1: all axes already at LS -> immediate done, no close start.
- limit_ls = 3'b111;
- limit_hs = 3'b000;
- limit_sync_valid = 1'b0;
- start_count = 0;
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- wait_sync_valid();
- wait_startup_done();
- if (start_count !== 0)
- $fatal(1, "expected 0 close starts when all LS high, got %0d", start_count);
- // Scenario 2: X hits LS during close; Y/Z finish close budget then backoff.
- rst_n = 1'b0;
- limit_ls = 3'b000;
- limit_hs = 3'b000;
- limit_sync_valid = 1'b0;
- start_count = 0;
- @(negedge clk);
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- wait_sync_valid();
- while (!startup_done) begin
- if (step[0] && limit_ls[0] == 1'b0) limit_ls[0] <= 1'b1;
- pulse_tick();
- end
- if (limit_ls[0] !== 1'b1) $fatal(1, "X should have reached LS");
- if (!startup_done) $fatal(1, "startup_done not set after close/backoff");
- // Scenario 3: post-done sticky; limit toggles must not restart.
- start_count = 0;
- limit_ls = 3'b000;
- limit_hs = 3'b111;
- repeat (20) pulse_tick();
- if (!startup_done) $fatal(1, "startup_done cleared after post-done limit toggle");
- if (start_count !== 0) $fatal(1, "post-done limit toggle restarted motion");
- $display("startup_close_seek_tb PASS");
- $finish;
- end
- endmodule
|