update debug mode

This commit is contained in:
Elias Bennour 2023-12-09 21:55:12 +01:00
parent 33a2994c29
commit 10a6883750
3 changed files with 87 additions and 66 deletions

141
njvm.c
View File

@ -7,8 +7,8 @@
#include "codeReader.c"
#include "SDA.c"
// Debug
int debug = 1;
// Debug
int debug = 0;
// Stack
struct stack stack;
@ -37,124 +37,128 @@ void execute(struct program program) {
for (i = 0; i < *program.size; ++i) {
switch (program.program[i] >> 24) {
case HALT:
if(debug == 1) printf("halt\n");
if (debug == 1) printf("halt\n");
goto end;
case PUSHC:
if(debug == 1) printf("pushc\n");
if (debug == 1) printf("pushc: %i\n", IMMEDIATE(program.program[i]));
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
break;
case ADD:
if(debug == 1) printf("add\n");
if (debug == 1) printf("add: %i + %i\n", peek(stack, 2), peek(stack, 1));
push(stack, pop(stack) + pop(stack));
break;
case SUB:
if(debug == 1) printf("sub\n");
if (debug == 1) printf("sub: %i - %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
push(stack, pop(stack) - temp);
break;
case MUL:
if(debug == 1) printf("mul\n");
if (debug == 1) printf("mul: %i * %i\n", peek(stack, 2), peek(stack, 1));
push(stack, pop(stack) * pop(stack));
break;
case DIV:
if(debug == 1) printf("div\n");
if (debug == 1) printf("div: %i / %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
push(stack, pop(stack) / temp);
break;
case MOD:
if(debug == 1) printf("mod\n");
if (debug == 1) printf("mod: %i %% %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
push(stack, pop(stack) % temp);
break;
case RDINT:
if(debug == 1) printf("rdint\n");
if (debug == 1) printf("rdint\n");
scanf("%i", &intInput);
push(stack, intInput);
if (debug == 1) printf("pushed %i\n", intInput);
break;
case WRINT:
if(debug == 1) printf("wrint\n");
if (debug == 1) printf("wrint: %i\n", peek(stack, 1));
printf("%i", pop(stack));
break;
case RDCHR:
if(debug == 1) printf("rdchr\n");
if (debug == 1) printf("rdchr\n");
scanf("%c", &charInput);
push(stack, charInput);
if (debug == 1) printf("pushed %c\n", charInput);
break;
case WRCHR:
if(debug == 1) printf("wrchr\n");
if (debug == 1) printf("wrchr: %c\n", peek(stack, 1));
printf("%c", pop(stack));
break;
case PUSHG:
if(debug == 1) printf("pushg\n");
if (debug == 1) printf("pushg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda));
break;
case POPG:
if(debug == 1) printf("popg\n");
if (debug == 1) printf("popg: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda);
break;
case ASF:
if(debug == 1) printf("asf\n");
if (debug == 1) printf("asf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, *stack.current);
fp = *stack.current - 1;
fp = *stack.current;
*stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i]));
break;
case RSF:
if(debug == 1) printf("rsf\n");
if (debug == 1) printf("rsf\n");
*stack.current = fp + 2;
fp = pop(stack);
case POPL:
if(debug == 1) printf("popl\n");
if (debug == 1) printf("popl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
break;
case PUSHL:
if(debug == 1) printf("pushl\n");
if (debug == 1) printf("pushl: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]);
break;
case NE:
if(debug == 1) printf("ne\n");
if (debug == 1) printf("ne: %i != %i\n", peek(stack, 2), peek(stack, 1));
if (pop(stack) != pop(stack)) push(stack, 1);
else push(stack, 0);
else push(stack, 0);
break;
case EQ:
if(debug == 1) printf("eq\n");
case EQ:
if (debug == 1) printf("eq: %i == %i\n", peek(stack, 2), peek(stack, 1));
if (pop(stack) == pop(stack)) push(stack, 1);
else push(stack, 0);
else push(stack, 0);
break;
case LT:
if(debug == 1) printf("lt\n");
case LT:
if (debug == 1) printf("lt: %i < %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
if (pop(stack) < temp) push(stack, 1);
else push(stack, 0);
else push(stack, 0);
break;
case LE:
if(debug == 1) printf("le\n");
case LE:
if (debug == 1) printf("le: %i <= %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
if (pop(stack) <= temp) push(stack, 1);
else push(stack, 0);
else push(stack, 0);
break;
case GT:
if(debug == 1) printf("gt\n");
case GT:
if (debug == 1) printf("gt: %i > %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
if (pop(stack) > temp) push(stack, 1);
else push(stack, 0);
else push(stack, 0);
break;
case GE:
if(debug == 1) printf("ge\n");
case GE:
if (debug == 1) printf("ge: %i >= %i\n", peek(stack, 2), peek(stack, 1));
temp = pop(stack);
if (pop(stack) >= temp) push(stack, 1);
else push(stack, 0);
else push(stack, 0);
break;
case BRF:
if(debug == 1) printf("brf\n");
if(pop(stack)==0)
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
if (debug == 1) printf("brf: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
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]));
if (debug == 1) printf("brt: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
if (debug == 1) printf("pop: %i\n", peek(stack, 1));
if (pop(stack) == 1)
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
case JMP:
if(debug == 1) printf("jmp\n");
if (debug == 1) printf("jmp: %i\n", SIGN_EXTEND(IMMEDIATE(program.program[i])));
i = SIGN_EXTEND(IMMEDIATE(program.program[i]));
break;
}
@ -184,29 +188,38 @@ int main(int argc, char *argv[]) {
program.program = p;
program.saveProgram = &saveProgram;
if (argc > 2) {
if (strcmp(argv[2], "--debug")) {
debug = 1;
}
}
if (debug){
tests();
}
int run = 0;
int sizeSDA;
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);
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;
}
}
}
if (debug) {
tests();
}
if (run) {
unsigned int s[sizeSDA];
sda.size = &sizeSDA;
sda.sda = s;
if (debug == 1) printProgram(program);
execute(program);
} else {
return 0;
printf("Error: no code file specified\n");
return 1;
}
}

View File

@ -38,4 +38,12 @@ unsigned int pop(struct stack s) {
*s.current = *s.current -1;
return s.stack[*s.current];
}
unsigned int peek(struct stack s, int steps) {
if (*s.current - steps < 0) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
return s.stack[*s.current - steps];
}
#endif