diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d8fa11..d648b47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,4 +5,5 @@ set(CMAKE_C_STANDARD 99) add_compile_options(-g -Wall -pedantic) -add_executable(ninja njvm.c) +add_executable(ninja njvm.c + SDA.c) diff --git a/SDA.c b/SDA.c new file mode 100644 index 0000000..7be99b9 --- /dev/null +++ b/SDA.c @@ -0,0 +1,28 @@ +// +// Created by Nils on 03.12.2023 +// +#ifndef SDA +#define SDA + +#include + +struct sda { + int *size; + unsigned int *sda; +}; + +unsigned int getSDA(int i, struct sda s) { + return s.sda[i]; +} + +void setSDA(int point, int val, struct sda s) { + s.sda[point] = val; +} + +void printSDA(struct sda s) { + for (int i; i < *s.size; i++) { + printf("%i", get(i, s)); + } +} + +#endif \ No newline at end of file diff --git a/codeReader.c b/codeReader.c index ca66ddb..a698b10 100644 --- a/codeReader.c +++ b/codeReader.c @@ -1,32 +1,37 @@ #ifndef CODEREADER #define CODEREADER -#include "consts.c" + +#include "consts.c" #include #include #include "program.c" -void fromFile(char *path, struct program program){ - unsigned int countInstructions; - unsigned int staticVars; - FILE *fptr; - fptr = fopen(path, "r+b"); - if(fptr == NULL) { - printf("Error: cannot open code file %s", path); - exit(EXIT_FAILURE); - } - unsigned int buffer[4]; - fread(buffer, 4, 4, fptr); - // Check file type - if(buffer[0] != 0x46424A4E){ - printf("Error: wrong file type"); - exit(EXIT_FAILURE); - } - if(buffer[1] != VERSION){ - printf("Error: wrong version"); - } - countInstructions = buffer[2]; - staticVars = buffer[3]; - unsigned int instBuffer[countInstructions]; - fread(instBuffer, 4, countInstructions, fptr); - copyToProgram(instBuffer,countInstructions,program); + +unsigned int fromFile(char *path, struct program program) { + unsigned int countInstructions; + unsigned int staticVars; + FILE *fptr; + fptr = fopen(path, "r+b"); + if (fptr == NULL) { + printf("Error: cannot open code file %s", path); + exit(EXIT_FAILURE); + } + unsigned int buffer[4]; + fread(buffer, 4, 4, fptr); + // Check file type + if (buffer[0] != 0x46424A4E) { + printf("Error: wrong file type"); + exit(EXIT_FAILURE); + } + if (buffer[1] != VERSION) { + printf("Error: wrong version"); + exit(EXIT_FAILURE); + } + countInstructions = buffer[2]; + staticVars = buffer[3]; + unsigned int instBuffer[countInstructions]; + fread(instBuffer, 4, countInstructions, fptr); + copyToProgram(instBuffer, countInstructions, program); + return staticVars; } + #endif /* ifdef CODEREADER */ diff --git a/instruktion.c b/instruktion.c index 4f8459c..7853372 100644 --- a/instruktion.c +++ b/instruktion.c @@ -16,5 +16,11 @@ #define WRINT 8 #define RDCHR 9 #define WRCHR 10 +#define PUSHG 11 +#define POPG 12 +#define ASF 13 +#define RSF 14 +#define PUSHL 15 +#define POPL 16 #endif /* ifndef INSREUKTION */ diff --git a/njvm.c b/njvm.c index ddf73ad..6bfb565 100644 --- a/njvm.c +++ b/njvm.c @@ -4,10 +4,12 @@ #include "code.c" #include "stack.c" #include "program.c" +#include "codeReader.c" +#include "SDA.c" //Comment to disable debug -//#define DEBUG +#define DEBUG // Stack struct stack stack; @@ -16,6 +18,10 @@ struct stack stack; // 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__); } @@ -33,7 +39,6 @@ void execute(struct program program) { switch (program.program[i] >> 24) { case HALT: goto end; - break; case PUSHC: push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i]))); break; @@ -69,6 +74,26 @@ void execute(struct program program) { case WRCHR: printf("%c", pop(stack)); break; + case PUSHG: + push(stack, getSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), sda)); + break; + case POPG: + setSDA(SIGN_EXTEND(IMMEDIATE(program.program[i])), pop(stack), sda); + break; + case ASF: + push(stack, *stack.current); + fp = *stack.current; + *stack.current = *stack.current + SIGN_EXTEND(IMMEDIATE(program.program[i])); + break; + case RSF: + *stack.current = fp; + fp = pop(stack); + case PUSHL: + stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))] = pop(stack); + break; + case POPL: + push(stack, stack.stack[fp + SIGN_EXTEND(IMMEDIATE(program.program[i]))]); + break; } } end: @@ -77,10 +102,13 @@ void execute(struct program program) { #ifdef DEBUG -void tests(void){ - printf("Runnig debug mode\n"); - copyToProgram(code1,sizeof(code1)/sizeof(code1[0]),program); - printProgram(program); +void tests(void) { + printf("Runnig debug mode\n"); + int sizeSDA = fromFile("C:\\Users\\Nilss\\CLionProjects\\njvm\\prog1.bin", program); + unsigned int s[sizeSDA]; + sda.size = &sizeSDA; + sda.sda = s; + printProgram(program); } #endif /* ifdef DEBUG */ diff --git a/prog1.bin b/prog1.bin new file mode 100644 index 0000000..9aa0fba Binary files /dev/null and b/prog1.bin differ diff --git a/program.c b/program.c index 7da099f..02bed77 100644 --- a/program.c +++ b/program.c @@ -59,6 +59,24 @@ void printProgram(struct program program) { case HALT: strcpy(c, "halt"); break; + case PUSHG : + strcpy(c, "pushg"); + break; + case POPG: + strcpy(c, "popg"); + break; + case ASF: + strcpy(c, "asf"); + break; + case RSF: + strcpy(c, "rsf"); + break; + case PUSHL: + strcpy(c, "pushl"); + break; + case POPL: + strcpy(c, "popl"); + break; default: strcpy(c, "ERROR"); break;