summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-04-13 18:28:56 +0200
committerBad Diode <bd@badd10de.dev>2021-04-13 18:28:56 +0200
commit951c7f3a146359b501abc712a09a8113cfd1a8a2 (patch)
tree0ba4d0f0154f27219bc1cb2cf387f186173cb01f
parent8535023423b9b21e362424820bb8564ff48e398e (diff)
downloadgba-experiments-951c7f3a146359b501abc712a09a8113cfd1a8a2.tar.gz
gba-experiments-951c7f3a146359b501abc712a09a8113cfd1a8a2.zip
Update initial example w/ utility macros/functions
The initial implementation was very obtuse. Using macros for different areas of GBA memory and some other utilities will make it much easier to understand.
-rw-r--r--src/main.c80
-rw-r--r--src/shorthand.h36
2 files changed, 109 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 655373b..88f4f4e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,12 +1,78 @@
1int main() 1#include "shorthand.h"
2{
3 *(unsigned int*)0x04000000 = 0x0403;
4 2
5 ((unsigned short*)0x06000000)[120+80*240] = 0x001F; 3//
6 ((unsigned short*)0x06000000)[136+80*240] = 0x03E0; 4// Memory sections.
7 ((unsigned short*)0x06000000)[120+96*240] = 0x7C00; 5//
8 6
9 while(1); 7// Defines for the different memory sections in the GBA.
8#define MEM_SROM 0x00000000
9#define MEM_EW 0x02000000
10#define MEM_IW 0x03000000
11#define MEM_IO 0x04000000
12#define MEM_PAL 0x05000000
13#define MEM_VRAM 0x06000000
14#define MEM_OAM 0x07000000
15#define MEM_PAK 0x08000000
16#define MEM_CART 0x0E000000
17
18//
19// Display modes.
20//
21
22// Screen settings.
23#define SCREEN_WIDTH 240
24#define SCREEN_HEIGHT 160
25
26// Display modes.
27#define DISP_MODE_0 0x0000
28#define DISP_MODE_1 0x0001
29#define DISP_MODE_2 0x0002
30#define DISP_MODE_3 0x0003
31#define DISP_MODE_4 0x0004
32#define DISP_MODE_5 0x0005
33
34// Layers.
35#define DISP_BG0 0x0100
36#define DISP_BG1 0x0200
37#define DISP_BG2 0x0400
38#define DISP_BG3 0x0800
39#define DISP_OBJ 0x1000
40
41static inline void
42set_display_mode(u16 value) {
43 *((volatile u32*)(MEM_IO + 0x0000)) = value;
44}
45
46//
47// Colors.
48//
49
50// The GBA in mode 3 expects rbg15 colors in the VRAM, where each component
51// (RGB) have a 0--31 range. For example, pure red would be rgb15(31, 0, 0).
52typedef u16 Color;
53
54static inline Color
55rgb15(u32 red, u32 green, u32 blue ) {
56 return (blue << 10) | (green << 5) | red;
57}
58
59static inline void
60put_pixel(int x, int y, Color clr) {
61 ((volatile u16*)MEM_VRAM)[x + y * SCREEN_WIDTH] = clr;
62}
63
64//
65// Main functions.
66//
67
68int main(void) {
69 set_display_mode(DISP_MODE_3 | DISP_BG2);
70
71 put_pixel(120 , 80, rgb15(31, 0, 0));
72 put_pixel(136 , 80, rgb15(0, 31, 0));
73 put_pixel(120 , 96, rgb15(0, 0, 31));
74
75 while(true);
10 76
11 return 0; 77 return 0;
12} 78}
diff --git a/src/shorthand.h b/src/shorthand.h
new file mode 100644
index 0000000..42eb935
--- /dev/null
+++ b/src/shorthand.h
@@ -0,0 +1,36 @@
1#ifndef UTILS_SHORTHAND_H
2#define UTILS_SHORTHAND_H
3
4#include <assert.h>
5#include <stdbool.h>
6#include <stddef.h>
7#include <stdint.h>
8
9//
10// This simple header just typedefs the basic C define types to a shorter name,
11// loads the quality of life bool macro for _Bool and defines shorthand macros
12// for byte sizes.
13
14typedef uint8_t u8;
15typedef uint16_t u16;
16typedef uint32_t u32;
17typedef uint64_t u64;
18typedef int8_t s8;
19typedef int16_t s16;
20typedef int32_t s32;
21typedef int64_t s64;
22typedef volatile u8 vu8;
23typedef volatile u16 vu16;
24typedef volatile u32 vu32;
25typedef volatile u64 vu64;
26typedef volatile s8 vs8;
27typedef volatile s16 vs16;
28typedef volatile s32 vs32;
29typedef volatile s64 vs64;
30
31#define KB(N) ((u64)(N) * 1024)
32#define MB(N) ((u64)KB(N) * 1024)
33#define GB(N) ((u64)MB(N) * 1024)
34#define TB(N) ((u64)GB(N) * 1024)
35
36#endif // UTILS_SHORTHAND_H