forked from waffledood/ReverseSnake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gba.h
276 lines (251 loc) · 9.71 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
///////////////////////////////////////////////////////////////////////////////
// gba.h
///////////////////////////////////////////////////////////////////////////////
#ifndef GBA_H
#define GBA_H
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef unsigned char byte;
typedef unsigned short hword;
typedef unsigned int word;
// some defines
#define FALSE 0x00
#define TRUE 0x01
// screen defines
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 160
// memory register defines
#define OAM_MEM_START 0x7000000
#define VRAM_START 0x6000000
#define BG_RAM_START 0x6000000
#define OBJ_RAM_START 0x6010000
#define BG_PALETTE_MEM 0x5000000
#define OBJ_PALETTE_MEM 0x5000200
// buffer defines
#define FRONT_BUFFER (u16*)0x6000000
#define BACK_BUFFER (u16*)0x600A000
// DMA defines
#define REG_DMA0SAD *(u32*)0x40000B0
#define REG_DMA0DAD *(u32*)0x40000B4
#define REG_DMA0CNT *(u32*)0x40000B8
#define REG_DMA1SAD *(u32*)0x40000BC
#define REG_DMA1DAD *(u32*)0x40000C0
#define REG_DMA1CNT *(u32*)0x40000C4
#define REG_DMA2SAD *(u32*)0x40000C8
#define REG_DMA2DAD *(u32*)0x40000CC
#define REG_DMA2CNT *(u32*)0x40000D0
#define REG_DMA3SAD *(u32*)0x40000D4
#define REG_DMA3DAD *(u32*)0x40000D8
#define REG_DMA3CNT *(u32*)0x40000DC
#define WORD_DMA 0x04000000
#define HALF_WORD_DMA 0x00000000
#define ENABLE_DMA 0x80000000
#define START_ON_FIFO_EMPTY 0x30000000
#define DMA_REPEAT 0x02000000
#define DEST_REG_SAME 0x00400000
#define DMA_CHANNEL_0 0x0
#define DMA_CHANNEL_1 0x1
#define DMA_CHANNEL_2 0x2
#define DMA_CHANNEL_3 0x3
#define DMA_CHANNEL_DEFAULT 0x3
// background defines
#define REG_BG0CNT *(u16*)0x4000008
#define REG_BG1CNT *(u16*)0x400000A
#define REG_BG2CNT *(u16*)0x400000C
#define REG_BG3CNT *(u16*)0x400000E
#define REG_BG0HOFS *(u16*)0x4000010
#define REG_BG0VOFS *(u16*)0x4000012
#define REG_BG1HOFS *(u16*)0x4000014
#define REG_BG1VOFS *(u16*)0x4000016
#define REG_BG2HOFS *(u16*)0x4000018
#define REG_BG2VOFS *(u16*)0x400001A
#define REG_BG3HOFS *(u16*)0x400001C
#define REG_BG3VOFS *(u16*)0x400001E
#define REG_BG2PA *(u16*)0x4000020
#define REG_BG2PB *(u16*)0x4000022
#define REG_BG2PC *(u16*)0x4000024
#define REG_BG2PD *(u16*)0x4000026
#define REG_BG2X *(u32*)0x4000028
#define REG_BG2Y *(u32*)0x400002C
#define REG_BG3PA *(u16*)0x4000030
#define REG_BG3PB *(u16*)0x4000032
#define REG_BG3PC *(u16*)0x4000034
#define REG_BG3PD *(u16*)0x4000036
#define REG_BG3X *(u32*)0x4000038
#define REG_BG3Y *(u32*)0x400003C
// input defines
#define REG_KEYS *(volatile u16*)0x4000130
// mosaic defines
#define REG_MOSAIC *(u32*)0x400004C
#define REG_MOSAIC_L *(u32*)0x400004C
#define REG_MOSAIC_H *(u32*)0x400004E
// display mode defines
#define REG_DISPCNT *(u32*)0x4000000
#define REG_DISPCNT_L *(u16*)0x4000000
#define REG_DISPCNT_H *(u16*)0x4000002
#define REG_DISPSTAT *(u16*)0x4000004
// sound hardware defines
#define REG_SOUND1CNT_L *(u16*)0x04000060 // sound 1 sweep control
#define REG_SOUND1CNT_H *(u16*)0x04000062 // sound 1 length, wave duty and envelope control
#define REG_SOUND1CNT_X *(u16*)0x04000064 // sound 1 frequency, reset and loop control
#define REG_SOUND2CNT_L *(u16*)0x04000068 // sound 2 length, wave duty and envelope control
#define REG_SOUND2CNT_H *(u16*)0x0400006C // sound 2 frequency, reset and loop control
#define REG_SOUND3CNT_L *(u16*)0x04000070 // sound 3 enable and wave ram bank control
#define REG_SOUND3CNT_H *(u16*)0x04000072 // sound 3 sound length and output level control
#define REG_SOUND3CNT_X *(u16*)0x04000074 // sound 3 frequency, reset and loop control
#define REG_SOUND4CNT_L *(u16*)0x04000078 // sound 4 length, output level and envelope control
#define REG_SOUND4CNT_H *(u16*)0x0400007C // sound 4 noise parameters, reset and loop control
#define REG_SOUNDCNT_L *(u16*)0x04000080 // sound 1-4 output level and stereo control
#define REG_SOUNDCNT_H *(u16*)0x04000082 // direct sound control and sound 1-4 output ratio
#define REG_SOUNDCNT_X *(u32*)0x04000084 // master sound enable and sound 1-4 play status
#define REG_SOUNDBIAS *(u32*)0x04000088 // sound bias and amplitude resolution control
#define REG_WAVE_RAM0_L *(u16*)0x04000090 // sound 3 samples 0-3
#define REG_WAVE_RAM0_H *(u16*)0x04000092 // sound 3 samples 4-7
#define REG_WAVE_RAM1_L *(u16*)0x04000094 // sound 3 samples 8-11
#define REG_WAVE_RAM1_H *(u16*)0x04000096 // sound 3 samples 12-15
#define REG_WAVE_RAM2_L *(u16*)0x04000098 // sound 3 samples 16-19
#define REG_WAVE_RAM2_H *(u16*)0x0400009A // sound 3 samples 20-23
#define REG_WAVE_RAM3_L *(u16*)0x0400009C // sound 3 samples 23-27
#define REG_WAVE_RAM3_H *(u16*)0x0400009E // sound 3 samples 28-31
#define REG_FIFO_A 0x040000A0 // direct sound channel A samples 0-3
#define REG_FIFO_A_L 0x040000A0 // direct sound channel A samples 0-1
#define REG_FIFO_A_H 0x040000A2 // direct sound channel A samples 2-3
#define REG_FIFO_B 0x040000A4 // direct sound channel B samples 0-3
#define REG_FIFO_B_L 0x040000A4 // direct sound channel B samples 0-1
#define REG_FIFO_B_H 0x040000A6 // direct sound channel B samples 2-3
// Interrupt enabling
#define INT_VBLANK 0x1
#define INT_HBLANK 0x2
#define INT_VCOUNT 0x4
#define INT_TIMER0 0x8
#define INT_TIMER1 0x10
#define INT_TIMER2 0x20
#define INT_TIMER3 0x40
#define INT_COM 0x80
#define INT_DMA0 0x100
#define INT_DMA1 0x200
#define INT_DMA2 0x400
#define INT_DMA3 0x800
#define INT_BUTTON 0x1000
#define INT_CART 0x2000
// timer defines
#define REG_TM0D *(u16*)0x4000100
#define REG_TM0CNT *(u16*)0x4000102
#define REG_TM1D *(u16*)0x4000104
#define REG_TM1CNT *(u16*)0x4000106
#define REG_TM2D *(u16*)0x4000108
#define REG_TM2CNT *(u16*)0x400010A
#define REG_TM3D *(u16*)0x400010C
#define REG_TM3CNT *(u16*)0x400010E
#define TIMER_ENABLE 0x0080
#define TIMER_INTERRUPTS 0x0040
#define TIMER_FREQUENCY 0x0000
#define TIMER_FREQUENCY_64 0x0001
#define TIMER_FREQUENCY_256 0x0002
#define TIMER_FREQUENCY_1024 0x0003
// other defines
#define REG_VCOUNT *(u16*)0x4000006
#define REG_WIN0H *(u16*)0x4000040
#define REG_WIN1H *(u16*)0x4000042
#define REG_WIN0V *(u16*)0x4000044
#define REG_WIN1V *(u16*)0x4000046
#define REG_WININ *(u16*)0x4000048
#define REG_WINOUT *(u16*)0x400004A
#define REG_BLDMOD *(u16*)0x4000050
#define REG_COLEV *(u16*)0x4000052
#define REG_COLEY *(u16*)0x4000054
#define REG_SGFIF0A *(u32*)0x40000A0
#define REG_SCD0 *(u16*)0x4000120
#define REG_SCD1 *(u16*)0x4000122
#define REG_SCD2 *(u16*)0x4000124
#define REG_SCD3 *(u16*)0x4000126
#define REG_SCCNT *(u32*)0x4000128
#define REG_SCCNT_L *(u16*)0x4000128
#define REG_SCCNT_H *(u16*)0x400012A
#define REG_P1 *(u16*)0x4000130
#define REG_P1CNT *(u16*)0x4000132
#define REG_R *(u16*)0x4000134
#define REG_HS_CTRL *(u16*)0x4000140
#define REG_JOYRE *(u32*)0x4000150
#define REG_JOYRE_L *(u16*)0x4000150
#define REG_JOYRE_H *(u16*)0x4000152
#define REG_JOYTR *(u32*)0x4000154
#define REG_JOYTR_L *(u16*)0x4000154
#define REG_JOYTR_H *(u16*)0x4000156
#define REG_JSTAT *(u32*)0x4000158
#define REG_JSTAT_L *(u16*)0x4000158
#define REG_JSTAT_H *(u16*)0x400015A
#define REG_IE *(u16*)0x4000200
#define REG_IF *(volatile u16 *)0x4000202
#define REG_PAUSE *(u16*)0x4000300
// instruction prefetch enable register define
#define REG_WSCNT *(u16*)0x4000204
// interrupt register defines
#define REG_INTR_HANDLER *(u32*)0x3007FFC
#define REG_IME *(u16*)0x4000208
// interrupt handler enumeration
enum
{
VBLANK_HANDLER,
HBLANK_HANDLER,
VCOUNT_HANDLER,
TIMER0_HANDLER,
TIMER1_HANDLER,
TIMER2_HANDLER,
TIMER3_HANDLER,
SERIAL_COM_HANDLER,
DMA0_HANDLER,
DMA1_HANDLER,
DMA2_HANDLER,
DMA3_HANDLER,
KEYS_HANDLER,
CART_HANDLER,
MAX_INTERRUPT_HANDLERS,
};
// display (REG_DISPCNT) defines
#define BG0_ENABLE 0x100
#define BG1_ENABLE 0x200
#define BG2_ENABLE 0x400
#define BG3_ENABLE 0x800
#define OBJ_ENABLE 0x1000
#define WIN0_ENABLE 0x2000
#define WIN1_ENABLE 0x4000
#define WINOBJ_ENABLE 0x8000
#define BACK_BUFFER_FLAG 0x10
#define H_BLANK_OAM 0x20
#define OBJ_MAP_2D 0x0
#define OBJ_MAP_1D 0x40
#define FORCE_BLANK 0x80
// video modes
#define MODE0 0x0
#define MODE1 0x1
#define MODE2 0x2
#define MODE3 0x3
#define MODE4 0x4
#define MODE5 0x5
// input defines
#define KEY_MASK 0x3FF
#define KEY_A 0x001
#define KEY_B 0x002
#define KEY_SELECT 0x004
#define KEY_START 0x008
#define KEY_RIGHT 0x010
#define KEY_LEFT 0x020
#define KEY_UP 0x040
#define KEY_DOWN 0x080
#define KEY_R 0x100
#define KEY_L 0x200
// some structs
typedef void (*fp)(void);
// helper macros
#define RGB(r, g, b) ((r) + (g<<5) + (b<<10))
#define SCANLINE_COUNTER (*(volatile u16*)0x4000006)
#define INPUT (KEY_MASK & (~REG_KEYS))
#define spriteData ((unsigned short *) 0x6010000)
#define spritePal ((unsigned short *) 0x5000200)
#define REG_INT (*(unsigned int*)0x3007FFC)
#endif