diff --git a/High_level_prototypes/execve_image.c b/High_level_prototypes/execve_image.c index 192f5bf..4381af1 100644 --- a/High_level_prototypes/execve_image.c +++ b/High_level_prototypes/execve_image.c @@ -29,6 +29,7 @@ struct arguments }; int IP; +char* image; int string_length(char* s) { @@ -64,6 +65,12 @@ void write_int(int value, FILE* output) void clone_file(FILE* in, FILE* out) { + if(NULL == in) + { + fprintf(stderr, "Was unable to open input binary: %s\naborting hard\n", image); + exit(EXIT_FAILURE); + } + int c; for(c = fgetc(in); EOF != c; c = fgetc(in)) { @@ -95,9 +102,10 @@ int main(int argc, char **argv) struct arguments* head = NULL; struct arguments* i; - clone_file(fopen(argv[1], "r"), stdout); + image = argv[1]; + clone_file(fopen(image, "r"), stdout); - int option_index = 2; + int option_index = 1; while(option_index < argc) { i = calloc(1, sizeof(struct arguments)); diff --git a/User_Interface.py b/User_Interface.py index be87be0..41314bf 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -21,7 +21,7 @@ import sys, getopt vm = ctypes.CDLL('./libvm.so') -vm.initialize_lilith.argtype = ctypes.c_uint +vm.initialize_lilith.argtype = (ctypes.c_uint, ctypes.c_uint) vm.get_memory.argtype = ctypes.c_uint vm.get_memory.restype = ctypes.c_char_p vm.step_lilith.restype = ctypes.c_uint @@ -48,7 +48,7 @@ def Reset_lilith(): chunks = Memory_Size / (1024 * 1024 * 1024) print("Current Memory Size is: " + str(chunks) + unit) - vm.initialize_lilith(Memory_Size) + vm.initialize_lilith(Memory_Size, POSIX_MODE) global Current_IP Current_IP = 0 global Watchpoints diff --git a/vm.c b/vm.c index 6d0344b..a491524 100644 --- a/vm.c +++ b/vm.c @@ -19,7 +19,7 @@ #include /* Load program tape into Memory */ -void load_program(struct lilith* vm, char* rom_name) +size_t load_program(struct lilith* vm, char* rom_name) { FILE* program; program = fopen(rom_name, "r"); @@ -40,6 +40,7 @@ void load_program(struct lilith* vm, char* rom_name) fread(vm->memory, 1, end, program); fclose(program); + return end; } void execute_vm(struct lilith* vm) @@ -60,6 +61,7 @@ void execute_vm(struct lilith* vm) /* Standard C main program */ int main(int argc, char **argv) { + POSIX_MODE = false; int c; int Memory_Size = (16 * 1024); @@ -73,11 +75,12 @@ int main(int argc, char **argv) {"tape_01", required_argument, 0, '1'}, {"tape_02", required_argument, 0, '2'}, {"memory", required_argument, 0, 'm'}, + {"POSIX-MODE", no_argument, 0, 'P'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; int option_index = 0; - while ((c = getopt_long(argc, argv, "r:h:1:2:m", long_options, &option_index)) != -1) + while ((c = getopt_long(argc, argv, "r:h:1:2:m:P", long_options, &option_index)) != -1) { switch (c) { @@ -122,6 +125,11 @@ int main(int argc, char **argv) } break; } + case 'P': + { + POSIX_MODE = true; + break; + } default: { exit(EXIT_FAILURE); @@ -137,8 +145,10 @@ int main(int argc, char **argv) /* Perform all the essential stages in order */ struct lilith* vm; + size_t image; vm = create_vm(Memory_Size); - load_program(vm, rom_name); + image = load_program(vm, rom_name); + if(POSIX_MODE) vm->reg[15] = image; execute_vm(vm); destroy_vm(vm); diff --git a/vm.h b/vm.h index 64021c2..fcaeba8 100644 --- a/vm.h +++ b/vm.h @@ -305,3 +305,6 @@ void outside_of_world(struct lilith* vm, unsigned_vm_register place, char* messa /* Allow tape names to be effectively changed */ char* tape_01_name; char* tape_02_name; + +/* Enable POSIX Mode */ +bool POSIX_MODE; diff --git a/vm_instructions.c b/vm_instructions.c index c87ffad..8533a9d 100644 --- a/vm_instructions.c +++ b/vm_instructions.c @@ -16,7 +16,9 @@ */ #include "vm.h" +#include #include +#include FILE* tape_01; FILE* tape_02; @@ -117,80 +119,140 @@ unsigned_vm_register shift_register(unsigned_vm_register source, unsigned_vm_reg return tmp; } +char* string_copy(struct lilith* vm, signed_vm_register address) +{ + int i = 0; + char* r = calloc(4096, sizeof(char)); + int c = vm->memory[address]; + while(0 != c) + { + r[i] = c; + i = i + 1; + c = vm->memory[address + i]; + } + + return r; +} + void vm_FOPEN_READ(struct lilith* vm) { struct stat sb; - if(0x00001100 == vm->reg[0]) + if(POSIX_MODE) { - if(-1 == stat(tape_01_name, &sb)) + char* s = string_copy(vm, vm->reg[0]); + if(-1 == stat(s, &sb)) { - fprintf(stderr, "File named %s does not exist\n", tape_01_name); + fprintf(stderr, "File named %s does not exist\n", s); exit(EXIT_FAILURE); } - tape_01 = fopen(tape_01_name, "r"); + vm->reg[0] = open(s, 0); + free(s); } - - if (0x00001101 == vm->reg[0]) + else { - if(-1 == stat(tape_02_name, &sb)) + if(0x00001100 == vm->reg[0]) { - fprintf(stderr, "File named %s does not exist\n", tape_02_name); - exit(EXIT_FAILURE); + if(-1 == stat(tape_01_name, &sb)) + { + fprintf(stderr, "File named %s does not exist\n", tape_01_name); + exit(EXIT_FAILURE); + } + tape_01 = fopen(tape_01_name, "r"); + } + + if (0x00001101 == vm->reg[0]) + { + if(-1 == stat(tape_02_name, &sb)) + { + fprintf(stderr, "File named %s does not exist\n", tape_02_name); + exit(EXIT_FAILURE); + } + tape_02 = fopen(tape_02_name, "r"); } - tape_02 = fopen(tape_02_name, "r"); } } void vm_FOPEN_WRITE(struct lilith* vm) { - if(0x00001100 == vm->reg[0]) + if(POSIX_MODE) { - tape_01 = fopen(tape_01_name, "w"); + char* s = string_copy(vm, vm->reg[0]); + /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */ + vm->reg[0] = open(s, 577, 384); + free(s); } - - if (0x00001101 == vm->reg[0]) + else { - tape_02 = fopen(tape_02_name, "w"); + if(0x00001100 == vm->reg[0]) + { + tape_01 = fopen(tape_01_name, "w"); + } + + if (0x00001101 == vm->reg[0]) + { + tape_02 = fopen(tape_02_name, "w"); + } } } void vm_FCLOSE(struct lilith* vm) { - if(0x00001100 == vm->reg[0]) + if(POSIX_MODE) { - fclose(tape_01); + close(vm->reg[0]); } - - if (0x00001101 == vm->reg[0]) + else { - fclose(tape_02); + if(0x00001100 == vm->reg[0]) + { + fclose(tape_01); + } + + if (0x00001101 == vm->reg[0]) + { + fclose(tape_02); + } } } void vm_FSEEK(struct lilith* vm) { - if(0x00001100 == vm->reg[0]) + if(POSIX_MODE) { - fseek(tape_01, vm->reg[1], SEEK_CUR); + lseek(vm->reg[0], vm->reg[1], SEEK_CUR); } - - if (0x00001101 == vm->reg[0]) + else { - fseek(tape_02, vm->reg[1], SEEK_CUR); + if(0x00001100 == vm->reg[0]) + { + fseek(tape_01, vm->reg[1], SEEK_CUR); + } + + if (0x00001101 == vm->reg[0]) + { + fseek(tape_02, vm->reg[1], SEEK_CUR); + } } } void vm_REWIND(struct lilith* vm) { - if(0x00001100 == vm->reg[0]) + if(POSIX_MODE) { - rewind(tape_01); + lseek(vm->reg[0], 0, SEEK_SET); } - - if (0x00001101 == vm->reg[0]) + else { - rewind(tape_02); + if(0x00001100 == vm->reg[0]) + { + rewind(tape_01); + } + + if (0x00001101 == vm->reg[0]) + { + rewind(tape_02); + } } } @@ -198,24 +260,32 @@ void vm_FGETC(struct lilith* vm) { signed_vm_register byte = -1; - if (0x00000000 == vm->reg[1]) + if(POSIX_MODE) { - #ifdef tty_lib - byte = tty_getchar(); - #endif - #ifndef tty_lib - byte = fgetc(stdin); - #endif + read(vm->reg[1], &byte, 1); + if(EOF != byte) byte = byte & 0xFF; } - - if(0x00001100 == vm->reg[1]) + else { - byte = fgetc(tape_01); - } + if (0x00000000 == vm->reg[1]) + { + #ifdef tty_lib + byte = tty_getchar(); + #endif + #ifndef tty_lib + byte = fgetc(stdin); + #endif + } - if (0x00001101 == vm->reg[1]) - { - byte = fgetc(tape_02); + if(0x00001100 == vm->reg[1]) + { + byte = fgetc(tape_01); + } + + if (0x00001101 == vm->reg[1]) + { + byte = fgetc(tape_02); + } } vm->reg[0] = byte; @@ -223,24 +293,31 @@ void vm_FGETC(struct lilith* vm) void vm_FPUTC(struct lilith* vm) { - signed_vm_register byte = vm->reg[0]; + signed_vm_register byte = vm->reg[0] & 0xFF; - if (0x00000000 == vm->reg[1]) + if(POSIX_MODE) { - fputc(byte, stdout); - #ifdef tty_lib - fflush(stdout); - #endif + write(vm->reg[1], &byte, 1); } - - if(0x00001100 == vm->reg[1]) + else { - fputc(byte, tape_01); - } + if (0x00000000 == vm->reg[1]) + { + fputc(byte, stdout); + #ifdef tty_lib + fflush(stdout); + #endif + } - if (0x00001101 == vm->reg[1]) - { - fputc(byte, tape_02); + if(0x00001100 == vm->reg[1]) + { + fputc(byte, tape_01); + } + + if (0x00001101 == vm->reg[1]) + { + fputc(byte, tape_02); + } } } diff --git a/wrapper.c b/wrapper.c index 8a988af..7a95f80 100644 --- a/wrapper.c +++ b/wrapper.c @@ -52,8 +52,9 @@ void execute_vm(struct lilith* vm) return; } -void initialize_lilith(unsigned int size) +void initialize_lilith(unsigned int size, unsigned int posix) { + POSIX_MODE = posix; tape_01_name = "tape_01"; tape_02_name = "tape_02"; struct lilith* vm;