Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 82f8676

Browse files
committed
Libretro: Improve support for retroachievements
1 parent 638cddf commit 82f8676

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

Core/Console.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,17 +894,24 @@ bool Console::IsRecordingTapeFile()
894894
return false;
895895
}
896896

897-
uint8_t* Console::GetRamBuffer(DebugMemoryType memoryType, uint32_t &size)
897+
uint8_t* Console::GetRamBuffer(DebugMemoryType memoryType, uint32_t &size, int32_t &startAddr)
898898
{
899899
//Only used by libretro port for achievements - should not be used by anything else.
900900
switch(memoryType) {
901901
case DebugMemoryType::InternalRam:
902902
size = MemoryManager::InternalRAMSize;
903+
startAddr = 0;
903904
return _memoryManager->GetInternalRAM();
904905

905906
case DebugMemoryType::SaveRam:
906907
size = _mapper->GetMemorySize(DebugMemoryType::SaveRam);
908+
startAddr = _mapper->FromAbsoluteAddress(0, AddressType::SaveRam);
907909
return _mapper->GetSaveRam();
910+
911+
case DebugMemoryType::WorkRam:
912+
size = _mapper->GetMemorySize(DebugMemoryType::WorkRam);
913+
startAddr = _mapper->FromAbsoluteAddress(0, AddressType::WorkRam);
914+
return _mapper->GetWorkRam();
908915
}
909916

910917
throw std::runtime_error("unsupported memory type");

Core/Console.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,5 @@ class Console
143143
static shared_ptr<Console> GetInstance();
144144
static void Release();
145145

146-
uint8_t* GetRamBuffer(DebugMemoryType memoryType, uint32_t &size);
146+
uint8_t* GetRamBuffer(DebugMemoryType memoryType, uint32_t &size, int32_t &startAddr);
147147
};

Libretro/libretro.cpp

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ static retro_environment_t retroEnv = nullptr;
4141
static unsigned _inputDevices[5] = { DEVICE_AUTO, DEVICE_AUTO, DEVICE_AUTO, DEVICE_AUTO, DEVICE_AUTO };
4242
static bool _hdPacksEnabled = false;
4343
static string _mesenVersion = "";
44-
int32_t _saveStateSize = -1;
44+
static int32_t _saveStateSize = -1;
45+
static struct retro_memory_descriptor _descriptors[3];
46+
static struct retro_memory_map _memoryMap;
4547

4648
//Include game database as an array of strings (need an automated way to generate the include file)
4749
static vector<string> gameDb = {
@@ -880,6 +882,45 @@ extern "C" {
880882

881883
EmulationSettings::SetFlagState(EmulationFlags::HasFourScore, hasFourScore);
882884
}
885+
886+
void retro_set_memory_maps()
887+
{
888+
//Expose internal RAM and work/save RAM for retroachievements
889+
memset(_descriptors, 0, sizeof(_descriptors));
890+
memset(&_memoryMap, 0, sizeof(_memoryMap));
891+
892+
uint32_t i = 0;
893+
uint32_t size = 0;
894+
int32_t startAddr = 0;
895+
uint8_t* internalRam = Console::GetInstance()->GetRamBuffer(DebugMemoryType::InternalRam, size, startAddr);
896+
_descriptors[i].ptr = internalRam;
897+
_descriptors[i].start = startAddr;
898+
_descriptors[i].len = size;
899+
_descriptors[i].select = 0;
900+
i++;
901+
902+
uint8_t* saveRam = Console::GetInstance()->GetRamBuffer(DebugMemoryType::SaveRam, size, startAddr);
903+
if(size > 0 && startAddr > 0) {
904+
_descriptors[i].ptr = saveRam;
905+
_descriptors[i].start = startAddr;
906+
_descriptors[i].len = size;
907+
_descriptors[i].select = 0;
908+
i++;
909+
}
910+
911+
uint8_t* workRam = Console::GetInstance()->GetRamBuffer(DebugMemoryType::WorkRam, size, startAddr);
912+
if(size > 0 && startAddr > 0) {
913+
_descriptors[i].ptr = workRam;
914+
_descriptors[i].start = startAddr;
915+
_descriptors[i].len = size;
916+
_descriptors[i].select = 0;
917+
i++;
918+
}
919+
920+
_memoryMap.descriptors = _descriptors;
921+
_memoryMap.num_descriptors = i;
922+
retroEnv(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &_memoryMap);
923+
}
883924

884925
RETRO_API void retro_set_controller_port_device(unsigned port, unsigned device)
885926
{
@@ -927,7 +968,7 @@ extern "C" {
927968

928969
if(result) {
929970
update_core_controllers();
930-
update_input_descriptors();
971+
update_input_descriptors();
931972

932973
//Savestates in Mesen may change size over time
933974
//Retroarch doesn't like this for netplay or rewinding - it requires the states to always be the exact same size
@@ -937,6 +978,7 @@ extern "C" {
937978

938979
//Round up to the next 1kb multiple
939980
_saveStateSize = ((ss.str().size() * 2) + 0x400) & ~0x3FF;
981+
retro_set_memory_maps();
940982
}
941983

942984
return result;
@@ -997,19 +1039,21 @@ extern "C" {
9971039
RETRO_API void *retro_get_memory_data(unsigned id)
9981040
{
9991041
uint32_t size;
1042+
int32_t startAddr;
10001043
switch(id) {
1001-
case RETRO_MEMORY_SAVE_RAM: return Console::GetInstance()->GetRamBuffer(DebugMemoryType::SaveRam, size);
1002-
case RETRO_MEMORY_SYSTEM_RAM: return Console::GetInstance()->GetRamBuffer(DebugMemoryType::InternalRam, size);
1044+
case RETRO_MEMORY_SAVE_RAM: return Console::GetInstance()->GetRamBuffer(DebugMemoryType::SaveRam, size, startAddr);
1045+
case RETRO_MEMORY_SYSTEM_RAM: return Console::GetInstance()->GetRamBuffer(DebugMemoryType::InternalRam, size, startAddr);
10031046
}
10041047
return nullptr;
10051048
}
10061049

10071050
RETRO_API size_t retro_get_memory_size(unsigned id)
10081051
{
10091052
uint32_t size = 0;
1053+
int32_t startAddr;
10101054
switch(id) {
1011-
case RETRO_MEMORY_SAVE_RAM: Console::GetInstance()->GetRamBuffer(DebugMemoryType::SaveRam, size); break;
1012-
case RETRO_MEMORY_SYSTEM_RAM: Console::GetInstance()->GetRamBuffer(DebugMemoryType::InternalRam, size); break;
1055+
case RETRO_MEMORY_SAVE_RAM: Console::GetInstance()->GetRamBuffer(DebugMemoryType::SaveRam, size, startAddr); break;
1056+
case RETRO_MEMORY_SYSTEM_RAM: Console::GetInstance()->GetRamBuffer(DebugMemoryType::InternalRam, size, startAddr); break;
10131057
}
10141058
return size;
10151059
}

0 commit comments

Comments
 (0)