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

84 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 T read_range_value(const BitRange<T>& range) const {
return IOPort::read().get_value(range);
}
constexpr void write_raw(T value) {
this->value = value;
}
constexpr void write(const S& value) {
this->value = static_cast<T>(value);
}
constexpr void write_range_value(const BitRangeValue<T>& value) {
IOPort<T, S>::write(S{S::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 __declare_global_raw(cv, type, name, adr) static __always_inline cv auto& name = *reinterpret_cast<type*>(__io_port_adr(adr))
#define __io_port_adr(adr) (IO_Base_Adr + (adr & ~IO_Base_Mask))
#define __declare_io_port_global_raw(cv, type, sub_type, name, adr) __declare_global_raw(cv, __collect(IOPort<type, sub_type>), name, adr)
#define __declare_io_port_global(type, name, adr) __declare_io_port_global_raw(, type::UnderlyingType, type, name, adr)
#define __declare_io_port_global_const(type, name, adr) __declare_io_port_global_raw(const, type::UnderlyingType, type, name, adr)
#define __declare_io_port_global_simple(type, name, adr) __declare_io_port_global_raw(, type, type, name, adr)
#define __declare_io_port_global_const_simple(type, name, adr) __declare_io_port_global_raw(const, type, type, name, adr)
#define __declare_io_port_member(type, name, adr) __declare_io_port_global_raw(inline, type::UnderlyingType, type, name, adr)
#define __declare_io_port_member_const(type, name, adr) __declare_io_port_global_raw(const inline, type::UnderlyingType, type, name, adr)
#define __declare_io_port_member_simple(type, name, adr) __declare_io_port_global_raw(inline, type, type, name, adr)
#define __declare_io_port_member_const_simple(type, name, adr) __declare_io_port_global_raw(const inline, type, 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_Base_Adr + (adr & ~IO_Base_Mask))))
#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__