1 #include "scd_simulator.h"
8 #include "scd_cont_man_master.h"
9 #include "scd_cont_man_slave.h"
10 #include "scd_init_listener.h"
11 #include "scd_logging.h"
14 scd_simulator::scd_simulator(const std::string& name,
15 const std::string &loc_host, uint16_t loc_port, bool master):
16 _name(name), _loc_host(loc_host), _loc_port(loc_port)
18 _poller = new scd_sock_poller;
20 if (master == SCD_MASTER)
21 _cont_man = new scd_cont_man_master(*this);
23 _cont_man = new scd_cont_man_slave(*this);
25 _chan_man = new scd_chan_man(*this);
29 scd_simulator::scd_simulator(const std::string& name, uint16_t loc_port,
31 _name(name), _loc_host(""), _loc_port(loc_port)
33 _poller = new scd_sock_poller;
35 if (master == SCD_MASTER)
36 _cont_man = new scd_cont_man_master(*this);
38 _cont_man = new scd_cont_man_slave(*this);
40 _chan_man = new scd_chan_man(*this);
44 scd_simulator::~scd_simulator()
52 const std::string& scd_simulator::get_name() const { return _name; }
55 scd_sock_poller& scd_simulator::get_poller() { return *_poller; }
58 scd_chan_man& scd_simulator::get_chan_man() { return *_chan_man; }
61 scd_cont_man& scd_simulator::get_cont_man() { return *_cont_man; }
64 bool scd_simulator::init()
66 scd_info("initialization start");
68 /* open port for incomming connections */
69 scd_init_listener listener(*this, _loc_host, _loc_port);
73 * init until the initialization failed or chan_man and cont_man are
74 * successfully initiated
76 while ( !_cont_man->failed() &&
77 ( !_chan_man->ready() || !_cont_man->active() ) )
79 // poll the sockets and execute callback handlers
83 _chan_man->init_process();
85 // process state transitions
88 /* as we might be waiting for a peer coming up we can not use
89 * a blocking poll as errors would always exist and the poller would
91 * => sleep some milliseconds
98 if (_cont_man->failed())
100 scd_error("initialization failed");
105 scd_info("initialization done");
112 bool scd_simulator::start()
114 scd_info("simulation start");
116 /* first delta cycle to initialize the SystemC scheduler
117 * else the _pending*() functions would fail */
118 sc_core::sc_start(sc_core::SC_ZERO_TIME);
120 while (_cont_man->active())
122 /* signalize the simulation state to the controller */
127 // events in this time step
128 _cont_man->set_busy();
132 // signalize time step until next event
133 sc_core::sc_time step;
134 step = _next_time() - sc_core::sc_time_stamp();
135 _cont_man->set_idle(step);
139 _cont_man->set_done();
141 // check for state changes
142 _cont_man->process();
144 /* run simulation if something to do and in right state */
145 if (_cont_man->busy())
147 // simulate one delta cycle
148 sc_core::sc_start(sc_core::SC_ZERO_TIME);
150 // check if we have to wait for new socket events
151 _chan_man->process();
153 // poll the sockets to induce transmissions
158 if (_cont_man->advance_time())
160 sc_core::sc_time step = _cont_man->get_time_step();
161 // advance simulation time, nothing should actually be simulated
163 scd_info("time advanced by " + step.to_string() + " to "
164 + sc_core::sc_time_stamp().to_string() );
166 else if ( _cont_man->active() )
168 // wait for activity on sockets
169 _poller->wait_process();
173 } // while simulation is running
175 /* simulation terminated */
177 if (_cont_man->failed())
179 scd_error("simulation failed");
184 scd_info("simulation done");
192 * Returns true if events exist at current simulation time.
194 bool scd_simulator::_pending_now()
196 return sc_core::sc_pending_activity_at_current_time();
201 * Returns true if events exist at current or later simulation time.
203 bool scd_simulator::_pending()
205 sc_core::sc_time next = _next_time();
206 return ( (next != sc_core::SC_ZERO_TIME) || _pending_now() );
211 * Returns the time of the next earliest timed event
212 * or SC_ZERO_TIME if no such event exists.
214 const sc_core::sc_time scd_simulator::_next_time()
216 return sc_core::sc_get_curr_simcontext()->next_time();