all repos — mgba @ 015e6700d73b08dc4b57fd5d66ab0405f6481c98

mGBA Game Boy Advance Emulator

src/arm/common.h (view raw)

 1#ifndef COMMON_H
 2#define COMMON_H
 3
 4#include <ctype.h>
 5#include <fcntl.h>
 6#include <limits.h>
 7#include <math.h>
 8#include <stdarg.h>
 9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16
17#define UNUSED(V) (void)(V)
18
19#ifdef __BIG_ENDIAN__
20
21#if defined(__PPC__) || defined(__POWERPC__)
22#define SWAP_32(DEST, VAR) asm(\
23	"rlwinm %0, %1, 8, 24, 31\n" \
24	"rlwimi %0, %1, 24, 16, 23\n" \
25	"rlwimi %0, %1, 8, 8, 15\n" \
26	"rlwimi %0, %1, 24, 0, 7\n" \
27	: "+r"(DEST) : "r"(VAR))
28
29#define SWAP_16(DEST, VAR) asm(\
30	"rlwinm %0, %1, 24, 24, 31\n" \
31	"rlwimi %0, %1, 8, 16, 23\n" \
32	: "+r"(DEST) : "r"(VAR))
33#endif
34
35#define LOAD_32(DEST, ADDR, ARR) { \
36	uint32_t _tmp = ((uint32_t*) ARR)[(ADDR) >> 2]; \
37	SWAP_32(DEST, _tmp); \
38}
39
40#define LOAD_16(DEST, ADDR, ARR) { \
41	uint16_t _tmp = ((uint16_t*) ARR)[(ADDR) >> 1]; \
42	SWAP_16(DEST, _tmp); \
43}
44
45#define STORE_32(SRC, ADDR, ARR) { \
46	uint32_t _tmp; \
47	SWAP_32(_tmp, SRC); \
48	((uint32_t*) ARR)[(ADDR) >> 2] = _tmp; \
49}
50
51#define STORE_16(SRC, ADDR, ARR) { \
52	uint16_t _tmp; \
53	SWAP_16(_tmp, SRC); \
54	((uint16_t*) ARR)[(ADDR) >> 2] = _tmp; \
55}
56
57#else
58#define LOAD_32(DEST, ADDR, ARR) DEST = ((uint32_t*) ARR)[(ADDR) >> 2]
59#define LOAD_16(DEST, ADDR, ARR) DEST = ((uint16_t*) ARR)[(ADDR) >> 1]
60#define STORE_32(SRC, ADDR, ARR) ((uint32_t*) ARR)[(ADDR) >> 2] = SRC
61#define STORE_16(SRC, ADDR, ARR) ((uint16_t*) ARR)[(ADDR) >> 1] = SRC
62#endif
63
64#define MAKE_MASK(START, END) (((1 << ((END) - (START))) - 1) << (START))
65#define CHECK_BITS(SRC, START, END) ((SRC) & MAKE_MASK(START, END))
66#define EXT_BITS(SRC, START, END) (((SRC) >> (START)) & ((1 << ((END) - (START))) - 1))
67
68#define DECL_BITFIELD(NAME, TYPE) typedef TYPE NAME
69
70#define DECL_BITS(TYPE, FIELD, START, SIZE) \
71	static inline TYPE TYPE ## Is ## FIELD (TYPE src) { \
72		return CHECK_BITS(src, (START), (START) + (SIZE)); \
73	} \
74	static inline TYPE TYPE ## Get ## FIELD (TYPE src) { \
75		return EXT_BITS(src, (START), (START) + (SIZE)); \
76	}
77
78#define DECL_BIT(TYPE, FIELD, BIT) DECL_BITS(TYPE, FIELD, BIT, 1)
79#endif