Structurized the code

This commit is contained in:
nilsplk 2023-10-26 22:36:54 +02:00
parent efe7663637
commit 9890324ea5
4 changed files with 27 additions and 19 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ cmake-build-debug
njvm
njvm.dSYM
njvm.exe
njvm2

2
code.c
View File

@ -18,7 +18,7 @@ unsigned int code1[] = {
(HALT)
};
unsigned int code2[] = {
(PUSHC << 24) | IMMEDIATE(-2),
(PUSHC << 24) | IMMEDIATE(SIGN_EXTEND(-2)),
(RDINT << 24),
(MUL << 24),
(PUSHC << 24) | IMMEDIATE(3),

View File

@ -2,6 +2,8 @@
#define IMMEDIATE(x) ((x) & 0x00FFFFFF)
#define SIGN_EXTEND(i) ((i) & 0x00800000 ? (i) | 0xFF000000 : (i))
#define HALT 0
#define PUSHC 1
#define ADD 2

39
njvm.c
View File

@ -11,7 +11,7 @@
//Comment to disable debug
#define DEBUG
// #define DEBUG
unsigned int* programmSpeicher;
@ -49,18 +49,6 @@ void version(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");
}
void useption(int argc, char *argv[]) {
if (argc > 1) {
if (strcmp(argv[1], "--version") == 0) {
version();
} else if (strcmp(argv[1], "--help") == 0) {
help();
} else {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
}
}
}
void printProgramm(){
int i = 0;
char c[10];
@ -182,12 +170,29 @@ int main(int argc, char *argv[]) {
#ifdef DEBUG
tests();
#endif /* ifdef DEBUG */
if (argc > 1) useption(argc, argv);
else {
if (argc > 1) {
if (strcmp(argv[1], "--version") == 0) {
version();
} else if (strcmp(argv[1], "--help") == 0) {
help();
} else if (strcmp(argv[1], "--prog1") == 0) {
copyToProgramm(code1);
goto run;
} else if (strcmp(argv[1],"--prog2") == 0) {
copyToProgramm(code2);
goto run;
}else if (strcmp(argv[1],"--prog3") == 0) {
copyToProgramm(code3);
goto run;
}else {
printf("unknown command line argument '%s', try './njvm --help'", argv[1]);
}
} else {
run:
// Started
printf("Ninja Virtual Machine started\n");
printProgramm();
execute();
// Stopped
printf("Ninja Virtual Machine stopped\n");
return 0;