首页 > 其他 > 详细

Verilog log2 函数

时间:2016-04-21 23:20:29      阅读:543      评论:0      收藏:0      [点我收藏+]
使用系统函数 $clog2()
或者自己写一个function
注意,这样的style
input [$clog2(LEN+1)-1 -1:0] addra,
它用不了funclog2函数。换一种style应该就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

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

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


endmodule
技术分享

Verilog log2 函数

原文:http://www.cnblogs.com/luoyanghero/p/5419142.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!