how to use LOG2 function in verilog 2001

Y

Yang Luo

Guest
I wrote a module as follow:
the complier tells me that the LOG2 function is not right. how should I do?
Do I need include some header file? like `include math.h or sth. else?

module simple_dual_ram #(
parameter SIZE = 10,
parameter LEN = 1024
)
(
input clka,
input ena,
input wea,
input [LOG2(LEN)-1:0] addra,
input [SIZE-1:0] dina,
input clkb,
input enb,
input [LOG2(LEN)-1:0] addrb,
output reg [SIZE-1:0] doutb
);

reg [SIZE-1:0] r_data[LEN-1:0];
reg [LOG2(LEN):0] r_cnt;//LOG2(LEN)+1 LOG2(1024)+1 -> 11; LOG2(1023)+1 -> 10

initial //cannot be synthesis
begin
doutb <= {(SIZE-1){1'b0}};
for(r_cnt=0; r_cnt<LEN; r_cnt=r_cnt+1)
r_data[r_cnt] <= {(SIZE-1){1'b0}};
end

always@(posedge clka)
if(wea&ena)
r_data[addra] <= dina;

always@(posedge clkb)
if(enb)
doutb <= r_data[addrb];

endmodule
 
Yang Luo wrote:
I wrote a module as follow:
the complier tells me that the LOG2 function is not right. how should I do?
Do I need include some header file? like `include math.h or sth. else?

module simple_dual_ram #(
parameter SIZE = 10,
parameter LEN = 1024
)
(
input clka,
input ena,
input wea,
input [LOG2(LEN)-1:0] addra,
input [SIZE-1:0] dina,
input clkb,
input enb,
input [LOG2(LEN)-1:0] addrb,
output reg [SIZE-1:0] doutb
);

reg [SIZE-1:0] r_data[LEN-1:0];
reg [LOG2(LEN):0] r_cnt;//LOG2(LEN)+1 LOG2(1024)+1 -> 11; LOG2(1023)+1 -> 10

initial //cannot be synthesis
begin
doutb <= {(SIZE-1){1'b0}};
for(r_cnt=0; r_cnt<LEN; r_cnt=r_cnt+1)
r_data[r_cnt] <= {(SIZE-1){1'b0}};
end

always@(posedge clka)
if(wea&ena)
r_data[addra] <= dina;

always@(posedge clkb)
if(enb)
doutb <= r_data[addrb];

endmodule

I'm not sure where you found "LOG2", but the only log base two in the
Verilog language itself is $clog2(), which is a system function. If
you found this in an example (including some in the Verilog LRM) it is
likely that it implies the existence of a function within the scope of
the current module that implements a log base 2. For most compilers
it is permitted to write:

reg [$clog2(LEN):0] r_cnt;

I have had problems with this code when using Xilinx tools, however
where I had to use a workaround function provided in one of their
answer records. As I recall, the original reason for the answer
record was that an older version of their tools implemented a natural
logarithm rather than log base 2. However even in newer tool versions
the above code would create errors, while using a function does not.

Example:

// Workaround to broken $clog2 function in ISE 13
// From Answer Record #44586
function integer clog2;
input integer value;
begin
value = value-1;
for (clog2=0; value>0; clog2=clog2+1)
value = value>>1;
end
endfunction

reg [clog2(LEN):0] r_cnt;

--
Gabor
 

Welcome to EDABoard.com

Sponsor

Back
Top