From e73d0d9b7157b14b8d9185225bc26d0a2244a561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Sat, 27 Oct 2018 16:54:33 +0200 Subject: [PATCH] added global test --- test/Makefile | 29 +++++++++++ test/main.c | 118 ++++++++++++++++++++++++++++++++++++++++++ test/test.py | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 287 insertions(+) create mode 100644 test/Makefile create mode 100644 test/main.c create mode 100644 test/test.py diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..1466c91 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,29 @@ +CC=gcc +CFLAG= -c -g -O -DDEBUG -I ../interpreter -o +objects=../interpreter/stack.o \ + ../interpreter/interpreter/core_methods.o \ + ../interpreter/interpreter/interpreter.o \ + ../interpreter/method_dispatcher/method_dispatcher.o \ + main.o + + +all: clean lib $(objects) test +clean: + - rm code.bin + - rm main +lib: + cd ../interpreter && make -s + +%.o: %.c + $(CC) $(CFLAG) $@ $< + +main: main.o + $(CC) -O -g $(objects) -o main + +test: main code.bin + cd ../assembler && make test + valgrind ./main + +code.bin: + python3 test.py + diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..6cf0732 --- /dev/null +++ b/test/main.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include "interpreter/interpreter.h" +#include "interpreter/core_methods.h" +#include "method_dispatcher/method_dispatcher.h" + +size_t copy_cache(uint16_t * cache + , uint16_t offset + , size_t cache_size + , void * cache_function_data); + +bci_core_method_t core_methods[22] = { + (bci_core_method_t) bci_cm_ldi + , (bci_core_method_t) bci_cm_ld + , (bci_core_method_t) bci_cm_st + , (bci_core_method_t) bci_cm_inc + , (bci_core_method_t) bci_cm_dec + , (bci_core_method_t) bci_cm_add + , (bci_core_method_t) bci_cm_sub + , (bci_core_method_t) bci_cm_mul + , (bci_core_method_t) bci_cm_div + , (bci_core_method_t) bci_cm_gt + , (bci_core_method_t) bci_cm_ge + , (bci_core_method_t) bci_cm_lt + , (bci_core_method_t) bci_cm_le + , (bci_core_method_t) bci_cm_eq + , (bci_core_method_t) bci_cm_not + , (bci_core_method_t) bci_cm_jmp + , (bci_core_method_t) bci_cm_cjmp + , (bci_core_method_t) bci_cm_call + , (bci_core_method_t) bci_cm_ccall + , (bci_core_method_t) bci_cm_ret + , (bci_core_method_t) bci_cm_cl + , (bci_core_method_t) bci_cm_stop +}; + +int main(void) +{ + int i; + + FILE * program = fopen("code.bin", "r"); + if(!program) + { + return 1; + } + uint16_t program_size; + read(fileno(program), &program_size, 2); + + dispatch_tree_t tree = dispatch_tree_t_new(); + dispatch_tree_autoinserter_t inserter = dispatch_tree_autoinserter_t_new(tree); + for(i = 0; i < 22; i++) + { + dispatch_tree_autoinserter_t_insert(inserter, core_methods[i]); + } + + machine_state_t state = machine_state_t_new( + program + , program_size + , 1024 + , 512 + , copy_cache + , tree); + + char status = machine_state_t_exec(state); + + printf("######## STATUS ##############\n"); + printf("state->error = 0x%x\n", state->error); + printf("state->shutdown_reg = 0x%x\n", state->shutdown_reg); + printf("state->status_reg = 0x%x\n", state->status_reg); + printf("state->program_counter = 0x%x\n", state->program_counter); + printf("######## REGISTERS ###########\n"); + for(i = 0; i < BCI_CORE_NUM_REG; i++) + { + printf("\t[%d] = 0x%x\n", i, state->data_reg[i]); + } +// printf("####### MEMORY ###############\n"); +// for(i = 0; i < 512; i++) +// { +// printf("\t[%d] = 0x%x\n", i, state->memory[i]); +// } + + machine_state_t_del(state); + fclose(program); + return 0; +} + +size_t copy_cache(uint16_t * cache + , uint16_t offset + , size_t cache_size + , void * cache_function_data) +{ + FILE * program_file = (FILE *) cache_function_data; + size_t did_read = 0; + + fseek(program_file, (offset + 1) * 2, 0); + did_read = read(fileno(program_file), cache, cache_size * 2); + + if(did_read == 0 || did_read > cache_size) + { + // This is an actual error. + return 0; + } + if(did_read < cache_size && offset == 0) + { + // We just hit EOF, but that is not a problem, + // our program is just smaller than the cache. + int i; + for(i = did_read; i < cache_size; i++) + { + cache[i] = 0; + } + return cache_size; + } + + return did_read / 2; +} diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..cf1a6ef --- /dev/null +++ b/test/test.py @@ -0,0 +1,140 @@ +import sys +from io import StringIO + +sys.path.append("../assembler") +from assembler.assembler import Assembler + + +mnemonic_order = [ "ldi" , "ld" , "st" , "inc" , "dec" , "add" , "sub" + , "mul" , "div" , "gt" , "ge" , "lt" , "le" , "eq" , "not" + , "jmp" , "cjmp" , "call" , "ccall" , "ret" , "cl" , "stop" ] + +mnemonics = [ + { + "mnemonic": "ldi" + , "args": ["register", "direct_input"] + } + , { + "mnemonic": "ld" + , "args": ["register", "register"] + } + , { + "mnemonic": "st" + , "args": ["register", "register"] + } + , { + "mnemonic": "inc" + , "args": ["register"] + } + , { + "mnemonic": "dec" + , "args": ["register"] + } + , { + "mnemonic": "add" + , "args": ["register", "register"] + } + , { + "mnemonic": "sub" + , "args": ["register", "register"] + } + , { + "mnemonic": "mul" + , "args": ["register", "register"] + } + , { + "mnemonic": "div" + , "args": ["register", "register"] + } + , { + "mnemonic": "gt" + , "args": ["register"] + } + , { + "mnemonic": "ge" + , "args": ["register"] + } + , { + "mnemonic": "lt" + , "args": ["register"] + } + , { + "mnemonic": "le" + , "args": ["register"] + } + , { + "mnemonic": "eq" + , "args": ["register"] + } + , { + "mnemonic": "not" + , "args": [] + } + , { + "mnemonic": "jmp" + , "args": ["register"] + } + , { + "mnemonic": "call" + , "args": ["register"] + } + , { + "mnemonic": "ret" + , "args": [] + } + , { + "mnemonic": "stop" + , "args": [] + } + , { + "mnemonic": "cl" + , "args": [] + } + , { + "mnemonic": "cjmp" + , "args": ["register"] + } + , { + "mnemonic": "ccall" + , "args": ["register"] + } +] + +mnemonics = sorted(mnemonics, key=lambda x: mnemonic_order.index(x["mnemonic"])) + +memory_definition = { + "registers": 40 + , "memory": 512 + , "program_memory": 2048 +} + +if( __name__ == "__main__"): + data = StringIO( + ''' + ldi r1, 12 + ldi r0, 2 + ldi r2, 0xff + ldi r3, loop + + loop: + add r0, r2 + dec r1 + gt r1 + ; r3 = loop + cjmp r3 + + stop + ''' + ) + assembler = Assembler(data, memory_definition, mnemonics, {}) + assembler.parse() + + for i in assembler._code_objects: + print(i, i&0b1111111111000000, i&0b0000000000111111) + + with open("code.bin", "wb") as f: + assembler.bindump(f) + + + +