205 lines
7.1 KiB
C++
205 lines
7.1 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_io_port(name, adr) \
|
|
typedef name##_io_base<VolatileValue> name##_v; \
|
|
typedef name##_io_base<NormalValue> name##_t; \
|
|
static auto& name = *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; \
|
|
\
|
|
constexpr name##_io_base() = default; \
|
|
\
|
|
constexpr name##_io_base(type value) : raw_value(value) {} \
|
|
__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; \
|
|
} \
|
|
\
|
|
constexpr name##_io_base& set(const IOValueSet::IOValueSetPair<type>& value) { \
|
|
this->set(value.first, value.second); \
|
|
return *this; \
|
|
} \
|
|
\
|
|
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; \
|
|
} \
|
|
\
|
|
\
|
|
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<typename T>
|
|
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<typename T>
|
|
struct __attribute__((deprecated)) 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 {
|
|
typedef ComplexBitMap<uint16_t> Base16;
|
|
|
|
VolatileBitMapPOD<Base16> low;
|
|
VolatileBitMapPOD<Base16> 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<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__
|