prod2u_6400_uart_e2e_tb.v 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. `timescale 1ns/1ps
  2. // UART-level end-to-end test for the 6400 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: SET_ALL 32-byte payloads and an 83-byte GET_DATA reply.
  6. //
  7. // Request frames, the reply image and the expected CRCs are the vectors
  8. // from the independent Python codec (tools/crc16_ccitt_table.frame_bytes),
  9. // hand-embedded here; the TB verifies the reply CRC with its own
  10. // bit-serial CRC-16/CCITT-FALSE over the decoded wire bytes, never with
  11. // the DUT's accumulator.
  12. //
  13. // NOTE (protocol worker, 2026-09-20): this bench deliberately covers the
  14. // shared transport primitives plus the 6400 frame sizes. The product
  15. // command core (thermal_host_command) is mid-migration to the frozen
  16. // 8-bit macro set by its owner; when it lands, its own host-level bench
  17. // re-attaches the command semantics. Everything this bench drives is in
  18. // this worker's scope.
  19. module prod2u_6400_uart_e2e_tb;
  20. localparam integer CLKS = 27;
  21. localparam integer TIMEOUT_CYCLES = 250000;
  22. reg clk = 1'b0;
  23. reg rst_n = 1'b0;
  24. reg rs232_rx = 1'b1;
  25. wire rs232_tx;
  26. reg ttl_rx = 1'b1;
  27. wire ttl_tx;
  28. reg req_ready = 1'b0;
  29. wire req_valid;
  30. wire [7:0] req_type;
  31. wire [7:0] req_length;
  32. wire req_ram_wr_en;
  33. wire [7:0] req_ram_wr_addr;
  34. wire [7:0] req_ram_wr_data;
  35. wire [7:0] req_ram_rd_addr;
  36. wire [7:0] req_ram_rd_data;
  37. reg tx_start = 1'b0;
  38. reg [7:0] tx_type = 8'd0;
  39. reg [7:0] tx_length = 8'd0;
  40. wire tx_busy;
  41. wire tx_done;
  42. wire [7:0] tx_ram_rd_addr;
  43. wire [7:0] tx_ram_rd_data;
  44. integer i;
  45. integer k;
  46. always #20 clk = ~clk; // 25 MHz
  47. host_uart_endpoint #(
  48. .CLKS_PER_BIT(CLKS),
  49. .INTER_BYTE_TIMEOUT_CYCLES(TIMEOUT_CYCLES),
  50. .MAX_PAYLOAD(128),
  51. .ADDR_WIDTH(8)
  52. ) dut (
  53. .clk(clk), .rst_n(rst_n),
  54. .rs232_rx(rs232_rx), .rs232_tx(rs232_tx),
  55. .ttl_rx(ttl_rx), .ttl_tx(ttl_tx),
  56. .req_valid(req_valid), .req_ready(req_ready),
  57. .req_type(req_type), .req_length(req_length),
  58. .req_ram_wr_en(req_ram_wr_en), .req_ram_wr_addr(req_ram_wr_addr),
  59. .req_ram_wr_data(req_ram_wr_data),
  60. .req_ram_rd_addr(req_ram_rd_addr), .req_ram_rd_data(req_ram_rd_data),
  61. .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length),
  62. .tx_busy(tx_busy), .tx_done(tx_done),
  63. .tx_ram_rd_addr(tx_ram_rd_addr), .tx_ram_rd_data(tx_ram_rd_data)
  64. );
  65. // Request staging RAM (128 bytes), parser owns the write port; the
  66. // endpoint's read address drives the synchronous read port for
  67. // validation/commit.
  68. reg [7:0] req_mem [0:127];
  69. initial begin
  70. for (i = 0; i < 128; i = i + 1) req_mem[i] = 8'hA9;
  71. end
  72. always @(posedge clk) begin
  73. if (req_ram_wr_en) req_mem[req_ram_wr_addr] <= req_ram_wr_data;
  74. end
  75. assign req_ram_rd_data = req_mem[req_ram_rd_addr];
  76. integer req_writes;
  77. initial req_writes = 0;
  78. always @(negedge clk) begin
  79. if (req_ram_wr_en) begin
  80. req_writes = req_writes + 1;
  81. if (req_ram_wr_addr >= 128)
  82. $fatal(1, "request RAM address out of range %0d", req_ram_wr_addr);
  83. end
  84. end
  85. // Reply staging RAM (128 bytes, synchronous read) feeds frame_tx.
  86. reg rep_wr_en = 1'b0;
  87. reg [7:0] rep_wr_addr = 8'd0;
  88. reg [7:0] rep_wr_data = 8'd0;
  89. reply_ram #(.DEPTH(128), .ADDR_WIDTH(8)) u_reply (
  90. .clk(clk), .wr_en(rep_wr_en), .wr_addr(rep_wr_addr),
  91. .wr_data(rep_wr_data), .rd_addr(tx_ram_rd_addr),
  92. .rd_data(tx_ram_rd_data)
  93. );
  94. // ---- independent CRC-16/CCITT-FALSE over decoded wire bytes ----
  95. reg [7:0] rx_snapshot [0:511];
  96. integer rx_snapshot_n;
  97. initial rx_snapshot_n = 0;
  98. // CRC over the framed body (TYPE, LENGTH, PAYLOAD): starts at the
  99. // byte after the A5 5A header, exactly as the wire contract defines.
  100. function [15:0] tb_crc;
  101. input integer nbody;
  102. integer ck, bitno;
  103. reg [15:0] c;
  104. begin
  105. c = 16'hFFFF;
  106. for (ck = 0; ck < nbody; ck = ck + 1) begin
  107. c = c ^ {rx_snapshot[(2 + ck) & 511], 8'd0};
  108. for (bitno = 0; bitno < 8; bitno = bitno + 1)
  109. c = c[15] ? (c << 1) ^ 16'h1021 : c << 1;
  110. end
  111. tb_crc = c;
  112. end
  113. endfunction
  114. // ---- real UART stimulus on the RS232 RX line ----
  115. task send_byte;
  116. input [7:0] value;
  117. integer kk;
  118. begin
  119. rs232_rx = 1'b0;
  120. #1080;
  121. for (kk = 0; kk < 8; kk = kk + 1) begin
  122. rs232_rx = value[kk];
  123. #1080;
  124. end
  125. rs232_rx = 1'b1;
  126. #1080;
  127. end
  128. endtask
  129. task send_get;
  130. begin
  131. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h02); send_byte(8'h00);
  132. send_byte(8'h6D); send_byte(8'h7B);
  133. end
  134. endtask
  135. task send_set32_zero;
  136. integer kk;
  137. begin
  138. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01); send_byte(8'h20);
  139. for (kk = 0; kk < 32; kk = kk + 1) send_byte(8'h00);
  140. send_byte(8'h0D); send_byte(8'h8C);
  141. end
  142. endtask
  143. task send_set32_hot5;
  144. integer kk;
  145. begin
  146. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01); send_byte(8'h20);
  147. for (kk = 0; kk < 32; kk = kk + 1)
  148. send_byte((kk == 5) ? 8'h64 : 8'h00);
  149. send_byte(8'h32); send_byte(8'h5C);
  150. end
  151. endtask
  152. task await_req;
  153. input [7:0] etype;
  154. input [7:0] elen;
  155. integer guard;
  156. begin
  157. guard = 0;
  158. while (!req_valid && guard < 200000) begin
  159. @(posedge clk);
  160. guard = guard + 1;
  161. end
  162. if (!req_valid) $fatal(1, "request not parsed type=%h", etype);
  163. if (req_type !== etype || req_length !== elen)
  164. $fatal(1, "request metadata mismatch type=%h len=%0d",
  165. req_type, req_length);
  166. end
  167. endtask
  168. task accept_req;
  169. begin
  170. req_ready = 1'b1;
  171. while (req_valid) @(posedge clk);
  172. @(posedge clk);
  173. @(negedge clk);
  174. req_ready = 1'b0;
  175. repeat (2) @(posedge clk);
  176. while (dut.rx_owner != 2'd0) @(posedge clk);
  177. repeat (4) @(posedge clk);
  178. end
  179. endtask
  180. // ---- TX decoder: samples RS232 and TTL at the same bit centres, so
  181. // every decoded byte also proves the broadcast copies agree bit for
  182. // bit. Records into rx_snapshot for the independent CRC. ----
  183. reg [7:0] dec_byte;
  184. task decode_byte_both;
  185. integer b;
  186. begin
  187. // Byte assembled in a scalar first, then stored: the same
  188. // shape the proven host_uart_endpoint decoder uses (direct
  189. // bit-by-bit stores into a task-written array read back as
  190. // zeros under Icarus, 2026-09-20).
  191. // Mid-bit sampling: 1.5 bit periods after the start fall
  192. // lands in the middle of data bit 0 (the transmitter changes
  193. // the line every CLKS clocks; sampling at exactly one bit
  194. // period races that edge), then one bit per sample.
  195. @(negedge rs232_tx);
  196. #(CLKS * 40 + CLKS * 20);
  197. for (b = 0; b < 8; b = b + 1) begin
  198. if (rs232_tx !== ttl_tx)
  199. $fatal(1, "TX broadcast diverged at bit %0d", b);
  200. dec_byte[b] = rs232_tx;
  201. if (b < 7) #1080;
  202. end
  203. rx_snapshot[rx_snapshot_n & 511] = dec_byte;
  204. rx_snapshot_n = rx_snapshot_n + 1;
  205. #1080;
  206. if (rs232_tx !== 1'b1 || ttl_tx !== 1'b1)
  207. $fatal(1, "TX stop bit is low");
  208. end
  209. endtask
  210. task decode_and_check;
  211. input [7:0] rtype;
  212. input integer rlen;
  213. integer n;
  214. reg [15:0] c;
  215. begin
  216. rx_snapshot_n = 0;
  217. for (n = 0; n < rlen + 6; n = n + 1) decode_byte_both;
  218. if (rx_snapshot[0] !== 8'hA5 || rx_snapshot[1] !== 8'h5A)
  219. $fatal(1, "reply magic mismatch");
  220. if (rx_snapshot[2] !== rtype || rx_snapshot[3] !== rlen[7:0])
  221. $fatal(1, "reply header mismatch type=%h len=%0d",
  222. rx_snapshot[2], rx_snapshot[3]);
  223. c = tb_crc(rlen + 2);
  224. if (c !== {rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]})
  225. $fatal(1, "reply CRC mismatch calc=%h wire=%h%h",
  226. c, rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]);
  227. end
  228. endtask
  229. task prefill_reply_pattern;
  230. input [7:0] base;
  231. input [7:0] step;
  232. integer n2;
  233. begin
  234. // Drive the reply-RAM write port at the falling edge: a value
  235. // set right after a rising edge is sampled by that same edge
  236. // in Icarus's event order, which silently dropped writes
  237. // (observed 2026-09-20).
  238. for (n2 = 0; n2 < 128; n2 = n2 + 1) begin
  239. @(negedge clk);
  240. rep_wr_en = 1'b1;
  241. rep_wr_addr = n2[7:0];
  242. rep_wr_data = base + (step * n2[7:0]);
  243. end
  244. @(negedge clk);
  245. rep_wr_en = 1'b0;
  246. repeat (4) @(posedge clk);
  247. end
  248. endtask
  249. task launch_reply;
  250. input [7:0] rtype;
  251. input [7:0] rlen;
  252. integer guard;
  253. begin
  254. guard = 0;
  255. while (tx_busy && guard < 200000) begin
  256. @(posedge clk);
  257. guard = guard + 1;
  258. end
  259. if (tx_busy) $fatal(1, "previous reply never finished");
  260. tx_type = rtype;
  261. tx_length = rlen;
  262. tx_start = 1'b1;
  263. while (!tx_busy) @(posedge clk);
  264. @(posedge clk);
  265. tx_start = 1'b0;
  266. end
  267. endtask
  268. initial begin
  269. repeat (4) @(posedge clk);
  270. rst_n = 1'b1;
  271. repeat (4) @(posedge clk);
  272. // ---- 1. SET_ALL 32 zero bytes ----
  273. // Wire image A5 5A 01 20 + 32x00 + 0D 8C (independent codec).
  274. req_writes = 0;
  275. send_set32_zero();
  276. await_req(8'h01, 8'd32);
  277. if (req_writes !== 32) $fatal(1, "SET32 writes=%0d", req_writes);
  278. for (k = 0; k < 32; k = k + 1)
  279. if (req_mem[k] !== 8'h00) $fatal(1, "SET32 ram[%0d]=%h", k, req_mem[k]);
  280. if (req_mem[32] !== 8'hA9) $fatal(1, "SET32 spilled past extent");
  281. // the validation read port holds the last staged byte
  282. if (req_ram_rd_addr !== 8'd31)
  283. $fatal(1, "validation read address %0d", req_ram_rd_addr);
  284. if (req_ram_rd_data !== 8'h00)
  285. $fatal(1, "validation read data %h", req_ram_rd_data);
  286. accept_req();
  287. // ---- 2. SET_ALL with index 5 = 100 (0x64), wire CRC 32 5C ----
  288. req_writes = 0;
  289. send_set32_hot5();
  290. await_req(8'h01, 8'd32);
  291. if (req_writes !== 32) $fatal(1, "hot5 writes=%0d", req_writes);
  292. if (req_mem[5] !== 8'h64) $fatal(1, "hot5 ram[5]=%h", req_mem[5]);
  293. if (req_mem[6] !== 8'h00) $fatal(1, "hot5 ram[6]=%h", req_mem[6]);
  294. accept_req();
  295. // ---- 3. GET_ALL with an empty payload (golden A5 5A 02 00 6D 7B) ----
  296. req_writes = 0;
  297. send_get();
  298. await_req(8'h02, 8'd0);
  299. if (req_writes !== 0) $fatal(1, "GET writes=%0d", req_writes);
  300. accept_req();
  301. // ---- 4. GET_DATA reply, 83-byte payload through reply_ram ----
  302. prefill_reply_pattern(8'h03, 8'h07); // byte k = 0x03 + 7k (mod 256)
  303. launch_reply(8'h42, 8'd83);
  304. decode_and_check(8'h42, 83);
  305. for (k = 0; k < 83; k = k + 1)
  306. if (rx_snapshot[4 + k] !== (8'h03 + 8'h07 * k[7:0]))
  307. $fatal(1, "GET_DATA payload[%0d]=%h", k, rx_snapshot[4 + k]);
  308. // ---- 5. SET_ACK reply, golden vector A5 5A 81 00 A6 35 ----
  309. prefill_reply_pattern(8'h00, 8'h00);
  310. launch_reply(8'h81, 8'd0);
  311. decode_and_check(8'h81, 0);
  312. if (rx_snapshot[4] !== 8'hA6 || rx_snapshot[5] !== 8'h35)
  313. $fatal(1, "SET_ACK wire mismatch %h %h", rx_snapshot[4], rx_snapshot[5]);
  314. // ---- 6. NACK reply, golden vector A5 5A C0 02 01 04 B1 2A ----
  315. @(negedge clk); rep_wr_en = 1'b1; rep_wr_addr = 8'd0; rep_wr_data = 8'h01;
  316. @(negedge clk); rep_wr_addr = 8'd1; rep_wr_data = 8'h04;
  317. @(negedge clk); rep_wr_en = 1'b0;
  318. repeat (4) @(posedge clk);
  319. launch_reply(8'hC0, 8'd2);
  320. decode_and_check(8'hC0, 2);
  321. if (rx_snapshot[4] !== 8'h01 || rx_snapshot[5] !== 8'h04 ||
  322. rx_snapshot[6] !== 8'hB1 || rx_snapshot[7] !== 8'h2A)
  323. $fatal(1, "NACK wire mismatch %h %h %h %h",
  324. rx_snapshot[4], rx_snapshot[5], rx_snapshot[6], rx_snapshot[7]);
  325. // ---- 7. the link still parses a legal request after all replies ----
  326. send_get();
  327. await_req(8'h02, 8'd0);
  328. accept_req();
  329. $display("prod2u_6400_uart_e2e_tb PASS");
  330. $finish;
  331. end
  332. initial begin
  333. #400000000;
  334. $fatal(1, "6400 e2e deadline");
  335. end
  336. endmodule