| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- `timescale 1ns/1ps
- // Smoke: three axes already on close limit → measure-open with forced rises →
- // startup_complete; then mid-percent request on X uses planner.
- module door_motion_controller_tb;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg tick_500us = 1'b0;
- reg limit_sync_valid = 1'b0;
- reg [2:0] open_limit = 3'b000;
- reg [2:0] close_limit = 3'b111;
- reg [2:0] gated_step = 3'b000;
- reg [2:0] gated_step_rise = 3'b000;
- reg motor_request_valid = 1'b0;
- reg [7:0] percent_x = 8'd0;
- reg [7:0] percent_y = 8'd0;
- reg [7:0] percent_z = 8'd0;
- reg watchdog_stop = 1'b0;
- wire [2:0] step;
- wire [2:0] dir_close;
- wire startup_complete;
- wire [2:0] calibrated_mask;
- wire [2:0] fault_mask;
- reg status_capture = 1'b0;
- reg [7:0] status_rd_addr = 8'd0;
- wire [7:0] status_rd_data;
- always #1 clk = ~clk;
- door_motion_controller #(
- .MAX_CAL_STEPS(32),
- .MIN_TRAVEL(2),
- .ENDPOINT_GUARD_EXTRA(8),
- .DIR_SETUP_TICKS(1),
- .PERCENT_MAX(100)
- ) dut (
- .clk(clk), .rst_n(rst_n),
- .tick_500us(tick_500us),
- .limit_sync_valid(limit_sync_valid),
- .open_limit(open_limit), .close_limit(close_limit),
- .gated_step(gated_step), .gated_step_rise(gated_step_rise),
- .motor_request_valid(motor_request_valid),
- .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
- .watchdog_stop(watchdog_stop), .watchdog_tripped(watchdog_stop),
- .step(step), .dir_close(dir_close),
- .startup_complete(startup_complete),
- .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
- .status_capture(status_capture),
- .status_rd_addr(status_rd_addr), .status_rd_data(status_rd_data)
- );
- task pulse_tick;
- begin
- @(negedge clk); tick_500us = 1'b1;
- @(negedge clk); tick_500us = 1'b0;
- end
- endtask
- task force_rise;
- input [2:0] mask;
- begin
- @(negedge clk);
- gated_step = mask;
- gated_step_rise = mask;
- @(negedge clk);
- gated_step_rise = 3'b000;
- gated_step = 3'b000;
- end
- endtask
- integer i;
- initial begin
- #500000 $fatal(1, "door_motion_controller_tb timeout");
- end
- initial begin
- repeat (2) @(posedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- limit_sync_valid = 1'b1;
- // Already on close → jump to measure-open; need DIR setup then rises.
- repeat (4) pulse_tick();
- // Measure-open: 4 rises then hit open_limit same cycle as 4th rise.
- for (i = 0; i < 3; i = i + 1) begin
- force_rise(3'b111);
- pulse_tick();
- end
- @(negedge clk);
- open_limit = 3'b111;
- close_limit = 3'b000;
- gated_step = 3'b111;
- gated_step_rise = 3'b111;
- @(negedge clk);
- gated_step = 3'b000;
- gated_step_rise = 3'b000;
- repeat (8) @(negedge clk);
- if (!startup_complete)
- $fatal(1, "startup_complete not set after measure-open");
- if (calibrated_mask !== 3'b111)
- $fatal(1, "calibrated_mask=%b", calibrated_mask);
- if (fault_mask !== 3'b000)
- $fatal(1, "unexpected fault_mask=%b", fault_mask);
- // Snapshot byte0: cal=111, fault=000, startup=1, wd=0 → 0x47
- @(negedge clk);
- status_capture = 1'b1;
- @(negedge clk);
- status_capture = 1'b0;
- status_rd_addr = 8'd0;
- @(negedge clk);
- if (status_rd_data !== 8'h47)
- $fatal(1, "status byte0=%h expect 47", status_rd_data);
- // Mid request 50% on X only (travel=4 → target clamp half-up = 2)
- open_limit = 3'b000;
- @(negedge clk);
- percent_x = 8'd50;
- percent_y = 8'd0;
- percent_z = 8'd0;
- motor_request_valid = 1'b1;
- @(negedge clk);
- motor_request_valid = 1'b0;
- // Wait planner + DIR setup + a few steps toward close
- for (i = 0; i < 80; i = i + 1) begin
- pulse_tick();
- if (step[0]) begin
- @(negedge clk);
- gated_step[0] = 1'b1;
- gated_step_rise[0] = 1'b1;
- @(negedge clk);
- gated_step[0] = 1'b0;
- gated_step_rise[0] = 1'b0;
- end
- end
- if (dir_close[0] !== 1'b1)
- $fatal(1, "X mid 50%% from open should move close");
- $display("door_motion_controller_tb PASS");
- $finish;
- end
- endmodule
|