all repos — mgba @ 05c2fc3e0d4cec701af25581cbb6b807b16b66a9

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, 0xBF);
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, 0xBF);
153	GBIOWrite(gb, REG_NR21, 0x3F);
154	GBIOWrite(gb, REG_NR22, 0x00);
155	GBIOWrite(gb, REG_NR34, 0xBF);
156	GBIOWrite(gb, REG_NR30, 0x7F);
157	GBIOWrite(gb, REG_NR31, 0xFF);
158	GBIOWrite(gb, REG_NR32, 0x9F);
159	GBIOWrite(gb, REG_NR44, 0xBF);
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	GBIOWrite(gb, REG_OBP0, 0xFF);
171	GBIOWrite(gb, REG_OBP1, 0xFF);
172	GBIOWrite(gb, REG_WY, 0x00);
173	GBIOWrite(gb, REG_WX, 0x00);
174	GBIOWrite(gb, REG_VBK, 0);
175	GBIOWrite(gb, REG_BCPS, 0);
176	GBIOWrite(gb, REG_OCPS, 0);
177	GBIOWrite(gb, REG_SVBK, 1);
178	GBIOWrite(gb, REG_HDMA1, 0xFF);
179	GBIOWrite(gb, REG_HDMA2, 0xFF);
180	GBIOWrite(gb, REG_HDMA3, 0xFF);
181	GBIOWrite(gb, REG_HDMA4, 0xFF);
182	gb->memory.io[REG_HDMA5] = 0xFF;
183	GBIOWrite(gb, REG_IE, 0x00);
184}
185
186void GBIOWrite(struct GB* gb, unsigned address, uint8_t value) {
187	switch (address) {
188	case REG_SB:
189		GBSIOWriteSB(&gb->sio, value);
190		break;
191	case REG_SC:
192		GBSIOWriteSC(&gb->sio, value);
193		break;
194	case REG_DIV:
195		GBTimerDivReset(&gb->timer);
196		return;
197	case REG_NR10:
198		if (gb->audio.enable) {
199			GBAudioWriteNR10(&gb->audio, value);
200		} else {
201			value = 0;
202		}
203		break;
204	case REG_NR11:
205		if (gb->audio.enable) {
206			GBAudioWriteNR11(&gb->audio, value);
207		} else {
208			if (gb->audio.style == GB_AUDIO_DMG) {
209				GBAudioWriteNR11(&gb->audio, value & _registerMask[REG_NR11]);
210			}
211			value = 0;
212		}
213		break;
214	case REG_NR12:
215		if (gb->audio.enable) {
216			GBAudioWriteNR12(&gb->audio, value);
217		} else {
218			value = 0;
219		}
220		break;
221	case REG_NR13:
222		if (gb->audio.enable) {
223			GBAudioWriteNR13(&gb->audio, value);
224		} else {
225			value = 0;
226		}
227		break;
228	case REG_NR14:
229		if (gb->audio.enable) {
230			GBAudioWriteNR14(&gb->audio, value);
231		} else {
232			value = 0;
233		}
234		break;
235	case REG_NR21:
236		if (gb->audio.enable) {
237			GBAudioWriteNR21(&gb->audio, value);
238		} else {
239			if (gb->audio.style == GB_AUDIO_DMG) {
240				GBAudioWriteNR21(&gb->audio, value & _registerMask[REG_NR21]);
241			}
242			value = 0;
243		}
244		break;
245	case REG_NR22:
246		if (gb->audio.enable) {
247			GBAudioWriteNR22(&gb->audio, value);
248		} else {
249			value = 0;
250		}
251		break;
252	case REG_NR23:
253		if (gb->audio.enable) {
254			GBAudioWriteNR23(&gb->audio, value);
255		} else {
256			value = 0;
257		}
258		break;
259	case REG_NR24:
260		if (gb->audio.enable) {
261			GBAudioWriteNR24(&gb->audio, value);
262		} else {
263			value = 0;
264		}
265		break;
266	case REG_NR30:
267		if (gb->audio.enable) {
268			GBAudioWriteNR30(&gb->audio, value);
269		} else {
270			value = 0;
271		}
272		break;
273	case REG_NR31:
274		if (gb->audio.enable || gb->audio.style == GB_AUDIO_DMG) {
275			GBAudioWriteNR31(&gb->audio, value);
276		} else {
277			value = 0;
278		}
279		break;
280	case REG_NR32:
281		if (gb->audio.enable) {
282			GBAudioWriteNR32(&gb->audio, value);
283		} else {
284			value = 0;
285		}
286		break;
287	case REG_NR33:
288		if (gb->audio.enable) {
289			GBAudioWriteNR33(&gb->audio, value);
290		} else {
291			value = 0;
292		}
293		break;
294	case REG_NR34:
295		if (gb->audio.enable) {
296			GBAudioWriteNR34(&gb->audio, value);
297		} else {
298			value = 0;
299		}
300		break;
301	case REG_NR41:
302		if (gb->audio.enable || gb->audio.style == GB_AUDIO_DMG) {
303			GBAudioWriteNR41(&gb->audio, value);
304		} else {
305			value = 0;
306		}
307		break;
308	case REG_NR42:
309		if (gb->audio.enable) {
310			GBAudioWriteNR42(&gb->audio, value);
311		} else {
312			value = 0;
313		}
314		break;
315	case REG_NR43:
316		if (gb->audio.enable) {
317			GBAudioWriteNR43(&gb->audio, value);
318		} else {
319			value = 0;
320		}
321		break;
322	case REG_NR44:
323		if (gb->audio.enable) {
324			GBAudioWriteNR44(&gb->audio, value);
325		} else {
326			value = 0;
327		}
328		break;
329	case REG_NR50:
330		if (gb->audio.enable) {
331			GBAudioWriteNR50(&gb->audio, value);
332		} else {
333			value = 0;
334		}
335		break;
336	case REG_NR51:
337		if (gb->audio.enable) {
338			GBAudioWriteNR51(&gb->audio, value);
339		} else {
340			value = 0;
341		}
342		break;
343	case REG_NR52:
344		GBAudioWriteNR52(&gb->audio, value);
345		value &= 0x80;
346		value |= gb->memory.io[REG_NR52] & 0x0F;
347		break;
348	case REG_WAVE_0:
349	case REG_WAVE_1:
350	case REG_WAVE_2:
351	case REG_WAVE_3:
352	case REG_WAVE_4:
353	case REG_WAVE_5:
354	case REG_WAVE_6:
355	case REG_WAVE_7:
356	case REG_WAVE_8:
357	case REG_WAVE_9:
358	case REG_WAVE_A:
359	case REG_WAVE_B:
360	case REG_WAVE_C:
361	case REG_WAVE_D:
362	case REG_WAVE_E:
363	case REG_WAVE_F:
364		if (!gb->audio.playingCh3 || gb->audio.style != GB_AUDIO_DMG) {
365			gb->audio.ch3.wavedata8[address - REG_WAVE_0] = value;
366		} else if(gb->audio.ch3.readable) {
367			gb->audio.ch3.wavedata8[gb->audio.ch3.window >> 1] = value;
368		}
369		break;
370	case REG_JOYP:
371		if (gb->model == GB_MODEL_SGB) {
372			_writeSGBBits(gb, (value >> 4) & 3);
373		}
374		break;
375	case REG_TIMA:
376	case REG_TMA:
377		// Handled transparently by the registers
378		break;
379	case REG_TAC:
380		value = GBTimerUpdateTAC(&gb->timer, value);
381		break;
382	case REG_IF:
383		gb->memory.io[REG_IF] = value | 0xE0;
384		GBUpdateIRQs(gb);
385		return;
386	case REG_LCDC:
387		// TODO: handle GBC differences
388		GBVideoProcessDots(&gb->video);
389		value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
390		GBVideoWriteLCDC(&gb->video, value);
391		break;
392	case REG_LYC:
393		GBVideoWriteLYC(&gb->video, value);
394		break;
395	case REG_DMA:
396		GBMemoryDMA(gb, value << 8);
397		break;
398	case REG_SCY:
399	case REG_SCX:
400	case REG_WY:
401	case REG_WX:
402		GBVideoProcessDots(&gb->video);
403		value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
404		break;
405	case REG_BGP:
406	case REG_OBP0:
407	case REG_OBP1:
408		GBVideoProcessDots(&gb->video);
409		GBVideoWritePalette(&gb->video, address, value);
410		break;
411	case REG_STAT:
412		GBVideoWriteSTAT(&gb->video, value);
413		value = gb->video.stat;
414		break;
415	case 0x50:
416		if (gb->memory.romBase < gb->memory.rom || gb->memory.romBase > &gb->memory.rom[gb->memory.romSize - 1]) {
417			free(gb->memory.romBase);
418			gb->memory.romBase = gb->memory.rom;
419		}
420		break;
421	case REG_IE:
422		gb->memory.ie = value;
423		GBUpdateIRQs(gb);
424		return;
425	default:
426		if (gb->model >= GB_MODEL_CGB) {
427			switch (address) {
428			case REG_KEY1:
429				value &= 0x1;
430				value |= gb->memory.io[address] & 0x80;
431				break;
432			case REG_VBK:
433				GBVideoSwitchBank(&gb->video, value);
434				break;
435			case REG_HDMA1:
436			case REG_HDMA2:
437			case REG_HDMA3:
438			case REG_HDMA4:
439				// Handled transparently by the registers
440				break;
441			case REG_HDMA5:
442				GBMemoryWriteHDMA5(gb, value);
443				value &= 0x7F;
444				break;
445			case REG_BCPS:
446				gb->video.bcpIndex = value & 0x3F;
447				gb->video.bcpIncrement = value & 0x80;
448				gb->memory.io[REG_BCPD] = gb->video.palette[gb->video.bcpIndex >> 1] >> (8 * (gb->video.bcpIndex & 1));
449				break;
450			case REG_BCPD:
451				GBVideoProcessDots(&gb->video);
452				GBVideoWritePalette(&gb->video, address, value);
453				return;
454			case REG_OCPS:
455				gb->video.ocpIndex = value & 0x3F;
456				gb->video.ocpIncrement = value & 0x80;
457				gb->memory.io[REG_OCPD] = gb->video.palette[8 * 4 + (gb->video.ocpIndex >> 1)] >> (8 * (gb->video.ocpIndex & 1));
458				break;
459			case REG_OCPD:
460				GBVideoProcessDots(&gb->video);
461				GBVideoWritePalette(&gb->video, address, value);
462				return;
463			case REG_SVBK:
464				GBMemorySwitchWramBank(&gb->memory, value);
465				value = gb->memory.wramCurrentBank;
466				break;
467			default:
468				goto failed;
469			}
470			goto success;
471		}
472		failed:
473		mLOG(GB_IO, STUB, "Writing to unknown register FF%02X:%02X", address, value);
474		if (address >= GB_SIZE_IO) {
475			return;
476		}
477		break;
478	}
479	success:
480	gb->memory.io[address] = value;
481}
482
483static uint8_t _readKeys(struct GB* gb) {
484	uint8_t keys = *gb->keySource;
485	switch (gb->memory.io[REG_JOYP] & 0x30) {
486	case 0x30:
487	// TODO: Increment
488		keys = gb->model == GB_MODEL_SGB ? 0xF : 0;
489		break;
490	case 0x20:
491		keys >>= 4;
492		break;
493	case 0x10:
494		break;
495	case 0x00:
496		keys |= keys >> 4;
497		break;
498	}
499	return (0xC0 | (gb->memory.io[REG_JOYP] | 0xF)) ^ (keys & 0xF);
500}
501
502uint8_t GBIORead(struct GB* gb, unsigned address) {
503	switch (address) {
504	case REG_JOYP:
505		return _readKeys(gb);
506	case REG_IE:
507		return gb->memory.ie;
508	case REG_WAVE_0:
509	case REG_WAVE_1:
510	case REG_WAVE_2:
511	case REG_WAVE_3:
512	case REG_WAVE_4:
513	case REG_WAVE_5:
514	case REG_WAVE_6:
515	case REG_WAVE_7:
516	case REG_WAVE_8:
517	case REG_WAVE_9:
518	case REG_WAVE_A:
519	case REG_WAVE_B:
520	case REG_WAVE_C:
521	case REG_WAVE_D:
522	case REG_WAVE_E:
523	case REG_WAVE_F:
524		if (gb->audio.playingCh3) {
525			if (gb->audio.ch3.readable || gb->audio.style != GB_AUDIO_DMG) {
526				return gb->audio.ch3.wavedata8[gb->audio.ch3.window >> 1];
527			} else {
528				return 0xFF;
529			}
530		} else {
531			return gb->audio.ch3.wavedata8[address - REG_WAVE_0];
532		}
533		break;
534	case REG_SB:
535	case REG_SC:
536	case REG_IF:
537	case REG_NR10:
538	case REG_NR11:
539	case REG_NR12:
540	case REG_NR14:
541	case REG_NR21:
542	case REG_NR22:
543	case REG_NR24:
544	case REG_NR30:
545	case REG_NR32:
546	case REG_NR34:
547	case REG_NR41:
548	case REG_NR42:
549	case REG_NR43:
550	case REG_NR44:
551	case REG_NR50:
552	case REG_NR51:
553	case REG_NR52:
554	case REG_DIV:
555	case REG_TIMA:
556	case REG_TMA:
557	case REG_TAC:
558	case REG_STAT:
559	case REG_LCDC:
560	case REG_SCY:
561	case REG_SCX:
562	case REG_LY:
563	case REG_LYC:
564	case REG_BGP:
565	case REG_OBP0:
566	case REG_OBP1:
567	case REG_WY:
568	case REG_WX:
569		// Handled transparently by the registers
570		break;
571	default:
572		if (gb->model >= GB_MODEL_CGB) {
573			switch (address) {
574			case REG_KEY1:
575			case REG_VBK:
576			case REG_HDMA1:
577			case REG_HDMA2:
578			case REG_HDMA3:
579			case REG_HDMA4:
580			case REG_HDMA5:
581			case REG_BCPS:
582			case REG_BCPD:
583			case REG_OCPS:
584			case REG_OCPD:
585			case REG_SVBK:
586				// Handled transparently by the registers
587				goto success;
588			default:
589				break;
590			}
591		}
592		mLOG(GB_IO, STUB, "Reading from unknown register FF%02X", address);
593		return 0xFF;
594	}
595	success:
596	return gb->memory.io[address] | _registerMask[address];
597}
598
599void GBTestKeypadIRQ(struct GB* gb) {
600	if (_readKeys(gb)) {
601		gb->memory.io[REG_IF] |= (1 << GB_IRQ_KEYPAD);
602		GBUpdateIRQs(gb);
603	}
604}
605
606struct GBSerializedState;
607void GBIOSerialize(const struct GB* gb, struct GBSerializedState* state) {
608	memcpy(state->io, gb->memory.io, GB_SIZE_IO);
609	state->ie = gb->memory.ie;
610}
611
612void GBIODeserialize(struct GB* gb, const struct GBSerializedState* state) {
613	memcpy(gb->memory.io, state->io, GB_SIZE_IO);
614	gb->memory.ie = state->ie;
615
616	if (GBAudioEnableGetEnable(*gb->audio.nr52)) {
617		GBIOWrite(gb, REG_NR10, gb->memory.io[REG_NR10]);
618		GBIOWrite(gb, REG_NR11, gb->memory.io[REG_NR11]);
619		GBIOWrite(gb, REG_NR12, gb->memory.io[REG_NR12]);
620		GBIOWrite(gb, REG_NR13, gb->memory.io[REG_NR13]);
621		gb->audio.ch1.control.frequency &= 0xFF;
622		gb->audio.ch1.control.frequency |= GBAudioRegisterControlGetFrequency(gb->memory.io[REG_NR14] << 8);
623		gb->audio.ch1.control.stop = GBAudioRegisterControlGetStop(gb->memory.io[REG_NR14] << 8);
624		GBIOWrite(gb, REG_NR21, gb->memory.io[REG_NR21]);
625		GBIOWrite(gb, REG_NR22, gb->memory.io[REG_NR22]);
626		GBIOWrite(gb, REG_NR22, gb->memory.io[REG_NR23]);
627		gb->audio.ch2.control.frequency &= 0xFF;
628		gb->audio.ch2.control.frequency |= GBAudioRegisterControlGetFrequency(gb->memory.io[REG_NR24] << 8);
629		gb->audio.ch2.control.stop = GBAudioRegisterControlGetStop(gb->memory.io[REG_NR24] << 8);
630		GBIOWrite(gb, REG_NR30, gb->memory.io[REG_NR30]);
631		GBIOWrite(gb, REG_NR31, gb->memory.io[REG_NR31]);
632		GBIOWrite(gb, REG_NR32, gb->memory.io[REG_NR32]);
633		GBIOWrite(gb, REG_NR32, gb->memory.io[REG_NR33]);
634		gb->audio.ch3.rate &= 0xFF;
635		gb->audio.ch3.rate |= GBAudioRegisterControlGetRate(gb->memory.io[REG_NR34] << 8);
636		gb->audio.ch3.stop = GBAudioRegisterControlGetStop(gb->memory.io[REG_NR34] << 8);
637		GBIOWrite(gb, REG_NR41, gb->memory.io[REG_NR41]);
638		GBIOWrite(gb, REG_NR42, gb->memory.io[REG_NR42]);
639		GBIOWrite(gb, REG_NR43, gb->memory.io[REG_NR43]);
640		gb->audio.ch4.stop = GBAudioRegisterNoiseControlGetStop(gb->memory.io[REG_NR44]);
641		GBIOWrite(gb, REG_NR50, gb->memory.io[REG_NR50]);
642		GBIOWrite(gb, REG_NR51, gb->memory.io[REG_NR51]);
643	}
644
645	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_LCDC, state->io[REG_LCDC]);
646	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_SCY, state->io[REG_SCY]);
647	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_SCX, state->io[REG_SCX]);
648	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_WY, state->io[REG_WY]);
649	gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_WX, state->io[REG_WX]);
650	if (gb->model == GB_MODEL_SGB) {
651		gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_BGP, state->io[REG_BGP]);
652		gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_OBP0, state->io[REG_OBP0]);
653		gb->video.renderer->writeVideoRegister(gb->video.renderer, REG_OBP1, state->io[REG_OBP1]);
654	}
655	gb->video.stat = state->io[REG_STAT];
656}