简体   繁体   English

如何在Verilog中的if语句中实例化模块?

[英]How can i instantiate a module inside an if statement in verilog?

if (btn[0] == 1)
    begin
        operaciones op(A,B,numop,C);
        display disp(C,led);
    end

I need to instantiate it inside this if, how can i do that? 如果需要,我需要在其中实例化该怎么做?

Before you go and use a generate statement for this (which allows module instantiation inside an if statement), you need to consider what you are saying. 在使用并为此generate语句(允许在if语句中进行模块实例化)之前,您需要考虑您在说什么。 From what it appears you are trying to do in your code snippet, when button 0 is pressed, you want to do some operation and display the result on the LEDs. 从您试图在代码段中显示的内容来看,当按下按钮0时,您想要进行一些操作并将结果显示在LED上。 However, you are treating modules like function calls when they are something very different. 但是,当模块与函数调用非常不同时,您会将它们视为函数调用。

Module instantiations are declarations that the module hardware exists within its parent module. 模块实例化是模块硬件在其父模块中存在的声明。 So, when I have something like this: 所以,当我有这样的事情:

module top;

  module_name instances_name( ... );

endmodule

I declare that a module_name (all the hardware of module_name ) exists within top (which probably has more hardware). 我宣布一个module_name (所有的硬件module_name )中存在top (其中可能有更多的硬件)。 Think of modules as ICs and the parent module as a breadboard, instantiating that module is like putting that IC down on the breadboard with the port connections being your wires to each pin of that IC. 将模块视为IC,将父模块视为面包板,实例化该模块就像将IC放在面包板上,而端口连接就是您连接到该IC每个引脚的电线。 All the ICs necesary to do anything that design does exist on in the circuit somewhere; 所有需要做任何设计工作的IC都存在于电路中的某个位置。 they dont appear and disappear depending on what the user inputs. 它们不会出现或消失取决于用户输入的内容。

Now think back to what you wanted to say: you are saying, when buttton 0 is pressed, put this IC on the breadboard. 现在回想一下您想说的内容:您要说的是,当按下按钮0时,将此IC放在面包板上。 If not pressed, take it away. 如果不按,则将其取走。 As ICs dont appear and disappear from a circuit board, this doesnt make sense. 由于IC不会从电路板上出现和消失,所以这没有任何意义。 So you need to treat module instantiations the same way you'd treat ICs if you were building the design on a breadboard; 因此,如果您要在面包板上构建设计,则需要像对待IC一样对待模块实例化。 you can declare there is hardware to preform this operation (put down and wire up the IC), and when botton 0 is pressed, route the output from that hardware to the LEDs (using another IC or small circuit to do the routing; think mux). 您可以声明存在执行该操作的硬件(放下并连接IC,然后按一下botton 0),将来自该硬件的输出路由到LED(使用另一个IC或小型电路进行路由;考虑多路复用器) )。

只需添加@Unn所说的内容,您真正需要实例化模块,然后在if语句中打开(或关闭)模块的启用信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM