how to do log2(n) in verilog?

G

goannae

Guest
Hi,

I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
verilog as:
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.

Thank you in advance,

Goanna
 
goannae wrote:
Hi,

I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
verilog as:
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.

Thank you in advance,

Goanna
I think the following will work:

parameter InputLength = 8;
parameter CounterSize = 1<<InputLength - 1;

Correct me if I'm wrong or I misunderstood your question.
 
On Wed, 5 Jul 2006 23:07:49 +1000, "goannae" <goannae@myuniv.edu.au>
wrote:

Hi,

I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
verilog as:
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.
They are if you define a log2 like this:

function integer log2;
input [31:0] value;
for (log2=0; value>0; log2=log2+1)
value = value>>1;
endfunction

You can also parameterize the size of the input value.
 
Thank you for your help. I did not put my question correctly. Please excuse
my bad English.
It would be clearer if I had asked how many bits are required to represent
an integer (eg, an integer 13 would require 4 bits).
The answer would be at least log2(n) bits where n is the integer. Then the
question was how to realise log2(n) in verilog.

Thanks again,

Goanna

<daniel.larkin@gmail.com> wrote in message
news:1152111158.311212.68280@b68g2000cwa.googlegroups.com...
goannae wrote:
Hi,

I would like to parameterize a counter to count an n bit binary input.
Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
verilog as:
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.

Thank you in advance,

Goanna

I think the following will work:

parameter InputLength = 8;
parameter CounterSize = 1<<InputLength - 1;

Correct me if I'm wrong or I misunderstood your question.
 
Thank you very much for your help, mate. It works fine. Much appreciated.

Goanna

"mk" <kal*@dspia.*comdelete> wrote in message
news:3m0oa2dlmgj6ihvpnb3v4ah28jfad8ng1m@4ax.com...
On Wed, 5 Jul 2006 23:07:49 +1000, "goannae" <goannae@myuniv.edu.au
wrote:

Hi,

I would like to parameterize a counter to count an n bit binary input.
Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog. I am wondering if log2(n) can be done in
verilog as:
parameter InputLength = 8;
parameter CounterSize = log2(InputLength);
are not acceptable.

They are if you define a log2 like this:

function integer log2;
input [31:0] value;
for (log2=0; value>0; log2=log2+1)
value = value>>1;
endfunction

You can also parameterize the size of the input value.
 
goannae wrote:
Hi,

I would like to parameterize a counter to count an n bit binary input. Thus
the size of the count is at lease log2(n) bits. I have difficulty to
calculate log2(n) in verilog.
In Verilog-2001, you can write your own functions that can be used in
constant expressions, as long as they abide by the restrictions imposed
for such "constant functions." Then you have to write the function
correctly.
This is clearly non-trivial, since the version originally published as
an
example in the Verilog-2001 standard was wrong.

In Verilog-2005, there is a built-in $clog2 system function that
computes
what you want and can be used in constant expressions. However, I
doubt
that anyone has implemented it yet.
 

Welcome to EDABoard.com

Sponsor

Back
Top