#include <shade/platform/ports.h>

/*
 * Read 1 byte from specified port
 */
unsigned char inb(unsigned short port) {
    unsigned char result;
    // source and dest are backwards.
    // "=a (result)" set the C variable to the value of register e'a'x
    // '"d" (port) map the C variable port into e'd'x register
    __asm__("in %%dx, %%al" : "=a" (result) : "d" (port));
}

void outb(unsigned short port, unsigned char data) {
    // both regs are C vars, nothing is returned, so no '=' in asm syntax
    // comma because two vars in input area and none in return area
    __asm__("out %%al, %%dx" : : "a" (data), "d" (port));
}

unsigned short inw(unsigned short port) {
    unsigned short result;
    __asm__("in %%dx, %%ax" : "=a" (result) : "d" (port));
    return result;
}

void outw(unsigned short port, unsigned short data) {
    __asm__("out %%ax, %%dx" : : "a" (data), "d" (port));
}

void io_wait() {
    outb(0x80, 0);
}