如何在verilog中将输入存储到reg中?

时间:2015-04-14 13:21:14

标签: verilog fpga hdl

我'试图从名为'的电线中存储价值。进入reg' a。 但是,问题在于reg' a'正在展示' xxxx'在模拟器中。然而,电线的价值在'显示正确。 我的目标只是从输入线读取值并将其存储到寄存器中。

module test(
input [3:0] in,
output [3:0] out
);
reg [3:0] a;

initial
begin
a = in;
end
endmodule

1 个答案:

答案 0 :(得分:2)

a的值为' xxxx'在模拟中,a可能只设置为in的值最初,而a可能尚未设置为任何特定时间模拟中此时的值。


在Verilog中声明reg并不一定意味着代码描述了硬件寄存器。这通常涉及使用时钟信号:

module test(
  input clk,
  input [3:0] in,
  output [3:0] out
);

// this describes a register with input "in" and output "a"
reg [3:0] a;
always @(posedge clk) begin
  a <= in;
end

// I assume you want "a" to be the output of the module
assign out = a;

endmodule

这是一个反例,其中reg用于描述不是寄存器的东西,而只是简单的线:

module not_a_register(
  input in,
  output out
);

reg a;
always @(in) begin
  a <= in;
end

assign out = a;

endmodule

另请注意,我在<=块中使用了非阻塞赋值运算符always,这在描述同步逻辑时是一种很好的做法。您可以阅读更多相关信息here