// Learn with Examples, 2020, MIT license #include using namespace sc_core; int poprzedni = 5; SC_MODULE(SUBMODULE1) { // a submodule that writes to channel sc_port> p; SC_CTOR(SUBMODULE1) { SC_THREAD(writer); SC_THREAD(stan2); sensitive << p; // triggered by value change on the channel // dont_initialize(); } void writer() { char init; // init value while (true) { wait(SC_ZERO_TIME); std::cout << "Podaj stan: "; std::cin >> init; if (init == 'q'){ sc_stop(); } p->write(init); // write to channel through port wait(1, SC_SEC); } } void stan2() { while (true) { if (p->read() == '2' && (poprzedni==1)){ std::cout << "Sygnalizator 1: czerw pomaranczowy | Sygnalizator 2: pomaranczowy | Tramwaj 1 Czerwony | Tramwaj 2 Zielony \n"; poprzedni=2; } wait(1, SC_SEC); // receives from channel through port } } }; SC_MODULE(SUBMODULE2) { // a submodule that reads from channel sc_port> p; SC_CTOR(SUBMODULE2) { SC_THREAD(stan5); sensitive << p; // triggered by value change on the channel } void stan5() { while (true) { if (p->read() >= '5'){ std::cout << "Sygnalizator: Pomaranczowy awaryjny \n"; poprzedni = 5; } wait(1, SC_SEC); // receives from channel through port } } }; SC_MODULE(MODULE1) { // top-level module sc_port> p; // port SUBMODULE1 sub1; // declares submodule SC_CTOR(MODULE1): sub1("sub1") { // instantiate submodule sub1.p(p); // bind submodule's port directly to parent's port SC_THREAD(stan1); SC_THREAD(stan3); SC_THREAD(stan4); } void stan1() { while (true) { if (p->read() == '1' && poprzedni == 5){ std::cout << "Sygnalizator 1: Czerwony | Sygnalizator 2: Zielony | Tramwaj 1 Czerwony | Tramwaj 2 Zielony \n"; poprzedni=1; } wait(1, SC_SEC); // receives from channel through port } } void stan3() { while (true) { if (p->read() == '3' && (poprzedni==2)){ std::cout << "Sygnalizator 1: zielony | Sygnalizator 2: czerwony | Tramwaj 1 Zielony | Tramwaj 2 Czerwony \n"; poprzedni=3; } wait(1, SC_SEC); // receives from channel through port } } void stan4() { while (true) { if (p->read() == '4' && (poprzedni==3)){ std::cout << "Sygnalizator 1: pomaranczowy | Sygnalizator 2: Czerwony + pomaranczowy | Tramwaj 1 Zielony | Tramwaj 2 Czerwony \n"; poprzedni=4; } wait(1, SC_SEC); // receives from channel through port } } }; SC_MODULE(MODULE2) { sc_port> p; SUBMODULE2 sub2; SC_CTOR(MODULE2): sub2("sub2") { sub2.p(p); // bind submodule's port directly to parent's port } }; int sc_main(int, char*[]) { MODULE1 module1("module1"); // instantiate module1 MODULE2 module2("module2"); // instantiate module2 sc_signal s; // define channel outside module1 and module2 module1.p(s); // bind module1's port to channel, for writing purpose module2.p(s); // bind module2's port to channel, for reading purpose sc_start(); return 0; }