From 329fd269ed708465664df4bf5ade05414fdcfbaa Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Mon, 28 Aug 2017 18:52:48 +0100 Subject: [PATCH] Share some more code from the 6502 processor implementation. Signed-off-by: Adrian.Conlon --- Intel8080/inc/Intel8080.h | 4 +-- Intel8080/src/Intel8080.cpp | 21 +++++++-------- LR35902/inc/LR35902.h | 4 +-- LR35902/src/LR35902.cpp | 20 +++++++------- M6502/inc/mos6502.h | 44 +++++++++++++----------------- M6502/src/mos6502.cpp | 5 ++++ Z80/inc/Z80.h | 8 +++--- Z80/src/Z80.cpp | 54 ++++++++++++++++++------------------- inc/IntelProcessor.h | 12 ++++----- inc/Processor.h | 8 +++++- 10 files changed, 91 insertions(+), 89 deletions(-) diff --git a/Intel8080/inc/Intel8080.h b/Intel8080/inc/Intel8080.h index c752487..281e97a 100644 --- a/Intel8080/inc/Intel8080.h +++ b/Intel8080/inc/Intel8080.h @@ -73,7 +73,7 @@ namespace EightBit { case 0b101: return L(); case 0b110: - return m_memory.read(HL()); + return getByte(HL()); case 0b111: return A(); default: @@ -105,7 +105,7 @@ namespace EightBit { L() = value; break; case 0b110: - m_memory.write(HL(), value); + setByte(HL(), value); break; case 0b111: A() = value; diff --git a/Intel8080/src/Intel8080.cpp b/Intel8080/src/Intel8080.cpp index 54ae09b..aab027f 100644 --- a/Intel8080/src/Intel8080.cpp +++ b/Intel8080/src/Intel8080.cpp @@ -256,13 +256,12 @@ void EightBit::Intel8080::cmc(uint8_t& a, uint8_t& f) { } void EightBit::Intel8080::xhtl() { - m_memory.ADDRESS() = SP(); - MEMPTR().low = m_memory.read(SP()); - m_memory.write(L()); + MEMPTR().low = getByte(SP()); + setByte(L()); L() = MEMPTR().low; m_memory.ADDRESS().word++; - MEMPTR().high = m_memory.read(); - m_memory.write(H()); + MEMPTR().high = getByte(); + setByte(H()); H() = MEMPTR().high; } @@ -337,13 +336,13 @@ void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { case 0: // LD (BC),A MEMPTR() = BC(); memptrReference(); - m_memory.write(MEMPTR().high = a); + setByte(MEMPTR().high = a); cycles += 7; break; case 1: // LD (DE),A MEMPTR() = DE(); memptrReference(); - m_memory.write(MEMPTR().high = a); + setByte(MEMPTR().high = a); cycles += 7; break; case 2: // LD (nn),HL @@ -354,7 +353,7 @@ void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { case 3: // LD (nn),A fetchWord(); memptrReference(); - m_memory.write(MEMPTR().high = a); + setByte(MEMPTR().high = a); cycles += 13; break; default: @@ -366,13 +365,13 @@ void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { case 0: // LD A,(BC) MEMPTR() = BC(); memptrReference(); - a = m_memory.read(); + a = getByte(); cycles += 7; break; case 1: // LD A,(DE) MEMPTR() = DE(); memptrReference(); - a = m_memory.read(); + a = getByte(); cycles += 7; break; case 2: // LD HL,(nn) @@ -383,7 +382,7 @@ void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { case 3: // LD A,(nn) fetchWord(); memptrReference(); - a = m_memory.read(); + a = getByte(); cycles += 13; break; default: diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index f706850..2fcaeb5 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -82,7 +82,7 @@ namespace EightBit { case 5: return L(); case 6: - return m_memory.read(HL()); + return getByte(HL()); case 7: return a; } @@ -110,7 +110,7 @@ namespace EightBit { L() = value; break; case 6: - m_memory.write(HL(), value); + setByte(HL(), value); break; case 7: a = value; diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 9c0e1a8..693352d 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -513,19 +513,19 @@ void EightBit::LR35902::executeOther(int x, int y, int z, int p, int q) { case 0: switch (p) { case 0: // LD (BC),A - m_memory.write(BC().word, a); + setByte(BC(), a); cycles += 2; break; case 1: // LD (DE),A - m_memory.write(DE().word, a); + setByte(DE(), a); cycles += 2; break; case 2: // GB: LDI (HL),A - m_memory.write(HL().word++, a); + setByte(HL().word++, a); cycles += 2; break; case 3: // GB: LDD (HL),A - m_memory.write(HL().word--, a); + setByte(HL().word--, a); cycles += 2; break; default: @@ -535,19 +535,19 @@ void EightBit::LR35902::executeOther(int x, int y, int z, int p, int q) { case 1: switch (p) { case 0: // LD A,(BC) - a = m_memory.read(BC().word); + a = getByte(BC()); cycles += 2; break; case 1: // LD A,(DE) - a = m_memory.read(DE().word); + a = getByte(DE()); cycles += 2; break; case 2: // GB: LDI A,(HL) - a = m_memory.read(HL().word++); + a = getByte(HL().word++); cycles += 2; break; case 3: // GB: LDD A,(HL) - a = m_memory.read(HL().word--); + a = getByte(HL().word--); cycles += 2; break; default: @@ -764,7 +764,7 @@ void EightBit::LR35902::executeOther(int x, int y, int z, int p, int q) { break; case 5: // GB: LD (nn),A fetchWord(); - m_bus.write(MEMPTR().word, a); + setByte(MEMPTR(), a); cycles += 4; break; case 6: // GB: LD A,(FF00 + C) @@ -773,7 +773,7 @@ void EightBit::LR35902::executeOther(int x, int y, int z, int p, int q) { break; case 7: // GB: LD A,(nn) fetchWord(); - a = m_bus.read(MEMPTR().word); + a = getByte(MEMPTR().word); cycles += 4; break; default: diff --git a/M6502/inc/mos6502.h b/M6502/inc/mos6502.h index 180ac2a..c02e837 100644 --- a/M6502/inc/mos6502.h +++ b/M6502/inc/mos6502.h @@ -64,12 +64,7 @@ namespace EightBit { void getWord(register16_t& output); void getWord(uint16_t offset, register16_t& output); - - uint8_t getByte() { return m_memory.read(); } - uint8_t getByte(uint16_t offset) { return m_memory.read(offset); } - - void setByte(uint8_t value) { m_memory.write(value); } - void setByte(uint16_t offset, uint8_t value) { m_memory.write(offset, value); } + void getWord(const register16_t& offset, register16_t& output); protected: virtual void interrupt(uint16_t vector); @@ -105,14 +100,12 @@ namespace EightBit { void Address_ZeroPageIndirect() { Address_ZeroPage(); - m_memory.ADDRESS() = MEMPTR(); - getWord(MEMPTR()); + getWord(MEMPTR(), MEMPTR()); } void Address_Indirect() { Address_Absolute(); - m_memory.ADDRESS() = MEMPTR(); - getWord(MEMPTR()); + getWord(MEMPTR(), MEMPTR()); } void Address_ZeroPageX() { @@ -137,8 +130,7 @@ namespace EightBit { void Address_IndexedIndirectX() { Address_ZeroPageX(); - m_memory.ADDRESS() = MEMPTR(); - getWord(MEMPTR()); + getWord(MEMPTR(), MEMPTR()); } void Address_IndirectIndexedY() { @@ -160,12 +152,12 @@ namespace EightBit { uint8_t AM_Absolute() { Address_Absolute(); - return m_memory.read(MEMPTR()); + return getByte(MEMPTR()); } uint8_t AM_ZeroPage() { Address_ZeroPage(); - return m_memory.read(MEMPTR()); + return getByte(MEMPTR()); } uint8_t AM_AbsoluteX() { @@ -186,17 +178,17 @@ namespace EightBit { uint8_t AM_ZeroPageX() { Address_ZeroPageX(); - return m_memory.read(MEMPTR()); + return getByte(MEMPTR()); } uint8_t AM_ZeroPageY() { Address_ZeroPageY(); - return m_memory.read(MEMPTR()); + return getByte(MEMPTR()); } uint8_t AM_IndexedIndirectX() { Address_IndexedIndirectX(); - return m_memory.read(MEMPTR()); + return getByte(MEMPTR()); } uint8_t AM_IndirectIndexedY() { @@ -217,42 +209,42 @@ namespace EightBit { void AM_Absolute(uint8_t value) { Address_Absolute(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_ZeroPage(uint8_t value) { Address_ZeroPage(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_AbsoluteX(uint8_t value) { Address_AbsoluteX(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_AbsoluteY(uint8_t value) { Address_AbsoluteY(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_ZeroPageX(uint8_t value) { Address_ZeroPageX(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_ZeroPageY(uint8_t value) { Address_ZeroPageY(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_IndexedIndirectX(uint8_t value) { Address_IndexedIndirectX(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } void AM_IndirectIndexedY(uint8_t value) { Address_IndirectIndexedY(); - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); } #pragma endregion Addressing modes, write @@ -392,7 +384,7 @@ namespace EightBit { case 0b011: case 0b101: case 0b111: - m_memory.write(MEMPTR(), value); + setByte(MEMPTR(), value); break; case 0b000: case 0b100: diff --git a/M6502/src/mos6502.cpp b/M6502/src/mos6502.cpp index 21e1ab4..68d6d21 100644 --- a/M6502/src/mos6502.cpp +++ b/M6502/src/mos6502.cpp @@ -78,6 +78,11 @@ void EightBit::MOS6502::getWord(uint16_t offset, register16_t& output) { getWord(output); } +void EightBit::MOS6502::getWord(const register16_t& offset, register16_t& output) { + m_memory.ADDRESS() = offset; + getWord(output); +} + void EightBit::MOS6502::interrupt(uint16_t vector) { pushWord(PC()); push(P()); diff --git a/Z80/inc/Z80.h b/Z80/inc/Z80.h index 8feee7a..85a6a23 100644 --- a/Z80/inc/Z80.h +++ b/Z80/inc/Z80.h @@ -161,7 +161,7 @@ namespace EightBit { case 5: return HL2().low; case 6: - return m_memory.read(m_displaced ? displacedAddress() : HL().word); + return getByte(m_displaced ? displacedAddress() : HL().word); case 7: return a; default: @@ -191,7 +191,7 @@ namespace EightBit { HL2().low = value; break; case 6: - m_memory.write(m_displaced ? displacedAddress() : HL().word, value); + setByte(m_displaced ? displacedAddress() : HL().word, value); break; case 7: a = value; @@ -216,7 +216,7 @@ namespace EightBit { case 5: return L(); case 6: - return m_memory.read(HL()); + return getByte(HL()); case 7: return a; default: @@ -246,7 +246,7 @@ namespace EightBit { L() = value; break; case 6: - m_memory.write(HL(), value); + setByte(HL(), value); break; case 7: a = value; diff --git a/Z80/src/Z80.cpp b/Z80/src/Z80.cpp index 829bef6..51751b4 100644 --- a/Z80/src/Z80.cpp +++ b/Z80/src/Z80.cpp @@ -516,12 +516,12 @@ void EightBit::Z80::ccf(uint8_t a, uint8_t& f) { } void EightBit::Z80::xhtl(register16_t& operand) { - MEMPTR().low = m_memory.read(SP()); - m_memory.write(operand.low); + MEMPTR().low = getByte(SP()); + setByte(operand.low); operand.low = MEMPTR().low; m_memory.ADDRESS().word++; - MEMPTR().high = m_memory.read(); - m_memory.write(operand.high); + MEMPTR().high = getByte(); + setByte(operand.high); operand.high = MEMPTR().high; } @@ -533,7 +533,7 @@ void EightBit::Z80::xhtl(register16_t& operand) { void EightBit::Z80::blockCompare(uint8_t a, uint8_t& f) { - const auto value = m_memory.read(HL()); + const auto value = getByte(HL()); uint8_t result = a - value; setFlag(f, PF, --BC().word); @@ -583,8 +583,8 @@ bool EightBit::Z80::cpdr(uint8_t a, uint8_t& f) { #pragma region Block load instructions void EightBit::Z80::blockLoad(uint8_t a, uint8_t& f, register16_t source, register16_t destination) { - const auto value = m_memory.read(source); - m_memory.write(destination, value); + const auto value = getByte(source); + setByte(destination, value); const auto xy = a + value; setFlag(f, XF, xy & 8); setFlag(f, YF, xy & 2); @@ -629,7 +629,7 @@ void EightBit::Z80::ini(uint8_t& f) { MEMPTR().word++; readPort(); auto value = m_memory.DATA(); - m_memory.write(HL().word++, value); + setByte(HL().word++, value); decrement(f, B()); setFlag(f, NF); } @@ -639,7 +639,7 @@ void EightBit::Z80::ind(uint8_t& f) { MEMPTR().word--; readPort(); auto value = m_memory.DATA(); - m_memory.write(HL().word--, value); + setByte(HL().word--, value); decrement(f, B()); setFlag(f, NF); } @@ -659,7 +659,7 @@ bool EightBit::Z80::indr(uint8_t& f) { #pragma region Block output instructions void EightBit::Z80::blockOut(uint8_t& f) { - auto value = m_memory.read(); + auto value = getByte(); m_memory.ADDRESS() = BC(); writePort(); decrement(f, B()); @@ -699,8 +699,8 @@ bool EightBit::Z80::otdr(uint8_t& f) { void EightBit::Z80::rrd(uint8_t& a, uint8_t& f) { MEMPTR() = HL(); memptrReference(); - auto memory = m_memory.read(); - m_memory.write(promoteNibble(a) | highNibble(memory)); + auto memory = getByte(); + setByte(promoteNibble(a) | highNibble(memory)); a = (a & 0xf0) | lowNibble(memory); adjustSZPXY(f, a); clearFlag(f, NF | HC); @@ -709,8 +709,8 @@ void EightBit::Z80::rrd(uint8_t& a, uint8_t& f) { void EightBit::Z80::rld(uint8_t& a, uint8_t& f) { MEMPTR() = HL(); memptrReference(); - auto memory = m_memory.read(); - m_memory.write(promoteNibble(memory) | lowNibble(a)); + auto memory = getByte(); + setByte(promoteNibble(memory) | lowNibble(a)); a = (a & 0xf0) | highNibble(memory); adjustSZPXY(f, a); clearFlag(f, NF | HC); @@ -795,7 +795,7 @@ void EightBit::Z80::executeCB(int x, int y, int z) { auto& f = F(); switch (x) { case 0: { // rot[y] r[z] - auto operand = m_displaced ? m_memory.read(displacedAddress()) : R(z, a); + auto operand = m_displaced ? getByte(displacedAddress()) : R(z, a); switch (y) { case 0: operand = rlc(f, operand); @@ -828,7 +828,7 @@ void EightBit::Z80::executeCB(int x, int y, int z) { if (m_displaced) { if (z != 6) R2(z, a, operand); - m_memory.write(operand); + setByte(operand); cycles += 15; } else { R(z, a, operand); @@ -848,7 +848,7 @@ void EightBit::Z80::executeCB(int x, int y, int z) { adjustXY(f, operand); } } else { - bit(f, y, m_memory.read(displacedAddress())); + bit(f, y, getByte(displacedAddress())); adjustXY(f, MEMPTR().high); cycles += 12; } @@ -860,9 +860,9 @@ void EightBit::Z80::executeCB(int x, int y, int z) { if (z == 6) cycles += 7; } else { - auto operand = m_memory.read(displacedAddress()); + auto operand = getByte(displacedAddress()); operand = res(y, operand); - m_memory.write(operand); + setByte(operand); R2(z, a, operand); cycles += 15; } @@ -874,9 +874,9 @@ void EightBit::Z80::executeCB(int x, int y, int z) { if (z == 6) cycles += 7; } else { - auto operand = m_memory.read(displacedAddress()); + auto operand = getByte(displacedAddress()); operand = set(y, operand); - m_memory.write(operand); + setByte(operand); R2(z, a, operand); cycles += 15; } @@ -1179,13 +1179,13 @@ void EightBit::Z80::executeOther(int x, int y, int z, int p, int q) { case 0: // LD (BC),A MEMPTR() = BC(); memptrReference(); - m_memory.write(MEMPTR().high = a); + setByte(MEMPTR().high = a); cycles += 7; break; case 1: // LD (DE),A MEMPTR() = DE(); memptrReference(); - m_memory.write(MEMPTR().high = a); + setByte(MEMPTR().high = a); cycles += 7; break; case 2: // LD (nn),HL @@ -1196,7 +1196,7 @@ void EightBit::Z80::executeOther(int x, int y, int z, int p, int q) { case 3: // LD (nn),A fetchWord(); memptrReference(); - m_memory.write(MEMPTR().high = a); + setByte(MEMPTR().high = a); cycles += 13; break; default: @@ -1208,13 +1208,13 @@ void EightBit::Z80::executeOther(int x, int y, int z, int p, int q) { case 0: // LD A,(BC) MEMPTR() = BC(); memptrReference(); - a = m_memory.read(); + a = getByte(); cycles += 7; break; case 1: // LD A,(DE) MEMPTR() = DE(); memptrReference(); - a = m_memory.read(); + a = getByte(); cycles += 7; break; case 2: // LD HL,(nn) @@ -1225,7 +1225,7 @@ void EightBit::Z80::executeOther(int x, int y, int z, int p, int q) { case 3: // LD A,(nn) fetchWord(); memptrReference(); - a = m_memory.read(); + a = getByte(); cycles += 13; break; default: diff --git a/inc/IntelProcessor.h b/inc/IntelProcessor.h index 278f9fc..cdf016c 100644 --- a/inc/IntelProcessor.h +++ b/inc/IntelProcessor.h @@ -120,11 +120,11 @@ namespace EightBit { } virtual void push(uint8_t value) { - m_memory.write(--SP().word, value); + setByte(--SP().word, value); } virtual uint8_t pop() { - return m_memory.read(SP().word++); + return getByte(SP().word++); } void fetchWord() { @@ -140,16 +140,16 @@ namespace EightBit { virtual void getWordViaMemptr(register16_t& value) { memptrReference(); - value.low = m_memory.read(); + value.low = getByte(); m_memory.ADDRESS().word++; - value.high = m_memory.read(); + value.high = getByte(); } virtual void setWordViaMemptr(register16_t value) { memptrReference(); - m_memory.write(value.low); + setByte(value.low); m_memory.ADDRESS().word++; - m_memory.write(value.high); + setByte(value.high); } // diff --git a/inc/Processor.h b/inc/Processor.h index f6e44bf..a3fd0aa 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -76,7 +76,7 @@ namespace EightBit { int cycles; virtual uint8_t fetchByte() { - return m_memory.read(PC().word++); + return getByte(PC().word++); } virtual void fetchWord(register16_t& output) { @@ -88,6 +88,12 @@ namespace EightBit { return execute(fetchByte()); } + uint8_t getByte() { return m_memory.read(); } + template uint8_t getByte(T offset) { return m_memory.read(offset); } + + void setByte(uint8_t value) { m_memory.write(value); } + template void setByte(T offset, uint8_t value) { m_memory.write(offset, value); } + virtual void push(uint8_t value) = 0; virtual uint8_t pop() = 0;