概述
Verilog刷题HDLBits——Count clock
- 题目描述
- 代码
- 结果
题目描述
Create a set of counters suitable for use as a 12-hour clock (with am/pm indicator). Your counters are clocked by a fast-running clk, with a pulse on ena whenever your clock should increment (i.e., once per second).
reset resets the clock to 12:00 AM. pm is 0 for AM and 1 for PM. hh, mm, and ss are two BCD (Binary-Coded Decimal) digits each for hours (01-12), minutes (00-59), and seconds (00-59). Reset has higher priority than enable, and can occur even when not enabled.
The following timing diagram shows the rollover behaviour from 11:59:59 AM to 12:00:00 PM and the synchronous reset and enable behaviour.
代码
module top_module(
input clk,
input reset,
input ena,
output pm,
output [7:0] hh,
output [7:0] mm,
output [7:0] ss);
wire ena1,ena2,ena3,ena4,ena5;
assign ena1 = ena&&ss[3:0]==4'd9;
assign ena2 = ena1&&ss[7:4]==4'd5;
assign ena3 = ena2&&mm[3:0]==4'd9;
assign ena4 = ena3&&mm[7:4]==4'd5;
assign ena5 = ena4&&hh=={4'b0001,4'b0001};
count60 t_ss1(clk,reset,ena,4'b1001,ss[3:0]);
count60 t_ss2(clk,reset,ena1,4'b0101,ss[7:4]);
count60 t_mm1(clk,reset,ena2,4'b1001,mm[3:0]);
count60 t_mm2(clk,reset,ena3,4'b0101,mm[7:4]);
count12 t_hh(clk,reset,ena4,hh);
count2 t_pm(clk,reset,ena5,pm);
endmodule
module count60 (input clk,reset,ena,input[3:0] cc,output[3:0] tt);
always@(posedge clk)
begin
if(reset||(ena&&tt==cc))
tt<=0;
else
if(ena)
tt<=tt+1'b1;
end
endmodule
module count12 (input clk,reset,ena,output[7:0] tt);
always@(posedge clk)
begin
if(reset) // 同步置12
tt<={4'b0001,4'b0010};
else
begin
if(ena&&tt=={4'b0001,4'b0010}) // 12时变01
tt<={4'b0000,4'b0001};
else
begin
if(ena)
begin
if(tt[3:0]==4'b1001)
begin
tt[3:0]<=4'b0000;
tt[7:4]<=tt[7:4]+1'b1;
end
else
tt[3:0]<=tt[3:0]+1'b1;
end
end
end
end
endmodule
module count2 (input clk,reset,ena,output pm);
always@(posedge clk)
begin
if(reset||(ena&&pm==1))
pm<=0;
else
if(ena)
pm<=pm+1;
end
endmodule
结果
最后
以上就是危机眼神为你收集整理的Verilog刷题HDLBits——Count clock题目描述代码结果的全部内容,希望文章能够帮你解决Verilog刷题HDLBits——Count clock题目描述代码结果所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复