`timescale 1ns/1ps // Real actuator + arbiter + host frame/UART path. The ordinary reply builder // writes while an older DONE is on the wire and the same axis finishes again. // Define DONE_SNAPSHOT_FIX for the patched interface; omit for the baseline. module mrdimm_done_parallel_tb; localparam integer BIT_CLKS = 27; reg clk = 0; always #20 clk = ~clk; reg rst_n = 0; reg [4:0] us_count = 0; wire tick_us = (us_count == 24); always @(posedge clk) begin if (!rst_n || tick_us) us_count <= 0; else us_count <= us_count + 1'b1; end reg enqueue_we = 0; reg [15:0] enqueue_steps = 0; wire [11:0] pending, ack; wire [3:0] done_rd_axis; wire done_rd_busy; wire [1:0] done_rd_reason; wire [15:0] done_rd_steps; mrdimm_actuator_core actuator ( .clk(clk), .rst_n(rst_n), .tick_us(tick_us), .watchdog_expired(1'b0), .enqueue_we(enqueue_we), .enqueue_axis(4'd5), .enqueue_steps(enqueue_steps), .enqueue_half_period_us(16'd1), .enqueue_dir(1'b0), .stop_clear_mask(12'd0), .axis_queue_full(), .limit_lower_raw(12'd0), .limit_upper_raw(12'd0), .limit_lower_sync(), .limit_upper_sync(), .step_out(), .dir_out(), .axis_active_out(), .done_pending(pending), .done_rd_axis(done_rd_axis), .done_rd_busy(done_rd_busy), .done_rd_reason(done_rd_reason), .done_rd_steps(done_rd_steps), .done_ack(ack) ); reg group_valid = 0; wire group_done, tx_start, tx_busy, tx_done; wire [7:0] tx_type, tx_length, tx_base, payload_addr, payload_data; wire old_wr_en; wire [7:0] old_wr_addr, old_wr_data; wire [7:0] snapshot_data; unified_tx_arbiter arbiter ( .clk(clk), .rst_n(rst_n), .group_valid(group_valid), .group_second(1'b1), .group_type(8'h82), .group_length(8'd0), .second_type(8'h42), .second_length(8'd65), .group_base(8'd0), .second_base(8'd0), .group_done(group_done), .done_pending(pending), .done_rd_axis(done_rd_axis), .done_rd_busy(done_rd_busy), .done_rd_reason(done_rd_reason), .done_rd_steps(done_rd_steps), .done_ack(ack), .rep_wr_en(old_wr_en), .rep_wr_addr(old_wr_addr), .rep_wr_data(old_wr_data), `ifdef DONE_SNAPSHOT_FIX .done_payload_addr(payload_addr[1:0]), .done_payload_data(snapshot_data), `endif .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length), .tx_base(tx_base), .tx_busy(tx_busy), .tx_done(tx_done) ); reg builder_we = 0; reg [7:0] builder_addr = 0, builder_data = 0; wire [7:0] ordinary_data; reply_ram ram ( .clk(clk), .wr_en(old_wr_en | builder_we), .wr_addr(old_wr_en ? old_wr_addr : builder_addr), .wr_data(old_wr_en ? old_wr_data : builder_data), .rd_addr(payload_addr + tx_base), .rd_data(ordinary_data) ); `ifdef DONE_SNAPSHOT_FIX assign payload_data = (tx_type == 8'h50) ? snapshot_data : ordinary_data; `else assign payload_data = ordinary_data; `endif wire serial_tx, ttl_tx; host_uart_endpoint #(.CLKS_PER_BIT(BIT_CLKS)) endpoint ( .clk(clk), .rst_n(rst_n), .rs232_rx(1'b1), .rs232_tx(serial_tx), .ttl_rx(1'b1), .ttl_tx(ttl_tx), .req_valid(), .req_ready(1'b1), .req_type(), .req_length(), .req_ram_wr_en(), .req_ram_wr_addr(), .req_ram_wr_data(), .req_ram_rd_addr(), .req_ram_rd_data(8'd0), .tx_start(tx_start), .tx_type(tx_type), .tx_length(tx_length), .tx_busy(tx_busy), .tx_done(tx_done), .tx_ram_rd_addr(payload_addr), .tx_ram_rd_data(payload_data) ); wire rx_valid, rx_error; wire [7:0] rx_data; uart_rx_byte #(.CLKS_PER_BIT(BIT_CLKS)) receiver ( .clk(clk), .rst_n(rst_n), .rx_i(serial_tx), .byte_valid(rx_valid), .byte_data(rx_data), .framing_error(rx_error) ); reg [7:0] received [0:127]; integer rx_count = 0, launch_count = 0, ack_count = 0; always @(posedge clk) begin if (rst_n) begin if (rx_error) $fatal(1, "UART framing error"); if (serial_tx !== ttl_tx) $fatal(1, "TX broadcast diverged"); if (rx_valid) begin received[rx_count] = rx_data; rx_count = rx_count + 1; end if (tx_start) launch_count = launch_count + 1; if (ack[5]) ack_count = ack_count + 1; end end function [15:0] crc_of; input integer start_index; input integer byte_count; integer i, bit_no; reg [15:0] crc; begin crc = 16'hffff; for (i = start_index; i < start_index + byte_count; i = i + 1) for (bit_no = 7; bit_no >= 0; bit_no = bit_no - 1) crc = (crc[15] ^ received[i][bit_no]) ? {crc[14:0], 1'b0} ^ 16'h1021 : {crc[14:0], 1'b0}; crc_of = crc; end endfunction task check_frame; input integer offset; input [7:0] wanted_type; input integer wanted_length; reg [15:0] crc; begin if (received[offset] !== 8'ha5 || received[offset+1] !== 8'h5a || received[offset+2] !== wanted_type || received[offset+3] !== wanted_length[7:0]) $fatal(1, "frame header at %0d: %02x %02x %02x %02x", offset, received[offset], received[offset+1], received[offset+2], received[offset+3]); crc = crc_of(offset+2, wanted_length+2); if ({received[offset+5+wanted_length], received[offset+4+wanted_length]} !== crc) $fatal(1, "frame CRC at %0d", offset); end endtask task check_done; input integer offset; input [15:0] wanted_steps; begin check_frame(offset, 8'h50, 4); if (received[offset+4] !== 8'd5 || received[offset+5] !== 8'd0 || {received[offset+7], received[offset+6]} !== wanted_steps) $fatal(1, "DONE overwritten/lost at %0d: axis=%0d reason=%0d steps=%0d wanted=%0d", offset, received[offset+4], received[offset+5], {received[offset+7], received[offset+6]}, wanted_steps); end endtask task enqueue; input [15:0] count; begin @(negedge clk); enqueue_we = 1; enqueue_steps = count; @(negedge clk); enqueue_we = 0; end endtask integer i; reg no_get; initial begin no_get = $test$plusargs("no_get_write"); repeat (6) @(negedge clk); rst_n = 1; enqueue(16'd2); wait(tx_start && tx_type == 8'h50); fork begin // This second segment completes in about 16us, well before // the first 10-byte DONE finishes at about 108us. enqueue(16'd3); end begin if (!no_get) begin for (i = 0; i < 65; i = i + 1) begin @(negedge clk); builder_we = 1; builder_addr = i; builder_data = 8'ha0 + i; end @(negedge clk); builder_we = 0; group_valid = 1; wait(group_done); @(negedge clk); group_valid = 0; end end join if (no_get) begin wait(rx_count == 20); check_done(0, 16'd2); check_done(10, 16'd3); end else begin wait(rx_count >= 10); check_done(0, 16'd2); wait(rx_count == 97); check_frame(10, 8'h82, 0); check_frame(16, 8'h42, 65); for (i = 0; i < 65; i = i + 1) if (received[20+i] !== (8'ha0 + i) % 256) $fatal(1, "GET payload collision at %0d got=%02x", i, received[20+i]); check_done(87, 16'd3); end repeat (2*BIT_CLKS) @(negedge clk); if (pending !== 0 || ack_count != 2 || launch_count != (no_get ? 2 : 4)) $fatal(1, "event count wrong pending=%h ack=%0d launches=%0d", pending, ack_count, launch_count); $display("PASS mrdimm_done_parallel no_get=%0d bytes=%0d frames=%0d acknowledgements=%0d", no_get, rx_count, launch_count, ack_count); $finish; end initial begin #2000000; $fatal(1, "timeout: bytes=%0d frames=%0d ack=%0d pending=%h (old TX ack may have cleared newer DONE)", rx_count, launch_count, ack_count, pending); end endmodule