Finished cleaning up the code
This commit is contained in:
parent
dfedf2e717
commit
1363db7c11
28
njvm.c
28
njvm.c
@ -1,6 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include "instruktion.c"
|
#include "instruktion.c"
|
||||||
#include "code.c"
|
#include "code.c"
|
||||||
#include "stack.c"
|
#include "stack.c"
|
||||||
@ -11,16 +10,13 @@
|
|||||||
|
|
||||||
#define DEBUG
|
#define DEBUG
|
||||||
|
|
||||||
|
|
||||||
// Stack
|
// Stack
|
||||||
struct stack stack;
|
struct stack stack;
|
||||||
#define SIZE 1000
|
#define SIZE 1000
|
||||||
|
|
||||||
// ProgrammSpeicher
|
// Program
|
||||||
struct program program;
|
struct program program;
|
||||||
|
|
||||||
unsigned int* programmSpeicher;
|
|
||||||
|
|
||||||
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__);
|
||||||
}
|
}
|
||||||
@ -29,19 +25,18 @@ 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 execute(void) {
|
void execute(struct program program) {
|
||||||
int i = 0;
|
int i;
|
||||||
int intInput;
|
int intInput;
|
||||||
int temp;
|
unsigned int temp;
|
||||||
char charInput;
|
char charInput;
|
||||||
while (1) {
|
for (i = 0; i < *program.size; ++i) {
|
||||||
|
switch (program.program[i] >> 24) {
|
||||||
switch (programmSpeicher[i] >> 24) {
|
|
||||||
case HALT:
|
case HALT:
|
||||||
goto end;
|
goto end;
|
||||||
break;
|
break;
|
||||||
case PUSHC:
|
case PUSHC:
|
||||||
push(stack,IMMEDIATE(programmSpeicher[i]));
|
push(stack,IMMEDIATE(program.program[i]));
|
||||||
break;
|
break;
|
||||||
case ADD:
|
case ADD:
|
||||||
push(stack,pop(stack)+pop(stack));
|
push(stack,pop(stack)+pop(stack));
|
||||||
@ -76,9 +71,8 @@ void execute(void) {
|
|||||||
printf("%c",pop(stack));
|
printf("%c",pop(stack));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
@ -101,8 +95,8 @@ int main(int argc, char *argv[]) {
|
|||||||
stack.stack = s;
|
stack.stack = s;
|
||||||
|
|
||||||
// Initialize ProgrammSpeicher
|
// Initialize ProgrammSpeicher
|
||||||
int psize = 1;
|
int psize = 1000;
|
||||||
unsigned int p[1];
|
unsigned int p[1000];
|
||||||
program.size = &psize;
|
program.size = &psize;
|
||||||
program.program = p;
|
program.program = p;
|
||||||
|
|
||||||
@ -132,7 +126,7 @@ int main(int argc, char *argv[]) {
|
|||||||
// Started
|
// Started
|
||||||
printf("Ninja Virtual Machine started\n");
|
printf("Ninja Virtual Machine started\n");
|
||||||
printProgram(program);
|
printProgram(program);
|
||||||
execute();
|
execute(program);
|
||||||
// Stopped
|
// Stopped
|
||||||
printf("Ninja Virtual Machine stopped\n");
|
printf("Ninja Virtual Machine stopped\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
46
program.c
46
program.c
@ -3,65 +3,67 @@
|
|||||||
*/
|
*/
|
||||||
#ifndef PROGRAM
|
#ifndef PROGRAM
|
||||||
#define PROGRAM
|
#define PROGRAM
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "instruktion.c"
|
#include "instruktion.c"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
struct program {
|
struct program {
|
||||||
int* size;
|
int *size;
|
||||||
unsigned int* program;
|
unsigned int *program;
|
||||||
};
|
};
|
||||||
|
|
||||||
void copyToProgram(unsigned int codeToCopy[], int size, struct program program){
|
void copyToProgram(const unsigned int codeToCopy[], int size, struct program program) {
|
||||||
program.program = codeToCopy;
|
for (int i = 0; i < size; ++i) {
|
||||||
|
program.program[i] = codeToCopy[i];
|
||||||
|
}
|
||||||
*program.size = size;
|
*program.size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void printProgram(struct program program){
|
void printProgram(struct program program) {
|
||||||
printf("%i\n",*program.size);
|
|
||||||
char c[10];
|
char c[10];
|
||||||
for (int i = 0; i > *program.size; i++) {
|
for (int i = 0; i < *program.size; i++) {
|
||||||
switch (program.program[i] >> 24) {
|
switch (program.program[i] >> 24) {
|
||||||
case PUSHC:
|
case PUSHC:
|
||||||
strcpy(c,"pushc");
|
strcpy(c, "pushc");
|
||||||
break;
|
break;
|
||||||
case ADD:
|
case ADD:
|
||||||
strcpy(c,"add");
|
strcpy(c, "add");
|
||||||
break;
|
break;
|
||||||
case SUB:
|
case SUB:
|
||||||
strcpy(c,"sub");
|
strcpy(c, "sub");
|
||||||
break;
|
break;
|
||||||
case MUL:
|
case MUL:
|
||||||
strcpy(c,"mul");
|
strcpy(c, "mul");
|
||||||
break;
|
break;
|
||||||
case DIV:
|
case DIV:
|
||||||
strcpy(c,"div");
|
strcpy(c, "div");
|
||||||
break;
|
break;
|
||||||
case MOD:
|
case MOD:
|
||||||
strcpy(c,"mod");
|
strcpy(c, "mod");
|
||||||
break;
|
break;
|
||||||
case RDINT:
|
case RDINT:
|
||||||
strcpy(c,"rdint");
|
strcpy(c, "rdint");
|
||||||
break;
|
break;
|
||||||
case WRINT:
|
case WRINT:
|
||||||
strcpy(c,"wrint");
|
strcpy(c, "wrint");
|
||||||
break;
|
break;
|
||||||
case RDCHR:
|
case RDCHR:
|
||||||
strcpy(c,"rdchr");
|
strcpy(c, "rdchr");
|
||||||
break;
|
break;
|
||||||
case WRCHR:
|
case WRCHR:
|
||||||
strcpy(c,"wrchr");
|
strcpy(c, "wrchr");
|
||||||
break;
|
break;
|
||||||
case HALT:
|
case HALT:
|
||||||
strcpy(c,"halt");
|
strcpy(c, "halt");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
strcpy(c,"ERROR");
|
strcpy(c, "ERROR");
|
||||||
break;
|
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);
|
IMMEDIATE(program.program[i]) ? printf("%03i:\t%s\t%i\n", i, c, IMMEDIATE(program.program[i])) : printf(
|
||||||
printf("afslaf");
|
"%03i:\t%s\n", i, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ifndef PROGRAMM */
|
#endif /* ifndef PROGRAMM */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user