2.设计16位二进制计数器,带异步复位、同步使能、同步装载、同步清零、同步置位
法一
author : Mr.Mao
e-mail : [email protected]
module CNT_16B2(rst_a, en_s, load_s, clr_s, clk, q, cnt, data) ;
input rst_a, en_s, load_s, clr_s, clk ;
input [0:15] data ;
output reg [0:15] cnt ; //2^16 = 65536
output reg q ;
always@(posedge clk or negedge rst_a)
begin
if(!rst_a) cnt <= 0 ;
else if(en_s)
begin
if(load_s) cnt <= data ;
else if(clr_s) cnt <= 0 ;
else if(cnt < 65536) cnt <= cnt + 1 ;
else cnt <= cnt ;
end
end
always@(posedge clk)
begin
if(cnt == 65535) q <= 1'b1 ;
else q <= 1'b0 ;
end
endmodule
法二
author : Mr.Mao
e-mail : [email protected]
module cntx
(
input clk,
input reset,
input [5:0] d,
input en,
input load,
input sclr,
input sset,
output reg[15:0] q,
output cout
);
always @(posedge clk,posedge reset)
if(reset)
q <= 0;
else if(sclr)
q <= 0;
else if(sset)
q <= -1;
else if(load)
q <= d;
else if(en)
q <= q + 1'b1;
assign cout = (q==60-1);
endmodule