pwm_bitmap_engine_tb.v 11 KB

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