src/gba/sio/joybus.c (view raw)
1/* Copyright (c) 2013-2017 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 <mgba/internal/gba/sio.h>
7
8#include <mgba/internal/gba/gba.h>
9#include <mgba/internal/gba/io.h>
10
11static uint16_t GBASIOJOYWriteRegister(struct GBASIODriver* sio, uint32_t address, uint16_t value);
12
13void GBASIOJOYCreate(struct GBASIODriver* sio) {
14 sio->init = NULL;
15 sio->deinit = NULL;
16 sio->load = NULL;
17 sio->unload = NULL;
18 sio->writeRegister = GBASIOJOYWriteRegister;
19}
20
21uint16_t GBASIOJOYWriteRegister(struct GBASIODriver* sio, uint32_t address, uint16_t value) {
22 switch (address) {
23 case REG_JOYCNT:
24 mLOG(GBA_SIO, DEBUG, "JOY write: CNT <- %04X", value);
25 return (value & 0x0040) | (sio->p->p->memory.io[REG_JOYCNT >> 1] & ~(value & 0x7) & ~0x0040);
26 case REG_JOYSTAT:
27 mLOG(GBA_SIO, DEBUG, "JOY write: STAT <- %04X", value);
28 return (value & 0x0030) | (sio->p->p->memory.io[REG_JOYSTAT >> 1] & ~0x30);
29 case REG_JOY_TRANS_LO:
30 mLOG(GBA_SIO, DEBUG, "JOY write: TRANS_LO <- %04X", value);
31 break;
32 case REG_JOY_TRANS_HI:
33 mLOG(GBA_SIO, DEBUG, "JOY write: TRANS_HI <- %04X", value);
34 break;
35 default:
36 mLOG(GBA_SIO, DEBUG, "JOY write: Unknown reg %03X <- %04X", address, value);
37 // Fall through
38 case REG_RCNT:
39 break;
40 }
41 return value;
42}
43
44int GBASIOJOYSendCommand(struct GBASIODriver* sio, enum GBASIOJOYCommand command, uint8_t* data) {
45 switch (command) {
46 case JOY_RESET:
47 sio->p->p->memory.io[REG_JOYCNT >> 1] |= JOYCNT_RESET;
48 if (sio->p->p->memory.io[REG_JOYCNT >> 1] & 0x40) {
49 GBARaiseIRQ(sio->p->p, IRQ_SIO, 0);
50 }
51 // Fall through
52 case JOY_POLL:
53 data[0] = 0x00;
54 data[1] = 0x04;
55 data[2] = sio->p->p->memory.io[REG_JOYSTAT >> 1];
56
57 mLOG(GBA_SIO, DEBUG, "JOY %s: %02X (%02X)", command == JOY_POLL ? "poll" : "reset", data[2], sio->p->p->memory.io[REG_JOYCNT >> 1]);
58 return 3;
59 case JOY_RECV:
60 sio->p->p->memory.io[REG_JOYCNT >> 1] |= JOYCNT_RECV;
61 sio->p->p->memory.io[REG_JOYSTAT >> 1] |= JOYSTAT_RECV;
62
63 sio->p->p->memory.io[REG_JOY_RECV_LO >> 1] = data[0] | (data[1] << 8);
64 sio->p->p->memory.io[REG_JOY_RECV_HI >> 1] = data[2] | (data[3] << 8);
65
66 data[0] = sio->p->p->memory.io[REG_JOYSTAT >> 1];
67
68 mLOG(GBA_SIO, DEBUG, "JOY recv: %02X (%02X)", data[0], sio->p->p->memory.io[REG_JOYCNT >> 1]);
69
70 if (sio->p->p->memory.io[REG_JOYCNT >> 1] & 0x40) {
71 GBARaiseIRQ(sio->p->p, IRQ_SIO, 0);
72 }
73 return 1;
74 case JOY_TRANS:
75 data[0] = sio->p->p->memory.io[REG_JOY_TRANS_LO >> 1];
76 data[1] = sio->p->p->memory.io[REG_JOY_TRANS_LO >> 1] >> 8;
77 data[2] = sio->p->p->memory.io[REG_JOY_TRANS_HI >> 1];
78 data[3] = sio->p->p->memory.io[REG_JOY_TRANS_HI >> 1] >> 8;
79 data[4] = sio->p->p->memory.io[REG_JOYSTAT >> 1];
80
81 sio->p->p->memory.io[REG_JOYCNT >> 1] |= JOYCNT_TRANS;
82 sio->p->p->memory.io[REG_JOYSTAT >> 1] &= ~JOYSTAT_TRANS;
83
84 mLOG(GBA_SIO, DEBUG, "JOY trans: %02X%02X%02X%02X:%02X (%02X)", data[0], data[1], data[2], data[3], data[4], sio->p->p->memory.io[REG_JOYCNT >> 1]);
85
86 if (sio->p->p->memory.io[REG_JOYCNT >> 1] & 0x40) {
87 GBARaiseIRQ(sio->p->p, IRQ_SIO, 0);
88 }
89 return 5;
90 }
91 return 0;
92}