motor_engine.v 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. `timescale 1ns/1ps
  2. `include "generated/mrdimm/prod2u_mrdimm_params.vh"
  3. // MRDIMM shared motor engine (2026 contract, section 10).
  4. //
  5. // AXIS_COUNT axes share one queue BRAM, one batch-staging buffer, one
  6. // event ring and one 1 us time base. Each axis owns a motor_axis_core
  7. // whose final STEP decision runs every clock (limits -> abort/preempt ->
  8. // scheduler). X/Y/Z map to axis 0/1/2.
  9. //
  10. // Queue: 32 x 16-byte waiting slots per axis plus one urgent (preempt)
  11. // preinstall slot per axis. A batch is validated in full (fields, per
  12. // axis space, preempt/clear placement) before any state changes; the
  13. // per-axis depth/tail state is then published in one register write, so
  14. // a rejected batch changes nothing and an accepted batch never shows a
  15. // half-published queue.
  16. module motor_engine #(
  17. parameter integer AXIS_COUNT = 3,
  18. parameter integer PARALLEL_TELEMETRY=1,
  19. // default half periods in us: X/Y 1000 Hz -> 500 us, Z 25 Hz -> 20000 us
  20. parameter [31:0] X_DEFAULT_HALF_US = 32'd500,
  21. parameter [31:0] Y_DEFAULT_HALF_US = 32'd500,
  22. parameter [31:0] Z_DEFAULT_HALF_US = 32'd20000,
  23. parameter [31:0] X_MIN_HALF_US = 32'd500,
  24. parameter [31:0] Y_MIN_HALF_US = 32'd500,
  25. parameter [31:0] Z_MIN_HALF_US = 32'd20000,
  26. parameter integer DIR_SETUP_TICKS = 1000,
  27. parameter DIR_LOWER_LEVEL = 1'b0
  28. ) (
  29. input wire clk,
  30. input wire rst_n,
  31. input wire tick_us,
  32. input wire [AXIS_COUNT-1:0] limit_lower_raw,
  33. input wire [AXIS_COUNT-1:0] limit_upper_raw,
  34. // MOTOR_QUEUE_WRITE staging (slot*16+offset bytes) then submit
  35. input wire qw_byte_we,
  36. input wire [6:0] qw_byte_addr, // 0..127
  37. input wire [7:0] qw_byte_data,
  38. input wire qw_submit,
  39. input wire [3:0] qw_count, // 1..8
  40. output reg qw_accept,
  41. output reg [7:0] qw_status,
  42. output reg [15:0] qw_detail,
  43. // MOTOR_CONTROL: provider holds all fields from ctl_valid through ctl_accept.
  44. input wire ctl_valid,
  45. input wire [2:0] ctl_op,
  46. input wire [15:0] ctl_axis_mask,
  47. input wire [7:0] ctl_flags,
  48. input wire [31:0] ctl_value,
  49. output reg ctl_accept,
  50. output reg [7:0] ctl_status,
  51. output reg [15:0] ctl_detail,
  52. // startup: one pulse submits internal SEEK_LOWER for every axis
  53. input wire startup_seek,
  54. // event ring host view
  55. input wire [7:0] ev_rd_index,
  56. input wire ev_hold,
  57. output wire [127:0] ev_rd_data,
  58. input wire [3:0] ev_rd_byte_index,
  59. output wire [7:0] ev_rd_byte_data,
  60. output wire [7:0] ev_count,
  61. output wire [7:0] ev_overflow,
  62. output wire ev_view_ready,
  63. output wire [7:0] ev_view_count,
  64. output reg [31:0] event_pending_overflow,
  65. output wire [31:0] ev_time_us,
  66. output wire [31:0] ev_time_high,event_ring_overflow,
  67. output wire any_limit_live,
  68. output wire motor_telemetry_ready,motor_scan_deadline_miss,
  69. output reg [15:0] motor_status_epoch,
  70. input wire ev_clear_n,
  71. input wire [7:0] ev_clear_count,
  72. output wire [AXIS_COUNT*`MR_AXIS_RECORD_BYTES*8-1:0] axis_telemetry,
  73. input wire [4:0] telemetry_word_addr,
  74. output wire [3:0] telemetry_axis,
  75. output wire [15:0] telemetry_word_data,
  76. output wire [AXIS_COUNT-1:0] step_out,
  77. output wire [AXIS_COUNT-1:0] dir_out
  78. );
  79. localparam [2:0] M_REL = 3'd0, M_SEEK_LOWER = 3'd1, M_SEEK_UPPER = 3'd2,
  80. M_CONT = 3'd3, M_HOLD = 3'd4;
  81. localparam [2:0] OP_START_ARMED = 3'd0, OP_ABORT = 3'd1, OP_CLEAR_QUEUE = 3'd2,
  82. OP_CLEAR_COUNTERS = 3'd3, OP_SET_POSITION = 3'd4, OP_CLEAR_LIMIT_LATCH = 3'd5,
  83. OP_HOME_LOWER = 3'd6, OP_SEEK_UPPER = 3'd7;
  84. localparam [7:0] ST_OK = 8'd0, ST_BAD_VALUE = 8'd3, ST_BAD_AXIS = 8'd6;
  85. localparam [7:0] EV_SEGMENT = 8'd1, EV_LOWER = 8'd2, EV_UPPER = 8'd3,
  86. EV_ABORT = 8'd4, EV_PREEMPT = 8'd5, EV_BOTH = 8'd7, EV_HOST_CAP = 8'd8,
  87. EV_HOLD = 8'd9, EV_HOME = 8'd10;
  88. localparam [3:0] SR_LOWER = 4'd2, SR_UPPER = 4'd3;
  89. genvar g;
  90. integer i;
  91. localparam integer AXIS_INDEX_BITS=(AXIS_COUNT<=1) ? 1 : $clog2(AXIS_COUNT);
  92. localparam integer AXIS_COUNTER_BITS=$clog2(AXIS_COUNT+1);
  93. // ---- time base ----
  94. reg [63:0] time_us;
  95. always @(posedge clk or negedge rst_n) begin
  96. if (!rst_n) time_us <= 64'd0;
  97. else if (tick_us) time_us <= time_us + 64'd1;
  98. end
  99. assign ev_time_us = time_us[31:0];
  100. assign ev_time_high = time_us[63:32];
  101. // ---- limit 2FF synchronization ----
  102. reg [AXIS_COUNT-1:0] ll_s1, ll_s2, ul_s1, ul_s2;
  103. assign any_limit_live=(|ll_s2) || (|ul_s2);
  104. always @(posedge clk or negedge rst_n) begin
  105. if (!rst_n) begin
  106. ll_s1 <= {AXIS_COUNT{1'b0}}; ll_s2 <= {AXIS_COUNT{1'b0}};
  107. ul_s1 <= {AXIS_COUNT{1'b0}}; ul_s2 <= {AXIS_COUNT{1'b0}};
  108. end else begin
  109. ll_s1 <= limit_lower_raw; ll_s2 <= ll_s1;
  110. ul_s1 <= limit_upper_raw; ul_s2 <= ul_s1;
  111. end
  112. end
  113. // ---- shared queue RAM ----
  114. localparam QAW = $clog2(AXIS_COUNT * 33);
  115. reg [QAW-1:0] q_wr_addr, q_rd_addr;
  116. reg [127:0] q_wr_data;
  117. reg q_we,q_re;
  118. wire [127:0] q_rd;
  119. wire q_write_busy,q_read_valid;
  120. motor_queue_storage #(.SLOTS(AXIS_COUNT*33)) u_queue (
  121. .clk(clk),.rst_n(rst_n),.wr_req(q_we),.wr_slot(q_wr_addr),.wr_data(q_wr_data),
  122. .wr_busy(q_write_busy),.rd_req(q_re),.rd_slot(q_rd_addr),.rd_data(q_rd),.rd_valid(q_read_valid)
  123. );
  124. // ---- per-axis queue state ----
  125. reg [5:0] depth_r [0:AXIS_COUNT-1]; // waiting slots 0..32
  126. reg [5:0] head_r [0:AXIS_COUNT-1]; // oldest waiting slot
  127. reg queue_paused_r [0:AXIS_COUNT-1];
  128. reg urgent_valid_r [0:AXIS_COUNT-1];
  129. reg urgent_from_queue [0:AXIS_COUNT-1];
  130. reg home_pending_r [0:AXIS_COUNT-1];
  131. // ---- byte-wide batch BRAM and one shared segment assembly register ----
  132. // Validate every segment before publishing queue state. Two clocks per
  133. // byte hide the synchronous RAM latency; STEP scanning never waits here.
  134. reg [3:0] qc_index;
  135. reg [3:0] stage_byte_index;
  136. reg stage_for_write;
  137. reg [127:0] stage_data;
  138. wire [7:0] stage_read_data;
  139. ef2_bram_dp #(.DATA_WIDTH(8),.ADDR_WIDTH(7),.DATA_DEPTH(128)) u_stage (
  140. .clk(clk),.a_en(qw_byte_we),.a_we(qw_byte_we),.a_addr(qw_byte_addr),
  141. .a_wdata(qw_byte_data),.a_rdata(),.b_en(1'b1),.b_we(1'b0),
  142. .b_addr({qc_index[2:0],stage_byte_index}),.b_wdata(8'd0),.b_rdata(stage_read_data)
  143. );
  144. // segment field slices — wire segment byte order (contract 10.2):
  145. // byte0 axis, byte1 mode, byte2 flags, byte3 reserved,
  146. // bytes 4..7 count, bytes 8..11 half, bytes 12..13 tag, bytes 14..15 reserved
  147. function [7:0] f_axis; input [127:0] s; f_axis = s[7:0]; endfunction
  148. function [7:0] f_mode; input [127:0] s; f_mode = s[15:8]; endfunction
  149. function f_upper; input [127:0] s; f_upper = s[16]; endfunction
  150. function f_preempt; input [127:0] s; f_preempt = s[17]; endfunction
  151. function f_arm; input [127:0] s; f_arm = s[18]; endfunction
  152. function f_clear; input [127:0] s; f_clear = s[19]; endfunction
  153. function [31:0] f_count; input [127:0] s; f_count = s[63:32]; endfunction
  154. function [31:0] f_half; input [127:0] s; f_half = s[95:64]; endfunction
  155. function [15:0] f_tag; input [127:0] s; f_tag = s[111:96]; endfunction
  156. function seg_field_bad;
  157. input [127:0] s;
  158. input [31:0] min_half;
  159. begin
  160. seg_field_bad =
  161. (f_axis(s) >= AXIS_COUNT) ||
  162. (f_mode(s) == M_REL && f_count(s) == 32'd0) ||
  163. (f_mode(s) == M_HOLD && (f_half(s) != 32'd0)) ||
  164. (f_mode(s) != M_HOLD && f_half(s) == 32'd0) ||
  165. ((f_mode(s) == M_REL) || (f_mode(s) == M_CONT) ||
  166. (f_mode(s) == M_SEEK_LOWER) || (f_mode(s) == M_SEEK_UPPER)) &&
  167. (f_half(s) < min_half) ||
  168. (f_mode(s) > M_HOLD) ||
  169. (s[23:20] != 4'd0) || (s[31:24] != 8'd0) || (s[127:112] != 16'd0);
  170. end
  171. endfunction
  172. // ---- batch validate/commit FSM ----
  173. localparam [3:0] QC_IDLE=0, QC_QUIESCE=1, QC_VALIDATE=2, QC_SPACE=3,
  174. QC_WRITE=4, QC_DRAIN=5, QC_PUBLISH=6, QC_STAGE_WAIT=7, QC_STAGE_READ=8;
  175. reg [3:0] qc_state;
  176. reg [3:0] qc_count;
  177. reg [7:0] qc_is_urgent;
  178. wire [5:0] qc_base_depth [0:AXIS_COUNT-1];
  179. reg [3:0] qc_new_count [0:AXIS_COUNT-1]; // one batch has at most8 descriptors
  180. reg [4:0] qc_new_head [0:AXIS_COUNT-1];
  181. reg [AXIS_COUNT-1:0] qc_resume, start_credit;
  182. reg [AXIS_INDEX_BITS-1:0] qc_space_axis;
  183. wire [6:0] qc_space_total={1'b0,qc_base_depth[qc_space_axis]}+{3'd0,qc_new_count[qc_space_axis]};
  184. wire qc_space_bad=qc_space_total>7'd32;
  185. reg [5:0] qc_proj_depth [0:AXIS_COUNT-1]; // projected waiting depth
  186. reg [5:0] qc_append_pos [0:AXIS_COUNT-1]; // next free slot per axis
  187. reg [AXIS_COUNT-1:0] qc_first_of_axis;
  188. reg [AXIS_COUNT-1:0] qc_want_preempt; // first-seg preempt request per axis
  189. reg [AXIS_COUNT-1:0] qc_want_clear;
  190. // The loader is quiescent and controls wait until QC_IDLE. Live depth is
  191. // therefore stable throughout validation/write preparation; only Clear
  192. // changes the scratch base, without changing the live queue.
  193. generate for(g=0;g<AXIS_COUNT;g=g+1)begin:g_capacity_base
  194. assign qc_base_depth[g]=qc_want_clear[g] ? 6'd0 : depth_r[g];
  195. end endgenerate
  196. wire [31:0] stage_min_half = (f_axis(stage_data) == 2'd0) ? X_MIN_HALF_US :
  197. (f_axis(stage_data) == 2'd1) ? Y_MIN_HALF_US : Z_MIN_HALF_US;
  198. wire [AXIS_INDEX_BITS-1:0] qc_axis = f_axis(stage_data);
  199. // ---- execute fetch FSM ----
  200. localparam [2:0] FX_IDLE=0, FX_RD=1, FX_LOAD=2, FX_WAIT=3, FX_CONSUME=4;
  201. reg [2:0] fx_state;
  202. reg [AXIS_INDEX_BITS-1:0] fx_axis;
  203. reg [AXIS_INDEX_BITS-1:0] fx_round;
  204. integer fx_k,fx_probe;
  205. reg [AXIS_COUNT-1:0] fx_eligible;
  206. reg [7:0] fx_candidate;
  207. reg fx_candidate_valid;
  208. reg fx_from_urgent;
  209. reg [127:0] fx_seg;
  210. // ---- event writer ----
  211. reg ev_wr_req;
  212. reg [3:0] ev_axis;
  213. reg [7:0] ev_code;
  214. reg [15:0] ev_tag;
  215. reg [31:0] ev_pos;
  216. reg [31:0] ev_total;
  217. reg [31:0] ev_timestamp;
  218. wire ev_ack;
  219. // ---- control/startup one-shot staging ----
  220. // The command provider owns this context until acceptance. In particular,
  221. // SET_POSITION does not acknowledge before all pending scans consume value.
  222. wire [2:0] cs_op=ctl_op;
  223. wire [15:0] cs_mask=ctl_axis_mask;
  224. wire [31:0] cs_value=ctl_value;
  225. wire [7:0] cs_flags=ctl_flags;
  226. reg [1:0] cs_state; // 0 idle, 1 exec, 2 done-pulse
  227. wire seek_context=cs_state==2 && (cs_op==OP_HOME_LOWER || cs_op==OP_SEEK_UPPER);
  228. wire [AXIS_COUNT-1:0] internal_urgent_pending;
  229. reg [AXIS_COUNT-1:0] cs_start_mask;
  230. reg [1:0] cs_axis;
  231. reg cs_home;
  232. reg [AXIS_COUNTER_BITS-1:0] su_axis; // includes terminal AXIS_COUNT
  233. reg su_wait;
  234. // ---- axis cores ----
  235. wire [AXIS_COUNT-1:0] ax_active, ax_armed, ax_load_ready, ax_ref_seen, ax_done;
  236. wire [3:0] ax_reason [0:AXIS_COUNT-1];
  237. wire [15:0] ax_dtag [0:AXIS_COUNT-1];
  238. wire [31:0] ax_dexec [0:AXIS_COUNT-1];
  239. wire [31:0] ax_dpos [0:AXIS_COUNT-1],ax_dtotal [0:AXIS_COUNT-1],ax_dtime [0:AXIS_COUNT-1];
  240. wire [31:0] ax_pos [0:AXIS_COUNT-1];
  241. wire [31:0] ax_total [0:AXIS_COUNT-1];
  242. wire [31:0] ax_lhits [0:AXIS_COUNT-1];
  243. wire [31:0] ax_uhits [0:AXIS_COUNT-1];
  244. wire [`MR_AXIS_RECORD_BYTES*8-1:0] ax_record [0:AXIS_COUNT-1];
  245. wire [AXIS_COUNT-1:0] ax_llat, ax_ulat, ax_both,ax_limit_pulse;
  246. wire [127:0] ax_limit_data,ax_done_word;
  247. wire [31:0] ax_limit_time_high;
  248. wire [AXIS_COUNT*4-1:0] ax_limit_drops;
  249. wire [3:0] ax_last_reason [0:AXIS_COUNT-1];
  250. wire [AXIS_COUNT-1:0] step_out_w, dir_out_w;
  251. assign step_out = step_out_w;
  252. assign dir_out = dir_out_w;
  253. reg [AXIS_COUNT-1:0] ax_seg_load;
  254. wire [AXIS_COUNT-1:0] ax_seg_upper,ax_seg_armed,ax_seg_consumed;
  255. wire [2:0] ax_seg_mode [0:AXIS_COUNT-1];
  256. wire [31:0] ax_seg_count [0:AXIS_COUNT-1],ax_seg_half [0:AXIS_COUNT-1];
  257. wire [15:0] ax_seg_tag [0:AXIS_COUNT-1];
  258. // FX_CONSUME holds the fetched descriptor until the scanner accepts it.
  259. // Startup uses the same owner; no second wide descriptor copy is needed.
  260. wire [2:0] load_mode=fx_seg[10:8];
  261. wire load_upper=fx_seg[16],load_armed=fx_seg[18];
  262. wire [31:0] load_count=fx_seg[63:32],load_half=fx_seg[95:64];
  263. wire [15:0] load_tag=fx_seg[111:96];
  264. reg [AXIS_COUNT-1:0] ax_start_armed, ax_abort, ax_preempt;
  265. reg [AXIS_COUNT-1:0] ax_pos_load, ax_ctr_clear, ax_pos_zero, ax_lat_clear;
  266. wire [AXIS_COUNT*3-1:0] bank_seg_mode;
  267. wire [AXIS_COUNT*32-1:0] bank_seg_count;
  268. wire [AXIS_COUNT*32-1:0] bank_seg_half_us;
  269. wire [AXIS_COUNT*16-1:0] bank_seg_tag;
  270. wire [AXIS_COUNT*32-1:0] bank_position_value;
  271. wire [AXIS_COUNT*4-1:0] bank_done_reason;
  272. wire [AXIS_COUNT*16-1:0] bank_done_tag;
  273. wire [AXIS_COUNT*32-1:0] bank_done_executed;
  274. wire [AXIS_COUNT*32-1:0] bank_done_position;
  275. wire [AXIS_COUNT*32-1:0] bank_done_total;
  276. wire [AXIS_COUNT*32-1:0] bank_done_time;
  277. wire [AXIS_COUNT*32-1:0] bank_position;
  278. wire [AXIS_COUNT*32-1:0] bank_total_steps;
  279. wire [AXIS_COUNT*32-1:0] bank_lower_hit_count;
  280. wire [AXIS_COUNT*32-1:0] bank_upper_hit_count;
  281. wire [AXIS_COUNT*4-1:0] bank_last_stop_reason;
  282. wire [AXIS_COUNT*`MR_AXIS_RECORD_BYTES*8-1:0] bank_telemetry_record;
  283. wire bank_telemetry_ready,bank_telemetry_changing;
  284. wire [3:0] bank_telemetry_axis;
  285. wire [15:0] bank_telemetry_word;
  286. wire [15:0] queue_depth_word={2'd0,depth_r[telemetry_axis],8'd0};
  287. wire [15:0] queue_pause_word=queue_paused_r[telemetry_axis] ? (16'd1<<`MR_AXIS_FLAG_QUEUE_PAUSED_BIT) : 16'd0;
  288. // Parallel copy is diagnostic only; production has one addressed bank.
  289. reg [159:0] limit_record [0:AXIS_COUNT-1];
  290. reg [AXIS_COUNT-1:0] limit_history_valid;
  291. integer ev_select,ev_scan,ev_i;
  292. reg select_limit,reset_pending;
  293. // The history head follows the registered one-axis event producer.
  294. // Status sees every axis without arbitration stalls. A pending limit
  295. // event waits at most one rotation for its selected record to reach head.
  296. wire [AXIS_INDEX_BITS-1:0] history_scan_axis;
  297. wire event_dispatch_ready=!select_limit || ev_select==history_scan_axis;
  298. wire [AXIS_INDEX_BITS-1:0] limit_read_axis=history_scan_axis;
  299. wire [159:0] limit_read_record;
  300. motor_limit_history #(.AXIS_COUNT(AXIS_COUNT)) u_limit_history(
  301. .clk(clk),.rst_n(rst_n),.write_enable(|ax_limit_pulse),
  302. .write_data({ax_limit_time_high,ax_limit_data}),
  303. .read_axis(history_scan_axis),.read_data(limit_read_record));
  304. wire [159:0] status_limit_record=limit_history_valid[limit_read_axis] ? limit_read_record : 160'd0;
  305. reg [31:0] history_pair;
  306. wire history_selected=telemetry_word_addr>=`MR_AXIS_LAST_LIMIT_POSITION_OFFSET/2 &&
  307. telemetry_word_addr<(`MR_AXIS_LAST_LIMIT_TIME_HIGH_OFFSET+4)/2;
  308. assign telemetry_axis=history_selected ? limit_read_axis : bank_telemetry_axis;
  309. always @* begin
  310. case(telemetry_word_addr[4:1])
  311. `MR_AXIS_LAST_LIMIT_POSITION_OFFSET/4:history_pair=status_limit_record[95:64];
  312. `MR_AXIS_LAST_LIMIT_TOTAL_OFFSET/4:history_pair=status_limit_record[127:96];
  313. `MR_AXIS_LAST_LIMIT_TAG_OFFSET/4:history_pair={status_limit_record[47:40],8'd0,status_limit_record[63:48]};
  314. `MR_AXIS_LAST_LIMIT_TIME_LOW_OFFSET/4:history_pair=status_limit_record[31:0];
  315. `MR_AXIS_LAST_LIMIT_TIME_HIGH_OFFSET/4:history_pair=status_limit_record[159:128];
  316. default:history_pair=0;
  317. endcase
  318. end
  319. wire [15:0] history_word=telemetry_word_addr[0] ? history_pair[31:16] : history_pair[15:0];
  320. // Stop reason remains axis-owned in the low byte of the event/reason word.
  321. 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;
  322. assign telemetry_word_data=history_selected ? (history_word | history_stop_word) :
  323. (bank_telemetry_word | (telemetry_word_addr==0 ? queue_pause_word : (telemetry_word_addr==1 ? queue_depth_word : 16'd0)));
  324. wire status_changing=bank_telemetry_changing || qc_state!=QC_IDLE || cs_state!=0 ||
  325. fx_state!=FX_IDLE || qw_submit || ctl_valid || startup_seek || su_axis<AXIS_COUNT ||
  326. (|ax_done) || (|ax_seg_load) || (|ax_limit_pulse);
  327. assign motor_telemetry_ready=bank_telemetry_ready && !status_changing;
  328. always @(posedge clk or negedge rst_n) begin
  329. if(!rst_n)motor_status_epoch<=0;
  330. else if(status_changing)motor_status_epoch<=motor_status_epoch+1'b1;
  331. end
  332. generate for(g=0;g<AXIS_COUNT;g=g+1)begin:g_bank_ports
  333. assign internal_urgent_pending[g]=urgent_valid_r[g] && !urgent_from_queue[g];
  334. assign ax_seg_mode[g]=load_mode;
  335. assign ax_seg_upper[g]=load_upper;assign ax_seg_armed[g]=load_armed;
  336. assign ax_seg_count[g]=load_count;assign ax_seg_half[g]=load_half;assign ax_seg_tag[g]=load_tag;
  337. assign bank_seg_mode[g*3 +:3]=ax_seg_mode[g];
  338. assign bank_seg_count[g*32 +:32]=ax_seg_count[g];
  339. assign bank_seg_half_us[g*32 +:32]=ax_seg_half[g];
  340. assign bank_seg_tag[g*16 +:16]=ax_seg_tag[g];
  341. assign bank_position_value[g*32 +:32]=cs_value;
  342. assign ax_reason[g]=bank_done_reason[g*4 +:4];
  343. assign ax_dtag[g]=bank_done_tag[g*16 +:16];
  344. assign ax_dexec[g]=bank_done_executed[g*32 +:32];
  345. assign ax_dpos[g]=bank_done_position[g*32 +:32];
  346. assign ax_dtotal[g]=bank_done_total[g*32 +:32];
  347. assign ax_dtime[g]=bank_done_time[g*32 +:32];
  348. assign ax_pos[g]=bank_position[g*32 +:32];
  349. assign ax_total[g]=bank_total_steps[g*32 +:32];
  350. assign ax_lhits[g]=bank_lower_hit_count[g*32 +:32];
  351. assign ax_uhits[g]=bank_upper_hit_count[g*32 +:32];
  352. assign ax_last_reason[g]=bank_last_stop_reason[g*4 +:4];
  353. assign ax_record[g]=bank_telemetry_record[g*`MR_AXIS_RECORD_BYTES*8 +:`MR_AXIS_RECORD_BYTES*8];
  354. end endgenerate
  355. 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 (
  356. .seg_mode(bank_seg_mode),
  357. .seg_count(bank_seg_count),
  358. .seg_half_us(bank_seg_half_us),
  359. .seg_tag(bank_seg_tag),
  360. .position_value(bank_position_value),
  361. .done_reason(bank_done_reason),
  362. .done_tag(bank_done_tag),
  363. .done_executed(bank_done_executed),
  364. .done_position(bank_done_position),
  365. .done_total(bank_done_total),
  366. .done_time(bank_done_time),
  367. .position(bank_position),
  368. .total_steps(bank_total_steps),
  369. .lower_hit_count(bank_lower_hit_count),
  370. .upper_hit_count(bank_upper_hit_count),
  371. .last_stop_reason(bank_last_stop_reason),
  372. .telemetry_record(bank_telemetry_record),
  373. .telemetry_word_addr(telemetry_word_addr),.telemetry_axis(bank_telemetry_axis),.telemetry_word_data(bank_telemetry_word),
  374. .clk(clk),
  375. .rst_n(rst_n),
  376. .tick_us(tick_us),
  377. .limit_lower(ll_s2),
  378. .limit_upper(ul_s2),
  379. .seg_load(ax_seg_load),
  380. .seg_consumed(ax_seg_consumed),
  381. .seg_dir_upper(ax_seg_upper),
  382. .seg_armed(ax_seg_armed),
  383. .start_armed(ax_start_armed),
  384. .abort_req(ax_abort),
  385. .preempt_req(ax_preempt),
  386. .step_out(step_out_w),
  387. .dir_out(dir_out_w),
  388. .active(ax_active),
  389. .armed(ax_armed),
  390. .load_ready(ax_load_ready),
  391. .done_pulse(ax_done),
  392. .event_clock_us(time_us[31:0]),
  393. .event_clock_high(time_us[63:32]),
  394. .last_executed(),
  395. .position_reference_seen(ax_ref_seen),
  396. .lower_latched(ax_llat),
  397. .upper_latched(ax_ulat),
  398. .both_latched(ax_both),
  399. .clear_limit_latch(ax_lat_clear),
  400. .position_load(ax_pos_load),
  401. .counters_clear(ax_ctr_clear),
  402. .pos_zero_with_counters(ax_pos_zero),
  403. .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),
  404. .limit_event_time_high(ax_limit_time_high),
  405. .telemetry_ready(bank_telemetry_ready),.telemetry_changing(bank_telemetry_changing),
  406. .scan_deadline_miss(motor_scan_deadline_miss)
  407. );
  408. // Generated field offsets; add queue-owned values without overlapping core fields.
  409. generate
  410. if(PARALLEL_TELEMETRY)begin:diagnostic_status
  411. for(g=0;g<AXIS_COUNT;g=g+1) begin:g_tel
  412. wire [`MR_AXIS_RECORD_BYTES*8-1:0] queue_field={{(`MR_AXIS_RECORD_BYTES*8-6){1'b0}},depth_r[g]};
  413. wire [`MR_AXIS_RECORD_BYTES*8-1:0] pause_field={{(`MR_AXIS_RECORD_BYTES*8-1){1'b0}},queue_paused_r[g]};
  414. wire [159:0] history=limit_history_valid[g] ? limit_record[g] : 160'd0;
  415. reg [`MR_AXIS_RECORD_BYTES*8-1:0] record;
  416. always @* begin
  417. record=ax_record[g] | (queue_field << (`MR_AXIS_QUEUE_DEPTH_OFFSET*8)) |
  418. (pause_field << (`MR_AXIS_FLAGS_OFFSET*8+`MR_AXIS_FLAG_QUEUE_PAUSED_BIT));
  419. record[`MR_AXIS_LAST_LIMIT_POSITION_OFFSET*8+:32]=history[95:64];
  420. record[`MR_AXIS_LAST_LIMIT_TOTAL_OFFSET*8+:32]=history[127:96];
  421. record[`MR_AXIS_LAST_LIMIT_TAG_OFFSET*8+:16]=history[63:48];
  422. record[`MR_AXIS_LAST_LIMIT_EVENT_OFFSET*8+:8]=history[47:40];
  423. record[`MR_AXIS_LAST_LIMIT_TIME_LOW_OFFSET*8+:32]=history[31:0];
  424. record[`MR_AXIS_LAST_LIMIT_TIME_HIGH_OFFSET*8+:32]=history[159:128];
  425. end
  426. assign axis_telemetry[g*`MR_AXIS_RECORD_BYTES*8 +: `MR_AXIS_RECORD_BYTES*8]=record;
  427. end
  428. end else begin:no_parallel_status
  429. assign axis_telemetry=0;
  430. end
  431. endgenerate
  432. // ---- event ring ----
  433. motor_event_ring #(.DEPTH(64)) u_events (
  434. .clk(clk), .rst_n(rst_n),
  435. .wr_req(ev_wr_req), .wr_ack(ev_ack), .wr_axis(ev_axis), .wr_event(ev_code),
  436. .wr_tag(ev_tag), .wr_position(ev_pos), .wr_total(ev_total), .time_us(ev_timestamp),
  437. .rd_index(ev_rd_index), .rd_hold(ev_hold), .rd_data(ev_rd_data), .record_count(ev_count),
  438. .rd_byte_index(ev_rd_byte_index),.rd_byte_data(ev_rd_byte_data),
  439. .overflow_lsb(ev_overflow),
  440. .view_ready(ev_view_ready), .view_count(ev_view_count), .overflow_count(event_ring_overflow),
  441. .clear_n(ev_clear_n), .clear_count(ev_clear_count)
  442. );
  443. // Physical limit edges and segment completions have separate pending slots.
  444. // A held limit stopping a new request does not fabricate another edge event.
  445. reg [AXIS_COUNT-1:0] done_pending,limit_pending;
  446. reg [127:0] pending_record [0:AXIS_COUNT-1];
  447. // Persistent last-limit history doubles as the pending event record.
  448. // Clearing a pending bit or the host ring never destroys this history.
  449. reg [3:0] pending_drop_union;
  450. reg pending_limit_loss,pending_done_loss;
  451. wire [4:0] pending_loss_total={1'b0,pending_drop_union}+{4'd0,pending_limit_loss}+{4'd0,pending_done_loss};
  452. wire [8:0] pending_losses={4'd0,pending_loss_total};
  453. wire [127:0] selected_record=select_limit ? limit_read_record[127:0] : pending_record[ev_select];
  454. integer done_write_axis,limit_write_axis,record_axis;
  455. reg done_record_write;
  456. always @* begin
  457. done_write_axis=0;limit_write_axis=0;done_record_write=0;
  458. // The shared axis service emits at most one completion and one limit
  459. // record per clock. Two independent memories retain both kinds.
  460. for(record_axis=0;record_axis<AXIS_COUNT;record_axis=record_axis+1)begin
  461. if(ax_done[record_axis] && (event_for_stop(ax_reason[record_axis])!=0 ||
  462. (home_pending_r[record_axis] && ax_reason[record_axis]==SR_LOWER)))begin
  463. done_write_axis=record_axis;done_record_write=1;
  464. end
  465. if(ax_limit_pulse[record_axis])limit_write_axis=record_axis;
  466. end
  467. end
  468. // One addressed write port per bank allows RAM inference. Reset clears
  469. // validity bits below; stale storage is never emitted after reset.
  470. always @(posedge clk)begin
  471. if(PARALLEL_TELEMETRY && rst_n && (|ax_limit_pulse))limit_record[limit_write_axis]<={ax_limit_time_high,ax_limit_data};
  472. if(rst_n && done_record_write)
  473. pending_record[done_write_axis]<={ax_done_word[127:48],
  474. ((home_pending_r[done_write_axis] && ax_reason[done_write_axis]==SR_LOWER)?EV_HOME:event_for_stop(ax_reason[done_write_axis])),
  475. ax_done_word[39:0]};
  476. end
  477. function [7:0] event_for_stop;
  478. input [3:0] r;
  479. begin
  480. case(r)
  481. 1:event_for_stop=EV_SEGMENT;
  482. 4:event_for_stop=EV_ABORT;5:event_for_stop=EV_PREEMPT;6:event_for_stop=8'd6;
  483. 8:event_for_stop=EV_HOST_CAP;9:event_for_stop=EV_HOLD;
  484. default:event_for_stop=0;
  485. endcase
  486. end
  487. endfunction
  488. // Factor one-bit eligibility/priority from the wide descriptor datapath.
  489. // This preserves the original round-robin winner and one-clock arbitration.
  490. always @* begin
  491. for(fx_k=0;fx_k<AXIS_COUNT;fx_k=fx_k+1)
  492. fx_eligible[fx_k]=ax_load_ready[fx_k] && !ax_seg_load[fx_k] && !queue_paused_r[fx_k] &&
  493. (cs_state==0 || (cs_state==3 && cs_start_mask[fx_k]) || (seek_context && cs_mask[fx_k])) &&
  494. (urgent_valid_r[fx_k] || depth_r[fx_k]!=0);
  495. fx_candidate=0;fx_candidate_valid=0;fx_probe=0;
  496. for(fx_k=AXIS_COUNT-1;fx_k>=0;fx_k=fx_k-1)begin
  497. fx_probe=fx_round+fx_k;
  498. if(fx_probe>=AXIS_COUNT)fx_probe=fx_probe-AXIS_COUNT;
  499. if(fx_eligible[fx_probe])begin fx_candidate=fx_probe[7:0];fx_candidate_valid=1;end
  500. end
  501. end
  502. always @* begin
  503. ev_select=-1;select_limit=0;pending_drop_union=0;pending_limit_loss=0;pending_done_loss=0;
  504. for(ev_scan=AXIS_COUNT-1;ev_scan>=0;ev_scan=ev_scan-1)
  505. if(done_pending[ev_scan]) ev_select=ev_scan;
  506. for(ev_scan=AXIS_COUNT-1;ev_scan>=0;ev_scan=ev_scan-1)
  507. if(limit_pending[ev_scan]) begin ev_select=ev_scan;select_limit=1;end
  508. // The core emits at most one record of each kind per clock. OR the
  509. // one-hot-qualified fields instead of building an axis-wide adder chain.
  510. // This uses constant per-axis indices, not the rejected dynamic selector.
  511. for(ev_scan=0;ev_scan<AXIS_COUNT;ev_scan=ev_scan+1) begin
  512. if(ax_limit_pulse[ev_scan]) begin
  513. pending_drop_union=pending_drop_union | ax_limit_drops[ev_scan*4+:4];
  514. if(limit_pending[ev_scan] && (ev_wr_req || reset_pending || ev_select!=ev_scan || !select_limit || !event_dispatch_ready)) pending_limit_loss=1;
  515. end
  516. if(ax_done[ev_scan] && (event_for_stop(ax_reason[ev_scan])!=0 || (home_pending_r[ev_scan] && ax_reason[ev_scan]==SR_LOWER)) &&
  517. done_pending[ev_scan] && (ev_wr_req || reset_pending || ev_select!=ev_scan || select_limit)) pending_done_loss=1;
  518. end
  519. end
  520. always @(posedge clk or negedge rst_n) begin
  521. if(!rst_n) begin
  522. ev_wr_req<=0;ev_code<=0;ev_tag<=0;ev_pos<=0;ev_total<=0;ev_axis<=4'hF;ev_timestamp<=0;
  523. done_pending<=0;limit_pending<=0;limit_history_valid<=0;event_pending_overflow<=0;reset_pending<=1;
  524. end else begin
  525. event_pending_overflow<=event_pending_overflow+pending_losses;
  526. if(ev_wr_req && ev_ack) ev_wr_req<=0;
  527. else if(!ev_wr_req && reset_pending) begin
  528. 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;
  529. end else if(!ev_wr_req && ev_select>=0 && event_dispatch_ready) begin
  530. ev_wr_req<=1;ev_axis<=selected_record[35:32];ev_timestamp<=selected_record[31:0];ev_code<=selected_record[47:40];
  531. ev_tag<=selected_record[63:48];ev_pos<=selected_record[95:64];ev_total<=selected_record[127:96];
  532. if(select_limit)limit_pending[ev_select]<=0;else done_pending[ev_select]<=0;
  533. end
  534. for(ev_i=0;ev_i<AXIS_COUNT;ev_i=ev_i+1) begin
  535. if(ax_limit_pulse[ev_i])begin limit_pending[ev_i]<=1;limit_history_valid[ev_i]<=1;end
  536. 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
  537. done_pending[ev_i]<=1;
  538. end
  539. end
  540. end
  541. end
  542. // ---- queue commit helper wires ----
  543. wire [5:0] ax_depth_0 = depth_r[0];
  544. // Unpacked-array reduction (|depth_r) is not legal Verilog; fold per axis.
  545. reg unused_depth;
  546. integer ud_i;
  547. always @* begin
  548. unused_depth = 1'b0;
  549. for (ud_i = 0; ud_i < AXIS_COUNT; ud_i = ud_i + 1)
  550. unused_depth = unused_depth | (depth_r[ud_i] != 6'd0);
  551. end
  552. // ---- main sequencer: batch validate/commit + fetch + control ----
  553. always @(posedge clk or negedge rst_n) begin
  554. if (!rst_n) begin
  555. qc_state <= QC_IDLE;qc_space_axis<=0; qc_index <= 4'd0;start_credit<=0;
  556. stage_byte_index<=0;stage_for_write<=0;stage_data<=0;
  557. qw_accept <= 1'b0; qw_status <= 8'd0; qw_detail <= 16'd0;
  558. ctl_accept <= 1'b0; ctl_status <= 8'd0; ctl_detail <= 16'd0;
  559. fx_state <= FX_IDLE; fx_axis <= 2'd0; fx_from_urgent <= 1'b0;fx_round<=0;cs_start_mask<=0;
  560. ax_seg_load <= {AXIS_COUNT{1'b0}};
  561. ax_start_armed <= {AXIS_COUNT{1'b0}};
  562. ax_abort <= {AXIS_COUNT{1'b0}};
  563. ax_preempt <= {AXIS_COUNT{1'b0}};
  564. ax_pos_load <= {AXIS_COUNT{1'b0}};
  565. ax_ctr_clear <= {AXIS_COUNT{1'b0}};
  566. ax_pos_zero <= {AXIS_COUNT{1'b0}};
  567. ax_lat_clear <= {AXIS_COUNT{1'b0}};
  568. for (i = 0; i < AXIS_COUNT; i = i + 1) begin
  569. depth_r[i] <= 6'd0; head_r[i] <= 6'd0; queue_paused_r[i] <= 1'b0;
  570. urgent_valid_r[i] <= 1'b0; urgent_from_queue[i] <= 1'b0;
  571. home_pending_r[i] <= 1'b0;
  572. end
  573. q_we <= 1'b0;q_re<=0;
  574. cs_state <= 2'd0; su_axis <= AXIS_COUNT;su_wait<=0;
  575. end else begin
  576. // default single-cycle pulses
  577. ax_seg_load <= {AXIS_COUNT{1'b0}};
  578. ax_start_armed <= {AXIS_COUNT{1'b0}};
  579. ax_abort <= {AXIS_COUNT{1'b0}};
  580. ax_preempt <= {AXIS_COUNT{1'b0}};
  581. ax_pos_load <= {AXIS_COUNT{1'b0}};
  582. ax_ctr_clear <= {AXIS_COUNT{1'b0}};
  583. ax_lat_clear <= {AXIS_COUNT{1'b0}};
  584. qw_accept <= 1'b0;
  585. ctl_accept <= 1'b0;
  586. q_we <= 1'b0;q_re<=0;
  587. // ---- MOTOR_QUEUE_WRITE: quiesce loader, validate, write, publish ----
  588. for(i=0;i<AXIS_COUNT;i=i+1)
  589. if(ax_done[i] && (ax_reason[i]==2 || ax_reason[i]==3 || ax_reason[i]==7))
  590. queue_paused_r[i]<=1;
  591. // Active STEP execution never waits for these RAM transactions.
  592. if(qw_submit && qc_state==QC_IDLE) begin
  593. if(qw_count<1 || qw_count>8) begin
  594. qw_accept<=1;qw_status<=ST_BAD_VALUE;qw_detail<=0;
  595. end else begin qc_count<=qw_count;qc_state<=QC_QUIESCE;end
  596. end else if(qc_state==QC_QUIESCE && fx_state==FX_IDLE && ax_seg_load==0) begin
  597. qc_index<=0;qc_is_urgent<=0;qc_space_axis<=0;
  598. for(i=0;i<AXIS_COUNT;i=i+1) begin
  599. qc_new_count[i]<=0;
  600. qc_first_of_axis[i]<=1;qc_want_preempt[i]<=0;qc_want_clear[i]<=0;qc_resume[i]<=0;
  601. end
  602. stage_byte_index<=0;stage_for_write<=0;qc_state<=QC_STAGE_WAIT;
  603. end else if(qc_state==QC_STAGE_WAIT) begin
  604. qc_state<=QC_STAGE_READ;
  605. end else if(qc_state==QC_STAGE_READ) begin
  606. stage_data<={stage_read_data,stage_data[127:8]};
  607. if(stage_byte_index==15) qc_state<=stage_for_write ? QC_WRITE : QC_VALIDATE;
  608. else begin stage_byte_index<=stage_byte_index+1'b1;qc_state<=QC_STAGE_WAIT;end
  609. end else if(qc_state==QC_VALIDATE) begin
  610. if(qc_index==qc_count) qc_state<=QC_SPACE;
  611. else if(f_axis(stage_data)>=AXIS_COUNT || f_mode(stage_data)>M_HOLD ||
  612. seg_field_bad(stage_data,stage_min_half) ||
  613. (!qc_first_of_axis[qc_axis] && (f_preempt(stage_data) || f_clear(stage_data)))) begin
  614. qw_accept<=1;qw_detail<=qc_index;qc_state<=QC_IDLE;
  615. qw_status<=(f_axis(stage_data)>=AXIS_COUNT) ? 8'd6 :
  616. ((f_mode(stage_data)>M_HOLD) ? 8'd7 : ST_BAD_VALUE);
  617. end else begin
  618. if(qc_first_of_axis[qc_axis]) begin
  619. qc_resume[qc_axis]<=!f_arm(stage_data);
  620. qc_want_clear[qc_axis]<=f_clear(stage_data);
  621. qc_want_preempt[qc_axis]<=f_preempt(stage_data);
  622. if(f_preempt(stage_data)) begin
  623. qc_is_urgent[qc_index]<=1;
  624. end else qc_new_count[qc_axis]<=qc_new_count[qc_axis]+1'b1;
  625. qc_first_of_axis[qc_axis]<=0;
  626. end else qc_new_count[qc_axis]<=qc_new_count[qc_axis]+1'b1;
  627. qc_index<=qc_index+1'b1;stage_byte_index<=0;
  628. qc_state<=qc_index+1'b1==qc_count ? QC_SPACE : QC_STAGE_WAIT;
  629. end
  630. end else if(qc_state==QC_SPACE) begin
  631. if(qc_space_bad) begin
  632. qw_accept<=1;qw_status<=8'd5;qw_detail<=0;qc_state<=QC_IDLE;
  633. end else begin
  634. // One queue-space/address datapath serves one scratch
  635. // row per clock. No live queue state or RAM is published
  636. // until every row and every descriptor has passed.
  637. qc_proj_depth[qc_space_axis]<=qc_space_total[5:0];
  638. qc_new_head[qc_space_axis]<=qc_want_preempt[qc_space_axis] && !qc_want_clear[qc_space_axis] ?
  639. (head_r[qc_space_axis][4:0]-{1'b0,qc_new_count[qc_space_axis]}) : head_r[qc_space_axis][4:0];
  640. qc_append_pos[qc_space_axis]<=qc_want_preempt[qc_space_axis] ?
  641. (qc_want_clear[qc_space_axis] ? head_r[qc_space_axis][4:0] :
  642. ((head_r[qc_space_axis][4:0]-{1'b0,qc_new_count[qc_space_axis]}) & 5'h1F)) :
  643. ((head_r[qc_space_axis][4:0]+qc_base_depth[qc_space_axis][4:0]) & 5'h1F);
  644. if(qc_space_axis==AXIS_COUNT-1)begin
  645. qc_index<=0;stage_byte_index<=0;stage_for_write<=1;qc_state<=QC_STAGE_WAIT;
  646. end else qc_space_axis<=qc_space_axis+1'b1;
  647. end
  648. end else if(qc_state==QC_WRITE) begin
  649. if(qc_index==qc_count) qc_state<=QC_DRAIN;
  650. else if(!q_write_busy)begin
  651. if(qc_is_urgent[qc_index])begin
  652. // Reserved urgent slots follow all waiting slots.
  653. // Write after full validation, publish valid atomically.
  654. q_wr_addr<=AXIS_COUNT*32+qc_axis;
  655. end else begin
  656. q_wr_addr<=qc_axis*32+qc_append_pos[qc_axis][4:0];
  657. qc_append_pos[qc_axis]<=(qc_append_pos[qc_axis]+1'b1)&6'd31;
  658. end
  659. q_wr_data<=stage_data;q_we<=1;
  660. qc_index<=qc_index+1'b1;stage_byte_index<=0;
  661. qc_state<=qc_index+1'b1==qc_count ? QC_DRAIN : QC_STAGE_WAIT;
  662. end
  663. end else if(qc_state==QC_DRAIN && !q_write_busy) begin
  664. // Last synchronous RAM write has completed before any reader
  665. // can see the new head/depth. All axes publish on this edge.
  666. for(i=0;i<AXIS_COUNT;i=i+1) begin
  667. if(!qc_first_of_axis[i]) begin
  668. home_pending_r[i]<=0;
  669. depth_r[i]<=qc_proj_depth[i];head_r[i]<=qc_new_head[i];
  670. if(qc_want_clear[i]) urgent_valid_r[i]<=0;
  671. if(qc_want_preempt[i]) begin
  672. urgent_valid_r[i]<=1;urgent_from_queue[i]<=1;ax_preempt[i]<=1;
  673. end
  674. // A new nonarmed command or preempt explicitly resumes
  675. // an axis paused by Abort/limit. ARM alone cannot do so.
  676. if(qc_resume[i] || qc_want_preempt[i]) queue_paused_r[i]<=0;
  677. end
  678. end
  679. qc_state<=QC_PUBLISH;
  680. end else if(qc_state==QC_PUBLISH) begin
  681. qw_accept<=1;qw_status<=ST_OK;qw_detail<=0;qc_state<=QC_IDLE;
  682. end
  683. // ---- execution fetch: idle axis takes urgent, else head ----
  684. case (fx_state)
  685. FX_IDLE: begin
  686. // Arbitration selects only an axis. One datapath below
  687. // owns address and descriptor construction for that winner.
  688. begin
  689. if (fx_candidate_valid && !ctl_valid && qc_state==QC_IDLE && !qw_submit &&
  690. su_axis>=AXIS_COUNT && !startup_seek) begin
  691. if (urgent_valid_r[fx_candidate]) begin
  692. fx_axis <= fx_candidate[AXIS_INDEX_BITS-1:0];
  693. fx_from_urgent <= 1'b1;
  694. if(urgent_from_queue[fx_candidate])begin
  695. fx_state<=FX_RD;q_rd_addr<=AXIS_COUNT*32+fx_candidate;q_re<=1;
  696. end else begin
  697. // The accepted seek context is held until all
  698. // selected internal descriptors are consumed.
  699. fx_state<=FX_LOAD;
  700. fx_seg<={32'd0,(cs_value!=0 ? cs_value :
  701. (fx_candidate==0 ? X_DEFAULT_HALF_US : (fx_candidate==1 ? Y_DEFAULT_HALF_US : Z_DEFAULT_HALF_US))),
  702. 32'd0,8'd0,8'd0,(cs_op==OP_HOME_LOWER ? 8'd1 : 8'd2),fx_candidate[7:0]};
  703. end
  704. end else if (depth_r[fx_candidate] != 0) begin
  705. fx_state <= FX_RD;
  706. fx_axis <= fx_candidate[AXIS_INDEX_BITS-1:0];
  707. fx_from_urgent <= 1'b0;
  708. q_rd_addr <= fx_candidate * 32 + head_r[fx_candidate][4:0];
  709. q_re<=1;
  710. end
  711. end
  712. end
  713. end
  714. FX_RD: begin
  715. fx_state <= FX_WAIT;
  716. end
  717. FX_WAIT: if(q_read_valid)begin
  718. fx_seg <= q_rd;
  719. fx_state <= FX_LOAD;
  720. end
  721. FX_LOAD: begin
  722. ax_seg_load[fx_axis] <= 1'b1;
  723. // Resolve ARM once using the old credit, before clearing it.
  724. fx_seg[18] <= cs_state==3 ? 1'b1 : (f_arm(fx_seg) && !start_credit[fx_axis]);
  725. start_credit[fx_axis] <= 0;
  726. if (fx_from_urgent) urgent_valid_r[fx_axis] <= 1'b0;
  727. else begin
  728. // pop head
  729. head_r[fx_axis] <= (head_r[fx_axis] == 31) ? 6'd0 : head_r[fx_axis] + 6'd1;
  730. depth_r[fx_axis] <= depth_r[fx_axis] - 6'd1;
  731. // Each head's ARM bit is loaded into the axis;
  732. // the pause latch belongs to Abort/limit, not ARM.
  733. end
  734. fx_state <= FX_CONSUME;
  735. fx_round<=fx_axis==AXIS_COUNT-1 ? 0 : fx_axis+1'b1;
  736. end
  737. FX_CONSUME:if(ax_seg_consumed[fx_axis])fx_state<=FX_IDLE;
  738. default: fx_state <= FX_IDLE;
  739. endcase
  740. // ---- MOTOR_CONTROL: validate the full mask before any effect ----
  741. if (ctl_valid && cs_state == 0) begin
  742. ctl_detail<=16'hFFFF;
  743. cs_start_mask<=0;
  744. if(ctl_axis_mask==0 || (ctl_axis_mask >> AXIS_COUNT)!=0) begin
  745. ctl_accept<=1;ctl_status<=ST_BAD_AXIS;ctl_detail<=0;
  746. end else if((ctl_flags & ((ctl_op==OP_ABORT || ctl_op==OP_CLEAR_COUNTERS) ? 8'hFE : 8'hFF))!=0 ||
  747. (ctl_op==OP_SET_POSITION && ((ctl_axis_mask & (ctl_axis_mask-1'b1))!=0))) begin
  748. ctl_accept<=1;ctl_status<=ST_BAD_VALUE;ctl_detail<=0;
  749. end else if((ctl_op==OP_HOME_LOWER || ctl_op==OP_SEEK_UPPER) && ctl_value!=0 &&
  750. ((ctl_axis_mask[0] && ctl_value<X_MIN_HALF_US) ||
  751. (ctl_axis_mask[1] && ctl_value<Y_MIN_HALF_US) ||
  752. ((ctl_axis_mask >> 2)!=0 && ctl_value<Z_MIN_HALF_US))) begin
  753. ctl_accept<=1;ctl_status<=ST_BAD_VALUE;ctl_detail<=0;
  754. end else cs_state<=1;
  755. end else if(cs_state==1 && fx_state==FX_IDLE && qc_state==QC_IDLE) begin
  756. // All selected control pulses commit together. SEEK uses the
  757. // urgent loader after the old active acknowledges the abort;
  758. // loading and aborting the same active cycle loses the load.
  759. for(i=AXIS_COUNT-1;i>=0;i=i-1) begin
  760. if(cs_mask[i]) begin
  761. case(cs_op)
  762. OP_START_ARMED: begin
  763. queue_paused_r[i]<=0;
  764. if(ax_armed[i] || (ax_load_ready[i] && (depth_r[i]!=0 || urgent_valid_r[i]))) cs_start_mask[i]<=1;
  765. else ctl_detail<=i;
  766. end
  767. OP_ABORT: begin
  768. ax_abort[i]<=1;queue_paused_r[i]<=1;
  769. if(cs_flags[0]) begin depth_r[i]<=0;urgent_valid_r[i]<=0;end
  770. end
  771. OP_CLEAR_QUEUE: begin depth_r[i]<=0;urgent_valid_r[i]<=0;end
  772. OP_CLEAR_COUNTERS: begin ax_ctr_clear[i]<=1;ax_pos_zero[i]<=cs_flags[0];end
  773. OP_SET_POSITION: ax_pos_load[i]<=1;
  774. OP_CLEAR_LIMIT_LATCH: ax_lat_clear[i]<=1;
  775. OP_HOME_LOWER,OP_SEEK_UPPER: begin
  776. ax_preempt[i]<=1;depth_r[i]<=0;queue_paused_r[i]<=0;
  777. urgent_valid_r[i]<=1;urgent_from_queue[i]<=0;
  778. home_pending_r[i]<=(cs_op==OP_HOME_LOWER);
  779. end
  780. endcase
  781. end
  782. end
  783. cs_state<=cs_op==OP_START_ARMED ? 3 : 2;
  784. end else if(cs_state==3 && (ax_armed & cs_start_mask)==cs_start_mask && fx_state==FX_IDLE && ax_seg_load==0) begin
  785. ax_start_armed<=cs_start_mask;
  786. cs_state<=2;
  787. // Keep the shared value stable until the scanner consumed all
  788. // control pulses. ACK then releases the one command context.
  789. end else if(cs_state==2 && !bank_telemetry_changing &&
  790. (!seek_context || (internal_urgent_pending==0 && fx_state==FX_IDLE && ax_seg_load==0))) begin
  791. ctl_accept<=1;ctl_status<=ST_OK;cs_state<=0;
  792. end
  793. // ---- startup: submit internal SEEK_LOWER per axis ----
  794. if (startup_seek) begin
  795. su_axis <= 2'd0;su_wait<=0;
  796. end else if (su_axis < AXIS_COUNT && fx_state==FX_IDLE) begin
  797. if(su_wait)begin
  798. if(ax_seg_consumed[su_axis])begin su_wait<=0;su_axis<=su_axis+1'b1;end
  799. end else begin
  800. ax_seg_load[su_axis] <= 1'b1;
  801. fx_seg <= {32'd0,((su_axis==0)?X_DEFAULT_HALF_US:((su_axis==1)?Y_DEFAULT_HALF_US:Z_DEFAULT_HALF_US)),
  802. 32'd0,8'd0,8'd0,8'd1,8'd0}; // unlimited SEEK_LOWER, no ARM
  803. su_wait<=1;
  804. end
  805. end
  806. end
  807. end
  808. endmodule
  809. // The core emits the previous scan axis once per clock. Reset starts this
  810. // head one axis behind the core, so a new event always updates the head as
  811. // it rotates to the tail. Validity remains in the owner; uninitialized data
  812. // never becomes a status/event record. No dynamic wide address mux is needed.
  813. module motor_limit_history #(
  814. parameter integer AXIS_COUNT=3,
  815. parameter integer ADDRESS_WIDTH=(AXIS_COUNT<=1)?1:$clog2(AXIS_COUNT)
  816. )(
  817. input wire clk,rst_n,write_enable,
  818. output reg [ADDRESS_WIDTH-1:0] read_axis,
  819. input wire [159:0] write_data,
  820. output wire [159:0] read_data
  821. );
  822. reg [159:0] records [0:AXIS_COUNT-1];
  823. integer i;
  824. always @(posedge clk or negedge rst_n)begin
  825. if(!rst_n)read_axis<=AXIS_COUNT-1;
  826. else read_axis<=read_axis==AXIS_COUNT-1 ? 0 : read_axis+1'b1;
  827. end
  828. always @(posedge clk)if(rst_n)begin
  829. for(i=0;i<AXIS_COUNT-1;i=i+1)records[i]<=records[i+1];
  830. records[AXIS_COUNT-1]<=write_enable ? write_data : records[0];
  831. end
  832. assign read_data=records[0];
  833. endmodule
  834. // Validated queue descriptors need85 bits: mode3, direction1, arm1,
  835. // count32, half-period32 and tag16. Axis is implicit in the slot address;
  836. // preempt/clear/reserved fields have already been consumed by batch commit.
  837. // Five18-bit words preserve the external16-byte descriptor contract while
  838. // packing96 waiting plus3 urgent slots into495 words of one512x18 BRAM.
  839. module motor_queue_storage #(parameter integer SLOTS=96,
  840. parameter integer SLOT_AW=$clog2(SLOTS)) (
  841. input wire clk,rst_n,wr_req,rd_req,
  842. input wire [SLOT_AW-1:0] wr_slot,rd_slot,
  843. input wire [127:0] wr_data,
  844. output wire wr_busy,
  845. output reg [127:0] rd_data,
  846. output reg rd_valid
  847. );
  848. localparam integer MEMORY_AW=$clog2(SLOTS*5);
  849. reg write_active,read_active,read_phase;
  850. reg [2:0] write_word,read_word;
  851. reg [MEMORY_AW-1:0] write_base,read_base;
  852. reg [89:0] write_shift,read_shift;
  853. wire [17:0] memory_data;
  854. wire [89:0] assembled={memory_data,read_shift[89:18]};
  855. wire [MEMORY_AW-1:0] write_address=write_base+write_word;
  856. wire [MEMORY_AW-1:0] read_address=read_base+read_word;
  857. assign wr_busy=write_active || wr_req;
  858. ef2_bram_dp #(.DATA_WIDTH(18),.ADDR_WIDTH(MEMORY_AW),.DATA_DEPTH(SLOTS*5)) u_memory (
  859. .clk(clk),.a_en(write_active),.a_we(write_active),.a_addr(write_address),
  860. .a_wdata(write_shift[17:0]),.a_rdata(),.b_en(1'b1),.b_we(1'b0),
  861. .b_addr(read_address),.b_wdata(18'd0),.b_rdata(memory_data)
  862. );
  863. always @(posedge clk or negedge rst_n)begin
  864. if(!rst_n)begin
  865. write_active<=0;read_active<=0;read_phase<=0;write_word<=0;read_word<=0;
  866. write_base<=0;read_base<=0;write_shift<=0;read_shift<=0;rd_data<=0;rd_valid<=0;
  867. end else begin
  868. rd_valid<=0;
  869. if(wr_req && !write_active)begin
  870. 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]};
  871. write_base<=wr_slot*5;write_word<=0;write_active<=1;
  872. end else if(write_active)begin
  873. write_shift<=write_shift>>18;
  874. if(write_word==4)write_active<=0;
  875. else write_word<=write_word+1'b1;
  876. end
  877. if(rd_req && !read_active)begin
  878. read_base<=rd_slot*5;read_word<=0;read_phase<=0;read_active<=1;
  879. end else if(read_active)begin
  880. if(!read_phase)read_phase<=1;
  881. else begin
  882. read_phase<=0;read_shift<=assembled;
  883. if(read_word==4)begin
  884. read_active<=0;rd_valid<=1;
  885. rd_data<={16'd0,assembled[84:69],assembled[68:37],assembled[36:5],
  886. 8'd0,5'd0,assembled[4],1'b0,assembled[3],5'd0,assembled[2:0],8'd0};
  887. end else read_word<=read_word+1'b1;
  888. end
  889. end
  890. end
  891. end
  892. endmodule