Skip to content

Commit

Permalink
Update osEeprom Read and Write (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaMech authored Apr 14, 2024
1 parent 0bb6992 commit 3dd4c07
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 191 deletions.
2 changes: 1 addition & 1 deletion include/PR/os_cont.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
typedef struct {
u16 type; /* Controller Type */
u8 status; /* Controller status */
u8 errno;
u8 errnum;
}OSContStatus;

typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion src/os/contramread.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ s32 __osContRamRead(OSMesgQueue *mq, int channel, u16 address, u8 *buffer) {
ptr = (u8 *)&__osPfsPifRam;
retry = 2;
__osSiGetAccess();
_osLastSentSiCmd = CONT_CMD_READ_MEMPACK;
__osContLastCmd = CONT_CMD_READ_MEMPACK;
__osPackRamReadData(channel, address);
ret = __osSiRawStartDma(OS_WRITE, &__osPfsPifRam);
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
Expand Down
2 changes: 1 addition & 1 deletion src/os/contramwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ s32 __osContRamWrite(OSMesgQueue *mq, int channel, u16 address, u8 *buffer, int
return 0;
}
__osSiGetAccess();
_osLastSentSiCmd = CONT_CMD_WRITE_MEMPACK;
__osContLastCmd = CONT_CMD_WRITE_MEMPACK;
__osPackRamWriteData(channel, address, buffer);
ret = __osSiRawStartDma(OS_WRITE, &__osPfsPifRam);
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
Expand Down
2 changes: 1 addition & 1 deletion src/os/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ u8 __osContAddressCrc(u16 addr);
u8 __osContDataCrc(u8 *data);
s32 __osPfsGetStatus(OSMesgQueue *queue, int channel);

extern u8 _osLastSentSiCmd;
extern u8 __osContLastCmd;
extern OSTimer __osEepromTimer;
extern OSMesg __osEepromTimerMsg;
extern OSMesgQueue __osEepromTimerQ;
Expand Down
8 changes: 4 additions & 4 deletions src/os/osContInit.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ u32 _osContInitialized = 0;
extern u64 osClockRate;

// these probably belong in EEPROMlongread or something
u8 _osLastSentSiCmd;
u8 __osContLastCmd;
u8 _osContNumControllers;
OSTimer D_80196548;
OSMesgQueue _osContMesgQueue;
Expand Down Expand Up @@ -39,7 +39,7 @@ s32 osContInit(OSMesgQueue *mq, u8 *bitpattern, OSContStatus *status) {
ret = __osSiRawStartDma(OS_READ, _osContCmdBuf);
osRecvMesg(mq, &mesg, OS_MESG_BLOCK);
__osContGetInitData(bitpattern, status);
_osLastSentSiCmd = 0;
__osContLastCmd = 0;
__osSiCreateAccessQueue();
osCreateMesgQueue(&_osContMesgQueue, _osContMesgBuff, 1);
return ret;
Expand All @@ -54,8 +54,8 @@ void __osContGetInitData(u8 *bitpattern, OSContStatus *status) {
cmdBufPtr = &(_osContCmdBuf[0].request);
for (i = 0; i < _osContNumControllers; i++, cmdBufPtr++, status++) {
response = *(OSContPackedRequest *) cmdBufPtr;
status->errno = (response.rxLen & 0xc0) >> 4;
if (status->errno == 0) {
status->errnum = (response.rxLen & 0xc0) >> 4;
if (status->errnum == 0) {
status->type = response.data2 << 8 | response.data1;
status->status = response.data3;

Expand Down
6 changes: 3 additions & 3 deletions src/os/osContStartReadData.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ u32 _osContPifCtrl;
ALIGNED8 OSContPackedStruct _osContCmdBuf[8];
#endif

extern u8 _osLastSentSiCmd;
extern u8 __osContLastCmd;
extern u8 _osContNumControllers;

void __osPackReadData(void);
s32 osContStartReadData(OSMesgQueue *mesg) {
s32 ret = 0;
s32 i;
__osSiGetAccess();
if (_osLastSentSiCmd != 1) {
if (__osContLastCmd != 1) {
__osPackReadData();
ret = __osSiRawStartDma(OS_WRITE, _osContCmdBuf);
osRecvMesg(mesg, NULL, OS_MESG_BLOCK);
Expand All @@ -30,7 +30,7 @@ s32 osContStartReadData(OSMesgQueue *mesg) {

_osContPifCtrl = 0;
ret = __osSiRawStartDma(OS_READ, _osContCmdBuf);
_osLastSentSiCmd = 1;
__osContLastCmd = 1;
__osSiRelAccess();
return ret;
}
Expand Down
8 changes: 4 additions & 4 deletions src/os/osEepromLongRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern OSTimer D_80196548; // not sure what this is yet
extern OSMesgQueue _osContMesgQueue;
extern OSMesg _osContMesgBuff[4];

s32 osEepromLongRead(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
s32 osEepromLongRead(OSMesgQueue *mq, u8 address, u8 *buffer, s32 nbytes) {
s32 status = 0;
if (address > 0x40) {
return -1;
Expand All @@ -19,9 +19,9 @@ s32 osEepromLongRead(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
return status;
}

nbytes -= 8;
address += 1;
buffer += 8;
nbytes -= EEPROM_BLOCK_SIZE;
address++;
buffer += EEPROM_BLOCK_SIZE;
osSetTimer(&D_80196548, 12000 * osClockRate / 1000000, 0, &_osContMesgQueue, _osContMesgBuff);
osRecvMesg(&_osContMesgQueue, NULL, OS_MESG_BLOCK);
}
Expand Down
10 changes: 5 additions & 5 deletions src/os/osEepromLongWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ extern u8 _osContNumControllers;
extern OSTimer D_80196548;
extern OSMesgQueue _osContMesgQueue;
extern OSMesg _osContMesgBuff[4];
// exactly the same as osEepromLongRead except for osEepromWrite call

s32 osEepromLongWrite(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
// exactly the same as osEepromLongRead except for osEepromWrite call
s32 osEepromLongWrite(OSMesgQueue *mq, u8 address, u8 *buffer, s32 nbytes) {
s32 result = 0;
if (address > 0x40) {
return -1;
Expand All @@ -20,9 +20,9 @@ s32 osEepromLongWrite(OSMesgQueue *mq, u8 address, u8 *buffer, int nbytes) {
return result;
}

nbytes -= 8;
address += 1;
buffer += 8;
nbytes -= EEPROM_BLOCK_SIZE;
address++;
buffer += EEPROM_BLOCK_SIZE;
osSetTimer(&D_80196548, 12000 * osClockRate / 1000000, 0, &_osContMesgQueue, _osContMesgBuff);
osRecvMesg(&_osContMesgQueue, NULL, OS_MESG_BLOCK);
}
Expand Down
25 changes: 8 additions & 17 deletions src/os/osEepromProbe.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
#include "macros.h"
#include "libultra_internal.h"
#include "controller.h"

//! @todo merge with osEepromWrite
typedef struct {
u16 unk00;
u8 unk02;
u8 unk03;
} unkStruct;

s32 __osEepStatus(OSMesgQueue *, unkStruct *);
s32 __osEepStatus(OSMesgQueue *, OSContStatus *);
s32 osEepromProbe(OSMesgQueue *mq) {
s32 status = 0;
unkStruct sp18;
s32 ret = 0;
OSContStatus status;

__osSiGetAccess();
status = __osEepStatus(mq, &sp18);
if (status == 0 && (sp18.unk00 & 0x8000) != 0) {
status = 1;
} else {
status = 0;
}
ret = __osEepStatus(mq, &status);
ret = (ret == 0 && (status.type & CONT_EEPROM) != 0) ? EEPROM_TYPE_4K : 0;
__osSiRelAccess();
return status;
return ret;
}
120 changes: 63 additions & 57 deletions src/os/osEepromRead.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#include "libultra_internal.h"
#include "controller.h"
#include "macros.h"

extern u8 _osLastSentSiCmd;
extern u8 __osContLastCmd;

extern u32 __osEepPifRam[15];
extern u32 D_8019769C;
extern OSPifRam __osEepPifRam;

typedef struct {
u16 unk00;
u8 unk02;
u8 unk03;
} unkStruct;
typedef struct {
u8 unk00;
u8 unk01;
Expand All @@ -29,74 +25,84 @@ typedef struct {
unkStruct3 unk04;
} unkStruct2;

s32 __osEepStatus(OSMesgQueue *, unkStruct *);
s32 __osPackEepReadData(u8);
s32 __osEepStatus(OSMesgQueue *, OSContStatus *);
void __osPackEepReadData(u8);

s32 osEepromRead(OSMesgQueue *mq, u8 address, u8 *buffer) {
s32 sp34;
s32 sp30;
u8 *sp2c;
unkStruct sp28;
unkStruct2 sp20;
sp34 = 0;
sp30 = 0;
sp2c = (u8 *) &__osEepPifRam;
if (address > 0x40) {
s32 ret = 0;
s32 i = 0;
u8 *ptr = (u8 *) &__osEepPifRam.ramarray;
OSContStatus sdata;
__OSContEepromFormat eepromformat;

if (address > EEPROM_MAXBLOCKS) {
return -1;
}

__osSiGetAccess();
sp34 = __osEepStatus(mq, &sp28);
if (sp34 != 0 || sp28.unk00 != 0x8000) {
ret = __osEepStatus(mq, &sdata);
if (ret != 0 || sdata.type != CONT_EEPROM) {

return 8;
return CONT_NO_RESPONSE_ERROR;
}
while (sp28.unk02 & 0x80) {
__osEepStatus(mq, &sp28);

while (sdata.status & CONT_EEPROM_BUSY) {
__osEepStatus(mq, &sdata);
}
__osPackEepReadData(address);
sp34 = __osSiRawStartDma(OS_WRITE, &__osEepPifRam);

ret = __osSiRawStartDma(OS_WRITE, &__osEepPifRam);
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
for (sp30 = 0; sp30 < 0x10; sp30++) {
__osEepPifRam[sp30] = 255;

for (i = 0; i < ARRAY_COUNT(__osEepPifRam.ramarray) + 1; i++) {
__osEepPifRam.ramarray[i] = CONT_CMD_NOP;
}
D_8019769C = 0;
sp34 = __osSiRawStartDma(OS_READ, __osEepPifRam);
_osLastSentSiCmd = 4;
__osEepPifRam.pifstatus = 0;

ret = __osSiRawStartDma(OS_READ, &__osEepPifRam);
__osContLastCmd = CONT_CMD_READ_EEPROM;
osRecvMesg(mq, NULL, OS_MESG_BLOCK);
for (sp30 = 0; sp30 < 4; sp30++) {
sp2c++;

for (i = 0; i < 4; i++) {
ptr++;
}
sp20 = *(unkStruct2 *) sp2c;
sp34 = (sp20.unk01 & 0xc0) >> 4;
if (sp34 == 0) {
for (sp30 = 0; sp30 < 8; sp30++) {
*buffer++ = ((u8 *) &sp20.unk04)[sp30];

eepromformat = *(__OSContEepromFormat *) ptr;
ret = CHNL_ERR(eepromformat);

if (ret == 0) {
for (i = 0; i < 8; i++) {
*buffer++ = eepromformat.data[i];
}
}

__osSiRelAccess();
return sp34;
return ret;
}

s32 __osPackEepReadData(u8 address) {
u8 *sp14;
unkStruct2 sp8;
s32 sp4;
sp14 = (u8 *) &__osEepPifRam;
for (sp4 = 0; sp4 < 0x10; sp4++) {
__osEepPifRam[sp4] = 255;
void __osPackEepReadData(u8 address) {
u8 *ptr = (u8 *) &__osEepPifRam.ramarray;
__OSContEepromFormat eepromformat;
s32 i;

for (i = 0; i < ARRAY_COUNT(__osEepPifRam.ramarray) + 1; i++) {
__osEepPifRam.ramarray[i] = CONT_CMD_NOP;
}
D_8019769C = 1;
sp8.unk00 = 2;
sp8.unk01 = 8;
sp8.unk02 = 4;
sp8.unk03 = address;
for (sp4 = 0; sp4 < 8; sp4++) {
((u8 *) &sp8.unk04)[sp4] = 0;
__osEepPifRam.pifstatus = CONT_CMD_EXE;

eepromformat.txsize = CONT_CMD_READ_EEPROM_TX;
eepromformat.rxsize = CONT_CMD_READ_EEPROM_RX;
eepromformat.cmd = CONT_CMD_READ_EEPROM;
eepromformat.address = address;
for (i = 0; i < ARRAY_COUNT(eepromformat.data); i++) {
eepromformat.data[i] = 0;
}
for (sp4 = 0; sp4 < 4; sp4++) {
*sp14++ = 0;

for (i = 0; i < 4; i++) {
*ptr++ = 0;
}
*(unkStruct2 *) sp14 = sp8;
sp14 += 0xc;
*sp14 = 254;

*(__OSContEepromFormat *) ptr = eepromformat;
ptr += sizeof(__OSContEepromFormat);
*ptr = CONT_CMD_END;
}
Loading

0 comments on commit 3dd4c07

Please sign in to comment.