Electronic – modules in verilog

verilogxilinx

In my recent project in Xilinx ISE, I need to create two modules, one for 4-bit multiplier and other for 4-bit adder.

In between i need to call the 4-bit adder in multiplier module which I am doing by calling adder:

module multiplier(inputs ..., output ...) begin

always @(posedge clk) begin

    shift_product[23:16]=16'b1;

    twentyfourbit_adder(sum,shift_product,sum,cout);

end

I didn't write the whole code because I am just asking that is this correct method to use one module in other.

Best Answer

In Verilog, when you are instantiating a module, that means you are adding extra hardware to the board.

This hardware must be added before simulation starts(i.e. at compile time). Here, you can not add/remove hardware at each clock pulse.

Once instantiated, the module is executed/checked for each timestamp of simulation, till the end.

So to execute any module, just instantiate it, providing the clk and other required inputs to it, and add the always block in the sub-module itself.

Once the hardware is instantiated, it shall be executed according to the logic inside it, for entire life time.

One more comment, instantiation of module requires an instance name, to uniquely identify all the pieces of hardware. So for twentyfourbit_adder, use any instance name like twentyfourbit_adder ta(sum,shift_product,sum,cout);

Your code may go as follows:

module multiplier(inputs ..., output ...) begin

// Instantiate just once
twentyfourbit_adder ta(sum,shift_product,sum,cout);

always @(posedge clk) begin

    shift_product[23:16]=16'b1;
   // Other stuff here...    
end

Side Note:

You may have mixed the understanding about module instantiation and calling of task/function. Module is a static entity while task/function can be dynamic ones. As you showed, if twentyfourbit_adder is a task, then the above call is valid.

I have added most of my answer from previous answer to this similar question. For more information, refer to Module Instantiation and Instantiating Modules and Primitives links.