prod2u_mrdimm_uart_e2e_tb.v 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. `timescale 1ns/1ps
  2. // UART-level end-to-end test for the mrdimm-chamber product stack over the
  3. // unified 16-bit-length protocol: real UART byte timing -> frame_rx ->
  4. // mrdimm_host_command -> mrdimm_output_core + motor_engine. Vectors are
  5. // generated by the independent Python codec (hth_protocol).
  6. module prod2u_mrdimm_uart_e2e_tb;
  7. localparam integer CLKS = 8;
  8. reg clk = 1'b0;
  9. reg rst_n = 1'b0;
  10. reg rx = 1'b1;
  11. reg tick_us = 1'b0;
  12. reg tick_1s = 1'b0;
  13. reg [2:0] limit_lower_raw = 3'b000;
  14. reg [2:0] limit_upper_raw = 3'b000;
  15. wire frame_valid, frame_ready, frame_accept_enable;
  16. wire [7:0] frame_type;
  17. wire [15:0] frame_length;
  18. wire [15:0] payload_rd_addr;
  19. wire [7:0] payload_rd_data;
  20. wire payload_wr_en;
  21. wire [15:0] payload_wr_addr;
  22. wire [7:0] payload_wr_data;
  23. wire thermal_wr_en, thermal_commit, thermal_commit_done, thermal_commit_ok;
  24. wire [2:0] thermal_wr_addr;
  25. wire [7:0] thermal_wr_data;
  26. wire keepalive_reload, snapshot_capture, snapshot_ready;
  27. wire qw_byte_we, qw_submit, qw_accept, ctl_valid, ctl_accept;
  28. wire [6:0] qw_byte_addr;
  29. wire [7:0] qw_byte_data, qw_status, ctl_status;
  30. wire [3:0] qw_count;
  31. wire [15:0] qw_detail, ctl_detail;
  32. wire [2:0] ctl_op;
  33. wire [15:0] ctl_axis_mask;
  34. wire [7:0] ctl_flags;
  35. wire [31:0] ctl_value;
  36. wire [7:0] ev_rd_index_w;
  37. wire ev_hold;
  38. wire [127:0] ev_rd_data;
  39. wire ev_view_ready;
  40. wire [7:0] ev_view_count;
  41. wire [7:0] ev_count, ev_overflow;
  42. wire [31:0] ev_time_us;
  43. wire ev_clear_n_w;
  44. wire [7:0] ev_clear_count_w;
  45. wire [7:0] status_byte_in;
  46. wire tx_start, tx_done;
  47. wire [7:0] tx_frame_type, tx_payload_data;
  48. wire [15:0] tx_payload_length;
  49. wire host_command_busy;
  50. wire [3:0] fan_on, ptc_on;
  51. wire [7:0] applied_fan0;
  52. wire [2:0] step_bus, dir_bus;
  53. wire host_response_done_w;
  54. integer commits, acks;
  55. reg in_data_phase;
  56. reg [15:0] data_len_seen;
  57. always #1 clk = ~clk;
  58. always begin
  59. #25 tick_us = 1'b1; #10 tick_us = 1'b0; // ~1us at 40ns-ish period
  60. #25 tick_1s = 1'b0;
  61. end
  62. uart_rx_byte #(.CLKS_PER_BIT(CLKS)) u_uart_rx (
  63. .clk(clk), .rst_n(rst_n), .rx_i(rx),
  64. .byte_valid(), .byte_data(), .framing_error()
  65. );
  66. // Byte-level feeder shared by frame_rx: reuse uart_rx_byte output via
  67. // explicit wires (declared above as internal names).
  68. wire uart_rx_valid_w;
  69. wire [7:0] uart_rx_data_w;
  70. wire uart_framing_error_w;
  71. uart_rx_byte #(.CLKS_PER_BIT(CLKS)) u_host_rx (
  72. .clk(clk), .rst_n(rst_n), .rx_i(rx),
  73. .byte_valid(uart_rx_valid_w), .byte_data(uart_rx_data_w),
  74. .framing_error(uart_framing_error_w)
  75. );
  76. frame_rx #(.MAX_PAYLOAD(256), .INTER_BYTE_TIMEOUT_CYCLES(400), .EXTERNAL_CRC(1)) u_frame_rx (
  77. .clk(clk), .rst_n(rst_n), .byte_valid(uart_rx_valid_w),
  78. .byte_data(uart_rx_data_w), .accept_enable(frame_accept_enable),
  79. .frame_valid(frame_valid), .frame_ready(frame_ready),
  80. .frame_type(frame_type), .frame_length(frame_length),
  81. .payload_read_addr(payload_rd_addr), .payload_read_data(payload_rd_data),
  82. .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr),
  83. .payload_wr_data(payload_wr_data),
  84. .crc_start_o(crc_start_w), .crc_data_valid_o(crc_dv_w), .crc_data_o(crc_byte_w),
  85. .crc_busy_i(host_crc_busy_w), .crc_data_done_i(crc_done_w), .crc_value_i(crc_val_w)
  86. );
  87. payload_ram #(.DEPTH(256), .ADDR_WIDTH(8)) u_payload (
  88. .clk(clk), .wr_en(payload_wr_en), .wr_addr(payload_wr_addr),
  89. .wr_data(payload_wr_data), .rd_addr(payload_rd_addr), .rd_data(payload_rd_data)
  90. );
  91. wire host_crc_busy_w, crc_start_w, crc_dv_w, crc_done_w;
  92. wire [7:0] crc_byte_w;
  93. wire [15:0] crc_val_w;
  94. crc16_serial u_crc (
  95. .clk(clk), .rst_n(rst_n),
  96. .start(crc_start_w), .data_valid(crc_dv_w),
  97. .data(crc_byte_w), .busy(host_crc_busy_w),
  98. .data_done(crc_done_w), .crc(crc_val_w)
  99. );
  100. mrdimm_host_command #(.SNAPSHOT_LENGTH(50)) dut (
  101. .clk(clk), .rst_n(rst_n),
  102. .frame_valid(frame_valid), .frame_ready(frame_ready),
  103. .frame_accept_enable(frame_accept_enable), .frame_type(frame_type),
  104. .frame_length(frame_length),
  105. .payload_read_addr(payload_rd_addr), .payload_read_data(payload_rd_data),
  106. .thermal_wr_en(thermal_wr_en), .thermal_wr_addr(thermal_wr_addr),
  107. .thermal_wr_data(thermal_wr_data), .thermal_commit(thermal_commit),
  108. .thermal_commit_done(thermal_commit_done), .thermal_commit_ok(thermal_commit_ok),
  109. .lease_active(lease_active_w), .keepalive_reload(keepalive_reload),
  110. .snapshot_capture(snapshot_capture), .snapshot_ready(snapshot_ready),
  111. .qw_byte_we(qw_byte_we), .qw_byte_addr(qw_byte_addr),
  112. .qw_byte_data(qw_byte_data), .qw_submit(qw_submit), .qw_count(qw_count),
  113. .qw_accept(qw_accept), .qw_status(qw_status), .qw_detail(qw_detail),
  114. .ctl_valid(ctl_valid), .ctl_op(ctl_op), .ctl_axis_mask(ctl_axis_mask),
  115. .ctl_flags(ctl_flags), .ctl_value(ctl_value),
  116. .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
  117. .ev_rd_index(ev_rd_index_w), .ev_hold(ev_hold),
  118. .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
  119. .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
  120. .ev_clear_n(ev_clear_n_w), .ev_clear_count(ev_clear_count_w),
  121. .status_byte_in(status_byte_in),
  122. .tx_payload_addr(u_frame_tx.payload_addr),
  123. .ack_payload_data(), .tx_payload_data(tx_payload_data),
  124. .tx_start(tx_start), .tx_frame_type(tx_frame_type),
  125. .tx_payload_length(tx_payload_length), .tx_done(host_response_done_w),
  126. .transaction_done(), .busy(host_command_busy)
  127. );
  128. wire lease_active_w;
  129. wire [7:0] applied_fan [0:3];
  130. wire [7:0] applied_ptc [0:3];
  131. wire [7:0] watchdog_remaining_w;
  132. mrdimm_output_core #(
  133. .LEASE_SECONDS(8'd30), .PWM_PERIOD_CLKS(1000), .PWM_CLKS_PER_PERCENT(10)
  134. ) u_output (
  135. .clk(clk), .rst_n(rst_n), .tick_1s(tick_1s),
  136. .wr_en(thermal_wr_en), .wr_addr(thermal_wr_addr), .wr_data(thermal_wr_data),
  137. .commit(thermal_commit), .commit_done(thermal_commit_done),
  138. .commit_ok(thermal_commit_ok), .keepalive_reload(keepalive_reload),
  139. .applied_fan(applied_fan), .applied_ptc(applied_ptc),
  140. .manual_active(), .lease_active(lease_active_w),
  141. .watchdog_remaining(watchdog_remaining_w),
  142. .fan_pwm_on(fan_on), .ptc_pwm_on(ptc_on)
  143. );
  144. wire [287:0] axis_telemetry;
  145. wire [31:0] unused_time;
  146. motor_engine #(
  147. .AXIS_COUNT(3),
  148. .X_DEFAULT_HALF_US(32'd4), .Y_DEFAULT_HALF_US(32'd4), .Z_DEFAULT_HALF_US(32'd8),
  149. .X_MIN_HALF_US(32'd2), .Y_MIN_HALF_US(32'd2), .Z_MIN_HALF_US(32'd4),
  150. .DIR_SETUP_TICKS(2)
  151. ) u_motor (
  152. .clk(clk), .rst_n(rst_n), .tick_us(tick_us),
  153. .limit_lower_raw(limit_lower_raw), .limit_upper_raw(limit_upper_raw),
  154. .qw_byte_we(qw_byte_we), .qw_byte_addr(qw_byte_addr),
  155. .qw_byte_data(qw_byte_data), .qw_submit(qw_submit), .qw_count(qw_count),
  156. .qw_accept(qw_accept), .qw_status(qw_status), .qw_detail(qw_detail),
  157. .ctl_valid(ctl_valid), .ctl_op(ctl_op), .ctl_axis_mask(ctl_axis_mask),
  158. .ctl_flags(ctl_flags), .ctl_value(ctl_value),
  159. .ctl_accept(ctl_accept), .ctl_status(ctl_status), .ctl_detail(ctl_detail),
  160. .startup_seek(1'b0),
  161. .ev_rd_index(ev_rd_index_w), .ev_hold(ev_hold),
  162. .ev_rd_data(ev_rd_data), .ev_count(ev_count), .ev_overflow(ev_overflow),
  163. .ev_view_ready(ev_view_ready), .ev_view_count(ev_view_count),
  164. .ev_time_us(unused_time),
  165. .ev_clear_n(ev_clear_n_w), .ev_clear_count(ev_clear_count_w),
  166. .axis_telemetry(axis_telemetry),
  167. .step_out(step_bus), .dir_out(dir_bus)
  168. );
  169. // ALL_STATUS byte window: engine telemetry is checked through the
  170. // snapshot capture; here a stub window is enough for the length check.
  171. assign status_byte_in = 8'h00;
  172. mrdimm_snapshot_mux #(.SNAPSHOT_LENGTH(50)) u_snapshot (
  173. .clk(clk), .rst_n(rst_n), .capture(snapshot_capture), .ready(snapshot_ready),
  174. .fan_applied(applied_fan), .ptc_applied(applied_ptc),
  175. .axis_sel(), .axis_data(axis_telemetry[95:0]),
  176. .time_us(unused_time), .ev_count(ev_count), .ev_overflow(ev_overflow),
  177. .read_addr(8'd0), .read_data(status_byte_in_unused)
  178. );
  179. wire [7:0] status_byte_in_unused;
  180. // frame_tx consumes the command core payload for ACK/DATA lengths.
  181. wire frame_tx_done_w, frame_tx_busy_w, frame_tx_byte_valid_w;
  182. wire [7:0] frame_tx_byte_data_w;
  183. wire [15:0] frame_tx_payload_addr_w;
  184. frame_tx #(.PAYLOAD_IDX_BITS(8), .EXTERNAL_CRC(0), .PAYLOAD_READ_LATENCY(1)) u_frame_tx (
  185. .clk(clk), .rst_n(rst_n), .start(tx_start), .frame_type(tx_frame_type),
  186. .payload_length(tx_payload_length), .busy(frame_tx_busy_w),
  187. .byte_valid(frame_tx_byte_valid_w), .byte_data(frame_tx_byte_data_w),
  188. .byte_ready(1'b1), .payload_addr(frame_tx_payload_addr_w),
  189. .payload_data(tx_payload_data), .done(frame_tx_done_w),
  190. .crc_start_o(), .crc_data_valid_o(), .crc_data_o(),
  191. .crc_busy_i(1'b0), .crc_data_done_i(), .crc_value_i()
  192. );
  193. assign host_response_done_w = frame_tx_done_w;
  194. // ---- UART byte feeder (shared for all frames) ----
  195. task send_byte;
  196. input [7:0] value;
  197. integer k;
  198. begin
  199. rx = 1'b0;
  200. repeat (CLKS) @(posedge clk);
  201. for (k = 0; k < 8; k = k + 1) begin
  202. rx = value[k];
  203. repeat (CLKS) @(posedge clk);
  204. end
  205. rx = 1'b1;
  206. repeat (CLKS) @(posedge clk);
  207. end
  208. endtask
  209. initial begin
  210. commits = 0; acks = 0;
  211. forever begin
  212. @(posedge clk);
  213. if (thermal_commit) commits = commits + 1;
  214. if (tx_start && tx_frame_type==8'h80) begin
  215. acks = acks + 1;
  216. if (tx_frame_type !== 8'h80)
  217. $fatal(1, "frame %02x is not an ACK", tx_frame_type);
  218. if (tx_payload_length !== 16'd4)
  219. $fatal(1, "ACK payload length %0d", tx_payload_length);
  220. end
  221. end
  222. end
  223. initial begin
  224. wait (rst_n);
  225. // ---- SET_ALL fan20/ptc0, endpoints FF (no motor touch) ----
  226. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01);
  227. send_byte(8'h0B); send_byte(8'h00);
  228. send_byte(8'h14); send_byte(8'h14); send_byte(8'h14); send_byte(8'h14);
  229. send_byte(8'h00); send_byte(8'h00); send_byte(8'h00); send_byte(8'h00);
  230. send_byte(8'hFF); send_byte(8'hFF); send_byte(8'hFF);
  231. send_byte(8'h89); send_byte(8'h04);
  232. repeat (2000) @(posedge clk);
  233. if (commits !== 1) $fatal(1, "SET_ALL commits=%0d", commits);
  234. if (acks !== 1) $fatal(1, "SET_ALL acks=%0d", acks);
  235. if (applied_fan[0] !== 8'd20) $fatal(1, "fan duty not applied");
  236. if (u_motor.g_axis[0].u_axis.active !== 1'b0)
  237. $fatal(1, "FF endpoint must not touch motors");
  238. // ---- SET_ALL with X=lower endpoint: commit + engine accepted ----
  239. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01);
  240. send_byte(8'h0B); send_byte(8'h00);
  241. send_byte(8'h14); send_byte(8'h14); send_byte(8'h14); send_byte(8'h14);
  242. send_byte(8'h00); send_byte(8'h00); send_byte(8'h00); send_byte(8'h00);
  243. send_byte(8'hFF); send_byte(8'h00); send_byte(8'hFF);
  244. send_byte(8'h76); send_byte(8'h07);
  245. repeat (3000) @(posedge clk);
  246. if (commits !== 2) $fatal(1, "X-low commits=%0d", commits);
  247. if (u_motor.g_axis[0].u_axis.active !== 1'b1)
  248. $fatal(1, "X seek not accepted");
  249. if (u_motor.g_axis[0].u_axis.r_mode !== 3'd1)
  250. $fatal(1, "X must run seek_lower");
  251. // ---- bad CRC: silent ----
  252. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h01);
  253. send_byte(8'h0B); send_byte(8'h00);
  254. send_byte(8'h14); send_byte(8'h14); send_byte(8'h14); send_byte(8'h14);
  255. send_byte(8'h00); send_byte(8'h00); send_byte(8'h00); send_byte(8'h00);
  256. send_byte(8'hFF); send_byte(8'hFF); send_byte(8'hFF);
  257. send_byte(8'h89); send_byte(8'hFB);
  258. repeat (1000) @(posedge clk);
  259. if (commits !== 2) $fatal(1, "bad CRC committed");
  260. if (acks !== 2) $fatal(1, "bad CRC acked");
  261. // ---- MOTOR_QUEUE_WRITE with count=0: BAD_LENGTH NACK ----
  262. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h03);
  263. send_byte(8'h11); send_byte(8'h00);
  264. for (int j = 0; j < 17; j = j + 1) send_byte(8'h00);
  265. send_byte(8'h2F); send_byte(8'h46);
  266. repeat (2000) @(posedge clk);
  267. if (acks !== 3) $fatal(1, "QW acks=%0d", acks);
  268. if (commits !== 2) $fatal(1, "QW must not commit");
  269. // ---- KEEPALIVE contract vector: ACK only ----
  270. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h05);
  271. send_byte(8'h00); send_byte(8'h00);
  272. send_byte(8'h6C); send_byte(8'h27);
  273. repeat (500) @(posedge clk);
  274. if (acks !== 4) $fatal(1, "keepalive acks=%0d", acks);
  275. if (commits !== 2) $fatal(1, "keepalive must not commit");
  276. // ---- GET_ALL contract vector: ACK then 81 DATA (50B) ----
  277. send_byte(8'hA5); send_byte(8'h5A); send_byte(8'h02);
  278. send_byte(8'h00); send_byte(8'h00);
  279. send_byte(8'hFC); send_byte(8'hA2);
  280. repeat (2000) @(posedge clk);
  281. if (data_len_seen !== 16'd50) $fatal(1, "ALL_STATUS length %0d", data_len_seen);
  282. if (response_checks!==6) $fatal(1,"incomplete serialized responses: %0d",response_checks);
  283. // Abort the running X seek, then read its stable event through 80/83.
  284. send_byte(8'hA5);send_byte(8'h5A);send_byte(8'h04);send_byte(8'h08);send_byte(0);
  285. send_byte(1);send_byte(1);repeat(6) send_byte(0);send_byte(8'hB1);send_byte(8'h30);
  286. repeat(1000) @(posedge clk);
  287. if(u_motor.g_axis[0].u_axis.active || acks!=6) $fatal(1,"control abort transaction");
  288. send_byte(8'hA5);send_byte(8'h5A);send_byte(8'h08);send_byte(1);send_byte(0);
  289. send_byte(8);send_byte(8'h3B);send_byte(8'hB7);
  290. repeat(2000) @(posedge clk);
  291. if(response_checks!=9 || acks!=7) $fatal(1,"EVENT_READ did not complete ACK plus DATA");
  292. $display("prod2u_mrdimm_uart_e2e_tb PASS");
  293. $finish;
  294. end
  295. // track the DATA phase length of GET_ALL responses
  296. always @(posedge clk) begin
  297. if (tx_start && tx_frame_type == 8'h81) data_len_seen <= tx_payload_length;
  298. end
  299. // Independently inspect the actual serializer bytes. Counting tx_start
  300. // alone would miss a one-bit payload bus or DATA being sent in an ACK.
  301. reg [7:0] response_bytes [0:1023];
  302. integer response_count=0, response_checks=0, rb, bitno;
  303. reg [15:0] crc_check;
  304. always @(posedge clk) begin
  305. if (frame_tx_byte_valid_w) begin
  306. response_bytes[response_count]=frame_tx_byte_data_w;
  307. response_count=response_count+1;
  308. end
  309. if (frame_tx_done_w) begin
  310. if(response_count != 7+{response_bytes[4],response_bytes[3]})
  311. $fatal(1,"response length does not match wire bytes");
  312. crc_check=16'hFFFF;
  313. for(rb=2;rb<response_count-2;rb=rb+1) begin
  314. crc_check=crc_check ^ {response_bytes[rb],8'd0};
  315. for(bitno=0;bitno<8;bitno=bitno+1)
  316. crc_check=crc_check[15] ? (crc_check<<1)^16'h1021 : crc_check<<1;
  317. end
  318. if(crc_check !== {response_bytes[response_count-1],response_bytes[response_count-2]})
  319. $fatal(1,"response CRC mismatch");
  320. if(response_bytes[2]==8'h80) begin
  321. if(response_bytes[5] !== ((acks<=2) ? 8'h01 : (acks==3 ? 8'h03 : (acks==4 ? 8'h05 : (acks==5 ? 8'h02 : (acks==6 ? 8'h04 : 8'h08))))))
  322. $fatal(1,"ACK request type corrupted: %02x",response_bytes[5]);
  323. if(response_bytes[6] !== (acks==3 ? 8'd2 : 8'd0))
  324. $fatal(1,"ACK business status corrupted: %02x",response_bytes[6]);
  325. end else if(response_bytes[2]==8'h81) begin
  326. if(acks!=5) $fatal(1,"DATA preceded its ACK");
  327. end else if(response_bytes[2]==8'h83) begin
  328. if(acks!=7 || response_bytes[5]!=1 || response_bytes[10]!=0 || response_bytes[11]!=4)
  329. $fatal(1,"EVENT payload count/axis/code corruption");
  330. end else $fatal(1,"unexpected response type");
  331. response_checks=response_checks+1;response_count=0;
  332. end
  333. end
  334. initial begin #1000000; $fatal(1,"MRDIMM e2e deadline");end
  335. initial begin
  336. clk = 1'b0;
  337. repeat (4) @(posedge clk);
  338. rst_n = 1'b1;
  339. end
  340. endmodule