prod2u_6400_v2_temp_arb_tb.v 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. `timescale 1ns/1ps
  2. // Historical stand-alone arbitration-model test for Prod2U6400V2.
  3. // The queue logic below is copied into the TB; only temp_ram is production RTL.
  4. // Passing this file does not verify arbitration inside the production top.
  5. // External CT/BMC acquisition coverage is provided by the acquisition BFM.
  6. // Verifies:
  7. // 1. Cycle N: BMC + CT0 simultaneous write -> BMC has priority, CT0 buffered.
  8. // 2. Cycle N+1: CT1 write + pending CT0 retirement -> CT0 written to RAM, CT1 buffered.
  9. // 3. Cycle N+2: BMC + CT2 write + pending CT1 -> BMC written to RAM, CT1 & CT2 buffered.
  10. // 4. Cycle N+3..N+4: CT1 and CT2 retire in order to RAM.
  11. // 5. Read RAM verify all BMC and CT writes committed correctly in order.
  12. // 6. Queue overflow detection when overloaded.
  13. module prod2u_6400_v2_temp_arb_tb;
  14. reg clk = 1'b0;
  15. reg rst_n = 1'b0;
  16. always #20 clk = ~clk; // 25 MHz clock (40ns period)
  17. reg bmc_wr_en;
  18. reg [5:0] bmc_wr_addr;
  19. reg [7:0] bmc_wr_data;
  20. reg ct_wr_en;
  21. reg [5:0] ct_wr_addr;
  22. reg [7:0] ct_wr_data;
  23. // Arbiter internal queue state & RAM write signals under test
  24. reg [5:0] q_addr [0:3];
  25. reg [7:0] q_data [0:3];
  26. reg [1:0] q_head;
  27. reg [1:0] q_tail;
  28. reg [2:0] q_count;
  29. reg q_overflow;
  30. wire push_ct = ct_wr_en && (bmc_wr_en || (q_count > 3'd0));
  31. wire pop_ct = !bmc_wr_en && (q_count > 3'd0);
  32. always @(posedge clk or negedge rst_n) begin
  33. if (!rst_n) begin
  34. q_head <= 2'd0;
  35. q_tail <= 2'd0;
  36. q_count <= 3'd0;
  37. q_overflow <= 1'b0;
  38. end else begin
  39. case ({push_ct, pop_ct})
  40. 2'b10: begin
  41. if (q_count < 3'd4) begin
  42. q_addr[q_tail] <= ct_wr_addr;
  43. q_data[q_tail] <= ct_wr_data;
  44. q_tail <= q_tail + 2'd1;
  45. q_count <= q_count + 3'd1;
  46. end else begin
  47. q_overflow <= 1'b1;
  48. end
  49. end
  50. 2'b01: begin
  51. q_head <= q_head + 2'd1;
  52. q_count <= q_count - 3'd1;
  53. end
  54. 2'b11: begin
  55. q_head <= q_head + 2'd1;
  56. q_addr[q_tail] <= ct_wr_addr;
  57. q_data[q_tail] <= ct_wr_data;
  58. q_tail <= q_tail + 2'd1;
  59. end
  60. default: ;
  61. endcase
  62. end
  63. end
  64. wire temp_wr_en = bmc_wr_en | (q_count > 3'd0) | ct_wr_en;
  65. wire [5:0] temp_wr_addr = bmc_wr_en ? bmc_wr_addr :
  66. (q_count > 3'd0) ? q_addr[q_head] : ct_wr_addr;
  67. wire [7:0] temp_wr_data = bmc_wr_en ? bmc_wr_data :
  68. (q_count > 3'd0) ? q_data[q_head] : ct_wr_data;
  69. // Instantiate temperature RAM
  70. reg [5:0] rd_addr;
  71. wire [7:0] rd_data;
  72. wire rd_valid;
  73. temp_ram #(
  74. .CT_ACTIVE_COUNT(6'd9)
  75. ) u_temp_ram (
  76. .clk(clk),
  77. .rst_n(rst_n),
  78. .wr_en(temp_wr_en),
  79. .wr_addr(temp_wr_addr),
  80. .wr_data(temp_wr_data),
  81. .rd_addr(rd_addr),
  82. .rd_data(rd_data),
  83. .rd_valid(rd_valid)
  84. );
  85. initial begin
  86. bmc_wr_en = 1'b0;
  87. bmc_wr_addr = 6'd0;
  88. bmc_wr_data = 8'd0;
  89. ct_wr_en = 1'b0;
  90. ct_wr_addr = 6'd0;
  91. ct_wr_data = 8'd0;
  92. rd_addr = 6'd0;
  93. // Reset pulse
  94. rst_n = 1'b0;
  95. repeat (5) @(posedge clk);
  96. rst_n = 1'b1;
  97. repeat (2) @(posedge clk);
  98. $display("=== [TEST 1] Cycle N..N+3 Priority Arbitration & Orderly Retirement ===");
  99. // Cycle N: BMC + CT0
  100. // BMC writes to addr 0 with 0x25, CT0 writes to addr 32 with 0x30
  101. @(posedge clk);
  102. bmc_wr_en <= 1'b1;
  103. bmc_wr_addr <= 6'd0;
  104. bmc_wr_data <= 8'h25;
  105. ct_wr_en <= 1'b1;
  106. ct_wr_addr <= 6'd32;
  107. ct_wr_data <= 8'h30;
  108. #1;
  109. if (temp_wr_en !== 1'b1 || temp_wr_addr !== 6'd0 || temp_wr_data !== 8'h25) begin
  110. $display("[FAIL] Cycle N: BMC did not win RAM write! addr=%d data=%h", temp_wr_addr, temp_wr_data);
  111. $finish(1);
  112. end
  113. // Cycle N+1: CT1 write + pending CT0 retirement (BMC idle)
  114. @(posedge clk);
  115. bmc_wr_en <= 1'b0;
  116. ct_wr_en <= 1'b1;
  117. ct_wr_addr <= 6'd33;
  118. ct_wr_data <= 8'h31;
  119. #1;
  120. if (temp_wr_en !== 1'b1 || temp_wr_addr !== 6'd32 || temp_wr_data !== 8'h30) begin
  121. $display("[FAIL] Cycle N+1: Pending CT0 did not retire to RAM! addr=%d data=%h", temp_wr_addr, temp_wr_data);
  122. $finish(1);
  123. end
  124. // Cycle N+2: BMC + CT2 write + pending CT1
  125. @(posedge clk);
  126. bmc_wr_en <= 1'b1;
  127. bmc_wr_addr <= 6'd1;
  128. bmc_wr_data <= 8'h26;
  129. ct_wr_en <= 1'b1;
  130. ct_wr_addr <= 6'd34;
  131. ct_wr_data <= 8'h32;
  132. #1;
  133. if (temp_wr_en !== 1'b1 || temp_wr_addr !== 6'd1 || temp_wr_data !== 8'h26) begin
  134. $display("[FAIL] Cycle N+2: BMC did not win RAM write! addr=%d data=%h", temp_wr_addr, temp_wr_data);
  135. $finish(1);
  136. end
  137. // Cycle N+3: Idle inputs -> pending CT1 retires
  138. @(posedge clk);
  139. bmc_wr_en <= 1'b0;
  140. ct_wr_en <= 1'b0;
  141. #1;
  142. if (temp_wr_en !== 1'b1 || temp_wr_addr !== 6'd33 || temp_wr_data !== 8'h31) begin
  143. $display("[FAIL] Cycle N+3: Pending CT1 did not retire to RAM! addr=%d data=%h", temp_wr_addr, temp_wr_data);
  144. $finish(1);
  145. end
  146. // Cycle N+4: Idle inputs -> pending CT2 retires
  147. @(posedge clk);
  148. bmc_wr_en <= 1'b0;
  149. ct_wr_en <= 1'b0;
  150. #1;
  151. if (temp_wr_en !== 1'b1 || temp_wr_addr !== 6'd34 || temp_wr_data !== 8'h32) begin
  152. $display("[FAIL] Cycle N+4: Pending CT2 did not retire to RAM! addr=%d data=%h", temp_wr_addr, temp_wr_data);
  153. $finish(1);
  154. end
  155. // Cycle N+5: Queue should now be empty
  156. @(posedge clk);
  157. #1;
  158. if (q_count !== 3'd0) begin
  159. $display("[FAIL] Cycle N+5: Queue should be empty! count=%d", q_count);
  160. $finish(1);
  161. end
  162. // Verify RAM readback for all written addresses
  163. $display("=== [TEST 2] Verifying RAM readback contents ===");
  164. @(posedge clk); rd_addr <= 6'd0; @(posedge clk); #1;
  165. if (rd_data !== 8'h25) begin $display("[FAIL] RAM[0] != 0x25, got %h", rd_data); $finish(1); end
  166. @(posedge clk); rd_addr <= 6'd1; @(posedge clk); #1;
  167. if (rd_data !== 8'h26) begin $display("[FAIL] RAM[1] != 0x26, got %h", rd_data); $finish(1); end
  168. @(posedge clk); rd_addr <= 6'd32; @(posedge clk); #1;
  169. if (rd_data !== 8'h30) begin $display("[FAIL] RAM[32] != 0x30, got %h", rd_data); $finish(1); end
  170. @(posedge clk); rd_addr <= 6'd33; @(posedge clk); #1;
  171. if (rd_data !== 8'h31) begin $display("[FAIL] RAM[33] != 0x31, got %h", rd_data); $finish(1); end
  172. @(posedge clk); rd_addr <= 6'd34; @(posedge clk); #1;
  173. if (rd_data !== 8'h32) begin $display("[FAIL] RAM[34] != 0x32, got %h", rd_data); $finish(1); end
  174. $display("=== [TEST 3] Queue Overflow Detection ===");
  175. // Hold BMC high for 5 cycles while pushing 5 CT writes
  176. @(posedge clk);
  177. bmc_wr_en <= 1'b1; bmc_wr_addr <= 6'd2; bmc_wr_data <= 8'h50;
  178. ct_wr_en <= 1'b1; ct_wr_addr <= 6'd40; ct_wr_data <= 8'hA1; // entry 0
  179. @(posedge clk);
  180. ct_wr_addr <= 6'd41; ct_wr_data <= 8'hA2; // entry 1
  181. @(posedge clk);
  182. ct_wr_addr <= 6'd42; ct_wr_data <= 8'hA3; // entry 2
  183. @(posedge clk);
  184. ct_wr_addr <= 6'd43; ct_wr_data <= 8'hA4; // entry 3 (full)
  185. @(posedge clk);
  186. ct_wr_addr <= 6'd44; ct_wr_data <= 8'hA5; // 5th write request
  187. @(posedge clk);
  188. #1;
  189. if (q_overflow !== 1'b1) begin
  190. $display("[FAIL] Overflow flag not asserted on 5th write! q_count=%d", q_count);
  191. $finish(1);
  192. end
  193. $display("[PASS] Queue overflow asserted correctly without memory corruption: q_overflow=%b", q_overflow);
  194. @(posedge clk);
  195. bmc_wr_en <= 1'b0;
  196. ct_wr_en <= 1'b0;
  197. repeat (10) @(posedge clk);
  198. $display("\n=======================================================");
  199. $display("ALL 6400 V2.0 TEMPERATURE ARBITRATION TESTS PASSED OK!");
  200. $display("=======================================================\n");
  201. $finish(0);
  202. end
  203. endmodule