`timescale 1ns/1ps `include "generated/mrdimm/prod2u_mrdimm_params.vh" // MRDIMM shared motor engine (2026 contract, section 10). // // AXIS_COUNT axes share one queue BRAM, one batch-staging buffer, one // event ring and one 1 us time base. Each axis owns a motor_axis_core // whose final STEP decision runs every clock (limits -> abort/preempt -> // scheduler). X/Y/Z map to axis 0/1/2. // // Queue: 32 x 16-byte waiting slots per axis plus one urgent (preempt) // preinstall slot per axis. A batch is validated in full (fields, per // axis space, preempt/clear placement) before any state changes; the // per-axis depth/tail state is then published in one register write, so // a rejected batch changes nothing and an accepted batch never shows a // half-published queue. module motor_engine #( parameter integer AXIS_COUNT = 3, parameter integer PARALLEL_TELEMETRY=1, // default half periods in us: X/Y 1000 Hz -> 500 us, Z 25 Hz -> 20000 us parameter [31:0] X_DEFAULT_HALF_US = 32'd500, parameter [31:0] Y_DEFAULT_HALF_US = 32'd500, parameter [31:0] Z_DEFAULT_HALF_US = 32'd20000, parameter [31:0] X_MIN_HALF_US = 32'd500, parameter [31:0] Y_MIN_HALF_US = 32'd500, parameter [31:0] Z_MIN_HALF_US = 32'd20000, parameter integer DIR_SETUP_TICKS = 1000, parameter DIR_LOWER_LEVEL = 1'b0 ) ( input wire clk, input wire rst_n, input wire tick_us, input wire [AXIS_COUNT-1:0] limit_lower_raw, input wire [AXIS_COUNT-1:0] limit_upper_raw, // MOTOR_QUEUE_WRITE staging (slot*16+offset bytes) then submit input wire qw_byte_we, input wire [6:0] qw_byte_addr, // 0..127 input wire [7:0] qw_byte_data, input wire qw_submit, input wire [3:0] qw_count, // 1..8 output reg qw_accept, output reg [7:0] qw_status, output reg [15:0] qw_detail, // MOTOR_CONTROL: provider holds all fields from ctl_valid through ctl_accept. input wire ctl_valid, input wire [2:0] ctl_op, input wire [15:0] ctl_axis_mask, input wire [7:0] ctl_flags, input wire [31:0] ctl_value, output reg ctl_accept, output reg [7:0] ctl_status, output reg [15:0] ctl_detail, // startup: one pulse submits internal SEEK_LOWER for every axis input wire startup_seek, // event ring host view input wire [7:0] ev_rd_index, input wire ev_hold, output wire [127:0] ev_rd_data, input wire [3:0] ev_rd_byte_index, output wire [7:0] ev_rd_byte_data, output wire [7:0] ev_count, output wire [7:0] ev_overflow, output wire ev_view_ready, output wire [7:0] ev_view_count, output reg [31:0] event_pending_overflow, output wire [31:0] ev_time_us, output wire [31:0] ev_time_high,event_ring_overflow, output wire any_limit_live, output wire motor_telemetry_ready,motor_scan_deadline_miss, output reg [15:0] motor_status_epoch, input wire ev_clear_n, input wire [7:0] ev_clear_count, output wire [AXIS_COUNT*`MR_AXIS_RECORD_BYTES*8-1:0] axis_telemetry, input wire [4:0] telemetry_word_addr, output wire [3:0] telemetry_axis, output wire [15:0] telemetry_word_data, output wire [AXIS_COUNT-1:0] step_out, output wire [AXIS_COUNT-1:0] dir_out ); localparam [2:0] M_REL = 3'd0, M_SEEK_LOWER = 3'd1, M_SEEK_UPPER = 3'd2, M_CONT = 3'd3, M_HOLD = 3'd4; localparam [2:0] OP_START_ARMED = 3'd0, OP_ABORT = 3'd1, OP_CLEAR_QUEUE = 3'd2, OP_CLEAR_COUNTERS = 3'd3, OP_SET_POSITION = 3'd4, OP_CLEAR_LIMIT_LATCH = 3'd5, OP_HOME_LOWER = 3'd6, OP_SEEK_UPPER = 3'd7; localparam [7:0] ST_OK = 8'd0, ST_BAD_VALUE = 8'd3, ST_BAD_AXIS = 8'd6; localparam [7:0] EV_SEGMENT = 8'd1, EV_LOWER = 8'd2, EV_UPPER = 8'd3, EV_ABORT = 8'd4, EV_PREEMPT = 8'd5, EV_BOTH = 8'd7, EV_HOST_CAP = 8'd8, EV_HOLD = 8'd9, EV_HOME = 8'd10; localparam [3:0] SR_LOWER = 4'd2, SR_UPPER = 4'd3; genvar g; integer i; localparam integer AXIS_INDEX_BITS=(AXIS_COUNT<=1) ? 1 : $clog2(AXIS_COUNT); localparam integer AXIS_COUNTER_BITS=$clog2(AXIS_COUNT+1); // ---- time base ---- reg [63:0] time_us; always @(posedge clk or negedge rst_n) begin if (!rst_n) time_us <= 64'd0; else if (tick_us) time_us <= time_us + 64'd1; end assign ev_time_us = time_us[31:0]; assign ev_time_high = time_us[63:32]; // ---- limit 2FF synchronization ---- reg [AXIS_COUNT-1:0] ll_s1, ll_s2, ul_s1, ul_s2; assign any_limit_live=(|ll_s2) || (|ul_s2); always @(posedge clk or negedge rst_n) begin if (!rst_n) begin ll_s1 <= {AXIS_COUNT{1'b0}}; ll_s2 <= {AXIS_COUNT{1'b0}}; ul_s1 <= {AXIS_COUNT{1'b0}}; ul_s2 <= {AXIS_COUNT{1'b0}}; end else begin ll_s1 <= limit_lower_raw; ll_s2 <= ll_s1; ul_s1 <= limit_upper_raw; ul_s2 <= ul_s1; end end // ---- shared queue RAM ---- localparam QAW = $clog2(AXIS_COUNT * 33); reg [QAW-1:0] q_wr_addr, q_rd_addr; reg [127:0] q_wr_data; reg q_we,q_re; wire [127:0] q_rd; wire q_write_busy,q_read_valid; motor_queue_storage #(.SLOTS(AXIS_COUNT*33)) u_queue ( .clk(clk),.rst_n(rst_n),.wr_req(q_we),.wr_slot(q_wr_addr),.wr_data(q_wr_data), .wr_busy(q_write_busy),.rd_req(q_re),.rd_slot(q_rd_addr),.rd_data(q_rd),.rd_valid(q_read_valid) ); // ---- per-axis queue state ---- reg [5:0] depth_r [0:AXIS_COUNT-1]; // waiting slots 0..32 reg [5:0] head_r [0:AXIS_COUNT-1]; // oldest waiting slot reg queue_paused_r [0:AXIS_COUNT-1]; reg urgent_valid_r [0:AXIS_COUNT-1]; reg urgent_from_queue [0:AXIS_COUNT-1]; reg home_pending_r [0:AXIS_COUNT-1]; // ---- byte-wide batch BRAM and one shared segment assembly register ---- // Validate every segment before publishing queue state. Two clocks per // byte hide the synchronous RAM latency; STEP scanning never waits here. reg [3:0] qc_index; reg [3:0] stage_byte_index; reg stage_for_write; reg [127:0] stage_data; wire [7:0] stage_read_data; ef2_bram_dp #(.DATA_WIDTH(8),.ADDR_WIDTH(7),.DATA_DEPTH(128)) u_stage ( .clk(clk),.a_en(qw_byte_we),.a_we(qw_byte_we),.a_addr(qw_byte_addr), .a_wdata(qw_byte_data),.a_rdata(),.b_en(1'b1),.b_we(1'b0), .b_addr({qc_index[2:0],stage_byte_index}),.b_wdata(8'd0),.b_rdata(stage_read_data) ); // segment field slices — wire segment byte order (contract 10.2): // byte0 axis, byte1 mode, byte2 flags, byte3 reserved, // bytes 4..7 count, bytes 8..11 half, bytes 12..13 tag, bytes 14..15 reserved function [7:0] f_axis; input [127:0] s; f_axis = s[7:0]; endfunction function [7:0] f_mode; input [127:0] s; f_mode = s[15:8]; endfunction function f_upper; input [127:0] s; f_upper = s[16]; endfunction function f_preempt; input [127:0] s; f_preempt = s[17]; endfunction function f_arm; input [127:0] s; f_arm = s[18]; endfunction function f_clear; input [127:0] s; f_clear = s[19]; endfunction function [31:0] f_count; input [127:0] s; f_count = s[63:32]; endfunction function [31:0] f_half; input [127:0] s; f_half = s[95:64]; endfunction function [15:0] f_tag; input [127:0] s; f_tag = s[111:96]; endfunction function seg_field_bad; input [127:0] s; input [31:0] min_half; begin seg_field_bad = (f_axis(s) >= AXIS_COUNT) || (f_mode(s) == M_REL && f_count(s) == 32'd0) || (f_mode(s) == M_HOLD && (f_half(s) != 32'd0)) || (f_mode(s) != M_HOLD && f_half(s) == 32'd0) || ((f_mode(s) == M_REL) || (f_mode(s) == M_CONT) || (f_mode(s) == M_SEEK_LOWER) || (f_mode(s) == M_SEEK_UPPER)) && (f_half(s) < min_half) || (f_mode(s) > M_HOLD) || (s[23:20] != 4'd0) || (s[31:24] != 8'd0) || (s[127:112] != 16'd0); end endfunction // ---- batch validate/commit FSM ---- localparam [3:0] QC_IDLE=0, QC_QUIESCE=1, QC_VALIDATE=2, QC_SPACE=3, QC_WRITE=4, QC_DRAIN=5, QC_PUBLISH=6, QC_STAGE_WAIT=7, QC_STAGE_READ=8; reg [3:0] qc_state; reg [3:0] qc_count; reg [7:0] qc_is_urgent; reg [5:0] qc_old_depth [0:AXIS_COUNT-1]; reg [5:0] qc_new_count [0:AXIS_COUNT-1]; reg [4:0] qc_new_head [0:AXIS_COUNT-1]; reg [AXIS_COUNT-1:0] qc_resume, start_credit; reg qc_space_bad; integer space_axis; always @* begin qc_space_bad=0; for(space_axis=0;space_axis32) qc_space_bad=1; end reg [5:0] qc_proj_depth [0:AXIS_COUNT-1]; // projected waiting depth reg [5:0] qc_append_pos [0:AXIS_COUNT-1]; // next free slot per axis reg [AXIS_COUNT-1:0] qc_first_of_axis; reg [AXIS_COUNT-1:0] qc_want_preempt; // first-seg preempt request per axis reg [AXIS_COUNT-1:0] qc_want_clear; wire [31:0] stage_min_half = (f_axis(stage_data) == 2'd0) ? X_MIN_HALF_US : (f_axis(stage_data) == 2'd1) ? Y_MIN_HALF_US : Z_MIN_HALF_US; wire [AXIS_INDEX_BITS-1:0] qc_axis = f_axis(stage_data); // ---- execute fetch FSM ---- localparam [2:0] FX_IDLE=0, FX_RD=1, FX_LOAD=2, FX_WAIT=3, FX_CONSUME=4; reg [2:0] fx_state; reg [AXIS_INDEX_BITS-1:0] fx_axis; reg [AXIS_INDEX_BITS-1:0] fx_round; integer fx_k,fx_probe; reg [AXIS_COUNT-1:0] fx_eligible; reg [7:0] fx_candidate; reg fx_candidate_valid; reg fx_from_urgent; reg [127:0] fx_seg; // ---- event writer ---- reg ev_wr_req; reg [3:0] ev_axis; reg [7:0] ev_code; reg [15:0] ev_tag; reg [31:0] ev_pos; reg [31:0] ev_total; reg [31:0] ev_timestamp; wire ev_ack; // ---- control/startup one-shot staging ---- // The command provider owns this context until acceptance. In particular, // SET_POSITION does not acknowledge before all pending scans consume value. wire [2:0] cs_op=ctl_op; wire [15:0] cs_mask=ctl_axis_mask; wire [31:0] cs_value=ctl_value; wire [7:0] cs_flags=ctl_flags; reg [1:0] cs_state; // 0 idle, 1 exec, 2 done-pulse wire seek_context=cs_state==2 && (cs_op==OP_HOME_LOWER || cs_op==OP_SEEK_UPPER); wire [AXIS_COUNT-1:0] internal_urgent_pending; reg [AXIS_COUNT-1:0] cs_start_mask; reg [1:0] cs_axis; reg cs_home; reg [AXIS_COUNTER_BITS-1:0] su_axis; // includes terminal AXIS_COUNT reg su_wait; // ---- axis cores ---- wire [AXIS_COUNT-1:0] ax_active, ax_armed, ax_load_ready, ax_ref_seen, ax_done; wire [3:0] ax_reason [0:AXIS_COUNT-1]; wire [15:0] ax_dtag [0:AXIS_COUNT-1]; wire [31:0] ax_dexec [0:AXIS_COUNT-1]; wire [31:0] ax_dpos [0:AXIS_COUNT-1],ax_dtotal [0:AXIS_COUNT-1],ax_dtime [0:AXIS_COUNT-1]; wire [31:0] ax_pos [0:AXIS_COUNT-1]; wire [31:0] ax_total [0:AXIS_COUNT-1]; wire [31:0] ax_lhits [0:AXIS_COUNT-1]; wire [31:0] ax_uhits [0:AXIS_COUNT-1]; wire [`MR_AXIS_RECORD_BYTES*8-1:0] ax_record [0:AXIS_COUNT-1]; wire [AXIS_COUNT-1:0] ax_llat, ax_ulat, ax_both,ax_limit_pulse; wire [127:0] ax_limit_data,ax_done_word; wire [31:0] ax_limit_time_high; wire [AXIS_COUNT*5-1:0] ax_limit_drops; wire [3:0] ax_last_reason [0:AXIS_COUNT-1]; wire [AXIS_COUNT-1:0] step_out_w, dir_out_w; assign step_out = step_out_w; assign dir_out = dir_out_w; reg [AXIS_COUNT-1:0] ax_seg_load; wire [AXIS_COUNT-1:0] ax_seg_upper,ax_seg_armed,ax_seg_consumed; wire [2:0] ax_seg_mode [0:AXIS_COUNT-1]; wire [31:0] ax_seg_count [0:AXIS_COUNT-1],ax_seg_half [0:AXIS_COUNT-1]; wire [15:0] ax_seg_tag [0:AXIS_COUNT-1]; // FX_CONSUME holds the fetched descriptor until the scanner accepts it. // Startup uses the same owner; no second wide descriptor copy is needed. wire [2:0] load_mode=fx_seg[10:8]; wire load_upper=fx_seg[16],load_armed=fx_seg[18]; wire [31:0] load_count=fx_seg[63:32],load_half=fx_seg[95:64]; wire [15:0] load_tag=fx_seg[111:96]; reg [AXIS_COUNT-1:0] ax_start_armed, ax_abort, ax_preempt; reg [AXIS_COUNT-1:0] ax_pos_load, ax_ctr_clear, ax_pos_zero, ax_lat_clear; wire [AXIS_COUNT*3-1:0] bank_seg_mode; wire [AXIS_COUNT*32-1:0] bank_seg_count; wire [AXIS_COUNT*32-1:0] bank_seg_half_us; wire [AXIS_COUNT*16-1:0] bank_seg_tag; wire [AXIS_COUNT*32-1:0] bank_position_value; wire [AXIS_COUNT*4-1:0] bank_done_reason; wire [AXIS_COUNT*16-1:0] bank_done_tag; wire [AXIS_COUNT*32-1:0] bank_done_executed; wire [AXIS_COUNT*32-1:0] bank_done_position; wire [AXIS_COUNT*32-1:0] bank_done_total; wire [AXIS_COUNT*32-1:0] bank_done_time; wire [AXIS_COUNT*32-1:0] bank_position; wire [AXIS_COUNT*32-1:0] bank_total_steps; wire [AXIS_COUNT*32-1:0] bank_lower_hit_count; wire [AXIS_COUNT*32-1:0] bank_upper_hit_count; wire [AXIS_COUNT*4-1:0] bank_last_stop_reason; wire [AXIS_COUNT*`MR_AXIS_RECORD_BYTES*8-1:0] bank_telemetry_record; wire bank_telemetry_ready,bank_telemetry_changing; wire [3:0] bank_telemetry_axis; wire [15:0] bank_telemetry_word; wire [15:0] queue_depth_word={2'd0,depth_r[telemetry_axis],8'd0}; wire [15:0] queue_pause_word=queue_paused_r[telemetry_axis] ? (16'd1<<`MR_AXIS_FLAG_QUEUE_PAUSED_BIT) : 16'd0; // Parallel copy is diagnostic only; production has one addressed bank. reg [159:0] limit_record [0:AXIS_COUNT-1]; reg [AXIS_COUNT-1:0] limit_history_valid; integer ev_select,ev_scan,ev_i; reg select_limit,reset_pending; // One physical read address serves retained history and event dispatch. // The history scanner pauses for one dispatch cycle, then resumes at // its own axis, so event traffic cannot skip a status axis indefinitely. reg [AXIS_INDEX_BITS-1:0] history_scan_axis; wire limit_dispatch=!ev_wr_req && !reset_pending && ev_select>=0 && select_limit; wire [AXIS_INDEX_BITS-1:0] limit_read_axis=limit_dispatch ? ev_select[AXIS_INDEX_BITS-1:0] : history_scan_axis; wire [159:0] limit_read_record; motor_limit_history #(.AXIS_COUNT(AXIS_COUNT)) u_limit_history( .clk(clk),.write_enable(rst_n && (|ax_limit_pulse)), .write_axis(limit_write_axis[AXIS_INDEX_BITS-1:0]),.write_data({ax_limit_time_high,ax_limit_data}), .read_axis(limit_read_axis),.read_data(limit_read_record)); wire [159:0] status_limit_record=limit_history_valid[limit_read_axis] ? limit_read_record : 160'd0; reg [31:0] history_pair; wire history_selected=telemetry_word_addr>=`MR_AXIS_LAST_LIMIT_POSITION_OFFSET/2 && telemetry_word_addr<(`MR_AXIS_LAST_LIMIT_TIME_HIGH_OFFSET+4)/2; assign telemetry_axis=history_selected ? limit_read_axis : bank_telemetry_axis; always @(posedge clk or negedge rst_n)begin if(!rst_n)history_scan_axis<=0; else if(!limit_dispatch)history_scan_axis<=history_scan_axis==AXIS_COUNT-1 ? 0 : history_scan_axis+1'b1; end always @* begin case(telemetry_word_addr[4:1]) `MR_AXIS_LAST_LIMIT_POSITION_OFFSET/4:history_pair=status_limit_record[95:64]; `MR_AXIS_LAST_LIMIT_TOTAL_OFFSET/4:history_pair=status_limit_record[127:96]; `MR_AXIS_LAST_LIMIT_TAG_OFFSET/4:history_pair={status_limit_record[47:40],8'd0,status_limit_record[63:48]}; `MR_AXIS_LAST_LIMIT_TIME_LOW_OFFSET/4:history_pair=status_limit_record[31:0]; `MR_AXIS_LAST_LIMIT_TIME_HIGH_OFFSET/4:history_pair=status_limit_record[159:128]; default:history_pair=0; endcase end wire [15:0] history_word=telemetry_word_addr[0] ? history_pair[31:16] : history_pair[15:0]; // Stop reason remains axis-owned in the low byte of the event/reason word. wire [15:0] history_stop_word=telemetry_word_addr==`MR_AXIS_LAST_STOP_REASON_OFFSET/2 ? {12'd0,ax_last_reason[limit_read_axis]} : 16'd0; assign telemetry_word_data=history_selected ? (history_word | history_stop_word) : (bank_telemetry_word | (telemetry_word_addr==0 ? queue_pause_word : (telemetry_word_addr==1 ? queue_depth_word : 16'd0))); wire status_changing=bank_telemetry_changing || qc_state!=QC_IDLE || cs_state!=0 || fx_state!=FX_IDLE || qw_submit || ctl_valid || startup_seek || su_axis=0;fx_k=fx_k-1)begin fx_probe=fx_round+fx_k; if(fx_probe>=AXIS_COUNT)fx_probe=fx_probe-AXIS_COUNT; if(fx_eligible[fx_probe])begin fx_candidate=fx_probe[7:0];fx_candidate_valid=1;end end end always @* begin ev_select=-1;select_limit=0;pending_losses=0; for(ev_scan=AXIS_COUNT-1;ev_scan>=0;ev_scan=ev_scan-1) if(done_pending[ev_scan]) ev_select=ev_scan; for(ev_scan=AXIS_COUNT-1;ev_scan>=0;ev_scan=ev_scan-1) if(limit_pending[ev_scan]) begin ev_select=ev_scan;select_limit=1;end for(ev_scan=0;ev_scan=0) begin ev_wr_req<=1;ev_axis<=selected_record[35:32];ev_timestamp<=selected_record[31:0];ev_code<=selected_record[47:40]; ev_tag<=selected_record[63:48];ev_pos<=selected_record[95:64];ev_total<=selected_record[127:96]; if(select_limit)limit_pending[ev_select]<=0;else done_pending[ev_select]<=0; end for(ev_i=0;ev_i8) begin qw_accept<=1;qw_status<=ST_BAD_VALUE;qw_detail<=0; end else begin qc_count<=qw_count;qc_state<=QC_QUIESCE;end end else if(qc_state==QC_QUIESCE && fx_state==FX_IDLE && ax_seg_load==0) begin qc_index<=0;qc_is_urgent<=0; for(i=0;i=AXIS_COUNT || f_mode(stage_data)>M_HOLD || seg_field_bad(stage_data,stage_min_half) || (!qc_first_of_axis[qc_axis] && (f_preempt(stage_data) || f_clear(stage_data)))) begin qw_accept<=1;qw_detail<=qc_index;qc_state<=QC_IDLE; qw_status<=(f_axis(stage_data)>=AXIS_COUNT) ? 8'd6 : ((f_mode(stage_data)>M_HOLD) ? 8'd7 : ST_BAD_VALUE); end else begin if(qc_first_of_axis[qc_axis]) begin qc_resume[qc_axis]<=!f_arm(stage_data); qc_want_clear[qc_axis]<=f_clear(stage_data); if(f_clear(stage_data)) qc_old_depth[qc_axis]<=0; qc_want_preempt[qc_axis]<=f_preempt(stage_data); if(f_preempt(stage_data)) begin qc_is_urgent[qc_index]<=1; end else qc_new_count[qc_axis]<=qc_new_count[qc_axis]+1'b1; qc_first_of_axis[qc_axis]<=0; end else qc_new_count[qc_axis]<=qc_new_count[qc_axis]+1'b1; qc_index<=qc_index+1'b1;stage_byte_index<=0; qc_state<=qc_index+1'b1==qc_count ? QC_SPACE : QC_STAGE_WAIT; end end else if(qc_state==QC_SPACE) begin if(qc_space_bad) begin qw_accept<=1;qw_status<=8'd5;qw_detail<=0;qc_state<=QC_IDLE; end else begin for(i=0;i=AXIS_COUNT && !startup_seek) begin if (urgent_valid_r[fx_candidate]) begin fx_axis <= fx_candidate[AXIS_INDEX_BITS-1:0]; fx_from_urgent <= 1'b1; if(urgent_from_queue[fx_candidate])begin fx_state<=FX_RD;q_rd_addr<=AXIS_COUNT*32+fx_candidate;q_re<=1; end else begin // The accepted seek context is held until all // selected internal descriptors are consumed. fx_state<=FX_LOAD; fx_seg<={32'd0,(cs_value!=0 ? cs_value : (fx_candidate==0 ? X_DEFAULT_HALF_US : (fx_candidate==1 ? Y_DEFAULT_HALF_US : Z_DEFAULT_HALF_US))), 32'd0,8'd0,8'd0,(cs_op==OP_HOME_LOWER ? 8'd1 : 8'd2),fx_candidate[7:0]}; end end else if (depth_r[fx_candidate] != 0) begin fx_state <= FX_RD; fx_axis <= fx_candidate[AXIS_INDEX_BITS-1:0]; fx_from_urgent <= 1'b0; q_rd_addr <= fx_candidate * 32 + head_r[fx_candidate][4:0]; q_re<=1; end end end end FX_RD: begin fx_state <= FX_WAIT; end FX_WAIT: if(q_read_valid)begin fx_seg <= q_rd; fx_state <= FX_LOAD; end FX_LOAD: begin ax_seg_load[fx_axis] <= 1'b1; // Resolve ARM once using the old credit, before clearing it. fx_seg[18] <= cs_state==3 ? 1'b1 : (f_arm(fx_seg) && !start_credit[fx_axis]); start_credit[fx_axis] <= 0; if (fx_from_urgent) urgent_valid_r[fx_axis] <= 1'b0; else begin // pop head head_r[fx_axis] <= (head_r[fx_axis] == 31) ? 6'd0 : head_r[fx_axis] + 6'd1; depth_r[fx_axis] <= depth_r[fx_axis] - 6'd1; // Each head's ARM bit is loaded into the axis; // the pause latch belongs to Abort/limit, not ARM. end fx_state <= FX_CONSUME; fx_round<=fx_axis==AXIS_COUNT-1 ? 0 : fx_axis+1'b1; end FX_CONSUME:if(ax_seg_consumed[fx_axis])fx_state<=FX_IDLE; default: fx_state <= FX_IDLE; endcase // ---- MOTOR_CONTROL: validate the full mask before any effect ---- if (ctl_valid && cs_state == 0) begin ctl_detail<=16'hFFFF; cs_start_mask<=0; if(ctl_axis_mask==0 || (ctl_axis_mask >> AXIS_COUNT)!=0) begin ctl_accept<=1;ctl_status<=ST_BAD_AXIS;ctl_detail<=0; end else if((ctl_flags & ((ctl_op==OP_ABORT || ctl_op==OP_CLEAR_COUNTERS) ? 8'hFE : 8'hFF))!=0 || (ctl_op==OP_SET_POSITION && ((ctl_axis_mask & (ctl_axis_mask-1'b1))!=0))) begin ctl_accept<=1;ctl_status<=ST_BAD_VALUE;ctl_detail<=0; end else if((ctl_op==OP_HOME_LOWER || ctl_op==OP_SEEK_UPPER) && ctl_value!=0 && ((ctl_axis_mask[0] && ctl_value> 2)!=0 && ctl_value=0;i=i-1) begin if(cs_mask[i]) begin case(cs_op) OP_START_ARMED: begin queue_paused_r[i]<=0; if(ax_armed[i] || (ax_load_ready[i] && (depth_r[i]!=0 || urgent_valid_r[i]))) cs_start_mask[i]<=1; else ctl_detail<=i; end OP_ABORT: begin ax_abort[i]<=1;queue_paused_r[i]<=1; if(cs_flags[0]) begin depth_r[i]<=0;urgent_valid_r[i]<=0;end end OP_CLEAR_QUEUE: begin depth_r[i]<=0;urgent_valid_r[i]<=0;end OP_CLEAR_COUNTERS: begin ax_ctr_clear[i]<=1;ax_pos_zero[i]<=cs_flags[0];end OP_SET_POSITION: ax_pos_load[i]<=1; OP_CLEAR_LIMIT_LATCH: ax_lat_clear[i]<=1; OP_HOME_LOWER,OP_SEEK_UPPER: begin ax_preempt[i]<=1;depth_r[i]<=0;queue_paused_r[i]<=0; urgent_valid_r[i]<=1;urgent_from_queue[i]<=0; home_pending_r[i]<=(cs_op==OP_HOME_LOWER); end endcase end end cs_state<=cs_op==OP_START_ARMED ? 3 : 2; end else if(cs_state==3 && (ax_armed & cs_start_mask)==cs_start_mask && fx_state==FX_IDLE && ax_seg_load==0) begin ax_start_armed<=cs_start_mask; cs_state<=2; // Keep the shared value stable until the scanner consumed all // control pulses. ACK then releases the one command context. end else if(cs_state==2 && !bank_telemetry_changing && (!seek_context || (internal_urgent_pending==0 && fx_state==FX_IDLE && ax_seg_load==0))) begin ctl_accept<=1;ctl_status<=ST_OK;cs_state<=0; end // ---- startup: submit internal SEEK_LOWER per axis ---- if (startup_seek) begin su_axis <= 2'd0;su_wait<=0; end else if (su_axis < AXIS_COUNT && fx_state==FX_IDLE) begin if(su_wait)begin if(ax_seg_consumed[su_axis])begin su_wait<=0;su_axis<=su_axis+1'b1;end end else begin ax_seg_load[su_axis] <= 1'b1; fx_seg <= {32'd0,((su_axis==0)?X_DEFAULT_HALF_US:((su_axis==1)?Y_DEFAULT_HALF_US:Z_DEFAULT_HALF_US)), 32'd0,8'd0,8'd0,8'd1,8'd0}; // unlimited SEEK_LOWER, no ARM su_wait<=1; end end end end endmodule // Keep the production memory's single read/write ports explicit. Diagnostic // parallel views live outside this boundary and cannot change its inference. module motor_limit_history #( parameter integer AXIS_COUNT=3, parameter integer ADDRESS_WIDTH=(AXIS_COUNT<=1)?1:$clog2(AXIS_COUNT) )( input wire clk,write_enable, input wire [ADDRESS_WIDTH-1:0] write_axis,read_axis, input wire [159:0] write_data, output wire [159:0] read_data ); reg [159:0] records [0:AXIS_COUNT-1]; always @(posedge clk)if(write_enable)records[write_axis]<=write_data; assign read_data=records[read_axis]; endmodule // Validated queue descriptors need85 bits: mode3, direction1, arm1, // count32, half-period32 and tag16. Axis is implicit in the slot address; // preempt/clear/reserved fields have already been consumed by batch commit. // Five18-bit words preserve the external16-byte descriptor contract while // packing96 waiting plus3 urgent slots into495 words of one512x18 BRAM. module motor_queue_storage #(parameter integer SLOTS=96, parameter integer SLOT_AW=$clog2(SLOTS)) ( input wire clk,rst_n,wr_req,rd_req, input wire [SLOT_AW-1:0] wr_slot,rd_slot, input wire [127:0] wr_data, output wire wr_busy, output reg [127:0] rd_data, output reg rd_valid ); localparam integer MEMORY_AW=$clog2(SLOTS*5); reg write_active,read_active,read_phase; reg [2:0] write_word,read_word; reg [MEMORY_AW-1:0] write_base,read_base; reg [89:0] write_shift,read_shift; wire [17:0] memory_data; wire [89:0] assembled={memory_data,read_shift[89:18]}; wire [MEMORY_AW-1:0] write_address=write_base+write_word; wire [MEMORY_AW-1:0] read_address=read_base+read_word; assign wr_busy=write_active || wr_req; ef2_bram_dp #(.DATA_WIDTH(18),.ADDR_WIDTH(MEMORY_AW),.DATA_DEPTH(SLOTS*5)) u_memory ( .clk(clk),.a_en(write_active),.a_we(write_active),.a_addr(write_address), .a_wdata(write_shift[17:0]),.a_rdata(),.b_en(1'b1),.b_we(1'b0), .b_addr(read_address),.b_wdata(18'd0),.b_rdata(memory_data) ); always @(posedge clk or negedge rst_n)begin if(!rst_n)begin write_active<=0;read_active<=0;read_phase<=0;write_word<=0;read_word<=0; write_base<=0;read_base<=0;write_shift<=0;read_shift<=0;rd_data<=0;rd_valid<=0; end else begin rd_valid<=0; if(wr_req && !write_active)begin write_shift<={5'd0,wr_data[111:96],wr_data[95:64],wr_data[63:32],wr_data[18],wr_data[16],wr_data[10:8]}; write_base<=wr_slot*5;write_word<=0;write_active<=1; end else if(write_active)begin write_shift<=write_shift>>18; if(write_word==4)write_active<=0; else write_word<=write_word+1'b1; end if(rd_req && !read_active)begin read_base<=rd_slot*5;read_word<=0;read_phase<=0;read_active<=1; end else if(read_active)begin if(!read_phase)read_phase<=1; else begin read_phase<=0;read_shift<=assembled; if(read_word==4)begin read_active<=0;rd_valid<=1; rd_data<={16'd0,assembled[84:69],assembled[68:37],assembled[36:5], 8'd0,5'd0,assembled[4],1'b0,assembled[3],5'd0,assembled[2:0],8'd0}; end else read_word<=read_word+1'b1; end end end end endmodule