My module gets a parameter and I want to have a counter in my code to that value. say for example, parameter n=16 and I want to define reg [x:0] i so I can count to n. how can I find x ? there should be a real easy way ?
2 Answers
There are a couple of options, depending on what your tools support.
- Write your own
log2function - Use
$clog2
There is a lengthy discussion of this here: http://www.rhinocerus.net/forum/lang-verilog/646074-how-get-bit-width-value-verilog-compile-time.html
1 Comment
Morgan
You could then instantiate
reg [$clog(n)-1:0] i or integer i_w = $clog(n); reg [i_w-1:0] iyou must use generate statement here are some samples http://www.rhinocerus.net/forum/lang-verilog/431130-using-generate-statement-verilog.html