Add new IOPort design
This commit is contained in:
@@ -3,94 +3,82 @@
|
||||
#include "../../Auxiliary/complex_bitmap.hpp"
|
||||
|
||||
namespace JabyEngine {
|
||||
template<typename T>
|
||||
template<typename T, typename S/* = T*/>
|
||||
class __no_align IOPort {
|
||||
private:
|
||||
T value;
|
||||
volatile T value;
|
||||
|
||||
public:
|
||||
// decltype(this) instead of IOPort<T>* lead to wrong runtime behaviour somethow...
|
||||
constexpr T read() const {
|
||||
return const_cast<const volatile IOPort<T>*>(this)->value;
|
||||
constexpr T read_raw() const {
|
||||
return this->value;
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
constexpr S read(const BitRange<S>& range) const {
|
||||
constexpr S read() const {
|
||||
return S{this->value};
|
||||
}
|
||||
|
||||
constexpr T read_range_value(const BitRange<T>& range) const {
|
||||
return IOPort::read().get_value(range);
|
||||
}
|
||||
|
||||
template<typename...ARGS>
|
||||
constexpr void write_combined(const ARGS&... value) {
|
||||
IOPort::write(T::with(value...));
|
||||
constexpr void write_raw(T value) {
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
template<typename S>
|
||||
constexpr void write(const BitRangeValue<S>& value) {
|
||||
IOPort::write(T::with(value));
|
||||
constexpr void write(const S& value) {
|
||||
this->value = static_cast<T>(value);
|
||||
}
|
||||
|
||||
constexpr void write(const T& value) {
|
||||
const_cast<volatile IOPort<T>*>(this)->value = value;
|
||||
constexpr void write_range_value(const BitRangeValue<T>& value) {
|
||||
IOPort<T, S>::write(S{S::with(value)});
|
||||
}
|
||||
|
||||
// We keep this a POD so we will not add assignment operators anymore
|
||||
// We also removed ref() to be more percise
|
||||
};
|
||||
|
||||
struct __no_align ubus32_t {
|
||||
ComplexBitMap<uint16_t> low;
|
||||
ComplexBitMap<uint16_t> high;
|
||||
typedef ComplexBitMap<uint16_t> Base16;
|
||||
|
||||
IOPort<Base16::UnderlyingType, Base16> low;
|
||||
IOPort<Base16::UnderlyingType, Base16> high;
|
||||
|
||||
constexpr ubus32_t(uint32_t value) {
|
||||
*this = value;
|
||||
}
|
||||
|
||||
constexpr operator uint32_t() const {
|
||||
return ((this->high.raw << 16) | this->low.raw);
|
||||
constexpr void write(uint32_t value) {
|
||||
*this = value;
|
||||
}
|
||||
|
||||
operator uint32_t() const volatile {
|
||||
return ((this->high.raw << 16) | this->low.raw);
|
||||
constexpr operator uint32_t() const {
|
||||
return ((this->high.read_raw() << 16) | this->low.read_raw());
|
||||
}
|
||||
|
||||
constexpr ubus32_t& operator=(uint32_t value) {
|
||||
this->low.raw = (value & 0xFFFF);
|
||||
this->high.raw = (value >> 16);
|
||||
this->low.write_raw(value & 0xFFFF);
|
||||
this->high.write_raw(value >> 16);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr void operator=(uint32_t value) volatile {
|
||||
this->low.raw = (value & 0xFFFF);
|
||||
this->high.raw = (value >> 16);
|
||||
}
|
||||
};
|
||||
static constexpr uintptr_t IO_Base_Mask = 0xF0000000;
|
||||
static constexpr uintptr_t IO_Base_Adr = 0x10000000;
|
||||
|
||||
#define __declare_global_raw(cv, type, name, adr) static __always_inline cv auto& name = *reinterpret_cast<type*>(__io_port_adr(adr))
|
||||
|
||||
#define __io_port_adr(adr) (IO_Base_Adr + (adr & ~IO_Base_Mask))
|
||||
#define __declare_io_port_global_raw(cv, type, name, adr) static __always_inline cv auto& name = *reinterpret_cast<IOPort<type>*>(__io_port_adr(adr))
|
||||
|
||||
#define __declare_io_port_global(type, name, adr) __declare_io_port_global_raw(, type, name, adr)
|
||||
#define __declare_io_port_global_const(type, name, adr) __declare_io_port_global_raw(const, type, name, adr)
|
||||
#define __declare_io_port_global_raw(cv, type, sub_type, name, adr) __declare_global_raw(cv, __collect(IOPort<type, sub_type>), name, adr)
|
||||
|
||||
#define __declare_io_port_member(type, name, adr) __declare_io_port_global_raw(inline, type, name, adr)
|
||||
#define __declare_io_port_member_const(type, name, adr) __declare_io_port_global_raw(const inline, type, name, adr)
|
||||
#define __declare_io_port_global(type, name, adr) __declare_io_port_global_raw(, type::UnderlyingType, type, name, adr)
|
||||
#define __declare_io_port_global_const(type, name, adr) __declare_io_port_global_raw(const, type::UnderlyingType, type, name, adr)
|
||||
#define __declare_io_port_global_simple(type, name, adr) __declare_io_port_global_raw(, type, type, name, adr)
|
||||
#define __declare_io_port_global_const_simple(type, name, adr) __declare_io_port_global_raw(const, type, type, name, adr)
|
||||
|
||||
#define __declare_io_port_member(type, name, adr) __declare_io_port_global_raw(inline, type::UnderlyingType, type, name, adr)
|
||||
#define __declare_io_port_member_const(type, name, adr) __declare_io_port_global_raw(const inline, type::UnderlyingType, type, name, adr)
|
||||
#define __declare_io_port_member_simple(type, name, adr) __declare_io_port_global_raw(inline, type, type, name, adr)
|
||||
#define __declare_io_port_member_const_simple(type, name, adr) __declare_io_port_global_raw(const inline, type, type, name, adr)
|
||||
|
||||
#define __declare_io_port_global_array(type, name, adr, size) static __always_inline auto& name = reinterpret_cast<type(&)[size]>(*reinterpret_cast<type*>((IO_Base_Adr + (adr & ~IO_Base_Mask))))
|
||||
#define __declare_io_port_global_struct(type, name, adr) static __always_inline auto& name = *reinterpret_cast<type*>(__io_port_adr(adr))
|
||||
|
||||
#define __io_port_inherit_complex_bit_map(name) \
|
||||
constexpr __always_inline name() = default; \
|
||||
constexpr __always_inline name(ComplexBitMap value) : ComplexBitMap(value) { \
|
||||
} \
|
||||
template<typename...ARGS> \
|
||||
static constexpr __always_inline name with(ARGS...args) { \
|
||||
return {ComplexBitMap::with(args...)}; \
|
||||
}\
|
||||
template<typename T> \
|
||||
constexpr void __always_inline operator=(ComplexBitMap<T> value) volatile { \
|
||||
this->raw = value.raw; \
|
||||
}
|
||||
}
|
||||
#endif //!__JABYENGINE_IOPORT_HPP__
|
||||
Reference in New Issue
Block a user