| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- `timescale 1ns/1ps
- // Logical PWM scheduler. Project polarity and channel meaning belong to the
- // top-level adapter; this module only scans twelve logical duty values.
- module pwm_scheduler #(
- parameter integer CHANNEL_COUNT = 12,
- parameter integer PWM_BITS = 8,
- parameter integer PWM_DUTY_WIDTH = 7,
- parameter integer PWM_PHASE_COUNT = 100,
- parameter integer PWM_SLOT_CLKS = 1042
- ) (
- input wire clk,
- input wire rst_n,
- output wire [(CHANNEL_COUNT <= 1 ? 1 : $clog2(CHANNEL_COUNT))-1:0] target_rd_addr,
- input wire [PWM_BITS-1:0] target_rd_data,
- output reg [CHANNEL_COUNT-1:0] logical_pwm_on
- );
- localparam integer CHANNEL_BITS = (CHANNEL_COUNT <= 1) ? 1 : $clog2(CHANNEL_COUNT);
- localparam integer PHASE_BITS = (PWM_PHASE_COUNT <= 1) ? 1 : $clog2(PWM_PHASE_COUNT);
- localparam integer SLOT_BITS = (PWM_SLOT_CLKS <= 1) ? 1 : $clog2(PWM_SLOT_CLKS);
- reg [CHANNEL_BITS-1:0] current_channel;
- reg [PHASE_BITS-1:0] phase;
- reg [SLOT_BITS-1:0] slot_counter;
- wire [PWM_DUTY_WIDTH-1:0] selected_duty = target_rd_data[PWM_DUTY_WIDTH-1:0];
- wire logical_on = selected_duty > phase;
- assign target_rd_addr = current_channel;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- current_channel <= {CHANNEL_BITS{1'b0}};
- phase <= {PHASE_BITS{1'b0}};
- slot_counter <= {SLOT_BITS{1'b0}};
- logical_pwm_on <= {CHANNEL_COUNT{1'b0}};
- end else if (slot_counter < PWM_SLOT_CLKS - 1) begin
- slot_counter <= slot_counter + 1'b1;
- end else begin
- slot_counter <= {SLOT_BITS{1'b0}};
- // The synchronous RAM has already seen current_channel during
- // the preceding clock in this slot, so the returned data belongs
- // to the channel currently being completed.
- logical_pwm_on[current_channel] <= logical_on;
- if (current_channel == CHANNEL_COUNT - 1) begin
- current_channel <= {CHANNEL_BITS{1'b0}};
- if (phase == PWM_PHASE_COUNT - 1)
- phase <= {PHASE_BITS{1'b0}};
- else
- phase <= phase + 1'b1;
- end else begin
- current_channel <= current_channel + 1'b1;
- end
- end
- end
- endmodule
|