diff --git a/ps2xRecomp/src/lib/special_translator.cpp b/ps2xRecomp/src/lib/special_translator.cpp index b01a937715..5b4dc26534 100644 --- a/ps2xRecomp/src/lib/special_translator.cpp +++ b/ps2xRecomp/src/lib/special_translator.cpp @@ -120,10 +120,11 @@ namespace ps2recomp return fmt::format("SET_GPR_U64(ctx, {}, ((int64_t)GPR_S64(ctx, {}) < (int64_t)GPR_S64(ctx, {})) ? 1 : 0);", inst.rd, inst.rs, inst.rt); case SPECIAL_SLTU: return fmt::format("SET_GPR_U64(ctx, {}, ((uint64_t)GPR_U64(ctx, {}) < (uint64_t)GPR_U64(ctx, {})) ? 1 : 0);", inst.rd, inst.rs, inst.rt); + // MOVZ/MOVN move the low 64 bits of rs; the upper 64 bits of rd are left unchanged. case SPECIAL_MOVZ: - return fmt::format("if (GPR_U64(ctx, {}) == 0) SET_GPR_VEC(ctx, {}, GPR_VEC(ctx, {}));", inst.rt, inst.rd, inst.rs); + return fmt::format("if (GPR_U64(ctx, {}) == 0) SET_GPR_U64(ctx, {}, GPR_U64(ctx, {}));", inst.rt, inst.rd, inst.rs); case SPECIAL_MOVN: - return fmt::format("if (GPR_U64(ctx, {}) != 0) SET_GPR_VEC(ctx, {}, GPR_VEC(ctx, {}));", inst.rt, inst.rd, inst.rs); + return fmt::format("if (GPR_U64(ctx, {}) != 0) SET_GPR_U64(ctx, {}, GPR_U64(ctx, {}));", inst.rt, inst.rd, inst.rs); case SPECIAL_MFSA: return fmt::format("SET_GPR_U32(ctx, {}, ctx->sa);", inst.rd); case SPECIAL_MTSA: diff --git a/ps2xTest/src/code_generator_tests.cpp b/ps2xTest/src/code_generator_tests.cpp index b2daaee325..86eaa3b220 100644 --- a/ps2xTest/src/code_generator_tests.cpp +++ b/ps2xTest/src/code_generator_tests.cpp @@ -291,6 +291,33 @@ void register_code_generator_tests() "MULT should not write rd when rd is zero"); }); + tc.Run("R5900 MOVZ/MOVN move the low 64 bits and keep rd's upper half", [](TestCase &t) { + CodeGenerator gen({}, {}); + + Instruction movz{}; + movz.opcode = OPCODE_SPECIAL; + movz.function = SPECIAL_MOVZ; + movz.rs = 4; + movz.rt = 5; + movz.rd = 3; + + std::string generated = gen.translateInstruction(movz); + printGeneratedCode("R5900 MOVZ", generated); + t.IsTrue(generated.find("if (GPR_U64(ctx, 5) == 0) SET_GPR_U64(ctx, 3, GPR_U64(ctx, 4));") != std::string::npos, + "MOVZ should move the low 64 bits of rs into rd when rt is zero"); + t.IsTrue(generated.find("SET_GPR_VEC") == std::string::npos, + "MOVZ must not copy all 128 bits"); + + Instruction movn = movz; + movn.function = SPECIAL_MOVN; + generated = gen.translateInstruction(movn); + printGeneratedCode("R5900 MOVN", generated); + t.IsTrue(generated.find("if (GPR_U64(ctx, 5) != 0) SET_GPR_U64(ctx, 3, GPR_U64(ctx, 4));") != std::string::npos, + "MOVN should move the low 64 bits of rs into rd when rt is non-zero"); + t.IsTrue(generated.find("SET_GPR_VEC") == std::string::npos, + "MOVN must not copy all 128 bits"); + }); + tc.Run("R5900 MMI MULT1 writes rd when rd is non-zero", [](TestCase &t) { CodeGenerator gen({}, {}); diff --git a/ps2xTest/src/ps2_runtime_expansion_tests.cpp b/ps2xTest/src/ps2_runtime_expansion_tests.cpp index 2175c842b5..e0e7b85083 100644 --- a/ps2xTest/src/ps2_runtime_expansion_tests.cpp +++ b/ps2xTest/src/ps2_runtime_expansion_tests.cpp @@ -345,6 +345,29 @@ void register_ps2_runtime_expansion_tests() } }); + tc.Run("MOVZ/MOVN code keeps the destination's upper 64 bits", [](TestCase &t) + { + // The statements the code generator emits for MOVZ/MOVN rd=3, rs=4, rt=5. + R5900Context context; + R5900Context *ctx = &context; + const auto reset = [&](uint64_t rtValue) { + ctx->r[3] = _mm_set_epi64x(0x3333333333333333LL, 0x1111111111111111LL); + ctx->r[4] = _mm_set_epi64x(0x4444444444444444LL, 0x2222222222222222LL); + ctx->r[5] = _mm_set_epi64x(0, static_cast(rtValue)); + }; + const auto high = [&](int reg) { alignas(16) uint64_t v[2]; _mm_store_si128(reinterpret_cast<__m128i *>(v), ctx->r[reg]); return v[1]; }; + + reset(0); + if (GPR_U64(ctx, 5) == 0) SET_GPR_U64(ctx, 3, GPR_U64(ctx, 4)); + t.Equals(GPR_U64(ctx, 3), 0x2222222222222222ULL, "MOVZ should move rs's low 64 bits"); + t.Equals(high(3), 0x3333333333333333ULL, "MOVZ should keep rd's upper 64 bits"); + + reset(1); + if (GPR_U64(ctx, 5) != 0) SET_GPR_U64(ctx, 3, GPR_U64(ctx, 4)); + t.Equals(GPR_U64(ctx, 3), 0x2222222222222222ULL, "MOVN should move rs's low 64 bits"); + t.Equals(high(3), 0x3333333333333333ULL, "MOVN should keep rd's upper 64 bits"); + }); + tc.Run("lookupFunction rejects internal resume PCs without exact registration", [](TestCase &t) { PS2Runtime runtime;