diff --git a/kernel/Makefile b/kernel/Makefile index 7256072..ab1e072 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -13,7 +13,7 @@ export CFLAGS := -Wall -O2 export CXXFLAGS := -Wall -O2 -fno-rtti -fno-exceptions export LDFLAGS := -T $(LDSCRIPT) -Map $(KERNEL).map -SUBDIRS := boot mm +SUBDIRS := boot mm lang SUBDIRS_clean := $(SUBDIRS:%=%.clean) .PHONY: all diff --git a/kernel/lang/Makefile b/kernel/lang/Makefile new file mode 100644 index 0000000..cae81d7 --- /dev/null +++ b/kernel/lang/Makefile @@ -0,0 +1,2 @@ + +include $(HOS_TOPLEVEL)/subdir.mak diff --git a/kernel/lang/kio.cc b/kernel/lang/kio.cc new file mode 100644 index 0000000..227d76c --- /dev/null +++ b/kernel/lang/kio.cc @@ -0,0 +1,157 @@ + +#include "kio.h" +#include "string.h" + +#include /* va_*() */ + +static void fmt_d2a(char * buf, int val); +static void fmt_u2a(char * buf, unsigned int val); +static void fmt_ll2a(char * buf, long long val); +static void fmt_ull2a(char * buf, unsigned long long val); +static void fmt_x2a(char * buf, unsigned int val); +static void fmt_o2a(char * buf, unsigned int val); + +extern "C" { + +void kprintf(char * fmt, ...) +{ + va_list args; + va_start(args, fmt); + + kvprintf(fmt, args); + + va_end(args); +} + +void kvprintf(char * fmt, va_list args) +{ + char tmpbuf[25]; + for ( ; *fmt; fmt++) + { + if (*fmt == '%') + { + fmt++; + if (*fmt) + { + switch (*fmt) + { + case 'c': + kputc(va_arg(args, int)); + break; + case 'd': + fmt_d2a(tmpbuf, va_arg(args, int)); + kputs(tmpbuf); + break; + case 'l': + fmt_ll2a(tmpbuf, va_arg(args, long long)); + kputs(tmpbuf); + break; + case 'L': + fmt_ull2a(tmpbuf, va_arg(args, unsigned long long)); + kputs(tmpbuf); + break; + case 'o': + fmt_o2a(tmpbuf, va_arg(args, unsigned int)); + kputs(tmpbuf); + break; + case 's': + kputs(va_arg(args, char *)); + break; + case 'u': + fmt_u2a(tmpbuf, va_arg(args, unsigned int)); + kputs(tmpbuf); + break; + case 'x': + fmt_x2a(tmpbuf, va_arg(args, unsigned int)); + kputs(tmpbuf); + break; + case '%': + kputc('%'); + break; + } + } + } + else + { + kputc(*fmt); + } + } +} + +void kputc(char c) +{ +} + +void kputs(char * s) +{ + while (*s) + { + kputc(*s); + s++; + } +} + +} /* extern "C" */ + +static void fmt_d2a(char * buf, int val) +{ + if (val == 0x7FFFFFFF) + { + strcpy(buf, "-2147483648"); + } + else + { + if (val < 0) + { + *buf++ = '-'; + val = -val; + } + fmt_u2a(buf, (unsigned int) val); + } +} + +static void fmt_u2a(char * buf, unsigned int val) +{ +} + +static void fmt_ll2a(char * buf, long long val) +{ +} + +static void fmt_ull2a(char * buf, unsigned long long val) +{ +} + +static void fmt_x2a(char * buf, unsigned int val) +{ + bool printing = false; + for (int s = 28; s >= 0; s -= 4) + { + unsigned int n = (val >> s) & 0xF; + if (n) + { + printing = true; + } + if (printing) + { + *buf++ = "0123456789ABCDEF"[n]; + } + } +} + +static void fmt_o2a(char * buf, unsigned int val) +{ + bool printing = false; + for (int s = 30; s >= 0; s -= 3) + { + unsigned int n = (val >> s) & 0x7; + if (n) + { + printing = true; + } + if (printing) + { + *buf++ = "01234567"[n]; + } + } +} diff --git a/kernel/lang/kio.h b/kernel/lang/kio.h new file mode 100644 index 0000000..7916f0b --- /dev/null +++ b/kernel/lang/kio.h @@ -0,0 +1,23 @@ + +#ifndef KIO_H +#define KIO_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void kprintf(char * fmt, ...); + +void kvprintf(char * fmt, va_list args); + +void kputc(char c); + +void kputs(char * s); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/kernel/lang/string.cc b/kernel/lang/string.cc new file mode 100644 index 0000000..8614fd9 --- /dev/null +++ b/kernel/lang/string.cc @@ -0,0 +1,14 @@ + +#include "string.h" + +extern "C" { + +void strcpy(char * dst, const char * src) +{ + while (*src) + { + *dst++ = *src++; + } +} + +} /* extern "C" */ diff --git a/kernel/lang/string.h b/kernel/lang/string.h new file mode 100644 index 0000000..7505167 --- /dev/null +++ b/kernel/lang/string.h @@ -0,0 +1,15 @@ + +#ifndef STRING_H +#define STRING_H + +#ifdef __cplusplus +extern "C" { +#endif + +void strcpy(char * dst, const char * src); + +#ifdef __cplusplus +} +#endif + +#endif