Aufgabe 2 fertig

This commit is contained in:
Nils 2023-12-03 18:14:27 +01:00
parent 8b3770df80
commit 0e52973475
7 changed files with 118 additions and 32 deletions

View File

@ -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)

28
SDA.c Normal file
View File

@ -0,0 +1,28 @@
//
// Created by Nils on 03.12.2023
//
#ifndef SDA
#define SDA
#include <stdio.h>
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

View File

@ -1,32 +1,37 @@
#ifndef CODEREADER
#define CODEREADER
#include "consts.c"
#include "consts.c"
#include <stdio.h>
#include <stdlib.h>
#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 */

View File

@ -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 */

40
njvm.c
View File

@ -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 */

BIN
prog1.bin Normal file

Binary file not shown.

View File

@ -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;