30SC_MODULE (realtimelimiter) {
32 cci::cci_param<double> p_RTquantum_ms;
33 cci::cci_param<double> p_SCTimeout_ms;
34 cci::cci_param<double> p_MaxTime_ms;
36 std::chrono::high_resolution_clock::time_point startRT;
37 sc_core::sc_time startSC;
38 sc_core::sc_time runto;
39 std::thread m_tick_thread;
41 bool suspended =
false;
42 sc_core::sc_time suspend_at = sc_core::SC_ZERO_TIME;
48 tick.async_detach_suspending();
49 sc_core::sc_unsuspend_all();
52 if (suspended ==
true && (sc_core::sc_time_stamp() > suspend_at)) {
54 sc_core::sc_time drift = sc_core::sc_time_stamp() - suspend_at;
56 SCP_WARN(())(
"Time drift of {}s", drift.to_seconds());
58 if (p_MaxTime_ms && sc_core::sc_time_stamp() > (sc_core::sc_time(p_MaxTime_ms, sc_core::SC_MS) + startSC)) {
59 SCP_FATAL(())(
"Max timeout expired");
62 if (sc_core::sc_time_stamp() >= runto) {
63 SCP_TRACE(())(
"Suspending");
64 tick.async_attach_suspending();
65 sc_core::sc_suspend_all();
67 suspend_at = sc_core::sc_time_stamp();
70 SCP_TRACE(())(
"Resuming");
73 tick.notify((runto + sc_core::sc_time(p_RTquantum_ms, sc_core::SC_MS) / 2) - sc_core::sc_time_stamp());
74 tick.async_detach_suspending();
75 sc_core::sc_unsuspend_all();
81 sc_core::sc_time last = sc_core::SC_ZERO_TIME;
83 std::this_thread::sleep_for(std::chrono::milliseconds(p_RTquantum_ms));
85 runto = sc_core::sc_time(std::chrono::duration_cast<std::chrono::microseconds>(
86 std::chrono::high_resolution_clock::now() - startRT)
90 if (last > sc_core::SC_ZERO_TIME && sc_core::sc_time_stamp() == last) {
91 p_RTquantum_ms.set_value(p_RTquantum_ms.get_value() + 100);
94 (
"Stalled ? (runto is {}s ahead, systemc time has not changed, new limit {}ms {}s)",
95 (runto - sc_core::sc_time_stamp()).to_seconds(), p_RTquantum_ms.get_value(), runto.to_seconds());
99 (runto > sc_core::sc_time_stamp() + sc_core::sc_time(p_SCTimeout_ms, sc_core::SC_MS))) {
101 (
"Requested runto is {}s ahead (SCTimeout_ms set to {}s)",
102 (runto - sc_core::sc_time_stamp()).to_seconds(), p_SCTimeout_ms / 1000);
105 last = sc_core::sc_time_stamp();
116 assert(running ==
false);
120 startRT = std::chrono::high_resolution_clock::now();
121 startSC = sc_core::sc_time_stamp();
122 runto = sc_core::sc_time(p_RTquantum_ms, sc_core::SC_MS) + sc_core::sc_time_stamp();
123 tick.notify(sc_core::sc_time(p_RTquantum_ms, sc_core::SC_MS));
125 m_tick_thread = std::thread(&realtimelimiter::RTticker,
this);
132 m_tick_thread.join();
135 realtimelimiter(
const sc_core::sc_module_name& name): realtimelimiter(name,
true) {}
136 realtimelimiter(
const sc_core::sc_module_name& name,
bool autostart)
138 , p_RTquantum_ms(
"RTquantum_ms", 100,
"Real time quantum in milliseconds")
139 , p_SCTimeout_ms(
"SCTimeout_ms", 0,
"Timeout for SystemC in milliseconds")
140 , p_MaxTime_ms(
"MaxTime_ms", 0,
"Maximum run time in ms (0=no limit)")
143 SCP_TRACE(())(
"realtimelimiter constructor");
152 void end_of_simulation() { disable(); }