added global test
This commit is contained in:
parent
a049d1963d
commit
e73d0d9b71
29
test/Makefile
Normal file
29
test/Makefile
Normal file
|
@ -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
|
||||
|
118
test/main.c
Normal file
118
test/main.c
Normal file
|
@ -0,0 +1,118 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#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;
|
||||
}
|
140
test/test.py
Normal file
140
test/test.py
Normal file
|
@ -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)
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user