pwm_bitmap_engine_tb.v 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. `timescale 1ns/1ps
  2. // L6 acceptance: exercise the production 12500-clock phase (20 Hz) and all 100 phases.
  3. // Event-driven monitoring on phase boundaries and bitmap transitions verifies exact
  4. // 20 Hz timing (1,250,000 clocks/period) without per-clock loop overhead.
  5. module pwm_bitmap_engine_tb;
  6. localparam integer PHASE_CLKS = 12500;
  7. localparam integer PERIOD_CLKS = 100 * PHASE_CLKS;
  8. reg clk = 0;
  9. reg rst_n = 0;
  10. reg runtime_duty_ready = 0;
  11. wire [4:0] addr;
  12. reg [7:0] duty_data;
  13. wire [31:0] bitmap;
  14. wire boundary;
  15. wire boundary_advance;
  16. wire miss;
  17. wire [6:0] phase;
  18. wire idle;
  19. wire [23:0] ptc_pins = bitmap[23:0];
  20. wire [3:0] front_fan_balls;
  21. wire [3:0] rear_fan_balls;
  22. wire [7:0] fan_balls = {rear_fan_balls, front_fan_balls};
  23. wire [7:0] fan_connector_pwm = {rear_fan_balls, ~front_fan_balls};
  24. reg [7:0] duty [0:31];
  25. reg [31:0] previous_bitmap;
  26. integer high_count [0:31];
  27. integer fan_active_count [0:7];
  28. integer last_edge_cycle [0:31];
  29. integer last_boundary_cycle;
  30. integer boundary_count;
  31. integer channel;
  32. integer period;
  33. reg previous_valid;
  34. reg idle_seen;
  35. reg check_safe_mode = 0;
  36. reg check_runtime_mode = 0;
  37. integer safe_fan0_count = 0;
  38. always #20 clk = ~clk;
  39. always @(posedge clk) duty_data <= duty[addr];
  40. pwm_bitmap_engine dut (
  41. .clk(clk), .rst_n(rst_n), .runtime_duty_ready(runtime_duty_ready),
  42. .duty_rd_addr(addr), .duty_rd_data(duty_data),
  43. .logical_bitmap(bitmap), .phase_boundary(boundary),
  44. .phase_boundary_advance(boundary_advance), .deadline_miss(miss),
  45. .phase_value(phase), .scan_idle(idle)
  46. );
  47. fan_board_output_adapter board_adapter (
  48. .logical_fan_on(bitmap[31:24]), .front_fan_ball(front_fan_balls),
  49. .rear_fan_ball(rear_fan_balls)
  50. );
  51. always @(posedge idle) begin
  52. if (rst_n) idle_seen = 1;
  53. end
  54. always @(negedge rst_n) begin
  55. previous_bitmap = bitmap;
  56. previous_valid = 0;
  57. last_boundary_cycle = -1;
  58. boundary_count = 0;
  59. idle_seen = 0;
  60. for (channel = 0; channel < 32; channel = channel + 1)
  61. last_edge_cycle[channel] = -1;
  62. end
  63. // Any bitmap transition must occur exclusively on a phase boundary.
  64. always @(bitmap) begin
  65. #1;
  66. if (check_safe_mode) begin
  67. if (ptc_pins !== 0) $fatal(1, "PTC high during safe mode check");
  68. if (fan_connector_pwm !== {8{fan_connector_pwm[0]}})
  69. $fatal(1, "safe mode fan channels diverged: %b", fan_connector_pwm);
  70. end
  71. if (rst_n && previous_valid && bitmap !== previous_bitmap) begin
  72. if (!boundary)
  73. $fatal(1, "bitmap changed away from phase boundary at cycle %0d", $time / 40);
  74. for (channel = 0; channel < 32; channel = channel + 1) begin
  75. if (bitmap[channel] !== previous_bitmap[channel]) begin
  76. if (last_edge_cycle[channel] >= 0 &&
  77. ($time / 40) - last_edge_cycle[channel] < PHASE_CLKS)
  78. $fatal(1, "channel %0d pulse narrower than one phase", channel);
  79. last_edge_cycle[channel] = $time / 40;
  80. end
  81. end
  82. end
  83. if (rst_n) begin
  84. previous_bitmap = bitmap;
  85. previous_valid = 1;
  86. end
  87. end
  88. always @(posedge boundary) begin
  89. #1;
  90. if (check_safe_mode) begin
  91. if (ptc_pins !== 0) $fatal(1, "PTC high during safe mode check");
  92. if (fan_connector_pwm !== {8{fan_connector_pwm[0]}})
  93. $fatal(1, "safe mode fan channels diverged: %b", fan_connector_pwm);
  94. if (fan_connector_pwm[0])
  95. safe_fan0_count = safe_fan0_count + PHASE_CLKS;
  96. end
  97. if (check_runtime_mode) begin
  98. if (miss) $fatal(1, "unexpected scan deadline miss");
  99. if (fan_connector_pwm !== bitmap[31:24])
  100. $fatal(1, "fan connector polarity mismatch");
  101. if (fan_balls[3:0] !== ~bitmap[27:24])
  102. $fatal(1, "front fan board inversion mismatch");
  103. if (fan_balls[7:4] !== bitmap[31:28])
  104. $fatal(1, "rear fan direct path mismatch");
  105. if (ptc_pins !== bitmap[23:0])
  106. $fatal(1, "PTC polarity mismatch");
  107. for (channel = 0; channel < 32; channel = channel + 1)
  108. if (bitmap[channel]) high_count[channel] = high_count[channel] + PHASE_CLKS;
  109. for (channel = 0; channel < 8; channel = channel + 1)
  110. if (fan_connector_pwm[channel])
  111. fan_active_count[channel] = fan_active_count[channel] + PHASE_CLKS;
  112. end
  113. if (rst_n) begin
  114. if (last_boundary_cycle >= 0 &&
  115. ($time / 40) - last_boundary_cycle != PHASE_CLKS)
  116. $fatal(1, "phase boundary spacing=%0d expected=%0d",
  117. ($time / 40) - last_boundary_cycle, PHASE_CLKS);
  118. last_boundary_cycle = $time / 40;
  119. boundary_count = boundary_count + 1;
  120. previous_bitmap = bitmap;
  121. previous_valid = 1;
  122. end
  123. end
  124. task wait_for_boundary;
  125. begin
  126. @(posedge boundary);
  127. @(negedge clk);
  128. end
  129. endtask
  130. task wait_for_phase_zero;
  131. begin
  132. wait_for_boundary();
  133. while (phase != 0) wait_for_boundary();
  134. end
  135. endtask
  136. initial begin
  137. duty_data = 0;
  138. previous_bitmap = 0;
  139. previous_valid = 0;
  140. last_boundary_cycle = -1;
  141. boundary_count = 0;
  142. idle_seen = 0;
  143. for (channel = 0; channel < 32; channel = channel + 1) begin
  144. if (channel < 24) begin
  145. case (channel % 6)
  146. 0: duty[channel] = 0;
  147. 1: duty[channel] = 1;
  148. 2: duty[channel] = 2;
  149. 3: duty[channel] = 50;
  150. 4: duty[channel] = 99;
  151. default: duty[channel] = 100;
  152. endcase
  153. end else begin
  154. case (channel)
  155. 24: duty[channel] = 20;
  156. 25: duty[channel] = 50;
  157. 26: duty[channel] = 100;
  158. 27: duty[channel] = 0;
  159. 28: duty[channel] = 50;
  160. 29: duty[channel] = 20;
  161. 30: duty[channel] = 0;
  162. default: duty[channel] = 100;
  163. endcase
  164. end
  165. high_count[channel] = 0;
  166. last_edge_cycle[channel] = -1;
  167. end
  168. for (channel = 0; channel < 8; channel = channel + 1)
  169. fan_active_count[channel] = 0;
  170. // Control reset held for three complete 20 Hz carriers: connector FAN20 / PTC0.
  171. wait_for_phase_zero();
  172. for (period = 0; period < 3; period = period + 1) begin
  173. safe_fan0_count = fan_connector_pwm[0] ? PHASE_CLKS : 0;
  174. check_safe_mode = 1;
  175. repeat (99) @(posedge boundary);
  176. #10;
  177. check_safe_mode = 0;
  178. for (channel = 0; channel < 8; channel = channel + 1)
  179. if (safe_fan0_count != 20 * PHASE_CLKS)
  180. $fatal(1, "reset period %0d fan %0d high=%0d", period, channel, safe_fan0_count);
  181. wait_for_boundary();
  182. end
  183. rst_n = 1;
  184. // POR released while control output RAM is not ready: two complete safe carriers.
  185. wait_for_phase_zero();
  186. for (period = 0; period < 2; period = period + 1) begin
  187. safe_fan0_count = fan_connector_pwm[0] ? PHASE_CLKS : 0;
  188. check_safe_mode = 1;
  189. repeat (99) @(posedge boundary);
  190. #10;
  191. check_safe_mode = 0;
  192. for (channel = 0; channel < 8; channel = channel + 1)
  193. if (safe_fan0_count != 20 * PHASE_CLKS)
  194. $fatal(1, "pre-ready period %0d fan %0d high=%0d", period, channel, safe_fan0_count);
  195. wait_for_boundary();
  196. end
  197. if (dut.runtime_scan_complete)
  198. $fatal(1, "runtime scan completed before runtime_duty_ready");
  199. runtime_duty_ready = 1;
  200. wait_for_boundary();
  201. if (dut.runtime_scan_complete)
  202. $fatal(1, "runtime scan marked complete at scan-start boundary");
  203. while (!dut.runtime_scan_complete) begin
  204. if (ptc_pins !== 0)
  205. $fatal(1, "runtime bitmap committed before complete scan");
  206. @(negedge clk);
  207. end
  208. wait_for_boundary();
  209. // Start at phase zero and count one exact 50 ms / 1,250,000-clock carrier (20 Hz).
  210. wait_for_phase_zero();
  211. for (channel = 0; channel < 32; channel = channel + 1)
  212. high_count[channel] = bitmap[channel] ? PHASE_CLKS : 0;
  213. for (channel = 0; channel < 8; channel = channel + 1)
  214. fan_active_count[channel] = fan_connector_pwm[channel] ? PHASE_CLKS : 0;
  215. check_runtime_mode = 1;
  216. repeat (99) @(posedge boundary);
  217. #10;
  218. check_runtime_mode = 0;
  219. for (channel = 0; channel < 32; channel = channel + 1) begin
  220. if (high_count[channel] != duty[channel] * PHASE_CLKS)
  221. $fatal(1, "channel %0d duty=%0d high=%0d expected=%0d",
  222. channel, duty[channel], high_count[channel], duty[channel] * PHASE_CLKS);
  223. end
  224. for (channel = 0; channel < 8; channel = channel + 1) begin
  225. if (fan_active_count[channel] != duty[channel + 24] * PHASE_CLKS)
  226. $fatal(1, "active-low fan %0d duty=%0d active=%0d expected=%0d",
  227. channel, duty[channel + 24], fan_active_count[channel],
  228. duty[channel + 24] * PHASE_CLKS);
  229. end
  230. if (!idle_seen) $fatal(1, "32-channel scan never completed before deadline");
  231. if (boundary_count < 100) $fatal(1, "full 100-phase carrier was not observed");
  232. // Apply global safe image before next boundary.
  233. for (channel = 0; channel < 24; channel = channel + 1) duty[channel] = 0;
  234. for (channel = 24; channel < 32; channel = channel + 1) duty[channel] = 20;
  235. wait_for_boundary();
  236. wait_for_boundary();
  237. if (bitmap[23:0] !== 0)
  238. $fatal(1, "atomic safety bitmap mismatch: %h", bitmap);
  239. if (ptc_pins !== 24'd0)
  240. $fatal(1, "safe physical polarity mismatch ptc=%h balls=%h connector=%h",
  241. ptc_pins, fan_balls, fan_connector_pwm);
  242. if (miss) $fatal(1, "scan deadline miss after safety transition");
  243. $display("PASS: PWM reset 3 periods FAN20 plus 0/20/50/100 connector polarity");
  244. $display("pwm_bitmap_engine_tb PASS");
  245. $finish;
  246. end
  247. endmodule