GameBoy: Correct a few (very minor C++) niggles in the implementation. No functional changes.

Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2020-11-07 09:41:12 +00:00
parent 33dd479421
commit 759b4a9fa8
25 changed files with 134 additions and 97 deletions

View File

@ -1,7 +1,8 @@
#pragma once
#include <vector>
#include <array>
#include <cstdint>
#include <cassert>
namespace EightBit {
namespace GameBoy {
@ -14,16 +15,15 @@ namespace EightBit {
Dark
};
AbstractColourPalette() noexcept
: m_colours(4) {
}
AbstractColourPalette() = default;
auto getColour(size_t index) const {
[[nodiscard]] auto colour(size_t index) const noexcept {
assert(index < m_colours.size());
return m_colours[index];
}
protected:
std::vector<uint32_t> m_colours;
std::array<uint32_t, 4> m_colours;
};
}
}

View File

@ -12,7 +12,7 @@ namespace EightBit {
public:
CharacterDefinition(Ram& vram, uint16_t address);
std::array<int, 8> get(int row);
[[nodiscard]] std::array<int, 8> get(int row) const;
private:
Ram& m_vram;

View File

@ -20,7 +20,7 @@ namespace EightBit {
static std::string state(LR35902& cpu);
std::string disassemble(LR35902& cpu);
static std::string flag(uint8_t value, int flag, const std::string& represents);
static std::string flag(uint8_t value, int flag, std::string represents);
static std::string flags(uint8_t value);
static std::string hex(uint8_t value);
static std::string hex(uint16_t value);
@ -70,7 +70,7 @@ namespace EightBit {
static std::string cc(int flag);
static std::string alu(int which);
Bus& BUS() { return m_bus; }
Bus& BUS() noexcept { return m_bus; }
};
}
}

View File

@ -29,9 +29,9 @@ namespace EightBit {
Display(const AbstractColourPalette* colours, Bus& bus, Ram& oam, Ram& vram);
const std::array<uint32_t, PixelCount>& pixels() const;
[[nodiscard]] const auto& pixels() const noexcept { return m_pixels; }
void render();
void renderCurrentScanline();
void loadObjectAttributes();
private:
@ -48,7 +48,7 @@ namespace EightBit {
uint8_t m_control = 0;
uint8_t m_scanLine = 0;
std::array<int, 4> createPalette(int address);
[[nodiscard]] std::array<int, 4> createPalette(int address);
void renderBackground();
void renderBackground(
@ -59,12 +59,24 @@ namespace EightBit {
void renderObjects();
void renderSpriteTile(
int height,
int drawX, int drawY,
bool flipX, bool flipY,
const std::array<int, 4>& palette,
const CharacterDefinition& definition);
void renderBackgroundTile(
int drawX, int drawY,
const std::array<int, 4>& palette,
const CharacterDefinition& definition);
void renderTile(
int height,
int drawX, int drawY,
bool flipX, bool flipY, bool allowTransparencies,
const std::array<int, 4>& palette,
CharacterDefinition& definition);
const CharacterDefinition& definition);
};
}
}

View File

@ -39,21 +39,21 @@ namespace EightBit {
virtual void raisePOWER() override;
virtual void lowerPOWER() override;
auto& CPU() { return m_cpu; }
auto& VRAM() { return m_videoRam; }
auto& OAMRAM() { return m_oamRam; }
auto& IO() { return m_ioPorts; }
[[nodiscard]] auto& CPU() noexcept { return m_cpu; }
[[nodiscard]] auto& VRAM() noexcept { return m_videoRam; }
[[nodiscard]] auto& OAMRAM() noexcept { return m_oamRam; }
[[nodiscard]] auto& IO() noexcept { return m_ioPorts; }
void reset();
void disableGameRom() { m_disableGameRom = true; }
void enableGameRom() { m_disableGameRom = false; }
void disableGameRom() noexcept { m_disableGameRom = true; }
void enableGameRom() noexcept { m_disableGameRom = false; }
bool gameRomDisabled() const { return m_disableGameRom; }
bool gameRomEnabled() const { return !gameRomDisabled(); }
[[nodiscard]] bool gameRomDisabled() const noexcept { return m_disableGameRom; }
[[nodiscard]] bool gameRomEnabled() const noexcept { return !gameRomDisabled(); }
void loadBootRom(const std::string& path);
void loadGameRom(const std::string& path);
void loadBootRom(std::string path);
void loadGameRom(std::string path);
void runRasterLines();
void runVerticalBlankLines();

View File

@ -133,35 +133,35 @@ namespace EightBit {
void updateLcdStatusMode(int mode);
void disableBootRom() { m_disableBootRom = true; }
void enableBootRom() { m_disableBootRom = false; }
void disableBootRom() noexcept { m_disableBootRom = true; }
void enableBootRom() noexcept { m_disableBootRom = false; }
auto bootRomDisabled() const { return m_disableBootRom; }
auto bootRomEnabled() const { return !bootRomDisabled(); }
auto bootRomDisabled() const noexcept { return m_disableBootRom; }
auto bootRomEnabled() const noexcept { return !bootRomDisabled(); }
void pressRight() { m_p14 = m_p10 = false; triggerKeypadInterrupt(); }
void releaseRight() { m_p14 = m_p10 = true; }
void releaseRight() noexcept { m_p14 = m_p10 = true; }
void pressLeft() { m_p14 = m_p11 = false, triggerKeypadInterrupt(); }
void releaseLeft() { m_p14 = m_p11 = true; }
void releaseLeft() noexcept { m_p14 = m_p11 = true; }
void pressUp() { m_p14 = m_p12 = false, triggerKeypadInterrupt(); }
void releaseUp() { m_p14 = m_p12 = true; }
void releaseUp() noexcept { m_p14 = m_p12 = true; }
void pressDown() { m_p14 = m_p13 = false, triggerKeypadInterrupt(); }
void releaseDown() { m_p14 = m_p13 = true; }
void releaseDown() noexcept { m_p14 = m_p13 = true; }
void pressA() { m_p15 = m_p10 = false, triggerKeypadInterrupt(); }
void releaseA() { m_p15 = m_p10 = true; }
void releaseA() noexcept { m_p15 = m_p10 = true; }
void pressB() { m_p15 = m_p11 = false, triggerKeypadInterrupt(); }
void releaseB() { m_p15 = m_p11 = true; }
void releaseB() noexcept { m_p15 = m_p11 = true; }
void pressSelect() { m_p15 = m_p12 = false, triggerKeypadInterrupt(); }
void releaseSelect() { m_p15 = m_p12 = true; }
void releaseSelect() noexcept { m_p15 = m_p12 = true; }
void pressStart() { m_p15 = m_p13 = false, triggerKeypadInterrupt(); }
void releaseStart() { m_p15 = m_p13 = true; }
void releaseStart() noexcept { m_p15 = m_p13 = true; }
private:
Bus& m_bus;

View File

@ -26,7 +26,7 @@ namespace EightBit {
Signal<LR35902> ExecutingInstruction;
Signal<LR35902> ExecutedInstruction;
auto clockCycles() const {
[[nodiscard]] auto clockCycles() const noexcept {
return cycles() * 4;
}
@ -35,6 +35,8 @@ namespace EightBit {
virtual register16_t& DE() final;
virtual register16_t& HL() final;
uint8_t maskedInterrupts();
protected:
virtual int execute() final;
virtual int step() final;
@ -55,7 +57,7 @@ namespace EightBit {
bool m_prefixCB = false;
bool& IME() { return m_ime; }
bool& IME() noexcept { return m_ime; }
auto R(const int r) {
ASSUME(r >= 0);
@ -165,9 +167,9 @@ namespace EightBit {
void increment(uint8_t& operand);
void decrement(uint8_t& operand);
void stop() { m_stopped = true; }
void start() { m_stopped = false; }
bool stopped() const { return m_stopped; }
void stop(bool value = true) noexcept { m_stopped = value; }
void start() noexcept { stop(false); }
bool stopped() const noexcept { return m_stopped; }
void di();
void ei();

View File

@ -2,7 +2,7 @@
#include <cstdint>
#include <Processor.h>
#include <Chip.h>
namespace EightBit {
@ -14,18 +14,18 @@ namespace EightBit {
ObjectAttribute() = default;
ObjectAttribute(Ram& ram, uint16_t address);
auto positionY() const { return m_positionY; }
auto positionX() const { return m_positionX; }
auto pattern() const { return m_pattern; }
auto flags() const { return m_flags; }
[[nodiscard]] auto positionY() const noexcept { return m_positionY; }
[[nodiscard]] auto positionX() const noexcept { return m_positionX; }
[[nodiscard]] auto pattern() const noexcept { return m_pattern; }
[[nodiscard]] auto flags() const noexcept { return m_flags; }
auto priority() const { return flags() & Chip::Bit7; }
[[nodiscard]] auto priority() const noexcept { return flags() & Chip::Bit7; }
auto highPriority() const { return !!priority(); }
auto lowPriority() const { return !priority(); }
auto flipY() const { return !!(flags() & Chip::Bit6); }
auto flipX() const { return !!(flags() & Chip::Bit5); }
auto palette() const { return (flags() & Chip::Bit4) >> 4; }
[[nodiscard]] auto highPriority() const noexcept { return !!priority(); }
[[nodiscard]] auto lowPriority() const noexcept { return !priority(); }
[[nodiscard]] auto flipY() const noexcept { return !!(flags() & Chip::Bit6); }
[[nodiscard]] auto flipX() const noexcept { return !!(flags() & Chip::Bit5); }
[[nodiscard]] auto palette() const noexcept { return (flags() & Chip::Bit4) >> 4; }
private:
uint8_t m_positionY;

View File

@ -8,7 +8,7 @@ EightBit::GameBoy::CharacterDefinition::CharacterDefinition(Ram& vram, const uin
m_address(address) {
}
std::array<int, 8> EightBit::GameBoy::CharacterDefinition::get(int row) {
std::array<int, 8> EightBit::GameBoy::CharacterDefinition::get(int row) const {
std::array<int, 8> returned;
@ -26,7 +26,8 @@ std::array<int, 8> EightBit::GameBoy::CharacterDefinition::get(int row) {
const auto colour = bitHigh | bitLow;
returned[7 - bit] = colour;
const auto index = 7 - bit;
returned[index] = colour;
}
return returned;

View File

@ -525,7 +525,7 @@ void EightBit::GameBoy::Disassembler::disassembleOther(
}
}
std::string EightBit::GameBoy::Disassembler::flag(uint8_t value, int flag, const std::string& represents) {
std::string EightBit::GameBoy::Disassembler::flag(uint8_t value, int flag, const std::string represents) {
std::ostringstream output;
output << (value & flag ? represents : "-");
return output.str();

View File

@ -14,20 +14,15 @@ EightBit::GameBoy::Display::Display(const AbstractColourPalette* colours, Bus& b
m_colours(colours) {
}
const std::array<uint32_t, EightBit::GameBoy::Display::PixelCount>& EightBit::GameBoy::Display::pixels() const {
return m_pixels;
}
void EightBit::GameBoy::Display::render() {
void EightBit::GameBoy::Display::renderCurrentScanline() {
m_scanLine = m_bus.IO().peek(IoRegisters::LY);
if (m_scanLine < RasterHeight) {
m_control = m_bus.IO().peek(IoRegisters::LCDC);
if (m_control & IoRegisters::LCD_EN) {
if (m_control & IoRegisters::BG_EN)
renderBackground();
if (m_control & IoRegisters::OBJ_EN)
renderObjects();
}
assert(m_control & IoRegisters::LCD_EN);
if (m_control & IoRegisters::BG_EN)
renderBackground();
if (m_control & IoRegisters::OBJ_EN)
renderObjects();
}
}
@ -71,15 +66,15 @@ void EightBit::GameBoy::Display::renderObjects() {
const auto drawX = spriteX - 8;
const auto sprite = current.pattern();
auto definition = CharacterDefinition(m_vram, characterAddressMultiplier * sprite);
const auto definition = CharacterDefinition(m_vram, characterAddressMultiplier * sprite);
const auto& palette = palettes[current.palette()];
const auto flipX = current.flipX();
const auto flipY = current.flipY();
renderTile(
renderSpriteTile(
objBlockHeight,
drawX, drawY,
flipX, flipY, true,
flipX, flipY,
palette,
definition);
}
@ -120,22 +115,46 @@ void EightBit::GameBoy::Display::renderBackground(
const auto character = m_vram.peek(address++);
const auto definitionOffset = offsetType == tile_offset_t::Signed ? 16 * (int8_t)character : 16 * character;
auto definition = CharacterDefinition(m_vram, bgCharacters + definitionOffset);
renderTile(
8,
const auto definition = CharacterDefinition(m_vram, bgCharacters + definitionOffset);
renderBackgroundTile(
column * 8 + offsetX, row * 8 + offsetY,
false, false, false,
palette,
definition);
}
}
void EightBit::GameBoy::Display::renderSpriteTile(
const int height,
const int drawX, const int drawY,
const bool flipX, const bool flipY,
const std::array<int, 4>& palette,
const CharacterDefinition& definition) {
renderTile(
height,
drawX, drawY,
flipX, flipY, true,
palette,
definition);
}
void EightBit::GameBoy::Display::renderBackgroundTile(
const int drawX, const int drawY,
const std::array<int, 4>& palette,
const CharacterDefinition& definition) {
renderTile(
8,
drawX, drawY,
false, false, false,
palette,
definition);
}
void EightBit::GameBoy::Display::renderTile(
const int height,
const int drawX, const int drawY,
const bool flipX, const bool flipY, const bool allowTransparencies,
const std::array<int, 4>& palette,
CharacterDefinition& definition) {
const CharacterDefinition& definition) {
const auto width = 8;
@ -160,7 +179,7 @@ void EightBit::GameBoy::Display::renderTile(
const auto colour = rowDefinition[cx];
if (!allowTransparencies || (allowTransparencies && (colour > 0))) {
const auto outputPixel = lineAddress + x;
m_pixels[outputPixel] = m_colours->getColour(palette[colour]);
m_pixels[outputPixel] = m_colours->colour(palette[colour]);
}
}
}

View File

@ -25,11 +25,11 @@ void EightBit::GameBoy::Bus::reset() {
CPU().lowerRESET();
}
void EightBit::GameBoy::Bus::loadBootRom(const std::string& path) {
void EightBit::GameBoy::Bus::loadBootRom(const std::string path) {
m_bootRom.load(path);
}
void EightBit::GameBoy::Bus::loadGameRom(const std::string& path) {
void EightBit::GameBoy::Bus::loadGameRom(const std::string path) {
const auto bankSize = 0x4000;
m_gameRomBanks.resize(1);
const auto size = m_gameRomBanks[0].load(path, 0, 0, bankSize);

View File

@ -297,6 +297,12 @@ void EightBit::GameBoy::LR35902::ccf() {
F() = clearBit(F(), CF, F() & CF);
}
uint8_t EightBit::GameBoy::LR35902::maskedInterrupts() {
const auto interruptEnable = BUS().peek(IoRegisters::BASE + IoRegisters::IE);
const auto interruptFlags = m_bus.IO().peek(IoRegisters::IF);
return interruptEnable & interruptFlags;
}
int EightBit::GameBoy::LR35902::step() {
ExecutingInstruction.fire(*this);
@ -304,10 +310,7 @@ int EightBit::GameBoy::LR35902::step() {
resetCycles();
if (LIKELY(powered())) {
const auto interruptEnable = BUS().peek(IoRegisters::BASE + IoRegisters::IE);
const auto interruptFlags = m_bus.IO().peek(IoRegisters::IF);
const auto masked = interruptEnable & interruptFlags;
const auto masked = maskedInterrupts();
if (masked) {
if (IME()) {
m_bus.IO().poke(IoRegisters::IF, 0);
@ -331,7 +334,6 @@ int EightBit::GameBoy::LR35902::step() {
m_bus.IO().checkTimers(clockCycles());
m_bus.IO().transferDma();
}
ExecutedInstruction.fire(*this);
return clockCycles();

View File

@ -2,6 +2,7 @@
#pragma once
#endif
#include <cassert>
#include <cstdint>
#include <stdexcept>

View File

@ -10,7 +10,7 @@ namespace EightBit {
public:
virtual ~BigEndianProcessor() = default;
register16_t peekWord(register16_t address) final;
[[nodiscard]] register16_t peekWord(register16_t address) final;
void pokeWord(register16_t address, register16_t value) final;
protected:

View File

@ -89,9 +89,9 @@ namespace EightBit {
Low, High
};
static constexpr auto raised(const PinLevel line) { return line == PinLevel::High; }
[[nodiscard]] static constexpr auto raised(const PinLevel line) { return line == PinLevel::High; }
static void raise(PinLevel& line) noexcept { line = PinLevel::High; }
static constexpr auto lowered(const PinLevel line) { return line == PinLevel::Low; }
[[nodiscard]] static constexpr auto lowered(const PinLevel line) { return line == PinLevel::Low; }
static void lower(PinLevel& line) noexcept { line = PinLevel::Low; }
static void match(PinLevel& line, int condition) noexcept { match(line, condition != 0); }

View File

@ -17,10 +17,10 @@ namespace EightBit {
[[nodiscard]] uint8_t& reference(uint16_t address) override;
int load(std::ifstream& file, int writeOffset = 0, int readOffset = 0, int limit = -1) override;
int load(const std::string& path, int writeOffset = 0, int readOffset = 0, int limit = -1) override;
int load(std::string path, int writeOffset = 0, int readOffset = 0, int limit = -1) override;
int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) override;
AccessType getAccessType() const noexcept { return m_access; }
[[nodiscard]] AccessType getAccessType() const noexcept { return m_access; }
void setAccessType(AccessType value) noexcept { m_access = value; }
auto readPort(uint8_t port, AccessType access) {

View File

@ -10,7 +10,7 @@ namespace EightBit {
public:
~LittleEndianProcessor() = default;
register16_t peekWord(register16_t address) final;
[[nodiscard]] register16_t peekWord(register16_t address) final;
void pokeWord(register16_t address, register16_t value) final;
protected:

View File

@ -22,7 +22,7 @@ namespace EightBit {
[[nodiscard]] virtual uint8_t& reference(uint16_t);
virtual int load(std::ifstream& file, int writeOffset = 0, int readOffset = 0, int limit = -1) = 0;
virtual int load(const std::string& path, int writeOffset = 0, int readOffset = 0, int limit = -1) = 0;
virtual int load(std::string path, int writeOffset = 0, int readOffset = 0, int limit = -1) = 0;
virtual int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) = 0;
protected:

View File

@ -23,14 +23,14 @@ namespace EightBit {
public:
static int load(std::ifstream& file, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
static int load(const std::string& path, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
static int load(std::string path, std::vector<uint8_t>& output, int writeOffset = 0, int readOffset = 0, int limit = -1, int maximumSize = -1);
Rom(size_t size = 0);
[[nodiscard]] size_t size() const final;
int load(std::ifstream& file, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(const std::string& path, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(std::string path, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
[[nodiscard]] uint8_t peek(uint16_t address) const final;

View File

@ -20,7 +20,7 @@ namespace EightBit {
[[nodiscard]] uint8_t peek(uint16_t address) const final;
int load(std::ifstream& file, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(const std::string& path, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(std::string path, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
int load(const std::vector<uint8_t>& bytes, int writeOffset = 0, int readOffset = 0, int limit = -1) final;
protected:

View File

@ -30,7 +30,7 @@ int EightBit::InputOutput::load(std::ifstream&, int, int, int) {
throw std::logic_error("load operation not allowed.");
}
int EightBit::InputOutput::load(const std::string&, int, int, int) {
int EightBit::InputOutput::load(std::string, int, int, int) {
throw std::logic_error("load operation not allowed.");
}

View File

@ -6,8 +6,8 @@ EightBit::IntelProcessor::IntelProcessor(Bus& bus)
for (int i = 0; i < 0x100; ++i)
m_decodedOpcodes.at(i) = i;
LoweredHALT.connect([this](EventArgs) { --PC(); });
RaisedHALT.connect([this](EventArgs) { ++PC(); });
LoweredHALT.connect([this](EventArgs) noexcept { --PC(); });
RaisedHALT.connect([this](EventArgs) noexcept { ++PC(); });
RaisedPOWER.connect([this](EventArgs) {
PC() = SP() = AF() = BC() = DE() = HL() = Mask16;

View File

@ -25,7 +25,7 @@ int EightBit::Rom::load(std::ifstream& file, std::vector<uint8_t>& output, const
return size;
}
int EightBit::Rom::load(const std::string& path, std::vector<uint8_t>& output, const int writeOffset, const int readOffset, const int limit, const int maximumSize) {
int EightBit::Rom::load(const std::string path, std::vector<uint8_t>& output, const int writeOffset, const int readOffset, const int limit, const int maximumSize) {
std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
@ -53,7 +53,7 @@ int EightBit::Rom::load(std::ifstream& file, const int writeOffset, const int re
return load(file, m_bytes, writeOffset, readOffset, limit, maximumSize);
}
int EightBit::Rom::load(const std::string& path, const int writeOffset, const int readOffset, const int limit) {
int EightBit::Rom::load(const std::string path, const int writeOffset, const int readOffset, const int limit) {
const auto maximumSize = size() - writeOffset;
return load(path, m_bytes, writeOffset, readOffset, limit, maximumSize);
}

View File

@ -16,7 +16,7 @@ int EightBit::UnusedMemory::load(std::ifstream&, int, int, int) {
throw std::logic_error("load operation not allowed.");
}
int EightBit::UnusedMemory::load(const std::string&, int, int, int) {
int EightBit::UnusedMemory::load(std::string, int, int, int) {
throw std::logic_error("load operation not allowed.");
}