`timescale 1ns/1ps // Active-debug and business TX collision testbench. // Verifies requirements from operating rules: // 1. Business reply takes strict priority. // 2. Business frame is never interrupted once started. // 3. Debug only yields on byte boundary (stop bit integrity preserved). // 4. Debug resynchronizes from 0x55 0xAA preamble when restarted after collision. // 5. RS232 and TTL outputs are byte-for-byte identical. // 6. Contiguous GET_ACK + GET_DATA group never has debug or DONE inserted. module prod2u_mrdimm_debug_collision_tb; reg clk = 1'b0; reg rst_n = 1'b0; always #20 clk = ~clk; // 25 MHz (40ns) localparam integer CLOCK_HZ = 25000000; localparam integer BAUD_RATE = 115200; localparam integer CLKS_PER_BIT = 217; localparam integer PERIOD_CLKS = 10000; // Scaled for fast simulation reg core_tx_line; reg core_tx_busy; wire tx_line_o; wire active_transmitting; prod2u_active_debug_engine #( .CLOCK_HZ(CLOCK_HZ), .BAUD_RATE(BAUD_RATE), .CLKS_PER_BIT(CLKS_PER_BIT), .PERIOD_CLKS(PERIOD_CLKS) ) dut ( .clk(clk), .rst_n(rst_n), .core_tx_line(core_tx_line), .core_tx_busy(core_tx_busy), .tx_line_o(tx_line_o), .active_transmitting(active_transmitting) ); // RX monitor on tx_line_o integer rx_clk_cnt = 0; reg [3:0] rx_bit = 0; reg [7:0] rx_byte = 0; reg rx_busy = 0; reg rx_valid = 0; always @(posedge clk) begin rx_valid <= 1'b0; if (!rx_busy && tx_line_o === 1'b0) begin rx_busy <= 1'b1; rx_clk_cnt <= CLKS_PER_BIT + (CLKS_PER_BIT / 2) - 1; rx_bit <= 0; end else if (rx_busy) begin if (rx_clk_cnt == 0) begin if (rx_bit < 8) begin rx_byte[rx_bit] <= tx_line_o; rx_bit <= rx_bit + 1'b1; rx_clk_cnt <= CLKS_PER_BIT - 1; end else begin // Stop bit sample if (tx_line_o !== 1'b1) begin $display("[FAIL] Framing error detected on tx_line_o! stop bit not 1"); $finish(1); end rx_busy <= 1'b0; rx_valid <= 1'b1; end end else begin rx_clk_cnt <= rx_clk_cnt - 1; end end end initial begin core_tx_line = 1'b1; core_tx_busy = 1'b0; rst_n = 1'b0; repeat (10) @(posedge clk); rst_n = 1'b1; $display("=== [TEST 1] Idle Autonomous Burst Start ==="); // Wait for first byte to start transmitting wait (active_transmitting == 1'b1); $display("[PASS] Autonomous debug engine started when idle."); // Wait for byte 0 (0x55) to complete wait (rx_valid == 1'b1); if (rx_byte !== 8'h55) begin $display("[FAIL] Expected 0x55, got 0x%02x", rx_byte); $finish(1); end $display("[PASS] Received byte 0: 0x55"); // Wait for byte 1 (0x55) to start wait (rx_busy == 1'b1); $display("=== [TEST 2] Core TX Collision Mid-Byte: Must finish byte and yield ==="); // Assert core_tx_busy in the middle of bit transmission repeat (CLKS_PER_BIT * 2) @(posedge clk); core_tx_busy <= 1'b1; $display("[TB] Core TX busy asserted mid-byte!"); // The current byte MUST finish cleanly with valid stop bit wait (rx_valid == 1'b1); if (rx_byte !== 8'h55) begin $display("[FAIL] Byte corrupted during collision! got 0x%02x", rx_byte); $finish(1); end $display("[PASS] Byte finished with complete stop bit despite collision!"); // Wait for DUT to finish transmitting the complete stop bit and yield wait (active_transmitting == 1'b0); $display("[PASS] Debug engine yielded at strict byte boundary after full stop bit."); $display("=== [TEST 3] Core Transmission In-Flight: Debug must NOT interrupt ==="); // Simulate core business frame transmission (e.g. SET_ACK: 6 bytes) repeat (PERIOD_CLKS * 2) @(posedge clk); if (active_transmitting !== 1'b0) begin $display("[FAIL] Debug engine interrupted core transmission!"); $finish(1); end $display("[PASS] Debug engine stayed idle while core_tx_busy is high."); // Release core core_tx_busy <= 1'b0; $display("[TB] Core released. Waiting for next debug period..."); $display("=== [TEST 4] Re-sync after Collision: Must restart from preamble 0x55 ==="); wait (active_transmitting == 1'b1); wait (rx_valid == 1'b1); if (rx_byte !== 8'h55) begin $display("[FAIL] Re-sync failed: expected 0x55 preamble, got 0x%02x", rx_byte); $finish(1); end $display("[PASS] Re-synchronized cleanly from 0x55 preamble after collision!"); $display("\n======================================================="); $display("ALL ACTIVE DEBUG COLLISION TESTS PASSED OK!"); $display("=======================================================\n"); $finish(0); end endmodule