-1

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

1 Answer 1

0

The error you are getting is due to the ports not being connected to anything. The other issue is the while loop which will just block forever. If you want to use while loops 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 while loop.

#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(){

        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;
    }
};

SC_MODULE(test_harness){
    sc_out<bool> sw1, sw2;
    sc_in<bool> l1, l2, l3, clk;
    

    SC_CTOR(test_harness){
    SC_METHOD(proc);
    sensitive << clk.pos();     
}

void proc(void){

    bool s1 = (value & 1);
    bool s2 = (value & 2);
    sw1.write(s1);
    sw2.write(s2);
    value = (++value) % 4;
    
    std::cout << "s2 = " << s2 << ", s1 = " << s1 << '\n';  
    std::cout << "l1 = " << l1.read() << ", l2 = " << l2.read() << ", l3 = " << l3.read() << '\n';      
}

private:
    unsigned value = 0;
};

int sc_main(int argc, char **argv)
{
    test_harness th("test_harness");
    lights_on ls("lights_on");  
    sc_clock clk("clk", 1.0, SC_SEC);

    // create some signals to connect up the ports
    sc_signal<bool> s1, s2, l1, l2, l3;

    // connect signals to test harness
    th.clk(clk);
    th.sw1(s1);
    th.sw2(s2);
    th.l1(l1);
    th.l2(l2);
    th.l3(l3);

    // connect signals to module under test
    ls.switch_1(s1);
    ls.switch_2(s2);
    ls.light_l1(l1);
    ls.light_l2(l2);
    ls.light_l3(l3);


    sc_start(10.0, SC_SEC); // run for 10 seconds

    return 0; 
 }

I compiled the above with g++ -std=c++17 -o test test.cpp -lsystemc and got the following output.

s2 = 0, s1 = 0
l1 = 1, l2 = 1, l3 = 1
s2 = 0, s1 = 1
l1 = 1, l2 = 1, l3 = 1
s2 = 1, s1 = 0
l1 = 1, l2 = 0, l3 = 1
s2 = 1, s1 = 1
l1 = 1, l2 = 0, l3 = 0
s2 = 0, s1 = 0
l1 = 1, l2 = 0, l3 = 0
s2 = 0, s1 = 1
l1 = 1, l2 = 0, l3 = 0
s2 = 1, s1 = 0
l1 = 1, l2 = 0, l3 = 0
s2 = 1, s1 = 1
l1 = 1, l2 = 0, l3 = 0
s2 = 0, s1 = 0
l1 = 1, l2 = 0, l3 = 0
s2 = 0, s1 = 1
l1 = 1, l2 = 0, l3 = 0
s2 = 1, s1 = 0
l1 = 1, l2 = 0, l3 = 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.