HDLBits刷题合集—12 Counters
HDLBits-99 Count15
Problem Statement
构建一个4位二进制计数器,其计数范围从0到15(包括0和15),周期为16。置位输入是同步的,应该将计数器重置为0。
代码如下:
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:0] q);
always @(posedge clk) begin
if (reset)
q <= 4'b0000;
else
q <= q + 1'b1;
end
endmodule
HDLBits-100 Count10
Problem Statement
构建一个十进制计数器,其计数范围从0到9(包括0和9),周期为10。置位输入是同步的,应该将计数器重置为0。
代码如下:
module top_module (
input clk,
input reset, // Synchronous active-high reset
output [3:0] q);
always @(posedge clk)
begin
if (reset)
q <= 4'b0000;
else if (q < 4'b1001)
q <= q + 1'b1;
else
q <= 4'b0000;
end
endmodule
HDLBits-101 Count1to10
Problem Statement
构建一个十进制计数器,其计数范围从0到9(包括0和9),周期为10。置位输入是同步的,应该将计数器重置为1。
代码如下:
module top_module (
input clk,
input reset,
output [3:0] q);
always @(posedge clk)
begin
if (reset)
q <= 4'b0001;
else if (q <= 4'b1001)
q <= q + 1'b1;
else
q <= 4'b0001;
end
endmodule
HDLBits-102 Countslow
Problem Statement
构建一个十进制计数器,其计数范围从0到9(包括0和9),周期为10。置位输入是同步的,应该将计数器重置为0。
我们希望能够暂停计数器,而不是总是每个时钟周期递增,因此slowena输入指出计数器应该何时递增。
代码如下:
module top_module (
input clk,
input slowena,
input reset,
output [3:0] q);
always @(posedge clk) begin
if (reset)
q <= 4'b0000;
else if (slowena == 1)
begin
if (q <= 4'b1000)
q <= q + 1'b1;
else
q <= 4'b0000;
end
end
endmodule
HDLBits-102 Countslow
Problem Statement
设计一个从1计数到12的计数器,输入和输出如下:
Reset:同步高电平复位,强制计数器为1
Enable:高电平有效,以使计数器计数
Clk:时钟输入上升沿触发
Q[3:0]:计数器的输出
c_enable, c_load, c_