【 1. 源代码 】
- 异步清零、同步使能、同步翻转计数
//【copyright:Promethus】
//【Funtion】: counter
//【Ports】: clk:时钟输入[posedge]
// clr:异步清零[L];
// en:同步使能计数[H];
// ctrl:同步翻转计数;
// out:当前计数值;
// co:进位输出值[H];
//【Instruction】:更改value为计数值,以及输出值out的位数,使得计数值不超过out所允许的最大值。
//【Data】:2020/12/18。
//【Version】:1.0
module count(clk,en,clr,ctrl,out,co);
parameter value=8; //更改value为计数值。
input wire clk,clr,ctrl,en;
output reg co;
output reg [3:0] out; //更改输出值out的位数限制。
always@(posedge clk,negedge clr)
begin
if(!clr)
begin co<=0;out<=0; end
else
if(en)
begin
if(ctrl) //加计数
begin
if(out==value) begin out<=0;co<=1; end
else begin out<=out+4'b1;co<=0;end
end
else //减计数
begin
if(out==0)begin out<=value;co<=1;end
else begin out=out-4'b1;co<=0;end
end
end
end
endmodule