【问题标题】:How to store input into reg from wire in verilog?如何将输入从verilog中的电线存储到reg中?
【发布时间】:2015-04-14 13:21:14
【问题描述】:

我试图将值从名为 'in' 的线存储到 reg 'a' 中。 但是,问题是 reg 'a' 的值在模拟器中显示 'xxxx'。但是,“in”线的值显示正确。 我的目标只是从输入线上读取值并将其存储到寄存器中。

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

initial
begin
a = in;
end
endmodule

【问题讨论】:

  • 你是不是故意不将a连接到out
  • 你为什么要使用 Reg ?从您的逻辑看来,您希望将输入复制到 reg,因此每当输入更改时,reg 应该更改(您的逻辑),那么为什么不使用电线?

标签: verilog fpga hdl


【解决方案1】:

模拟中a的值是'xxxx'的原因可能是a被设置为in的值只有一次最初,而@987654325 @ 在模拟中此时可能尚未设置为任何特定值。


在 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 块内使用了非阻塞赋值运算符&lt;=,这是描述同步逻辑时的好习惯。你可以阅读更多关于它的信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多