BCI-base/interpreter/interpreter.h

122 lines
3.3 KiB
C

#ifndef bci_interpreter_h__
#define bci_interpreter_h__
#include <stddef.h>
#include "../base.h"
#include "../method_dispatcher/method_dispatcher.h"
#include "../stack.h"
typedef struct machine_state_s
{
// The size of the program in 16-bit words
uint16_t program_size;
void * cache_function_data;
size_t cache_size;
uint16_t cache_offset;
uint16_t * cache;
size_t (* cache_block)(uint16_t * cache
, uint16_t offset
, size_t cache_size
, void * cache_function_data);
char cache_loaded;
uint16_t program_counter;
uint16_t status_reg;
// Any non-zero value in this register will
// break the program execution.
uint16_t shutdown_reg;
uint16_t data_reg[BCI_CORE_NUM_REG];
bci_stack_t stack;
size_t memory_size;
uint16_t * memory;
dispatch_tree_t method_dispatcher;
// Possible Error Codes:
// 0b01xxxxxx: memory related internal error
// 0b10xxxxxx: core method related internal error
// 0b00xxxxxx: application related internal error
//
//
// 0b01000000 | 1: Program offset out of bounds
// 0b01000000 | 2: Failed to cache new data
// 0b01000000 | 15: Memory offset out of bounds
//
//
// 0b10000000 | 1: Unknown opcode
char error;
} * machine_state_t;
machine_state_t machine_state_t_new( void * cache_function_data
, size_t program_size
, size_t cache_size
, size_t memory_size
, size_t (* cache_block)(uint16_t * cache
, uint16_t offset
, size_t cache_size
, void * cache_function_data)
, dispatch_tree_t method_dispatcher);
void machine_state_t_del(machine_state_t state);
/*
* This function essentially executes one cycle of the machine execution.
* It will fetch the opcode and argument, dispatch the method and execute it.
*
* Also this will update the cache if needed.
*
* Possible return codes:
*
* - 0bxyXXXXXX
* Error while executing the cycle.
* - 0b01XXXXXX
* Error in the internal memory management (i.e. program counter out
* of bounds)
* - 0b01000000 | 1: program_counter or argument offset > program_size
* - 0b01000000 | 2: failed to cache new data
*
* - 0b10XXXXXX
* Error while dispatching the method
* - 0b10000000 | 1: unknown opcode
*
* - 0b00XXXXXX
* XXXXXX is the return value of the bci_core_method_t method.
* */
char machine_state_t_exec_cycle(machine_state_t state);
/*
* Runs ``machine_state_t_exec_cycle`` until either this function
* returns a non-zero value or a non-zero value is stored in
* ``state->shutdown_reg``.
* Sets ``state->error`` to the last return value of ``machine_state_t_exec_cycle``
* and returns it.
* */
char machine_state_t_exec(machine_state_t state);
/*
* Do the default updates on the machine state.
* This will be basically increasing the program counter.
* */
char machine_state_t_default_afterexec(machine_state_t state, uint8_t argc);
/*
* Get the word at offset in the program memory.
* Returns 0 on success, a non-zero value otherwise.
*
* It will set state->error appropiately.
* */
char machine_state_t_get_word(machine_state_t state, uint16_t offset, uint16_t * result);
/*
* Get the word at offset in the RAM-alike memory.
* Works like machine_state_t_get_word.
* */
char machine_state_t_get_mem(machine_state_t state, uint16_t offset, uint16_t * result);
char machine_state_t_set_mem(machine_state_t state, uint16_t offset, uint16_t data);
#endif