all repos — mgba @ 7ebd2d6e7546b91217d6dba88d0d2d01d72f4905

mGBA Game Boy Advance Emulator

src/gb/io.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include <mgba/internal/gb/io.h>
  7
  8#include <mgba/internal/gb/gb.h>
  9#include <mgba/internal/gb/sio.h>
 10#include <mgba/internal/gb/serialize.h>
 11
 12mLOG_DEFINE_CATEGORY(GB_IO, "GB I/O", "gb.io");
 13
 14const char* const GBIORegisterNames[] = {
 15	[REG_JOYP] = "JOYP",
 16	[REG_SB] = "SB",
 17	[REG_SC] = "SC",
 18	[REG_DIV] = "DIV",
 19	[REG_TIMA] = "TIMA",
 20	[REG_TMA] = "TMA",
 21	[REG_TAC] = "TAC",
 22	[REG_IF] = "IF",
 23	[REG_NR10] = "NR10",
 24	[REG_NR11] = "NR11",
 25	[REG_NR12] = "NR12",
 26	[REG_NR13] = "NR13",
 27	[REG_NR14] = "NR14",
 28	[REG_NR21] = "NR21",
 29	[REG_NR22] = "NR22",
 30	[REG_NR23] = "NR23",
 31	[REG_NR24] = "NR24",
 32	[REG_NR30] = "NR30",
 33	[REG_NR31] = "NR31",
 34	[REG_NR32] = "NR32",
 35	[REG_NR33] = "NR33",
 36	[REG_NR34] = "NR34",
 37	[REG_NR41] = "NR41",
 38	[REG_NR42] = "NR42",
 39	[REG_NR43] = "NR43",
 40	[REG_NR44] = "NR44",
 41	[REG_NR50] = "NR50",
 42	[REG_NR51] = "NR51",
 43	[REG_NR52] = "NR52",
 44	[REG_LCDC] = "LCDC",
 45	[REG_STAT] = "STAT",
 46	[REG_SCY] = "SCY",
 47	[REG_SCX] = "SCX",
 48	[REG_LY] = "LY",
 49	[REG_LYC] = "LYC",
 50	[REG_DMA] = "DMA",
 51	[REG_BGP] = "BGP",
 52	[REG_OBP0] = "OBP0",
 53	[REG_OBP1] = "OBP1",
 54	[REG_WY] = "WY",
 55	[REG_WX] = "WX",
 56	[REG_KEY1] = "KEY1",
 57	[REG_VBK] = "VBK",
 58	[REG_HDMA1] = "HDMA1",
 59	[REG_HDMA2] = "HDMA2",
 60	[REG_HDMA3] = "HDMA3",
 61	[REG_HDMA4] = "HDMA4",
 62	[REG_HDMA5] = "HDMA5",
 63	[REG_RP] = "RP",
 64	[REG_BCPS] = "BCPS",
 65	[REG_BCPD] = "BCPD",
 66	[REG_OCPS] = "OCPS",
 67	[REG_OCPD] = "OCPD",
 68	[REG_SVBK] = "SVBK",
 69	[REG_IE] = "IE",
 70};
 71
 72static const uint8_t _registerMask[] = {
 73	[REG_SC]   = 0x7E, // TODO: GBC differences
 74	[REG_IF]   = 0xE0,
 75	[REG_TAC]  = 0xF8,
 76	[REG_NR10] = 0x80,
 77	[REG_NR11] = 0x3F,
 78	[REG_NR12] = 0x00,
 79	[REG_NR13] = 0xFF,
 80	[REG_NR14] = 0xBF,
 81	[REG_NR21] = 0x3F,
 82	[REG_NR22] = 0x00,
 83	[REG_NR23] = 0xFF,
 84	[REG_NR24] = 0xBF,
 85	[REG_NR30] = 0x7F,
 86	[REG_NR31] = 0xFF,
 87	[REG_NR32] = 0x9F,
 88	[REG_NR33] = 0xFF,
 89	[REG_NR34] = 0xBF,
 90	[REG_NR41] = 0xFF,
 91	[REG_NR42] = 0x00,
 92	[REG_NR43] = 0x00,
 93	[REG_NR44] = 0xBF,
 94	[REG_NR50] = 0x00,
 95	[REG_NR51] = 0x00,
 96	[REG_NR52] = 0x70,
 97	[REG_STAT] = 0x80,
 98	[REG_KEY1] = 0x7E,
 99	[REG_VBK] = 0xFE,
100	[REG_OCPS] = 0x40,
101	[REG_BCPS] = 0x40,
102	[REG_UNK6C] = 0xFE,
103	[REG_SVBK] = 0xF8,
104	[REG_UNK75] = 0x8F,
105	[REG_IE]   = 0xE0,
106};
107
108static void _writeSGBBits(struct GB* gb, int bits) {
109	if (!bits) {
110		gb->sgbBit = 0;
111		memset(gb->sgbPacket, 0, sizeof(gb->sgbPacket));
112	}
113	if (bits == gb->currentSgbBits) {
114		return;
115	}
116	gb->currentSgbBits = bits;
117	if (gb->sgbBit == 128 && bits == 2) {
118		GBVideoWriteSGBPacket(&gb->video, gb->sgbPacket);
119		++gb->sgbBit;
120	}
121	if (gb->sgbBit >= 128) {
122		return;
123	}
124	switch (bits) {
125	case 1:
126		gb->sgbPacket[gb->sgbBit >> 3] |= 1 << (gb->sgbBit & 7);
127		// Fall through
128	case 2:
129		++gb->sgbBit;
130	default:
131		break;
132	}
133}
134
135void GBIOInit(struct GB* gb) {
136	memset(gb->memory.io, 0, sizeof(gb->memory.io));
137}
138
139void GBIOReset(struct GB* gb) {
140	memset(gb->memory.io, 0, sizeof(gb->memory.io));
141
142	GBIOWrite(gb, REG_TIMA, 0);
143	GBIOWrite(gb, REG_TMA, 0);
144	GBIOWrite(gb, REG_TAC, 0);
145	GBIOWrite(gb, REG_IF, 1);
146	GBIOWrite(gb, REG_NR52, 0xF1);
147	GBIOWrite(gb, REG_NR14, 0x3F);
148	GBIOWrite(gb, REG_NR10, 0x80);
149	GBIOWrite(gb, REG_NR11, 0xBF);
150	GBIOWrite(gb, REG_NR12, 0xF3);
151	GBIOWrite(gb, REG_NR13, 0xF3);
152	GBIOWrite(gb, REG_NR24, 0x3F);
153	GBIOWrite(gb, REG_NR21, 0x3F);
154	GBIOWrite(gb, REG_NR22, 0x00);
155	GBIOWrite(gb, REG_NR34, 0x3F);
156	GBIOWrite(gb, REG_NR30, 0x7F);
157	GBIOWrite(gb, REG_NR31, 0xFF);
158	GBIOWrite(gb, REG_NR32, 0x9F);
159	GBIOWrite(gb, REG_NR44, 0x3F);
160	GBIOWrite(gb, REG_NR41, 0xFF);
161	GBIOWrite(gb, REG_NR42, 0x00);
162	GBIOWrite(gb, REG_NR43, 0x00);
163	GBIOWrite(gb, REG_NR50, 0x77);
164	GBIOWrite(gb, REG_NR51, 0xF3);
165	GBIOWrite(gb, REG_LCDC, 0x91);
166	GBIOWrite(gb, REG_SCY, 0x00);
167	GBIOWrite(gb, REG_SCX, 0x00);
168	GBIOWrite(gb, REG_LYC, 0x00);
169	GBIOWrite(gb, REG_BGP, 0xFC);
170	if (gb->model < GB_MODEL_CGB) {
171		GBIOWrite(gb, REG_OBP0, 0xFF);
172		GBIOWrite(gb, REG_OBP1, 0xFF);
173	}
174	GBIOWrite(gb, REG_WY, 0x00);
175	GBIOWrite(gb, REG_WX, 0x00);
176	if (gb->model >= GB_MODEL_CGB) {
177		GBIOWrite(gb, REG_JOYP, 0xFF);
178		GBIOWrite(gb, REG_VBK, 0);
179		GBIOWrite(gb, REG_BCPS, 0);
180		GBIOWrite(gb, REG_OCPS, 0);
181		GBIOWrite(gb, REG_SVBK, 1);
182		GBIOWrite(gb, REG_HDMA1, 0xFF);
183		GBIOWrite(gb, REG_HDMA2, 0xFF);
184		GBIOWrite(gb, REG_HDMA3, 0xFF);
185		GBIOWrite(gb, REG_HDMA4, 0xFF);
186		gb->memory.io[REG_HDMA5] = 0xFF;
187	} else if (gb->model == GB_MODEL_SGB) {
188		GBIOWrite(gb, REG_JOYP, 0xFF);
189	}
190	GBIOWrite(gb, REG_IE, 0x00);
191}
192
193void GBIOWrite(struct GB* gb, unsigned address, uint8_t value) {
194	switch (address) {
195	case REG_SB:
196		GBSIOWriteSB(&gb->sio, value);
197		break;
198	case REG_SC:
199		GBSIOWriteSC(&gb->sio, value);
200		break;
201	case REG_DIV:
202		GBTimerDivReset(&gb->timer);
203		return;
204	case REG_NR10:
205		if (gb->audio.enable) {
206			GBAudioWriteNR10(&gb->audio, value);
207		} else {
208			value = 0;
209		}
210		break;
211	case REG_NR11:
212		if (gb->audio.enable) {
213			GBAudioWriteNR11(&gb->audio, value);
214		} else {
215			if (gb->audio.style == GB_AUDIO_DMG) {
216				GBAudioWriteNR11(&gb->audio, value & _registerMask[REG_NR11]);
217			}
218			value = 0;
219		}
220		break;
221	case REG_NR12:
222		if (gb->audio.enable) {
223			GBAudioWriteNR12(&gb->audio, value);
224		} else {
225			value = 0;
226		}
227		break;
228	case REG_NR13:
229		if (gb->audio.enable) {
230			GBAudioWriteNR13(&gb->audio, value);
231		} else {
232			value = 0;
233		}
234		break;
235	case REG_NR14:
236		if (gb->audio.enable) {
237			GBAudioWriteNR14(&gb->audio, value);
238		} else {
239			value = 0;
240		}
241		break;
242	case REG_NR21:
243		if (gb->audio.enable) {
244			GBAudioWriteNR21(&gb->audio, value);
245		} else {
246			if (gb->audio.style == GB_AUDIO_DMG) {
247				GBAudioWriteNR21(&gb->audio, value & _registerMask[REG_NR21]);
248			}
249			value = 0;
250		}
251		break;
252	case REG_NR22:
253		if (gb->audio.enable) {
254			GBAudioWriteNR22(&gb->audio, value);
255		} else {
256			value = 0;
257		}
258		break;
259	case REG_NR23:
260		if (gb->audio.enable) {
261			GBAudioWriteNR23(&gb->audio, value);
262		} else {
263			value = 0;
264		}
265		break;
266	case REG_NR24:
267		if (gb->audio.enable) {
268			GBAudioWriteNR24(&gb->audio, value);
269		} else {
270			value = 0;
271		}
272		break;
273	case REG_NR30:
274		if (gb->audio.enable) {
275			GBAudioWriteNR30(&gb->audio, value);
276		} else {
277			value = 0;
278		}
279		break;
280	case REG_NR31:
281		if (gb->audio.enable || gb->audio.style == GB_AUDIO_DMG) {
282			GBAudioWriteNR31(&gb->audio, value);
283		} else {
284			value = 0;
285		}
286		break;
287	case REG_NR32:
288		if (gb->audio.enable) {
289			GBAudioWriteNR32(&gb->audio, value);
290		} else {
291			value = 0;
292		}
293		break;
294	case REG_NR33:
295		if (gb->audio.enable) {
296			GBAudioWriteNR33(&gb->audio, value);
297		} else {
298			value = 0;
299		}
300		break;
301	case REG_NR34:
302		if (gb->audio.enable) {
303			GBAudioWriteNR34(&gb->audio, value);
304		} else {
305			value = 0;
306		}
307		break;
308	case REG_NR41:
309		if (gb->audio.enable || gb->audio.style == GB_AUDIO_DMG) {
310			GBAudioWriteNR41(&gb->audio, value);
311		} else {
312			value = 0;
313		}
314		break;
315	case REG_NR42:
316		if (gb->audio.enable) {
317			GBAudioWriteNR42(&gb->audio, value);
318		} else {
319			value = 0;
320		}
321		break;
322	case REG_NR43:
323		if (gb->audio.enable) {
324			GBAudioWriteNR43(&gb->audio, value);
325		} else {
326			value = 0;
327		}
328		break;
329	case REG_NR44:
330		if (gb->audio.enable) {
331			GBAudioWriteNR44(&gb->audio, value);
332		} else {
333			value = 0;
334		}
335		break;
336	case REG_NR50:
337		if (gb->audio.enable) {
338			GBAudioWriteNR50(&gb->audio, value);
339		} else {
340			value = 0;
341		}
342		break;
343	case REG_NR51:
344		if (gb->audio.enable) {
345			GBAudioWriteNR51(&gb->audio, value);
346		} else {
347			value = 0;
348		}
349		break;
350	case REG_NR52:
351		GBAudioWriteNR52(&gb->audio, value);
352		value &= 0x80;
353		value |= gb->memory.io[REG_NR52] & 0x0F;
354		break;
355	case REG_WAVE_0:
356	case REG_WAVE_1:
357	case REG_WAVE_2:
358	case REG_WAVE_3:
359	case REG_WAVE_4:
360	case REG_WAVE_5:
361	case REG_WAVE_6:
362	case REG_WAVE_7:
363	case REG_WAVE_8:
364	case REG_WAVE_9:
365	case REG_WAVE_A:
366	case REG_WAVE_B:
367	case REG_WAVE_C:
368	case REG_WAVE_D:
369	case REG_WAVE_E:
370	case REG_WAVE_F:
371		if (!gb->audio.playingCh3 || gb->audio.style != GB_AUDIO_DMG) {
372			gb->audio.ch3.wavedata8[address - REG_WAVE_0] = value;
373		} else if(gb->audio.ch3.readable) {
374			gb->audio.ch3.wavedata8[gb->audio.ch3.window >> 1] = value;
375		}
376		break;
377	case REG_JOYP:
378		if (gb->model == GB_MODEL_SGB) {
379			_writeSGBBits(gb, (value >> 4) & 3);
380		}
381		break;
382	case REG_TIMA:
383	case REG_TMA:
384		// Handled transparently by the registers
385		break;
386	case REG_TAC:
387		value = GBTimerUpdateTAC(&gb->timer, value);
388		break;
389	case REG_IF:
390		gb->memory.io[REG_IF] = value | 0xE0;
391		GBUpdateIRQs(gb);
392		return;
393	case REG_LCDC:
394		// TODO: handle GBC differences
395		GBVideoProcessDots(&gb->video, 0);
396		value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
397		GBVideoWriteLCDC(&gb->video, value);
398		break;
399	case REG_LYC:
400		GBVideoWriteLYC(&gb->video, value);
401		break;
402	case REG_DMA:
403		GBMemoryDMA(gb, value << 8);
404		break;
405	case REG_SCY:
406	case REG_SCX:
407	case REG_WY:
408	case REG_WX:
409		GBVideoProcessDots(&gb->video, 0);
410		value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
411		break;
412	case REG_BGP:
413	case REG_OBP0:
414	case REG_OBP1:
415		GBVideoProcessDots(&gb->video, 0);
416		GBVideoWritePalette(&gb->video, address, value);
417		break;
418	case REG_STAT:
419		GBVideoWriteSTAT(&gb->video, value);
420		value = gb->video.stat;
421		break;
422	case 0x50:
423		if (gb->memory.romBase < gb->memory.rom || gb->memory.romBase > &gb->memory.rom[gb->memory.romSize - 1]) {
424			free(gb->memory.romBase);
425			gb->memory.romBase = gb->memory.rom;
426		}
427		if (gb->model >= GB_MODEL_CGB && gb->memory.io[0x6C]) {
428			gb->model = GB_MODEL_DMG;
429			GBVideoDisableCGB(&gb->video);
430		}
431		break;
432	case REG_IE:
433		gb->memory.ie = value;
434		GBUpdateIRQs(gb);
435		return;
436	default:
437		if (gb->model >= GB_MODEL_CGB) {
438			switch (address) {
439			case REG_KEY1:
440				value &= 0x1;
441				value |= gb->memory.io[address] & 0x80;
442				break;
443			case REG_VBK:
444				GBVideoSwitchBank(&gb->video, value);
445				break;
446			case REG_HDMA1:
447			case REG_HDMA2:
448			case REG_HDMA3:
449			case REG_HDMA4:
450				// Handled transparently by the registers
451				break;
452			case REG_HDMA5:
453				GBMemoryWriteHDMA5(gb, value);
454				value &= 0x7F;
455				break;
456			case REG_BCPS:
457				gb->video.bcpIndex = value & 0x3F;
458				gb->video.bcpIncrement = value & 0x80;
459				gb->memory.io[REG_BCPD] = gb->video.palette[gb->video.bcpIndex >> 1] >> (8 * (gb->video.bcpIndex & 1));
460				break;
461			case REG_BCPD:
462				GBVideoProcessDots(&gb->video, 0);
463				GBVideoWritePalette(&gb->video, address, value);
464				return;
465			case REG_OCPS:
466				gb->video.ocpIndex = value & 0x3F;
467				gb->video.ocpIncrement = value & 0x80;
468				gb->memory.io[REG_OCPD] = gb->video.palette[8 * 4 + (gb->video.ocpIndex >> 1)] >> (8 * (gb->video.ocpIndex & 1));
469				break;
470			case REG_OCPD:
471				GBVideoProcessDots(&gb->video, 0);
472				GBVideoWritePalette(&gb->video, address, value);
473				return;
474			case REG_SVBK:
475				GBMemorySwitchWramBank(&gb->memory, value);
476				value = gb->memory.wramCurrentBank;
477				break;
478			default:
479				goto failed;
480			}
481			goto success;
482		}
483		failed:
484		mLOG(GB_IO, STUB, "Writing to unknown register FF%02X:%02X", address, value);
485		if (address >= GB_SIZE_IO) {
486			return;
487		}
488		break;
489	}
490	success:
491	gb->memory.io[address] = value;
492}
493
494static uint8_t _readKeys(struct GB* gb) {
495	uint8_t keys = *gb->keySource;
496	switch (gb->memory.io[REG_JOYP] & 0x30) {
497	case 0x30:
498	// TODO: Increment
499		keys = (gb->video.sgbCommandHeader >> 3) == SGB_MLT_REG ? 0xF : 0;
500		break;
501	case 0x20:
502		keys >>= 4;
503		break;
504	case 0x10:
505		break;
506	case 0x00:
507		keys |= keys >> 4;
508		break;
509	}
510	return (0xC0 | (gb->memory.io[REG_JOYP] | 0xF)) ^ (keys & 0xF);
511}
512
513uint8_t GBIORead(struct GB* gb, unsigned address) {
514	switch (address) {
515	case REG_JOYP:
516		return _readKeys(gb);
517	case REG_IE:
518		return gb->memory.ie;
519	case REG_WAVE_0:
520	case REG_WAVE_1:
521	case REG_WAVE_2:
522	case REG_WAVE_3:
523	case REG_WAVE_4:
524	case REG_WAVE_5:
525	case REG_WAVE_6:
526	case REG_WAVE_7:
527	case REG_WAVE_8:
528	case REG_WAVE_9:
529	case REG_WAVE_A:
530	case REG_WAVE_B:
531	case REG_WAVE_C:
532	case REG_WAVE_D:
533	case REG_WAVE_E:
534	case REG_WAVE_F:
535		if (gb->audio.playingCh3) {
536			if (gb->audio.ch3.readable || gb->audio.style != GB_AUDIO_DMG) {
537				return gb->audio.ch3.wavedata8[gb->audio.ch3.window >> 1];
538			} else {
539				return 0xFF;
540			}
541		} else {
542			return gb->audio.ch3.wavedata8[address - REG_WAVE_0];
543		}
544		break;
545	case REG_SB:
546	case REG_SC:
547	case REG_IF:
548	case REG_NR10:
549	case REG_NR11:
550	case REG_NR12:
551	case REG_NR14:
552	case REG_NR21:
553	case REG_NR22:
554	case REG_NR24:
555	case REG_NR30:
556	case REG_NR32:
557	case REG_NR34:
558	case REG_NR41:
559	case REG_NR42:
560	case REG_NR43:
561	case REG_NR44:
562	case REG_NR50:
563	case REG_NR51:
564	case REG_NR52:
565	case REG_DIV:
566	case REG_TIMA:
567	case REG_TMA:
568	case REG_TAC:
569	case REG_STAT:
570	case REG_LCDC:
571	case REG_SCY:
572	case REG_SCX:
573	case REG_LY:
574	case REG_LYC:
575	case REG_BGP:
576	case REG_OBP0:
577	case REG_OBP1:
578	case REG_WY:
579	case REG_WX:
580		// Handled transparently by the registers
581		break;
582	default:
583		if (gb->model >= GB_MODEL_CGB) {
584			switch (address) {
585			case REG_KEY1:
586			case REG_VBK:
587			case REG_HDMA1:
588			case REG_HDMA2:
589			case REG_HDMA3:
590			case REG_HDMA4:
591			case REG_HDMA5:
592			case REG_BCPS:
593			case REG_BCPD:
594			case REG_OCPS:
595			case REG_OCPD:
596			case REG_SVBK:
597				// Handled transparently by the registers
598				goto success;
599			case REG_DMA:
600				mLOG(GB_IO, STUB, "Reading from unknown register FF%02X", address);
601				return 0;
602			default:
603				break;
604			}
605		}
606		mLOG(GB_IO, STUB, "Reading from unknown register FF%02X", address);
607		return 0xFF;
608	}
609	success:
610	return gb->memory.io[address] | _registerMask[address];
611}
612
613void GBTestKeypadIRQ(struct GB* gb) {
614	if (_readKeys(gb)) {
615		gb->memory.io[REG_IF] |= (1 << GB_IRQ_KEYPAD);
616		GBUpdateIRQs(gb);
617	}
618}
619
620struct GBSerializedState;
621void GBIOSerialize(const struct GB* gb, struct GBSerializedState* state) {
622	memcpy(state->io, gb->memory.io, GB_SIZE_IO);
623	state->ie = gb->memory.ie;
624}
625
626void GBIODeserialize(struct GB* gb, const struct GBSerializedState* state) {
627	memcpy(gb->memory.io, state->io, GB_SIZE_IO);
628	gb->memory.ie = state->ie;
629
630	if (GBAudioEnableGetEnable(*gb->audio.nr52)) {
631		GBIOWrite(gb, REG_NR10, gb->memory.io[REG_NR10]);
632		GBIOWrite(gb, REG_NR11, gb->memory.io[REG_NR11]);
633		GBIOWrite(gb, REG_NR12, gb->memory.io[REG_NR12]);
634		GBIOWrite(gb, REG_NR13, gb->memory.io[REG_NR13]);
635		gb->audio.ch1.control.frequency &= 0xFF;
636		gb->audio.ch1.control.frequency |= GBAudioRegisterControlGetFrequency(gb->memory.io[REG_NR14] << 8);
637		gb->audio.ch1.control.stop = GBAudioRegisterControlGetStop(gb->memory.io[REG_NR14] << 8);
638		GBIOWrite(gb, REG_NR21, gb->memory.io[REG_NR21]);
639		GBIOWrite(gb, REG_NR22, gb->memory.io[REG_NR22]);
640		GBIOWrite(gb, REG_NR22, gb->memory.io[REG_NR23]);
641		gb->audio.ch2.control.frequency &= 0xFF;
642		gb->audio.ch2.control.frequency |= GBAudioRegisterControlGetFrequency(gb->memory.io[REG_NR24] << 8);
643		gb->audio.ch2.control.stop = GBAudioRegisterControlGetStop(gb->memory.io[REG_NR24] << 8);
644		GBIOWrite(gb, REG_NR30, gb->memory.io[REG_NR30]);
645		GBIOWrite(gb, REG_NR31, gb->memory.io[REG_NR31]);
646		GBIOWrite(gb, REG_NR32, gb->memory.io[REG_NR32]);
647		GBIOWrite(gb, REG_NR32, gb->memory.io[REG_NR33]);
648		gb->audio.ch3.rate &= 0xFF;
649		gb->audio.ch3.rate |= GBAudioRegisterControlGetRate(gb->memory.io[REG_NR34] << 8);
650		gb->audio.ch3.stop = GBAudioRegisterControlGetStop(gb->memory.io[REG_NR34] << 8);
651		GBIOWrite(gb, REG_NR41, gb->memory.io[REG_NR41]);
652		GBIOWrite(gb, REG_NR42, gb->memory.io[REG_NR42]);
653		GBIOWrite(gb, REG_NR43, gb->memory.io[REG_NR43]);
654		gb->audio.ch4.stop = GBAudioRegisterNoiseControlGetStop(gb->memory.io[REG_NR44]);
655		GBIOWrite(gb, REG_NR50, gb->memory.io[REG_NR50]);
656		GBIOWrite(gb, REG_NR51, gb->memory.io[REG_NR51]);
657	}
658
659	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_LCDC, state->io[REG_LCDC]);
660	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_SCY, state->io[REG_SCY]);
661	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_SCX, state->io[REG_SCX]);
662	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_WY, state->io[REG_WY]);
663	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_WX, state->io[REG_WX]);
664	if (gb->model == GB_MODEL_SGB) {
665		gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_BGP, state->io[REG_BGP]);
666		gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_OBP0, state->io[REG_OBP0]);
667		gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_OBP1, state->io[REG_OBP1]);
668	}
669	gb->video.stat = state->io[REG_STAT];
670}