BCI-base/interpreter/test/test_interpreter.c

70 lines
1.8 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "../interpreter.h"
#include "../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);
int main(void)
{
dispatch_tree_t tree = dispatch_tree_t_new();
dispatch_tree_autoinserter_t inserter = dispatch_tree_autoinserter_t_new(tree);
uint16_t ldi = dispatch_tree_autoinserter_t_insert(inserter, (bci_core_method_t) bci_cm_ldi);
uint16_t add = dispatch_tree_autoinserter_t_insert(inserter, (bci_core_method_t) bci_cm_add);
uint16_t * program = calloc(sizeof(uint16_t), 20);
program[0] = ldi;
program[1] = 0xfefe;
program[2] = ldi | 1;
program[3] = 2;
program[4] = ldi | 2;
program[5] = 3;
program[6] = add | 1;
program[7] = 2;
machine_state_t state = machine_state_t_new(
program
, 8
, 20
, 20
, copy_cache
, tree);
machine_state_t_exec_cycle(state);
machine_state_t_exec_cycle(state);
machine_state_t_exec_cycle(state);
machine_state_t_exec_cycle(state);
printf("data_reg[0]: %x\n", state->data_reg[0]);
assert(state->data_reg[0] == 0xfefe);
printf("data_reg[1]: %x\n", state->data_reg[1]);
assert(state->data_reg[1] == 5);
printf("data_reg[2]: %x\n", state->data_reg[2]);
assert(state->data_reg[2] == 3);
printf("program_counter: %d\n", state->program_counter);
assert(state->program_counter == 8);
dispatch_tree_autoinserter_t_del(inserter);
machine_state_t_del(state);
free(program);
return 0;
}
size_t copy_cache(uint16_t * cache
, uint16_t offset
, size_t cache_size
, void * cache_function_data)
{
size_t i;
for(i = 0; i < cache_size; i++)
{
printf("\tCP [%zd]->[%zd]: %x\n", i + offset, i, ((uint16_t *) cache_function_data)[i + offset]);
cache[i] = ((uint16_t *) cache_function_data)[i + offset];
}
return i;
}