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
23 #define ARGS state, pc, flags, cycle_count
25 extern void (*dispatch_table[])(PARAMS);
27 void op_return(PARAMS)
30 state->final_cycle_count = cycle_count;
37 uint8_t dest = reg>>4, src = reg & 0xf;
39 int32_t v = state->regs[dest];
40 v += state->regs[src];
42 flags.zero = (v == 0);
43 flags.negative = (v < 0);
44 state->regs[dest] = v;
47 uint8_t opcode = *pc++;
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 dispatch_table[opcode](ARGS);
71 uint8_t dest = reg>>4, src = reg & 0xf;
73 state->regs[dest] = state->regs[src];
76 uint8_t opcode = *pc++;
77 dispatch_table[opcode](ARGS);
83 uint8_t dest = reg>>4;
85 int32_t imm = *(int32_t *)pc;
88 state->regs[dest] = imm;
91 uint8_t opcode = *pc++;
92 dispatch_table[opcode](ARGS);
101 uint8_t opcode = *pc++;
102 dispatch_table[opcode](ARGS);
114 uint8_t opcode = *pc++;
115 dispatch_table[opcode](ARGS);
118 void (*dispatch_table[])(PARAMS) =
129 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
131 CpuState local_state;
132 CpuState *state = &local_state;
133 uint8_t *pc = program;
134 state->regs[X0] = param;
135 uint32_t cycle_count = 0;
136 Flags flags = {0, 0};
138 uint8_t opcode = *pc++;
139 dispatch_table[opcode](ARGS);
141 return std::make_pair(state->regs[X0], state->final_cycle_count);