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

66 lines
765 B
Coq
Raw Permalink Normal View History

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