118 lines
4.3 KiB
C++
118 lines
4.3 KiB
C++
#pragma once
|
|
#include "../../Auxiliary/types.hpp"
|
|
#include "../../Auxiliary/bits.hpp"
|
|
|
|
namespace JabyEngine {
|
|
namespace IOAdress {
|
|
constexpr uintptr_t patch_adr(uintptr_t adr) {
|
|
constexpr uintptr_t Mask = 0xF0000000;
|
|
constexpr uintptr_t Base = 0x10000000; // We might want to change this later to 0xB0000000 for caching and stuff (More research needed)
|
|
|
|
return (Base + (adr & ~Mask));
|
|
}
|
|
}
|
|
|
|
namespace internal {
|
|
template<typename T, typename S>
|
|
struct IOValue {
|
|
typedef S UnderlyingType;
|
|
|
|
UnderlyingType raw;
|
|
|
|
template<typename...ARGS>
|
|
static constexpr T from(const ARGS&...args) {
|
|
return T{0}.set(args...);
|
|
}
|
|
|
|
constexpr T& set(Bit bit) {
|
|
this->raw = bit::set(this->raw, bit);
|
|
return static_cast<T&>(*this);
|
|
}
|
|
|
|
constexpr T& set(ClearBit bit) {
|
|
this->raw = bit::set(this->raw, bit);
|
|
return static_cast<T&>(*this);
|
|
}
|
|
|
|
constexpr T& set_range(const BitRange& bits, UnderlyingType value) {
|
|
this->raw = bit::value::set_normalized(this->raw, bits, value);
|
|
return static_cast<T&>(*this);
|
|
}
|
|
|
|
template<typename U>
|
|
constexpr T& set(const BitRange::RangeValuePair<U>& value) {
|
|
this->raw = bit::value::set_normalized(this->raw, value);
|
|
return static_cast<T&>(*this);
|
|
}
|
|
|
|
template<typename U, typename...ARGS>
|
|
constexpr T& set(const U& head, const ARGS&...tail) {
|
|
return this->set(head).set(tail...);
|
|
}
|
|
|
|
constexpr IOValue<T, S>::UnderlyingType get(BitRange bits) const {
|
|
return bit::value::get_normalized(this->raw, bits.pos, bits.length);
|
|
}
|
|
|
|
constexpr T& clear(Bit bit) {
|
|
this->raw = bit::clear(this->raw, bit);
|
|
return static_cast<T&>(*this);
|
|
}
|
|
|
|
constexpr bool is_set(Bit bit) const {
|
|
return bit::is_set(this->raw, bit);
|
|
}
|
|
};
|
|
}
|
|
|
|
struct ubus32_t {
|
|
uint16_t low;
|
|
uint16_t high;
|
|
|
|
static ubus32_t from(uint32_t value) {
|
|
return {.low = static_cast<uint16_t>(value & 0xFFFF), .high = static_cast<uint16_t>(value >> 16)};
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
struct IOPort {
|
|
T value;
|
|
|
|
T read() const {
|
|
return {const_cast<const volatile IOPort<T>*>(this)->value.raw};
|
|
}
|
|
|
|
void write(T value) {
|
|
const_cast<volatile IOPort<T>*>(this)->value.raw = value.raw;
|
|
}
|
|
};
|
|
|
|
template<>
|
|
struct IOPort<ubus32_t> {
|
|
ubus32_t value;
|
|
|
|
ubus32_t read() const {
|
|
auto*const cv_this = const_cast<const volatile IOPort<ubus32_t>*>(this);
|
|
|
|
return {.low = cv_this->value.low, .high = cv_this->value.high};
|
|
}
|
|
|
|
void write(ubus32_t value) {
|
|
auto*const cv_this = const_cast<volatile IOPort<ubus32_t>*>(this);
|
|
|
|
cv_this->value.low = value.low;
|
|
cv_this->value.high = value.high;
|
|
}
|
|
|
|
void write(uint32_t value) {
|
|
IOPort<ubus32_t>::write(ubus32_t::from(value));
|
|
}
|
|
};
|
|
|
|
#define __declare_io_value(name, type) struct name : public ::JabyEngine::internal::IOValue<struct name, type>
|
|
#define __declare_value_at(cv, type, name, adr) static cv auto& name = *reinterpret_cast<type*>(::JabyEngine::IOAdress::patch_adr(adr))
|
|
#define __declare_array_at(cv, type, name, size, adr) static cv auto& name = reinterpret_cast<type(&)[size]>(*reinterpret_cast<type*>(IOAdress::patch_adr(adr)))
|
|
#define __declare_io_port_w_type(cv, type, name, adr) __declare_value_at(cv, ::JabyEngine::IOPort<type>, name, adr)
|
|
#define __declare_io_port(cv, name, adr) __declare_io_port_w_type(cv, struct name, name, adr)
|
|
#define __declare_io_port_array(cv, name, size, adr) __declare_array_at(cv, struct name, name, size, adr)
|
|
} |