fix prog1.bin not working

add debug parameter
add nja-mac
This commit is contained in:
Elias Bennour 2023-12-04 01:20:41 +01:00
parent fb8497fbee
commit 88271c91ca
6 changed files with 46 additions and 44 deletions

2
SDA.c
View File

@ -24,4 +24,4 @@ void printSDA(struct sda s) {
} }
} }
#endif #endif

View File

@ -12,7 +12,7 @@ unsigned int fromFile(char *path, struct program program) {
FILE *fptr; FILE *fptr;
fptr = fopen(path, "r+b"); fptr = fopen(path, "r+b");
if (fptr == NULL) { if (fptr == NULL) {
printf("Error: cannot open code file %s", path); printf("Error: cannot open code file %s\n", path);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
unsigned int buffer[4]; unsigned int buffer[4];

0
nja Normal file → Executable file
View File

BIN
nja-mac Executable file

Binary file not shown.

86
njvm.c
View File

@ -7,10 +7,6 @@
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c" #include "SDA.c"
//Comment to disable debug
#define DEBUG
// Stack // Stack
struct stack stack; struct stack stack;
#define SIZE 1000 #define SIZE 1000
@ -92,7 +88,7 @@ void execute(struct program program) {
stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack); stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack);
break; break;
case POPL: 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; break;
} }
} }
@ -100,20 +96,18 @@ void execute(struct program program) {
return; return;
} }
#ifdef DEBUG
void tests(void) { void tests(void) {
printf("Runnig debug mode\n"); printf("Runnig debug mode\n");
int temp = fromFile("./prog1.bin", program); int temp = fromFile("./prog2.bin", program);
int sizeSDA = temp; int sizeSDA = temp;
unsigned int s[sizeSDA]; unsigned int s[sizeSDA];
sda.size = &temp; sda.size = &temp;
sda.sda = s; sda.sda = s;
printProgram(program); printProgram(program);
execute(program);
printSDA(sda);
} }
#endif /* ifdef DEBUG */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Initialize the Stack // Initialize the Stack
int size = SIZE; int size = SIZE;
@ -131,41 +125,49 @@ int main(int argc, char *argv[]) {
program.program = p; program.program = p;
program.saveProgram = &saveProgram; program.saveProgram = &saveProgram;
// Initialize runtime variables
int debug = 0;
int run = 0;
int sizeSDA;
#ifdef DEBUG
tests();
#endif /* ifdef DEBUG */
if (argc > 1) { if (argc > 1) {
if (strcmp(argv[1], "--version") == 0) { for (int i = 1; i < argc; ++i) {
version(); if (strcmp(argv[i], "--debug") == 0) {
} else if (strcmp(argv[1], "--help") == 0) { debug = 1;
help(); } else if (strcmp(argv[i], "--version") == 0) {
} else if (strcmp(argv[1], "--prog1") == 0) { version();
copyToProgram(code1, sizeof(code1) / sizeof(code1[0]), program); return 0;
goto run; } else if (strcmp(argv[i], "--help") == 0) {
} else if (strcmp(argv[1], "--prog2") == 0) { help();
copyToProgram(code2, sizeof(code2) / sizeof(code2[0]), program); return 0;
goto run; } else {
} else if (strcmp(argv[1], "--prog3") == 0) { sizeSDA = fromFile(argv[i], program);
copyToProgram(code3, sizeof(code3) / sizeof(code3[0]), program); run = 1;
goto run; }
} else {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
} }
} else { }
run:
// Started /*
if (*program.saveProgram == 1) { * Debug mode
printf("Ninja Virtual Machine started\n"); */
printProgram(program); if (debug) {
execute(program); tests();
printSDA(sda); }
} else {
printf("Error: no code file specified\n"); /*
return 1; * Run program
} */
// Stopped 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"); printf("Ninja Virtual Machine stopped\n");
return 0; } else {
printf("Error: no code file specified\n");
return 1;
} }
} }

BIN
prog1.bin

Binary file not shown.