gpt4 book ai didi

Verilog 注册分配?

转载 作者:行者123 更新时间:2023-12-04 01:50:03 24 4
gpt4 key购买 nike

我是 Verilog 编程的新手,我不知道在哪里初始化 reg 变量?

让我们看一下以下片段:编辑: Warning at synthesize

module test (
output LED0
);
reg led = 1'b1;
assign LED0 = led;
endmodule

module test (
output LED0
);

reg led;
initial begin
reg led <= 1'b1;
end

assign LED0 = led;
endmodule

给我:使用 led 的初始值,因为它从未在以下行分配:reg led = 1'b1;

reg 类型是否只在 always@ block 中赋值?

另一个例子:

module fourBitCounter
(input clk,
output [3:0]counter
);

wire clk;
initial begin
reg[3:0] counter = 4'b1;
end

always@ (posedge clk) begin

if(counter > 15)
counter <= 0;
else
counter <= counter + 1;
end endmodule

此处 reg 的初始值为 0,但我之前将其设置为 1... 有什么问题吗?谢谢!

最佳答案

Are reg types only assigned in always@ block?

不可以,reg 类型可以在 always block 和 initial block 中分配(加上 taskfunction 但我会在这个问题的范围内跳过它们)

对于您的 fourBitCounter,在 initial block 中声明的 reg[3:0] counter 创建了一个也称为 的局部变量counter 只能在创建它的 block 的范围内访问。您需要删除初始 block 中的 reg[3:0] 以便赋值应用到预期的计数器。但它仍然无法工作,因为您将 counter 声明为推断的连线类型,并且 always/initial block 无法分配连线。

counter 被声明为 4 位推断线的输出(output [3:0] counteroutput wire [3:0 ] 计数器)。由于 counter 是在 always block 和 initial block 中分配的,因此它需要是 reg 类型。因此它应该声明为 output reg [3:0] counter

此外,您将 clk 声明为输入和本地线路,不能两者兼而有之。端口可以​​在本地访问,没有理由将它们重新声明为本地网络。

仅供引用:对于 4 位值,15+1 等于 0,因为没有任何内容可存储 MSB。

module fourBitCounter (
input clk,
output <i><b>reg</b></i> [3:0] counter // 'output reg', not 'output'
);

//wire clk; // do not do this, clk is an input
initial begin
counter = 4'b1; // no 'reg' here
end

always @(posedge clk) begin
if(counter > 15) // this will never evaluate as true with counter declared as 4-bit
counter <= 0;
else
counter <= counter + 1;
end
endmodule

对于 Verilog,assign 语句只能应用于网络类型(例如 wire)。这是合法的:

module test ( output LED0 ); // LED0 is an inferred wire
assign LED0 = 1'b1;
endmodule

这是非法的:

module test ( output reg LED0 ); // Explicit reg
assign LED0 = 1'b1; // illegal, assign on a reg
endmodule

关于Verilog 注册分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40847977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com