Skip to content

Commit

Permalink
Further generalise.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHarte committed Oct 11, 2023
1 parent a768b10 commit e75ef70
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
32 changes: 12 additions & 20 deletions InstructionSets/x86/Implementation/PerformImplementation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ template <
&immediate);
};

// Performs a displacement jump only if @c condition is true.
const auto jcc = [&](bool condition) {
Primitive::jump(
condition,
Expand All @@ -855,12 +856,15 @@ template <
flow_controller);
};

const auto muldiv_high = [&]() -> IntT& {
// Some instructions use a pair of registers as an extended accumulator — DX:AX or EDX:EAX.
// The two following return the high and low parts of that pair; they also work in Byte mode to return AH:AL,
// i.e. AX split into high and low parts.
const auto pair_high = [&]() -> IntT& {
if constexpr (data_size == DataSize::Byte) return registers.ah();
else if constexpr (data_size == DataSize::Word) return registers.dx();
else if constexpr (data_size == DataSize::DWord) return registers.edx();
};
const auto muldiv_low = [&]() -> IntT& {
const auto pair_low = [&]() -> IntT& {
if constexpr (data_size == DataSize::Byte) return registers.al();
else if constexpr (data_size == DataSize::Word) return registers.ax();
else if constexpr (data_size == DataSize::DWord) return registers.eax();
Expand All @@ -882,20 +886,8 @@ template <
case Operation::DAA: Primitive::daa(registers.al(), status); return;
case Operation::DAS: Primitive::das(registers.al(), status); return;

case Operation::CBW:
if constexpr (data_size == DataSize::Word) {
Primitive::cbw(registers.ax());
} else if constexpr (is_32bit(model) && data_size == DataSize::DWord) {
Primitive::cbw(registers.eax());
}
return;
case Operation::CWD:
if constexpr (data_size == DataSize::Word) {
Primitive::cwd(registers.dx(), registers.ax());
} else if constexpr (data_size == DataSize::DWord) {
Primitive::cwd(registers.edx(), registers.eax());
}
return;
case Operation::CBW: Primitive::cbw(pair_low()); return;
case Operation::CWD: Primitive::cwd(pair_high(), pair_low()); return;

case Operation::ESC:
case Operation::NOP: return;
Expand All @@ -910,10 +902,10 @@ template <
case Operation::CMP: Primitive::sub<false, false>(destination(), source(), status); break;
case Operation::TEST: Primitive::test(destination(), source(), status); break;

case Operation::MUL: Primitive::mul(muldiv_high(), muldiv_low(), source(), status); return;
case Operation::IMUL_1: Primitive::imul(muldiv_high(), muldiv_low(), source(), status); return;
case Operation::DIV: Primitive::div(muldiv_high(), muldiv_low(), source(), flow_controller); return;
case Operation::IDIV: Primitive::idiv(muldiv_high(), muldiv_low(), source(), flow_controller); return;
case Operation::MUL: Primitive::mul(pair_high(), pair_low(), source(), status); return;
case Operation::IMUL_1: Primitive::imul(pair_high(), pair_low(), source(), status); return;
case Operation::DIV: Primitive::div(pair_high(), pair_low(), source(), flow_controller); return;
case Operation::IDIV: Primitive::idiv(pair_high(), pair_low(), source(), flow_controller); return;

case Operation::INC: Primitive::inc(destination(), status); break;
case Operation::DEC: Primitive::dec(destination(), status); break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Release"
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
Expand Down

0 comments on commit e75ef70

Please sign in to comment.