52class sc_sync_window :
public sc_core::sc_module,
public sc_core::sc_prim_channel
54 static_assert(std::is_base_of<sc_sync_policy_base, sc_sync_policy>::value,
55 "sc_sync_policy must derive from sc_sync_policy_base");
57 sc_core::sc_event m_sweep_ev;
58 sc_ob_event m_step_ev;
61 sc_sync_policy policy;
66 sc_core::sc_time from;
68 bool operator==(
const window& other)
const {
return other.to == to && other.from == from; }
71 static inline const struct window zero_window = { sc_core::SC_ZERO_TIME, sc_core::SC_ZERO_TIME };
72 static inline const struct window open_window = { sc_core::SC_ZERO_TIME, sc_core::sc_max_time() };
76 window m_incomming_window;
80 std::function<void(
const window&)> m_other_async_set_window_fn;
82 void do_other_async_set_window_fn(window w)
84 if (m_other_async_set_window_fn) {
85 auto now = sc_core::sc_time_stamp();
86 m_other_async_set_window_fn(w);
93 auto now = sc_core::sc_time_stamp();
94 auto to = m_window.to;
95 SCP_INFO(()) <<
"End window check"
96 <<
" their window " << m_window.from <<
" " << to;
99 sc_core::sc_unsuspend_all();
102 SCP_TRACE(()) <<
"Suspend at: " << now <<
" quantum: " << policy.quantum();
103 do_other_async_set_window_fn({ now, now + policy.quantum() });
105 if (!policy.keep_alive()) async_attach_suspending();
106 sc_core::sc_suspend_all();
109 sc_core::sc_unsuspend_all();
110 if (!policy.keep_alive()) async_detach_suspending();
113 m_step_ev.notify(to - now);
122 auto now = sc_core::sc_time_stamp();
124 SCP_INFO(()) <<
"Done sweep"
125 <<
" their window " << m_window.from <<
" - " << m_window.to;
127 do_other_async_set_window_fn({ now, now + policy.quantum() });
133 std::lock_guard<std::mutex> lg(m_mutex);
135 m_window = m_incomming_window;
136 auto now = sc_core::sc_time_stamp();
137 SCP_INFO(()) <<
"Update"
138 <<
" their window " << m_window.from <<
" - " << m_window.to;
140 if (m_window.from > now) {
141 m_sweep_ev.notify(m_window.from - now);
146 m_update_ev.notify(sc_core::SC_ZERO_TIME);
157 void async_set_window(
const window& w)
160 std::lock_guard<std::mutex> lg(m_mutex);
162 if (!m_is_attached) {
163 SCP_FATAL(()) <<
"Cannot set window on detached sync_window. "
164 <<
"Window must be attached before calling async_set_window(). ";
166 m_incomming_window = w;
167 if (!(w == m_window)) {
168 SCP_INFO(()) <<
"sync " << w.from <<
" " << w.to;
169 async_request_update();
186 m_sc.run_on_sysc([
this] { async_attach_suspending(); },
false);
187 m_is_attached =
true;
199 void detach(
const sc_core::sc_time now = sc_core::SC_ZERO_TIME)
201 async_set_window({ now, sc_core::sc_max_time() });
203 m_sc.run_on_sysc([
this] { async_detach_suspending(); },
false);
204 m_is_attached =
false;
208 if (m_other_async_set_window_fn) {
209 SC_REPORT_WARNING(
"sc_sync_window",
210 "m_other_async_set_window_fn was already registered or other "
211 "sc_sync_window was already bound!");
213 m_other_async_set_window_fn = std::bind(&sc_sync_window::async_set_window, other, std::placeholders::_1);
215 void register_sync_cb(std::function<
void(
const window&)> fn)
217 if (m_other_async_set_window_fn) {
218 SC_REPORT_WARNING(
"sc_sync_window",
219 "m_other_async_set_window_fn was already registered or other "
220 "sc_sync_window was already bound!");
222 m_other_async_set_window_fn = fn;
224 SC_CTOR (sc_sync_window) : m_window({sc_core::SC_ZERO_TIME, policy.quantum()}), m_is_attached(
false)
226 SCP_TRACE(())(
"Constructor");
227 SC_METHOD(sweep_helper);
229 sensitive << m_sweep_ev;
231 SC_METHOD(step_helper);
233 sensitive << m_step_ev << m_update_ev;
235 m_step_ev.notify(sc_core::SC_ZERO_TIME);
void detach(const sc_core::sc_time now=sc_core::SC_ZERO_TIME)
Detach from the primitive channel's suspend/resume mechanism and fully open the time window.
Definition sync_window.h:199