From dfab120bfb85c03d2b84fee27cf27677b2b04eee Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 22 Mar 2023 20:46:08 +0100 Subject: [PATCH] Remove the ComplexBitMap --- include/PSX/Auxiliary/bits.hpp | 119 ++++++++++-- include/PSX/Auxiliary/complex_bitmap.hpp | 202 -------------------- include/PSX/File/file_types.hpp | 22 ++- include/PSX/GPU/gpu_types.hpp | 16 +- include/PSX/System/IOPorts/cd_io.hpp | 38 ++-- include/PSX/System/IOPorts/dma_io.hpp | 68 +++---- include/PSX/System/IOPorts/gpu_io.hpp | 88 ++++----- include/PSX/System/IOPorts/interrupt_io.hpp | 30 +-- include/PSX/System/IOPorts/ioport.hpp | 101 ++-------- include/PSX/System/IOPorts/spu_io.hpp | 64 +++---- include/PSX/System/IOPorts/timer_io.hpp | 26 +-- src/Library/src/CD/cd.cpp | 20 +- src/Library/src/startup.cpp | 2 +- 13 files changed, 307 insertions(+), 489 deletions(-) diff --git a/include/PSX/Auxiliary/bits.hpp b/include/PSX/Auxiliary/bits.hpp index 8ef3a671..8857b64a 100644 --- a/include/PSX/Auxiliary/bits.hpp +++ b/include/PSX/Auxiliary/bits.hpp @@ -1,17 +1,88 @@ #ifndef __JABYENGINE_BITS_HPP__ #define __JABYENGINE_BITS_HPP__ #include "../jabyengine_defines.h" +#include "types.hpp" namespace JabyEngine { namespace bit { + namespace value { + template + static constexpr T set_normalized(T raw_value, T value, size_t start_bit, size_t length); + } + } + + struct ClearBit { + uint16_t pos; + + constexpr ClearBit(uint16_t bit_pos) : pos(bit_pos) { + } + }; + + struct Bit { + uint16_t pos; + + constexpr Bit(uint16_t bit_pos) : pos(bit_pos) { + } + + constexpr ClearBit operator!() const { + return ClearBit(this->pos); + } + }; + + struct BitRange { + template + using RangeValuePair = pair; + + uint16_t pos; + uint16_t length; + + constexpr BitRange(uint16_t pos, uint16_t length) : pos(pos), length(length) { + } + + static constexpr BitRange from_to(uint16_t first_bit, uint16_t last_bit) { + return BitRange(first_bit, (last_bit - first_bit) + 1); + } + + template + constexpr RangeValuePair with(T value) const { + return {*this, value}; + } + + template + constexpr T as_value(T value) const { + return bit::value::set_normalized(static_cast(0), value, this->pos, this->length); + } + + template + constexpr RangeValuePair range_max() const { + return BitRange::with((1 << this->length) - 1); + } + }; + + namespace bit { + template + static constexpr T clear(T raw_value, size_t bit) { + return (raw_value & ~(1 << bit)); + } + + template + static constexpr T clear(T raw_value, Bit bit) { + return clear(raw_value, bit.pos); + } + template static constexpr T set(T raw_value, size_t bit) { return (raw_value | (1 << bit)); } template - static constexpr T clear(T raw_value, size_t bit) { - return (raw_value & ~(1 << bit)); + static constexpr T set(T raw_value, Bit bit) { + return set(raw_value, bit.pos); + } + + template + static constexpr T set(T raw_value, ClearBit bit) { + return clear(raw_value, bit.pos); } template @@ -19,30 +90,52 @@ namespace JabyEngine { return static_cast(raw_value & (1 << bit)); } - namespace value { - template - static constexpr T crop_value(T raw_value, size_t length) { - return (raw_value & ((1 << length) - 1)); - } + template + static constexpr bool is_set(T raw_value, Bit bit) { + return is_set(raw_value, bit.pos); + } - template - static constexpr T range_mask(size_t start_bit, size_t length) { - return (((1 << length) - 1) << start_bit); + namespace value { + namespace helper { + template + static constexpr T crop_value(T raw_value, size_t length) { + return (raw_value & ((1 << length) - 1)); + } + + template + static constexpr T range_mask(size_t start_bit, size_t length) { + return (((1 << length) - 1) << start_bit); + } } template static constexpr T clear_normalized(T raw_value, size_t start_bit, size_t length) { - return (raw_value & ~range_mask(start_bit, length)); + return (raw_value & ~helper::range_mask(start_bit, length)); } template static constexpr T set_normalized(T raw_value, T value, size_t start_bit, size_t length) { - return (clear_normalized(raw_value, start_bit, length) | (crop_value(value, length) << start_bit)); + return (clear_normalized(raw_value, start_bit, length) | (helper::crop_value(value, length) << start_bit)); + } + + template + static constexpr T set_normalized(T raw_value, BitRange bits, T value) { + return set_normalized(raw_value, value, bits.pos, bits.length); + } + + template + static constexpr T set_normalized(T raw_value, const BitRange::RangeValuePair &value_pair) { + return set_normalized(raw_value, value_pair.first, static_cast(value_pair.second)); } template static constexpr T get_normalized(T raw_value, size_t start_bit, size_t length) { - return crop_value((raw_value & range_mask(start_bit, length)) >> start_bit, length); + return helper::crop_value((raw_value & helper::range_mask(start_bit, length)) >> start_bit, length); + } + + template + static constexpr T get_normalized(T raw_value, BitRange range) { + return get_normalized(raw_value, range.pos, range.length); } } diff --git a/include/PSX/Auxiliary/complex_bitmap.hpp b/include/PSX/Auxiliary/complex_bitmap.hpp index b1a6bb64..4709a4b7 100644 --- a/include/PSX/Auxiliary/complex_bitmap.hpp +++ b/include/PSX/Auxiliary/complex_bitmap.hpp @@ -3,208 +3,6 @@ #include "bits.hpp" namespace JabyEngine { - struct ClearBitValue { - size_t bit; - - constexpr ClearBitValue(size_t bit) : bit(bit) { - } - }; - - template - struct Bit { - typedef T ValueType; - - size_t value; - - constexpr Bit(size_t value) : value(value) { - } - - constexpr operator size_t() const { - return this->value; - } - - constexpr ClearBitValue operator!() const { - return ClearBitValue(this->value); - } - }; - - template - struct BitRangeValue { - T value; - size_t begin; - size_t length; - - constexpr BitRangeValue() = default; - constexpr BitRangeValue(T value, size_t begin, size_t length) : value(value), begin(begin), length(length) { - } - }; - - template - struct BitRange { - typedef T ValueType; - - size_t begin; - size_t length; - - static constexpr BitRange from_to(size_t start, size_t end) { - return {start, (end - start + 1)}; - } - - constexpr BitRangeValue with(T value) const { - return BitRangeValue(value, this->begin, this->length); - } - - constexpr BitRangeValue max() const { - return BitRange::with((1 << this->length) - 1); - } - }; - - template - static constexpr __always_inline BitRangeValue operator<<(const BitRange& range, T value) { - return BitRangeValue{value, range.begin, range.length}; - } - - template - class ComplexBitMap { - public: - typedef T UnderlyingType; - T raw; - - private: - template - constexpr __always_inline ComplexBitMap& set_va(const S& value) { - return this->set(value); - } - - template - constexpr __always_inline ComplexBitMap& set_va(const S& value, const ARGS&...args) { - return this->set_va(value).set_va(args...); - } - - public: - template - static constexpr __always_inline ComplexBitMap with(ARGS...args) { - return ComplexBitMap().set_va(args...); - } - - //Accesssing bits - template - constexpr ComplexBitMap& set_bit(S bit) { - this->raw = bit::set(this->raw, static_cast(bit)); - return *this; - } - - template - constexpr void set_bit(S bit) volatile { - this->raw = bit::set(this->raw, static_cast(bit)); - } - - template - constexpr ComplexBitMap& clear_bit(S bit) { - this->raw = bit::clear(this->raw, static_cast(bit)); - return *this; - } - - template - constexpr void clear_bit(S bit) volatile { - this->raw = bit::clear(this->raw, static_cast(bit)); - } - - template - constexpr bool is_bit_set(S bit) { - return bit::is_set(this->raw, static_cast(bit)); - } - - template - constexpr bool is_bit_set(S bit) const volatile { - return bit::is_set(this->raw, static_cast(bit)); - } - - //Accessing values - template - constexpr ComplexBitMap& set_value(S value, const BitRange& range) { - this->raw = bit::value::set_normalized(this->raw, static_cast(value), range.begin, range.length); - return *this; - } - - template - constexpr void set_value(S value, const BitRange& range) volatile { - this->raw = bit::value::set_normalized(this->raw, static_cast(value), range.begin, range.length); - } - - template - constexpr ComplexBitMap& clear_value(const BitRange& range) { - this->raw = bit::value::clear_normalized(this->raw, range.begin, range.length); - return *this; - } - - template - constexpr void clear_value(const BitRange& range) volatile { - this->raw = bit::value::clear_normalized(this->raw, range.begin, range.length); - } - - template - constexpr S get_value(const BitRange& range) const { - return static_cast(bit::value::get_normalized(this->raw, range.begin, range.length)); - } - - template - constexpr S get_value(const BitRange& range) const volatile { - return static_cast(bit::value::get_normalized(this->raw, range.begin, range.length)); - } - - //For easier checking - constexpr bool is(Bit bit) const { - return ComplexBitMap::is_bit_set(bit); - } - - constexpr bool is(Bit bit) const volatile { - return ComplexBitMap::is_bit_set(bit); - } - - // For easier constructing - template - constexpr __always_inline ComplexBitMap& set(const BitRange& range, T value) { - this->set_value(value, range); - return *this; - } - - template - constexpr __always_inline ComplexBitMap& set(const BitRangeValue& value) { - this->set_value(value.value, {value.begin, value.length}); - return *this; - } - - template - constexpr __always_inline ComplexBitMap& set(const Bit& bit) { - this->set_bit(bit.value); - return *this; - } - - constexpr __always_inline ComplexBitMap& set(const ClearBitValue& value) { - this->clear_bit(value.bit); - return *this; - } - - constexpr __always_inline ComplexBitMap& operator|(const BitRangeValue& value) { - this->set_value(value.value, value.range); - return *this; - } - - constexpr __always_inline ComplexBitMap& operator|(const Bit& bit) { - this->set_bit(bit.value); - return *this; - } - - constexpr __always_inline ComplexBitMap& operator|(const ClearBitValue& value) { - this->clear_bit(value.bit); - return *this; - } - - constexpr __always_inline operator T() const { - return this->raw; - } - }; } #endif //!__JABYENGINE_COMPLEX_BITMAP_HPP__ \ No newline at end of file diff --git a/include/PSX/File/file_types.hpp b/include/PSX/File/file_types.hpp index 6f353d7c..77e420e6 100644 --- a/include/PSX/File/file_types.hpp +++ b/include/PSX/File/file_types.hpp @@ -4,33 +4,35 @@ #include "../jabyengine_defines.h" namespace JabyEngine { - struct __no_align SimpleTIM : private ComplexBitMap { - static constexpr auto TextureX = BitRange(0, 8); - static constexpr auto TextureY = BitRange(9, 16); - static constexpr auto ClutX = BitRange(17, 22); - static constexpr auto ClutY = BitRange(23, 31); + struct __no_align SimpleTIM { + static constexpr auto TextureX = BitRange::from_to(0, 8); + static constexpr auto TextureY = BitRange::from_to(9, 16); + static constexpr auto ClutX = BitRange::from_to(17, 22); + static constexpr auto ClutY = BitRange::from_to(23, 31); + + uint32_t raw; constexpr SimpleTIM() { this->raw = 0; } - constexpr SimpleTIM(uint16_t texX, uint16_t texY, uint16_t clutX, uint16_t clutY) : ComplexBitMap(ComplexBitMap::with(TextureX.with(texX >> 1), TextureY.with(texY >> 1), ClutX.with(clutX >> 4), ClutY.with(clutY))) { + constexpr SimpleTIM(uint16_t texX, uint16_t texY, uint16_t clutX, uint16_t clutY) : raw(TextureX.as_value(texX >> 1) | TextureY.as_value(texY >> 1) | ClutX.as_value(clutX >> 4) | ClutY.as_value(clutY)) { } constexpr uint16_t getTextureX() const { - return (ComplexBitMap::get_value(SimpleTIM::TextureX) << 1); + return bit::value::get_normalized(this->raw, TextureX) << 1; } constexpr uint16_t getTextureY() const { - return (ComplexBitMap::get_value(SimpleTIM::TextureY) << 1); + return bit::value::get_normalized(this->raw, TextureY) << 1; } constexpr uint16_t getClutX() const { - return (ComplexBitMap::get_value(SimpleTIM::ClutX) << 4); + return bit::value::get_normalized(this->raw, SimpleTIM::ClutX) << 4; } constexpr uint16_t getClutY() const { - return ComplexBitMap::get_value(SimpleTIM::ClutY); + return bit::value::get_normalized(this->raw, ClutY); } }; diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index 814f7370..79bce891 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -41,12 +41,12 @@ namespace JabyEngine { class Color { private: - static constexpr auto RedRange = BitRange::from_to(0, 4); - static constexpr auto GreenRange = BitRange::from_to(5, 9); - static constexpr auto BlueRange = BitRange::from_to(10, 14); - static constexpr auto SemiTransperancyBit = Bit(15); + static constexpr auto RedRange = BitRange::from_to(0, 4); + static constexpr auto GreenRange = BitRange::from_to(5, 9); + static constexpr auto BlueRange = BitRange::from_to(10, 14); + static constexpr auto SemiTransperancyBit = Bit(15); - ComplexBitMap value = {0}; + uint16_t value = 0; public: static constexpr Color from_rgb(uint8_t r, uint8_t g, uint8_t b) { @@ -58,17 +58,17 @@ namespace JabyEngine { } constexpr Color& set_red(uint8_t red) { - this->value.set_value(static_cast(red), RedRange); + this->value = bit::value::set_normalized(this->value, RedRange.with(red)); return *this; } constexpr Color& set_green(uint8_t green) { - this->value.set_value(static_cast(green), GreenRange); + this->value = bit::value::set_normalized(this->value, GreenRange.with(green)); return *this; } constexpr Color& set_blue(uint8_t blue) { - this->value.set_value(static_cast(blue), BlueRange); + this->value = bit::value::set_normalized(this->value, BlueRange.with(blue)); return *this; } }; diff --git a/include/PSX/System/IOPorts/cd_io.hpp b/include/PSX/System/IOPorts/cd_io.hpp index 9e3ba268..e6d2c36c 100644 --- a/include/PSX/System/IOPorts/cd_io.hpp +++ b/include/PSX/System/IOPorts/cd_io.hpp @@ -32,41 +32,41 @@ namespace JabyEngine { }; __declare_io_type(IndexStatus, uint8_t, - static constexpr auto PortIndex = IOValueSet::from_to(0, 1); - static constexpr auto HasXAFifoData = IOBitSet(2); - static constexpr auto IsParameterFifoEmpty = IOBitSet(3); - static constexpr auto HasParameterFifoSpace = IOBitSet(4); - static constexpr auto HasResponseFifoData = IOBitSet(5); - static constexpr auto HasDataFifoData = IOBitSet(6); - static constexpr auto IsTransmissionBusy = IOBitSet(7); + static constexpr auto PortIndex = BitRange::from_to(0, 1); + static constexpr auto HasXAFifoData = Bit(2); + static constexpr auto IsParameterFifoEmpty = Bit(3); + static constexpr auto HasParameterFifoSpace = Bit(4); + static constexpr auto HasResponseFifoData = Bit(5); + static constexpr auto HasDataFifoData = Bit(6); + static constexpr auto IsTransmissionBusy = Bit(7); ); __declare_io_type(InterruptEnable, uint8_t, - static constexpr auto InterruptTypValue = IOValueSet::from_to(0, 2); - static constexpr auto InterruptExtended = IOValueSet::from_to(0, 4); - static constexpr auto UnknownIRQ = IOBitSet(3); - static constexpr auto CommandStartIRQ = IOBitSet(4); + static constexpr auto InterruptTypValue = BitRange::from_to(0, 2); + static constexpr auto InterruptExtended = BitRange::from_to(0, 4); + static constexpr auto UnknownIRQ = Bit(3); + static constexpr auto CommandStartIRQ = Bit(4); ); typedef InterruptEnable_v InterruptFlag_v; __declare_io_type(Request, uint8_t, - static constexpr auto WantCommandStartIRQ = IOBitSet(5); - static constexpr auto WantData = IOBitSet(7); + static constexpr auto WantCommandStartIRQ = Bit(5); + static constexpr auto WantData = Bit(7); ); __declare_io_type(SoundMapCoding, uint8_t, - static constexpr auto Stereo = IOBitSet(0); + static constexpr auto Stereo = Bit(0); static constexpr auto Mono = !Stereo; - static constexpr auto SampleRate_18900hz = IOBitSet(2); + static constexpr auto SampleRate_18900hz = Bit(2); static constexpr auto SampleRate_37800hz = !SampleRate_18900hz; - static constexpr auto BitsPerSample8 = IOBitSet(4); + static constexpr auto BitsPerSample8 = Bit(4); static constexpr auto BitsPerSample4 = !BitsPerSample8; - static constexpr auto Emphasis = IOBitSet(6); + static constexpr auto Emphasis = Bit(6); ); __declare_io_type(AudioVolumeApply, uint8_t, - static constexpr auto Mute = IOBitSet(0); - static constexpr auto ApplyChanges = IOBitSet(5); + static constexpr auto Mute = Bit(0); + static constexpr auto ApplyChanges = Bit(5); ); __declare_io_type(ResponseFifo, uint8_t,); diff --git a/include/PSX/System/IOPorts/dma_io.hpp b/include/PSX/System/IOPorts/dma_io.hpp index 8af6a4f8..c17b54b9 100644 --- a/include/PSX/System/IOPorts/dma_io.hpp +++ b/include/PSX/System/IOPorts/dma_io.hpp @@ -5,18 +5,18 @@ namespace JabyEngine { namespace DMA_IO { __declare_io_type(MADR, uint32_t, - static constexpr auto MemoryAdr = IOValueSet::from_to(0, 23); + static constexpr auto MemoryAdr = BitRange::from_to(0, 23); ); __declare_io_type(BCR, uint32_t, - struct __no_align SyncMode0 { - static constexpr auto NumberOfWords = IOValueSet::from_to(0, 15); - static constexpr auto CD_OneBlock = IOBitSet(16); + struct SyncMode0 { + static constexpr auto NumberOfWords = BitRange::from_to(0, 15); + static constexpr auto CD_OneBlock = Bit(16); }; - struct SyncMode1 : public ComplexBitMap { - static constexpr auto BlockSize = IOValueSet::from_to(0, 15); - static constexpr auto BlockAmount = IOValueSet::from_to(16, 31); + struct SyncMode1 { + static constexpr auto BlockSize = BitRange::from_to(0, 15); + static constexpr auto BlockAmount = BitRange::from_to(16, 31); }; struct SyncMode2 { @@ -30,25 +30,25 @@ namespace JabyEngine { Sync2 = 2, //Linked List }; - static constexpr auto ManualStart = IOBitSet(28); + static constexpr auto ManualStart = Bit(28); - static constexpr auto Start = IOBitSet(24); + static constexpr auto Start = Bit(24); static constexpr auto Busy = Start; - static constexpr auto ChoppingCPUWindowSize = IOValueSet::from_to(20, 22); - static constexpr auto ChoppingDMAWindowSize = IOValueSet::from_to(16, 18); + static constexpr auto ChoppingCPUWindowSize = BitRange::from_to(20, 22); + static constexpr auto ChoppingDMAWindowSize = BitRange::from_to(16, 18); - static constexpr auto SyncMode = IOValueSet::from_to(9, 10); + static constexpr auto SyncMode = BitRange::from_to(9, 10); static constexpr auto UseSyncMode0 = SyncMode.with(Sync0); static constexpr auto UseSyncMode1 = SyncMode.with(Sync1); static constexpr auto UseSyncMode2 = SyncMode.with(Sync2); - static constexpr auto UseChopping = IOBitSet(8); + static constexpr auto UseChopping = Bit(8); - static constexpr auto MemoryAdrDecreaseBy4 = IOBitSet(1); + static constexpr auto MemoryAdrDecreaseBy4 = Bit(1); static constexpr auto MemoryAdrIncreaseBy4 = !MemoryAdrDecreaseBy4; - static constexpr auto FromMainRAM = IOBitSet(0); + static constexpr auto FromMainRAM = Bit(0); static constexpr auto ToMainRAM = !FromMainRAM; static constexpr Self StartMDECin() { @@ -105,34 +105,34 @@ namespace JabyEngine { static constexpr Priority LowestPriority = 7; __declare_io_type(DPCR, uint32_t, - static constexpr auto OTCEnable = IOBitSet(27); - static constexpr auto OTCPriority = IOValueSet::from_to(24, 26); + static constexpr auto OTCEnable = Bit(27); + static constexpr auto OTCPriority = BitRange::from_to(24, 26); - static constexpr auto PIOEnable = IOBitSet(23); - static constexpr auto PIOPriority = IOValueSet::from_to(20, 22); + static constexpr auto PIOEnable = Bit(23); + static constexpr auto PIOPriority = BitRange::from_to(20, 22); - static constexpr auto SPUEnable = IOBitSet(19); - static constexpr auto SPUPriority = IOValueSet::from_to(16, 18); + static constexpr auto SPUEnable = Bit(19); + static constexpr auto SPUPriority = BitRange::from_to(16, 18); - static constexpr auto CDROMEnable = IOBitSet(15); - static constexpr auto CDROMPriority = IOValueSet::from_to(12, 14); + static constexpr auto CDROMEnable = Bit(15); + static constexpr auto CDROMPriority = BitRange::from_to(12, 14); - static constexpr auto GPUEnable = IOBitSet(11); - static constexpr auto GPUPriority = IOValueSet::from_to(8, 10); + static constexpr auto GPUEnable = Bit(11); + static constexpr auto GPUPriority = BitRange::from_to(8, 10); - static constexpr auto MDECoutEnable = IOBitSet(7); - static constexpr auto MDECoutPriority = IOValueSet::from_to(4, 6); + static constexpr auto MDECoutEnable = Bit(7); + static constexpr auto MDECoutPriority = BitRange::from_to(4, 6); - static constexpr auto MDECinEnable = IOBitSet(3); - static constexpr auto MDECinPriority = IOValueSet::from_to(0, 2); + static constexpr auto MDECinEnable = Bit(3); + static constexpr auto MDECinPriority = BitRange::from_to(0, 2); ); __declare_io_type(DICR, uint32_t, - static constexpr auto MasterEnable = IOBitSet(31); - static constexpr auto Flags = IOValueSet::from_to(24, 30); - static constexpr auto MasterEnableDPCR = IOBitSet(23); - static constexpr auto EnableDPCR = IOValueSet::from_to(16, 22); - static constexpr auto ForceIRQ = IOBitSet(15); + static constexpr auto MasterEnable = Bit(31); + static constexpr auto Flags = BitRange::from_to(24, 30); + static constexpr auto MasterEnableDPCR = Bit(23); + static constexpr auto EnableDPCR = BitRange::from_to(16, 22); + static constexpr auto ForceIRQ = Bit(15); ); __declare_new_io_port(MDECin, 0x1F801080); diff --git a/include/PSX/System/IOPorts/gpu_io.hpp b/include/PSX/System/IOPorts/gpu_io.hpp index b4ede548..43d93dfb 100644 --- a/include/PSX/System/IOPorts/gpu_io.hpp +++ b/include/PSX/System/IOPorts/gpu_io.hpp @@ -53,12 +53,12 @@ namespace JabyEngine { PAL = 1, }; - static constexpr auto HorizontalResolution368 = IOBitSet(6); - static constexpr auto VerticalInterlace = IOBitSet(5); - static constexpr auto DisplayAreaColorDepth = IOValueSet::from_to(4, 4); - static constexpr auto VideoMode = IOValueSet::from_to(3, 3); - static constexpr auto VerticalResolution = IOValueSet::from_to(2, 2); - static constexpr auto HorizontalResolution = IOValueSet::from_to(0, 1); + static constexpr auto HorizontalResolution368 = Bit(6); + static constexpr auto VerticalInterlace = Bit(5); + static constexpr auto DisplayAreaColorDepth = BitRange::from_to(4, 4); + static constexpr auto VideoMode = BitRange::from_to(3, 3); + static constexpr auto VerticalResolution = BitRange::from_to(2, 2); + static constexpr auto HorizontalResolution = BitRange::from_to(0, 1); static constexpr Self PAL() { return Self::from( @@ -86,12 +86,12 @@ namespace JabyEngine { ); - struct Command { + struct Command { struct Helper { static constexpr GP0_t DrawAreaTemplate(uint8_t code, uint16_t x, uint16_t y) { - constexpr auto Command = IOValueSet::from_to(24, 31); - constexpr auto Y = IOValueSet::from_to(10, 18); - constexpr auto X = IOValueSet::from_to(0, 9); + constexpr auto Command = BitRange::from_to(24, 31); + constexpr auto Y = BitRange::from_to(10, 18); + constexpr auto X = BitRange::from_to(0, 9); return GP0_t::from(Command.with(code), Y.with(y), X.with(x)); } @@ -142,24 +142,24 @@ namespace JabyEngine { } static constexpr GP1_t DisplayArea(uint16_t x, uint16_t y) { - constexpr auto X = BitRange::from_to(0, 9); - constexpr auto Y = BitRange::from_to(10, 18); + constexpr auto X = BitRange::from_to(0, 9); + constexpr auto Y = BitRange::from_to(10, 18); - return {Helper::construct_cmd(0x05, ComplexBitMap::with(X.with(x), Y.with(y)).raw)}; + return {Helper::construct_cmd(0x05, X.as_value(x) | Y.as_value(y))}; } static constexpr GP1_t HorizontalDisplayRange(uint32_t x1, uint32_t x2) { - constexpr auto X1 = BitRange::from_to(0, 11); - constexpr auto X2 = BitRange::from_to(12, 23); + constexpr auto X1 = BitRange::from_to(0, 11); + constexpr auto X2 = BitRange::from_to(12, 23); - return {Helper::construct_cmd(0x06, ComplexBitMap::with(X1.with(x1), X2.with(x2)).raw)}; + return {Helper::construct_cmd(0x06, X1.as_value(x1) | X2.as_value(x2))}; } static constexpr GP1_t VerticalDisplayRange(uint32_t y1, uint32_t y2) { - constexpr auto Y1 = BitRange::from_to(0, 9); - constexpr auto Y2 = BitRange::from_to(10, 19); + constexpr auto Y1 = BitRange::from_to(0, 9); + constexpr auto Y2 = BitRange::from_to(10, 19); - return {Helper::construct_cmd(0x07, ComplexBitMap::with(Y1.with(y1), Y2.with(y2)).raw)}; + return {Helper::construct_cmd(0x07, Y1.as_value(y1) | Y2.as_value(y2))}; } static constexpr GP1_t DisplayMode(DisplayMode_t mode) { @@ -168,32 +168,32 @@ namespace JabyEngine { }; __declare_io_type(GPUSTAT, uint32_t, - static constexpr auto DrawingOddLinesInterlaced = IOBitSet(31); - static constexpr auto DMADirectionValue = IOValueSet::from_to(29, 30); - static constexpr auto DMAReady = IOBitSet(28); - static constexpr auto VRAMtoCPUtransferReay = IOBitSet(27); - static constexpr auto GP0ReadyForCMD = IOBitSet(26); - static constexpr auto FifoNotFull = IOBitSet(25); // Only for Fifo - static constexpr auto InterruptRequest = IOBitSet(24); - static constexpr auto DisplayDisabled = IOBitSet(23); - static constexpr auto VerticalInterlaceOn = IOBitSet(22); - static constexpr auto DisplayAreaColorDepth = IOValueSet::from_to(21, 21); - static constexpr auto VideoModePal = IOBitSet(20); - static constexpr auto VerticalResolutionValue = IOValueSet::from_to(19, 19); - static constexpr auto HorizontalResolutionValue = IOValueSet::from_to(17, 18); - static constexpr auto HorizontalResolution368 = IOBitSet(16); - static constexpr auto TexturesDisabled = IOBitSet(15); - static constexpr auto NotDrawingMaskedPixels = IOBitSet(12); - static constexpr auto MaskBitSetDuringDrawEnabled = IOBitSet(11); - static constexpr auto DrawingToDisplayAreadAllowed = IOBitSet(10); - static constexpr auto DitherEnabled = IOBitSet(9); - static constexpr auto TexturePageColorValue = IOValueSet::from_to(7, 8); - static constexpr auto SemiTransparencyValue = IOValueSet::from_to(5, 6); - static constexpr auto TexturePageY = IOValueSet::from_to(4, 4); // N*256 - static constexpr auto TexturePageX = IOValueSet::from_to(0, 3); // N*64 + static constexpr auto DrawingOddLinesInterlaced = Bit(31); + static constexpr auto DMADirectionValue = BitRange::from_to(29, 30); + static constexpr auto DMAReady = Bit(28); + static constexpr auto VRAMtoCPUtransferReay = Bit(27); + static constexpr auto GP0ReadyForCMD = Bit(26); + static constexpr auto FifoNotFull = Bit(25); // Only for Fifo + static constexpr auto InterruptRequest = Bit(24); + static constexpr auto DisplayDisabled = Bit(23); + static constexpr auto VerticalInterlaceOn = Bit(22); + static constexpr auto DisplayAreaColorDepth = BitRange::from_to(21, 21); + static constexpr auto VideoModePal = Bit(20); + static constexpr auto VerticalResolutionValue = BitRange::from_to(19, 19); + static constexpr auto HorizontalResolutionValue = BitRange::from_to(17, 18); + static constexpr auto HorizontalResolution368 = Bit(16); + static constexpr auto TexturesDisabled = Bit(15); + static constexpr auto NotDrawingMaskedPixels = Bit(12); + static constexpr auto MaskBitSetDuringDrawEnabled = Bit(11); + static constexpr auto DrawingToDisplayAreadAllowed = Bit(10); + static constexpr auto DitherEnabled = Bit(9); + static constexpr auto TexturePageColorValue = BitRange::from_to(7, 8); + static constexpr auto SemiTransparencyValue = BitRange::from_to(5, 6); + static constexpr auto TexturePageY = BitRange::from_to(4, 4); // N*256 + static constexpr auto TexturePageX = BitRange::from_to(0, 3); // N*64 - static constexpr auto VerticalResolution480 = IOBitSet(19); - static constexpr auto TexturePageY256 = IOBitSet(4); + static constexpr auto VerticalResolution480 = Bit(19); + static constexpr auto TexturePageY256 = Bit(4); ); typedef volatile uint32_t GPUREAD_v; diff --git a/include/PSX/System/IOPorts/interrupt_io.hpp b/include/PSX/System/IOPorts/interrupt_io.hpp index 4586d619..83683d39 100644 --- a/include/PSX/System/IOPorts/interrupt_io.hpp +++ b/include/PSX/System/IOPorts/interrupt_io.hpp @@ -4,17 +4,17 @@ namespace JabyEngine { struct Interrupt { - static constexpr auto VBlank = IOBitSet(0); - static constexpr auto GPU = IOBitSet(1); - static constexpr auto CDROM = IOBitSet(2); - static constexpr auto DMA = IOBitSet(3); - static constexpr auto Timer0 = IOBitSet(4); - static constexpr auto Timer1 = IOBitSet(5); - static constexpr auto Timer2 = IOBitSet(6); - static constexpr auto Periphery = IOBitSet(7); - static constexpr auto SIO = IOBitSet(8); - static constexpr auto SPU = IOBitSet(9); - static constexpr auto Controller = IOBitSet(10); + static constexpr auto VBlank = Bit(0); + static constexpr auto GPU = Bit(1); + static constexpr auto CDROM = Bit(2); + static constexpr auto DMA = Bit(3); + static constexpr auto Timer0 = Bit(4); + static constexpr auto Timer1 = Bit(5); + static constexpr auto Timer2 = Bit(6); + static constexpr auto Periphery = Bit(7); + static constexpr auto SIO = Bit(8); + static constexpr auto SPU = Bit(9); + static constexpr auto Controller = Bit(10); static constexpr auto LightPen = Controller; __declare_io_type(Status, uint32_t, @@ -26,19 +26,19 @@ namespace JabyEngine { __declare_new_io_port(Status, 0x1F801070); __declare_new_io_port(Mask, 0x1F801074); - static constexpr bool is_irq(IOBitSet irq) { + static constexpr bool is_irq(Bit irq) { return Status.is_set(irq); } - static constexpr void ack_irq(IOBitSet irq) { + static constexpr void ack_irq(Bit irq) { Status.clear(irq); } - static constexpr void disable_irq(IOBitSet irq) { + static constexpr void disable_irq(Bit irq) { Mask.clear(irq); } - static void enable_irq(IOBitSet irq) { + static void enable_irq(Bit irq) { Mask.set(irq); } }; diff --git a/include/PSX/System/IOPorts/ioport.hpp b/include/PSX/System/IOPorts/ioport.hpp index 6719a2c2..7daf0627 100644 --- a/include/PSX/System/IOPorts/ioport.hpp +++ b/include/PSX/System/IOPorts/ioport.hpp @@ -4,49 +4,6 @@ #include "../../Auxiliary/types.hpp" namespace JabyEngine { - struct IOBitUnset { - uint16_t pos; - - constexpr IOBitUnset(uint16_t bit_pos) : pos(bit_pos) { - } - }; - - struct IOBitSet { - uint16_t pos; - - constexpr IOBitSet(uint16_t bit_pos) : pos(bit_pos) { - } - - constexpr IOBitUnset operator!() const { - return IOBitUnset(this->pos); - } - }; - - struct IOValueSet { - template - using IOValueSetPair = pair; - - uint16_t pos; - uint16_t length; - - constexpr IOValueSet(uint16_t pos, uint16_t length) : pos(pos), length(length) { - } - - static constexpr IOValueSet from_to(uint16_t first_bit, uint16_t last_bit) { - return IOValueSet(first_bit, (last_bit - first_bit) + 1); - } - - template - constexpr IOValueSetPair with(T value) const { - return {*this, value}; - } - - template - constexpr IOValueSetPair range_max() const { - return IOValueSet::with((1 << this->length) - 1); - } - }; - namespace IOPort { struct IOValueType { template @@ -91,24 +48,24 @@ namespace JabyEngine { return Self().set_va(args...); \ } \ \ - constexpr Self& set(IOBitSet bit) { \ - this->raw_value = bit::set(this->raw_value, bit.pos); \ + constexpr Self& set(Bit bit) { \ + this->raw_value = bit::set(this->raw_value, bit); \ return *this; \ } \ \ - constexpr Self& set(IOBitUnset bit) { \ - this->raw_value = bit::clear(this->raw_value, bit.pos); \ + constexpr Self& set(ClearBit bit) { \ + this->raw_value = bit::set(this->raw_value, bit); \ return *this; \ } \ \ - constexpr Self& set(IOValueSet bits, UnderlyingValue value) { \ - this->raw_value = bit::value::set_normalized(this->raw_value, value, bits.pos, bits.length); \ + constexpr Self& set(BitRange bits, UnderlyingValue value) { \ + this->raw_value = bit::value::set_normalized(this->raw_value, bits, value); \ return *this; \ } \ \ template \ - constexpr Self& set(const IOValueSet::IOValueSetPair& value) { \ - this->set(value.first, static_cast(value.second)); \ + constexpr Self& set(const BitRange::RangeValuePair& value) { \ + this->raw_value = bit::value::set_normalized(this->raw_value, value); \ return *this; \ } \ \ @@ -122,17 +79,17 @@ namespace JabyEngine { return this->set(head).set_va(tail...); \ } \ \ - constexpr UnderlyingValue get(IOValueSet bits) const { \ + constexpr UnderlyingValue get(BitRange bits) const { \ return bit::value::get_normalized(this->raw_value, bits.pos, bits.length); \ } \ \ - constexpr Self& clear(IOBitSet bit) { \ - this->raw_value = bit::clear(this->raw_value, bit.pos); \ + constexpr Self& clear(Bit bit) { \ + this->raw_value = bit::clear(this->raw_value, bit); \ return *this; \ } \ \ - constexpr bool is_set(IOBitSet bit) const { \ - return bit::is_set(this->raw_value, bit.pos); \ + constexpr bool is_set(Bit bit) const { \ + return bit::is_set(this->raw_value, bit); \ } \ \ constexpr void operator=(UnderlyingValue value) { \ @@ -160,38 +117,6 @@ namespace JabyEngine { } }; - // For use with ComplexBitMaps or what else satisfies this API - template - struct VolatileBitMapPOD { - typedef typename T::UnderlyingType Raw; - - VolatilePOD pod; - - constexpr Raw read_raw() const { - return this->pod.read(); - } - - constexpr T read() const { - return T{this->pod.read()}; - } - - constexpr Raw read(const BitRange& range) const { - return VolatileBitMapPOD::read().get_value(range); - } - - constexpr void write_raw(Raw value) { - this->pod.write(value); - } - - constexpr void write(const T& value) { - this->pod.write(static_cast(value)); - } - - constexpr void write(const BitRangeValue& value) { - VolatileBitMapPOD::write(T{T::with(value)}); - } - }; - struct __no_align ubus32_t { __declare_io_type(uint16_t, uint16_t,); uint16_t_v low; diff --git a/include/PSX/System/IOPorts/spu_io.hpp b/include/PSX/System/IOPorts/spu_io.hpp index b5451779..e5450ccb 100644 --- a/include/PSX/System/IOPorts/spu_io.hpp +++ b/include/PSX/System/IOPorts/spu_io.hpp @@ -42,33 +42,33 @@ namespace JabyEngine { __declare_io_type(SweepVolume, int16_t, // For Volume Mode - static constexpr auto SweepEnable = IOBitSet(15); + static constexpr auto SweepEnable = Bit(15); static constexpr auto VolumeEnable = !SweepEnable; - static constexpr auto Volume = IOValueSet::from_to(0, 14); + static constexpr auto Volume = BitRange::from_to(0, 14); // For Sweep Mode - static constexpr auto SweepMode = IOBitSet(14); - static constexpr auto SweepDirection = IOBitSet(13); - static constexpr auto SweepPhase = IOBitSet(12); - static constexpr auto SweepShift = IOValueSet::from_to(2, 6); - static constexpr auto SweepStep = IOValueSet::from_to(0, 1); + static constexpr auto SweepMode = Bit(14); + static constexpr auto SweepDirection = Bit(13); + static constexpr auto SweepPhase = Bit(12); + static constexpr auto SweepShift = BitRange::from_to(2, 6); + static constexpr auto SweepStep = BitRange::from_to(0, 1); ); __declare_io_type(SR, uint16_t, - static constexpr auto SustainMode = IOBitSet(31 - 16); - static constexpr auto SustainDirection = IOBitSet(30 - 16); - static constexpr auto SustainShift = IOValueSet::from_to((24 - 16), (28 - 16)); - static constexpr auto SustainStep = IOValueSet::from_to((22 - 16), (23 - 16)); - static constexpr auto ReleaseMode = IOBitSet(21 - 16); - static constexpr auto ReleaseShift = IOValueSet::from_to((16 - 16), (20 - 16)); + static constexpr auto SustainMode = Bit(31 - 16); + static constexpr auto SustainDirection = Bit(30 - 16); + static constexpr auto SustainShift = BitRange::from_to((24 - 16), (28 - 16)); + static constexpr auto SustainStep = BitRange::from_to((22 - 16), (23 - 16)); + static constexpr auto ReleaseMode = Bit(21 - 16); + static constexpr auto ReleaseShift = BitRange::from_to((16 - 16), (20 - 16)); ); __declare_io_type(AD, uint16_t, - static constexpr auto AttackMode = IOBitSet(15); - static constexpr auto AttackShift = IOValueSet::from_to(10, 14); - static constexpr auto AttackStep = IOValueSet::from_to(8, 9); - static constexpr auto DecayShift = IOValueSet::from_to(4, 7); - static constexpr auto SustainLevel = IOValueSet::from_to(0, 3); + static constexpr auto AttackMode = Bit(15); + static constexpr auto AttackShift = BitRange::from_to(10, 14); + static constexpr auto AttackStep = BitRange::from_to(8, 9); + static constexpr auto DecayShift = BitRange::from_to(4, 7); + static constexpr auto SustainLevel = BitRange::from_to(0, 3); ); struct __no_align Voice_v { @@ -90,29 +90,29 @@ namespace JabyEngine { DMARead = 3 }; - static constexpr auto Enable = IOBitSet(15); - static constexpr auto Unmute = IOBitSet(14); - static constexpr auto NoiseFrequcenyShift = IOValueSet::from_to(10, 13); - static constexpr auto NoiseFrequcenyStep = IOValueSet::from_to(8, 9); - static constexpr auto ReverbMasterEnable = IOBitSet(7); - static constexpr auto IRQ9Enable = IOBitSet(6); - static constexpr auto TransferMode = IOValueSet::from_to(4, 5); - static constexpr auto ExternalAudioReverb = IOBitSet(3); - static constexpr auto CDAudioReverb = IOBitSet(2); - static constexpr auto ExternalAudioEnable = IOBitSet(1); - static constexpr auto CDAudioEnable = IOBitSet(0); + static constexpr auto Enable = Bit(15); + static constexpr auto Unmute = Bit(14); + static constexpr auto NoiseFrequcenyShift = BitRange::from_to(10, 13); + static constexpr auto NoiseFrequcenyStep = BitRange::from_to(8, 9); + static constexpr auto ReverbMasterEnable = Bit(7); + static constexpr auto IRQ9Enable = Bit(6); + static constexpr auto TransferMode = BitRange::from_to(4, 5); + static constexpr auto ExternalAudioReverb = Bit(3); + static constexpr auto CDAudioReverb = Bit(2); + static constexpr auto ExternalAudioEnable = Bit(1); + static constexpr auto CDAudioEnable = Bit(0); ); __declare_io_type(PMON, uint16_t, - static constexpr auto EnableBits = IOValueSet::from_to(1, 23); + static constexpr auto EnableBits = BitRange::from_to(1, 23); ); __declare_io_type(NON, uint16_t, - static constexpr auto NoiseBits = IOValueSet::from_to(0, 23); + static constexpr auto NoiseBits = BitRange::from_to(0, 23); ); __declare_io_type(EON, uint16_t, - static constexpr auto EchoBits = IOValueSet::from_to(0, 23); + static constexpr auto EchoBits = BitRange::from_to(0, 23); ); static constexpr size_t VoiceCount = 24; diff --git a/include/PSX/System/IOPorts/timer_io.hpp b/include/PSX/System/IOPorts/timer_io.hpp index 3962f3be..eadd5d44 100644 --- a/include/PSX/System/IOPorts/timer_io.hpp +++ b/include/PSX/System/IOPorts/timer_io.hpp @@ -5,28 +5,28 @@ namespace JabyEngine { namespace Timer_IO { __declare_io_type(CounterMode, uint32_t, - static constexpr auto SyncEnable = IOBitSet(0); + static constexpr auto SyncEnable = Bit(0); static constexpr auto FreeRun = !SyncEnable; - static constexpr auto SyncMode = IOValueSet::from_to(1, 2); - static constexpr auto ResetAfterTarget = IOBitSet(3); - static constexpr auto IRQAtTarget = IOBitSet(4); - static constexpr auto IRQAtMax = IOBitSet(5); - static constexpr auto IRQEveryTime = IOBitSet(6); + static constexpr auto SyncMode = BitRange::from_to(1, 2); + static constexpr auto ResetAfterTarget = Bit(3); + static constexpr auto IRQAtTarget = Bit(4); + static constexpr auto IRQAtMax = Bit(5); + static constexpr auto IRQEveryTime = Bit(6); static constexpr auto IRQOneShot = !IRQEveryTime; - static constexpr auto IRQToggle = IOBitSet(7); + static constexpr auto IRQToggle = Bit(7); static constexpr auto IRQPulse = !IRQToggle; - static constexpr auto ClockSource = IOValueSet::from_to(8, 9); - static constexpr auto HasIRQRequest = IOBitSet(10); - static constexpr auto IsTargetReached = IOBitSet(11); - static constexpr auto IsMaxReached = IOBitSet(12); + static constexpr auto ClockSource = BitRange::from_to(8, 9); + static constexpr auto HasIRQRequest = Bit(10); + static constexpr auto IsTargetReached = Bit(11); + static constexpr auto IsMaxReached = Bit(12); ); __declare_io_type(CounterTarget, uint32_t, - static constexpr auto CounterTargetValue = IOValueSet::from_to(0, 15); + static constexpr auto CounterTargetValue = BitRange::from_to(0, 15); ); __declare_io_type(CounterValue, uint32_t, - static constexpr auto Value = IOValueSet::from_to(0, 15); + static constexpr auto Value = BitRange::from_to(0, 15); ); struct __no_align Counter { diff --git a/src/Library/src/CD/cd.cpp b/src/Library/src/CD/cd.cpp index d6497a07..6edba049 100644 --- a/src/Library/src/CD/cd.cpp +++ b/src/Library/src/CD/cd.cpp @@ -7,19 +7,19 @@ namespace JabyEngine { namespace CD { namespace internal { - struct Mode : public ComplexBitMap { - static constexpr auto DoubleSpeed = Bit(7); + __declare_io_type(Mode, uint8_t, + static constexpr auto DoubleSpeed = Bit(7); static constexpr auto SingleSpeed = !DoubleSpeed; - static constexpr auto XADPCM = Bit(6); - static constexpr auto WholeSector = Bit(5); + static constexpr auto XADPCM = Bit(6); + static constexpr auto WholeSector = Bit(5); static constexpr auto DataSector = !WholeSector; - static constexpr auto UseXAFilter = Bit(3); - static constexpr auto AudioPlayIRQ = Bit(2); - static constexpr auto AutoPauseTrack = Bit(1); - static constexpr auto CDDA = Bit(0); - }; + static constexpr auto UseXAFilter = Bit(3); + static constexpr auto AudioPlayIRQ = Bit(2); + static constexpr auto AutoPauseTrack = Bit(1); + static constexpr auto CDDA = Bit(0); + ); - static constexpr auto DataSectorMode = Mode::with(Mode::DoubleSpeed, Mode::DataSector); + static constexpr auto DataSectorMode = Mode_t::from(Mode_t::DoubleSpeed, Mode_t::DataSector); static SectorBufferAllocator sector_allocator; static uint16_t sectors_left; diff --git a/src/Library/src/startup.cpp b/src/Library/src/startup.cpp index cb3e9fae..b99bcf79 100644 --- a/src/Library/src/startup.cpp +++ b/src/Library/src/startup.cpp @@ -10,7 +10,7 @@ namespace JabyEngine { void start() { NextRoutine next_routine = JabyEngine::NextRoutine::from(boot::Start::setup); - printf("Starting Planschbecken 0x%p\n", next_routine.value); + printf("Starting Planschbecken Version 0 0x%p\n", next_routine.value); while(true) { if(next_routine.is_null()) { break;