-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr35902_cpu.h
165 lines (144 loc) · 3.81 KB
/
lr35902_cpu.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#pragma once
#include <cstdint>
#include "memory.h"
#define CPU_FREQ 4194304
class lr35902_cpu
{
public:
lr35902_cpu(Memory& memory);
uint8_t update();
void run(uint32_t cycles);
private:
uint8_t nop(uint8_t);
using instructionHandler = decltype(&lr35902_cpu::nop);
static instructionHandler handlers[256];
static instructionHandler cb_handlers[256];
struct registers_t {
uint8_t A; // Accumulator
union {
uint16_t w;
uint8_t b[2];
} BC; // General purpose
union {
uint16_t w;
uint8_t b[2];
} DE; // General purpose
union {
uint16_t w;
uint8_t b[2];
} HL; // General purpose
union {
uint16_t w;
uint8_t b[2];
} SP; // Stack pointer
uint16_t PC; // Program counter
bool ZF; // Zero flag
bool N; // Add/Sub flag
bool H; // Half carry flag
bool CY; // Carry flag
uint8_t IR;
uint8_t divider;
} m_registers;
uint8_t m_stub;
std::reference_wrapper<uint8_t> m_8b_reg_map[8] = {
m_registers.BC.b[1], // 0 => B
m_registers.BC.b[0], // 1 => C
m_registers.DE.b[1], // 2 => D
m_registers.DE.b[0], // 3 => E
m_registers.HL.b[1], // 4 => H
m_registers.HL.b[0], // 5 => L
m_stub,
m_registers.A // 7 => A
};
bool m_halted = false;
bool m_stoped = false;
bool m_maskedInterrupts = false;
bool m_maskInterrupts = false;
bool m_unMaskInterrupts = false;
uint64_t m_clkCounter = 0;
uint64_t m_divRegisterCounter = 0;
uint32_t m_timerDivider = CPU_FREQ;
bool m_timerEnabled = false;
Memory& m_memory;
uint8_t fetchIR();
void processTimer(uint8_t executedCycles);
uint8_t handleInterrupts();
inline uint8_t getValForReg3b(uint8_t regnum);
inline uint16_t getValForReg2b(uint8_t regnum);
inline uint8_t getFlags8b();
uint8_t stop(uint8_t);
uint8_t jr_r8(uint8_t opcode);
uint8_t ld_d16(uint8_t opcode);
uint8_t ld_a(uint8_t opcode);
uint8_t inc_16(uint8_t opcode);
uint8_t dec_16(uint8_t opcode);
uint8_t inc_8(uint8_t opcode);
uint8_t inc_hl(uint8_t);
uint8_t dec_8(uint8_t opcode);
uint8_t dec_hl(uint8_t);
uint8_t ld_d8(uint8_t opcode);
uint8_t rlca(uint8_t);
uint8_t rla(uint8_t);
uint8_t rrca(uint8_t);
uint8_t rra(uint8_t);
uint8_t daa(uint8_t);
uint8_t scf(uint8_t);
uint8_t cpl(uint8_t);
uint8_t ccf(uint8_t);
uint8_t ld_a16_sp(uint8_t);
uint8_t add_hl(uint8_t opcode);
uint8_t ld_reg8(uint8_t opcode);
uint8_t halt(uint8_t);
uint8_t add_a(uint8_t opcode);
uint8_t sub(uint8_t opcode);
uint8_t adc(uint8_t opcode);
uint8_t sbc(uint8_t opcode);
uint8_t and_a(uint8_t opcode);
uint8_t xor_a(uint8_t opcode);
uint8_t or_a(uint8_t opcode);
uint8_t cp(uint8_t opcode);
uint8_t invalid(uint8_t opcode);
uint8_t ldh(uint8_t opcode);
uint8_t jp_cc_a16(uint8_t opcode);
uint8_t call_a16 (uint8_t opcode);
uint8_t rst(uint8_t opcode);
uint8_t di(uint8_t);
uint8_t ei(uint8_t);
uint8_t add_sp_r8(uint8_t);
uint8_t jp_hl(uint8_t);
uint8_t ldhl_spr8(uint8_t);
uint8_t ld_sp_hl(uint8_t);
uint8_t ld_a_a16(uint8_t opcode);
uint8_t ret_cc(uint8_t opcode);
uint8_t push(uint8_t opcode);
uint8_t pop(uint8_t opcode);
uint8_t reti(uint8_t);
uint8_t ret(uint8_t);
uint8_t jp_a16(uint8_t);
uint8_t cb(uint8_t);
uint8_t rlc(uint8_t opcode);
uint8_t rlcHL(uint8_t);
uint8_t rrc(uint8_t opcode);
uint8_t rrcHL(uint8_t);
uint8_t rl(uint8_t opcode);
uint8_t rlHL(uint8_t);
uint8_t rr(uint8_t opcode);
uint8_t rrHL(uint8_t);
uint8_t sla(uint8_t opcode);
uint8_t slaHL(uint8_t);
uint8_t sra(uint8_t opcode);
uint8_t sraHL(uint8_t);
uint8_t swap(uint8_t opcode);
uint8_t swapHL(uint8_t);
uint8_t srl(uint8_t opcode);
uint8_t srlHL(uint8_t);
uint8_t bit(uint8_t opcode);
uint8_t bitHL(uint8_t opcode);
uint8_t res(uint8_t opcode);
uint8_t resHL(uint8_t opcode);
uint8_t set(uint8_t opcode);
uint8_t setHL(uint8_t opcode);
void write(uint16_t address, uint8_t data);
uint8_t read(uint16_t address);
void debugDisplay();
};