diff --git a/InstructionSets/x86/Implementation/BCD.hpp b/InstructionSets/x86/Implementation/BCD.hpp index f8de9e6fd..26fd87056 100644 --- a/InstructionSets/x86/Implementation/BCD.hpp +++ b/InstructionSets/x86/Implementation/BCD.hpp @@ -15,34 +15,21 @@ namespace InstructionSet::x86::Primitive { -template -void aaa( +/// If @c add is @c true, performs an AAA; otherwise perfoms an AAS. +template +void aaas( CPU::RegisterPair16 &ax, ContextT &context -) { // P. 313 - /* - IF ((AL AND 0FH) > 9) OR (AF = 1) - THEN - AL ← (AL + 6); - AH ← AH + 1; - AF ← 1; - CF ← 1; - ELSE - AF ← 0; - CF ← 0; - FI; - AL ← AL AND 0FH; - */ - /* - The AF and CF flags are set to 1 if the adjustment results in a decimal carry; - otherwise they are cleared to 0. The OF, SF, ZF, and PF flags are undefined. - */ +) { if((ax.halves.low & 0x0f) > 9 || context.flags.template flag()) { - ax.halves.low += 6; - ++ax.halves.high; + if constexpr (add) { + ax.halves.low += 6; + ++ax.halves.high; + } else { + ax.halves.low -= 6; + --ax.halves.high; + } context.flags.template set_from(1); - } else { - context.flags.template set_from(0); } ax.halves.low &= 0x0f; } @@ -96,143 +83,27 @@ void aam( context.flags.template set_from(ax.halves.low); } -template -void aas( - CPU::RegisterPair16 &ax, - ContextT &context -) { - /* - IF ((AL AND 0FH) > 9) OR (AF = 1) - THEN - AL ← AL – 6; - AH ← AH – 1; - AF ← 1; - CF ← 1; - ELSE - CF ← 0; - AF ← 0; - FI; - AL ← AL AND 0FH; - */ - /* - The AF and CF flags are set to 1 if there is a decimal borrow; - otherwise, they are cleared to 0. The OF, SF, ZF, and PF flags are undefined. - */ - if((ax.halves.low & 0x0f) > 9 || context.flags.template flag()) { - ax.halves.low -= 6; - --ax.halves.high; - context.flags.template set_from(1); - } else { - context.flags.template set_from(0); - } - ax.halves.low &= 0x0f; -} - -template -void daa( +/// If @c add is @c true, performs a DAA; otherwise perfoms a DAS. +template +void daas( uint8_t &al, ContextT &context ) { - /* - (as modified by https://www.felixcloutier.com/x86/daa ...) - - old_AL ← AL; - old_CF ← CF; - CF ← 0; - - IF (((AL AND 0FH) > 9) or AF = 1) - THEN - AL ← AL + 6; - CF ← old_CF OR CarryFromLastAddition; (* CF OR carry from AL ← AL + 6 *) - AF ← 1; - ELSE - AF ← 0; - FI; - IF ((old_AL > 99H) or old_CF = 1) - THEN - AL ← AL + 60H; - CF ← 1; - ELSE - CF ← 0; - FI; - */ - /* - The CF and AF flags are set if the adjustment of the value results in a - decimal carry in either digit of the result (see the “Operation” section above). - The SF, ZF, and PF flags are set according to the result. The OF flag is undefined. - */ - const uint8_t old_al = al; - const auto old_carry = context.flags.template flag(); - context.flags.template set_from(0); + bool top_exceeded_threshold; + if constexpr (ContextT::model == Model::i8086) { + top_exceeded_threshold = al > (context.flags.template flag() ? 0x9f : 0x99); + } else { + top_exceeded_threshold = al > 0x99; + } if((al & 0x0f) > 0x09 || context.flags.template flag()) { - context.flags.template set_from(old_carry | (al > 0xf9)); - al += 0x06; + if constexpr (add) al += 0x06; else al -= 0x06; context.flags.template set_from(1); - } else { - context.flags.template set_from(0); } - if(old_al > 0x99 || old_carry) { - al += 0x60; + if(top_exceeded_threshold || context.flags.template flag()) { + if constexpr (add) al += 0x60; else al -= 0x60; context.flags.template set_from(1); - } else { - context.flags.template set_from(0); - } - - context.flags.template set_from(al); -} - -template -void das( - uint8_t &al, - ContextT &context -) { - /* - (as modified by https://www.felixcloutier.com/x86/daa ...) - - old_AL ← AL; - old_CF ← CF; - CF ← 0; - - IF (((AL AND 0FH) > 9) or AF = 1) - THEN - AL ← AL - 6; - CF ← old_CF OR CarryFromLastAddition; (* CF OR borrow from AL ← AL - 6 *) - AF ← 1; - ELSE - AF ← 0; - FI; - IF ((old_AL > 99H) or old_CF = 1) - THEN - AL ← AL - 60H; - CF ← 1; - ELSE - CF ← 0; - FI; - */ - /* - The CF and AF flags are set if the adjustment of the value results in a - decimal carry in either digit of the result (see the “Operation” section above). - The SF, ZF, and PF flags are set according to the result. The OF flag is undefined. - */ - const uint8_t old_al = al; - const auto old_carry = context.flags.template flag(); - context.flags.template set_from(0); - - if((al & 0x0f) > 0x09 || context.flags.template flag()) { - context.flags.template set_from(old_carry | (al < 0x06)); - al -= 0x06; - context.flags.template set_from(1); - } else { - context.flags.template set_from(0); - } - - if(old_al > 0x99 || old_carry) { - al -= 0x60; - context.flags.template set_from(1); - } else { - context.flags.template set_from(0); } context.flags.template set_from(al); diff --git a/InstructionSets/x86/Implementation/PerformImplementation.hpp b/InstructionSets/x86/Implementation/PerformImplementation.hpp index 1090d4a17..a28073a1b 100644 --- a/InstructionSets/x86/Implementation/PerformImplementation.hpp +++ b/InstructionSets/x86/Implementation/PerformImplementation.hpp @@ -175,12 +175,12 @@ template < case Operation::ESC: case Operation::NOP: return; - case Operation::AAA: Primitive::aaa(context.registers.axp(), context); return; - case Operation::AAD: Primitive::aad(context.registers.axp(), instruction.operand(), context); return; case Operation::AAM: Primitive::aam(context.registers.axp(), instruction.operand(), context); return; - case Operation::AAS: Primitive::aas(context.registers.axp(), context); return; - case Operation::DAA: Primitive::daa(context.registers.al(), context); return; - case Operation::DAS: Primitive::das(context.registers.al(), context); return; + case Operation::AAD: Primitive::aad(context.registers.axp(), instruction.operand(), context); return; + case Operation::AAA: Primitive::aaas(context.registers.axp(), context); return; + case Operation::AAS: Primitive::aaas(context.registers.axp(), context); return; + case Operation::DAA: Primitive::daas(context.registers.al(), context); return; + case Operation::DAS: Primitive::daas(context.registers.al(), context); return; case Operation::CBW: Primitive::cbw(pair_low()); return; case Operation::CWD: Primitive::cwd(pair_high(), pair_low()); return; diff --git a/OSBindings/Mac/Clock SignalTests/8088Tests.mm b/OSBindings/Mac/Clock SignalTests/8088Tests.mm index 2c96678b7..0cf44421f 100644 --- a/OSBindings/Mac/Clock SignalTests/8088Tests.mm +++ b/OSBindings/Mac/Clock SignalTests/8088Tests.mm @@ -394,8 +394,6 @@ struct FailedExecution { NSString *path = [NSString stringWithUTF8String:TestSuiteHome]; NSSet *allowList = [NSSet setWithArray:@[ // Current execution failures: -// @"27.json.gz", // DAA -// @"2F.json.gz", // DAS // @"D4.json.gz", // AAM // @"F6.7.json.gz", // IDIV // @"F7.7.json.gz", // IDIV @@ -459,8 +457,6 @@ struct FailedExecution { return hexInstruction; }; - EACCES; - const auto decoded = decoder.decode(data.data(), data.size()); const bool sizeMatched = decoded.first == data.size(); if(assert) { @@ -748,10 +744,9 @@ struct FailedExecution { } // Lock in current failure rate. - XCTAssertLessThanOrEqual(execution_failures.size(), 4138); + XCTAssertLessThanOrEqual(execution_failures.size(), 4009); // Current accepted failures: - // * 65 instances of DAA with invalid BCD input, and 64 of DAS; // * 2484 instances of LEA from a register, which officially has undefined results; // * 42 instances of AAM 00h for which I can't figure out what to do with flags; and // * 1486 instances of IDIV, most either with a rep or repne that on the 8086 specifically negatives the result,