gameboy wormboy manboy gameworm gameman wormgame mangame manworm

Dependencies:   mbed SDFileSystem2

Committer:
dicarloj
Date:
Sun Jan 13 19:00:10 2019 +0000
Revision:
17:c9afe1a7b423
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dicarloj 17:c9afe1a7b423 1 // Implementation of OPCODES and cpu step function
dicarloj 17:c9afe1a7b423 2
dicarloj 17:c9afe1a7b423 3 #define NDEBUG
dicarloj 17:c9afe1a7b423 4 #include <assert.h>
dicarloj 17:c9afe1a7b423 5
dicarloj 17:c9afe1a7b423 6 #include "cpu.h"
dicarloj 17:c9afe1a7b423 7 #include "mem.h"
dicarloj 17:c9afe1a7b423 8
dicarloj 17:c9afe1a7b423 9 // cpu registers, halted state, and timers
dicarloj 17:c9afe1a7b423 10 CpuState globalState;
dicarloj 17:c9afe1a7b423 11
dicarloj 17:c9afe1a7b423 12 typedef void OpcodeHandler(u8 opcode);
dicarloj 17:c9afe1a7b423 13
dicarloj 17:c9afe1a7b423 14 // the GB has several invalid opcodes
dicarloj 17:c9afe1a7b423 15 void invHandler(u8 opcode) {
dicarloj 17:c9afe1a7b423 16 printf("got invalid opcode 0x%x\n", opcode);
dicarloj 17:c9afe1a7b423 17 assert(false);
dicarloj 17:c9afe1a7b423 18 }
dicarloj 17:c9afe1a7b423 19
dicarloj 17:c9afe1a7b423 20 ////////////////////////////////
dicarloj 17:c9afe1a7b423 21 // CB Opcode Helper Functions //
dicarloj 17:c9afe1a7b423 22 ////////////////////////////////
dicarloj 17:c9afe1a7b423 23
dicarloj 17:c9afe1a7b423 24 u8 rlcReg(u8 value, bool isA) {
dicarloj 17:c9afe1a7b423 25 u8 result = value;
dicarloj 17:c9afe1a7b423 26 clearAllFlags();
dicarloj 17:c9afe1a7b423 27 if((result & 0x80) != 0) {
dicarloj 17:c9afe1a7b423 28 setCarryFlag();
dicarloj 17:c9afe1a7b423 29 result <<= 1;
dicarloj 17:c9afe1a7b423 30 result |= 0x1;
dicarloj 17:c9afe1a7b423 31 } else {
dicarloj 17:c9afe1a7b423 32 result <<= 1;
dicarloj 17:c9afe1a7b423 33 }
dicarloj 17:c9afe1a7b423 34 if(!isA) {
dicarloj 17:c9afe1a7b423 35 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 36 setZeroFlag();
dicarloj 17:c9afe1a7b423 37 }
dicarloj 17:c9afe1a7b423 38 }
dicarloj 17:c9afe1a7b423 39 return result;
dicarloj 17:c9afe1a7b423 40 }
dicarloj 17:c9afe1a7b423 41
dicarloj 17:c9afe1a7b423 42 u8 rlReg(u8 value, bool isA) {
dicarloj 17:c9afe1a7b423 43 u8 carry = getCarryFlag() ? (u8)1 : (u8)0;
dicarloj 17:c9afe1a7b423 44 u8 result = value;
dicarloj 17:c9afe1a7b423 45 clearAllFlags();
dicarloj 17:c9afe1a7b423 46 if((result & 0x80) != 0) {
dicarloj 17:c9afe1a7b423 47 setCarryFlag();
dicarloj 17:c9afe1a7b423 48 }
dicarloj 17:c9afe1a7b423 49 result <<= 1;
dicarloj 17:c9afe1a7b423 50 result |= carry;
dicarloj 17:c9afe1a7b423 51 if(!isA) {
dicarloj 17:c9afe1a7b423 52 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 53 setZeroFlag();
dicarloj 17:c9afe1a7b423 54 }
dicarloj 17:c9afe1a7b423 55 }
dicarloj 17:c9afe1a7b423 56 return result;
dicarloj 17:c9afe1a7b423 57 }
dicarloj 17:c9afe1a7b423 58
dicarloj 17:c9afe1a7b423 59 u8 rrc(u8 value, bool isA) {
dicarloj 17:c9afe1a7b423 60 u8 result = value;
dicarloj 17:c9afe1a7b423 61 clearAllFlags();
dicarloj 17:c9afe1a7b423 62 if((result & 1) != 0) {
dicarloj 17:c9afe1a7b423 63 setCarryFlag();
dicarloj 17:c9afe1a7b423 64 result >>= 1;
dicarloj 17:c9afe1a7b423 65 result |= 0x80;
dicarloj 17:c9afe1a7b423 66 } else {
dicarloj 17:c9afe1a7b423 67 result >>= 1;
dicarloj 17:c9afe1a7b423 68 }
dicarloj 17:c9afe1a7b423 69 if(!isA) {
dicarloj 17:c9afe1a7b423 70 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 71 setZeroFlag();
dicarloj 17:c9afe1a7b423 72 }
dicarloj 17:c9afe1a7b423 73 }
dicarloj 17:c9afe1a7b423 74 return result;
dicarloj 17:c9afe1a7b423 75 }
dicarloj 17:c9afe1a7b423 76
dicarloj 17:c9afe1a7b423 77 u8 rr(u8 value, bool isA) {
dicarloj 17:c9afe1a7b423 78 u8 carry = getCarryFlag() ? (u8)0x80 : (u8)0x00;
dicarloj 17:c9afe1a7b423 79 u8 result = value;
dicarloj 17:c9afe1a7b423 80 clearAllFlags();
dicarloj 17:c9afe1a7b423 81 if((result & 1) != 0) {
dicarloj 17:c9afe1a7b423 82 setCarryFlag();
dicarloj 17:c9afe1a7b423 83 }
dicarloj 17:c9afe1a7b423 84 result >>= 1;
dicarloj 17:c9afe1a7b423 85 result |= carry;
dicarloj 17:c9afe1a7b423 86 if(!isA) {
dicarloj 17:c9afe1a7b423 87 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 88 setZeroFlag();
dicarloj 17:c9afe1a7b423 89 }
dicarloj 17:c9afe1a7b423 90 }
dicarloj 17:c9afe1a7b423 91 return result;
dicarloj 17:c9afe1a7b423 92 }
dicarloj 17:c9afe1a7b423 93
dicarloj 17:c9afe1a7b423 94 u8 srl(u8 value) {
dicarloj 17:c9afe1a7b423 95 u8 result = value;
dicarloj 17:c9afe1a7b423 96 clearAllFlags();
dicarloj 17:c9afe1a7b423 97 if(result & 1) {
dicarloj 17:c9afe1a7b423 98 setCarryFlag();
dicarloj 17:c9afe1a7b423 99 }
dicarloj 17:c9afe1a7b423 100 result >>= 1;
dicarloj 17:c9afe1a7b423 101 if(result == 0) {
dicarloj 17:c9afe1a7b423 102 setZeroFlag();
dicarloj 17:c9afe1a7b423 103 }
dicarloj 17:c9afe1a7b423 104 return result;
dicarloj 17:c9afe1a7b423 105 }
dicarloj 17:c9afe1a7b423 106
dicarloj 17:c9afe1a7b423 107 u8 sla(u8 value) {
dicarloj 17:c9afe1a7b423 108 clearAllFlags();
dicarloj 17:c9afe1a7b423 109 if(value & 0x80) {
dicarloj 17:c9afe1a7b423 110 setCarryFlag();
dicarloj 17:c9afe1a7b423 111 }
dicarloj 17:c9afe1a7b423 112 u8 result = value << 1;
dicarloj 17:c9afe1a7b423 113 if(result == 0) {
dicarloj 17:c9afe1a7b423 114 setZeroFlag();
dicarloj 17:c9afe1a7b423 115 }
dicarloj 17:c9afe1a7b423 116 return result;
dicarloj 17:c9afe1a7b423 117 }
dicarloj 17:c9afe1a7b423 118
dicarloj 17:c9afe1a7b423 119 u8 sra(u8 value) {
dicarloj 17:c9afe1a7b423 120 u8 result = value;
dicarloj 17:c9afe1a7b423 121 clearAllFlags();
dicarloj 17:c9afe1a7b423 122 if(result & 1) {
dicarloj 17:c9afe1a7b423 123 setCarryFlag();
dicarloj 17:c9afe1a7b423 124 }
dicarloj 17:c9afe1a7b423 125 if((result & 0x80)) {
dicarloj 17:c9afe1a7b423 126 result >>= 1;
dicarloj 17:c9afe1a7b423 127 result |= 0x80;
dicarloj 17:c9afe1a7b423 128 } else {
dicarloj 17:c9afe1a7b423 129 result >>= 1;
dicarloj 17:c9afe1a7b423 130 }
dicarloj 17:c9afe1a7b423 131 if(result == 0) {
dicarloj 17:c9afe1a7b423 132 setZeroFlag();
dicarloj 17:c9afe1a7b423 133 }
dicarloj 17:c9afe1a7b423 134 return result;
dicarloj 17:c9afe1a7b423 135 }
dicarloj 17:c9afe1a7b423 136
dicarloj 17:c9afe1a7b423 137 u8 swapRegister(u8 value) {
dicarloj 17:c9afe1a7b423 138 u8 low = value & 0xf;
dicarloj 17:c9afe1a7b423 139 u8 hi = (value >> 4) & 0xf;
dicarloj 17:c9afe1a7b423 140 u8 result = (low << 4) + hi;
dicarloj 17:c9afe1a7b423 141 clearAllFlags();
dicarloj 17:c9afe1a7b423 142 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 143 setZeroFlag();
dicarloj 17:c9afe1a7b423 144 }
dicarloj 17:c9afe1a7b423 145 return result;
dicarloj 17:c9afe1a7b423 146 }
dicarloj 17:c9afe1a7b423 147
dicarloj 17:c9afe1a7b423 148
dicarloj 17:c9afe1a7b423 149 ////////////////////////////////
dicarloj 17:c9afe1a7b423 150 // CB Opcodes //
dicarloj 17:c9afe1a7b423 151 ////////////////////////////////
dicarloj 17:c9afe1a7b423 152
dicarloj 17:c9afe1a7b423 153 void SLA_A(u8 opcode) { // 0x27
dicarloj 17:c9afe1a7b423 154 globalState.pc++;
dicarloj 17:c9afe1a7b423 155 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 156 globalState.a = sla(globalState.a);
dicarloj 17:c9afe1a7b423 157 }
dicarloj 17:c9afe1a7b423 158
dicarloj 17:c9afe1a7b423 159 void SLA_B(u8 opcode) { // 0x20
dicarloj 17:c9afe1a7b423 160 globalState.pc++;
dicarloj 17:c9afe1a7b423 161 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 162 globalState.bc.hi = sla(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 163 }
dicarloj 17:c9afe1a7b423 164
dicarloj 17:c9afe1a7b423 165 void SLA_C(u8 opcode) { // 0x21
dicarloj 17:c9afe1a7b423 166 globalState.pc++;
dicarloj 17:c9afe1a7b423 167 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 168 globalState.bc.lo = sla(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 169 }
dicarloj 17:c9afe1a7b423 170
dicarloj 17:c9afe1a7b423 171 void SLA_D(u8 opcode) { // 0x22
dicarloj 17:c9afe1a7b423 172 globalState.pc++;
dicarloj 17:c9afe1a7b423 173 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 174 globalState.de.hi = sla(globalState.de.hi);
dicarloj 17:c9afe1a7b423 175 }
dicarloj 17:c9afe1a7b423 176
dicarloj 17:c9afe1a7b423 177 void SLA_E(u8 opcode) { // 0x23
dicarloj 17:c9afe1a7b423 178 globalState.pc++;
dicarloj 17:c9afe1a7b423 179 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 180 globalState.de.lo = sla(globalState.de.lo);
dicarloj 17:c9afe1a7b423 181 }
dicarloj 17:c9afe1a7b423 182
dicarloj 17:c9afe1a7b423 183 void SLA_H(u8 opcode) { // 0x24
dicarloj 17:c9afe1a7b423 184 globalState.pc++;
dicarloj 17:c9afe1a7b423 185 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 186 globalState.hl.hi = sla(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 187 }
dicarloj 17:c9afe1a7b423 188
dicarloj 17:c9afe1a7b423 189 void SLA_L(u8 opcode) { // 0x25
dicarloj 17:c9afe1a7b423 190 globalState.pc++;
dicarloj 17:c9afe1a7b423 191 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 192 globalState.hl.lo = sla(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 193 }
dicarloj 17:c9afe1a7b423 194
dicarloj 17:c9afe1a7b423 195 void SLA_DHL(u8 opcode) { // 0x26
dicarloj 17:c9afe1a7b423 196 globalState.pc++;
dicarloj 17:c9afe1a7b423 197 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 198 writeByte(sla(readByte(globalState.hl.v)), globalState.hl.v);
dicarloj 17:c9afe1a7b423 199 }
dicarloj 17:c9afe1a7b423 200
dicarloj 17:c9afe1a7b423 201 void SRA_A(u8 opcode) { // 0x2f
dicarloj 17:c9afe1a7b423 202 globalState.pc++;
dicarloj 17:c9afe1a7b423 203 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 204 globalState.a = sra(globalState.a);
dicarloj 17:c9afe1a7b423 205 }
dicarloj 17:c9afe1a7b423 206
dicarloj 17:c9afe1a7b423 207 void SRA_B(u8 opcode) { // 0x28
dicarloj 17:c9afe1a7b423 208 globalState.pc++;
dicarloj 17:c9afe1a7b423 209 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 210 globalState.bc.hi = sra(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 211 }
dicarloj 17:c9afe1a7b423 212
dicarloj 17:c9afe1a7b423 213 void SRA_C(u8 opcode) { // 0x29
dicarloj 17:c9afe1a7b423 214 globalState.pc++;
dicarloj 17:c9afe1a7b423 215 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 216 globalState.bc.lo = sra(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 217 }
dicarloj 17:c9afe1a7b423 218
dicarloj 17:c9afe1a7b423 219 void SRA_D(u8 opcode) { // 0x2a
dicarloj 17:c9afe1a7b423 220 globalState.pc++;
dicarloj 17:c9afe1a7b423 221 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 222 globalState.de.hi = sra(globalState.de.hi);
dicarloj 17:c9afe1a7b423 223 }
dicarloj 17:c9afe1a7b423 224
dicarloj 17:c9afe1a7b423 225 void SRA_E(u8 opcode) { // 0x2b
dicarloj 17:c9afe1a7b423 226 globalState.pc++;
dicarloj 17:c9afe1a7b423 227 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 228 globalState.de.lo = sra(globalState.de.lo);
dicarloj 17:c9afe1a7b423 229 }
dicarloj 17:c9afe1a7b423 230
dicarloj 17:c9afe1a7b423 231 void SRA_H(u8 opcode) { // 0x2c
dicarloj 17:c9afe1a7b423 232 globalState.pc++;
dicarloj 17:c9afe1a7b423 233 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 234 globalState.hl.hi = sra(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 235 }
dicarloj 17:c9afe1a7b423 236
dicarloj 17:c9afe1a7b423 237 void SRA_L(u8 opcode) { // 0x2d
dicarloj 17:c9afe1a7b423 238 globalState.pc++;
dicarloj 17:c9afe1a7b423 239 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 240 globalState.hl.lo = sra(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 241 }
dicarloj 17:c9afe1a7b423 242
dicarloj 17:c9afe1a7b423 243 void SRA_DHL(u8 opcode) { // 0x2e
dicarloj 17:c9afe1a7b423 244 globalState.pc++;
dicarloj 17:c9afe1a7b423 245 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 246 writeByte(sra(readByte(globalState.hl.v)), globalState.hl.v);
dicarloj 17:c9afe1a7b423 247 }
dicarloj 17:c9afe1a7b423 248
dicarloj 17:c9afe1a7b423 249
dicarloj 17:c9afe1a7b423 250 void SRL_A(u8 opcode) { // 0x3f
dicarloj 17:c9afe1a7b423 251 globalState.pc++;
dicarloj 17:c9afe1a7b423 252 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 253 globalState.a = srl(globalState.a);
dicarloj 17:c9afe1a7b423 254 }
dicarloj 17:c9afe1a7b423 255
dicarloj 17:c9afe1a7b423 256 void SRL_B(u8 opcode) { // 0x38
dicarloj 17:c9afe1a7b423 257 globalState.pc++;
dicarloj 17:c9afe1a7b423 258 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 259 globalState.bc.hi = srl(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 260 }
dicarloj 17:c9afe1a7b423 261
dicarloj 17:c9afe1a7b423 262 void SRL_C(u8 opcode) { // 0x39
dicarloj 17:c9afe1a7b423 263 globalState.pc++;
dicarloj 17:c9afe1a7b423 264 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 265 globalState.bc.lo = srl(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 266 }
dicarloj 17:c9afe1a7b423 267
dicarloj 17:c9afe1a7b423 268 void SRL_D(u8 opcode) { // 0x3a
dicarloj 17:c9afe1a7b423 269 globalState.pc++;
dicarloj 17:c9afe1a7b423 270 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 271 globalState.de.hi = srl(globalState.de.hi);
dicarloj 17:c9afe1a7b423 272 }
dicarloj 17:c9afe1a7b423 273
dicarloj 17:c9afe1a7b423 274 void SRL_E(u8 opcode) { // 0x3b
dicarloj 17:c9afe1a7b423 275 globalState.pc++;
dicarloj 17:c9afe1a7b423 276 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 277 globalState.de.lo = srl(globalState.de.lo);
dicarloj 17:c9afe1a7b423 278 }
dicarloj 17:c9afe1a7b423 279
dicarloj 17:c9afe1a7b423 280 void SRL_H(u8 opcode) { // 0x3c
dicarloj 17:c9afe1a7b423 281 globalState.pc++;
dicarloj 17:c9afe1a7b423 282 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 283 globalState.hl.hi = srl(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 284 }
dicarloj 17:c9afe1a7b423 285
dicarloj 17:c9afe1a7b423 286 void SRL_L(u8 opcode) { // 0x3d
dicarloj 17:c9afe1a7b423 287 globalState.pc++;
dicarloj 17:c9afe1a7b423 288 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 289 globalState.hl.lo = srl(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 290 }
dicarloj 17:c9afe1a7b423 291
dicarloj 17:c9afe1a7b423 292 void SRL_DHL(u8 opcode) { // 0x3e
dicarloj 17:c9afe1a7b423 293 globalState.pc++;
dicarloj 17:c9afe1a7b423 294 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 295 writeByte(srl(readByte(globalState.hl.v)), globalState.hl.v);
dicarloj 17:c9afe1a7b423 296 }
dicarloj 17:c9afe1a7b423 297
dicarloj 17:c9afe1a7b423 298 void RR_A(u8 opcode) { // 0x1f
dicarloj 17:c9afe1a7b423 299 globalState.pc++;
dicarloj 17:c9afe1a7b423 300 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 301 globalState.a = rr(globalState.a, false);
dicarloj 17:c9afe1a7b423 302 }
dicarloj 17:c9afe1a7b423 303
dicarloj 17:c9afe1a7b423 304 void RR_B(u8 opcode) { // 0x18
dicarloj 17:c9afe1a7b423 305 globalState.pc++;
dicarloj 17:c9afe1a7b423 306 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 307 globalState.bc.hi = rr(globalState.bc.hi, false);
dicarloj 17:c9afe1a7b423 308 }
dicarloj 17:c9afe1a7b423 309
dicarloj 17:c9afe1a7b423 310 void RR_C(u8 opcode) { // 0x19
dicarloj 17:c9afe1a7b423 311 globalState.pc++;
dicarloj 17:c9afe1a7b423 312 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 313 globalState.bc.lo = rr(globalState.bc.lo, false);
dicarloj 17:c9afe1a7b423 314 }
dicarloj 17:c9afe1a7b423 315
dicarloj 17:c9afe1a7b423 316 void RR_D(u8 opcode) { // 0x1a
dicarloj 17:c9afe1a7b423 317 globalState.pc++;
dicarloj 17:c9afe1a7b423 318 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 319 globalState.de.hi = rr(globalState.de.hi, false);
dicarloj 17:c9afe1a7b423 320 }
dicarloj 17:c9afe1a7b423 321
dicarloj 17:c9afe1a7b423 322 void RR_E(u8 opcode) { // 0x1b
dicarloj 17:c9afe1a7b423 323 globalState.pc++;
dicarloj 17:c9afe1a7b423 324 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 325 globalState.de.lo = rr(globalState.de.lo, false);
dicarloj 17:c9afe1a7b423 326 }
dicarloj 17:c9afe1a7b423 327
dicarloj 17:c9afe1a7b423 328 void RR_H(u8 opcode) { // 0x1c
dicarloj 17:c9afe1a7b423 329 globalState.pc++;
dicarloj 17:c9afe1a7b423 330 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 331 globalState.hl.hi = rr(globalState.hl.hi, false);
dicarloj 17:c9afe1a7b423 332 }
dicarloj 17:c9afe1a7b423 333
dicarloj 17:c9afe1a7b423 334 void RR_L(u8 opcode) { // 0x1d
dicarloj 17:c9afe1a7b423 335 globalState.pc++;
dicarloj 17:c9afe1a7b423 336 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 337 globalState.hl.lo = rr(globalState.hl.lo, false);
dicarloj 17:c9afe1a7b423 338 }
dicarloj 17:c9afe1a7b423 339
dicarloj 17:c9afe1a7b423 340 void RR_DHL(u8 opcode) { // 0x1e
dicarloj 17:c9afe1a7b423 341 globalState.pc++;
dicarloj 17:c9afe1a7b423 342 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 343 writeByte(rr(readByte(globalState.hl.v), false), globalState.hl.v);
dicarloj 17:c9afe1a7b423 344 }
dicarloj 17:c9afe1a7b423 345
dicarloj 17:c9afe1a7b423 346 void RL_A(u8 opcode) { // 0x17
dicarloj 17:c9afe1a7b423 347 globalState.pc++;
dicarloj 17:c9afe1a7b423 348 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 349 globalState.a = rlReg(globalState.a, false);
dicarloj 17:c9afe1a7b423 350 }
dicarloj 17:c9afe1a7b423 351
dicarloj 17:c9afe1a7b423 352 void RL_B(u8 opcode) { // 0x10
dicarloj 17:c9afe1a7b423 353 globalState.pc++;
dicarloj 17:c9afe1a7b423 354 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 355 globalState.bc.hi = rlReg(globalState.bc.hi, false);
dicarloj 17:c9afe1a7b423 356 }
dicarloj 17:c9afe1a7b423 357
dicarloj 17:c9afe1a7b423 358 void RL_C(u8 opcode) { // 0x11
dicarloj 17:c9afe1a7b423 359 globalState.pc++;
dicarloj 17:c9afe1a7b423 360 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 361 globalState.bc.lo = rlReg(globalState.bc.lo, false);
dicarloj 17:c9afe1a7b423 362 }
dicarloj 17:c9afe1a7b423 363
dicarloj 17:c9afe1a7b423 364 void RL_D(u8 opcode) { // 0x12
dicarloj 17:c9afe1a7b423 365 globalState.pc++;
dicarloj 17:c9afe1a7b423 366 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 367 globalState.de.hi = rlReg(globalState.de.hi, false);
dicarloj 17:c9afe1a7b423 368 }
dicarloj 17:c9afe1a7b423 369
dicarloj 17:c9afe1a7b423 370 void RL_E(u8 opcode) { // 0x13
dicarloj 17:c9afe1a7b423 371 globalState.pc++;
dicarloj 17:c9afe1a7b423 372 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 373 globalState.de.lo = rlReg(globalState.de.lo, false);
dicarloj 17:c9afe1a7b423 374 }
dicarloj 17:c9afe1a7b423 375
dicarloj 17:c9afe1a7b423 376 void RL_H(u8 opcode) { // 0x14
dicarloj 17:c9afe1a7b423 377 globalState.pc++;
dicarloj 17:c9afe1a7b423 378 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 379 globalState.hl.hi = rlReg(globalState.hl.hi, false);
dicarloj 17:c9afe1a7b423 380 }
dicarloj 17:c9afe1a7b423 381
dicarloj 17:c9afe1a7b423 382 void RL_L(u8 opcode) { // 0x15
dicarloj 17:c9afe1a7b423 383 globalState.pc++;
dicarloj 17:c9afe1a7b423 384 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 385 globalState.hl.lo = rlReg(globalState.hl.lo, false);
dicarloj 17:c9afe1a7b423 386 }
dicarloj 17:c9afe1a7b423 387
dicarloj 17:c9afe1a7b423 388 void RL_DHL(u8 opcode) { // 0x16
dicarloj 17:c9afe1a7b423 389 globalState.pc++;
dicarloj 17:c9afe1a7b423 390 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 391 writeByte(rlReg(readByte(globalState.hl.v), false), globalState.hl.v);
dicarloj 17:c9afe1a7b423 392 }
dicarloj 17:c9afe1a7b423 393
dicarloj 17:c9afe1a7b423 394 void SWAP_A(u8 opcode) { // 37
dicarloj 17:c9afe1a7b423 395 globalState.pc++;
dicarloj 17:c9afe1a7b423 396 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 397 globalState.a = swapRegister(globalState.a);
dicarloj 17:c9afe1a7b423 398 }
dicarloj 17:c9afe1a7b423 399
dicarloj 17:c9afe1a7b423 400 void SWAP_B(u8 opcode) { // 30
dicarloj 17:c9afe1a7b423 401 globalState.pc++;
dicarloj 17:c9afe1a7b423 402 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 403 globalState.bc.hi = swapRegister(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 404 }
dicarloj 17:c9afe1a7b423 405
dicarloj 17:c9afe1a7b423 406 void SWAP_C(u8 opcode) { // 31
dicarloj 17:c9afe1a7b423 407 globalState.pc++;
dicarloj 17:c9afe1a7b423 408 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 409 globalState.bc.lo = swapRegister(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 410 }
dicarloj 17:c9afe1a7b423 411
dicarloj 17:c9afe1a7b423 412 void SWAP_D(u8 opcode) { // 32
dicarloj 17:c9afe1a7b423 413 globalState.pc++;
dicarloj 17:c9afe1a7b423 414 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 415 globalState.de.hi = swapRegister(globalState.de.hi);
dicarloj 17:c9afe1a7b423 416 }
dicarloj 17:c9afe1a7b423 417
dicarloj 17:c9afe1a7b423 418 void SWAP_E(u8 opcode) { // 33
dicarloj 17:c9afe1a7b423 419 globalState.pc++;
dicarloj 17:c9afe1a7b423 420 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 421 globalState.de.lo = swapRegister(globalState.de.lo);
dicarloj 17:c9afe1a7b423 422 }
dicarloj 17:c9afe1a7b423 423
dicarloj 17:c9afe1a7b423 424 void SWAP_H(u8 opcode) { // 34
dicarloj 17:c9afe1a7b423 425 globalState.pc++;
dicarloj 17:c9afe1a7b423 426 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 427 globalState.hl.hi = swapRegister(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 428 }
dicarloj 17:c9afe1a7b423 429
dicarloj 17:c9afe1a7b423 430 void SWAP_L(u8 opcode) { // 35
dicarloj 17:c9afe1a7b423 431 globalState.pc++;
dicarloj 17:c9afe1a7b423 432 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 433 globalState.hl.lo = swapRegister(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 434 }
dicarloj 17:c9afe1a7b423 435
dicarloj 17:c9afe1a7b423 436 void SWAP_DHL(u8 opcode) { // 36
dicarloj 17:c9afe1a7b423 437 globalState.pc++;
dicarloj 17:c9afe1a7b423 438 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 439 writeByte(swapRegister(readByte(globalState.hl.v)), globalState.hl.v);
dicarloj 17:c9afe1a7b423 440 }
dicarloj 17:c9afe1a7b423 441
dicarloj 17:c9afe1a7b423 442 void RLC_A(u8 opcode) { // 07
dicarloj 17:c9afe1a7b423 443 globalState.pc++;
dicarloj 17:c9afe1a7b423 444 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 445 globalState.a = rlcReg(globalState.a, false);
dicarloj 17:c9afe1a7b423 446 }
dicarloj 17:c9afe1a7b423 447
dicarloj 17:c9afe1a7b423 448 void RLC_B(u8 opcode) { // 00
dicarloj 17:c9afe1a7b423 449 globalState.pc++;
dicarloj 17:c9afe1a7b423 450 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 451 globalState.bc.hi = rlcReg(globalState.bc.hi, false);
dicarloj 17:c9afe1a7b423 452 }
dicarloj 17:c9afe1a7b423 453
dicarloj 17:c9afe1a7b423 454 void RLC_C(u8 opcode) { // 01
dicarloj 17:c9afe1a7b423 455 globalState.pc++;
dicarloj 17:c9afe1a7b423 456 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 457 globalState.bc.lo = rlcReg(globalState.bc.lo, false);
dicarloj 17:c9afe1a7b423 458 }
dicarloj 17:c9afe1a7b423 459
dicarloj 17:c9afe1a7b423 460 void RLC_D(u8 opcode) { // 02
dicarloj 17:c9afe1a7b423 461 globalState.pc++;
dicarloj 17:c9afe1a7b423 462 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 463 globalState.de.hi = rlcReg(globalState.de.hi, false);
dicarloj 17:c9afe1a7b423 464 }
dicarloj 17:c9afe1a7b423 465
dicarloj 17:c9afe1a7b423 466 void RLC_E(u8 opcode) { // 03
dicarloj 17:c9afe1a7b423 467 globalState.pc++;
dicarloj 17:c9afe1a7b423 468 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 469 globalState.de.lo = rlcReg(globalState.de.lo, false);
dicarloj 17:c9afe1a7b423 470 }
dicarloj 17:c9afe1a7b423 471
dicarloj 17:c9afe1a7b423 472 void RLC_H(u8 opcode) { // 04
dicarloj 17:c9afe1a7b423 473 globalState.pc++;
dicarloj 17:c9afe1a7b423 474 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 475 globalState.hl.hi = rlcReg(globalState.hl.hi, false);
dicarloj 17:c9afe1a7b423 476 }
dicarloj 17:c9afe1a7b423 477
dicarloj 17:c9afe1a7b423 478 void RLC_L(u8 opcode) { // 05
dicarloj 17:c9afe1a7b423 479 globalState.pc++;
dicarloj 17:c9afe1a7b423 480 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 481 globalState.hl.lo = rlcReg(globalState.hl.lo, false);
dicarloj 17:c9afe1a7b423 482 }
dicarloj 17:c9afe1a7b423 483
dicarloj 17:c9afe1a7b423 484 void RLC_DHL(u8 opcode) { // 06
dicarloj 17:c9afe1a7b423 485 globalState.pc++;
dicarloj 17:c9afe1a7b423 486 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 487 u8 result = rlcReg(readByte(globalState.hl.v), false);
dicarloj 17:c9afe1a7b423 488 writeByte(result, globalState.hl.v);
dicarloj 17:c9afe1a7b423 489 }
dicarloj 17:c9afe1a7b423 490
dicarloj 17:c9afe1a7b423 491
dicarloj 17:c9afe1a7b423 492 void RRC_A(u8 opcode) { // 0f
dicarloj 17:c9afe1a7b423 493 globalState.pc++;
dicarloj 17:c9afe1a7b423 494 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 495 globalState.a = rrc(globalState.a, false);
dicarloj 17:c9afe1a7b423 496 }
dicarloj 17:c9afe1a7b423 497
dicarloj 17:c9afe1a7b423 498 void RRC_B(u8 opcode) { // 08
dicarloj 17:c9afe1a7b423 499 globalState.pc++;
dicarloj 17:c9afe1a7b423 500 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 501 globalState.bc.hi = rrc(globalState.bc.hi, false);
dicarloj 17:c9afe1a7b423 502 }
dicarloj 17:c9afe1a7b423 503
dicarloj 17:c9afe1a7b423 504 void RRC_C(u8 opcode) { // 09
dicarloj 17:c9afe1a7b423 505 globalState.pc++;
dicarloj 17:c9afe1a7b423 506 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 507 globalState.bc.lo = rrc(globalState.bc.lo, false);
dicarloj 17:c9afe1a7b423 508 }
dicarloj 17:c9afe1a7b423 509
dicarloj 17:c9afe1a7b423 510 void RRC_D(u8 opcode) { // 0a
dicarloj 17:c9afe1a7b423 511 globalState.pc++;
dicarloj 17:c9afe1a7b423 512 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 513 globalState.de.hi = rrc(globalState.de.hi, false);
dicarloj 17:c9afe1a7b423 514 }
dicarloj 17:c9afe1a7b423 515
dicarloj 17:c9afe1a7b423 516 void RRC_E(u8 opcode) { // 0b
dicarloj 17:c9afe1a7b423 517 globalState.pc++;
dicarloj 17:c9afe1a7b423 518 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 519 globalState.de.lo = rrc(globalState.de.lo, false);
dicarloj 17:c9afe1a7b423 520 }
dicarloj 17:c9afe1a7b423 521
dicarloj 17:c9afe1a7b423 522 void RRC_H(u8 opcode) { // 0c
dicarloj 17:c9afe1a7b423 523 globalState.pc++;
dicarloj 17:c9afe1a7b423 524 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 525 globalState.hl.hi = rrc(globalState.hl.hi, false);
dicarloj 17:c9afe1a7b423 526 }
dicarloj 17:c9afe1a7b423 527
dicarloj 17:c9afe1a7b423 528 void RRC_L(u8 opcode) { // 0d
dicarloj 17:c9afe1a7b423 529 globalState.pc++;
dicarloj 17:c9afe1a7b423 530 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 531 globalState.hl.lo = rrc(globalState.hl.lo, false);
dicarloj 17:c9afe1a7b423 532 }
dicarloj 17:c9afe1a7b423 533
dicarloj 17:c9afe1a7b423 534 void RRC_DHL(u8 opcode) { // 0e
dicarloj 17:c9afe1a7b423 535 globalState.pc++;
dicarloj 17:c9afe1a7b423 536 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 537 u8 result = rrc(readByte(globalState.hl.v), false);
dicarloj 17:c9afe1a7b423 538 writeByte(result, globalState.hl.v);
dicarloj 17:c9afe1a7b423 539 }
dicarloj 17:c9afe1a7b423 540
dicarloj 17:c9afe1a7b423 541
dicarloj 17:c9afe1a7b423 542 void bit_B_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 543 u8 bitID = (opcode - (u8)0xC0) >> 3;
dicarloj 17:c9afe1a7b423 544 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 545 globalState.bc.hi |= (1 << bitID);
dicarloj 17:c9afe1a7b423 546 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 547 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 548 }
dicarloj 17:c9afe1a7b423 549
dicarloj 17:c9afe1a7b423 550 void bit_C_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 551 u8 bitID = (opcode - (u8)0xC1) >> 3;
dicarloj 17:c9afe1a7b423 552 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 553 globalState.bc.lo |= (1 << bitID);
dicarloj 17:c9afe1a7b423 554 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 555 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 556 }
dicarloj 17:c9afe1a7b423 557
dicarloj 17:c9afe1a7b423 558 void bit_D_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 559 u8 bitID = (opcode - (u8)0xC2) >> 3;
dicarloj 17:c9afe1a7b423 560 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 561 globalState.de.hi |= (1 << bitID);
dicarloj 17:c9afe1a7b423 562 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 563 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 564 }
dicarloj 17:c9afe1a7b423 565
dicarloj 17:c9afe1a7b423 566 void bit_E_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 567 u8 bitID = (opcode - (u8)0xC3) >> 3;
dicarloj 17:c9afe1a7b423 568 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 569 globalState.de.lo |= (1 << bitID);
dicarloj 17:c9afe1a7b423 570 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 571 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 572 }
dicarloj 17:c9afe1a7b423 573
dicarloj 17:c9afe1a7b423 574 void bit_H_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 575 u8 bitID = (opcode - (u8)0xC4) >> 3;
dicarloj 17:c9afe1a7b423 576 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 577 globalState.hl.hi |= (1 << bitID);
dicarloj 17:c9afe1a7b423 578 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 579 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 580 }
dicarloj 17:c9afe1a7b423 581
dicarloj 17:c9afe1a7b423 582 void bit_L_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 583 u8 bitID = (opcode - (u8)0xC5) >> 3;
dicarloj 17:c9afe1a7b423 584 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 585 globalState.hl.lo |= (1 << bitID);
dicarloj 17:c9afe1a7b423 586 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 587 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 588 }
dicarloj 17:c9afe1a7b423 589
dicarloj 17:c9afe1a7b423 590 void bit_DHL_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 591 u8 bitID = (opcode - (u8)0xC6) >> 3;
dicarloj 17:c9afe1a7b423 592 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 593 u8 value = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 594 value |= (1 << bitID);
dicarloj 17:c9afe1a7b423 595 writeByte(value, globalState.hl.v);
dicarloj 17:c9afe1a7b423 596 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 597 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 598 }
dicarloj 17:c9afe1a7b423 599
dicarloj 17:c9afe1a7b423 600 void bit_A_set(u8 opcode) {
dicarloj 17:c9afe1a7b423 601 u8 bitID = (opcode - (u8)0xC7) >> 3;
dicarloj 17:c9afe1a7b423 602 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 603 globalState.a |= (1 << bitID);
dicarloj 17:c9afe1a7b423 604 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 605 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 606 }
dicarloj 17:c9afe1a7b423 607
dicarloj 17:c9afe1a7b423 608 void bit_B_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 609 u8 bitID = (opcode - (u8)0x80) >> 3;
dicarloj 17:c9afe1a7b423 610 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 611 globalState.bc.hi &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 612 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 613 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 614 }
dicarloj 17:c9afe1a7b423 615
dicarloj 17:c9afe1a7b423 616 void bit_C_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 617 u8 bitID = (opcode - (u8)0x81) >> 3;
dicarloj 17:c9afe1a7b423 618 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 619 globalState.bc.lo &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 620 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 621 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 622 }
dicarloj 17:c9afe1a7b423 623
dicarloj 17:c9afe1a7b423 624 void bit_D_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 625 u8 bitID = (opcode - (u8)0x82) >> 3;
dicarloj 17:c9afe1a7b423 626 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 627 globalState.de.hi &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 628 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 629 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 630 }
dicarloj 17:c9afe1a7b423 631
dicarloj 17:c9afe1a7b423 632 void bit_E_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 633 u8 bitID = (opcode - (u8)0x83) >> 3;
dicarloj 17:c9afe1a7b423 634 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 635 globalState.de.lo &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 636 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 637 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 638 }
dicarloj 17:c9afe1a7b423 639
dicarloj 17:c9afe1a7b423 640 void bit_H_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 641 u8 bitID = (opcode - (u8)0x84) >> 3;
dicarloj 17:c9afe1a7b423 642 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 643 globalState.hl.hi &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 644 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 645 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 646 }
dicarloj 17:c9afe1a7b423 647
dicarloj 17:c9afe1a7b423 648 void bit_L_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 649 u8 bitID = (opcode - (u8)0x85) >> 3;
dicarloj 17:c9afe1a7b423 650 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 651 globalState.hl.lo &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 652 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 653 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 654 }
dicarloj 17:c9afe1a7b423 655
dicarloj 17:c9afe1a7b423 656 void bit_DHL_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 657 u8 bitID = (opcode - (u8)0x86) >> 3;
dicarloj 17:c9afe1a7b423 658 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 659 u8 value = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 660 value &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 661 writeByte(value, globalState.hl.v);
dicarloj 17:c9afe1a7b423 662 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 663 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 664 }
dicarloj 17:c9afe1a7b423 665
dicarloj 17:c9afe1a7b423 666 void bit_A_res(u8 opcode) {
dicarloj 17:c9afe1a7b423 667 u8 bitID = (opcode - (u8)0x87) >> 3;
dicarloj 17:c9afe1a7b423 668 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 669 globalState.a &= ~(1 << bitID);
dicarloj 17:c9afe1a7b423 670 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 671 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 672 }
dicarloj 17:c9afe1a7b423 673
dicarloj 17:c9afe1a7b423 674 void bit_A_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 675 u8 bitID = (opcode - (u8)0x47) >> 3;
dicarloj 17:c9afe1a7b423 676 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 677 //printf("check bit %d of A\n", bitID);
dicarloj 17:c9afe1a7b423 678 u8 val = globalState.a;
dicarloj 17:c9afe1a7b423 679 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 680 setZeroFlag();
dicarloj 17:c9afe1a7b423 681 } else {
dicarloj 17:c9afe1a7b423 682 clearZeroFlag();
dicarloj 17:c9afe1a7b423 683 }
dicarloj 17:c9afe1a7b423 684 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 685 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 686 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 687 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 688 }
dicarloj 17:c9afe1a7b423 689
dicarloj 17:c9afe1a7b423 690 void bit_B_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 691 u8 bitID = (opcode - (u8)0x40) >> 3;
dicarloj 17:c9afe1a7b423 692 //printf("B opcode 0x%x bitId %d\n", opcode, bitID);
dicarloj 17:c9afe1a7b423 693 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 694 //printf("check bit %d of B\n", bitID);
dicarloj 17:c9afe1a7b423 695 u8 val = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 696 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 697 setZeroFlag();
dicarloj 17:c9afe1a7b423 698 } else {
dicarloj 17:c9afe1a7b423 699 clearZeroFlag();
dicarloj 17:c9afe1a7b423 700 }
dicarloj 17:c9afe1a7b423 701 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 702 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 703 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 704 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 705 }
dicarloj 17:c9afe1a7b423 706
dicarloj 17:c9afe1a7b423 707
dicarloj 17:c9afe1a7b423 708 void bit_C_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 709 u8 bitID = (opcode - (u8)0x41) >> 3;
dicarloj 17:c9afe1a7b423 710 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 711 //printf("check bit %d of C\n", bitID);
dicarloj 17:c9afe1a7b423 712 u8 val = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 713 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 714 setZeroFlag();
dicarloj 17:c9afe1a7b423 715 } else {
dicarloj 17:c9afe1a7b423 716 clearZeroFlag();
dicarloj 17:c9afe1a7b423 717 }
dicarloj 17:c9afe1a7b423 718 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 719 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 720 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 721 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 722 }
dicarloj 17:c9afe1a7b423 723
dicarloj 17:c9afe1a7b423 724 void bit_D_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 725 u8 bitID = (opcode - (u8)0x42) >> 3;
dicarloj 17:c9afe1a7b423 726 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 727 //printf("check bit %d of D\n", bitID);
dicarloj 17:c9afe1a7b423 728 u8 val = globalState.de.hi;
dicarloj 17:c9afe1a7b423 729 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 730 setZeroFlag();
dicarloj 17:c9afe1a7b423 731 } else {
dicarloj 17:c9afe1a7b423 732 clearZeroFlag();
dicarloj 17:c9afe1a7b423 733 }
dicarloj 17:c9afe1a7b423 734 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 735 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 736 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 737 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 738 }
dicarloj 17:c9afe1a7b423 739
dicarloj 17:c9afe1a7b423 740 void bit_E_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 741 u8 bitID = (opcode - (u8)0x43) >> 3;
dicarloj 17:c9afe1a7b423 742 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 743 //printf("check bit %d of E\n", bitID);
dicarloj 17:c9afe1a7b423 744 u8 val = globalState.de.lo;
dicarloj 17:c9afe1a7b423 745 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 746 setZeroFlag();
dicarloj 17:c9afe1a7b423 747 } else {
dicarloj 17:c9afe1a7b423 748 clearZeroFlag();
dicarloj 17:c9afe1a7b423 749 }
dicarloj 17:c9afe1a7b423 750 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 751 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 752 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 753 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 754 }
dicarloj 17:c9afe1a7b423 755
dicarloj 17:c9afe1a7b423 756 void bit_H_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 757 u8 bitID = (opcode - (u8)0x44) >> 3;
dicarloj 17:c9afe1a7b423 758 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 759 //printf("check bit %d of H\n", bitID);
dicarloj 17:c9afe1a7b423 760 u8 val = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 761 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 762 setZeroFlag();
dicarloj 17:c9afe1a7b423 763 } else {
dicarloj 17:c9afe1a7b423 764 clearZeroFlag();
dicarloj 17:c9afe1a7b423 765 }
dicarloj 17:c9afe1a7b423 766 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 767 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 768 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 769 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 770 }
dicarloj 17:c9afe1a7b423 771
dicarloj 17:c9afe1a7b423 772 void bit_L_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 773 u8 bitID = (opcode - (u8)0x45) >> 3;
dicarloj 17:c9afe1a7b423 774 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 775 //printf("check bit %d of L\n", bitID);
dicarloj 17:c9afe1a7b423 776 u8 val = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 777 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 778 setZeroFlag();
dicarloj 17:c9afe1a7b423 779 } else {
dicarloj 17:c9afe1a7b423 780 clearZeroFlag();
dicarloj 17:c9afe1a7b423 781 }
dicarloj 17:c9afe1a7b423 782 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 783 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 784 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 785 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 786 }
dicarloj 17:c9afe1a7b423 787
dicarloj 17:c9afe1a7b423 788 void bit_DHL_test(u8 opcode) {
dicarloj 17:c9afe1a7b423 789 u8 bitID = (opcode - (u8)0x45) >> 3;
dicarloj 17:c9afe1a7b423 790 assert(bitID < 8);
dicarloj 17:c9afe1a7b423 791 //printf("check bit %d of L\n", bitID);
dicarloj 17:c9afe1a7b423 792 u8 val = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 793 if(((val >> bitID) & 1) == 0) {
dicarloj 17:c9afe1a7b423 794 setZeroFlag();
dicarloj 17:c9afe1a7b423 795 } else {
dicarloj 17:c9afe1a7b423 796 clearZeroFlag();
dicarloj 17:c9afe1a7b423 797 }
dicarloj 17:c9afe1a7b423 798 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 799 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 800 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 801 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 802 }
dicarloj 17:c9afe1a7b423 803
dicarloj 17:c9afe1a7b423 804 // CB-prefixed opcode handler table
dicarloj 17:c9afe1a7b423 805 static OpcodeHandler* opcode_cbs[256] =
dicarloj 17:c9afe1a7b423 806 {RLC_B, RLC_C, RLC_D, RLC_E, RLC_H, RLC_L, RLC_DHL, RLC_A, // 0x0 - 0x7
dicarloj 17:c9afe1a7b423 807 RRC_B, RRC_C, RRC_D, RRC_E, RRC_H, RRC_L, RRC_DHL, RRC_A, // 0x8 - 0xf
dicarloj 17:c9afe1a7b423 808 RL_B, RL_C, RL_D, RL_E, RL_H, RL_L, RL_DHL, RL_A, // 0x10 - 0x17
dicarloj 17:c9afe1a7b423 809 RR_B, RR_C, RR_D, RR_E, RR_H, RR_L, RR_DHL, RR_A, // 0x18 - 0x1f
dicarloj 17:c9afe1a7b423 810 SLA_B, SLA_C, SLA_D, SLA_E, SLA_H, SLA_L, SLA_DHL, SLA_A, // 0x20 - 0x27
dicarloj 17:c9afe1a7b423 811 SRA_B, SRA_C, SRA_D, SRA_E, SRA_H, SRA_L, SRA_DHL, SRA_A, // 0x28 - 0x2f
dicarloj 17:c9afe1a7b423 812 SWAP_B, SWAP_C, SWAP_D, SWAP_E, SWAP_H, SWAP_L, SWAP_DHL, SWAP_A, // 0x30 - 0x37
dicarloj 17:c9afe1a7b423 813 SRL_B, SRL_C, SRL_D, SRL_E, SRL_H, SRL_L, SRL_DHL, SRL_A, // 0x38 - 0x3f
dicarloj 17:c9afe1a7b423 814 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x40 - 0x47
dicarloj 17:c9afe1a7b423 815 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x48 - 0x4f
dicarloj 17:c9afe1a7b423 816 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x50 - 0x57
dicarloj 17:c9afe1a7b423 817 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x58 - 0x5f
dicarloj 17:c9afe1a7b423 818 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x60 - 0x67
dicarloj 17:c9afe1a7b423 819 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x68 - 0x6f
dicarloj 17:c9afe1a7b423 820 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x70 - 0x77
dicarloj 17:c9afe1a7b423 821 bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x78 - 0x7f
dicarloj 17:c9afe1a7b423 822 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x80 - 0x8
dicarloj 17:c9afe1a7b423 823 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x88 - 0x8f
dicarloj 17:c9afe1a7b423 824 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x90 - 0x97
dicarloj 17:c9afe1a7b423 825 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x98 - 0x9f
dicarloj 17:c9afe1a7b423 826 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xa0 - 0xa7
dicarloj 17:c9afe1a7b423 827 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xa8 - 0xaf
dicarloj 17:c9afe1a7b423 828 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xb0 - 0xb7
dicarloj 17:c9afe1a7b423 829 bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xb8 - 0xbf
dicarloj 17:c9afe1a7b423 830 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xc0 - 0xc7
dicarloj 17:c9afe1a7b423 831 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xc8 - 0xcf
dicarloj 17:c9afe1a7b423 832 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xd0 - 0xd7
dicarloj 17:c9afe1a7b423 833 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xd8 - 0xdf
dicarloj 17:c9afe1a7b423 834 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xe0 - 0xe7
dicarloj 17:c9afe1a7b423 835 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xe8 - 0xef
dicarloj 17:c9afe1a7b423 836 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xf0 - 0xf7
dicarloj 17:c9afe1a7b423 837 bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set }; // 0xf8 - 0xff
dicarloj 17:c9afe1a7b423 838
dicarloj 17:c9afe1a7b423 839
dicarloj 17:c9afe1a7b423 840 ////////////////////////////////
dicarloj 17:c9afe1a7b423 841 // Opcodes //
dicarloj 17:c9afe1a7b423 842 ////////////////////////////////
dicarloj 17:c9afe1a7b423 843 void LD_B_n(u8 opocde) { // 06
dicarloj 17:c9afe1a7b423 844 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 845 globalState.pc++;
dicarloj 17:c9afe1a7b423 846 globalState.bc.hi = imm;
dicarloj 17:c9afe1a7b423 847 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 848 }
dicarloj 17:c9afe1a7b423 849
dicarloj 17:c9afe1a7b423 850 void LD_C_n(u8 opocde) { // 0E
dicarloj 17:c9afe1a7b423 851 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 852 globalState.pc++;
dicarloj 17:c9afe1a7b423 853 globalState.bc.lo = imm;
dicarloj 17:c9afe1a7b423 854 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 855 }
dicarloj 17:c9afe1a7b423 856
dicarloj 17:c9afe1a7b423 857 void LD_D_n(u8 opocde) { // 16
dicarloj 17:c9afe1a7b423 858 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 859 globalState.pc++;
dicarloj 17:c9afe1a7b423 860 globalState.de.hi = imm;
dicarloj 17:c9afe1a7b423 861 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 862 }
dicarloj 17:c9afe1a7b423 863
dicarloj 17:c9afe1a7b423 864 void LD_E_n(u8 opocde) { // 1e
dicarloj 17:c9afe1a7b423 865 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 866 globalState.pc++;
dicarloj 17:c9afe1a7b423 867 globalState.de.lo = imm;
dicarloj 17:c9afe1a7b423 868 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 869 }
dicarloj 17:c9afe1a7b423 870
dicarloj 17:c9afe1a7b423 871 void LD_H_n(u8 opocde) { // 26
dicarloj 17:c9afe1a7b423 872 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 873 globalState.pc++;
dicarloj 17:c9afe1a7b423 874 globalState.hl.hi = imm;
dicarloj 17:c9afe1a7b423 875 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 876 }
dicarloj 17:c9afe1a7b423 877
dicarloj 17:c9afe1a7b423 878 void LD_L_n(u8 opocde) { // 2e
dicarloj 17:c9afe1a7b423 879 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 880 globalState.pc++;
dicarloj 17:c9afe1a7b423 881 globalState.hl.lo = imm;
dicarloj 17:c9afe1a7b423 882 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 883 }
dicarloj 17:c9afe1a7b423 884
dicarloj 17:c9afe1a7b423 885 // REGISTER-REGISTER LOADS (REGISTER A)
dicarloj 17:c9afe1a7b423 886 void LD_A_A(u8 opcode) { // 0x7f
dicarloj 17:c9afe1a7b423 887 globalState.pc++;
dicarloj 17:c9afe1a7b423 888 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 889 globalState.a = globalState.a;
dicarloj 17:c9afe1a7b423 890 }
dicarloj 17:c9afe1a7b423 891
dicarloj 17:c9afe1a7b423 892 void LD_A_B(u8 opcode) { // 0x78
dicarloj 17:c9afe1a7b423 893 globalState.pc++;
dicarloj 17:c9afe1a7b423 894 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 895 globalState.a = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 896 }
dicarloj 17:c9afe1a7b423 897
dicarloj 17:c9afe1a7b423 898 void LD_A_C(u8 opcode) { // 0x79
dicarloj 17:c9afe1a7b423 899 globalState.pc++;
dicarloj 17:c9afe1a7b423 900 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 901 globalState.a = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 902 }
dicarloj 17:c9afe1a7b423 903
dicarloj 17:c9afe1a7b423 904 void LD_A_D(u8 opcode) { // 0x7a
dicarloj 17:c9afe1a7b423 905 globalState.pc++;
dicarloj 17:c9afe1a7b423 906 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 907 globalState.a = globalState.de.hi;
dicarloj 17:c9afe1a7b423 908 }
dicarloj 17:c9afe1a7b423 909
dicarloj 17:c9afe1a7b423 910 void LD_A_E(u8 opcode) { // 0x7b
dicarloj 17:c9afe1a7b423 911 globalState.pc++;
dicarloj 17:c9afe1a7b423 912 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 913 globalState.a = globalState.de.lo;
dicarloj 17:c9afe1a7b423 914 }
dicarloj 17:c9afe1a7b423 915
dicarloj 17:c9afe1a7b423 916 void LD_A_H(u8 opcode) { // 0x7c
dicarloj 17:c9afe1a7b423 917 globalState.pc++;
dicarloj 17:c9afe1a7b423 918 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 919 globalState.a = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 920 }
dicarloj 17:c9afe1a7b423 921
dicarloj 17:c9afe1a7b423 922 void LD_A_L(u8 opcode) { // 0x7d
dicarloj 17:c9afe1a7b423 923 globalState.pc++;
dicarloj 17:c9afe1a7b423 924 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 925 globalState.a = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 926 }
dicarloj 17:c9afe1a7b423 927
dicarloj 17:c9afe1a7b423 928 void LD_A_DHL(u8 opcode) { // 0x7e
dicarloj 17:c9afe1a7b423 929 globalState.pc++;
dicarloj 17:c9afe1a7b423 930 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 931 globalState.a = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 932 }
dicarloj 17:c9afe1a7b423 933
dicarloj 17:c9afe1a7b423 934
dicarloj 17:c9afe1a7b423 935
dicarloj 17:c9afe1a7b423 936 // REGISTER-REGISTER LOADS (REGISTER B)
dicarloj 17:c9afe1a7b423 937 void LD_B_B(u8 opcode) { // 0x40
dicarloj 17:c9afe1a7b423 938 globalState.pc++;
dicarloj 17:c9afe1a7b423 939 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 940 globalState.bc.hi = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 941 }
dicarloj 17:c9afe1a7b423 942
dicarloj 17:c9afe1a7b423 943 void LD_B_C(u8 opcode) { // 0x41
dicarloj 17:c9afe1a7b423 944 globalState.pc++;
dicarloj 17:c9afe1a7b423 945 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 946 globalState.bc.hi = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 947 }
dicarloj 17:c9afe1a7b423 948
dicarloj 17:c9afe1a7b423 949 void LD_B_D(u8 opcode) { // 0x42
dicarloj 17:c9afe1a7b423 950 globalState.pc++;
dicarloj 17:c9afe1a7b423 951 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 952 globalState.bc.hi = globalState.de.hi;
dicarloj 17:c9afe1a7b423 953 }
dicarloj 17:c9afe1a7b423 954
dicarloj 17:c9afe1a7b423 955 void LD_B_E(u8 opcode) { // 0x43
dicarloj 17:c9afe1a7b423 956 globalState.pc++;
dicarloj 17:c9afe1a7b423 957 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 958 globalState.bc.hi = globalState.de.lo;
dicarloj 17:c9afe1a7b423 959 }
dicarloj 17:c9afe1a7b423 960
dicarloj 17:c9afe1a7b423 961 void LD_B_H(u8 opcode) { // 0x44
dicarloj 17:c9afe1a7b423 962 globalState.pc++;
dicarloj 17:c9afe1a7b423 963 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 964 globalState.bc.hi = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 965 }
dicarloj 17:c9afe1a7b423 966
dicarloj 17:c9afe1a7b423 967 void LD_B_L(u8 opcode) { // 0x45
dicarloj 17:c9afe1a7b423 968 globalState.pc++;
dicarloj 17:c9afe1a7b423 969 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 970 globalState.bc.hi = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 971 }
dicarloj 17:c9afe1a7b423 972
dicarloj 17:c9afe1a7b423 973 void LD_B_DHL(u8 opcode) { // 0x46
dicarloj 17:c9afe1a7b423 974 globalState.pc++;
dicarloj 17:c9afe1a7b423 975 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 976 globalState.bc.hi = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 977 }
dicarloj 17:c9afe1a7b423 978
dicarloj 17:c9afe1a7b423 979
dicarloj 17:c9afe1a7b423 980
dicarloj 17:c9afe1a7b423 981 // REGISTER-REGISTER LOADS (REGISTER C)
dicarloj 17:c9afe1a7b423 982 void LD_C_B(u8 opcode) { // 0x48
dicarloj 17:c9afe1a7b423 983 globalState.pc++;
dicarloj 17:c9afe1a7b423 984 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 985 globalState.bc.lo = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 986 }
dicarloj 17:c9afe1a7b423 987
dicarloj 17:c9afe1a7b423 988 void LD_C_C(u8 opcode) { // 0x49
dicarloj 17:c9afe1a7b423 989 globalState.pc++;
dicarloj 17:c9afe1a7b423 990 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 991 globalState.bc.lo = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 992 }
dicarloj 17:c9afe1a7b423 993
dicarloj 17:c9afe1a7b423 994 void LD_C_D(u8 opcode) { // 0x4a
dicarloj 17:c9afe1a7b423 995 globalState.pc++;
dicarloj 17:c9afe1a7b423 996 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 997 globalState.bc.lo = globalState.de.hi;
dicarloj 17:c9afe1a7b423 998 }
dicarloj 17:c9afe1a7b423 999
dicarloj 17:c9afe1a7b423 1000 void LD_C_E(u8 opcode) { // 0x4b
dicarloj 17:c9afe1a7b423 1001 globalState.pc++;
dicarloj 17:c9afe1a7b423 1002 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1003 globalState.bc.lo = globalState.de.lo;
dicarloj 17:c9afe1a7b423 1004 }
dicarloj 17:c9afe1a7b423 1005
dicarloj 17:c9afe1a7b423 1006 void LD_C_H(u8 opcode) { // 0x4c
dicarloj 17:c9afe1a7b423 1007 globalState.pc++;
dicarloj 17:c9afe1a7b423 1008 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1009 globalState.bc.lo = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 1010 }
dicarloj 17:c9afe1a7b423 1011
dicarloj 17:c9afe1a7b423 1012 void LD_C_L(u8 opcode) { // 0x4d
dicarloj 17:c9afe1a7b423 1013 globalState.pc++;
dicarloj 17:c9afe1a7b423 1014 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1015 globalState.bc.lo = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 1016 }
dicarloj 17:c9afe1a7b423 1017
dicarloj 17:c9afe1a7b423 1018 void LD_C_DHL(u8 opcode) { // 0x4e
dicarloj 17:c9afe1a7b423 1019 globalState.pc++;
dicarloj 17:c9afe1a7b423 1020 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1021 globalState.bc.lo = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1022 }
dicarloj 17:c9afe1a7b423 1023
dicarloj 17:c9afe1a7b423 1024
dicarloj 17:c9afe1a7b423 1025
dicarloj 17:c9afe1a7b423 1026 // REGISTER-REGISTER LOADS (REGISTER D)
dicarloj 17:c9afe1a7b423 1027 void LD_D_B(u8 opcode) { // 0x50
dicarloj 17:c9afe1a7b423 1028 globalState.pc++;
dicarloj 17:c9afe1a7b423 1029 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1030 globalState.de.hi = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 1031 }
dicarloj 17:c9afe1a7b423 1032
dicarloj 17:c9afe1a7b423 1033 void LD_D_C(u8 opcode) { // 0x51
dicarloj 17:c9afe1a7b423 1034 globalState.pc++;
dicarloj 17:c9afe1a7b423 1035 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1036 globalState.de.hi = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 1037 }
dicarloj 17:c9afe1a7b423 1038
dicarloj 17:c9afe1a7b423 1039 void LD_D_D(u8 opcode) { // 0x52
dicarloj 17:c9afe1a7b423 1040 globalState.pc++;
dicarloj 17:c9afe1a7b423 1041 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1042 globalState.de.hi = globalState.de.hi;
dicarloj 17:c9afe1a7b423 1043 }
dicarloj 17:c9afe1a7b423 1044
dicarloj 17:c9afe1a7b423 1045 void LD_D_E(u8 opcode) { // 0x53
dicarloj 17:c9afe1a7b423 1046 globalState.pc++;
dicarloj 17:c9afe1a7b423 1047 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1048 globalState.de.hi = globalState.de.lo;
dicarloj 17:c9afe1a7b423 1049 }
dicarloj 17:c9afe1a7b423 1050
dicarloj 17:c9afe1a7b423 1051 void LD_D_H(u8 opcode) { // 0x54
dicarloj 17:c9afe1a7b423 1052 globalState.pc++;
dicarloj 17:c9afe1a7b423 1053 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1054 globalState.de.hi = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 1055 }
dicarloj 17:c9afe1a7b423 1056
dicarloj 17:c9afe1a7b423 1057 void LD_D_L(u8 opcode) { // 0x55
dicarloj 17:c9afe1a7b423 1058 globalState.pc++;
dicarloj 17:c9afe1a7b423 1059 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1060 globalState.de.hi = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 1061 }
dicarloj 17:c9afe1a7b423 1062
dicarloj 17:c9afe1a7b423 1063 void LD_D_DHL(u8 opcode) { // 0x56
dicarloj 17:c9afe1a7b423 1064 globalState.pc++;
dicarloj 17:c9afe1a7b423 1065 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1066 globalState.de.hi = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1067 }
dicarloj 17:c9afe1a7b423 1068
dicarloj 17:c9afe1a7b423 1069
dicarloj 17:c9afe1a7b423 1070 // REGISTER-REGISTER LOADS (REGISTER E)
dicarloj 17:c9afe1a7b423 1071 void LD_E_B(u8 opcode) { // 0x58
dicarloj 17:c9afe1a7b423 1072 globalState.pc++;
dicarloj 17:c9afe1a7b423 1073 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1074 globalState.de.lo = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 1075 }
dicarloj 17:c9afe1a7b423 1076
dicarloj 17:c9afe1a7b423 1077 void LD_E_C(u8 opcode) { // 0x59
dicarloj 17:c9afe1a7b423 1078 globalState.pc++;
dicarloj 17:c9afe1a7b423 1079 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1080 globalState.de.lo = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 1081 }
dicarloj 17:c9afe1a7b423 1082
dicarloj 17:c9afe1a7b423 1083 void LD_E_D(u8 opcode) { // 0x5a
dicarloj 17:c9afe1a7b423 1084 globalState.pc++;
dicarloj 17:c9afe1a7b423 1085 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1086 globalState.de.lo = globalState.de.hi;
dicarloj 17:c9afe1a7b423 1087 }
dicarloj 17:c9afe1a7b423 1088
dicarloj 17:c9afe1a7b423 1089 void LD_E_E(u8 opcode) { // 0x5b
dicarloj 17:c9afe1a7b423 1090 globalState.pc++;
dicarloj 17:c9afe1a7b423 1091 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1092 globalState.de.lo = globalState.de.lo;
dicarloj 17:c9afe1a7b423 1093 }
dicarloj 17:c9afe1a7b423 1094
dicarloj 17:c9afe1a7b423 1095 void LD_E_H(u8 opcode) { // 0x5c
dicarloj 17:c9afe1a7b423 1096 globalState.pc++;
dicarloj 17:c9afe1a7b423 1097 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1098 globalState.de.lo = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 1099 }
dicarloj 17:c9afe1a7b423 1100
dicarloj 17:c9afe1a7b423 1101 void LD_E_L(u8 opcode) { // 0x5d
dicarloj 17:c9afe1a7b423 1102 globalState.pc++;
dicarloj 17:c9afe1a7b423 1103 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1104 globalState.de.lo = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 1105 }
dicarloj 17:c9afe1a7b423 1106
dicarloj 17:c9afe1a7b423 1107 void LD_E_DHL(u8 opcode) { // 0x5e
dicarloj 17:c9afe1a7b423 1108 globalState.pc++;
dicarloj 17:c9afe1a7b423 1109 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1110 globalState.de.lo = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1111 }
dicarloj 17:c9afe1a7b423 1112
dicarloj 17:c9afe1a7b423 1113 // REGISTER-REGISTER LOADS (REGISTER H)
dicarloj 17:c9afe1a7b423 1114 void LD_H_B(u8 opcode) { // 0x60
dicarloj 17:c9afe1a7b423 1115 globalState.pc++;
dicarloj 17:c9afe1a7b423 1116 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1117 globalState.hl.hi = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 1118 }
dicarloj 17:c9afe1a7b423 1119
dicarloj 17:c9afe1a7b423 1120 void LD_H_C(u8 opcode) { // 0x61
dicarloj 17:c9afe1a7b423 1121 globalState.pc++;
dicarloj 17:c9afe1a7b423 1122 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1123 globalState.hl.hi = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 1124 }
dicarloj 17:c9afe1a7b423 1125
dicarloj 17:c9afe1a7b423 1126 void LD_H_D(u8 opcode) { // 0x62
dicarloj 17:c9afe1a7b423 1127 globalState.pc++;
dicarloj 17:c9afe1a7b423 1128 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1129 globalState.hl.hi = globalState.de.hi;
dicarloj 17:c9afe1a7b423 1130 }
dicarloj 17:c9afe1a7b423 1131
dicarloj 17:c9afe1a7b423 1132 void LD_H_E(u8 opcode) { // 0x63
dicarloj 17:c9afe1a7b423 1133 globalState.pc++;
dicarloj 17:c9afe1a7b423 1134 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1135 globalState.hl.hi = globalState.de.lo;
dicarloj 17:c9afe1a7b423 1136 }
dicarloj 17:c9afe1a7b423 1137
dicarloj 17:c9afe1a7b423 1138 void LD_H_H(u8 opcode) { // 0x64
dicarloj 17:c9afe1a7b423 1139 globalState.pc++;
dicarloj 17:c9afe1a7b423 1140 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1141 globalState.hl.hi = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 1142 }
dicarloj 17:c9afe1a7b423 1143
dicarloj 17:c9afe1a7b423 1144 void LD_H_L(u8 opcode) { // 0x65
dicarloj 17:c9afe1a7b423 1145 globalState.pc++;
dicarloj 17:c9afe1a7b423 1146 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1147 globalState.hl.hi = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 1148 }
dicarloj 17:c9afe1a7b423 1149
dicarloj 17:c9afe1a7b423 1150 void LD_H_DHL(u8 opcode) { // 0x66
dicarloj 17:c9afe1a7b423 1151 globalState.pc++;
dicarloj 17:c9afe1a7b423 1152 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1153 globalState.hl.hi = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1154 }
dicarloj 17:c9afe1a7b423 1155
dicarloj 17:c9afe1a7b423 1156 // REGISTER-REGISTER LOADS (REGISTER L)
dicarloj 17:c9afe1a7b423 1157 void LD_L_B(u8 opcode) { // 0x60
dicarloj 17:c9afe1a7b423 1158 globalState.pc++;
dicarloj 17:c9afe1a7b423 1159 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1160 globalState.hl.lo = globalState.bc.hi;
dicarloj 17:c9afe1a7b423 1161 }
dicarloj 17:c9afe1a7b423 1162
dicarloj 17:c9afe1a7b423 1163 void LD_L_C(u8 opcode) { // 0x61
dicarloj 17:c9afe1a7b423 1164 globalState.pc++;
dicarloj 17:c9afe1a7b423 1165 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1166 globalState.hl.lo = globalState.bc.lo;
dicarloj 17:c9afe1a7b423 1167 }
dicarloj 17:c9afe1a7b423 1168
dicarloj 17:c9afe1a7b423 1169 void LD_L_D(u8 opcode) { // 0x62
dicarloj 17:c9afe1a7b423 1170 globalState.pc++;
dicarloj 17:c9afe1a7b423 1171 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1172 globalState.hl.lo = globalState.de.hi;
dicarloj 17:c9afe1a7b423 1173 }
dicarloj 17:c9afe1a7b423 1174
dicarloj 17:c9afe1a7b423 1175 void LD_L_E(u8 opcode) { // 0x63
dicarloj 17:c9afe1a7b423 1176 globalState.pc++;
dicarloj 17:c9afe1a7b423 1177 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1178 globalState.hl.lo = globalState.de.lo;
dicarloj 17:c9afe1a7b423 1179 }
dicarloj 17:c9afe1a7b423 1180
dicarloj 17:c9afe1a7b423 1181 void LD_L_H(u8 opcode) { // 0x64
dicarloj 17:c9afe1a7b423 1182 globalState.pc++;
dicarloj 17:c9afe1a7b423 1183 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1184 globalState.hl.lo = globalState.hl.hi;
dicarloj 17:c9afe1a7b423 1185 }
dicarloj 17:c9afe1a7b423 1186
dicarloj 17:c9afe1a7b423 1187 void LD_L_L(u8 opcode) { // 0x65
dicarloj 17:c9afe1a7b423 1188 globalState.pc++;
dicarloj 17:c9afe1a7b423 1189 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1190 globalState.hl.lo = globalState.hl.lo;
dicarloj 17:c9afe1a7b423 1191 }
dicarloj 17:c9afe1a7b423 1192
dicarloj 17:c9afe1a7b423 1193 void LD_L_DHL(u8 opcode) { // 0x66
dicarloj 17:c9afe1a7b423 1194 globalState.pc++;
dicarloj 17:c9afe1a7b423 1195 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1196 globalState.hl.lo = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1197 }
dicarloj 17:c9afe1a7b423 1198
dicarloj 17:c9afe1a7b423 1199 // REGISTER-REGISTER LOADS (REGISTER (HL))
dicarloj 17:c9afe1a7b423 1200 void LD_DHL_B(u8 opcode) { // 0x70
dicarloj 17:c9afe1a7b423 1201 globalState.pc++;
dicarloj 17:c9afe1a7b423 1202 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1203 writeByte(globalState.bc.hi, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1204 }
dicarloj 17:c9afe1a7b423 1205
dicarloj 17:c9afe1a7b423 1206 void LD_DHL_C(u8 opcode) { // 0x71
dicarloj 17:c9afe1a7b423 1207 globalState.pc++;
dicarloj 17:c9afe1a7b423 1208 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1209 writeByte(globalState.bc.lo, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1210 }
dicarloj 17:c9afe1a7b423 1211
dicarloj 17:c9afe1a7b423 1212 void LD_DHL_D(u8 opcode) { // 0x72
dicarloj 17:c9afe1a7b423 1213 globalState.pc++;
dicarloj 17:c9afe1a7b423 1214 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1215 writeByte(globalState.de.hi, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1216 }
dicarloj 17:c9afe1a7b423 1217
dicarloj 17:c9afe1a7b423 1218 void LD_DHL_E(u8 opcode) { // 0x73
dicarloj 17:c9afe1a7b423 1219 globalState.pc++;
dicarloj 17:c9afe1a7b423 1220 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1221 writeByte(globalState.de.lo, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1222 }
dicarloj 17:c9afe1a7b423 1223
dicarloj 17:c9afe1a7b423 1224 void LD_DHL_H(u8 opcode) { // 0x74
dicarloj 17:c9afe1a7b423 1225 globalState.pc++;
dicarloj 17:c9afe1a7b423 1226 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1227 writeByte(globalState.hl.hi, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1228 }
dicarloj 17:c9afe1a7b423 1229
dicarloj 17:c9afe1a7b423 1230 void LD_DHL_L(u8 opcode) { // 0x75
dicarloj 17:c9afe1a7b423 1231 globalState.pc++;
dicarloj 17:c9afe1a7b423 1232 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1233 writeByte(globalState.hl.lo, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1234 }
dicarloj 17:c9afe1a7b423 1235
dicarloj 17:c9afe1a7b423 1236 // Random
dicarloj 17:c9afe1a7b423 1237 void LD_DHL_n(u8 opcode) { // 0x36
dicarloj 17:c9afe1a7b423 1238 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1239 globalState.pc++;
dicarloj 17:c9afe1a7b423 1240 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1241 writeByte(imm, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1242 }
dicarloj 17:c9afe1a7b423 1243
dicarloj 17:c9afe1a7b423 1244 // Load Into Register A Specials
dicarloj 17:c9afe1a7b423 1245 void LD_A_DBC(u8 opcode) { // 0A
dicarloj 17:c9afe1a7b423 1246 globalState.pc++;
dicarloj 17:c9afe1a7b423 1247 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1248 globalState.a = readByte(globalState.bc.v);
dicarloj 17:c9afe1a7b423 1249 }
dicarloj 17:c9afe1a7b423 1250
dicarloj 17:c9afe1a7b423 1251 void LD_A_DDE(u8 opcode) { // 1A
dicarloj 17:c9afe1a7b423 1252 globalState.pc++;
dicarloj 17:c9afe1a7b423 1253 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1254 globalState.a = readByte(globalState.de.v);
dicarloj 17:c9afe1a7b423 1255 }
dicarloj 17:c9afe1a7b423 1256
dicarloj 17:c9afe1a7b423 1257
dicarloj 17:c9afe1a7b423 1258 void LD_A_Dnn(u8 opcode) { // FA
dicarloj 17:c9afe1a7b423 1259 globalState.pc++;
dicarloj 17:c9afe1a7b423 1260 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 1261 globalState.a = readByte(readU16(globalState.pc));
dicarloj 17:c9afe1a7b423 1262 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 1263 }
dicarloj 17:c9afe1a7b423 1264
dicarloj 17:c9afe1a7b423 1265 void LD_A_n(u8 opocde) { // 0x3e
dicarloj 17:c9afe1a7b423 1266 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1267 globalState.pc++;
dicarloj 17:c9afe1a7b423 1268 globalState.a = imm;
dicarloj 17:c9afe1a7b423 1269 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1270 }
dicarloj 17:c9afe1a7b423 1271
dicarloj 17:c9afe1a7b423 1272 // LOAD FROM REGISTER A
dicarloj 17:c9afe1a7b423 1273 void LD_B_A(u8 opcode) { // 0x47
dicarloj 17:c9afe1a7b423 1274 globalState.pc++;
dicarloj 17:c9afe1a7b423 1275 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1276 globalState.bc.hi = globalState.a;
dicarloj 17:c9afe1a7b423 1277 }
dicarloj 17:c9afe1a7b423 1278
dicarloj 17:c9afe1a7b423 1279 void LD_C_A(u8 opcode) { // 0x4f
dicarloj 17:c9afe1a7b423 1280 globalState.pc++;
dicarloj 17:c9afe1a7b423 1281 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1282 globalState.bc.lo = globalState.a;
dicarloj 17:c9afe1a7b423 1283 }
dicarloj 17:c9afe1a7b423 1284
dicarloj 17:c9afe1a7b423 1285 void LD_D_A(u8 opcode) { // 0x57
dicarloj 17:c9afe1a7b423 1286 globalState.pc++;
dicarloj 17:c9afe1a7b423 1287 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1288 globalState.de.hi = globalState.a;
dicarloj 17:c9afe1a7b423 1289 }
dicarloj 17:c9afe1a7b423 1290
dicarloj 17:c9afe1a7b423 1291 void LD_E_A(u8 opcode) { // 0x5f
dicarloj 17:c9afe1a7b423 1292 globalState.pc++;
dicarloj 17:c9afe1a7b423 1293 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1294 globalState.de.lo = globalState.a;
dicarloj 17:c9afe1a7b423 1295 }
dicarloj 17:c9afe1a7b423 1296
dicarloj 17:c9afe1a7b423 1297 void LD_H_A(u8 opcode) { // 0x67
dicarloj 17:c9afe1a7b423 1298 globalState.pc++;
dicarloj 17:c9afe1a7b423 1299 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1300 globalState.hl.hi = globalState.a;
dicarloj 17:c9afe1a7b423 1301 }
dicarloj 17:c9afe1a7b423 1302
dicarloj 17:c9afe1a7b423 1303 void LD_L_A(u8 opcode) { // 0x6f
dicarloj 17:c9afe1a7b423 1304 globalState.pc++;
dicarloj 17:c9afe1a7b423 1305 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1306 globalState.hl.lo = globalState.a;
dicarloj 17:c9afe1a7b423 1307 }
dicarloj 17:c9afe1a7b423 1308
dicarloj 17:c9afe1a7b423 1309 void LD_DBC_A(u8 opcode) { // 0x02
dicarloj 17:c9afe1a7b423 1310 globalState.pc++;
dicarloj 17:c9afe1a7b423 1311 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1312 writeByte(globalState.a, globalState.bc.v);
dicarloj 17:c9afe1a7b423 1313 }
dicarloj 17:c9afe1a7b423 1314
dicarloj 17:c9afe1a7b423 1315 void LD_DDE_A(u8 opcode) { // 0x12
dicarloj 17:c9afe1a7b423 1316 globalState.pc++;
dicarloj 17:c9afe1a7b423 1317 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1318 writeByte(globalState.a, globalState.de.v);
dicarloj 17:c9afe1a7b423 1319 }
dicarloj 17:c9afe1a7b423 1320
dicarloj 17:c9afe1a7b423 1321 void LD_DHL_A(u8 opcode) { // 0x77
dicarloj 17:c9afe1a7b423 1322 globalState.pc++;
dicarloj 17:c9afe1a7b423 1323 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1324 writeByte(globalState.a, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1325 }
dicarloj 17:c9afe1a7b423 1326
dicarloj 17:c9afe1a7b423 1327 void LD_Dnn_A(u8 opcode) { // 0xea
dicarloj 17:c9afe1a7b423 1328 globalState.pc++;
dicarloj 17:c9afe1a7b423 1329 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 1330 writeByte(globalState.a, readU16(globalState.pc));
dicarloj 17:c9afe1a7b423 1331 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 1332 }
dicarloj 17:c9afe1a7b423 1333
dicarloj 17:c9afe1a7b423 1334 // Load A with memory offset from 0xff00
dicarloj 17:c9afe1a7b423 1335 void LD_A_FF00_C(u8 opcode) { //0xf2
dicarloj 17:c9afe1a7b423 1336 globalState.pc++;
dicarloj 17:c9afe1a7b423 1337 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1338 globalState.a = readByte((u16)0xff00 + globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1339 }
dicarloj 17:c9afe1a7b423 1340
dicarloj 17:c9afe1a7b423 1341 // Store A with memory offset from 0xff00
dicarloj 17:c9afe1a7b423 1342 void LD_FF00_C_A(u8 opcode) { // 0xe2
dicarloj 17:c9afe1a7b423 1343 globalState.pc++;
dicarloj 17:c9afe1a7b423 1344 writeByte(globalState.a, (u16)0xff00 + globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1345 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1346 }
dicarloj 17:c9afe1a7b423 1347
dicarloj 17:c9afe1a7b423 1348 // Load (HL) into A, decrement HL
dicarloj 17:c9afe1a7b423 1349 void LDD_A_DHL(u8 opcode) { // 0x3a
dicarloj 17:c9afe1a7b423 1350 globalState.pc++;
dicarloj 17:c9afe1a7b423 1351 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1352 globalState.a = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1353 globalState.hl.v--;
dicarloj 17:c9afe1a7b423 1354 }
dicarloj 17:c9afe1a7b423 1355
dicarloj 17:c9afe1a7b423 1356 // Load A into (HL), decrement HL
dicarloj 17:c9afe1a7b423 1357 void LDD_DHL_A(u8 opcode) { // 0x32
dicarloj 17:c9afe1a7b423 1358 writeByte(globalState.a, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1359 globalState.hl.v--;
dicarloj 17:c9afe1a7b423 1360 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 1361 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1362 }
dicarloj 17:c9afe1a7b423 1363
dicarloj 17:c9afe1a7b423 1364 // Load (HL) into A, increment HL
dicarloj 17:c9afe1a7b423 1365 void LDI_A_DHL(u8 opcode){ // 0x2a
dicarloj 17:c9afe1a7b423 1366 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 1367 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1368 globalState.a = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 1369 globalState.hl.v++;
dicarloj 17:c9afe1a7b423 1370 }
dicarloj 17:c9afe1a7b423 1371
dicarloj 17:c9afe1a7b423 1372 // Load A into (HL), increment HL
dicarloj 17:c9afe1a7b423 1373 void LDI_DHL_A(u8 opcode) { // 0x22
dicarloj 17:c9afe1a7b423 1374 writeByte(globalState.a, globalState.hl.v);
dicarloj 17:c9afe1a7b423 1375 globalState.hl.v++;
dicarloj 17:c9afe1a7b423 1376 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 1377 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1378 }
dicarloj 17:c9afe1a7b423 1379 // page 75
dicarloj 17:c9afe1a7b423 1380
dicarloj 17:c9afe1a7b423 1381 // Store A into FF00 + n
dicarloj 17:c9afe1a7b423 1382 void LD_FF00_n_A(u8 opcode) { //0xe0
dicarloj 17:c9afe1a7b423 1383 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1384 globalState.pc++;
dicarloj 17:c9afe1a7b423 1385 writeByte(globalState.a, (u16)0xff00 + imm);
dicarloj 17:c9afe1a7b423 1386 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1387 }
dicarloj 17:c9afe1a7b423 1388
dicarloj 17:c9afe1a7b423 1389 // Load A with FF00 + n
dicarloj 17:c9afe1a7b423 1390 void LD_A_FF00_n(u8 opcode) { // 0xf0
dicarloj 17:c9afe1a7b423 1391 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1392 globalState.pc++;
dicarloj 17:c9afe1a7b423 1393 globalState.a = readByte((u16)0xff00 + imm);
dicarloj 17:c9afe1a7b423 1394 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1395 }
dicarloj 17:c9afe1a7b423 1396
dicarloj 17:c9afe1a7b423 1397 // 16-bit loads
dicarloj 17:c9afe1a7b423 1398 void LD_BC_nn(u8 opcode) { // 0x01
dicarloj 17:c9afe1a7b423 1399 u16 imm = readU16(globalState.pc + (u16)1);
dicarloj 17:c9afe1a7b423 1400 globalState.bc.v = imm;
dicarloj 17:c9afe1a7b423 1401 globalState.pc += 3;
dicarloj 17:c9afe1a7b423 1402 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1403 }
dicarloj 17:c9afe1a7b423 1404
dicarloj 17:c9afe1a7b423 1405 void LD_DE_nn(u8 opcode) { // 0x11
dicarloj 17:c9afe1a7b423 1406 u16 imm = readU16(globalState.pc + (u16)1);
dicarloj 17:c9afe1a7b423 1407 globalState.de.v = imm;
dicarloj 17:c9afe1a7b423 1408 globalState.pc += 3;
dicarloj 17:c9afe1a7b423 1409 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1410 }
dicarloj 17:c9afe1a7b423 1411
dicarloj 17:c9afe1a7b423 1412 void LD_HL_nn(u8 opcode) { // 0x21
dicarloj 17:c9afe1a7b423 1413 u16 imm = readU16(globalState.pc + (u16)1);
dicarloj 17:c9afe1a7b423 1414 globalState.hl.v = imm;
dicarloj 17:c9afe1a7b423 1415 globalState.pc += 3;
dicarloj 17:c9afe1a7b423 1416 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1417 }
dicarloj 17:c9afe1a7b423 1418
dicarloj 17:c9afe1a7b423 1419 void LD_SP_nn(u8 opcode) { // 0x31
dicarloj 17:c9afe1a7b423 1420 u16 imm = readU16(globalState.pc + (u16)1);
dicarloj 17:c9afe1a7b423 1421 globalState.sp = imm;
dicarloj 17:c9afe1a7b423 1422 globalState.pc += 3;
dicarloj 17:c9afe1a7b423 1423 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1424 }
dicarloj 17:c9afe1a7b423 1425
dicarloj 17:c9afe1a7b423 1426 void LD_SP_HL(u8 opcode) { // 0xf9
dicarloj 17:c9afe1a7b423 1427 globalState.sp = globalState.hl.v;
dicarloj 17:c9afe1a7b423 1428 globalState.pc++;
dicarloj 17:c9afe1a7b423 1429 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1430 }
dicarloj 17:c9afe1a7b423 1431
dicarloj 17:c9afe1a7b423 1432 // load effective address relative to stack pointer
dicarloj 17:c9afe1a7b423 1433 void LD_HL_SP_n(u8 opcode) { //0xf8
dicarloj 17:c9afe1a7b423 1434 globalState.pc++;
dicarloj 17:c9afe1a7b423 1435 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1436 s8 imm = readByte(globalState.pc);
dicarloj 17:c9afe1a7b423 1437 globalState.pc++;
dicarloj 17:c9afe1a7b423 1438 clearAllFlags();
dicarloj 17:c9afe1a7b423 1439 u16 result = globalState.sp + imm;
dicarloj 17:c9afe1a7b423 1440 if(((globalState.sp ^ imm ^ result) & 0x100) == 0x100) {
dicarloj 17:c9afe1a7b423 1441 setCarryFlag();
dicarloj 17:c9afe1a7b423 1442 }
dicarloj 17:c9afe1a7b423 1443 if(((globalState.sp ^ imm ^ result) & 0x10) == 0x10) {
dicarloj 17:c9afe1a7b423 1444 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 1445 }
dicarloj 17:c9afe1a7b423 1446 globalState.hl.v = result;
dicarloj 17:c9afe1a7b423 1447 }
dicarloj 17:c9afe1a7b423 1448
dicarloj 17:c9afe1a7b423 1449 // store stack pointer
dicarloj 17:c9afe1a7b423 1450 void LD_Dnn_SP(u8 opcode) { // 08
dicarloj 17:c9afe1a7b423 1451 globalState.pc++;
dicarloj 17:c9afe1a7b423 1452 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 1453 globalState.pc+=2;
dicarloj 17:c9afe1a7b423 1454 globalState.cycleCount += 20;
dicarloj 17:c9afe1a7b423 1455 writeU16(globalState.sp, addr);
dicarloj 17:c9afe1a7b423 1456 }
dicarloj 17:c9afe1a7b423 1457
dicarloj 17:c9afe1a7b423 1458 // push U16 register onto stack
dicarloj 17:c9afe1a7b423 1459 void PUSH_AF(u8 opcode) { // 0xf5
dicarloj 17:c9afe1a7b423 1460 globalState.pc++;
dicarloj 17:c9afe1a7b423 1461 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 1462 writeU16(globalState.f + (((u16)globalState.a) << 8), globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 1463 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 1464 }
dicarloj 17:c9afe1a7b423 1465
dicarloj 17:c9afe1a7b423 1466 void PUSH_BC(u8 opcode) { // 0xc5
dicarloj 17:c9afe1a7b423 1467 globalState.pc++;
dicarloj 17:c9afe1a7b423 1468 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 1469 writeU16(globalState.bc.v, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 1470 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 1471 }
dicarloj 17:c9afe1a7b423 1472
dicarloj 17:c9afe1a7b423 1473 void PUSH_DE(u8 opcode) { // 0xd5
dicarloj 17:c9afe1a7b423 1474 globalState.pc++;
dicarloj 17:c9afe1a7b423 1475 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 1476 writeU16(globalState.de.v, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 1477 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 1478 }
dicarloj 17:c9afe1a7b423 1479
dicarloj 17:c9afe1a7b423 1480 void PUSH_HL(u8 opcode) { // 0xe5
dicarloj 17:c9afe1a7b423 1481 globalState.pc++;
dicarloj 17:c9afe1a7b423 1482 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 1483 writeU16(globalState.hl.v, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 1484 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 1485 }
dicarloj 17:c9afe1a7b423 1486
dicarloj 17:c9afe1a7b423 1487 // POP U16 register from stack
dicarloj 17:c9afe1a7b423 1488 void POP_AF(u8 opcode) { // 0xf1
dicarloj 17:c9afe1a7b423 1489 globalState.pc++;
dicarloj 17:c9afe1a7b423 1490 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1491 u16 v = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 1492 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 1493 globalState.a = (u8)(v >> 8);
dicarloj 17:c9afe1a7b423 1494 globalState.f = (u8)(v & 0xf0);
dicarloj 17:c9afe1a7b423 1495 }
dicarloj 17:c9afe1a7b423 1496
dicarloj 17:c9afe1a7b423 1497 void POP_BC(u8 opcode) { // 0xc1
dicarloj 17:c9afe1a7b423 1498 globalState.pc++;
dicarloj 17:c9afe1a7b423 1499 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1500 u16 v = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 1501 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 1502 globalState.bc.v = v;
dicarloj 17:c9afe1a7b423 1503 }
dicarloj 17:c9afe1a7b423 1504
dicarloj 17:c9afe1a7b423 1505 void POP_DE(u8 opcode) { // 0xd1
dicarloj 17:c9afe1a7b423 1506 globalState.pc++;
dicarloj 17:c9afe1a7b423 1507 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1508 u16 v = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 1509 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 1510 globalState.de.v = v;
dicarloj 17:c9afe1a7b423 1511 }
dicarloj 17:c9afe1a7b423 1512
dicarloj 17:c9afe1a7b423 1513 void POP_HL(u8 opcode) { // 0xe1
dicarloj 17:c9afe1a7b423 1514 globalState.pc++;
dicarloj 17:c9afe1a7b423 1515 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 1516 u16 v = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 1517 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 1518 globalState.hl.v = v;
dicarloj 17:c9afe1a7b423 1519 }
dicarloj 17:c9afe1a7b423 1520
dicarloj 17:c9afe1a7b423 1521 void addToA(u8 number) {
dicarloj 17:c9afe1a7b423 1522 int result = globalState.a + number;
dicarloj 17:c9afe1a7b423 1523 int carryBits = globalState.a ^ number ^ result;
dicarloj 17:c9afe1a7b423 1524 globalState.a = (u8)result;
dicarloj 17:c9afe1a7b423 1525 clearAllFlags();
dicarloj 17:c9afe1a7b423 1526
dicarloj 17:c9afe1a7b423 1527 if((u8)result == 0){
dicarloj 17:c9afe1a7b423 1528 setZeroFlag();
dicarloj 17:c9afe1a7b423 1529 }
dicarloj 17:c9afe1a7b423 1530
dicarloj 17:c9afe1a7b423 1531 if((carryBits & 0x100) != 0) {
dicarloj 17:c9afe1a7b423 1532 setCarryFlag();
dicarloj 17:c9afe1a7b423 1533 }
dicarloj 17:c9afe1a7b423 1534 if((carryBits & 0x10) != 0) {
dicarloj 17:c9afe1a7b423 1535 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 1536 }
dicarloj 17:c9afe1a7b423 1537 }
dicarloj 17:c9afe1a7b423 1538
dicarloj 17:c9afe1a7b423 1539 // ADD Opcodes
dicarloj 17:c9afe1a7b423 1540 void ADD_A(u8 opcode) { // 0x87
dicarloj 17:c9afe1a7b423 1541 globalState.pc++;
dicarloj 17:c9afe1a7b423 1542 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1543 addToA(globalState.a);
dicarloj 17:c9afe1a7b423 1544 }
dicarloj 17:c9afe1a7b423 1545
dicarloj 17:c9afe1a7b423 1546 void ADD_B(u8 opcode) { // 0x80
dicarloj 17:c9afe1a7b423 1547 globalState.pc++;
dicarloj 17:c9afe1a7b423 1548 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1549 addToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1550 }
dicarloj 17:c9afe1a7b423 1551
dicarloj 17:c9afe1a7b423 1552 void ADD_C(u8 opcode) { // 0x81
dicarloj 17:c9afe1a7b423 1553 globalState.pc++;
dicarloj 17:c9afe1a7b423 1554 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1555 addToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1556 }
dicarloj 17:c9afe1a7b423 1557
dicarloj 17:c9afe1a7b423 1558 void ADD_D(u8 opcode) { // 0x82
dicarloj 17:c9afe1a7b423 1559 globalState.pc++;
dicarloj 17:c9afe1a7b423 1560 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1561 addToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1562 }
dicarloj 17:c9afe1a7b423 1563
dicarloj 17:c9afe1a7b423 1564 void ADD_E(u8 opcode) { // 0x83
dicarloj 17:c9afe1a7b423 1565 globalState.pc++;
dicarloj 17:c9afe1a7b423 1566 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1567 addToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1568 }
dicarloj 17:c9afe1a7b423 1569
dicarloj 17:c9afe1a7b423 1570 void ADD_H(u8 opcode) { // 0x84
dicarloj 17:c9afe1a7b423 1571 globalState.pc++;
dicarloj 17:c9afe1a7b423 1572 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1573 addToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1574 }
dicarloj 17:c9afe1a7b423 1575
dicarloj 17:c9afe1a7b423 1576 void ADD_L(u8 opcode) { // 0x85
dicarloj 17:c9afe1a7b423 1577 globalState.pc++;
dicarloj 17:c9afe1a7b423 1578 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1579 addToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1580 }
dicarloj 17:c9afe1a7b423 1581
dicarloj 17:c9afe1a7b423 1582 void ADD_DHL(u8 opcode) { // 0x86
dicarloj 17:c9afe1a7b423 1583 globalState.pc++;
dicarloj 17:c9afe1a7b423 1584 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1585 addToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 1586 }
dicarloj 17:c9afe1a7b423 1587
dicarloj 17:c9afe1a7b423 1588 void ADD_n(u8 opcode) { // 0xC6
dicarloj 17:c9afe1a7b423 1589 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1590 globalState.pc++;
dicarloj 17:c9afe1a7b423 1591 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1592 addToA(imm);
dicarloj 17:c9afe1a7b423 1593 }
dicarloj 17:c9afe1a7b423 1594
dicarloj 17:c9afe1a7b423 1595 void adcToA(u8 number) {
dicarloj 17:c9afe1a7b423 1596 int carry = getCarryFlag() ? 1 : 0;
dicarloj 17:c9afe1a7b423 1597 int result = globalState.a + number + carry;
dicarloj 17:c9afe1a7b423 1598 clearAllFlags();
dicarloj 17:c9afe1a7b423 1599 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 1600 setZeroFlag();
dicarloj 17:c9afe1a7b423 1601 }
dicarloj 17:c9afe1a7b423 1602 if(result > 0xff) {
dicarloj 17:c9afe1a7b423 1603 setCarryFlag();
dicarloj 17:c9afe1a7b423 1604 }
dicarloj 17:c9afe1a7b423 1605 if(((globalState.a & 0xf) + (number & 0xf) + carry) > 0xf) {
dicarloj 17:c9afe1a7b423 1606 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 1607 }
dicarloj 17:c9afe1a7b423 1608 globalState.a = (u8) result;
dicarloj 17:c9afe1a7b423 1609 }
dicarloj 17:c9afe1a7b423 1610
dicarloj 17:c9afe1a7b423 1611 // ADC Opcodes
dicarloj 17:c9afe1a7b423 1612 void ADC_A(u8 opcode) { // 0x8f
dicarloj 17:c9afe1a7b423 1613 globalState.pc++;
dicarloj 17:c9afe1a7b423 1614 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1615 adcToA(globalState.a);
dicarloj 17:c9afe1a7b423 1616 }
dicarloj 17:c9afe1a7b423 1617
dicarloj 17:c9afe1a7b423 1618 void ADC_B(u8 opcode) { // 0x88
dicarloj 17:c9afe1a7b423 1619 globalState.pc++;
dicarloj 17:c9afe1a7b423 1620 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1621 adcToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1622 }
dicarloj 17:c9afe1a7b423 1623
dicarloj 17:c9afe1a7b423 1624 void ADC_C(u8 opcode) { // 0x89
dicarloj 17:c9afe1a7b423 1625 globalState.pc++;
dicarloj 17:c9afe1a7b423 1626 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1627 adcToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1628 }
dicarloj 17:c9afe1a7b423 1629
dicarloj 17:c9afe1a7b423 1630 void ADC_D(u8 opcode) { // 0x8a
dicarloj 17:c9afe1a7b423 1631 globalState.pc++;
dicarloj 17:c9afe1a7b423 1632 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1633 adcToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1634 }
dicarloj 17:c9afe1a7b423 1635
dicarloj 17:c9afe1a7b423 1636 void ADC_E(u8 opcode) { // 0x8b
dicarloj 17:c9afe1a7b423 1637 globalState.pc++;
dicarloj 17:c9afe1a7b423 1638 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1639 adcToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1640 }
dicarloj 17:c9afe1a7b423 1641
dicarloj 17:c9afe1a7b423 1642 void ADC_H(u8 opcode) { // 0x8c
dicarloj 17:c9afe1a7b423 1643 globalState.pc++;
dicarloj 17:c9afe1a7b423 1644 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1645 adcToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1646 }
dicarloj 17:c9afe1a7b423 1647
dicarloj 17:c9afe1a7b423 1648 void ADC_L(u8 opcode) { // 0x8d
dicarloj 17:c9afe1a7b423 1649 globalState.pc++;
dicarloj 17:c9afe1a7b423 1650 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1651 adcToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1652 }
dicarloj 17:c9afe1a7b423 1653
dicarloj 17:c9afe1a7b423 1654 void ADC_DHL(u8 opcode) { // 0x8e
dicarloj 17:c9afe1a7b423 1655 globalState.pc++;
dicarloj 17:c9afe1a7b423 1656 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1657 adcToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 1658 }
dicarloj 17:c9afe1a7b423 1659
dicarloj 17:c9afe1a7b423 1660 void ADC_n(u8 opcode) { // 0xCe
dicarloj 17:c9afe1a7b423 1661 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1662 globalState.pc++;
dicarloj 17:c9afe1a7b423 1663 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1664 adcToA(imm);
dicarloj 17:c9afe1a7b423 1665 }
dicarloj 17:c9afe1a7b423 1666
dicarloj 17:c9afe1a7b423 1667 void subToA(u8 number) {
dicarloj 17:c9afe1a7b423 1668 int result = globalState.a - number;
dicarloj 17:c9afe1a7b423 1669 int carryBits = globalState.a ^ number ^ result;
dicarloj 17:c9afe1a7b423 1670 globalState.a = (u8)result;
dicarloj 17:c9afe1a7b423 1671 clearAllFlags();
dicarloj 17:c9afe1a7b423 1672 setSubtractFlag();
dicarloj 17:c9afe1a7b423 1673 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 1674 setZeroFlag();
dicarloj 17:c9afe1a7b423 1675 }
dicarloj 17:c9afe1a7b423 1676 if((carryBits & 0x100) != 0) {
dicarloj 17:c9afe1a7b423 1677 setCarryFlag();
dicarloj 17:c9afe1a7b423 1678 }
dicarloj 17:c9afe1a7b423 1679 if((carryBits & 0x10) != 0) {
dicarloj 17:c9afe1a7b423 1680 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 1681 }
dicarloj 17:c9afe1a7b423 1682 }
dicarloj 17:c9afe1a7b423 1683
dicarloj 17:c9afe1a7b423 1684 // SUB Opcodes
dicarloj 17:c9afe1a7b423 1685 void SUB_A(u8 opcode) { // 0x97
dicarloj 17:c9afe1a7b423 1686 globalState.pc++;
dicarloj 17:c9afe1a7b423 1687 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1688 subToA(globalState.a);
dicarloj 17:c9afe1a7b423 1689 }
dicarloj 17:c9afe1a7b423 1690
dicarloj 17:c9afe1a7b423 1691 void SUB_B(u8 opcode) { // 0x90
dicarloj 17:c9afe1a7b423 1692 globalState.pc++;
dicarloj 17:c9afe1a7b423 1693 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1694 subToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1695 }
dicarloj 17:c9afe1a7b423 1696
dicarloj 17:c9afe1a7b423 1697 void SUB_C(u8 opcode) { // 0x91
dicarloj 17:c9afe1a7b423 1698 globalState.pc++;
dicarloj 17:c9afe1a7b423 1699 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1700 subToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1701 }
dicarloj 17:c9afe1a7b423 1702
dicarloj 17:c9afe1a7b423 1703 void SUB_D(u8 opcode) { // 0x92
dicarloj 17:c9afe1a7b423 1704 globalState.pc++;
dicarloj 17:c9afe1a7b423 1705 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1706 subToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1707 }
dicarloj 17:c9afe1a7b423 1708
dicarloj 17:c9afe1a7b423 1709 void SUB_E(u8 opcode) { // 0x93
dicarloj 17:c9afe1a7b423 1710 globalState.pc++;
dicarloj 17:c9afe1a7b423 1711 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1712 subToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1713 }
dicarloj 17:c9afe1a7b423 1714
dicarloj 17:c9afe1a7b423 1715 void SUB_H(u8 opcode) { // 0x94
dicarloj 17:c9afe1a7b423 1716 globalState.pc++;
dicarloj 17:c9afe1a7b423 1717 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1718 subToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1719 }
dicarloj 17:c9afe1a7b423 1720
dicarloj 17:c9afe1a7b423 1721 void SUB_L(u8 opcode) { // 0x95
dicarloj 17:c9afe1a7b423 1722 globalState.pc++;
dicarloj 17:c9afe1a7b423 1723 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1724 subToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1725 }
dicarloj 17:c9afe1a7b423 1726
dicarloj 17:c9afe1a7b423 1727 void SUB_DHL(u8 opcode) { // 0x96
dicarloj 17:c9afe1a7b423 1728 globalState.pc++;
dicarloj 17:c9afe1a7b423 1729 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1730 subToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 1731 }
dicarloj 17:c9afe1a7b423 1732
dicarloj 17:c9afe1a7b423 1733 void SUB_n(u8 opcode) { // 0xD6
dicarloj 17:c9afe1a7b423 1734 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1735 globalState.pc++;
dicarloj 17:c9afe1a7b423 1736 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1737 subToA(imm);
dicarloj 17:c9afe1a7b423 1738 }
dicarloj 17:c9afe1a7b423 1739
dicarloj 17:c9afe1a7b423 1740 void sbcToA(u8 number) {
dicarloj 17:c9afe1a7b423 1741 int carry = getCarryFlag() ? 1 : 0;
dicarloj 17:c9afe1a7b423 1742 int result = globalState.a - number - carry;
dicarloj 17:c9afe1a7b423 1743 clearAllFlags();
dicarloj 17:c9afe1a7b423 1744 setSubtractFlag();
dicarloj 17:c9afe1a7b423 1745 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 1746 setZeroFlag();
dicarloj 17:c9afe1a7b423 1747 }
dicarloj 17:c9afe1a7b423 1748 if(result < 0) {
dicarloj 17:c9afe1a7b423 1749 setCarryFlag();
dicarloj 17:c9afe1a7b423 1750 }
dicarloj 17:c9afe1a7b423 1751 if(((globalState.a & 0xf) - (number & 0xf) - carry) < 0) {
dicarloj 17:c9afe1a7b423 1752 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 1753 }
dicarloj 17:c9afe1a7b423 1754 globalState.a = (u8)result;
dicarloj 17:c9afe1a7b423 1755 }
dicarloj 17:c9afe1a7b423 1756
dicarloj 17:c9afe1a7b423 1757 // SBC Opcodes
dicarloj 17:c9afe1a7b423 1758 void SBC_A(u8 opcode) { // 0x9f
dicarloj 17:c9afe1a7b423 1759 globalState.pc++;
dicarloj 17:c9afe1a7b423 1760 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1761 sbcToA(globalState.a);
dicarloj 17:c9afe1a7b423 1762 }
dicarloj 17:c9afe1a7b423 1763
dicarloj 17:c9afe1a7b423 1764 void SBC_B(u8 opcode) { // 0x98
dicarloj 17:c9afe1a7b423 1765 globalState.pc++;
dicarloj 17:c9afe1a7b423 1766 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1767 sbcToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1768 }
dicarloj 17:c9afe1a7b423 1769
dicarloj 17:c9afe1a7b423 1770 void SBC_C(u8 opcode) { // 0x99
dicarloj 17:c9afe1a7b423 1771 globalState.pc++;
dicarloj 17:c9afe1a7b423 1772 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1773 sbcToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1774 }
dicarloj 17:c9afe1a7b423 1775
dicarloj 17:c9afe1a7b423 1776 void SBC_D(u8 opcode) { // 0x9a
dicarloj 17:c9afe1a7b423 1777 globalState.pc++;
dicarloj 17:c9afe1a7b423 1778 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1779 sbcToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1780 }
dicarloj 17:c9afe1a7b423 1781
dicarloj 17:c9afe1a7b423 1782 void SBC_E(u8 opcode) { // 0x9b
dicarloj 17:c9afe1a7b423 1783 globalState.pc++;
dicarloj 17:c9afe1a7b423 1784 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1785 sbcToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1786 }
dicarloj 17:c9afe1a7b423 1787
dicarloj 17:c9afe1a7b423 1788 void SBC_H(u8 opcode) { // 0x9c
dicarloj 17:c9afe1a7b423 1789 globalState.pc++;
dicarloj 17:c9afe1a7b423 1790 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1791 sbcToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1792 }
dicarloj 17:c9afe1a7b423 1793
dicarloj 17:c9afe1a7b423 1794 void SBC_L(u8 opcode) { // 0x9d
dicarloj 17:c9afe1a7b423 1795 globalState.pc++;
dicarloj 17:c9afe1a7b423 1796 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1797 sbcToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1798 }
dicarloj 17:c9afe1a7b423 1799
dicarloj 17:c9afe1a7b423 1800 void SBC_DHL(u8 opcode) { // 0x9e
dicarloj 17:c9afe1a7b423 1801 globalState.pc++;
dicarloj 17:c9afe1a7b423 1802 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1803 sbcToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 1804 }
dicarloj 17:c9afe1a7b423 1805
dicarloj 17:c9afe1a7b423 1806 void SBC_n(u8 opcode) { // 0xDe
dicarloj 17:c9afe1a7b423 1807 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1808 globalState.pc++;
dicarloj 17:c9afe1a7b423 1809 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1810 sbcToA(imm);
dicarloj 17:c9afe1a7b423 1811 }
dicarloj 17:c9afe1a7b423 1812
dicarloj 17:c9afe1a7b423 1813 void andtoA(u8 number) {
dicarloj 17:c9afe1a7b423 1814 u8 result = globalState.a & number;
dicarloj 17:c9afe1a7b423 1815 globalState.a = result;
dicarloj 17:c9afe1a7b423 1816 clearAllFlags();
dicarloj 17:c9afe1a7b423 1817 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 1818 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 1819 setZeroFlag();
dicarloj 17:c9afe1a7b423 1820 }
dicarloj 17:c9afe1a7b423 1821 }
dicarloj 17:c9afe1a7b423 1822
dicarloj 17:c9afe1a7b423 1823 // AND Opcodes
dicarloj 17:c9afe1a7b423 1824 void AND_A(u8 opcode) { // 0xa7
dicarloj 17:c9afe1a7b423 1825 globalState.pc++;
dicarloj 17:c9afe1a7b423 1826 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1827 andtoA(globalState.a);
dicarloj 17:c9afe1a7b423 1828 }
dicarloj 17:c9afe1a7b423 1829
dicarloj 17:c9afe1a7b423 1830 void AND_B(u8 opcode) { // 0xa0
dicarloj 17:c9afe1a7b423 1831 globalState.pc++;
dicarloj 17:c9afe1a7b423 1832 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1833 andtoA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1834 }
dicarloj 17:c9afe1a7b423 1835
dicarloj 17:c9afe1a7b423 1836 void AND_C(u8 opcode) { // 0xa1
dicarloj 17:c9afe1a7b423 1837 globalState.pc++;
dicarloj 17:c9afe1a7b423 1838 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1839 andtoA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1840 }
dicarloj 17:c9afe1a7b423 1841
dicarloj 17:c9afe1a7b423 1842 void AND_D(u8 opcode) { // 0xa2
dicarloj 17:c9afe1a7b423 1843 globalState.pc++;
dicarloj 17:c9afe1a7b423 1844 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1845 andtoA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1846 }
dicarloj 17:c9afe1a7b423 1847
dicarloj 17:c9afe1a7b423 1848 void AND_E(u8 opcode) { // 0xa3
dicarloj 17:c9afe1a7b423 1849 globalState.pc++;
dicarloj 17:c9afe1a7b423 1850 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1851 andtoA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1852 }
dicarloj 17:c9afe1a7b423 1853
dicarloj 17:c9afe1a7b423 1854 void AND_H(u8 opcode) { // 0xa4
dicarloj 17:c9afe1a7b423 1855 globalState.pc++;
dicarloj 17:c9afe1a7b423 1856 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1857 andtoA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1858 }
dicarloj 17:c9afe1a7b423 1859
dicarloj 17:c9afe1a7b423 1860 void AND_L(u8 opcode) { // 0xa5
dicarloj 17:c9afe1a7b423 1861 globalState.pc++;
dicarloj 17:c9afe1a7b423 1862 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1863 andtoA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1864 }
dicarloj 17:c9afe1a7b423 1865
dicarloj 17:c9afe1a7b423 1866 void AND_DHL(u8 opcode) { // 0xa6
dicarloj 17:c9afe1a7b423 1867 globalState.pc++;
dicarloj 17:c9afe1a7b423 1868 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1869 andtoA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 1870 }
dicarloj 17:c9afe1a7b423 1871
dicarloj 17:c9afe1a7b423 1872 void AND_n(u8 opcode) { // 0xe6
dicarloj 17:c9afe1a7b423 1873 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1874 globalState.pc++;
dicarloj 17:c9afe1a7b423 1875 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1876 andtoA(imm);
dicarloj 17:c9afe1a7b423 1877 }
dicarloj 17:c9afe1a7b423 1878
dicarloj 17:c9afe1a7b423 1879 void xorToA(u8 number) {
dicarloj 17:c9afe1a7b423 1880 u8 result = globalState.a ^ number;
dicarloj 17:c9afe1a7b423 1881 globalState.a = result;
dicarloj 17:c9afe1a7b423 1882 clearAllFlags();
dicarloj 17:c9afe1a7b423 1883 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 1884 setZeroFlag();
dicarloj 17:c9afe1a7b423 1885 }
dicarloj 17:c9afe1a7b423 1886 }
dicarloj 17:c9afe1a7b423 1887
dicarloj 17:c9afe1a7b423 1888 // XOR Opcodes
dicarloj 17:c9afe1a7b423 1889 void XOR_A(u8 opcode) { // 0xaf
dicarloj 17:c9afe1a7b423 1890 globalState.pc++;
dicarloj 17:c9afe1a7b423 1891 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1892 xorToA(globalState.a);
dicarloj 17:c9afe1a7b423 1893 }
dicarloj 17:c9afe1a7b423 1894
dicarloj 17:c9afe1a7b423 1895 void XOR_B(u8 opcode) { // 0xa8
dicarloj 17:c9afe1a7b423 1896 globalState.pc++;
dicarloj 17:c9afe1a7b423 1897 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1898 xorToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1899 }
dicarloj 17:c9afe1a7b423 1900
dicarloj 17:c9afe1a7b423 1901 void XOR_C(u8 opcode) { // 0xa9
dicarloj 17:c9afe1a7b423 1902 globalState.pc++;
dicarloj 17:c9afe1a7b423 1903 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1904 xorToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1905 }
dicarloj 17:c9afe1a7b423 1906
dicarloj 17:c9afe1a7b423 1907 void XOR_D(u8 opcode) { // 0xaa
dicarloj 17:c9afe1a7b423 1908 globalState.pc++;
dicarloj 17:c9afe1a7b423 1909 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1910 xorToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1911 }
dicarloj 17:c9afe1a7b423 1912
dicarloj 17:c9afe1a7b423 1913 void XOR_E(u8 opcode) { // 0xab
dicarloj 17:c9afe1a7b423 1914 globalState.pc++;
dicarloj 17:c9afe1a7b423 1915 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1916 xorToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1917 }
dicarloj 17:c9afe1a7b423 1918
dicarloj 17:c9afe1a7b423 1919 void XOR_H(u8 opcode) { // 0xac
dicarloj 17:c9afe1a7b423 1920 globalState.pc++;
dicarloj 17:c9afe1a7b423 1921 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1922 xorToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1923 }
dicarloj 17:c9afe1a7b423 1924
dicarloj 17:c9afe1a7b423 1925 void XOR_L(u8 opcode) { // 0xad
dicarloj 17:c9afe1a7b423 1926 globalState.pc++;
dicarloj 17:c9afe1a7b423 1927 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1928 xorToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1929 }
dicarloj 17:c9afe1a7b423 1930
dicarloj 17:c9afe1a7b423 1931 void XOR_DHL(u8 opcode) { // 0xae
dicarloj 17:c9afe1a7b423 1932 globalState.pc++;
dicarloj 17:c9afe1a7b423 1933 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1934 xorToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 1935 }
dicarloj 17:c9afe1a7b423 1936
dicarloj 17:c9afe1a7b423 1937 void XOR_n(u8 opcode) { // 0xee
dicarloj 17:c9afe1a7b423 1938 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 1939 globalState.pc++;
dicarloj 17:c9afe1a7b423 1940 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1941 xorToA(imm);
dicarloj 17:c9afe1a7b423 1942 }
dicarloj 17:c9afe1a7b423 1943
dicarloj 17:c9afe1a7b423 1944 void orToA(u8 number) {
dicarloj 17:c9afe1a7b423 1945 u8 result = globalState.a | number;
dicarloj 17:c9afe1a7b423 1946 globalState.a = result;
dicarloj 17:c9afe1a7b423 1947 clearAllFlags();
dicarloj 17:c9afe1a7b423 1948 if((u8)result == 0) {
dicarloj 17:c9afe1a7b423 1949 setZeroFlag();
dicarloj 17:c9afe1a7b423 1950 }
dicarloj 17:c9afe1a7b423 1951 }
dicarloj 17:c9afe1a7b423 1952
dicarloj 17:c9afe1a7b423 1953 // OR Opcodes
dicarloj 17:c9afe1a7b423 1954 void OR_A(u8 opcode) { // 0xb7
dicarloj 17:c9afe1a7b423 1955 globalState.pc++;
dicarloj 17:c9afe1a7b423 1956 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1957 orToA(globalState.a);
dicarloj 17:c9afe1a7b423 1958 }
dicarloj 17:c9afe1a7b423 1959
dicarloj 17:c9afe1a7b423 1960 void OR_B(u8 opcode) { // 0xb0
dicarloj 17:c9afe1a7b423 1961 globalState.pc++;
dicarloj 17:c9afe1a7b423 1962 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1963 orToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 1964 }
dicarloj 17:c9afe1a7b423 1965
dicarloj 17:c9afe1a7b423 1966 void OR_C(u8 opcode) { // 0xb1
dicarloj 17:c9afe1a7b423 1967 globalState.pc++;
dicarloj 17:c9afe1a7b423 1968 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1969 orToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 1970 }
dicarloj 17:c9afe1a7b423 1971
dicarloj 17:c9afe1a7b423 1972 void OR_D(u8 opcode) { // 0xb2
dicarloj 17:c9afe1a7b423 1973 globalState.pc++;
dicarloj 17:c9afe1a7b423 1974 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1975 orToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 1976 }
dicarloj 17:c9afe1a7b423 1977
dicarloj 17:c9afe1a7b423 1978 void OR_E(u8 opcode) { // 0xb3
dicarloj 17:c9afe1a7b423 1979 globalState.pc++;
dicarloj 17:c9afe1a7b423 1980 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1981 orToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 1982 }
dicarloj 17:c9afe1a7b423 1983
dicarloj 17:c9afe1a7b423 1984 void OR_H(u8 opcode) { // 0xb4
dicarloj 17:c9afe1a7b423 1985 globalState.pc++;
dicarloj 17:c9afe1a7b423 1986 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1987 orToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 1988 }
dicarloj 17:c9afe1a7b423 1989
dicarloj 17:c9afe1a7b423 1990 void OR_L(u8 opcode) { // 0xb5
dicarloj 17:c9afe1a7b423 1991 globalState.pc++;
dicarloj 17:c9afe1a7b423 1992 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 1993 orToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 1994 }
dicarloj 17:c9afe1a7b423 1995
dicarloj 17:c9afe1a7b423 1996 void OR_DHL(u8 opcode) { // 0xb6
dicarloj 17:c9afe1a7b423 1997 globalState.pc++;
dicarloj 17:c9afe1a7b423 1998 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 1999 orToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 2000 }
dicarloj 17:c9afe1a7b423 2001
dicarloj 17:c9afe1a7b423 2002 void OR_n(u8 opcode) { // 0xf6
dicarloj 17:c9afe1a7b423 2003 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2004 globalState.pc++;
dicarloj 17:c9afe1a7b423 2005 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2006 orToA(imm);
dicarloj 17:c9afe1a7b423 2007 }
dicarloj 17:c9afe1a7b423 2008
dicarloj 17:c9afe1a7b423 2009 void cpToA(u8 number) {
dicarloj 17:c9afe1a7b423 2010 clearAllFlags();
dicarloj 17:c9afe1a7b423 2011 setSubtractFlag();
dicarloj 17:c9afe1a7b423 2012 if(globalState.a < number) {
dicarloj 17:c9afe1a7b423 2013 setCarryFlag();
dicarloj 17:c9afe1a7b423 2014 }
dicarloj 17:c9afe1a7b423 2015 if(globalState.a == number) {
dicarloj 17:c9afe1a7b423 2016 setZeroFlag();
dicarloj 17:c9afe1a7b423 2017 }
dicarloj 17:c9afe1a7b423 2018 if(((globalState.a - number) & 0xf) > (globalState.a & 0xf)) {
dicarloj 17:c9afe1a7b423 2019 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2020 }
dicarloj 17:c9afe1a7b423 2021 }
dicarloj 17:c9afe1a7b423 2022
dicarloj 17:c9afe1a7b423 2023 // CP Opcodes
dicarloj 17:c9afe1a7b423 2024 void CP_A(u8 opcode) { // 0xbf
dicarloj 17:c9afe1a7b423 2025 globalState.pc++;
dicarloj 17:c9afe1a7b423 2026 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2027 cpToA(globalState.a);
dicarloj 17:c9afe1a7b423 2028 }
dicarloj 17:c9afe1a7b423 2029
dicarloj 17:c9afe1a7b423 2030 void CP_B(u8 opcode) { // 0xb8
dicarloj 17:c9afe1a7b423 2031 globalState.pc++;
dicarloj 17:c9afe1a7b423 2032 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2033 cpToA(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 2034 }
dicarloj 17:c9afe1a7b423 2035
dicarloj 17:c9afe1a7b423 2036 void CP_C(u8 opcode) { // 0xb9
dicarloj 17:c9afe1a7b423 2037 globalState.pc++;
dicarloj 17:c9afe1a7b423 2038 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2039 cpToA(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 2040 }
dicarloj 17:c9afe1a7b423 2041
dicarloj 17:c9afe1a7b423 2042 void CP_D(u8 opcode) { // 0xba
dicarloj 17:c9afe1a7b423 2043 globalState.pc++;
dicarloj 17:c9afe1a7b423 2044 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2045 cpToA(globalState.de.hi);
dicarloj 17:c9afe1a7b423 2046 }
dicarloj 17:c9afe1a7b423 2047
dicarloj 17:c9afe1a7b423 2048 void CP_E(u8 opcode) { // 0xbb
dicarloj 17:c9afe1a7b423 2049 globalState.pc++;
dicarloj 17:c9afe1a7b423 2050 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2051 cpToA(globalState.de.lo);
dicarloj 17:c9afe1a7b423 2052 }
dicarloj 17:c9afe1a7b423 2053
dicarloj 17:c9afe1a7b423 2054 void CP_H(u8 opcode) { // 0xbc
dicarloj 17:c9afe1a7b423 2055 globalState.pc++;
dicarloj 17:c9afe1a7b423 2056 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2057 cpToA(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 2058 }
dicarloj 17:c9afe1a7b423 2059
dicarloj 17:c9afe1a7b423 2060 void CP_L(u8 opcode) { // 0xbd
dicarloj 17:c9afe1a7b423 2061 globalState.pc++;
dicarloj 17:c9afe1a7b423 2062 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2063 cpToA(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 2064 }
dicarloj 17:c9afe1a7b423 2065
dicarloj 17:c9afe1a7b423 2066 void CP_DHL(u8 opcode) { // 0xbe
dicarloj 17:c9afe1a7b423 2067 globalState.pc++;
dicarloj 17:c9afe1a7b423 2068 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2069 cpToA(readByte(globalState.hl.v));
dicarloj 17:c9afe1a7b423 2070 }
dicarloj 17:c9afe1a7b423 2071
dicarloj 17:c9afe1a7b423 2072 void CP_n(u8 opcode) { // 0xfe
dicarloj 17:c9afe1a7b423 2073 u8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2074 globalState.pc++;
dicarloj 17:c9afe1a7b423 2075 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2076 cpToA(imm);
dicarloj 17:c9afe1a7b423 2077 }
dicarloj 17:c9afe1a7b423 2078
dicarloj 17:c9afe1a7b423 2079 void DecodeCB(u8 opcode) {
dicarloj 17:c9afe1a7b423 2080 globalState.pc += 1;
dicarloj 17:c9afe1a7b423 2081 u8 newOpcode = readByte(globalState.pc);
dicarloj 17:c9afe1a7b423 2082 opcode_cbs[newOpcode](newOpcode);
dicarloj 17:c9afe1a7b423 2083 }
dicarloj 17:c9afe1a7b423 2084
dicarloj 17:c9afe1a7b423 2085 u8 increment(u8 in) {
dicarloj 17:c9afe1a7b423 2086 u8 result = in + (u8)1;
dicarloj 17:c9afe1a7b423 2087 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2088 clearAllFlags();
dicarloj 17:c9afe1a7b423 2089 setCarryFlag();
dicarloj 17:c9afe1a7b423 2090 } else {
dicarloj 17:c9afe1a7b423 2091 clearAllFlags();
dicarloj 17:c9afe1a7b423 2092 }
dicarloj 17:c9afe1a7b423 2093 if(result == 0) {
dicarloj 17:c9afe1a7b423 2094 setZeroFlag();
dicarloj 17:c9afe1a7b423 2095 }
dicarloj 17:c9afe1a7b423 2096 if((result & 0x0f) == 0x00) {
dicarloj 17:c9afe1a7b423 2097 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2098 }
dicarloj 17:c9afe1a7b423 2099 return result;
dicarloj 17:c9afe1a7b423 2100 }
dicarloj 17:c9afe1a7b423 2101
dicarloj 17:c9afe1a7b423 2102 void INC_A(u8 opcode) { // 0x3c
dicarloj 17:c9afe1a7b423 2103 globalState.a = increment(globalState.a);
dicarloj 17:c9afe1a7b423 2104 globalState.pc++;
dicarloj 17:c9afe1a7b423 2105 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2106 }
dicarloj 17:c9afe1a7b423 2107
dicarloj 17:c9afe1a7b423 2108 void INC_B(u8 opcode) { // 0x04
dicarloj 17:c9afe1a7b423 2109 globalState.bc.hi = increment(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 2110 globalState.pc++;
dicarloj 17:c9afe1a7b423 2111 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2112 }
dicarloj 17:c9afe1a7b423 2113
dicarloj 17:c9afe1a7b423 2114 void INC_C(u8 opcode) { // 0x0c
dicarloj 17:c9afe1a7b423 2115 globalState.bc.lo = increment(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 2116 globalState.pc++;
dicarloj 17:c9afe1a7b423 2117 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2118 }
dicarloj 17:c9afe1a7b423 2119
dicarloj 17:c9afe1a7b423 2120 void INC_D(u8 opcode) { // 0x14
dicarloj 17:c9afe1a7b423 2121 globalState.de.hi = increment(globalState.de.hi);
dicarloj 17:c9afe1a7b423 2122 globalState.pc++;
dicarloj 17:c9afe1a7b423 2123 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2124 }
dicarloj 17:c9afe1a7b423 2125
dicarloj 17:c9afe1a7b423 2126 void INC_E(u8 opcode) { // 0x1c
dicarloj 17:c9afe1a7b423 2127 globalState.de.lo = increment(globalState.de.lo);
dicarloj 17:c9afe1a7b423 2128 globalState.pc++;
dicarloj 17:c9afe1a7b423 2129 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2130 }
dicarloj 17:c9afe1a7b423 2131
dicarloj 17:c9afe1a7b423 2132 void INC_H(u8 opcode) { // 0x24
dicarloj 17:c9afe1a7b423 2133 globalState.hl.hi = increment(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 2134 globalState.pc++;
dicarloj 17:c9afe1a7b423 2135 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2136 }
dicarloj 17:c9afe1a7b423 2137
dicarloj 17:c9afe1a7b423 2138 void INC_L(u8 opcode) { // 0x2c
dicarloj 17:c9afe1a7b423 2139 globalState.hl.lo = increment(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 2140 globalState.pc++;
dicarloj 17:c9afe1a7b423 2141 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2142 }
dicarloj 17:c9afe1a7b423 2143
dicarloj 17:c9afe1a7b423 2144 void INC_DHL(u8 opcode) { // 0x34
dicarloj 17:c9afe1a7b423 2145 // todo this one is hard.
dicarloj 17:c9afe1a7b423 2146 u8 v = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 2147 writeByte(increment(v), globalState.hl.v);
dicarloj 17:c9afe1a7b423 2148 globalState.pc++;
dicarloj 17:c9afe1a7b423 2149 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2150 }
dicarloj 17:c9afe1a7b423 2151
dicarloj 17:c9afe1a7b423 2152 u8 decrement(u8 in) {
dicarloj 17:c9afe1a7b423 2153 u8 result = in - (u8)1;
dicarloj 17:c9afe1a7b423 2154 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2155 clearAllFlags();
dicarloj 17:c9afe1a7b423 2156 setCarryFlag();
dicarloj 17:c9afe1a7b423 2157 } else {
dicarloj 17:c9afe1a7b423 2158 clearAllFlags();
dicarloj 17:c9afe1a7b423 2159 }
dicarloj 17:c9afe1a7b423 2160 setSubtractFlag();
dicarloj 17:c9afe1a7b423 2161 if(result == 0) {
dicarloj 17:c9afe1a7b423 2162 setZeroFlag();
dicarloj 17:c9afe1a7b423 2163 }
dicarloj 17:c9afe1a7b423 2164 if((result & 0xf) == 0xf) {
dicarloj 17:c9afe1a7b423 2165 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2166 }
dicarloj 17:c9afe1a7b423 2167 return result;
dicarloj 17:c9afe1a7b423 2168 }
dicarloj 17:c9afe1a7b423 2169
dicarloj 17:c9afe1a7b423 2170 void DEC_A(u8 opcode) { // 0x3d
dicarloj 17:c9afe1a7b423 2171 globalState.a = decrement(globalState.a);
dicarloj 17:c9afe1a7b423 2172 globalState.pc++;
dicarloj 17:c9afe1a7b423 2173 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2174 }
dicarloj 17:c9afe1a7b423 2175
dicarloj 17:c9afe1a7b423 2176 void DEC_B(u8 opcode) { // 0x05
dicarloj 17:c9afe1a7b423 2177 globalState.bc.hi = decrement(globalState.bc.hi);
dicarloj 17:c9afe1a7b423 2178 globalState.pc++;
dicarloj 17:c9afe1a7b423 2179 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2180 }
dicarloj 17:c9afe1a7b423 2181
dicarloj 17:c9afe1a7b423 2182 void DEC_C(u8 opcode) { // 0x0d
dicarloj 17:c9afe1a7b423 2183 globalState.bc.lo = decrement(globalState.bc.lo);
dicarloj 17:c9afe1a7b423 2184 globalState.pc++;
dicarloj 17:c9afe1a7b423 2185 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2186 }
dicarloj 17:c9afe1a7b423 2187
dicarloj 17:c9afe1a7b423 2188 void DEC_D(u8 opcode) { // 0x15
dicarloj 17:c9afe1a7b423 2189 globalState.de.hi = decrement(globalState.de.hi);
dicarloj 17:c9afe1a7b423 2190 globalState.pc++;
dicarloj 17:c9afe1a7b423 2191 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2192 }
dicarloj 17:c9afe1a7b423 2193
dicarloj 17:c9afe1a7b423 2194 void DEC_E(u8 opcode) { // 0x1d
dicarloj 17:c9afe1a7b423 2195 globalState.de.lo = decrement(globalState.de.lo);
dicarloj 17:c9afe1a7b423 2196 globalState.pc++;
dicarloj 17:c9afe1a7b423 2197 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2198 }
dicarloj 17:c9afe1a7b423 2199
dicarloj 17:c9afe1a7b423 2200 void DEC_H(u8 opcode) { // 0x25
dicarloj 17:c9afe1a7b423 2201 globalState.hl.hi = decrement(globalState.hl.hi);
dicarloj 17:c9afe1a7b423 2202 globalState.pc++;
dicarloj 17:c9afe1a7b423 2203 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2204 }
dicarloj 17:c9afe1a7b423 2205
dicarloj 17:c9afe1a7b423 2206 void DEC_L(u8 opcode) { // 0x2d
dicarloj 17:c9afe1a7b423 2207 globalState.hl.lo = decrement(globalState.hl.lo);
dicarloj 17:c9afe1a7b423 2208 globalState.pc++;
dicarloj 17:c9afe1a7b423 2209 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2210 }
dicarloj 17:c9afe1a7b423 2211
dicarloj 17:c9afe1a7b423 2212 void DEC_DHL(u8 opcode) { // 0x35
dicarloj 17:c9afe1a7b423 2213 // todo this one is hard.
dicarloj 17:c9afe1a7b423 2214 u8 v = readByte(globalState.hl.v);
dicarloj 17:c9afe1a7b423 2215 writeByte(decrement(v), globalState.hl.v);
dicarloj 17:c9afe1a7b423 2216 globalState.pc++;
dicarloj 17:c9afe1a7b423 2217 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2218 }
dicarloj 17:c9afe1a7b423 2219
dicarloj 17:c9afe1a7b423 2220 void addToHl(u16 number) {
dicarloj 17:c9afe1a7b423 2221 int result = globalState.hl.v + number;
dicarloj 17:c9afe1a7b423 2222 if(getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2223 clearAllFlags();
dicarloj 17:c9afe1a7b423 2224 setZeroFlag();
dicarloj 17:c9afe1a7b423 2225 } else {
dicarloj 17:c9afe1a7b423 2226 clearAllFlags();
dicarloj 17:c9afe1a7b423 2227 }
dicarloj 17:c9afe1a7b423 2228 if(result & 0x10000) {
dicarloj 17:c9afe1a7b423 2229 setCarryFlag();
dicarloj 17:c9afe1a7b423 2230 }
dicarloj 17:c9afe1a7b423 2231 if((globalState.hl.v ^ number ^ (result & 0xffff)) & 0x1000) {
dicarloj 17:c9afe1a7b423 2232 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2233 }
dicarloj 17:c9afe1a7b423 2234 globalState.hl.v = (u16)result;
dicarloj 17:c9afe1a7b423 2235 }
dicarloj 17:c9afe1a7b423 2236
dicarloj 17:c9afe1a7b423 2237 void ADD_HL_BC(u8 opcode) { // 09
dicarloj 17:c9afe1a7b423 2238 globalState.pc++;
dicarloj 17:c9afe1a7b423 2239 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2240 addToHl(globalState.bc.v);
dicarloj 17:c9afe1a7b423 2241 }
dicarloj 17:c9afe1a7b423 2242
dicarloj 17:c9afe1a7b423 2243 void ADD_HL_DE(u8 opcode) { // 19
dicarloj 17:c9afe1a7b423 2244 globalState.pc++;
dicarloj 17:c9afe1a7b423 2245 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2246 addToHl(globalState.de.v);
dicarloj 17:c9afe1a7b423 2247 }
dicarloj 17:c9afe1a7b423 2248
dicarloj 17:c9afe1a7b423 2249 void ADD_HL_HL(u8 opcode) { // 29
dicarloj 17:c9afe1a7b423 2250 globalState.pc++;
dicarloj 17:c9afe1a7b423 2251 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2252 addToHl(globalState.hl.v);
dicarloj 17:c9afe1a7b423 2253 }
dicarloj 17:c9afe1a7b423 2254
dicarloj 17:c9afe1a7b423 2255 void ADD_HL_SP(u8 opcode) { // 39
dicarloj 17:c9afe1a7b423 2256 globalState.pc++;
dicarloj 17:c9afe1a7b423 2257 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2258 addToHl(globalState.sp);
dicarloj 17:c9afe1a7b423 2259 }
dicarloj 17:c9afe1a7b423 2260
dicarloj 17:c9afe1a7b423 2261 void ADD_SP_n(u8 opcode) { // E8
dicarloj 17:c9afe1a7b423 2262 s8 number = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2263 globalState.pc++;
dicarloj 17:c9afe1a7b423 2264 globalState.cycleCount += 16;
dicarloj 17:c9afe1a7b423 2265 int result = globalState.sp + number;
dicarloj 17:c9afe1a7b423 2266 clearAllFlags();
dicarloj 17:c9afe1a7b423 2267 if(((globalState.sp ^ number ^ (result & 0xffff)) & 0x100) == 0x100) {
dicarloj 17:c9afe1a7b423 2268 setCarryFlag();
dicarloj 17:c9afe1a7b423 2269 }
dicarloj 17:c9afe1a7b423 2270 if(((globalState.sp ^ number ^ (result & 0xffff)) & 0x10) == 0x10) {
dicarloj 17:c9afe1a7b423 2271 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2272 }
dicarloj 17:c9afe1a7b423 2273 globalState.sp = (u16)result;
dicarloj 17:c9afe1a7b423 2274 }
dicarloj 17:c9afe1a7b423 2275
dicarloj 17:c9afe1a7b423 2276 // 16 bit incs
dicarloj 17:c9afe1a7b423 2277 void INC_BC(u8 opcode) { // 03
dicarloj 17:c9afe1a7b423 2278 globalState.pc++;
dicarloj 17:c9afe1a7b423 2279 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2280 globalState.bc.v++;
dicarloj 17:c9afe1a7b423 2281 }
dicarloj 17:c9afe1a7b423 2282
dicarloj 17:c9afe1a7b423 2283 void INC_DE(u8 opcode) { // 13
dicarloj 17:c9afe1a7b423 2284 globalState.pc++;
dicarloj 17:c9afe1a7b423 2285 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2286 globalState.de.v++;
dicarloj 17:c9afe1a7b423 2287 }
dicarloj 17:c9afe1a7b423 2288
dicarloj 17:c9afe1a7b423 2289 void INC_HL(u8 opcode) { // 23
dicarloj 17:c9afe1a7b423 2290 globalState.pc++;
dicarloj 17:c9afe1a7b423 2291 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2292 globalState.hl.v++;
dicarloj 17:c9afe1a7b423 2293 }
dicarloj 17:c9afe1a7b423 2294
dicarloj 17:c9afe1a7b423 2295 void INC_SP(u8 opcode) { // 33
dicarloj 17:c9afe1a7b423 2296 globalState.pc++;
dicarloj 17:c9afe1a7b423 2297 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2298 globalState.sp++;
dicarloj 17:c9afe1a7b423 2299 }
dicarloj 17:c9afe1a7b423 2300
dicarloj 17:c9afe1a7b423 2301 // 16 bit decs
dicarloj 17:c9afe1a7b423 2302 void DEC_BC(u8 opcode) { // 0B
dicarloj 17:c9afe1a7b423 2303 globalState.pc++;
dicarloj 17:c9afe1a7b423 2304 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2305 globalState.bc.v--;
dicarloj 17:c9afe1a7b423 2306 }
dicarloj 17:c9afe1a7b423 2307
dicarloj 17:c9afe1a7b423 2308 void DEC_DE(u8 opcode) { // 1B
dicarloj 17:c9afe1a7b423 2309 globalState.pc++;
dicarloj 17:c9afe1a7b423 2310 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2311 globalState.de.v--;
dicarloj 17:c9afe1a7b423 2312 }
dicarloj 17:c9afe1a7b423 2313
dicarloj 17:c9afe1a7b423 2314 void DEC_HL(u8 opcode) { // 2B
dicarloj 17:c9afe1a7b423 2315 globalState.pc++;
dicarloj 17:c9afe1a7b423 2316 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2317 globalState.hl.v--;
dicarloj 17:c9afe1a7b423 2318 }
dicarloj 17:c9afe1a7b423 2319
dicarloj 17:c9afe1a7b423 2320 void DEC_SP(u8 opcode) { // 3B
dicarloj 17:c9afe1a7b423 2321 globalState.pc++;
dicarloj 17:c9afe1a7b423 2322 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2323 globalState.sp--;
dicarloj 17:c9afe1a7b423 2324 }
dicarloj 17:c9afe1a7b423 2325
dicarloj 17:c9afe1a7b423 2326 void DAA(u8 opcode) { // 0x27
dicarloj 17:c9afe1a7b423 2327 globalState.pc++;
dicarloj 17:c9afe1a7b423 2328 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2329 u8 a = globalState.a;
dicarloj 17:c9afe1a7b423 2330
dicarloj 17:c9afe1a7b423 2331 if(!getSubtractFlag()) {
dicarloj 17:c9afe1a7b423 2332 if(getCarryFlag() || a > 0x99) {
dicarloj 17:c9afe1a7b423 2333 a += 0x60;
dicarloj 17:c9afe1a7b423 2334 setCarryFlag();
dicarloj 17:c9afe1a7b423 2335 }
dicarloj 17:c9afe1a7b423 2336 if(getHalfCarryFlag() || (a & 0x0f) > 0x09) {
dicarloj 17:c9afe1a7b423 2337 a += 0x6;
dicarloj 17:c9afe1a7b423 2338 }
dicarloj 17:c9afe1a7b423 2339 } else {
dicarloj 17:c9afe1a7b423 2340 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2341 a -= 0x60;
dicarloj 17:c9afe1a7b423 2342 }
dicarloj 17:c9afe1a7b423 2343 if(getHalfCarryFlag()) {
dicarloj 17:c9afe1a7b423 2344 a -= 0x6;
dicarloj 17:c9afe1a7b423 2345 }
dicarloj 17:c9afe1a7b423 2346 }
dicarloj 17:c9afe1a7b423 2347 clearZeroFlag();
dicarloj 17:c9afe1a7b423 2348 clearHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2349 if(a == 0) {
dicarloj 17:c9afe1a7b423 2350 setZeroFlag();
dicarloj 17:c9afe1a7b423 2351 }
dicarloj 17:c9afe1a7b423 2352 globalState.a = (u8)a;
dicarloj 17:c9afe1a7b423 2353 }
dicarloj 17:c9afe1a7b423 2354
dicarloj 17:c9afe1a7b423 2355 void CPL(u8 opcode) { // 0x2f
dicarloj 17:c9afe1a7b423 2356 globalState.pc++;
dicarloj 17:c9afe1a7b423 2357 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2358 globalState.a = ~globalState.a;
dicarloj 17:c9afe1a7b423 2359 setHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2360 setSubtractFlag();
dicarloj 17:c9afe1a7b423 2361 }
dicarloj 17:c9afe1a7b423 2362
dicarloj 17:c9afe1a7b423 2363 void CCF(u8 opcode) { // 0x3f
dicarloj 17:c9afe1a7b423 2364 globalState.pc++;
dicarloj 17:c9afe1a7b423 2365 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2366 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2367 clearCarryFlag();
dicarloj 17:c9afe1a7b423 2368 } else {
dicarloj 17:c9afe1a7b423 2369 setCarryFlag();
dicarloj 17:c9afe1a7b423 2370 }
dicarloj 17:c9afe1a7b423 2371 clearHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2372 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 2373 }
dicarloj 17:c9afe1a7b423 2374
dicarloj 17:c9afe1a7b423 2375 void SCF(u8 opcode) { // 0x37
dicarloj 17:c9afe1a7b423 2376 globalState.pc++;
dicarloj 17:c9afe1a7b423 2377 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2378 clearHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2379 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 2380 setCarryFlag();
dicarloj 17:c9afe1a7b423 2381 }
dicarloj 17:c9afe1a7b423 2382
dicarloj 17:c9afe1a7b423 2383 void NOP(u8 opcode) { // 00
dicarloj 17:c9afe1a7b423 2384 globalState.pc++;
dicarloj 17:c9afe1a7b423 2385 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2386 }
dicarloj 17:c9afe1a7b423 2387
dicarloj 17:c9afe1a7b423 2388
dicarloj 17:c9afe1a7b423 2389 void HALT(u8 opcode) { // 76
dicarloj 17:c9afe1a7b423 2390 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2391 globalState.pc++;
dicarloj 17:c9afe1a7b423 2392 //if(globalState.ime) {
dicarloj 17:c9afe1a7b423 2393 globalState.halt = true;
dicarloj 17:c9afe1a7b423 2394 //}
dicarloj 17:c9afe1a7b423 2395
dicarloj 17:c9afe1a7b423 2396 }
dicarloj 17:c9afe1a7b423 2397
dicarloj 17:c9afe1a7b423 2398 void STOP(u8 opcode) { // 10 (00)
dicarloj 17:c9afe1a7b423 2399 printf("stop not yet implemented\n");
dicarloj 17:c9afe1a7b423 2400 assert(false);
dicarloj 17:c9afe1a7b423 2401 }
dicarloj 17:c9afe1a7b423 2402
dicarloj 17:c9afe1a7b423 2403 void DI(u8 opcode) { // F3
dicarloj 17:c9afe1a7b423 2404 // todo instruction after bs
dicarloj 17:c9afe1a7b423 2405 globalState.ime = 0;
dicarloj 17:c9afe1a7b423 2406 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2407 globalState.pc++;
dicarloj 17:c9afe1a7b423 2408 // printf("di not yet implemented\n");
dicarloj 17:c9afe1a7b423 2409 // assert(false);
dicarloj 17:c9afe1a7b423 2410 }
dicarloj 17:c9afe1a7b423 2411
dicarloj 17:c9afe1a7b423 2412 void EI(u8 opcode) { // FB
dicarloj 17:c9afe1a7b423 2413 globalState.ime = 1;
dicarloj 17:c9afe1a7b423 2414 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2415 globalState.pc++;
dicarloj 17:c9afe1a7b423 2416 }
dicarloj 17:c9afe1a7b423 2417
dicarloj 17:c9afe1a7b423 2418 void RLCA(u8 opcode) { // 07
dicarloj 17:c9afe1a7b423 2419 globalState.pc++;
dicarloj 17:c9afe1a7b423 2420 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2421 globalState.a = rlcReg(globalState.a, true);
dicarloj 17:c9afe1a7b423 2422 }
dicarloj 17:c9afe1a7b423 2423
dicarloj 17:c9afe1a7b423 2424 void RLA(u8 opcode) { // 17
dicarloj 17:c9afe1a7b423 2425 globalState.pc++;
dicarloj 17:c9afe1a7b423 2426 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2427 globalState.a = rlReg(globalState.a, true);
dicarloj 17:c9afe1a7b423 2428 }
dicarloj 17:c9afe1a7b423 2429
dicarloj 17:c9afe1a7b423 2430 void RRCA(u8 opcode) { // 0x0f
dicarloj 17:c9afe1a7b423 2431 globalState.pc++;
dicarloj 17:c9afe1a7b423 2432 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2433 globalState.a = rrc(globalState.a, true);
dicarloj 17:c9afe1a7b423 2434 }
dicarloj 17:c9afe1a7b423 2435
dicarloj 17:c9afe1a7b423 2436 void RRA(u8 opcode) { // 0x1f
dicarloj 17:c9afe1a7b423 2437 globalState.pc++;
dicarloj 17:c9afe1a7b423 2438 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2439 globalState.a = rr(globalState.a, true);
dicarloj 17:c9afe1a7b423 2440 }
dicarloj 17:c9afe1a7b423 2441
dicarloj 17:c9afe1a7b423 2442 void JP_nn(u8 opcodes) { // 0xc3
dicarloj 17:c9afe1a7b423 2443 globalState.pc++;
dicarloj 17:c9afe1a7b423 2444 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2445 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2446 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2447 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2448 }
dicarloj 17:c9afe1a7b423 2449
dicarloj 17:c9afe1a7b423 2450 void JP_NZ_nn(u8 opcodes) { // 0xc2
dicarloj 17:c9afe1a7b423 2451 globalState.pc++;
dicarloj 17:c9afe1a7b423 2452 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2453 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2454 if(!getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2455 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2456 }
dicarloj 17:c9afe1a7b423 2457 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2458 }
dicarloj 17:c9afe1a7b423 2459
dicarloj 17:c9afe1a7b423 2460 void JP_Z_nn(u8 opcodes) { // 0xca
dicarloj 17:c9afe1a7b423 2461 globalState.pc++;
dicarloj 17:c9afe1a7b423 2462 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2463 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2464 if(getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2465 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2466 }
dicarloj 17:c9afe1a7b423 2467 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2468 }
dicarloj 17:c9afe1a7b423 2469
dicarloj 17:c9afe1a7b423 2470 void JP_NC_nn(u8 opcodes) { // 0xd2
dicarloj 17:c9afe1a7b423 2471 globalState.pc++;
dicarloj 17:c9afe1a7b423 2472 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2473 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2474 if(!getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2475 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2476 }
dicarloj 17:c9afe1a7b423 2477 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2478 }
dicarloj 17:c9afe1a7b423 2479
dicarloj 17:c9afe1a7b423 2480 void JP_C_nn(u8 opcodes) { // 0xda
dicarloj 17:c9afe1a7b423 2481 globalState.pc++;
dicarloj 17:c9afe1a7b423 2482 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2483 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2484 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2485 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2486 }
dicarloj 17:c9afe1a7b423 2487 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2488 }
dicarloj 17:c9afe1a7b423 2489
dicarloj 17:c9afe1a7b423 2490 void JP_HL(u8 opcodes) { // 0xe9
dicarloj 17:c9afe1a7b423 2491 globalState.pc = globalState.hl.v;
dicarloj 17:c9afe1a7b423 2492 globalState.cycleCount += 4;
dicarloj 17:c9afe1a7b423 2493 }
dicarloj 17:c9afe1a7b423 2494
dicarloj 17:c9afe1a7b423 2495 void JR_n(u8 opcode) { // 18
dicarloj 17:c9afe1a7b423 2496 s8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2497 globalState.pc++;
dicarloj 17:c9afe1a7b423 2498 globalState.pc += imm;
dicarloj 17:c9afe1a7b423 2499 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2500 }
dicarloj 17:c9afe1a7b423 2501
dicarloj 17:c9afe1a7b423 2502 void JR_NZ(u8 opcode) { // 20
dicarloj 17:c9afe1a7b423 2503 s8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2504 globalState.pc++;
dicarloj 17:c9afe1a7b423 2505 if(!getZeroFlag()){
dicarloj 17:c9afe1a7b423 2506 globalState.pc += imm;
dicarloj 17:c9afe1a7b423 2507 }
dicarloj 17:c9afe1a7b423 2508 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2509 }
dicarloj 17:c9afe1a7b423 2510
dicarloj 17:c9afe1a7b423 2511 void JR_Z(u8 opcode) { // 28
dicarloj 17:c9afe1a7b423 2512 s8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2513 globalState.pc++;
dicarloj 17:c9afe1a7b423 2514 if(getZeroFlag()){
dicarloj 17:c9afe1a7b423 2515 globalState.pc += imm;
dicarloj 17:c9afe1a7b423 2516 }
dicarloj 17:c9afe1a7b423 2517 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2518 }
dicarloj 17:c9afe1a7b423 2519
dicarloj 17:c9afe1a7b423 2520 void JR_NC(u8 opcode) { // 30
dicarloj 17:c9afe1a7b423 2521 s8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2522 globalState.pc++;
dicarloj 17:c9afe1a7b423 2523 if(!getCarryFlag()){
dicarloj 17:c9afe1a7b423 2524 globalState.pc += imm;
dicarloj 17:c9afe1a7b423 2525 }
dicarloj 17:c9afe1a7b423 2526 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2527 }
dicarloj 17:c9afe1a7b423 2528
dicarloj 17:c9afe1a7b423 2529 void JR_C(u8 opcode) { // 38
dicarloj 17:c9afe1a7b423 2530 s8 imm = readByte(++globalState.pc);
dicarloj 17:c9afe1a7b423 2531 globalState.pc++;
dicarloj 17:c9afe1a7b423 2532 if(getCarryFlag()){
dicarloj 17:c9afe1a7b423 2533 globalState.pc += imm;
dicarloj 17:c9afe1a7b423 2534 }
dicarloj 17:c9afe1a7b423 2535 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2536 }
dicarloj 17:c9afe1a7b423 2537
dicarloj 17:c9afe1a7b423 2538
dicarloj 17:c9afe1a7b423 2539 void CALL_nn(u8 opcode) { // 0xCD
dicarloj 17:c9afe1a7b423 2540 globalState.pc++;
dicarloj 17:c9afe1a7b423 2541 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2542 //printf("call address 0x%x\n", addr);
dicarloj 17:c9afe1a7b423 2543 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2544 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2545 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2546 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2547 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2548 }
dicarloj 17:c9afe1a7b423 2549
dicarloj 17:c9afe1a7b423 2550 void CALL_NZ(u8 opcode) { // 0xC4
dicarloj 17:c9afe1a7b423 2551 globalState.pc++;
dicarloj 17:c9afe1a7b423 2552 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2553 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2554 if(!getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2555 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2556 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2557 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2558 }
dicarloj 17:c9afe1a7b423 2559 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2560 }
dicarloj 17:c9afe1a7b423 2561
dicarloj 17:c9afe1a7b423 2562 void CALL_Z(u8 opcode) { // 0xCc
dicarloj 17:c9afe1a7b423 2563 globalState.pc++;
dicarloj 17:c9afe1a7b423 2564 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2565 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2566 if(getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2567 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2568 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2569 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2570 }
dicarloj 17:c9afe1a7b423 2571 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2572 }
dicarloj 17:c9afe1a7b423 2573
dicarloj 17:c9afe1a7b423 2574
dicarloj 17:c9afe1a7b423 2575 void CALL_NC(u8 opcode) { // 0d4
dicarloj 17:c9afe1a7b423 2576 globalState.pc++;
dicarloj 17:c9afe1a7b423 2577 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2578 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2579 if(!getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2580 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2581 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2582 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2583 }
dicarloj 17:c9afe1a7b423 2584 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2585 }
dicarloj 17:c9afe1a7b423 2586
dicarloj 17:c9afe1a7b423 2587 void CALL_C(u8 opcode) { // 0xdc
dicarloj 17:c9afe1a7b423 2588 globalState.pc++;
dicarloj 17:c9afe1a7b423 2589 u16 addr = readU16(globalState.pc);
dicarloj 17:c9afe1a7b423 2590 globalState.pc += 2;
dicarloj 17:c9afe1a7b423 2591 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2592 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2593 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2594 globalState.pc = addr;
dicarloj 17:c9afe1a7b423 2595 }
dicarloj 17:c9afe1a7b423 2596 globalState.cycleCount += 12;
dicarloj 17:c9afe1a7b423 2597 }
dicarloj 17:c9afe1a7b423 2598
dicarloj 17:c9afe1a7b423 2599 void REST_00(u8 opcode) { // 0xc7
dicarloj 17:c9afe1a7b423 2600 globalState.pc++;
dicarloj 17:c9afe1a7b423 2601 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2602 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2603 globalState.pc = 0x0000;
dicarloj 17:c9afe1a7b423 2604 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2605 }
dicarloj 17:c9afe1a7b423 2606
dicarloj 17:c9afe1a7b423 2607 void REST_08(u8 opcode) { // 0xcf
dicarloj 17:c9afe1a7b423 2608 globalState.pc++;
dicarloj 17:c9afe1a7b423 2609 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2610 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2611 globalState.pc = 0x0008;
dicarloj 17:c9afe1a7b423 2612 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2613 }
dicarloj 17:c9afe1a7b423 2614
dicarloj 17:c9afe1a7b423 2615 void REST_10(u8 opcode) { // 0xd7
dicarloj 17:c9afe1a7b423 2616 globalState.pc++;
dicarloj 17:c9afe1a7b423 2617 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2618 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2619 globalState.pc = 0x0010;
dicarloj 17:c9afe1a7b423 2620 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2621 }
dicarloj 17:c9afe1a7b423 2622
dicarloj 17:c9afe1a7b423 2623 void REST_18(u8 opcode) { // 0xdf
dicarloj 17:c9afe1a7b423 2624 globalState.pc++;
dicarloj 17:c9afe1a7b423 2625 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2626 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2627 globalState.pc = 0x0018;
dicarloj 17:c9afe1a7b423 2628 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2629 }
dicarloj 17:c9afe1a7b423 2630
dicarloj 17:c9afe1a7b423 2631 void REST_20(u8 opcode) { // 0xe7
dicarloj 17:c9afe1a7b423 2632 globalState.pc++;
dicarloj 17:c9afe1a7b423 2633 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2634 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2635 globalState.pc = 0x0020;
dicarloj 17:c9afe1a7b423 2636 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2637 }
dicarloj 17:c9afe1a7b423 2638
dicarloj 17:c9afe1a7b423 2639 void REST_28(u8 opcode) { // 0xef
dicarloj 17:c9afe1a7b423 2640 globalState.pc++;
dicarloj 17:c9afe1a7b423 2641 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2642 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2643 globalState.pc = 0x0028;
dicarloj 17:c9afe1a7b423 2644 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2645 }
dicarloj 17:c9afe1a7b423 2646
dicarloj 17:c9afe1a7b423 2647 void REST_30(u8 opcode) { // 0xf7
dicarloj 17:c9afe1a7b423 2648 globalState.pc++;
dicarloj 17:c9afe1a7b423 2649 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2650 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2651 globalState.pc = 0x0030;
dicarloj 17:c9afe1a7b423 2652 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2653 }
dicarloj 17:c9afe1a7b423 2654
dicarloj 17:c9afe1a7b423 2655 void REST_38(u8 opcode) { // 0xff
dicarloj 17:c9afe1a7b423 2656 globalState.pc++;
dicarloj 17:c9afe1a7b423 2657 writeU16(globalState.pc, globalState.sp - (u16)2);
dicarloj 17:c9afe1a7b423 2658 globalState.sp -= 2;
dicarloj 17:c9afe1a7b423 2659 globalState.pc = 0x0038;
dicarloj 17:c9afe1a7b423 2660 globalState.cycleCount += 32;
dicarloj 17:c9afe1a7b423 2661 }
dicarloj 17:c9afe1a7b423 2662
dicarloj 17:c9afe1a7b423 2663 void RET(u8 opcode) { // 0xc9
dicarloj 17:c9afe1a7b423 2664 globalState.pc++;
dicarloj 17:c9afe1a7b423 2665 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2666 globalState.pc = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 2667 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 2668 }
dicarloj 17:c9afe1a7b423 2669
dicarloj 17:c9afe1a7b423 2670 void RET_NZ(u8 opcode) { // 0xc0
dicarloj 17:c9afe1a7b423 2671 globalState.pc++;
dicarloj 17:c9afe1a7b423 2672 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2673 if(!getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2674 globalState.pc = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 2675 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 2676 }
dicarloj 17:c9afe1a7b423 2677 }
dicarloj 17:c9afe1a7b423 2678
dicarloj 17:c9afe1a7b423 2679 void RET_Z(u8 opcode) { // 0xc8
dicarloj 17:c9afe1a7b423 2680 globalState.pc++;
dicarloj 17:c9afe1a7b423 2681 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2682 if(getZeroFlag()) {
dicarloj 17:c9afe1a7b423 2683 globalState.pc = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 2684 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 2685 }
dicarloj 17:c9afe1a7b423 2686 }
dicarloj 17:c9afe1a7b423 2687
dicarloj 17:c9afe1a7b423 2688 void RET_NC(u8 opcode) { // 0xd0
dicarloj 17:c9afe1a7b423 2689 globalState.pc++;
dicarloj 17:c9afe1a7b423 2690 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2691 if(!getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2692 globalState.pc = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 2693 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 2694 }
dicarloj 17:c9afe1a7b423 2695 }
dicarloj 17:c9afe1a7b423 2696
dicarloj 17:c9afe1a7b423 2697 void RET_C(u8 opcode) { // 0xd8
dicarloj 17:c9afe1a7b423 2698 globalState.pc++;
dicarloj 17:c9afe1a7b423 2699 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2700 if(getCarryFlag()) {
dicarloj 17:c9afe1a7b423 2701 globalState.pc = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 2702 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 2703 }
dicarloj 17:c9afe1a7b423 2704 }
dicarloj 17:c9afe1a7b423 2705
dicarloj 17:c9afe1a7b423 2706 void RETI(u8 opcode) { // 0xd9
dicarloj 17:c9afe1a7b423 2707 globalState.pc++;
dicarloj 17:c9afe1a7b423 2708 globalState.cycleCount += 8;
dicarloj 17:c9afe1a7b423 2709 globalState.pc = readU16(globalState.sp);
dicarloj 17:c9afe1a7b423 2710 globalState.sp += 2;
dicarloj 17:c9afe1a7b423 2711 globalState.ime = 1;
dicarloj 17:c9afe1a7b423 2712 }
dicarloj 17:c9afe1a7b423 2713
dicarloj 17:c9afe1a7b423 2714 // normal opcode handler table
dicarloj 17:c9afe1a7b423 2715 static OpcodeHandler* opcodes[256] =
dicarloj 17:c9afe1a7b423 2716 {NOP, LD_BC_nn, LD_DBC_A, INC_BC, INC_B, DEC_B, LD_B_n, RLCA, // 0x0 - 0x7
dicarloj 17:c9afe1a7b423 2717 LD_Dnn_SP, ADD_HL_BC, LD_A_DBC, DEC_BC, INC_C, DEC_C, LD_C_n, RRCA, // 0x8 - 0xf
dicarloj 17:c9afe1a7b423 2718 STOP, LD_DE_nn, LD_DDE_A, INC_DE, INC_D, DEC_D, LD_D_n, RLA, // 0x10 - 0x17
dicarloj 17:c9afe1a7b423 2719 JR_n, ADD_HL_DE, LD_A_DDE, DEC_DE, INC_E, DEC_E, LD_E_n, RRA, // 0x18 - 0x1f
dicarloj 17:c9afe1a7b423 2720 JR_NZ, LD_HL_nn, LDI_DHL_A, INC_HL, INC_H, DEC_H, LD_H_n, DAA, // 0x20 - 0x27
dicarloj 17:c9afe1a7b423 2721 JR_Z, ADD_HL_HL, LDI_A_DHL, DEC_HL, INC_L, DEC_L, LD_L_n, CPL, // 0x28 - 0x2f
dicarloj 17:c9afe1a7b423 2722 JR_NC, LD_SP_nn, LDD_DHL_A, INC_SP, INC_DHL, DEC_DHL, LD_DHL_n, SCF, // 0x30 - 0x37
dicarloj 17:c9afe1a7b423 2723 JR_C, ADD_HL_SP, LDD_A_DHL, DEC_SP, INC_A, DEC_A, LD_A_n, CCF, // 0x38 - 0x3f
dicarloj 17:c9afe1a7b423 2724 LD_B_B, LD_B_C, LD_B_D, LD_B_E, LD_B_H, LD_B_L, LD_B_DHL, LD_B_A, // 0x40 - 0x47
dicarloj 17:c9afe1a7b423 2725 LD_C_B, LD_C_C, LD_C_D, LD_C_E, LD_C_H, LD_C_L, LD_C_DHL, LD_C_A, // 0x48 - 0x4f
dicarloj 17:c9afe1a7b423 2726 LD_D_B, LD_D_C, LD_D_D, LD_D_E, LD_D_H, LD_D_L, LD_D_DHL, LD_D_A, // 0x50 - 0x57
dicarloj 17:c9afe1a7b423 2727 LD_E_B, LD_E_C, LD_E_D, LD_E_E, LD_E_H, LD_E_L, LD_E_DHL, LD_E_A, // 0x58 - 0x5f
dicarloj 17:c9afe1a7b423 2728 LD_H_B, LD_H_C, LD_H_D, LD_H_E, LD_H_H, LD_H_L, LD_H_DHL, LD_H_A, // 0x60 - 0x67
dicarloj 17:c9afe1a7b423 2729 LD_L_B, LD_L_C, LD_L_D, LD_L_E, LD_L_H, LD_L_L, LD_L_DHL, LD_L_A, // 0x68 - 0x6f
dicarloj 17:c9afe1a7b423 2730 LD_DHL_B, LD_DHL_C, LD_DHL_D, LD_DHL_E, LD_DHL_H, LD_DHL_L, HALT, LD_DHL_A, // 0x70 - 0x77
dicarloj 17:c9afe1a7b423 2731 LD_A_B, LD_A_C, LD_A_D, LD_A_E, LD_A_H, LD_A_L, LD_A_DHL, LD_A_A , // 0x78 - 0x7f
dicarloj 17:c9afe1a7b423 2732 ADD_B, ADD_C, ADD_D, ADD_E, ADD_H, ADD_L, ADD_DHL, ADD_A, // 0x80 - 0x87
dicarloj 17:c9afe1a7b423 2733 ADC_B, ADC_C, ADC_D, ADC_E, ADC_H, ADC_L, ADC_DHL, ADC_A, // 0x88 - 0x8f
dicarloj 17:c9afe1a7b423 2734 SUB_B, SUB_C, SUB_D, SUB_E, SUB_H, SUB_L, SUB_DHL, SUB_A, // 0x90 - 0x97
dicarloj 17:c9afe1a7b423 2735 SBC_B, SBC_C, SBC_D, SBC_E, SBC_H, SBC_L, SBC_DHL, SBC_A, // 0x98 - 0x9f
dicarloj 17:c9afe1a7b423 2736 AND_B, AND_C, AND_D, AND_E, AND_H, AND_L, AND_DHL, AND_A, // 0xa0 - 0xa7
dicarloj 17:c9afe1a7b423 2737 XOR_B, XOR_C, XOR_D, XOR_E, XOR_H, XOR_L, XOR_DHL, XOR_A, // 0xa8 - 0xaf
dicarloj 17:c9afe1a7b423 2738 OR_B, OR_C, OR_D, OR_E, OR_H, OR_L, OR_DHL, OR_A, // 0xb0 - 0xb7
dicarloj 17:c9afe1a7b423 2739 CP_B, CP_C, CP_D, CP_E, CP_H, CP_L, CP_DHL, CP_A, // 0xb8 - 0xbf
dicarloj 17:c9afe1a7b423 2740 RET_NZ, POP_BC, JP_NZ_nn, JP_nn, CALL_NZ, PUSH_BC, ADD_n, REST_00, // 0xc0 - 0xc7
dicarloj 17:c9afe1a7b423 2741 RET_Z, RET, JP_Z_nn, DecodeCB, CALL_Z, CALL_nn, ADC_n, REST_08, // 0xc8 - 0xcf
dicarloj 17:c9afe1a7b423 2742 RET_NC, POP_DE, JP_NC_nn, invHandler, CALL_NC, PUSH_DE, SUB_n, REST_10, // 0xd0 - 0xd7
dicarloj 17:c9afe1a7b423 2743 RET_C, RETI, JP_C_nn, invHandler, CALL_C, invHandler, SBC_n, REST_18, // 0xd8 - 0xdf
dicarloj 17:c9afe1a7b423 2744 LD_FF00_n_A, POP_HL, LD_FF00_C_A, invHandler, invHandler, PUSH_HL, AND_n, REST_20, // 0xe0 - 0xe7
dicarloj 17:c9afe1a7b423 2745 ADD_SP_n, JP_HL, LD_Dnn_A, invHandler, invHandler, invHandler, XOR_n, REST_28, // 0xe8 - 0xef
dicarloj 17:c9afe1a7b423 2746 LD_A_FF00_n, POP_AF, LD_A_FF00_C, DI, invHandler, PUSH_AF, OR_n, REST_30, // 0xf0 - 0xf7
dicarloj 17:c9afe1a7b423 2747 LD_HL_SP_n, LD_SP_HL, LD_A_Dnn, EI, invHandler, invHandler, CP_n, REST_38}; // 0xf8 - 0xff
dicarloj 17:c9afe1a7b423 2748
dicarloj 17:c9afe1a7b423 2749
dicarloj 17:c9afe1a7b423 2750 // reset the globalState structure, including registers and timers
dicarloj 17:c9afe1a7b423 2751 void resetCpu() {
dicarloj 17:c9afe1a7b423 2752 globalState.f = 0x1; // or 0x11? (
dicarloj 17:c9afe1a7b423 2753 globalState.a = 0xb0; // maybe f, a swap??
dicarloj 17:c9afe1a7b423 2754 globalState.bc.v = 0x0013;
dicarloj 17:c9afe1a7b423 2755 globalState.de.v = 0x00d8;
dicarloj 17:c9afe1a7b423 2756 globalState.hl.v = 0x014d;
dicarloj 17:c9afe1a7b423 2757 globalState.sp = 0xfffe;
dicarloj 17:c9afe1a7b423 2758 globalState.pc = 0x0;
dicarloj 17:c9afe1a7b423 2759 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2760 globalState.cycleCount = 0;
dicarloj 17:c9afe1a7b423 2761 globalState.divOffset = 0;
dicarloj 17:c9afe1a7b423 2762 globalState.timSubcount = 0;
dicarloj 17:c9afe1a7b423 2763 }
dicarloj 17:c9afe1a7b423 2764
dicarloj 17:c9afe1a7b423 2765 // set the globalState so the next call to step() will run an ISR
dicarloj 17:c9afe1a7b423 2766 void interrupt(u16 addr) {
dicarloj 17:c9afe1a7b423 2767 globalState.ime = 0; // disable interrupts
dicarloj 17:c9afe1a7b423 2768 writeU16(globalState.pc, globalState.sp - (u16)2); // push pc to stack
dicarloj 17:c9afe1a7b423 2769 globalState.sp -= 2; // push pc to stack
dicarloj 17:c9afe1a7b423 2770 globalState.cycleCount += 12; // timing
dicarloj 17:c9afe1a7b423 2771 globalState.pc = addr; // jump to ISR
dicarloj 17:c9afe1a7b423 2772 }
dicarloj 17:c9afe1a7b423 2773
dicarloj 17:c9afe1a7b423 2774 // timer speeds: once this number of clock cycles has elapsed, the timer ticks.
dicarloj 17:c9afe1a7b423 2775 static u32 timReset[4] = {(1 << 10), (1 << 4), (1 << 6), (1 << 8)};
dicarloj 17:c9afe1a7b423 2776
dicarloj 17:c9afe1a7b423 2777 // step the CPU 1 instruction
dicarloj 17:c9afe1a7b423 2778 // returns number of clock cycles
dicarloj 17:c9afe1a7b423 2779 u32 cpuStep() {
dicarloj 17:c9afe1a7b423 2780 uint64_t oldCycleCount = globalState.cycleCount;
dicarloj 17:c9afe1a7b423 2781
dicarloj 17:c9afe1a7b423 2782 // update div register
dicarloj 17:c9afe1a7b423 2783 globalMemState.ioRegs[IO_DIV] = (u8)((globalState.cycleCount - globalState.divOffset) >> 8);
dicarloj 17:c9afe1a7b423 2784
dicarloj 17:c9afe1a7b423 2785 // execute, if we aren't halted.
dicarloj 17:c9afe1a7b423 2786 if(!globalState.halt) {
dicarloj 17:c9afe1a7b423 2787 // fetch opcode
dicarloj 17:c9afe1a7b423 2788 u8 opcode = readByte(globalState.pc);
dicarloj 17:c9afe1a7b423 2789 // execute opcode
dicarloj 17:c9afe1a7b423 2790 opcodes[opcode](opcode);
dicarloj 17:c9afe1a7b423 2791 }
dicarloj 17:c9afe1a7b423 2792
dicarloj 17:c9afe1a7b423 2793 assert(false);
dicarloj 17:c9afe1a7b423 2794 // interrupts
dicarloj 17:c9afe1a7b423 2795 if(globalState.ime && globalMemState.upperRam[0x7f] && globalMemState.ioRegs[IO_IF]) {
dicarloj 17:c9afe1a7b423 2796
dicarloj 17:c9afe1a7b423 2797 // mask interrupts with the interrupt enable register at 0xffff.
dicarloj 17:c9afe1a7b423 2798 u8 interrupts = globalMemState.upperRam[0x7f] & globalMemState.ioRegs[IO_IF];
dicarloj 17:c9afe1a7b423 2799
dicarloj 17:c9afe1a7b423 2800 if(interrupts & 0x01) {
dicarloj 17:c9afe1a7b423 2801 globalMemState.ioRegs[IO_IF] &= ~1;
dicarloj 17:c9afe1a7b423 2802 interrupt(VBLANK_INTERRUPT);
dicarloj 17:c9afe1a7b423 2803 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2804 } else if(interrupts & 0x02) {
dicarloj 17:c9afe1a7b423 2805 globalMemState.ioRegs[IO_IF] &= ~2;
dicarloj 17:c9afe1a7b423 2806 interrupt(LCDC_INTERRUPT);
dicarloj 17:c9afe1a7b423 2807 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2808 } else if(interrupts & 0x04) {
dicarloj 17:c9afe1a7b423 2809 globalMemState.ioRegs[IO_IF] &= ~4;
dicarloj 17:c9afe1a7b423 2810 interrupt(TIMER_INTERRUPT);
dicarloj 17:c9afe1a7b423 2811 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2812 } else if(interrupts & 0x08) {
dicarloj 17:c9afe1a7b423 2813 globalMemState.ioRegs[IO_IF] &= ~8;
dicarloj 17:c9afe1a7b423 2814 interrupt(SERIAL_INTERRUPT);
dicarloj 17:c9afe1a7b423 2815 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2816 } else if(interrupts & 0x10) {
dicarloj 17:c9afe1a7b423 2817 globalMemState.ioRegs[IO_IF] &= ~0x10;
dicarloj 17:c9afe1a7b423 2818 interrupt(HIGH_TO_LOW_P10_P13);
dicarloj 17:c9afe1a7b423 2819 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2820 }
dicarloj 17:c9afe1a7b423 2821 }
dicarloj 17:c9afe1a7b423 2822
dicarloj 17:c9afe1a7b423 2823 // even if we have IME off, and we're halted, we're supposed to check IF and IE register
dicarloj 17:c9afe1a7b423 2824 // this won't fire an interrupt, but will get us out of halt
dicarloj 17:c9afe1a7b423 2825 // this behavior isn't well documented, but was required to pass cpu_instr.gb
dicarloj 17:c9afe1a7b423 2826 // (though none of the games seem to need it...)
dicarloj 17:c9afe1a7b423 2827 if(globalState.halt) {
dicarloj 17:c9afe1a7b423 2828 globalState.cycleCount += 800; // just to keep ticking the timer...
dicarloj 17:c9afe1a7b423 2829 u8 interrupts = globalMemState.upperRam[0x7f] & globalMemState.ioRegs[IO_IF];
dicarloj 17:c9afe1a7b423 2830 if(interrupts & 0x01) {
dicarloj 17:c9afe1a7b423 2831 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2832 } else if(interrupts & 0x02) {
dicarloj 17:c9afe1a7b423 2833 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2834 } else if(interrupts & 0x04) {
dicarloj 17:c9afe1a7b423 2835 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2836 } else if(interrupts & 0x08) {
dicarloj 17:c9afe1a7b423 2837 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2838 } else if(interrupts & 0x10) {
dicarloj 17:c9afe1a7b423 2839 globalState.halt = false;
dicarloj 17:c9afe1a7b423 2840 }
dicarloj 17:c9afe1a7b423 2841 }
dicarloj 17:c9afe1a7b423 2842
dicarloj 17:c9afe1a7b423 2843
dicarloj 17:c9afe1a7b423 2844 // cycle count
dicarloj 17:c9afe1a7b423 2845 uint64_t cyclesThisIteration = globalState.cycleCount - oldCycleCount;
dicarloj 17:c9afe1a7b423 2846
dicarloj 17:c9afe1a7b423 2847 // update timer
dicarloj 17:c9afe1a7b423 2848 u8 tac = globalMemState.ioRegs[IO_TAC];
dicarloj 17:c9afe1a7b423 2849 bool ten = ((tac >> 2) & 1) != 0; // timer enable?
dicarloj 17:c9afe1a7b423 2850 if(ten) {
dicarloj 17:c9afe1a7b423 2851 u8 tclk = (tac & (u8)3); // timer speed
dicarloj 17:c9afe1a7b423 2852 globalState.timSubcount += cyclesThisIteration;
dicarloj 17:c9afe1a7b423 2853 if(globalState.timSubcount >= timReset[tclk]) { // timer tick
dicarloj 17:c9afe1a7b423 2854 globalState.timSubcount = 0;
dicarloj 17:c9afe1a7b423 2855 u8 timv = globalMemState.ioRegs[IO_TIMA]; // check for overflow
dicarloj 17:c9afe1a7b423 2856 if(timv == 255) {
dicarloj 17:c9afe1a7b423 2857 globalMemState.ioRegs[IO_IF] |= 4; // set interrupt
dicarloj 17:c9afe1a7b423 2858 globalMemState.ioRegs[IO_TIMA] = globalMemState.ioRegs[IO_TMA]; // reset
dicarloj 17:c9afe1a7b423 2859 } else {
dicarloj 17:c9afe1a7b423 2860 globalMemState.ioRegs[IO_TIMA] = timv + (u8)1; // increment.
dicarloj 17:c9afe1a7b423 2861 }
dicarloj 17:c9afe1a7b423 2862 }
dicarloj 17:c9afe1a7b423 2863 }
dicarloj 17:c9afe1a7b423 2864
dicarloj 17:c9afe1a7b423 2865 return (u32)cyclesThisIteration;
dicarloj 17:c9afe1a7b423 2866 }
dicarloj 17:c9afe1a7b423 2867
dicarloj 17:c9afe1a7b423 2868 bool getZeroFlag() {
dicarloj 17:c9afe1a7b423 2869 return (globalState.f & 0x80) != 0;
dicarloj 17:c9afe1a7b423 2870 }
dicarloj 17:c9afe1a7b423 2871
dicarloj 17:c9afe1a7b423 2872 bool getSubtractFlag() {
dicarloj 17:c9afe1a7b423 2873 return (globalState.f & 0x40) != 0;
dicarloj 17:c9afe1a7b423 2874 }
dicarloj 17:c9afe1a7b423 2875
dicarloj 17:c9afe1a7b423 2876 bool getHalfCarryFlag() {
dicarloj 17:c9afe1a7b423 2877 return (globalState.f & 0x20) != 0;
dicarloj 17:c9afe1a7b423 2878 }
dicarloj 17:c9afe1a7b423 2879
dicarloj 17:c9afe1a7b423 2880 bool getCarryFlag() {
dicarloj 17:c9afe1a7b423 2881 return (globalState.f & 0x10) != 0;
dicarloj 17:c9afe1a7b423 2882 }
dicarloj 17:c9afe1a7b423 2883
dicarloj 17:c9afe1a7b423 2884 void setZeroFlag() {
dicarloj 17:c9afe1a7b423 2885 globalState.f = globalState.f | (u8)0x80;
dicarloj 17:c9afe1a7b423 2886 }
dicarloj 17:c9afe1a7b423 2887
dicarloj 17:c9afe1a7b423 2888 void setSubtractFlag() {
dicarloj 17:c9afe1a7b423 2889 globalState.f = globalState.f | (u8)0x40;
dicarloj 17:c9afe1a7b423 2890 }
dicarloj 17:c9afe1a7b423 2891
dicarloj 17:c9afe1a7b423 2892 void setHalfCarryFlag() {
dicarloj 17:c9afe1a7b423 2893 globalState.f = globalState.f | (u8)0x20;
dicarloj 17:c9afe1a7b423 2894 }
dicarloj 17:c9afe1a7b423 2895
dicarloj 17:c9afe1a7b423 2896 void setCarryFlag() {
dicarloj 17:c9afe1a7b423 2897 globalState.f = globalState.f | (u8)0x10;
dicarloj 17:c9afe1a7b423 2898 }
dicarloj 17:c9afe1a7b423 2899
dicarloj 17:c9afe1a7b423 2900 void clearZeroFlag(){
dicarloj 17:c9afe1a7b423 2901 globalState.f = globalState.f & ~((u8)0x80);
dicarloj 17:c9afe1a7b423 2902 }
dicarloj 17:c9afe1a7b423 2903
dicarloj 17:c9afe1a7b423 2904 void clearSubtractFlag(){
dicarloj 17:c9afe1a7b423 2905 globalState.f = globalState.f & ~((u8)0x40);
dicarloj 17:c9afe1a7b423 2906 }
dicarloj 17:c9afe1a7b423 2907
dicarloj 17:c9afe1a7b423 2908 void clearHalfCarryFlag(){
dicarloj 17:c9afe1a7b423 2909 globalState.f = globalState.f & ~((u8)0x20);
dicarloj 17:c9afe1a7b423 2910 }
dicarloj 17:c9afe1a7b423 2911
dicarloj 17:c9afe1a7b423 2912 void clearCarryFlag(){
dicarloj 17:c9afe1a7b423 2913 globalState.f = globalState.f & ~((u8)0x10);
dicarloj 17:c9afe1a7b423 2914 }
dicarloj 17:c9afe1a7b423 2915
dicarloj 17:c9afe1a7b423 2916 void clearAllFlags() {
dicarloj 17:c9afe1a7b423 2917 clearZeroFlag();
dicarloj 17:c9afe1a7b423 2918 clearSubtractFlag();
dicarloj 17:c9afe1a7b423 2919 clearHalfCarryFlag();
dicarloj 17:c9afe1a7b423 2920 clearCarryFlag();
dicarloj 17:c9afe1a7b423 2921 }