jabyengine/include/PSX/System/IOPorts/ioport.hpp

119 lines
4.4 KiB
C++

#ifndef __JABYENGINE_IOPORT_HPP__
#define __JABYENGINE_IOPORT_HPP__
#include "../../Auxiliary/complex_bitmap.hpp"
namespace JabyEngine {
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 value; \
\
__VA_ARGS__ \
}
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__