pwm_bitmap_engine_tb.v 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 miss;
  11. wire [6:0] phase;
  12. wire idle;
  13. wire [23:0] ptc_pins = bitmap[23:0];
  14. wire [7:0] fan_pins = ~bitmap[31:24];
  15. reg [7:0] duty [0:31];
  16. reg [31:0] previous_bitmap;
  17. integer high_count [0:31];
  18. integer fan_active_count [0:7];
  19. integer last_edge_cycle [0:31];
  20. integer cycle_index;
  21. integer last_boundary_cycle;
  22. integer boundary_count;
  23. integer channel;
  24. integer sample;
  25. reg previous_valid;
  26. reg idle_seen;
  27. always #5 clk = ~clk;
  28. always @(posedge clk) duty_data <= duty[addr];
  29. pwm_bitmap_engine dut (
  30. .clk(clk), .rst_n(rst_n), .duty_rd_addr(addr), .duty_rd_data(duty_data),
  31. .logical_bitmap(bitmap), .phase_boundary(boundary), .deadline_miss(miss),
  32. .phase_value(phase), .scan_idle(idle)
  33. );
  34. // Outputs may change only on the common phase boundary. Therefore every
  35. // physical pulse is an integer multiple of the 500-clock phase and no
  36. // safety update can create a narrow asynchronous pulse.
  37. always @(negedge clk) begin
  38. if (!rst_n) begin
  39. previous_bitmap = bitmap;
  40. previous_valid = 0;
  41. cycle_index = 0;
  42. last_boundary_cycle = -1;
  43. boundary_count = 0;
  44. idle_seen = 0;
  45. for (channel = 0; channel < 32; channel = channel + 1)
  46. last_edge_cycle[channel] = -1;
  47. end else begin
  48. cycle_index = cycle_index + 1;
  49. if (idle) idle_seen = 1;
  50. if (boundary) begin
  51. if (last_boundary_cycle >= 0 &&
  52. cycle_index - last_boundary_cycle != 500)
  53. $fatal(1, "phase boundary spacing=%0d expected=500",
  54. cycle_index - last_boundary_cycle);
  55. last_boundary_cycle = cycle_index;
  56. boundary_count = boundary_count + 1;
  57. end
  58. if (previous_valid && bitmap !== previous_bitmap) begin
  59. if (!boundary)
  60. $fatal(1, "bitmap changed away from phase boundary");
  61. for (channel = 0; channel < 32; channel = channel + 1) begin
  62. if (bitmap[channel] !== previous_bitmap[channel]) begin
  63. if (last_edge_cycle[channel] >= 0 &&
  64. cycle_index - last_edge_cycle[channel] < 500)
  65. $fatal(1, "channel %0d pulse narrower than one phase", channel);
  66. last_edge_cycle[channel] = cycle_index;
  67. end
  68. end
  69. end
  70. previous_bitmap = bitmap;
  71. previous_valid = 1;
  72. end
  73. end
  74. task wait_for_boundary;
  75. begin
  76. @(negedge clk);
  77. while (!boundary) @(negedge clk);
  78. end
  79. endtask
  80. task wait_for_phase_zero;
  81. begin
  82. @(negedge clk);
  83. while (!(boundary && phase == 0)) @(negedge clk);
  84. end
  85. endtask
  86. initial begin
  87. duty_data = 0;
  88. previous_bitmap = 0;
  89. previous_valid = 0;
  90. cycle_index = 0;
  91. last_boundary_cycle = -1;
  92. boundary_count = 0;
  93. idle_seen = 0;
  94. for (channel = 0; channel < 32; channel = channel + 1) begin
  95. case (channel % 6)
  96. 0: duty[channel] = 0;
  97. 1: duty[channel] = 1;
  98. 2: duty[channel] = 2;
  99. 3: duty[channel] = 50;
  100. 4: duty[channel] = 99;
  101. default: duty[channel] = 100;
  102. endcase
  103. high_count[channel] = 0;
  104. last_edge_cycle[channel] = -1;
  105. end
  106. for (channel = 0; channel < 8; channel = channel + 1)
  107. fan_active_count[channel] = 0;
  108. repeat (4) @(negedge clk);
  109. if (bitmap !== 32'hff000000 || ptc_pins !== 24'd0 || fan_pins !== 8'd0)
  110. $fatal(1, "reset polarity mismatch bitmap=%h ptc=%h fan=%h",
  111. bitmap, ptc_pins, fan_pins);
  112. rst_n = 1;
  113. // Start at phase zero and count one exact 2 ms / 50,000-clock carrier.
  114. wait_for_phase_zero();
  115. for (sample = 0; sample < 50000; sample = sample + 1) begin
  116. if (miss) $fatal(1, "unexpected scan deadline miss");
  117. for (channel = 0; channel < 32; channel = channel + 1)
  118. if (bitmap[channel]) high_count[channel] = high_count[channel] + 1;
  119. for (channel = 0; channel < 8; channel = channel + 1) begin
  120. if (!fan_pins[channel])
  121. fan_active_count[channel] = fan_active_count[channel] + 1;
  122. if (fan_pins[channel] !== ~bitmap[channel + 24])
  123. $fatal(1, "fan polarity mismatch channel=%0d", channel);
  124. end
  125. if (ptc_pins !== bitmap[23:0])
  126. $fatal(1, "PTC polarity mismatch");
  127. if (sample != 49999) @(negedge clk);
  128. end
  129. for (channel = 0; channel < 32; channel = channel + 1) begin
  130. if (high_count[channel] != duty[channel] * 500)
  131. $fatal(1, "channel %0d duty=%0d high=%0d expected=%0d",
  132. channel, duty[channel], high_count[channel], duty[channel] * 500);
  133. end
  134. for (channel = 0; channel < 8; channel = channel + 1) begin
  135. if (fan_active_count[channel] != duty[channel + 24] * 500)
  136. $fatal(1, "active-low fan %0d duty=%0d active=%0d expected=%0d",
  137. channel, duty[channel + 24], fan_active_count[channel],
  138. duty[channel + 24] * 500);
  139. end
  140. if (!idle_seen) $fatal(1, "32-channel scan never completed before deadline");
  141. if (boundary_count < 100) $fatal(1, "full 100-phase carrier was not observed");
  142. // Apply a global safe image immediately before the phase-zero boundary.
  143. // The already-built phase zero remains intact; the following phase
  144. // commits all 32 new values together with no mid-phase output change.
  145. for (channel = 0; channel < 24; channel = channel + 1) duty[channel] = 0;
  146. for (channel = 24; channel < 32; channel = channel + 1) duty[channel] = 100;
  147. wait_for_boundary();
  148. wait_for_boundary();
  149. if (bitmap !== 32'hff000000)
  150. $fatal(1, "atomic safety bitmap mismatch: %h", bitmap);
  151. if (ptc_pins !== 24'd0 || fan_pins !== 8'd0)
  152. $fatal(1, "safe physical polarity mismatch ptc=%h fan=%h", ptc_pins, fan_pins);
  153. if (miss) $fatal(1, "scan deadline miss after safety transition");
  154. $display("PASS: L6 PWM 32ch 500Hz duties=0,1,2,50,99,100 atomic safety polarity");
  155. $finish;
  156. end
  157. endmodule