mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
LibBareMetal: Add IOAddress class
This commit is contained in:
parent
9440c45d6e
commit
7d39e380f9
Notes:
sideshowbarker
2024-07-19 08:55:58 +09:00
Author: https://github.com/supercomputer7 Commit: https://github.com/SerenityOS/serenity/commit/7d39e380f92 Pull-request: https://github.com/SerenityOS/serenity/pull/1322
1 changed files with 64 additions and 0 deletions
|
@ -26,6 +26,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
#if defined(KERNEL)
|
||||
|
@ -100,3 +103,64 @@ inline void delay()
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
class IOAddress {
|
||||
public:
|
||||
IOAddress() {}
|
||||
explicit IOAddress(u16 address)
|
||||
: m_address(address)
|
||||
{
|
||||
}
|
||||
|
||||
IOAddress offset(u16 o) const { return IOAddress(m_address + o); }
|
||||
u16 get() const { return m_address; }
|
||||
void set(u16 address) { m_address = address; }
|
||||
void mask(u16 m) { m_address &= m; }
|
||||
|
||||
template<typename T>
|
||||
[[gnu::always_inline]] inline T in()
|
||||
{
|
||||
if constexpr (sizeof(T) == 4)
|
||||
return IO::in32(get());
|
||||
if constexpr (sizeof(T) == 2)
|
||||
return IO::in16(get());
|
||||
if constexpr (sizeof(T) == 1)
|
||||
return IO::in8(get());
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[gnu::always_inline]] inline void out(T value)
|
||||
{
|
||||
if constexpr (sizeof(T) == 4) {
|
||||
IO::out32(get(), value);
|
||||
return;
|
||||
}
|
||||
if constexpr (sizeof(T) == 2) {
|
||||
IO::out16(get(), value);
|
||||
return;
|
||||
}
|
||||
if constexpr (sizeof(T) == 1) {
|
||||
IO::out8(get(), value);
|
||||
return;
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool is_null() const { return m_address == 0; }
|
||||
|
||||
bool operator==(const IOAddress& other) const { return m_address == other.m_address; }
|
||||
bool operator!=(const IOAddress& other) const { return m_address != other.m_address; }
|
||||
bool operator>(const IOAddress& other) const { return m_address > other.m_address; }
|
||||
bool operator>=(const IOAddress& other) const { return m_address >= other.m_address; }
|
||||
bool operator<(const IOAddress& other) const { return m_address < other.m_address; }
|
||||
bool operator<=(const IOAddress& other) const { return m_address <= other.m_address; }
|
||||
|
||||
private:
|
||||
u16 m_address { 0 };
|
||||
};
|
||||
|
||||
inline const LogStream& operator<<(const LogStream& stream, IOAddress value)
|
||||
{
|
||||
return stream << "IO " << String::format("%x", value.get());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue