|
【例9.23】可变模加法/减法计数器
module updown_count(d,clk,clear,load,up_down,qd);
input[7:0] d;
input clk,clear,load;
input up_down;
output[7:0] qd;
reg[7:0] cnt;
assign qd = cnt;
always @(posedge clk)
begin
if (!clear) cnt = 8'h00; //同步清0,低电平有效
else if (load) cnt = d; //同步预置
else if (up_down) cnt = cnt + 1; //加法计数
else cnt = cnt - 1; //减法计数
end
endmodule
【例9.24】4位Johnson计数器(异步复位)
module johnson(clk,clr,out);
input clk,clr;
output[3:0] out;
reg[3:0] out;
always @(posedge clk or posedge clr)
begin
if (clr) out<= 4'h0;
else
begin out<= out<< 1;
out[0]<= ~out[3];
end
end
endmodule
【例3.2】4位计数器
module count4(out,reset,clk);
output[3:0] out;
input reset,clk;
reg[3:0] out;
always @(posedge clk)
begin
if (reset) out<=0; //同步复位
else out<=out+1; //计数
end
endmodule
【例5.2】同步置数、同步清零的计数器
module count(out,data,load,reset,clk);
output[7:0] out;
input[7:0] data;
input load,clk,reset;
reg[7:0] out;
always @(posedge clk) //clk上升沿触发
begin
if (!reset) out = 8'h00; //同步清0,低电平有效
else if (load) out = data; //同步预置
else out = out + 1; //计数
end
endmodule
【例5.11】模为60的BCD码加法计数器
module count60(qout,cout,data,load,cin,reset,clk);
output[7:0] qout;
output cout;
input[7:0] data;
input load,cin,clk,reset;
reg[7:0] qout;
always @(posedge clk) //clk上升沿时刻计数
begin
if (reset) qout<=0; //同步复位
else if(load) qout<=data; //同步置数
else if(cin)
begin
if(qout[3:0]==9) //低位是否为9,是则
begin
qout[3:0]<=0; //回0,并判断高位是否为
if (qout[7:4]==5) qout[7:4]<=0;
else
qout[7:4]<=qout[7:4]+1; //高位不为5,则加1
end
else //低位不为9,则加1
qout[3:0]<=qout[3:0]+1;
end
end
assign cout=((qout==8'h59)&cin)?1:0; //产生进位输出信号
endmodule |
|