Verilog log2 function

Source: Internet
Author: User


Use the system function $clog2()
Or write a function yourself
Note that this style

input [$clog 2 (len+1) -1-1:0] Addra,
It doesn't use the FUNCLOG2 function. A different style should be OK.
module simple_dual_ram #(
parameter SIZE  = 10,
  parameter LEN   = 1024
)
(
  input clka,
  input ena,
  input wea,
  input [$clog2(LEN+1)-1 -1:0] addra,//$clog2(8)=3; $clog2(9)=4
  input [SIZE-1:0] dina,
  input clkb, 
  input enb,
  input [$clog2(LEN+1)-1 -1:0] addrb,//
  output reg [SIZE-1:0] doutb
);

function integer funclog2; 
   input integer value; 
   begin 
     value = value-1; 
     for (funclog2=0; value>0; funclog2=funclog2+1) 
       value = value>>1; 
   end 
endfunction 

localparam TEST_LOG2 = funclog2(LEN);
reg [TEST_LOG2-1:0] r_test_log2;

reg [SIZE-1:0] r_data[LEN-1:0];
reg [$clog2(LEN+1)-1:0] r_cnt;//clog2(LEN+1)  clog2(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

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

[email protected](posedge clkb)
  if(enb)
    doutb <= r_data[addrb];


endmodule


Verilog log2 function


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.