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

90 lines
3.5 KiB
C++

#ifndef __JABYENGINE_IOPORT_HPP__
#define __JABYENGINE_IOPORT_HPP__
#include "../../Auxiliary/complex_bitmap.hpp"
namespace JabyEngine {
template<typename T, typename S = T>
class __no_align IOPort {
private:
volatile T value;
public:
constexpr T read_raw() const {
return this->value;
}
constexpr S read() const {
return S{this->value};
}
constexpr void write_raw(T value) {
this->value = value;
}
constexpr void write(const S& value) {
this->value = static_cast<T>(value);
}
};
// For use with ComplexBitMaps or what else satisfies this API
template<typename T>
class __no_align IOPortEx : public IOPort<typename T::UnderlyingType, T> {
private:
typedef typename T::UnderlyingType Raw;
public:
constexpr Raw read_range_value(const BitRange<Raw>& range) const {
return IOPort<Raw, T>::read().get_value(range);
}
constexpr void write_range_value(const BitRangeValue<Raw>& value) {
IOPort<Raw, T>::write(T{T::with(value)});
}
};
struct __no_align ubus32_t {
typedef ComplexBitMap<uint16_t> Base16;
IOPort<Base16::UnderlyingType, Base16> low;
IOPort<Base16::UnderlyingType, 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(, IOPortEx<type>, name, adr)
#define __declare_io_port_global_const(type, name, adr) __cast_io_adr_with_type(const, IOPortEx<type>, name, adr)
#define __declare_io_port_global_simple(type, name, adr) __cast_io_adr_with_type(, IOPort<type>, name, adr)
#define __declare_io_port_global_const_simple(type, name, adr) __cast_io_adr_with_type(const, IOPort<type>, name, adr)
#define __declare_io_port_member(type, name, adr) __cast_io_adr_with_type(inline, IOPortEx<type>, name, adr)
#define __declare_io_port_member_const(type, name, adr) __cast_io_adr_with_type(const inline, IOPortEx<type>, name, adr)
#define __declare_io_port_member_simple(type, name, adr) __cast_io_adr_with_type(inline, IOPort<type>, name, adr)
#define __declare_io_port_member_const_simple(type, name, adr) __cast_io_adr_with_type(const inline, IOPort<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__