Skip to content

Commit

Permalink
m_Do_MemCardRWmng work
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Jul 14, 2024
1 parent ebfca99 commit 7cc7ad2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/d/d_com_inf_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class dComIfG_play_c {

JKRAramBlock* getPictureBoxData(int i) { return mPictureBoxData[i]; }
void setPictureBoxData(JKRAramBlock* aramBlock, int i) { mPictureBoxData[i] = aramBlock; }
bool isPictureFlag(u8 i) { return mPictureFlag & (1 << i); }
bool isPictureFlag(u8 i) { return mPictureFlag & (u8)(1 << i); }
void offPictureFlag(u8 i) {
u8 mask = (1 << i);
mPictureFlag &= ~mask;
Expand Down
33 changes: 29 additions & 4 deletions include/m_Do/m_Do_MemCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ class mDoMemCd_Ctrl_c {
s32 checkspace();
void setCardState(s32);

bool isCardCommNone() { return mCardCommand != NULL; }
bool isCardCommNone() { return mCommand != CARD_NO_COMMAND; }
u8 getNowSlot() { return mCardSlot; }
u8* getPictDataPtr() { return mPictDataPtr; }
u8* getPictWriteDataPtr() { return mPictDataWritePtr; }
void setCardSerialNo(u64 v) { mCardSerialNo = v; }
void setDataVersion(u32 v) { mDataVersion = v; }

/* 0x0000 */ u8 mData[0x1650];
/* 0x1650 */ u8* mCardCommand;
/* 0x1654 */ u8* mCardBuf;
/* 0x1650 */ u8* mPictDataPtr;
/* 0x1654 */ u8* mPictDataWritePtr;
/* 0x1658 */ u8 mCardSlot;
/* 0x1659 */ u8 field_0x1659;
/* 0x165A */ u8 field_0x165A;
Expand All @@ -60,7 +65,7 @@ class mDoMemCd_Ctrl_c {
/* 0x167C */ OSCond mCond;
/* 0x1684 */ u32 field_0x1684;
/* 0x1688 */ u64 mCardSerialNo;
/* 0x1690 */ u32 field_0x1690;
/* 0x1690 */ u32 mDataVersion;
/* 0x1694 */ u32 field_0x1694;
}; // Size: 0x1698

Expand All @@ -80,4 +85,24 @@ inline void mDoMemCd_save(void* i_data, u32 param_1, u32 param_2) {
g_mDoMemCd_control.save(i_data,param_1,param_2);
}

inline u8 mDoMemCd_getNowSlot() {
return g_mDoMemCd_control.getNowSlot();
}

inline void mDoMemCd_setCardSerialNo(u64 v) {
g_mDoMemCd_control.setCardSerialNo(v);
}

inline void mDoMemCd_setDataVersion(u32 v) {
g_mDoMemCd_control.setDataVersion(v);
}

inline u8* mDoMemCd_getPictDataPtr() {
return g_mDoMemCd_control.getPictDataPtr();
}

inline u8* mDoMemCd_getPictWriteDataPtr() {
return g_mDoMemCd_control.getPictWriteDataPtr();
}

#endif /* M_DO_M_DO_MEMCARD_H */
4 changes: 2 additions & 2 deletions src/m_Do/m_Do_MemCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ mDoMemCd_Ctrl_c::mDoMemCd_Ctrl_c() {
/* 80018D70-80018E18 .text ThdInit__15mDoMemCd_Ctrl_cFv */
void mDoMemCd_Ctrl_c::ThdInit() {
CARDInit();
mCardCommand = NULL;
mCardBuf = NULL;
mPictDataPtr = NULL;
mPictDataWritePtr = NULL;
field_0x1659 = 0;
field_0x165A = 2;
field_0x1660 = CARD_NO_COMMAND;
Expand Down
52 changes: 25 additions & 27 deletions src/m_Do/m_Do_MemCardRWmng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct card_gamedata
struct card_savedata
{
u32 saveCount;
u32 field_0x04;
u32 dataVersion;
card_gamedata gamedata[3];
u32 field_0x1658[0x269];
u32 csum;
Expand All @@ -52,6 +52,7 @@ static u32 sSaveCount;
/* 80019940-80019CE8 .text mDoMemCdRWm_Store__FP12CARDFileInfoPvUl */
s32 mDoMemCdRWm_Store(CARDFileInfo* card, void* data, u32 size) {
/* Nonmatching */

s32 ret;
mDoMemCdRWm_BuildHeader((mDoMemCdRWm_HeaderData*)sTmpBuf);
ret = CARDWrite(card, sTmpBuf, 0x2000, 0x0000);
Expand All @@ -68,10 +69,10 @@ s32 mDoMemCdRWm_Store(CARDFileInfo* card, void* data, u32 size) {

memset(sTmpBuf, 0, 0x2000);
card_savedata* save = (card_savedata*)sTmpBuf;
save->field_0x04 = 0;
save->dataVersion = 0;
memcpy(save->gamedata, data, size);
save->saveCount = ++sSaveCount;
s32 csum = mDoMemCdRWm_CalcCheckSum(data, 0x1FFC);
s32 csum = mDoMemCdRWm_CalcCheckSum(save, 0x1FFC);
save->csum = csum;

ret = CARDWrite(card, sTmpBuf, 0x2000, 0x2000);
Expand All @@ -86,34 +87,32 @@ s32 mDoMemCdRWm_Store(CARDFileInfo* card, void* data, u32 size) {
if (ret != CARD_ERROR_READY) return ret;
if (mDoMemCdRWm_CalcCheckSum(sTmpBuf, 0x1FFC) != csum) return ret;

if (g_mDoMemCd_control.mCardBuf != NULL) {
u32 slot = g_mDoMemCd_control.field_0x1659;
if (slot < 3) {
u8* picData = g_mDoMemCd_control.mCardBuf;
for (u32 i = 0; i < 3; i++, picData += 0x2000) {
u32 cardOffset = (slot * 3 + 3 + i) * 0x2000;
ret = CARDWrite(card, picData, 0x2000, cardOffset);
if (mDoMemCd_getPictWriteDataPtr() != NULL) {
// mDoMemCd_getCopyToPos ?
if (g_mDoMemCd_control.field_0x1659 < 3) {
u32 slot = g_mDoMemCd_control.field_0x1659 * 3 + 3;
u8* picData = mDoMemCd_getPictWriteDataPtr();
for (s32 i = 0; i < 3; i++, picData += 0x2000) {
ret = CARDWrite(card, picData, 0x2000, (slot + i) * 0x2000);
if (ret != CARD_ERROR_READY) return ret;
ret = CARDRead(card, picData, 0x2000, cardOffset);
ret = CARDRead(card, sTmpBuf, 0x2000, (slot + i) * 0x2000);
if (ret != CARD_ERROR_READY) return ret;
if (!mDoMemCdRWm_TestCheckSumPictData(picData)) return CARD_ERROR_READY;
ret = CARD_ERROR_READY;
if (!mDoMemCdRWm_TestCheckSumPictData(picData)) return ret;
}
}
} else {
u8 dataNum = dComIfGs_getDataNum();
u32 slot = dComIfGs_getDataNum() * 3 + 3;
for (s32 i = 0; i < 3; i++) {
if (dComIfGp_isPictureFlag(i)) {
memset(sTmpBuf, 0, 0x2000);
JKRAramToMainRam(dComIfGp_getPictureBoxData(i), sTmpBuf, 0x2000);
u32 cardOffset = (dataNum * 3 + 3 + i) * 0x2000;
ret = CARDWrite(card, sTmpBuf, 0x2000, cardOffset);
ret = CARDWrite(card, sTmpBuf, 0x2000, (slot + i) * 0x2000);
if (ret != CARD_ERROR_READY) return ret;
ret = CARDRead(card, sTmpBuf, 0x2000, cardOffset);
ret = CARDRead(card, sTmpBuf, 0x2000, (slot + i) * 0x2000);
if (ret != CARD_ERROR_READY) return ret;
}
}
}
}

mDoMemCdRWm_SetCardStat(card);
return ret;
Expand Down Expand Up @@ -171,15 +170,14 @@ s32 mDoMemCdRWm_Restore(CARDFileInfo* card, void* dst, u32 size) {
if (ret != CARD_ERROR_READY) return ret;
memcpy(dst, save->gamedata, size);
sSaveCount = save->saveCount;
g_mDoMemCd_control.field_0x1690 = save->field_0x04;
if (!invalid && g_mDoMemCd_control.mCardCommand != NULL) {
ret = CARDRead(card, g_mDoMemCd_control.mCardCommand, 0x12000, 0x6000);
mDoMemCd_setDataVersion(save->dataVersion);
if (!invalid && mDoMemCd_getPictDataPtr() != NULL) {
ret = CARDRead(card, mDoMemCd_getPictDataPtr(), 0x12000, 0x6000);
if (ret != CARD_ERROR_READY) return ret;
}

serialNo;
CARDGetSerialNo(g_mDoMemCd_control.mCardSlot, &serialNo);
g_mDoMemCd_control.mCardSerialNo = serialNo;
CARDGetSerialNo(mDoMemCd_getNowSlot(), &serialNo);
mDoMemCd_setCardSerialNo(serialNo);
}

return CARD_ERROR_READY;
Expand Down Expand Up @@ -212,7 +210,7 @@ void mDoMemCdRWm_BuildHeader(mDoMemCdRWm_HeaderData* header) {
/* 8001A0A8-8001A1EC .text mDoMemCdRWm_SetCardStat__FP12CARDFileInfo */
void mDoMemCdRWm_SetCardStat(CARDFileInfo* card) {
CARDStat stat;
CARDGetStatus(g_mDoMemCd_control.mCardSlot, card->fileNo, &stat);
CARDGetStatus(mDoMemCd_getNowSlot(), card->fileNo, &stat);
stat.iconAddr = 0;
stat.commentAddr = offsetof(mDoMemCdRWm_HeaderData, comment);
CARDSetBannerFormat(&stat, 1);
Expand All @@ -233,13 +231,13 @@ void mDoMemCdRWm_SetCardStat(CARDFileInfo* card) {
CARDSetIconSpeed(&stat, 5, 0);
CARDSetIconSpeed(&stat, 6, 0);
CARDSetIconSpeed(&stat, 7, 0);
CARDSetStatus(g_mDoMemCd_control.mCardSlot, card->fileNo, &stat);;
CARDSetStatus(mDoMemCd_getNowSlot(), card->fileNo, &stat);;
}

/* 8001A1EC-8001A2F0 .text mDoMemCdRWm_CheckCardStat__FP12CARDFileInfo */
BOOL mDoMemCdRWm_CheckCardStat(CARDFileInfo* card) {
CARDStat stat;
CARDGetStatus(g_mDoMemCd_control.mCardSlot, card->fileNo, &stat);
CARDGetStatus(mDoMemCd_getNowSlot(), card->fileNo, &stat);
if (!(stat.iconAddr == 0 &&
stat.commentAddr == 0x1C00 &&
CARDGetBannerFormat(&stat) == 1 &&
Expand Down

0 comments on commit 7cc7ad2

Please sign in to comment.