This commit is contained in:
nilsplk 2023-10-30 11:59:56 +01:00
parent 4d547cca6a
commit dfedf2e717
3 changed files with 95 additions and 71 deletions

90
njvm.c
View File

@ -4,22 +4,22 @@
#include "instruktion.c" #include "instruktion.c"
#include "code.c" #include "code.c"
#include "stack.c" #include "stack.c"
#include "program.c"
//Comment to disable debug //Comment to disable debug
// #define DEBUG #define DEBUG
// Stack // Stack
struct stack stack; struct stack stack;
#define SIZE 1000 #define SIZE 1000
unsigned int* programmSpeicher; // ProgrammSpeicher
struct program program;
void copyToProgramm(unsigned int codeToCopy[]){ unsigned int* programmSpeicher;
programmSpeicher = codeToCopy;
}
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__);
@ -28,51 +28,7 @@ void version(void) {
void help(void) { void help(void) {
printf("usage: ./njvm [option] [option] ...\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n"); printf("usage: ./njvm [option] [option] ...\n\t--version\tshow version and exit\n\t--help\t\tshow this help and exit\n");
} }
void printProgramm(){
int i = 0;
char c[10];
while (programmSpeicher[i] != 0)
{
switch (programmSpeicher[i] >> 24) {
case PUSHC:
strcpy(c,"pushc");
break;
case ADD:
strcpy(c,"add");
break;
case SUB:
strcpy(c,"sub");
break;
case MUL:
strcpy(c,"mul");
break;
case DIV:
strcpy(c,"div");
break;
case MOD:
strcpy(c,"mod");
break;
case RDINT:
strcpy(c,"rdint");
break;
case WRINT:
strcpy(c,"wrint");
break;
case RDCHR:
strcpy(c,"rdchr");
break;
case WRCHR:
strcpy(c,"wrchr");
break;
default:
strcpy(c,"halt");
break;
}
IMMEDIATE(programmSpeicher[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(programmSpeicher[i])) : printf("%03i:\t%s\n",i,c);
i++;
}
printf("%03i:\thalt\n",i);
}
void execute(void) { void execute(void) {
int i = 0; int i = 0;
int intInput; int intInput;
@ -129,20 +85,28 @@ end:
void tests(void){ void tests(void){
printf("Runnig debug mode\n"); printf("Runnig debug mode\n");
copyToProgramm(code1); copyToProgram(code1,sizeof(code1)/sizeof(code1[0]),program);
printProgramm(); printProgram(program);
execute();
} }
#endif /* ifdef DEBUG */ #endif /* ifdef DEBUG */
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int size = SIZE; // Initialize the Stack
int current = 0; int size = SIZE;
unsigned int s[SIZE]; int current = 0;
stack.size = &size; unsigned int s[SIZE];
stack.current = &current; stack.size = &size;
stack.stack = s; stack.current = &current;
stack.stack = s;
// Initialize ProgrammSpeicher
int psize = 1;
unsigned int p[1];
program.size = &psize;
program.program = p;
#ifdef DEBUG #ifdef DEBUG
tests(); tests();
#endif /* ifdef DEBUG */ #endif /* ifdef DEBUG */
@ -152,13 +116,13 @@ int main(int argc, char *argv[]) {
} else if (strcmp(argv[1], "--help") == 0) { } else if (strcmp(argv[1], "--help") == 0) {
help(); help();
} else if (strcmp(argv[1], "--prog1") == 0) { } else if (strcmp(argv[1], "--prog1") == 0) {
copyToProgramm(code1); copyToProgram(code1, sizeof(code1)/sizeof(code1[0]),program);
goto run; goto run;
} else if (strcmp(argv[1],"--prog2") == 0) { } else if (strcmp(argv[1],"--prog2") == 0) {
copyToProgramm(code2); copyToProgram(code2, sizeof(code2)/sizeof(code2[0]),program);
goto run; goto run;
}else if (strcmp(argv[1],"--prog3") == 0) { }else if (strcmp(argv[1],"--prog3") == 0) {
copyToProgramm(code3); copyToProgram(code3, sizeof(code3)/sizeof(code3[0]),program);
goto run; goto run;
}else { }else {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]); printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
@ -167,7 +131,7 @@ int main(int argc, char *argv[]) {
run: run:
// Started // Started
printf("Ninja Virtual Machine started\n"); printf("Ninja Virtual Machine started\n");
printProgramm(); printProgram(program);
execute(); execute();
// Stopped // Stopped
printf("Ninja Virtual Machine stopped\n"); printf("Ninja Virtual Machine stopped\n");

67
program.c Normal file
View File

@ -0,0 +1,67 @@
/*
* Created by Nils on 30.10.2023.
*/
#ifndef PROGRAM
#define PROGRAM
#include <string.h>
#include "instruktion.c"
#include <stdio.h>
struct program {
int* size;
unsigned int* program;
};
void copyToProgram(unsigned int codeToCopy[], int size, struct program program){
program.program = codeToCopy;
*program.size = size;
}
void printProgram(struct program program){
printf("%i\n",*program.size);
char c[10];
for (int i = 0; i > *program.size; i++) {
switch (program.program[i] >> 24) {
case PUSHC:
strcpy(c,"pushc");
break;
case ADD:
strcpy(c,"add");
break;
case SUB:
strcpy(c,"sub");
break;
case MUL:
strcpy(c,"mul");
break;
case DIV:
strcpy(c,"div");
break;
case MOD:
strcpy(c,"mod");
break;
case RDINT:
strcpy(c,"rdint");
break;
case WRINT:
strcpy(c,"wrint");
break;
case RDCHR:
strcpy(c,"rdchr");
break;
case WRCHR:
strcpy(c,"wrchr");
break;
case HALT:
strcpy(c,"halt");
break;
default:
strcpy(c,"ERROR");
break;
}
IMMEDIATE(program.program[i])? printf("%03i:\t%s\t%i\n",i,c,IMMEDIATE(program.program[i])) : printf("%03i:\t%s\n",i,c);
printf("afslaf");
}
}
#endif /* ifndef PROGRAMM */

View File

@ -1,5 +1,5 @@
// //
// Created by Nilss on 29.10.2023. // Created by Nils on 29.10.2023.
// //
#ifndef STACK #ifndef STACK
#define STACK #define STACK
@ -20,13 +20,6 @@ void printStack(struct stack stack) {
} }
} }
struct stack initStack(int size) {
unsigned int a[size];
int current = 0;
struct stack s1 = {&size, &current, a};
return s1;
}
void push(struct stack s, unsigned int value) { void push(struct stack s, unsigned int value) {
if (*s.current >= *s.size) { if (*s.current >= *s.size) {
printf("Stack Overflow\n"); printf("Stack Overflow\n");