summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 28239cc676191985938fe5b30133a5f56bae7b01 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "shorthand.h"
#include "bd-font.c"

//
// Memory sections.
//

// Defines for the different memory sections in the GBA.
#define MEM_SROM  0x00000000
#define MEM_EW    0x02000000
#define MEM_IW    0x03000000
#define MEM_IO    0x04000000
#define MEM_PAL   0x05000000
#define MEM_VRAM  0x06000000
#define MEM_OAM   0x07000000
#define MEM_PAK   0x08000000
#define MEM_CART  0x0E000000

//
// Display modes.
//

// Display registers.
#define DISP_CONTROL *((vu32*)(MEM_IO + 0x0000))
#define DISP_STATUS  *((vu32*)(MEM_IO + 0x0004))
#define DISP_VCOUNT  *((vu32*)(MEM_IO + 0x0006))

// Bits for display control.
#define DISP_CONTROL_PAGE (1 << 4)

// Display modes.
#define DISP_MODE_0 0x0000
#define DISP_MODE_1 0x0001
#define DISP_MODE_2 0x0002
#define DISP_MODE_3 0x0003
#define DISP_MODE_4 0x0004
#define DISP_MODE_5 0x0005

// Layers.
#define DISP_BG_0 0x0100
#define DISP_BG_1 0x0200
#define DISP_BG_2 0x0400
#define DISP_BG_3 0x0800
#define DISP_OBJ  0x1000

// Screen settings.
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 160

// The GBA in mode 3 expects rbg15 colors in the VRAM, where each component
// (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0).
typedef u16 Color;

// We can treat the screen as a HxW matrix. With the following macro we can
// write a pixel to the screen at the (x, y) position using:
//
//     FRAMEBUFFER[y][x] = color;
//
typedef Color Scanline[SCREEN_WIDTH];
#define FRAMEBUFFER ((Scanline*)MEM_VRAM)
#define SCREEN_BUFFER ((vu16*) MEM_VRAM)
#define PAL_BUFFER ((vu16*) MEM_PAL)

//
// Colors.
//

static inline Color
rgb15(u32 red, u32 green, u32 blue ) {
    return (blue << 10) | (green << 5) | red;
}

#define COLOR_RED   rgb15(31, 0, 12)
#define COLOR_BLUE  rgb15(2, 15, 30)
#define COLOR_CYAN  rgb15(0, 30, 30)
#define COLOR_GREY  rgb15(4, 4, 4)
#define COLOR_BLACK rgb15(0, 0, 0)
#define COLOR_WHITE rgb15(28, 28, 28)

// Using bd-font, an 8x8 bitmap font.
static void
put_char(int x, int y, Color clr, u8 chr) {
    for (size_t i = 0; i < 8; ++i) {
        for (size_t j = 0; j < 8; ++j) {
            if ((font[chr][i] >> (7 - j)) & 0x1) {
                FRAMEBUFFER[y + i][x + j] = clr;
            }
        }
    }
}

static void
put_text(int x, int y, Color clr, char *msg) {
    int count = 0;
    while (*msg) {
        put_char(x + count, y, clr, *msg++);
        count += 8;
    }
}

// Draws a line with the given color between (x0,y0) and (x1,y1) using the
// Bresenham's line drawing algorithm using exclusively integer arithmetic.
static void
draw_line(int x0, int y0, int x1, int y1, Color clr) {
    // Pointer to the initial position of the screen buffer where we will start
    // writing our data.
    vu16 *destination = (u16*)(SCREEN_BUFFER + y0 * SCREEN_WIDTH + x0);

    // Adjust the step direction and calculate deltas.
    int x_step;
    int y_step;
    int dx;
    int dy;
    if (x0 > x1) {
        x_step = -1;
        dx = x0 - x1;
    } else {
        x_step = 1;
        dx = x1 - x0;
    }
    if (y0 > y1) {
        y_step = -SCREEN_WIDTH;
        dy = y0 - y1;
    } else {
        y_step = +SCREEN_WIDTH;
        dy = y1 - y0;
    }

    if(dy == 0) {
        // Horizontal line.
        for(int i = 0; i <= dx; i++) {
            destination[i * x_step] = clr;
        }
    } else if(dx == 0) {
        // Vertical line.
        for(int i = 0; i <= dy; i++) {
            destination[i * y_step] = clr;
        }
    } else if (dx >= dy){
        // Positive slope.
        int diff = 2 * dy - dx;
        for (int i = 0; i <= dx; ++i) {
            *destination = clr;
            if (diff >= 0) {
                destination += y_step;
                diff -= 2 * dx;
            }
            destination += x_step;
            diff += 2 * dy;
        }
    } else {
        // Negative slope.
        int diff = 2 * dx - dy;
        for (int i = 0; i <= dy; ++i) {
            *destination = clr;
            if (diff >= 0) {
                destination += x_step;
                diff -= 2 * dy;
            }
            destination += y_step;
            diff += 2 * dx;
        }
    }
}

static inline void
draw_rect(int x0, int y0, int x1, int y1, Color clr) {
    if (x0 > x1) {
        int tmp = x0;
        x0 = x1;
        x1 = tmp;
    }
    if (y0 > y1) {
        int tmp = y0;
        y0 = y1;
        y1 = tmp;
    }
    int dx = x1 - x0;
    int dy = y1 - y0;
    for (int i = 0; i <= dx; ++i) {
        int x = x0 + i;
        FRAMEBUFFER[y0][x] = clr;
        FRAMEBUFFER[y1][x] = clr;
    }
    for (int j = 0; j <= dy; ++j) {
        int y = y0 + j;
        FRAMEBUFFER[y][x0] = clr;
        FRAMEBUFFER[y][x1] = clr;
    }
}

static inline void
draw_fill_rect(int x0, int y0, int x1, int y1, Color clr) {
    if (x0 > x1) {
        int tmp = x0;
        x0 = x1;
        x1 = tmp;
    }
    if (y0 > y1) {
        int tmp = y0;
        y0 = y1;
        y1 = tmp;
    }
    int dx = x1 - x0;
    int dy = y1 - y0;
    for (int i = 0; i <= dx; ++i) {
        for (int j = 0; j <= dy; ++j) {
            int x = x0 + i;
            int y = y0 + j;
            FRAMEBUFFER[y][x] = clr;
        }
    }
}

static inline void
wait_vsync() {
    while(DISP_VCOUNT >= 160);
    while(DISP_VCOUNT < 160);
}

//
// Main functions.
//

// In Mode4 the buffer is of 8 bytes per pixel instead of 16. We can't write the
// color directly, instead the color is stored in the palette memory at
// `MEM_PAL`. Note that in this mode MEM_PAL[0] is the background color. This
// plotter takes an index to a color stored in MEM_PAL[col_index]. Because the
// GBA needs to meet memory alignment requirements, we can't write a u8 into
// memory, instead we need to read a u16 word, mask and or the corresponding
// bits and wave the updated u16.
static void
put_pixel_m4(int x, int y, u8 col_index, vu16 *buffer) {
    int buffer_index = (y * SCREEN_WIDTH + x) / 2;
    vu16 *destination = &buffer[buffer_index];
    // Odd pixels will go to the top 8 bits of the destination. Even pixels to
    // the lower 8 bits.
    int odd = x & 0x1;
    if(odd) {
        *destination= (*destination & 0xFF) | (col_index << 8);
    } else {
        *destination= (*destination & ~0xFF) |  col_index;
    }
}

static void
draw_fill_rect_m4(int x0, int y0, int x1, int y1, u8 col_index, vu16 *buffer) {
    int ix, iy;
    for(iy = y0; iy < y1; iy++) {
        for(ix = x0; ix < x1; ix++) {
            put_pixel_m4(ix, iy, col_index, buffer);
        }
    }
}

static inline void
flip_page() {
    DISP_CONTROL ^= DISP_CONTROL_PAGE;
}

#define SCREEN_PAGE_1 ((vu16*) MEM_VRAM)
#define SCREEN_PAGE_2 ((vu16*) (MEM_VRAM + 0xa000))

//
// Profiling.
//

#define TIMER_DATA_0  *((vu16*) (0x04000100 + 0x04 * 0))
#define TIMER_DATA_1  *((vu16*) (0x04000100 + 0x04 * 1))
#define TIMER_DATA_2  *((vu16*) (0x04000100 + 0x04 * 2))
#define TIMER_DATA_3  *((vu16*) (0x04000100 + 0x04 * 3))
#define TIMER_CTRL_0  *((vu16*) (0x04000102 + 0x04 * 0))
#define TIMER_CTRL_1  *((vu16*) (0x04000102 + 0x04 * 1))
#define TIMER_CTRL_2  *((vu16*) (0x04000102 + 0x04 * 2))
#define TIMER_CTRL_3  *((vu16*) (0x04000102 + 0x04 * 3))

// Timer control bits.
#define TIMER_CTRL_FREQ_0   0
#define TIMER_CTRL_FREQ_1   1
#define TIMER_CTRL_FREQ_2   2
#define TIMER_CTRL_FREQ_3   3
#define TIMER_CTRL_CASCADE (1 << 2)
#define TIMER_CTRL_IRQ     (1 << 6)
#define TIMER_CTRL_ENABLE  (1 << 7)

// We use timers 2 and 3 to count the number of cycles since the profile_start
// functions is called. Don't use if the code we are trying to profile make use
// of these timers.
static inline
void profile_start() {
    TIMER_DATA_2 = 0;
    TIMER_DATA_3 = 0;
    TIMER_CTRL_2 = 0;
    TIMER_CTRL_3 = 0;
    TIMER_CTRL_3 = TIMER_CTRL_ENABLE | TIMER_CTRL_CASCADE;
    TIMER_CTRL_2 = TIMER_CTRL_ENABLE;
}

static inline
u32 profile_stop() {
   TIMER_CTRL_2 = 0;
   return (TIMER_DATA_3 << 16) | TIMER_DATA_2;
}

//
// Input handling.
//

// Memory address for key input register
#define KEY_INPUTS  *((vu16*) 0x04000130)

// Alias for key pressing bits.
#define KEY_A      (1 << 0)
#define KEY_B      (1 << 1)
#define KEY_SELECT (1 << 2)
#define KEY_START  (1 << 3)
#define KEY_RIGHT  (1 << 4)
#define KEY_LEFT   (1 << 5)
#define KEY_UP     (1 << 6)
#define KEY_DOWN   (1 << 7)
#define KEY_R      (1 << 8)
#define KEY_L      (1 << 9)

// Check if the given key/button is currently pressed.
#define KEY_PRESSED(key) (~(KEY_INPUTS) & key)

int main(void) {
    DISP_CONTROL = DISP_MODE_3 | DISP_BG_2;

    draw_fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_GREY);

    int frame_counter = 0;
    while(true) {
        wait_vsync();
        if (frame_counter++ > 30) {
            frame_counter = 0;
        }

        if (KEY_PRESSED(KEY_DOWN)) {
            put_text(8, 8, COLOR_RED, "down");
        }
        else if (KEY_PRESSED(KEY_UP)) {
            put_text(8, 8, COLOR_RED, "up");
        }
        else if (KEY_PRESSED(KEY_LEFT)) {
            put_text(8, 8, COLOR_RED, "left");
        }
        else if (KEY_PRESSED(KEY_RIGHT)) {
            put_text(8, 8, COLOR_RED, "right");
        }
        else if (KEY_PRESSED(KEY_A)) {
            put_text(8, 8, COLOR_RED, "A");
        }
        else if (KEY_PRESSED(KEY_B)) {
            put_text(8, 8, COLOR_RED, "B");
        }
        else if (KEY_PRESSED(KEY_START)) {
            put_text(8, 8, COLOR_RED, "start");
        }
        else if (KEY_PRESSED(KEY_SELECT)) {
            put_text(8, 8, COLOR_RED, "select");
        }
        else if (KEY_PRESSED(KEY_L)) {
            put_text(8, 8, COLOR_RED, "L");
        }
        else if (KEY_PRESSED(KEY_R)) {
            put_text(8, 8, COLOR_RED, "R");
        } else {
            draw_fill_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, COLOR_GREY);
        }
    };

    return 0;
}