Simplify register usage

This commit is contained in:
Adrian Conlon committed 2026-03-14 13:09:28 +00:00
1 parent 8ef462f5c4
commit 81c1703ea9
5 files changed
+66 -67

No files matched your search

+19 -17
View File
@@ -362,7 +362,9 @@ namespace EightBit {
}
}
auto subtract(uint8_t operand, uint8_t value, int carry = 0) noexcept {
auto subtract(uint8_t value, int carry = 0) noexcept {
const auto operand = A();
const register16_t subtraction = operand - value - carry;
const auto result = subtraction.low;
@@ -407,11 +409,13 @@ namespace EightBit {
void returnConditionalFlag(int flag) noexcept final;
[[nodiscard]] register16_t sbc(register16_t operand, register16_t value) noexcept;
[[nodiscard]] register16_t adc(register16_t operand, register16_t value) noexcept;
[[nodiscard]] register16_t add(register16_t operand, register16_t value, int carry = 0) noexcept;
void sbc(register16_t value) noexcept;
void adc(register16_t value) noexcept;
void add(register16_t value, int carry = 0) noexcept;
[[nodiscard]] auto add(uint8_t operand, uint8_t value, int carry = 0) noexcept {
void add(uint8_t value, int carry = 0) noexcept {
const auto operand = A();
intermediate() = operand + value + carry;
const auto result = intermediate().low;
@@ -423,21 +427,19 @@ namespace EightBit {
setBit(CF, carryTest(intermediate().high));
adjustSZXY(result);
return result;
A() = result;
}
[[nodiscard]] auto adc(uint8_t operand, uint8_t value) noexcept {
return add(operand, value, carry());
void adc(uint8_t value) noexcept {
add(value, carry());
}
[[nodiscard]] auto sub(uint8_t operand, uint8_t value, int carry = 0) noexcept {
const auto subtraction = subtract(operand, value, carry);
adjustSZXY(subtraction);
return subtraction;
void sub(uint8_t value, int carry = 0) noexcept {
adjustSZXY(A() = subtract(value, carry));
}
[[nodiscard]] auto sbc(uint8_t operand, uint8_t value) noexcept {
return sub(operand, value, carry());
void sbc(uint8_t value) noexcept {
sub(value, carry());
}
[[nodiscard]] void andr(uint8_t value) noexcept {
@@ -457,7 +459,7 @@ namespace EightBit {
}
void compare(uint8_t value) noexcept {
subtract(A(), value);
subtract(value);
adjustXY(value);
}
@@ -564,13 +566,13 @@ namespace EightBit {
adjustSZPXY(A() = updated);
}
void scf(uint8_t operand) noexcept {
void scf() noexcept {
setBit(CF);
clearBit(HC | NF);
adjustXY((Q() ^ F()) | A());
}
void ccf(uint8_t operand) noexcept {
void ccf() noexcept {
clearBit(NF);
const auto carrying = carry();
setBit(HC, carrying);
+35 -32
View File
@@ -175,14 +175,14 @@ uint8_t EightBit::Z80::memoryRead() noexcept {
lowerMREQ();
lowerRD();
tick();
IntelProcessor::memoryRead();
const auto data = IntelProcessor::memoryRead();
raiseRD();
raiseMREQ();
if (lowered(M1()))
refreshMemory();
tick();
ReadMemory.fire();
return BUS().DATA();
return data;
}
void EightBit::Z80::handleRESET() noexcept {
@@ -280,7 +280,9 @@ void EightBit::Z80::jumpRelative(int8_t offset) noexcept {
tick(5);
}
EightBit::register16_t EightBit::Z80::sbc(const register16_t operand, const register16_t value) noexcept {
void EightBit::Z80::sbc(const register16_t value) noexcept {
const auto operand = HL2();
const auto subtraction = operand.word - value.word - carry();
intermediate() = subtraction;
@@ -300,25 +302,26 @@ EightBit::register16_t EightBit::Z80::sbc(const register16_t operand, const regi
MEMPTR() = operand + 1;
return intermediate();
HL2() = intermediate();
}
EightBit::register16_t EightBit::Z80::adc(const register16_t operand, const register16_t value) noexcept {
void EightBit::Z80::adc(const register16_t value) noexcept {
auto _ = add(operand, value, carry());
const auto beforeNegative = signTest(HL2().high);
const auto valueNegative = signTest(value.high);
add(value, carry());
setBit(ZF, intermediate().zero());
const auto beforeNegative = signTest(operand.high);
const auto valueNegative = signTest(value.high);
const auto afterNegative = signTest(intermediate().high);
setBit(SF, afterNegative);
adjustOverflowAdd(beforeNegative, valueNegative, afterNegative);
return intermediate();
}
EightBit::register16_t EightBit::Z80::add(const register16_t operand, const register16_t value, int carry) noexcept {
void EightBit::Z80::add(const register16_t value, int carry) noexcept {
const auto operand = HL2();
const auto addition = operand.word + value.word + carry;
intermediate().word = addition;
@@ -330,7 +333,7 @@ EightBit::register16_t EightBit::Z80::add(const register16_t operand, const regi
MEMPTR() = operand + 1;
return intermediate();
HL2() = intermediate();
}
void EightBit::Z80::xhtl(register16_t& exchange) noexcept {
@@ -349,13 +352,13 @@ void EightBit::Z80::xhtl(register16_t& exchange) noexcept {
void EightBit::Z80::blockCompare() noexcept {
IntelProcessor::memoryRead(HL());
uint8_t result = A() - BUS().DATA();
const auto data = IntelProcessor::memoryRead(HL());
uint8_t result = A() - data;
setBit(PF, --BC().word);
adjustSZ(result);
adjustHalfCarrySub(A(), BUS().DATA(), result);
adjustHalfCarrySub(A(), data, result);
setBit(NF);
result -= (halfCarry() >> 4);
@@ -398,9 +401,9 @@ void EightBit::Z80::cpdr() noexcept {
}
void EightBit::Z80::blockLoad() noexcept {
IntelProcessor::memoryRead(HL());
const auto data = IntelProcessor::memoryRead(HL());
IntelProcessor::memoryWrite(DE());
const auto xy = A() + BUS().DATA();
const auto xy = A() + data;
setBit(XF, xy & Bit3);
setBit(YF, xy & Bit1);
clearBit(NF | HC);
@@ -654,9 +657,9 @@ void EightBit::Z80::fetchDisplacement() noexcept {
// is in the HALT state
uint8_t EightBit::Z80::fetchInstruction() noexcept {
lowerM1();
IntelProcessor::fetchInstruction();
const auto data = IntelProcessor::fetchInstruction();
raiseM1();
return BUS().DATA();
return data;
}
void EightBit::Z80::loadAccumulatorIndirect(addresser_t addresser) noexcept {
@@ -933,10 +936,10 @@ void EightBit::Z80::executeED(const int x, const int y, const int z, const int p
case 2: // 16-bit add/subtract with carry
switch (q) {
case 0: // SBC HL, rp[p]
HL2() = sbc(HL2(), RP(p));
sbc(RP(p));
break;
case 1: // ADC HL, rp[p]
HL2() = adc(HL2(), RP(p));
adc(RP(p));
break;
default:
UNREACHABLE;
@@ -1131,7 +1134,7 @@ void EightBit::Z80::executeOther(const int x, const int y, const int z, const in
RP(p) = fetchWord();
break;
case 1: // ADD HL,rp
HL2() = add(HL2(), RP(p));
add(RP(p));
tick(7);
break;
default:
@@ -1243,10 +1246,10 @@ void EightBit::Z80::executeOther(const int x, const int y, const int z, const in
cpl();
break;
case 6:
scf(A());
scf();
break;
case 7:
ccf(A());
ccf();
break;
default:
UNREACHABLE;
@@ -1303,16 +1306,16 @@ void EightBit::Z80::executeOther(const int x, const int y, const int z, const in
const auto value = R(z);
switch (y) {
case 0: // ADD A,r
A() = add(A(), value);
add(value);
break;
case 1: // ADC A,r
A() = adc(A(), value);
adc(value);
break;
case 2: // SUB r
A() = sub(A(), value);
sub(value);
break;
case 3: // SBC A,r
A() = sbc(A(), value);
sbc(value);
break;
case 4: // AND r
andr(value);
@@ -1442,16 +1445,16 @@ void EightBit::Z80::executeOther(const int x, const int y, const int z, const in
const auto operand = fetchByte();
switch (y) {
case 0: // ADD A,n
A() = add(A(), operand);
add(operand);
break;
case 1: // ADC A,n
A() = adc(A(), operand);
adc(operand);
break;
case 2: // SUB n
A() = sub(A(), operand);
sub(operand);
break;
case 3: // SBC A,n
A() = sbc(A(), operand);
sbc(operand);
break;
case 4: // AND n
andr(operand);
+5 -7
View File
@@ -160,13 +160,13 @@ namespace EightBit {
//
virtual void restart(uint8_t address);
virtual void callConditional(bool condition);
void restart(uint8_t address);
void callConditional(bool condition);
virtual void jumpConditional(bool condition);
virtual void jumpRelativeConditional(bool condition);
void jumpRelativeConditional(bool condition);
virtual void returnConditional(bool condition);
virtual void jumpIndirect();
virtual void jump();
void jumpIndirect();
void jump();
void callIndirect();
void call();
virtual void call(register16_t destination);
@@ -174,8 +174,6 @@ namespace EightBit {
void jumpRelative(uint8_t offset) noexcept { jumpRelative((int8_t)offset); }
void ret() override;
virtual void fetchWordMEMPTR();
void resetWorkingRegisters() noexcept;
[[nodiscard]] virtual bool convertCondition(int flag) noexcept = 0;
+1
View File
@@ -7,6 +7,7 @@
namespace EightBit {
class Mapper {
public:
virtual ~Mapper() noexcept = default;
[[nodiscard]] virtual MemoryMapping mapping(uint16_t address) noexcept = 0;
};
}
+6 -11
View File
@@ -59,8 +59,8 @@ EightBit::register16_t& EightBit::IntelProcessor::incrementPC() {
}
uint8_t EightBit::IntelProcessor::fetchInstruction() {
fetchByte();
return lowered(HALT()) ? (uint8_t)0 : BUS().DATA();
const auto data = fetchByte();
return lowered(HALT()) ? (uint8_t)0 : data;
}
EightBit::register16_t EightBit::IntelProcessor::getWord() {
@@ -80,13 +80,13 @@ void EightBit::IntelProcessor::restart(const uint8_t address) {
}
void EightBit::IntelProcessor::callConditional(bool condition) {
fetchWordMEMPTR();
MEMPTR() = fetchWord();
if (condition)
call();
}
void EightBit::IntelProcessor::jumpConditional(bool condition) {
fetchWordMEMPTR();
MEMPTR() = fetchWord();
if (condition)
jump();
}
@@ -113,13 +113,8 @@ void EightBit::IntelProcessor::ret() {
MEMPTR() = PC();
}
void EightBit::IntelProcessor::fetchWordMEMPTR() {
const auto _ = fetchWord();
MEMPTR() = intermediate();
}
void EightBit::IntelProcessor::jumpIndirect() {
fetchWordMEMPTR();
MEMPTR() = fetchWord();
jump();
}
@@ -128,7 +123,7 @@ void EightBit::IntelProcessor::jump() {
}
void EightBit::IntelProcessor::callIndirect() {
fetchWordMEMPTR();
MEMPTR() = fetchWord();
call();
}