/
myLib.h
executable file
·332 lines (255 loc) · 9.96 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#ifndef MY_LIB_H
#define MY_LIB_H
typedef unsigned short u16;
typedef unsigned int u32;
#define REG_DISPCTL *(unsigned short *)0x4000000
#define MODE0 0
#define MODE3 3
#define MODE4 4
#define BG2_ENABLE (1<<10)
#define ROWMASK 0xFF
#define COLMASK 0x1FF
// 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)
#define BACKBUFFER (1<<4)
#define SCANLINECOUNTER *(volatile u16 *)0x4000006
#define COLOR(r,g,b) ((r) | (g)<<5 | (b)<<10)
#define RED COLOR(31,0,0)
#define GREEN COLOR(0,31,0)
#define BLUE COLOR(0,0,31)
#define WHITE COLOR(31,31,31)
#define BLACK 0
#define SCREENWIDTH 240
#define OFFSET(r,c,rowlen) ((r)*(rowlen)+(c))
extern unsigned short *videoBuffer;
extern unsigned short *frontBuffer;
extern unsigned short *backBuffer;
// Prototypes
//Utility functions
int scale_up(int);
int scale_down(int);
// MODE 3 Drawing functions
void setPixel3(int row, int col, unsigned short color);
void drawRect3(int row, int col, int height, int width, unsigned short color);
void fillScreen3(unsigned short color);
void drawImage3(const unsigned short* image, int row, int col, int height, int width);
// MODE 4 Drawing functions
void setPixel4(int row, int col, unsigned char colorIndex);
void drawRect4(int row, int col, int height, int width, unsigned char colorIndex);
void fillScreen4(unsigned char color);
void drawBackgroundImage4(const unsigned short* image);
void drawImage4(const unsigned short* image, int row, int col, int height, int width);
void drawSubImage4(const unsigned short* sourceImage, int sourceRow, int sourceCol, int sourceWidth,
int row, int col, int height, int width);
void loadPalette(const unsigned short* palette);
void waitForVblank();
void flipPage();
// *** Input =========================================================
// Buttons
#define KEY_MASK 0x03FF
#define BUTTON_ADDRESS (*((unsigned short *) 0x4000130))
#define BUTTON_HELD(key) (~BUTTON_ADDRESS & (key))
#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)
inline void key_poll();
inline u32 key_curr_state();
inline u32 key_prev_state();
inline u32 key_is_down(u32 key);
inline u32 key_is_up(u32 key);
inline u32 key_was_down(u32 key);
inline u32 key_was_up(u32 key);
inline u32 key_held(u32 key);
inline u32 key_hit(u32 key);
inline u32 key_released(u32 key);
// *** DMA =========================================================
void DMANow(int channel, void* source, 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
typedef volatile struct
{
volatile const void *src;
volatile void *dst;
volatile unsigned int cnt;
} DMA;
extern DMA *dma;
#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)
typedef struct
{
int row;
int col;
int rdel;
int cdel;
int size;
u16 color;
int AI_STATE; // adding the AI_STATE to do the AI stuff in main.c
} MOVOBJ;
enum {IDLE, CHASE, FLEE};
// this enum is for our various AI_STATES so we can use a switch
// to change between them.
// Basically, IDLE = 0, CHASE = 1, FLEE = 2 as far as the compiler is concerned.
// Timers
// CONTROLLERS
#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
// Time factors to multiply clock ticks to convert to microsec (usec)
// The next line (uncommented) should be in myLib.c
// double timefactors[] = {0.059604, 3.811, 15.259, 59.382};
// extern double timefactors[];
// *** 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
#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
#define DSA_TIMER1 (1<<10)
#define DSA_FIFO_RESET (1<<11)
#define DSB_OUTPUT_RATIO_50 0
#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
#define DSB_TIMER1 (1<<14)
#define DSB_FIFO_RESET (1<<15)
// FIFO address defines
#define REG_FIFO_A 0x040000A0
#define REG_FIFO_B 0x040000A4
void setupSounds();
void playSoundA( const unsigned char* sound, int length, int frequency, int isLooping );
void playSoundB( const unsigned char* sound, int length, int frequency, int isLooping );
void setupInterrupts();
void interruptHandler();
// *** 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 (Never used it before so don't ask me -Peter)
#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. We can talk about it on thursday perhaps
#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 OAM ((OBJ_ATTR*)(0x7000000))
#define SPRITE_PALETTE ((unsigned short*)(0x5000200))
#define SPRITEOFFSET16(r,c) (r)*32+(c)
typedef struct{
unsigned short attr0;
unsigned short attr1;
unsigned short attr2;
unsigned short fill;
}OBJ_ATTR;
typedef struct {
int row;
int col;
} Sprite;
#endif