一、什么是模为60的BCD加法器?
1.模为60的BCD加法器本质上是一个专门用于处理六十进制(Sexagesimal)计数的数字逻辑电路。
2.它从 0 计数到 59。当输入再加 1 时,结果不是 60,而是归零,并产生一个进位信号给高位。
3。采用8位的res作为结果。高四位表示十位,范围从0到5;低四位表示个位,范围从0到9.
二、代码
BCD_60counter.v代码如下:
c
`timescale 1ns / 1ps
module BCD_60counter(
input clk ,
input rst ,
input load ,
input [7:0] load_data ,
input cin ,
output reg[7:0]res ,
output cout
);
always @(posedge clk) begin
if(!rst) res<=0;
else if(load) res <= load_data;
else
if(cin) begin
if(res[3:0] == 4'h9)
begin
res[3:0] <= 4'h0;
if(res[7:4] == 4'h5) res[7:4] <= 4'h0; else res[7:4] <= res[7:4] +1'b1;
end
else
res[3:0] <= res[3:0] + 1'b1;
end
end
assign cout = (res ==8'h59)&&(cin ==1'b1 ) ? 1'b1:1'b0;
endmodule
BCD_60counter_tb.v仿真文件代码如下:
c
`timescale 1ns / 1ps
module BCD_60counter_tb;
reg clk ;
reg rst ;
reg load ;
reg [7:0] load_data ;
reg cin ;
wire [7:0] res;
wire cout;
BCD_60counter u_counter(
. clk ( clk) ,
. rst ( rst) ,
. load ( load) ,
. load_data ( load_data) ,
. cin ( cin) ,
.res (res),
.cout (cout)
);
parameter cycle = 20;
always #(cycle/2) clk=~clk;
initial begin
clk =0;rst=0;cin =0; load =0; load_data=0;
#cycle rst =1'b1; cin =1'b1;
#(cycle*100 ) load=1;load_data=8'h58;
#cycle load =0; load_data=0;
#(cycle*50) $finish;
end
endmodule
三、仿真截图如下:
1.从tb文件可知:时钟周期是20ns
2.看懂initial块的的执行动作:
0ns,赋值为0
20ns,rst置为1,取消复位;cin置为1,使能计数器计数累加
再过2000ns,加载数据
下一个周期,取消加载,不取消的话会一直加载,res一直是58
再过50个周期,停止运行。
3.判断仿真结果正确的两个点
第一个是59之后是否变为00

第二个是加载58是否成功

从图中看到,都是成功的。
四、原理图:
