all repos — mgba @ 67050e44dd98b358655e5813a54b4b40a6a81893

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 "io.h"
 7
 8#include "gb/gb.h"
 9
10void GBIOInit(struct GB* gb) {
11	memset(gb->memory.io, 0, sizeof(gb->memory.io));
12}
13
14void GBIOReset(struct GB* gb) {
15	memset(gb->memory.io, 0, sizeof(gb->memory.io));
16
17	GBIOWrite(gb, 0x05, 0);
18	GBIOWrite(gb, 0x06, 0);
19	GBIOWrite(gb, 0x07, 0);
20	GBIOWrite(gb, 0x10, 0x80);
21	GBIOWrite(gb, 0x11, 0xBF);
22	GBIOWrite(gb, 0x12, 0xF3);
23	GBIOWrite(gb, 0x12, 0xF3);
24	GBIOWrite(gb, 0x14, 0xBF);
25	GBIOWrite(gb, 0x16, 0x3F);
26	GBIOWrite(gb, 0x17, 0x00);
27	GBIOWrite(gb, 0x19, 0xBF);
28	GBIOWrite(gb, 0x1A, 0x7F);
29	GBIOWrite(gb, 0x1B, 0xFF);
30	GBIOWrite(gb, 0x1C, 0x9F);
31	GBIOWrite(gb, 0x1E, 0xBF);
32	GBIOWrite(gb, 0x20, 0xFF);
33	GBIOWrite(gb, 0x21, 0x00);
34	GBIOWrite(gb, 0x22, 0x00);
35	GBIOWrite(gb, 0x23, 0xBF);
36	GBIOWrite(gb, 0x24, 0x77);
37	GBIOWrite(gb, 0x25, 0xF3);
38	GBIOWrite(gb, 0x26, 0xF1);
39	GBIOWrite(gb, 0x40, 0x91);
40	GBIOWrite(gb, 0x42, 0x00);
41	GBIOWrite(gb, 0x43, 0x00);
42	GBIOWrite(gb, 0x45, 0x00);
43	GBIOWrite(gb, 0x47, 0xFC);
44	GBIOWrite(gb, 0x48, 0xFF);
45	GBIOWrite(gb, 0x49, 0xFF);
46	GBIOWrite(gb, 0x4A, 0x00);
47	GBIOWrite(gb, 0x4B, 0x00);
48	GBIOWrite(gb, 0xFF, 0x00);
49}
50
51void GBIOWrite(struct GB* gb, unsigned address, uint8_t value) {
52	switch (address) {
53	case REG_IF:
54		gb->memory.io[REG_IF] = value;
55		GBUpdateIRQs(gb);
56		return;
57	case REG_LCDC:
58		// TODO: handle GBC differences
59		GBVideoWriteLCDC(&gb->video, value);
60		break;
61	case REG_STAT:
62		GBVideoWriteSTAT(&gb->video, value);
63		break;
64	case REG_IE:
65		gb->memory.ie = value;
66		GBUpdateIRQs(gb);
67		return;
68	default:
69		// TODO: Log
70		if (address >= GB_SIZE_IO) {
71			return;
72		}
73		break;
74	}
75	gb->memory.io[address] = value;
76}
77
78uint8_t GBIORead(struct GB* gb, unsigned address) {
79	switch (address) {
80	case REG_IF:
81		break;
82	case REG_IE:
83		return gb->memory.ie;
84	default:
85		// TODO: Log
86		if (address >= GB_SIZE_IO) {
87			return 0;
88		}
89		break;
90	}
91	return gb->memory.io[address];
92}