Nucleus
Barry System headers (remove libc dependency) 18495cf (3 years, 2 months ago)
diff --git a/include/nucleus/io.h b/include/nucleus/io.h
new file mode 100644
index 0000000..1868e77
--- /dev/null
+++ b/include/nucleus/io.h
@@ -0,0 +1,97 @@
+#ifndef _IO_H
+#define _IO_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* Read byte from port */
+static inline uint8_t
+inb(uint16_t port)
+{
+ uint8_t value;
+ asm volatile("inb %w1, %0" : "=a" (value) : "Nd" (port));
+ return value;
+}
+/* Write byte to port */
+static inline void
+outb(uint16_t port, uint8_t value)
+{
+ asm volatile("outb %b0, %w1" : : "a" (value), "Nd" (port));
+}
+
+/* Read word from port */
+static inline uint16_t
+inw(uint16_t port)
+{
+ uint16_t value;
+ asm volatile("inw %w1, %0" : "=a" (value) : "Nd" (port));
+ return value;
+}
+/* Write word to port */
+static inline void
+outw(uint16_t port, uint16_t value)
+{
+ asm volatile("outw %w0, %w1" : : "a" (value), "Nd" (port));
+}
+
+/* Read dword from port */
+static inline uint32_t
+inl(uint16_t port)
+{
+ uint32_t value;
+ asm volatile("inl %1, %0" : "=a" (value) : "Nd" (port));
+ return value;
+}
+/* Write dword to port */
+static inline void
+outl(uint16_t port, uint32_t value)
+{
+ asm volatile("outl %0, %1" : : "a" (value), "Nd" (port));
+}
+
+/* Wait for IO to be ready */
+static inline void
+io_wait(void)
+{
+ outb(0x80, 0);
+}
+
+/* Read words into buffer */
+static inline void
+insw(uint16_t port, void *addr, size_t count)
+{
+ asm volatile(
+ "cld;"
+ "repne; insw;"
+ : "=D" (addr), "=c" (count)
+ : "d" (port), "0" (addr), "1" (count)
+ : "memory", "cc"
+ );
+}
+/* Write words out from buffer */
+static inline void
+outsw(uint16_t port, void *addr, size_t count)
+{
+ asm volatile(
+ "cld;"
+ "repne; outsw;"
+ : "=D" (addr), "=c" (count)
+ : "d" (port), "0" (addr), "1" (count)
+ : "memory", "cc"
+ );
+}
+
+/* Read dwords into buffer */
+static inline void
+insl(uint16_t port, void *addr, size_t count)
+{
+ asm volatile(
+ "cld;"
+ "repne; insl;"
+ : "=D" (addr), "=c" (count)
+ : "d" (port), "0" (addr), "1" (count)
+ : "memory", "cc"
+ );
+}
+
+#endif