3 #include "interpreter.h"
10 int32_t regs[16] = {0};
11 uint32_t final_cycle_count = 0;
17 #define PARAMS CpuState *state, uint8_t *pc, bool flags_zero, bool flags_negative, uint32_t cycle_count, void *dispatch_table_void
18 #define ARGS state, pc, flags_zero, flags_negative, cycle_count, dispatch_table_void
20 typedef void (*DispatchFun)(PARAMS);
22 void op_return(PARAMS)
25 state->final_cycle_count = cycle_count;
32 uint8_t dest = reg>>4, src = reg & 0xf;
34 int32_t v = state->regs[dest];
35 v += state->regs[src];
37 flags_zero = (v == 0);
38 flags_negative = (v < 0);
39 state->regs[dest] = v;
42 uint8_t opcode = *pc++;
43 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
44 dispatch_table[opcode](ARGS);
50 uint8_t dest = reg>>4, src = reg & 0xf;
52 int32_t v = state->regs[dest];
53 v -= state->regs[src];
55 flags_zero = (v == 0);
56 flags_negative = (v < 0);
57 state->regs[dest] = v;
60 uint8_t opcode = *pc++;
61 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
62 dispatch_table[opcode](ARGS);
68 uint8_t dest = reg>>4, src = reg & 0xf;
70 state->regs[dest] = state->regs[src];
73 uint8_t opcode = *pc++;
74 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
75 dispatch_table[opcode](ARGS);
81 uint8_t dest = reg>>4;
83 int32_t imm = *(int32_t *)pc;
86 state->regs[dest] = imm;
89 uint8_t opcode = *pc++;
90 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
91 dispatch_table[opcode](ARGS);
100 uint8_t opcode = *pc++;
101 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
102 dispatch_table[opcode](ARGS);
114 uint8_t opcode = *pc++;
115 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
116 dispatch_table[opcode](ARGS);
119 void (*dispatch_table[])(PARAMS) =
130 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
132 CpuState local_state;
133 CpuState *state = &local_state;
134 uint8_t *pc = program;
135 state->regs[X0] = param;
136 uint32_t cycle_count = 0;
137 bool flags_zero = false;
138 bool flags_negative = false;
139 void *dispatch_table_void = (void*)&dispatch_table[0];
141 uint8_t opcode = *pc++;
142 dispatch_table[opcode](ARGS);
143 return std::make_pair(state->regs[X0], state->final_cycle_count);