Question on clever use of parameters

  • Thread starter news.optonline.net
  • Start date
N

news.optonline.net

Guest
Given the code below for a parameterized PWM generator, how can I eliminate
having the user specify the counter width. In other words, what clever math
could I use to automatically compute the required counter width
(PwmCountWidth) and make it a localparam?

Thanks...

module VRefPWM #(
parameter ClockFreq = 8000000, // Hz
PwmFreq = 62500, // Hz
PwmCountWidth = 7 //Output width (needs to be wide enough to hold
MaxCount-1(localparam)
)
(
input CLOCK,
input RESET,
input [PwmCountWidth-1:0] PWM_SET,
output reg [PwmCountWidth-1:0] PWM_COUNT,
output ROLL_OVER,
output VREF_PWM
);

localparam MaxCount = ClockFreq/PwmFreq;

// Pwm Counter
always @(posedge CLOCK, posedge RESET)
if (RESET)
PWM_COUNT <= {PwmCountWidth{1'b0}};
else begin
PWM_COUNT <= PWM_COUNT + 1;
if (PWM_COUNT == MaxCount-1)
PWM_COUNT <= {PwmCountWidth{1'b0}};
end

// Generate cycle start for current controller
assign ROLL_OVER = (PWM_COUNT == MaxCount-1);

// Generate Vref PWM
assign VREF_PWM = (PWM_COUNT <= PWM_SET);

endmodule
 
You already define MaxCount as localparam. All you need to
do is to find out lg(MaxCount) + 1 where lg is log to the base 2.
This can be done by finding out what is the first most significant
bit of MaxCount that contains 1. Can you solve it now?

--
SystemVerilog, DPI, Verilog PLI and all other good stuffs.
Project VeriPage: http://www.project-veripage.co­m
Get information on new articles:
<URL: http://www.project-veripage.co­­m/list/?p=subscribe&id=1>
 

Welcome to EDABoard.com

Sponsor

Back
Top