diff --git a/ISA_HEX_Map.org b/ISA_HEX_Map.org index f93cd98..f50c212 100644 --- a/ISA_HEX_Map.org +++ b/ISA_HEX_Map.org @@ -453,13 +453,28 @@ DO NOT USE. 42 hh hh hh is the HALCODE callID, invalid HALCODE SHOULD NOT BE USED. *** HALCODE Reserved for Operating Systems -The following block contains both instructions that are reserved for Operating systems and for internal use by Operating systems +The following block contains both instructions that are reserved for Operating +systems and for internal use by Operating systems. When initialized by the +Operating system, the HALCODEs are tagged with a priviledge mode, if the binary +is not also tagged with permissions for that mode, an illegal instruction trap +occurs when the HALCODE is decoded. -| Hex | Name | -|-------------+----------| -| 42 00 xx xx | Reserved | -| ... | Reserved | -| 42 0F xx xx | Reserved | +The default reserved Block: 42000000-420FFFFF; however all HALCODEs can be remapped, +replaced and repurposed by the Operating system after initialization and you should +refer to your system documentation from your Operating system provider to know what +mappings have been exposed for use. + +**** Example posix mapping +| Hex | Name | Comment | +|-------------+--------+----------------------------------------------------------------------------| +| 42 00 00 02 | FOPEN | open(char* FILENAME, int FLAGS, int MODE) using R0, R1 and R2 respectively | +| 42 00 00 03 | FCLOSE | close(int FD) using R0 | +| 42 00 00 08 | FSEEK | seek(int FD, off_t OFFSET, int WHENCE) using R0, R1 and R2 respectively | +| 42 00 00 3C | EXIT | exit(int ERROR_CODE) using R0 | +| 42 00 00 5A | CHMOD | chmod(char* PATHNAME, int MODE) using R0 and R1 respectively | +|-------------+--------+----------------------------------------------------------------------------| +| 42 10 01 00 | FGETC | read 1 byte into register 0 from device who's ID is in register 1 | +| 42 10 02 00 | FPUTC | write 1 byte from register 0 to device who's ID is in register 1 | *** Tape console HALCODE This HALCODE is used for interacting with any tape console attached to the system. @@ -539,7 +554,6 @@ The following 3 devices must exist with the following exact IDs | ... | Reserved | | 42 FF FF FF | Reserved | - ** Reserved Block 1 At this time these instructions only produce a warning; but could do anything. DO NOT USE. diff --git a/vm.h b/vm.h index fcaeba8..06cfa0d 100644 --- a/vm.h +++ b/vm.h @@ -94,6 +94,9 @@ struct Instruction }; /* Prototypes for functions in vm_instructions.c*/ +void vm_EXIT(struct lilith* vm, uint64_t performance_counter); +void vm_CHMOD(struct lilith* vm); +void vm_FOPEN(struct lilith* vm); void vm_FOPEN_READ(struct lilith* vm); void vm_FOPEN_WRITE(struct lilith* vm); void vm_FCLOSE(struct lilith* vm); diff --git a/vm_decode.c b/vm_decode.c index 76423cb..0e9ab0e 100644 --- a/vm_decode.c +++ b/vm_decode.c @@ -232,101 +232,193 @@ bool eval_HALCODE(struct lilith* vm, struct Instruction* c) char Name[20] = "ILLEGAL_HALCODE"; #endif - switch(c->HAL_CODE) + if(POSIX_MODE) { - case 0x100000: /* fopen_read */ + switch(c->HAL_CODE) { - #ifdef DEBUG - strncpy(Name, "FOPEN_READ", 19); - #elif TRACE - record_trace("FOPEN_READ"); - #endif + case 0x000002: /* fopen */ + { + #ifdef DEBUG + strncpy(Name, "FOPEN", 19); + #elif TRACE + record_trace("FOPEN"); + #endif - vm_FOPEN_READ(vm); - break; - } - case 0x100001: /* fopen_write */ - { - #ifdef DEBUG - strncpy(Name, "FOPEN_WRITE", 19); - #elif TRACE - record_trace("FOPEN_WRITE"); - #endif + vm_FOPEN(vm); + break; + } + case 0x000003: /* fclose */ + { + #ifdef DEBUG + strncpy(Name, "FCLOSE", 19); + #elif TRACE + record_trace("FCLOSE"); + #endif - vm_FOPEN_WRITE(vm); - break; - } - case 0x100002: /* fclose */ - { - #ifdef DEBUG - strncpy(Name, "FCLOSE", 19); - #elif TRACE - record_trace("FCLOSE"); - #endif + vm_FCLOSE(vm); + break; + } + case 0x000008: /* fseek */ + { + #ifdef DEBUG + strncpy(Name, "FSEEK", 19); + #elif TRACE + record_trace("FSEEK"); + #endif - vm_FCLOSE(vm); - break; - } - case 0x100003: /* rewind */ - { - #ifdef DEBUG - strncpy(Name, "REWIND", 19); - #elif TRACE - record_trace("REWIND"); - #endif + vm_FSEEK(vm); + break; + } + case 0x00003C: /* EXIT */ + { + #ifdef DEBUG + strncpy(Name, "EXIT", 19); + #elif TRACE + record_trace("EXIT"); + #endif - vm_REWIND(vm); - break; - } - case 0x100004: /* fseek */ - { - #ifdef DEBUG - strncpy(Name, "FSEEK", 19); - #elif TRACE - record_trace("FSEEK"); - #endif + vm_EXIT(vm, performance_counter); + break; + } + case 0x00005A: /* EXIT */ + { + #ifdef DEBUG + strncpy(Name, "CHMOD", 19); + #elif TRACE + record_trace("CHMOD"); + #endif - vm_FSEEK(vm); - break; - } - case 0x100100: /* fgetc */ - { - #ifdef DEBUG - strncpy(Name, "FGETC", 19); - #elif TRACE - record_trace("FGETC"); - #endif + vm_CHMOD(vm); + break; + } + case 0x100100: /* fgetc */ + { + #ifdef DEBUG + strncpy(Name, "FGETC", 19); + #elif TRACE + record_trace("FGETC"); + #endif - vm_FGETC(vm); - break; - } - case 0x100200: /* fputc */ - { - #ifdef DEBUG - strncpy(Name, "FPUTC", 19); - #elif TRACE - record_trace("FPUTC"); - #endif + vm_FGETC(vm); + break; + } + case 0x100200: /* fputc */ + { + #ifdef DEBUG + strncpy(Name, "FPUTC", 19); + #elif TRACE + record_trace("FPUTC"); + #endif - vm_FPUTC(vm); - break; + vm_FPUTC(vm); + break; + } + default: + { + fprintf(stderr, "Invalid HALCODE\nComputer Program has Halted\n"); + illegal_instruction(vm, c); + break; + } } - case 0x110000: /* HAL_MEM */ + } + else + { + switch(c->HAL_CODE) { - #ifdef DEBUG - strncpy(Name, "HAL_MEM", 19); - #elif TRACE - record_trace("HAL_MEM"); - #endif + case 0x100000: /* fopen_read */ + { + #ifdef DEBUG + strncpy(Name, "FOPEN_READ", 19); + #elif TRACE + record_trace("FOPEN_READ"); + #endif - vm_HAL_MEM(vm); - break; - } - default: - { - fprintf(stderr, "Invalid HALCODE\nComputer Program has Halted\n"); - illegal_instruction(vm, c); - break; + vm_FOPEN_READ(vm); + break; + } + case 0x100001: /* fopen_write */ + { + #ifdef DEBUG + strncpy(Name, "FOPEN_WRITE", 19); + #elif TRACE + record_trace("FOPEN_WRITE"); + #endif + + vm_FOPEN_WRITE(vm); + break; + } + case 0x100002: /* fclose */ + { + #ifdef DEBUG + strncpy(Name, "FCLOSE", 19); + #elif TRACE + record_trace("FCLOSE"); + #endif + + vm_FCLOSE(vm); + break; + } + case 0x100003: /* rewind */ + { + #ifdef DEBUG + strncpy(Name, "REWIND", 19); + #elif TRACE + record_trace("REWIND"); + #endif + + vm_REWIND(vm); + break; + } + case 0x100004: /* fseek */ + { + #ifdef DEBUG + strncpy(Name, "FSEEK", 19); + #elif TRACE + record_trace("FSEEK"); + #endif + + vm_FSEEK(vm); + break; + } + case 0x100100: /* fgetc */ + { + #ifdef DEBUG + strncpy(Name, "FGETC", 19); + #elif TRACE + record_trace("FGETC"); + #endif + + vm_FGETC(vm); + break; + } + case 0x100200: /* fputc */ + { + #ifdef DEBUG + strncpy(Name, "FPUTC", 19); + #elif TRACE + record_trace("FPUTC"); + #endif + + vm_FPUTC(vm); + break; + } + case 0x110000: /* HAL_MEM */ + { + #ifdef DEBUG + strncpy(Name, "HAL_MEM", 19); + #elif TRACE + record_trace("HAL_MEM"); + #endif + + vm_HAL_MEM(vm); + break; + } + default: + { + fprintf(stderr, "Invalid HALCODE\nComputer Program has Halted\n"); + illegal_instruction(vm, c); + break; + } } } diff --git a/vm_instructions.c b/vm_instructions.c index 8533a9d..f88640b 100644 --- a/vm_instructions.c +++ b/vm_instructions.c @@ -134,6 +134,40 @@ char* string_copy(struct lilith* vm, signed_vm_register address) return r; } +void vm_EXIT(struct lilith* vm, uint64_t performance_counter) +{ + vm->halted = true; + fprintf(stderr, "Computer Program has Halted\nAfter Executing %lu instructions\n", performance_counter); + + #ifdef TRACE + record_trace("HALT"); + print_traces(); + #endif + + exit(vm->reg[0]); +} + +void vm_CHMOD(struct lilith* vm) +{ + char* s = string_copy(vm, vm->reg[0]); + chmod(s, vm->reg[1]); + free(s); +} + +void vm_FOPEN(struct lilith* vm) +{ + struct stat sb; + + char* s = string_copy(vm, vm->reg[0]); + if(-1 == stat(s, &sb)) + { + fprintf(stderr, "File named %s does not exist\n", s); + exit(EXIT_FAILURE); + } + vm->reg[0] = open(s, vm->reg[1], vm->reg[2]); + free(s); +} + void vm_FOPEN_READ(struct lilith* vm) { struct stat sb;