// SPDX-License-Identifier: MIT // Helpers for the picture scenes: register names (GBATEK §GBA I/O Map), and procedural tiles // and palettes so each scene is a few lines. Everything is written with 16- or 32-bit // stores (8-bit stores to VRAM and OAM are not plain writes: GBATEK §GBA Unpredictable // Things). #ifndef SCENE_H #define SCENE_H typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; typedef signed short s16; typedef signed int s32; #define REG16(a) (*(volatile u16 *)(a)) #define REG32(a) (*(volatile u32 *)(a)) #define DISPCNT REG16(0x04000000) #define GREENSWAP REG16(0x04000002) #define BGCNT(n) REG16(0x04000008 + 2 * (n)) #define BGHOFS(n) REG16(0x04000010 + 4 * (n)) #define BGVOFS(n) REG16(0x04000012 + 4 * (n)) #define BGPA(n) REG16(0x04000020 + 0x10 * ((n) - 2)) #define BGPB(n) REG16(0x04000022 + 0x10 * ((n) - 2)) #define BGPC(n) REG16(0x04000024 + 0x10 * ((n) - 2)) #define BGPD(n) REG16(0x04000026 + 0x10 * ((n) - 2)) #define BGX(n) REG32(0x04000028 + 0x10 * ((n) - 2)) #define BGY(n) REG32(0x0400002C + 0x10 * ((n) - 2)) #define WIN0H REG16(0x04000040) #define WIN1H REG16(0x04000042) #define WIN0V REG16(0x04000044) #define WIN1V REG16(0x04000046) #define WININ REG16(0x04000048) #define WINOUT REG16(0x0400004A) #define MOSAIC REG16(0x0400004C) #define BLDCNT REG16(0x04000050) #define BLDALPHA REG16(0x04000052) #define BLDY REG16(0x04000054) #define VRAM16 ((volatile u16 *)0x06000000) #define PAL16 ((volatile u16 *)0x05000000) #define OAM16 ((volatile u16 *)0x07000000) static inline u16 rgb(int r, int g, int b) { return (r & 31) | (g & 31) << 5 | (b & 31) << 10; } // Palette entry i of 256 (BG at 0, OBJ at 256): a spread of colours. static void palette_fill(int first, int count, int seed) { for (int i = 0; i < count; i++) { int k = i + seed; PAL16[first + i] = rgb(k * 7 + 3, k * 13 + 5, k * 3 + 11); } } // A 4-bit tile at a byte address in VRAM: dot (x, y) has colour f, 0 (transparent) where // the pattern leaves holes. static void tile4(u32 at, int n) { volatile u16 *p = (volatile u16 *)(0x06000000 + at); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x += 4) { u16 v = 0; for (int i = 0; i < 4; i++) { int c = ((x + i) + y + n) % 15 + 1; if ((((x + i) ^ y ^ n) & 7) == 0) c = 0; v |= c << (4 * i); } p[y * 2 + x / 4] = v; } } } // An 8-bit tile: dot (x, y) has colour 1-255, 0 in a few holes. static void tile8(u32 at, int n) { volatile u16 *p = (volatile u16 *)(0x06000000 + at); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x += 2) { int a = (x * 9 + y * 29 + n * 5) % 255 + 1; int b = ((x + 1) * 9 + y * 29 + n * 5) % 255 + 1; if (((x ^ y ^ n) & 7) == 1) a = 0; p[y * 4 + x / 2] = a | b << 8; } } } static void obj_set(int n, u16 a0, u16 a1, u16 a2) { OAM16[n * 4] = a0; OAM16[n * 4 + 1] = a1; OAM16[n * 4 + 2] = a2; } static void obj_affine(int group, s16 pa, s16 pb, s16 pc, s16 pd) { OAM16[group * 16 + 3] = pa; OAM16[group * 16 + 7] = pb; OAM16[group * 16 + 11] = pc; OAM16[group * 16 + 15] = pd; } // Hide all 128 OBJs (OBJ disable, GBATEK §LCD OBJ - OAM Attributes). static void obj_clear(void) { for (int n = 0; n < 128; n++) obj_set(n, 0x0200, 0, 0); } // A text map: entry (x, y) of a 32x32 block at a 2 KB screen base. static void map_text(int base, int blocks, int tiles, int flags) { volatile u16 *m = VRAM16 + base * 0x400; for (int i = 0; i < 0x400 * blocks; i++) { int t = (i * 7 + (i >> 5)) % tiles; u16 e = t; if (flags & 1) e |= ((i >> 1) & 3) << 10; // flips if (flags & 2) e |= ((i >> 2) % 16) << 12; // palettes m[i] = e; } } static void forever(void) { for (;;) __asm__ volatile("nop"); } #endif