From 6230c60296c37410f6e81d05dbdd37960f6cc350 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 13 Jul 2020 13:31:51 +0200 Subject: [PATCH] UserspaceEmulator: Make SBB actually respect the SoftCPU carry flag We were forgetting to set the host CPU's carry flag before executing the SBB instruction. This made the result a bit unpredictable. :^) --- DevTools/UserspaceEmulator/SoftCPU.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/DevTools/UserspaceEmulator/SoftCPU.cpp b/DevTools/UserspaceEmulator/SoftCPU.cpp index cb967970a6..cdae654a05 100644 --- a/DevTools/UserspaceEmulator/SoftCPU.cpp +++ b/DevTools/UserspaceEmulator/SoftCPU.cpp @@ -343,12 +343,17 @@ static T op_sub(SoftCPU& cpu, const T& dest, const T& src) return result; } -template -static T op_sbb(SoftCPU& cpu, const T& dest, const T& src) +template +static T op_sbb_impl(SoftCPU& cpu, const T& dest, const T& src) { T result = 0; u32 new_flags = 0; + if constexpr (cf) + asm volatile("stc"); + else + asm volatile("clc"); + if constexpr (sizeof(T) == 4) { asm volatile("sbbl %%ecx, %%eax\n" : "=a"(result) @@ -374,6 +379,14 @@ static T op_sbb(SoftCPU& cpu, const T& dest, const T& src) return result; } +template +static T op_sbb(SoftCPU& cpu, T& dest, const T& src) +{ + if (cpu.cf()) + return op_sbb_impl(cpu, dest, src); + return op_sbb_impl(cpu, dest, src); +} + template static T op_add(SoftCPU& cpu, T& dest, const T& src) {