赞
踩
题目来源于HDLBits
大致意思是设计一个12小时的时钟,分为时 分 秒 ,每个单位又由两个bcd码组成,复位为12:00:00;且有pm信号输出,pm为1时为下午。
代码如下:
module top_module(
input clk,
input reset,
input ena,
output pm,
output [7:0] hh,
output [7:0] mm,
output [7:0] ss);
wire [4:0] enable;
wire pm_ding;
assign enable[0] = ss[3:0]== 4'd9&&ena;
assign enable[1] = (ss[7:4]==4'd5) && enable[0];
assign enable[2] = (mm[3:0]==4'd9)&&enable[1];
assign enable[3] = (mm[7:4]==4'd5)&&enable[2];
assign enable[4] =(hh[3:0]==4'd9)&&enable[3];
assign pm_ding = (hh[7:4]==4'd1)&&(hh[3:0]==4'd1)&&enable[3];
assign pm = pm_temp;
counter10 inst1(clk,reset,ena,ss[3:0]);
counter6 inst2(clk,reset,enable[0],ss[7:4]);
counter10 inst3(clk,reset,enable[1],mm[3:0]);
counter6 inster4(clk,reset,enable[2],mm[7:4]);
counterh10 inster5(clk,reset,enable[3],hh[7:4],hh[3:0]);
counterh6 inster6(clk,reset,enable[3],enable[4],hh[3:0],hh[7:4]);
reg pm_temp;
always@(posedge clk)begin
if(reset)
pm_temp <= 1'b0;
else if(pm_ding)
pm_temp <= ~pm_temp;
end
endmodule
module counter10(
input clk,
input reset,
input ena,
output reg [3:0] q);
always@(posedge clk)begin
if(reset)
q <= 4'd0;
else if(ena)begin
if(q == 4'd9)
q <= 4'd0;
else
q <= q + 1'b1;
end
end
endmodule
module counter6(
input clk,
input reset,
input ena,
output reg [3:0] q);
always@(posedge clk)begin
if(reset)
q <= 4'd0;
else if(ena)begin
if(q == 4'd5)
q <= 4'd0;
else
q <= q + 1'b1;
end
end
endmodule
module counterh10(
input clk,
input reset,
input ena,
input [3:0] h,
output reg [3:0] q);
always@(posedge clk)begin
if(reset)
q <= 4'd2;
else if(ena)begin
if(q == 4'd9)
q <= 4'd0;
else if(h==4'd1&&q==4'd2)
q <= 4'd1;
else
q <= q + 1'b1;
end
end
endmodule
module counterh6(
input clk,
input reset,
input ena,
input ena1,
input [3:0] h,
output reg [3:0] q);
always@(posedge clk)begin
if(reset)
q <= 4'd1;
else if(ena)begin
if(q==4'd1&&h==4'd2)
q <= 4'd0;
else if(ena1)
q <= q + 1'b1;
end
end
endmodule
我是首先写了几个小模块分别为10位和6位计数器控制分和秒,另外因为小时这两位复位和置位比较特殊,所以还写了另两个模块counterh10,counterh6。
其实主要设计为使能信号的控制,另外要特别注意小时这两位的使能信号和置位。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。