diff --git a/.gitignore b/.gitignore index c6127b3..010ee81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Prerequisites +build *.d # Object files diff --git a/boot/boot.c b/boot/boot.c index 8dcd959..4a29add 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -2,7 +2,6 @@ #include "dat.h" #include "fn.h" - void (*probe1[])(void) = { a20up, }; @@ -31,24 +30,10 @@ machdep(void) } static void -puts(char *s) -{ - for(;*s; ++s) - putchar(*s); -} - -static void -putcn(char *s, int n) -{ - for(int i=0; i < n; ++i) - putchar(s[i]); -} - -static int rdline(char *buf, int n) { char *p = buf; - char *e = buf + n; + char *e = buf + n - 1; while(p < e){ int c = getchar(); @@ -70,18 +55,20 @@ rdline(char *buf, int n) } putchar('\n'); done: - return p - buf; + *p = 0; } void boot(int bootdev) { machdep(); + print("\n\n===> Hello world <===\n\tBooted on disk 0x%x\n", bootdev); + for(;;){ char buf[8192]; - puts("\nboot >> "); - int l = rdline(buf, sizeof(buf)); - putcn(buf, l); + print("\nboot >> "); + rdline(buf, sizeof buf); + print("%s", buf); } } diff --git a/boot/fn.h b/boot/fn.h index bc3c673..3d4c75c 100644 --- a/boot/fn.h +++ b/boot/fn.h @@ -6,6 +6,7 @@ void pcputc(int dev, int c); // print.c void putchar(int c); int getchar(void); +void print(const char *fmt, ...); // a20.c void a20up(void); diff --git a/boot/print.c b/boot/print.c index 6d571b5..857ff56 100644 --- a/boot/print.c +++ b/boot/print.c @@ -2,13 +2,55 @@ #include "dat.h" #include "fn.h" -#define TABWIDTH 8 +#define TABWIDTH 4 static void conputc(int c); static int congetc(void); - +static void doprint(void (*put)(int), const char *fmt, va_list ap); +static void putint(void (*put)(int), int n, const char *sym, int base); static int pos = 0; +void +print(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + doprint(putchar, fmt, args); + va_end(args); +} + +static void +doprint(void (*put)(int), const char *s, va_list ap) +{ + while(*s){ + if(*s != '%'){ + put(*s++); + continue; + } + ++s; + switch(*s){ + case 'd': + putint(put, va_arg(ap,int), "0123456789", 10); + break; + case 'x': + putint(put, va_arg(ap,int), "0123456789abcdef", 16); + break; + case 'X': + putint(put, va_arg(ap,int), "0123456789ABCDEF", 16); + break; + case 's': + for(char *p=va_arg(ap,char*); *p; ++p) + put(*p); + break; + default: + return; + } + ++s; + } +} + + int getchar(void) { @@ -67,3 +109,19 @@ congetc(void) { return condev.getc(condev.dev); } + +static void +putint(void (*put)(int), int n, const char *sym, int base) +{ + int i; + char buf[16]; + + i = 0; + do{ + buf[i++] = n%base; + n /= base; + }while(n); + do{ + put((int)sym[(int)buf[--i]]); + }while(i); +} \ No newline at end of file diff --git a/u.h b/u.h index 6be17ee..920a125 100644 --- a/u.h +++ b/u.h @@ -1,5 +1,6 @@ #define elem(x) ((int)(sizeof(x)/sizeof((x)[0]))) #define nil ((void*)0) +#define USED(x) if(x); typedef unsigned short ushort; typedef unsigned char uchar; @@ -18,4 +19,16 @@ typedef unsigned long long u64; typedef signed char i8; typedef signed short i16; typedef signed int i32; -typedef signed long long i64; \ No newline at end of file +typedef signed long long i64; + +typedef char* va_list; +#define va_start(list, start) ((list) = (char*)((int*)&(start)+1)) +#define va_end(list) \ + USED(list) + +#define va_arg(list, mode) \ + ((sizeof(mode) == 1) ? \ + ((list += 4), (mode*)list)[-4]: \ + (sizeof(mode) == 2) ? \ + ((list += 4), (mode*)list)[-2]: \ + ((list += sizeof(mode)), (mode*)list)[-1])