2

I have a verilog code where I wish to use recursion. However, whenever I try this in an always block, it gives an error saying is not a task.

Is there any way I can implement a module in an always block? Also is there anyway I can use recursion within the always block?

Breakpoint
  • 428
  • 6
  • 19
  • 2
    Can you give an example of the code? Or explain the algorithm you are trying to implement using recursion? You cannot put a module instance inside an always block. – dwikle Dec 11 '12 at 04:40
  • The module, multiplies 2 numbers using a complex algorithm. One of the steps in the algorithm is to break down the 2 inputs, into smaller numbers and multiply them. I need to use recursion for the same. – Prashant Vaidyanathan Dec 11 '12 at 05:08

1 Answers1

5

You can write recursive modules using a generate block:

module unary_and
#(parameter WIDTH = 32)
(input [WIDTH-1:0] in_and,
output            out_and)

generate
 if(WIDTH == 1) begin
   assign out_and = in_and;
 end
 else if(WIDTH == 2) begin
   assign out_and = in_and[0] & in_and[1];
 end
 else begin
   unary_and #(.WIDTH (WIDTH/2))
     unary_and_low
       (.in_and  (in_and[WIDTH/2-1:0]),
        .out_and (out_and_low));

   unary_and #(.WIDTH (WIDTH - WIDTH/2))
     unary_and_high
       (.in_and  (in_and[WIDTH-1:WIDTH/2]),
        .out_and (out_and_high));

   assign out_and = out_and_low & out_and_high;
 end
endgenerate
endmodule

This is from Recursive and Iterative designs in Verilog where you can find other solutions as well. You can check out Recursive Modules too.

Maybe you should also take a look at these questions and answers:
Could we have generate inside an always block?
Verilog generate/genvar in an always block

totymedli
  • 29,531
  • 22
  • 131
  • 165