3 #include "interpreter.h"
11 unsigned int negative:16;
16 int32_t regs[16] = {0};
17 uint32_t final_cycle_count = 0;
21 struct ReturnException
27 #define PARAMS CpuState *state, uint8_t *pc, Flags flags, uint32_t cycle_count
28 #define ARGS state, pc, flags, cycle_count
30 extern void (*dispatch_table[])(PARAMS);
32 void op_return(PARAMS)
35 state->final_cycle_count = cycle_count;
36 throw ReturnException();
42 uint8_t dest = reg>>4, src = reg & 0xf;
44 int32_t v = state->regs[dest];
45 v += state->regs[src];
47 flags.zero = (v == 0);
48 flags.negative = (v < 0);
49 state->regs[dest] = v;
52 uint8_t opcode = *pc++;
53 dispatch_table[opcode](ARGS);
59 uint8_t dest = reg>>4, src = reg & 0xf;
61 int32_t v = state->regs[dest];
62 v -= state->regs[src];
64 flags.zero = (v == 0);
65 flags.negative = (v < 0);
66 state->regs[dest] = v;
69 uint8_t opcode = *pc++;
70 dispatch_table[opcode](ARGS);
76 uint8_t dest = reg>>4, src = reg & 0xf;
78 state->regs[dest] = state->regs[src];
81 uint8_t opcode = *pc++;
82 dispatch_table[opcode](ARGS);
88 uint8_t dest = reg>>4;
90 int32_t imm = *(int32_t *)pc;
93 state->regs[dest] = imm;
96 uint8_t opcode = *pc++;
97 dispatch_table[opcode](ARGS);
106 uint8_t opcode = *pc++;
107 dispatch_table[opcode](ARGS);
119 uint8_t opcode = *pc++;
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};
146 uint8_t opcode = *pc++;
147 dispatch_table[opcode](ARGS);
151 } catch(ReturnException ex)
153 return std::make_pair(state->regs[X0], state->final_cycle_count);