njvm/njvm.c
2023-10-30 00:17:17 +01:00

177 lines
4.2 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "instruktion.c"
#include "code.c"
#include "stack.c"
//Comment to disable debug
// #define DEBUG
// Stack
struct stack stack;
#define SIZE 1000
unsigned int* programmSpeicher;
void copyToProgramm(unsigned int codeToCopy[]){
programmSpeicher = codeToCopy;
}
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 printProgramm(){
int i = 0;
char c[10];
// while (programmSpeicher[i] != 0)
// {
// switch (programmSpeicher[i] >> 24) {
// case PUSHC:
// strcpy(c,"pushc");
// break;
// case ADD:
// strcpy(c,"add");
// break;
// case SUB:
// strcpy(c,"sub");
// break;
// case MUL:
// strcpy(c,"mul");
// break;
// case DIV:
// strcpy(c,"div");
// break;
// case MOD:
// strcpy(c,"mod");
// break;
// case RDINT:
// strcpy(c,"rdint");
// break;
// case WRINT:
// strcpy(c,"wrint");
// break;
// case RDCHR:
// strcpy(c,"rdchr");
// break;
// case WRCHR:
// strcpy(c,"wrchr");
// break;
// default:
// strcpy(c,"halt");
// break;
// }
// IMMEDIATE(programmSpeicher[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(programmSpeicher[i])) : printf("%03i:\t%s\n",i,c);
// i++;
// }
printf("%03i:\thalt\n",i);
}
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");
copyToProgramm(code1);
printProgramm();
execute();
}
#endif /* ifdef DEBUG */
int main(int argc, char *argv[]) {
int size = SIZE;
int current = 0;
unsigned int s[SIZE];
stack.size = &size;
stack.current = &current;
stack.stack = s;
#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) {
copyToProgramm(code1);
goto run;
} else if (strcmp(argv[1],"--prog2") == 0) {
copyToProgramm(code2);
goto run;
}else if (strcmp(argv[1],"--prog3") == 0) {
copyToProgramm(code3);
goto run;
}else {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
}
} else {
run:
// Started
printf("Ninja Virtual Machine started\n");
printProgramm();
// execute();
// Stopped
printf("Ninja Virtual Machine stopped\n");
return 0;
}
}