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 _GBSIOProcessEvents(struct mTiming* timing, void* context, uint32_t cyclesLate);
13
14void GBSIOInit(struct GBSIO* sio) {
15 sio->pendingSB = 0xFF;
16 sio->event.context = sio;
17 sio->event.name = "GB SIO";
18 sio->event.callback = _GBSIOProcessEvents;
19 sio->event.priority = 0x30;
20}
21
22void GBSIOReset(struct GBSIO* sio) {
23 sio->nextEvent = INT_MAX;
24 sio->remainingBits = 0;
25}
26
27void GBSIODeinit(struct GBSIO* sio) {
28 UNUSED(sio);
29 // Nothing to do yet
30}
31
32void _GBSIOProcessEvents(struct mTiming* timing, void* context, uint32_t cyclesLate) {
33 UNUSED(cyclesLate);
34 struct GBSIO* sio = context;
35 --sio->remainingBits;
36 sio->p->memory.io[REG_SB] &= ~(8 >> sio->remainingBits);
37 sio->p->memory.io[REG_SB] |= sio->pendingSB & ~(8 >> sio->remainingBits);
38 if (!sio->remainingBits) {
39 sio->p->memory.io[REG_IF] |= (1 << GB_IRQ_SIO);
40 sio->p->memory.io[REG_SC] = GBRegisterSCClearEnable(sio->p->memory.io[REG_SC]);
41 GBUpdateIRQs(sio->p);
42 } else {
43 mTimingSchedule(timing, &sio->event, sio->period);
44 }
45}
46
47void GBSIOWriteSC(struct GBSIO* sio, uint8_t sc) {
48 sio->period = 0x1000; // TODO Shift Clock
49 if (GBRegisterSCIsEnable(sc)) {
50 if (GBRegisterSCIsShiftClock(sc)) {
51 mTimingSchedule(&sio->p->timing, &sio->event, sio->period);
52 }
53 sio->remainingBits = 8;
54 }
55}
56