// Utilities for writing to and reading from I/O ports #include "ports.h" // Note: We use the volatile modifier everywhere because // the compiler might try and optimize some of our code // away, since its effects are not directly visible from // the code itself, which would be a nightmare to debug! byte readByte(u16 port) { // Reads a byte from the specified I/O port volatile byte result; __asm__ volatile ("in %%dx, %%al" : "=a" (result) : "d" (port)); return result; } void writeByte(u16 port, byte data) { // Writes a byte to the specified I/O port __asm__ volatile ("out %%al, %%dx" : : "a" (data), "d" (port)); } u16 readWord(u16 port) { // Reads a word (16 bits) from the specified I/O port volatile u16 result; __asm__ volatile ("in %%dx, %%ax" : "=a" (result) : "d" (port)); return result; } void writeWord(u16 port, u16 data) { // Writes a word (16 bits) to the specified I/O port __asm__ volatile ("out %%ax, %%dx" : : "a" (data), "d" (port)); }