changeset 25:2b19746a4e97

Added stack_destroy, separated programs, added swap operation stack_destroy unallocates all space that is currently being used by a given stack. I now have the rpn program and my memory test programs in separate files. The swap operation is exactly what one would expect.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Wed, 16 Mar 2011 17:29:25 -0400
parents 45a80ea314ae
children c1ad124f2aaf
files .hgignore Makefile hercules/dynamic.cnf hercules/herc.cnf hercules/sarpn.cnf include/operations.h include/stack.h src/dynamic.c src/init.c src/operations.c src/sarpn.c src/stack.c
diffstat 12 files changed, 149 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Mar 16 12:00:43 2011 -0400
+++ b/.hgignore	Wed Mar 16 17:29:25 2011 -0400
@@ -2,6 +2,7 @@
 \.rto$
 ^ipl/ipl_ccws\.S$
 ^sarpn$
+^dynamic$
 ^loader\.bin$
 ^cscope\.out$
 \.swp$
--- a/Makefile	Wed Mar 16 12:00:43 2011 -0400
+++ b/Makefile	Wed Mar 16 17:29:25 2011 -0400
@@ -10,9 +10,10 @@
 CXXFLAGS=$(CFLAGS)
 LDFLAGS=-m elf64_s390
 
-BINS=sarpn
+BINS=sarpn dynamic
 
-sarpn_OBJS=src/init.o arch/io.o src/std.o src/stack.o src/operations.o src/math.o
+sarpn_OBJS=src/sarpn.o arch/io.o src/std.o src/stack.o src/operations.o src/math.o
+dynamic_OBJS=src/dynamic.o arch/io.o src/std.o
 
 .PHONY: all build clean tags
 
@@ -22,6 +23,7 @@
 
 clean:
 	rm -f $(sarpn_OBJS)
+	rm -f $(dynamic_OBJS)
 	rm -f $(BINS)
 	rm -f loader.bin ipl/*.o ipl/*.rto ipl/ipl_ccws.S cscope.out
 
@@ -31,6 +33,8 @@
 
 sarpn: $(sarpn_OBJS)
 	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
+dynamic: $(dynamic_OBJS)
+	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
 
 %.o: %.S
 	$(AS) -m64 -o $@ $<
@@ -78,8 +82,9 @@
 
 # DO NOT DELETE
 
-src/init.o: include/std.h include/operations.h include/stack.h
+src/dynamic.o: include/std.h include/operations.h include/stack.h
 src/operations.o: include/operations.h include/std.h include/stack.h
 src/operations.o: include/math.h
+src/sarpn.o: include/std.h include/operations.h include/stack.h
 src/stack.o: include/stack.h include/std.h
 src/std.o: include/std.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hercules/dynamic.cnf	Wed Mar 16 17:29:25 2011 -0400
@@ -0,0 +1,18 @@
+CPUSERIAL 314359        # CPU serial number
+CPUMODEL  2097          # CPU model number
+MAINSIZE  128           # Main storage size in megabytes
+XPNDSIZE  0             # Expanded storage size in megabytes
+CNSLPORT  3270          # TCP port number to which consoles connect
+NUMCPU    1             # Number of CPUs
+#OSTAILOR  QUIET         # OS tailoring
+OSTAILOR  NULL          # OS tailoring
+PANRATE   SLOW          # Panel refresh rate
+
+# .-----------------------Device number
+# |     .-----------------Device type
+# |     |       .---------File name and parameters
+# |     |       |
+# V     V       V
+#---    ----    --------------------
+0009	3215	
+000C    3505	../loader.bin ../dynamic ebcdic multifile intrq
--- a/hercules/herc.cnf	Wed Mar 16 12:00:43 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,18 +0,0 @@
-CPUSERIAL 314359        # CPU serial number
-CPUMODEL  2097          # CPU model number
-MAINSIZE  128           # Main storage size in megabytes
-XPNDSIZE  0             # Expanded storage size in megabytes
-CNSLPORT  3270          # TCP port number to which consoles connect
-NUMCPU    1             # Number of CPUs
-#OSTAILOR  QUIET         # OS tailoring
-OSTAILOR  NULL          # OS tailoring
-PANRATE   SLOW          # Panel refresh rate
-
-# .-----------------------Device number
-# |     .-----------------Device type
-# |     |       .---------File name and parameters
-# |     |       |
-# V     V       V
-#---    ----    --------------------
-0009	3215	
-000C    3505	../loader.bin ../sarpn ebcdic multifile intrq
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hercules/sarpn.cnf	Wed Mar 16 17:29:25 2011 -0400
@@ -0,0 +1,18 @@
+CPUSERIAL 314359        # CPU serial number
+CPUMODEL  2097          # CPU model number
+MAINSIZE  128           # Main storage size in megabytes
+XPNDSIZE  0             # Expanded storage size in megabytes
+CNSLPORT  3270          # TCP port number to which consoles connect
+NUMCPU    1             # Number of CPUs
+#OSTAILOR  QUIET         # OS tailoring
+OSTAILOR  NULL          # OS tailoring
+PANRATE   SLOW          # Panel refresh rate
+
+# .-----------------------Device number
+# |     .-----------------Device type
+# |     |       .---------File name and parameters
+# |     |       |
+# V     V       V
+#---    ----    --------------------
+0009	3215	
+000C    3505	../loader.bin ../sarpn ebcdic multifile intrq
--- a/include/operations.h	Wed Mar 16 12:00:43 2011 -0400
+++ b/include/operations.h	Wed Mar 16 17:29:25 2011 -0400
@@ -4,7 +4,7 @@
 #include <std.h>
 #include <stack.h>
 
-enum Operation {PRINT, ADD, SUB, MULT, DIV, POW, DUP, DROP, LOG, EXP, MAXOP};
+enum Operation {PRINT, ADD, SUB, MULT, DIV, POW, DUP, DROP, LOG, EXP, SWAP, MAXOP};
 extern const char operationNames[MAXOP][10];
 extern void (*operation[MAXOP])(Stack*);
 
@@ -21,5 +21,6 @@
 void op_drop(Stack *stack);
 void op_log(Stack *stack);
 void op_exp(Stack *stack);
+void op_swap(Stack *stack);
 
 #endif //__OPERATIONS_H
--- a/include/stack.h	Wed Mar 16 12:00:43 2011 -0400
+++ b/include/stack.h	Wed Mar 16 17:29:25 2011 -0400
@@ -23,6 +23,6 @@
 eltCon pop(Stack *stack);
 void push(Stack *stack, eltType val);
 Stack* stack_init();
-//void stack_destroy(Stack *stack); //TODO
+void stack_destroy(Stack *stack);
 
 #endif //__STACK_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dynamic.c	Wed Mar 16 17:29:25 2011 -0400
@@ -0,0 +1,53 @@
+/*
+ * This is where everything starts
+ */
+
+#include <std.h>
+#include <operations.h>
+#include <stack.h>
+
+void dumpBuffer(char *a, int b)
+{
+	int i;
+	for(i = 0; i < b; i++) {
+		int foo = (int) a[i];
+		char c[10];
+		itoa(foo, c);
+		sPrint(c);
+		sPrint(" ");
+		if(!((i + 1)%20)) sPrint("\n");
+	}
+}
+
+void start(u64 __memsize)
+{
+	malloc_init(__memsize - HEAP_START);
+	size_t n;
+	char *buffer;
+	char input[30];
+
+	while(1) {
+		sPrint("How long do you want the string? ");
+		n = atoi(sGet(input, 30));
+
+		buffer = (char*) malloc(n);
+		if (!buffer) { //allocation failed, too large
+			sPrint("ERROR\n");
+			break;
+		}
+
+//		size_t i;
+//		for (i = 0; i < n - 1; i++)
+//			buffer[i]='a'; //I need to make rand...
+//		buffer[n - 1]='\0';
+
+//		sPrint(buffer), sPrint("\n");
+//		dumpBuffer(buffer, n);
+
+		sPrint(append(itoa((unsigned long)buffer, input), "\n"));
+		free(buffer);
+	}
+
+	for(;;)
+		;
+}
--- a/src/init.c	Wed Mar 16 12:00:43 2011 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,68 +0,0 @@
-/*
- * This is where everything starts
- */
-
-#include <std.h>
-#include <operations.h>
-#include <stack.h>
-
-void dumpBuffer(char *a, int b)
-{
-	int i;
-	for(i = 0; i < b; i++) {
-		int foo = (int) a[i];
-		char c[10];
-		itoa(foo, c);
-		sPrint(c);
-		sPrint(" ");
-		if(!((i + 1)%20)) sPrint("\n");
-	}
-}
-
-void start(u64 __memsize)
-{
-	malloc_init(__memsize - HEAP_START);
-	/*
-  int i, n;
-  char *buffer;
-	char input[30];
-
-	while(1) {
-		sPrint("How long do you want the string? ");
-		n = atoi(sGet(input, 30));
-
-		buffer = (char*) malloc(n);
-		if (!buffer) { //allocation failed, too large
-			sPrint("ERROR\n");
-			break;
-		}
-
-//		for (i = 0; i < n - 1; i++)
-//			buffer[i]='a'; //I need to make rand...
-//		buffer[n - 1]='\0';
-
-//		sPrint(buffer), sPrint("\n");
-//		dumpBuffer(buffer, n);
-
-		sPrint(append(itoa((unsigned long)buffer, input), "\n"));
-		free(buffer);
-	}
-*/
-
-	Stack *stack = stack_init();
-	while(1) {
-		int opVal;
-		char input[30];
-		sPrint("Please enter an operation or value: ");
-		opVal = arrayLookup(sGet(input, 30), operationNames, MAXOP);
-		if(opVal != MAXOP) {
-			(*operation[opVal]) (stack); //call the function array
-		} else {
-			push(stack, atof(input));
-		}
-	}
-
-	for(;;)
-		;
-//	stack_destroy(stack);
-}
--- a/src/operations.c	Wed Mar 16 12:00:43 2011 -0400
+++ b/src/operations.c	Wed Mar 16 17:29:25 2011 -0400
@@ -3,9 +3,9 @@
 #include <stack.h>
 #include <math.h>
 
-const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "**", "dup", "drop", "log", "exp"};
+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_div, op_pow, op_dup, op_drop, op_log, op_exp, op_swap};
 
 void op_math1(Stack *stack, eltType (*mathop)(eltType))
 {
@@ -63,9 +63,8 @@
 	eltCon con = pop(stack);
 	if(con.error) sPrint("ERROR, no values on stack\n");
 	else {
-		eltType val = con.val;
-		push(stack, val);
-		push(stack, val);
+		push(stack, con.val);
+		push(stack, con.val);
 	}
 }
 
@@ -83,3 +82,12 @@
 {
 	op_math1(stack, math_exp);
 }
+
+void op_swap(Stack *stack)
+{
+	eltCon con2 = pop(stack);
+	eltCon con1 = pop(stack);
+	if(con1.error || con2.error) sPrint("ERROR, stack not large enough\n");
+	push(stack, con2.val);
+	push(stack, con1.val);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sarpn.c	Wed Mar 16 17:29:25 2011 -0400
@@ -0,0 +1,29 @@
+/*
+ * This is where everything starts
+ */
+
+#include <std.h>
+#include <operations.h>
+#include <stack.h>
+
+void start(u64 __memsize)
+{
+	malloc_init(__memsize - HEAP_START);
+
+	Stack *stack = stack_init();
+	while(1) {
+		int opVal;
+		char input[30];
+		sPrint("Please enter an operation or value: ");
+		opVal = arrayLookup(sGet(input, 30), operationNames, MAXOP);
+		if(opVal != MAXOP) {
+			(*operation[opVal]) (stack); //call the function array
+		} else {
+			push(stack, atof(input));
+		}
+	}
+	stack_destroy(stack);
+
+	for(;;)
+		;
+}
--- a/src/stack.c	Wed Mar 16 12:00:43 2011 -0400
+++ b/src/stack.c	Wed Mar 16 17:29:25 2011 -0400
@@ -30,3 +30,9 @@
 	new->top = NULL;
 	return new;
 }
+
+void stack_destroy(Stack *stack)
+{
+	while(stack->top) pop(stack);
+	free(stack);
+}