final_core_units_tb.v 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. `timescale 1ns/1ps
  2. // Unit testbench for duty_register_bank, watchdog_30s and get_data_6400
  3. // with independent expected values.
  4. module final_core_units_tb;
  5. reg clk;
  6. reg rst_n;
  7. initial begin
  8. clk = 0;
  9. forever #20 clk = ~clk; // 40 ns period; functional timing only
  10. end
  11. integer failures;
  12. reg [7:0] expected;
  13. // ---------- duty_register_bank ----------
  14. reg [4:0] pwm_addr;
  15. wire [7:0] pwm_duty;
  16. reg stage_we;
  17. reg [4:0] stage_addr;
  18. reg [7:0] stage_data;
  19. reg commit;
  20. reg invalidate;
  21. wire bank_valid;
  22. wire bank_active;
  23. duty_register_bank #(.CHANNEL_COUNT(32), .ADDR_WIDTH(5),
  24. .DEFAULT_PTC_MASK(32'h00FFFFFF)) u_bank (
  25. .clk(clk), .rst_n(rst_n),
  26. .pwm_addr(pwm_addr), .pwm_duty(pwm_duty),
  27. .stage_we(stage_we), .stage_addr(stage_addr), .stage_data(stage_data),
  28. .commit(commit), .invalidate(invalidate),
  29. .valid(bank_valid), .active_bank(bank_active)
  30. );
  31. // ---------- watchdog_30s (short timeout) ----------
  32. reg arm;
  33. reg reload;
  34. wire expired;
  35. wire counting;
  36. watchdog_30s #(.TIMEOUT_CLKS(20)) u_wd (
  37. .clk(clk), .rst_n(rst_n),
  38. .arm(arm), .reload(reload),
  39. .expired(expired), .counting(counting)
  40. );
  41. // ---------- get_data_6400 ----------
  42. reg gd_start;
  43. wire gd_ready;
  44. wire [4:0] gd_bank_addr;
  45. reg [7:0] gd_bank_duty;
  46. wire [5:0] gd_temp_addr;
  47. wire [7:0] gd_temp_data;
  48. wire gd_temp_valid;
  49. wire gd_we;
  50. wire [7:0] gd_addr;
  51. wire [7:0] gd_data;
  52. // Production temperature RAM: the assembler is proven against the
  53. // real synchronous model, pre-written with independently computed bytes.
  54. reg tw_en;
  55. reg [5:0] tw_addr;
  56. reg [7:0] tw_data;
  57. temp_ram u_temp (
  58. .clk(clk),
  59. .wr_en(tw_en), .wr_addr(tw_addr), .wr_data(tw_data),
  60. .rd_addr(gd_temp_addr), .rd_data(gd_temp_data), .rd_valid(gd_temp_valid)
  61. );
  62. // Registered bank answer model: matches the real duty_register_bank
  63. // (BRAM registered output + combinational select = lag-1).
  64. reg [7:0] gd_bank_q;
  65. always @(posedge clk) gd_bank_q <= 8'd60 + gd_bank_addr;
  66. get_data_6400 u_gd (
  67. .clk(clk), .rst_n(rst_n),
  68. .start(gd_start), .ready(gd_ready),
  69. .bank_addr(gd_bank_addr), .bank_duty(gd_bank_q),
  70. .temp_addr(gd_temp_addr), .temp_data(gd_temp_data), .temp_valid(gd_temp_valid),
  71. .reply_we(gd_we), .reply_addr(gd_addr), .reply_data(gd_data)
  72. );
  73. reg [7:0] captured [0:82];
  74. integer i;
  75. task check;
  76. input [7:0] got;
  77. input [7:0] want;
  78. input [255:0] name;
  79. begin
  80. if (got !== want) begin
  81. $display("FAIL %0s: got %0d want %0d", name, got, want);
  82. failures = failures + 1;
  83. end
  84. end
  85. endtask
  86. initial begin
  87. failures = 0;
  88. rst_n = 0;
  89. pwm_addr = 0; stage_we = 0; stage_addr = 0; stage_data = 0;
  90. commit = 0; invalidate = 0;
  91. arm = 0; reload = 0;
  92. gd_start = 0; tw_en = 0; tw_addr = 0; tw_data = 0;
  93. repeat (4) @(posedge clk);
  94. rst_n = 1;
  95. repeat (2) @(posedge clk);
  96. // --- bank: defaults presented while invalid ---
  97. pwm_addr = 5'd0; repeat (3) @(posedge clk);
  98. check(pwm_duty, 8'd0, "default ptc0");
  99. pwm_addr = 5'd24; repeat (3) @(posedge clk);
  100. check(pwm_duty, 8'd20, "default front0");
  101. if (bank_valid !== 1'b0) begin
  102. $display("FAIL valid should be 0 after reset");
  103. failures = failures + 1;
  104. end
  105. // --- stage 32 values, commit, read back exact duty ---
  106. // Hold each byte across its sampling edge: present on the falling
  107. // edge so the synchronous BRAM sees stable address/data at posedge.
  108. for (i = 0; i < 32; i = i + 1) begin
  109. @(negedge clk);
  110. stage_we = 1; stage_addr = i[4:0]; stage_data = 8'd60 + i[7:0];
  111. end
  112. @(negedge clk);
  113. stage_we = 0;
  114. @(negedge clk);
  115. commit = 1;
  116. @(posedge clk); #1;
  117. commit = 0;
  118. @(negedge clk);
  119. stage_we = 0; stage_addr = 5'd0; stage_data = 8'd0;
  120. pwm_addr = 5'd0;
  121. repeat (4) @(posedge clk);
  122. if (bank_valid !== 1'b1) begin
  123. $display("FAIL valid should be 1 after commit");
  124. failures = failures + 1;
  125. end
  126. // The address is a registered input; the duty is valid on the
  127. // cycle after a full address-stable clock. Prime the address,
  128. // wait one full clock, then sample one channel per cycle.
  129. // The duty is the registered read result of the address present
  130. // at the last posedge, so the sample for channel i is taken
  131. // after presenting channel i+1 (one pipeline beat behind).
  132. // Prime the address, then sample one channel per cycle with the
  133. // address held across each sampling edge.
  134. // The duty is the registered read result of the address present
  135. // at the last posedge: sample channel i after presenting i+1.
  136. // The duty is the registered read result of the address present
  137. // at the last posedge: hold address i across its sampling edge,
  138. // then sample. Present the first address before the loop.
  139. // Sample channel i while presenting channel i: the negedge
  140. // presents the address, the posedge samples the settled beat.
  141. for (i = 0; i < 32; i = i + 1) begin
  142. @(negedge clk);
  143. pwm_addr = i[4:0];
  144. @(posedge clk); @(posedge clk); #1;
  145. expected = 8'd60 + i[7:0];
  146. check(pwm_duty, expected, "bank value");
  147. end
  148. // --- invalidate restores defaults, bank content is kept but hidden ---
  149. invalidate = 1; @(posedge clk); invalidate = 0;
  150. repeat (3) @(posedge clk);
  151. pwm_addr = 5'd5; repeat (3) @(posedge clk);
  152. check(pwm_duty, 8'd0, "expiry ptc5");
  153. // --- watchdog: arm then expire after 20 sampling edges ---
  154. arm = 1; @(posedge clk); #1; arm = 0;
  155. if (counting !== 1'b1) begin
  156. $display("FAIL watchdog should count after arm");
  157. failures = failures + 1;
  158. end
  159. repeat (20) @(posedge clk); #1;
  160. if (expired !== 1'b1) begin
  161. $display("FAIL watchdog should expire after 20 clocks");
  162. failures = failures + 1;
  163. end
  164. @(posedge clk); #1;
  165. if (expired !== 1'b0) begin
  166. $display("FAIL expiry pulse must be one cycle");
  167. failures = failures + 1;
  168. end
  169. if (counting !== 1'b0) begin
  170. $display("FAIL watchdog should stop after expiry");
  171. failures = failures + 1;
  172. end
  173. // reload wins over expiry on the same edge: reload at terminal count.
  174. // The counter was already fixed to fire exactly 20 edges after arm
  175. // (off-by-one repair), so the terminal count is reached after 19
  176. // more edges here.
  177. arm = 1; @(posedge clk); #1; arm = 0;
  178. repeat (19) @(posedge clk); #1;
  179. reload = 1; @(posedge clk); #1; reload = 0;
  180. repeat (2) @(posedge clk); #1;
  181. if (expired !== 1'b0) begin
  182. $display("FAIL reload must suppress expiry at terminal count");
  183. failures = failures + 1;
  184. end
  185. if (counting !== 1'b1) begin
  186. $display("FAIL watchdog should keep counting after reload");
  187. failures = failures + 1;
  188. end
  189. repeat (18) @(posedge clk); #1;
  190. if (expired !== 1'b1) begin
  191. $display("FAIL watchdog should expire 20 clocks after reload");
  192. failures = failures + 1;
  193. end
  194. // --- get_data_6400: 83-byte assembly against the real temp RAM ---
  195. // bank presents committed values (independently known: 60+i)
  196. // Pre-write the production temp RAM with independently computed bytes:
  197. // BMC channel i = 100+i, CT channel i = (i<24 ? i : i+8)+100.
  198. for (i = 0; i < 24; i = i + 1) begin
  199. @(negedge clk);
  200. tw_en = 1; tw_addr = i[5:0]; tw_data = i[7:0] + 8'd100;
  201. end
  202. for (i = 0; i < 27; i = i + 1) begin
  203. @(negedge clk);
  204. tw_en = 1; tw_addr = i[5:0] + 6'd32;
  205. tw_data = ((i < 24) ? i : (i + 8)) + 100;
  206. end
  207. @(negedge clk);
  208. tw_en = 0;
  209. gd_start = 1; @(posedge clk); #1; gd_start = 0;
  210. // feed sources every cycle: bank model answers next cycle, temperature
  211. // answers with a two-cycle lag so the assembler must wait on valid
  212. // The bank model answers with the registered synchronous-RAM
  213. // contract: the duty for the address present at the last edge.
  214. // Both sources answer with the registered synchronous-RAM
  215. // contract: the value for the address present at the last edge.
  216. // Drive them from one synchronous model AFTER the clock so the
  217. // assembler (which samples at the edge) sees last-edge values.
  218. for (i = 0; i < 900; i = i + 1) begin
  219. @(posedge clk); #1;
  220. if (gd_we) captured[gd_addr] = gd_data;
  221. if (gd_ready) begin
  222. // Sample the staged reply on the SAME edge as ready: the
  223. // blocking-assignment capture above already took it.
  224. i = 100000;
  225. end
  226. end
  227. if (gd_ready !== 1'b1) begin
  228. $display("FAIL get_data ready never fired");
  229. failures = failures + 1;
  230. end
  231. for (i = 0; i < 32; i = i + 1) begin
  232. expected = 8'd60 + i[7:0];
  233. check(captured[i], expected, "get ptc/front/rear");
  234. end
  235. for (i = 0; i < 24; i = i + 1) begin
  236. expected = i[7:0] + 8'd100;
  237. check(captured[32 + i], expected, "get bmc");
  238. end
  239. for (i = 0; i < 27; i = i + 1) begin
  240. expected = ((i < 24) ? i : (i + 8)) + 100;
  241. check(captured[56 + i], expected[7:0], "get ct");
  242. end
  243. if (failures == 0) $display("final_core_units_tb PASS");
  244. else $display("final_core_units_tb FAIL (%0d)", failures);
  245. if (failures != 0) $finish(1);
  246. $finish;
  247. end
  248. endmodule