[Reprint] The meaning of the System Verilog context context and the scope of setting the imported function

Put lost, reproduce it, the original text:

https://blog.csdn.net/qq_31348733/article/details/101054625

1. The meaning of context

The context of an imported function is where the function is defined, such as $unit, module, program or package scope (scope), which is the same as the normal sv method. If you import a function into two different scopes, the corresponding C code will be executed according to the context of the import statement. This is similar to defining a run() task in two different modules of SV, and each task will explicitly access the internal variables of its own module.

Here is an example of a C method calling different SV methods depending on the context of the import and export statements:

//top.sv

module top;

import "DPI-C" context

endmodule:top

2. Set the scope of imported functions

Just as SV code can call methods in the local scope, imported C methods can also call methods outside of its default context. Use the svGetScope method to obtain a handle to the current scope, which can then be used in a call to svGetScope to make the C code think it is in another context.

Here is an example:

The first method save_my_scope() saves the scope where it was called in the SV

The second method c_display() sets its scope to the saved scope and prints the information

//c_display.c

#include <stdio.h>

#include <svdpi.h>

extern void sv_display();

svScope my_scope;

void save_my_scope(){

my_scope = svGetScope();

}

void c_display(){

// print the current scope

io_printf("C:c_display called from scope %s\n", svGetNameFromScope(svGetScope()));

// set new scope

//io_prinf("scope: %s", svGetNameFromScope(svGetScope()));

svSetScope(my_scope);

io_printf("C:calling %s.sv_display\n", svGetNameFromScope(svGetScope()));

sv_display();

}

The above C code calls svGetNameFromScope(), which returns a string representing the current scope. The returned scope is printed twice, once with the scope when the C code was first called, and once with the previously saved scope.

In the following SV code code, the first module block calls a C method to save context information. When the top module calls the c_display() method, the method sets the scope back to block, so that it calls sv_display() in the block module instead of the method of the same name in the top module

//top.sv

`timescale1ns/1ns

moduletop;

//=====================================================================\

// ********** Define Parameter and Internal Signals *************

//=====================================================================/

//======================================================================

// *************** Main Code ****************

//======================================================================

import "DPI-C" context function void c_display();

export "DPI-C" function sv_display;

function void sv_display();

$display("SV:top %m");

endfunction

block b1();

initial begin

#1 c_display();

end

endmodule:top

module block;

import "DPI-C" context function void c_display();

import "DPI-C" context function void save_my_scope();

export "DPI-C" function sv_display;

function void sv_display();

$display("SV:block %m");

endfunction

initial begin

save_my_scope();

c_display();

end

endmodule:block

The following is the result of running the program

# C:c_display called from scope top.b1

# C:calling top.b1.sv_display

# SV:block top.b1.sv_display

# C:c_display called from scope top

# C:calling top.b1.sv_display

# SV:block top.b1.sv_display

————————————————

Copyright statement: This article is an original article by CSDN blogger "Advanced Obsessive-Compulsive Disorder" and follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.

Original link: https://blog.csdn.net/qq_31348733/article/details/101054625

Guess you like

Origin blog.csdn.net/m0_38037810/article/details/129377399