Skip to content

Commit 1c49a8a

Browse files
committed
nn_nfp: Implement GetNfpReadOnlyInfo and fix deactivate event
Fixes Amiibos not being detected in MK8
1 parent 47001ad commit 1c49a8a

File tree

1 file changed

+59
-41
lines changed

1 file changed

+59
-41
lines changed

src/Cafe/OS/libs/nn_nfp/nn_nfp.cpp

+59-41
Original file line numberDiff line numberDiff line change
@@ -334,45 +334,63 @@ void nnNfpExport_MountRom(PPCInterpreter_t* hCPU)
334334
osLib_returnFromFunction(hCPU, BUILD_NN_RESULT(NN_RESULT_LEVEL_SUCCESS, NN_RESULT_MODULE_NN_NFP, 0));
335335
}
336336

337-
typedef struct
338-
{
339-
/* +0x00 */ uint8 characterId[3];
340-
/* +0x03 */ uint8 amiiboSeries;
341-
/* +0x04 */ uint16be number;
342-
/* +0x06 */ uint8 nfpType;
343-
/* +0x07 */ uint8 unused[0x2F];
344-
}nfpRomInfo_t;
345-
346-
static_assert(offsetof(nfpRomInfo_t, amiiboSeries) == 0x3, "nfpRomInfo.seriesId has invalid offset");
347-
static_assert(offsetof(nfpRomInfo_t, number) == 0x4, "nfpRomInfo.number has invalid offset");
348-
static_assert(offsetof(nfpRomInfo_t, nfpType) == 0x6, "nfpRomInfo.nfpType has invalid offset");
349-
static_assert(sizeof(nfpRomInfo_t) == 0x36, "nfpRomInfo_t has invalid size");
350-
351-
void nnNfpExport_GetNfpRomInfo(PPCInterpreter_t* hCPU)
337+
namespace nn::nfp
352338
{
353-
cemuLog_log(LogType::NN_NFP, "GetNfpRomInfo(0x{:08x})", hCPU->gpr[3]);
354-
ppcDefineParamStructPtr(romInfo, nfpRomInfo_t, 0);
339+
struct RomInfo
340+
{
341+
/* +0x00 */ uint8 characterId[3];
342+
/* +0x03 */ uint8 amiiboSeries;
343+
/* +0x04 */ uint16be number;
344+
/* +0x06 */ uint8 nfpType;
345+
/* +0x07 */ uint8 unused[0x2F];
346+
};
355347

356-
nnNfpLock();
357-
if (nfp_data.hasActiveAmiibo == false)
348+
static_assert(offsetof(RomInfo, amiiboSeries) == 0x3);
349+
static_assert(offsetof(RomInfo, number) == 0x4);
350+
static_assert(offsetof(RomInfo, nfpType) == 0x6);
351+
static_assert(sizeof(RomInfo) == 0x36);
352+
353+
using ReadOnlyInfo = RomInfo; // same layout
354+
355+
void GetRomInfo(RomInfo* romInfo)
358356
{
359-
nnNfpUnlock();
360-
osLib_returnFromFunction(hCPU, BUILD_NN_RESULT(NN_RESULT_LEVEL_STATUS, NN_RESULT_MODULE_NN_NFP, 0)); // todo: Return correct error code
361-
return;
357+
cemu_assert_debug(nfp_data.hasActiveAmiibo);
358+
memset(romInfo, 0x00, sizeof(RomInfo));
359+
romInfo->characterId[0] = nfp_data.amiiboNFCData.amiiboIdentificationBlock.gameAndCharacterId[0];
360+
romInfo->characterId[1] = nfp_data.amiiboNFCData.amiiboIdentificationBlock.gameAndCharacterId[1];
361+
romInfo->characterId[2] = nfp_data.amiiboNFCData.amiiboIdentificationBlock.characterVariation; // guessed
362+
romInfo->amiiboSeries = nfp_data.amiiboNFCData.amiiboIdentificationBlock.amiiboSeries; // guessed
363+
romInfo->number = *(uint16be*)nfp_data.amiiboNFCData.amiiboIdentificationBlock.amiiboModelNumber; // guessed
364+
romInfo->nfpType = nfp_data.amiiboNFCData.amiiboIdentificationBlock.amiiboFigureType; // guessed
365+
memset(romInfo->unused, 0x00, sizeof(romInfo->unused));
362366
}
363-
memset(romInfo, 0x00, sizeof(nfpRomInfo_t));
364367

365-
romInfo->characterId[0] = nfp_data.amiiboNFCData.amiiboIdentificationBlock.gameAndCharacterId[0];
366-
romInfo->characterId[1] = nfp_data.amiiboNFCData.amiiboIdentificationBlock.gameAndCharacterId[1];
367-
romInfo->characterId[2] = nfp_data.amiiboNFCData.amiiboIdentificationBlock.characterVariation; // guessed
368-
369-
romInfo->amiiboSeries = nfp_data.amiiboNFCData.amiiboIdentificationBlock.amiiboSeries; // guessed
370-
romInfo->number = *(uint16be*)nfp_data.amiiboNFCData.amiiboIdentificationBlock.amiiboModelNumber; // guessed
371-
romInfo->nfpType = nfp_data.amiiboNFCData.amiiboIdentificationBlock.amiiboFigureType; // guessed
368+
nnResult GetNfpRomInfo(RomInfo* romInfo)
369+
{
370+
nnNfpLock();
371+
if (nfp_data.hasActiveAmiibo == false)
372+
{
373+
nnNfpUnlock();
374+
return BUILD_NN_RESULT(NN_RESULT_LEVEL_STATUS, NN_RESULT_MODULE_NN_NFP, 0); // todo: Return correct error code
375+
}
376+
GetRomInfo(romInfo);
377+
nnNfpUnlock();
378+
return BUILD_NN_RESULT(NN_RESULT_LEVEL_SUCCESS, NN_RESULT_MODULE_NN_NFP, 0);
379+
}
372380

373-
nnNfpUnlock();
374-
osLib_returnFromFunction(hCPU, BUILD_NN_RESULT(NN_RESULT_LEVEL_SUCCESS, NN_RESULT_MODULE_NN_NFP, 0));
375-
}
381+
nnResult GetNfpReadOnlyInfo(ReadOnlyInfo* readOnlyInfo)
382+
{
383+
nnNfpLock();
384+
if (nfp_data.hasActiveAmiibo == false)
385+
{
386+
nnNfpUnlock();
387+
return BUILD_NN_RESULT(NN_RESULT_LEVEL_STATUS, NN_RESULT_MODULE_NN_NFP, 0); // todo: Return correct error code
388+
}
389+
GetRomInfo(readOnlyInfo);
390+
nnNfpUnlock();
391+
return BUILD_NN_RESULT(NN_RESULT_LEVEL_SUCCESS, NN_RESULT_MODULE_NN_NFP, 0);
392+
}
393+
};
376394

377395
typedef struct
378396
{
@@ -880,13 +898,13 @@ void nnNfp_update()
880898
if (amiiboElapsedTouchTime >= 1500)
881899
{
882900
nnNfp_unloadAmiibo();
901+
if (nfp_data.deactivateEvent)
902+
{
903+
coreinit::OSEvent* osEvent = (coreinit::OSEvent*)memory_getPointerFromVirtualOffset(nfp_data.deactivateEvent);
904+
coreinit::OSSignalEvent(osEvent);
905+
}
883906
}
884907
nnNfpUnlock();
885-
if (nfp_data.deactivateEvent)
886-
{
887-
coreinit::OSEvent* osEvent = (coreinit::OSEvent*)memory_getPointerFromVirtualOffset(nfp_data.deactivateEvent);
888-
coreinit::OSSignalEvent(osEvent);
889-
}
890908
}
891909

892910
void nnNfpExport_GetNfpState(PPCInterpreter_t* hCPU)
@@ -1001,8 +1019,6 @@ namespace nn::nfp
10011019
osLib_addFunction("nn_nfp", "Mount__Q2_2nn3nfpFv", nnNfpExport_Mount);
10021020
osLib_addFunction("nn_nfp", "MountRom__Q2_2nn3nfpFv", nnNfpExport_MountRom);
10031021
osLib_addFunction("nn_nfp", "Unmount__Q2_2nn3nfpFv", nnNfpExport_Unmount);
1004-
1005-
osLib_addFunction("nn_nfp", "GetNfpRomInfo__Q2_2nn3nfpFPQ3_2nn3nfp7RomInfo", nnNfpExport_GetNfpRomInfo);
10061022
osLib_addFunction("nn_nfp", "GetNfpCommonInfo__Q2_2nn3nfpFPQ3_2nn3nfp10CommonInfo", nnNfpExport_GetNfpCommonInfo);
10071023
osLib_addFunction("nn_nfp", "GetNfpRegisterInfo__Q2_2nn3nfpFPQ3_2nn3nfp12RegisterInfo", nnNfpExport_GetNfpRegisterInfo);
10081024

@@ -1028,7 +1044,9 @@ namespace nn::nfp
10281044
{
10291045
nnNfp_load(); // legacy interface, update these to use cafeExportRegister / cafeExportRegisterFunc
10301046

1031-
cafeExportRegisterFunc(nn::nfp::GetErrorCode, "nn_nfp", "GetErrorCode__Q2_2nn3nfpFRCQ2_2nn6Result", LogType::Placeholder);
1047+
cafeExportRegisterFunc(nn::nfp::GetErrorCode, "nn_nfp", "GetErrorCode__Q2_2nn3nfpFRCQ2_2nn6Result", LogType::NN_NFP);
1048+
cafeExportRegisterFunc(nn::nfp::GetNfpRomInfo, "nn_nfp", "GetNfpRomInfo__Q2_2nn3nfpFPQ3_2nn3nfp7RomInfo", LogType::NN_NFP);
1049+
cafeExportRegisterFunc(nn::nfp::GetNfpReadOnlyInfo, "nn_nfp", "GetNfpReadOnlyInfo__Q2_2nn3nfpFPQ3_2nn3nfp12ReadOnlyInfo", LogType::NN_NFP);
10321050
}
10331051

10341052
}

0 commit comments

Comments
 (0)