In program block , using task which declared outside

Hi, Can i use task which is declared outside in the program block?
It seems not work. My top module is written in verilog
ex:

module top();

task test();
$display("i am in outside \n");
endtask

program test1();
test();
endprogram : test1

endmodule

In reply to peter:

It would really help other if you said more than just “It does not work”. If your top level module was written in Verilog, you would get a syntax error because it would not recognize the program block syntax.

Your main problems as Prashant showed you are that your were missing an initial block, and you need to instantiate your nested program block. But the example is more complicated than it needs to be.

module top();
 
 test1 t();
task test();
  $display("I am in outside \n");
endtask

 
program test1();
  initial test();
endprogram : test1
 
endmodule

In Verilog/SystemVerilog, a task/function call will search up the hierarchy to find that task/function if it is not defined locally. This works regardless of module/program/interface, or if the module/program/Interface definition is nested or not.

BYW I strongly discourage the use of program blocks.

In reply to dave_59:

Thanks for reply. The top module(testbench) is written by others in verilog , and there are some task in the top module.
, and i want to do some verification with that task declared in the top module using system verilog. Therefore, I used the program block ,call that task in the program block.
By the way , are there methods to do this without using program block?

In reply to peter:

By the way , are there methods to do this without using program block?

Use a
module
instead of a
program
.

In reply to dave_59:

In reply to peter:
Use a
module
instead of a
program
.

Yes, it works regardless of module/program/interface.
it is the syntax problem
Thanks for your help.
Would you explain that a task/function call will search up the hierarchy?
Do you mean that it will find task in the top module , if find nothing , i will search it search down from the top?

In reply to peter:
No, it will search up the hierarchy from where the call was made. The search ends when it gets to the $root of the hierarchy.

In my example above, test() was called from test1, and there was no test defined in test1. test1’s parent is top, so the search finds test there. This happens regardless of whether the definition of test1 is nested in the definition of top or not. What matters is the instance hierarchy. The only reason for nesting is hiding the definition of test1 from other modules so only top can instantiate test1.

In reply to dave_59:

Thanks a lot !