added machine_state_t_exec and fixed some bugs

This commit is contained in:
Daniel Knüttel 2018-10-27 16:53:40 +02:00
parent 84ca38924e
commit d472a14f59
3 changed files with 42 additions and 3 deletions

View File

@ -271,16 +271,19 @@ char bci_cm_jmp(uint8_t small_arg, machine_state_t state)
// else: do nothing
char bci_cm_cjmp(uint8_t small_arg, machine_state_t state)
{
uint16_t arg;
uint16_t arg = small_arg;
BCI_CORE_REGISTER_CHECK(small_arg);
#ifdef DEBUG
printf("MSG: cjmp: arg=0x%x (=0x%x)\n", arg, state->data_reg[arg]);
#endif
arg = state->data_reg[arg];
if(state->status_reg)
{
state->program_counter = arg;
state->status_reg = 0;
return 0;
}
state->status_reg = 0;
return machine_state_t_default_afterexec(state, 0);
}

View File

@ -50,7 +50,7 @@ machine_state_t machine_state_t_new( void * cache_function_data
int i;
for(i = 0; i < BCI_CORE_NUM_REG; i++)
{
state->data_reg[BCI_CORE_NUM_REG] = 0;
state->data_reg[i] = 0;
}
state->stack = NULL;
@ -95,6 +95,9 @@ char update_cache(machine_state_t state, uint16_t offset)
if(cached != state->cache_size)
{
#ifdef DEBUG
printf("ERROR: should have cached %zu words, but got %zu.\n", state->cache_size, cached);
#endif
return 1;
}
state->cache_offset = cache_offset;
@ -121,6 +124,9 @@ char machine_state_t_get_word(machine_state_t state, uint16_t offset, uint16_t *
{
if(update_cache(state, offset))
{
#ifdef DEBUG
printf("ERROR: failed to update cache\n");
#endif
state->error = 0b01000000 | 2;
return 0b01000000 | 2;
}
@ -144,6 +150,12 @@ char machine_state_t_exec_cycle(machine_state_t state)
return res;
}
#ifdef DEBUG
printf("MSG: opcode = 0x%x, small_arg = 0x%x\n"
, opcode & BCI_CORE_OPCODE_MASK
, opcode & BCI_CORE_SMALL_ARG_MASK);
#endif
bci_core_method_t method = dispatch_tree_t_dispatch(
state->method_dispatcher
, opcode & BCI_CORE_OPCODE_MASK);
@ -189,3 +201,17 @@ char machine_state_t_set_mem(machine_state_t state, uint16_t offset, uint16_t da
state->memory[offset] = data;
return 0;
}
char machine_state_t_exec(machine_state_t state)
{
char status = 0;
while((!status) && (!state->shutdown_reg))
{
status = machine_state_t_exec_cycle(state);
}
state->error = status;
return status;
}

View File

@ -88,6 +88,16 @@ void machine_state_t_del(machine_state_t state);
* */
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.