all repos — mgba @ b6b10606763faceb96587161126f92f91e801c31

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#endif