Gameboy, some random tidy ups.

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2018-05-02 02:47:47 +01:00
parent 9de0f597f6
commit 0b2c1fa084
9 changed files with 50 additions and 45 deletions

View File

@ -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<int, 8> get(int row) const;
private:
Ram* m_ram = nullptr;
const Ram& m_vram;
uint16_t m_address = ~0;
};
}

View File

@ -16,7 +16,7 @@ namespace EightBit {
class CharacterDefinition;
class Bus;
class Display {
class Display final {
public:
enum {
BufferWidth = 256,

View File

@ -12,7 +12,7 @@ namespace EightBit {
class Bus;
class IoRegisters : public EightBit::Ram {
class IoRegisters final : public EightBit::Ram {
public:
enum {

View File

@ -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);

View File

@ -9,7 +9,7 @@ namespace EightBit {
class Ram;
namespace GameBoy {
class ObjectAttribute {
class ObjectAttribute final {
public:
ObjectAttribute() = default;
ObjectAttribute(Ram& ram, uint16_t address);

View File

@ -3,8 +3,8 @@
#include <Ram.h>
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<int, 8> 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) {

View File

@ -38,11 +38,12 @@ void EightBit::GameBoy::Display::render() {
std::array<int, 4> EightBit::GameBoy::Display::createPalette(const int address) {
const auto raw = m_bus.IO().peek(address);
std::array<int, 4> palette;
palette[0] = raw & 0b11;
palette[1] = (raw & 0b1100) >> 2;
palette[2] = (raw & 0b110000) >> 4;
palette[3] = (raw & 0b11000000) >> 6;
const std::array<int, 4> 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<std::array<int, 4>> palettes(2);
palettes[0] = createPalette(IoRegisters::OBP0);
palettes[1] = createPalette(IoRegisters::OBP1);
const std::array<std::array<int, 4>, 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,

View File

@ -36,7 +36,7 @@ void EightBit::GameBoy::LR35902::decrement(uint8_t& f, uint8_t& operand) {
adjustZero<LR35902>(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;

View File

@ -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)