From 682b85e5dd1a488ab1a929d14490ecc74b144458 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Tue, 26 Feb 2019 21:53:32 -0500 Subject: [PATCH] Solved the problem of testing knight-posix binaries --- High_level_prototypes/execve_image.c | 140 +++++++++++++++++++++++++++ User_Interface.py | 13 ++- wrapper.c | 3 +- 3 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 High_level_prototypes/execve_image.c diff --git a/High_level_prototypes/execve_image.c b/High_level_prototypes/execve_image.c new file mode 100644 index 0000000..192f5bf --- /dev/null +++ b/High_level_prototypes/execve_image.c @@ -0,0 +1,140 @@ +/* -*- c-file-style: "linux";indent-tabs-mode:t -*- */ +/* Copyright (C) 2017 Jeremiah Orians + * Copyright (C) 2017 Jan Nieuwenhuizen + * This file is part of mescc-tools + * + * mescc-tools is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * mescc-tools is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with mescc-tools. If not, see . + */ + +#include +#include + +struct arguments +{ + struct arguments* next; + int ip; + int size; + char* text; +}; + +int IP; + +int string_length(char* s) +{ + int c = 0; + while(0 != s[0]) + { + c = c + 1; + s = s + 1; + } + + c = (c | 0x3) + 1; + return c; +} + +void write_padded(char* s, int c, FILE* out) +{ + while(0 < c) + { + fputc(s[0], out); + if(0 != s[0]) s = s + 1; + c = c - 1; + } +} + +void write_int(int value, FILE* output) +{ + fputc((value >> 24), output); + fputc(((value >> 16)%256), output); + fputc(((value >> 8)%256), output); + fputc((value % 256), output); + IP = IP + 4; +} + +void clone_file(FILE* in, FILE* out) +{ + int c; + for(c = fgetc(in); EOF != c; c = fgetc(in)) + { + IP = IP + 1; + fputc(c, out); + } +} + +struct arguments* reverse_list(struct arguments* head) +{ + struct arguments* root = NULL; + while(NULL != head) + { + struct arguments* next = head->next; + head->next = root; + root = head; + head = next; + } + return root; +} + + +/* Standard C main program */ +int main(int argc, char **argv) +{ + IP = 0; + int count = 0; + int table; + struct arguments* head = NULL; + struct arguments* i; + + clone_file(fopen(argv[1], "r"), stdout); + + int option_index = 2; + while(option_index < argc) + { + i = calloc(1, sizeof(struct arguments)); + i->next = head; + head = i; + head->ip = IP; + head->text = argv[option_index]; + option_index = option_index + 1; + head->size = string_length(head->text); + IP = IP + head->size; + count = count + 1; + } + + head = reverse_list(head); + + for(i = head; NULL != i; i = i->next) + { + write_padded(i->text, i->size, stdout); + } + + /* Get Address of the ARGV strings */ + table = IP; + + /* Write out pointers to argvs */ + for(i = head; NULL != i; i = i->next) + { + write_int(i->ip, stdout); + } + + /* Do the NULL padding */ + write_int(0, stdout); + + /* Write out the ARGC */ + write_int(count, stdout); + write_int(table, stdout); + write_int(0, stdout); + write_int(0, stdout); + + return EXIT_SUCCESS; +} diff --git a/User_Interface.py b/User_Interface.py index 19ad696..be87be0 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -30,6 +30,8 @@ vm.set_memory.argtype = (ctypes.c_uint, ctypes.c_ubyte) vm.get_register.argtype = ctypes.c_uint vm.get_register.restype = ctypes.c_uint +POSIX_MODE = False + def Reset_lilith(): global Memory_Size if 0 <= Memory_Size < 1024: @@ -52,7 +54,11 @@ def Reset_lilith(): global Watchpoints Watchpoints = {0} global ROM_Name - vm.load_lilith(ctypes.create_string_buffer(ROM_Name.encode('ascii'))) + size = vm.load_lilith(ctypes.create_string_buffer(ROM_Name.encode('ascii'))) + print ("Size of loaded ROM image: " + str(size) + " bytes\n") + + if(POSIX_MODE): + Set_Register(15, size) def Step_lilith(): global Current_IP @@ -216,7 +222,7 @@ def main(argv): global Debug_Point help_string = 'Knight.py --ROM=$NAME [--DEBUG=$NUMBER] [--WINDOW=$NUMBER] [--MEMORY=$SIZE]\n' try: - opts, args = getopt.getopt(argv,"R:D:W:M:",["ROM=","DEBUG=","WINDOW=", "MEMORY="]) + opts, args = getopt.getopt(argv,"R:D:W:M:P:",["ROM=","DEBUG=","WINDOW=", "MEMORY=", "POSIX-MODE"]) except getopt.GetoptError: print (help_string) sys.exit(2) @@ -241,6 +247,9 @@ def main(argv): Memory_Size = (int(arg[:-1]) * 1024 * 1024) elif arg.endswith('G'): Memory_Size = (int(arg[:-1]) * 1024 * 1024 * 1024) + elif opt in ("-P", "--POSIX-MODE"): + global POSIX_MODE + POSIX_MODE = True subprocess.call("./bin/dis " + ROM_Name + " >| z_disassembled", shell=True) Reset_lilith() diff --git a/wrapper.c b/wrapper.c index 00285e1..8a988af 100644 --- a/wrapper.c +++ b/wrapper.c @@ -23,7 +23,7 @@ static struct lilith* Globalvm; void unpack_byte(uint8_t a, char* c); /* Load program tape into Memory */ -void load_program(struct lilith* vm, char* name) +size_t load_program(struct lilith* vm, char* name) { FILE* program; program = fopen(name, "r"); @@ -37,6 +37,7 @@ void load_program(struct lilith* vm, char* name) fread(vm->memory, 1, end, program); fclose(program); + return end; } void execute_vm(struct lilith* vm)