227 lines
8.0 KiB
C++
227 lines
8.0 KiB
C++
#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<typename T>
|
|
using IOValueSetPair = pair<IOValueSet, T>;
|
|
|
|
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<typename T>
|
|
constexpr IOValueSetPair<T> with(T value) const {
|
|
return {*this, value};
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
struct NormalValue {
|
|
typedef T Value;
|
|
};
|
|
|
|
template<typename T>
|
|
struct VolatileValue {
|
|
typedef volatile T Value;
|
|
};
|
|
|
|
#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, ...) \
|
|
template<template<typename> typename T> \
|
|
struct name##_io_base { \
|
|
T<type>::Value raw_value = 0; \
|
|
typedef name##_io_base Self; \
|
|
\
|
|
__VA_ARGS__ \
|
|
template<typename...ARGS> \
|
|
static constexpr name##_io_base from(const ARGS&...args) { \
|
|
return name##_io_base().set_va(args...); \
|
|
} \
|
|
constexpr name##_io_base& set(IOBitSet bit) { \
|
|
this->raw_value = bit::set(this->raw_value, bit.pos); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
constexpr name##_io_base& set(IOBitUnset bit) { \
|
|
this->raw_value = bit::clear(this->raw_value, bit.pos); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
constexpr name##_io_base& set(IOValueSet bits, type value) { \
|
|
this->raw_value = bit::value::set_normalized(this->raw_value, value, bits.pos, bits.length); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
template<typename S> \
|
|
constexpr name##_io_base& set(const IOValueSet::IOValueSetPair<S>& value) { \
|
|
this->set(value.first, static_cast<type>(value.second)); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
template<typename S> \
|
|
constexpr name##_io_base& set_va(const S& head) { \
|
|
return this->set(head); \
|
|
} \
|
|
template<typename S, typename...ARGS> \
|
|
constexpr name##_io_base& set_va(const S& head, const ARGS&...tail) { \
|
|
return this->set(head).set_va(tail...); \
|
|
} \
|
|
constexpr type get(IOValueSet bits) const { \
|
|
return bit::value::get_normalized(this->raw_value, bits.pos, bits.length); \
|
|
} \
|
|
\
|
|
\
|
|
constexpr name##_io_base& clear(IOBitSet bit) { \
|
|
this->raw_value = bit::clear(this->raw_value, bit.pos); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
\
|
|
constexpr bool is_set(IOBitSet bit) const { \
|
|
return bit::is_set(this->raw_value, bit.pos); \
|
|
} \
|
|
\
|
|
\
|
|
constexpr void operator=(type value) { \
|
|
this->raw_value = value; \
|
|
} \
|
|
\
|
|
constexpr type operator*() const { \
|
|
return this->raw_value; \
|
|
} \
|
|
}; \
|
|
\
|
|
typedef name##_io_base<VolatileValue> name##_v; \
|
|
typedef name##_io_base<NormalValue> 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;
|
|
}
|
|
};
|
|
|
|
// For use with ComplexBitMaps or what else satisfies this API
|
|
template<typename T>
|
|
struct VolatileBitMapPOD {
|
|
typedef typename T::UnderlyingType Raw;
|
|
|
|
VolatilePOD<Raw> pod;
|
|
|
|
constexpr Raw read_raw() const {
|
|
return this->pod.read();
|
|
}
|
|
|
|
constexpr T read() const {
|
|
return T{this->pod.read()};
|
|
}
|
|
|
|
constexpr Raw read(const BitRange<Raw>& range) const {
|
|
return VolatileBitMapPOD<T>::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<Raw>(value));
|
|
}
|
|
|
|
constexpr void write(const BitRangeValue<Raw>& value) {
|
|
VolatileBitMapPOD<T>::write(T{T::with(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__
|