blob: 7636c149c8a0e04aa45a34a5743ec5f8073ec38d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#ifndef PORTIO_HPP
#define PORTIO_HPP
#include <cstdint>
inline void outb(std::uint16_t port, std::uint8_t val)
{
asm volatile("out %%al, %%dx" :: "a"(val), "Nd"(port) : "memory");
}
inline std::uint8_t inb(std::uint16_t port)
{
std::uint8_t val;
asm volatile("inb %%dx" : "=a"(val) : "Nd"(port) : "memory");
return val;
}
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;
}
inline void io_wait()
{
outb(0x80, 0);
}
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;
}
};
#endif // PORTIO_HPP
|