summaryrefslogtreecommitdiffstats
path: root/src/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h67
1 files changed, 45 insertions, 22 deletions
diff --git a/src/common.h b/src/common.h
index 6047e5f..669fb6e 100644
--- a/src/common.h
+++ b/src/common.h
@@ -47,16 +47,10 @@
47#define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D 47#define DISP_ENABLE_SPRITES DISP_OBJ | DISP_OBJ_1D
48 48
49// Registers to control of BG layers. 49// Registers to control of BG layers.
50#define BG_CTRL_0 *((vu16*)(0x04000008 + 0x0002 * 0)) 50#define BG_CTRL(N) *((vu16*)(0x04000008 + 0x0002 * (N)))
51#define BG_CTRL_1 *((vu16*)(0x04000008 + 0x0002 * 1))
52#define BG_CTRL_2 *((vu16*)(0x04000008 + 0x0002 * 2))
53#define BG_CTRL_3 *((vu16*)(0x04000008 + 0x0002 * 3))
54 51
55// Bits to control the background. 52// Bits to control the background.
56#define BG_PRIORITY_0 0x0 53#define BG_PRIORITY(N) ((N) & 0x3)
57#define BG_PRIORITY_1 0x1
58#define BG_PRIORITY_2 0x2
59#define BG_PRIORITY_3 0x3
60#define BG_CHARBLOCK(N) ((N) << 2) 54#define BG_CHARBLOCK(N) ((N) << 2)
61#define BG_MOSAIC (1 << 6) 55#define BG_MOSAIC (1 << 6)
62#define BG_HIGH_COLOR (1 << 7) 56#define BG_HIGH_COLOR (1 << 7)
@@ -76,20 +70,6 @@
76#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2)) 70#define BG_V_SCROLL_2 *((vu16*)(0x04000012 + 0x0004 * 2))
77#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3)) 71#define BG_V_SCROLL_3 *((vu16*)(0x04000012 + 0x0004 * 3))
78 72
79// Screenblocks for BGs.
80typedef u16 ScreenBlock[1024];
81#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM)
82
83// Screenblock entry bits.
84#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA)
85#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB)
86#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC)
87
88size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) {
89 size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5));
90 return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32);
91}
92
93// Screen settings. 73// Screen settings.
94#define SCREEN_WIDTH 240 74#define SCREEN_WIDTH 240
95#define SCREEN_HEIGHT 160 75#define SCREEN_HEIGHT 160
@@ -113,6 +93,23 @@ typedef struct Tile {
113typedef Tile TileBlock[512]; 93typedef Tile TileBlock[512];
114#define TILE_MEM ((TileBlock*) MEM_VRAM) 94#define TILE_MEM ((TileBlock*) MEM_VRAM)
115 95
96// Screenblocks and charblocks for backgrounds.
97typedef u16 ScreenBlock[1024];
98typedef Tile CharBlock[512];
99#define CHARBLOCK_MEM ((CharBlock*)MEM_VRAM)
100#define SCREENBLOCK_MEM ((ScreenBlock*)MEM_VRAM)
101
102// Screenblock entry bits.
103#define SCREENBLOCK_ENTRY_H_FLIP (1 << 0xA)
104#define SCREENBLOCK_ENTRY_V_FLIP (1 << 0xB)
105#define SCREENBLOCK_ENTRY_PAL(N) ((N) << 0xC)
106
107size_t se_index(size_t tile_x, size_t tile_y, size_t map_width) {
108 size_t sbb = ((tile_x >> 5) + (tile_y >> 5) * (map_width >> 5));
109 return sbb * 1024 + ((tile_x & 31) + (tile_y & 31) * 32);
110}
111
112
116// We can treat the screen as a HxW matrix. With the following macro we can 113// We can treat the screen as a HxW matrix. With the following macro we can
117// write a pixel to the screen at the (x, y) position using: 114// write a pixel to the screen at the (x, y) position using:
118// 115//
@@ -293,4 +290,30 @@ key_hold(u32 key) {
293// Check if the given key/button is currently pressed. 290// Check if the given key/button is currently pressed.
294#define KEY_PRESSED(key) (~(KEY_INPUTS) & key) 291#define KEY_PRESSED(key) (~(KEY_INPUTS) & key)
295 292
293// Back/unpack bits.
294static inline u32
295unpack_1bb(u8 hex) {
296 const u32 conversion_u32[16] = {
297 0x00000000, 0x00000001, 0x00000010, 0x00000011,
298 0x00000100, 0x00000101, 0x00000110, 0x00000111,
299 0x00001000, 0x00001001, 0x00001010, 0x00001011,
300 0x00001100, 0x00001101, 0x00001110, 0x00001111,
301 };
302 u8 low = hex & 0xF;
303 u8 high = (hex >> 4) & 0xF;
304 return (conversion_u32[high] << 16) | conversion_u32[low];
305}
306
307// Unpack N tiles packed at 1bpp.
308unpack_tiles(u32 *src, u32 *dst, size_t n_tiles) {
309 u32 *target_src = src + n_tiles * 2;
310 while (src != target_src) {
311 *dst++ = unpack_1bb((*src >> 24) & 0xFF);
312 *dst++ = unpack_1bb((*src >> 16) & 0xFF);
313 *dst++ = unpack_1bb((*src >> 8) & 0xFF);
314 *dst++ = unpack_1bb(*src & 0xFF);
315 src++;
316 }
317}
318
296#endif // GBAEXP_COMMON_H 319#endif // GBAEXP_COMMON_H