Aufgabe 2 fertig
This commit is contained in:
parent
8b3770df80
commit
0e52973475
@ -5,4 +5,5 @@ set(CMAKE_C_STANDARD 99)
|
|||||||
|
|
||||||
add_compile_options(-g -Wall -pedantic)
|
add_compile_options(-g -Wall -pedantic)
|
||||||
|
|
||||||
add_executable(ninja njvm.c)
|
add_executable(ninja njvm.c
|
||||||
|
SDA.c)
|
||||||
|
|||||||
28
SDA.c
Normal file
28
SDA.c
Normal 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
|
||||||
53
codeReader.c
53
codeReader.c
@ -1,32 +1,37 @@
|
|||||||
#ifndef CODEREADER
|
#ifndef CODEREADER
|
||||||
#define CODEREADER
|
#define CODEREADER
|
||||||
|
|
||||||
#include "consts.c"
|
#include "consts.c"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "program.c"
|
#include "program.c"
|
||||||
void fromFile(char *path, struct program program){
|
|
||||||
unsigned int countInstructions;
|
unsigned int fromFile(char *path, struct program program) {
|
||||||
unsigned int staticVars;
|
unsigned int countInstructions;
|
||||||
FILE *fptr;
|
unsigned int staticVars;
|
||||||
fptr = fopen(path, "r+b");
|
FILE *fptr;
|
||||||
if(fptr == NULL) {
|
fptr = fopen(path, "r+b");
|
||||||
printf("Error: cannot open code file %s", path);
|
if (fptr == NULL) {
|
||||||
exit(EXIT_FAILURE);
|
printf("Error: cannot open code file %s", path);
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
unsigned int buffer[4];
|
}
|
||||||
fread(buffer, 4, 4, fptr);
|
unsigned int buffer[4];
|
||||||
// Check file type
|
fread(buffer, 4, 4, fptr);
|
||||||
if(buffer[0] != 0x46424A4E){
|
// Check file type
|
||||||
printf("Error: wrong file type");
|
if (buffer[0] != 0x46424A4E) {
|
||||||
exit(EXIT_FAILURE);
|
printf("Error: wrong file type");
|
||||||
}
|
exit(EXIT_FAILURE);
|
||||||
if(buffer[1] != VERSION){
|
}
|
||||||
printf("Error: wrong version");
|
if (buffer[1] != VERSION) {
|
||||||
}
|
printf("Error: wrong version");
|
||||||
countInstructions = buffer[2];
|
exit(EXIT_FAILURE);
|
||||||
staticVars = buffer[3];
|
}
|
||||||
unsigned int instBuffer[countInstructions];
|
countInstructions = buffer[2];
|
||||||
fread(instBuffer, 4, countInstructions, fptr);
|
staticVars = buffer[3];
|
||||||
copyToProgram(instBuffer,countInstructions,program);
|
unsigned int instBuffer[countInstructions];
|
||||||
|
fread(instBuffer, 4, countInstructions, fptr);
|
||||||
|
copyToProgram(instBuffer, countInstructions, program);
|
||||||
|
return staticVars;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ifdef CODEREADER */
|
#endif /* ifdef CODEREADER */
|
||||||
|
|||||||
@ -16,5 +16,11 @@
|
|||||||
#define WRINT 8
|
#define WRINT 8
|
||||||
#define RDCHR 9
|
#define RDCHR 9
|
||||||
#define WRCHR 10
|
#define WRCHR 10
|
||||||
|
#define PUSHG 11
|
||||||
|
#define POPG 12
|
||||||
|
#define ASF 13
|
||||||
|
#define RSF 14
|
||||||
|
#define PUSHL 15
|
||||||
|
#define POPL 16
|
||||||
|
|
||||||
#endif /* ifndef INSREUKTION */
|
#endif /* ifndef INSREUKTION */
|
||||||
|
|||||||
40
njvm.c
40
njvm.c
@ -4,10 +4,12 @@
|
|||||||
#include "code.c"
|
#include "code.c"
|
||||||
#include "stack.c"
|
#include "stack.c"
|
||||||
#include "program.c"
|
#include "program.c"
|
||||||
|
#include "codeReader.c"
|
||||||
|
#include "SDA.c"
|
||||||
|
|
||||||
//Comment to disable debug
|
//Comment to disable debug
|
||||||
|
|
||||||
//#define DEBUG
|
#define DEBUG
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
@ -16,6 +18,10 @@ struct stack stack;
|
|||||||
// Program
|
// Program
|
||||||
struct program program;
|
struct program program;
|
||||||
|
|
||||||
|
// SDA
|
||||||
|
struct sda sda;
|
||||||
|
unsigned fp;
|
||||||
|
|
||||||
void version(void) {
|
void version(void) {
|
||||||
printf("Ninja Virtual Machine version %i (compiled %s, %s)\n", 0, __DATE__, __TIME__);
|
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) {
|
switch (program.program[i] >> 24) {
|
||||||
case HALT:
|
case HALT:
|
||||||
goto end;
|
goto end;
|
||||||
break;
|
|
||||||
case PUSHC:
|
case PUSHC:
|
||||||
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
push(stack, SIGN_EXTEND(IMMEDIATE(program.program[i])));
|
||||||
break;
|
break;
|
||||||
@ -69,6 +74,26 @@ void execute(struct program program) {
|
|||||||
case WRCHR:
|
case WRCHR:
|
||||||
printf("%c", pop(stack));
|
printf("%c", pop(stack));
|
||||||
break;
|
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:
|
end:
|
||||||
@ -77,10 +102,13 @@ void execute(struct program program) {
|
|||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
|
||||||
void tests(void){
|
void tests(void) {
|
||||||
printf("Runnig debug mode\n");
|
printf("Runnig debug mode\n");
|
||||||
copyToProgram(code1,sizeof(code1)/sizeof(code1[0]),program);
|
int sizeSDA = fromFile("C:\\Users\\Nilss\\CLionProjects\\njvm\\prog1.bin", program);
|
||||||
printProgram(program);
|
unsigned int s[sizeSDA];
|
||||||
|
sda.size = &sizeSDA;
|
||||||
|
sda.sda = s;
|
||||||
|
printProgram(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ifdef DEBUG */
|
#endif /* ifdef DEBUG */
|
||||||
|
|||||||
18
program.c
18
program.c
@ -59,6 +59,24 @@ void printProgram(struct program program) {
|
|||||||
case HALT:
|
case HALT:
|
||||||
strcpy(c, "halt");
|
strcpy(c, "halt");
|
||||||
break;
|
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:
|
default:
|
||||||
strcpy(c, "ERROR");
|
strcpy(c, "ERROR");
|
||||||
break;
|
break;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user