`timescale 1ns/1ps // Production rate-dispatch integration TB. // Instantiates door_motion_controller (dispatcher + builder), not builder alone. // Asserts axis tags match travel and rq == travel * profile_scale[level]. module door_rate_quantum_dispatch_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; wire [2:0] step, dir_close; wire startup_complete; wire [2:0] calibrated_mask, fault_mask; integer done_count; integer skew_hits; integer t; integer r; integer u; integer match_streak; integer done_before; integer rr; integer exp_x; integer exp_y; integer exp_z; always #1 clk = ~clk; function [8:0] f_scale; input [3:0] level; input crawl; begin if (crawl) f_scale = 9'd64; else begin case (level) 4'd0: f_scale = 9'd64; 4'd1: f_scale = 9'd66; 4'd2: f_scale = 9'd73; 4'd3: f_scale = 9'd84; 4'd4: f_scale = 9'd98; 4'd5: f_scale = 9'd114; 4'd6: f_scale = 9'd132; 4'd7: f_scale = 9'd150; 4'd8: f_scale = 9'd170; 4'd9: f_scale = 9'd188; 4'd10: f_scale = 9'd206; 4'd11: f_scale = 9'd222; 4'd12: f_scale = 9'd236; 4'd13: f_scale = 9'd247; 4'd14: f_scale = 9'd254; default: f_scale = 9'd256; endcase end end endfunction door_motion_controller #( .MAX_CAL_STEPS(32), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100), .MOTION_SERVICE_CLKS(4), .PHASE_FACTOR(64), .CAL_RAMP_UNIT(2) ) 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(1'b0), .percent_x(8'd0), .percent_y(8'd0), .percent_z(8'd0), .watchdog_stop(1'b0), .watchdog_tripped(1'b0), .step(step), .dir_close(dir_close), .startup_complete(startup_complete), .calibrated_mask(calibrated_mask), .fault_mask(fault_mask), .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data() ); always @(posedge clk) begin if (rst_n && dut.rate_done) begin done_count = done_count + 1; if (dut.rate_result !== (dut.rate_travel_done * dut.rate_scale_done)) $fatal(1, "product mismatch axis=%0d t=%0d s=%0d p=%0d", dut.rate_axis_done, dut.rate_travel_done, dut.rate_scale_done, dut.rate_result); if (startup_complete && !dut.in_cal_x && !dut.in_cal_y && !dut.in_cal_z) begin if (dut.rate_axis_done == 2'd0 && dut.st_trav_x != 0 && dut.rate_travel_done != dut.st_trav_x) skew_hits = skew_hits + 1; if (dut.rate_axis_done == 2'd1 && dut.st_trav_y != 0 && dut.rate_travel_done != dut.st_trav_y) skew_hits = skew_hits + 1; if (dut.rate_axis_done == 2'd2 && dut.st_trav_z != 0 && dut.rate_travel_done != dut.st_trav_z) skew_hits = skew_hits + 1; end end end task pulse_tick; begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end endtask task force_rise; input integer ax; reg [2:0] m; begin m = 3'b001 << ax; @(negedge clk); gated_step = gated_step | m; gated_step_rise = m; @(negedge clk); gated_step_rise = 3'b0; gated_step = gated_step & ~m; end endtask task wait_step; input integer ax; input want; begin for (t = 0; t < 10000; t = t + 1) begin if (step[ax] === want) disable wait_step; @(negedge clk); end $fatal(1, "timeout step ax=%0d want=%0d", ax, want); end endtask task cal_ok; input integer ax; input integer travel; begin close_limit[ax] = 1'b1; open_limit[ax] = 1'b0; repeat (4) pulse_tick(); for (r = 0; r < travel; r = r + 1) begin wait_step(ax, 1); force_rise(ax); wait_step(ax, 0); end @(negedge clk); open_limit[ax] = 1'b1; close_limit[ax] = 1'b0; begin : w for (t = 0; t < 8000; t = t + 1) begin if (calibrated_mask[ax]) disable w; @(negedge clk); end $fatal(1, "cal ax=%0d", ax); end end endtask initial begin #30_000_000 $fatal(1, "door_rate_quantum_dispatch_tb timeout"); end initial begin done_count = 0; skew_hits = 0; repeat (2) @(posedge clk); rst_n = 1'b1; @(negedge clk); limit_sync_valid = 1'b1; // Travels 4/6/8: on old tip axis tags rotate (skew_hits>0 / wrong rq mapping). cal_ok(0, 4); cal_ok(1, 6); cal_ok(2, 8); begin : ws for (u = 0; u < 8000; u = u + 1) begin if (startup_complete) disable ws; @(negedge clk); end $fatal(1, "startup"); end match_streak = 0; begin : settle for (t = 0; t < 8000; t = t + 1) begin exp_x = dut.st_trav_x * f_scale(dut.lvl_x, dut.crawl_x); exp_y = dut.st_trav_y * f_scale(dut.lvl_y, dut.crawl_y); exp_z = dut.st_trav_z * f_scale(dut.lvl_z, dut.crawl_z); if (dut.rq_x == exp_x[20:0] && dut.rq_y == exp_y[20:0] && dut.rq_z == exp_z[20:0] && !dut.in_cal_x && !dut.in_cal_y && !dut.in_cal_z && dut.st_trav_x == 13'd4 && dut.st_trav_y == 13'd6 && dut.st_trav_z == 13'd8) begin match_streak = match_streak + 1; if (match_streak >= 8) disable settle; end else begin match_streak = 0; end @(negedge clk); end $fatal(1, "Case A FAIL rq=%0d/%0d/%0d exp~%0d/%0d/%0d skew_hits=%0d done=%0d lvl=%0d/%0d/%0d", dut.rq_x, dut.rq_y, dut.rq_z, exp_x, exp_y, exp_z, skew_hits, done_count, dut.lvl_x, dut.lvl_y, dut.lvl_z); end if (skew_hits != 0) $fatal(1, "Case A skew_hits=%0d", skew_hits); $display("Case A PASS rq=%0d/%0d/%0d lvl=%0d/%0d/%0d", dut.rq_x, dut.rq_y, dut.rq_z, dut.lvl_x, dut.lvl_y, dut.lvl_z); done_before = done_count; for (rr = 0; rr < 64; rr = rr + 1) @(negedge clk); if (done_count < done_before + 3) $fatal(1, "multi-round refresh delta=%0d", done_count - done_before); exp_x = dut.st_trav_x * f_scale(dut.lvl_x, dut.crawl_x); exp_y = dut.st_trav_y * f_scale(dut.lvl_y, dut.crawl_y); exp_z = dut.st_trav_z * f_scale(dut.lvl_z, dut.crawl_z); if (!(dut.rq_x == exp_x[20:0] && dut.rq_y == exp_y[20:0] && dut.rq_z == exp_z[20:0])) $fatal(1, "multi-round drift rq=%0d/%0d/%0d", dut.rq_x, dut.rq_y, dut.rq_z); if (skew_hits != 0) $fatal(1, "multi-round skew_hits=%0d", skew_hits); $display("Case multi-round PASS done=%0d", done_count); if (dut.in_cal_x || dut.in_cal_y || dut.in_cal_z) $fatal(1, "still in cal"); $display("door_rate_quantum_dispatch_tb PASS done=%0d rq=%0d/%0d/%0d skew_hits=%0d", done_count, dut.rq_x, dut.rq_y, dut.rq_z, skew_hits); $finish; end endmodule