Feature/runtime ecosystem refactor (#107)

* feat: remove memory and pad from stub section

* feat: add support for resume entry targets in CodeGenerator (this allow jumps in address outside function)
feat: refactor entry point discovery one more try to reduce big generated file

* feat: optmizations for release build

* feat: remove unused  file

* feat: added some test cases for code gen

* feat: added log macro and remove win specific code

* feat: refactor runtime folder structure
feat: added reset sound driver RPC state and compatibility layout
feat: rename and added new test
feat: update RPC calls to use defined constants
feat: added more PSMC(16, 32)
feat: change cd read to try find the asset ignoring case sensitive
fix: fix some render problems
feat: add logs on pad
feat: added more RPC handles

* feat: added game override for code veronica

* feat: apply vita patch

* feat: flags to disable build

* feat: fix merges
feat: break a lot of tests

* feat: better throw error on empty cd path
feat: remove recompiler unusde function
feat: apply missing patch

* feat: gamedp is now part of lib
feat: missing file

* feat: small cleanup

* feat: missing vita changes

* feat: fix more merge

* feat: fix tests

* feat: last missing feature

* feat: added missing import

* feat: rename test local functions

* feat: init syscall on ps2 list

* feat: added DMA helpers

* feat: faster builds
feat: more implement for darkcloud

* feat: back missing file

* feat: added missing includes

* feat: remove test

* feat: missing include

* feat: read register funtion

* feat: build  fix

* feat: force  exit on detach thread
This commit is contained in:
Ranieri
2026-04-04 23:09:56 -03:00
committed by GitHub
parent 553a9027d8
commit 93e221feaa
137 changed files with 26594 additions and 15314 deletions
+324
View File
@@ -16,11 +16,40 @@ namespace
// Guest memory address ranges for test data
constexpr uint32_t GUEST_STRING_AREA_START = 0x1000;
constexpr uint32_t GUEST_BUFFER_AREA_START = 0x2000;
constexpr uint32_t GUEST_STACK_AREA_START = 0x6000;
constexpr uint32_t GUEST_MC_SYNC_CMD_ADDR = GUEST_BUFFER_AREA_START + 0x1C00;
constexpr uint32_t GUEST_MC_SYNC_RESULT_ADDR = GUEST_BUFFER_AREA_START + 0x1C04;
constexpr uint32_t GUEST_MC_TABLE_ADDR = GUEST_BUFFER_AREA_START + 0x2000;
// Common file I/O flag combinations
constexpr uint32_t PS2_FIO_WRITE_CREATE_TRUNC =
PS2_FIO_O_WRONLY | PS2_FIO_O_CREAT | PS2_FIO_O_TRUNC;
struct SceMcStDateTime
{
uint8_t resv2;
uint8_t sec;
uint8_t min;
uint8_t hour;
uint8_t day;
uint8_t month;
uint16_t year;
};
struct SceMcTblGetDir
{
SceMcStDateTime create;
SceMcStDateTime modify;
uint32_t fileSizeByte;
uint16_t attrFile;
uint16_t reserve1;
uint32_t reserve2;
uint32_t pdaAplNo;
char entryName[32];
};
static_assert(sizeof(SceMcTblGetDir) == 64, "sceMcTblGetDir size mismatch");
void setRegU32(R5900Context &ctx, int reg, uint32_t value)
{
ctx.r[reg] = _mm_set_epi64x(0, static_cast<int64_t>(value));
@@ -36,6 +65,51 @@ namespace
std::memcpy(rdram + addr, value.c_str(), value.size() + 1);
}
void writeGuestU32(uint8_t *rdram, uint32_t addr, uint32_t value)
{
std::memcpy(rdram + addr, &value, sizeof(value));
}
int32_t readGuestS32(const uint8_t *rdram, uint32_t addr)
{
int32_t value = 0;
std::memcpy(&value, rdram + addr, sizeof(value));
return value;
}
uint32_t readGuestU32(const uint8_t *rdram, uint32_t addr)
{
uint32_t value = 0;
std::memcpy(&value, rdram + addr, sizeof(value));
return value;
}
void clearContext(R5900Context &ctx)
{
std::memset(&ctx, 0, sizeof(ctx));
}
void writeStackArg(std::vector<uint8_t> &rdram, R5900Context &ctx, uint32_t slotIndex, uint32_t value)
{
const uint32_t sp = ::getRegU32(&ctx, 29);
writeGuestU32(rdram.data(), sp + 16u + slotIndex * sizeof(uint32_t), value);
}
int32_t syncMc(std::vector<uint8_t> &rdram, int32_t *cmdOut = nullptr)
{
R5900Context syncCtx{};
setRegU32(syncCtx, 4, 0u);
setRegU32(syncCtx, 5, GUEST_MC_SYNC_CMD_ADDR);
setRegU32(syncCtx, 6, GUEST_MC_SYNC_RESULT_ADDR);
ps2_stubs::sceMcSync(rdram.data(), &syncCtx, nullptr);
if (cmdOut)
{
*cmdOut = readGuestS32(rdram.data(), GUEST_MC_SYNC_CMD_ADDR);
}
return readGuestS32(rdram.data(), GUEST_MC_SYNC_RESULT_ADDR);
}
struct TempPaths
{
std::filesystem::path base;
@@ -276,6 +350,193 @@ void register_ps2_runtime_io_tests()
"mc0: directory should NOT exist under cdRoot");
});
tc.Run("sceMc open write read and close roundtrip through sync", [](TestCase &t)
{
TestContext test;
const uint32_t dirAddr = GUEST_STRING_AREA_START + 0x400;
const uint32_t fileAddr = GUEST_STRING_AREA_START + 0x500;
const uint32_t writeBufAddr = GUEST_BUFFER_AREA_START + 0x300;
const uint32_t readBufAddr = GUEST_BUFFER_AREA_START + 0x500;
const std::string payload = "libmc roundtrip";
writeGuestString(test.rdram.data(), dirAddr, "/SAVEDATA");
writeGuestString(test.rdram.data(), fileAddr, "/SAVEDATA/test.bin");
std::memcpy(test.rdram.data() + writeBufAddr, payload.data(), payload.size());
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, dirAddr);
ps2_stubs::sceMcMkdir(test.rdram.data(), &test.ctx, nullptr);
t.Equals(getRegS32(&test.ctx, 2), 0, "sceMcMkdir should dispatch successfully");
int32_t cmd = 0;
t.Equals(syncMc(test.rdram, &cmd), 0, "sceMcMkdir should finish successfully");
t.Equals(cmd, 0x0B, "sceMcSync should report MKDIR as the last command");
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, fileAddr);
setRegU32(test.ctx, 7, PS2_FIO_O_RDWR | PS2_FIO_O_CREAT | PS2_FIO_O_TRUNC);
ps2_stubs::sceMcOpen(test.rdram.data(), &test.ctx, nullptr);
t.Equals(getRegS32(&test.ctx, 2), 0, "sceMcOpen should dispatch successfully");
const int32_t fd = syncMc(test.rdram, &cmd);
t.IsTrue(fd > 0, "sceMcOpen should produce a positive descriptor in sceMcSync");
t.Equals(cmd, 0x02, "sceMcSync should report OPEN as the last command");
clearContext(test.ctx);
setRegU32(test.ctx, 4, static_cast<uint32_t>(fd));
setRegU32(test.ctx, 5, writeBufAddr);
setRegU32(test.ctx, 6, static_cast<uint32_t>(payload.size()));
ps2_stubs::sceMcWrite(test.rdram.data(), &test.ctx, nullptr);
t.Equals(syncMc(test.rdram, &cmd), static_cast<int32_t>(payload.size()),
"sceMcWrite should report the full byte count via sceMcSync");
t.Equals(cmd, 0x06, "sceMcSync should report WRITE as the last command");
clearContext(test.ctx);
setRegU32(test.ctx, 4, static_cast<uint32_t>(fd));
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, PS2_FIO_SEEK_SET);
ps2_stubs::sceMcSeek(test.rdram.data(), &test.ctx, nullptr);
t.Equals(syncMc(test.rdram, &cmd), 0, "sceMcSeek should rewind to offset zero");
t.Equals(cmd, 0x04, "sceMcSync should report SEEK as the last command");
std::memset(test.rdram.data() + readBufAddr, 0, payload.size());
clearContext(test.ctx);
setRegU32(test.ctx, 4, static_cast<uint32_t>(fd));
setRegU32(test.ctx, 5, readBufAddr);
setRegU32(test.ctx, 6, static_cast<uint32_t>(payload.size()));
ps2_stubs::sceMcRead(test.rdram.data(), &test.ctx, nullptr);
t.Equals(syncMc(test.rdram, &cmd), static_cast<int32_t>(payload.size()),
"sceMcRead should report the full byte count via sceMcSync");
t.Equals(cmd, 0x05, "sceMcSync should report READ as the last command");
std::string readback(reinterpret_cast<const char *>(test.rdram.data() + readBufAddr), payload.size());
t.Equals(readback, payload, "sceMcRead should fill the guest buffer with the written payload");
clearContext(test.ctx);
setRegU32(test.ctx, 4, static_cast<uint32_t>(fd));
ps2_stubs::sceMcClose(test.rdram.data(), &test.ctx, nullptr);
t.Equals(syncMc(test.rdram, &cmd), 0, "sceMcClose should finish successfully");
t.Equals(cmd, 0x03, "sceMcSync should report CLOSE as the last command");
const std::filesystem::path hostPath = test.paths.mcRoot / "SAVEDATA" / "test.bin";
t.IsTrue(std::filesystem::exists(hostPath), "sceMcOpen/sceMcWrite should create the host file under mcRoot");
});
tc.Run("sceMcGetDir includes dot entries and file metadata", [](TestCase &t)
{
TestContext test;
std::filesystem::create_directories(test.paths.mcRoot / "SAVEDATA");
const std::string hostPayload = "abc123";
{
std::ofstream out(test.paths.mcRoot / "SAVEDATA" / "game.dat", std::ios::binary);
out.write(hostPayload.data(), static_cast<std::streamsize>(hostPayload.size()));
}
const uint32_t patternAddr = GUEST_STRING_AREA_START + 0x700;
writeGuestString(test.rdram.data(), patternAddr, "/SAVEDATA/*");
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, patternAddr);
setRegU32(test.ctx, 7, 0u);
setRegU32(test.ctx, 29, GUEST_STACK_AREA_START);
writeStackArg(test.rdram, test.ctx, 0u, 8u);
writeStackArg(test.rdram, test.ctx, 1u, GUEST_MC_TABLE_ADDR);
ps2_stubs::sceMcGetDir(test.rdram.data(), &test.ctx, nullptr);
int32_t cmd = 0;
const int32_t entryCount = syncMc(test.rdram, &cmd);
t.Equals(cmd, 0x0D, "sceMcSync should report GETDIR as the last command");
t.Equals(entryCount, 3, "sceMcGetDir should return '.', '..', and the matching file");
const auto *entries = reinterpret_cast<const SceMcTblGetDir *>(test.rdram.data() + GUEST_MC_TABLE_ADDR);
t.Equals(std::string(entries[0].entryName), std::string("."), "sceMcGetDir should return '.' first");
t.Equals(std::string(entries[1].entryName), std::string(".."), "sceMcGetDir should return '..' second");
t.Equals(std::string(entries[2].entryName), std::string("game.dat"), "sceMcGetDir should include the matching file entry");
t.Equals(entries[2].fileSizeByte, static_cast<uint32_t>(hostPayload.size()),
"sceMcGetDir should report the host file size");
t.IsTrue((entries[2].attrFile & 0x0080u) != 0u,
"sceMcGetDir file entries should carry the closed-file attribute");
});
tc.Run("sceMcGetInfo reports formatted and unformatted states", [](TestCase &t)
{
TestContext test;
constexpr uint32_t typeAddr = GUEST_BUFFER_AREA_START + 0x900;
constexpr uint32_t freeAddr = GUEST_BUFFER_AREA_START + 0x904;
constexpr uint32_t formatAddr = GUEST_BUFFER_AREA_START + 0x908;
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, typeAddr);
setRegU32(test.ctx, 7, freeAddr);
setRegU32(test.ctx, 29, GUEST_STACK_AREA_START);
writeStackArg(test.rdram, test.ctx, 0u, formatAddr);
ps2_stubs::sceMcGetInfo(test.rdram.data(), &test.ctx, nullptr);
int32_t cmd = 0;
t.Equals(syncMc(test.rdram, &cmd), 0, "formatted cards should report success through sceMcSync");
t.Equals(cmd, 0x01, "sceMcSync should report GETINFO as the last command");
t.Equals(readGuestS32(test.rdram.data(), typeAddr), 2, "sceMcGetInfo should report a PS2 memory card");
t.Equals(readGuestS32(test.rdram.data(), formatAddr), 1, "sceMcGetInfo should report a formatted card");
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
ps2_stubs::sceMcUnformat(test.rdram.data(), &test.ctx, nullptr);
t.Equals(syncMc(test.rdram, &cmd), 0, "sceMcUnformat should complete successfully");
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, typeAddr);
setRegU32(test.ctx, 7, freeAddr);
setRegU32(test.ctx, 29, GUEST_STACK_AREA_START);
writeStackArg(test.rdram, test.ctx, 0u, formatAddr);
ps2_stubs::sceMcGetInfo(test.rdram.data(), &test.ctx, nullptr);
t.Equals(syncMc(test.rdram, &cmd), -2, "unformatted cards should report sceMcResNoFormat through sceMcSync");
t.Equals(readGuestS32(test.rdram.data(), formatAddr), 0, "sceMcGetInfo should report an unformatted card after sceMcUnformat");
});
tc.Run("sceMcEnd resets libmc state so sync reports no active command", [](TestCase &t)
{
TestContext test;
constexpr uint32_t dirAddr = GUEST_STRING_AREA_START + 0xB00;
clearContext(test.ctx);
ps2_stubs::sceMcInit(test.rdram.data(), &test.ctx, nullptr);
t.Equals(getRegS32(&test.ctx, 2), 0, "sceMcInit should succeed");
writeGuestString(test.rdram.data(), dirAddr, "/SAVEDATA");
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 0u);
setRegU32(test.ctx, 6, dirAddr);
ps2_stubs::sceMcMkdir(test.rdram.data(), &test.ctx, nullptr);
int32_t cmd = 0;
t.Equals(syncMc(test.rdram, &cmd), 0, "sceMcMkdir should complete before teardown");
t.Equals(cmd, 0x0B, "sceMcSync should report MKDIR before teardown");
clearContext(test.ctx);
ps2_stubs::sceMcEnd(test.rdram.data(), &test.ctx, nullptr);
t.Equals(getRegS32(&test.ctx, 2), 0, "sceMcEnd should succeed");
t.Equals(syncMc(test.rdram, &cmd), 0, "sceMcSync should report cleared result after sceMcEnd");
t.Equals(cmd, 0, "sceMcEnd should clear the last active libmc command");
});
tc.Run("sceIoctl cmd1 updates wait flag state", [](TestCase &t)
{
TestContext test;
@@ -296,5 +557,68 @@ void register_ps2_runtime_io_tests()
std::memcpy(&state, test.rdram.data() + statusAddr, sizeof(state));
t.Equals(state, 0u, "sceIoctl cmd1 should clear wait state from busy to ready");
});
tc.Run("sceCdSearchFile resolves movie filenames with zero-padded host leaf", [](TestCase &t)
{
TestContext test;
std::filesystem::create_directories(test.paths.cdRoot / "movie");
{
std::ofstream out(test.paths.cdRoot / "movie" / "mv_016.pss", std::ios::binary);
const std::string payload = "pss";
out.write(payload.data(), static_cast<std::streamsize>(payload.size()));
}
constexpr uint32_t fileAddr = GUEST_BUFFER_AREA_START + 0x1A00;
constexpr uint32_t pathAddr = GUEST_STRING_AREA_START + 0xA00;
writeGuestString(test.rdram.data(), pathAddr, "\\MOVIE\\MV_16.PSS;1");
clearContext(test.ctx);
setRegU32(test.ctx, 4, fileAddr);
setRegU32(test.ctx, 5, pathAddr);
ps2_stubs::sceCdSearchFile(test.rdram.data(), &test.ctx, nullptr);
t.Equals(getRegS32(&test.ctx, 2), 1, "sceCdSearchFile should resolve the extracted movie file");
t.Equals(readGuestU32(test.rdram.data(), fileAddr + 4), 3u,
"sceCdSearchFile should report the host file size");
t.IsTrue(readGuestU32(test.rdram.data(), fileAddr + 0) >= 0x00100000u,
"sceCdSearchFile should assign a pseudo LSN for the resolved host file");
});
tc.Run("sceCdRead reads from explicit cdImage path", [](TestCase &t)
{
TestContext test;
constexpr uint32_t kSectorSize = 2048u;
constexpr uint32_t bufAddr = GUEST_BUFFER_AREA_START + 0x1C80;
const std::filesystem::path imagePath = test.paths.base / "disc.iso";
{
std::vector<uint8_t> sector(kSectorSize, 0);
const char payload[] = "cd-image";
std::memcpy(sector.data(), payload, sizeof(payload) - 1);
std::ofstream out(imagePath, std::ios::binary);
out.write(reinterpret_cast<const char *>(sector.data()),
static_cast<std::streamsize>(sector.size()));
}
PS2Runtime::IoPaths ioPaths;
ioPaths.elfDirectory = test.paths.cdRoot;
ioPaths.hostRoot = test.paths.cdRoot;
ioPaths.cdRoot = test.paths.cdRoot;
ioPaths.mcRoot = test.paths.mcRoot;
ioPaths.cdImage = imagePath;
PS2Runtime::setIoPaths(ioPaths);
clearContext(test.ctx);
setRegU32(test.ctx, 4, 0u);
setRegU32(test.ctx, 5, 1u);
setRegU32(test.ctx, 6, bufAddr);
ps2_stubs::sceCdRead(test.rdram.data(), &test.ctx, nullptr);
t.Equals(getRegS32(&test.ctx, 2), 1, "sceCdRead should succeed when cdImage is configured");
t.Equals(std::memcmp(test.rdram.data() + bufAddr, "cd-image", 8), 0,
"sceCdRead should copy sector data from the configured image");
});
});
}