Jump to content

communication between SystemC module and C++ code


bailin7134

Recommended Posts

Hi there,

I am right now trying to implement a SystemC module so that I can call it from my c++ code. It seems there are two methods:

1. directly instantiate the SystemC module and using sc_start() to run it.

2. using socket that SystemC module and C++ code can transmit message between each other.

I tried to do something as attach figure shows, but it seems sc_main() is a must to run SystemC module. So could you please supply a simple example to show how can I achieve this goal?

 

Best regards,

Lin

Screenshot from 2022-02-04 12-58-50.png

Link to comment
Share on other sites

6 hours ago, DavidC said:

I'm not entirely sure if I understand your specific goal (beyond running your testbench, that is).

But if you simply rename your

int main(void)

with

int sc_main(int argc, char *argv[])

then at least it should start doing something !

Hi thank you for the reply. I understand if using sc_main, the code works. But what I want to do is calling the SystemC module from my c++ program. Suppose I have a c++ function. Inside of it, I want to instantiate systemC module and run it. Hope this explanation is clear.

Link to comment
Share on other sites

Before calling any of the SystemC stuf you need to initialize the SystemC kernel. This is done in the main function provided by the reference simulation and this main in turn calls the user provided sc_main function. Since3 sc_main is a C++ function you can move all your C++ code here without any hassle. If you need to provide a main function with your code for whatever reason you need to implement something like this: https://github.com/accellera-official/systemc/blob/master/src/sysc/kernel/sc_main_main.cpp

Link to comment
Share on other sites

  • 2 years later...

hi, thank you for your solution.   Here is my c++ program to call an systemc module according to your solution:

    

class DoFFT: public sc_module {
	public:
		SC_HAS_PROCESS(DoFFT);
		DoFFT(sc_module_name name){
			SC_THREAD(dofft);
		};
		void dofft(){
			cout << " # calculate fft !"<< endl;
		};
};
int sc_main(int argc, char* argv[]) {
	DoFFT dofft("do");
	sc_start(1, SC_SEC);
	cout << " sc_main finish !" << endl;
	return 0;
}

int main(){
	sc_main(0, NULL);
	cout << " here is main fun !" << endl;
	sc_main(0, NULL);
	return 0;
}


It works when i only add one "sc_main()" in main function.  But there is error when i add more than one, as shown in above mian function.     what  if i want to call this systemc module multi times ?    
 

Link to comment
Share on other sites

20 hours ago, Mudong said:

It works when i only add one "sc_main()" in main function.  But there is error when i add more than one, as shown in above mian function.     what  if i want to call this systemc module multi times ?    

maybe the solution above is not good.  I find better solution here :

thanks .

Link to comment
Share on other sites

SystemC is not designed to allow more than one sc_main to run. If you want more than one kernel to run, you will need to launch a separate OS process and use IPC. You will probably also need to learn how to use the new sc_suspend_all method and its cousins from section 4.6.3 in IEEE-1666-2023. This also requires you to use SystemC 3.0 (released just over a week ago on March 29th, 2024) and C++17. async_request_update can then be used to resume.

Please note this is an advanced topic and requires have a solid grasp of SystemC simulation semantics including the subtle differences between SystemC processes (SC_THREAD and SC_METHOD) and OS processes vs OS threads. [Hint: SystemC is mostly not OS thread-safe; hence, the need for async_request_update and the suspend mcchanisms.]. You will also need a strong understanding of software concurrency including  thread_local variables, std::mutex, std::lock_guard, and std::atomic. In other words, you need to be a proficient Modern C++ programmer. Hacking, googling, and even AI won't do the job.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...