使用Verilog HDL语言设计一个计数器,给出设计代码和测试代码,要求不要太普通
时间: 2024-05-13 18:19:59 浏览: 79
用Verilog HDL设计计数器.pdf
设计代码:
```verilog
module counter (
input clk,
input rst,
output reg [3:0] count
);
always @(posedge clk or posedge rst) begin
if (rst) begin
count <= 4'b0;
end else begin
count <= count + 1;
end
end
endmodule
```
测试代码:
```verilog
module counter_test;
reg clk, rst;
wire [3:0] count;
counter dut (
.clk(clk),
.rst(rst),
.count(count)
);
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
rst = 1;
#10 rst = 0;
end
initial begin
#20 $display("count = %d", count);
#20 $display("count = %d", count);
#20 $display("count = %d", count);
#20 $display("count = %d", count);
#20 $display("count = %d", count);
#20 $display("count = %d", count);
#20 $display("count = %d", count);
#20 $display("count = %d", count);
end
endmodule
```
这个计数器是一个四位二进制计数器,每次时钟上升沿时计数器加1,当复位信号为高时计数器被重置为0。测试代码中,时钟信号每5个时间单位翻转一次,复位信号在10个时间单位后变为低电平,然后每20个时间单位打印一次计数器的值。
阅读全文