OSCR
Open Source Cartridge Reader
Loading...
Searching...
No Matches
macros.h
1/********************************************************************
2 * Open Source Cartridge Reader *
3 ********************************************************************/
4#pragma once
5#ifndef OSCR_MACROS_H_
6# define OSCR_MACROS_H_
7
8#pragma region Helpers
9
10# if !defined(DEC)
11# define DEC 10
12# endif /* DEC */
13
14# if !defined(HEX)
15# define HEX 16
16# endif /* HEX */
17
18# if !defined(OCT)
19# define OCT 8
20# endif /* OCT */
21
22# if !defined(BIN)
23# define BIN 2
24# endif /* BIN */
25
26# if !defined(STR)
27# define STR(x) #x
28# endif
29
30# if !defined(NUM2STR)
31# define NUM2STR(x) STR(x)
32# endif
33
34# if !defined(XSTR)
35#define XSTR(x) STR(x)
36# endif
37
38# if !defined(MSG)
39#define MSG(x) _Pragma (STR(message (x)))
40# endif
41
42# if !defined(DISPLAY_GCC_VERSION)
43# define DISPLAY_GCC_VERSION MSG("GCC version: " XSTR(__GNUC__.__GNUC_MINOR__.__GNUC_PATCHLEVEL__))
44# endif
45
55
56
60#if defined(__has_cpp_attribute)
61
62 // constexpr (C++)
63# if defined(__cpp_constexpr)
64 // relaxed constexpr (C++14)
65# if __cpp_constexpr < 201304L
66# define COMPILER_OUTDATED
67# else
68# define __relaxed_constexpr constexpr
69# endif
70# else
71# define COMPILER_TOO_OLD
72# endif
73
74 // template non-type parameter are allowed to be `auto` (C++17)
75# if defined(__cpp_nontype_template_parameter_auto)
76# define __TEMPLATE_AUTO_UINT8 auto
77# else
78# define COMPILER_DEPRECATED
79# endif
80
81# if defined(__cpp_if_constexpr)
82# define __if_constexpr constexpr
83#else
84# define COMPILER_DEPRECATED
85# endif
86
87 // variadic template arguments (C++11)
88# if !defined(__cpp_variadic_templates)
89# define COMPILER_DEPRECATED
90# endif
91
92 // constinit (C++20)
93# if defined(__cpp_constinit)
94# define __constinit constinit
95# define __constinitexpr constinit
96# endif
97
98 // consteval (C++20)
99# if defined(__cpp_consteval)
100# define __CONSTEVAL consteval
101# endif
102
103#endif /* __has_cpp_attribute */
104
105// "Safe" defaults are defined here in case `__has_cpp_attribute` wasn't defined
106
107#if !defined(__if_constexpr)
108# define __if_constexpr
109#endif
110
111// uint8_t when `auto` is unsupported
112#if !defined(__TEMPLATE_AUTO_UINT8)
113# define __TEMPLATE_AUTO_UINT8 uint8_t
114#endif
115
116// Empty when relaxed `constexpr`s are unsupported
117#if !defined(__relaxed_constexpr)
118# define __relaxed_constexpr
119#endif
120
121// Empty when `constinit` is unsupported
122#if !defined(__constinit)
123# define __constinit
124#endif
125
126// `constexpr` when `consteval` is unsupported
127#if !defined(__CONSTEVAL)
128# define __CONSTEVAL constexpr
129#endif
130
131#if !defined(__constinitexpr)
145# define __constinitexpr constexpr
146#endif
147
148#if defined(COMPILER_TOO_OLD) || defined(COMPILER_OUTDATED)
149//# pragma message "Outdated compiler or dialect detected!"
150//# pragma message "The recommended minimum dialect is gnu++17 (or gnu++1z)."
151//# pragma message "See: https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html"
152#endif
153
154#if defined(COMPILER_TOO_OLD)
155//# error "Your compiler is too old or you are compiling with the wrong C++ dialect."
156#endif
157
158#if defined(COMPILER_DEPRECATED) && !defined(COMPILER_TOO_OLD) && !defined(COMPILER_OUTDATED)
159//# pragma message "Deprecated compiler or dialect detected!"
160//# pragma message "Your compiler will stop working in the future. The minimum dialect is gnu++17 (or gnu++1z)."
161//# pragma message "See: https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html"
162#endif
163
164#if defined(ARDUINO_ARCH_AVR)
165
166# define LitStr(s) F(s)
167# define LitStr_P(s) PSTR(s)
168
169# define __AVR__ALWAYS_INLINE__ __attribute__((alwaysinline))
170# define __AVR__NEVER_INLINE__ __attribute__((noinline))
171
172// ARDUINO_AVR_MEGA2560
173// ARDUINO_AVR_UNO
174
175#else
176
177# define LitStr(s) s
178# define LitStr_P(s) s
179
180# define __AVR__NEVER_INLINE__
181
182#define PINA fakePin
183#define PORTA fakePort
184#define DDRA fakeDDR
185
186#define PINB fakePin
187#define PORTB fakePort
188#define DDRB fakeDDR
189
190#define PINC fakePin
191#define PORTC fakePort
192#define DDRC fakeDDR
193
194#define PIND fakePin
195#define PORTD fakePort
196#define DDRD fakeDDR
197
198#define PINE fakePin
199#define PORTE fakePort
200#define DDRE fakeDDR
201
202#define PINF fakePin
203#define PORTF fakePort
204#define DDRF fakeDDR
205
206#define PING fakePin
207#define PORTG fakePort
208#define DDRG fakeDDR
209
210#define PINH fakePin
211#define PORTH fakePort
212#define DDRH fakeDDR
213
214#define PINJ fakePin
215#define PORTJ fakePort
216#define DDRJ fakeDDR
217
218#define PINK fakePin
219#define PORTK fakePort
220#define DDRK fakeDDR
221
222#define PINL fakePin
223#define PORTL fakePort
224#define DDRL fakeDDR
225
226#endif
227
228#define STRINGIFY(x) #x
229#define STRINGY2(x) STRINGIFY(x)
230
231#define IF_EN3(X, Y) (STRINGIFY(X) == STRINGIFY(Y))
232#define IF_EN2(X, Y) (STRINGIFY(X) == STRINGIFY(Y))
233#define IF_EN(X, Y) (X==Y)
234
235#if !defined(CONCAT)
236# define CONCAT(X, Y) X ## Y
237#endif
238
239#define GET_HW_VER(X, Y) ((Y == 0) ? X Custom : CONCAT(X V, Y))
240#define OSCR_HARDWARE_VERSION GET_HW_VER(HW::, HW_VERSION)
241#define OSCR_FEATURE_FLAG(EN, FEAT) (EN ? (1 << static_cast<uint32_t>(FEAT)) : 0)
242
243#define _GET_MACRO_0_1(_0, _1, NAME, ...) NAME
244#define _GET_MACRO_0_2(_0, _1, _2, NAME, ...) NAME
245#define _GET_MACRO_0_3(_0, _1, _2, _3, NAME, ...) NAME
246#define _GET_MACRO_0_4(_0, _1, _2, _3, _4, NAME, ...) NAME
247#define _GET_MACRO_0_5(_0, _1, _2, _3, _4, _5, NAME, ...) NAME
248
249#define GET_MACRO_0_1(MNAME, ...) _GET_MACRO_0_1(_0, ##__VA_ARGS__, MNAME ## 1, MNAME ## 0)(__VA_ARGS__)
250#define GET_MACRO_0_2(MNAME, ...) _GET_MACRO_0_2(_0, ##__VA_ARGS__, MNAME ## 2, MNAME ## 1, MNAME ## 0)(__VA_ARGS__)
251#define GET_MACRO_0_3(MNAME, ...) _GET_MACRO_0_3(_0, ##__VA_ARGS__, MNAME ## 3, MNAME ## 2, MNAME ## 1, MNAME ## 0)(__VA_ARGS__)
252#define GET_MACRO_0_4(MNAME, ...) _GET_MACRO_0_4(_0, ##__VA_ARGS__, MNAME ## 4, MNAME ## 3, MNAME ## 2, MNAME ## 1, MNAME ## 0)(__VA_ARGS__)
253#define GET_MACRO_0_5(MNAME, ...) _GET_MACRO_0_5(_0, ##__VA_ARGS__, MNAME ## 5, MNAME ## 4, MNAME ## 3, MNAME ## 2, MNAME ## 1, MNAME ## 0)(__VA_ARGS__)
254
255#define GET_MACRO_1_2(MNAME, ...) _GET_MACRO_0_1(##__VA_ARGS__, MNAME ## 2, MNAME ## 1)(__VA_ARGS__)
256#define GET_MACRO_1_3(MNAME, ...) _GET_MACRO_0_2(##__VA_ARGS__, MNAME ## 3, MNAME ## 2, MNAME ## 1)(__VA_ARGS__)
257#define GET_MACRO_1_4(MNAME, ...) _GET_MACRO_0_3(##__VA_ARGS__, MNAME ## 4, MNAME ## 3, MNAME ## 2, MNAME ## 1)(__VA_ARGS__)
258#define GET_MACRO_1_5(MNAME, ...) _GET_MACRO_0_4(##__VA_ARGS__, MNAME ## 5, MNAME ## 4, MNAME ## 3, MNAME ## 2, MNAME ## 1)(__VA_ARGS__)
259
260#define GET_MACRO_2_3(MNAME, ...) _GET_MACRO_0_2(##__VA_ARGS__, MNAME ## 3, MNAME ## 2)(__VA_ARGS__)
261#define GET_MACRO_2_4(MNAME, ...) _GET_MACRO_0_3(##__VA_ARGS__, MNAME ## 4, MNAME ## 3, MNAME ## 2)(__VA_ARGS__)
262#define GET_MACRO_2_5(MNAME, ...) _GET_MACRO_0_4(##__VA_ARGS__, MNAME ## 5, MNAME ## 4, MNAME ## 3, MNAME ## 2)(__VA_ARGS__)
263
264#define GET_MACRO_3_4(MNAME, ...) _GET_MACRO_0_3(##__VA_ARGS__, MNAME ## 4, MNAME ## 3)(__VA_ARGS__)
265#define GET_MACRO_3_5(MNAME, ...) _GET_MACRO_0_4(##__VA_ARGS__, MNAME ## 5, MNAME ## 4, MNAME ## 3)(__VA_ARGS__)
266
267#define GET_MACRO_4_5(MNAME, ...) _GET_MACRO_0_4(##__VA_ARGS__, MNAME ## 5, MNAME ## 4)(__VA_ARGS__)
268
269#define _ISEMPTY(x) (((x ## 0) == 0) && ((1 ## x) == 1))
270#define ISEMPTY(x) _ISEMPTY(x)
271
272#define assertIsNumber(x, msg) static_assert(OSCR::Util::is_integer<decltype(x)>::value, msg);
273
277#define TRAIT_DEF(NAME, ...) template<__VA_ARGS__> struct NAME : __spec_disabled
278
282#define TRAIT_COND(NAME, ...) template< > struct NAME<__VA_ARGS__>: __spec_enabled
283
287#define TRAIT_HELPER_VAL(NAME) template <class T> inline constexpr bool NAME ## _v = NAME<T>::value
288#define TRAIT_HELPER_TYPE(NAME) template <class T> using if_ ## NAME ## _t = enable_if_t<NAME<T>::value, bool>
289#define TRAIT_HELPERS(NAME) TRAIT_HELPER_VAL(NAME); TRAIT_HELPER_TYPE(NAME)
290
291#define CONV_DEF(NAME, ...) template <class T, __VA_ARGS__> struct NAME { typedef T type; }
292#define CONV_TYPE(NAME, TYPE, ...) template <> struct NAME <__VA_ARGS__> { typedef TYPE type; }
293#define CONV_HELPER(NAME) template <class T> using NAME ## _t = typename NAME<T>::type
294
295#define sizeofarray(a) sizeof(a)/sizeof(a[0])
296#define BUFFN(buff) buff, (size_t)sizeof(buff)
297
298#define BANNEDF(FUNC) sorry_##FUNC##_is_a_banned_function
299#define BANNEDV(VAR) sorry_##VAR##_is_a_banned_variable
300
304#if !defined(CRC)
305 #define CRC BANNEDV(CRC);
306#endif
307
311#if !defined(RTC)
312 #define RTC BANNEDV(RTC);
313#endif
314
315#include "arch/macros.h"
316
317#endif /* OSCR_MACROS_H_ */
Target-specific macros.