Clarify some function names and add more essenital SIO architecture

This commit is contained in:
Jeffrey Pfau 2014-02-05 01:03:34 -08:00
parent e5ee1aa07f
commit 924c6cf5a6
2 changed files with 46 additions and 15 deletions

View File

@ -2,6 +2,8 @@
#include "gba-io.h"
#include <limits.h>
static struct GBASIODriver* _lookupDriver(struct GBASIO* sio, enum GBASIOMode mode) {
switch (mode) {
case SIO_MULTI:
@ -22,12 +24,12 @@ static void _switchMode(struct GBASIO* sio) {
sio->mode = (enum GBASIOMode) (mode & 0xC);
}
if (oldMode != mode) {
if (sio->activeDriver && sio->activeDriver->detach) {
sio->activeDriver->detach(sio->activeDriver);
if (sio->activeDriver && sio->activeDriver->unload) {
sio->activeDriver->unload(sio->activeDriver);
}
sio->activeDriver = _lookupDriver(sio, mode);
if (sio->activeDriver && sio->activeDriver->attach) {
sio->activeDriver->attach(sio->activeDriver);
if (sio->activeDriver && sio->activeDriver->load) {
sio->activeDriver->load(sio->activeDriver);
}
}
}
@ -38,6 +40,15 @@ void GBASIOInit(struct GBASIO* sio) {
_switchMode(sio);
}
void GBASIODeinit(struct GBASIO* sio) {
if (sio->drivers.multiplayer && sio->drivers.multiplayer->deinit) {
sio->drivers.multiplayer->deinit(sio->drivers.multiplayer);
}
if (sio->drivers.joybus && sio->drivers.joybus->deinit) {
sio->drivers.joybus->deinit(sio->drivers.joybus);
}
}
void GBASIOSetDriverSet(struct GBASIO* sio, struct GBASIODriverSet* drivers) {
if (drivers->multiplayer) {
GBASIOSetDriver(sio, drivers->multiplayer, SIO_MULTI);
@ -61,20 +72,27 @@ void GBASIOSetDriver(struct GBASIO* sio, struct GBASIODriver* driver, enum GBASI
return;
}
if (*driverLoc) {
if ((*driverLoc)->detach) {
(*driverLoc)->detach(*driverLoc);
if ((*driverLoc)->unload) {
(*driverLoc)->unload(*driverLoc);
}
if ((*driverLoc)->deinit) {
(*driverLoc)->deinit(*driverLoc);
}
}
if (*driverLoc == sio->activeDriver) {
sio->activeDriver = driver;
if (driver) {
driver->p = sio;
if (driver->init) {
driver->init(driver);
}
if (*driverLoc == sio->activeDriver) {
sio->activeDriver = driver;
if ((*driverLoc)->load) {
(*driverLoc)->load(*driverLoc);
}
}
}
*driverLoc = driver;
if (driver && driver->init) {
driver->init(driver, sio);
}
}
void GBASIOWriteRCNT(struct GBASIO* sio, uint16_t value) {
@ -98,3 +116,10 @@ void GBASIOWriteSIOMLT_SEND(struct GBASIO* sio, uint16_t value) {
sio->activeDriver->writeRegister(sio->activeDriver, REG_SIOMLT_SEND, value);
}
}
int32_t GBASIOProcessEvents(struct GBASIO* sio, int32_t cycles) {
if (sio->activeDriver && sio->activeDriver->processEvents) {
return sio->activeDriver->processEvents(sio->activeDriver, cycles);
}
return INT_MAX;
}

View File

@ -19,12 +19,14 @@ enum {
struct GBASIO;
struct GBASIODriver {
void (*init)(struct GBASIODriver* driver, struct GBASIO* sio);
void (*deinit)(struct GBASIODriver* driver);
struct GBASIO* p;
int (*attach)(struct GBASIODriver* driver);
int (*detach)(struct GBASIODriver* driver);
void (*init)(struct GBASIODriver* driver);
void (*deinit)(struct GBASIODriver* driver);
int (*load)(struct GBASIODriver* driver);
int (*unload)(struct GBASIODriver* driver);
void (*writeRegister)(struct GBASIODriver* driver, uint32_t address, uint16_t value);
int32_t (*processEvents)(struct GBASIODriver* driver, int32_t cycles);
};
struct GBASIODriverSet {
@ -72,6 +74,8 @@ struct GBASIO {
};
void GBASIOInit(struct GBASIO* sio);
void GBASIODeinit(struct GBASIO* sio);
void GBASIOSetDriverSet(struct GBASIO* sio, struct GBASIODriverSet* drivers);
void GBASIOSetDriver(struct GBASIO* sio, struct GBASIODriver* driver, enum GBASIOMode mode);
@ -79,4 +83,6 @@ void GBASIOWriteRCNT(struct GBASIO* sio, uint16_t value);
void GBASIOWriteSIOCNT(struct GBASIO* sio, uint16_t value);
void GBASIOWriteSIOMLT_SEND(struct GBASIO* sio, uint16_t value);
int32_t GBASIOProcessEvents(struct GBASIO* sio, int32_t cycles);
#endif