|
发表于 2017-5-24 10:33:39
|
显示全部楼层
回复 1# hxy110
module freq_divider #( parameter DIV_NUM = 9 )
( reset_n,
clk_in,
clk_o );
input reset_n;
input clk_in;
output clk_o;
function integer clogb2 ( input integer depth );
integer i, result;
begin
result = 1;
for ( i = 0; 2 ** i < depth; i = i + 1 )
result = i + 1;
clogb2 = result;
end
endfunction
reg [ clogb2 ( DIV_NUM ) - 1 : 0 ] cnt;
always @( negedge reset_n or posedge clk_in )
if ( ~ reset_n )
cnt <= 0;
else if ( cnt == DIV_NUM - 1 )
cnt <= 0;
else
cnt <= cnt + 1;
reg clk_buff;
always @( negedge reset_n or posedge clk_in )
if ( ~ reset_n )
clk_buff <= 0;
else if ( cnt == DIV_NUM - 1 )
clk_buff <= 0;
else if ( cnt < ( DIV_NUM - 1 ) / 2 )
clk_buff <= 0;
else
clk_buff <= 1;
generate
if (( DIV_NUM % 2 ) == 1 )
begin
reg tmp;
always @( negedge reset_n or negedge clk_in )
if ( ~ reset_n )
tmp <= 0;
else
tmp <= clk_buff;
assign clk_o = clk_buff | tmp;
end
else
assign clk_o = clk_buff;
endgenerate
endmodule |
|