`timescale 1ns/1ps // RECONSTRUCTED Scheme A1 sequential shared DDA (pending-queue + drain parity). // Provenance: recovered from agent transcript 7c830c69 (Round-2 A1 implementation) // matching yosys-a1-dda.stat signals (state/service_pending/f_fire/f_next). // Label: reconstructed — must pass same functional tests as A2 before fair TD. // Drain semantics (Round-4): phase always wraps when active && sum>=thr; // toggle fires on wrap without allow_toggle (axis helper gates rises; high drain // must still receive pulses). service_pending sticky + overrun when busy. module door_shared_dda_scheduler #( parameter integer TRAVEL_WIDTH = 13, parameter integer PHASE_WIDTH = 26, parameter integer RATE_WIDTH = 21, parameter integer THRESH_WIDTH = 25, parameter integer CAL_PHASE_THRESHOLD = 5120 ) ( input wire clk, input wire rst_n, input wire service_ce, input wire [2:0] active, input wire [2:0] allow_toggle, input wire [2:0] cal_mode, input wire [RATE_WIDTH-1:0] rate0, input wire [RATE_WIDTH-1:0] rate1, input wire [RATE_WIDTH-1:0] rate2, input wire [THRESH_WIDTH-1:0] phase_threshold, output reg [2:0] toggle_pulse, output reg busy, output wire service_overrun ); localparam [1:0] ST_IDLE = 2'd0, ST_X = 2'd1, ST_Y = 2'd2, ST_Z = 2'd3; reg [1:0] state; reg [PHASE_WIDTH-1:0] phase0, phase1, phase2; reg service_pending; reg overrun_r; wire _unused_allow = |allow_toggle; assign service_overrun = overrun_r; wire [PHASE_WIDTH-1:0] thr_run = {{(PHASE_WIDTH-THRESH_WIDTH){1'b0}}, phase_threshold}; wire [PHASE_WIDTH-1:0] thr_cal = CAL_PHASE_THRESHOLD[PHASE_WIDTH-1:0]; // Phase always advances/wraps when enabled; allow only gates fire. function automatic [PHASE_WIDTH-1:0] f_next; input [PHASE_WIDTH-1:0] ph; input [RATE_WIDTH-1:0] rate; input [PHASE_WIDTH-1:0] thr; input en; reg [PHASE_WIDTH-1:0] sum; begin if (!en) begin f_next = {PHASE_WIDTH{1'b0}}; end else begin sum = ph + {{(PHASE_WIDTH-RATE_WIDTH){1'b0}}, rate}; if (sum >= thr) f_next = sum - thr; else f_next = sum; end end endfunction // Fire on wrap whenever active. Axis helper gates rises via allow_rise and // always accepts a pulse to drain STEP high (!allow must not stick high). function automatic f_fire; input [PHASE_WIDTH-1:0] ph; input [RATE_WIDTH-1:0] rate; input [PHASE_WIDTH-1:0] thr; input en; reg [PHASE_WIDTH-1:0] sum; begin sum = ph + {{(PHASE_WIDTH-RATE_WIDTH){1'b0}}, rate}; f_fire = en && (sum >= thr); end endfunction always @(posedge clk or negedge rst_n) begin if (!rst_n) begin state <= ST_IDLE; phase0 <= {PHASE_WIDTH{1'b0}}; phase1 <= {PHASE_WIDTH{1'b0}}; phase2 <= {PHASE_WIDTH{1'b0}}; toggle_pulse <= 3'b000; busy <= 1'b0; service_pending <= 1'b0; overrun_r <= 1'b0; end else begin toggle_pulse <= 3'b000; // Always latch the request. Short SERVICE_CLKS TBs (e.g. 4) would // otherwise drop pulses while the 3-cycle X→Y→Z walk is busy. if (service_ce) begin if (busy) overrun_r <= 1'b1; service_pending <= 1'b1; end case (state) ST_IDLE: begin busy <= 1'b0; if (service_pending) begin service_pending <= 1'b0; busy <= 1'b1; state <= ST_X; end end ST_X: begin begin : ax0 reg [PHASE_WIDTH-1:0] thr; thr = cal_mode[0] ? thr_cal : thr_run; toggle_pulse[0] <= f_fire(phase0, rate0, thr, active[0]); phase0 <= f_next(phase0, rate0, thr, active[0]); end state <= ST_Y; end ST_Y: begin begin : ax1 reg [PHASE_WIDTH-1:0] thr; thr = cal_mode[1] ? thr_cal : thr_run; toggle_pulse[1] <= f_fire(phase1, rate1, thr, active[1]); phase1 <= f_next(phase1, rate1, thr, active[1]); end state <= ST_Z; end default: begin begin : ax2 reg [PHASE_WIDTH-1:0] thr; thr = cal_mode[2] ? thr_cal : thr_run; toggle_pulse[2] <= f_fire(phase2, rate2, thr, active[2]); phase2 <= f_next(phase2, rate2, thr, active[2]); end if (service_pending) begin service_pending <= 1'b0; busy <= 1'b1; state <= ST_X; end else begin busy <= 1'b0; state <= ST_IDLE; end end endcase end end endmodule