This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/Quartus/v3/jyh_4490_3_counter.v
2022-04-05 16:19:24 +08:00

57 lines
652 B
Verilog

module jyh_4490_3_counter(Q,clk,clr,load,in,en,upd,co);
input[3:0] in;
input en,clk,clr,load,upd;
output reg [3:0] Q;
output reg co;
always@(posedge clk,negedge clr)
begin
//异步清零
if(!clr)
Q<=0;
else if(en)
begin
//同步置数
if(load)
begin
Q<=in;
co<=1'b1;
end
//正反计数
else if(upd)
begin
if(Q>=4'd9)
begin
Q<=4'd0;
co<=1'b1;
end
else
begin
Q <= Q+1;
co<=0;
end
end
else
begin
if(Q<=4'd0)
begin
Q<=4'd9;
co<=1'b1;
end
else
begin
Q <= Q-1;
co<=0;
end
end
end
else
Q<=0;
end
endmodule