在 ModelSim 中运行这段 Verilog 代码时遇到问题。我已经建立了一个应该计算毫秒的电路。电路(模块分子)由两部分组成:一个接收5MHz频率时钟信号的电路(模块计数器),每经过5000cct(相当于1ms)产生一个高逻辑,因此每毫秒产生输出高),另一个电路接收相同的时钟信号并计算经过的毫秒数(模块预设)。此外,第二个电路接收负载信号(当激活时,负载产生低逻辑)。我的问题是为什么电路的输出在每毫秒过去后增加两次(而且不仅仅是一次,正如预期的那样)。我发布了 Verilog 源代码和图表(图表)。
`timescale 1ns/1ns
//1ms pulse generator
module counter(
input clk,rst,en,
output reg out
);
reg [12:0] st,st_nxt;
always @(posedge clk) begin
if(rst) st <= 13'd0;//requires rst
else st <= st_nxt;//for initialization
end
always @ * begin
out = 1'b0;
if(en) begin
st_nxt = st + 1;
if(st == 13'd4999) begin
st_nxt = 13'd0;
out = 1'b1;
end
end
end
endmodule
//tb for the first circuit
module counter_tb(
output reg clk,rst,en,
output out
);
counter cut(.clk(clk),.rst(rst),.en(en),.out(out));
initial en = 1'd1;
initial begin
rst = 1'd1;
#50 rst = 1'd0;
end
initial begin
clk = 1'd1;
forever
#100 clk = ~clk;
end
endmodule
//preset counter
module preset(
input clk,rst,en,ld,
output reg [12:0] out
);
reg [12:0] st,st_nxt;
always @(posedge clk) begin
if(rst) st <= 13'd0;//requires rst
else st <= st_nxt;//for initialization
end
always @ * begin
if(ld) st_nxt = 13'd0;//the circuit is designed
else if(en) st_nxt = st + 1;//to load only 0
out = st;
end
endmodule
//tb preset counter
module preset_tb(
output reg clk,rst,en,ld,
output [12:0]out
);
preset cut(.clk(clk),.rst(rst),.en(en),.ld(ld),.out(out));
initial begin
rst = 1'd1;
#50 rst = 1'd0;
end
initial begin
clk = 1'd1;
forever
#100 clk = ~clk;
end
initial begin
en = 1'd1;
#1000 en = 1'd0;
#200 en = 1'd1;
end
initial begin
ld = 1'd0;
#2000 ld = 1'd1;
#400 ld = 1'd0;
end
endmodule
//ms couonter
module numarator(
input clk,rst,en,ld,
output [12:0] out
);
wire f;
counter i0(.clk(clk),.rst(rst),.en(en),.out(f));
preset i1(.clk(clk),.rst(rst),.en(f),.ld(ld),.out(out));
endmodule
//tb ms counter
module numarator_tb(
output reg clk,rst,en,ld,
output [12:0] out
);
numarator cut(.clk(clk),.rst(rst),.en(en),.ld(ld),.out(out));
initial begin
rst = 1'd1;
#50 rst = 1'd0;
end
initial begin
clk = 1'd1;
forever
#100 clk = ~clk;
end
initial begin
en = 1'd1;
#2000000 en = 1'd0;
#1000000 en = 1'd1;
end
initial begin
ld = 1'd0;
#4000000 ld = 1'd1;
#1000000 ld = 1'd0;
end
endmodule