mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2025-01-16 03:30:23 +00:00
Sync C++ Z80 implementation with C#
Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
parent
a69770addd
commit
5e347b9414
@ -80,7 +80,7 @@ namespace EightBit {
|
||||
}
|
||||
|
||||
void exxAF() {
|
||||
m_accumulatorFlagsSet = !m_accumulatorFlagsSet;
|
||||
m_accumulatorFlagsSet ^= 1;
|
||||
}
|
||||
|
||||
DECLARE_PIN_INPUT(NMI)
|
||||
@ -282,26 +282,26 @@ namespace EightBit {
|
||||
return setBit(f, HC, calculateHalfCarryAdd(before, value, calculation));
|
||||
}
|
||||
|
||||
static void adjustHalfCarrySub(uint8_t& f, const uint8_t before, const uint8_t value, const int calculation) {
|
||||
f = setBit(f, HC, calculateHalfCarrySub(before, value, calculation));
|
||||
[[nodiscard]] static uint8_t adjustHalfCarrySub(uint8_t f, const uint8_t before, const uint8_t value, const int calculation) {
|
||||
return setBit(f, HC, calculateHalfCarrySub(before, value, calculation));
|
||||
}
|
||||
|
||||
static void adjustOverflowAdd(uint8_t& f, const uint8_t before, const uint8_t value, const uint8_t calculation) {
|
||||
adjustOverflowAdd(f, before & SF, value & SF, calculation & SF);
|
||||
[[nodiscard]] static uint8_t adjustOverflowAdd(uint8_t f, const uint8_t before, const uint8_t value, const uint8_t calculation) {
|
||||
return adjustOverflowAdd(f, before & SF, value & SF, calculation & SF);
|
||||
}
|
||||
|
||||
static void adjustOverflowAdd(uint8_t& f, const int beforeNegative, const int valueNegative, const int afterNegative) {
|
||||
[[nodiscard]] static uint8_t adjustOverflowAdd(uint8_t f, const int beforeNegative, const int valueNegative, const int afterNegative) {
|
||||
const auto overflow = (beforeNegative == valueNegative) && (beforeNegative != afterNegative);
|
||||
f = setBit(f, VF, overflow);
|
||||
return setBit(f, VF, overflow);
|
||||
}
|
||||
|
||||
static void adjustOverflowSub(uint8_t& f, const uint8_t before, const uint8_t value, const uint8_t calculation) {
|
||||
adjustOverflowSub(f, before & SF, value & SF, calculation & SF);
|
||||
[[nodiscard]] static uint8_t adjustOverflowSub(uint8_t f, const uint8_t before, const uint8_t value, const uint8_t calculation) {
|
||||
return adjustOverflowSub(f, before & SF, value & SF, calculation & SF);
|
||||
}
|
||||
|
||||
static void adjustOverflowSub(uint8_t& f, const int beforeNegative, const int valueNegative, const int afterNegative) {
|
||||
[[nodiscard]] static uint8_t adjustOverflowSub(uint8_t f, const int beforeNegative, const int valueNegative, const int afterNegative) {
|
||||
const auto overflow = (beforeNegative != valueNegative) && (beforeNegative != afterNegative);
|
||||
f = setBit(f, VF, overflow);
|
||||
return setBit(f, VF, overflow);
|
||||
}
|
||||
|
||||
static uint8_t subtract(uint8_t& f, uint8_t operand, uint8_t value, int carry = 0);
|
||||
|
@ -253,13 +253,12 @@ bool EightBit::Z80::callConditionalFlag(const uint8_t f, const int flag) {
|
||||
EightBit::register16_t EightBit::Z80::sbc(uint8_t& f, const register16_t operand, const register16_t value) {
|
||||
|
||||
const auto subtraction = operand.word - value.word - (f & CF);
|
||||
|
||||
const register16_t result = subtraction;
|
||||
|
||||
f = setBit(f, NF);
|
||||
f = clearBit(f, ZF, result.word);
|
||||
f = setBit(f, CF, subtraction & Bit16);
|
||||
adjustHalfCarrySub(f, operand.high, value.high, result.high);
|
||||
f = adjustHalfCarrySub(f, operand.high, value.high, result.high);
|
||||
f = adjustXY<Z80>(f, result.high);
|
||||
|
||||
const auto beforeNegative = operand.high & SF;
|
||||
@ -267,7 +266,7 @@ EightBit::register16_t EightBit::Z80::sbc(uint8_t& f, const register16_t operand
|
||||
const auto afterNegative = result.high & SF;
|
||||
|
||||
f = setBit(f, SF, afterNegative);
|
||||
adjustOverflowSub(f, beforeNegative, valueNegative, afterNegative);
|
||||
f = adjustOverflowSub(f, beforeNegative, valueNegative, afterNegative);
|
||||
|
||||
MEMPTR() = operand + 1;
|
||||
|
||||
@ -284,7 +283,7 @@ EightBit::register16_t EightBit::Z80::adc(uint8_t& f, const register16_t operand
|
||||
const auto afterNegative = result.high & SF;
|
||||
|
||||
f = setBit(f, SF, afterNegative);
|
||||
adjustOverflowAdd(f, beforeNegative, valueNegative, afterNegative);
|
||||
f = adjustOverflowAdd(f, beforeNegative, valueNegative, afterNegative);
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -310,7 +309,7 @@ uint8_t EightBit::Z80::add(uint8_t& f, const uint8_t operand, const uint8_t valu
|
||||
const auto result = addition.low;
|
||||
|
||||
f = adjustHalfCarryAdd(f, operand, value, result);
|
||||
adjustOverflowAdd(f, operand, value, result);
|
||||
f = adjustOverflowAdd(f, operand, value, result);
|
||||
|
||||
f = clearBit(f, NF);
|
||||
f = setBit(f, CF, addition.high & CF);
|
||||
@ -328,8 +327,8 @@ uint8_t EightBit::Z80::subtract(uint8_t& f, const uint8_t operand, const uint8_t
|
||||
const register16_t subtraction = operand - value - carry;
|
||||
const auto result = subtraction.low;
|
||||
|
||||
adjustHalfCarrySub(f, operand, value, result);
|
||||
adjustOverflowSub(f, operand, value, result);
|
||||
f = adjustHalfCarrySub(f, operand, value, result);
|
||||
f = adjustOverflowSub(f, operand, value, result);
|
||||
|
||||
f = setBit(f, NF);
|
||||
f = setBit(f, CF, subtraction.high & CF);
|
||||
@ -459,13 +458,13 @@ void EightBit::Z80::bit(uint8_t& f, const int n, const uint8_t operand) {
|
||||
uint8_t EightBit::Z80::res(const int n, const uint8_t operand) {
|
||||
ASSUME(n >= 0);
|
||||
ASSUME(n <= 7);
|
||||
return operand & ~Chip::bit(n);
|
||||
return clearBit(operand, Chip::bit(n));
|
||||
}
|
||||
|
||||
uint8_t EightBit::Z80::set(const int n, const uint8_t operand) {
|
||||
ASSUME(n >= 0);
|
||||
ASSUME(n <= 7);
|
||||
return operand | Chip::bit(n);
|
||||
return setBit(operand, Chip::bit(n));
|
||||
}
|
||||
|
||||
uint8_t EightBit::Z80::neg(uint8_t& f, uint8_t operand) {
|
||||
@ -476,8 +475,8 @@ uint8_t EightBit::Z80::neg(uint8_t& f, uint8_t operand) {
|
||||
|
||||
const uint8_t result = (~operand + 1); // two's complement
|
||||
|
||||
adjustHalfCarrySub(f, 0U, operand, result);
|
||||
adjustOverflowSub(f, 0U, operand, result);
|
||||
f = adjustHalfCarrySub(f, 0U, operand, result);
|
||||
f = adjustOverflowSub(f, 0U, operand, result);
|
||||
|
||||
f = adjustSZXY<Z80>(f, result);
|
||||
|
||||
@ -549,7 +548,7 @@ void EightBit::Z80::blockCompare(uint8_t& f, const uint8_t value, const register
|
||||
f = setBit(f, PF, --counter.word);
|
||||
|
||||
f = adjustSZ<Z80>(f, result);
|
||||
adjustHalfCarrySub(f, value, contents, result);
|
||||
f = adjustHalfCarrySub(f, value, contents, result);
|
||||
f = setBit(f, NF);
|
||||
|
||||
result -= ((f & HC) >> 4);
|
||||
|
Loading…
x
Reference in New Issue
Block a user