From 46b5d5d8b01a7788190164f34bbff5bd1a3d239b Mon Sep 17 00:00:00 2001 From: DotDebian Date: Sun, 4 Oct 2026 17:48:12 +0200 Subject: [PATCH] feat(agc): implement the sceAgcBranchPatch setters sceAgcBranchPatchSetCompareAddress, sceAgcBranchPatchSetThenTarget and sceAgcBranchPatchSetElseTarget rewrite the compare address and the two buffer targets of the packet sceAgcCbBranch emits. The then target is the first buffer and the else target the second, as in AMD's COND_INDIRECT_BUFFER packet. The cache policy bits are kept. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libSceAgc/CMakeLists.txt | 18 +++ core/libs/prx/libSceAgc/Patch/src/Branch.cpp | 32 +++-- core/libs/prx/libSceAgc/tests/BranchPatch.cpp | 122 ++++++++++++++++++ 3 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 core/libs/prx/libSceAgc/tests/BranchPatch.cpp diff --git a/core/libs/prx/libSceAgc/CMakeLists.txt b/core/libs/prx/libSceAgc/CMakeLists.txt index ed4ed41a3..2431196d3 100644 --- a/core/libs/prx/libSceAgc/CMakeLists.txt +++ b/core/libs/prx/libSceAgc/CMakeLists.txt @@ -87,6 +87,24 @@ if(BUILD_TESTING) endif() endif() +add_test_executable(agc_branch_patch_tests EXCLUDE_FROM_ALL + tests/BranchPatch.cpp + Command/src/Packet.cpp + Cb/src/Branch.cpp + Patch/src/Branch.cpp +) +target_include_directories(agc_branch_patch_tests PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(agc_branch_patch_tests PRIVATE libc) +configure_windows_unwind(agc_branch_patch_tests) +if(BUILD_TESTING) + set_target_properties(agc_branch_patch_tests PROPERTIES EXCLUDE_FROM_ALL FALSE) + add_test(NAME agc_branch_patch_tests COMMAND agc_branch_patch_tests) + if(WIN32) + set_tests_properties(agc_branch_patch_tests PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() + add_test_executable(agc_shader_fusion_tests EXCLUDE_FROM_ALL tests/ShaderFusion.cpp) target_include_directories(agc_shader_fusion_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(agc_shader_fusion_tests PRIVATE libSceAgc libc) diff --git a/core/libs/prx/libSceAgc/Patch/src/Branch.cpp b/core/libs/prx/libSceAgc/Patch/src/Branch.cpp index 03c3993bc..b2f791615 100644 --- a/core/libs/prx/libSceAgc/Patch/src/Branch.cpp +++ b/core/libs/prx/libSceAgc/Patch/src/Branch.cpp @@ -7,28 +7,38 @@ #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" +namespace { + +void PatchBranchTarget(std::uint32_t* cmd, std::uint32_t field, const volatile std::uint32_t* target, std::uint32_t sizeInDwords, const char* function) { + Agc::Command::ValidatePacket(cmd, 0x3fu, 14, function); + const auto address = reinterpret_cast(target); + Agc::Command::CheckAddress(address, 4, function); + Agc::Command::CheckBits(sizeInDwords, 0xfffffu, function); + cmd[field] = static_cast(address); + cmd[field + 1u] = static_cast(address >> 32u); + cmd[field + 2u] = (cmd[field + 2u] & ~0xfffffu) | sizeInDwords; +} + +} + extern "C" { int APS5_VABI sceAgcBranchPatchSetCompareAddress(std::uint32_t* cmd, const volatile std::uint64_t* compareAddress) { - (void)cmd; - (void)compareAddress; - NotImplemented_nid_no_patch(__func__); + Agc::Command::ValidatePacket(cmd, 0x3fu, 14, __func__); + const auto address = reinterpret_cast(compareAddress); + Agc::Command::CheckAddress(address, 8, __func__); + cmd[2] = static_cast(address); + cmd[3] = static_cast(address >> 32u); return 0; } int APS5_VABI sceAgcBranchPatchSetThenTarget(std::uint32_t* cmd, const volatile std::uint32_t* target, std::uint32_t sizeInDwords) { - (void)cmd; - (void)target; - (void)sizeInDwords; - NotImplemented_nid_no_patch(__func__); + PatchBranchTarget(cmd, 8, target, sizeInDwords, __func__); return 0; } int APS5_VABI sceAgcBranchPatchSetElseTarget(std::uint32_t* cmd, const volatile std::uint32_t* target, std::uint32_t sizeInDwords) { - (void)cmd; - (void)target; - (void)sizeInDwords; - NotImplemented_nid_no_patch(__func__); + PatchBranchTarget(cmd, 11, target, sizeInDwords, __func__); return 0; } diff --git a/core/libs/prx/libSceAgc/tests/BranchPatch.cpp b/core/libs/prx/libSceAgc/tests/BranchPatch.cpp new file mode 100644 index 000000000..0062068af --- /dev/null +++ b/core/libs/prx/libSceAgc/tests/BranchPatch.cpp @@ -0,0 +1,122 @@ +#include "prx/libSceAgc/Command/include/Packet.hpp" + +#include +#include +#include +#include +#include +#include + +extern "C" { +std::uint32_t* APS5_VABI sceAgcCbBranch(CommandBuffer*, std::uint8_t, std::uint8_t, const volatile std::uint64_t*, std::uint64_t, std::uint64_t, std::uint8_t, const volatile std::uint32_t*, std::uint32_t, std::uint8_t, const volatile std::uint32_t*, std::uint32_t); +std::uint32_t APS5_VABI sceAgcCbBranchGetSize(); +int APS5_VABI sceAgcBranchPatchSetCompareAddress(std::uint32_t*, const volatile std::uint64_t*); +int APS5_VABI sceAgcBranchPatchSetThenTarget(std::uint32_t*, const volatile std::uint32_t*, std::uint32_t); +int APS5_VABI sceAgcBranchPatchSetElseTarget(std::uint32_t*, const volatile std::uint32_t*, std::uint32_t); +} + +namespace { + +void check(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +template +void expectFailure(TAction action) { + try { + action(); + } catch (const std::runtime_error&) { + return; + } + throw std::runtime_error("expected invalid input to fail"); +} + +const volatile std::uint64_t* compareAt(std::uint64_t address) { + return reinterpret_cast(static_cast(address)); +} + +const volatile std::uint32_t* targetAt(std::uint64_t address) { + return reinterpret_cast(static_cast(address)); +} + +struct Storage { + std::array words{}; + CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0}; + std::uint32_t* packet = nullptr; + + Storage() { + words.fill(0xabcdef01u); + packet = sceAgcCbBranch(&buffer, 2, 3, compareAt(0x0000123456789ab8ull), 0x1122334455667788ull, 0x99aabbccddeeff00ull, 1, targetAt(0x0000002233445564ull), 0x123u, 2, targetAt(0x0000006677889900ull), 0x456u); + } +}; + +void testWriter() { + Storage storage; + const std::array expected{0xc00c3f00u, 0x302u, 0x56789ab8u, 0x1234u, 0x55667788u, 0x11223344u, 0xddeeff00u, 0x99aabbccu, 0x33445564u, 0x22u, 0x10000123u, 0x77889900u, 0x66u, 0x20000456u}; + check(storage.packet == storage.words.data() && std::equal(expected.begin(), expected.end(), storage.packet), "incorrect branch packet"); + check(storage.buffer.cursor_up == storage.packet + expected.size() && sceAgcCbBranchGetSize() == expected.size() * sizeof(std::uint32_t), "branch size/cursor mismatch"); + check(storage.packet[expected.size()] == 0xabcdef01u, "branch command overwrote following word"); +} + +void testCompareAddress() { + Storage storage; + auto expected = storage.words; + expected[2] = 0xfedcba98u; + expected[3] = 0x7654u; + check(sceAgcBranchPatchSetCompareAddress(storage.packet, compareAt(0x00007654fedcba98ull)) == 0, "compare address setter failed"); + check(storage.words == expected, "compare address setter wrote the wrong words"); + expectFailure([&] { sceAgcBranchPatchSetCompareAddress(storage.packet, nullptr); }); + expectFailure([&] { sceAgcBranchPatchSetCompareAddress(storage.packet, compareAt(0x00007654fedcba9cull)); }); + expectFailure([&] { sceAgcBranchPatchSetCompareAddress(nullptr, compareAt(0x1000u)); }); + check(storage.words == expected, "invalid compare address modified the packet"); +} + +template +void testTarget(TSetter setter, std::size_t field, std::uint32_t cachePolicyBits, const char* name) { + Storage storage; + auto expected = storage.words; + expected[field] = 0x89abcdecu; + expected[field + 1] = 0x4567u; + expected[field + 2] = cachePolicyBits | 0xfffffu; + check(setter(storage.packet, targetAt(0x0000456789abcdecull), 0xfffffu) == 0, name); + check(storage.words == expected, name); + expected[field] = 0x1000u; + expected[field + 1] = 0; + expected[field + 2] = cachePolicyBits; + check(setter(storage.packet, targetAt(0x1000u), 0) == 0, name); + check(storage.words == expected, name); + expectFailure([&] { setter(storage.packet, nullptr, 1); }); + expectFailure([&] { setter(storage.packet, targetAt(0x1002u), 1); }); + expectFailure([&] { setter(storage.packet, targetAt(0x2000u), 0x100000u); }); + expectFailure([&] { setter(nullptr, targetAt(0x2000u), 1); }); + check(storage.words == expected, name); +} + +void testMalformedPackets() { + for (const auto header : {0xc0023f00u, 0xc00b3f00u, 0xc00c3f01u, 0xc00c2200u}) { + Storage storage; + storage.packet[0] = header; + const auto malformed = storage.words; + expectFailure([&] { sceAgcBranchPatchSetCompareAddress(storage.packet, compareAt(0x1000u)); }); + expectFailure([&] { sceAgcBranchPatchSetThenTarget(storage.packet, targetAt(0x2000u), 1); }); + expectFailure([&] { sceAgcBranchPatchSetElseTarget(storage.packet, targetAt(0x2000u), 1); }); + check(storage.words == malformed, "setter modified a packet that is not a branch"); + } +} + +} + +int main() { + try { + testWriter(); + testCompareAddress(); + testTarget(sceAgcBranchPatchSetThenTarget, 8, 0x10000000u, "then target setter wrote the wrong words"); + testTarget(sceAgcBranchPatchSetElseTarget, 11, 0x20000000u, "else target setter wrote the wrong words"); + testMalformedPackets(); + std::puts("AGC branch patch tests passed"); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +}