From 0b2c1fa08400b6574212550b239a46d90f3408ac Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Wed, 2 May 2018 02:47:47 +0100 Subject: [PATCH] Gameboy, some random tidy ups. Signed-off-by: Adrian Conlon --- LR35902/inc/CharacterDefinition.h | 7 +++---- LR35902/inc/Display.h | 2 +- LR35902/inc/IoRegisters.h | 2 +- LR35902/inc/LR35902.h | 12 ++++++------ LR35902/inc/ObjectAttribute.h | 2 +- LR35902/src/CharacterDefinition.cpp | 8 ++++---- LR35902/src/Display.cpp | 22 ++++++++++++---------- LR35902/src/LR35902.cpp | 16 ++++++++-------- inc/EightBitCompilerDefinitions.h | 24 ++++++++++++++---------- 9 files changed, 50 insertions(+), 45 deletions(-) diff --git a/LR35902/inc/CharacterDefinition.h b/LR35902/inc/CharacterDefinition.h index 1580c74..81751fe 100644 --- a/LR35902/inc/CharacterDefinition.h +++ b/LR35902/inc/CharacterDefinition.h @@ -8,15 +8,14 @@ namespace EightBit { class Ram; namespace GameBoy { - class CharacterDefinition { + class CharacterDefinition final { public: - CharacterDefinition() = default; - CharacterDefinition(Ram* ram, uint16_t address); + CharacterDefinition(const Ram& vram, uint16_t address); std::array get(int row) const; private: - Ram* m_ram = nullptr; + const Ram& m_vram; uint16_t m_address = ~0; }; } diff --git a/LR35902/inc/Display.h b/LR35902/inc/Display.h index 03966bc..fe99cf9 100644 --- a/LR35902/inc/Display.h +++ b/LR35902/inc/Display.h @@ -16,7 +16,7 @@ namespace EightBit { class CharacterDefinition; class Bus; - class Display { + class Display final { public: enum { BufferWidth = 256, diff --git a/LR35902/inc/IoRegisters.h b/LR35902/inc/IoRegisters.h index 9123dab..8cb4cd9 100644 --- a/LR35902/inc/IoRegisters.h +++ b/LR35902/inc/IoRegisters.h @@ -12,7 +12,7 @@ namespace EightBit { class Bus; - class IoRegisters : public EightBit::Ram { + class IoRegisters final : public EightBit::Ram { public: enum { diff --git a/LR35902/inc/LR35902.h b/LR35902/inc/LR35902.h index d492ff2..9a70ab6 100644 --- a/LR35902/inc/LR35902.h +++ b/LR35902/inc/LR35902.h @@ -175,10 +175,10 @@ namespace EightBit { void reti(); - bool jrConditionalFlag(uint8_t& f, int flag); - bool returnConditionalFlag(uint8_t& f, int flag); - bool jumpConditionalFlag(uint8_t& f, int flag); - bool callConditionalFlag(uint8_t& f, int flag); + bool jrConditionalFlag(uint8_t f, int flag); + bool returnConditionalFlag(uint8_t f, int flag); + bool jumpConditionalFlag(uint8_t f, int flag); + bool callConditionalFlag(uint8_t f, int flag); void add(uint8_t& f, register16_t& operand, register16_t value); @@ -204,8 +204,8 @@ namespace EightBit { static void daa(uint8_t& a, uint8_t& f); - static void scf(uint8_t& a, uint8_t& f); - static void ccf(uint8_t& a, uint8_t& f); + static void scf(uint8_t& f); + static void ccf(uint8_t& f); static void cpl(uint8_t& a, uint8_t& f); static uint8_t swap(uint8_t& f, uint8_t operand); diff --git a/LR35902/inc/ObjectAttribute.h b/LR35902/inc/ObjectAttribute.h index e6d5529..fd621ff 100644 --- a/LR35902/inc/ObjectAttribute.h +++ b/LR35902/inc/ObjectAttribute.h @@ -9,7 +9,7 @@ namespace EightBit { class Ram; namespace GameBoy { - class ObjectAttribute { + class ObjectAttribute final { public: ObjectAttribute() = default; ObjectAttribute(Ram& ram, uint16_t address); diff --git a/LR35902/src/CharacterDefinition.cpp b/LR35902/src/CharacterDefinition.cpp index cf885a5..7204335 100644 --- a/LR35902/src/CharacterDefinition.cpp +++ b/LR35902/src/CharacterDefinition.cpp @@ -3,8 +3,8 @@ #include -EightBit::GameBoy::CharacterDefinition::CharacterDefinition(Ram* ram, uint16_t address) -: m_ram(ram), +EightBit::GameBoy::CharacterDefinition::CharacterDefinition(const Ram& vram, const uint16_t address) +: m_vram(vram), m_address(address) { } @@ -14,8 +14,8 @@ std::array EightBit::GameBoy::CharacterDefinition::get(int row) const { const auto planeAddress = m_address + row * 2; - const auto planeLow = m_ram->peek(planeAddress); - const auto planeHigh = m_ram->peek(planeAddress + 1); + const auto planeLow = m_vram.peek(planeAddress); + const auto planeHigh = m_vram.peek(planeAddress + 1); for (int bit = 0; bit < 8; ++bit) { diff --git a/LR35902/src/Display.cpp b/LR35902/src/Display.cpp index 7489a65..a2fa9a6 100644 --- a/LR35902/src/Display.cpp +++ b/LR35902/src/Display.cpp @@ -38,11 +38,12 @@ void EightBit::GameBoy::Display::render() { std::array EightBit::GameBoy::Display::createPalette(const int address) { const auto raw = m_bus.IO().peek(address); - std::array palette; - palette[0] = raw & 0b11; - palette[1] = (raw & 0b1100) >> 2; - palette[2] = (raw & 0b110000) >> 4; - palette[3] = (raw & 0b11000000) >> 6; + const std::array palette = { + raw & 0b11, + (raw & 0b1100) >> 2, + (raw & 0b110000) >> 4, + (raw & 0b11000000) >> 6, + }; return palette; } @@ -55,9 +56,10 @@ void EightBit::GameBoy::Display::renderObjects() { const auto objBlockHeight = (m_control & IoRegisters::ObjectBlockCompositionSelection) ? 16 : 8; - std::vector> palettes(2); - palettes[0] = createPalette(IoRegisters::OBP0); - palettes[1] = createPalette(IoRegisters::OBP1); + const std::array, 2> palettes = { + createPalette(IoRegisters::OBP0), + createPalette(IoRegisters::OBP1) + }; for (int i = 0; i < 40; ++i) { @@ -72,7 +74,7 @@ void EightBit::GameBoy::Display::renderObjects() { const auto drawX = spriteX - 8; const auto sprite = current.pattern(); - const auto definition = CharacterDefinition(&m_vram, (objBlockHeight == 8 ? 16 : 8) * sprite); + const auto definition = CharacterDefinition(m_vram, (objBlockHeight == 8 ? 16 : 8) * sprite); const auto& palette = palettes[current.palette()]; const auto flipX = current.flipX(); const auto flipY = current.flipY(); @@ -119,7 +121,7 @@ void EightBit::GameBoy::Display::renderBackground( const auto character = m_vram.peek(address++); - const auto definition = CharacterDefinition(&m_vram, bgCharacters + 16 * character); + const auto definition = CharacterDefinition(m_vram, bgCharacters + 16 * character); renderTile( 8, column * 8 + offsetX, row * 8 + offsetY, diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 1eb4971..87c60c6 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -36,7 +36,7 @@ void EightBit::GameBoy::LR35902::decrement(uint8_t& f, uint8_t& operand) { adjustZero(f, --operand); } -bool EightBit::GameBoy::LR35902::jrConditionalFlag(uint8_t& f, int flag) { +bool EightBit::GameBoy::LR35902::jrConditionalFlag(uint8_t f, int flag) { switch (flag) { case 0: // NZ return jrConditional(!(f & ZF)); @@ -52,7 +52,7 @@ bool EightBit::GameBoy::LR35902::jrConditionalFlag(uint8_t& f, int flag) { throw std::logic_error("Unhandled JR conditional"); } -bool EightBit::GameBoy::LR35902::jumpConditionalFlag(uint8_t& f, int flag) { +bool EightBit::GameBoy::LR35902::jumpConditionalFlag(uint8_t f, int flag) { switch (flag) { case 0: // NZ return jumpConditional(!(f & ZF)); @@ -73,7 +73,7 @@ void EightBit::GameBoy::LR35902::reti() { ei(); } -bool EightBit::GameBoy::LR35902::returnConditionalFlag(uint8_t& f, int flag) { +bool EightBit::GameBoy::LR35902::returnConditionalFlag(uint8_t f, int flag) { switch (flag) { case 0: // NZ return returnConditional(!(f & ZF)); @@ -89,7 +89,7 @@ bool EightBit::GameBoy::LR35902::returnConditionalFlag(uint8_t& f, int flag) { throw std::logic_error("Unhandled RET conditional"); } -bool EightBit::GameBoy::LR35902::callConditionalFlag(uint8_t& f, int flag) { +bool EightBit::GameBoy::LR35902::callConditionalFlag(uint8_t f, int flag) { switch (flag) { case 0: // NZ return callConditional(!(f & ZF)); @@ -269,12 +269,12 @@ void EightBit::GameBoy::LR35902::cpl(uint8_t& a, uint8_t& f) { a = ~a; } -void EightBit::GameBoy::LR35902::scf(uint8_t& a, uint8_t& f) { +void EightBit::GameBoy::LR35902::scf(uint8_t& f) { setFlag(f, CF); clearFlag(f, HC | NF); } -void EightBit::GameBoy::LR35902::ccf(uint8_t& a, uint8_t& f) { +void EightBit::GameBoy::LR35902::ccf(uint8_t& f) { clearFlag(f, NF | HC); clearFlag(f, CF, f & CF); } @@ -566,10 +566,10 @@ void EightBit::GameBoy::LR35902::executeOther(uint8_t& a, uint8_t& f, int x, int cpl(a, f); break; case 6: - scf(a, f); + scf(f); break; case 7: - ccf(a, f); + ccf(f); break; default: UNREACHABLE; diff --git a/inc/EightBitCompilerDefinitions.h b/inc/EightBitCompilerDefinitions.h index 8356cb4..8a04530 100644 --- a/inc/EightBitCompilerDefinitions.h +++ b/inc/EightBitCompilerDefinitions.h @@ -15,20 +15,24 @@ namespace EightBit { void assume(int expression); } -/* -Published in 1988, the C Programming Language 2nd Ed. -(by Brian W.Kernighan and Dennis M.Ritchie) mentions -this in exercise 2 - 9. On April 19, 2006 Don Knuth pointed -out to me that this method "was first published by Peter -Wegner in CACM 3 (1960), 322. -(Also discovered independently by Derrick Lehmer and published -in 1964 in a book edited by Beckenbach.)" -*/ inline int EightBit::countBits(uint8_t value) { +#ifdef _MSC_VER + return __popcnt(value); +#else + /* + Published in 1988, the C Programming Language 2nd Ed. + (by Brian W.Kernighan and Dennis M.Ritchie) mentions + this in exercise 2 - 9. On April 19, 2006 Don Knuth pointed + out to me that this method "was first published by Peter + Wegner in CACM 3 (1960), 322. + (Also discovered independently by Derrick Lehmer and published + in 1964 in a book edited by Beckenbach.)" + */ int count; // c accumulates the total bits set in value for (count = 0; value; ++count) value &= value - 1; // clear the least significant bit set return count; +#endif } inline bool EightBit::oddParity(uint8_t value) { @@ -66,7 +70,7 @@ inline void EightBit::assume(int expression) { # define LIKELY(x) (x) # define UNLIKELY(x) (x) -# define PARITY(x) (__popcnt(x) % 2) +# define PARITY(x) EightBit::oddParity(x); # define UNREACHABLE ASSUME(0)