changeset 112:aec919038b60

Started working on redoing the directory structure
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sat, 11 Jun 2011 14:13:21 -0400
parents fb2c2f2a074f
children d971d4288a5a
files Makefile src/ls.c src/math.c src/operations.c src/os/math.c src/os/shell.c src/os/std.c src/os/stdio.c src/os/string.c src/osTest.c src/prog/hello.c src/prog/ls.c src/prog/math.c src/prog/operations.c src/prog/stack.c src/prog/std.c src/prog/stdio.c src/prog/string.c src/shell.c src/stack.c src/std.c src/stdio.c src/string.c
diffstat 23 files changed, 668 insertions(+), 660 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Jun 11 12:42:18 2011 -0400
+++ b/Makefile	Sat Jun 11 14:13:21 2011 -0400
@@ -11,47 +11,55 @@
 CXXFLAGS=$(CFLAGS)
 LDFLAGS=-m elf64_s390
 
-BINS=shell osTest ls
+OSBINS=shell
+PROGBINS=hello ls
 
-sarpn_OBJS=src/sarpn.o src/std.o src/string.o src/stack.o src/operations.o src/math.o arch/arch.a
+shell_OBJS=src/os/shell.o src/os/std.o src/os/fs.o src/os/string.o src/os/stdio.o src/os/scall.o arch/arch.a
+hello_OBJS=src/prog/hello.o
+ls_OBJS=src/prog/ls.o
+
+sarpn_OBJS=src/sarpn.o src/std.o src/string.o src/prog/stack.o src/operations.o src/math.o arch/arch.a
 dynamic_OBJS=src/dynamic.o src/std.o src/string.o src/stack.o arch/arch.a
 testFS_OBJS=src/testFS.o src/std.o src/string.o src/os/fs.o arch/arch.a
-shell_OBJS=src/shell.o src/std.o src/os/fs.o src/string.o src/stdio.o src/os/scall.o arch/arch.a
-osTest_OBJS=src/osTest.o
-ls_OBJS=src/ls.o
 
 ARCH_OBJS=arch/io.o arch/cons.o arch/ebcdic.o arch/fba.o arch/ioint.o \
 	  arch/svc.o arch/svcint.o
 
 .PHONY: all build clean tags
 
-all: $(BINS) loader.bin
+all: $(OSBINS) $(PROGBINS) loader.bin
+	utility/fsGen.py $(PROGBINS) > hercules/curDisk.img
 	@echo "Loader is `stat -c %s loader.bin` bytes"
 
 clean:
+	rm -f $(shell_OBJS)
+	rm -f $(osTest_OBJS)
+	rm -f $(ls_OBJS)
 	rm -f $(sarpn_OBJS)
 	rm -f $(dynamic_OBJS)
 	rm -f $(testFS_OBJS)
 	rm -f $(ARCH_OBJS)
-	rm -f $(BINS)
+	rm -f $(OSBINS)
+	rm -f $(PROGBINS)
 	rm -f loader.bin ipl/*.o ipl/*.rto ipl/ipl_ccws.S cscope.out
 
 tags:
 	cscope -R -b
 	ctags -R
 
+shell: $(shell_OBJS)
+	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
+hello: $(hello_OBJS)
+	$(LD) $(LDFLAGS) -T scripts/linkerProg.script -o $@ $^
+ls: $(ls_OBJS)
+	$(LD) $(LDFLAGS) -T scripts/linkerProg.script -o $@ $^
+
 sarpn: $(sarpn_OBJS)
 	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
 dynamic: $(dynamic_OBJS)
 	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
 testFS: $(testFS_OBJS)
 	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
-shell: $(shell_OBJS)
-	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
-osTest: $(osTest_OBJS)
-	$(LD) $(LDFLAGS) -T scripts/linkerProg.script -o $@ $^
-ls: $(ls_OBJS)
-	$(LD) $(LDFLAGS) -T scripts/linkerProg.script -o $@ $^
 
 arch/arch.a: $(ARCH_OBJS)
 	$(AR) rc $@ $^
--- a/src/ls.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,16 +0,0 @@
-#include <os/fsStructs.h>
-#include <svcCalls.h>
-
-void start()
-{
-	size_t i;
-	for(i = 0; 1; i++) {
-		Direntry de;
-		ErrCode err = finfo(i, &de);
-		if(err == mkError(MODFS, OUTOFRANGE, WARN)) break;
-		else if(isError(err)) exit();
-		printLine(de.fname, 28);
-		printLine("\n", 1);
-	}
-	exit();
-}
--- a/src/math.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,84 +0,0 @@
-double iPow(double base, int exponent)
-{
-	double ans = 1;
-	short isNeg = 0;
-	if(exponent < 0) {
-		isNeg = 1;
-		exponent *= -1;
-	}
-	while(exponent > 0) {
-		if(exponent%2) {
-			exponent -= 1;
-			ans *= base;
-		} else {
-			exponent /= 2;
-			base *= base;
-		}
-	}
-	return isNeg ? 1/ans : ans;
-}
-
-double exp(double exp)
-{
-	unsigned int i; //a counter for the loop
-	double ans = 1;
-	double prev = -1;
-	double term = 1;
-	short isNeg = 0;
-	if(exp < 0) {
-		isNeg = 1;
-		exp *= -1;
-	}
-	for(i = 1; ans - prev; i++) {
-		prev = ans;
-		term *= exp/i;
-		ans += term;
-	}
-	return isNeg ? 1/ans : ans;
-}
-
-double log(double num)
-{
-	unsigned int denom = 1;
-	double euler = 2.71828182845904;
-	double ans = 0;
-	double prev = -1;
-	double term = -1;
-	while(num <= -1 || num >= 1) {
-		num /= euler;
-		ans += 1;
-	}
-	for(; ans - prev; denom++) {
-		prev = ans;
-		term *= (-1*(num - 1));
-		ans += term/denom;
-	}
-	return ans;
-}
-
-double pow(double base, double exponent)
-{
-	if(exponent == (int) exponent)
-		return iPow(base, (int) exponent);
-	return exp(exponent*log(base));
-}
-
-double math_add(double first, double sec)
-{
-	return first + sec;
-}
-
-double math_sub(double first, double sec)
-{
-	return first - sec;
-}
-
-double math_mult(double first, double sec)
-{
-	return first*sec;
-}
-
-double math_div(double first, double sec)
-{
-	return first/sec;
-}
--- a/src/operations.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-#include <operations.h>
-#include <std.h>
-#include <string.h>
-#include <stack.h>
-#include <math.h>
-
-const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "**", "dup", "drop", "log", "exp", "swap"};
-void (*operation[MAXOP])(Stack*) = {op_print, op_add, op_sub, op_mult,
-	op_div, op_pow, op_dup, op_drop, op_log, op_exp, op_swap};
-
-static void error_unk()
-{
-	sPrint("ERROR: CAUSE UNKNOWN\n");
-}
-
-static void error_small()
-{
-	sPrint("ERROR: STACK TOO SMALL\n");
-}
-
-void op_math1(Stack *stack, eltType (*mathop)(eltType))
-{
-	if(stack->size >= 1) {
-		eltType val;
-		if(pop(stack, &val)) error_unk();
-		else push(stack, mathop(val));
-	} else error_small();
-}
-
-void op_math2(Stack *stack, eltType (*mathop)(eltType, eltType))
-{
-	if(stack->size >= 2) {
-		eltType val1, val2;
-		if(pop(stack, &val2) || pop(stack, &val1)) error_unk();
-		else push(stack, mathop(val1, val2));
-	} else error_small();
-}
-
-void op_print(Stack *stack)
-{
-	if(stack->size >= 1) {
-		char output[30];
-		eltType val;
-		if(pop(stack, &val)) error_unk();
-		else sPrint(strcat(ftoa(val, output, 10), "\n"));
-	} else error_small();
-}
-
-void op_add(Stack *stack)
-{
-	op_math2(stack, math_add);
-}
-
-void op_sub(Stack *stack)
-{
-	op_math2(stack, math_sub);
-}
-
-void op_mult(Stack *stack)
-{
-	op_math2(stack, math_mult);
-}
-
-void op_div(Stack *stack)
-{
-	op_math2(stack, math_div);
-}
-
-void op_pow(Stack *stack)
-{
-	op_math2(stack, pow);
-}
-
-void op_dup(Stack *stack)
-{
-	if(stack->size >= 1) {
-		eltType val;
-		if(pop(stack, &val)) error_unk();
-		else {
-			push(stack, val);
-			push(stack, val);
-		}
-	} else error_small();
-}
-
-void op_drop(Stack *stack)
-{
-	if(stack->size >= 1) {
-		eltType val;
-		pop(stack, &val);
-	} else error_small();
-}
-
-void op_log(Stack *stack)
-{
-	op_math1(stack, log);
-}
-
-void op_exp(Stack *stack)
-{
-	op_math1(stack, exp);
-}
-
-void op_swap(Stack *stack)
-{
-	if(stack->size >= 2) {
-		eltType val1, val2;
-		if(pop(stack, &val2) || pop(stack, &val1)) error_unk();
-		else {
-			push(stack, val2);
-			push(stack, val1);
-		}
-	} else error_small();
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/os/math.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,84 @@
+double iPow(double base, int exponent)
+{
+	double ans = 1;
+	short isNeg = 0;
+	if(exponent < 0) {
+		isNeg = 1;
+		exponent *= -1;
+	}
+	while(exponent > 0) {
+		if(exponent%2) {
+			exponent -= 1;
+			ans *= base;
+		} else {
+			exponent /= 2;
+			base *= base;
+		}
+	}
+	return isNeg ? 1/ans : ans;
+}
+
+double exp(double exp)
+{
+	unsigned int i; //a counter for the loop
+	double ans = 1;
+	double prev = -1;
+	double term = 1;
+	short isNeg = 0;
+	if(exp < 0) {
+		isNeg = 1;
+		exp *= -1;
+	}
+	for(i = 1; ans - prev; i++) {
+		prev = ans;
+		term *= exp/i;
+		ans += term;
+	}
+	return isNeg ? 1/ans : ans;
+}
+
+double log(double num)
+{
+	unsigned int denom = 1;
+	double euler = 2.71828182845904;
+	double ans = 0;
+	double prev = -1;
+	double term = -1;
+	while(num <= -1 || num >= 1) {
+		num /= euler;
+		ans += 1;
+	}
+	for(; ans - prev; denom++) {
+		prev = ans;
+		term *= (-1*(num - 1));
+		ans += term/denom;
+	}
+	return ans;
+}
+
+double pow(double base, double exponent)
+{
+	if(exponent == (int) exponent)
+		return iPow(base, (int) exponent);
+	return exp(exponent*log(base));
+}
+
+double math_add(double first, double sec)
+{
+	return first + sec;
+}
+
+double math_sub(double first, double sec)
+{
+	return first - sec;
+}
+
+double math_mult(double first, double sec)
+{
+	return first*sec;
+}
+
+double math_div(double first, double sec)
+{
+	return first/sec;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/os/shell.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,80 @@
+#include <std.h>
+#include <string.h>
+#include <stdio.h>
+#include <error.h>
+#include <os/fs.h>
+#include <die.h>
+#include <elf.h>
+#include <psw.h>
+#include <svc.h>
+#include <os/scall.h>
+
+#define PROGRAM_STACK_START 0x400000 - 160 //this is correct, ignore the documentation
+#define KERNEL_STACK_START 0x380000 - 160 //this is correct, ignore the documentation
+
+void start(u64 __memsize)
+{
+	init_all(__memsize);
+	if(isError(init_fs(0x100, __memsize))) die();
+	char buffer[1024];
+	ErrCode err;
+	//set up the svc call handler
+	set_svc_handler(svc_handler, (void*)KERNEL_STACK_START);
+	
+	while(1) {
+		sPrint("$ ");
+		sGet(buffer, 78);
+		u32 fid;
+		if(isError(err = lookupFile(buffer, &fid))) {
+			if(errCode(err) == NOTFILE) sPrint("ERROR: command not found\n");
+			continue;
+		} else {
+			u32 fileSize;
+			if(isError(getFileSize(fid, &fileSize)));
+			void *data = malloc(fileSize);
+			if(!data) continue;
+			if(isError(getFileData(fid, data))) goto loopCont;
+			Elf64_Ehdr *eHdr = data;
+			if(eHdr->e_ident[EI_MAG0] != ELFMAG0 || eHdr->e_ident[EI_MAG1] != ELFMAG1 ||
+					eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3)
+				goto loopCont;
+			if(eHdr->e_ident[EI_CLASS] != ELFCLASS64) goto loopCont;
+			int i;
+			for(i = 0; i < eHdr->e_phnum; i++) {
+				Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr->e_phoff) + i);
+				if(pHdr->p_type == PT_LOAD) {
+					void *fromLoc = data + pHdr->p_offset;
+					void *toLoc = (void*)pHdr->p_vaddr;
+					memcpy(toLoc, fromLoc, pHdr->p_filesz);
+					size_t diff = pHdr->p_memsz - pHdr->p_filesz;
+					/*
+					sprintf(buffer, "file: %x, mem: %x, toLoc: %x\n", pHdr->p_filesz, pHdr->p_memsz, toLoc);
+					sPrint(buffer);
+					*/
+					if(diff) memset(toLoc + pHdr->p_filesz, 0, diff);
+				}
+			}
+
+			Psw psw;
+			u64 registers[16];
+			memset(&psw, 0, sizeof(psw));
+			memset(registers, 0, sizeof(registers));
+			registers[15] = PROGRAM_STACK_START;
+			psw.p = 1;
+			psw.ea = 1;
+			psw.ba = 1;
+			psw.ptr = eHdr->e_entry;
+			/*
+			sprintf(buffer, "Going to memory address %x\n", eHdr->e_entry);
+			sPrint(buffer);
+			*/
+			swapcontext(&shellPsw, shellRegisters, &psw, registers);
+		loopCont:
+			free(data);
+		}
+	}
+
+//END:
+	sPrint("DONE\n");
+	die();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/os/std.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,193 @@
+#include <std.h>
+#include <string.h>
+
+void init_all(u64 __memsize)
+{
+	init_io_int();
+	init_console();
+	malloc_init(__memsize - HEAP_START);
+}
+
+double abs(double num)
+{
+	if(num < 0) return num*-1;
+	else return num;
+}
+
+char* itoa(s64 n, char *a, unsigned short base)
+{
+	char *ret = a;
+	if(n < 0) {
+		*a++ = '-';
+		n *= -1;
+	}
+	char *b = a;
+	if(!n) *b++ = '0';
+	for(; n; b++) {
+		int temp = n%base;
+		if(temp < 10) *b = '0' + temp;
+		else *b = 'a' + temp - 10;
+		n = n/base;
+	}
+	*b-- = '\0';
+	for(; a < b; a++, b--) { //reverse
+		char temp = *b;
+		*b = *a;
+		*a = temp;
+	}
+	return ret;
+}
+
+char* ftoa(double x, char *a, unsigned int prec)
+{
+	char *ret = a;
+	if(x == INF || x == -INF) {
+		strcpy(a, "INF");
+	} else if(x != x) { //NAN != NAN
+		strcpy(a, "NAN");
+	} else {
+		s64 n = (s64) x; //integer part
+		double d = abs(x - (double) n); //fractional part;
+		itoa(n, a, 10);
+		if(prec) { //only do the decimal part if decimal parts were asked for
+			while(*a && *++a); //get to the null character from itoa
+			int i; //counter variable for the for loop
+			*a++ = '.'; //put the decimal in place
+			for(i = 0; i < prec; i++) {
+				d *= 10; //the integer part is the decimal digit
+				*a++ = ((int) d) + '0'; //add the integer part of d to the string
+				d -= (int) d; //chop off the integer part
+			} a--; //move back to the last digit
+			while(*a != '.') {
+				if(*a == '0') {
+					a--;
+					continue;
+				} else if(*a > '0' && *a <= '9') {
+					a++;
+					break;
+				} else {
+					sPrint("ERROR: SOMETHING IS VERY WRONG\n");
+					break;
+				}
+			}
+			*a = '\0';
+		}
+	}
+	return ret;
+}
+
+s64 atoi(char *a)
+{
+	short neg = 0;
+	s64 n = 0;
+	if(*a == '-') {
+		neg = 1;
+		a++;
+	} else if(*a == '+') a++;
+	while(*a >= '0' && *a <= '9')
+		n = n*10 + (*a++ - '0');
+	if(neg) n *= -1;
+	return n;
+}
+
+double atof(char *a)
+{
+	s64 n = atoi(a);
+	double x = 0;
+	double dec = .1;
+	short neg = 0;
+	if(*a == '-') {
+		neg = 1;
+		a++;
+	}
+	while(*a != '.') {
+		if(!(*a >= '0' && *a <= '9') && *a != '-') return n;
+		a++;
+	} a++; //a will be immediately after the decimal point
+	while(*a >= '0' && *a <= '9') { //goes through the decimal part
+		x += (*a - '0')*dec;
+		dec *= .1;
+		a++;
+	}
+	if(neg) x*=-1;
+	return n + x;
+}
+
+void sPrint(char *a)
+{
+	char *b = a;
+	while(*b && *++b);
+	do {
+		putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); //TODO supervisor call
+		a += CON_LEN;
+	} while(a < b);
+}
+
+char* sGet(char *a, unsigned int n)
+{
+	int length = getline(a, n); //TODO supervisor call
+	a[(length < n)?length:n - 1] = '\0';
+	return a;
+}
+
+//DYNAMIC MEMORY
+
+static Header base;
+static Header *allocp = NULL; //the location of the last known free block
+
+void malloc_init(size_t memSize) //TODO get rid of this
+{
+	allocp = &base;
+	allocp->size = 0;
+	allocp->next = (void*)HEAP_START;
+	allocp->next->next = &base;
+	allocp->next->size = (memSize - HEAP_START)/sizeof(blockUnit);
+	if((sizeof(blockUnit)%sizeof(u64))) {
+		sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n");
+	}
+}
+
+void* malloc(size_t size)
+{
+	if(allocp == NULL) return NULL;
+	size_t nUnits = ((size + sizeof(Header)) + sizeof(blockUnit) - 1)/sizeof(blockUnit);
+	Header *prev = allocp;
+	Header *cur, *temp;
+	for(cur = prev->next;; prev = cur, cur = cur->next) {
+		if(cur->size >= nUnits) {
+			if(cur->size == nUnits) {
+				prev->next = cur->next;
+			} else {
+				temp = cur + nUnits; //This requires sizeof(blockUnit) == sizeof(Header).  TODO fix
+				temp->size = cur->size - nUnits;
+				temp->next = cur->next;
+				prev->next = temp;
+				cur->size = nUnits;
+			}
+			allocp = prev;
+			return (void*)(cur + 1);
+		} else if(cur == allocp) { //We went back to the start...
+			//TODO ask the OS for more momory
+			return NULL;
+		}
+	}
+}
+
+void free(void *ptr)
+{
+	Header *toFree = (Header *)ptr - 1;
+	Header *scan;
+	for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next)
+		if(scan->next < scan && (toFree > scan || toFree < scan->next)) break;
+	toFree->next = scan->next;
+	scan->next = toFree;
+	if(scan + scan->size == toFree) {
+		scan->size += toFree->size;
+		scan->next = toFree->next;
+		toFree = scan;
+	}
+	if(toFree + toFree->size == toFree->next) {
+		toFree->size += toFree->next->size;
+		toFree->next = toFree->next->next;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/os/stdio.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <std.h>
+#include <string.h>
+#include <stdarg.h>
+
+int sprintf(char *str, const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	*str = '\0';
+	while(*fmt) {
+		if(*fmt != '%') {
+			*str++ = *fmt++;
+		} else {
+			switch(*(fmt + 1)) {
+				case '%':
+					*str++ = '%';
+					break;
+				case 'x':
+					{
+						char buffer[32];
+						long long num = va_arg(ap, long long);
+						itoa(num, buffer, 16);
+						memcpy(str, buffer, strlen(buffer));
+						str += strlen(buffer);
+					}
+					break;
+				case 'i':
+					{
+						char buffer[32];
+						long long num = va_arg(ap, long long);
+						itoa(num, buffer, 10);
+						memcpy(str, buffer, strlen(buffer));
+						str += strlen(buffer);
+					}
+					break;
+				case 'f':
+					{
+						char buffer[32];
+						double num = va_arg(ap, double);
+						ftoa(num, buffer, 10);
+						memcpy(str, buffer, strlen(buffer));
+						str += strlen(buffer);
+					}
+					break;
+				case 's':
+					{
+						char *text = va_arg(ap, char*);
+						memcpy(str, text, strlen(text));
+						str += strlen(text);
+					}
+					break;
+			}
+			fmt += 2;
+		}
+	}
+	*str = '\0';
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/os/string.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,50 @@
+int strcmp(const char *a, const char *b)
+{
+	while(1) {
+		if(*a - *b) return *a - *b;
+		if(*a == '\0') return 0;
+		a++;
+		b++;
+	}
+	return -1;
+}
+
+void strcpy(char *dest, const char *src)
+{
+	while((*dest++ = *src++));
+}
+
+char* strcat(char *dest, const char *src)
+{
+	char *ret = dest;
+	while(*dest&& *++dest); //get to null in first string
+	while((*dest++ = *src++));
+	return ret;
+}
+
+void* memcpy(void *dest, const void *src, size_t num)
+{
+	size_t i;
+	const u8 *from = src;
+	u8 *to = dest;
+	for(i = 0; i < num; i++)
+		to[i] = from[i];
+	return dest;
+}
+
+void* memset(void *dest, u8 value, size_t num)
+{
+	size_t i;
+	u8 *ptr = dest;
+	for(i = 0; i < num; i++, ptr++) {
+		*ptr = value;
+	}
+	return dest;
+}
+
+size_t strlen(const char *str)
+{
+	const char *ptr = str;
+	while(*ptr && *++ptr);
+	return ptr - str;
+}
--- a/src/osTest.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-#include <svcCalls.h>
-
-void start()
-{
-	printLine("Hello world!  This is a longer message, it is longer...\n", 56);
-	printLine("Hello world!\n", 13);
-	printLine("TEST\n", 5);
-	exit();
-	for(;;)
-		;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/hello.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,7 @@
+#include <svcCalls.h>
+
+void start()
+{
+	printLine("Hello world!\n", 13);
+	exit();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/ls.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,16 @@
+#include <os/fsStructs.h>
+#include <svcCalls.h>
+
+void start()
+{
+	size_t i;
+	for(i = 0; 1; i++) {
+		Direntry de;
+		ErrCode err = finfo(i, &de);
+		if(err == mkError(MODFS, OUTOFRANGE, WARN)) break;
+		else if(isError(err)) exit();
+		printLine(de.fname, 28);
+		printLine("\n", 1);
+	}
+	exit();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/math.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,1 @@
+../os/math.c
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/operations.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,114 @@
+#include <operations.h>
+#include <std.h>
+#include <string.h>
+#include <stack.h>
+#include <math.h>
+
+const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "**", "dup", "drop", "log", "exp", "swap"};
+void (*operation[MAXOP])(Stack*) = {op_print, op_add, op_sub, op_mult,
+	op_div, op_pow, op_dup, op_drop, op_log, op_exp, op_swap};
+
+static void error_unk()
+{
+	sPrint("ERROR: CAUSE UNKNOWN\n");
+}
+
+static void error_small()
+{
+	sPrint("ERROR: STACK TOO SMALL\n");
+}
+
+void op_math1(Stack *stack, eltType (*mathop)(eltType))
+{
+	if(stack->size >= 1) {
+		eltType val;
+		if(pop(stack, &val)) error_unk();
+		else push(stack, mathop(val));
+	} else error_small();
+}
+
+void op_math2(Stack *stack, eltType (*mathop)(eltType, eltType))
+{
+	if(stack->size >= 2) {
+		eltType val1, val2;
+		if(pop(stack, &val2) || pop(stack, &val1)) error_unk();
+		else push(stack, mathop(val1, val2));
+	} else error_small();
+}
+
+void op_print(Stack *stack)
+{
+	if(stack->size >= 1) {
+		char output[30];
+		eltType val;
+		if(pop(stack, &val)) error_unk();
+		else sPrint(strcat(ftoa(val, output, 10), "\n"));
+	} else error_small();
+}
+
+void op_add(Stack *stack)
+{
+	op_math2(stack, math_add);
+}
+
+void op_sub(Stack *stack)
+{
+	op_math2(stack, math_sub);
+}
+
+void op_mult(Stack *stack)
+{
+	op_math2(stack, math_mult);
+}
+
+void op_div(Stack *stack)
+{
+	op_math2(stack, math_div);
+}
+
+void op_pow(Stack *stack)
+{
+	op_math2(stack, pow);
+}
+
+void op_dup(Stack *stack)
+{
+	if(stack->size >= 1) {
+		eltType val;
+		if(pop(stack, &val)) error_unk();
+		else {
+			push(stack, val);
+			push(stack, val);
+		}
+	} else error_small();
+}
+
+void op_drop(Stack *stack)
+{
+	if(stack->size >= 1) {
+		eltType val;
+		pop(stack, &val);
+	} else error_small();
+}
+
+void op_log(Stack *stack)
+{
+	op_math1(stack, log);
+}
+
+void op_exp(Stack *stack)
+{
+	op_math1(stack, exp);
+}
+
+void op_swap(Stack *stack)
+{
+	if(stack->size >= 2) {
+		eltType val1, val2;
+		if(pop(stack, &val2) || pop(stack, &val1)) error_unk();
+		else {
+			push(stack, val2);
+			push(stack, val1);
+		}
+	} else error_small();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/stack.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,40 @@
+#include <stack.h>
+#include <std.h>
+
+short pop(Stack *stack, eltType *value)
+{
+	if(stack->top == NULL) {
+		return -1; //failure
+	} else {
+		StackElt *tip = stack->top;
+		*value = tip->elt;
+		stack->top = tip->next;
+		free(tip);
+		stack->size--;
+		return 0; //success
+	}
+}
+
+void push(Stack *stack, eltType val)
+{
+	StackElt *new = malloc(sizeof(StackElt));
+	new->elt = val;
+	new->next = stack->top;
+	stack->top = new;
+	stack->size++;
+}
+
+Stack* stack_init()
+{
+	Stack *new = malloc(sizeof(Stack));
+	new->top = NULL;
+	return new;
+}
+
+void stack_destroy(Stack *stack)
+{
+	eltType *val = malloc(sizeof(eltType));
+	while(stack->top) pop(stack, val);
+	free(stack);
+	free(val);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/std.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,1 @@
+../os/std.c
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/stdio.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,1 @@
+../os/stdio.c
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/prog/string.c	Sat Jun 11 14:13:21 2011 -0400
@@ -0,0 +1,1 @@
+../os/string.c
\ No newline at end of file
--- a/src/shell.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-#include <std.h>
-#include <string.h>
-#include <stdio.h>
-#include <error.h>
-#include <os/fs.h>
-#include <die.h>
-#include <elf.h>
-#include <psw.h>
-#include <svc.h>
-#include <os/scall.h>
-
-#define PROGRAM_STACK_START 0x400000 - 160 //this is correct, ignore the documentation
-#define KERNEL_STACK_START 0x380000 - 160 //this is correct, ignore the documentation
-
-void start(u64 __memsize)
-{
-	init_all(__memsize);
-	if(isError(init_fs(0x100, __memsize))) die();
-	char buffer[1024];
-	ErrCode err;
-	//set up the svc call handler
-	set_svc_handler(svc_handler, (void*)KERNEL_STACK_START);
-	
-	while(1) {
-		sPrint("$ ");
-		sGet(buffer, 78);
-		u32 fid;
-		if(isError(err = lookupFile(buffer, &fid))) {
-			if(errCode(err) == NOTFILE) sPrint("ERROR: command not found\n");
-			continue;
-		} else {
-			u32 fileSize;
-			if(isError(getFileSize(fid, &fileSize)));
-			void *data = malloc(fileSize);
-			if(!data) continue;
-			if(isError(getFileData(fid, data))) goto loopCont;
-			Elf64_Ehdr *eHdr = data;
-			if(eHdr->e_ident[EI_MAG0] != ELFMAG0 || eHdr->e_ident[EI_MAG1] != ELFMAG1 ||
-					eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3)
-				goto loopCont;
-			if(eHdr->e_ident[EI_CLASS] != ELFCLASS64) goto loopCont;
-			int i;
-			for(i = 0; i < eHdr->e_phnum; i++) {
-				Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr->e_phoff) + i);
-				if(pHdr->p_type == PT_LOAD) {
-					void *fromLoc = data + pHdr->p_offset;
-					void *toLoc = (void*)pHdr->p_vaddr;
-					memcpy(toLoc, fromLoc, pHdr->p_filesz);
-					size_t diff = pHdr->p_memsz - pHdr->p_filesz;
-					/*
-					sprintf(buffer, "file: %x, mem: %x, toLoc: %x\n", pHdr->p_filesz, pHdr->p_memsz, toLoc);
-					sPrint(buffer);
-					*/
-					if(diff) memset(toLoc + pHdr->p_filesz, 0, diff);
-				}
-			}
-
-			Psw psw;
-			u64 registers[16];
-			memset(&psw, 0, sizeof(psw));
-			memset(registers, 0, sizeof(registers));
-			registers[15] = PROGRAM_STACK_START;
-			psw.p = 1;
-			psw.ea = 1;
-			psw.ba = 1;
-			psw.ptr = eHdr->e_entry;
-			/*
-			sprintf(buffer, "Going to memory address %x\n", eHdr->e_entry);
-			sPrint(buffer);
-			*/
-			swapcontext(&shellPsw, shellRegisters, &psw, registers);
-		loopCont:
-			free(data);
-		}
-	}
-
-//END:
-	sPrint("DONE\n");
-	die();
-}
--- a/src/stack.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-#include <stack.h>
-#include <std.h>
-
-short pop(Stack *stack, eltType *value)
-{
-	if(stack->top == NULL) {
-		return -1; //failure
-	} else {
-		StackElt *tip = stack->top;
-		*value = tip->elt;
-		stack->top = tip->next;
-		free(tip);
-		stack->size--;
-		return 0; //success
-	}
-}
-
-void push(Stack *stack, eltType val)
-{
-	StackElt *new = malloc(sizeof(StackElt));
-	new->elt = val;
-	new->next = stack->top;
-	stack->top = new;
-	stack->size++;
-}
-
-Stack* stack_init()
-{
-	Stack *new = malloc(sizeof(Stack));
-	new->top = NULL;
-	return new;
-}
-
-void stack_destroy(Stack *stack)
-{
-	eltType *val = malloc(sizeof(eltType));
-	while(stack->top) pop(stack, val);
-	free(stack);
-	free(val);
-}
--- a/src/std.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,193 +0,0 @@
-#include <std.h>
-#include <string.h>
-
-void init_all(u64 __memsize)
-{
-	init_io_int();
-	init_console();
-	malloc_init(__memsize - HEAP_START);
-}
-
-double abs(double num)
-{
-	if(num < 0) return num*-1;
-	else return num;
-}
-
-char* itoa(s64 n, char *a, unsigned short base)
-{
-	char *ret = a;
-	if(n < 0) {
-		*a++ = '-';
-		n *= -1;
-	}
-	char *b = a;
-	if(!n) *b++ = '0';
-	for(; n; b++) {
-		int temp = n%base;
-		if(temp < 10) *b = '0' + temp;
-		else *b = 'a' + temp - 10;
-		n = n/base;
-	}
-	*b-- = '\0';
-	for(; a < b; a++, b--) { //reverse
-		char temp = *b;
-		*b = *a;
-		*a = temp;
-	}
-	return ret;
-}
-
-char* ftoa(double x, char *a, unsigned int prec)
-{
-	char *ret = a;
-	if(x == INF || x == -INF) {
-		strcpy(a, "INF");
-	} else if(x != x) { //NAN != NAN
-		strcpy(a, "NAN");
-	} else {
-		s64 n = (s64) x; //integer part
-		double d = abs(x - (double) n); //fractional part;
-		itoa(n, a, 10);
-		if(prec) { //only do the decimal part if decimal parts were asked for
-			while(*a && *++a); //get to the null character from itoa
-			int i; //counter variable for the for loop
-			*a++ = '.'; //put the decimal in place
-			for(i = 0; i < prec; i++) {
-				d *= 10; //the integer part is the decimal digit
-				*a++ = ((int) d) + '0'; //add the integer part of d to the string
-				d -= (int) d; //chop off the integer part
-			} a--; //move back to the last digit
-			while(*a != '.') {
-				if(*a == '0') {
-					a--;
-					continue;
-				} else if(*a > '0' && *a <= '9') {
-					a++;
-					break;
-				} else {
-					sPrint("ERROR: SOMETHING IS VERY WRONG\n");
-					break;
-				}
-			}
-			*a = '\0';
-		}
-	}
-	return ret;
-}
-
-s64 atoi(char *a)
-{
-	short neg = 0;
-	s64 n = 0;
-	if(*a == '-') {
-		neg = 1;
-		a++;
-	} else if(*a == '+') a++;
-	while(*a >= '0' && *a <= '9')
-		n = n*10 + (*a++ - '0');
-	if(neg) n *= -1;
-	return n;
-}
-
-double atof(char *a)
-{
-	s64 n = atoi(a);
-	double x = 0;
-	double dec = .1;
-	short neg = 0;
-	if(*a == '-') {
-		neg = 1;
-		a++;
-	}
-	while(*a != '.') {
-		if(!(*a >= '0' && *a <= '9') && *a != '-') return n;
-		a++;
-	} a++; //a will be immediately after the decimal point
-	while(*a >= '0' && *a <= '9') { //goes through the decimal part
-		x += (*a - '0')*dec;
-		dec *= .1;
-		a++;
-	}
-	if(neg) x*=-1;
-	return n + x;
-}
-
-void sPrint(char *a)
-{
-	char *b = a;
-	while(*b && *++b);
-	do {
-		putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); //TODO supervisor call
-		a += CON_LEN;
-	} while(a < b);
-}
-
-char* sGet(char *a, unsigned int n)
-{
-	int length = getline(a, n); //TODO supervisor call
-	a[(length < n)?length:n - 1] = '\0';
-	return a;
-}
-
-//DYNAMIC MEMORY
-
-static Header base;
-static Header *allocp = NULL; //the location of the last known free block
-
-void malloc_init(size_t memSize) //TODO get rid of this
-{
-	allocp = &base;
-	allocp->size = 0;
-	allocp->next = (void*)HEAP_START;
-	allocp->next->next = &base;
-	allocp->next->size = (memSize - HEAP_START)/sizeof(blockUnit);
-	if((sizeof(blockUnit)%sizeof(u64))) {
-		sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n");
-	}
-}
-
-void* malloc(size_t size)
-{
-	if(allocp == NULL) return NULL;
-	size_t nUnits = ((size + sizeof(Header)) + sizeof(blockUnit) - 1)/sizeof(blockUnit);
-	Header *prev = allocp;
-	Header *cur, *temp;
-	for(cur = prev->next;; prev = cur, cur = cur->next) {
-		if(cur->size >= nUnits) {
-			if(cur->size == nUnits) {
-				prev->next = cur->next;
-			} else {
-				temp = cur + nUnits; //This requires sizeof(blockUnit) == sizeof(Header).  TODO fix
-				temp->size = cur->size - nUnits;
-				temp->next = cur->next;
-				prev->next = temp;
-				cur->size = nUnits;
-			}
-			allocp = prev;
-			return (void*)(cur + 1);
-		} else if(cur == allocp) { //We went back to the start...
-			//TODO ask the OS for more momory
-			return NULL;
-		}
-	}
-}
-
-void free(void *ptr)
-{
-	Header *toFree = (Header *)ptr - 1;
-	Header *scan;
-	for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next)
-		if(scan->next < scan && (toFree > scan || toFree < scan->next)) break;
-	toFree->next = scan->next;
-	scan->next = toFree;
-	if(scan + scan->size == toFree) {
-		scan->size += toFree->size;
-		scan->next = toFree->next;
-		toFree = scan;
-	}
-	if(toFree + toFree->size == toFree->next) {
-		toFree->size += toFree->next->size;
-		toFree->next = toFree->next->next;
-	}
-}
--- a/src/stdio.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-#include <stdio.h>
-#include <std.h>
-#include <string.h>
-#include <stdarg.h>
-
-int sprintf(char *str, const char *fmt, ...)
-{
-	va_list ap;
-	va_start(ap, fmt);
-	*str = '\0';
-	while(*fmt) {
-		if(*fmt != '%') {
-			*str++ = *fmt++;
-		} else {
-			switch(*(fmt + 1)) {
-				case '%':
-					*str++ = '%';
-					break;
-				case 'x':
-					{
-						char buffer[32];
-						long long num = va_arg(ap, long long);
-						itoa(num, buffer, 16);
-						memcpy(str, buffer, strlen(buffer));
-						str += strlen(buffer);
-					}
-					break;
-				case 'i':
-					{
-						char buffer[32];
-						long long num = va_arg(ap, long long);
-						itoa(num, buffer, 10);
-						memcpy(str, buffer, strlen(buffer));
-						str += strlen(buffer);
-					}
-					break;
-				case 'f':
-					{
-						char buffer[32];
-						double num = va_arg(ap, double);
-						ftoa(num, buffer, 10);
-						memcpy(str, buffer, strlen(buffer));
-						str += strlen(buffer);
-					}
-					break;
-				case 's':
-					{
-						char *text = va_arg(ap, char*);
-						memcpy(str, text, strlen(text));
-						str += strlen(text);
-					}
-					break;
-			}
-			fmt += 2;
-		}
-	}
-	*str = '\0';
-	return 0;
-}
--- a/src/string.c	Sat Jun 11 12:42:18 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-int strcmp(const char *a, const char *b)
-{
-	while(1) {
-		if(*a - *b) return *a - *b;
-		if(*a == '\0') return 0;
-		a++;
-		b++;
-	}
-	return -1;
-}
-
-void strcpy(char *dest, const char *src)
-{
-	while((*dest++ = *src++));
-}
-
-char* strcat(char *dest, const char *src)
-{
-	char *ret = dest;
-	while(*dest&& *++dest); //get to null in first string
-	while((*dest++ = *src++));
-	return ret;
-}
-
-void* memcpy(void *dest, const void *src, size_t num)
-{
-	size_t i;
-	const u8 *from = src;
-	u8 *to = dest;
-	for(i = 0; i < num; i++)
-		to[i] = from[i];
-	return dest;
-}
-
-void* memset(void *dest, u8 value, size_t num)
-{
-	size_t i;
-	u8 *ptr = dest;
-	for(i = 0; i < num; i++, ptr++) {
-		*ptr = value;
-	}
-	return dest;
-}
-
-size_t strlen(const char *str)
-{
-	const char *ptr = str;
-	while(*ptr && *++ptr);
-	return ptr - str;
-}