pwm_bitmap_engine.v 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. `timescale 1ns/1ps
  2. // One comparator builds the next 32-bit phase image. Physical outputs switch
  3. // together only at a phase boundary.
  4. module pwm_bitmap_engine #(
  5. parameter integer CHANNEL_COUNT = 32,
  6. parameter integer PHASE_COUNT = 100,
  7. parameter integer PTC_PHASE_CLKS = 500,
  8. parameter integer FAN_PHASE_CLKS = 500,
  9. parameter integer ADDR_WIDTH = 5,
  10. parameter [31:0] RESET_LOGICAL_BITMAP = 32'hFF000000
  11. ) (
  12. input wire clk,
  13. input wire rst_n,
  14. output wire [ADDR_WIDTH-1:0] duty_rd_addr,
  15. input wire [7:0] duty_rd_data,
  16. output reg [CHANNEL_COUNT-1:0] logical_bitmap,
  17. output reg phase_boundary,
  18. output wire phase_boundary_advance,
  19. output reg deadline_miss,
  20. output wire [6:0] phase_value,
  21. output wire scan_idle
  22. );
  23. localparam integer PHASE_BITS = (PHASE_COUNT <= 1) ? 1 : $clog2(PHASE_COUNT);
  24. // V1 preserves one comparator and one scan because the independently
  25. // generated PTC and FAN carrier parameters are equal in this closure.
  26. // Unequal future contracts fail this backend explicitly and must select a
  27. // dual-carrier scheduler; they cannot silently inherit the PTC frequency.
  28. localparam integer SHARED_PHASE_CLKS = PTC_PHASE_CLKS;
  29. localparam integer SLOT_BITS = (SHARED_PHASE_CLKS <= 1) ? 1 : $clog2(SHARED_PHASE_CLKS);
  30. /* verilator lint_off WIDTH */
  31. localparam [PHASE_BITS-1:0] LAST_PHASE = PHASE_COUNT - 1;
  32. localparam [SLOT_BITS-1:0] LAST_SLOT = SHARED_PHASE_CLKS - 1;
  33. localparam [ADDR_WIDTH-1:0] LAST_CHANNEL = CHANNEL_COUNT - 1;
  34. /* verilator lint_on WIDTH */
  35. reg [PHASE_BITS-1:0] phase;
  36. reg [SLOT_BITS-1:0] phase_clk;
  37. reg [ADDR_WIDTH-1:0] scan_addr;
  38. reg [ADDR_WIDTH-1:0] capture_addr;
  39. reg scanning;
  40. reg capture_valid;
  41. reg [CHANNEL_COUNT-1:0] next_bitmap;
  42. initial begin
  43. if (PTC_PHASE_CLKS != FAN_PHASE_CLKS)
  44. $error("shared_equal_frequency_v1 requires equal independently generated PTC/FAN phase clocks");
  45. end
  46. wire [PHASE_BITS-1:0] build_phase =
  47. (phase == LAST_PHASE) ? {PHASE_BITS{1'b0}} : phase + 1'b1;
  48. wire [7:0] build_phase_8 = {{(8-PHASE_BITS){1'b0}}, build_phase};
  49. assign duty_rd_addr = scan_addr;
  50. assign phase_value = {{(7-PHASE_BITS){1'b0}}, phase};
  51. assign scan_idle = !scanning && !capture_valid;
  52. assign phase_boundary_advance = phase_clk == LAST_SLOT;
  53. always @(posedge clk or negedge rst_n) begin
  54. if (!rst_n) begin
  55. phase <= {PHASE_BITS{1'b0}};
  56. phase_clk <= {SLOT_BITS{1'b0}};
  57. scan_addr <= {ADDR_WIDTH{1'b0}};
  58. capture_addr <= {ADDR_WIDTH{1'b0}};
  59. scanning <= 1'b1;
  60. capture_valid <= 1'b0;
  61. next_bitmap <= {CHANNEL_COUNT{1'b0}};
  62. logical_bitmap <= RESET_LOGICAL_BITMAP[CHANNEL_COUNT-1:0];
  63. phase_boundary <= 1'b0;
  64. deadline_miss <= 1'b0;
  65. end else begin
  66. phase_boundary <= 1'b0;
  67. if (capture_valid) begin
  68. next_bitmap[capture_addr] <= (duty_rd_data > build_phase_8);
  69. end
  70. capture_valid <= scanning;
  71. if (scanning) begin
  72. capture_addr <= scan_addr;
  73. if (scan_addr == LAST_CHANNEL) begin
  74. scanning <= 1'b0;
  75. end else begin
  76. scan_addr <= scan_addr + 1'b1;
  77. end
  78. end
  79. if (phase_clk == LAST_SLOT) begin
  80. phase_clk <= {SLOT_BITS{1'b0}};
  81. phase <= (phase == LAST_PHASE) ? {PHASE_BITS{1'b0}} : phase + 1'b1;
  82. phase_boundary <= 1'b1;
  83. if (!scanning && !capture_valid) begin
  84. logical_bitmap <= next_bitmap;
  85. end else begin
  86. deadline_miss <= 1'b1;
  87. end
  88. next_bitmap <= {CHANNEL_COUNT{1'b0}};
  89. scan_addr <= {ADDR_WIDTH{1'b0}};
  90. scanning <= 1'b1;
  91. capture_valid <= 1'b0;
  92. end else begin
  93. phase_clk <= phase_clk + 1'b1;
  94. end
  95. end
  96. end
  97. `ifdef FORMAL
  98. reg formal_past_valid;
  99. function [6:0] formal_scan_rank;
  100. input [ADDR_WIDTH-1:0] address;
  101. input scan_active;
  102. input capture_pending;
  103. begin
  104. if (scan_active)
  105. formal_scan_rank = CHANNEL_COUNT[6:0] - address + 1'b1;
  106. else if (capture_pending)
  107. formal_scan_rank = 7'd1;
  108. else
  109. formal_scan_rank = 7'd0;
  110. end
  111. endfunction
  112. wire [6:0] formal_scan_remaining = formal_scan_rank(scan_addr, scanning, capture_valid);
  113. initial formal_past_valid = 0;
  114. always @(posedge clk) begin
  115. formal_past_valid <= 1;
  116. if (rst_n) begin
  117. // P11: the 32-channel scan always completes before the 500-clock deadline.
  118. assert(!deadline_miss);
  119. assert(phase <= LAST_PHASE);
  120. assert(phase_clk <= LAST_SLOT);
  121. assert(phase_boundary_advance == (phase_clk == LAST_SLOT));
  122. if (phase_clk < CHANNEL_COUNT + 1)
  123. assert(formal_scan_remaining == CHANNEL_COUNT + 1 - phase_clk);
  124. else
  125. assert(formal_scan_remaining == 0);
  126. if (formal_past_valid && $past(rst_n) && logical_bitmap != $past(logical_bitmap))
  127. assert(phase_boundary);
  128. if (!scanning && !capture_valid)
  129. assert(scan_idle);
  130. if (formal_past_valid && $past(rst_n))
  131. assert(phase_boundary == $past(phase_clk == LAST_SLOT));
  132. cover(scan_idle);
  133. cover(phase_boundary && !deadline_miss);
  134. end
  135. end
  136. `endif
  137. endmodule