3 #include "interpreter.h"
11 unsigned int negative:16;
16 int32_t regs[16] = {0};
17 uint32_t cycle_count = 0;
29 #define PARAMS CpuState *state, uint8_t *pc, Flags flags
30 #define ARGS state, pc, flags
31 #define RETURN_VAL {pc, flags}
33 ReturnVal op_return(PARAMS)
35 state->cycle_count += 1;
40 ReturnVal op_add(PARAMS)
43 uint8_t dest = reg>>4, src = reg & 0xf;
45 int32_t v = state->regs[dest];
46 v += state->regs[src];
48 flags.zero = (v == 0);
49 flags.negative = (v < 0);
50 state->regs[dest] = v;
51 state->cycle_count += 2;
55 ReturnVal op_sub(PARAMS)
58 uint8_t dest = reg>>4, src = reg & 0xf;
60 int32_t v = state->regs[dest];
61 v -= state->regs[src];
63 flags.zero = (v == 0);
64 flags.negative = (v < 0);
65 state->regs[dest] = v;
66 state->cycle_count += 2;
70 ReturnVal op_mov(PARAMS)
73 uint8_t dest = reg>>4, src = reg & 0xf;
75 state->regs[dest] = state->regs[src];
76 state->cycle_count += 2;
81 ReturnVal op_movi(PARAMS)
84 uint8_t dest = reg>>4;
86 int32_t imm = *(int32_t *)pc;
89 state->regs[dest] = imm;
90 state->cycle_count += 6;
95 ReturnVal op_b(PARAMS)
99 state->cycle_count += 2;
103 ReturnVal op_bnz(PARAMS)
110 state->cycle_count += 2;
114 ReturnVal (*dispatch_table[])(PARAMS) =
125 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
127 CpuState local_state;
128 CpuState *state = &local_state;
129 uint8_t *pc = program;
130 state->regs[X0] = param;
131 state->cycle_count = 0;
133 Flags flags = {0, 0};
137 uint8_t opcode = *pc++;
138 ReturnVal ret = dispatch_table[opcode](ARGS);
143 return std::make_pair(state->regs[X0], state->cycle_count);