Constant function (Verilog-2001)

P

pico

Guest
Verilog-2001 adds constant functions
I try to call a constant expression for "Vector width declarations"
My code is below , but appear error message under ncverilog 05.00
Please help me to find where I make wrong
Thans~~~~


module c_t;

parameter size=8;

reg clk=0;
reg [clogb2(size)-1:0] count=0;
//reg [size-1:0] count=0;

initial begin
forever #1 clk=~clk;
end

always@(posedge clk)
count<=count+1;

function integer clogb2 ( input integer depth);
begin
for(clogb2=0; depth>0; clogb2=clogb2+1)
depth=depth>>>1;
end
endfunction

endmodule
 
pico wrote:
Verilog-2001 adds constant functions
I try to call a constant expression for "Vector width declarations"
My code is below , but appear error message under ncverilog 05.00.
Constant functions are the one Verilog-2001 extension
for which NC-Verilog still has no support. They would
require a significant effort to implement, and there
are other things that have had higher priority. It
does appear that they may get implemented within the
next year.

The only thing anyone seems to want constant functions
for is to compute log-base-2, and that can be done
reasonably with a parameterized macro. In fact, the
macro approach has advantages, since you only have to
define the macro once for the entire compilation. A
function would have to be declared in every module that
wants to use it. It is expected that Verilog-2005 will
make this even easier by adding a $clog2 built-in system
function that can be used in constant expressions.

A built-in $clog2 function would also avoid the problem
of errors in user-written log-2 functions. The function
that you provided in your example is actually incorrect,
and does not compute the correct value for exact powers
of 2. The example in the Verilog-2001 LRM had the same
kind of bug. People seem to have trouble writing this
function correctly.

I am appending a log-2 macro definition that should work
in any tool that supports Verilog-1995. Once you have
defined it, you can use `clog2(size) instead of your
clog2(size). The news posting software may mangle the
text of the macro by wrapping the lines. If so, you
should be able to re-assemble them by realizing that
all but the last line of the macro have a backslash
at the end of them.

`define clogb2(n) ((n) <= (1<<0) ? 0 : (n) <= (1<<1) ? 1 :\
(n) <= (1<<2) ? 2 : (n) <= (1<<3) ? 3 :\
(n) <= (1<<4) ? 4 : (n) <= (1<<5) ? 5 :\
(n) <= (1<<6) ? 6 : (n) <= (1<<7) ? 7 :\
(n) <= (1<<8) ? 8 : (n) <= (1<<9) ? 9 :\
(n) <= (1<<10) ? 10 : (n) <= (1<<11) ? 11 :\
(n) <= (1<<12) ? 12 : (n) <= (1<<13) ? 13 :\
(n) <= (1<<14) ? 14 : (n) <= (1<<15) ? 15 :\
(n) <= (1<<16) ? 16 : (n) <= (1<<17) ? 17 :\
(n) <= (1<<18) ? 18 : (n) <= (1<<19) ? 19 :\
(n) <= (1<<20) ? 20 : (n) <= (1<<21) ? 21 :\
(n) <= (1<<22) ? 22 : (n) <= (1<<23) ? 23 :\
(n) <= (1<<24) ? 24 : (n) <= (1<<25) ? 25 :\
(n) <= (1<<26) ? 26 : (n) <= (1<<27) ? 27 :\
(n) <= (1<<28) ? 28 : (n) <= (1<<29) ? 29 :\
(n) <= (1<<30) ? 30 : (n) <= (1<<31) ? 31 : 32)
 

Welcome to EDABoard.com

Sponsor

Back
Top