From 29edc46966911ccf33145e6a656ace889c559c7c Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Wed, 10 Jan 2018 23:08:14 +0000 Subject: [PATCH] Simplify some MEMPTR usage in Intel processors. Signed-off-by: Adrian Conlon --- Intel8080/inc/Intel8080.h | 2 +- Intel8080/src/Intel8080.cpp | 35 +++++++++++++++++------------------ LR35902/inc/LR35902.h | 4 ++-- LR35902/src/LR35902.cpp | 18 +++++++++--------- Z80/src/Z80.cpp | 37 +++++++++++++++++-------------------- inc/IntelProcessor.h | 9 ++------- src/IntelProcessor.cpp | 10 ++++------ 7 files changed, 52 insertions(+), 63 deletions(-) diff --git a/Intel8080/inc/Intel8080.h b/Intel8080/inc/Intel8080.h index 199b610..43cc9d4 100644 --- a/Intel8080/inc/Intel8080.h +++ b/Intel8080/inc/Intel8080.h @@ -142,7 +142,7 @@ namespace EightBit { static void subtract(uint8_t& f, uint8_t& operand, uint8_t value, int carry = 0); - void execute(int x, int y, int z, int p, int q); + void execute(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q); static void increment(uint8_t& f, uint8_t& operand); static void decrement(uint8_t& f, uint8_t& operand); diff --git a/Intel8080/src/Intel8080.cpp b/Intel8080/src/Intel8080.cpp index 276d390..36b6b7d 100644 --- a/Intel8080/src/Intel8080.cpp +++ b/Intel8080/src/Intel8080.cpp @@ -296,7 +296,11 @@ int EightBit::Intel8080::execute(uint8_t opcode) { const auto p = decoded.p; const auto q = decoded.q; - execute(x, y, z, p, q); + auto& af = AF(); + auto& a = af.high; + auto& f = af.low; + + execute(a, f, x, y, z, p, q); if (UNLIKELY(cycles() == 0)) throw std::logic_error("Unhandled opcode"); @@ -304,9 +308,7 @@ int EightBit::Intel8080::execute(uint8_t opcode) { return cycles(); } -void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { - auto& a = A(); - auto& f = F(); +void EightBit::Intel8080::execute(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q) { switch (x) { case 0: switch (z) { @@ -337,25 +339,25 @@ void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { switch (p) { case 0: // LD (BC),A MEMPTR() = BC(); - memptrReference(); - setByte(MEMPTR().high = a); + setByte(MEMPTR().word++, a); + MEMPTR().high = a; addCycles(7); break; case 1: // LD (DE),A MEMPTR() = DE(); - memptrReference(); - setByte(MEMPTR().high = a); + setByte(MEMPTR().word++, a); + MEMPTR().high = a; addCycles(7); break; case 2: // LD (nn),HL fetchWord(); - setWordViaMemptr(HL()); + setWord(HL()); addCycles(16); break; case 3: // LD (nn),A fetchWord(); - memptrReference(); - setByte(MEMPTR().high = a); + setByte(MEMPTR().word++, a); + MEMPTR().high = a; addCycles(13); break; default: @@ -366,25 +368,22 @@ void EightBit::Intel8080::execute(int x, int y, int z, int p, int q) { switch (p) { case 0: // LD A,(BC) MEMPTR() = BC(); - memptrReference(); - a = getByte(); + a = getByte(MEMPTR().word++); addCycles(7); break; case 1: // LD A,(DE) MEMPTR() = DE(); - memptrReference(); - a = getByte(); + a = getByte(MEMPTR().word++); addCycles(7); break; case 2: // LD HL,(nn) fetchWord(); - getWordViaMemptr(HL()); + getWord(HL()); addCycles(16); break; case 3: // LD A,(nn) fetchWord(); - memptrReference(); - a = getByte(); + a = getByte(MEMPTR().word++); addCycles(13); break; default: diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index 6e79800..2cd6024 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -160,8 +160,8 @@ namespace EightBit { static void subtract(uint8_t& f, uint8_t& operand, uint8_t value, int carry = 0); - void executeCB(int x, int y, int z, int p, int q); - void executeOther(int x, int y, int z, int p, int q); + void executeCB(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q); + void executeOther(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q); static void increment(uint8_t& f, uint8_t& operand); static void decrement(uint8_t& f, uint8_t& operand); diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 5930ee3..27bb3e9 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -338,10 +338,14 @@ int EightBit::GameBoy::LR35902::execute(uint8_t opcode) { const auto p = decoded.p; const auto q = decoded.q; + auto& af = AF(); + auto& a = af.high; + auto& f = af.low; + if (LIKELY(!m_prefixCB)) - executeOther(x, y, z, p, q); + executeOther(a, f, x, y, z, p, q); else - executeCB(x, y, z, p, q); + executeCB(a, f, x, y, z, p, q); if (UNLIKELY(cycles() == 0)) throw std::logic_error("Unhandled opcode"); @@ -349,9 +353,7 @@ int EightBit::GameBoy::LR35902::execute(uint8_t opcode) { return clockCycles(); } -void EightBit::GameBoy::LR35902::executeCB(int x, int y, int z, int p, int q) { - auto& a = A(); - auto& f = F(); +void EightBit::GameBoy::LR35902::executeCB(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q) { switch (x) { case 0: { // rot[y] r[z] auto operand = R(z, a); @@ -412,9 +414,7 @@ void EightBit::GameBoy::LR35902::executeCB(int x, int y, int z, int p, int q) { } } -void EightBit::GameBoy::LR35902::executeOther(int x, int y, int z, int p, int q) { - auto& a = A(); - auto& f = F(); +void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int y, int z, int p, int q) { switch (x) { case 0: switch (z) { @@ -425,7 +425,7 @@ void EightBit::GameBoy::LR35902::executeOther(int x, int y, int z, int p, int q) break; case 1: // GB: LD (nn),SP fetchWord(); - setWordViaMemptr(SP()); + setWord(SP()); addCycles(5); break; case 2: // GB: STOP diff --git a/Z80/src/Z80.cpp b/Z80/src/Z80.cpp index 7791d5e..0d270bb 100644 --- a/Z80/src/Z80.cpp +++ b/Z80/src/Z80.cpp @@ -602,9 +602,9 @@ bool EightBit::Z80::otdr(uint8_t& f) { } void EightBit::Z80::rrd(uint8_t& a, uint8_t& f) { - MEMPTR() = HL(); - memptrReference(); + BUS().ADDRESS() = MEMPTR() = HL(); const auto memory = getByte(); + MEMPTR().word++; setByte(promoteNibble(a) | highNibble(memory)); a = (a & 0xf0) | lowNibble(memory); adjustSZPXY(f, a); @@ -612,9 +612,9 @@ void EightBit::Z80::rrd(uint8_t& a, uint8_t& f) { } void EightBit::Z80::rld(uint8_t& a, uint8_t& f) { - MEMPTR() = HL(); - memptrReference(); + BUS().ADDRESS() = MEMPTR() = HL(); const auto memory = getByte(); + MEMPTR().word++; setByte(promoteNibble(memory) | lowNibble(a)); a = (a & 0xf0) | highNibble(memory); adjustSZPXY(f, a); @@ -871,11 +871,11 @@ void EightBit::Z80::executeED(uint8_t& a, uint8_t& f, const int x, const int y, switch (q) { case 0: // LD (nn), rp[p] fetchWord(); - setWordViaMemptr(RP(p)); + setWord(RP(p)); break; case 1: // LD rp[p], (nn) fetchWord(); - getWordViaMemptr(RP(p)); + getWord(RP(p)); break; default: UNREACHABLE; @@ -1114,25 +1114,25 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int switch (p) { case 0: // LD (BC),A MEMPTR() = BC(); - memptrReference(); - setByte(MEMPTR().high = a); + setByte(MEMPTR().word++, a); + MEMPTR().high = a; addCycles(7); break; case 1: // LD (DE),A MEMPTR() = DE(); - memptrReference(); - setByte(MEMPTR().high = a); + setByte(MEMPTR().word++, a); + MEMPTR().high = a; addCycles(7); break; case 2: // LD (nn),HL fetchWord(); - setWordViaMemptr(HL2()); + setWord(HL2()); addCycles(16); break; case 3: // LD (nn),A fetchWord(); - memptrReference(); - setByte(MEMPTR().high = a); + setByte(MEMPTR().word++, a); + MEMPTR().high = a; addCycles(13); break; default: @@ -1143,25 +1143,22 @@ void EightBit::Z80::executeOther(uint8_t& a, uint8_t& f, const int x, const int switch (p) { case 0: // LD A,(BC) MEMPTR() = BC(); - memptrReference(); - a = getByte(); + a = getByte(MEMPTR().word++); addCycles(7); break; case 1: // LD A,(DE) MEMPTR() = DE(); - memptrReference(); - a = getByte(); + a = getByte(MEMPTR().word++); addCycles(7); break; case 2: // LD HL,(nn) fetchWord(); - getWordViaMemptr(HL2()); + getWord(HL2()); addCycles(16); break; case 3: // LD A,(nn) fetchWord(); - memptrReference(); - a = getByte(); + a = getByte(MEMPTR().word++); addCycles(13); break; default: diff --git a/inc/IntelProcessor.h b/inc/IntelProcessor.h index 89494ec..aca5ed7 100644 --- a/inc/IntelProcessor.h +++ b/inc/IntelProcessor.h @@ -120,13 +120,8 @@ namespace EightBit { // - void memptrReference() { - BUS().ADDRESS() = MEMPTR(); - MEMPTR().word++; - } - - void getWordViaMemptr(register16_t& value); - void setWordViaMemptr(register16_t value); + void getWord(register16_t& value); + void setWord(register16_t value); // diff --git a/src/IntelProcessor.cpp b/src/IntelProcessor.cpp index 58fd3bb..5dd2b96 100644 --- a/src/IntelProcessor.cpp +++ b/src/IntelProcessor.cpp @@ -20,16 +20,14 @@ uint8_t EightBit::IntelProcessor::pop() { return getByte(SP().word++); } -void EightBit::IntelProcessor::getWordViaMemptr(register16_t& value) { - memptrReference(); - value.low = getByte(); +void EightBit::IntelProcessor::getWord(register16_t& value) { + value.low = getByte(MEMPTR().word++); BUS().ADDRESS().word++; value.high = getByte(); } -void EightBit::IntelProcessor::setWordViaMemptr(register16_t value) { - memptrReference(); - setByte(value.low); +void EightBit::IntelProcessor::setWord(register16_t value) { + setByte(MEMPTR().word++, value.low); BUS().ADDRESS().word++; setByte(value.high); }