From 37cb11cec0735aa98052dab252a0cdb958c115d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sat, 24 Feb 2018 10:00:00 +0100 Subject: [PATCH] Add ports files and add use ANSI C. --- Makefile | 6 +++--- kernel/ports.c | 23 +++++++++++++++++++++++ kernel/ports.h | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 kernel/ports.c create mode 100644 kernel/ports.h diff --git a/Makefile b/Makefile index 3e52a10..26ff8db 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ ASM=nasm BIN=bin -CFLAGS=-ffreestanding -fno-pie -m32 +CFLAGS=-ffreestanding -fno-pie -m32 -ansi CC=gcc LD=ld LDFLAGS=-melf_i386 -Ttext 0x1000 --oformat binary -OBJ=kernel/kernel_entry.o kernel/kernel.o +OBJ=kernel/kernel_entry.o kernel/kernel.o kernel/ports.o all: os-image @@ -29,5 +29,5 @@ kernel/kernel.bin: $(OBJ) .PHONY: clean clean: - rm -rf boot/*.bin boot/*.o + rm -rf boot/*.bin kernel/*.bin rm -rf kernel/*.o boot/*.o drivers/*.o diff --git a/kernel/ports.c b/kernel/ports.c new file mode 100644 index 0000000..a795775 --- /dev/null +++ b/kernel/ports.c @@ -0,0 +1,23 @@ +unsigned char port_byte_in(unsigned char port) { + unsigned char res; + /* + * `"=a" (res)' means: put result of `al' into `res' variable + * `"d" (port)' means: load `port' into `dx'. + */ + __asm__("in %%dx, %%al" : "=a" (res) : "d" (port)); + return res; +} + +void port_byte_out(unsigned char port, unsigned char data) { + __asm__("out %%al, %%dx" : : "a" (data), "d" (port)); +} + +unsigned short port_word_in(unsigned short port) { + unsigned short res; + __asm__("in %%dx, %%ax" : "=a" (res) : "d" (port)); + return res; +} + +void port_word_out(unsigned short port, unsigned short data) { + __asm__("out %%ax, %%dx" : : "a" (data), "d" (port)); +} diff --git a/kernel/ports.h b/kernel/ports.h new file mode 100644 index 0000000..ab47a3b --- /dev/null +++ b/kernel/ports.h @@ -0,0 +1,18 @@ +#pragma once + +/* + * return a byte from a port. + */ +unsigned char port_byte_in(unsigned char port); +/* + * write a byte of data to a port. + */ +void port_byte_out(unsigned char port, unsigned char data); +/* + * return a word of data from a port. + */ +unsigned short port_word_in(unsigned short port); +/* + * write a word of data to a port. + */ +void port_word_out(unsigned short port, unsigned short data);