-
Notifications
You must be signed in to change notification settings - Fork 0
/
gba.h
188 lines (152 loc) · 6.29 KB
/
gba.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef GBA_H
#define GBA_H
// ---------------------------------------------------------------------------
// USEFUL TYPEDEFS
// ---------------------------------------------------------------------------
/* An unsigned 32-bit (4-byte) type */
typedef unsigned int u32;
/* An unsigned 16-bit (2-byte) type */
typedef unsigned short u16;
/* An unsigned 8-bit (1-byte) type. Note that this type cannot be written onto
* RAM directly. */
typedef unsigned char u8;
// ---------------------------------------------------------------------------
// MODE3 MACROS
// ---------------------------------------------------------------------------
#define OFFSET(row, col, width) ((col) + (width) * (row))
#define REG_DISPCNT (*(volatile unsigned short *)0x4000000)
#define MODE3 3
#define BG2_ENABLE (1 << 10)
#define COLOR(r, g, b) ((r) | (g) << 5 | (b) << 10)
#define COL_WHITE COLOR(31, 31, 31)
#define COL_RED COLOR(31, 0, 0)
#define COL_GREEN COLOR(0, 31, 0)
#define COL_BLUE COLOR(0, 0, 31)
#define COL_MAGENTA COLOR(31, 0, 31)
#define COL_CYAN COLOR(0, 31, 31)
#define COL_YELLOW COLOR(31, 31, 0)
#define COL_BLACK COLOR(0, 0, 0)
#define COL_GRAY COLOR(5, 5, 5)
// The size of the GBA Screen
#define WIDTH 240
#define HEIGHT 160
// This is initialized in gba.c
extern volatile unsigned short *videoBuffer;
// ---------------------------------------------------------------------------
// BUTTON INPUT
// ---------------------------------------------------------------------------
#define BUTTON_A (1 << 0)
#define BUTTON_B (1 << 1)
#define BUTTON_SELECT (1 << 2)
#define BUTTON_START (1 << 3)
#define BUTTON_RIGHT (1 << 4)
#define BUTTON_LEFT (1 << 5)
#define BUTTON_UP (1 << 6)
#define BUTTON_DOWN (1 << 7)
#define BUTTON_R (1 << 8)
#define BUTTON_L (1 << 9)
#define KEY_MASK 0x03FF
#define BUTTONS (*(volatile u32 *)0x4000130)
#define KEY_DOWN(key, buttons) (~(buttons) & (key))
// Remember that a button is recently pressed if it wasn't pressed in the last
// input (oldButtons) but is pressed in the current input. Use the KEY_DOWN
// macro to check if the button was pressed in the inputs.
// not used, using inline functions instead
#define KEY_JUST_PRESSED(key, buttons, oldbuttons) (buttons ^ oldbuttons) & key
extern u16 __key_curr, __key_prev;
static inline void key_poll(void) {
__key_prev = __key_curr;
__key_curr = (~BUTTONS) & KEY_MASK;
}
// Basic state checks
static inline u32 key_curr_state(void) { return __key_curr; }
static inline u32 key_prev_state(void) { return __key_prev; }
static inline u32 key_is_down(u32 key) { return __key_curr & key; }
static inline u32 key_is_up(u32 key) { return ~__key_curr & key; }
static inline u32 key_was_down(u32 key) { return __key_prev & key; }
static inline u32 key_was_up(u32 key) { return ~__key_prev & key; }
// Key is changing state.
static inline u32 key_transit(u32 key) {
return (__key_curr ^ __key_prev) & key;
}
// Key is held (down now and before).
static inline u32 key_held(u32 key) { return (__key_curr & __key_prev) & key; }
// Key is being hit (down now, but not before).
static inline u32 key_hit(u32 key) { return (__key_curr & ~__key_prev) & key; }
// Key is not held but was held before
static inline u32 key_released(u32 key) {
return (~__key_curr & __key_prev) & key;
}
// ---------------------------------------------------------------------------
// DMA
// ---------------------------------------------------------------------------
typedef struct {
const volatile void *src;
const volatile void *dst;
u32 cnt;
} DMA_CONTROLLER;
#define DMA ((volatile DMA_CONTROLLER *)0x040000B0)
// Defines
#define DMA_CHANNEL_0 0
#define DMA_CHANNEL_1 1
#define DMA_CHANNEL_2 2
#define DMA_CHANNEL_3 3
#define DMA_DESTINATION_INCREMENT (0 << 21)
#define DMA_DESTINATION_DECREMENT (1 << 21)
#define DMA_DESTINATION_FIXED (2 << 21)
#define DMA_DESTINATION_RESET (3 << 21)
#define DMA_SOURCE_INCREMENT (0 << 23)
#define DMA_SOURCE_DECREMENT (1 << 23)
#define DMA_SOURCE_FIXED (2 << 23)
#define DMA_REPEAT (1 << 25)
#define DMA_16 (0 << 26)
#define DMA_32 (1 << 26)
#define DMA_NOW (0 << 28)
#define DMA_AT_VBLANK (1 << 28)
#define DMA_AT_HBLANK (2 << 28)
#define DMA_AT_REFRESH (3 << 28)
#define DMA_IRQ (1 << 30)
#define DMA_ON (1 << 31)
// ---------------------------------------------------------------------------
// VBLANK
// ---------------------------------------------------------------------------
#define SCANLINECOUNTER (*(volatile unsigned short *)0x4000006)
// Use this variable to count vBlanks. Initialized in gba.c and to be
// manipulated by waitForVBlank()
extern u32 vBlankCounter;
/*
* Runs a blocking loop until the start of next VBlank.
*/
void waitForVBlank(void);
void mWaitForVBlank(void);
// ---------------------------------------------------------------------------
// MISC
// ---------------------------------------------------------------------------
#define UNUSED(param) ((void)((param)))
/*
* Generates a pseudo-random number between min and max.
*
* @param min bottom end of range (inclusive).
* @param max top end of range (exclusive).
* @return random number in the given range.
*/
int randint(int min, int max);
// ---------------------------------------------------------------------------
// DRAWING
// ---------------------------------------------------------------------------
void setPixel(int row, int col, u16 color);
void drawRectDMA(int row, int col, int width, int height, volatile u16 color);
void drawRectDMA32(int row, int col, int width, int height, volatile u16 color);
void drawFullScreenImageDMA(const u16 *image);
void drawImageDMA(int row, int col, int width, int height, const u16 *image);
void drawImageDMA32(int row, int col, int width, int height, const u16 *image);
void undrawImageDMA(int row, int col, int width, int height, const u16 *image);
void fillScreenDMA(volatile u16 color);
void drawChar(int row, int col, char ch, u16 color);
void drawString(int row, int col, char *str, u16 color);
void drawCenteredString(int row, int col, int width, int height, char *str,
u16 color);
/* Contains the pixels of each character from a 6x8 font */
// This is in the font.c file. You can replace the font if you want.
extern const unsigned char fontdata_6x8[12288];
#endif