From 951c7f3a146359b501abc712a09a8113cfd1a8a2 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 13 Apr 2021 18:28:56 +0200 Subject: 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. --- src/main.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/shorthand.h | 36 ++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/shorthand.h 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 @@ -int main() -{ - *(unsigned int*)0x04000000 = 0x0403; +#include "shorthand.h" - ((unsigned short*)0x06000000)[120+80*240] = 0x001F; - ((unsigned short*)0x06000000)[136+80*240] = 0x03E0; - ((unsigned short*)0x06000000)[120+96*240] = 0x7C00; +// +// Memory sections. +// - while(1); +// 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; } 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 @@ +#ifndef UTILS_SHORTHAND_H +#define UTILS_SHORTHAND_H + +#include +#include +#include +#include + +// +// This simple header just typedefs the basic C define types to a shorter name, +// loads the quality of life bool macro for _Bool and defines shorthand macros +// for byte sizes. + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; +typedef volatile u8 vu8; +typedef volatile u16 vu16; +typedef volatile u32 vu32; +typedef volatile u64 vu64; +typedef volatile s8 vs8; +typedef volatile s16 vs16; +typedef volatile s32 vs32; +typedef volatile s64 vs64; + +#define KB(N) ((u64)(N) * 1024) +#define MB(N) ((u64)KB(N) * 1024) +#define GB(N) ((u64)MB(N) * 1024) +#define TB(N) ((u64)GB(N) * 1024) + +#endif // UTILS_SHORTHAND_H -- cgit v1.2.1