#ifndef __JABYENGINE_IOPORT_HPP__ #define __JABYENGINE_IOPORT_HPP__ #include "../../Auxiliary/types.hpp" #include "../../Auxiliary/bits.hpp" namespace JabyEngine { namespace New { namespace internal { template struct IOValue { typedef S UnderlyingType; UnderlyingType raw; template static constexpr T from(const ARGS&...args) { return T{0}.set2(args...); } constexpr T& set2(Bit bit) { this->raw = bit::set(this->raw, bit); return static_cast(*this); } constexpr T& set2(ClearBit bit) { this->raw = bit::set(this->raw, bit); return static_cast(*this); } constexpr T& set_range(const BitRange& bits, UnderlyingType value) { this->raw = bit::value::set_normalized(this->raw, bits, value); return static_cast(*this); } template constexpr T& set2(const BitRange::RangeValuePair& value) { this->raw = bit::value::set_normalized(this->raw, value); return static_cast(*this); } template constexpr T& set2(const U& head, const ARGS&...tail) { return this->set2(head).set2(tail...); } constexpr IOValue::UnderlyingType get2(BitRange bits) const { return bit::value::get_normalized(this->raw, bits.pos, bits.length); } constexpr T& clear2(Bit bit) { this->raw = bit::clear(this->raw, bit); return static_cast(*this); } constexpr bool is_set2(Bit bit) const { return bit::is_set(this->raw, bit); } }; } struct ubus32_t { uint16_t low; uint16_t high; static ubus32_t from(uint32_t value) { return {.low = static_cast(value & 0xFFFF), .high = static_cast(value >> 16)}; } }; template struct IOPort { T value; T read() const { return {const_cast*>(this)->value.raw}; } void write(T value) { const_cast*>(this)->value.raw = value.raw; } }; template<> struct IOPort { ubus32_t value; ubus32_t read() const { auto*const cv_this = const_cast*>(this); return {.low = cv_this->value.low, .high = cv_this->value.high}; } void write(ubus32_t value) { auto*const cv_this = const_cast*>(this); cv_this->value.low = value.low; cv_this->value.high = value.high; } void write(uint32_t value) { IOPort::write(ubus32_t::from(value)); } }; #define __new_declare_io_value(name, type) struct name : public ::JabyEngine::New::internal::IOValue #define __new_declare_value_at(cv, type, name, adr) static cv auto& name = *reinterpret_cast(IOAdress::patch_adr(adr)) #define __new_declare_array_at(cv, type, name, size, adr) static cv auto& name = reinterpret_cast(*reinterpret_cast(IOAdress::patch_adr(adr))) #define __new_declare_io_port_w_type(cv, type, name, adr) __new_declare_value_at(cv, ::JabyEngine::New::IOPort, name, adr) #define __new_declare_io_port(cv, name, adr) __new_declare_io_port_w_type(cv, struct name, name, adr) #define __new_declare_io_port_array(cv, name, size, adr) __new_declare_array_at(cv, struct name, name, size, adr) } namespace IOPort { struct IOValueType { template struct Normal { typedef T Value; typedef T UnderlyingValue; }; template struct Volatile { typedef volatile T Value; typedef T UnderlyingValue; }; }; } namespace IOAdress { constexpr uintptr_t patch_adr(uintptr_t adr) { constexpr uintptr_t Mask = 0xF0000000; constexpr uintptr_t Base = 0x10000000; // We might want to change this later to 0xB0000000 for caching and stuff (More research needed) return (Base + (adr & ~Mask)); } } #define __declare_new_named_io_port(type, name, adr) \ static inline auto& name = *reinterpret_cast(IOAdress::patch_adr(adr)) #define __declare_new_io_port(name, adr) \ __declare_new_named_io_port(name, name, adr) #define __declare_new_const_io_port(name, adr) \ __declare_new_named_io_port(const name, name, adr) #define __declare_new_io_port_array(name, adr, size) \ static inline auto& name = reinterpret_cast(*reinterpret_cast(adr)) // We need this construct to ensure the types end up being a POD - Inheritance doesn't qualify for a POD #define __declare_io_type(name, type, ...) \ template typename T> \ struct name##_io_base { \ typedef T::UnderlyingValue UnderlyingValue; \ typedef name##_io_base Self; \ \ T::Value raw_value = 0; \ \ __VA_ARGS__ \ \ template \ static constexpr Self from(const ARGS&...args) { \ return Self().set_va(args...); \ } \ \ constexpr Self& set(Bit bit) { \ this->raw_value = bit::set(this->raw_value, bit); \ return *this; \ } \ \ constexpr Self& set(ClearBit bit) { \ this->raw_value = bit::set(this->raw_value, bit); \ return *this; \ } \ \ 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 BitRange::RangeValuePair& value) { \ this->raw_value = bit::value::set_normalized(this->raw_value, value); \ return *this; \ } \ \ template \ constexpr Self& set_va(const S& head) { \ return this->set(head); \ } \ \ template \ constexpr Self& set_va(const S& head, const ARGS&...tail) { \ return this->set(head).set_va(tail...); \ } \ \ constexpr UnderlyingValue get(BitRange bits) const { \ return bit::value::get_normalized(this->raw_value, bits.pos, bits.length); \ } \ \ constexpr Self& clear(Bit bit) { \ this->raw_value = bit::clear(this->raw_value, bit); \ return *this; \ } \ \ constexpr bool is_set(Bit bit) const { \ return bit::is_set(this->raw_value, bit); \ } \ \ constexpr void operator=(UnderlyingValue value) { \ this->raw_value = value; \ } \ \ constexpr operator UnderlyingValue() const { \ return this->raw_value; \ } \ }; \ \ typedef name##_io_base name##_v; \ typedef name##_io_base name##_t #pragma pack(push, 1) struct ubus32_t { __declare_io_type(uint16_t, uint16_t,); uint16_t_v low; uint16_t_v high; constexpr ubus32_t(uint32_t value) : low{0}, high{0} { *this = value; } constexpr void write(uint32_t value) { *this = value; } constexpr operator uint32_t() const { const uint32_t high = this->high; const uint32_t low = this->low; return ((high << 16) | low); } constexpr ubus32_t& operator=(uint32_t value) { this->low = (value & 0xFFFF); this->high = (value >> 16); return *this; } }; #pragma pack(pop) } #endif //!__JABYENGINE_IOPORT_HPP__