Hrishikesh Posted September 4, 2023 Report Share Posted September 4, 2023 Hi, I am new to system c and in the process of exploring I am facing below Issue please help me out in finding out the root cause. Error: (E112) get interface failed: index out of range: port 'mux.port_0' (sc_in) In file: ../../../src/sysc/communication/sc_port.cpp:235 //---------------------------------------------------------------------- // 4 TO 1 Multiplexer //---------------------------------------------------------------------- #include "systemc.h" SC_MODULE(mux_4_to_1){ // Port Decleration sc_in<sc_bv<4>> i_i; // 4-Bit Input sc_in<sc_bv<2>> s_i; // 2-Bit Select Line sc_out<bool> y_o; // Output // Multiplexer Logic void mux(){ // Convert bit vector to int int select_value = s_i.read().to_uint(); switch(select_value){ case 0 : y_o.write(i_i[0]); break; case 1 : y_o.write(i_i[1]); break; case 2 : y_o.write(i_i[2]); break; case 3 : y_o.write(i_i[3]); break; default : y_o.write(false); } } // Constructor SC_CTOR(mux_4_to_1){ SC_METHOD(mux); sensitive <<i_i <<s_i; } }; //---------------------------------------------------------------------- // 4 TO 1 Multiplexer Driver //---------------------------------------------------------------------- #include "systemc.h" SC_MODULE(driver){ // Signals to be driven sc_out<sc_bv<4>> i_i; sc_out<sc_bv<2>> s_i; // Function to apply inputs void inputs(){ i_i.write("0010"); s_i.write("00"); wait(5,SC_NS); s_i.write("01"); wait(5,SC_NS); s_i.write("10"); wait(5,SC_NS); s_i.write("11"); wait(5,SC_NS); } // Constructor SC_CTOR(driver){ SC_THREAD(inputs); sensitive <<i_i <<s_i; } }; //---------------------------------------------------------------------- // 4 TO 1 Multiplexer Driver //---------------------------------------------------------------------- #include "systemc.h" SC_MODULE(monitor){ // Signals to be monitored sc_in<sc_bv<4>> i_i; sc_in<sc_bv<2>> s_i; sc_in<bool> y_o; // Function to monitor signals void mon(){ cout << "Input: "<<i_i.read() << "Select:" <<s_i.read() << " Output: "<<y_o.read() <<endl; } // Constructor SC_CTOR(monitor){ SC_METHOD(mon); sensitive <<i_i <<s_i <<y_o; } }; //---------------------------------------------------------------------- // 4 TO 1 Multiplexer Main //---------------------------------------------------------------------- #include "systemc.h" #include "mux_4_to_1.h" #include "driver.h" #include "monitor.h" // Main Function int sc_main( int argc, char* argv[]){ sc_signal <sc_bv<4>> i_i; sc_signal <sc_bv<2>> s_i; sc_signal <bool> y_o; mux_4_to_1 mux("mux"); driver drive("driver"); monitor mon("monitor"); mux.i_i(i_i); mux.s_i(s_i); mux.y_o(y_o); drive.i_i(i_i); drive.s_i(s_i); mon.i_i(i_i); mon.s_i(s_i); mon.y_o(y_o); //Waveform sc_trace_file *Tf; Tf = sc_create_vcd_trace_file("traces"); sc_trace(Tf, i_i, "i_i"); sc_trace(Tf, s_i, "s_i"); sc_trace(Tf, y_o, "y_o"); sc_start(30,SC_NS); sc_close_vcd_trace_file(Tf); return 0; } Thank you in advance, I want some suggestions and guidance from the community to ramp up my learning journey. Quote Link to comment Share on other sites More sharing options...
Eyck Posted September 4, 2023 Report Share Posted September 4, 2023 Actually you try to call an array operator for the port i_i in mux_4_to_1. This does not access the respective bit in the data but the n-th interface of a (potential) multi-port. So you need to do a explcit read to the the sc_bit and then an array access: // Multiplexer Logic void mux(){ // Convert bit vector to int int select_value = s_i.read().to_uint(); auto in_val = i_i.read(); switch(select_value){ case 0 : y_o.write(in_val[0]); break; case 1 : y_o.write(in_val[1]); break; case 2 : y_o.write(in_val[2]); break; case 3 : y_o.write(in_val[3]); break; default : y_o.write(false); } } There are several other things to your code: You should use C++11 You should name your ports and signals. Using C++11 this would look like sc_out<sc_bv<4>> i_i{"i_i"}; This eases debugging (and tracing). sc_main should only instantiate the toplevel module/testbench. No structural code and no signals in sc_main. This eases reuse of modules and porting it to other simulators (Synopsys, SiemensEDA, or Cadence). The only other code should be paring command line arguments, setting up tracing/logging, starting simulation and evaluating the results. It also unifies your sc_main across different models. Maros SC_CTOR and SC_MODULE are discuraged and, as far as I know, will be deprecated and removed from future standards. The do not add substantial functionality. Hrishikesh 1 Quote Link to comment Share on other sites More sharing options...
Hrishikesh Posted September 7, 2023 Author Report Share Posted September 7, 2023 Thank you for taking time to go through the code and pointing out the problem, also thank you for your valuable suggestions. I also had to explicitly convert the read value to bool before writing it to output. // Multiplexer Logic void mux(){ // Convert bit vector to int int select_value = s_i.read().to_uint(); sc_bv<4> in = i_i.read(); switch(select_value){ case 0 : y_o.write(in[0].to_bool()); break; case 1 : y_o.write(in[1].to_bool()); break; case 2 : y_o.write(in[2].to_bool()); break; case 3 : y_o.write(in[3].to_bool()); break; default : y_o.write(false); } } I have limited knowledge in filed of system level design, but how modules will be designed if SC_MODULE macro is discontinued. Quote Link to comment Share on other sites More sharing options...
AmeyaVS Posted September 8, 2023 Report Share Posted September 8, 2023 Hello @Hrishikesh, You can always see how the SC_MODULE macro is expanded. One can run the source through pre-processor stage of the compiler to see the actual code. SystemC is C++ Library, i.e. these macros are there for convenience to succinctly capture the model intent. One can easily forgo them and usually recommended approach is to usually avoid them. Hope it helps. Regards, Ameya Vikram Singh Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.