1

I'm trying to learn Verilog and I have a Verilog module and what I wish to do is then call another file and run that from within my current module.

So I have my module like:

module maths();
//register etc details
initial begin

`include "add.v"

end
endmodule

and my add.v file that is being called from the maths module is like:

task add;
    A = $random;
    B = $random;
    C = A + B;
    $display("Answer: %d", C);
endtask

But I am receiving the errors from the task file near "task": syntax error, unexpected "task" and near "endtask": syntax error, unexpected "endtask".

I read the answer at How to call tasks from a separate module in Verilog? but the answer given there about needing to call the task from within an initial or always block hasn't helped because it is within an initial block in the module.

Where am I going wrong with this?

Stussy
  • 61
  • 1
  • 7
  • Just for beginners, do **not** use `include` inside modules. This is a very bad programming style (unfortunately used in some projects) and it is very difficult to debug. – Serge Oct 19 '17 at 22:17

1 Answers1

0

Like Serge said, always include your files in the beginning of the file, right before the module statement.

`include "add.v"

module maths();
//register etc details
initial begin
    add;
end
endmodule

As for the problem, you are missing begin-end statements in the add task. Tasks always need these two statements to wrap your task code.

So, this should work:

reg A, B, C;

task add;
begin
    A = $random;
    B = $random;
    C = A + B;
    $display("Answer: %d", C);
end
endtask

Don't forget about A, B, C declarations :) !

spaceman
  • 88
  • 10
  • thanks, Ive defined wires and registers in the parent file, and its giving me undefined variable errors for those in my included file, but defining them in the included file isn't allowed because global declarations are illegal in Verilog 2001 syntax, how can I fix this? – Stussy Oct 20 '17 at 11:24
  • add them in the task, right before the begin statement. – spaceman Oct 20 '17 at 11:39