You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.4 KiB
C++
73 lines
1.4 KiB
C++
4 weeks ago
|
#ifndef PORTIO_HPP
|
||
|
#define PORTIO_HPP
|
||
|
|
||
|
#include <cstdint>
|
||
|
|
||
|
inline void outb(std::uint16_t port, std::uint8_t val)
|
||
|
{
|
||
3 weeks ago
|
asm volatile("out %%al, %%dx" :: "a"(val), "Nd"(port) : "memory");
|
||
4 weeks ago
|
}
|
||
|
|
||
|
inline std::uint8_t inb(std::uint16_t port)
|
||
|
{
|
||
|
std::uint8_t val;
|
||
3 weeks ago
|
asm volatile("inb %%dx" : "=a"(val) : "Nd"(port) : "memory");
|
||
4 weeks ago
|
return val;
|
||
|
}
|
||
3 weeks ago
|
|
||
|
inline void outw(std::uint16_t port, std::uint16_t val)
|
||
|
{
|
||
|
asm volatile("out %%ax, %%dx" :: "a"(val), "Nd"(port) : "memory");
|
||
|
}
|
||
|
|
||
|
inline std::uint16_t inw(std::uint16_t port)
|
||
|
{
|
||
|
std::uint16_t val;
|
||
|
asm volatile("inw %%dx" : "=a"(val) : "Nd"(port) : "memory");
|
||
|
return val;
|
||
|
}
|
||
|
|
||
4 weeks ago
|
inline void io_wait()
|
||
|
{
|
||
|
outb(0x80, 0);
|
||
|
}
|
||
|
|
||
3 weeks ago
|
template<std::uint16_t Addr>
|
||
|
struct Port
|
||
|
{
|
||
|
Port() = default;
|
||
|
|
||
|
template<typename T>
|
||
|
auto operator=(T val) noexcept {
|
||
|
if constexpr (sizeof(T) == 1)
|
||
|
outb(Addr, val);
|
||
|
else if constexpr (sizeof(T) == 2)
|
||
|
outw(Addr, val);
|
||
|
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
template<typename T>
|
||
|
operator T() const noexcept {
|
||
|
if constexpr (sizeof(T) == 1)
|
||
|
return inb(Addr);
|
||
|
else if constexpr (sizeof(T) == 2)
|
||
|
return inw(Addr);
|
||
|
}
|
||
|
|
||
|
template<typename T>
|
||
|
bool operator==(T val) const noexcept {
|
||
|
T dat = *this;
|
||
|
return dat == val;
|
||
|
}
|
||
|
|
||
|
template<typename T>
|
||
|
auto operator&(T val) const noexcept {
|
||
|
T dat = *this;
|
||
|
return dat & val;
|
||
|
}
|
||
|
};
|
||
|
|
||
4 weeks ago
|
#endif // PORTIO_HPP
|
||
|
|