#ifndef __JABYENGINE_IOPORT_HPP__ #define __JABYENGINE_IOPORT_HPP__ #include "../../Auxiliary/complex_bitmap.hpp" #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 struct NormalValue { typedef T Value; }; template struct VolatileValue { typedef volatile T Value; }; #define __declare_new_io_port(name, adr) \ typedef name##_io_base name##_v; \ typedef name##_io_base name##_t; \ static auto& name = *reinterpret_cast(adr) #define __declare_io_type(name, type, ...) \ template typename T> \ struct name##_io_base { \ T::Value raw_value; \ \ name##_io_base(type value) : raw_value(value) {} \ __VA_ARGS__ \ \ void set(IOBitSet bit) { \ this->raw_value = bit::set(this->raw_value, bit.pos); \ } \ \ void set(IOBitUnset bit) { \ this->raw_value = bit::clear(this->raw_value, bit.pos); \ } \ \ void set(IOValueSet bits, type value) { \ this->raw_value = bit::value::set_normalized(this->raw_value, value, bits.pos, bits.length); \ } \ \ void set(const IOValueSet::IOValueSetPair& value) { \ this->set(value.first, value.second); \ } \ \ type get(IOValueSet bits) const { \ return bit::value::get_normalized(this->raw_value, bits.pos, bits.length); \ } \ \ \ void clear(IOBitSet bit) { \ this->raw_value = bit::clear(this->raw_value, bit.pos); \ } \ \ \ bool is_set(IOBitSet bit) const { \ return bit::is_set(this->raw_value, bit.pos); \ } \ \ \ void operator=(type value) { \ this->raw_value = value; \ } \ \ type operator*() const { \ return this->raw_value; \ } \ } template struct __attribute__((deprecated)) VolatilePOD { volatile T raw; constexpr T read() const { return this->raw; } constexpr void write(T value) { this->raw = value; } }; // For use with ComplexBitMaps or what else satisfies this API template struct __attribute__((deprecated)) 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 { typedef ComplexBitMap Base16; VolatileBitMapPOD low; VolatileBitMapPOD high; constexpr ubus32_t(uint32_t value) { *this = value; } constexpr void write(uint32_t value) { *this = value; } constexpr operator uint32_t() const { return ((this->high.read_raw() << 16) | this->low.read_raw()); } constexpr ubus32_t& operator=(uint32_t value) { this->low.write_raw(value & 0xFFFF); this->high.write_raw(value >> 16); return *this; } }; static constexpr uintptr_t IO_Base_Mask = 0xF0000000; static constexpr uintptr_t IO_Base_Adr = 0x10000000; #define __io_port_adr(adr) (IO_Base_Adr + (adr & ~IO_Base_Mask)) #define __cast_io_adr_with_type(cv, type, name, adr) static __always_inline cv auto& name = *reinterpret_cast(__io_port_adr(adr)) #define __declare_io_port_global(type, name, adr) __cast_io_adr_with_type(, VolatileBitMapPOD, name, adr) #define __declare_io_port_global_const(type, name, adr) __cast_io_adr_with_type(const, VolatileBitMapPOD, name, adr) #define __declare_io_port_global_simple(type, name, adr) __cast_io_adr_with_type(, VolatilePOD, name, adr) #define __declare_io_port_global_const_simple(type, name, adr) __cast_io_adr_with_type(const, VolatilePOD, name, adr) #define __declare_io_port_member(type, name, adr) __cast_io_adr_with_type(inline, VolatileBitMapPOD, name, adr) #define __declare_io_port_member_const(type, name, adr) __cast_io_adr_with_type(const inline, VolatileBitMapPOD, name, adr) #define __declare_io_port_member_simple(type, name, adr) __cast_io_adr_with_type(inline, VolatilePOD, name, adr) #define __declare_io_port_member_const_simple(type, name, adr) __cast_io_adr_with_type(const inline, VolatilePOD, name, adr) #define __declare_io_port_global_array(type, name, adr, size) static __always_inline auto& name = reinterpret_cast(*reinterpret_cast(__io_port_adr(adr))) #define __declare_io_port_global_struct(type, name, adr) static __always_inline auto& name = *reinterpret_cast(__io_port_adr(adr)) } #endif //!__JABYENGINE_IOPORT_HPP__