From 67487b5b6ef1b4a2fd317cdfd626e914d627068a Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Sat, 16 Jun 2018 00:55:32 +0100 Subject: [PATCH] Simplify the usage of the register16_t union. Signed-off-by: Adrian Conlon --- Intel8080/inc/Intel8080.h | 6 +++--- Intel8080/src/Intel8080.cpp | 13 +++++-------- Intel8080/test/Configuration.h | 6 +++--- LR35902/inc/IoRegisters.h | 2 +- LR35902/inc/LR35902.h | 15 +++++++-------- LR35902/src/LR35902.cpp | 6 ++---- M6502/inc/mos6502.h | 2 +- M6502/src/mos6502.cpp | 10 ++++------ Z80/inc/Z80.h | 6 +++--- Z80/src/Z80.cpp | 18 ++++++++---------- Z80/test/Configuration.h | 6 +++--- inc/Bus.h | 14 +++++++++----- inc/IntelProcessor.h | 4 ++-- inc/Processor.h | 16 +++++++--------- inc/Register.h | 7 +++++-- src/Bus.cpp | 27 +++------------------------ src/IntelProcessor.cpp | 9 +++++---- 17 files changed, 71 insertions(+), 96 deletions(-) diff --git a/Intel8080/inc/Intel8080.h b/Intel8080/inc/Intel8080.h index b92a4e8..d37be25 100644 --- a/Intel8080/inc/Intel8080.h +++ b/Intel8080/inc/Intel8080.h @@ -47,9 +47,9 @@ namespace EightBit { InputOutput& m_ports; register16_t af; - register16_t bc = { { 0xff, 0xff } }; - register16_t de = { { 0xff, 0xff } }; - register16_t hl = { { 0xff, 0xff } }; + register16_t bc = 0xffff; + register16_t de = 0xffff; + register16_t hl = 0xffff; uint8_t R(int r) { switch (r) { diff --git a/Intel8080/src/Intel8080.cpp b/Intel8080/src/Intel8080.cpp index 20caabe..232244c 100644 --- a/Intel8080/src/Intel8080.cpp +++ b/Intel8080/src/Intel8080.cpp @@ -12,10 +12,9 @@ EightBit::register16_t& EightBit::Intel8080::AF() { } EightBit::register16_t EightBit::Intel8080::AF() const { - register16_t returned; - returned.low = (af.low | Bit1) & ~(Bit5 | Bit3); - returned.high = af.high; - return returned; + const auto low = (af.low | Bit1) & ~(Bit5 | Bit3); + const auto high = af.high; + return register16_t(low, high); } EightBit::register16_t EightBit::Intel8080::BC() const { @@ -142,8 +141,7 @@ void EightBit::Intel8080::add(register16_t value) { void EightBit::Intel8080::add(uint8_t value, int carry) { - register16_t result; - result.word = A() + value + carry; + const register16_t result = A() + value + carry; adjustAuxiliaryCarryAdd(F(), A(), value, result.word); @@ -159,8 +157,7 @@ void EightBit::Intel8080::adc(uint8_t value) { void EightBit::Intel8080::subtract(uint8_t& operand, uint8_t value, int carry) { - register16_t result; - result.word = operand - value - carry; + const register16_t result = operand - value - carry; adjustAuxiliaryCarrySub(F(), operand, value, result.word); diff --git a/Intel8080/test/Configuration.h b/Intel8080/test/Configuration.h index d5d2717..213fc69 100644 --- a/Intel8080/test/Configuration.h +++ b/Intel8080/test/Configuration.h @@ -29,9 +29,9 @@ public: } EightBit::register16_t getStartAddress() const { - EightBit::register16_t returned; - returned.word = 0x100; - return returned; + //EightBit::register16_t returned; + //returned.word = 0x100; + return 0x100; } private: diff --git a/LR35902/inc/IoRegisters.h b/LR35902/inc/IoRegisters.h index 90d0a11..ae143f8 100644 --- a/LR35902/inc/IoRegisters.h +++ b/LR35902/inc/IoRegisters.h @@ -172,7 +172,7 @@ namespace EightBit { int m_timerCounter = 0; int m_timerRate = 0; - register16_t m_dmaAddress = { 0, 0 }; + register16_t m_dmaAddress; bool m_dmaTransferActive = false; bool m_scanP15 = false; diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index 36fb2d8..e238205 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -36,10 +36,9 @@ namespace EightBit { } register16_t AF() const final { - register16_t returned; - returned.low = higherNibble(af.low); - returned.high = af.high; - return returned; + const auto low = higherNibble(af.low); + const auto high = af.high; + return register16_t(low, high); } virtual register16_t BC() const final { return bc; } @@ -59,10 +58,10 @@ namespace EightBit { private: Bus& m_bus; - register16_t af = { { 0xff, 0xff } }; - register16_t bc = { { 0xff, 0xff } }; - register16_t de = { { 0xff, 0xff } }; - register16_t hl = { { 0xff, 0xff } }; + register16_t af = 0xffff; + register16_t bc = 0xffff; + register16_t de = 0xffff; + register16_t hl = 0xffff; bool m_ime = false; bool m_stopped = false; diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index d6eabe4..c0c5bc1 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -120,8 +120,7 @@ void EightBit::GameBoy::LR35902::add(uint8_t& f, register16_t& operand, register void EightBit::GameBoy::LR35902::add(uint8_t& f, uint8_t& operand, uint8_t value, int carry) { - register16_t result; - result.word = operand + value + carry; + const register16_t result = operand + value + carry; adjustHalfCarryAdd(f, operand, value, result.low); @@ -138,8 +137,7 @@ void EightBit::GameBoy::LR35902::adc(uint8_t& f, uint8_t& operand, uint8_t value void EightBit::GameBoy::LR35902::subtract(uint8_t& f, uint8_t& operand, uint8_t value, int carry) { - register16_t result; - result.word = operand - value - carry; + const register16_t result = operand - value - carry; adjustHalfCarrySub(f, operand, value, result.low); diff --git a/M6502/inc/mos6502.h b/M6502/inc/mos6502.h index 8f1e2f3..5589cab 100644 --- a/M6502/inc/mos6502.h +++ b/M6502/inc/mos6502.h @@ -336,6 +336,6 @@ namespace EightBit { PinLevel m_soLine = Low; - register16_t m_intermediate = { { 0, 0 } }; + register16_t m_intermediate; }; } \ No newline at end of file diff --git a/M6502/src/mos6502.cpp b/M6502/src/mos6502.cpp index 0a15a21..546659e 100644 --- a/M6502/src/mos6502.cpp +++ b/M6502/src/mos6502.cpp @@ -51,11 +51,10 @@ void EightBit::MOS6502::reset() { } EightBit::register16_t EightBit::MOS6502::getWordPaged(uint8_t page, uint8_t offset) { - EightBit::register16_t returned; - returned.low = getBytePaged(page, offset); + const auto low = getBytePaged(page, offset); ++BUS().ADDRESS().low; - returned.high = BUS().read(); - return returned; + const auto high = BUS().read(); + return register16_t(low, high); } uint8_t EightBit::MOS6502::getBytePaged(uint8_t page, uint8_t offset) { @@ -442,8 +441,7 @@ uint8_t EightBit::MOS6502::SUB_d(const uint8_t operand, const uint8_t data, cons } void EightBit::MOS6502::CMP(uint8_t first, uint8_t second) { - register16_t result; - result.word = first - second; + const register16_t result = first - second; adjustNZ(result.low); clearFlag(P(), CF, result.high); } diff --git a/Z80/inc/Z80.h b/Z80/inc/Z80.h index 2deda33..628f928 100644 --- a/Z80/inc/Z80.h +++ b/Z80/inc/Z80.h @@ -114,8 +114,8 @@ namespace EightBit { std::array m_accumulatorFlags; int m_accumulatorFlagsSet = 0; - register16_t m_ix = { { 0xff, 0xff } }; - register16_t m_iy = { { 0xff, 0xff } }; + register16_t m_ix = 0xffff; + register16_t m_iy = 0xffff; refresh_t m_refresh = 0x7f; @@ -421,7 +421,7 @@ namespace EightBit { void ccf(); void cpl(); - void xhtl(register16_t& operand); + void xhtl(); void blockCompare(); diff --git a/Z80/src/Z80.cpp b/Z80/src/Z80.cpp index 801eda0..2cdbb09 100644 --- a/Z80/src/Z80.cpp +++ b/Z80/src/Z80.cpp @@ -255,8 +255,7 @@ void EightBit::Z80::add(const register16_t value) { void EightBit::Z80::add(const uint8_t value, const int carry) { - register16_t result; - result.word = A() + value + carry; + const register16_t result = A() + value + carry; adjustHalfCarryAdd(F(), A(), value, result.low); adjustOverflowAdd(F(), A(), value, result.low); @@ -274,8 +273,7 @@ void EightBit::Z80::adc(const uint8_t value) { void EightBit::Z80::subtract(uint8_t& operand, const uint8_t value, const int carry) { - register16_t result; - result.word = operand - value - carry; + const register16_t result = operand - value - carry; adjustHalfCarrySub(F(), operand, value, result.low); adjustOverflowSub(F(), operand, value, result.low); @@ -463,14 +461,14 @@ void EightBit::Z80::ccf() { adjustXY(F(), A()); } -void EightBit::Z80::xhtl(register16_t& operand) { +void EightBit::Z80::xhtl() { MEMPTR().low = BUS().read(SP()); - BUS().write(operand.low); - operand.low = MEMPTR().low; + BUS().write(HL2().low); + HL2().low = MEMPTR().low; ++BUS().ADDRESS().word; MEMPTR().high = BUS().read(); - BUS().write(operand.high); - operand.high = MEMPTR().high; + BUS().write(HL2().high); + HL2().high = MEMPTR().high; } void EightBit::Z80::blockCompare() { @@ -1420,7 +1418,7 @@ void EightBit::Z80::executeOther(const int x, const int y, const int z, const in addCycles(11); break; case 4: // EX (SP),HL - xhtl(HL2()); + xhtl(); addCycles(19); break; case 5: // EX DE,HL diff --git a/Z80/test/Configuration.h b/Z80/test/Configuration.h index d5d2717..213fc69 100644 --- a/Z80/test/Configuration.h +++ b/Z80/test/Configuration.h @@ -29,9 +29,9 @@ public: } EightBit::register16_t getStartAddress() const { - EightBit::register16_t returned; - returned.word = 0x100; - return returned; + //EightBit::register16_t returned; + //returned.word = 0x100; + return 0x100; } private: diff --git a/inc/Bus.h b/inc/Bus.h index efdecad..c611bb3 100644 --- a/inc/Bus.h +++ b/inc/Bus.h @@ -30,13 +30,17 @@ namespace EightBit { uint16_t peekWord(uint16_t address) const; uint8_t read(); - uint8_t read(uint16_t offset); - uint8_t read(register16_t address); + template uint8_t read(const T address) { + ADDRESS() = address; + return read(); + } void write(); void write(uint8_t value); - void write(uint16_t offset, uint8_t value); - void write(register16_t address, uint8_t value); + template void write(const T offset, const uint8_t value) { + ADDRESS() = offset; + write(value); + } protected: virtual uint8_t& reference(uint16_t address) = 0; @@ -47,6 +51,6 @@ namespace EightBit { private: uint8_t m_data = 0xff; - register16_t m_address{ { 0xff, 0xff } }; + register16_t m_address = 0xffff; }; } diff --git a/inc/IntelProcessor.h b/inc/IntelProcessor.h index ad1c45e..7b10a65 100644 --- a/inc/IntelProcessor.h +++ b/inc/IntelProcessor.h @@ -183,7 +183,7 @@ namespace EightBit { private: std::array m_decodedOpcodes; - register16_t m_sp = { { 0xff, 0xff } }; - register16_t m_memptr = { { 0, 0 } }; + register16_t m_sp = 0xffff; + register16_t m_memptr; }; } diff --git a/inc/Processor.h b/inc/Processor.h index dbdfb29..5d3e3ee 100644 --- a/inc/Processor.h +++ b/inc/Processor.h @@ -124,10 +124,9 @@ namespace EightBit { } register16_t fetchWord() { - register16_t returned; - returned.low = fetchByte(); - returned.high = fetchByte(); - return returned; + const auto low = fetchByte(); + const auto high = fetchByte(); + return register16_t(low, high); } virtual void push(uint8_t value) = 0; @@ -139,10 +138,9 @@ namespace EightBit { } register16_t popWord() { - register16_t returned; - returned.low = pop(); - returned.high = pop(); - return returned; + const auto low = pop(); + const auto high = pop(); + return register16_t(low, high); } void jump(const register16_t destination) { @@ -166,7 +164,7 @@ namespace EightBit { private: Bus& m_bus; int m_cycles = 0; - register16_t m_pc = { { 0, 0 } }; + register16_t m_pc; PinLevel m_intLine = Low; PinLevel m_nmiLine = Low; diff --git a/inc/Register.h b/inc/Register.h index 017ee17..13184d3 100644 --- a/inc/Register.h +++ b/inc/Register.h @@ -18,7 +18,7 @@ #endif namespace EightBit { - typedef union { + union register16_t { struct { #ifdef HOST_LITTLE_ENDIAN uint8_t low; @@ -30,5 +30,8 @@ namespace EightBit { #endif }; uint16_t word; - } register16_t; + register16_t() : word(0) {} + register16_t(uint16_t w) : word(w) {} + register16_t(uint8_t l, uint8_t h) : low(l), high(h) {} + } ; } diff --git a/src/Bus.cpp b/src/Bus.cpp index 0ebc0fd..c6e8f9e 100644 --- a/src/Bus.cpp +++ b/src/Bus.cpp @@ -18,10 +18,9 @@ void EightBit::Bus::poke(const uint16_t address, const uint8_t value) { } uint16_t EightBit::Bus::peekWord(const uint16_t address) const { - register16_t returned; - returned.low = peek(address); - returned.high = peek(address + 1); - return returned.word; + const auto low = peek(address); + const auto high = peek(address + 1); + return register16_t(low, high).word; } uint8_t EightBit::Bus::read() { @@ -31,16 +30,6 @@ uint8_t EightBit::Bus::read() { return DATA(); } -uint8_t EightBit::Bus::read(const uint16_t offset) { - ADDRESS().word = offset; - return read(); -} - -uint8_t EightBit::Bus::read(const register16_t address) { - ADDRESS() = address; - return read(); -} - void EightBit::Bus::write() { WritingByte.fire(EventArgs::empty()); reference() = DATA(); @@ -52,16 +41,6 @@ void EightBit::Bus::write(const uint8_t value) { write(); } -void EightBit::Bus::write(const uint16_t offset, const uint8_t value) { - ADDRESS().word = offset; - write(value); -} - -void EightBit::Bus::write(const register16_t address, const uint8_t value) { - ADDRESS() = address; - write(value); -} - uint8_t EightBit::Bus::reference() const { return reference(ADDRESS().word); } diff --git a/src/IntelProcessor.cpp b/src/IntelProcessor.cpp index 778dde0..caba993 100644 --- a/src/IntelProcessor.cpp +++ b/src/IntelProcessor.cpp @@ -21,10 +21,11 @@ uint8_t EightBit::IntelProcessor::pop() { } EightBit::register16_t EightBit::IntelProcessor::getWord() { - register16_t returned; - returned.low = BUS().read(MEMPTR().word++); - returned.high = BUS().read(++BUS().ADDRESS().word); - return returned; + BUS().ADDRESS().word = MEMPTR().word++; + const auto low = BUS().read(); + ++BUS().ADDRESS().word; + const auto high = BUS().read(); + return register16_t(low, high); } void EightBit::IntelProcessor::setWord(const register16_t value) {