Enable POSIX-MODE in knight-vm for testing purposes

This commit is contained in:
Jeremiah Orians 2019-02-28 22:43:47 -05:00
parent 682b85e5dd
commit 5493a77697
No known key found for this signature in database
GPG Key ID: 5410E91C14959E87
6 changed files with 164 additions and 65 deletions

View File

@ -29,6 +29,7 @@ struct arguments
}; };
int IP; int IP;
char* image;
int string_length(char* s) int string_length(char* s)
{ {
@ -64,6 +65,12 @@ void write_int(int value, FILE* output)
void clone_file(FILE* in, FILE* out) 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; int c;
for(c = fgetc(in); EOF != c; c = fgetc(in)) 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* head = NULL;
struct arguments* i; 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) while(option_index < argc)
{ {
i = calloc(1, sizeof(struct arguments)); i = calloc(1, sizeof(struct arguments));

View File

@ -21,7 +21,7 @@ import sys, getopt
vm = ctypes.CDLL('./libvm.so') 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.argtype = ctypes.c_uint
vm.get_memory.restype = ctypes.c_char_p vm.get_memory.restype = ctypes.c_char_p
vm.step_lilith.restype = ctypes.c_uint vm.step_lilith.restype = ctypes.c_uint
@ -48,7 +48,7 @@ def Reset_lilith():
chunks = Memory_Size / (1024 * 1024 * 1024) chunks = Memory_Size / (1024 * 1024 * 1024)
print("Current Memory Size is: " + str(chunks) + unit) print("Current Memory Size is: " + str(chunks) + unit)
vm.initialize_lilith(Memory_Size) vm.initialize_lilith(Memory_Size, POSIX_MODE)
global Current_IP global Current_IP
Current_IP = 0 Current_IP = 0
global Watchpoints global Watchpoints

16
vm.c
View File

@ -19,7 +19,7 @@
#include <getopt.h> #include <getopt.h>
/* Load program tape into Memory */ /* 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; FILE* program;
program = fopen(rom_name, "r"); program = fopen(rom_name, "r");
@ -40,6 +40,7 @@ void load_program(struct lilith* vm, char* rom_name)
fread(vm->memory, 1, end, program); fread(vm->memory, 1, end, program);
fclose(program); fclose(program);
return end;
} }
void execute_vm(struct lilith* vm) void execute_vm(struct lilith* vm)
@ -60,6 +61,7 @@ void execute_vm(struct lilith* vm)
/* Standard C main program */ /* Standard C main program */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
POSIX_MODE = false;
int c; int c;
int Memory_Size = (16 * 1024); int Memory_Size = (16 * 1024);
@ -73,11 +75,12 @@ int main(int argc, char **argv)
{"tape_01", required_argument, 0, '1'}, {"tape_01", required_argument, 0, '1'},
{"tape_02", required_argument, 0, '2'}, {"tape_02", required_argument, 0, '2'},
{"memory", required_argument, 0, 'm'}, {"memory", required_argument, 0, 'm'},
{"POSIX-MODE", no_argument, 0, 'P'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
int option_index = 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) switch (c)
{ {
@ -122,6 +125,11 @@ int main(int argc, char **argv)
} }
break; break;
} }
case 'P':
{
POSIX_MODE = true;
break;
}
default: default:
{ {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -137,8 +145,10 @@ int main(int argc, char **argv)
/* Perform all the essential stages in order */ /* Perform all the essential stages in order */
struct lilith* vm; struct lilith* vm;
size_t image;
vm = create_vm(Memory_Size); 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); execute_vm(vm);
destroy_vm(vm); destroy_vm(vm);

3
vm.h
View File

@ -305,3 +305,6 @@ void outside_of_world(struct lilith* vm, unsigned_vm_register place, char* messa
/* Allow tape names to be effectively changed */ /* Allow tape names to be effectively changed */
char* tape_01_name; char* tape_01_name;
char* tape_02_name; char* tape_02_name;
/* Enable POSIX Mode */
bool POSIX_MODE;

View File

@ -16,7 +16,9 @@
*/ */
#include "vm.h" #include "vm.h"
#include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
FILE* tape_01; FILE* tape_01;
FILE* tape_02; FILE* tape_02;
@ -117,80 +119,140 @@ unsigned_vm_register shift_register(unsigned_vm_register source, unsigned_vm_reg
return tmp; 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) void vm_FOPEN_READ(struct lilith* vm)
{ {
struct stat sb; 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); exit(EXIT_FAILURE);
} }
tape_01 = fopen(tape_01_name, "r"); vm->reg[0] = open(s, 0);
free(s);
} }
else
if (0x00001101 == vm->reg[0])
{ {
if(-1 == stat(tape_02_name, &sb)) if(0x00001100 == vm->reg[0])
{ {
fprintf(stderr, "File named %s does not exist\n", tape_02_name); if(-1 == stat(tape_01_name, &sb))
exit(EXIT_FAILURE); {
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) 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);
} }
else
if (0x00001101 == vm->reg[0])
{ {
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) void vm_FCLOSE(struct lilith* vm)
{ {
if(0x00001100 == vm->reg[0]) if(POSIX_MODE)
{ {
fclose(tape_01); close(vm->reg[0]);
} }
else
if (0x00001101 == vm->reg[0])
{ {
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) 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);
} }
else
if (0x00001101 == vm->reg[0])
{ {
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) void vm_REWIND(struct lilith* vm)
{ {
if(0x00001100 == vm->reg[0]) if(POSIX_MODE)
{ {
rewind(tape_01); lseek(vm->reg[0], 0, SEEK_SET);
} }
else
if (0x00001101 == vm->reg[0])
{ {
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; signed_vm_register byte = -1;
if (0x00000000 == vm->reg[1]) if(POSIX_MODE)
{ {
#ifdef tty_lib read(vm->reg[1], &byte, 1);
byte = tty_getchar(); if(EOF != byte) byte = byte & 0xFF;
#endif
#ifndef tty_lib
byte = fgetc(stdin);
#endif
} }
else
if(0x00001100 == vm->reg[1])
{ {
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]) if(0x00001100 == vm->reg[1])
{ {
byte = fgetc(tape_02); byte = fgetc(tape_01);
}
if (0x00001101 == vm->reg[1])
{
byte = fgetc(tape_02);
}
} }
vm->reg[0] = byte; vm->reg[0] = byte;
@ -223,24 +293,31 @@ void vm_FGETC(struct lilith* vm)
void vm_FPUTC(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); write(vm->reg[1], &byte, 1);
#ifdef tty_lib
fflush(stdout);
#endif
} }
else
if(0x00001100 == vm->reg[1])
{ {
fputc(byte, tape_01); if (0x00000000 == vm->reg[1])
} {
fputc(byte, stdout);
#ifdef tty_lib
fflush(stdout);
#endif
}
if (0x00001101 == vm->reg[1]) if(0x00001100 == vm->reg[1])
{ {
fputc(byte, tape_02); fputc(byte, tape_01);
}
if (0x00001101 == vm->reg[1])
{
fputc(byte, tape_02);
}
} }
} }

View File

@ -52,8 +52,9 @@ void execute_vm(struct lilith* vm)
return; 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_01_name = "tape_01";
tape_02_name = "tape_02"; tape_02_name = "tape_02";
struct lilith* vm; struct lilith* vm;