Skip to content

Commit

Permalink
Separate out idea of comm hardware present & ready vs ready only
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Sep 6, 2024
1 parent 0e3ff11 commit a8f3143
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 33 deletions.
24 changes: 13 additions & 11 deletions src/comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ static bool countsInBounds(void);

typedef struct CommVTable {
void (*init)(void);
bool (*is_present)(void);
u8 (*read_ready)(void);
u8 (*read)(void);
u8 (*write_ready)(void);
void (*write)(u8 data);
} CommVTable;

static const CommVTable Demo_VTable = { comm_demo_init, comm_demo_read_ready, comm_demo_read,
comm_demo_write_ready, comm_demo_write };
static const CommVTable Demo_VTable = { comm_demo_init, comm_demo_is_present, comm_demo_read_ready,
comm_demo_read, comm_demo_write_ready, comm_demo_write };

static const CommVTable Everdrive_VTable = { comm_everdrive_init, comm_everdrive_read_ready,
comm_everdrive_read, comm_everdrive_write_ready, comm_everdrive_write };
static const CommVTable Everdrive_VTable
= { comm_everdrive_init, comm_everdrive_is_present, comm_everdrive_read_ready,
comm_everdrive_read, comm_everdrive_write_ready, comm_everdrive_write };

static const CommVTable EverdrivePro_VTable
= { comm_everdrive_pro_init, comm_everdrive_pro_read_ready, comm_everdrive_pro_read,
comm_everdrive_pro_write_ready, comm_everdrive_pro_write };
= { comm_everdrive_pro_init, comm_everdrive_pro_is_present, comm_everdrive_pro_read_ready,
comm_everdrive_pro_read, comm_everdrive_pro_write_ready, comm_everdrive_pro_write };

static const CommVTable Serial_VTable = { comm_serial_init, comm_serial_read_ready,
comm_serial_read, comm_serial_write_ready, comm_serial_write };
static const CommVTable Serial_VTable = { comm_serial_init, comm_serial_is_present,
comm_serial_read_ready, comm_serial_read, comm_serial_write_ready, comm_serial_write };

static const CommVTable Megawifi_VTable = { comm_megawifi_init, comm_megawifi_read_ready,
comm_megawifi_read, comm_megawifi_write_ready, comm_megawifi_write };
static const CommVTable Megawifi_VTable = { comm_megawifi_init, comm_megawifi_is_present,
comm_megawifi_read_ready, comm_megawifi_read, comm_megawifi_write_ready, comm_megawifi_write };

static const CommVTable* commTypes[] = {
#if COMM_EVERDRIVE_X7 == 1
Expand Down Expand Up @@ -73,7 +75,7 @@ static bool readReady(void)
{
if (activeCommType == NULL) {
for (u16 i = 0; i < COMM_TYPES; i++) {
if (commTypes[i]->read_ready()) {
if (commTypes[i]->is_present() && commTypes[i]->read_ready()) {
activeCommType = commTypes[i];
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/comm_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void comm_demo_init(void)
program = 0;
}

bool comm_demo_is_present(void)
{
return true;
}

u8 comm_demo_read_ready(void)
{
if (!enabled) {
Expand Down
1 change: 1 addition & 0 deletions src/comm_demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "types.h"

void comm_demo_init(void);
bool comm_demo_is_present(void);
u8 comm_demo_read_ready(void);
u8 comm_demo_read(void);
u8 comm_demo_write_ready(void);
Expand Down
32 changes: 30 additions & 2 deletions src/comm_everdrive.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@
#include "everdrive_led.h"

#define SSF_REG16(reg) *((volatile u16*)(0xA13000 + reg))
#define REG_USB 226
#define REG_STE 228
#define REG_USB 0xE2
#define REG_STE 0xE4
#define STE_USB_RD_RDY 4
#define STE_USB_WR_RDY 2 // usb write ready bit
#define STE_SPI_RDY 1

#define IO_STATUS_HI_SD 0x00
#define IO_STATUS_HI_SDHC 0x40

bool comm_everdrive_is_present(void)
{
/* REG_STE values:
0x3F00 with OpenEmu v2.4.1
0x3F00 with Exodus
0x3F00 with Regen v0.97d
0xFFFF with Fusion 3.6.4
0x3F00 with BlastEm nightly (0.6.3-pre-4c418ee9a9d8) Win
0x3015 with BlastEm nightly (0.6.3-pre) (also 0x3014)
0x4003 when ME X7 idle with USB in
0x4009 when ME X7 idle loaded via SD without USB cable connected
0x4003 when ME X7 idle loaded via SD with USB cable connected
0x3F00 with ME PRO
*/

u8 status = SSF_REG16(REG_STE) >> 8;
return status == IO_STATUS_HI_SD || status == IO_STATUS_HI_SDHC;
}

u16 comm_everdrive_raw(void)
{
return SSF_REG16(REG_STE);
}

u8 comm_everdrive_read_ready(void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/comm_everdrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "types.h"

void comm_everdrive_init(void);
bool comm_everdrive_is_present(void);
u8 comm_everdrive_read_ready(void);
u8 comm_everdrive_read(void);
u8 comm_everdrive_write_ready(void);
void comm_everdrive_write(u8 data);
u16 comm_everdrive_raw(void);
5 changes: 5 additions & 0 deletions src/comm_everdrive_pro.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,8 @@ void comm_everdrive_pro_write(u8 data)
void comm_everdrive_pro_init(void)
{
}

bool comm_everdrive_pro_is_present(void)
{
return pro_present();
}
1 change: 1 addition & 0 deletions src/comm_everdrive_pro.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "types.h"

void comm_everdrive_pro_init(void);
bool comm_everdrive_pro_is_present(void);
u8 comm_everdrive_pro_read_ready(void);
u8 comm_everdrive_pro_read(void);
u8 comm_everdrive_pro_write_ready(void);
Expand Down
8 changes: 8 additions & 0 deletions src/comm_megawifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "log.h"
#include "ext/mw/megawifi.h"
#include "ext/mw/lsd.h"
#include "ext/mw/16c550.h"
#include "vstring.h"
#include "settings.h"
#include "buffer.h"
Expand Down Expand Up @@ -145,6 +146,13 @@ void comm_megawifi_init(void)
status = Listening;
}

bool comm_megawifi_is_present(void)
{
u16 current = UART_SPR;
UART_SPR++;
return (current != UART_SPR);
}

u8 comm_megawifi_read_ready(void)
{
if (!recvData)
Expand Down
1 change: 1 addition & 0 deletions src/comm_megawifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "types.h"

void comm_megawifi_init(void);
bool comm_megawifi_is_present(void);
u8 comm_megawifi_read_ready(void);
u8 comm_megawifi_read(void);
u8 comm_megawifi_write_ready(void);
Expand Down
5 changes: 5 additions & 0 deletions src/comm_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ void comm_serial_init(void)
}
}

bool comm_serial_is_present(void)
{
return true;
}

u8 comm_serial_read_ready(void)
{
if (!recvData)
Expand Down
1 change: 1 addition & 0 deletions src/comm_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "types.h"

void comm_serial_init(void);
bool comm_serial_is_present(void);
u8 comm_serial_read_ready(void);
u8 comm_serial_read(void);
u8 comm_serial_write_ready(void);
Expand Down
9 changes: 9 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,31 @@ MD_MOCKS=SYS_setVIntCallback \
SPR_setFrame \
SPR_setAnimAndFrame \
SPR_setVisibility \
comm_megawifi_is_present \
comm_megawifi_read_ready \
comm_megawifi_read \
comm_megawifi_write_ready \
comm_megawifi_write \
comm_serial_init \
comm_serial_is_present \
comm_serial_read_ready \
comm_serial_read \
comm_serial_write_ready \
comm_serial_write \
comm_everdrive_init \
comm_everdrive_is_present \
comm_everdrive_read_ready \
comm_everdrive_read \
comm_everdrive_write_ready \
comm_everdrive_write \
comm_everdrive_pro_init \
comm_everdrive_pro_is_present \
comm_everdrive_pro_read_ready \
comm_everdrive_pro_read \
comm_everdrive_pro_write_ready \
comm_everdrive_pro_write \
comm_demo_init \
comm_demo_is_present \
comm_demo_read_ready \
comm_demo_read \
comm_demo_ready \
Expand Down
5 changes: 5 additions & 0 deletions tests/asserts.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include "asserts.h"
#include "comm.h"

void expect_everdrive_to_be_present(void)
{
will_return(__wrap_comm_everdrive_is_present, true);
}

void stub_usb_receive_nothing(void)
{
will_return(__wrap_comm_everdrive_read_ready, 0);
Expand Down
1 change: 1 addition & 0 deletions tests/asserts.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "debug.h"
#include "cmocka.h"

void expect_everdrive_to_be_present(void);
void expect_usb_sent_byte(u8 value);
void stub_usb_receive_nothing(void);
void stub_usb_receive_byte(u8 value);
Expand Down
Loading

0 comments on commit a8f3143

Please sign in to comment.