167 lines
12 KiB
C++
167 lines
12 KiB
C++
#ifndef __JABYENGINE_IOPORT_HPP__
|
|
#define __JABYENGINE_IOPORT_HPP__
|
|
#include "../../Auxiliary/types.hpp"
|
|
#include "../../Auxiliary/bits.hpp"
|
|
|
|
namespace JabyEngine {
|
|
namespace IOPort {
|
|
struct IOValueType {
|
|
template<typename T>
|
|
struct Normal {
|
|
typedef T Value;
|
|
typedef T UnderlyingValue;
|
|
};
|
|
|
|
template<typename T>
|
|
struct Volatile {
|
|
typedef volatile T Value;
|
|
typedef T UnderlyingValue;
|
|
};
|
|
};
|
|
}
|
|
|
|
#define __declare_new_named_io_port(type, name, adr) \
|
|
static inline auto& name = *reinterpret_cast<type##_v*>(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<name##_v(&)[size]>(*reinterpret_cast<name##_v*>(adr))
|
|
|
|
#define __declare_io_type(name, type, ...) \
|
|
/*We need this type to be a POD sadly*/ \
|
|
template<template<typename> typename T> \
|
|
struct name##_io_base { \
|
|
typedef T<type>::UnderlyingValue UnderlyingValue; \
|
|
typedef name##_io_base Self; \
|
|
\
|
|
T<type>::Value raw_value = 0; \
|
|
\
|
|
__VA_ARGS__ \
|
|
\
|
|
template<typename...ARGS> \
|
|
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<typename S> \
|
|
constexpr Self& set(const BitRange::RangeValuePair<S>& value) { \
|
|
this->raw_value = bit::value::set_normalized(this->raw_value, value); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
template<typename S> \
|
|
constexpr Self& set_va(const S& head) { \
|
|
return this->set(head); \
|
|
} \
|
|
\
|
|
template<typename S, typename...ARGS> \
|
|
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<IOPort::IOValueType::Volatile> name##_v; \
|
|
typedef name##_io_base<IOPort::IOValueType::Normal> name##_t
|
|
|
|
template<typename T>
|
|
struct VolatilePOD {
|
|
volatile T raw;
|
|
|
|
constexpr T read() const {
|
|
return this->raw;
|
|
}
|
|
|
|
constexpr void write(T value) {
|
|
this->raw = value;
|
|
}
|
|
};
|
|
|
|
struct __no_align 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;
|
|
}
|
|
};
|
|
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<type*>(__io_port_adr(adr))
|
|
|
|
|
|
#define __declare_io_port_global(type, name, adr) __cast_io_adr_with_type(, VolatileBitMapPOD<type>, name, adr)
|
|
#define __declare_io_port_global_const(type, name, adr) __cast_io_adr_with_type(const, VolatileBitMapPOD<type>, name, adr)
|
|
#define __declare_io_port_global_simple(type, name, adr) __cast_io_adr_with_type(, VolatilePOD<type>, name, adr)
|
|
#define __declare_io_port_global_const_simple(type, name, adr) __cast_io_adr_with_type(const, VolatilePOD<type>, name, adr)
|
|
|
|
#define __declare_io_port_member(type, name, adr) __cast_io_adr_with_type(inline, VolatileBitMapPOD<type>, name, adr)
|
|
#define __declare_io_port_member_const(type, name, adr) __cast_io_adr_with_type(const inline, VolatileBitMapPOD<type>, name, adr)
|
|
#define __declare_io_port_member_simple(type, name, adr) __cast_io_adr_with_type(inline, VolatilePOD<type>, name, adr)
|
|
#define __declare_io_port_member_const_simple(type, name, adr) __cast_io_adr_with_type(const inline, VolatilePOD<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_port_adr(adr)))
|
|
#define __declare_io_port_global_struct(type, name, adr) static __always_inline auto& name = *reinterpret_cast<type*>(__io_port_adr(adr))
|
|
}
|
|
#endif //!__JABYENGINE_IOPORT_HPP__
|