From 4a3ef802d5fc74e85922c2343b0ce95aa48e3d62 Mon Sep 17 00:00:00 2001 From: Denis Kopyrin Date: Sun, 27 Mar 2022 10:39:44 +0800 Subject: [PATCH] Fixed eeprom saving on VC (#334) * Fixed eeprom saving on VC * Specify SM for EEP4K, for any other savetype use ED * Moved IS_VC() check to an earlier point, switch IS_VC()->gIsVC * Moved binary VC files to bin folder, added checks for EEP * Do not keep vc_bin if EEP is not set --- asm/rom_header.s | 4 +++ asm/vc_bin.s | 23 +++++++++++++ bin/vc/osEepStatusVC.bin | Bin 0 -> 560 bytes bin/vc/osEepromReadVC.bin | Bin 0 -> 512 bytes bin/vc/osEepromWriteVC.bin | Bin 0 -> 448 bytes sm64.ld | 3 ++ src/boot/main.c | 3 ++ src/game/game_init.c | 5 ++- src/game/save_file.c | 11 ++++-- src/game/vc_bin.h | 7 ++++ src/game/vc_ultra.c | 68 +++++++++++++++++++++++++++++++++++++ src/game/vc_ultra.h | 7 ++++ 12 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 asm/vc_bin.s create mode 100644 bin/vc/osEepStatusVC.bin create mode 100644 bin/vc/osEepromReadVC.bin create mode 100644 bin/vc/osEepromWriteVC.bin create mode 100644 src/game/vc_bin.h create mode 100644 src/game/vc_ultra.c create mode 100644 src/game/vc_ultra.h diff --git a/asm/rom_header.s b/asm/rom_header.s index cc9432d0..0673aee8 100644 --- a/asm/rom_header.s +++ b/asm/rom_header.s @@ -17,7 +17,11 @@ .ascii INTERNAL_ROM_NAME /* Internal ROM name */ .word 0x00000000 /* Unknown */ .word 0x0000004E /* Cartridge */ +#if defined(EEP4K) +.ascii "SM" /* Cartridge ID */ +#else .ascii "ED" /* Cartridge ID */ +#endif /* Region */ #if defined(VERSION_JP) || defined(VERSION_SH) diff --git a/asm/vc_bin.s b/asm/vc_bin.s new file mode 100644 index 00000000..7a89d676 --- /dev/null +++ b/asm/vc_bin.s @@ -0,0 +1,23 @@ +// VC is using binary matching to stub functions - this hex was copied from SM64 +// osEepromRead/Write are performing infinite cycle inside them which causes recompiler do go crazy +// _osEepStatus does not have any suspicious asm in it but it is just VC hacked anyways +// If it is not replaced with a VC variant, EEP probe fails + +.include "macros.inc" +.section .text + +#if defined(EEP) +// 0x80329150 = 0xE4150 +.balign 16 +glabel osEepromReadVC +.incbin "bin/vc/osEepromReadVC.bin" + +// 0x80328af0 = 0xE3AF0 +.balign 16 +glabel osEepromWriteVC +.incbin "bin/vc/osEepromWriteVC.bin" + +.balign 16 +glabel __osEepStatusVC +.incbin "bin/vc/osEepStatusVC.bin" +#endif diff --git a/bin/vc/osEepStatusVC.bin b/bin/vc/osEepStatusVC.bin new file mode 100644 index 0000000000000000000000000000000000000000..e763f78c248255c6eae011d163359c959f2150a8 GIT binary patch literal 560 zcmZvaKX21O7{-6DU798qNu>^{6p9UZataK!?3zgV zx2{;X`}12D4@URPDcz$?AF;of`hd46}n`R4oJe%#kzqoKKA-+boSOz4@E z(5z6hb`v9A(j8cFpXO!E^O|3H(J<&@9a*&Yz0GekmHm3$SG!o%oPHGzi=(J8^IMGt zDtiaXO0)`;{w8OC;^+~U5&avDB}%o~r~@oj!xpWLs!KPdT{iiePG0gLpK_R|Jj`c! z`5ZrA@F*{M9IWV!uH=qNr_P_jcHEz3P-fVpA2@0f4V8m6D{;z_on$ILcBz>e%5~4) zW>3JJ5hlU_y4rO=pzev62HRDpN-upn{8;H!^Wf?K%}c&;FUCfy1)&wU^q?L0gSIeU z-!WC$wRjb-mET*Ij$R25VR^$t_$T5mnA17&;nc@ozJQYu=QLzz`5I@u=1`dSo;A6o z`QFaHV2?}u@d8iI_BeQn)p(;Pk}X-&8yCXLWlP?)_vsh*UV7(q%Su~X9P32>L2%Bo LeNC;(@NfME^(B@g literal 0 HcmV?d00001 diff --git a/bin/vc/osEepromReadVC.bin b/bin/vc/osEepromReadVC.bin new file mode 100644 index 0000000000000000000000000000000000000000..dd3481a17c216263580cd52e77d698e5809911cf GIT binary patch literal 512 zcmZXRF-yZh6vzK*QcdyHiWMm+q>!UkgH#MfAz)n!Ty+=Tuqp155lyU# zYm~(WY|3KBfGWDM(m=&b#4%$H2NXWtW9;fE-$Fq&VEaATvp`bT&hJpL6%W%nsPas` z`(^@q(8l6Q3f`0fUFyR#f*Y!B!<#vXzh#ZF=1b6|{gPhEY*k(2?wJ~tUly@}0%z<5 z<_KxDryo%Y6dlDYVchkwtmjOIIJBxR|AexnAe}Md578Ukk#j?%8ve}&3~pwj$$rVW zSa(n5e*ZW(#A(Cg+fWt`q45;)Z>3N1m*~LqLs?hIx{uLFKqFlz zeT;NH=wW^}iw9&k)fr$?vgD ZLYe&8`Sn!(1^r{bk2Upm;Iq_+;|C06hZq0= literal 0 HcmV?d00001 diff --git a/bin/vc/osEepromWriteVC.bin b/bin/vc/osEepromWriteVC.bin new file mode 100644 index 0000000000000000000000000000000000000000..9bc94ae5b0fabe4791b4a6abd13e7789aba821a7 GIT binary patch literal 448 zcmZXRF-yZh7>3_8siqjEVnqr?QbJcSJ3b0XzEcP^eXk?&< zV{Gr`5UlG^g}&$!PN>&Guz8w(i(WWFll&_zLd(qtbtCdUVjmj{q+cHKfr_bV7ciDc z8=%G#S{yZ33hG$(0_>_~Lqi(dzGr?y+L4ei=;;Ta&d&(nSoh;UZ$LMVBC=v$Fa`(S zrMTli&V)E)*k%V(^$d!cBmO;V + +s32 osEepromReadVC(OSMesgQueue *mq, u8 address, u8 *buffer); +s32 osEepromWriteVC(OSMesgQueue *mq, u8 address, u8 *buffer); +s32 __osEepStatusVC(OSMesgQueue *mq, OSContStatus *data); diff --git a/src/game/vc_ultra.c b/src/game/vc_ultra.c new file mode 100644 index 00000000..fe11661e --- /dev/null +++ b/src/game/vc_ultra.c @@ -0,0 +1,68 @@ +#include "vc_ultra.h" + +#include "vc_bin.h" + +#ifdef EEP +// These functions are real implementations of libultra functions +// with an exception that some VC useless code was dropped from them +// for the cleaner understanding purposes. +// These functions are generally wrappers around binary stubs from vc_bin +s32 osEepromProbeVC(OSMesgQueue *mq) +{ + s32 ret; + u16 type; + ret = 0; + OSContStatus sdata; + ret = __osEepStatusVC(mq, &sdata); // call to binary stub + type = sdata.type & (CONT_EEPROM | CONT_EEP16K); + if (ret != 0) + { + ret = 0; + } + else + { + switch (type) + { + case CONT_EEPROM: + ret = EEPROM_TYPE_4K; + break; + case CONT_EEPROM | CONT_EEP16K: + ret = EEPROM_TYPE_16K; + break; + default: + ret = 0; + break; + } + } + + return ret; +} + +s32 osEepromLongReadVC(OSMesgQueue *mq, u8 address, u8 *buffer, int length) +{ + s32 ret; + ret = 0; + while (length > 0) + { + osEepromReadVC(mq, address, buffer); // call to binary stub + length -= EEPROM_BLOCK_SIZE; + address++; + buffer += EEPROM_BLOCK_SIZE; + } + return ret; +} + +s32 osEepromLongWriteVC(OSMesgQueue *mq, u8 address, u8 *buffer, int length) +{ + s32 ret; + ret = 0; + while (length > 0) + { + osEepromWriteVC(mq, address, buffer); // call to binary stub + length -= EEPROM_BLOCK_SIZE; + address++; + buffer += EEPROM_BLOCK_SIZE; + } + return ret; +} +#endif diff --git a/src/game/vc_ultra.h b/src/game/vc_ultra.h new file mode 100644 index 00000000..0faf942a --- /dev/null +++ b/src/game/vc_ultra.h @@ -0,0 +1,7 @@ +// Some libultra functions that use VC alternatives + +#include + +s32 osEepromProbeVC(OSMesgQueue *mq); +s32 osEepromLongReadVC(OSMesgQueue *mq, u8 address, u8 *buffer, int length); +s32 osEepromLongWriteVC(OSMesgQueue *mq, u8 address, u8 *buffer, int length);