BCI-base/interpreter/core_methods.c

368 lines
7.5 KiB
C
Raw Normal View History

2018-10-03 21:26:02 +00:00
#include "core_methods.h"
#define BCI_CORE_REGISTER_CHECK(var) if(var > BCI_CORE_NUM_REG) return 1
// #1 -> $0
char bci_cm_ldi(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
uint16_t arg;
char res;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
state->data_reg[small_arg] = arg;
return machine_state_t_default_afterexec(state, 1);
}
2018-10-09 09:43:00 +00:00
// @($1) -> $0
2018-10-03 21:26:02 +00:00
char bci_cm_ld(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
uint16_t arg;
char res;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
2018-10-09 09:43:00 +00:00
BCI_CORE_REGISTER_CHECK(arg);
arg = state->data_reg[arg];
2018-10-03 21:26:02 +00:00
res = machine_state_t_get_mem(state, arg, &arg);
if(res)
{
return res;
}
state->data_reg[small_arg] = arg;
return machine_state_t_default_afterexec(state, 1);
}
2018-10-09 09:43:00 +00:00
// $0 -> @($1)
2018-10-03 21:26:02 +00:00
char bci_cm_st(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
char res;
uint16_t arg;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
2018-10-09 09:43:00 +00:00
BCI_CORE_REGISTER_CHECK(arg);
arg = state->data_reg[arg];
2018-10-03 21:26:02 +00:00
res = machine_state_t_set_mem(state, arg, state->data_reg[small_arg]);
if(res)
{
return res;
}
return machine_state_t_default_afterexec(state, 1);
}
// $0 + 1 -> $0
char bci_cm_inc(uint8_t small_arg, machine_state_t state)
{
int32_t result;
BCI_CORE_REGISTER_CHECK(small_arg);
result = state->data_reg[small_arg] + 1;
state->data_reg[small_arg] = result & 0xffff;
state->status_reg = result >> 16;
return machine_state_t_default_afterexec(state, 0);
}
// $0 - 1 -> $0
char bci_cm_dec(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
int32_t result = state->data_reg[small_arg] - 1;
state->data_reg[small_arg] = result & 0xffff;
state->status_reg = result >> 16;
return machine_state_t_default_afterexec(state, 0);
}
// $0 + $1 -> $0
char bci_cm_add(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
uint16_t arg;
char res;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
BCI_CORE_REGISTER_CHECK(arg);
int32_t result = state->data_reg[small_arg] + state->data_reg[arg];
state->data_reg[small_arg] = result & 0xffff;
state->status_reg = result >> 16;
return machine_state_t_default_afterexec(state, 1);
}
// $0 - $1 -> $0
char bci_cm_sub(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
uint16_t arg;
char res;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
BCI_CORE_REGISTER_CHECK(arg);
int32_t result = state->data_reg[small_arg] - state->data_reg[arg];
state->data_reg[small_arg] = result & 0xffff;
state->status_reg = result >> 16;
return machine_state_t_default_afterexec(state, 1);
}
// $0 * $1 -> $0
char bci_cm_mul(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
uint16_t arg;
char res;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
BCI_CORE_REGISTER_CHECK(arg);
int32_t result = state->data_reg[small_arg] * state->data_reg[arg];
state->data_reg[small_arg] = result & 0xffff;
state->status_reg = result >> 16;
return machine_state_t_default_afterexec(state, 1);
}
// $0 / $1 -> $0
char bci_cm_div(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
uint16_t arg;
char res;
res = machine_state_t_get_word(state, state->program_counter + 1, &arg);
if(res)
{
return res;
}
BCI_CORE_REGISTER_CHECK(arg);
int32_t result = state->data_reg[small_arg] / state->data_reg[arg];
state->data_reg[small_arg] = result & 0xffff;
state->status_reg = result >> 16;
return machine_state_t_default_afterexec(state, 1);
}
// if($0 > 0): 1 -> $st_reg
// else: 0 -> $st_reg
char bci_cm_gt(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
if(state->data_reg[small_arg] > 0)
{
state->status_reg = 1;
}
else
{
state->status_reg = 0;
}
return machine_state_t_default_afterexec(state, 0);
}
// if($0 >= 0): 1 -> $st_reg
// else: 0 -> $st_reg
char bci_cm_ge(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
if(state->data_reg[small_arg] >= 0)
{
state->status_reg = 1;
}
else
{
state->status_reg = 0;
}
return machine_state_t_default_afterexec(state, 0);
}
// if($0 < 0): 1 -> $st_reg
// else: 0 -> $st_reg
char bci_cm_lt(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
if(state->data_reg[small_arg] < 0)
{
state->status_reg = 1;
}
else
{
state->status_reg = 0;
}
return machine_state_t_default_afterexec(state, 0);
}
// if($0 <= 0): 1 -> $st_reg
// else: 0 -> $st_reg
char bci_cm_le(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
if(state->data_reg[small_arg] <= 0)
{
state->status_reg = 1;
}
else
{
state->status_reg = 0;
}
return machine_state_t_default_afterexec(state, 0);
}
// if($0 == 0): 1 -> $st_reg
// else: 0 -> $st_reg
char bci_cm_eq(uint8_t small_arg, machine_state_t state)
{
BCI_CORE_REGISTER_CHECK(small_arg);
if(state->data_reg[small_arg] == 0)
{
state->status_reg = 1;
}
else
{
state->status_reg = 0;
}
return machine_state_t_default_afterexec(state, 0);
}
2018-10-09 09:43:00 +00:00
// $0 -> $pc; 0 -> $st_reg
2018-10-03 21:26:02 +00:00
char bci_cm_jmp(uint8_t small_arg, machine_state_t state)
{
2018-10-27 10:14:30 +00:00
BCI_CORE_REGISTER_CHECK(small_arg);
2018-10-09 09:43:00 +00:00
uint16_t arg = state->data_reg[arg];
2018-10-03 21:26:02 +00:00
2018-10-09 09:43:00 +00:00
state->status_reg = 0;
2018-10-03 21:26:02 +00:00
state->program_counter = arg;
return 0;
}
2018-10-09 09:43:00 +00:00
// if($st_reg): $0 -> $pc; 0 -> $st_reg
2018-10-03 21:26:02 +00:00
// else: do nothing
char bci_cm_cjmp(uint8_t small_arg, machine_state_t state)
{
uint16_t arg;
2018-10-27 10:14:30 +00:00
BCI_CORE_REGISTER_CHECK(small_arg);
2018-10-09 09:43:00 +00:00
arg = state->data_reg[arg];
2018-10-03 21:26:02 +00:00
if(state->status_reg)
{
state->program_counter = arg;
return 0;
}
2018-10-09 09:43:00 +00:00
state->status_reg = 0;
2018-10-03 21:26:02 +00:00
2018-10-09 09:43:00 +00:00
return machine_state_t_default_afterexec(state, 0);
2018-10-03 21:26:02 +00:00
}
// $pc -> @stack
2018-10-09 09:43:00 +00:00
// $0 -> $pc
2018-10-03 21:26:02 +00:00
char bci_cm_call(uint8_t small_arg, machine_state_t state)
{
uint16_t arg;
2018-10-27 10:14:30 +00:00
BCI_CORE_REGISTER_CHECK(small_arg);
2018-10-09 09:43:00 +00:00
arg = state->data_reg[arg];
2018-10-03 21:26:02 +00:00
2018-10-09 09:43:00 +00:00
char res = bci_stack_t_push(&(state->stack), state->program_counter);
2018-10-03 21:26:02 +00:00
if(res)
{
return res;
}
state->program_counter = arg;
return 0;
}
2018-10-09 09:43:00 +00:00
// if($st_reg): $pc -> @stack; $0 -> $pc; 0 -> $st_reg
2018-10-03 21:26:02 +00:00
// else: do nothing
char bci_cm_ccall(uint8_t small_arg, machine_state_t state)
{
uint16_t arg;
2018-10-27 10:14:30 +00:00
char res;
2018-10-09 09:43:00 +00:00
BCI_CORE_REGISTER_CHECK(small_arg);
arg = state->data_reg[small_arg];
2018-10-03 21:26:02 +00:00
if(state->status_reg)
{
state->program_counter = arg;
res = bci_stack_t_push(&(state->stack), state->program_counter);
if(res)
{
return res;
}
return 0;
}
2018-10-09 09:43:00 +00:00
state->status_reg = 0;
return machine_state_t_default_afterexec(state, 0);
2018-10-03 21:26:02 +00:00
}
// @stack -> $pc
char bci_cm_ret(uint8_t small_arg, machine_state_t state)
{
uint16_t result;
char res = bci_stack_t_pop(&(state->stack), &result);
if(res)
{
return res;
}
state->program_counter = result;
return 0;
}
// 0 -> $st_reg
char bci_cm_cl(uint8_t small_arg, machine_state_t state)
{
state->status_reg = 0;
return machine_state_t_default_afterexec(state, 0);
}
// 1 -> $su_reg
char bci_cm_stop(uint8_t small_arg, machine_state_t state)
{
2018-10-09 09:43:00 +00:00
state->shutdown_reg = 1;
2018-10-03 21:26:02 +00:00
return machine_state_t_default_afterexec(state, 0);
}
2018-10-09 09:43:00 +00:00
// if($st_reg): 0 -> $st_reg
// else: 1 -> $st_reg
char bci_cm_not(uint8_t small_arg, machine_state_t state)
{
if(state->status_reg)
{
state->status_reg = 0;
}
else
{
2018-10-27 10:14:30 +00:00
state->status_reg = 1;
2018-10-09 09:43:00 +00:00
}
return machine_state_t_default_afterexec(state, 0);
}