用Verilog HDL 语言设计一个四位递减计数器以及测试激励
时间: 2024-02-16 11:00:38 浏览: 36
下面是一个用 Verilog HDL 语言设计的四位递减计数器以及测试激励:
```verilog
module counter(
input clk, // 时钟信号
input rst, // 复位信号
output reg [3:0] count // 计数器输出
);
// 定义计数器的初始值
parameter INIT_VALUE = 4'hF;
// 时钟上升沿触发
always @(posedge clk) begin
if (rst) begin
// 复位时计数器置为初始值
count <= INIT_VALUE;
end else begin
// 否则计数器递减
count <= count - 1;
end
end
endmodule
```
上述代码中,计数器从初始值 `INIT_VALUE` 开始递减,直到计数器值为 0。同时,当复位信号 `rst` 为高电平时,计数器会被复位为初始值。
下面是一个测试激励代码,它可以测试计数器从初始值开始递减,直到计数器值为 0,然后再次从初始值开始递减:
```verilog
module testbench;
// 时钟周期
parameter PERIOD = 10;
// 输入信号
reg clk;
reg rst;
// 输出信号
wire [3:0] count;
// 实例化计数器
counter uut (
.clk(clk),
.rst(rst),
.count(count)
);
// 时钟信号
always #(PERIOD / 2) clk = ~clk;
// 测试用例
initial begin
// 复位计数器
rst = 1;
#PERIOD;
rst = 0;
#PERIOD;
// 计数器从初始值开始递减
assert(count === 4'hF) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hE) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hD) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hC) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hB) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hA) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h9) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h8) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h7) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h6) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h5) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h4) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h3) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h2) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h1) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h0) else $error("Error: count=%h", count);
// 计数器从初始值开始递减
#PERIOD;
assert(count === 4'hF) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hE) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hD) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hC) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hB) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'hA) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h9) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h8) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h7) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h6) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h5) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h4) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h3) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h2) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h1) else $error("Error: count=%h", count);
#PERIOD;
assert(count === 4'h0) else $error("Error: count=%h", count);
$display("All test cases passed!");
$finish;
end
endmodule
```
上述代码中,测试激励会通过时钟信号 `clk`,以及复位信号 `rst`,对计数器进行测试。测试激励首先会将计数器复位,然后计数器会从初始值开始递减,直到计数器值为 0。接着,计数器会再次从初始值开始递减,直到计数器值为 0。最后,测试激励会输出所有测试用例都通过的消息,并结束仿真。