From ccc7eeff60d41b9f740ff9c879efb56b7e683749 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 2 Mar 2016 02:48:25 -0500 Subject: [PATCH] Add low-level I/O functions --- src/IO.hh | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/IO.hh diff --git a/src/IO.hh b/src/IO.hh new file mode 100644 index 0000000..3b64bfb --- /dev/null +++ b/src/IO.hh @@ -0,0 +1,83 @@ +/* IO.hh + * vim: set tw=80: + * Eryn Wells + */ +/** + * Low-level hardware I/O functions. + */ + +#ifndef __IO_HH__ +#define __IO_HH__ + +#include + +namespace kernel { +namespace io { + +/* + * Input + */ + +inline uint8_t +inb(uint16_t port) +{ + uint8_t ret; + asm volatile("inb %[port], %[ret]", + : [result] "=a"(ret) + : [port] "Nd"(port)); + return ret; +} + + +inline uint16_t +inw(uint16_t port) +{ + uint16_t ret; + asm volatile("inw %[port], %[ret]", + : [result] "=a"(ret) + : [port] "Nd"(port)); + return ret; +} + + +inline uint32_t +inl(uint16_t port) +{ + uint32_t ret; + asm volatile("inl %[port], %[ret]", + : [result] "=a"(ret) + : [port] "Nd"(port)); + return ret; +} + +/* + * Output + */ + +inline void +outb(uint16_t port, + uint8_t value) +{ + asm volatile("outb %0, %1" : : "a"(value), "Nd"(port)); +} + + +inline void +outw(uint16_t port, + uint16_t value) +{ + asm volatile("outw %0, %1" : : "a"(value), "Nd"(port)); +} + + +inline void +outl(uint16_t port, + uint32_t value) +{ + asm volatile("outl %0, %1" : : "a"(value), "Nd"(port)); +} + +} /* namespace io */ +} /* namespace kernel */ + +#endif /* __IO_HH__ */