From 32caa6c474146c8fba212da014e9377675fac4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo=20Taylor=20Ienczak=20Zanette?= Date: Tue, 9 Jan 2024 19:20:34 -0300 Subject: [PATCH] utils, general: Move bitset to its own header and use gba:: namespace for utils --- libgba-cpp/arch/cpu/interrupts.cpp | 2 +- libgba-cpp/arch/registers.h | 138 +++++++------------------- libgba-cpp/arch/sound/directsound.cpp | 2 +- libgba-cpp/arch/sound/sound.cpp | 2 +- libgba-cpp/engine/graphics/map.h | 49 --------- libgba-cpp/engine/graphics/maps.h | 92 ----------------- libgba-cpp/utils/bitset.h | 79 +++++++++++++++ libgba-cpp/utils/general.h | 2 +- 8 files changed, 118 insertions(+), 248 deletions(-) delete mode 100644 libgba-cpp/engine/graphics/map.h delete mode 100644 libgba-cpp/engine/graphics/maps.h create mode 100644 libgba-cpp/utils/bitset.h diff --git a/libgba-cpp/arch/cpu/interrupts.cpp b/libgba-cpp/arch/cpu/interrupts.cpp index 3a72d4e..784887e 100644 --- a/libgba-cpp/arch/cpu/interrupts.cpp +++ b/libgba-cpp/arch/cpu/interrupts.cpp @@ -53,7 +53,7 @@ auto& register_at(uint16_t address) { } void set_interrupt_bit(Handler handler, bool enabled) { - const auto& sender = senders[utils::value_of(handler.type)]; + const auto& sender = senders[gba::utils::value_of(handler.type)]; register_at(sender.offset)[sender.bit] = enabled; } diff --git a/libgba-cpp/arch/registers.h b/libgba-cpp/arch/registers.h index e962a97..ab8de54 100644 --- a/libgba-cpp/arch/registers.h +++ b/libgba-cpp/arch/registers.h @@ -6,75 +6,7 @@ #include #include - -template -class bitset_bit { -public: - bitset_bit(T& value, std::size_t index) - : index_(index) - , value_(value) - {} - - bitset_bit& operator=(bool bit) { - if (bit) { - value_ |= (1 << index_); - } else { - value_ &= ~(1 << index_); - } - return *this; - }; - - operator bool() const { - return value_ & (1 << index_); - } - - operator bool() { - return value_ & (1 << index_); - } - -private: - std::size_t index_; - T& value_; -}; - -template -class bitset { -public: - bitset() = default; - - bitset(T value) - : value_(value) - {} - - auto operator[](std::size_t index) -> bitset_bit { - return bitset_bit(value_, index); - } - - auto operator[](std::size_t index) const -> const bitset_bit { - return bitset_bit(value_, index); - } - - auto operator[](std::size_t index) volatile const -> const bitset_bit { - return bitset_bit(value_, index); - } - - auto operator=(const T& other) -> uint32_t { - value_ = other; - return value_; - } - - auto operator|=(const T& other) -> T& { - value_ |= other; - return value_; - } - - auto to_ulong() const -> unsigned long int { - return value_; - } - -private: - T value_; -}; +#include /** * Elements related to GBA's physical architecture. @@ -118,18 +50,18 @@ namespace display { /** * Display Control Register. */ -static auto& lcd_control = at>(0x0400'0000); +static auto& lcd_control = at>(0x0400'0000); static_assert(sizeof(lcd_control) == 2, "LCD Control must exactly have 2 bytes."); /** * Green Swap Register (no official documentation). */ -static auto& green_swap = raw_at>(0x0400'0002); +static auto& green_swap = raw_at>(0x0400'0002); /** * Display status register. */ -static auto& lcd_status = raw_at>(0x0400'0004); +static auto& lcd_status = raw_at>(0x0400'0004); /** * Number of scanline currently being processed by PPU. @@ -218,7 +150,7 @@ static auto& window_inside = raw_at(0x0400'0048); static auto& window_outside = raw_at(0x0400'004a); static auto& mosaic_size = raw_at(0x0400'004c); -static auto& effect_control = raw_at>(0x0400'0050); +static auto& effect_control = raw_at>(0x0400'0050); static auto& blend_a = raw_at(0x0400'0052); static auto& blend_b = raw_at(0x0400'0054); @@ -230,38 +162,38 @@ static auto& bg_palette = raw_at>(0x05 */ namespace sound { -static auto& channel1_sweep = raw_at>(0x0400'0060); -static auto& channel1_envelope = raw_at>(0x0400'0062); -static auto& channel1_control = raw_at>(0x0400'0064); +static auto& channel1_sweep = raw_at>(0x0400'0060); +static auto& channel1_envelope = raw_at>(0x0400'0062); +static auto& channel1_control = raw_at>(0x0400'0064); -static auto& channel2_envelope = raw_at>(0x0400'0068); -static auto& channel2_control = raw_at>(0x0400'006c); +static auto& channel2_envelope = raw_at>(0x0400'0068); +static auto& channel2_control = raw_at>(0x0400'006c); -static auto& channel3_wave_bank = raw_at>(0x0400'0070); -static auto& channel3_length = raw_at>(0x0400'0072); -static auto& channel3_control = raw_at>(0x0400'0074); +static auto& channel3_wave_bank = raw_at>(0x0400'0070); +static auto& channel3_length = raw_at>(0x0400'0072); +static auto& channel3_control = raw_at>(0x0400'0074); -static auto& channel4_envelope = raw_at>(0x0400'0078); -static auto& channel4_noise = raw_at>(0x0400'007c); +static auto& channel4_envelope = raw_at>(0x0400'0078); +static auto& channel4_noise = raw_at>(0x0400'007c); -static auto& dmg_control = raw_at>(0x0400'0080); -static auto& directsound_control = raw_at>(0x0400'0082); -static auto& status = raw_at>(0x0400'0084); +static auto& dmg_control = raw_at>(0x0400'0080); +static auto& directsound_control = raw_at>(0x0400'0082); +static auto& status = raw_at>(0x0400'0084); -static auto& bias = raw_at>(0x0400'0088); +static auto& bias = raw_at>(0x0400'0088); -static auto& wave_ram0_l = raw_at>(0x0400'0090); -static auto& wave_ram0_h = raw_at>(0x0400'0092); +static auto& wave_ram0_l = raw_at>(0x0400'0090); +static auto& wave_ram0_h = raw_at>(0x0400'0092); -static auto& wave_ram1_l = raw_at>(0x0400'0094); -static auto& wave_ram1_h = raw_at>(0x0400'0096); +static auto& wave_ram1_l = raw_at>(0x0400'0094); +static auto& wave_ram1_h = raw_at>(0x0400'0096); -static auto& wave_ram2_l = raw_at>(0x0400'0098); -static auto& wave_ram2_h = raw_at>(0x0400'009a); +static auto& wave_ram2_l = raw_at>(0x0400'0098); +static auto& wave_ram2_h = raw_at>(0x0400'009a); // TODO: Fix data types -static auto& wave_ram3_l = raw_at>(0x0400'009c); -static auto& wave_ram3_h = raw_at>(0x0400'009e); +static auto& wave_ram3_l = raw_at>(0x0400'009c); +static auto& wave_ram3_h = raw_at>(0x0400'009e); static auto& dsa_fifo_0 = raw_at(0x0400'00a0); static auto& dsa_fifo_1 = raw_at(0x0400'00a1); @@ -317,14 +249,14 @@ static auto& serial_third = raw_at(0x0400'0126); static auto& serial_control = raw_at(0x0400'0128); static auto& serial_send_data = raw_at(0x0400'012a); -static auto& keypad_status = raw_at>(0x0400'0130); -static auto& keypad_interrupt = raw_at>(0x0400'0132); +static auto& keypad_status = raw_at>(0x0400'0130); +static auto& keypad_interrupt = raw_at>(0x0400'0132); -static auto& serial_mode = raw_at>(0x0400'0134); -static auto& serial_joy_control = raw_at>(0x0400'0140); +static auto& serial_mode = raw_at>(0x0400'0134); +static auto& serial_joy_control = raw_at>(0x0400'0140); static auto& serial_joy_receive_data = raw_at(0x0400'0150); static auto& serial_joy_transmit_data = raw_at(0x0400'0154); -static auto& serial_joy_receive_status = raw_at>(0x0400'0158); +static auto& serial_joy_receive_status = raw_at>(0x0400'0158); } @@ -333,9 +265,9 @@ static auto& serial_joy_receive_status = raw_at>(0x0400'0158); //---------------------------------------------------------------------------- namespace cpu { -static auto& interrupt_enable = raw_at>(0x0400'0200); +static auto& interrupt_enable = raw_at>(0x0400'0200); static volatile auto& interrupt_request = raw_at(0x0400'0202); -static auto& gamepak_wait_control = raw_at>(0x0400'0204); +static auto& gamepak_wait_control = raw_at>(0x0400'0204); static auto& master_enable = raw_at(0x0400'0208); } @@ -345,7 +277,7 @@ static auto& master_enable = raw_at(0x0400'0208); //---------------------------------------------------------------------------- namespace bios { -static volatile auto& interrupt_check = raw_at>(0x0300'7ff8); +static volatile auto& interrupt_check = raw_at>(0x0300'7ff8); } diff --git a/libgba-cpp/arch/sound/directsound.cpp b/libgba-cpp/arch/sound/directsound.cpp index e9b0ad1..861d21e 100644 --- a/libgba-cpp/arch/sound/directsound.cpp +++ b/libgba-cpp/arch/sound/directsound.cpp @@ -4,7 +4,7 @@ #include "registers.h" -using utils::value_of; +using gba::utils::value_of; using namespace gba::arch::registers::sound; namespace gba { diff --git a/libgba-cpp/arch/sound/sound.cpp b/libgba-cpp/arch/sound/sound.cpp index 710328d..752cdb6 100644 --- a/libgba-cpp/arch/sound/sound.cpp +++ b/libgba-cpp/arch/sound/sound.cpp @@ -3,7 +3,7 @@ #include #include -using utils::value_of; +using gba::utils::value_of; using namespace gba::arch::registers::sound; namespace gba { diff --git a/libgba-cpp/engine/graphics/map.h b/libgba-cpp/engine/graphics/map.h deleted file mode 100644 index 6d84a90..0000000 --- a/libgba-cpp/engine/graphics/map.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef LIBGBA_ENGINE_MAP_H -#define LIBGBA_ENGINE_MAP_H - -#include -#include "tilemap.h" - -namespace gba::graphics { - -using gba::display::MapSize; - -class Map { -public: - Map(MapSize size, - const Tileset& tileset, - const Tilemap& tilemap): - size_{size}, - tileset_{tileset}, - tilemap_{tilemap} - {} - - const auto tileset() const { - return tileset_; - } - - const auto tilemap() const { - return tilemap_; - } - - const auto size() const { - return size_; - } - - const auto width() const { - return gba::display::extract_size(size_).width; - } - - const auto height() const { - return gba::display::extract_size(size_).height; - } - -private: - const MapSize size_; - const Tileset& tileset_; - const Tilemap& tilemap_; -}; - -} - -#endif diff --git a/libgba-cpp/engine/graphics/maps.h b/libgba-cpp/engine/graphics/maps.h deleted file mode 100644 index 00e48cc..0000000 --- a/libgba-cpp/engine/graphics/maps.h +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef GBA_ENGINE_GRAPHICS_MAPS_H -#define GBA_ENGINE_GRAPHICS_MAPS_H - -#include - -#include - -#include "geometry.h" - -namespace gba::engine::graphics { - -template -using TileSet = std::array; - - -template -class TileMap { - using contents_t = std::array; -public: - TileMap(contents_t& contents): - contents{contents} - {} - - auto& length() const { - return MapLength; - } - - const auto& operator[](int i) const { - return contents[i]; - } - -private: - const contents_t& contents; -}; - - -class Map { -public: - virtual auto length() const -> std::size_t = 0; - virtual auto operator()(int x, int y) -> int& = 0; - virtual auto operator()(int x, int y) const -> const int& = 0; -}; - - -template -class BaseMap: Map { - using tileset_t = TileSet; - using tilemap_t = TileMap; -public: - BaseMap(tileset_t& tileset, tilemap_t& tilemap, geometry::Size size = {0, 0}): - _tileset{tileset}, - _tilemap{tilemap}, - size{size} - {} - - auto length() const override { - return _tilemap.length(); - } - - auto& tileset() { - return _tileset; - } - - const auto& tileset() const { - return _tileset; - } - - auto& tilemap() { - return _tilemap; - } - - const auto& tilemap() const { - return _tilemap; - } - - auto& operator()(int x, int y) override { - return _tilemap[x + size.width * y]; - } - - const auto& operator()(int x, int y) const override { - return _tilemap[x + size.width * y]; - } - -private: - tileset_t& _tileset; - tilemap_t& _tilemap; - geometry::Size size; -}; - -} - -#endif diff --git a/libgba-cpp/utils/bitset.h b/libgba-cpp/utils/bitset.h new file mode 100644 index 0000000..f966e92 --- /dev/null +++ b/libgba-cpp/utils/bitset.h @@ -0,0 +1,79 @@ +#ifndef LIBGBACPP_UTILS_BITSET_H +#define LIBGBACPP_UTILS_BITSET_H + +#include + +namespace gba::utils { + +template +class bitset_bit { +public: + bitset_bit(T& value, std::size_t index) + : index_(index) + , value_(value) + {} + + bitset_bit& operator=(bool bit) { + if (bit) { + value_ |= (1 << index_); + } else { + value_ &= ~(1 << index_); + } + return *this; + }; + + operator bool() const { + return value_ & (1 << index_); + } + + operator bool() { + return value_ & (1 << index_); + } + +private: + std::size_t index_; + T& value_; +}; + +template +class bitset { +public: + bitset() = default; + + bitset(T value) + : value_(value) + {} + + auto operator[](std::size_t index) -> bitset_bit { + return bitset_bit(value_, index); + } + + auto operator[](std::size_t index) const -> const bitset_bit { + return bitset_bit(value_, index); + } + + auto operator[](std::size_t index) volatile const -> const bitset_bit { + return bitset_bit(value_, index); + } + + auto operator=(const T& other) -> uint32_t { + value_ = other; + return value_; + } + + auto operator|=(const T& other) -> T& { + value_ |= other; + return value_; + } + + auto to_ulong() const -> unsigned long int { + return value_; + } + +private: + T value_; +}; + +} + +#endif diff --git a/libgba-cpp/utils/general.h b/libgba-cpp/utils/general.h index f4a7f6e..4fe80cb 100644 --- a/libgba-cpp/utils/general.h +++ b/libgba-cpp/utils/general.h @@ -7,7 +7,7 @@ /** * General C++ Utilities. */ -namespace utils { +namespace gba::utils { /** * Extracts value from enum.