all repos — mgba @ b98f86bb08336b55d4fcb740caa1db496496a3c0

mGBA Game Boy Advance Emulator

include/mgba/internal/gba/sio.h (view raw)

 1/* Copyright (c) 2013-2015 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#ifndef GBA_SIO_H
 7#define GBA_SIO_H
 8
 9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/gba/interface.h>
14#include <mgba/core/log.h>
15
16#define MAX_GBAS 4
17
18extern const int GBASIOCyclesPerTransfer[4][MAX_GBAS];
19
20mLOG_DECLARE_CATEGORY(GBA_SIO);
21
22enum {
23	RCNT_INITIAL = 0x8000
24};
25
26enum {
27	JOY_CMD_RESET = 0xFF,
28	JOY_CMD_POLL = 0x00,
29	JOY_CMD_TRANS = 0x14,
30	JOY_CMD_RECV = 0x15,
31
32	JOYSTAT_TRANS = 8,
33	JOYSTAT_RECV = 2,
34
35	JOYCNT_RESET = 1,
36	JOYCNT_RECV = 2,
37	JOYCNT_TRANS = 4,
38};
39
40DECL_BITFIELD(GBASIONormal, uint16_t);
41DECL_BIT(GBASIONormal, Sc, 0);
42DECL_BIT(GBASIONormal, InternalSc, 1);
43DECL_BIT(GBASIONormal, Si, 2);
44DECL_BIT(GBASIONormal, IdleSo, 3);
45DECL_BIT(GBASIONormal, Start, 7);
46DECL_BIT(GBASIONormal, Length, 12);
47DECL_BIT(GBASIONormal, Irq, 14);
48DECL_BITFIELD(GBASIOMultiplayer, uint16_t);
49DECL_BITS(GBASIOMultiplayer, Baud, 0, 2);
50DECL_BIT(GBASIOMultiplayer, Slave, 2);
51DECL_BIT(GBASIOMultiplayer, Ready, 3);
52DECL_BITS(GBASIOMultiplayer, Id, 4, 2);
53DECL_BIT(GBASIOMultiplayer, Error, 6);
54DECL_BIT(GBASIOMultiplayer, Busy, 7);
55DECL_BIT(GBASIOMultiplayer, Irq, 14);
56
57struct GBASIODriverSet {
58	struct GBASIODriver* normal;
59	struct GBASIODriver* multiplayer;
60	struct GBASIODriver* joybus;
61};
62
63struct GBASIO {
64	struct GBA* p;
65
66	enum GBASIOMode mode;
67	struct GBASIODriverSet drivers;
68	struct GBASIODriver* activeDriver;
69
70	uint16_t rcnt;
71	uint16_t siocnt;
72};
73
74void GBASIOInit(struct GBASIO* sio);
75void GBASIODeinit(struct GBASIO* sio);
76void GBASIOReset(struct GBASIO* sio);
77
78void GBASIOSetDriverSet(struct GBASIO* sio, struct GBASIODriverSet* drivers);
79void GBASIOSetDriver(struct GBASIO* sio, struct GBASIODriver* driver, enum GBASIOMode mode);
80
81void GBASIOWriteRCNT(struct GBASIO* sio, uint16_t value);
82void GBASIOWriteSIOCNT(struct GBASIO* sio, uint16_t value);
83uint16_t GBASIOWriteRegister(struct GBASIO* sio, uint32_t address, uint16_t value);
84
85void GBASIOJOYCreate(struct GBASIODriver* sio);
86int GBASIOJOYSendCommand(struct GBASIODriver* sio, enum GBASIOJOYCommand command, uint8_t* data);
87
88CXX_GUARD_END
89
90#endif