pwm_bitmap_engine_tb.v 9.0 KB

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