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

3
.gitignore vendored
View File

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

2
code.c
View File

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

View File

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

39
njvm.c
View File

@ -11,7 +11,7 @@
//Comment to disable debug //Comment to disable debug
#define DEBUG // #define DEBUG
unsigned int* programmSpeicher; unsigned int* programmSpeicher;
@ -49,18 +49,6 @@ 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 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(){ void printProgramm(){
int i = 0; int i = 0;
char c[10]; char c[10];
@ -182,12 +170,29 @@ int main(int argc, char *argv[]) {
#ifdef DEBUG #ifdef DEBUG
tests(); tests();
#endif /* ifdef DEBUG */ #endif /* ifdef DEBUG */
if (argc > 1) useption(argc, argv); if (argc > 1) {
else { 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 // Started
printf("Ninja Virtual Machine started\n"); printf("Ninja Virtual Machine started\n");
printProgramm();
execute();
// Stopped // Stopped
printf("Ninja Virtual Machine stopped\n"); printf("Ninja Virtual Machine stopped\n");
return 0; return 0;