prod2u_mrdimm_uart_e2e_tb.v 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. `timescale 1ns/1ps
  2. // UART-level end-to-end test for the MRDIMM Host wire path over the final
  3. // 8-bit-length protocol, driven through the shared host_uart_endpoint
  4. // (real 921600/25 MHz bit timing: CLKS_PER_BIT=27) with the product's own
  5. // frame sizes: a 68-byte SET_ALL (fan/PTC plus twelve 5-byte axis slots),
  6. // a 4-byte MOTOR_DONE reply and a 63-byte GET_DATA reply.
  7. //
  8. // SET_ALL payload layout (AGENTS.md / contracts): 0..3 FAN, 4..7 logical
  9. // PTC, axis i slot at 8 + 5*i = {action:u8, steps:u16LE, half_period_us:u16LE}.
  10. // The request/reply CRCs are the independent Python codec
  11. // (tools/crc16_ccitt_table.frame_bytes) vectors, hand-embedded; the TB
  12. // verifies every received frame with its own bit-serial CRC over the
  13. // decoded wire bytes, never with the DUT's accumulator.
  14. //
  15. // NOTE (protocol worker, 2026-09-20): this bench covers the shared
  16. // transport primitives plus the MRDIMM frame sizes. The product command
  17. // core (mrdimm_host_command) is mid-migration to the frozen 8-bit macro
  18. // set by its owner; when it lands, its own host-level bench re-attaches
  19. // the command semantics.
  20. module prod2u_mrdimm_uart_e2e_tb;
  21. localparam integer CLKS = 27;
  22. localparam integer TIMEOUT_CYCLES = 250000;
  23. reg clk = 1'b0;
  24. reg rst_n = 1'b0;
  25. reg rs232_rx = 1'b1;
  26. wire rs232_tx;
  27. reg ttl_rx = 1'b1;
  28. wire ttl_tx;
  29. reg req_ready = 1'b0;
  30. wire req_valid;
  31. wire [7:0] req_type;
  32. wire [7:0] req_length;
  33. wire req_ram_wr_en;
  34. wire [7:0] req_ram_wr_addr;
  35. wire [7:0] req_ram_wr_data;
  36. wire [7:0] req_ram_rd_addr;
  37. wire [7:0] req_ram_rd_data;
  38. reg tx_start = 1'b0;
  39. reg [7:0] tx_type = 8'd0;
  40. reg [7:0] tx_length = 8'd0;
  41. wire tx_busy;
  42. wire tx_done;
  43. wire [7:0] tx_ram_rd_addr;
  44. wire [7:0] tx_ram_rd_data;
  45. integer i;
  46. integer k;
  47. always #20 clk = ~clk; // 25 MHz
  48. host_uart_endpoint #(
  49. .CLKS_PER_BIT(CLKS),
  50. .INTER_BYTE_TIMEOUT_CYCLES(TIMEOUT_CYCLES),
  51. .MAX_PAYLOAD(128),
  52. .ADDR_WIDTH(8)
  53. ) dut (
  54. .clk(clk), .rst_n(rst_n),
  55. .rs232_rx(rs232_rx), .rs232_tx(rs232_tx),
  56. .ttl_rx(ttl_rx), .ttl_tx(ttl_tx),
  57. .req_valid(req_valid), .req_ready(req_ready),
  58. .req_type(req_type), .req_length(req_length),
  59. .req_ram_wr_en(req_ram_wr_en), .req_ram_wr_addr(req_ram_wr_addr),
  60. .req_ram_wr_data(req_ram_wr_data),
  61. .req_ram_rd_addr(req_ram_rd_addr), .req_ram_rd_data(req_ram_rd_data),
  62. .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
  63. .tx_busy(tx_busy), .tx_done(tx_done),
  64. .tx_ram_rd_addr(tx_ram_rd_addr), .tx_ram_rd_data(tx_ram_rd_data)
  65. );
  66. // Request staging RAM (128 bytes); parser owns the write port.
  67. reg [7:0] req_mem [0:127];
  68. initial begin
  69. for (i = 0; i < 128; i = i + 1) req_mem[i] = 8'hA9;
  70. end
  71. always @(posedge clk) begin
  72. if (req_ram_wr_en) req_mem[req_ram_wr_addr] <= req_ram_wr_data;
  73. end
  74. assign req_ram_rd_data = req_mem[req_ram_rd_addr];
  75. integer req_writes;
  76. initial req_writes = 0;
  77. always @(negedge clk) begin
  78. if (req_ram_wr_en) begin
  79. req_writes = req_writes + 1;
  80. if (req_ram_wr_addr >= 128)
  81. $fatal(1, "request RAM address out of range %0d", req_ram_wr_addr);
  82. end
  83. end
  84. // Reply staging RAM (128 bytes, synchronous read) feeds frame_tx.
  85. reg rep_wr_en = 1'b0;
  86. reg [7:0] rep_wr_addr = 8'd0;
  87. reg [7:0] rep_wr_data = 8'd0;
  88. reply_ram #(.DEPTH(128), .ADDR_WIDTH(8)) u_reply (
  89. .clk(clk), .wr_en(rep_wr_en), .wr_addr(rep_wr_addr),
  90. .wr_data(rep_wr_data), .rd_addr(tx_ram_rd_addr),
  91. .rd_data(tx_ram_rd_data)
  92. );
  93. // ---- request frame buffer sent over the wire ----
  94. reg [7:0] frame_buf [0:95];
  95. integer frame_len;
  96. // ---- independent CRC-16/CCITT-FALSE over decoded wire bytes ----
  97. reg [7:0] rx_snapshot [0:511];
  98. integer rx_snapshot_n;
  99. initial rx_snapshot_n = 0;
  100. function [15:0] tb_crc;
  101. input integer base;
  102. input integer nbody;
  103. integer ck, bitno;
  104. reg [15:0] c;
  105. begin
  106. c = 16'hFFFF;
  107. for (ck = 0; ck < nbody; ck = ck + 1) begin
  108. c = c ^ {rx_snapshot[(base + ck) & 511], 8'd0};
  109. for (bitno = 0; bitno < 8; bitno = bitno + 1)
  110. c = c[15] ? (c << 1) ^ 16'h1021 : c << 1;
  111. end
  112. tb_crc = c;
  113. end
  114. endfunction
  115. // TB-side CRC over the frame buffer being sent (TYPE+LENGTH+PAYLOAD).
  116. function [15:0] buf_crc;
  117. input integer nbody;
  118. integer ck, bitno;
  119. reg [15:0] c;
  120. begin
  121. c = 16'hFFFF;
  122. for (ck = 0; ck < nbody; ck = ck + 1) begin
  123. c = c ^ {frame_buf[(2 + ck) & 511], 8'd0};
  124. for (bitno = 0; bitno < 8; bitno = bitno + 1)
  125. c = c[15] ? (c << 1) ^ 16'h1021 : c << 1;
  126. end
  127. buf_crc = c;
  128. end
  129. endfunction
  130. // ---- real UART stimulus on the RS232 RX line ----
  131. task send_byte;
  132. input [7:0] value;
  133. integer kk;
  134. begin
  135. rs232_rx = 1'b0;
  136. #1080;
  137. for (kk = 0; kk < 8; kk = kk + 1) begin
  138. rs232_rx = value[kk];
  139. #1080;
  140. end
  141. rs232_rx = 1'b1;
  142. #1080;
  143. end
  144. endtask
  145. task send_frame;
  146. integer kk;
  147. begin
  148. for (kk = 0; kk < frame_len; kk = kk + 1) send_byte(frame_buf[kk]);
  149. end
  150. endtask
  151. task await_req;
  152. input [7:0] etype;
  153. input [7:0] elen;
  154. integer guard;
  155. begin
  156. guard = 0;
  157. while (!req_valid && guard < 200000) begin
  158. @(posedge clk);
  159. guard = guard + 1;
  160. end
  161. if (!req_valid) $fatal(1, "request not parsed type=%h", etype);
  162. if (req_type !== etype || req_length !== elen)
  163. $fatal(1, "request metadata mismatch type=%h len=%0d",
  164. req_type, req_length);
  165. end
  166. endtask
  167. task accept_req;
  168. begin
  169. req_ready = 1'b1;
  170. while (req_valid) @(posedge clk);
  171. @(posedge clk);
  172. @(negedge clk);
  173. req_ready = 1'b0;
  174. repeat (2) @(posedge clk);
  175. while (dut.rx_owner != 2'd0) @(posedge clk);
  176. repeat (4) @(posedge clk);
  177. end
  178. endtask
  179. // ---- TX decoder: samples RS232 and TTL at the same bit centres, so
  180. // every decoded byte also proves the broadcast copies agree bit for
  181. // bit. Mid-bit sampling (1.5 bit periods after the start fall). ----
  182. reg [7:0] dec_byte;
  183. task decode_byte_both;
  184. integer b;
  185. begin
  186. @(negedge rs232_tx);
  187. #(CLKS * 40 + CLKS * 20);
  188. for (b = 0; b < 8; b = b + 1) begin
  189. if (rs232_tx !== ttl_tx)
  190. $fatal(1, "TX broadcast diverged at bit %0d", b);
  191. dec_byte[b] = rs232_tx;
  192. if (b < 7) #1080;
  193. end
  194. rx_snapshot[rx_snapshot_n & 511] = dec_byte;
  195. rx_snapshot_n = rx_snapshot_n + 1;
  196. #1080;
  197. if (rs232_tx !== 1'b1 || ttl_tx !== 1'b1)
  198. $fatal(1, "TX stop bit is low");
  199. end
  200. endtask
  201. task decode_and_check;
  202. input [7:0] rtype;
  203. input integer rlen;
  204. integer n;
  205. reg [15:0] c;
  206. begin
  207. rx_snapshot_n = 0;
  208. for (n = 0; n < rlen + 6; n = n + 1) decode_byte_both;
  209. if (rx_snapshot[0] !== 8'hA5 || rx_snapshot[1] !== 8'h5A)
  210. $fatal(1, "reply magic mismatch");
  211. if (rx_snapshot[2] !== rtype || rx_snapshot[3] !== rlen[7:0])
  212. $fatal(1, "reply header mismatch type=%h len=%0d",
  213. rx_snapshot[2], rx_snapshot[3]);
  214. c = tb_crc(2, rlen + 2);
  215. if (c !== {rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]})
  216. $fatal(1, "reply CRC mismatch calc=%h wire=%h%h",
  217. c, rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]);
  218. end
  219. endtask
  220. // Reply-RAM prefill driven at the falling edge (a value set right
  221. // after a rising edge is sampled by that same edge in Icarus's event
  222. // order and would be dropped).
  223. task prefill_pattern;
  224. input [7:0] base;
  225. input [7:0] step;
  226. input integer nbytes;
  227. integer n2;
  228. begin
  229. for (n2 = 0; n2 < nbytes; n2 = n2 + 1) begin
  230. @(negedge clk);
  231. rep_wr_en = 1'b1;
  232. rep_wr_addr = n2[7:0];
  233. rep_wr_data = base + (step * n2[7:0]);
  234. end
  235. @(negedge clk);
  236. rep_wr_en = 1'b0;
  237. repeat (4) @(posedge clk);
  238. end
  239. endtask
  240. task launch_reply;
  241. input [7:0] rtype;
  242. input [7:0] rlen;
  243. integer guard;
  244. begin
  245. guard = 0;
  246. while (tx_busy && guard < 200000) begin
  247. @(posedge clk);
  248. guard = guard + 1;
  249. end
  250. if (tx_busy) $fatal(1, "previous reply never finished");
  251. tx_type = rtype;
  252. tx_length = rlen;
  253. tx_start = 1'b1;
  254. while (!tx_busy) @(posedge clk);
  255. @(posedge clk);
  256. tx_start = 1'b0;
  257. end
  258. endtask
  259. initial begin
  260. repeat (4) @(posedge clk);
  261. rst_n = 1'b1;
  262. repeat (4) @(posedge clk);
  263. // ---- 1. SET_ALL: FAN 20 %, PTC 0 %, axis0 PUSH_DOWN 500/500,
  264. // axis4 PUSH_UP 0x1122 steps @ 0x0032 half period, rest NOTHING.
  265. frame_buf[0] = 8'hA5; frame_buf[1] = 8'h5A;
  266. frame_buf[2] = 8'h01; frame_buf[3] = 8'h44;
  267. for (k = 0; k < 4; k = k + 1) frame_buf[4 + k] = 8'h14;
  268. for (k = 4; k < 8; k = k + 1) frame_buf[4 + k] = 8'h00;
  269. for (k = 0; k < 60; k = k + 1) frame_buf[12 + k] = 8'h00;
  270. // axis0 slot at payload 8 (frame index 12)
  271. frame_buf[12] = 8'h01; frame_buf[13] = 8'hF4; frame_buf[14] = 8'h01;
  272. frame_buf[15] = 8'hF4; frame_buf[16] = 8'h00;
  273. // axis4 slot at payload 28 (frame index 32)
  274. frame_buf[32] = 8'h02; frame_buf[33] = 8'h22; frame_buf[34] = 8'h11;
  275. frame_buf[35] = 8'h32; frame_buf[36] = 8'h00;
  276. frame_len = 74;
  277. // The independent codec says the wire CRC is 16 06 (low 06).
  278. if (buf_crc(70) !== 16'h1606)
  279. $fatal(1, "TB CRC disagrees with the codec vector: %h", buf_crc(70));
  280. frame_buf[72] = 8'h06; frame_buf[73] = 8'h16;
  281. req_writes = 0;
  282. send_frame;
  283. await_req(8'h01, 8'd68);
  284. if (req_writes !== 68) $fatal(1, "SET68 writes=%0d", req_writes);
  285. for (k = 0; k < 4; k = k + 1)
  286. if (req_mem[k] !== 8'h14) $fatal(1, "fan[%0d]=%h", k, req_mem[k]);
  287. for (k = 4; k < 8; k = k + 1)
  288. if (req_mem[k] !== 8'h00) $fatal(1, "ptc[%0d]=%h", k, req_mem[k]);
  289. if (req_mem[8] !== 8'h01 || req_mem[9] !== 8'hF4 || req_mem[10] !== 8'h01 ||
  290. req_mem[11] !== 8'hF4 || req_mem[12] !== 8'h00)
  291. $fatal(1, "axis0 slot %h %h %h %h %h",
  292. req_mem[8], req_mem[9], req_mem[10], req_mem[11], req_mem[12]);
  293. if (req_mem[10 + 5 * 4] !== 8'h11 || req_mem[11 + 5 * 4] !== 8'h32 ||
  294. req_mem[8 + 5 * 4] !== 8'h02)
  295. $fatal(1, "axis4 slot %h %h %h",
  296. req_mem[8 + 5 * 4], req_mem[10 + 5 * 4], req_mem[11 + 5 * 4]);
  297. if (req_mem[13] !== 8'h00 || req_mem[67] !== 8'h00)
  298. $fatal(1, "NOTHING slots must stay zero");
  299. if (req_mem[68] !== 8'hA9) $fatal(1, "SET68 spilled past extent");
  300. if (req_ram_rd_addr !== 8'd67)
  301. $fatal(1, "validation read address %0d", req_ram_rd_addr);
  302. accept_req();
  303. // ---- 2. GET_ALL with an empty payload (golden A5 5A 02 00 6D 7B) ----
  304. frame_buf[0] = 8'hA5; frame_buf[1] = 8'h5A;
  305. frame_buf[2] = 8'h02; frame_buf[3] = 8'h00;
  306. frame_buf[4] = 8'h6D; frame_buf[5] = 8'h7B;
  307. frame_len = 6;
  308. req_writes = 0;
  309. send_frame;
  310. await_req(8'h02, 8'd0);
  311. if (req_writes !== 0) $fatal(1, "GET writes=%0d", req_writes);
  312. accept_req();
  313. // ---- 3. MOTOR_DONE reply: axis4 upper-limit 4387 steps, the
  314. // golden vector A5 5A 50 04 04 02 23 11 B6 02 ----
  315. prefill_pattern(8'h04, 8'h00, 4); // bytes 04 04 04 04; overwritten below
  316. @(negedge clk); rep_wr_en = 1'b1;
  317. rep_wr_addr = 8'd0; rep_wr_data = 8'h04;
  318. @(negedge clk);
  319. rep_wr_addr = 8'd1; rep_wr_data = 8'h02;
  320. @(negedge clk);
  321. rep_wr_addr = 8'd2; rep_wr_data = 8'h23;
  322. @(negedge clk);
  323. rep_wr_addr = 8'd3; rep_wr_data = 8'h11;
  324. @(negedge clk);
  325. rep_wr_en = 1'b0;
  326. repeat (4) @(posedge clk);
  327. launch_reply(8'h50, 8'd4);
  328. decode_and_check(8'h50, 4);
  329. if (rx_snapshot[4] !== 8'h04 || rx_snapshot[5] !== 8'h02 ||
  330. rx_snapshot[6] !== 8'h23 || rx_snapshot[7] !== 8'h11 ||
  331. rx_snapshot[8] !== 8'hB6 || rx_snapshot[9] !== 8'h02)
  332. $fatal(1, "MOTOR_DONE wire mismatch %h %h %h %h %h %h",
  333. rx_snapshot[4], rx_snapshot[5], rx_snapshot[6],
  334. rx_snapshot[7], rx_snapshot[8], rx_snapshot[9]);
  335. // ---- 4. GET_DATA reply, 63-byte payload (byte k = 0x09 + 5k) ----
  336. prefill_pattern(8'h09, 8'h05, 63);
  337. launch_reply(8'h42, 8'd63);
  338. decode_and_check(8'h42, 63);
  339. for (k = 0; k < 63; k = k + 1)
  340. if (rx_snapshot[4 + k] !== (8'h09 + 8'h05 * k[7:0]))
  341. $fatal(1, "GET_DATA payload[%0d]=%h", k, rx_snapshot[4 + k]);
  342. // The independent codec says this image's CRC is BB 3E (high BB).
  343. if (rx_snapshot[67] !== 8'h3E || rx_snapshot[68] !== 8'hBB)
  344. $fatal(1, "GET_DATA wire CRC mismatch %h %h",
  345. rx_snapshot[67], rx_snapshot[68]);
  346. // ---- 5. the link still parses a legal request after all replies ----
  347. frame_buf[0] = 8'hA5; frame_buf[1] = 8'h5A;
  348. frame_buf[2] = 8'h02; frame_buf[3] = 8'h00;
  349. frame_buf[4] = 8'h6D; frame_buf[5] = 8'h7B;
  350. frame_len = 6;
  351. send_frame;
  352. await_req(8'h02, 8'd0);
  353. accept_req();
  354. $display("prod2u_mrdimm_uart_e2e_tb PASS");
  355. $finish;
  356. end
  357. initial begin
  358. #400000000;
  359. $fatal(1, "mrdimm e2e deadline");
  360. end
  361. endmodule