| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- `timescale 1ns/1ps
- // 3-axis motor engine: 450 rising edges at tick_500us; start ignored while busy.
- module door_motor_engine_tb;
- localparam integer AXIS_COUNT = 3;
- localparam integer HOST_STEPS = 450;
- localparam integer DIR_SETUP = 2;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg start = 1'b0;
- reg [AXIS_COUNT-1:0] active_mask = {AXIS_COUNT{1'b1}};
- reg [AXIS_COUNT-1:0] dir_mask = 3'b101;
- reg [12:0] step_count = HOST_STEPS[12:0];
- reg tick_500us = 1'b0;
- reg abort = 1'b0;
- reg [AXIS_COUNT-1:0] stop_mask = {AXIS_COUNT{1'b0}};
- wire busy;
- wire done;
- wire [AXIS_COUNT-1:0] step;
- wire [AXIS_COUNT-1:0] dir;
- integer x_rises, y_rises, z_rises;
- reg prev_x, prev_y, prev_z;
- always #1 clk = ~clk;
- initial begin
- #10000000;
- $fatal(1, "door_motor_engine_tb timeout x=%0d y=%0d z=%0d busy=%b",
- x_rises, y_rises, z_rises, busy);
- end
- always @(posedge clk) begin
- if (step[0] && !prev_x) x_rises = x_rises + 1;
- if (step[1] && !prev_y) y_rises = y_rises + 1;
- if (step[2] && !prev_z) z_rises = z_rises + 1;
- prev_x = step[0];
- prev_y = step[1];
- prev_z = step[2];
- end
- door_motor_engine #(
- .AXIS_COUNT(AXIS_COUNT),
- .MAX_STEPS(HOST_STEPS),
- .DIR_SETUP_TICKS(DIR_SETUP)
- ) dut (
- .clk(clk), .rst_n(rst_n), .start(start),
- .active_mask(active_mask), .dir_mask(dir_mask), .step_count(step_count),
- .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; z_rises = 0;
- prev_x = 1'b0; prev_y = 1'b0; prev_z = 1'b0;
- repeat (3) @(posedge clk);
- rst_n = 1'b1;
- // All three axes overlap with mixed DIR.
- active_mask = 3'b111;
- dir_mask = 3'b101;
- step_count = HOST_STEPS[12:0];
- @(negedge clk); start = 1'b1;
- @(negedge clk); start = 1'b0;
- repeat (4) pulse_tick();
- if (dir !== 3'b101) $fatal(1, "mixed DIR not latched dir=%b", dir);
- while (busy) pulse_tick();
- @(posedge clk);
- if (x_rises !== HOST_STEPS || y_rises !== HOST_STEPS || z_rises !== HOST_STEPS)
- $fatal(1, "step count mismatch x=%0d y=%0d z=%0d", x_rises, y_rises, z_rises);
- // Busy ignores a new start (mask/dir must not relatch).
- x_rises = 0; y_rises = 0; z_rises = 0;
- prev_x = 1'b0; prev_y = 1'b0; prev_z = 1'b0;
- active_mask = 3'b111;
- dir_mask = 3'b000;
- @(negedge clk); start = 1'b1;
- @(negedge clk); start = 1'b0;
- repeat (20) pulse_tick();
- if (!busy) $fatal(1, "motor should still be busy");
- active_mask = 3'b100;
- dir_mask = 3'b100;
- @(negedge clk); start = 1'b1;
- @(negedge clk); start = 1'b0;
- repeat (4) pulse_tick();
- if (dir !== 3'b000) $fatal(1, "busy must ignore new start dir=%b", dir);
- while (busy) pulse_tick();
- @(posedge clk);
- if (x_rises !== HOST_STEPS || y_rises !== HOST_STEPS || z_rises !== HOST_STEPS)
- $fatal(1, "first run count x=%0d y=%0d z=%0d", x_rises, y_rises, z_rises);
- // Abort clears motion immediately.
- active_mask = 3'b001;
- dir_mask = 3'b000;
- @(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);
- if (busy !== 1'b0 || step !== 3'b000)
- $fatal(1, "abort did not stop motor immediately");
- $display("door_motor_engine_tb PASS");
- $finish;
- end
- endmodule
|