在verilog中更改被调用模块内的顶级变量

问题描述 投票:0回答:2

我一直试图以某种方式在Verilog中实现全局变量的使用。或者,在模块中具有共享变量。甚至可以通过引用传递电线或注册(就像我们在大多数高级语言中使用的那样,比如c ++)。他们中的任何一个都会为我做这个伎俩。但我无法实现其中任何一个!

module topLevel(output [1:0] Output);

    wire[1:0] PC;

    sum s1();

    assign Output = PC;


endmodule


module sum();

    assign topLevel.PC = 2'b11;

endmodule

代码运行没有任何错误,但“PC”的值没有改变。我该怎么办?

global-variables verilog system-verilog
2个回答
1
投票

使用SystemVerilog,您可以在包中声明变量。从那里,您可以将包的内容(或特定变量)导入模块。然后使用变量名称,就像在本地声明一样。或者您无法通过其包范围导入和访问变量。例:

package my_pkg; // packages are globally accessible
  logic [1:0] PC; // static variable
endpackage

module topLevel(output [1:0] Output);
  import my_pkg::PC;  // or   import my_pkg::*;

  sum1 #(0,3)  s1();
  sum2 #(10,1) s2();
  assign Output = PC; // or   assign Output = my_pkg::PC;
endmodule

module sum1 #(parameter TIME=0, VALUE=0) ();
  import my_pkg::PC;          // or   import my_pkg::*;
  initial #(TIME) PC = VALUE;
endmodule
module sum2 #(parameter TIME=0, VALUE=0) ();
  initial #(TIME) my_pkg::PC = VALUE;
endmodule

请注意,这仅适用于模拟,通常不可合成。

要合成或使用Verilog,必须将PC添加到整个层次结构中的端口列表,并确保只有一个活动驱动程序分配PC。这意味着由一个assign语句驱动或由一个always块更新。

Verilog和SystemVerilog是HDL(硬件描述语言)。网(wire)表示必须从一个逻辑门的输出路由到其他逻辑门的输入的物理线。添加了HDL语言中的任何类型的引用支持,仅供测试平台在模拟中使用。


0
投票

我在VCS中运行此代码,并且正确地更改了PC值。

请参阅以下内容。

module topLevel(output [1:0] Output);
  wire[1:0] PC;

  sum s1();

  assign Output = PC;

  initial
    $monitor ("PC - %0b", PC);
endmodule

module sum();
  assign topLevel.PC = 2'b11;
endmodule

// Output - 
PC - 11

但是我不确定以下代码的可合并性。

在模块内部,您应始终只访问内部网络和端口。

因此,在上面的设计中,您可以将Output net连接到sum模块的端口,如下所示。

module topLevel(output [1:0] Output);
  wire[1:0] PC;

  sum s1(PC);

  assign Output = PC;
endmodule

module sum(output [1:0] x);
  assign x = 2'b11;
endmodule
© www.soinside.com 2019 - 2024. All rights reserved.