1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//! Memory Map IO address for the GameBoy components.
//!
//! Documents are provided by [GB Pan Docs](https://gbdev.io/pandocs/Memory_Map.html)
use ;
/// [Joypad](https://gbdev.io/pandocs/Joypad_Input.html#ff00--p1joyp-joypad)
///
/// The eight GameBoy action/direction buttons are arranged as a 2x4 matrix.
/// Select either action or direction buttons by writing to this register, then read out the bits
/// 0-3.
///
/// <table class="bit-descrs"><thead><tr><th></th><th>7</th><th>6</th><th>5</th><th>4</th><th>3</th><th>2</th><th>1</th><th>0</th></tr></thead><tbody><tr><td><strong>P1</strong></td><td colspan="2" class="unused-field"></td><td colspan="1">Select buttons</td><td colspan="1">Select d-pad</td><td colspan="1">Start / Down</td><td colspan="1">Select / Up</td><td colspan="1">B / Left</td><td colspan="1">A / Right</td></tr></tbody></table>
///
/// * **Select buttons**: If this bit is `0`, then buttons (SsBA) can be read from the lower
/// nibble.
/// * **Select d-pad** : If this bit is `0`, then directional keys can be read from the lower
/// nibble.
/// * The lower nibble is *Read-only*. Note that, rather unconventionally for GameBoy, a button
/// being pressed is seen as the corresponding bit being `0`, not `1`.
///
/// If neigher buttons nor d-pad is selected (`$30` was written), then the low nibble reads `$F`
/// (all buttons released).
///
/// If both buttons and d-pad is selected, then the low nibble reads d-pad *or* button is pressed.
///
/// # Safety
/// After writing to [`JOYP`], if [`JOYP`] is read in the following instruction, the
/// unexpected value will be read. Therefore, if you want it to work as intended, you have
/// to give a brief delay.
pub const JOYP: =
unsafe ;
/// [Serial transfer data](https://gbdev.io/pandocs/Serial_Data_Transfer_(Link_Cable).html#ff01--sb-serial-transfer-data)
pub const SB: =
unsafe ;
/// [Serial transfer control](https://gbdev.io/pandocs/Serial_Data_Transfer_(Link_Cable).html#ff02--sc-serial-transfer-control)
pub const SC: =
unsafe ;
/// [Divider](https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff04--div-divider-register)
///
/// This register is incremented at a rate of 16384Hz (~16779Hz on SGB). Writing any value to this
/// register resets it to `$00`. Additionally, this register is reset when executing the `stop`
/// instruction, and only begins ticking again once `stop` mode ends. This also occurs during a
/// [speed switch](https://gbdev.io/pandocs/CGB_Registers.html#ff4d--key1-cgb-mode-only-prepare-speed-switch).
///
/// Note: This divider is affected by CGB double speed mode, and will increment at 32768Hz in
/// double speed.
pub const DIV: =
unsafe ;
/// [Timer counter](https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff05--tima-timer-counter)
///
/// This timer is incremented at the clock frequency specified by the [`TAC`] register.
/// When the value overflows, it is reset to the value specified in [`TMA`] and an interrupt is
/// requested.
pub const TIMA: =
unsafe ;
/// [Timer modulo](https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff06--tma-timer-modulo)
///
/// When TIMA overflows, it is reset to the value in this register and Timer interrupt is
/// requested. Example of use: if TMA is set to $FF, an interrupt is requested at the clock
/// frequency selected in [`TAC`] (because every increment is an overflow). However, if TMA is set
/// to $FE, an interrupt is only requested every two increments, which effectively divides the
/// selected clock by two. Setting TMA to $FD would divide the clock by three, and so on.
///
/// If a TMA write is executed on the same M-cycle as the content of [`TMA`] is transferred to
/// [`TIMA`] due to a timer overflow, the old value is transferred to [`TIMA`]
pub const TMA: =
unsafe ;
/// [Timer control](https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff07--tac-timer-control)
///
/// <table class="bit-descrs"><thead><tr><th></th><th>7</th><th>6</th><th>5</th><th>4</th><th>3</th><th>2</th><th>1</th><th>0</th></tr></thead><tbody><tr><td><strong>TAC</strong></td><td colspan="5" class="unused-field"></td><td colspan="1">Enable</td><td colspan="2">Clock select</td></tr></tbody></table>
///
/// - **Enable**: Controls whether `TIMA` is incremented.
/// Note that `DIV` is **always** counting, regardless of this bit.
/// - **Clock select**: Controls the frequency at which `TIMA` is incremented, as follows:
///
/// <div class="table-wrapper"><table>
/// <thead>
/// <tr><th rowspan=2>Clock select</th><th rowspan=2>Increment every</th><th colspan=3>Frequency (Hz)</th></tr>
/// <tr><th>DMG, SGB2, CGB in single-speed mode</th><th>SGB1</th><th>CGB in double-speed mode</th></tr>
/// </thead><tbody>
/// <tr><td>00</td><td>256 M-cycles </td><td> 4096</td><td> ~4194</td><td> 8192</td></tr>
/// <tr><td>01</td><td>4 M-cycles </td><td>262144</td><td>~268400</td><td>524288</td></tr>
/// <tr><td>10</td><td>16 M-cycles </td><td> 65536</td><td> ~67110</td><td>131072</td></tr>
/// <tr><td>11</td><td>64 M-cycles </td><td> 16384</td><td> ~16780</td><td> 32768</td></tr>
/// </tbody>
/// </table></div>
///
/// Note that writing to this register may increase [`TIMA`] once!
/// For more information, please refer [GB Pan Docs](https://gbdev.io/pandocs/Timer_and_Divider_Registers.html#ff07--tac-timer-control)
pub const TAC: =
unsafe ;
/// [Interrupt flag](https://gbdev.io/pandocs/Interrupts.html#ff0f--if-interrupt-flag)
///
/// <table class="bit-descrs"><thead><tr><th></th><th>7</th><th>6</th><th>5</th><th>4</th><th>3</th><th>2</th><th>1</th><th>0</th></tr></thead><tbody><tr><td><strong>IF</strong></td><td colspan="3" class="unused-field"></td><td colspan="1">Joypad</td><td colspan="1">Serial</td><td colspan="1">Timer</td><td colspan="1">LCD</td><td colspan="1">VBlank</td></tr></tbody></table>
///
/// * **VBlank** (Read/Write): Controls whether the VBlank interrupt handler is being requested.
/// * **LCD** (Read/Write) : Controls whether the LCD interrupt handler is being requested.
/// * **Timer** (Read/Write) : Controls whether the Timer interrupt handler is being requested.
/// * **Serial** (Read/Write) : Controls whether the Serial interrupt handler is being requested.
/// * **Joypad** (Read/Write) : Controls whether the Joypad interrupt handler is being requested.
///
///
/// When an interrupt request signal (some internal wire going from the PPU/APU/... to the CPU)
/// changes from low to high, the corresponding bit in the [`IF`] register becomes set. For
/// example, bit 0 becomes set when the PPU enters the VBlank period.
///
/// Any set bits in the [`IF`] register are only **requesting** an interrupt. The actual
/// **execution** of the interrupt handler happnes only if both the `IME` flag and the
/// corresponding bit in the [`IE`] register are set; otherwise the interrupt "waits" until
/// **both** `IME` and [`IE`] allow it to be serviced.
///
/// Since the CPU automatically sets and clears the bits in the [`IF`] register, it is usually not
/// necessary to write to the [`IF`] register. However, the user still do that in order to manually
/// request (or discard) interrupts. Just like real interrupts, a manually requeseted interrupt
/// isn't serviced unless/until `IME` and [`IE`] allow it.
pub const IF: =
unsafe ;
/// [Sound channel 1 sweep](https://gbdev.io/pandocs/Audio_Registers.html#ff10--nr10-channel-1-sweep)
pub const NR10: =
unsafe ;
/// [Sound channel 1 length timer & duty cycle](https://gbdev.io/pandocs/Audio_Registers.html#ff11--nr11-channel-1-length-timer--duty-cycle)
pub const NR11: =
unsafe ;
/// [Sound channel 1 volume & envelope](https://gbdev.io/pandocs/Audio_Registers.html#ff12--nr12-channel-1-volume--envelope)
pub const NR12: =
unsafe ;
/// [Sound channel 1 period low](https://gbdev.io/pandocs/Audio_Registers.html#ff13--nr13-channel-1-period-low-write-only)
pub const NR13: =
unsafe ;
/// [Sound channel 1 period high & control](https://gbdev.io/pandocs/Audio_Registers.html#ff14--nr14-channel-1-period-high--control)
pub const NR14: =
unsafe ;
/// [Sound channel 2 length timer & duty cycle](https://gbdev.io/pandocs/Audio_Registers.html#sound-channel-2--pulse)
pub const NR21: =
unsafe ;
/// [Sound channel 2 volume & envelope](https://gbdev.io/pandocs/Audio_Registers.html#sound-channel-2--pulse)
pub const NR22: =
unsafe ;
/// [Sound channel 2 period low](https://gbdev.io/pandocs/Audio_Registers.html#sound-channel-2--pulse)
pub const NR23: =
unsafe ;
/// [Sound channel 2 period high & control](https://gbdev.io/pandocs/Audio_Registers.html#sound-channel-2--pulse)
pub const NR24: =
unsafe ;
/// [Sound channel 3 DAC enable](https://gbdev.io/pandocs/Audio_Registers.html#ff1a--nr30-channel-3-dac-enable)
pub const NR30: =
unsafe ;
/// [Sound channel 3 length timer](https://gbdev.io/pandocs/Audio_Registers.html#ff1b--nr31-channel-3-length-timer-write-only)
pub const NR31: =
unsafe ;
/// [Sound channel 3 output level](https://gbdev.io/pandocs/Audio_Registers.html#ff1c--nr32-channel-3-output-level)
pub const NR32: =
unsafe ;
/// [Sound channel 3 period low](https://gbdev.io/pandocs/Audio_Registers.html#ff1d--nr33-channel-3-period-low-write-only)
pub const NR33: =
unsafe ;
/// [Sound channel 3 period high & control](https://gbdev.io/pandocs/Audio_Registers.html#ff1e--nr34-channel-3-period-high--control)
pub const NR34: =
unsafe ;
/// [Sound channel 4 length timer](https://gbdev.io/pandocs/Audio_Registers.html#ff20--nr41-channel-4-length-timer-write-only)
pub const NR41: =
unsafe ;
/// [Master volume & VIN panning](https://gbdev.io/pandocs/Audio_Registers.html#ff24--nr50-master-volume--vin-panning)
pub const NR50: =
unsafe ;
/// [Sound panning](https://gbdev.io/pandocs/Audio_Registers.html#ff25--nr51-sound-panning)
pub const NR51: =
unsafe ;
/// [Sound on/off](https://gbdev.io/pandocs/Audio_Registers.html#ff26--nr52-audio-master-control)
pub const NR52: =
unsafe ;
/// [Storage for one of the sound channel's waveform](https://gbdev.io/pandocs/Audio_Registers.html#ff30ff3f--wave-pattern-ram)
///
/// Wave RAM is 16 bytes long; each byte holds two "samples", each 4 bits.
///
/// As CH3 plays, it reads wave RAM left to right, upper nibble first. That is, `$FF30`'s upper
/// nibble, `$FF30`'s lower nibble, `$FF31`'s upper nibble, and so on.
///
/// # Warning
/// When CH3 is started, the first sample read is the one at index 1, i.e. the lower nibble of the
/// first byte, NOT the upper nibble.
///
/// # Safety
/// Accessing wave RAM while CH3 is **active** (i.e. playing) causes accesses to misbehave:
/// * On AGB, reads return `$FF`, and writes are ignored.
/// * On monochrome consoles, wave RAM can only be accessed on the same cycle that CH3 does.
/// Otherwise, reads return `$FF`, and writes are ignored.
/// * On other consoles, the byte accessed will be the on CH3 is currently reading; that is, if Ch3
/// is currently reading one of the first two samples, the CPU will really access `$FF30`,
/// regardless of the address being used.
///
/// Wave RAM can be accessed normally even if the DAC is on, as long as the channel is not active.
/// This is especially relevant on GBA whose mixer behaves as if DACs are always enabled.
///
/// The way it works is that wave RAM is a 16-byte memory buffer, and whie it's playing, CH3 has
/// priority over the CPU when choosing which of those 16 bytes is accessed. So, from the CPU's
/// point of view, wave RAM reads out the same byte, reagardless of the address.
pub const WAVE_RAM: =
unsafe ;
/// [LCD control](https://gbdev.io/pandocs/LCDC.html#ff40--lcdc-lcd-control)
pub const LCDC: =
unsafe ;
/// [LCD status](https://gbdev.io/pandocs/STAT.html#ff41--stat-lcd-status)
pub const STAT: =
unsafe ;
/// [Viewport Y position](https://gbdev.io/pandocs/Scrolling.html#ff42ff43--scy-scx-background-viewport-y-position-x-position)
pub const SCY: =
unsafe ;
/// [Viewport X position](https://gbdev.io/pandocs/Scrolling.html#ff42ff43--scy-scx-background-viewport-y-position-x-position)
pub const SCX: =
unsafe ;
/// [LCD Y coordinate](https://gbdev.io/pandocs/STAT.html#ff44--ly-lcd-y-coordinate-read-only)
pub const LY: =
unsafe ;
/// [LY compare](https://gbdev.io/pandocs/STAT.html#ff45--lyc-ly-compare)
pub const LYC: =
unsafe ;
/// [OAM DMA source address & start](https://gbdev.io/pandocs/OAM_DMA_Transfer.html#ff46--dma-oam-dma-source-address--start)
pub const DMA: =
unsafe ;
/// [BG palette data](https://gbdev.io/pandocs/Palettes.html#ff47--bgp-non-cgb-mode-only-bg-palette-data)
pub const BGP: =
unsafe ;
/// [OBJ palette 0 data](https://gbdev.io/pandocs/Palettes.html#ff48ff49--obp0-obp1-non-cgb-mode-only-obj-palette-0-1-data)
pub const OBP0: =
unsafe ;
/// [OBJ palette 1 data](https://gbdev.io/pandocs/Palettes.html#ff48ff49--obp0-obp1-non-cgb-mode-only-obj-palette-0-1-data)
pub const OBP1: =
unsafe ;
/// [Window Y position](https://gbdev.io/pandocs/Scrolling.html#ff4aff4b--wy-wx-window-y-position-x-position-plus-7)
pub const WY: =
unsafe ;
/// [Window X position plus 7](https://gbdev.io/pandocs/Scrolling.html#ff4aff4b--wy-wx-window-y-position-x-position-plus-7)
pub const WX: =
unsafe ;
/// [Prepare speed switch](https://gbdev.io/pandocs/CGB_Registers.html#ff4d--key1-cgb-mode-only-prepare-speed-switch)
pub const KEY1: =
unsafe ;
/// [VRAM bank](https://gbdev.io/pandocs/CGB_Registers.html#ff4f--vbk-cgb-mode-only-vram-bank)
///
/// This register can be written to change VRAM banks. Only bit 0 matters, all other bits are
/// ignored.
pub const VBK: =
unsafe ;
/// [VRAM DMA source high](https://gbdev.io/pandocs/CGB_Registers.html#ff51ff52--hdma1-hdma2-cgb-mode-only-vram-dma-source-high-low-write-only)
pub const HDMA1: =
unsafe ;
/// [VRAM DMA source low](https://gbdev.io/pandocs/CGB_Registers.html#ff51ff52--hdma1-hdma2-cgb-mode-only-vram-dma-source-high-low-write-only)
pub const HDMA2: =
unsafe ;
/// [VRAM DMA destination high](https://gbdev.io/pandocs/CGB_Registers.html#ff53ff54--hdma3-hdma4-cgb-mode-only-vram-dma-destination-high-low-write-only)
pub const HDMA3: =
unsafe ;
/// [VRAM DMA destination low](https://gbdev.io/pandocs/CGB_Registers.html#ff53ff54--hdma3-hdma4-cgb-mode-only-vram-dma-destination-high-low-write-only)
pub const HDMA4: =
unsafe ;
/// [VRAM DMA length/mode/start](https://gbdev.io/pandocs/CGB_Registers.html#ff55--hdma5-cgb-mode-only-vram-dma-lengthmodestart)
pub const HDMA5: =
unsafe ;
/// [Infrared communications port](https://gbdev.io/pandocs/CGB_Registers.html#ff56--rp-cgb-mode-only-infrared-communications-port)
pub const RP: =
unsafe ;
/// [Background color palette specification](https://gbdev.io/pandocs/Palettes.html#ff68--bcpsbgpi-cgb-mode-only-background-color-palette-specification--background-palette-index)
pub const BCPS: =
unsafe ;
/// [Background color palette data](https://gbdev.io/pandocs/Palettes.html#ff69--bcpdbgpd-cgb-mode-only-background-color-palette-data--background-palette-data)
pub const BCPD: =
unsafe ;
/// [OBJ color palette specification](https://gbdev.io/pandocs/Palettes.html#ff6aff6b--ocpsobpi-ocpdobpd-cgb-mode-only-obj-color-palette-specification--obj-palette-index-obj-color-palette-data--obj-palette-data)
pub const OCPS: =
unsafe ;
/// [OBJ color palette data](https://gbdev.io/pandocs/Palettes.html#ff6aff6b--ocpsobpi-ocpdobpd-cgb-mode-only-obj-color-palette-specification--obj-palette-index-obj-color-palette-data--obj-palette-data)
pub const OCPD: =
unsafe ;
/// [Object priority mode](https://gbdev.io/pandocs/CGB_Registers.html#ff6c--opri-cgb-mode-only-object-priority-mode)
pub const OPRI: =
unsafe ;
/// [WRAM bank](https://gbdev.io/pandocs/CGB_Registers.html#ff70--svbk-cgb-mode-only-wram-bank)
pub const SVBK: =
unsafe ;
/// [Audio digital outputs 1 & 2](https://gbdev.io/pandocs/Audio_details.html#ff76--pcm12-cgb-mode-only-digital-outputs-1--2-read-only)
///
/// The low nibble is a copy of a sound channel 1's digital output, the high nibble is a copy of
/// sound channel 2's
pub const PCM12: =
unsafe ;
/// [Audio digital outputs 3 & 4](https://gbdev.io/pandocs/Audio_details.html#ff77--pcm34-cgb-mode-only-digital-outputs-3--4-read-only)
///
/// Same with [`PCM12`], but with channels 3 and 4.
pub const PCM34: =
unsafe ;
/// [Interrupt enable](https://gbdev.io/pandocs/Interrupts.html#ffff--ie-interrupt-enable)
///
/// <table class="bit-descrs"><thead><tr><th></th><th>7</th><th>6</th><th>5</th><th>4</th><th>3</th><th>2</th><th>1</th><th>0</th></tr></thead><tbody><tr><td><strong>IE</strong></td><td colspan="3" class="unused-field"></td><td colspan="1">Joypad</td><td colspan="1">Serial</td><td colspan="1">Timer</td><td colspan="1">LCD</td><td colspan="1">VBlank</td></tr></tbody></table>
///
/// * **VBlank** (Read/Write) : Controls whether the VBlank interrupt handler may be called (see
/// [`IF`]).
/// * **LCD** (Read/Write) : Controls whether the LCD interrupt handler may be called (see [`IF`])
/// * **Timer** (Read/Write) : Controls whether the Timer interrupt handler may be calle (see
/// [`IF`])
/// * **Serial** (Read/Write) : Controls whether the Serial interrupt handler may be called (see
/// [`IF`])
/// * **Joypad** (Read/Write) : Controls whether the Joypad interrupt handler may be called (see
/// [`IF`])
pub const IE: =
unsafe ;