-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyLib.h
310 lines (244 loc) · 8.97 KB
/
myLib.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
extern unsigned short *videoBuffer;
// Useful addresses
#define REG_DISPCTL (*(u16 *)0x4000000)
#define SCANLINECOUNTER (*(volatile unsigned short *)0x4000006)
// Screen sizes
#define SCREENHEIGHT 160
#define SCREENWIDTH 240
#define ROWMASK (0xFF) //255
#define COLMASK (0x1FF) //511
// Generally useful symbols
#define FOREVER 1
#define TRUE 1
#define FALSE 0
#define BUFFER2 (1<<4)
#define BUFFER1FLAG 0x10
// Backgrounds
#define BG0_ENABLE (1<<8)
#define BG1_ENABLE (1<<9)
#define BG2_ENABLE (1<<10)
#define BG3_ENABLE (1<<11)
//background control registers
#define REG_BG0CNT *(volatile unsigned short*)0x4000008
#define REG_BG1CNT *(volatile unsigned short*)0x400000A
#define REG_BG2CNT *(volatile unsigned short*)0x400000C
#define REG_BG3CNT *(volatile unsigned short*)0x400000E
#define PALETTE ((u16 *)0x5000000)
// Video --Macros
#define MODE0 0
#define MODE3 3
#define MODE4 4
#define BG0_ENABLE (1<<8)
#define BG1_ENABLE (1<<9)
#define BG2_ENABLE (1<<10)
#define BG3_ENABLE (1<<11)
#define SCREENOFFSET(r,c) ((r)*240+(c))
#define OFFSET(r,c,rowlen) ((r)*(rowlen)+(c))
extern unsigned short *videoBuffer;
// Colors
#define COLOR(r,g,b) ((r) | (g)<<5 | (b)<<10)
#define WHITE COLOR(31,31,31)
#define BLACK 0
#define RED COLOR(31,0,0)
#define GREEN COLOR(0,31,0)
#define BLUE COLOR(0,0,31)
#define YELLOW COLOR(31,31,0)
#define CYAN COLOR(0,31,31)
#define MAGENTA COLOR(31,0,31)
#define GRAY COLOR(25, 25, 25)
// Prototypes
void drawRect4(int row, int col, int height, int width, unsigned char colorIndex);
void waitForVblank();
int sprintf (char *string, const char *form, ...);
void flipPage();
void loadPalette(volatile const unsigned short* palette);
void drawImage4(volatile const unsigned short* image, int row, int col, int height, int width);
void drawBackgroundImage4(volatile const unsigned short* image);
char *short2bin(short x, char arr[]);
// *** Input =========================================================
// Buttons
#define BUTTONS (*(volatile unsigned int *)0x04000130)
extern unsigned int oldButtons;
extern unsigned int buttons;
#define BUTTON_A 1
#define BUTTON_B 2
#define BUTTON_SELECT 4
#define BUTTON_START 8
#define BUTTON_RIGHT 16
#define BUTTON_LEFT 32
#define BUTTON_UP 64
#define BUTTON_DOWN 128
#define BUTTON_R 256
#define BUTTON_L 512
#define BUTTON_INDEX_A 0
#define BUTTON_INDEX_B 1
#define BUTTON_INDEX_SELECT 2
#define BUTTON_INDEX_START 3
#define BUTTON_INDEX_RIGHT 4
#define BUTTON_INDEX_LEFT 5
#define BUTTON_INDEX_UP 6
#define BUTTON_INDEX_DOWN 7
#define BUTTON_INDEX_R 8
#define BUTTON_INDEX_L 9
#define BUTTON_HELD(key) (~(BUTTONS) & key)
#define BUTTON_PRESSED(key) (!(~oldButtons&(key))&&(~buttons&(key)))
void DMANow(int channel, volatile const void* source, volatile const void* destination, unsigned int control);
// DMA channel 3 register definitions
#define REG_DMA3SAD *(volatile u32*)0x40000D4 // source address
#define REG_DMA3DAD *(volatile u32*)0x40000D8 // destination address
#define REG_DMA3CNT *(volatile u32*)0x40000DC // control register
// *** DMA =========================================================
typedef volatile struct {
volatile const void *src;
volatile const void *dst;
volatile unsigned int cnt;
} DMA;
extern DMA *dma;
// Defines
#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)
// *** Interrupts ====================================================
// CONTROLLER
#define REG_IME *(unsigned short*)0x4000208
// ENABLER
#define REG_IE *(unsigned short*)0x4000200
// FLAG
#define REG_IF *(volatile unsigned short*)0x4000202
//
#define REG_INTERRUPT *(unsigned int*)0x3007FFC
// DISPLAY STATUS
#define REG_DISPSTAT *(unsigned short*)0x4000004
//interrupt constants for turning them on
#define INT_VBLANK_ENABLE 1 << 3
//interrupt constants for checking which type of interrupt happened
#define INT_VBLANK 1 << 0
#define INT_TM1 1<<4
#define INT_BUTTON 1 << 12
// *** Sound =========================================================
#define REG_SOUNDCNT_X *(volatile u16 *)0x04000084
// register definitions
#define REG_SOUNDCNT_L *(u16*)0x04000080
#define REG_SOUNDCNT_H *(volatile u16*)0x04000082
// flags
#define SND_ENABLED (1<<7)
#define SND_OUTPUT_RATIO_25 0
#define SND_OUTPUT_RATIO_50 (1<<0)
#define SND_OUTPUT_RATIO_100 (1<<1)
#define DSA_OUTPUT_RATIO_50 (0<<2)
#define DSA_OUTPUT_RATIO_100 (1<<2)
#define DSA_OUTPUT_TO_RIGHT (1<<8)
#define DSA_OUTPUT_TO_LEFT (1<<9)
#define DSA_OUTPUT_TO_BOTH (3<<8)
#define DSA_TIMER0 (0<<10)
#define DSA_TIMER1 (1<<10)
#define DSA_FIFO_RESET (1<<11)
#define DSB_OUTPUT_RATIO_50 (0<<3)
#define DSB_OUTPUT_RATIO_100 (1<<3)
#define DSB_OUTPUT_TO_RIGHT (1<<12)
#define DSB_OUTPUT_TO_LEFT (1<<13)
#define DSB_OUTPUT_TO_BOTH (3<<12)
#define DSB_TIMER0 (0<<14)
#define DSB_TIMER1 (1<<14)
#define DSB_FIFO_RESET (1<<15)
// FIFO address defines
#define REG_FIFO_A (void*)0x040000A0
#define REG_FIFO_B (void*)0x040000A4
// *** Tiles =========================================================
typedef struct { u16 tileimg[8192]; } charblock;
typedef struct { u16 tilemap[1024]; } screenblock;
#define CHARBLOCKBASE ((charblock *)0x6000000)
#define SCREENBLOCKBASE ((screenblock *)0x6000000)
//background offset registers
#define REG_BG0HOFS *(volatile unsigned short *)0x04000010
#define REG_BG1HOFS *(volatile unsigned short *)0x04000014
#define REG_BG2HOFS *(volatile unsigned short *)0x04000018
#define REG_BG3HOFS *(volatile unsigned short *)0x0400001C
#define REG_BG0VOFS *(volatile unsigned short *)0x04000012
#define REG_BG1VOFS *(volatile unsigned short *)0x04000016
#define REG_BG2VOFS *(volatile unsigned short *)0x0400001A
#define REG_BG3VOFS *(volatile unsigned short *)0x0400001E
//macros and bit constants for setting the background control register specifics
#define SBB(num) num << 8
#define CBB(num) num << 2
#define COLOR256 1 << 7
#define BG_SIZE0 0<<14 // 32 x 32 tiles
#define BG_SIZE1 1<<14 // 64 x 32
#define BG_SIZE2 2<<14 // 32 x 64
#define BG_SIZE3 3<<14 // 64 x 64
// *** SPRITE =========================================================
#define SPRITE_MODE_1D (1 << 6) //put this into REG_DISPCNTL to enable 1D mapping mode
#define SPRITE_ENABLE (1 << 12) //put this into REG_DISPCNTL to enable sprites
#define ATTR0_REGULAR (0 << 8) //Normal Sprites
#define ATTR0_AFFINE (1 << 8) //Affine Sprites
#define ATTR0_HIDE (2 << 8) // Hide Sprites
#define ATTR0_DOUBLE_AFFINE (3 << 8) //Double Affine Sprites
#define ATTR0_NORMAL (0 << 10) //Normal Rendering
#define ATTR0_BLEND (1 << 10) //Enables Alpha Blending. Don't worry about it. Unless you want too =D
#define ATTR0_WIN (2 << 10) //Object Window mode. Again, no idea.
#define ATTR0_MOSAIC (1 << 12) //Enables the mosaic effect for this sprite. It's a cool visual effect.
#define ATTR0_4BPP (0 << 13) //16 color Sprites
#define ATTR0_8BPP (1 << 13) //256 color Sprites
#define ATTR0_SQUARE (0 << 14) //Square Shape
#define ATTR0_WIDE (1 << 14) // Wide Shape
#define ATTR0_TALL (2 << 14) //Tall Shape
#define ATTR1_HFLIP (1 << 12)
#define ATTR1_VFLIP (1 << 13)
#define ATTR1_SIZE8 (0 << 14)
#define ATTR1_SIZE16 (1 << 14)
#define ATTR1_SIZE32 (2 << 14)
#define ATTR1_SIZE64 (3 << 14)
#define ATTR2_PALBANK(i) ((i) << 12)
#define OAM ((OBJ_ATTR*)(0x7000000))
#define SPRITE_PALETTE ((unsigned short*)(0x5000200))
#define SPRITEOFFSET16(r,c) (r)*32+(c)
//for gravity
#define SHIFTUP(i) ((i) << 8)
#define SHIFTDOWN(i) ((i) >> 8)
typedef struct{
unsigned short attr0;
unsigned short attr1;
unsigned short attr2;
unsigned short fill;
}OBJ_ATTR;
typedef struct {
int row;
int col;
} Sprite;
// Interrupts
#define REG_TM0CNT *(volatile unsigned short*)0x4000102
#define REG_TM1CNT *(volatile unsigned short*)0x4000106
#define REG_TM2CNT *(volatile unsigned short*)0x400010A
#define REG_TM3CNT *(volatile unsigned short*)0x400010E
// TIMER VALUES
#define REG_TM0D *(volatile unsigned short*)0x4000100
#define REG_TM1D *(volatile unsigned short*)0x4000104
#define REG_TM2D *(volatile unsigned short*)0x4000108
#define REG_TM3D *(volatile unsigned short*)0x400010C
// Timer flags
#define TIMER_ON (1<<7)
#define TM_IRQ (1<<6)
#define TM_CASCADE (1<<2)
#define TM_FREQ_1 0
#define TM_FREQ_64 1
#define TM_FREQ_256 2
#define TM_FREQ_1024 3
// *** Sound =========================================================
#define PROCESSOR_CYCLES_PER_SECOND (16777216)
#define VBLANK_FREQ (59.727)