我已经上了一段时间的课程,一直在研究这个问题,但我似乎无法使其正常工作。我对verilog还是很陌生,所以希望它不会太明显。基本上,当我通过模拟器运行模块时,我的输出始终都是x。
这是我的代码:
module state_machine(
input clk_i,
input reset_n,
input LB,
input RB,
output reg [3:0] outputs
);
reg [3:0] state;
reg [3:0] state_n;
parameter FW = 4'b0101;
parameter BWL = 4'b0000;
parameter BWR = 4'b0000;
parameter SL = 4'b0001;
parameter SR = 4'b0100;
always @ (posedge clk_i, negedge reset_n)
begin
if(!reset_n)
state <= FW;
else
state <= state_n;
end
always @ (*)
begin
case(state)
FW: begin
if(!RB)
state_n = BWR;
else if(!LB)
state_n = BWL;
end
BWL: state_n = SL;
BWR: state_n = SR;
SL: state_n = FW;
SR: state_n = FW;
default: state_n = FW;
endcase
end
always @ (*)
begin
outputs = state;
end
endmodule
clk_i输入是使用计数器方法制作的慢速时钟,在这里:
module clock_counter(
input clk_i,
input reset_n,
output reg clk_o
);
reg [19:0] count;
always @ (posedge clk_i, negedge reset_n)
begin
count <= count + 1;
if(!reset_n)
begin
clk_o <= 0;
count <= 0;
end
else if(count >= 1039999)
begin
clk_o <= ~clk_o;
count <= 0;
end
end
endmodule
它们都由仅执行此操作的顶级模块实例化。我没有收到任何错误,但是我确实收到一些关于某些我不知道卡在零的警告。
谁能看到哪里出了问题吗?
编辑:这是我的测试台代码:
`timescale 1 ns / 1 ns
// Define Module for Test Fixture
module top_module_tf();
// Inputs
reg reset_n;
reg LB;
reg RB;
// Outputs
wire [3:0] outputs;
// Bidirs
// Instantiate the UUT
// Please check and add your parameters manually
top_module UUT (
.reset_n(reset_n),
.LB(LB),
.RB(RB),
.outputs(outputs)
);
// Initialize Inputs
// You can add your stimulus here
initial begin
reset_n = 1; LB = 1; RB = 0;
#500000000 reset_n = 1; LB = 1; RB = 1;
end
endmodule // top_module_tf
您需要在0时刻将测试台reset_n
信号设置为0,以重置逻辑。延迟后,应将reset_n
设置为1。
所有reg
信号默认为X。
使用重置是传统的方式,但是您也可以像这样给寄存器一个初始值
reg LB = 1'b0;
reg RB = 1'b0;
Modelsim支持这一点,Quartus将在启动时合成加载给定值的reigster。您也可以在verilog 2001端口声明中执行此操作
module some_module(
input wire clk,
output reg[15:0] counter_val = 16'd12345,
output wire some_other_signal);
因此,您可以减少仿真所需的重置代码量。不过,在摆脱重置操作之前,您需要仔细考虑,例如如果您的寄存器损坏,请确保您的逻辑最终会转为有效状态。