`timescale 1ns/1ps // Fixed 4-slot TDM shared DDA. // One shared phase add/compare; background FSM refreshes rates (one restoring div). module door_shared_dda_executor #( parameter integer AXIS_COUNT = 3, parameter integer TRAVEL_WIDTH = 13, parameter integer VALUE_WIDTH = 22, parameter integer PHASE_WIDTH = 33, parameter integer CAL_PHASE_THRESHOLD = 800000 ) ( input wire clk, input wire rst_n, input wire [AXIS_COUNT-1:0] cal_active, input wire [AXIS_COUNT-1:0] run_active, input wire [AXIS_COUNT-1:0] allow_toggle, input wire [AXIS_COUNT-1:0] endpoint_crawl, input wire [TRAVEL_WIDTH-1:0] distance_0, input wire [TRAVEL_WIDTH-1:0] distance_1, input wire [TRAVEL_WIDTH-1:0] distance_2, input wire [TRAVEL_WIDTH-1:0] done_steps_0, input wire [TRAVEL_WIDTH-1:0] done_steps_1, input wire [TRAVEL_WIDTH-1:0] done_steps_2, input wire [TRAVEL_WIDTH-1:0] ramp_unit_0, input wire [TRAVEL_WIDTH-1:0] ramp_unit_1, input wire [TRAVEL_WIDTH-1:0] ramp_unit_2, input wire [TRAVEL_WIDTH-1:0] travel_0, input wire [TRAVEL_WIDTH-1:0] travel_1, input wire [TRAVEL_WIDTH-1:0] travel_2, input wire [31:0] phase_threshold, output reg [AXIS_COUNT-1:0] exec_toggle_pulse, output reg all_axes_overlap_seen ); reg [PHASE_WIDTH-1:0] phase0, phase1, phase2; reg [VALUE_WIDTH-1:0] rate0, rate1, rate2; reg [1:0] slot; wire svc_cal = (slot == 2'd0) ? cal_active[0] : (slot == 2'd1) ? cal_active[1] : cal_active[2]; wire svc_run = (slot == 2'd0) ? run_active[0] : (slot == 2'd1) ? run_active[1] : run_active[2]; wire svc_allow = (slot == 2'd0) ? allow_toggle[0] : (slot == 2'd1) ? allow_toggle[1] : allow_toggle[2]; wire svc_active = svc_run || svc_cal; wire [PHASE_WIDTH-1:0] svc_phase = (slot == 2'd0) ? phase0 : (slot == 2'd1) ? phase1 : phase2; wire [VALUE_WIDTH-1:0] svc_rate = (slot == 2'd0) ? rate0 : (slot == 2'd1) ? rate1 : rate2; wire [31:0] svc_thresh = svc_cal ? CAL_PHASE_THRESHOLD[31:0] : phase_threshold; wire [PHASE_WIDTH-1:0] svc_sum = svc_phase + {{(PHASE_WIDTH-VALUE_WIDTH){1'b0}}, svc_rate}; wire [PHASE_WIDTH-1:0] svc_thr = {{(PHASE_WIDTH-32){1'b0}}, svc_thresh}; wire svc_fire = svc_active && svc_allow && (svc_sum >= svc_thr); wire [PHASE_WIDTH-1:0] svc_next = svc_fire ? (svc_sum - svc_thr) : svc_sum; localparam [3:0] RS_LOAD = 4'd0, RS_PRELOAD = 4'd1, RS_DIV3 = 4'd2, RS_DIV2 = 4'd3, RS_DIV1 = 4'd4, RS_DIV0 = 4'd5, RS_LEVEL = 4'd6, RS_MUL = 4'd7, RS_WRITE = 4'd8; reg [3:0] rs; reg [1:0] rr_axis; reg wr_cal, wr_crawl; reg [TRAVEL_WIDTH-1:0] wr_dist, wr_done, wr_ramp, wr_travel; reg [TRAVEL_WIDTH-1:0] div_rem; reg [3:0] div_q, level_r; reg [VALUE_WIDTH-1:0] rate_r; function [7:0] f_profile_scale; input [3:0] level; begin case (level) 0: f_profile_scale = 8'd64; 1: f_profile_scale = 8'd66; 2: f_profile_scale = 8'd73; 3: f_profile_scale = 8'd84; 4: f_profile_scale = 8'd98; 5: f_profile_scale = 8'd114; 6: f_profile_scale = 8'd132; 7: f_profile_scale = 8'd150; 8: f_profile_scale = 8'd170; 9: f_profile_scale = 8'd188; 10: f_profile_scale = 8'd206; 11: f_profile_scale = 8'd222; 12: f_profile_scale = 8'd236; 13: f_profile_scale = 8'd247; 14: f_profile_scale = 8'd254; default: f_profile_scale = 8'd255; endcase end endfunction wire [16:0] den_ext = {4'b0, wr_ramp}; wire [16:0] rem_ext = {4'b0, div_rem}; wire [16:0] trial3 = den_ext << 3; wire [16:0] trial2 = den_ext << 2; wire [16:0] trial1 = den_ext << 1; wire take3 = (wr_ramp != 0) && (rem_ext >= trial3); wire take2 = (wr_ramp != 0) && (rem_ext >= trial2); wire take1 = (wr_ramp != 0) && (rem_ext >= trial1); wire take0 = (wr_ramp != 0) && (div_rem >= wr_ramp); wire [TRAVEL_WIDTH-1:0] rem_steps = (wr_dist > wr_done) ? (wr_dist - wr_done - 1'b1) : {TRAVEL_WIDTH{1'b0}}; wire [TRAVEL_WIDTH-1:0] min_prog = (wr_done < rem_steps) ? wr_done : rem_steps; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin slot <= 2'd0; phase0 <= {PHASE_WIDTH{1'b0}}; phase1 <= {PHASE_WIDTH{1'b0}}; phase2 <= {PHASE_WIDTH{1'b0}}; rate0 <= {VALUE_WIDTH{1'b0}}; rate1 <= {VALUE_WIDTH{1'b0}}; rate2 <= {VALUE_WIDTH{1'b0}}; exec_toggle_pulse <= {AXIS_COUNT{1'b0}}; all_axes_overlap_seen <= 1'b0; rs <= RS_LOAD; rr_axis <= 2'd0; wr_cal <= 1'b0; wr_crawl <= 1'b0; wr_dist <= {TRAVEL_WIDTH{1'b0}}; wr_done <= {TRAVEL_WIDTH{1'b0}}; wr_ramp <= {{(TRAVEL_WIDTH-1){1'b0}}, 1'b1}; wr_travel <= {TRAVEL_WIDTH{1'b0}}; div_rem <= {TRAVEL_WIDTH{1'b0}}; div_q <= 4'd0; level_r <= 4'd0; rate_r <= {VALUE_WIDTH{1'b0}}; end else begin exec_toggle_pulse <= {AXIS_COUNT{1'b0}}; slot <= slot + 1'b1; if (run_active[0] && run_active[1] && run_active[2]) all_axes_overlap_seen <= 1'b1; if (slot != 2'd3) begin if (svc_active && svc_allow) begin case (slot) 2'd0: begin phase0 <= svc_next; exec_toggle_pulse[0] <= svc_fire; end 2'd1: begin phase1 <= svc_next; exec_toggle_pulse[1] <= svc_fire; end default: begin phase2 <= svc_next; exec_toggle_pulse[2] <= svc_fire; end endcase end else if (!svc_active) begin case (slot) 2'd0: phase0 <= {PHASE_WIDTH{1'b0}}; 2'd1: phase1 <= {PHASE_WIDTH{1'b0}}; default: phase2 <= {PHASE_WIDTH{1'b0}}; endcase end end case (rs) RS_LOAD: begin case (rr_axis) 2'd0: begin wr_cal <= cal_active[0]; wr_crawl <= endpoint_crawl[0]; wr_dist <= distance_0; wr_done <= done_steps_0; wr_ramp <= (ramp_unit_0 == 0) ? {{(TRAVEL_WIDTH-1){1'b0}}, 1'b1} : ramp_unit_0; wr_travel <= travel_0; end 2'd1: begin wr_cal <= cal_active[1]; wr_crawl <= endpoint_crawl[1]; wr_dist <= distance_1; wr_done <= done_steps_1; wr_ramp <= (ramp_unit_1 == 0) ? {{(TRAVEL_WIDTH-1){1'b0}}, 1'b1} : ramp_unit_1; wr_travel <= travel_1; end default: begin wr_cal <= cal_active[2]; wr_crawl <= endpoint_crawl[2]; wr_dist <= distance_2; wr_done <= done_steps_2; wr_ramp <= (ramp_unit_2 == 0) ? {{(TRAVEL_WIDTH-1){1'b0}}, 1'b1} : ramp_unit_2; wr_travel <= travel_2; end endcase div_q <= 4'd0; if (!((rr_axis == 2'd0) ? (run_active[0] || cal_active[0]) : (rr_axis == 2'd1) ? (run_active[1] || cal_active[1]) : (run_active[2] || cal_active[2]))) begin rate_r <= {VALUE_WIDTH{1'b0}}; rs <= RS_WRITE; end else rs <= RS_PRELOAD; end RS_PRELOAD: begin div_rem <= min_prog; div_q <= 4'd0; rs <= RS_DIV3; end RS_DIV3: begin if (take3) begin div_rem <= div_rem - trial3[TRAVEL_WIDTH-1:0]; div_q <= div_q | 4'h8; end rs <= RS_DIV2; end RS_DIV2: begin if (take2) begin div_rem <= div_rem - trial2[TRAVEL_WIDTH-1:0]; div_q <= div_q | 4'h4; end rs <= RS_DIV1; end RS_DIV1: begin if (take1) begin div_rem <= div_rem - trial1[TRAVEL_WIDTH-1:0]; div_q <= div_q | 4'h2; end rs <= RS_DIV0; end RS_DIV0: begin if (take0) begin div_rem <= div_rem - wr_ramp; div_q <= div_q | 4'h1; end rs <= RS_LEVEL; end RS_LEVEL: begin if (wr_crawl) level_r <= 4'd0; else if (wr_cal) level_r <= (wr_done[12:3] > 13'd15) ? 4'd15 : wr_done[6:3]; else if (wr_dist == 0) level_r <= 4'd0; else level_r <= div_q; rs <= RS_MUL; end RS_MUL: begin if (wr_cal) rate_r <= {{(VALUE_WIDTH-8){1'b0}}, f_profile_scale(level_r)}; else rate_r <= wr_travel * f_profile_scale(level_r); rs <= RS_WRITE; end RS_WRITE: begin case (rr_axis) 2'd0: rate0 <= rate_r; 2'd1: rate1 <= rate_r; default: rate2 <= rate_r; endcase rr_axis <= (rr_axis == 2'd2) ? 2'd0 : (rr_axis + 1'b1); rs <= RS_LOAD; end default: rs <= RS_LOAD; endcase end end endmodule