BarryServer : Git

All the code for all my projects
// BarryServer : Git / Orion / blob / d41a53cbc7d055b1c00cf0a339dbed6925f4f02c / io.h

// Related

Orion

Barry Importing existing Orion kernel d41a53c (2 years, 4 months ago)
#ifndef KERNEL_IO_H
#define KERNEL_IO_H

#include <stdint.h>
#include <stddef.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 from port */
static inline void
outb(uint16_t port, uint8_t value)
{
	asm volatile("outb %b0, %w1" : : "a" (value), "Nd" (port));
}

/* Read byte 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 byte from port */
static inline void
outw(uint16_t port, uint16_t value)
{
	asm volatile("outw %w0, %w1" : : "a" (value), "Nd" (port));
}

/* Read byte 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 byte from 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 cnt)
{
	asm volatile(
		"cld;"
		"repne; insw;"
		: "=D" (addr), "=c" (cnt)
		: "d" (port), "0" (addr), "1" (cnt)
		: "memory", "cc"
	);
}

/* Write words out from buffer */
static inline void
outsw(uint16_t port, void *addr, size_t cnt)
{
	asm volatile(
		"cld;"
		"repne; outsw;"
		: "=D" (addr), "=c" (cnt)
		: "d" (port), "0" (addr), "1" (cnt)
		: "memory", "cc"
	);
}

/* Read dwords into buffer */
static inline void
insl(uint16_t port, void *addr, size_t cnt)
{
	asm volatile(
		"cld;"
		"repne; insl;"
		: "=D" (addr), "=c" (cnt)
		: "d" (port), "0" (addr), "1" (cnt)
		: "memory", "cc"
	);
}

#endif