`timescale 1ns/1ps // UART-level end-to-end test for the MRDIMM Host wire path over the final // 8-bit-length protocol, driven through the shared host_uart_endpoint // (real 921600/25 MHz bit timing: CLKS_PER_BIT=27) with the product's own // frame sizes: a 68-byte SET_ALL (fan/PTC plus twelve 5-byte axis slots), // a 4-byte MOTOR_DONE reply and a 63-byte GET_DATA reply. // // SET_ALL payload layout (AGENTS.md / contracts): 0..3 FAN, 4..7 logical // PTC, axis i slot at 8 + 5*i = {action:u8, steps:u16LE, half_period_us:u16LE}. // The request/reply CRCs are the independent Python codec // (tools/crc16_ccitt_table.frame_bytes) vectors, hand-embedded; the TB // verifies every received frame with its own bit-serial CRC over the // decoded wire bytes, never with the DUT's accumulator. // // NOTE (protocol worker, 2026-09-20): this bench covers the shared // transport primitives plus the MRDIMM frame sizes. The product command // core (mrdimm_host_command) is mid-migration to the frozen 8-bit macro // set by its owner; when it lands, its own host-level bench re-attaches // the command semantics. module prod2u_mrdimm_uart_e2e_tb; localparam integer CLKS = 27; localparam integer TIMEOUT_CYCLES = 250000; reg clk = 1'b0; reg rst_n = 1'b0; reg rs232_rx = 1'b1; wire rs232_tx; reg ttl_rx = 1'b1; wire ttl_tx; reg req_ready = 1'b0; wire req_valid; wire [7:0] req_type; wire [7:0] req_length; wire req_ram_wr_en; wire [7:0] req_ram_wr_addr; wire [7:0] req_ram_wr_data; wire [7:0] req_ram_rd_addr; wire [7:0] req_ram_rd_data; reg tx_start = 1'b0; reg [7:0] tx_type = 8'd0; reg [7:0] tx_length = 8'd0; wire tx_busy; wire tx_done; wire [7:0] tx_ram_rd_addr; wire [7:0] tx_ram_rd_data; integer i; integer k; always #20 clk = ~clk; // 25 MHz host_uart_endpoint #( .CLKS_PER_BIT(CLKS), .INTER_BYTE_TIMEOUT_CYCLES(TIMEOUT_CYCLES), .MAX_PAYLOAD(128), .ADDR_WIDTH(8) ) dut ( .clk(clk), .rst_n(rst_n), .rs232_rx(rs232_rx), .rs232_tx(rs232_tx), .ttl_rx(ttl_rx), .ttl_tx(ttl_tx), .req_valid(req_valid), .req_ready(req_ready), .req_type(req_type), .req_length(req_length), .req_ram_wr_en(req_ram_wr_en), .req_ram_wr_addr(req_ram_wr_addr), .req_ram_wr_data(req_ram_wr_data), .req_ram_rd_addr(req_ram_rd_addr), .req_ram_rd_data(req_ram_rd_data), .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length), .tx_busy(tx_busy), .tx_done(tx_done), .tx_ram_rd_addr(tx_ram_rd_addr), .tx_ram_rd_data(tx_ram_rd_data) ); // Request staging RAM (128 bytes); parser owns the write port. reg [7:0] req_mem [0:127]; initial begin for (i = 0; i < 128; i = i + 1) req_mem[i] = 8'hA9; end always @(posedge clk) begin if (req_ram_wr_en) req_mem[req_ram_wr_addr] <= req_ram_wr_data; end assign req_ram_rd_data = req_mem[req_ram_rd_addr]; integer req_writes; initial req_writes = 0; always @(negedge clk) begin if (req_ram_wr_en) begin req_writes = req_writes + 1; if (req_ram_wr_addr >= 128) $fatal(1, "request RAM address out of range %0d", req_ram_wr_addr); end end // Reply staging RAM (128 bytes, synchronous read) feeds frame_tx. reg rep_wr_en = 1'b0; reg [7:0] rep_wr_addr = 8'd0; reg [7:0] rep_wr_data = 8'd0; reply_ram #(.DEPTH(128), .ADDR_WIDTH(8)) u_reply ( .clk(clk), .wr_en(rep_wr_en), .wr_addr(rep_wr_addr), .wr_data(rep_wr_data), .rd_addr(tx_ram_rd_addr), .rd_data(tx_ram_rd_data) ); // ---- request frame buffer sent over the wire ---- reg [7:0] frame_buf [0:95]; integer frame_len; // ---- independent CRC-16/CCITT-FALSE over decoded wire bytes ---- reg [7:0] rx_snapshot [0:511]; integer rx_snapshot_n; initial rx_snapshot_n = 0; function [15:0] tb_crc; input integer base; input integer nbody; integer ck, bitno; reg [15:0] c; begin c = 16'hFFFF; for (ck = 0; ck < nbody; ck = ck + 1) begin c = c ^ {rx_snapshot[(base + ck) & 511], 8'd0}; for (bitno = 0; bitno < 8; bitno = bitno + 1) c = c[15] ? (c << 1) ^ 16'h1021 : c << 1; end tb_crc = c; end endfunction // TB-side CRC over the frame buffer being sent (TYPE+LENGTH+PAYLOAD). function [15:0] buf_crc; input integer nbody; integer ck, bitno; reg [15:0] c; begin c = 16'hFFFF; for (ck = 0; ck < nbody; ck = ck + 1) begin c = c ^ {frame_buf[(2 + ck) & 511], 8'd0}; for (bitno = 0; bitno < 8; bitno = bitno + 1) c = c[15] ? (c << 1) ^ 16'h1021 : c << 1; end buf_crc = c; end endfunction // ---- real UART stimulus on the RS232 RX line ---- task send_byte; input [7:0] value; integer kk; begin rs232_rx = 1'b0; #1080; for (kk = 0; kk < 8; kk = kk + 1) begin rs232_rx = value[kk]; #1080; end rs232_rx = 1'b1; #1080; end endtask task send_frame; integer kk; begin for (kk = 0; kk < frame_len; kk = kk + 1) send_byte(frame_buf[kk]); end endtask task await_req; input [7:0] etype; input [7:0] elen; integer guard; begin guard = 0; while (!req_valid && guard < 200000) begin @(posedge clk); guard = guard + 1; end if (!req_valid) $fatal(1, "request not parsed type=%h", etype); if (req_type !== etype || req_length !== elen) $fatal(1, "request metadata mismatch type=%h len=%0d", req_type, req_length); end endtask task accept_req; begin req_ready = 1'b1; while (req_valid) @(posedge clk); @(posedge clk); @(negedge clk); req_ready = 1'b0; repeat (2) @(posedge clk); while (dut.rx_owner != 2'd0) @(posedge clk); repeat (4) @(posedge clk); end endtask // ---- TX decoder: samples RS232 and TTL at the same bit centres, so // every decoded byte also proves the broadcast copies agree bit for // bit. Mid-bit sampling (1.5 bit periods after the start fall). ---- reg [7:0] dec_byte; task decode_byte_both; integer b; begin @(negedge rs232_tx); #(CLKS * 40 + CLKS * 20); for (b = 0; b < 8; b = b + 1) begin if (rs232_tx !== ttl_tx) $fatal(1, "TX broadcast diverged at bit %0d", b); dec_byte[b] = rs232_tx; if (b < 7) #1080; end rx_snapshot[rx_snapshot_n & 511] = dec_byte; rx_snapshot_n = rx_snapshot_n + 1; #1080; if (rs232_tx !== 1'b1 || ttl_tx !== 1'b1) $fatal(1, "TX stop bit is low"); end endtask task decode_and_check; input [7:0] rtype; input integer rlen; integer n; reg [15:0] c; begin rx_snapshot_n = 0; for (n = 0; n < rlen + 6; n = n + 1) decode_byte_both; if (rx_snapshot[0] !== 8'hA5 || rx_snapshot[1] !== 8'h5A) $fatal(1, "reply magic mismatch"); if (rx_snapshot[2] !== rtype || rx_snapshot[3] !== rlen[7:0]) $fatal(1, "reply header mismatch type=%h len=%0d", rx_snapshot[2], rx_snapshot[3]); c = tb_crc(2, rlen + 2); if (c !== {rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]}) $fatal(1, "reply CRC mismatch calc=%h wire=%h%h", c, rx_snapshot[rlen + 5], rx_snapshot[rlen + 4]); end endtask // Reply-RAM prefill driven at the falling edge (a value set right // after a rising edge is sampled by that same edge in Icarus's event // order and would be dropped). task prefill_pattern; input [7:0] base; input [7:0] step; input integer nbytes; integer n2; begin for (n2 = 0; n2 < nbytes; n2 = n2 + 1) begin @(negedge clk); rep_wr_en = 1'b1; rep_wr_addr = n2[7:0]; rep_wr_data = base + (step * n2[7:0]); end @(negedge clk); rep_wr_en = 1'b0; repeat (4) @(posedge clk); end endtask task launch_reply; input [7:0] rtype; input [7:0] rlen; integer guard; begin guard = 0; while (tx_busy && guard < 200000) begin @(posedge clk); guard = guard + 1; end if (tx_busy) $fatal(1, "previous reply never finished"); tx_type = rtype; tx_length = rlen; tx_start = 1'b1; while (!tx_busy) @(posedge clk); @(posedge clk); tx_start = 1'b0; end endtask initial begin repeat (4) @(posedge clk); rst_n = 1'b1; repeat (4) @(posedge clk); // ---- 1. SET_ALL: FAN 20 %, PTC 0 %, axis0 PUSH_DOWN 500/500, // axis4 PUSH_UP 0x1122 steps @ 0x0032 half period, rest NOTHING. frame_buf[0] = 8'hA5; frame_buf[1] = 8'h5A; frame_buf[2] = 8'h01; frame_buf[3] = 8'h44; for (k = 0; k < 4; k = k + 1) frame_buf[4 + k] = 8'h14; for (k = 4; k < 8; k = k + 1) frame_buf[4 + k] = 8'h00; for (k = 0; k < 60; k = k + 1) frame_buf[12 + k] = 8'h00; // axis0 slot at payload 8 (frame index 12) frame_buf[12] = 8'h01; frame_buf[13] = 8'hF4; frame_buf[14] = 8'h01; frame_buf[15] = 8'hF4; frame_buf[16] = 8'h00; // axis4 slot at payload 28 (frame index 32) frame_buf[32] = 8'h02; frame_buf[33] = 8'h22; frame_buf[34] = 8'h11; frame_buf[35] = 8'h32; frame_buf[36] = 8'h00; frame_len = 74; // The independent codec says the wire CRC is 16 06 (low 06). if (buf_crc(70) !== 16'h1606) $fatal(1, "TB CRC disagrees with the codec vector: %h", buf_crc(70)); frame_buf[72] = 8'h06; frame_buf[73] = 8'h16; req_writes = 0; send_frame; await_req(8'h01, 8'd68); if (req_writes !== 68) $fatal(1, "SET68 writes=%0d", req_writes); for (k = 0; k < 4; k = k + 1) if (req_mem[k] !== 8'h14) $fatal(1, "fan[%0d]=%h", k, req_mem[k]); for (k = 4; k < 8; k = k + 1) if (req_mem[k] !== 8'h00) $fatal(1, "ptc[%0d]=%h", k, req_mem[k]); if (req_mem[8] !== 8'h01 || req_mem[9] !== 8'hF4 || req_mem[10] !== 8'h01 || req_mem[11] !== 8'hF4 || req_mem[12] !== 8'h00) $fatal(1, "axis0 slot %h %h %h %h %h", req_mem[8], req_mem[9], req_mem[10], req_mem[11], req_mem[12]); if (req_mem[10 + 5 * 4] !== 8'h11 || req_mem[11 + 5 * 4] !== 8'h32 || req_mem[8 + 5 * 4] !== 8'h02) $fatal(1, "axis4 slot %h %h %h", req_mem[8 + 5 * 4], req_mem[10 + 5 * 4], req_mem[11 + 5 * 4]); if (req_mem[13] !== 8'h00 || req_mem[67] !== 8'h00) $fatal(1, "NOTHING slots must stay zero"); if (req_mem[68] !== 8'hA9) $fatal(1, "SET68 spilled past extent"); if (req_ram_rd_addr !== 8'd67) $fatal(1, "validation read address %0d", req_ram_rd_addr); accept_req(); // ---- 2. GET_ALL with an empty payload (golden A5 5A 02 00 6D 7B) ---- frame_buf[0] = 8'hA5; frame_buf[1] = 8'h5A; frame_buf[2] = 8'h02; frame_buf[3] = 8'h00; frame_buf[4] = 8'h6D; frame_buf[5] = 8'h7B; frame_len = 6; req_writes = 0; send_frame; await_req(8'h02, 8'd0); if (req_writes !== 0) $fatal(1, "GET writes=%0d", req_writes); accept_req(); // ---- 3. MOTOR_DONE reply: axis4 upper-limit 4387 steps, the // golden vector A5 5A 50 04 04 02 23 11 B6 02 ---- prefill_pattern(8'h04, 8'h00, 4); // bytes 04 04 04 04; overwritten below @(negedge clk); rep_wr_en = 1'b1; rep_wr_addr = 8'd0; rep_wr_data = 8'h04; @(negedge clk); rep_wr_addr = 8'd1; rep_wr_data = 8'h02; @(negedge clk); rep_wr_addr = 8'd2; rep_wr_data = 8'h23; @(negedge clk); rep_wr_addr = 8'd3; rep_wr_data = 8'h11; @(negedge clk); rep_wr_en = 1'b0; repeat (4) @(posedge clk); launch_reply(8'h50, 8'd4); decode_and_check(8'h50, 4); if (rx_snapshot[4] !== 8'h04 || rx_snapshot[5] !== 8'h02 || rx_snapshot[6] !== 8'h23 || rx_snapshot[7] !== 8'h11 || rx_snapshot[8] !== 8'hB6 || rx_snapshot[9] !== 8'h02) $fatal(1, "MOTOR_DONE wire mismatch %h %h %h %h %h %h", rx_snapshot[4], rx_snapshot[5], rx_snapshot[6], rx_snapshot[7], rx_snapshot[8], rx_snapshot[9]); // ---- 4. GET_DATA reply, 63-byte payload (byte k = 0x09 + 5k) ---- prefill_pattern(8'h09, 8'h05, 63); launch_reply(8'h42, 8'd63); decode_and_check(8'h42, 63); for (k = 0; k < 63; k = k + 1) if (rx_snapshot[4 + k] !== (8'h09 + 8'h05 * k[7:0])) $fatal(1, "GET_DATA payload[%0d]=%h", k, rx_snapshot[4 + k]); // The independent codec says this image's CRC is BB 3E (high BB). if (rx_snapshot[67] !== 8'h3E || rx_snapshot[68] !== 8'hBB) $fatal(1, "GET_DATA wire CRC mismatch %h %h", rx_snapshot[67], rx_snapshot[68]); // ---- 5. the link still parses a legal request after all replies ---- frame_buf[0] = 8'hA5; frame_buf[1] = 8'h5A; frame_buf[2] = 8'h02; frame_buf[3] = 8'h00; frame_buf[4] = 8'h6D; frame_buf[5] = 8'h7B; frame_len = 6; send_frame; await_req(8'h02, 8'd0); accept_req(); $display("prod2u_mrdimm_uart_e2e_tb PASS"); $finish; end initial begin #400000000; $fatal(1, "mrdimm e2e deadline"); end endmodule