aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-09-28 17:04:46 -0400
committerClyne Sullivan <clyne@bitgloo.com>2024-09-28 17:04:46 -0400
commitb9d2cef90451fae9ab7e10f5f11b8840e9bfe67d (patch)
tree15a24a37c8740499639f9f00cdb76070dbf7ff24
parentb480537a274ed15d71a59f5babf194a651c78910 (diff)
create Port interface
-rw-r--r--keyboard.cpp3
-rw-r--r--portio.hpp54
2 files changed, 53 insertions, 4 deletions
diff --git a/keyboard.cpp b/keyboard.cpp
index 29178f9..db7743c 100644
--- a/keyboard.cpp
+++ b/keyboard.cpp
@@ -10,6 +10,7 @@
extern TextOutput& term;
static CircularBuffer<char> keyboardBuffer;
+static Port<0x60> keyboardPort;
static const std::array<char, 0x59> ScanCodeSet1 {{
0, K_ESCAPE,
@@ -38,7 +39,7 @@ void keyboard_initialize()
keyboardBuffer = CircularBuffer<char>(128);
idt_register_callback(33, [](auto& regs) {
- const auto kc = inb(0x60);
+ const std::uint8_t kc = keyboardPort;
if (!isReleased(kc)) {
const auto ch = ScanCodeSet1[keycode(kc)];
diff --git a/portio.hpp b/portio.hpp
index 0a10651..7636c14 100644
--- a/portio.hpp
+++ b/portio.hpp
@@ -5,20 +5,68 @@
inline void outb(std::uint16_t port, std::uint8_t val)
{
- asm volatile("outb %b0, %w1" :: "a"(val), "Nd"(port) : "memory");
+ 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 %w1, %b0" : "=a"(val) : "Nd"(port) : "memory");
+ 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