3 #include "interpreter.h"
11 unsigned int negative:16;
16 int32_t regs[16] = {0};
17 uint32_t final_cycle_count = 0;
22 #define PARAMS CpuState *state, uint8_t *pc, Flags flags, uint32_t cycle_count, void *dispatch_table_void
23 #define ARGS state, pc, flags, cycle_count, dispatch_table_void
25 typedef void (*DispatchFun)(PARAMS);
27 void op_return(PARAMS)
30 state->final_cycle_count = cycle_count;
36 uint8_t dest = reg>>4, src = reg & 0xf;
38 int32_t v = state->regs[dest];
39 v += state->regs[src];
41 flags.zero = (v == 0);
42 flags.negative = (v < 0);
43 state->regs[dest] = v;
46 uint8_t opcode = *pc++;
47 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
48 dispatch_table[opcode](ARGS);
54 uint8_t dest = reg>>4, src = reg & 0xf;
56 int32_t v = state->regs[dest];
57 v -= state->regs[src];
59 flags.zero = (v == 0);
60 flags.negative = (v < 0);
61 state->regs[dest] = v;
64 uint8_t opcode = *pc++;
65 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
66 dispatch_table[opcode](ARGS);
72 uint8_t dest = reg>>4, src = reg & 0xf;
74 state->regs[dest] = state->regs[src];
77 uint8_t opcode = *pc++;
78 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
79 dispatch_table[opcode](ARGS);
85 uint8_t dest = reg>>4;
87 int32_t imm = *(int32_t *)pc;
90 state->regs[dest] = imm;
93 uint8_t opcode = *pc++;
94 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
95 dispatch_table[opcode](ARGS);
104 uint8_t opcode = *pc++;
105 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
106 dispatch_table[opcode](ARGS);
118 uint8_t opcode = *pc++;
119 DispatchFun *dispatch_table = (DispatchFun *)dispatch_table_void;
120 dispatch_table[opcode](ARGS);
123 void (*dispatch_table[])(PARAMS) =
134 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
136 CpuState local_state;
137 CpuState *state = &local_state;
138 uint8_t *pc = program;
139 state->regs[X0] = param;
140 uint32_t cycle_count = 0;
141 Flags flags = {0, 0};
142 void *dispatch_table_void = (void*)&dispatch_table[0];
145 uint8_t opcode = *pc++;
146 dispatch_table[opcode](ARGS);
148 return std::make_pair(state->regs[X0], state->final_cycle_count);