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;
18 CpuState global_state;
22 global_state.exit = true;
27 uint8_t reg = *global_state.pc++;
28 uint8_t dest = reg>>4, src = reg & 0xf;
30 int32_t v = global_state.regs[dest];
31 v += global_state.regs[src];
33 global_state.zero_flag = (v == 0);
34 global_state.negative_flag = (v < 0);
35 global_state.regs[dest] = v;
41 uint8_t reg = *global_state.pc++;
42 uint8_t dest = reg>>4, src = reg & 0xf;
44 int32_t v = global_state.regs[dest];
45 v -= global_state.regs[src];
47 global_state.zero_flag = (v == 0);
48 global_state.negative_flag = (v < 0);
49 global_state.regs[dest] = v;
54 uint8_t reg = *global_state.pc++;
55 uint8_t dest = reg>>4, src = reg & 0xf;
57 global_state.regs[dest] = global_state.regs[src];
63 uint8_t reg = *global_state.pc++;
64 uint8_t dest = reg>>4;
66 int32_t imm = *(int32_t *)global_state.pc;
69 global_state.regs[dest] = imm;
75 int8_t rel = *global_state.pc++;
76 global_state.pc += rel;
81 int8_t rel = *global_state.pc++;
82 if(!global_state.zero_flag)
84 global_state.pc += rel;
88 void (*dispatch_table[])() =
99 std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
101 global_state.pc = program;
102 global_state.regs[X0] = param;
103 global_state.cycle_count = 0;
104 global_state.exit = false;
106 while(!global_state.exit)
108 uint8_t opcode = *global_state.pc++;
109 global_state.cycle_count += cycle_table[opcode];
110 dispatch_table[opcode]();
113 return std::make_pair(global_state.regs[X0], global_state.cycle_count);