3 #include "interpreter.h"
5 namespace interpreter10
10 int32_t regs[16] = {0};
11 uint32_t final_cycle_count = 0;
12 bool flags_zero = false;
13 bool flags_negative = false;
18 #define PARAMS CpuState *state, uint8_t *pc, uint32_t cycle_count, void *dispatch_table_void
19 #define ARGS state, pc, cycle_count, dispatch_table_void
21 typedef void (*DispatchFun)(PARAMS);
23 void op_return(PARAMS)
26 state->final_cycle_count = cycle_count;
33 uint8_t dest = reg>>4, src = reg & 0xf;
35 int32_t v = state->regs[dest];
36 v += state->regs[src];
38 state->flags_zero = (v == 0);
39 state->flags_negative = (v < 0);
40 state->regs[dest] = v;
43 uint8_t opcode = *pc++;
44 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
45 dispatch_table[opcode](ARGS);
51 uint8_t dest = reg>>4, src = reg & 0xf;
53 int32_t v = state->regs[dest];
54 v -= state->regs[src];
56 state->flags_zero = (v == 0);
57 state->flags_negative = (v < 0);
58 state->regs[dest] = v;
61 uint8_t opcode = *pc++;
62 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
63 dispatch_table[opcode](ARGS);
69 uint8_t dest = reg>>4, src = reg & 0xf;
71 state->regs[dest] = state->regs[src];
74 uint8_t opcode = *pc++;
75 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
76 dispatch_table[opcode](ARGS);
82 uint8_t dest = reg>>4;
84 int32_t imm = *(int32_t *)pc;
87 state->regs[dest] = imm;
90 uint8_t opcode = *pc++;
91 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
92 dispatch_table[opcode](ARGS);
101 uint8_t opcode = *pc++;
102 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
103 dispatch_table[opcode](ARGS);
109 if(!state->flags_zero)
115 uint8_t opcode = *pc++;
116 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
117 dispatch_table[opcode](ARGS);
120 void (*dispatch_table[])(PARAMS) =
131 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
133 CpuState local_state;
134 CpuState *state = &local_state;
135 uint8_t *pc = program;
136 state->regs[X0] = param;
137 uint32_t cycle_count = 0;
138 void *dispatch_table_void = (void*)&dispatch_table[0];
140 uint8_t opcode = *pc++;
141 dispatch_table[opcode](ARGS);
143 return std::make_pair(state->regs[X0], state->final_cycle_count);