all repos — mgba @ 05edd2fe52a2f50ba3774c06175cd176fe328984

mGBA Game Boy Advance Emulator

src/gb/sio.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 "sio.h"
 7
 8#include "gb/gb.h"
 9#include "gb/io.h"
10#include "gb/serialize.h"
11
12void GBSIOInit(struct GBSIO* sio) {
13	sio->pendingSB = 0xFF;
14}
15
16void GBSIOReset(struct GBSIO* sio) {
17	sio->nextEvent = INT_MAX;
18	sio->remainingBits = 0;
19}
20
21void GBSIODeinit(struct GBSIO* sio) {
22	UNUSED(sio);
23	// Nothing to do yet
24}
25
26int32_t GBSIOProcessEvents(struct GBSIO* sio, int32_t cycles) {
27	if (sio->nextEvent != INT_MAX) {
28		sio->nextEvent -= cycles;
29	}
30	if (sio->nextEvent <= 0) {
31		--sio->remainingBits;
32		sio->p->memory.io[REG_SB] &= ~(8 >> sio->remainingBits);
33		sio->p->memory.io[REG_SB] |= sio->pendingSB & ~(8 >> sio->remainingBits);
34		if (!sio->remainingBits) {
35			sio->p->memory.io[REG_IF] |= (1 << GB_IRQ_SIO);
36			sio->p->memory.io[REG_SC] = GBRegisterSCClearEnable(sio->p->memory.io[REG_SC]);
37			GBUpdateIRQs(sio->p);
38			sio->nextEvent = INT_MAX;
39		} else {
40			sio->nextEvent += sio->period;
41		}
42	}
43	return sio->nextEvent;
44}
45
46void GBSIOWriteSC(struct GBSIO* sio, uint8_t sc) {
47	sio->period = 0x1000; // TODO Shift Clock
48	if (GBRegisterSCIsEnable(sc)) {
49		if (GBRegisterSCIsShiftClock(sc)) {
50			sio->nextEvent = sio->p->cpu->cycles + sio->period;
51		}
52		sio->remainingBits = 8;
53	}
54}
55