vcs cpp compile

vcs cpp compile demo

tree

1
2
3
4
5
6
7
8
demo
|__ Makefile
|__ sv_src
|__ top.sv
|__ c_src
|__ my.cpp
|__ shared.cpp
|__ shared.hpp

Makefile

1
2
3
4
5
6
7
8
9
10
cmp:
vcs +v2k +vcs+lic+wait -full64 -sverilog ./sv_src/top.sv -top top_tp \
-l ./log/cmp.log -o ./exec/simv -timescale=1ns/1ps -debug_access+all \
-assert enable_diag

run: my.so
./exec/simv +vcs+lic+wait -l ./log/run.log -sv_lib ./my.so

my.so: my.cpp shared.cpp
g++ -m64 -g -fPIC -shared -o ./my.so ./c_src/my.cpp ./c_src/shared.cpp -I./c_src

top.sv

1
2
3
4
5
6
7
8
program top_tp;
import "DPI-C" function chandle get_inst(int din);
import "DPI-C" function void test(chandle inst);
initial begin
chandle c1 = get_inst(5);
test(c1);
end
endprogram

my.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "shared.hpp"
extern "C"{
extern void *get_inst(int din);
extern void test(void * inst);
}

void *get_inst(int din){
return new testclass(din);
}

void test (void * inst){
testclass* c1 = (testclass*)inst;
c1->test();
}

shared.hpp

1
2
3
4
5
6
7
8
9
10
extern "C"{
class testclass {
public:
testclass(int din);
~testclass();
void test();
private:
int cnt;
};
}

shared.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "shared.hpp"
#include <iostream>

testclass::testclass(int din=0){
cnt = din;
std::cout << "constructor, cnt="<<cnt<<std::endl;
}
testclass::~testclass(){
std::cout << "destructor"<<std::endl;
}
void testclass::test(){
std::cout << "hello world, cnt="<<cnt<<std::endl;
}