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_cfg/cci_param_typed.h>
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); }
185 inline constexpr uint64_t
operator"" _kB(
unsigned long long val) {
return val * 1 << 10; }
187 inline constexpr uint64_t
operator"" _MB(
unsigned long long val) {
return val * 1 << 20; }
189 inline constexpr uint64_t
operator"" _GB(
unsigned long long val) {
return val * 1 << 30; }
195 inline char* legalize_name(
char*
const name) {
198 if(*ptr ==
'.' || std::isspace(*ptr)) {
206 inline std::string legalize_name(std::string
const& name) {
209 ret += (c ==
'.' || std::isspace(c)) ?
'_' : c;
220 inline bool icompare(std::string
const& a, std::string
const& b) {
221 if(a.length() == b.length()) {
222 return std::equal(b.begin(), b.end(), a.begin(),
223 [](
unsigned char a,
unsigned char b) ->
bool { return std::tolower(a) == std::tolower(b); });
236 std::string::size_type offs{0};
237 double t_val = std::stod(value, &offs);
250 return t_val * 1_sec;
252 return sc_core::SC_ZERO_TIME;
261 std::string::size_type offs{0};
262 double t_val = std::stod(value, &offs);
264 std::string unit = value.substr(offs);
276 return t_val * 1_sec;
278 return sc_core::SC_ZERO_TIME;
281 inline sc_core::sc_time time_to_next_posedge(sc_core::sc_clock
const* clk) {
283 auto period = clk->period();
284 auto clk_run_time = sc_core::sc_time_stamp() - clk->start_time();
285 auto clk_period_point = clk_run_time % period;
286 if(clk->posedge_first())
287 return period - clk_period_point;
288 auto time_to_negedge = period * clk->duty_cycle();
289 auto time_to_posedge = period - time_to_negedge;
290 if(clk_period_point < time_to_posedge) {
291 return time_to_posedge - clk_period_point;
293 return period + time_to_posedge - clk_period_point;
296 return sc_core::SC_ZERO_TIME;
299 #if __cplusplus < 201402L
300 inline unsigned ilog2(uint32_t val) {
302 inline constexpr
unsigned ilog2(uint32_t val) {
306 return sizeof(uint32_t) * 8 - 1 - __builtin_clz(
static_cast<unsigned>(val));
319 template <
typename T>
inline T get_value(sc_core::sc_attribute<T>& a) {
return a.value; }
321 template <
typename T>
inline void set_value(sc_core::sc_attribute<T>& a, T&& value) { a.value = value; }
323 template <
typename T>
inline T get_value(cci::cci_param_typed<T>& a) {
return a.get_value(); }
325 template <
typename T>
inline void set_value(cci::cci_param_typed<T>& a, T&& value) { a.set_value(value); }
329 #define declare_method_process_cl(handle, name, host_tag, func) \
331 ::sc_core::sc_spawn_options opt; \
332 opt.dont_initialize(); \
333 opt.spawn_method(); \
334 ::sc_core::sc_process_handle handle = ::sc_core::sc_spawn(func, name, &opt); \
335 this->sensitive << handle; \
336 this->sensitive_pos << handle; \
337 this->sensitive_neg << handle; \
340 #define declare_thread_process_cl(handle, name, host_tag, func) \
342 ::sc_core::sc_spawn_options opt; \
343 ::sc_core::sc_process_handle handle = ::sc_core::sc_spawn(func, name, &opt); \
344 this->sensitive << handle; \
345 this->sensitive_pos << handle; \
348 #define SC_METHOD_CL(name, func) declare_method_process_cl(name##_handle, #name, SC_CURRENT_USER_MODULE, func)
350 #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)