`timescale 1ns/1ps // Three-axis door motion: Scheme A full-feature // - per-axis safety kernels // - serial X→Y→Z calibration // - shared 25 us DDA (active A1 sequential) + shared profile/rate builders module door_motion_controller #( parameter integer MAX_CAL_STEPS = 5000, parameter integer MIN_TRAVEL = 2, parameter integer ENDPOINT_GUARD_EXTRA = 500, parameter integer DIR_SETUP_TICKS = 2, parameter integer PERCENT_MAX = 100, parameter integer STATUS_PAYLOAD_LENGTH = 31, parameter integer MOTION_SERVICE_CLKS = 625, parameter integer PHASE_FACTOR = 5120, parameter integer CAL_RAMP_UNIT = 8 ) ( input wire clk, input wire rst_n, input wire tick_500us, input wire limit_sync_valid, input wire [2:0] open_limit, input wire [2:0] close_limit, input wire [2:0] gated_step, input wire [2:0] gated_step_rise, input wire motor_request_valid, input wire [7:0] percent_x, input wire [7:0] percent_y, input wire [7:0] percent_z, input wire watchdog_stop, input wire watchdog_tripped, output wire [2:0] step, output wire [2:0] dir_close, output reg startup_complete, output wire [2:0] calibrated_mask, output wire [2:0] fault_mask, input wire status_capture, input wire [7:0] status_rd_addr, output wire [7:0] status_rd_data ); wire planner_start; wire [1:0] planner_axis_id; wire [7:0] planner_percent_i; wire [12:0] planner_travel_i; wire planner_busy; wire planner_done; wire [12:0] planner_result; wire [1:0] planner_done_axis; wire [7:0] planner_done_percent; wire req_x, req_y, req_z; wire [7:0] pct_req_x, pct_req_y, pct_req_z; wire [12:0] trav_req_x, trav_req_y, trav_req_z; wire [2:0] st_state_x, st_state_y, st_state_z; wire [7:0] st_flags_x, st_flags_y, st_flags_z; wire [7:0] st_pct_x, st_pct_y, st_pct_z; wire [12:0] st_cur_x, st_cur_y, st_cur_z; wire [12:0] st_tgt_x, st_tgt_y, st_tgt_z; wire [12:0] st_trav_x, st_trav_y, st_trav_z; wire [7:0] st_fault_x, st_fault_y, st_fault_z; wire cal_x, cal_y, cal_z; wire fault_x, fault_y, fault_z; wire term_x, term_y, term_z; wire mov_x, mov_y, mov_z; wire allow_x, allow_y, allow_z; wire in_cal_x, in_cal_y, in_cal_z; wire crawl_x, crawl_y, crawl_z; wire dda_run_x, dda_run_y, dda_run_z; wire [3:0] lvl_x, lvl_y, lvl_z; wire [20:0] rq_x, rq_y, rq_z; wire [2:0] cal_enable; wire profile_start; wire profile_ready; wire profile_busy; wire profile_done; wire [12:0] max_travel; wire [24:0] phase_threshold; wire service_ce; wire [2:0] toggle_pulse; wire dda_busy; wire dda_overrun; wire all_axes_terminal_unused; wire [12:0] profile_distance_unused_x; wire [12:0] profile_done_steps_unused_x; wire [12:0] profile_ramp_unit_unused_x; wire [12:0] profile_distance_unused_y; wire [12:0] profile_done_steps_unused_y; wire [12:0] profile_ramp_unit_unused_y; wire [12:0] profile_distance_unused_z; wire [12:0] profile_done_steps_unused_z; wire [12:0] profile_ramp_unit_unused_z; // Shared rate refresh: travel * profile_scale[level] (or scale alone in cal). // Exact 21-bit product via door_rate_quantum_dispatcher (atomic launch record). wire rate_busy; wire rate_done; wire [1:0] rate_axis_done; wire [12:0] rate_travel_done; wire [8:0] rate_scale_done; wire [20:0] rate_result; wire rate_valid_x; wire rate_valid_y; wire rate_valid_z; function automatic [8:0] f_profile_scale; input [3:0] level; begin case (level) 4'd0: f_profile_scale = 9'd64; 4'd1: f_profile_scale = 9'd66; 4'd2: f_profile_scale = 9'd73; 4'd3: f_profile_scale = 9'd84; 4'd4: f_profile_scale = 9'd98; 4'd5: f_profile_scale = 9'd114; 4'd6: f_profile_scale = 9'd132; 4'd7: f_profile_scale = 9'd150; 4'd8: f_profile_scale = 9'd170; 4'd9: f_profile_scale = 9'd188; 4'd10: f_profile_scale = 9'd206; 4'd11: f_profile_scale = 9'd222; 4'd12: f_profile_scale = 9'd236; 4'd13: f_profile_scale = 9'd247; 4'd14: f_profile_scale = 9'd254; default: f_profile_scale = 9'd256; endcase end endfunction assign calibrated_mask = {cal_z, cal_y, cal_x}; assign fault_mask = {fault_z, fault_y, fault_x}; door_percent_planner u_planner ( .clk(clk), .rst_n(rst_n), .start(planner_start), .axis_id(planner_axis_id), .percent(planner_percent_i), .travel(planner_travel_i), .busy(planner_busy), .done(planner_done), .result(planner_result), .done_axis_id(planner_done_axis), .done_percent(planner_done_percent) ); reg grant_start; reg [1:0] grant_axis; reg [7:0] grant_percent; reg [12:0] grant_travel; reg [2:0] grant_hold; assign planner_start = grant_start; assign planner_axis_id = grant_axis; assign planner_percent_i = grant_percent; assign planner_travel_i = grant_travel; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin grant_start <= 1'b0; grant_axis <= 2'd0; grant_percent <= 8'd0; grant_travel <= 13'd0; grant_hold <= 3'b000; end else begin grant_start <= 1'b0; if (!req_x) grant_hold[0] <= 1'b0; if (!req_y) grant_hold[1] <= 1'b0; if (!req_z) grant_hold[2] <= 1'b0; if (!planner_busy && !grant_start) begin if (req_x && !grant_hold[0]) begin grant_start <= 1'b1; grant_hold[0] <= 1'b1; grant_axis <= 2'd0; grant_percent <= pct_req_x; grant_travel <= trav_req_x; end else if (req_y && !grant_hold[1]) begin grant_start <= 1'b1; grant_hold[1] <= 1'b1; grant_axis <= 2'd1; grant_percent <= pct_req_y; grant_travel <= trav_req_y; end else if (req_z && !grant_hold[2]) begin grant_start <= 1'b1; grant_hold[2] <= 1'b1; grant_axis <= 2'd2; grant_percent <= pct_req_z; grant_travel <= trav_req_z; end end end end door_motion_service_tick #(.SERVICE_CLKS(MOTION_SERVICE_CLKS)) u_svc ( .clk(clk), .rst_n(rst_n), .service_ce(service_ce) ); door_calibration_sequencer u_cal_seq ( .clk(clk), .rst_n(rst_n), .limit_sync_valid(limit_sync_valid), .term_x(term_x), .term_y(term_y), .term_z(term_z), .cal_ok_x(cal_x), .cal_ok_y(cal_y), .cal_ok_z(cal_z), .profile_busy(profile_busy), .profile_done(profile_done), .cal_enable(cal_enable), .profile_start(profile_start), .profile_ready(profile_ready), .all_axes_terminal(all_axes_terminal_unused) ); door_profile_quantum_builder #( .PHASE_FACTOR(PHASE_FACTOR) ) u_profile ( .clk(clk), .rst_n(rst_n), .start(profile_start), .travel_x(st_trav_x), .travel_y(st_trav_y), .travel_z(st_trav_z), .cal_ok_x(cal_x), .cal_ok_y(cal_y), .cal_ok_z(cal_z), .busy(profile_busy), .done(profile_done), .max_travel(max_travel), .phase_threshold(phase_threshold) ); wire [2:0] dda_active = {dda_run_z, dda_run_y, dda_run_x}; wire [2:0] dda_allow = {allow_z, allow_y, allow_x}; wire [2:0] dda_cal = {in_cal_z, in_cal_y, in_cal_x}; door_shared_dda_scheduler #( .CAL_PHASE_THRESHOLD(PHASE_FACTOR) ) u_dda ( .clk(clk), .rst_n(rst_n), .service_ce(service_ce), .active(dda_active), .allow_toggle(dda_allow), .cal_mode(dda_cal), .rate0(rq_x), .rate1(rq_y), .rate2(rq_z), .phase_threshold(phase_threshold), .toggle_pulse(toggle_pulse), .busy(dda_busy), .service_overrun(dda_overrun) ); wire [8:0] scale_x_w = f_profile_scale(crawl_x ? 4'd0 : lvl_x); wire [8:0] scale_y_w = f_profile_scale(crawl_y ? 4'd0 : lvl_y); wire [8:0] scale_z_w = f_profile_scale(crawl_z ? 4'd0 : lvl_z); // Round-robin shared rate builder with atomic launch record (no axis-tag skew). door_rate_quantum_dispatcher u_rate_disp ( .clk(clk), .rst_n(rst_n), .travel_x(st_trav_x), .travel_y(st_trav_y), .travel_z(st_trav_z), .scale_x(scale_x_w), .scale_y(scale_y_w), .scale_z(scale_z_w), .cal_x(in_cal_x), .cal_y(in_cal_y), .cal_z(in_cal_z), .busy(rate_busy), .done(rate_done), .rate_valid_x(rate_valid_x), .rate_valid_y(rate_valid_y), .rate_valid_z(rate_valid_z), .axis_id_out(rate_axis_done), .travel_out(rate_travel_done), .scale_out(rate_scale_done), .product(rate_result) ); // Silence unused capture ports (identity checked in dedicated TB). wire _unused_rate_tags = |{rate_travel_done, rate_scale_done, rate_busy}; door_axis_controller #( .AXIS_ID(0), .MAX_CAL_STEPS(MAX_CAL_STEPS), .MIN_TRAVEL(MIN_TRAVEL), .ENDPOINT_GUARD_EXTRA(ENDPOINT_GUARD_EXTRA), .DIR_SETUP_TICKS(DIR_SETUP_TICKS), .PERCENT_MAX(PERCENT_MAX), .CAL_RAMP_UNIT(CAL_RAMP_UNIT) ) u_axis_x ( .clk(clk), .rst_n(rst_n), .open_limit(open_limit[0]), .close_limit(close_limit[0]), .limit_sync_valid(limit_sync_valid), .cal_enable(cal_enable[0]), .gated_step(gated_step[0]), .gated_step_rise(gated_step_rise[0]), .tick_500us(tick_500us), .step_toggle_pulse(toggle_pulse[0]), .request_valid(motor_request_valid), .requested_percent_in(percent_x), .watchdog_stop(watchdog_stop), .rate_quantum_in(rate_result), .rate_quantum_valid(rate_valid_x), .planner_req(req_x), .planner_percent(pct_req_x), .planner_travel(trav_req_x), .planner_done(planner_done), .planner_done_axis(planner_done_axis), .planner_done_percent(planner_done_percent), .planner_result(planner_result), .step(step[0]), .dir_close(dir_close[0]), .status_state(st_state_x), .status_flags(st_flags_x), .status_requested_percent(st_pct_x), .status_current(st_cur_x), .status_target(st_tgt_x), .status_travel(st_trav_x), .status_fault_code(st_fault_x), .calibrated(cal_x), .faulted(fault_x), .terminal(term_x), .axis_moving(mov_x), .axis_allow_rise(allow_x), .axis_in_calibration(in_cal_x), .axis_endpoint_crawl(crawl_x), .axis_dda_run(dda_run_x), .profile_level(lvl_x), .profile_distance(profile_distance_unused_x), .profile_done_steps(profile_done_steps_unused_x), .profile_ramp_unit(profile_ramp_unit_unused_x), .rate_quantum(rq_x) ); door_axis_controller #( .AXIS_ID(1), .MAX_CAL_STEPS(MAX_CAL_STEPS), .MIN_TRAVEL(MIN_TRAVEL), .ENDPOINT_GUARD_EXTRA(ENDPOINT_GUARD_EXTRA), .DIR_SETUP_TICKS(DIR_SETUP_TICKS), .PERCENT_MAX(PERCENT_MAX), .CAL_RAMP_UNIT(CAL_RAMP_UNIT) ) u_axis_y ( .clk(clk), .rst_n(rst_n), .open_limit(open_limit[1]), .close_limit(close_limit[1]), .limit_sync_valid(limit_sync_valid), .cal_enable(cal_enable[1]), .gated_step(gated_step[1]), .gated_step_rise(gated_step_rise[1]), .tick_500us(tick_500us), .step_toggle_pulse(toggle_pulse[1]), .request_valid(motor_request_valid), .requested_percent_in(percent_y), .watchdog_stop(watchdog_stop), .rate_quantum_in(rate_result), .rate_quantum_valid(rate_valid_y), .planner_req(req_y), .planner_percent(pct_req_y), .planner_travel(trav_req_y), .planner_done(planner_done), .planner_done_axis(planner_done_axis), .planner_done_percent(planner_done_percent), .planner_result(planner_result), .step(step[1]), .dir_close(dir_close[1]), .status_state(st_state_y), .status_flags(st_flags_y), .status_requested_percent(st_pct_y), .status_current(st_cur_y), .status_target(st_tgt_y), .status_travel(st_trav_y), .status_fault_code(st_fault_y), .calibrated(cal_y), .faulted(fault_y), .terminal(term_y), .axis_moving(mov_y), .axis_allow_rise(allow_y), .axis_in_calibration(in_cal_y), .axis_endpoint_crawl(crawl_y), .axis_dda_run(dda_run_y), .profile_level(lvl_y), .profile_distance(profile_distance_unused_y), .profile_done_steps(profile_done_steps_unused_y), .profile_ramp_unit(profile_ramp_unit_unused_y), .rate_quantum(rq_y) ); door_axis_controller #( .AXIS_ID(2), .MAX_CAL_STEPS(MAX_CAL_STEPS), .MIN_TRAVEL(MIN_TRAVEL), .ENDPOINT_GUARD_EXTRA(ENDPOINT_GUARD_EXTRA), .DIR_SETUP_TICKS(DIR_SETUP_TICKS), .PERCENT_MAX(PERCENT_MAX), .CAL_RAMP_UNIT(CAL_RAMP_UNIT) ) u_axis_z ( .clk(clk), .rst_n(rst_n), .open_limit(open_limit[2]), .close_limit(close_limit[2]), .limit_sync_valid(limit_sync_valid), .cal_enable(cal_enable[2]), .gated_step(gated_step[2]), .gated_step_rise(gated_step_rise[2]), .tick_500us(tick_500us), .step_toggle_pulse(toggle_pulse[2]), .request_valid(motor_request_valid), .requested_percent_in(percent_z), .watchdog_stop(watchdog_stop), .rate_quantum_in(rate_result), .rate_quantum_valid(rate_valid_z), .planner_req(req_z), .planner_percent(pct_req_z), .planner_travel(trav_req_z), .planner_done(planner_done), .planner_done_axis(planner_done_axis), .planner_done_percent(planner_done_percent), .planner_result(planner_result), .step(step[2]), .dir_close(dir_close[2]), .status_state(st_state_z), .status_flags(st_flags_z), .status_requested_percent(st_pct_z), .status_current(st_cur_z), .status_target(st_tgt_z), .status_travel(st_trav_z), .status_fault_code(st_fault_z), .calibrated(cal_z), .faulted(fault_z), .terminal(term_z), .axis_moving(mov_z), .axis_allow_rise(allow_z), .axis_in_calibration(in_cal_z), .axis_endpoint_crawl(crawl_z), .axis_dda_run(dda_run_z), .profile_level(lvl_z), .profile_distance(profile_distance_unused_z), .profile_done_steps(profile_done_steps_unused_z), .profile_ramp_unit(profile_ramp_unit_unused_z), .rate_quantum(rq_z) ); // startup_complete only after serial cal terminal + profile ready always @(posedge clk or negedge rst_n) begin if (!rst_n) startup_complete <= 1'b0; else if (!startup_complete && profile_ready && term_x && term_y && term_z) startup_complete <= 1'b1; end reg [7:0] snap [0:30]; integer si; assign status_rd_data = (status_rd_addr < STATUS_PAYLOAD_LENGTH[7:0]) ? snap[status_rd_addr] : 8'd0; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin for (si = 0; si < STATUS_PAYLOAD_LENGTH; si = si + 1) snap[si] <= 8'd0; end else if (status_capture) begin snap[0] <= {watchdog_tripped, startup_complete, fault_mask, calibrated_mask}; snap[1] <= {5'd0, st_state_x}; snap[2] <= st_flags_x; snap[3] <= st_pct_x; snap[4] <= st_cur_x[7:0]; snap[5] <= {3'd0, st_cur_x[12:8]}; snap[6] <= st_tgt_x[7:0]; snap[7] <= {3'd0, st_tgt_x[12:8]}; snap[8] <= st_trav_x[7:0]; snap[9] <= {3'd0, st_trav_x[12:8]}; snap[10] <= st_fault_x; snap[11] <= {5'd0, st_state_y}; snap[12] <= st_flags_y; snap[13] <= st_pct_y; snap[14] <= st_cur_y[7:0]; snap[15] <= {3'd0, st_cur_y[12:8]}; snap[16] <= st_tgt_y[7:0]; snap[17] <= {3'd0, st_tgt_y[12:8]}; snap[18] <= st_trav_y[7:0]; snap[19] <= {3'd0, st_trav_y[12:8]}; snap[20] <= st_fault_y; snap[21] <= {5'd0, st_state_z}; snap[22] <= st_flags_z; snap[23] <= st_pct_z; snap[24] <= st_cur_z[7:0]; snap[25] <= {3'd0, st_cur_z[12:8]}; snap[26] <= st_tgt_z[7:0]; snap[27] <= {3'd0, st_tgt_z[12:8]}; snap[28] <= st_trav_z[7:0]; snap[29] <= {3'd0, st_trav_z[12:8]}; snap[30] <= st_fault_z; end end endmodule