80 lines
2.0 KiB
C
80 lines
2.0 KiB
C
|
#ifndef bci_interpreter_h__
|
||
|
#define bci_interpreter_h__
|
||
|
|
||
|
#include <stddef.h>
|
||
|
#include "base.h"
|
||
|
#include "method_dispatcher.h"
|
||
|
#include "stack.h"
|
||
|
|
||
|
typedef 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;
|
||
|
|
||
|
|
||
|
dispatch_tree_t method_dispatcher;
|
||
|
|
||
|
} * machine_state_t;
|
||
|
|
||
|
machine_state_t machine_state_t_new( void * cache_function_data
|
||
|
, size_t cache_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 > program_size
|
||
|
* - 0b01000000 | 2: failed to cache new data
|
||
|
* - 0b01000000 | 3: argument is out of program bounds
|
||
|
*
|
||
|
* - 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);
|
||
|
|
||
|
/*
|
||
|
* 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);
|
||
|
|
||
|
#endif
|