三倍分频
方式一,行为描述:
module threediv(rst,clk,clkout,clkout1,clkout2);
input rst,clk;
output clkout,clkout1,clkout2;
reg clkout;
reg clk1o;
reg clkout1;
reg clk2o;
reg clkout2;
always@(posedge clk)
if(!rst)
clkout2<=0;
else
clkout2<=(~clk1o)^clkout2;
always@(posedge clk)
if(!rst)
clk1o<=0;
else
clk1o<=clkout2;
always@(negedge clk)
if(!rst)
clkout1<=0;
else
clkout1<=(~clk2o)^clkout1;
always@(negedge clk)
if(!rst)
clk2o<=0;
else
clk2o<=clkout1;
always@(clkout1 or clkout2 or rst)
if(!rst)
clkout=0;
else
clkout=clkout2|clkout1;
endmodule
根据以上的逻辑,我用寄存器级描述了一下,更接近原理图的方式,可直接绘图,代码如下:
module dffdiv3(rst,clk,clko);
input rst;
input clk;
output clko;
dffp dp1(.rst(rst),.clk(clk),.din(feedp),.dout(wp1));
dffp dp2(.rst(rst),.clk(clk),.din(wp1),.dout(wp2));
assign feedp=~wp2^wp1;
dffn dn1(.rst(rst),.clk(clk),.din(feedn),.dout(wn1));
dffn dn2(.rst(rst),.clk(clk),.din(wn1),.dout(wn2));
assign feedn=~wn2^wn1;
assign clko="feedp|feedn";
endmodule
module dffp(rst,clk,din,dout);
input rst;
input clk;
input din;
output dout;
reg dout;
always @(negedge rst or posedge clk)
if(!rst)
dout<=0;
else
dout<=din;
endmodule
module dffn(rst,clk,din,dout);
input rst;
input clk;
input din;
output dout;
reg dout;
always @(negedge rst or negedge clk)
if(!rst)
dout<=0;
else
dout<=din;
endmodule
测试仿真代码:
`timescale 1ns/1ns
module dffdiv3_tp();
reg rst,clk;
wire clko;
dffdiv3 Dffdiv3(.rst(rst),.clk(clk),.clko(clko));
initial
begin
rst=1;
clk=0;
#20;
rst=0;
#20;
rst=1;
end
always #10 clk=~clk;
endmodule
//以上的实现没有时钟双沿的问题,因为时钟的正负沿使用针对不同的数据流
文章评论(0条评论)
登录后参与讨论