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 value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
60 GBVideoWriteLCDC(&gb->video, value);
61 break;
62 case REG_SCY:
63 case REG_SCX:
64 case REG_WY:
65 case REG_WX:
66 case REG_BGP:
67 case REG_OBP0:
68 case REG_OBP1:
69 value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
70 break;
71 case REG_STAT:
72 GBVideoWriteSTAT(&gb->video, value);
73 break;
74 case REG_IE:
75 gb->memory.ie = value;
76 GBUpdateIRQs(gb);
77 return;
78 default:
79 // TODO: Log
80 if (address >= GB_SIZE_IO) {
81 return;
82 }
83 break;
84 }
85 gb->memory.io[address] = value;
86}
87
88uint8_t GBIORead(struct GB* gb, unsigned address) {
89 switch (address) {
90 case REG_IF:
91 break;
92 case REG_IE:
93 return gb->memory.ie;
94 default:
95 // TODO: Log
96 if (address >= GB_SIZE_IO) {
97 return 0;
98 }
99 break;
100 }
101 return gb->memory.io[address];
102}