pwm_scheduler.v 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. `timescale 1ns/1ps
  2. // Logical PWM scheduler. Project polarity and channel meaning belong to the
  3. // top-level adapter; this module only scans twelve logical duty values.
  4. module pwm_scheduler #(
  5. parameter integer CHANNEL_COUNT = 12,
  6. parameter integer PWM_BITS = 8,
  7. parameter integer PWM_DUTY_WIDTH = 7,
  8. parameter integer PWM_PHASE_COUNT = 100,
  9. parameter integer PWM_SLOT_CLKS = 1042
  10. ) (
  11. input wire clk,
  12. input wire rst_n,
  13. output wire [(CHANNEL_COUNT <= 1 ? 1 : $clog2(CHANNEL_COUNT))-1:0] target_rd_addr,
  14. input wire [PWM_BITS-1:0] target_rd_data,
  15. output reg [CHANNEL_COUNT-1:0] logical_pwm_on
  16. );
  17. localparam integer CHANNEL_BITS = (CHANNEL_COUNT <= 1) ? 1 : $clog2(CHANNEL_COUNT);
  18. localparam integer PHASE_BITS = (PWM_PHASE_COUNT <= 1) ? 1 : $clog2(PWM_PHASE_COUNT);
  19. localparam integer SLOT_BITS = (PWM_SLOT_CLKS <= 1) ? 1 : $clog2(PWM_SLOT_CLKS);
  20. reg [CHANNEL_BITS-1:0] current_channel;
  21. reg [PHASE_BITS-1:0] phase;
  22. reg [SLOT_BITS-1:0] slot_counter;
  23. wire [PWM_DUTY_WIDTH-1:0] selected_duty = target_rd_data[PWM_DUTY_WIDTH-1:0];
  24. wire logical_on = selected_duty > phase;
  25. assign target_rd_addr = current_channel;
  26. always @(posedge clk or negedge rst_n) begin
  27. if (!rst_n) begin
  28. current_channel <= {CHANNEL_BITS{1'b0}};
  29. phase <= {PHASE_BITS{1'b0}};
  30. slot_counter <= {SLOT_BITS{1'b0}};
  31. logical_pwm_on <= {CHANNEL_COUNT{1'b0}};
  32. end else if (slot_counter < PWM_SLOT_CLKS - 1) begin
  33. slot_counter <= slot_counter + 1'b1;
  34. end else begin
  35. slot_counter <= {SLOT_BITS{1'b0}};
  36. // The synchronous RAM has already seen current_channel during
  37. // the preceding clock in this slot, so the returned data belongs
  38. // to the channel currently being completed.
  39. logical_pwm_on[current_channel] <= logical_on;
  40. if (current_channel == CHANNEL_COUNT - 1) begin
  41. current_channel <= {CHANNEL_BITS{1'b0}};
  42. if (phase == PWM_PHASE_COUNT - 1)
  43. phase <= {PHASE_BITS{1'b0}};
  44. else
  45. phase <= phase + 1'b1;
  46. end else begin
  47. current_channel <= current_channel + 1'b1;
  48. end
  49. end
  50. end
  51. endmodule