| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- `timescale 1ns/1ps
- // One comparator builds the next 32-bit phase image. Physical outputs switch
- // together only at a phase boundary.
- module pwm_bitmap_engine #(
- parameter integer CHANNEL_COUNT = 32,
- parameter integer PHASE_COUNT = 100,
- parameter integer PTC_PHASE_CLKS = 500,
- parameter integer FAN_PHASE_CLKS = 500,
- parameter integer ADDR_WIDTH = 5,
- parameter [31:0] RESET_LOGICAL_BITMAP = 32'hFF000000
- ) (
- input wire clk,
- input wire rst_n,
- output wire [ADDR_WIDTH-1:0] duty_rd_addr,
- input wire [7:0] duty_rd_data,
- output reg [CHANNEL_COUNT-1:0] logical_bitmap,
- output reg phase_boundary,
- output wire phase_boundary_advance,
- output reg deadline_miss,
- output wire [6:0] phase_value,
- output wire scan_idle
- );
- localparam integer PHASE_BITS = (PHASE_COUNT <= 1) ? 1 : $clog2(PHASE_COUNT);
- // V1 preserves one comparator and one scan because the independently
- // generated PTC and FAN carrier parameters are equal in this closure.
- // Unequal future contracts fail this backend explicitly and must select a
- // dual-carrier scheduler; they cannot silently inherit the PTC frequency.
- localparam integer SHARED_PHASE_CLKS = PTC_PHASE_CLKS;
- localparam integer SLOT_BITS = (SHARED_PHASE_CLKS <= 1) ? 1 : $clog2(SHARED_PHASE_CLKS);
- /* verilator lint_off WIDTH */
- localparam [PHASE_BITS-1:0] LAST_PHASE = PHASE_COUNT - 1;
- localparam [SLOT_BITS-1:0] LAST_SLOT = SHARED_PHASE_CLKS - 1;
- localparam [ADDR_WIDTH-1:0] LAST_CHANNEL = CHANNEL_COUNT - 1;
- /* verilator lint_on WIDTH */
- reg [PHASE_BITS-1:0] phase;
- reg [SLOT_BITS-1:0] phase_clk;
- reg [ADDR_WIDTH-1:0] scan_addr;
- reg [ADDR_WIDTH-1:0] capture_addr;
- reg scanning;
- reg capture_valid;
- reg [CHANNEL_COUNT-1:0] next_bitmap;
- initial begin
- if (PTC_PHASE_CLKS != FAN_PHASE_CLKS)
- $error("shared_equal_frequency_v1 requires equal independently generated PTC/FAN phase clocks");
- end
- wire [PHASE_BITS-1:0] build_phase =
- (phase == LAST_PHASE) ? {PHASE_BITS{1'b0}} : phase + 1'b1;
- wire [7:0] build_phase_8 = {{(8-PHASE_BITS){1'b0}}, build_phase};
- assign duty_rd_addr = scan_addr;
- assign phase_value = {{(7-PHASE_BITS){1'b0}}, phase};
- assign scan_idle = !scanning && !capture_valid;
- assign phase_boundary_advance = phase_clk == LAST_SLOT;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- phase <= {PHASE_BITS{1'b0}};
- phase_clk <= {SLOT_BITS{1'b0}};
- scan_addr <= {ADDR_WIDTH{1'b0}};
- capture_addr <= {ADDR_WIDTH{1'b0}};
- scanning <= 1'b1;
- capture_valid <= 1'b0;
- next_bitmap <= {CHANNEL_COUNT{1'b0}};
- logical_bitmap <= RESET_LOGICAL_BITMAP[CHANNEL_COUNT-1:0];
- phase_boundary <= 1'b0;
- deadline_miss <= 1'b0;
- end else begin
- phase_boundary <= 1'b0;
- if (capture_valid) begin
- next_bitmap[capture_addr] <= (duty_rd_data > build_phase_8);
- end
- capture_valid <= scanning;
- if (scanning) begin
- capture_addr <= scan_addr;
- if (scan_addr == LAST_CHANNEL) begin
- scanning <= 1'b0;
- end else begin
- scan_addr <= scan_addr + 1'b1;
- end
- end
- if (phase_clk == LAST_SLOT) begin
- phase_clk <= {SLOT_BITS{1'b0}};
- phase <= (phase == LAST_PHASE) ? {PHASE_BITS{1'b0}} : phase + 1'b1;
- phase_boundary <= 1'b1;
- if (!scanning && !capture_valid) begin
- logical_bitmap <= next_bitmap;
- end else begin
- deadline_miss <= 1'b1;
- end
- next_bitmap <= {CHANNEL_COUNT{1'b0}};
- scan_addr <= {ADDR_WIDTH{1'b0}};
- scanning <= 1'b1;
- capture_valid <= 1'b0;
- end else begin
- phase_clk <= phase_clk + 1'b1;
- end
- end
- end
- `ifdef FORMAL
- reg formal_past_valid;
- function [6:0] formal_scan_rank;
- input [ADDR_WIDTH-1:0] address;
- input scan_active;
- input capture_pending;
- begin
- if (scan_active)
- formal_scan_rank = CHANNEL_COUNT[6:0] - address + 1'b1;
- else if (capture_pending)
- formal_scan_rank = 7'd1;
- else
- formal_scan_rank = 7'd0;
- end
- endfunction
- wire [6:0] formal_scan_remaining = formal_scan_rank(scan_addr, scanning, capture_valid);
- initial formal_past_valid = 0;
- always @(posedge clk) begin
- formal_past_valid <= 1;
- if (rst_n) begin
- // P11: the 32-channel scan always completes before the 500-clock deadline.
- assert(!deadline_miss);
- assert(phase <= LAST_PHASE);
- assert(phase_clk <= LAST_SLOT);
- assert(phase_boundary_advance == (phase_clk == LAST_SLOT));
- if (phase_clk < CHANNEL_COUNT + 1)
- assert(formal_scan_remaining == CHANNEL_COUNT + 1 - phase_clk);
- else
- assert(formal_scan_remaining == 0);
- if (formal_past_valid && $past(rst_n) && logical_bitmap != $past(logical_bitmap))
- assert(phase_boundary);
- if (!scanning && !capture_valid)
- assert(scan_idle);
- if (formal_past_valid && $past(rst_n))
- assert(phase_boundary == $past(phase_clk == LAST_SLOT));
- cover(scan_idle);
- cover(phase_boundary && !deadline_miss);
- end
- end
- `endif
- endmodule
|