Improve IOPort code

This commit is contained in:
Jaby
2022-09-05 20:58:33 +02:00
committed by Jaby
parent d7f4f45e3a
commit 5574addb29
4 changed files with 106 additions and 78 deletions

View File

@@ -54,18 +54,18 @@ static constexpr __always_inline BitRangeValue<T> operator<<(const BitRange<T>&
}
template<typename T>
class __no_align IOPort {
class __no_align ComplexBitMap {
private:
T value = 0;
public:
constexpr IOPort() = default;
constexpr IOPort(T value) : value(value) {
constexpr ComplexBitMap() = default;
constexpr ComplexBitMap(T value) : value(value) {
}
//Accesssing bits
template<typename S>
constexpr IOPort<T>& set_bit(S bit) {
constexpr ComplexBitMap<T>& set_bit(S bit) {
this->value = bit::set(this->value, static_cast<size_t>(bit));
return *this;
}
@@ -76,7 +76,7 @@ public:
}
template<typename S>
constexpr IOPort<T>& clear_bit(S bit) {
constexpr ComplexBitMap<T>& clear_bit(S bit) {
this->value = bit::clear(this->value, static_cast<size_t>(bit));
return *this;
}
@@ -98,7 +98,7 @@ public:
//Accessing values
template<typename S>
constexpr IOPort<T>& set_value(S value, const BitRange<S>& range) {
constexpr ComplexBitMap<T>& set_value(S value, const BitRange<S>& range) {
this->value = bit::value::set_normalized(this->value, static_cast<T>(value), range.begin, range.length);
return *this;
}
@@ -109,7 +109,7 @@ public:
}
template<typename S>
constexpr IOPort<T>& clear_value(const BitRange<S>& range) {
constexpr ComplexBitMap<T>& clear_value(const BitRange<S>& range) {
this->value = bit::value::clear_normalized(this->value, range.begin, range.length);
return *this;
}
@@ -129,6 +129,67 @@ public:
return static_cast<S>(bit::value::get_normalized(this->value, range.begin, range.length));
}
// For easier constructing
constexpr ComplexBitMap<T>& set(const BitRange<T>& range, T value) {
this->set_value(value, range);
return *this;
}
constexpr ComplexBitMap<T>& set(const BitRangeValue<T>& value) {
this->set_value(value.value, value.range);
return *this;
}
constexpr ComplexBitMap<T>& set(const Bit<T>& bit) {
this->set_bit(bit.value);
return *this;
}
constexpr ComplexBitMap<T>& set(const ClearBitValue& value) {
this->clear_bit(value.bit);
return *this;
}
constexpr ComplexBitMap<T>& operator|(const BitRangeValue<T>& value) {
this->set_value(value.value, value.range);
return *this;
}
constexpr ComplexBitMap<T>& operator|(const Bit<T>& bit) {
this->set_bit(bit.value);
return *this;
}
constexpr ComplexBitMap<T>& operator|(const ClearBitValue& value) {
this->clear_bit(value.bit);
return *this;
}
//For raw access
constexpr operator T() const {
return this->value;
}
constexpr operator T() const volatile {
return this->value;
}
constexpr ComplexBitMap<T>& operator=(T value) {
this->value = value;
return *this;
}
constexpr void operator=(T value) volatile {
this->value = value;
}
};
template<typename T>
class __no_align IOPort {
private:
T value;
public:
//For easy access
constexpr T read() const {
return const_cast<volatile IOPort<T>*>(this)->value;
@@ -141,45 +202,11 @@ public:
constexpr volatile T& ref() {
return const_cast<volatile IOPort<T>*>(this)->value;
}
//For raw access
constexpr operator T() const {
return this->value;
}
constexpr operator T() const volatile {
return this->value;
}
constexpr IOPort<T>& operator=(T value) {
this->value = value;
return *this;
}
constexpr void operator=(T value) volatile {
this->value = value;
}
// For easier constructing
constexpr IOPort<T>& operator|(const BitRangeValue<T>& value) {
this->set_value(value.value, value.range);
return *this;
}
constexpr IOPort<T>& operator|(const Bit<T>& bit) {
this->set_bit(bit.value);
return *this;
}
constexpr IOPort<T>& operator|(const ClearBitValue& value) {
this->clear_bit(value.bit);
return *this;
}
};
struct __no_align ubus32_t {
IOPort<uint16_t> low;
IOPort<uint16_t> high;
ComplexBitMap<uint16_t> low;
ComplexBitMap<uint16_t> high;
constexpr ubus32_t(uint32_t value) {
*this = value;
@@ -210,10 +237,10 @@ static constexpr uintptr_t IO_Base_Adr = 0x10000000;
#define __declare_io_port_global(type, name, adr) static __always_inline auto& name = *reinterpret_cast<IOPort<type>*>((IO_Base_Adr + (adr & ~IO_Base_Mask)))
#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 __io_port_inherit(name) \
using IOPort::operator=; \
#define __io_port_inherit_complex_bit_map(name) \
using ComplexBitMap::operator=; \
constexpr name() = default; \
constexpr name(IOPort value) : IOPort(value) { \
constexpr name(ComplexBitMap value) : ComplexBitMap(value) { \
}
#endif //!__JABYENGINE_IOPORT_HPP__