141 lines
3.4 KiB
C
141 lines
3.4 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "instruktion.c"
|
|
#include "code.c"
|
|
#include "stack.c"
|
|
#include "program.c"
|
|
|
|
|
|
//Comment to disable debug
|
|
|
|
#define DEBUG
|
|
|
|
|
|
// Stack
|
|
struct stack stack;
|
|
#define SIZE 1000
|
|
|
|
// ProgrammSpeicher
|
|
struct program program;
|
|
|
|
unsigned int* programmSpeicher;
|
|
|
|
void version(void) {
|
|
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
|
|
}
|
|
|
|
void help(void) {
|
|
printf("usage: ./njvm [option] [option] ...\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n");
|
|
}
|
|
|
|
void execute(void) {
|
|
int i = 0;
|
|
int intInput;
|
|
int temp;
|
|
char charInput;
|
|
while (1) {
|
|
|
|
switch (programmSpeicher[i] >> 24) {
|
|
case HALT:
|
|
goto end;
|
|
break;
|
|
case PUSHC:
|
|
push(stack,IMMEDIATE(programmSpeicher[i]));
|
|
break;
|
|
case ADD:
|
|
push(stack,pop(stack)+pop(stack));
|
|
break;
|
|
case SUB:
|
|
temp = pop(stack);
|
|
push(stack,pop(stack) - temp);
|
|
break;
|
|
case MUL:
|
|
push(stack,pop(stack)*pop(stack));
|
|
break;
|
|
case DIV:
|
|
temp = pop(stack);
|
|
push(stack,pop(stack)/temp);
|
|
break;
|
|
case MOD:
|
|
temp = pop(stack);
|
|
push(stack,pop(stack)%temp);
|
|
break;
|
|
case RDINT:
|
|
scanf("%i",&intInput);
|
|
push(stack,intInput);
|
|
break;
|
|
case WRINT:
|
|
printf("%i",pop(stack));
|
|
break;
|
|
case RDCHR:
|
|
scanf("%c",&charInput);
|
|
push(stack,charInput);
|
|
break;
|
|
case WRCHR:
|
|
printf("%c",pop(stack));
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
end:
|
|
return;
|
|
}
|
|
#ifdef DEBUG
|
|
|
|
void tests(void){
|
|
printf("Runnig debug mode\n");
|
|
copyToProgram(code1,sizeof(code1)/sizeof(code1[0]),program);
|
|
printProgram(program);
|
|
}
|
|
|
|
#endif /* ifdef DEBUG */
|
|
|
|
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 = 1;
|
|
unsigned int p[1];
|
|
program.size = &psize;
|
|
program.program = p;
|
|
|
|
|
|
#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]);
|
|
}
|
|
} else {
|
|
run:
|
|
// Started
|
|
printf("Ninja Virtual Machine started\n");
|
|
printProgram(program);
|
|
execute();
|
|
// Stopped
|
|
printf("Ninja Virtual Machine stopped\n");
|
|
return 0;
|
|
}
|
|
}
|