From 5959349f341a2380c0c3f41ec40a1e8e0d969d00 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Wed, 29 Jan 2025 14:49:20 +0100 Subject: refactor: move emulation logic into sys --- lib/nes/util/CMakeLists.txt | 11 ------ lib/nes/util/address.hh | 73 ------------------------------------- lib/nes/util/button-mask.hh | 43 ---------------------- lib/nes/util/cycle-count.hh | 87 --------------------------------------------- lib/nes/util/debug.hh | 15 -------- lib/nes/util/display.hh | 44 ----------------------- lib/nes/util/rgb.hh | 33 ----------------- lib/nes/util/snapshot.hh | 63 -------------------------------- lib/nes/util/status.hh | 36 ------------------- 9 files changed, 405 deletions(-) delete mode 100644 lib/nes/util/CMakeLists.txt delete mode 100644 lib/nes/util/address.hh delete mode 100644 lib/nes/util/button-mask.hh delete mode 100644 lib/nes/util/cycle-count.hh delete mode 100644 lib/nes/util/debug.hh delete mode 100644 lib/nes/util/display.hh delete mode 100644 lib/nes/util/rgb.hh delete mode 100644 lib/nes/util/snapshot.hh delete mode 100644 lib/nes/util/status.hh (limited to 'lib/nes/util') diff --git a/lib/nes/util/CMakeLists.txt b/lib/nes/util/CMakeLists.txt deleted file mode 100644 index 6f5c112..0000000 --- a/lib/nes/util/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -target_sources( - nes - PRIVATE - address.hh - button-mask.hh - cycle-count.hh - debug.hh - display.hh - rgb.hh - snapshot.hh - status.hh) \ No newline at end of file diff --git a/lib/nes/util/address.hh b/lib/nes/util/address.hh deleted file mode 100644 index 2cce2f0..0000000 --- a/lib/nes/util/address.hh +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include - -namespace nes -{ - /// An address on the NES. - class address - { - std::uint16_t absolute_{ 0 }; - - public: - /// Create a zero address. - constexpr explicit address() = default; - - /// Create an address from its absolute value. - constexpr explicit address(std::uint16_t const absolute) - : absolute_{ absolute } - { - } - - /// Create an address from its page and offset combination. - constexpr explicit address(std::uint8_t const page, std::uint8_t const offset) - : address{ static_cast((page << 8) | (offset << 0)) } - { - } - - /// Get the absolute address value. - constexpr auto get_absolute() const -> std::uint16_t - { - return absolute_; - } - - /// Get the address page (the first 8 bits). - constexpr auto get_page() const -> std::uint8_t - { - return static_cast((get_absolute() & 0xFF00) >> 8); - } - - /// Get the offset within the page (the last 8 bits). - constexpr auto get_offset() const -> std::uint8_t - { - return static_cast((get_absolute() & 0x00FF) >> 0); - } - }; - - constexpr auto operator==(address const a, address const b) -> bool { return a.get_absolute() == b.get_absolute(); } - constexpr auto operator!=(address const a, address const b) -> bool { return a.get_absolute() != b.get_absolute(); } - constexpr auto operator<(address const a, address const b) -> bool { return a.get_absolute() < b.get_absolute(); } - constexpr auto operator>(address const a, address const b) -> bool { return a.get_absolute() > b.get_absolute(); } - constexpr auto operator<=(address const a, address const b) -> bool { return a.get_absolute() <= b.get_absolute(); } - constexpr auto operator>=(address const a, address const b) -> bool { return a.get_absolute() >= b.get_absolute(); } - - constexpr auto operator+(address const a, unsigned const b) -> address - { - return address{ static_cast(a.get_absolute() + b) }; - } - - constexpr auto operator+(unsigned const a, address const b) -> address - { - return address{ static_cast(a + b.get_absolute()) }; - } - - constexpr auto operator-(address const a, unsigned const b) -> address - { - return address{ static_cast(a.get_absolute() - b) }; - } - - constexpr auto operator%(address const a, unsigned const b) -> address - { - return address{ static_cast(a.get_absolute() % b) }; - } -} // namespace nes \ No newline at end of file diff --git a/lib/nes/util/button-mask.hh b/lib/nes/util/button-mask.hh deleted file mode 100644 index b010b9e..0000000 --- a/lib/nes/util/button-mask.hh +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include - -namespace nes -{ - class button_mask - { - std::uint8_t raw_value_{ 0 }; - - public: - constexpr explicit button_mask() = default; - - static constexpr auto from_raw_value(std::uint8_t const raw_value) -> button_mask - { - return button_mask{ raw_value }; - } - - constexpr auto add(button_mask const other) -> void { raw_value_ |= other.raw_value_; } - constexpr auto remove(button_mask const other) -> void { raw_value_ ^= other.raw_value_; } - constexpr auto contains(button_mask const other) const -> bool { return (raw_value_ & other.raw_value_) == other.raw_value_; } - constexpr auto is_empty() const -> bool { return raw_value_ == 0; } - constexpr auto get_raw_value() const -> std::uint8_t { return raw_value_; } - - private: - constexpr explicit button_mask(std::uint8_t const raw_value) - : raw_value_{ raw_value } - { - } - }; - - namespace buttons - { - static constexpr auto a = button_mask::from_raw_value(1 << 0); - static constexpr auto b = button_mask::from_raw_value(1 << 1); - static constexpr auto select = button_mask::from_raw_value(1 << 2); - static constexpr auto start = button_mask::from_raw_value(1 << 3); - static constexpr auto up = button_mask::from_raw_value(1 << 4); - static constexpr auto down = button_mask::from_raw_value(1 << 5); - static constexpr auto left = button_mask::from_raw_value(1 << 6); - static constexpr auto right = button_mask::from_raw_value(1 << 7); - } // namespace buttons -} // namespace nes \ No newline at end of file diff --git a/lib/nes/util/cycle-count.hh b/lib/nes/util/cycle-count.hh deleted file mode 100644 index f1a4a7e..0000000 --- a/lib/nes/util/cycle-count.hh +++ /dev/null @@ -1,87 +0,0 @@ -#pragma once - -#include - -namespace nes -{ - /// A cycle count that can be used for multiple components. - /// - /// Each component can have a different clock frequency and this structure offers conversions between different - /// cycle counts. - class cycle_count - { - friend constexpr auto operator+(cycle_count, cycle_count) -> cycle_count; - friend constexpr auto operator-(cycle_count, cycle_count) -> cycle_count; - friend constexpr auto operator*(std::uint64_t, cycle_count) -> cycle_count; - friend constexpr auto operator*(cycle_count, std::uint64_t) -> cycle_count; - friend constexpr auto operator/(cycle_count, cycle_count) -> std::uint64_t; - - std::uint64_t units_{ 0 }; // The most fine-granular cycle count (here: PPU cycles). - - static constexpr auto ppu_cycle() -> cycle_count { return cycle_count{ 1 }; } - static constexpr auto cpu_cycle() -> cycle_count { return cycle_count{ 3 }; } - static constexpr auto second() -> cycle_count { return 1789773 * cpu_cycle(); } - - public: - constexpr explicit cycle_count() = default; - - constexpr auto operator+=(cycle_count const other) -> cycle_count& { return *this = *this + other; } - constexpr auto operator*=(std::uint64_t const other) -> cycle_count& { return *this = *this * other; } - - static constexpr auto from_units(std::uint64_t const units) -> cycle_count - { - return cycle_count{ units }; - } - - static constexpr auto from_duration(std::chrono::microseconds const duration) -> cycle_count - { - return from_units(static_cast(duration.count()) * second().get_units() / (1000 * 1000)); - } - - static constexpr auto from_cpu(std::uint64_t const cpu_cycles) -> cycle_count - { - return cpu_cycles * cpu_cycle(); - } - - static constexpr auto from_ppu(std::uint64_t const ppu_cycles) -> cycle_count - { - return ppu_cycles * ppu_cycle(); - } - - /// Value in implementation-defined units. - constexpr auto get_units() const -> std::uint64_t { return units_; } - - constexpr auto to_duration() const -> std::chrono::microseconds - { - return std::chrono::microseconds{ get_units() * 1000 * 1000 / second().get_units() }; - } - - constexpr auto to_cpu() const -> std::uint64_t - { - return *this / cpu_cycle(); - } - - constexpr auto to_ppu() const -> std::uint64_t - { - return *this / ppu_cycle(); - } - - private: - constexpr explicit cycle_count(std::uint64_t const units) - : units_{ units } - { - } - }; - - constexpr auto operator==(cycle_count const a, cycle_count const b) -> bool { return a.get_units() == b.get_units(); } - constexpr auto operator!=(cycle_count const a, cycle_count const b) -> bool { return a.get_units() != b.get_units(); } - constexpr auto operator<(cycle_count const a, cycle_count const b) -> bool { return a.get_units() < b.get_units(); } - constexpr auto operator>(cycle_count const a, cycle_count const b) -> bool { return a.get_units() > b.get_units(); } - constexpr auto operator<=(cycle_count const a, cycle_count const b) -> bool { return a.get_units() <= b.get_units(); } - constexpr auto operator>=(cycle_count const a, cycle_count const b) -> bool { return a.get_units() >= b.get_units(); } - constexpr auto operator+(cycle_count const a, cycle_count const b) -> cycle_count { return cycle_count{ a.get_units() + b.get_units() }; } - constexpr auto operator-(cycle_count const a, cycle_count const b) -> cycle_count { return cycle_count{ a.get_units() - b.get_units() }; } - constexpr auto operator*(std::uint64_t const a, cycle_count const b) -> cycle_count { return cycle_count{ a * b.get_units() }; } - constexpr auto operator*(cycle_count const a, std::uint64_t const b) -> cycle_count { return cycle_count{ a.get_units() * b }; } - constexpr auto operator/(cycle_count const a, cycle_count const b) -> std::uint64_t { return a.get_units() / b.get_units(); } -} // namespace nes \ No newline at end of file diff --git a/lib/nes/util/debug.hh b/lib/nes/util/debug.hh deleted file mode 100644 index 409d55a..0000000 --- a/lib/nes/util/debug.hh +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#ifdef NES_ENABLE_DEBUG_OUTPUT - -#include - -#define NES_DEBUG_LOG(name, ...) \ - spdlog::info("[" #name "] " __VA_ARGS__) - -#else - -#define NES_DEBUG_LOG(name, fmt, ...) \ - (void)0 - -#endif \ No newline at end of file diff --git a/lib/nes/util/display.hh b/lib/nes/util/display.hh deleted file mode 100644 index 8b6e0e5..0000000 --- a/lib/nes/util/display.hh +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "nes/util/rgb.hh" - -namespace nes -{ - class rgb; - - /// Abstraction of the display logic. - class display - { - public: - static constexpr auto width = unsigned{ 256 }; - static constexpr auto height = unsigned{ 240 }; - - virtual ~display() = default; - - display(display const&) = delete; - display(display&&) = delete; - auto operator=(display const&) -> display& = delete; - auto operator=(display&&) -> display& = delete; - - /// Switch between front and back buffers. - virtual auto switch_buffers() -> void = 0; - /// Update the pixel at the given position in the back buffer. - virtual auto set(unsigned x, unsigned y, rgb value) -> void = 0; - /// Load the pixel value at the given position in the back buffer. - virtual auto get(unsigned x, unsigned y) const -> rgb = 0; - /// Clear the entire back buffer with a color. - virtual auto clear(rgb const color) -> void - { - for (auto y = unsigned{ 0 }; y < height; ++y) - { - for (auto x = unsigned{ 0 }; x < width; ++x) - { - set(x, y, color); - } - } - } - - protected: - explicit display() = default; - }; -} // namespace nes \ No newline at end of file diff --git a/lib/nes/util/rgb.hh b/lib/nes/util/rgb.hh deleted file mode 100644 index d62c70c..0000000 --- a/lib/nes/util/rgb.hh +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include - -namespace nes -{ - /// An RGB value. - class rgb - { - public: - explicit rgb() = default; - - explicit rgb(std::uint8_t const r, std::uint8_t const g, std::uint8_t const b) - : r{ r } - , g{ g } - , b{ b } - { - } - - static auto from_hex(std::uint32_t const hex) -> rgb - { - auto res = rgb{}; - res.r = (hex & 0x00FF0000) >> 16; - res.g = (hex & 0x0000FF00) >> 8; - res.b = (hex & 0x000000FF) >> 0; - return res; - } - - std::uint8_t r{ 0 }; - std::uint8_t g{ 0 }; - std::uint8_t b{ 0 }; - }; -} // namespace nes \ No newline at end of file diff --git a/lib/nes/util/snapshot.hh b/lib/nes/util/snapshot.hh deleted file mode 100644 index de23a46..0000000 --- a/lib/nes/util/snapshot.hh +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once - -#include "nes/util/cycle-count.hh" -#include -#include - -namespace nes -{ - /// System snapshot used for comparison with a reference implementation. - struct snapshot - { - explicit snapshot() = default; - - cycle_count cpu_cycle; - cycle_count ppu_cycle; - std::vector ram; - std::vector sram; - std::vector vram; - std::vector oam; - struct - { - std::uint16_t pc{}; - std::uint8_t sp{}; - std::uint8_t a{}; - std::uint8_t x{}; - std::uint8_t y{}; - std::uint8_t p{}; - bool c{}; - bool z{}; - bool i{}; - bool d{}; - bool b{}; - bool v{}; - bool n{}; - } registers{}; - }; - - inline auto operator==(snapshot const& a, snapshot const& b) -> bool - { - if (a.cpu_cycle != b.cpu_cycle) { return false; } - if (a.ppu_cycle != b.ppu_cycle) { return false; } - if (a.ram != b.ram) { return false; } - if (a.sram != b.sram) { return false; } - if (a.vram != b.vram) { return false; } - if (a.oam != b.oam) { return false; } - if (a.registers.pc != b.registers.pc) { return false; } - if (a.registers.sp != b.registers.sp) { return false; } - if (a.registers.a != b.registers.a) { return false; } - if (a.registers.x != b.registers.x) { return false; } - if (a.registers.y != b.registers.y) { return false; } - if (a.registers.p != b.registers.p) { return false; } - if (a.registers.c != b.registers.c) { return false; } - if (a.registers.z != b.registers.z) { return false; } - if (a.registers.i != b.registers.i) { return false; } - if (a.registers.d != b.registers.d) { return false; } - if (a.registers.b != b.registers.b) { return false; } - if (a.registers.v != b.registers.v) { return false; } - if (a.registers.n != b.registers.n) { return false; } - return true; - } - - inline auto operator!=(snapshot const& a, snapshot const& b) -> bool { return !(a == b); } -} // namespace nes \ No newline at end of file diff --git a/lib/nes/util/status.hh b/lib/nes/util/status.hh deleted file mode 100644 index 64f5969..0000000 --- a/lib/nes/util/status.hh +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -namespace nes -{ - /// A status code. - enum class status - { - success, - error_uninitialized, - error_system_error, - error_invalid_ines_data, - error_unsupported_mapper, - error_unsupported_instruction, - }; - - constexpr auto to_string(status const status) -> char const* - { - switch (status) - { - case status::success: - return "Success"; - case status::error_uninitialized: - return "Uninitialized"; - case status::error_system_error: - return "System error"; - case status::error_invalid_ines_data: - return "Invalid iNES data"; - case status::error_unsupported_mapper: - return "Unsupported mapper"; - case status::error_unsupported_instruction: - return "Unsupported instruction"; - } - - return "(invalid)"; - } -} // namespace nes \ No newline at end of file -- cgit v1.3.1