3

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?

2
  • 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. Commented Dec 11, 2012 at 4: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. Commented Dec 11, 2012 at 5:08

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.