-
Notifications
You must be signed in to change notification settings - Fork 0
/
gba.h
152 lines (124 loc) · 5.03 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
#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 WHITE COLOR(31, 31, 31)
#define RED COLOR(31, 0, 0)
#define GREEN COLOR(0, 31, 0)
#define BLUE COLOR(0, 0, 31)
#define MAGENTA COLOR(31, 0, 31)
#define CYAN COLOR(0, 31, 31)
#define YELLOW COLOR(31, 31, 0)
#define BLACK COLOR(0, 0, 0)
#define GRAY COLOR(5, 5, 5)
#define LIGHTBLUE COLOR(21, 22, 31)
#define LIGHTGREEN COLOR(22, 31, 22)
// 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 BUTTONS (*(volatile u32 *) 0x4000130)
#define KEY_DOWN(key, buttons) (~(buttons) & (key))
// TODO: COMPLETE THIS MACRO.
// 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.
#define KEY_JUST_PRESSED(key, buttons, oldbuttons) (KEY_DOWN(key, buttons) & KEY_DOWN(key, oldbuttons))
// ---------------------------------------------------------------------------
// 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);
// ---------------------------------------------------------------------------
// 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 drawFullScreenImageDMA(const u16 *image);
void drawImageDMA(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