+
+//
+// Endian related, for safe handling of endian-sensitive data
+// USAGE: GETBExx() is *always* an rvalue, a = pointer to a uint8_t,
+// r = offset from 0. SETBExx(), v = value to write into 'a'
+//
+#define GETBE16(a, r) \
+ (((uint16_t)(a)[(r + 0)] << 8) | ((uint16_t)(a)[(r + 1)]))
+
+#define GETBE32(a, r) \
+ (((uint32_t)(a)[(r + 0)] << 24) | ((uint32_t)(a)[(r + 1)] << 16) \
+ | ((uint32_t)(a)[(r + 2)] << 8) | ((uint32_t)(a)[(r + 3)]))
+
+#define GETBE64(a, r) \
+ (((uint64_t)(a)[(r + 0)] << 56) | ((uint64_t)(a)[(r + 1)] << 48) \
+ | ((uint64_t)(a)[(r + 2)] << 40) | ((uint64_t)(a)[(r + 3)] << 32) \
+ | ((uint64_t)(a)[(r + 4)] << 24) | ((uint64_t)(a)[(r + 5)] << 16) \
+ | ((uint64_t)(a)[(r + 6)] << 8) | ((uint64_t)(a)[(r + 7)]))
+
+#define SETBE16(a, r, v) \
+ { (a)[(r + 0)] = (uint8_t)((v) >> 8); \
+ (a)[(r + 1)] = (uint8_t)((v) & 0xFF); }
+
+#define SETBE32(a, r, v) \
+ { (a)[(r + 0)] = (uint8_t)((v) >> 24); \
+ (a)[(r + 1)] = (uint8_t)(((v) >> 16) & 0xFF); \
+ (a)[(r + 2)] = (uint8_t)(((v) >> 8) & 0xFF); \
+ (a)[(r + 3)] = (uint8_t)((v) & 0xFF); }
+
+#define SETBE64(a, r, v) \
+ { (a)[(r + 0)] = (uint8_t)((v) >> 56); \
+ (a)[(r + 1)] = (uint8_t)(((v) >> 48) & 0xFF); \
+ (a)[(r + 2)] = (uint8_t)(((v) >> 40) & 0xFF); \
+ (a)[(r + 3)] = (uint8_t)(((v) >> 32) & 0xFF); \
+ (a)[(r + 4)] = (uint8_t)(((v) >> 24) & 0xFF); \
+ (a)[(r + 5)] = (uint8_t)(((v) >> 16) & 0xFF); \
+ (a)[(r + 6)] = (uint8_t)(((v) >> 8) & 0xFF); \
+ (a)[(r + 7)] = (uint8_t)((v) & 0xFF); }
+
+// In 6502 mode, turns out we need this:
+#define SETLE16(a, r, v) \
+ { (a)[(r + 0)] = (uint8_t)((v) & 0xFF); \
+ (a)[(r + 1)] = (uint8_t)((v) >> 8); }
+
+// In DSP56001 mode, this is useful:
+#define SETBE24(a, v) \
+ { (a)[0] = (uint8_t)(((v) >> 16) & 0xFF); \
+ (a)[1] = (uint8_t)(((v) >> 8) & 0xFF); \
+ (a)[2] = (uint8_t)((v) & 0xFF); }
+
+// Byteswap crap
+#define BYTESWAP16(x) ((((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8))
+#define BYTESWAP32(x) ((((x) & 0x000000FF) << 24) | (((x) & 0x0000FF00) << 8) | (((x) & 0x00FF0000) >> 8) | (((x) & 0xFF000000) >> 24))
+#define BYTESWAP64(x) (BYTESWAP32(x >> 32) | (BYTESWAP32(x & 0xFFFFFFFF) << 32))
+#define WORDSWAP32(x) ((((x) & 0x0000FFFF) << 16) | (((x) & 0xFFFF0000) >> 16))
+