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_DIV:
54 GBTimerDivReset(&gb->timer);
55 return;
56 case REG_TIMA:
57 // ???
58 return;
59 case REG_TMA:
60 // Handled transparently by the registers
61 break;
62 case REG_TAC:
63 value = GBTimerUpdateTAC(&gb->timer, value);
64 break;
65 case REG_IF:
66 gb->memory.io[REG_IF] = value | 0xE0;
67 GBUpdateIRQs(gb);
68 return;
69 case REG_LCDC:
70 // TODO: handle GBC differences
71 value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
72 GBVideoWriteLCDC(&gb->video, value);
73 break;
74 case REG_DMA:
75 GBMemoryDMA(gb, value << 8);
76 break;
77 case REG_SCY:
78 case REG_SCX:
79 case REG_WY:
80 case REG_WX:
81 case REG_BGP:
82 case REG_OBP0:
83 case REG_OBP1:
84 value = gb->video.renderer->writeVideoRegister(gb->video.renderer, address, value);
85 break;
86 case REG_STAT:
87 GBVideoWriteSTAT(&gb->video, value);
88 break;
89 case REG_IE:
90 gb->memory.ie = value;
91 GBUpdateIRQs(gb);
92 return;
93 default:
94 // TODO: Log
95 if (address >= GB_SIZE_IO) {
96 return;
97 }
98 break;
99 }
100 gb->memory.io[address] = value;
101}
102
103static uint8_t _readKeys(struct GB* gb) {
104 uint8_t keys = *gb->keySource;
105 switch (gb->memory.io[REG_JOYP] & 0x30) {
106 case 0x20:
107 keys >>= 4;
108 break;
109 case 0x10:
110 break;
111 default:
112 // ???
113 keys = 0;
114 break;
115 }
116 return 0xC0 | (gb->memory.io[REG_JOYP] | 0xF) ^ (keys & 0xF);
117}
118
119uint8_t GBIORead(struct GB* gb, unsigned address) {
120 switch (address) {
121 case REG_JOYP:
122 return _readKeys(gb);
123 case REG_IF:
124 break;
125 case REG_IE:
126 return gb->memory.ie;
127 case REG_DIV:
128 case REG_TIMA:
129 case REG_TMA:
130 case REG_TAC:
131 case REG_LY:
132 // Handled transparently by the registers
133 break;
134 default:
135 // TODO: Log
136 if (address >= GB_SIZE_IO) {
137 return 0;
138 }
139 break;
140 }
141 return gb->memory.io[address];
142}