summaryrefslogtreecommitdiff
path: root/lib/nes/common
diff options
context:
space:
mode:
authorHannes Schulze <haschu0103@outlook.de>2025-01-29 14:49:20 +0100
committerHannes Schulze <haschu0103@outlook.de>2025-01-29 14:49:20 +0100
commit5959349f341a2380c0c3f41ec40a1e8e0d969d00 (patch)
tree8b36ba3f8cade68562af1dfdcea8b42160619975 /lib/nes/common
parent1d32f7561305d6c86b1c80858bc73d55b1c6c402 (diff)
refactor: move emulation logic into sys
Diffstat (limited to 'lib/nes/common')
-rw-r--r--lib/nes/common/CMakeLists.txt6
-rw-r--r--lib/nes/common/debug.hh15
-rw-r--r--lib/nes/common/display.hh44
-rw-r--r--lib/nes/common/rgb.hh33
4 files changed, 98 insertions, 0 deletions
diff --git a/lib/nes/common/CMakeLists.txt b/lib/nes/common/CMakeLists.txt
new file mode 100644
index 0000000..69ae435
--- /dev/null
+++ b/lib/nes/common/CMakeLists.txt
@@ -0,0 +1,6 @@
+target_sources(
+ nes
+ PRIVATE
+ debug.hh
+ rgb.hh
+ display.hh) \ No newline at end of file
diff --git a/lib/nes/common/debug.hh b/lib/nes/common/debug.hh
new file mode 100644
index 0000000..409d55a
--- /dev/null
+++ b/lib/nes/common/debug.hh
@@ -0,0 +1,15 @@
+#pragma once
+
+#ifdef NES_ENABLE_DEBUG_OUTPUT
+
+#include <spdlog/spdlog.h>
+
+#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/common/display.hh b/lib/nes/common/display.hh
new file mode 100644
index 0000000..ca78a85
--- /dev/null
+++ b/lib/nes/common/display.hh
@@ -0,0 +1,44 @@
+#pragma once
+
+#include "nes/common/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/common/rgb.hh b/lib/nes/common/rgb.hh
new file mode 100644
index 0000000..d62c70c
--- /dev/null
+++ b/lib/nes/common/rgb.hh
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <cstdint>
+
+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