aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--src/drivers/interrupts.asm1
-rw-r--r--src/drivers/interrupts.c6
-rw-r--r--src/drivers/keyboard.c2
-rw-r--r--src/drivers/serial.c2
-rw-r--r--src/lib/conv.c116
-rw-r--r--src/lib/inc/conv.h13
-rw-r--r--src/lib/inc/def.h9
-rw-r--r--src/lib/inc/math.h (renamed from src/lib/inc/string.h)6
-rw-r--r--src/lib/inc/mem.h18
-rw-r--r--src/lib/inc/str.h16
-rw-r--r--src/lib/math.c15
-rw-r--r--src/lib/mem.c33
-rw-r--r--src/lib/str.c82
-rw-r--r--src/lib/string.c11
15 files changed, 302 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index ecc501e..eb9e863 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,10 @@ COBJS = src/main.o \
src/drivers/interrupts.o \
src/drivers/interrupts_asm.o \
src/drivers/keyboard.o \
- src/lib/string.o
+ src/lib/str.o \
+ src/lib/mem.o \
+ src/lib/math.o \
+ src/lib/conv.o
CC = cross/opt/bin/i686-elf-gcc
LD = cross/opt/bin/i686-elf-ld
AS = nasm
diff --git a/src/drivers/interrupts.asm b/src/drivers/interrupts.asm
index 59c323c..fd5dbfa 100644
--- a/src/drivers/interrupts.asm
+++ b/src/drivers/interrupts.asm
@@ -109,7 +109,6 @@ ISR_NOERRCODE 28
ISR_NOERRCODE 29
ISR_NOERRCODE 30
ISR_NOERRCODE 31
-ISR_NOERRCODE 128
extern isr_handler
isr_common_stub:
diff --git a/src/drivers/interrupts.c b/src/drivers/interrupts.c
index e3e8699..edf5837 100644
--- a/src/drivers/interrupts.c
+++ b/src/drivers/interrupts.c
@@ -71,7 +71,6 @@ void irq_remap()
// Handle IRQ ISRs
void irq_handler(struct regs *r)
{
- __asm__("cli");
void (*handler)(struct regs * r);
// Execute custom handler if exists
@@ -85,7 +84,6 @@ void irq_handler(struct regs *r)
// Send EOI to master PIC
outb(0x20, 0x20);
- __asm__("sti");
}
// Map ISRs to the correct entries in the IDT
@@ -130,7 +128,6 @@ void isr_uninstall_handler(int isr)
void isr_handler(struct regs *r)
{
- __asm__("cli");
void (*handler)(struct regs * r);
// Execute fault handler if exists
@@ -143,7 +140,6 @@ void isr_handler(struct regs *r)
while (1) {
};
}
- __asm__("sti");
}
void isr_install()
@@ -183,8 +179,6 @@ void isr_install()
idt_set_gate(29, (u32)isr29, 0x08, 0x8E);
idt_set_gate(30, (u32)isr30, 0x08, 0x8E);
idt_set_gate(31, (u32)isr31, 0x08, 0x8E);
-
- idt_set_gate(0x80, (u32)isr128, 0x08, 0x8E);
}
/**
diff --git a/src/drivers/keyboard.c b/src/drivers/keyboard.c
index 85b905e..3bef356 100644
--- a/src/drivers/keyboard.c
+++ b/src/drivers/keyboard.c
@@ -6,7 +6,7 @@
u8 scancode;
-void keyboard_handler(struct regs *r)
+void keyboard_handler()
{
scancode = inb(0x60);
serial_print("KEY\n");
diff --git a/src/drivers/serial.c b/src/drivers/serial.c
index 1ef424a..dcee4dd 100644
--- a/src/drivers/serial.c
+++ b/src/drivers/serial.c
@@ -1,6 +1,6 @@
#include <cpu.h>
#include <def.h>
-#include <string.h>
+#include <str.h>
void serial_install()
{
diff --git a/src/lib/conv.c b/src/lib/conv.c
new file mode 100644
index 0000000..89d1786
--- /dev/null
+++ b/src/lib/conv.c
@@ -0,0 +1,116 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <def.h>
+#include <math.h>
+#include <mem.h>
+#include <str.h>
+
+static const char HTOA_TABLE[] = "0123456789ABCDEF";
+static const char ITOA_TABLE[] = "0123456789";
+
+int atoi(char *str)
+{
+ u32 s_str = strlen(str);
+ if (!s_str)
+ return 0;
+
+ u8 negative = 0;
+ if (str[0] == '-')
+ negative = 1;
+
+ u32 i = 0;
+ if (negative)
+ i++;
+
+ int ret = 0;
+ for (; i < s_str; i++) {
+ ret += (str[i] - '0') * pow(10, (int)((s_str - i) - 1));
+ }
+
+ if (negative)
+ ret *= -1;
+ return ret;
+}
+
+char *htoa(u32 n)
+{
+ char *ret = (char *)malloc(10);
+
+ int i = 0;
+ while (n) {
+ ret[i++] = HTOA_TABLE[n & 0xF];
+ n >>= 4;
+ }
+
+ if (!i) {
+ ret[0] = '0';
+ i++;
+ }
+
+ for (; i <= 9; i++)
+ ret[i] = 0;
+
+ char *aux = strdup(ret);
+ free(ret);
+ ret = aux;
+
+ strinv(ret);
+ return ret;
+}
+
+int htoi(char *str)
+{
+ u32 s_str = strlen(str);
+
+ u32 i = 0;
+ int ret = 0;
+ for (; i < s_str; i++) {
+ char c = str[i];
+ int aux = 0;
+ if (c >= '0' && c <= '9')
+ aux = c - '0';
+ else if (c >= 'A' && c <= 'F')
+ aux = (c - 'A') + 10;
+
+ ret += aux * pow(16, (int)((s_str - i) - 1));
+ }
+
+ return ret;
+}
+
+char *itoa(int n)
+{
+ if (!n) {
+ char *ret = (char *)malloc(2);
+ ret[0] = '0';
+ ret[1] = 0;
+ return ret;
+ }
+ u8 negative = (u8)(n < 0);
+ if (negative)
+ n *= -1;
+
+ int sz;
+ for (sz = 0; n % pow(10, sz) != n; sz++) {
+ }
+
+ char *ret = (char *)malloc((u32)(sz + 1));
+
+ for (int i = 0; i < sz; i++) {
+ int digit = (n % pow(10, i + 1)) / pow(10, i);
+ ret[i] = ITOA_TABLE[digit];
+ }
+ ret[sz] = 0;
+
+ if (negative) {
+ char *aux = (char *)malloc((u32)(sz + 2));
+ strcpy(aux, ret);
+ aux[sz] = '-';
+ aux[sz + 1] = 0;
+ free(ret);
+ ret = aux;
+ }
+
+ strinv(ret);
+ return ret;
+}
diff --git a/src/lib/inc/conv.h b/src/lib/inc/conv.h
new file mode 100644
index 0000000..8295756
--- /dev/null
+++ b/src/lib/inc/conv.h
@@ -0,0 +1,13 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef CONV_H
+#define CONV_H
+
+#include <def.h>
+
+int atoi(char *str);
+char *htoa(u32 n);
+int htoi(char *str);
+char *itoa(int n);
+
+#endif
diff --git a/src/lib/inc/def.h b/src/lib/inc/def.h
index 409c0b3..42939a0 100644
--- a/src/lib/inc/def.h
+++ b/src/lib/inc/def.h
@@ -24,14 +24,5 @@ typedef unsigned long long u64;
*/
#define NULL ((void *)0)
-#define malloc(n) ((void *)((HEAP += n) - n)) // TODO: Implement real/better malloc/free
-#define free(x)
-
-/**
- * Heap
- */
-
-extern u32 HEAP;
-extern u32 HEAP_START;
#endif
diff --git a/src/lib/inc/string.h b/src/lib/inc/math.h
index c97fada..268e3e7 100644
--- a/src/lib/inc/string.h
+++ b/src/lib/inc/math.h
@@ -1,10 +1,10 @@
// MIT License, Copyright (c) 2020 Marvin Borner
-#ifndef STRING_H
-#define STRING_H
+#ifndef MATH_H
+#define MATH_H
#include <def.h>
-u32 strlen(const char *str);
+int pow(int base, int exp);
#endif
diff --git a/src/lib/inc/mem.h b/src/lib/inc/mem.h
new file mode 100644
index 0000000..2aaf4d8
--- /dev/null
+++ b/src/lib/inc/mem.h
@@ -0,0 +1,18 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef MEM_H
+#define MEM_H
+
+#include <def.h>
+
+#define malloc(n) ((void *)((HEAP += n) - n)) // TODO: Implement real/better malloc/free
+#define free(x)
+
+extern u32 HEAP;
+extern u32 HEAP_START;
+
+void *memcpy(void *dst, const void *src, u32 n);
+void *memset(void *dst, int c, u32 n);
+int memcmp(const void *s1, const void *s2, u32 n);
+
+#endif
diff --git a/src/lib/inc/str.h b/src/lib/inc/str.h
new file mode 100644
index 0000000..0e00e75
--- /dev/null
+++ b/src/lib/inc/str.h
@@ -0,0 +1,16 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef STRING_H
+#define STRING_H
+
+#include <def.h>
+
+u32 strlen(const char *s);
+char *strcpy(char *dst, const char *src);
+char *strchr(const char *s, int c);
+char *strcat(char *dst, const char *src);
+int strcmp(const char *s1, const char *s2);
+char *strinv(char *s);
+char *strdup(const char *s);
+
+#endif
diff --git a/src/lib/math.c b/src/lib/math.c
new file mode 100644
index 0000000..9cd9cea
--- /dev/null
+++ b/src/lib/math.c
@@ -0,0 +1,15 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+int pow(int base, int exp)
+{
+ if (exp < 0)
+ return 0;
+
+ if (!exp)
+ return 1;
+
+ int ret = base;
+ for (int i = 1; i < exp; i++)
+ ret *= base;
+ return ret;
+}
diff --git a/src/lib/mem.c b/src/lib/mem.c
new file mode 100644
index 0000000..00b9735
--- /dev/null
+++ b/src/lib/mem.c
@@ -0,0 +1,33 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <def.h>
+
+void *memcpy(void *dst, const void *src, u32 n)
+{
+ const char *sp = (const char *)src;
+ char *dp = (char *)dst;
+ for (; n != 0; n--)
+ *dp++ = *sp++;
+ return dst;
+}
+
+void *memset(void *dst, char val, u32 n)
+{
+ char *temp = (char *)dst;
+ for (; n != 0; n--)
+ *temp++ = val;
+ return dst;
+}
+
+int memcmp(const void *s1, const void *s2, u32 n)
+{
+ const u8 *a = (const u8 *)s1;
+ const u8 *b = (const u8 *)s2;
+ for (u32 i = 0; i < n; i++) {
+ if (a[i] < b[i])
+ return -1;
+ else if (b[i] < a[i])
+ return 1;
+ }
+ return 0;
+}
diff --git a/src/lib/str.c b/src/lib/str.c
new file mode 100644
index 0000000..24f95e5
--- /dev/null
+++ b/src/lib/str.c
@@ -0,0 +1,82 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#include <def.h>
+#include <mem.h>
+
+u32 strlen(const char *s)
+{
+ const char *ss = s;
+ while (*ss)
+ ss++;
+ return ss - s;
+}
+
+char *strcpy(char *dst, const char *src)
+{
+ char *q = dst;
+ const char *p = src;
+ char ch;
+
+ do {
+ *q++ = ch = *p++;
+ } while (ch);
+
+ return dst;
+}
+
+int strcmp(const char *s1, const char *s2)
+{
+ const u8 *c1 = (const u8 *)s1;
+ const u8 *c2 = (const u8 *)s2;
+ u8 ch;
+ int d = 0;
+
+ while (1) {
+ d = (int)(ch = *c1++) - (int)*c2++;
+ if (d || !ch)
+ break;
+ }
+
+ return d;
+}
+
+char *strchr(const char *s, int c)
+{
+ while (*s != (char)c) {
+ if (!*s)
+ return NULL;
+ s++;
+ }
+
+ return (char *)s;
+}
+
+char *strcat(char *dst, const char *src)
+{
+ strcpy(strchr(dst, '\0'), src);
+ return dst;
+}
+
+char *strinv(char *s)
+{
+ u32 s_str = strlen(s);
+
+ int iterations = (int)s_str / 2;
+ for (int i = 0; i < iterations; i++) {
+ char aux = s[i];
+ s[i] = s[(s_str - i) - 1];
+ s[(s_str - i) - 1] = aux;
+ }
+ return s;
+}
+
+char *strdup(const char *s)
+{
+ int l = strlen(s) + 1;
+ char *d = malloc(l);
+
+ if (d)
+ memcpy(d, s, l);
+
+ return d;
+}
diff --git a/src/lib/string.c b/src/lib/string.c
deleted file mode 100644
index 1b7a2b8..0000000
--- a/src/lib/string.c
+++ /dev/null
@@ -1,11 +0,0 @@
-// MIT License, Copyright (c) 2020 Marvin Borner
-
-#include <def.h>
-
-u32 strlen(const char *str)
-{
- u32 len = 0;
- while (str[len])
- len++;
- return len;
-}