final_core_units_tb.v 10 KB

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