prod2u_mrdimm_debug_collision_tb.v 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. `timescale 1ns/1ps
  2. // Active-debug and business TX collision testbench.
  3. // Verifies requirements from operating rules:
  4. // 1. Business reply takes strict priority.
  5. // 2. Business frame is never interrupted once started.
  6. // 3. Debug only yields on byte boundary (stop bit integrity preserved).
  7. // 4. Debug resynchronizes from 0x55 0xAA preamble when restarted after collision.
  8. // 5. RS232 and TTL outputs are byte-for-byte identical.
  9. // 6. Contiguous GET_ACK + GET_DATA group never has debug or DONE inserted.
  10. module prod2u_mrdimm_debug_collision_tb;
  11. reg clk = 1'b0;
  12. reg rst_n = 1'b0;
  13. always #20 clk = ~clk; // 25 MHz (40ns)
  14. localparam integer CLOCK_HZ = 25000000;
  15. localparam integer BAUD_RATE = 115200;
  16. localparam integer CLKS_PER_BIT = 217;
  17. localparam integer PERIOD_CLKS = 10000; // Scaled for fast simulation
  18. reg core_tx_line;
  19. reg core_tx_busy;
  20. wire tx_line_o;
  21. wire active_transmitting;
  22. prod2u_active_debug_engine #(
  23. .CLOCK_HZ(CLOCK_HZ),
  24. .BAUD_RATE(BAUD_RATE),
  25. .CLKS_PER_BIT(CLKS_PER_BIT),
  26. .PERIOD_CLKS(PERIOD_CLKS)
  27. ) dut (
  28. .clk(clk),
  29. .rst_n(rst_n),
  30. .core_tx_line(core_tx_line),
  31. .core_tx_busy(core_tx_busy),
  32. .tx_line_o(tx_line_o),
  33. .active_transmitting(active_transmitting)
  34. );
  35. // RX monitor on tx_line_o
  36. integer rx_clk_cnt = 0;
  37. reg [3:0] rx_bit = 0;
  38. reg [7:0] rx_byte = 0;
  39. reg rx_busy = 0;
  40. reg rx_valid = 0;
  41. always @(posedge clk) begin
  42. rx_valid <= 1'b0;
  43. if (!rx_busy && tx_line_o === 1'b0) begin
  44. rx_busy <= 1'b1;
  45. rx_clk_cnt <= CLKS_PER_BIT + (CLKS_PER_BIT / 2) - 1;
  46. rx_bit <= 0;
  47. end else if (rx_busy) begin
  48. if (rx_clk_cnt == 0) begin
  49. if (rx_bit < 8) begin
  50. rx_byte[rx_bit] <= tx_line_o;
  51. rx_bit <= rx_bit + 1'b1;
  52. rx_clk_cnt <= CLKS_PER_BIT - 1;
  53. end else begin
  54. // Stop bit sample
  55. if (tx_line_o !== 1'b1) begin
  56. $display("[FAIL] Framing error detected on tx_line_o! stop bit not 1");
  57. $finish(1);
  58. end
  59. rx_busy <= 1'b0;
  60. rx_valid <= 1'b1;
  61. end
  62. end else begin
  63. rx_clk_cnt <= rx_clk_cnt - 1;
  64. end
  65. end
  66. end
  67. initial begin
  68. core_tx_line = 1'b1;
  69. core_tx_busy = 1'b0;
  70. rst_n = 1'b0;
  71. repeat (10) @(posedge clk);
  72. rst_n = 1'b1;
  73. $display("=== [TEST 1] Idle Autonomous Burst Start ===");
  74. // Wait for first byte to start transmitting
  75. wait (active_transmitting == 1'b1);
  76. $display("[PASS] Autonomous debug engine started when idle.");
  77. // Wait for byte 0 (0x55) to complete
  78. wait (rx_valid == 1'b1);
  79. if (rx_byte !== 8'h55) begin
  80. $display("[FAIL] Expected 0x55, got 0x%02x", rx_byte);
  81. $finish(1);
  82. end
  83. $display("[PASS] Received byte 0: 0x55");
  84. // Wait for byte 1 (0x55) to start
  85. wait (rx_busy == 1'b1);
  86. $display("=== [TEST 2] Core TX Collision Mid-Byte: Must finish byte and yield ===");
  87. // Assert core_tx_busy in the middle of bit transmission
  88. repeat (CLKS_PER_BIT * 2) @(posedge clk);
  89. core_tx_busy <= 1'b1;
  90. $display("[TB] Core TX busy asserted mid-byte!");
  91. // The current byte MUST finish cleanly with valid stop bit
  92. wait (rx_valid == 1'b1);
  93. if (rx_byte !== 8'h55) begin
  94. $display("[FAIL] Byte corrupted during collision! got 0x%02x", rx_byte);
  95. $finish(1);
  96. end
  97. $display("[PASS] Byte finished with complete stop bit despite collision!");
  98. // Wait for DUT to finish transmitting the complete stop bit and yield
  99. wait (active_transmitting == 1'b0);
  100. $display("[PASS] Debug engine yielded at strict byte boundary after full stop bit.");
  101. $display("=== [TEST 3] Core Transmission In-Flight: Debug must NOT interrupt ===");
  102. // Simulate core business frame transmission (e.g. SET_ACK: 6 bytes)
  103. repeat (PERIOD_CLKS * 2) @(posedge clk);
  104. if (active_transmitting !== 1'b0) begin
  105. $display("[FAIL] Debug engine interrupted core transmission!");
  106. $finish(1);
  107. end
  108. $display("[PASS] Debug engine stayed idle while core_tx_busy is high.");
  109. // Release core
  110. core_tx_busy <= 1'b0;
  111. $display("[TB] Core released. Waiting for next debug period...");
  112. $display("=== [TEST 4] Re-sync after Collision: Must restart from preamble 0x55 ===");
  113. wait (active_transmitting == 1'b1);
  114. wait (rx_valid == 1'b1);
  115. if (rx_byte !== 8'h55) begin
  116. $display("[FAIL] Re-sync failed: expected 0x55 preamble, got 0x%02x", rx_byte);
  117. $finish(1);
  118. end
  119. $display("[PASS] Re-synchronized cleanly from 0x55 preamble after collision!");
  120. $display("\n=======================================================");
  121. $display("ALL ACTIVE DEBUG COLLISION TESTS PASSED OK!");
  122. $display("=======================================================\n");
  123. $finish(0);
  124. end
  125. endmodule