3

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 2

3

There are a couple of options, depending on what your tools support.

  • Write your own log2 function
  • 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

Sign up to request clarification or add additional context in comments.

1 Comment

You could then instantiate reg [$clog(n)-1:0] i or integer i_w = $clog(n); reg [i_w-1:0] i
2

you must use generate statement here are some samples http://www.rhinocerus.net/forum/lang-verilog/431130-using-generate-statement-verilog.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.