源程序:
module clock(clk,rst,h1,h2,m1,m2,s1,s2);
input clk,rst;output h1,h2,m1,m2,s1,s2; // 1表示十位,2表示个位
reg [3:0]h1,h2,m1,m2,s1,s2;
always@(posedge clk)
begin
if(!rst) s2<=0;
else if(s2==9)
s2<=0;
else
s2<=s2+1;
end
always@(posedge clk)
begin
if(!rst) s1<=0;
else if(s2==9)
begin
if(s1==5)
s1<=0;
else
s1<=s1+1;
end
end
always@(posedge clk)
begin
if(!rst) m2<=0;
else if(s1==5&&s2==9)
begin
if(m2==9)
m2<=0;
else
m2<=m2+1;
end
end
always@(posedge clk)
begin
if(!rst) m1<=0;
else if(s1==5&&s2==9)
begin
if(m2==9)
begin
if(m1==5)
m1<=0;
else
m1<=m1+1;
end
end
end
always@(posedge clk)
begin
if(!rst) h2<=0;
else if((s1==5&&s2==9)&&(m1==5&&m2==9))
begin
if(h1==2&&h2==3)
h2<=0;
else
if(h2==9)
h2<=0;
else
h2<=h2+1;
end
end
always@(posedge clk)
begin
if(!rst)
h1<= 0;
else if((s1==5&&s2==9)&&(m1==5&&m2==9))
begin
if((h1==2)&&(h2==3))
h1<= 0;
else if(h2==9)
h1<=h1+1;
end
end
endmodule
测试:
module clock_tb();
reg clk,rst;
wire[3:0] h1,h2,m1,m2,s1,s2;
clock u1(clk,rst,h1,h2,m1,m2,s1,s2) ;
always #5 clk=~clk;
initial
begin
clk=0;
rst=0;
#10 rst=1;
end
endmodule
仿真: