diff --git a/core/libs/prx/libSceAgc/CMakeLists.txt b/core/libs/prx/libSceAgc/CMakeLists.txt index cfcf3e4ac..7a7d7adeb 100644 --- a/core/libs/prx/libSceAgc/CMakeLists.txt +++ b/core/libs/prx/libSceAgc/CMakeLists.txt @@ -125,6 +125,25 @@ if(BUILD_TESTING) endif() endif() +add_test_executable(agc_cb_cond_write_tests EXCLUDE_FROM_ALL + tests/CbCondWrite.cpp + Command/src/Packet.cpp + Command/src/Memory.cpp + Command/src/Control.cpp + Cb/src/Branch.cpp +) +target_include_directories(agc_cb_cond_write_tests PRIVATE ${LIBS_INCLUDE_DIR}) +target_link_libraries(agc_cb_cond_write_tests PRIVATE libc) +configure_windows_unwind(agc_cb_cond_write_tests) +if(BUILD_TESTING) + set_target_properties(agc_cb_cond_write_tests PROPERTIES EXCLUDE_FROM_ALL FALSE) + add_test(NAME agc_cb_cond_write_tests COMMAND agc_cb_cond_write_tests) + if(WIN32) + set_tests_properties(agc_cb_cond_write_tests PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() + add_test_executable(agc_payload_range_tests EXCLUDE_FROM_ALL tests/PayloadRange.cpp) target_include_directories(agc_payload_range_tests PRIVATE ${LIBS_INCLUDE_DIR}) target_link_libraries(agc_payload_range_tests PRIVATE libSceAgc libc) diff --git a/core/libs/prx/libSceAgc/Cb/src/Branch.cpp b/core/libs/prx/libSceAgc/Cb/src/Branch.cpp index 649b810f6..d27f1c28c 100644 --- a/core/libs/prx/libSceAgc/Cb/src/Branch.cpp +++ b/core/libs/prx/libSceAgc/Cb/src/Branch.cpp @@ -28,19 +28,20 @@ std::uint32_t APS5_VABI sceAgcCbBranchGetSize() { return 56; } -uint32_t* APS5_VABI sceAgcCbCondWrite(CommandBuffer* buf, uint32_t cond, uint32_t a, uint32_t b, uint32_t c) { - (void)buf; - (void)cond; - (void)a; - (void)b; - (void)c; - NotImplemented_nid_no_patch(__func__); - return nullptr; +std::uint32_t* APS5_VABI sceAgcCbCondWrite(CommandBuffer* buf, std::uint32_t compareFunction, std::uint32_t writeSpace, const volatile void* writeAddress, std::uint32_t writeData, const volatile void* pollAddress, std::uint32_t reference, std::uint32_t mask) { + Agc::Command::Require(compareFunction <= 6, __func__, "invalid write comparison"); + Agc::Command::Require(writeSpace == 1, __func__, "only memory writes are supported"); + const auto destination = reinterpret_cast(writeAddress); + const auto source = reinterpret_cast(pollAddress); + Agc::Command::CheckAddress(destination, 4, __func__); + Agc::Command::CheckAddress(source, 4, __func__); + Agc::Command::CheckBits(destination, 0xffffffffffffull, __func__); + Agc::Command::CheckBits(source, 0xffffffffffffull, __func__); + return Agc::Command::Emit(buf, 0x45u, {0x10u | compareFunction | (writeSpace << 8u), static_cast(source), static_cast(source >> 32u), reference, mask, static_cast(destination), static_cast(destination >> 32u), writeData}, __func__); } std::uint32_t APS5_VABI sceAgcCbCondWriteGetSize() { - NotImplemented_nid_no_patch(__func__); - return 0; + return 36; } } diff --git a/core/libs/prx/libSceAgc/tests/CbCondWrite.cpp b/core/libs/prx/libSceAgc/tests/CbCondWrite.cpp new file mode 100644 index 000000000..69186e10b --- /dev/null +++ b/core/libs/prx/libSceAgc/tests/CbCondWrite.cpp @@ -0,0 +1,86 @@ +#include "prx/libSceAgc/Command/include/Packet.hpp" + +#include +#include +#include +#include + +extern "C" { +std::uint32_t* APS5_VABI sceAgcCbCondWrite(CommandBuffer*, std::uint32_t, std::uint32_t, const volatile void*, std::uint32_t, const volatile void*, std::uint32_t, std::uint32_t); +std::uint32_t APS5_VABI sceAgcCbCondWriteGetSize(); +} + +namespace { + +constexpr std::uint32_t Sentinel = 0xabcdef01u; + +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 void* at(std::uintptr_t address) { + return reinterpret_cast(address); +} + +struct Storage { + std::array words{}; + CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0}; + + Storage() { words.fill(Sentinel); } +}; + +void testPacket() { + for (std::uint32_t compareFunction = 0; compareFunction <= 6; ++compareFunction) { + Storage storage; + const auto* packet = sceAgcCbCondWrite(&storage.buffer, compareFunction, 1, at(0x0000123456789abcu), 0xdeadbeefu, at(0x0000fedcba987654u), 0x11223344u, 0xff00ff00u); + check(packet == storage.words.data() && packet[0] == 0xc0074500u, "COND_WRITE header mismatch"); + check(packet[1] == (0x110u | compareFunction), "COND_WRITE control mismatch"); + check(packet[2] == 0xba987654u && packet[3] == 0x0000fedcu, "COND_WRITE poll address mismatch"); + check(packet[4] == 0x11223344u && packet[5] == 0xff00ff00u, "COND_WRITE reference or mask mismatch"); + check(packet[6] == 0x56789abcu && packet[7] == 0x00001234u, "COND_WRITE write address mismatch"); + check(packet[8] == 0xdeadbeefu, "COND_WRITE data mismatch"); + check(storage.buffer.cursor_up == storage.words.data() + 9 && storage.words[9] == Sentinel, "COND_WRITE cursor advance"); + } + check(sceAgcCbCondWriteGetSize() == 9 * sizeof(std::uint32_t), "COND_WRITE size mismatch"); +} + +void testInvalid() { + Storage storage; + const auto before = storage.words; + const auto poll = at(0x1000u); + const auto write = at(0x2000u); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 7, 1, write, 0, poll, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 0, write, 0, poll, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 2, write, 0, poll, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 1, nullptr, 0, poll, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 1, at(0x2002u), 0, poll, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 1, write, 0, nullptr, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 1, write, 0, at(0x1001u), 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 1, at(0x0001000000000000u), 0, poll, 0, 0); }); + expectFailure([&] { sceAgcCbCondWrite(&storage.buffer, 3, 1, write, 0, at(0x0001000000000000u), 0, 0); }); + check(storage.words == before && storage.buffer.cursor_up == storage.words.data(), "failed COND_WRITE modified the buffer"); +} + +} + +int main() { + try { + testPacket(); + testInvalid(); + std::puts("AGC COND_WRITE tests passed"); + return 0; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +} diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 5ab645b30..fde54e020 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -65,6 +65,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceAgcSetSubmitMode](../../core/libs/prx/libSceAgc/Misc/src/Suspend.cpp) (libSceAgc) - mode values unknown; only 0 is accepted - [SET_PREDICATION](../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (AGC driver) - boolean predication follows the AMD PM4 encoding [Mesa](https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/amd/common/sid.h) uses: operation 3 reads 64 bits and 4 reads 32 bits, and bit 8 runs predicated packets when the value is non-zero (clear: when it is zero); the bit 12 hint is ignored. The address must have its low 4 bits clear, as `sceAgcDcbSetPredication` writes it. Query predication (Z-pass, primitive count), a predicated COND_EXEC, predicated command buffer chains and flips in predicated command buffers throw - [sceAgcGetGsOversubscription](../../core/libs/prx/libSceAgc/Shader/src/GsOversubscription.cpp) (libSceAgc) - signature and occupancy formula (base and expanded vertex/export capacities, budget shift, `GE_PC_ALLOC` / `SPI_SHADER_PGM_RSRC4_GS` encoding) follow KytyPS5 only, not confirmed on hardware or a title; a negative or NaN factor that gives a negative target, a missing GS context register or a zero `GE_MAX_OUTPUT_PER_SUBGROUP` throws +- [sceAgcCbCondWrite](../../core/libs/prx/libSceAgc/Cb/src/Branch.cpp) (libSceAgc) - argument order and the 9-dword COND_WRITE layout follow the clean-room [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/main/src/game_compat.c) and [sharpemu](https://github.com/sharpemu/sharpemu/blob/main/src/SharpEmu.Libs/Agc/AgcExports.CommandPackets.cs), which agree on them; SharpProspero lists another order. The write space is accepted only as 1 (memory), the one value both encode the same way, and other values throw. - [sceAgcGetIsTrinityMode](../../core/libs/prx/libSceAgc/Misc/src/Platform.cpp) (libSceAgc) - signature from the only PPSA26344 call (a pointer to a one-byte flag, result ignored); a null pointer throws because its error code is unknown - [zARR5aCmkoY](../../core/libs/prx/libSceAgc/DcbFlow/src/Control.cpp) (libSceAgc) - unknown name, signature - [qj7QZpgr9Uw](../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - unknown name