| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 |
- `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;
- wire [5:0] qc_base_depth [0:AXIS_COUNT-1];
- reg [3:0] qc_new_count [0:AXIS_COUNT-1]; // one batch has at most8 descriptors
- reg [4:0] qc_new_head [0:AXIS_COUNT-1];
- reg [AXIS_COUNT-1:0] qc_resume, start_credit;
- reg [AXIS_INDEX_BITS-1:0] qc_space_axis;
- wire [6:0] qc_space_total={1'b0,qc_base_depth[qc_space_axis]}+{3'd0,qc_new_count[qc_space_axis]};
- wire qc_space_bad=qc_space_total>7'd32;
- 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;
- // The loader is quiescent and controls wait until QC_IDLE. Live depth is
- // therefore stable throughout validation/write preparation; only Clear
- // changes the scratch base, without changing the live queue.
- generate for(g=0;g<AXIS_COUNT;g=g+1)begin:g_capacity_base
- assign qc_base_depth[g]=qc_want_clear[g] ? 6'd0 : depth_r[g];
- end endgenerate
- 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*4-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;
- // The history head follows the registered one-axis event producer.
- // Status sees every axis without arbitration stalls. A pending limit
- // event waits at most one rotation for its selected record to reach head.
- wire [AXIS_INDEX_BITS-1:0] history_scan_axis;
- wire event_dispatch_ready=!select_limit || ev_select==history_scan_axis;
- wire [AXIS_INDEX_BITS-1:0] limit_read_axis=history_scan_axis;
- wire [159:0] limit_read_record;
- motor_limit_history #(.AXIS_COUNT(AXIS_COUNT)) u_limit_history(
- .clk(clk),.rst_n(rst_n),.write_enable(|ax_limit_pulse),
- .write_data({ax_limit_time_high,ax_limit_data}),
- .read_axis(history_scan_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 @* 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<AXIS_COUNT ||
- (|ax_done) || (|ax_seg_load) || (|ax_limit_pulse);
- assign motor_telemetry_ready=bank_telemetry_ready && !status_changing;
- always @(posedge clk or negedge rst_n) begin
- if(!rst_n)motor_status_epoch<=0;
- else if(status_changing)motor_status_epoch<=motor_status_epoch+1'b1;
- end
- generate for(g=0;g<AXIS_COUNT;g=g+1)begin:g_bank_ports
- assign internal_urgent_pending[g]=urgent_valid_r[g] && !urgent_from_queue[g];
- assign ax_seg_mode[g]=load_mode;
- assign ax_seg_upper[g]=load_upper;assign ax_seg_armed[g]=load_armed;
- assign ax_seg_count[g]=load_count;assign ax_seg_half[g]=load_half;assign ax_seg_tag[g]=load_tag;
- assign bank_seg_mode[g*3 +:3]=ax_seg_mode[g];
- assign bank_seg_count[g*32 +:32]=ax_seg_count[g];
- assign bank_seg_half_us[g*32 +:32]=ax_seg_half[g];
- assign bank_seg_tag[g*16 +:16]=ax_seg_tag[g];
- assign bank_position_value[g*32 +:32]=cs_value;
- assign ax_reason[g]=bank_done_reason[g*4 +:4];
- assign ax_dtag[g]=bank_done_tag[g*16 +:16];
- assign ax_dexec[g]=bank_done_executed[g*32 +:32];
- assign ax_dpos[g]=bank_done_position[g*32 +:32];
- assign ax_dtotal[g]=bank_done_total[g*32 +:32];
- assign ax_dtime[g]=bank_done_time[g*32 +:32];
- assign ax_pos[g]=bank_position[g*32 +:32];
- assign ax_total[g]=bank_total_steps[g*32 +:32];
- assign ax_lhits[g]=bank_lower_hit_count[g*32 +:32];
- assign ax_uhits[g]=bank_upper_hit_count[g*32 +:32];
- assign ax_last_reason[g]=bank_last_stop_reason[g*4 +:4];
- assign ax_record[g]=bank_telemetry_record[g*`MR_AXIS_RECORD_BYTES*8 +:`MR_AXIS_RECORD_BYTES*8];
- end endgenerate
- motor_axis_core #(.AXIS_COUNT(AXIS_COUNT),.PARALLEL_TELEMETRY(PARALLEL_TELEMETRY),.DIR_SETUP_TICKS(DIR_SETUP_TICKS),.DIR_LOWER_LEVEL(DIR_LOWER_LEVEL)) u_axes (
- .seg_mode(bank_seg_mode),
- .seg_count(bank_seg_count),
- .seg_half_us(bank_seg_half_us),
- .seg_tag(bank_seg_tag),
- .position_value(bank_position_value),
- .done_reason(bank_done_reason),
- .done_tag(bank_done_tag),
- .done_executed(bank_done_executed),
- .done_position(bank_done_position),
- .done_total(bank_done_total),
- .done_time(bank_done_time),
- .position(bank_position),
- .total_steps(bank_total_steps),
- .lower_hit_count(bank_lower_hit_count),
- .upper_hit_count(bank_upper_hit_count),
- .last_stop_reason(bank_last_stop_reason),
- .telemetry_record(bank_telemetry_record),
- .telemetry_word_addr(telemetry_word_addr),.telemetry_axis(bank_telemetry_axis),.telemetry_word_data(bank_telemetry_word),
- .clk(clk),
- .rst_n(rst_n),
- .tick_us(tick_us),
- .limit_lower(ll_s2),
- .limit_upper(ul_s2),
- .seg_load(ax_seg_load),
- .seg_consumed(ax_seg_consumed),
- .seg_dir_upper(ax_seg_upper),
- .seg_armed(ax_seg_armed),
- .start_armed(ax_start_armed),
- .abort_req(ax_abort),
- .preempt_req(ax_preempt),
- .step_out(step_out_w),
- .dir_out(dir_out_w),
- .active(ax_active),
- .armed(ax_armed),
- .load_ready(ax_load_ready),
- .done_pulse(ax_done),
- .event_clock_us(time_us[31:0]),
- .event_clock_high(time_us[63:32]),
- .last_executed(),
- .position_reference_seen(ax_ref_seen),
- .lower_latched(ax_llat),
- .upper_latched(ax_ulat),
- .both_latched(ax_both),
- .clear_limit_latch(ax_lat_clear),
- .position_load(ax_pos_load),
- .counters_clear(ax_ctr_clear),
- .pos_zero_with_counters(ax_pos_zero),
- .limit_event_pulse(ax_limit_pulse),.limit_event_data(),.limit_event_word(ax_limit_data),.done_event_word(ax_done_word),.limit_event_drops(ax_limit_drops),
- .limit_event_time_high(ax_limit_time_high),
- .telemetry_ready(bank_telemetry_ready),.telemetry_changing(bank_telemetry_changing),
- .scan_deadline_miss(motor_scan_deadline_miss)
- );
- // Generated field offsets; add queue-owned values without overlapping core fields.
- generate
- if(PARALLEL_TELEMETRY)begin:diagnostic_status
- for(g=0;g<AXIS_COUNT;g=g+1) begin:g_tel
- wire [`MR_AXIS_RECORD_BYTES*8-1:0] queue_field={{(`MR_AXIS_RECORD_BYTES*8-6){1'b0}},depth_r[g]};
- wire [`MR_AXIS_RECORD_BYTES*8-1:0] pause_field={{(`MR_AXIS_RECORD_BYTES*8-1){1'b0}},queue_paused_r[g]};
- wire [159:0] history=limit_history_valid[g] ? limit_record[g] : 160'd0;
- reg [`MR_AXIS_RECORD_BYTES*8-1:0] record;
- always @* begin
- record=ax_record[g] | (queue_field << (`MR_AXIS_QUEUE_DEPTH_OFFSET*8)) |
- (pause_field << (`MR_AXIS_FLAGS_OFFSET*8+`MR_AXIS_FLAG_QUEUE_PAUSED_BIT));
- record[`MR_AXIS_LAST_LIMIT_POSITION_OFFSET*8+:32]=history[95:64];
- record[`MR_AXIS_LAST_LIMIT_TOTAL_OFFSET*8+:32]=history[127:96];
- record[`MR_AXIS_LAST_LIMIT_TAG_OFFSET*8+:16]=history[63:48];
- record[`MR_AXIS_LAST_LIMIT_EVENT_OFFSET*8+:8]=history[47:40];
- record[`MR_AXIS_LAST_LIMIT_TIME_LOW_OFFSET*8+:32]=history[31:0];
- record[`MR_AXIS_LAST_LIMIT_TIME_HIGH_OFFSET*8+:32]=history[159:128];
- end
- assign axis_telemetry[g*`MR_AXIS_RECORD_BYTES*8 +: `MR_AXIS_RECORD_BYTES*8]=record;
- end
- end else begin:no_parallel_status
- assign axis_telemetry=0;
- end
- endgenerate
- // ---- event ring ----
- motor_event_ring #(.DEPTH(64)) u_events (
- .clk(clk), .rst_n(rst_n),
- .wr_req(ev_wr_req), .wr_ack(ev_ack), .wr_axis(ev_axis), .wr_event(ev_code),
- .wr_tag(ev_tag), .wr_position(ev_pos), .wr_total(ev_total), .time_us(ev_timestamp),
- .rd_index(ev_rd_index), .rd_hold(ev_hold), .rd_data(ev_rd_data), .record_count(ev_count),
- .rd_byte_index(ev_rd_byte_index),.rd_byte_data(ev_rd_byte_data),
- .overflow_lsb(ev_overflow),
- .view_ready(ev_view_ready), .view_count(ev_view_count), .overflow_count(event_ring_overflow),
- .clear_n(ev_clear_n), .clear_count(ev_clear_count)
- );
- // Physical limit edges and segment completions have separate pending slots.
- // A held limit stopping a new request does not fabricate another edge event.
- reg [AXIS_COUNT-1:0] done_pending,limit_pending;
- reg [127:0] pending_record [0:AXIS_COUNT-1];
- // Persistent last-limit history doubles as the pending event record.
- // Clearing a pending bit or the host ring never destroys this history.
- reg [3:0] pending_drop_union;
- reg pending_limit_loss,pending_done_loss;
- wire [4:0] pending_loss_total={1'b0,pending_drop_union}+{4'd0,pending_limit_loss}+{4'd0,pending_done_loss};
- wire [8:0] pending_losses={4'd0,pending_loss_total};
- wire [127:0] selected_record=select_limit ? limit_read_record[127:0] : pending_record[ev_select];
- integer done_write_axis,limit_write_axis,record_axis;
- reg done_record_write;
- always @* begin
- done_write_axis=0;limit_write_axis=0;done_record_write=0;
- // The shared axis service emits at most one completion and one limit
- // record per clock. Two independent memories retain both kinds.
- for(record_axis=0;record_axis<AXIS_COUNT;record_axis=record_axis+1)begin
- if(ax_done[record_axis] && (event_for_stop(ax_reason[record_axis])!=0 ||
- (home_pending_r[record_axis] && ax_reason[record_axis]==SR_LOWER)))begin
- done_write_axis=record_axis;done_record_write=1;
- end
- if(ax_limit_pulse[record_axis])limit_write_axis=record_axis;
- end
- end
- // One addressed write port per bank allows RAM inference. Reset clears
- // validity bits below; stale storage is never emitted after reset.
- always @(posedge clk)begin
- if(PARALLEL_TELEMETRY && rst_n && (|ax_limit_pulse))limit_record[limit_write_axis]<={ax_limit_time_high,ax_limit_data};
- if(rst_n && done_record_write)
- pending_record[done_write_axis]<={ax_done_word[127:48],
- ((home_pending_r[done_write_axis] && ax_reason[done_write_axis]==SR_LOWER)?EV_HOME:event_for_stop(ax_reason[done_write_axis])),
- ax_done_word[39:0]};
- end
- function [7:0] event_for_stop;
- input [3:0] r;
- begin
- case(r)
- 1:event_for_stop=EV_SEGMENT;
- 4:event_for_stop=EV_ABORT;5:event_for_stop=EV_PREEMPT;6:event_for_stop=8'd6;
- 8:event_for_stop=EV_HOST_CAP;9:event_for_stop=EV_HOLD;
- default:event_for_stop=0;
- endcase
- end
- endfunction
- // Factor one-bit eligibility/priority from the wide descriptor datapath.
- // This preserves the original round-robin winner and one-clock arbitration.
- always @* begin
- for(fx_k=0;fx_k<AXIS_COUNT;fx_k=fx_k+1)
- fx_eligible[fx_k]=ax_load_ready[fx_k] && !ax_seg_load[fx_k] && !queue_paused_r[fx_k] &&
- (cs_state==0 || (cs_state==3 && cs_start_mask[fx_k]) || (seek_context && cs_mask[fx_k])) &&
- (urgent_valid_r[fx_k] || depth_r[fx_k]!=0);
- fx_candidate=0;fx_candidate_valid=0;fx_probe=0;
- for(fx_k=AXIS_COUNT-1;fx_k>=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_drop_union=0;pending_limit_loss=0;pending_done_loss=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
- // The core emits at most one record of each kind per clock. OR the
- // one-hot-qualified fields instead of building an axis-wide adder chain.
- // This uses constant per-axis indices, not the rejected dynamic selector.
- for(ev_scan=0;ev_scan<AXIS_COUNT;ev_scan=ev_scan+1) begin
- if(ax_limit_pulse[ev_scan]) begin
- pending_drop_union=pending_drop_union | ax_limit_drops[ev_scan*4+:4];
- if(limit_pending[ev_scan] && (ev_wr_req || reset_pending || ev_select!=ev_scan || !select_limit || !event_dispatch_ready)) pending_limit_loss=1;
- end
- if(ax_done[ev_scan] && (event_for_stop(ax_reason[ev_scan])!=0 || (home_pending_r[ev_scan] && ax_reason[ev_scan]==SR_LOWER)) &&
- done_pending[ev_scan] && (ev_wr_req || reset_pending || ev_select!=ev_scan || select_limit)) pending_done_loss=1;
- end
- end
- always @(posedge clk or negedge rst_n) begin
- if(!rst_n) begin
- ev_wr_req<=0;ev_code<=0;ev_tag<=0;ev_pos<=0;ev_total<=0;ev_axis<=4'hF;ev_timestamp<=0;
- done_pending<=0;limit_pending<=0;limit_history_valid<=0;event_pending_overflow<=0;reset_pending<=1;
- end else begin
- event_pending_overflow<=event_pending_overflow+pending_losses;
- if(ev_wr_req && ev_ack) ev_wr_req<=0;
- else if(!ev_wr_req && reset_pending) begin
- ev_wr_req<=1;ev_axis<=4'hF;ev_code<=6;ev_tag<=0;ev_pos<=0;ev_total<=0;ev_timestamp<=0;reset_pending<=0;
- end else if(!ev_wr_req && ev_select>=0 && event_dispatch_ready) 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_i<AXIS_COUNT;ev_i=ev_i+1) begin
- if(ax_limit_pulse[ev_i])begin limit_pending[ev_i]<=1;limit_history_valid[ev_i]<=1;end
- if(ax_done[ev_i] && (event_for_stop(ax_reason[ev_i])!=0 || (home_pending_r[ev_i] && ax_reason[ev_i]==SR_LOWER))) begin
- done_pending[ev_i]<=1;
- end
- end
- end
- end
- // ---- queue commit helper wires ----
- wire [5:0] ax_depth_0 = depth_r[0];
- // Unpacked-array reduction (|depth_r) is not legal Verilog; fold per axis.
- reg unused_depth;
- integer ud_i;
- always @* begin
- unused_depth = 1'b0;
- for (ud_i = 0; ud_i < AXIS_COUNT; ud_i = ud_i + 1)
- unused_depth = unused_depth | (depth_r[ud_i] != 6'd0);
- end
- // ---- main sequencer: batch validate/commit + fetch + control ----
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- qc_state <= QC_IDLE;qc_space_axis<=0; qc_index <= 4'd0;start_credit<=0;
- stage_byte_index<=0;stage_for_write<=0;stage_data<=0;
- qw_accept <= 1'b0; qw_status <= 8'd0; qw_detail <= 16'd0;
- ctl_accept <= 1'b0; ctl_status <= 8'd0; ctl_detail <= 16'd0;
- fx_state <= FX_IDLE; fx_axis <= 2'd0; fx_from_urgent <= 1'b0;fx_round<=0;cs_start_mask<=0;
- ax_seg_load <= {AXIS_COUNT{1'b0}};
- ax_start_armed <= {AXIS_COUNT{1'b0}};
- ax_abort <= {AXIS_COUNT{1'b0}};
- ax_preempt <= {AXIS_COUNT{1'b0}};
- ax_pos_load <= {AXIS_COUNT{1'b0}};
- ax_ctr_clear <= {AXIS_COUNT{1'b0}};
- ax_pos_zero <= {AXIS_COUNT{1'b0}};
- ax_lat_clear <= {AXIS_COUNT{1'b0}};
- for (i = 0; i < AXIS_COUNT; i = i + 1) begin
- depth_r[i] <= 6'd0; head_r[i] <= 6'd0; queue_paused_r[i] <= 1'b0;
- urgent_valid_r[i] <= 1'b0; urgent_from_queue[i] <= 1'b0;
- home_pending_r[i] <= 1'b0;
- end
- q_we <= 1'b0;q_re<=0;
- cs_state <= 2'd0; su_axis <= AXIS_COUNT;su_wait<=0;
- end else begin
- // default single-cycle pulses
- ax_seg_load <= {AXIS_COUNT{1'b0}};
- ax_start_armed <= {AXIS_COUNT{1'b0}};
- ax_abort <= {AXIS_COUNT{1'b0}};
- ax_preempt <= {AXIS_COUNT{1'b0}};
- ax_pos_load <= {AXIS_COUNT{1'b0}};
- ax_ctr_clear <= {AXIS_COUNT{1'b0}};
- ax_lat_clear <= {AXIS_COUNT{1'b0}};
- qw_accept <= 1'b0;
- ctl_accept <= 1'b0;
- q_we <= 1'b0;q_re<=0;
- // ---- MOTOR_QUEUE_WRITE: quiesce loader, validate, write, publish ----
- for(i=0;i<AXIS_COUNT;i=i+1)
- if(ax_done[i] && (ax_reason[i]==2 || ax_reason[i]==3 || ax_reason[i]==7))
- queue_paused_r[i]<=1;
- // Active STEP execution never waits for these RAM transactions.
- if(qw_submit && qc_state==QC_IDLE) begin
- if(qw_count<1 || qw_count>8) 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;qc_space_axis<=0;
- for(i=0;i<AXIS_COUNT;i=i+1) begin
- qc_new_count[i]<=0;
- qc_first_of_axis[i]<=1;qc_want_preempt[i]<=0;qc_want_clear[i]<=0;qc_resume[i]<=0;
- end
- stage_byte_index<=0;stage_for_write<=0;qc_state<=QC_STAGE_WAIT;
- end else if(qc_state==QC_STAGE_WAIT) begin
- qc_state<=QC_STAGE_READ;
- end else if(qc_state==QC_STAGE_READ) begin
- stage_data<={stage_read_data,stage_data[127:8]};
- if(stage_byte_index==15) qc_state<=stage_for_write ? QC_WRITE : QC_VALIDATE;
- else begin stage_byte_index<=stage_byte_index+1'b1;qc_state<=QC_STAGE_WAIT;end
- end else if(qc_state==QC_VALIDATE) begin
- if(qc_index==qc_count) qc_state<=QC_SPACE;
- else if(f_axis(stage_data)>=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);
- 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
- // One queue-space/address datapath serves one scratch
- // row per clock. No live queue state or RAM is published
- // until every row and every descriptor has passed.
- qc_proj_depth[qc_space_axis]<=qc_space_total[5:0];
- qc_new_head[qc_space_axis]<=qc_want_preempt[qc_space_axis] && !qc_want_clear[qc_space_axis] ?
- (head_r[qc_space_axis][4:0]-{1'b0,qc_new_count[qc_space_axis]}) : head_r[qc_space_axis][4:0];
- qc_append_pos[qc_space_axis]<=qc_want_preempt[qc_space_axis] ?
- (qc_want_clear[qc_space_axis] ? head_r[qc_space_axis][4:0] :
- ((head_r[qc_space_axis][4:0]-{1'b0,qc_new_count[qc_space_axis]}) & 5'h1F)) :
- ((head_r[qc_space_axis][4:0]+qc_base_depth[qc_space_axis][4:0]) & 5'h1F);
- if(qc_space_axis==AXIS_COUNT-1)begin
- qc_index<=0;stage_byte_index<=0;stage_for_write<=1;qc_state<=QC_STAGE_WAIT;
- end else qc_space_axis<=qc_space_axis+1'b1;
- end
- end else if(qc_state==QC_WRITE) begin
- if(qc_index==qc_count) qc_state<=QC_DRAIN;
- else if(!q_write_busy)begin
- if(qc_is_urgent[qc_index])begin
- // Reserved urgent slots follow all waiting slots.
- // Write after full validation, publish valid atomically.
- q_wr_addr<=AXIS_COUNT*32+qc_axis;
- end else begin
- q_wr_addr<=qc_axis*32+qc_append_pos[qc_axis][4:0];
- qc_append_pos[qc_axis]<=(qc_append_pos[qc_axis]+1'b1)&6'd31;
- end
- q_wr_data<=stage_data;q_we<=1;
- qc_index<=qc_index+1'b1;stage_byte_index<=0;
- qc_state<=qc_index+1'b1==qc_count ? QC_DRAIN : QC_STAGE_WAIT;
- end
- end else if(qc_state==QC_DRAIN && !q_write_busy) begin
- // Last synchronous RAM write has completed before any reader
- // can see the new head/depth. All axes publish on this edge.
- for(i=0;i<AXIS_COUNT;i=i+1) begin
- if(!qc_first_of_axis[i]) begin
- home_pending_r[i]<=0;
- depth_r[i]<=qc_proj_depth[i];head_r[i]<=qc_new_head[i];
- if(qc_want_clear[i]) urgent_valid_r[i]<=0;
- if(qc_want_preempt[i]) begin
- urgent_valid_r[i]<=1;urgent_from_queue[i]<=1;ax_preempt[i]<=1;
- end
- // A new nonarmed command or preempt explicitly resumes
- // an axis paused by Abort/limit. ARM alone cannot do so.
- if(qc_resume[i] || qc_want_preempt[i]) queue_paused_r[i]<=0;
- end
- end
- qc_state<=QC_PUBLISH;
- end else if(qc_state==QC_PUBLISH) begin
- qw_accept<=1;qw_status<=ST_OK;qw_detail<=0;qc_state<=QC_IDLE;
- end
- // ---- execution fetch: idle axis takes urgent, else head ----
- case (fx_state)
- FX_IDLE: begin
- // Arbitration selects only an axis. One datapath below
- // owns address and descriptor construction for that winner.
- begin
- if (fx_candidate_valid && !ctl_valid && qc_state==QC_IDLE && !qw_submit &&
- su_axis>=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<X_MIN_HALF_US) ||
- (ctl_axis_mask[1] && ctl_value<Y_MIN_HALF_US) ||
- ((ctl_axis_mask >> 2)!=0 && ctl_value<Z_MIN_HALF_US))) begin
- ctl_accept<=1;ctl_status<=ST_BAD_VALUE;ctl_detail<=0;
- end else cs_state<=1;
- end else if(cs_state==1 && fx_state==FX_IDLE && qc_state==QC_IDLE) begin
- // All selected control pulses commit together. SEEK uses the
- // urgent loader after the old active acknowledges the abort;
- // loading and aborting the same active cycle loses the load.
- for(i=AXIS_COUNT-1;i>=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
- // The core emits the previous scan axis once per clock. Reset starts this
- // head one axis behind the core, so a new event always updates the head as
- // it rotates to the tail. Validity remains in the owner; uninitialized data
- // never becomes a status/event record. No dynamic wide address mux is needed.
- module motor_limit_history #(
- parameter integer AXIS_COUNT=3,
- parameter integer ADDRESS_WIDTH=(AXIS_COUNT<=1)?1:$clog2(AXIS_COUNT)
- )(
- input wire clk,rst_n,write_enable,
- output reg [ADDRESS_WIDTH-1:0] read_axis,
- input wire [159:0] write_data,
- output wire [159:0] read_data
- );
- reg [159:0] records [0:AXIS_COUNT-1];
- integer i;
- always @(posedge clk or negedge rst_n)begin
- if(!rst_n)read_axis<=AXIS_COUNT-1;
- else read_axis<=read_axis==AXIS_COUNT-1 ? 0 : read_axis+1'b1;
- end
- always @(posedge clk)if(rst_n)begin
- for(i=0;i<AXIS_COUNT-1;i=i+1)records[i]<=records[i+1];
- records[AXIS_COUNT-1]<=write_enable ? write_data : records[0];
- end
- assign read_data=records[0];
- 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
|