3 #include "interpreter.h"
10 int32_t regs[16] = {0};
11 bool zero_flag = false;
12 bool negative_flag = false;
13 uint8_t *pc = nullptr;
14 uint32_t cycle_count = 0;
17 CpuState global_state;
19 struct ReturnException
25 throw ReturnException();
30 uint8_t reg = *global_state.pc++;
31 uint8_t dest = reg>>4, src = reg & 0xf;
33 int32_t v = global_state.regs[dest];
34 v += global_state.regs[src];
36 global_state.zero_flag = (v == 0);
37 global_state.negative_flag = (v < 0);
38 global_state.regs[dest] = v;
44 uint8_t reg = *global_state.pc++;
45 uint8_t dest = reg>>4, src = reg & 0xf;
47 int32_t v = global_state.regs[dest];
48 v -= global_state.regs[src];
50 global_state.zero_flag = (v == 0);
51 global_state.negative_flag = (v < 0);
52 global_state.regs[dest] = v;
57 uint8_t reg = *global_state.pc++;
58 uint8_t dest = reg>>4, src = reg & 0xf;
60 global_state.regs[dest] = global_state.regs[src];
66 uint8_t reg = *global_state.pc++;
67 uint8_t dest = reg>>4;
69 int32_t imm = *(int32_t *)global_state.pc;
72 global_state.regs[dest] = imm;
78 int8_t rel = *global_state.pc++;
79 global_state.pc += rel;
84 int8_t rel = *global_state.pc++;
85 if(!global_state.zero_flag)
87 global_state.pc += rel;
91 void (*dispatch_table[])() =
102 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
104 global_state.pc = program;
105 global_state.regs[X0] = param;
106 global_state.cycle_count = 0;
111 uint8_t opcode = *global_state.pc++;
112 global_state.cycle_count += cycle_table[opcode];
113 dispatch_table[opcode]();
119 } catch(ReturnException ex)
121 return std::make_pair(global_state.regs[X0], global_state.cycle_count);