summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 88f4f4e29c6f3fa44b87eba8a7eff146710cca42 (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
#include "shorthand.h"

//
// 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.
//

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

// 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_BG0 0x0100
#define DISP_BG1 0x0200
#define DISP_BG2 0x0400
#define DISP_BG3 0x0800
#define DISP_OBJ 0x1000

static inline void
set_display_mode(u16 value) {
    *((volatile u32*)(MEM_IO + 0x0000)) = value;
}

//
// Colors.
//

// 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;

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

static inline void
put_pixel(int x, int y, Color clr) {
    ((volatile u16*)MEM_VRAM)[x + y * SCREEN_WIDTH] = clr;
}

//
// Main functions.
//

int main(void) {
    set_display_mode(DISP_MODE_3 | DISP_BG2);

    put_pixel(120 , 80, rgb15(31, 0, 0));
    put_pixel(136 , 80, rgb15(0, 31, 0));
    put_pixel(120 , 96, rgb15(0, 0, 31));

    while(true);

    return 0;
}