diff --git a/SDA.c b/SDA.c index 1c60f2a..ff50bd2 100644 --- a/SDA.c +++ b/SDA.c @@ -24,4 +24,4 @@ void printSDA(struct sda s) { } } -#endif \ No newline at end of file +#endif diff --git a/codeReader.c b/codeReader.c index a698b10..d94d25b 100644 --- a/codeReader.c +++ b/codeReader.c @@ -12,7 +12,7 @@ unsigned int fromFile(char *path, struct program program) { FILE *fptr; fptr = fopen(path, "r+b"); if (fptr == NULL) { - printf("Error: cannot open code file %s", path); + printf("Error: cannot open code file %s\n", path); exit(EXIT_FAILURE); } unsigned int buffer[4]; diff --git a/nja b/nja old mode 100644 new mode 100755 diff --git a/nja-mac b/nja-mac new file mode 100755 index 0000000..2a7ad14 Binary files /dev/null and b/nja-mac differ diff --git a/njvm.c b/njvm.c index 8dd700b..5d77770 100644 --- a/njvm.c +++ b/njvm.c @@ -7,10 +7,6 @@ #include "codeReader.c" #include "SDA.c" -//Comment to disable debug - -#define DEBUG - // Stack struct stack stack; #define SIZE 1000 @@ -92,7 +88,7 @@ void execute(struct program program) { stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack); break; case POPL: - push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); + push(&stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); break; } } @@ -100,20 +96,18 @@ void execute(struct program program) { return; } -#ifdef DEBUG - void tests(void) { printf("Runnig debug mode\n"); - int temp = fromFile("./prog1.bin", program); + int temp = fromFile("./prog2.bin", program); int sizeSDA = temp; unsigned int s[sizeSDA]; sda.size = &temp; sda.sda = s; printProgram(program); + execute(program); + printSDA(sda); } -#endif /* ifdef DEBUG */ - int main(int argc, char *argv[]) { // Initialize the Stack int size = SIZE; @@ -131,41 +125,49 @@ int main(int argc, char *argv[]) { program.program = p; program.saveProgram = &saveProgram; + // Initialize runtime variables + int debug = 0; + int run = 0; + int sizeSDA; -#ifdef DEBUG - tests(); -#endif /* ifdef DEBUG */ if (argc > 1) { - if (strcmp(argv[1], "--version") == 0) { - version(); - } else if (strcmp(argv[1], "--help") == 0) { - help(); - } else if (strcmp(argv[1], "--prog1") == 0) { - copyToProgram(code1, sizeof(code1) / sizeof(code1[0]), program); - goto run; - } else if (strcmp(argv[1], "--prog2") == 0) { - copyToProgram(code2, sizeof(code2) / sizeof(code2[0]), program); - goto run; - } else if (strcmp(argv[1], "--prog3") == 0) { - copyToProgram(code3, sizeof(code3) / sizeof(code3[0]), program); - goto run; - } else { - printf("unknown command line argument '%s', try './njvm --help'", argv[1]); + for (int i = 1; i < argc; ++i) { + if (strcmp(argv[i], "--debug") == 0) { + debug = 1; + } else if (strcmp(argv[i], "--version") == 0) { + version(); + return 0; + } else if (strcmp(argv[i], "--help") == 0) { + help(); + return 0; + } else { + sizeSDA = fromFile(argv[i], program); + run = 1; + } } - } else { - run: -// Started - if (*program.saveProgram == 1) { - printf("Ninja Virtual Machine started\n"); - printProgram(program); - execute(program); - printSDA(sda); - } else { - printf("Error: no code file specified\n"); - return 1; - } -// Stopped + } + + /* + * Debug mode + */ + if (debug) { + tests(); + } + + /* + * Run program + */ + if (run) { + printf("Ninja Virtual Machine started\n"); + unsigned int s[sizeSDA]; + sda.size = &sizeSDA; + sda.sda = s; + printProgram(program); + execute(program); + if (debug == 1) printSDA(sda); printf("Ninja Virtual Machine stopped\n"); - return 0; + } else { + printf("Error: no code file specified\n"); + return 1; } } diff --git a/prog1.bin b/prog1.bin deleted file mode 100644 index 8cc0eb1..0000000 Binary files a/prog1.bin and /dev/null differ