用行为语句设计一个8位可变模计数器, 要求:1. 当计数器计到1xx(“xx”为学号后两位)时,自动从零开始重新计数,同时产生一个高电平。 2. 当有加载信号时,装入预置的值(班内序号)。我的学号最后一位93,班内序号3 题目的modelsim测试程序
时间: 2024-02-18 09:06:04 浏览: 74
以下是一个简单的基于行为语句的8位可变模计数器的Verilog代码,同时包含了您的要求:
```verilog
module mod_counter(input clk, input rst, input load, output reg[7:0] count, output reg out);
reg[7:0] preset = 8'd93; // 学号最后一位93,班内序号3
reg[7:0] next_count;
always @(posedge clk or posedge rst) begin
if (rst) begin
count <= 8'd0;
out <= 1'b0;
end else begin
if (load) begin
count <= preset;
end else begin
if (count == 8'd1 && next_count == 8'd0) begin
out <= 1'b1;
end else begin
out <= 1'b0;
end
count <= next_count;
end
end
end
always @(count) begin
if (count == 8'd193) begin // 1xx("xx"为学号后两位)时
next_count <= 8'd0;
end else begin
next_count <= count + 1;
end
end
endmodule
```
接下来是一个简单的Testbench,可以用来测试上述代码:
```verilog
module tb_mod_counter;
reg clk, rst, load;
wire [7:0] count;
wire out;
mod_counter dut(clk, rst, load, count, out);
initial begin
clk = 0;
rst = 1;
load = 0;
#10 rst = 0;
#10 load = 1;
#10 load = 0;
#100 $finish;
end
always #5 clk = ~clk;
endmodule
```
当load信号为1时,计数器会将预设值装入。当计数器计到193时,它将会自动从0重新开始,同时out输出高电平。您可以根据自己的需要进行修改。
阅读全文