#include #include #include "instruktion.c" #include "code.c" #include "stack.c" #include "program.c" #include "codeReader.c" #include "SDA.c" // Debug int debug = 1; // Stack struct stack stack; #define SIZE 1000 // Program struct program program; // SDA struct sda sda; unsigned fp; void version(void) { printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__); } void help(void) { printf("Usage: ./njvm [options] \n\t--debug\tstart virtual machine in debug mode\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n"); } void execute(struct program program) { int i; int intInput; unsigned int temp; char charInput; for (i = 0; i < *program.size; ++i) { switch (program.program[i] >> 24) { case HALT: if(debug == 1) printf("halt\n"); goto end; case PUSHC: if(debug == 1) printf("pushc\n"); push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i]))); break; case ADD: if(debug == 1) printf("add\n"); push(stack, pop(stack) + pop(stack)); break; case SUB: if(debug == 1) printf("sub\n"); temp = pop(stack); push(stack, pop(stack) - temp); break; case MUL: if(debug == 1) printf("mul\n"); push(stack, pop(stack) * pop(stack)); break; case DIV: if(debug == 1) printf("div\n"); temp = pop(stack); push(stack, pop(stack) / temp); break; case MOD: if(debug == 1) printf("mod\n"); temp = pop(stack); push(stack, pop(stack) % temp); break; case RDINT: if(debug == 1) printf("rdint\n"); scanf("%i", &intInput); push(stack, intInput); break; case WRINT: if(debug == 1) printf("wrint\n"); printf("%i", pop(stack)); break; case RDCHR: if(debug == 1) printf("rdchr\n"); scanf("%c", &charInput); push(stack, charInput); break; case WRCHR: if(debug == 1) printf("wrchr\n"); printf("%c", pop(stack)); break; case PUSHG: if(debug == 1) printf("pushg\n"); push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)); break; case POPG: if(debug == 1) printf("popg\n"); setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda); break; case ASF: if(debug == 1) printf("asf\n"); push(stack, *stack.current); fp = *stack.current - 1; *stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i])); break; case RSF: if(debug == 1) printf("rsf\n"); *stack.current = fp + 2; fp = pop(stack); case POPL: if(debug == 1) printf("popl\n"); stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack); break; case PUSHL: if(debug == 1) printf("pushl\n"); push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); break; case NE: if(debug == 1) printf("ne\n"); if (pop(stack) != pop(stack)) push(stack, 1); else push(stack, 0); break; case EQ: if(debug == 1) printf("eq\n"); if (pop(stack) == pop(stack)) push(stack, 1); else push(stack, 0); break; case LT: if(debug == 1) printf("lt\n"); temp = pop(stack); if (pop(stack) < temp) push(stack, 1); else push(stack, 0); break; case LE: if(debug == 1) printf("le\n"); temp = pop(stack); if (pop(stack) <= temp) push(stack, 1); else push(stack, 0); break; case GT: if(debug == 1) printf("gt\n"); temp = pop(stack); if (pop(stack) > temp) push(stack, 1); else push(stack, 0); break; case GE: if(debug == 1) printf("ge\n"); temp = pop(stack); if (pop(stack) >= temp) push(stack, 1); else push(stack, 0); break; case BRF: if(debug == 1) printf("brf\n"); if(pop(stack)==0) i = SIGN_EXTEND(IMMEDIATE(program.program[i])); break; case BRT: if(debug == 1) printf("brt\n"); if(pop(stack)==1) i = SIGN_EXTEND(IMMEDIATE(program.program[i])); case JMP: if(debug == 1) printf("jmp\n"); i = SIGN_EXTEND(IMMEDIATE(program.program[i])); break; } } end: return; } void tests(void) { } int main(int argc, char *argv[]) { // Initialize the Stack int size = SIZE; int current = 0; unsigned int s[SIZE]; stack.size = &size; stack.current = ¤t; stack.stack = s; // Initialize ProgrammSpeicher int psize = 1000; int saveProgram = 0; unsigned int p[1000]; program.size = &psize; program.program = p; program.saveProgram = &saveProgram; if (argc > 2) { if (strcmp(argv[2], "--debug")) { debug = 1; } } if (debug){ tests(); } if (argc > 1) { if (strcmp(argv[1], "--version") == 0) { version(); } else if (strcmp(argv[1], "--help") == 0) { help(); } else { int temp = fromFile(argv[1], program); int sizeSDA = temp; unsigned int s[sizeSDA]; sda.size = &temp; sda.sda = s; execute(program); } } else { return 0; } }