17 #ifndef _SYSC_UTILITIES_H_
18 #define _SYSC_UTILITIES_H_
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #pragma GCC diagnostic pop
35 #include <cci_configuration>
44 template <
typename T,
typename... Args> std::unique_ptr<T> make_unique(Args&&... args) {
45 #if __cplusplus < 201402L
46 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
48 return std::make_unique<T>(std::forward<Args>(args)...);
53 #define NAMED(X, ...) X(#X, ##__VA_ARGS__)
54 #define NAMEDD(X, T, ...) X(scc::make_unique<T>(#X, ##__VA_ARGS__))
55 #define NAMEDC(X, T, I, ...) X(T::create<I>(#X, ##__VA_ARGS__))
57 #define TRACE_VAR(F, X) sc_core::sc_trace(F, X, std::string(this->name()) + "." #X)
58 #define TRACE_ARR(F, X, I) sc_core::sc_trace(F, X[I], (std::string(this->name()) + "." #X "(" + std::to_string(I) + ")").c_str());
59 #define TRACE_SIG(F, X) sc_core::sc_trace(F, X, X.name())
63 #if SC_VERSION_MAJOR <= 2 && SC_VERSION_MINOR <= 3 && SC_VERSION_PATCH < 2
64 #define HAS_NO_TIME_TRACE
67 #define HAS_NO_TIME_TRACE
70 #ifdef HAS_NO_TIME_TRACE
72 void sc_trace(sc_trace_file*,
const sc_time&,
const std::string&);
78 inline void sc_trace(sc_core::sc_trace_file*&,
const sc_core::sc_event&,
const char*) {}
88 template <>
void sc_trace(sc_trace_file* tf,
const sc_in<sc_time>& value,
const std::string& name);
96 template <>
void sc_trace(sc_trace_file* tf,
const sc_inout<sc_time>& value,
const std::string& name);
106 inline sc_core::sc_time
operator"" _sec(
long double val) {
return sc_core::sc_time(val, sc_core::SC_SEC); }
113 inline sc_core::sc_time
operator"" _sec(
unsigned long long val) {
return sc_core::sc_time(
double(val), sc_core::SC_SEC); }
120 inline sc_core::sc_time
operator"" _ms(
long double val) {
return sc_core::sc_time(val, sc_core::SC_MS); }
127 inline sc_core::sc_time
operator"" _ms(
unsigned long long val) {
return sc_core::sc_time(
double(val), sc_core::SC_MS); }
134 inline sc_core::sc_time
operator"" _us(
long double val) {
return sc_core::sc_time(val, sc_core::SC_US); }
141 inline sc_core::sc_time
operator"" _us(
unsigned long long val) {
return sc_core::sc_time(
double(val), sc_core::SC_US); }
148 inline sc_core::sc_time
operator"" _ns(
long double val) {
return sc_core::sc_time(val, sc_core::SC_NS); }
155 inline sc_core::sc_time
operator"" _ns(
unsigned long long val) {
return sc_core::sc_time(
double(val), sc_core::SC_NS); }
162 inline sc_core::sc_time
operator"" _ps(
long double val) {
return sc_core::sc_time(val, sc_core::SC_PS); }
169 inline sc_core::sc_time
operator"" _ps(
unsigned long long val) {
return sc_core::sc_time(
double(val), sc_core::SC_PS); }
176 inline sc_core::sc_time
operator"" _fs(
long double val) {
return sc_core::sc_time(val, sc_core::SC_FS); }
183 inline sc_core::sc_time
operator"" _fs(
unsigned long long val) {
return sc_core::sc_time(
double(val), sc_core::SC_FS); }
189 inline char* legalize_name(
char*
const name) {
192 if(*ptr ==
'.' || std::isspace(*ptr)) {
200 inline std::string legalize_name(std::string
const& name) {
203 ret += (c ==
'.' || std::isspace(c)) ?
'_' : c;
214 inline bool icompare(std::string
const& a, std::string
const& b) {
215 if(a.length() == b.length()) {
216 return std::equal(b.begin(), b.end(), a.begin(),
217 [](
unsigned char a,
unsigned char b) ->
bool { return std::tolower(a) == std::tolower(b); });
230 std::string::size_type offs{0};
231 double t_val = std::stod(value, &offs);
244 return t_val * 1_sec;
246 return sc_core::SC_ZERO_TIME;
255 std::string::size_type offs{0};
256 double t_val = std::stod(value, &offs);
258 std::string unit = value.substr(offs);
270 return t_val * 1_sec;
272 return sc_core::SC_ZERO_TIME;
275 inline sc_core::sc_time time_to_next_posedge(sc_core::sc_clock
const* clk) {
277 auto period = clk->period();
278 auto clk_run_time = sc_core::sc_time_stamp() - clk->start_time();
279 auto clk_period_point = clk_run_time % period;
280 if(clk->posedge_first())
281 return period - clk_period_point;
282 auto time_to_negedge = period * clk->duty_cycle();
283 auto time_to_posedge = period - time_to_negedge;
284 if(clk_period_point < time_to_posedge) {
285 return time_to_posedge - clk_period_point;
287 return period + time_to_posedge - clk_period_point;
290 return sc_core::SC_ZERO_TIME;
293 #if __cplusplus < 201402L
294 inline unsigned ilog2(uint32_t val) {
296 inline constexpr
unsigned ilog2(uint32_t val) {
300 return sizeof(uint32_t) * 8 - 1 - __builtin_clz(
static_cast<unsigned>(val));
313 template <
typename T>
inline T get_value(sc_core::sc_attribute<T>& a) {
return a.value; }
315 template <
typename T>
inline void set_value(sc_core::sc_attribute<T>& a, T&& value) { a.value = value; }
317 template <
typename T>
inline T get_value(cci::cci_param_typed<T>& a) {
return a.get_value(); }
319 template <
typename T>
inline void set_value(cci::cci_param_typed<T>& a, T&& value) { a.set_value(value); }
323 #define declare_method_process_cl(handle, name, host_tag, func) \
325 ::sc_core::sc_spawn_options opt; \
326 opt.dont_initialize(); \
327 opt.spawn_method(); \
328 ::sc_core::sc_process_handle handle = ::sc_core::sc_spawn(func, name, &opt); \
329 this->sensitive << handle; \
330 this->sensitive_pos << handle; \
331 this->sensitive_neg << handle; \
334 #define declare_thread_process_cl(handle, name, host_tag, func) \
336 ::sc_core::sc_spawn_options opt; \
337 ::sc_core::sc_process_handle handle = ::sc_core::sc_spawn(func, name, &opt); \
338 this->sensitive << handle; \
339 this->sensitive_pos << handle; \
342 #define SC_METHOD_CL(name, func) declare_method_process_cl(name##_handle, #name, SC_CURRENT_USER_MODULE, func)
344 #define SC_THREAD_CL(name, func) declare_thread_process_cl(name##_handle, #name, SC_CURRENT_USER_MODULE, func)
bool icompare(std::string const &a, std::string const &b)
sc_core::sc_time parse_from_string(std::string value, std::string unit) noexcept
CONSTEXPR unsigned ilog2(uint32_t val)