With 2 switches s1,s2 and 3 Lights l1 l2 l3. pressing s1 or s2 should switch on l1. Next step if I press s1, l1(off) and l2(on) or if I press s2, l1(off) and l3(on). Final state is on press of any switch the light which is on will be off and we are in initial state.
#include <systemc>
using namespace sc_core;
SC_MODULE(lights_on)
{
sc_in<bool> switch_1, switch_2;
sc_out<bool> light_l1, light_l2, light_l3;
void switch_on(){
while(true) {
if(switch_1.read() || switch_2.read())
{
light_l1.write(1);
if(switch_1.read()){
light_l1.write(true);
light_l2.write(false);
}
if(switch_2.read())
{
light_l1.write(true);
light_l3.write(false);
}
}
}
}
SC_CTOR(lights_on)
{
light_l1.initialize(true);
light_l2.initialize(true);
light_l3.initialize(true);
SC_METHOD(switch_on);
sensitive << switch_1 << switch_2;
}
};
int sc_main(int argc, char **argv)
{
lights_on lights("lights_on");
sc_start();
return 0;
}
below error is seen
g++ -o sim -lsystemc *.cpp && echo "Compile done. Starting run..." && ./sim
Compile done. Starting run...
Error: (E109) complete binding failed: port not bound: port 'lights_on.port_4' (sc_out)
In file: ../../../src/sysc/communication/sc_port.cpp:235
Exit code expected: 0, received: 1
The error you are getting is due to the ports not being connected to anything. The other issue is the
whileloop which will just block forever. If you want to usewhileloops you will need to use systemc threads but we don't need them here.Below I have created a test harness and some signals to connect it to your module. I have chosen to use a clock to drive the harness, it is not required to use a clock to drive it, it's just what I chose.
I did not change the internal logic, just pasted it in without the
whileloop.I compiled the above with
g++ -std=c++17 -o test test.cpp -lsystemcand got the following output.