Alchemy
Loading...
Searching...
No Matches

io_reg.h

Go to the documentation of this file.
1#ifndef ALCHEMY_IO_REG_H
2#define ALCHEMY_IO_REG_H
3
4/// @file
5/// The hardware's I/O registers and their bits, as pret names them.
6
7#include "types.h"
8
9#define REG_DISPCNT (*(volatile u16 *)0x04000000)
10#define REG_DISPSTAT (*(volatile u16 *)0x04000004)
11// DISPSTAT's interrupt enables, as pret names them; each is
12// DISPSTAT_VBLANK_INTR shifted by its IRQ_ number.
13#define DISPSTAT_VBLANK_INTR 0x0008
14#define DISPSTAT_HBLANK_INTR 0x0010
15#define DISPSTAT_VCOUNT_INTR 0x0020
16/// DISPSTAT's high byte: the line the VCount interrupt fires on (the LYC
17/// compare).
18#define DISPSTAT_VCOUNT_LINE(line) ((line) << 8)
19#define REG_VCOUNT (*(volatile u16 *)0x04000006)
20#define REG_BG1CNT (*(volatile u16 *)0x0400000a)
21#define REG_BG2CNT (*(volatile u16 *)0x0400000c)
22#define REG_BG3CNT (*(volatile u16 *)0x0400000e)
23
24#define BGCNT_PRIORITY(n) (n)
25#define BGCNT_CHAR_BASE(block) ((block) << 2)
26#define BGCNT_MOSAIC 0x0040
27#define BGCNT_256_COLORS 0x0080
28#define BGCNT_SCREEN_BASE(block) ((block) << 8)
29/// Affine backgrounds only: wrap around instead of showing the backdrop.
30#define BGCNT_WRAP 0x2000
31/// The background's size: for an affine one, 0 to 3 are 128, 256, 512 and 1024
32/// pixels square.
33#define BGCNT_SIZE(n) ((n) << 14)
34
36 DISPCNT_MODE_0 = 0x0000, ///< four text backgrounds
37 DISPCNT_MODE_1 = 0x0001, ///< BG0 and BG1 text, BG2 affine
38 DISPCNT_MODE_2 = 0x0002, ///< BG2 and BG3 affine
48};
49#define REG_BG1HOFS (*(volatile u16 *)0x04000014)
50#define REG_BG1VOFS (*(volatile u16 *)0x04000016)
51#define REG_BG2HOFS (*(volatile u16 *)0x04000018)
52#define REG_BG2VOFS (*(volatile u16 *)0x0400001a)
53#define REG_BG3HOFS (*(volatile u16 *)0x0400001c)
54#define REG_BG3VOFS (*(volatile u16 *)0x0400001e)
55
56/// The BG2/BG3 affine matrix entries are signed 8.8 fixed point: texels
57/// stepped per screen pixel. Q_8_8(1) draws the layer at its own size and
58/// Q_8_8(0.5) twice as large.
59#define Q_8_8(n) ((s16)((n) * 256))
60
61#define REG_BG2PA (*(volatile u16 *)0x04000020)
62#define REG_BG2PB (*(volatile u16 *)0x04000022)
63#define REG_BG2PC (*(volatile u16 *)0x04000024)
64#define REG_BG2PD (*(volatile u16 *)0x04000026)
65#define REG_BG2X (*(volatile s32 *)0x04000028)
66#define REG_BG2Y (*(volatile s32 *)0x0400002c)
67#define REG_BG3PA (*(volatile u16 *)0x04000030)
68#define REG_BG3PB (*(volatile u16 *)0x04000032)
69#define REG_BG3PC (*(volatile u16 *)0x04000034)
70#define REG_BG3PD (*(volatile u16 *)0x04000036)
71#define REG_BG3X (*(volatile s32 *)0x04000038)
72#define REG_BG3Y (*(volatile s32 *)0x0400003c)
73
74#define REG_WIN0H (*(volatile u16 *)0x04000040)
75#define REG_WIN1H (*(volatile u16 *)0x04000042)
76#define REG_WIN0V (*(volatile u16 *)0x04000044)
77#define REG_WIN1V (*(volatile u16 *)0x04000046)
78/// A REG_WINxH or REG_WINxV value: the window covers start up to, but not
79/// including, end.
80#define WIN_RANGE(start, end) (((start) << 8) | (end))
81#define REG_WININ (*(volatile u16 *)0x04000048)
82#define REG_WINOUT (*(volatile u16 *)0x0400004a)
83
84// REG_WININ: the layers shown inside window 0 (low byte) and window 1 (high
85// byte). CLR lets the colour effect of REG_BLDCNT apply there.
86#define WININ_WIN0_BG0 0x0001
87#define WININ_WIN0_BG1 0x0002
88#define WININ_WIN0_BG2 0x0004
89#define WININ_WIN0_BG3 0x0008
90#define WININ_WIN0_OBJ 0x0010
91#define WININ_WIN0_CLR 0x0020
92#define WININ_WIN0_ALL 0x003f
93#define WININ_WIN1_BG0 0x0100
94#define WININ_WIN1_BG1 0x0200
95#define WININ_WIN1_BG2 0x0400
96#define WININ_WIN1_BG3 0x0800
97#define WININ_WIN1_OBJ 0x1000
98#define WININ_WIN1_CLR 0x2000
99#define WININ_WIN1_ALL 0x3f00
100
101// REG_WINOUT: the layers shown outside both windows (low byte) and inside the
102// sprite window (high byte).
103#define WINOUT_WIN01_BG0 0x0001
104#define WINOUT_WIN01_BG1 0x0002
105#define WINOUT_WIN01_BG2 0x0004
106#define WINOUT_WIN01_BG3 0x0008
107#define WINOUT_WIN01_OBJ 0x0010
108#define WINOUT_WIN01_CLR 0x0020
109#define WINOUT_WIN01_ALL 0x003f
110#define WINOUT_WINOBJ_BG0 0x0100
111#define WINOUT_WINOBJ_BG1 0x0200
112#define WINOUT_WINOBJ_BG2 0x0400
113#define WINOUT_WINOBJ_BG3 0x0800
114#define WINOUT_WINOBJ_OBJ 0x1000
115#define WINOUT_WINOBJ_CLR 0x2000
116#define WINOUT_WINOBJ_ALL 0x3f00
117
118#define REG_MOSAIC (*(volatile u16 *)0x0400004c)
119#define REG_BLDCNT (*(volatile u16 *)0x04000050)
120
121// REG_BLDCNT: the first target layers, the colour effect, then the second
122// target layers that a blend mixes the first ones with.
123#define BLDCNT_TGT1_BG0 0x0001
124#define BLDCNT_TGT1_BG1 0x0002
125#define BLDCNT_TGT1_BG2 0x0004
126#define BLDCNT_TGT1_BG3 0x0008
127#define BLDCNT_TGT1_OBJ 0x0010
128#define BLDCNT_TGT1_BD 0x0020
129#define BLDCNT_TGT1_ALL 0x003f
130#define BLDCNT_EFFECT_NONE 0x0000
131#define BLDCNT_EFFECT_BLEND 0x0040 ///< weighted by REG_BLDALPHA
132#define BLDCNT_EFFECT_LIGHTEN 0x0080 ///< towards white by REG_BLDY
133#define BLDCNT_EFFECT_DARKEN 0x00c0 ///< towards black by REG_BLDY
134#define BLDCNT_TGT2_BG0 0x0100
135#define BLDCNT_TGT2_BG1 0x0200
136#define BLDCNT_TGT2_BG2 0x0400
137#define BLDCNT_TGT2_BG3 0x0800
138#define BLDCNT_TGT2_OBJ 0x1000
139#define BLDCNT_TGT2_BD 0x2000
140#define BLDCNT_TGT2_ALL 0x3f00
141
142#define REG_BLDALPHA (*(volatile u16 *)0x04000052)
143/// A REG_BLDALPHA value: top and bottom are the two targets' weights, out
144/// of 16.
145#define BLEND_ALPHA(top, bottom) ((top) | ((bottom) << 8))
146#define REG_BLDY (*(volatile u16 *)0x04000054)
147
148#define BG_CANVAS_MAP ((volatile s16 *)0x06003800)
149#define REG_BG0HOFS ((void *)0x04000010)
150
151#define REG_DMA0 ((volatile u16 *)0x040000b0)
152
153#define REG_DMA3 ((volatile u32 *)0x040000d4)
154
155#define PLTT_BACKDROP (*(volatile u16 *)0x05000000)
156
157#define BG_PLTT ((volatile u16 *)0x05000000)
158#define BG_PLTT_COLOR(palette, color) (BG_PLTT[(palette) * 16 + (color)])
159#define OBJ_PLTT ((volatile u16 *)0x05000200)
160#define OBJ_PLTT_COLOR(palette, color) (OBJ_PLTT[(palette) * 16 + (color)])
161
162/// A 15-bit colour from its red, green and blue levels (0 to 31 each), as
163/// the palette stores it: blue in the high bits.
164#define RGB(r, g, b) (((b) << 10) | ((g) << 5) | (r))
165
166#define BG_CHAR_BLOCK(n) ((u8 *)0x06000000 + (n) * 0x4000)
167#define BG_SCREEN_BLOCK(n) ((u8 *)0x06000000 + (n) * 0x800)
168#define TILE_SIZE_4BPP 32
169/// Sprite tile memory, as pret names it: the 32 KB from 0x06010000.
170#define OBJ_VRAM0 0x06010000
171
172#define REG_TM3CNT (*(volatile u32 *)0x0400010c)
173#define REG_TM3CNT_H (*(volatile u16 *)0x0400010e)
174
175/// TMxCNT_H flags, as pret names them.
176#define TIMER_INTR_ENABLE 0x0040
177#define TIMER_ENABLE 0x0080
178
179#define REG_SIODATA32 ((volatile u32 *)0x04000120)
180#define REG_SIOCNT (*(volatile u32 *)0x04000128)
181#define REG_SIOCNT16 (*(volatile u16 *)0x04000128)
182#define REG_SIODATA8 (*(volatile u16 *)0x0400012a)
183#define REG_KEYINPUT (*(volatile u16 *)0x04000130)
184#define REG_KEYCNT (*(volatile u16 *)0x04000132)
185#define REG_RCNT (*(volatile u16 *)0x04000134)
186
187// SIOCNT flags, as pret names them.
188#define SIO_115200_BPS 0x0003
189#define SIO_START 0x0080
190#define SIO_32BIT_MODE 0x1000
191#define SIO_MULTI_MODE 0x2000
192#define SIO_INTR_ENABLE 0x4000
193
194/// RCNT's mode: 0 leaves the port to SIOCNT's serial modes; bit 15 alone
195/// makes it general-purpose pins.
196#define RCNT_SIO_MODE 0x0000
197#define RCNT_GPIO_MODE 0x8000
198
199// DMAxCNT_H flags, as pret names them. A 32-bit control word takes them
200// shifted above its count: (DMA_ENABLE | DMA_32BIT) << 16 | words.
201#define DMA_DEST_INC 0x0000
202#define DMA_DEST_DEC 0x0020
203#define DMA_DEST_FIXED 0x0040
204#define DMA_DEST_RELOAD 0x0060
205#define DMA_SRC_INC 0x0000
206#define DMA_SRC_DEC 0x0080
207#define DMA_SRC_FIXED 0x0100
208#define DMA_REPEAT 0x0200
209#define DMA_16BIT 0x0000
210#define DMA_32BIT 0x0400
211#define DMA_DREQ_ON 0x0800
212#define DMA_START_NOW 0x0000
213#define DMA_START_VBLANK 0x1000
214#define DMA_START_HBLANK 0x2000
215#define DMA_START_SPECIAL 0x3000
216#define DMA_INTR_ENABLE 0x4000
217#define DMA_ENABLE 0x8000
218/// A DMA control's start timing (bits 12 and 13), cartridge request (bit 11)
219/// and repeat (bit 9): the bits cleared before a channel is stopped.
220#define DMA_START_TIMING_DRQ_REPEAT \
221 (DMA_START_SPECIAL | DMA_DREQ_ON | DMA_REPEAT)
222
223// DMA control words: copy n words or halfwords at once, or one halfword or
224// word each HBlank to a register reloaded every line (a scanline table that
225// drives a window edge, a scroll or an affine register).
226#define DMA_COPY_WORDS(n) ((DMA_ENABLE | DMA_32BIT) << 16 | (n))
227#define DMA_COPY_HALFWORDS(n) (DMA_ENABLE << 16 | (n))
228#define DMA_HBLANK_HALFWORD_PER_LINE \
229 ((DMA_ENABLE | DMA_START_HBLANK | DMA_REPEAT | DMA_DEST_RELOAD) << 16 | 1)
230#define DMA_HBLANK_WORD_PER_LINE \
231 ((DMA_ENABLE | DMA_START_HBLANK | DMA_REPEAT | DMA_DEST_RELOAD | DMA_32BIT) \
232 << 16 | \
233 1)
234
235/// BIOS CpuSet control flags, above its count.
236#define CPU_SET_SRC_FIXED 0x01000000
237#define CPU_SET_32BIT 0x04000000
238
239#define OAM ((void *)0x07000000)
240/// OAM's size in bytes: 128 sprites of 8 bytes.
241#define OAM_SIZE 0x400
243 KEY_A = 0x001,
244 KEY_B = 0x002,
245 KEY_SELECT = 0x004,
246 KEY_START = 0x008,
247 KEY_RIGHT = 0x010,
248 KEY_LEFT = 0x020,
249 KEY_UP = 0x040,
250 KEY_DOWN = 0x080,
251 KEY_R = 0x100,
252 KEY_L = 0x200,
257};
258
259#define KEYS_MASK 0x3ff
260
261#define REG_BG0CNT (*(volatile u16 *)0x04000008)
262#define REG_BG0VOFS (*(volatile u16 *)0x04000012)
263#define REG_SOUNDCNT_H (*(volatile u16 *)0x04000082)
264// SOUNDCNT_H flags, as pret names them: the mixing volumes, then each direct
265// sound channel's speakers, timer and FIFO reset.
266#define SOUND_CGB_MIX_FULL 0x0002
267#define SOUND_A_MIX_FULL 0x0004
268#define SOUND_B_MIX_FULL 0x0008
269#define SOUND_ALL_MIX_FULL \
270 (SOUND_CGB_MIX_FULL | SOUND_A_MIX_FULL | SOUND_B_MIX_FULL)
271#define SOUND_A_RIGHT_OUTPUT 0x0100
272#define SOUND_A_LEFT_OUTPUT 0x0200
273#define SOUND_A_TIMER_0 0x0000
274#define SOUND_A_FIFO_RESET 0x0800
275#define SOUND_B_RIGHT_OUTPUT 0x1000
276#define SOUND_B_LEFT_OUTPUT 0x2000
277#define SOUND_B_TIMER_0 0x0000
278#define SOUND_B_FIFO_RESET 0x8000
279#define REG_SOUNDBIAS (*(volatile u16 *)0x04000088)
280#define REG_FIFO_A (*(volatile u32 *)0x040000a0)
281#define REG_FIFO_B (*(volatile u32 *)0x040000a4)
282#define REG_DMA0SAD (*(volatile u32 *)0x040000b0)
283#define REG_DMA0DAD (*(volatile u32 *)0x040000b4)
284#define REG_DMA0CNT (*(volatile u32 *)0x040000b8)
285#define REG_DMA0CNT_H (*(volatile u16 *)0x040000ba)
286#define REG_DMA1SAD (*(volatile u32 *)0x040000bc)
287#define REG_DMA1DAD (*(volatile u32 *)0x040000c0)
288#define REG_DMA1CNT (*(volatile u32 *)0x040000c4)
289#define REG_DMA1CNT_H (*(volatile u16 *)0x040000c6)
290#define REG_DMA2SAD (*(volatile u32 *)0x040000c8)
291#define REG_DMA2DAD (*(volatile u32 *)0x040000cc)
292#define REG_DMA2CNT (*(volatile u32 *)0x040000d0)
293#define REG_DMA2CNT_H (*(volatile u16 *)0x040000d2)
294#define REG_DMA3SAD (*(volatile u32 *)0x040000d4)
295#define REG_DMA3DAD (*(volatile u32 *)0x040000d8)
296#define REG_DMA3CNT (*(volatile u32 *)0x040000dc)
297#define REG_DMA3CNT_H (*(volatile u16 *)0x040000de)
298#define REG_TM0CNT_L (*(volatile u16 *)0x04000100)
299#define REG_TM0CNT_H (*(volatile u16 *)0x04000102)
300#define REG_TM1CNT_L (*(volatile u16 *)0x04000104)
301#define REG_TM1CNT_H (*(volatile u16 *)0x04000106)
302#define REG_TM2CNT_L (*(volatile u16 *)0x04000108)
303#define REG_TM2CNT_H (*(volatile u16 *)0x0400010a)
304
305#define REG_IE (*(volatile u16 *)0x04000200)
306#define REG_IF (*(volatile u16 *)0x04000202)
307#define REG_WAITCNT (*(volatile u16 *)0x04000204)
308#define REG_IME (*(volatile u16 *)0x04000208)
309
310// KEYCNT values: the interrupt fires when all the listed keys are held at once.
311#define KEYCNT_SOFT_RESET 0xc00f ///< A, B, Select and Start.
312/// Requires all ten keys together to prevent the reset chord retriggering it.
313#define KEYCNT_DISARMED 0xc3ff
314#define KEYCNT_SHOULDERS_WAKE 0xc300 ///< L and R, which wake the sleep.
315#define KEYCNT_SLEEP_WAKE 0xc304
316
317#define WAITCNT_GAME 0x4014
318
319#endif
DisplayControl
Definition io_reg.h:35
@ DISPCNT_OBJ_ON
Definition io_reg.h:44
@ DISPCNT_MODE_0
four text backgrounds
Definition io_reg.h:36
@ DISPCNT_WIN0_ON
Definition io_reg.h:45
@ DISPCNT_BG3_ON
Definition io_reg.h:43
@ DISPCNT_MODE_2
BG2 and BG3 affine.
Definition io_reg.h:38
@ DISPCNT_BG0_ON
Definition io_reg.h:40
@ DISPCNT_OBJ_1D_MAP
Definition io_reg.h:39
@ DISPCNT_OBJWIN_ON
Definition io_reg.h:47
@ DISPCNT_MODE_1
BG0 and BG1 text, BG2 affine.
Definition io_reg.h:37
@ DISPCNT_WIN1_ON
Definition io_reg.h:46
@ DISPCNT_BG2_ON
Definition io_reg.h:42
@ DISPCNT_BG1_ON
Definition io_reg.h:41
KeyMask
Definition io_reg.h:242
@ KEY_UP
Definition io_reg.h:249
@ KEY_B
Definition io_reg.h:244
@ KEY_SELECT
Definition io_reg.h:245
@ KEYS_DPAD
Definition io_reg.h:255
@ KEY_LEFT
Definition io_reg.h:248
@ KEY_START
Definition io_reg.h:246
@ KEY_RIGHT
Definition io_reg.h:247
@ KEYS_HORIZONTAL
Definition io_reg.h:253
@ KEYS_VERTICAL
Definition io_reg.h:254
@ KEY_R
Definition io_reg.h:251
@ KEY_A
Definition io_reg.h:243
@ KEY_DOWN
Definition io_reg.h:250
@ KEYS_SHOULDERS
Definition io_reg.h:256
@ KEY_L
Definition io_reg.h:252
The integer types, TRUE, UNUSED and the fixed-address macros.