pwm_bitmap_engine_tb.v 11 KB

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