z80.js (87663B)
1 // Z80.js de Molly Howell (MIT, https://github.com/DrGoldfire/Z80.js), adapté pour zx81-web : 2 // - l'opcode est lu par core.fetch_opcode(pc) (et non mem_read), pour que la ULA du ZX81 3 // puisse intercepter les fetchs dans la zone « écho » (A15 = 1) ; 4 // - quelques accesseurs ajoutés (get_i, get_pc, set_pc, get_iff1, is_halted) ; 5 // - export en module ES. 6 /////////////////////////////////////////////////////////////////////////////// 7 /// @file Z80.js 8 /// 9 /// @brief Emulator for the Zilog Z80 microprocessor 10 /// 11 /// @author Molly Howell 12 /// 13 /// @remarks 14 /// This module is a simple, straightforward instruction interpreter. 15 /// There is no fancy dynamic recompilation or cycle-accurate emulation. 16 /// The author believes that this should be sufficient for any emulator that 17 /// would be feasible to write in JavaScript anyway. 18 /// The code and the comments in this file assume that the reader is familiar 19 /// with the Z80 architecture. If you're not, here are some references I use: 20 /// http://clrhome.org/table/ - Z80 instruction set tables 21 /// http://www.zilog.com/docs/z80/um0080.pdf - The official manual 22 /// http://www.myquest.nl/z80undocumented/z80-documented-v0.91.pdf 23 /// - The Undocumented Z80, Documented 24 /// 25 /// @copyright (c) Molly Howell 26 /// This code is released under the MIT license, 27 /// a copy of which is available in the associated README.md file, 28 /// or at http://opensource.org/licenses/MIT 29 /////////////////////////////////////////////////////////////////////////////// 30 31 "use strict"; 32 33 /////////////////////////////////////////////////////////////////////////////// 34 /// We'll begin with the object constructor and the public API functions. 35 /////////////////////////////////////////////////////////////////////////////// 36 function Z80(coreParameter) 37 { 38 // Obviously we'll be needing the core object's functions again. 39 let core = coreParameter; 40 41 // The argument to this constructor should be an object containing 4 functions: 42 // mem_read(address) should return the byte at the given memory address, 43 // mem_write(address, value) should write the given value to the given memory address, 44 // io_read(port) should read a return a byte read from the given I/O port, 45 // io_write(port, value) should write the given byte to the given I/O port. 46 // If any of those functions is missing, this module cannot run. 47 if (!core || (typeof core.mem_read !== "function") || (typeof core.mem_write !== "function") || 48 (typeof core.io_read !== "function") || (typeof core.io_write !== "function")) 49 throw("Z80: Core object is missing required functions."); 50 51 if (typeof window !== "undefined" && this === window) 52 throw("Z80: This function is a constructor; call it using operator new."); 53 54 // All right, let's initialize the registers. 55 // First, the standard 8080 registers. 56 let a = 0x00; 57 let b = 0x00; 58 let c = 0x00; 59 let d = 0x00; 60 let e = 0x00; 61 let h = 0x00; 62 let l = 0x00; 63 // Now the special Z80 copies of the 8080 registers 64 // (the ones used for the SWAP instruction and such). 65 let a_prime = 0x00; 66 let b_prime = 0x00; 67 let c_prime = 0x00; 68 let d_prime = 0x00; 69 let e_prime = 0x00; 70 let h_prime = 0x00; 71 let l_prime = 0x00; 72 // And now the Z80 index registers. 73 let ix = 0x0000; 74 let iy = 0x0000; 75 // Then the "utility" registers: the interrupt vector, 76 // the memory refresh, the stack pointer, and the program counter. 77 let i = 0x00; 78 let r = 0x00; 79 let sp = 0xdff0; 80 let pc = 0x0000; 81 // We don't keep an F register for the flags, 82 // because most of the time we're only accessing a single flag, 83 // so we optimize for that case and use utility functions 84 // for the rarer occasions when we need to access the whole register. 85 let flags = {S:0, Z:0, Y:0, H:0, X:0, P:0, N:0, C:0}; 86 let flags_prime = {S:0, Z:0, Y:0, H:0, X:0, P:0, N:0, C:0}; 87 // And finally we have the interrupt mode and flip-flop registers. 88 let imode = 0; 89 let iff1 = 0; 90 let iff2 = 0; 91 92 // These are all specific to this implementation, not Z80 features. 93 // Keep track of whether we've had a HALT instruction called. 94 let halted = false; 95 // EI and DI wait one instruction before they take effect; 96 // these flags tell us when we're in that wait state. 97 let do_delayed_di = false; 98 let do_delayed_ei = false; 99 // This tracks the number of cycles spent in a single instruction run, 100 // including processing any prefixes and handling interrupts. 101 let cycle_counter = 0; 102 103 function getState() { 104 return { 105 b: b, 106 a: a, 107 c: c, 108 d: d, 109 e: e, 110 h: h, 111 l: l, 112 a_prime: a_prime, 113 b_prime: b_prime, 114 c_prime: c_prime, 115 d_prime: d_prime, 116 e_prime: e_prime, 117 h_prime: h_prime, 118 l_prime: l_prime, 119 ix : ix, 120 iy : iy, 121 i : i, 122 r : r, 123 sp : sp, 124 pc : pc, 125 flags: { 126 S: flags.S, 127 Z: flags.Z, 128 Y: flags.Y, 129 H: flags.H, 130 X: flags.X, 131 P: flags.P, 132 N: flags.N, 133 C: flags.C 134 }, 135 flags_prime: { 136 S: flags_prime.S, 137 Z: flags_prime.Z, 138 Y: flags_prime.Y, 139 H: flags_prime.H, 140 X: flags_prime.X, 141 P: flags_prime.P, 142 N: flags_prime.N, 143 C: flags_prime.C 144 }, 145 imode : imode, 146 iff1 : iff1, 147 iff2 : iff2, 148 halted : halted, 149 do_delayed_di : do_delayed_di, 150 do_delayed_ei : do_delayed_ei, 151 cycle_counter : cycle_counter 152 }; 153 } 154 155 function setState(state) { 156 b = state.b; 157 a = state.a; 158 c = state.c; 159 d = state.d; 160 e = state.e; 161 h = state.h; 162 l = state.l; 163 a_prime = state.a_prime; 164 b_prime = state.b_prime; 165 c_prime = state.c_prime; 166 d_prime = state.d_prime; 167 e_prime = state.e_prime; 168 h_prime = state.h_prime; 169 l_prime = state.l_prime; 170 ix = state.ix; 171 iy = state.iy; 172 i = state.i; 173 r = state.r; 174 sp = state.sp; 175 pc = state.pc; 176 flags.S = state.flags.S; 177 flags.Z = state.flags.Z; 178 flags.Y = state.flags.Y; 179 flags.H = state.flags.H; 180 flags.X = state.flags.X; 181 flags.P = state.flags.P; 182 flags.N = state.flags.N; 183 flags.C = state.flags.C; 184 flags_prime.S = state.flags_prime.S; 185 flags_prime.Z = state.flags_prime.Z; 186 flags_prime.Y = state.flags_prime.Y; 187 flags_prime.H = state.flags_prime.H; 188 flags_prime.X = state.flags_prime.X; 189 flags_prime.P = state.flags_prime.P; 190 flags_prime.N = state.flags_prime.N; 191 flags_prime.C = state.flags_prime.C; 192 imode = state.imode; 193 iff1 = state.iff1; 194 iff2 = state.iff2; 195 halted = state.halted; 196 do_delayed_di = state.do_delayed_di; 197 do_delayed_ei = state.do_delayed_ei; 198 cycle_counter = state.cycle_counter; 199 } 200 201 /////////////////////////////////////////////////////////////////////////////// 202 /// @public reset 203 /// 204 /// @brief Re-initialize the processor as if a reset or power on had occured 205 /////////////////////////////////////////////////////////////////////////////// 206 let reset = function() 207 { 208 // These registers are the ones that have predictable states 209 // immediately following a power-on or a reset. 210 // The others are left alone, because their states are unpredictable. 211 sp = 0xdff0; 212 pc = 0x0000; 213 a = 0x00; 214 r = 0x00; 215 set_flags_register(0); 216 // Start up with interrupts disabled. 217 imode = 0; 218 iff1 = 0; 219 iff2 = 0; 220 // Don't start halted or in a delayed DI or EI. 221 halted = false; 222 do_delayed_di = false; 223 do_delayed_ei = false; 224 // Obviously we've not used any cycles yet. 225 cycle_counter = 0; 226 }; 227 228 /////////////////////////////////////////////////////////////////////////////// 229 /// @public run_instruction 230 /// 231 /// @brief Runs a single instruction 232 /// 233 /// @return The number of T cycles the instruction took to run, 234 /// plus any time that went into handling interrupts that fired 235 /// while this instruction was executing 236 /////////////////////////////////////////////////////////////////////////////// 237 let run_instruction = function() 238 { 239 if (!halted) 240 { 241 // If the previous instruction was a DI or an EI, 242 // we'll need to disable or enable interrupts 243 // after whatever instruction we're about to run is finished. 244 var doing_delayed_di = false, doing_delayed_ei = false; 245 if (do_delayed_di) 246 { 247 do_delayed_di = false; 248 doing_delayed_di = true; 249 } 250 else if (do_delayed_ei) 251 { 252 do_delayed_ei = false; 253 doing_delayed_ei = true; 254 } 255 256 // R is incremented at the start of every instruction cycle, 257 // before the instruction actually runs. 258 // The high bit of R is not affected by this increment, 259 // it can only be changed using the LD R, A instruction. 260 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 261 262 // Read the byte at the PC and run the instruction it encodes. 263 var opcode = core.fetch_opcode(pc); 264 decode_instruction(opcode); 265 pc = (pc + 1) & 0xffff; 266 267 // Actually do the delayed interrupt disable/enable if we have one. 268 if (doing_delayed_di) 269 { 270 iff1 = 0; 271 iff2 = 0; 272 } 273 else if (doing_delayed_ei) 274 { 275 iff1 = 1; 276 iff2 = 1; 277 } 278 279 // And finally clear out the cycle counter for the next instruction 280 // before returning it to the emulator core. 281 var retval = cycle_counter; 282 cycle_counter = 0; 283 return retval; 284 } 285 else 286 { 287 // While we're halted, claim that we spent a cycle doing nothing, 288 // so that the rest of the emulator can still proceed. 289 return 1; 290 } 291 }; 292 293 /////////////////////////////////////////////////////////////////////////////// 294 /// @public interrupt 295 /// 296 /// @brief Simulates pulsing the processor's INT (or NMI) pin 297 /// 298 /// @param non_maskable - true if this is a non-maskable interrupt 299 /// @param data - the value to be placed on the data bus, if needed 300 /////////////////////////////////////////////////////////////////////////////// 301 let interrupt = function(non_maskable, data) 302 { 303 if (non_maskable) 304 { 305 // The high bit of R is not affected by this increment, 306 // it can only be changed using the LD R, A instruction. 307 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 308 // Non-maskable interrupts are always handled the same way; 309 // clear IFF1 and then do a CALL 0x0066. 310 // Also, all interrupts reset the HALT state. 311 halted = false; 312 iff2 = iff1; 313 iff1 = 0; 314 push_word(pc); 315 pc = 0x66; 316 cycle_counter += 11; 317 } 318 else if (iff1) 319 { 320 // The high bit of R is not affected by this increment, 321 // it can only be changed using the LD R, A instruction. 322 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 323 324 halted = false; 325 iff1 = 0; 326 iff2 = 0; 327 328 if (imode === 0) 329 { 330 // In the 8080-compatible interrupt mode, 331 // decode the content of the data bus as an instruction and run it. 332 // it's probably a RST instruction, which pushes (PC+1) onto the stack 333 // so we should decrement PC before we decode the instruction 334 pc = (pc - 1) & 0xffff; 335 decode_instruction(data); 336 pc = (pc + 1) & 0xffff; // increment PC upon return 337 cycle_counter += 2; 338 } 339 else if (imode === 1) 340 { 341 // Mode 1 is always just RST 0x38. 342 push_word(pc); 343 pc = 0x38; 344 cycle_counter += 13; 345 } 346 else if (imode === 2) 347 { 348 // Mode 2 uses the value on the data bus as in index 349 // into the vector table pointer to by the I register. 350 push_word(pc); 351 // The Z80 manual says that this address must be 2-byte aligned, 352 // but it doesn't appear that this is actually the case on the hardware, 353 // so we don't attempt to enforce that here. 354 var vector_address = ((i << 8) | data); 355 pc = core.mem_read(vector_address) | 356 (core.mem_read((vector_address + 1) & 0xffff) << 8); 357 358 cycle_counter += 19; 359 } 360 } 361 }; 362 363 /////////////////////////////////////////////////////////////////////////////// 364 /// The public API functions end here. 365 /// 366 /// What begins here are just general utility functions, used variously. 367 /////////////////////////////////////////////////////////////////////////////// 368 let decode_instruction = function(opcode) 369 { 370 // The register-to-register loads and ALU instructions 371 // are all so uniform that we can decode them directly 372 // instead of going into the instruction array for them. 373 // This function gets the operand for all of these instructions. 374 var get_operand = function(opcode) 375 { 376 return ((opcode & 0x07) === 0) ? b : 377 ((opcode & 0x07) === 1) ? c : 378 ((opcode & 0x07) === 2) ? d : 379 ((opcode & 0x07) === 3) ? e : 380 ((opcode & 0x07) === 4) ? h : 381 ((opcode & 0x07) === 5) ? l : 382 ((opcode & 0x07) === 6) ? core.mem_read(l | (h << 8)) : a; 383 }; 384 385 // Handle HALT right up front, because it fouls up our LD decoding 386 // by falling where LD (HL), (HL) ought to be. 387 if (opcode === 0x76) 388 { 389 halted = true; 390 } 391 else if ((opcode >= 0x40) && (opcode < 0x80)) 392 { 393 // This entire range is all 8-bit register loads. 394 // Get the operand and assign it to the correct destination. 395 var operand = get_operand(opcode); 396 397 if (((opcode & 0x38) >>> 3) === 0) 398 b = operand; 399 else if (((opcode & 0x38) >>> 3) === 1) 400 c = operand; 401 else if (((opcode & 0x38) >>> 3) === 2) 402 d = operand; 403 else if (((opcode & 0x38) >>> 3) === 3) 404 e = operand; 405 else if (((opcode & 0x38) >>> 3) === 4) 406 h = operand; 407 else if (((opcode & 0x38) >>> 3) === 5) 408 l = operand; 409 else if (((opcode & 0x38) >>> 3) === 6) 410 core.mem_write(l | (h << 8), operand); 411 else if (((opcode & 0x38) >>> 3) === 7) 412 a = operand; 413 } 414 else if ((opcode >= 0x80) && (opcode < 0xc0)) 415 { 416 // These are the 8-bit register ALU instructions. 417 // We'll get the operand and then use this "jump table" 418 // to call the correct utility function for the instruction. 419 var operand = get_operand(opcode), 420 op_array = [do_add, do_adc, do_sub, do_sbc, 421 do_and, do_xor, do_or, do_cp]; 422 423 op_array[(opcode & 0x38) >>> 3]( operand); 424 } 425 else 426 { 427 // This is one of the less formulaic instructions; 428 // we'll get the specific function for it from our array. 429 var func = instructions[opcode]; 430 func(); 431 } 432 433 // Update the cycle counter with however many cycles 434 // the base instruction took. 435 // If this was a prefixed instruction, then 436 // the prefix handler has added its extra cycles already. 437 cycle_counter += cycle_counts[opcode]; 438 }; 439 440 let get_signed_offset_byte = function(value) 441 { 442 // This function requires some explanation. 443 // We just use JavaScript Number variables for our registers, 444 // not like a typed array or anything. 445 // That means that, when we have a byte value that's supposed 446 // to represent a signed offset, the value we actually see 447 // isn't signed at all, it's just a small integer. 448 // So, this function converts that byte into something JavaScript 449 // will recognize as signed, so we can easily do arithmetic with it. 450 // First, we clamp the value to a single byte, just in case. 451 value &= 0xff; 452 // We don't have to do anything if the value is positive. 453 if (value & 0x80) 454 { 455 // But if the value is negative, we need to manually un-two's-compliment it. 456 // I'm going to assume you can figure out what I meant by that, 457 // because I don't know how else to explain it. 458 // We could also just do value |= 0xffffff00, but I prefer 459 // not caring how many bits are in the integer representation 460 // of a JavaScript number in the currently running browser. 461 value = -((0xff & ~value) + 1); 462 } 463 return value; 464 }; 465 466 let get_flags_register = function() 467 { 468 // We need the whole F register for some reason. 469 // probably a PUSH AF instruction, 470 // so make the F register out of our separate flags. 471 return (flags.S << 7) | 472 (flags.Z << 6) | 473 (flags.Y << 5) | 474 (flags.H << 4) | 475 (flags.X << 3) | 476 (flags.P << 2) | 477 (flags.N << 1) | 478 (flags.C); 479 }; 480 481 let get_flags_prime = function() 482 { 483 // This is the same as the above for the F' register. 484 return (flags_prime.S << 7) | 485 (flags_prime.Z << 6) | 486 (flags_prime.Y << 5) | 487 (flags_prime.H << 4) | 488 (flags_prime.X << 3) | 489 (flags_prime.P << 2) | 490 (flags_prime.N << 1) | 491 (flags_prime.C); 492 }; 493 494 let set_flags_register = function(operand) 495 { 496 // We need to set the F register, probably for a POP AF, 497 // so break out the given value into our separate flags. 498 flags.S = (operand & 0x80) >>> 7; 499 flags.Z = (operand & 0x40) >>> 6; 500 flags.Y = (operand & 0x20) >>> 5; 501 flags.H = (operand & 0x10) >>> 4; 502 flags.X = (operand & 0x08) >>> 3; 503 flags.P = (operand & 0x04) >>> 2; 504 flags.N = (operand & 0x02) >>> 1; 505 flags.C = (operand & 0x01); 506 }; 507 508 let set_flags_prime = function(operand) 509 { 510 // Again, this is the same as the above for F'. 511 flags_prime.S = (operand & 0x80) >>> 7; 512 flags_prime.Z = (operand & 0x40) >>> 6; 513 flags_prime.Y = (operand & 0x20) >>> 5; 514 flags_prime.H = (operand & 0x10) >>> 4; 515 flags_prime.X = (operand & 0x08) >>> 3; 516 flags_prime.P = (operand & 0x04) >>> 2; 517 flags_prime.N = (operand & 0x02) >>> 1; 518 flags_prime.C = (operand & 0x01); 519 }; 520 521 let update_xy_flags = function(result) 522 { 523 // Most of the time, the undocumented flags 524 // (sometimes called X and Y, or 3 and 5), 525 // take their values from the corresponding bits 526 // of the result of the instruction, 527 // or from some other related value. 528 // This is a utility function to set those flags based on those bits. 529 flags.Y = (result & 0x20) >>> 5; 530 flags.X = (result & 0x08) >>> 3; 531 }; 532 533 let get_parity = function(value) 534 { 535 // We could try to actually calculate the parity every time, 536 // but why calculate what you can pre-calculate? 537 var parity_bits = [ 538 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 539 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 540 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 541 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 542 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 543 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 544 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 545 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 546 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 547 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 548 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 549 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 550 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 551 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 552 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 553 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 554 ]; 555 return parity_bits[value]; 556 }; 557 558 let push_word = function(operand) 559 { 560 // Pretty obvious what this function does; given a 16-bit value, 561 // decrement the stack pointer, write the high byte to the new 562 // stack pointer location, then repeat for the low byte. 563 sp = (sp - 1) & 0xffff; 564 core.mem_write(sp, (operand & 0xff00) >>> 8); 565 sp = (sp - 1) & 0xffff; 566 core.mem_write(sp, operand & 0x00ff); 567 }; 568 569 let pop_word = function() 570 { 571 // Again, not complicated; read a byte off the top of the stack, 572 // increment the stack pointer, rinse and repeat. 573 var retval = core.mem_read(sp) & 0xff; 574 sp = (sp + 1) & 0xffff; 575 retval |= core.mem_read(sp) << 8; 576 sp = (sp + 1) & 0xffff; 577 return retval; 578 }; 579 580 /////////////////////////////////////////////////////////////////////////////// 581 /// Now, the way most instructions work in this emulator is that they set up 582 /// their operands according to their addressing mode, and then they call a 583 /// utility function that handles all variations of that instruction. 584 /// Those utility functions begin here. 585 /////////////////////////////////////////////////////////////////////////////// 586 let do_conditional_absolute_jump = function(condition) 587 { 588 // This function implements the JP [condition],nn instructions. 589 if (condition) 590 { 591 // We're taking this jump, so write the new PC, 592 // and then decrement the thing we just wrote, 593 // because the instruction decoder increments the PC 594 // unconditionally at the end of every instruction 595 // and we need to counteract that so we end up at the jump target. 596 pc = core.mem_read((pc + 1) & 0xffff) | 597 (core.mem_read((pc + 2) & 0xffff) << 8); 598 pc = (pc - 1) & 0xffff; 599 } 600 else 601 { 602 // We're not taking this jump, just move the PC past the operand. 603 pc = (pc + 2) & 0xffff; 604 } 605 }; 606 607 let do_conditional_relative_jump = function(condition) 608 { 609 // This function implements the JR [condition],n instructions. 610 if (condition) 611 { 612 // We need a few more cycles to actually take the jump. 613 cycle_counter += 5; 614 // Calculate the offset specified by our operand. 615 var offset = get_signed_offset_byte(core.mem_read((pc + 1) & 0xffff)); 616 // Add the offset to the PC, also skipping past this instruction. 617 pc = (pc + offset + 1) & 0xffff; 618 } 619 else 620 { 621 // No jump happening, just skip the operand. 622 pc = (pc + 1) & 0xffff; 623 } 624 }; 625 626 let do_conditional_call = function(condition) 627 { 628 // This function is the CALL [condition],nn instructions. 629 // If you've seen the previous functions, you know this drill. 630 if (condition) 631 { 632 cycle_counter += 7; 633 push_word((pc + 3) & 0xffff); 634 pc = core.mem_read((pc + 1) & 0xffff) | 635 (core.mem_read((pc + 2) & 0xffff) << 8); 636 pc = (pc - 1) & 0xffff; 637 } 638 else 639 { 640 pc = (pc + 2) & 0xffff; 641 } 642 }; 643 644 let do_conditional_return = function(condition) 645 { 646 if (condition) 647 { 648 cycle_counter += 6; 649 pc = (pop_word() - 1) & 0xffff; 650 } 651 }; 652 653 let do_reset = function(address) 654 { 655 // The RST [address] instructions go through here. 656 push_word((pc + 1) & 0xffff); 657 pc = (address - 1) & 0xffff; 658 }; 659 660 let do_add = function(operand) 661 { 662 // This is the ADD A, [operand] instructions. 663 // We'll do the literal addition, which includes any overflow, 664 // so that we can more easily figure out whether we had 665 // an overflow or a carry and set the flags accordingly. 666 var result = a + operand; 667 668 // The great majority of the work for the arithmetic instructions 669 // turns out to be setting the flags rather than the actual operation. 670 flags.S = (result & 0x80) ? 1 : 0; 671 flags.Z = !(result & 0xff) ? 1 : 0; 672 flags.H = (((operand & 0x0f) + (a & 0x0f)) & 0x10) ? 1 : 0; 673 // An overflow has happened if the sign bits of the accumulator and the operand 674 // don't match the sign bit of the result value. 675 flags.P = ((a & 0x80) === (operand & 0x80)) && ((a & 0x80) !== (result & 0x80)) ? 1 : 0; 676 flags.N = 0; 677 flags.C = (result & 0x100) ? 1 : 0; 678 679 a = result & 0xff; 680 update_xy_flags(a); 681 }; 682 683 let do_adc = function(operand) 684 { 685 var result = a + operand + flags.C; 686 687 flags.S = (result & 0x80) ? 1 : 0; 688 flags.Z = !(result & 0xff) ? 1 : 0; 689 flags.H = (((operand & 0x0f) + (a & 0x0f) + flags.C) & 0x10) ? 1 : 0; 690 flags.P = ((a & 0x80) === (operand & 0x80)) && ((a & 0x80) !== (result & 0x80)) ? 1 : 0; 691 flags.N = 0; 692 flags.C = (result & 0x100) ? 1 : 0; 693 694 a = result & 0xff; 695 update_xy_flags(a); 696 }; 697 698 let do_sub = function(operand) 699 { 700 var result = a - operand; 701 702 flags.S = (result & 0x80) ? 1 : 0; 703 flags.Z = !(result & 0xff) ? 1 : 0; 704 flags.H = (((a & 0x0f) - (operand & 0x0f)) & 0x10) ? 1 : 0; 705 flags.P = ((a & 0x80) !== (operand & 0x80)) && ((a & 0x80) !== (result & 0x80)) ? 1 : 0; 706 flags.N = 1; 707 flags.C = (result & 0x100) ? 1 : 0; 708 709 a = result & 0xff; 710 update_xy_flags(a); 711 }; 712 713 let do_sbc = function(operand) 714 { 715 var result = a - operand - flags.C; 716 717 flags.S = (result & 0x80) ? 1 : 0; 718 flags.Z = !(result & 0xff) ? 1 : 0; 719 flags.H = (((a & 0x0f) - (operand & 0x0f) - flags.C) & 0x10) ? 1 : 0; 720 flags.P = ((a & 0x80) !== (operand & 0x80)) && ((a & 0x80) !== (result & 0x80)) ? 1 : 0; 721 flags.N = 1; 722 flags.C = (result & 0x100) ? 1 : 0; 723 724 a = result & 0xff; 725 update_xy_flags(a); 726 }; 727 728 let do_cp = function(operand) 729 { 730 // A compare instruction is just a subtraction that doesn't save the value, 731 // so we implement it as... a subtraction that doesn't save the value. 732 var temp = a; 733 do_sub(operand); 734 a = temp; 735 // Since this instruction has no "result" value, the undocumented flags 736 // are set based on the operand instead. 737 update_xy_flags(operand); 738 }; 739 740 let do_and = function(operand) 741 { 742 // The logic instructions are all pretty straightforward. 743 a &= operand & 0xff; 744 flags.S = (a & 0x80) ? 1 : 0; 745 flags.Z = !a ? 1 : 0; 746 flags.H = 1; 747 flags.P = get_parity(a); 748 flags.N = 0; 749 flags.C = 0; 750 update_xy_flags(a); 751 }; 752 753 let do_or = function(operand) 754 { 755 a = (operand | a) & 0xff; 756 flags.S = (a & 0x80) ? 1 : 0; 757 flags.Z = !a ? 1 : 0; 758 flags.H = 0; 759 flags.P = get_parity(a); 760 flags.N = 0; 761 flags.C = 0; 762 update_xy_flags(a); 763 }; 764 765 let do_xor = function(operand) 766 { 767 a = (operand ^ a) & 0xff; 768 flags.S = (a & 0x80) ? 1 : 0; 769 flags.Z = !a ? 1 : 0; 770 flags.H = 0; 771 flags.P = get_parity(a); 772 flags.N = 0; 773 flags.C = 0; 774 update_xy_flags(a); 775 }; 776 777 let do_inc = function(operand) 778 { 779 var result = operand + 1; 780 781 flags.S = (result & 0x80) ? 1 : 0; 782 flags.Z = !(result & 0xff) ? 1 : 0; 783 flags.H = ((operand & 0x0f) === 0x0f) ? 1 : 0; 784 // It's a good deal easier to detect overflow for an increment/decrement. 785 flags.P = (operand === 0x7f) ? 1 : 0; 786 flags.N = 0; 787 788 result &= 0xff; 789 update_xy_flags(result); 790 791 return result; 792 }; 793 794 let do_dec = function(operand) 795 { 796 var result = operand - 1; 797 798 flags.S = (result & 0x80) ? 1 : 0; 799 flags.Z = !(result & 0xff) ? 1 : 0; 800 flags.H = ((operand & 0x0f) === 0x00) ? 1 : 0; 801 flags.P = (operand === 0x80) ? 1 : 0; 802 flags.N = 1; 803 804 result &= 0xff; 805 update_xy_flags(result); 806 807 return result; 808 }; 809 810 let do_hl_add = function(operand) 811 { 812 // The HL arithmetic instructions are the same as the A ones, 813 // just with twice as many bits happening. 814 var hl = l | (h << 8), result = hl + operand; 815 816 flags.N = 0; 817 flags.C = (result & 0x10000) ? 1 : 0; 818 flags.H = (((hl & 0x0fff) + (operand & 0x0fff)) & 0x1000) ? 1 : 0; 819 820 l = result & 0xff; 821 h = (result & 0xff00) >>> 8; 822 823 update_xy_flags(h); 824 }; 825 826 let do_hl_adc = function(operand) 827 { 828 operand += flags.C; 829 var hl = l | (h << 8), result = hl + operand; 830 831 flags.S = (result & 0x8000) ? 1 : 0; 832 flags.Z = !(result & 0xffff) ? 1 : 0; 833 flags.H = (((hl & 0x0fff) + (operand & 0x0fff)) & 0x1000) ? 1 : 0; 834 flags.P = ((hl & 0x8000) === (operand & 0x8000)) && ((result & 0x8000) !== (hl & 0x8000)) ? 1 : 0; 835 flags.N = 0; 836 flags.C = (result & 0x10000) ? 1 : 0; 837 838 l = result & 0xff; 839 h = (result >>> 8) & 0xff; 840 841 update_xy_flags(h); 842 }; 843 844 let do_hl_sbc = function(operand) 845 { 846 operand += flags.C; 847 var hl = l | (h << 8), result = hl - operand; 848 849 flags.S = (result & 0x8000) ? 1 : 0; 850 flags.Z = !(result & 0xffff) ? 1 : 0; 851 flags.H = (((hl & 0x0fff) - (operand & 0x0fff)) & 0x1000) ? 1 : 0; 852 flags.P = ((hl & 0x8000) !== (operand & 0x8000)) && ((result & 0x8000) !== (hl & 0x8000)) ? 1 : 0; 853 flags.N = 1; 854 flags.C = (result & 0x10000) ? 1 : 0; 855 856 l = result & 0xff; 857 h = (result >>> 8) & 0xff; 858 859 update_xy_flags(h); 860 }; 861 862 let do_in = function(port) 863 { 864 var result = core.io_read(port); 865 866 flags.S = (result & 0x80) ? 1 : 0; 867 flags.Z = result ? 0 : 1; 868 flags.H = 0; 869 flags.P = get_parity(result) ? 1 : 0; 870 flags.N = 0; 871 update_xy_flags(result); 872 873 return result; 874 }; 875 876 let do_neg = function() 877 { 878 // This instruction is defined to not alter the register if it === 0x80. 879 if (a !== 0x80) 880 { 881 // This is a signed operation, so convert A to a signed value. 882 a = get_signed_offset_byte(a); 883 884 a = (-a) & 0xff; 885 } 886 887 flags.S = (a & 0x80) ? 1 : 0; 888 flags.Z = !a ? 1 : 0; 889 flags.H = (((-a) & 0x0f) > 0) ? 1 : 0; 890 flags.P = (a === 0x80) ? 1 : 0; 891 flags.N = 1; 892 flags.C = a ? 1 : 0; 893 update_xy_flags(a); 894 }; 895 896 let do_ldi = function() 897 { 898 // Copy the value that we're supposed to copy. 899 var read_value = core.mem_read(l | (h << 8)); 900 core.mem_write(e | (d << 8), read_value); 901 902 // Increment DE and HL, and decrement BC. 903 var result = (e | (d << 8)) + 1; 904 e = result & 0xff; 905 d = (result & 0xff00) >>> 8; 906 result = (l | (h << 8)) + 1; 907 l = result & 0xff; 908 h = (result & 0xff00) >>> 8; 909 result = (c | (b << 8)) - 1; 910 c = result & 0xff; 911 b = (result & 0xff00) >>> 8; 912 913 flags.H = 0; 914 flags.P = (c || b) ? 1 : 0; 915 flags.N = 0; 916 flags.Y = ((a + read_value) & 0x02) >>> 1; 917 flags.X = ((a + read_value) & 0x08) >>> 3; 918 }; 919 920 let do_cpi = function() 921 { 922 var temp_carry = flags.C; 923 var read_value = core.mem_read(l | (h << 8)) 924 do_cp(read_value); 925 flags.C = temp_carry; 926 flags.Y = ((a - read_value - flags.H) & 0x02) >>> 1; 927 flags.X = ((a - read_value - flags.H) & 0x08) >>> 3; 928 929 var result = (l | (h << 8)) + 1; 930 l = result & 0xff; 931 h = (result & 0xff00) >>> 8; 932 result = (c | (b << 8)) - 1; 933 c = result & 0xff; 934 b = (result & 0xff00) >>> 8; 935 936 flags.P = result ? 1 : 0; 937 }; 938 939 let do_ini = function() 940 { 941 b = do_dec(b); 942 943 core.mem_write(l | (h << 8), core.io_read((b << 8) | c)); 944 945 var result = (l | (h << 8)) + 1; 946 l = result & 0xff; 947 h = (result & 0xff00) >>> 8; 948 949 flags.N = 1; 950 }; 951 952 let do_outi = function() 953 { 954 core.io_write((b << 8) | c, core.mem_read(l | (h << 8))); 955 956 var result = (l | (h << 8)) + 1; 957 l = result & 0xff; 958 h = (result & 0xff00) >>> 8; 959 960 b = do_dec(b); 961 flags.N = 1; 962 }; 963 964 let do_ldd = function() 965 { 966 flags.N = 0; 967 flags.H = 0; 968 969 var read_value = core.mem_read(l | (h << 8)); 970 core.mem_write(e | (d << 8), read_value); 971 972 var result = (e | (d << 8)) - 1; 973 e = result & 0xff; 974 d = (result & 0xff00) >>> 8; 975 result = (l | (h << 8)) - 1; 976 l = result & 0xff; 977 h = (result & 0xff00) >>> 8; 978 result = (c | (b << 8)) - 1; 979 c = result & 0xff; 980 b = (result & 0xff00) >>> 8; 981 982 flags.P = (c || b) ? 1 : 0; 983 flags.Y = ((a + read_value) & 0x02) >>> 1; 984 flags.X = ((a + read_value) & 0x08) >>> 3; 985 }; 986 987 let do_cpd = function() 988 { 989 var temp_carry = flags.C 990 var read_value = core.mem_read(l | (h << 8)) 991 do_cp(read_value); 992 flags.C = temp_carry; 993 flags.Y = ((a - read_value - flags.H) & 0x02) >>> 1; 994 flags.X = ((a - read_value - flags.H) & 0x08) >>> 3; 995 996 var result = (l | (h << 8)) - 1; 997 l = result & 0xff; 998 h = (result & 0xff00) >>> 8; 999 result = (c | (b << 8)) - 1; 1000 c = result & 0xff; 1001 b = (result & 0xff00) >>> 8; 1002 1003 flags.P = result ? 1 : 0; 1004 }; 1005 1006 let do_ind = function() 1007 { 1008 b = do_dec(b); 1009 1010 core.mem_write(l | (h << 8), core.io_read((b << 8) | c)); 1011 1012 var result = (l | (h << 8)) - 1; 1013 l = result & 0xff; 1014 h = (result & 0xff00) >>> 8; 1015 1016 flags.N = 1; 1017 }; 1018 1019 let do_outd = function() 1020 { 1021 core.io_write((b << 8) | c, core.mem_read(l | (h << 8))); 1022 1023 var result = (l | (h << 8)) - 1; 1024 l = result & 0xff; 1025 h = (result & 0xff00) >>> 8; 1026 1027 b = do_dec(b); 1028 flags.N = 1; 1029 }; 1030 1031 let do_rlc = function(operand) 1032 { 1033 flags.N = 0; 1034 flags.H = 0; 1035 1036 flags.C = (operand & 0x80) >>> 7; 1037 operand = ((operand << 1) | flags.C) & 0xff; 1038 1039 flags.Z = !operand ? 1 : 0; 1040 flags.P = get_parity(operand); 1041 flags.S = (operand & 0x80) ? 1 : 0; 1042 update_xy_flags(operand); 1043 1044 return operand; 1045 }; 1046 1047 let do_rrc = function(operand) 1048 { 1049 flags.N = 0; 1050 flags.H = 0; 1051 1052 flags.C = operand & 1; 1053 operand = ((operand >>> 1) & 0x7f) | (flags.C << 7); 1054 1055 flags.Z = !(operand & 0xff) ? 1 : 0; 1056 flags.P = get_parity(operand); 1057 flags.S = (operand & 0x80) ? 1 : 0; 1058 update_xy_flags(operand); 1059 1060 return operand & 0xff; 1061 }; 1062 1063 let do_rl = function(operand) 1064 { 1065 flags.N = 0; 1066 flags.H = 0; 1067 1068 var temp = flags.C; 1069 flags.C = (operand & 0x80) >>> 7; 1070 operand = ((operand << 1) | temp) & 0xff; 1071 1072 flags.Z = !operand ? 1 : 0; 1073 flags.P = get_parity(operand); 1074 flags.S = (operand & 0x80) ? 1 : 0; 1075 update_xy_flags(operand); 1076 1077 return operand; 1078 }; 1079 1080 let do_rr = function(operand) 1081 { 1082 flags.N = 0; 1083 flags.H = 0; 1084 1085 var temp = flags.C; 1086 flags.C = operand & 1; 1087 operand = ((operand >>> 1) & 0x7f) | (temp << 7); 1088 1089 flags.Z = !operand ? 1 : 0; 1090 flags.P = get_parity(operand); 1091 flags.S = (operand & 0x80) ? 1 : 0; 1092 update_xy_flags(operand); 1093 1094 return operand; 1095 }; 1096 1097 let do_sla = function(operand) 1098 { 1099 flags.N = 0; 1100 flags.H = 0; 1101 1102 flags.C = (operand & 0x80) >>> 7; 1103 operand = (operand << 1) & 0xff; 1104 1105 flags.Z = !operand ? 1 : 0; 1106 flags.P = get_parity(operand); 1107 flags.S = (operand & 0x80) ? 1 : 0; 1108 update_xy_flags(operand); 1109 1110 return operand; 1111 }; 1112 1113 let do_sra = function(operand) 1114 { 1115 flags.N = 0; 1116 flags.H = 0; 1117 1118 flags.C = operand & 1; 1119 operand = ((operand >>> 1) & 0x7f) | (operand & 0x80); 1120 1121 flags.Z = !operand ? 1 : 0; 1122 flags.P = get_parity(operand); 1123 flags.S = (operand & 0x80) ? 1 : 0; 1124 update_xy_flags(operand); 1125 1126 return operand; 1127 }; 1128 1129 let do_sll = function(operand) 1130 { 1131 flags.N = 0; 1132 flags.H = 0; 1133 1134 flags.C = (operand & 0x80) >>> 7; 1135 operand = ((operand << 1) & 0xff) | 1; 1136 1137 flags.Z = !operand ? 1 : 0; 1138 flags.P = get_parity(operand); 1139 flags.S = (operand & 0x80) ? 1 : 0; 1140 update_xy_flags(operand); 1141 1142 return operand; 1143 }; 1144 1145 let do_srl = function(operand) 1146 { 1147 flags.N = 0; 1148 flags.H = 0; 1149 1150 flags.C = operand & 1; 1151 operand = (operand >>> 1) & 0x7f; 1152 1153 flags.Z = !operand ? 1 : 0; 1154 flags.P = get_parity(operand); 1155 flags.S = 0; 1156 update_xy_flags(operand); 1157 1158 return operand; 1159 }; 1160 1161 let do_ix_add = function(operand) 1162 { 1163 flags.N = 0; 1164 1165 var result = ix + operand; 1166 1167 flags.C = (result & 0x10000) ? 1 : 0; 1168 flags.H = (((ix & 0xfff) + (operand & 0xfff)) & 0x1000) ? 1 : 0; 1169 update_xy_flags((result & 0xff00) >>> 8); 1170 1171 ix = result; 1172 }; 1173 1174 1175 /////////////////////////////////////////////////////////////////////////////// 1176 /// This table contains the implementations for the instructions that weren't 1177 /// implemented directly in the decoder function (everything but the 8-bit 1178 /// register loads and the accumulator ALU instructions, in other words). 1179 /// Similar tables for the ED and DD/FD prefixes follow this one. 1180 /////////////////////////////////////////////////////////////////////////////// 1181 let instructions = []; 1182 1183 // 0x00 : NOP 1184 instructions[0x00] = function() { }; 1185 // 0x01 : LD BC, nn 1186 instructions[0x01] = function() 1187 { 1188 pc = (pc + 1) & 0xffff; 1189 c = core.mem_read(pc); 1190 pc = (pc + 1) & 0xffff; 1191 b = core.mem_read(pc); 1192 }; 1193 // 0x02 : LD (BC), A 1194 instructions[0x02] = function() 1195 { 1196 core.mem_write(c | (b << 8), a); 1197 }; 1198 // 0x03 : INC BC 1199 instructions[0x03] = function() 1200 { 1201 var result = (c | (b << 8)); 1202 result += 1; 1203 c = result & 0xff; 1204 b = (result & 0xff00) >>> 8; 1205 }; 1206 // 0x04 : INC B 1207 instructions[0x04] = function() 1208 { 1209 b = do_inc(b); 1210 }; 1211 // 0x05 : DEC B 1212 instructions[0x05] = function() 1213 { 1214 b = do_dec(b); 1215 }; 1216 // 0x06 : LD B, n 1217 instructions[0x06] = function() 1218 { 1219 pc = (pc + 1) & 0xffff; 1220 b = core.mem_read(pc); 1221 }; 1222 // 0x07 : RLCA 1223 instructions[0x07] = function() 1224 { 1225 // This instruction is implemented as a special case of the 1226 // more general Z80-specific RLC instruction. 1227 // Specifially, RLCA is a version of RLC A that affects fewer flags. 1228 // The same applies to RRCA, RLA, and RRA. 1229 var temp_s = flags.S, temp_z = flags.Z, temp_p = flags.P; 1230 a = do_rlc(a); 1231 flags.S = temp_s; 1232 flags.Z = temp_z; 1233 flags.P = temp_p; 1234 }; 1235 // 0x08 : EX AF, AF' 1236 instructions[0x08] = function() 1237 { 1238 var temp = a; 1239 a = a_prime; 1240 a_prime = temp; 1241 1242 temp = get_flags_register(); 1243 set_flags_register(get_flags_prime()); 1244 set_flags_prime(temp); 1245 }; 1246 // 0x09 : ADD HL, BC 1247 instructions[0x09] = function() 1248 { 1249 do_hl_add(c | (b << 8)); 1250 }; 1251 // 0x0a : LD A, (BC) 1252 instructions[0x0a] = function() 1253 { 1254 a = core.mem_read(c | (b << 8)); 1255 }; 1256 // 0x0b : DEC BC 1257 instructions[0x0b] = function() 1258 { 1259 var result = (c | (b << 8)); 1260 result -= 1; 1261 c = result & 0xff; 1262 b = (result & 0xff00) >>> 8; 1263 }; 1264 // 0x0c : INC C 1265 instructions[0x0c] = function() 1266 { 1267 c = do_inc(c); 1268 }; 1269 // 0x0d : DEC C 1270 instructions[0x0d] = function() 1271 { 1272 c = do_dec(c); 1273 }; 1274 // 0x0e : LD C, n 1275 instructions[0x0e] = function() 1276 { 1277 pc = (pc + 1) & 0xffff; 1278 c = core.mem_read(pc); 1279 }; 1280 // 0x0f : RRCA 1281 instructions[0x0f] = function() 1282 { 1283 var temp_s = flags.S, temp_z = flags.Z, temp_p = flags.P; 1284 a = do_rrc(a); 1285 flags.S = temp_s; 1286 flags.Z = temp_z; 1287 flags.P = temp_p; 1288 }; 1289 // 0x10 : DJNZ nn 1290 instructions[0x10] = function() 1291 { 1292 b = (b - 1) & 0xff; 1293 do_conditional_relative_jump(b !== 0); 1294 }; 1295 // 0x11 : LD DE, nn 1296 instructions[0x11] = function() 1297 { 1298 pc = (pc + 1) & 0xffff; 1299 e = core.mem_read(pc); 1300 pc = (pc + 1) & 0xffff; 1301 d = core.mem_read(pc); 1302 }; 1303 // 0x12 : LD (DE), A 1304 instructions[0x12] = function() 1305 { 1306 core.mem_write(e | (d << 8), a); 1307 }; 1308 // 0x13 : INC DE 1309 instructions[0x13] = function() 1310 { 1311 var result = (e | (d << 8)); 1312 result += 1; 1313 e = result & 0xff; 1314 d = (result & 0xff00) >>> 8; 1315 }; 1316 // 0x14 : INC D 1317 instructions[0x14] = function() 1318 { 1319 d = do_inc(d); 1320 }; 1321 // 0x15 : DEC D 1322 instructions[0x15] = function() 1323 { 1324 d = do_dec(d); 1325 }; 1326 // 0x16 : LD D, n 1327 instructions[0x16] = function() 1328 { 1329 pc = (pc + 1) & 0xffff; 1330 d = core.mem_read(pc); 1331 }; 1332 // 0x17 : RLA 1333 instructions[0x17] = function() 1334 { 1335 var temp_s = flags.S, temp_z = flags.Z, temp_p = flags.P; 1336 a = do_rl(a); 1337 flags.S = temp_s; 1338 flags.Z = temp_z; 1339 flags.P = temp_p; 1340 }; 1341 // 0x18 : JR n 1342 instructions[0x18] = function() 1343 { 1344 var offset = get_signed_offset_byte(core.mem_read((pc + 1) & 0xffff)); 1345 pc = (pc + offset + 1) & 0xffff; 1346 }; 1347 // 0x19 : ADD HL, DE 1348 instructions[0x19] = function() 1349 { 1350 do_hl_add(e | (d << 8)); 1351 }; 1352 // 0x1a : LD A, (DE) 1353 instructions[0x1a] = function() 1354 { 1355 a = core.mem_read(e | (d << 8)); 1356 }; 1357 // 0x1b : DEC DE 1358 instructions[0x1b] = function() 1359 { 1360 var result = (e | (d << 8)); 1361 result -= 1; 1362 e = result & 0xff; 1363 d = (result & 0xff00) >>> 8; 1364 }; 1365 // 0x1c : INC E 1366 instructions[0x1c] = function() 1367 { 1368 e = do_inc(e); 1369 }; 1370 // 0x1d : DEC E 1371 instructions[0x1d] = function() 1372 { 1373 e = do_dec(e); 1374 }; 1375 // 0x1e : LD E, n 1376 instructions[0x1e] = function() 1377 { 1378 pc = (pc + 1) & 0xffff; 1379 e = core.mem_read(pc); 1380 }; 1381 // 0x1f : RRA 1382 instructions[0x1f] = function() 1383 { 1384 var temp_s = flags.S, temp_z = flags.Z, temp_p = flags.P; 1385 a = do_rr(a); 1386 flags.S = temp_s; 1387 flags.Z = temp_z; 1388 flags.P = temp_p; 1389 }; 1390 // 0x20 : JR NZ, n 1391 instructions[0x20] = function() 1392 { 1393 do_conditional_relative_jump(!flags.Z); 1394 }; 1395 // 0x21 : LD HL, nn 1396 instructions[0x21] = function() 1397 { 1398 pc = (pc + 1) & 0xffff; 1399 l = core.mem_read(pc); 1400 pc = (pc + 1) & 0xffff; 1401 h = core.mem_read(pc); 1402 }; 1403 // 0x22 : LD (nn), HL 1404 instructions[0x22] = function() 1405 { 1406 pc = (pc + 1) & 0xffff; 1407 var address = core.mem_read(pc); 1408 pc = (pc + 1) & 0xffff; 1409 address |= core.mem_read(pc) << 8; 1410 1411 core.mem_write(address, l); 1412 core.mem_write((address + 1) & 0xffff, h); 1413 }; 1414 // 0x23 : INC HL 1415 instructions[0x23] = function() 1416 { 1417 var result = (l | (h << 8)); 1418 result += 1; 1419 l = result & 0xff; 1420 h = (result & 0xff00) >>> 8; 1421 }; 1422 // 0x24 : INC H 1423 instructions[0x24] = function() 1424 { 1425 h = do_inc(h); 1426 }; 1427 // 0x25 : DEC H 1428 instructions[0x25] = function() 1429 { 1430 h = do_dec(h); 1431 }; 1432 // 0x26 : LD H, n 1433 instructions[0x26] = function() 1434 { 1435 pc = (pc + 1) & 0xffff; 1436 h = core.mem_read(pc); 1437 }; 1438 // 0x27 : DAA 1439 instructions[0x27] = function() 1440 { 1441 var temp = a; 1442 if (!flags.N) 1443 { 1444 if (flags.H || ((a & 0x0f) > 9)) 1445 temp += 0x06; 1446 if (flags.C || (a > 0x99)) 1447 temp += 0x60; 1448 } 1449 else 1450 { 1451 if (flags.H || ((a & 0x0f) > 9)) 1452 temp -= 0x06; 1453 if (flags.C || (a > 0x99)) 1454 temp -= 0x60; 1455 } 1456 1457 flags.S = (temp & 0x80) ? 1 : 0; 1458 flags.Z = !(temp & 0xff) ? 1 : 0; 1459 flags.H = ((a & 0x10) ^ (temp & 0x10)) ? 1 : 0; 1460 flags.P = get_parity(temp & 0xff); 1461 // DAA never clears the carry flag if it was already set, 1462 // but it is able to set the carry flag if it was clear. 1463 // Don't ask me, I don't know. 1464 // Note also that we check for a BCD carry, instead of the usual. 1465 flags.C = (flags.C || (a > 0x99)) ? 1 : 0; 1466 1467 a = temp & 0xff; 1468 1469 update_xy_flags(a); 1470 }; 1471 // 0x28 : JR Z, n 1472 instructions[0x28] = function() 1473 { 1474 do_conditional_relative_jump(!!flags.Z); 1475 }; 1476 // 0x29 : ADD HL, HL 1477 instructions[0x29] = function() 1478 { 1479 do_hl_add(l | (h << 8)); 1480 }; 1481 // 0x2a : LD HL, (nn) 1482 instructions[0x2a] = function() 1483 { 1484 pc = (pc + 1) & 0xffff; 1485 var address = core.mem_read(pc); 1486 pc = (pc + 1) & 0xffff; 1487 address |= core.mem_read(pc) << 8; 1488 1489 l = core.mem_read(address); 1490 h = core.mem_read((address + 1) & 0xffff); 1491 }; 1492 // 0x2b : DEC HL 1493 instructions[0x2b] = function() 1494 { 1495 var result = (l | (h << 8)); 1496 result -= 1; 1497 l = result & 0xff; 1498 h = (result & 0xff00) >>> 8; 1499 }; 1500 // 0x2c : INC L 1501 instructions[0x2c] = function() 1502 { 1503 l = do_inc(l); 1504 }; 1505 // 0x2d : DEC L 1506 instructions[0x2d] = function() 1507 { 1508 l = do_dec(l); 1509 }; 1510 // 0x2e : LD L, n 1511 instructions[0x2e] = function() 1512 { 1513 pc = (pc + 1) & 0xffff; 1514 l = core.mem_read(pc); 1515 }; 1516 // 0x2f : CPL 1517 instructions[0x2f] = function() 1518 { 1519 a = (~a) & 0xff; 1520 flags.N = 1; 1521 flags.H = 1; 1522 update_xy_flags(a); 1523 }; 1524 // 0x30 : JR NC, n 1525 instructions[0x30] = function() 1526 { 1527 do_conditional_relative_jump(!flags.C); 1528 }; 1529 // 0x31 : LD SP, nn 1530 instructions[0x31] = function() 1531 { 1532 sp = core.mem_read((pc + 1) & 0xffff) | 1533 (core.mem_read((pc + 2) & 0xffff) << 8); 1534 pc = (pc + 2) & 0xffff; 1535 }; 1536 // 0x32 : LD (nn), A 1537 instructions[0x32] = function() 1538 { 1539 pc = (pc + 1) & 0xffff; 1540 var address = core.mem_read(pc); 1541 pc = (pc + 1) & 0xffff; 1542 address |= core.mem_read(pc) << 8; 1543 1544 core.mem_write(address, a); 1545 }; 1546 // 0x33 : INC SP 1547 instructions[0x33] = function() 1548 { 1549 sp = (sp + 1) & 0xffff; 1550 }; 1551 // 0x34 : INC (HL) 1552 instructions[0x34] = function() 1553 { 1554 var address = l | (h << 8); 1555 core.mem_write(address, do_inc(core.mem_read(address))); 1556 }; 1557 // 0x35 : DEC (HL) 1558 instructions[0x35] = function() 1559 { 1560 var address = l | (h << 8); 1561 core.mem_write(address, do_dec(core.mem_read(address))); 1562 }; 1563 // 0x36 : LD (HL), n 1564 instructions[0x36] = function() 1565 { 1566 pc = (pc + 1) & 0xffff; 1567 core.mem_write(l | (h << 8), core.mem_read(pc)); 1568 }; 1569 // 0x37 : SCF 1570 instructions[0x37] = function() 1571 { 1572 flags.N = 0; 1573 flags.H = 0; 1574 flags.C = 1; 1575 update_xy_flags(a); 1576 }; 1577 // 0x38 : JR C, n 1578 instructions[0x38] = function() 1579 { 1580 do_conditional_relative_jump(!!flags.C); 1581 }; 1582 // 0x39 : ADD HL, SP 1583 instructions[0x39] = function() 1584 { 1585 do_hl_add(sp); 1586 }; 1587 // 0x3a : LD A, (nn) 1588 instructions[0x3a] = function() 1589 { 1590 pc = (pc + 1) & 0xffff; 1591 var address = core.mem_read(pc); 1592 pc = (pc + 1) & 0xffff; 1593 address |= core.mem_read(pc) << 8; 1594 1595 a = core.mem_read(address); 1596 }; 1597 // 0x3b : DEC SP 1598 instructions[0x3b] = function() 1599 { 1600 sp = (sp - 1) & 0xffff; 1601 }; 1602 // 0x3c : INC A 1603 instructions[0x3c] = function() 1604 { 1605 a = do_inc(a); 1606 }; 1607 // 0x3d : DEC A 1608 instructions[0x3d] = function() 1609 { 1610 a = do_dec(a); 1611 }; 1612 // 0x3e : LD A, n 1613 instructions[0x3e] = function() 1614 { 1615 a = core.mem_read((pc + 1) & 0xffff); 1616 pc = (pc + 1) & 0xffff; 1617 }; 1618 // 0x3f : CCF 1619 instructions[0x3f] = function() 1620 { 1621 flags.N = 0; 1622 flags.H = flags.C; 1623 flags.C = flags.C ? 0 : 1; 1624 update_xy_flags(a); 1625 }; 1626 // 0xc0 : RET NZ 1627 instructions[0xc0] = function() 1628 { 1629 do_conditional_return(!flags.Z); 1630 }; 1631 // 0xc1 : POP BC 1632 instructions[0xc1] = function() 1633 { 1634 var result = pop_word(); 1635 c = result & 0xff; 1636 b = (result & 0xff00) >>> 8; 1637 }; 1638 // 0xc2 : JP NZ, nn 1639 instructions[0xc2] = function() 1640 { 1641 do_conditional_absolute_jump(!flags.Z); 1642 }; 1643 // 0xc3 : JP nn 1644 instructions[0xc3] = function() 1645 { 1646 pc = core.mem_read((pc + 1) & 0xffff) | 1647 (core.mem_read((pc + 2) & 0xffff) << 8); 1648 pc = (pc - 1) & 0xffff; 1649 }; 1650 // 0xc4 : CALL NZ, nn 1651 instructions[0xc4] = function() 1652 { 1653 do_conditional_call(!flags.Z); 1654 }; 1655 // 0xc5 : PUSH BC 1656 instructions[0xc5] = function() 1657 { 1658 push_word(c | (b << 8)); 1659 }; 1660 // 0xc6 : ADD A, n 1661 instructions[0xc6] = function() 1662 { 1663 pc = (pc + 1) & 0xffff; 1664 do_add(core.mem_read(pc)); 1665 }; 1666 // 0xc7 : RST 00h 1667 instructions[0xc7] = function() 1668 { 1669 do_reset(0x00); 1670 }; 1671 // 0xc8 : RET Z 1672 instructions[0xc8] = function() 1673 { 1674 do_conditional_return(!!flags.Z); 1675 }; 1676 // 0xc9 : RET 1677 instructions[0xc9] = function() 1678 { 1679 pc = (pop_word() - 1) & 0xffff; 1680 }; 1681 // 0xca : JP Z, nn 1682 instructions[0xca] = function() 1683 { 1684 do_conditional_absolute_jump(!!flags.Z); 1685 }; 1686 // 0xcb : CB Prefix 1687 instructions[0xcb] = function() 1688 { 1689 // R is incremented at the start of the second instruction cycle, 1690 // before the instruction actually runs. 1691 // The high bit of R is not affected by this increment, 1692 // it can only be changed using the LD R, A instruction. 1693 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 1694 1695 // We don't have a table for this prefix, 1696 // the instructions are all so uniform that we can directly decode them. 1697 pc = (pc + 1) & 0xffff; 1698 var opcode = core.mem_read(pc), 1699 bit_number = (opcode & 0x38) >>> 3, 1700 reg_code = opcode & 0x07; 1701 1702 if (opcode < 0x40) 1703 { 1704 // Shift/rotate instructions 1705 var op_array = [do_rlc, do_rrc, do_rl, do_rr, 1706 do_sla, do_sra, do_sll, do_srl]; 1707 1708 if (reg_code === 0) 1709 b = op_array[bit_number]( b); 1710 else if (reg_code === 1) 1711 c = op_array[bit_number]( c); 1712 else if (reg_code === 2) 1713 d = op_array[bit_number]( d); 1714 else if (reg_code === 3) 1715 e = op_array[bit_number]( e); 1716 else if (reg_code === 4) 1717 h = op_array[bit_number]( h); 1718 else if (reg_code === 5) 1719 l = op_array[bit_number]( l); 1720 else if (reg_code === 6) 1721 core.mem_write(l | (h << 8), 1722 op_array[bit_number]( core.mem_read(l | (h << 8)))); 1723 else if (reg_code === 7) 1724 a = op_array[bit_number]( a); 1725 } 1726 else if (opcode < 0x80) 1727 { 1728 // BIT instructions 1729 if (reg_code === 0) 1730 flags.Z = !(b & (1 << bit_number)) ? 1 : 0; 1731 else if (reg_code === 1) 1732 flags.Z = !(c & (1 << bit_number)) ? 1 : 0; 1733 else if (reg_code === 2) 1734 flags.Z = !(d & (1 << bit_number)) ? 1 : 0; 1735 else if (reg_code === 3) 1736 flags.Z = !(e & (1 << bit_number)) ? 1 : 0; 1737 else if (reg_code === 4) 1738 flags.Z = !(h & (1 << bit_number)) ? 1 : 0; 1739 else if (reg_code === 5) 1740 flags.Z = !(l & (1 << bit_number)) ? 1 : 0; 1741 else if (reg_code === 6) 1742 flags.Z = !((core.mem_read(l | (h << 8))) & (1 << bit_number)) ? 1 : 0; 1743 else if (reg_code === 7) 1744 flags.Z = !(a & (1 << bit_number)) ? 1 : 0; 1745 1746 flags.N = 0; 1747 flags.H = 1; 1748 flags.P = flags.Z; 1749 flags.S = ((bit_number === 7) && !flags.Z) ? 1 : 0; 1750 // For the BIT n, (HL) instruction, the X and Y flags are obtained 1751 // from what is apparently an internal temporary register used for 1752 // some of the 16-bit arithmetic instructions. 1753 // I haven't implemented that register here, 1754 // so for now we'll set X and Y the same way for every BIT opcode, 1755 // which means that they will usually be wrong for BIT n, (HL). 1756 flags.Y = ((bit_number === 5) && !flags.Z) ? 1 : 0; 1757 flags.X = ((bit_number === 3) && !flags.Z) ? 1 : 0; 1758 } 1759 else if (opcode < 0xc0) 1760 { 1761 // RES instructions 1762 if (reg_code === 0) 1763 b &= (0xff & ~(1 << bit_number)); 1764 else if (reg_code === 1) 1765 c &= (0xff & ~(1 << bit_number)); 1766 else if (reg_code === 2) 1767 d &= (0xff & ~(1 << bit_number)); 1768 else if (reg_code === 3) 1769 e &= (0xff & ~(1 << bit_number)); 1770 else if (reg_code === 4) 1771 h &= (0xff & ~(1 << bit_number)); 1772 else if (reg_code === 5) 1773 l &= (0xff & ~(1 << bit_number)); 1774 else if (reg_code === 6) 1775 core.mem_write(l | (h << 8), 1776 core.mem_read(l | (h << 8)) & ~(1 << bit_number)); 1777 else if (reg_code === 7) 1778 a &= (0xff & ~(1 << bit_number)); 1779 } 1780 else 1781 { 1782 // SET instructions 1783 if (reg_code === 0) 1784 b |= (1 << bit_number); 1785 else if (reg_code === 1) 1786 c |= (1 << bit_number); 1787 else if (reg_code === 2) 1788 d |= (1 << bit_number); 1789 else if (reg_code === 3) 1790 e |= (1 << bit_number); 1791 else if (reg_code === 4) 1792 h |= (1 << bit_number); 1793 else if (reg_code === 5) 1794 l |= (1 << bit_number); 1795 else if (reg_code === 6) 1796 core.mem_write(l | (h << 8), 1797 core.mem_read(l | (h << 8)) | (1 << bit_number)); 1798 else if (reg_code === 7) 1799 a |= (1 << bit_number); 1800 } 1801 1802 cycle_counter += cycle_counts_cb[opcode]; 1803 }; 1804 // 0xcc : CALL Z, nn 1805 instructions[0xcc] = function() 1806 { 1807 do_conditional_call(!!flags.Z); 1808 }; 1809 // 0xcd : CALL nn 1810 instructions[0xcd] = function() 1811 { 1812 push_word((pc + 3) & 0xffff); 1813 pc = core.mem_read((pc + 1) & 0xffff) | 1814 (core.mem_read((pc + 2) & 0xffff) << 8); 1815 pc = (pc - 1) & 0xffff; 1816 }; 1817 // 0xce : ADC A, n 1818 instructions[0xce] = function() 1819 { 1820 pc = (pc + 1) & 0xffff; 1821 do_adc(core.mem_read(pc)); 1822 }; 1823 // 0xcf : RST 08h 1824 instructions[0xcf] = function() 1825 { 1826 do_reset(0x08); 1827 }; 1828 // 0xd0 : RET NC 1829 instructions[0xd0] = function() 1830 { 1831 do_conditional_return(!flags.C); 1832 }; 1833 // 0xd1 : POP DE 1834 instructions[0xd1] = function() 1835 { 1836 var result = pop_word(); 1837 e = result & 0xff; 1838 d = (result & 0xff00) >>> 8; 1839 }; 1840 // 0xd2 : JP NC, nn 1841 instructions[0xd2] = function() 1842 { 1843 do_conditional_absolute_jump(!flags.C); 1844 }; 1845 // 0xd3 : OUT (n), A 1846 instructions[0xd3] = function() 1847 { 1848 pc = (pc + 1) & 0xffff; 1849 core.io_write((a << 8) | core.mem_read(pc), a); 1850 }; 1851 // 0xd4 : CALL NC, nn 1852 instructions[0xd4] = function() 1853 { 1854 do_conditional_call(!flags.C); 1855 }; 1856 // 0xd5 : PUSH DE 1857 instructions[0xd5] = function() 1858 { 1859 push_word(e | (d << 8)); 1860 }; 1861 // 0xd6 : SUB n 1862 instructions[0xd6] = function() 1863 { 1864 pc = (pc + 1) & 0xffff; 1865 do_sub(core.mem_read(pc)); 1866 }; 1867 // 0xd7 : RST 10h 1868 instructions[0xd7] = function() 1869 { 1870 do_reset(0x10); 1871 }; 1872 // 0xd8 : RET C 1873 instructions[0xd8] = function() 1874 { 1875 do_conditional_return(!!flags.C); 1876 }; 1877 // 0xd9 : EXX 1878 instructions[0xd9] = function() 1879 { 1880 var temp = b; 1881 b = b_prime; 1882 b_prime = temp; 1883 temp = c; 1884 c = c_prime; 1885 c_prime = temp; 1886 temp = d; 1887 d = d_prime; 1888 d_prime = temp; 1889 temp = e; 1890 e = e_prime; 1891 e_prime = temp; 1892 temp = h; 1893 h = h_prime; 1894 h_prime = temp; 1895 temp = l; 1896 l = l_prime; 1897 l_prime = temp; 1898 }; 1899 // 0xda : JP C, nn 1900 instructions[0xda] = function() 1901 { 1902 do_conditional_absolute_jump(!!flags.C); 1903 }; 1904 // 0xdb : IN A, (n) 1905 instructions[0xdb] = function() 1906 { 1907 pc = (pc + 1) & 0xffff; 1908 a = core.io_read((a << 8) | core.mem_read(pc)); 1909 }; 1910 // 0xdc : CALL C, nn 1911 instructions[0xdc] = function() 1912 { 1913 do_conditional_call(!!flags.C); 1914 }; 1915 // 0xdd : DD Prefix (IX instructions) 1916 instructions[0xdd] = function() 1917 { 1918 // R is incremented at the start of the second instruction cycle, 1919 // before the instruction actually runs. 1920 // The high bit of R is not affected by this increment, 1921 // it can only be changed using the LD R, A instruction. 1922 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 1923 1924 pc = (pc + 1) & 0xffff; 1925 var opcode = core.mem_read(pc), 1926 func = dd_instructions[opcode]; 1927 1928 if (func) 1929 { 1930 //func = func.bind(this); 1931 func(); 1932 cycle_counter += cycle_counts_dd[opcode]; 1933 } 1934 else 1935 { 1936 // Apparently if a DD opcode doesn't exist, 1937 // it gets treated as an unprefixed opcode. 1938 // What we'll do to handle that is just back up the 1939 // program counter, so that this byte gets decoded 1940 // as a normal instruction. 1941 pc = (pc - 1) & 0xffff; 1942 // And we'll add in the cycle count for a NOP. 1943 cycle_counter += cycle_counts[0]; 1944 } 1945 }; 1946 // 0xde : SBC n 1947 instructions[0xde] = function() 1948 { 1949 pc = (pc + 1) & 0xffff; 1950 do_sbc(core.mem_read(pc)); 1951 }; 1952 // 0xdf : RST 18h 1953 instructions[0xdf] = function() 1954 { 1955 do_reset(0x18); 1956 }; 1957 // 0xe0 : RET PO 1958 instructions[0xe0] = function() 1959 { 1960 do_conditional_return(!flags.P); 1961 }; 1962 // 0xe1 : POP HL 1963 instructions[0xe1] = function() 1964 { 1965 var result = pop_word(); 1966 l = result & 0xff; 1967 h = (result & 0xff00) >>> 8; 1968 }; 1969 // 0xe2 : JP PO, (nn) 1970 instructions[0xe2] = function() 1971 { 1972 do_conditional_absolute_jump(!flags.P); 1973 }; 1974 // 0xe3 : EX (SP), HL 1975 instructions[0xe3] = function() 1976 { 1977 var temp = core.mem_read(sp); 1978 core.mem_write(sp, l); 1979 l = temp; 1980 temp = core.mem_read((sp + 1) & 0xffff); 1981 core.mem_write((sp + 1) & 0xffff, h); 1982 h = temp; 1983 }; 1984 // 0xe4 : CALL PO, nn 1985 instructions[0xe4] = function() 1986 { 1987 do_conditional_call(!flags.P); 1988 }; 1989 // 0xe5 : PUSH HL 1990 instructions[0xe5] = function() 1991 { 1992 push_word(l | (h << 8)); 1993 }; 1994 // 0xe6 : AND n 1995 instructions[0xe6] = function() 1996 { 1997 pc = (pc + 1) & 0xffff; 1998 do_and(core.mem_read(pc)); 1999 }; 2000 // 0xe7 : RST 20h 2001 instructions[0xe7] = function() 2002 { 2003 do_reset(0x20); 2004 }; 2005 // 0xe8 : RET PE 2006 instructions[0xe8] = function() 2007 { 2008 do_conditional_return(!!flags.P); 2009 }; 2010 // 0xe9 : JP (HL) 2011 instructions[0xe9] = function() 2012 { 2013 pc = l | (h << 8); 2014 pc = (pc - 1) & 0xffff; 2015 }; 2016 // 0xea : JP PE, nn 2017 instructions[0xea] = function() 2018 { 2019 do_conditional_absolute_jump(!!flags.P); 2020 }; 2021 // 0xeb : EX DE, HL 2022 instructions[0xeb] = function() 2023 { 2024 var temp = d; 2025 d = h; 2026 h = temp; 2027 temp = e; 2028 e = l; 2029 l = temp; 2030 }; 2031 // 0xec : CALL PE, nn 2032 instructions[0xec] = function() 2033 { 2034 do_conditional_call(!!flags.P); 2035 }; 2036 // 0xed : ED Prefix 2037 instructions[0xed] = function() 2038 { 2039 // R is incremented at the start of the second instruction cycle, 2040 // before the instruction actually runs. 2041 // The high bit of R is not affected by this increment, 2042 // it can only be changed using the LD R, A instruction. 2043 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 2044 2045 pc = (pc + 1) & 0xffff; 2046 var opcode = core.mem_read(pc), 2047 func = ed_instructions[opcode]; 2048 2049 if (func) 2050 { 2051 //func = func.bind(this); 2052 func(); 2053 cycle_counter += cycle_counts_ed[opcode]; 2054 } 2055 else 2056 { 2057 // If the opcode didn't exist, the whole thing is a two-byte NOP. 2058 cycle_counter += cycle_counts[0]; 2059 } 2060 }; 2061 // 0xee : XOR n 2062 instructions[0xee] = function() 2063 { 2064 pc = (pc + 1) & 0xffff; 2065 do_xor(core.mem_read(pc)); 2066 }; 2067 // 0xef : RST 28h 2068 instructions[0xef] = function() 2069 { 2070 do_reset(0x28); 2071 }; 2072 // 0xf0 : RET P 2073 instructions[0xf0] = function() 2074 { 2075 do_conditional_return(!flags.S); 2076 }; 2077 // 0xf1 : POP AF 2078 instructions[0xf1] = function() 2079 { 2080 var result = pop_word(); 2081 set_flags_register(result & 0xff); 2082 a = (result & 0xff00) >>> 8; 2083 }; 2084 // 0xf2 : JP P, nn 2085 instructions[0xf2] = function() 2086 { 2087 do_conditional_absolute_jump(!flags.S); 2088 }; 2089 // 0xf3 : DI 2090 instructions[0xf3] = function() 2091 { 2092 // DI doesn't actually take effect until after the next instruction. 2093 do_delayed_di = true; 2094 }; 2095 // 0xf4 : CALL P, nn 2096 instructions[0xf4] = function() 2097 { 2098 do_conditional_call(!flags.S); 2099 }; 2100 // 0xf5 : PUSH AF 2101 instructions[0xf5] = function() 2102 { 2103 push_word(get_flags_register() | (a << 8)); 2104 }; 2105 // 0xf6 : OR n 2106 instructions[0xf6] = function() 2107 { 2108 pc = (pc + 1) & 0xffff; 2109 do_or(core.mem_read(pc)); 2110 }; 2111 // 0xf7 : RST 30h 2112 instructions[0xf7] = function() 2113 { 2114 do_reset(0x30); 2115 }; 2116 // 0xf8 : RET M 2117 instructions[0xf8] = function() 2118 { 2119 do_conditional_return(!!flags.S); 2120 }; 2121 // 0xf9 : LD SP, HL 2122 instructions[0xf9] = function() 2123 { 2124 sp = l | (h << 8); 2125 }; 2126 // 0xfa : JP M, nn 2127 instructions[0xfa] = function() 2128 { 2129 do_conditional_absolute_jump(!!flags.S); 2130 }; 2131 // 0xfb : EI 2132 instructions[0xfb] = function() 2133 { 2134 // EI doesn't actually take effect until after the next instruction. 2135 do_delayed_ei = true; 2136 }; 2137 // 0xfc : CALL M, nn 2138 instructions[0xfc] = function() 2139 { 2140 do_conditional_call(!!flags.S); 2141 }; 2142 // 0xfd : FD Prefix (IY instructions) 2143 instructions[0xfd] = function() 2144 { 2145 // R is incremented at the start of the second instruction cycle, 2146 // before the instruction actually runs. 2147 // The high bit of R is not affected by this increment, 2148 // it can only be changed using the LD R, A instruction. 2149 r = (r & 0x80) | (((r & 0x7f) + 1) & 0x7f); 2150 2151 pc = (pc + 1) & 0xffff; 2152 var opcode = core.mem_read(pc), 2153 func = dd_instructions[opcode]; 2154 2155 if (func) 2156 { 2157 // Rather than copy and paste all the IX instructions into IY instructions, 2158 // what we'll do is sneakily copy IY into IX, run the IX instruction, 2159 // and then copy the result into IY and restore the old IX. 2160 var temp = ix; 2161 ix = iy; 2162 //func = func.bind(this); 2163 func(); 2164 iy = ix; 2165 ix = temp; 2166 2167 cycle_counter += cycle_counts_dd[opcode]; 2168 } 2169 else 2170 { 2171 // Apparently if an FD opcode doesn't exist, 2172 // it gets treated as an unprefixed opcode. 2173 // What we'll do to handle that is just back up the 2174 // program counter, so that this byte gets decoded 2175 // as a normal instruction. 2176 pc = (pc - 1) & 0xffff; 2177 // And we'll add in the cycle count for a NOP. 2178 cycle_counter += cycle_counts[0]; 2179 } 2180 }; 2181 // 0xfe : CP n 2182 instructions[0xfe] = function() 2183 { 2184 pc = (pc + 1) & 0xffff; 2185 do_cp(core.mem_read(pc)); 2186 }; 2187 // 0xff : RST 38h 2188 instructions[0xff] = function() 2189 { 2190 do_reset(0x38); 2191 }; 2192 2193 2194 /////////////////////////////////////////////////////////////////////////////// 2195 /// This table of ED opcodes is pretty sparse; 2196 /// there are not very many valid ED-prefixed opcodes in the Z80, 2197 /// and many of the ones that are valid are not documented. 2198 /////////////////////////////////////////////////////////////////////////////// 2199 let ed_instructions = []; 2200 // 0x40 : IN B, (C) 2201 ed_instructions[0x40] = function() 2202 { 2203 b = do_in((b << 8) | c); 2204 }; 2205 // 0x41 : OUT (C), B 2206 ed_instructions[0x41] = function() 2207 { 2208 core.io_write((b << 8) | c, b); 2209 }; 2210 // 0x42 : SBC HL, BC 2211 ed_instructions[0x42] = function() 2212 { 2213 do_hl_sbc(c | (b << 8)); 2214 }; 2215 // 0x43 : LD (nn), BC 2216 ed_instructions[0x43] = function() 2217 { 2218 pc = (pc + 1) & 0xffff; 2219 var address = core.mem_read(pc); 2220 pc = (pc + 1) & 0xffff; 2221 address |= core.mem_read(pc) << 8; 2222 2223 core.mem_write(address, c); 2224 core.mem_write((address + 1) & 0xffff, b); 2225 }; 2226 // 0x44 : NEG 2227 ed_instructions[0x44] = function() 2228 { 2229 do_neg(); 2230 }; 2231 // 0x45 : RETN 2232 ed_instructions[0x45] = function() 2233 { 2234 pc = (pop_word() - 1) & 0xffff; 2235 iff1 = iff2; 2236 }; 2237 // 0x46 : IM 0 2238 ed_instructions[0x46] = function() 2239 { 2240 imode = 0; 2241 }; 2242 // 0x47 : LD I, A 2243 ed_instructions[0x47] = function() 2244 { 2245 i = a 2246 }; 2247 // 0x48 : IN C, (C) 2248 ed_instructions[0x48] = function() 2249 { 2250 c = do_in((b << 8) | c); 2251 }; 2252 // 0x49 : OUT (C), C 2253 ed_instructions[0x49] = function() 2254 { 2255 core.io_write((b << 8) | c, c); 2256 }; 2257 // 0x4a : ADC HL, BC 2258 ed_instructions[0x4a] = function() 2259 { 2260 do_hl_adc(c | (b << 8)); 2261 }; 2262 // 0x4b : LD BC, (nn) 2263 ed_instructions[0x4b] = function() 2264 { 2265 pc = (pc + 1) & 0xffff; 2266 var address = core.mem_read(pc); 2267 pc = (pc + 1) & 0xffff; 2268 address |= core.mem_read(pc) << 8; 2269 2270 c = core.mem_read(address); 2271 b = core.mem_read((address + 1) & 0xffff); 2272 }; 2273 // 0x4c : NEG (Undocumented) 2274 ed_instructions[0x4c] = function() 2275 { 2276 do_neg(); 2277 }; 2278 // 0x4d : RETI 2279 ed_instructions[0x4d] = function() 2280 { 2281 pc = (pop_word() - 1) & 0xffff; 2282 }; 2283 // 0x4e : IM 0 (Undocumented) 2284 ed_instructions[0x4e] = function() 2285 { 2286 imode = 0; 2287 }; 2288 // 0x4f : LD R, A 2289 ed_instructions[0x4f] = function() 2290 { 2291 r = a; 2292 }; 2293 // 0x50 : IN D, (C) 2294 ed_instructions[0x50] = function() 2295 { 2296 d = do_in((b << 8) | c); 2297 }; 2298 // 0x51 : OUT (C), D 2299 ed_instructions[0x51] = function() 2300 { 2301 core.io_write((b << 8) | c, d); 2302 }; 2303 // 0x52 : SBC HL, DE 2304 ed_instructions[0x52] = function() 2305 { 2306 do_hl_sbc(e | (d << 8)); 2307 }; 2308 // 0x53 : LD (nn), DE 2309 ed_instructions[0x53] = function() 2310 { 2311 pc = (pc + 1) & 0xffff; 2312 var address = core.mem_read(pc); 2313 pc = (pc + 1) & 0xffff; 2314 address |= core.mem_read(pc) << 8; 2315 2316 core.mem_write(address, e); 2317 core.mem_write((address + 1) & 0xffff, d); 2318 }; 2319 // 0x54 : NEG (Undocumented) 2320 ed_instructions[0x54] = function() 2321 { 2322 do_neg(); 2323 }; 2324 // 0x55 : RETN 2325 ed_instructions[0x55] = function() 2326 { 2327 pc = (pop_word() - 1) & 0xffff; 2328 iff1 = iff2; 2329 }; 2330 // 0x56 : IM 1 2331 ed_instructions[0x56] = function() 2332 { 2333 imode = 1; 2334 }; 2335 // 0x57 : LD A, I 2336 ed_instructions[0x57] = function() 2337 { 2338 a = i; 2339 flags.S = a & 0x80 ? 1 : 0; 2340 flags.Z = a ? 0 : 1; 2341 flags.H = 0; 2342 flags.P = iff2; 2343 flags.N = 0; 2344 update_xy_flags(a); 2345 }; 2346 // 0x58 : IN E, (C) 2347 ed_instructions[0x58] = function() 2348 { 2349 e = do_in((b << 8) | c); 2350 }; 2351 // 0x59 : OUT (C), E 2352 ed_instructions[0x59] = function() 2353 { 2354 core.io_write((b << 8) | c, e); 2355 }; 2356 // 0x5a : ADC HL, DE 2357 ed_instructions[0x5a] = function() 2358 { 2359 do_hl_adc(e | (d << 8)); 2360 }; 2361 // 0x5b : LD DE, (nn) 2362 ed_instructions[0x5b] = function() 2363 { 2364 pc = (pc + 1) & 0xffff; 2365 var address = core.mem_read(pc); 2366 pc = (pc + 1) & 0xffff; 2367 address |= core.mem_read(pc) << 8; 2368 2369 e = core.mem_read(address); 2370 d = core.mem_read((address + 1) & 0xffff); 2371 }; 2372 // 0x5c : NEG (Undocumented) 2373 ed_instructions[0x5c] = function() 2374 { 2375 do_neg(); 2376 }; 2377 // 0x5d : RETN 2378 ed_instructions[0x5d] = function() 2379 { 2380 pc = (pop_word() - 1) & 0xffff; 2381 iff1 = iff2; 2382 }; 2383 // 0x5e : IM 2 2384 ed_instructions[0x5e] = function() 2385 { 2386 imode = 2; 2387 }; 2388 // 0x5f : LD A, R 2389 ed_instructions[0x5f] = function() 2390 { 2391 a = r; 2392 flags.S = a & 0x80 ? 1 : 0; 2393 flags.Z = a ? 0 : 1; 2394 flags.H = 0; 2395 flags.P = iff2; 2396 flags.N = 0; 2397 update_xy_flags(a); 2398 }; 2399 // 0x60 : IN H, (C) 2400 ed_instructions[0x60] = function() 2401 { 2402 h = do_in((b << 8) | c); 2403 }; 2404 // 0x61 : OUT (C), H 2405 ed_instructions[0x61] = function() 2406 { 2407 core.io_write((b << 8) | c, h); 2408 }; 2409 // 0x62 : SBC HL, HL 2410 ed_instructions[0x62] = function() 2411 { 2412 do_hl_sbc(l | (h << 8)); 2413 }; 2414 // 0x63 : LD (nn), HL (Undocumented) 2415 ed_instructions[0x63] = function() 2416 { 2417 pc = (pc + 1) & 0xffff; 2418 var address = core.mem_read(pc); 2419 pc = (pc + 1) & 0xffff; 2420 address |= core.mem_read(pc) << 8; 2421 2422 core.mem_write(address, l); 2423 core.mem_write((address + 1) & 0xffff, h); 2424 }; 2425 // 0x64 : NEG (Undocumented) 2426 ed_instructions[0x64] = function() 2427 { 2428 do_neg(); 2429 }; 2430 // 0x65 : RETN 2431 ed_instructions[0x65] = function() 2432 { 2433 pc = (pop_word() - 1) & 0xffff; 2434 iff1 = iff2; 2435 }; 2436 // 0x66 : IM 0 2437 ed_instructions[0x66] = function() 2438 { 2439 imode = 0; 2440 }; 2441 // 0x67 : RRD 2442 ed_instructions[0x67] = function() 2443 { 2444 var hl_value = core.mem_read(l | (h << 8)); 2445 var temp1 = hl_value & 0x0f, temp2 = a & 0x0f; 2446 hl_value = ((hl_value & 0xf0) >>> 4) | (temp2 << 4); 2447 a = (a & 0xf0) | temp1; 2448 core.mem_write(l | (h << 8), hl_value); 2449 2450 flags.S = (a & 0x80) ? 1 : 0; 2451 flags.Z = a ? 0 : 1; 2452 flags.H = 0; 2453 flags.P = get_parity(a) ? 1 : 0; 2454 flags.N = 0; 2455 update_xy_flags(a); 2456 }; 2457 // 0x68 : IN L, (C) 2458 ed_instructions[0x68] = function() 2459 { 2460 l = do_in((b << 8) | c); 2461 }; 2462 // 0x69 : OUT (C), L 2463 ed_instructions[0x69] = function() 2464 { 2465 core.io_write((b << 8) | c, l); 2466 }; 2467 // 0x6a : ADC HL, HL 2468 ed_instructions[0x6a] = function() 2469 { 2470 do_hl_adc(l | (h << 8)); 2471 }; 2472 // 0x6b : LD HL, (nn) (Undocumented) 2473 ed_instructions[0x6b] = function() 2474 { 2475 pc = (pc + 1) & 0xffff; 2476 var address = core.mem_read(pc); 2477 pc = (pc + 1) & 0xffff; 2478 address |= core.mem_read(pc) << 8; 2479 2480 l = core.mem_read(address); 2481 h = core.mem_read((address + 1) & 0xffff); 2482 }; 2483 // 0x6c : NEG (Undocumented) 2484 ed_instructions[0x6c] = function() 2485 { 2486 do_neg(); 2487 }; 2488 // 0x6d : RETN 2489 ed_instructions[0x6d] = function() 2490 { 2491 pc = (pop_word() - 1) & 0xffff; 2492 iff1 = iff2; 2493 }; 2494 // 0x6e : IM 0 (Undocumented) 2495 ed_instructions[0x6e] = function() 2496 { 2497 imode = 0; 2498 }; 2499 // 0x6f : RLD 2500 ed_instructions[0x6f] = function() 2501 { 2502 var hl_value = core.mem_read(l | (h << 8)); 2503 var temp1 = hl_value & 0xf0, temp2 = a & 0x0f; 2504 hl_value = ((hl_value & 0x0f) << 4) | temp2; 2505 a = (a & 0xf0) | (temp1 >>> 4); 2506 core.mem_write(l | (h << 8), hl_value); 2507 2508 flags.S = (a & 0x80) ? 1 : 0; 2509 flags.Z = a ? 0 : 1; 2510 flags.H = 0; 2511 flags.P = get_parity(a) ? 1 : 0; 2512 flags.N = 0; 2513 update_xy_flags(a); 2514 }; 2515 // 0x70 : IN (C) (Undocumented) 2516 ed_instructions[0x70] = function() 2517 { 2518 do_in((b << 8) | c); 2519 }; 2520 // 0x71 : OUT (C), 0 (Undocumented) 2521 ed_instructions[0x71] = function() 2522 { 2523 core.io_write((b << 8) | c, 0); 2524 }; 2525 // 0x72 : SBC HL, SP 2526 ed_instructions[0x72] = function() 2527 { 2528 do_hl_sbc(sp); 2529 }; 2530 // 0x73 : LD (nn), SP 2531 ed_instructions[0x73] = function() 2532 { 2533 pc = (pc + 1) & 0xffff; 2534 var address = core.mem_read(pc); 2535 pc = (pc + 1) & 0xffff; 2536 address |= core.mem_read(pc) << 8; 2537 2538 core.mem_write(address, sp & 0xff); 2539 core.mem_write((address + 1) & 0xffff, (sp >>> 8) & 0xff); 2540 }; 2541 // 0x74 : NEG (Undocumented) 2542 ed_instructions[0x74] = function() 2543 { 2544 do_neg(); 2545 }; 2546 // 0x75 : RETN 2547 ed_instructions[0x75] = function() 2548 { 2549 pc = (pop_word() - 1) & 0xffff; 2550 iff1 = iff2; 2551 }; 2552 // 0x76 : IM 1 2553 ed_instructions[0x76] = function() 2554 { 2555 imode = 1; 2556 }; 2557 // 0x78 : IN A, (C) 2558 ed_instructions[0x78] = function() 2559 { 2560 a = do_in((b << 8) | c); 2561 }; 2562 // 0x79 : OUT (C), A 2563 ed_instructions[0x79] = function() 2564 { 2565 core.io_write((b << 8) | c, a); 2566 }; 2567 // 0x7a : ADC HL, SP 2568 ed_instructions[0x7a] = function() 2569 { 2570 do_hl_adc(sp); 2571 }; 2572 // 0x7b : LD SP, (nn) 2573 ed_instructions[0x7b] = function() 2574 { 2575 pc = (pc + 1) & 0xffff; 2576 var address = core.mem_read(pc); 2577 pc = (pc + 1) & 0xffff; 2578 address |= core.mem_read(pc) << 8; 2579 2580 sp = core.mem_read(address); 2581 sp |= core.mem_read((address + 1) & 0xffff) << 8; 2582 }; 2583 // 0x7c : NEG (Undocumented) 2584 ed_instructions[0x7c] = function() 2585 { 2586 do_neg(); 2587 }; 2588 // 0x7d : RETN 2589 ed_instructions[0x7d] = function() 2590 { 2591 pc = (pop_word() - 1) & 0xffff; 2592 iff1 = iff2; 2593 }; 2594 // 0x7e : IM 2 2595 ed_instructions[0x7e] = function() 2596 { 2597 imode = 2; 2598 }; 2599 // 0xa0 : LDI 2600 ed_instructions[0xa0] = function() 2601 { 2602 do_ldi(); 2603 }; 2604 // 0xa1 : CPI 2605 ed_instructions[0xa1] = function() 2606 { 2607 do_cpi(); 2608 }; 2609 // 0xa2 : INI 2610 ed_instructions[0xa2] = function() 2611 { 2612 do_ini(); 2613 }; 2614 // 0xa3 : OUTI 2615 ed_instructions[0xa3] = function() 2616 { 2617 do_outi(); 2618 }; 2619 // 0xa8 : LDD 2620 ed_instructions[0xa8] = function() 2621 { 2622 do_ldd(); 2623 }; 2624 // 0xa9 : CPD 2625 ed_instructions[0xa9] = function() 2626 { 2627 do_cpd(); 2628 }; 2629 // 0xaa : IND 2630 ed_instructions[0xaa] = function() 2631 { 2632 do_ind(); 2633 }; 2634 // 0xab : OUTD 2635 ed_instructions[0xab] = function() 2636 { 2637 do_outd(); 2638 }; 2639 // 0xb0 : LDIR 2640 ed_instructions[0xb0] = function() 2641 { 2642 do_ldi(); 2643 if (b || c) 2644 { 2645 cycle_counter += 5; 2646 pc = (pc - 2) & 0xffff; 2647 } 2648 }; 2649 // 0xb1 : CPIR 2650 ed_instructions[0xb1] = function() 2651 { 2652 do_cpi(); 2653 if (!flags.Z && (b || c)) 2654 { 2655 cycle_counter += 5; 2656 pc = (pc - 2) & 0xffff; 2657 } 2658 }; 2659 // 0xb2 : INIR 2660 ed_instructions[0xb2] = function() 2661 { 2662 do_ini(); 2663 if (b) 2664 { 2665 cycle_counter += 5; 2666 pc = (pc - 2) & 0xffff; 2667 } 2668 }; 2669 // 0xb3 : OTIR 2670 ed_instructions[0xb3] = function() 2671 { 2672 do_outi(); 2673 if (b) 2674 { 2675 cycle_counter += 5; 2676 pc = (pc - 2) & 0xffff; 2677 } 2678 }; 2679 // 0xb8 : LDDR 2680 ed_instructions[0xb8] = function() 2681 { 2682 do_ldd(); 2683 if (b || c) 2684 { 2685 cycle_counter += 5; 2686 pc = (pc - 2) & 0xffff; 2687 } 2688 }; 2689 // 0xb9 : CPDR 2690 ed_instructions[0xb9] = function() 2691 { 2692 do_cpd(); 2693 if (!flags.Z && (b || c)) 2694 { 2695 cycle_counter += 5; 2696 pc = (pc - 2) & 0xffff; 2697 } 2698 }; 2699 // 0xba : INDR 2700 ed_instructions[0xba] = function() 2701 { 2702 do_ind(); 2703 if (b) 2704 { 2705 cycle_counter += 5; 2706 pc = (pc - 2) & 0xffff; 2707 } 2708 }; 2709 // 0xbb : OTDR 2710 ed_instructions[0xbb] = function() 2711 { 2712 do_outd(); 2713 if (b) 2714 { 2715 cycle_counter += 5; 2716 pc = (pc - 2) & 0xffff; 2717 } 2718 }; 2719 2720 2721 /////////////////////////////////////////////////////////////////////////////// 2722 /// Like ED, this table is quite sparse, 2723 /// and many of the opcodes here are also undocumented. 2724 /// The undocumented instructions here are those that deal with only one byte 2725 /// of the two-byte IX register; the bytes are designed IXH and IXL here. 2726 /////////////////////////////////////////////////////////////////////////////// 2727 let dd_instructions = []; 2728 // 0x09 : ADD IX, BC 2729 dd_instructions[0x09] = function() 2730 { 2731 do_ix_add(c | (b << 8)); 2732 }; 2733 // 0x19 : ADD IX, DE 2734 dd_instructions[0x19] = function() 2735 { 2736 do_ix_add(e | (d << 8)); 2737 }; 2738 // 0x21 : LD IX, nn 2739 dd_instructions[0x21] = function() 2740 { 2741 pc = (pc + 1) & 0xffff; 2742 ix = core.mem_read(pc); 2743 pc = (pc + 1) & 0xffff; 2744 ix |= (core.mem_read(pc) << 8); 2745 }; 2746 // 0x22 : LD (nn), IX 2747 dd_instructions[0x22] = function() 2748 { 2749 pc = (pc + 1) & 0xffff; 2750 var address = core.mem_read(pc); 2751 pc = (pc + 1) & 0xffff; 2752 address |= (core.mem_read(pc) << 8); 2753 2754 core.mem_write(address, ix & 0xff); 2755 core.mem_write((address + 1) & 0xffff, (ix >>> 8) & 0xff); 2756 }; 2757 // 0x23 : INC IX 2758 dd_instructions[0x23] = function() 2759 { 2760 ix = (ix + 1) & 0xffff; 2761 }; 2762 // 0x24 : INC IXH (Undocumented) 2763 dd_instructions[0x24] = function() 2764 { 2765 ix = (do_inc(ix >>> 8) << 8) | (ix & 0xff); 2766 }; 2767 // 0x25 : DEC IXH (Undocumented) 2768 dd_instructions[0x25] = function() 2769 { 2770 ix = (do_dec(ix >>> 8) << 8) | (ix & 0xff); 2771 }; 2772 // 0x26 : LD IXH, n (Undocumented) 2773 dd_instructions[0x26] = function() 2774 { 2775 pc = (pc + 1) & 0xffff; 2776 ix = (core.mem_read(pc) << 8) | (ix & 0xff); 2777 }; 2778 // 0x29 : ADD IX, IX 2779 dd_instructions[0x29] = function() 2780 { 2781 do_ix_add(ix); 2782 }; 2783 // 0x2a : LD IX, (nn) 2784 dd_instructions[0x2a] = function() 2785 { 2786 pc = (pc + 1) & 0xffff; 2787 var address = core.mem_read(pc); 2788 pc = (pc + 1) & 0xffff; 2789 address |= (core.mem_read(pc) << 8); 2790 2791 ix = core.mem_read(address); 2792 ix |= (core.mem_read((address + 1) & 0xffff) << 8); 2793 }; 2794 // 0x2b : DEC IX 2795 dd_instructions[0x2b] = function() 2796 { 2797 ix = (ix - 1) & 0xffff; 2798 }; 2799 // 0x2c : INC IXL (Undocumented) 2800 dd_instructions[0x2c] = function() 2801 { 2802 ix = do_inc(ix & 0xff) | (ix & 0xff00); 2803 }; 2804 // 0x2d : DEC IXL (Undocumented) 2805 dd_instructions[0x2d] = function() 2806 { 2807 ix = do_dec(ix & 0xff) | (ix & 0xff00); 2808 }; 2809 // 0x2e : LD IXL, n (Undocumented) 2810 dd_instructions[0x2e] = function() 2811 { 2812 pc = (pc + 1) & 0xffff; 2813 ix = (core.mem_read(pc) & 0xff) | (ix & 0xff00); 2814 }; 2815 // 0x34 : INC (IX+n) 2816 dd_instructions[0x34] = function() 2817 { 2818 pc = (pc + 1) & 0xffff; 2819 var offset = get_signed_offset_byte(core.mem_read(pc)), 2820 value = core.mem_read((offset + ix) & 0xffff); 2821 core.mem_write((offset + ix) & 0xffff, do_inc(value)); 2822 }; 2823 // 0x35 : DEC (IX+n) 2824 dd_instructions[0x35] = function() 2825 { 2826 pc = (pc + 1) & 0xffff; 2827 var offset = get_signed_offset_byte(core.mem_read(pc)), 2828 value = core.mem_read((offset + ix) & 0xffff); 2829 core.mem_write((offset + ix) & 0xffff, do_dec(value)); 2830 }; 2831 // 0x36 : LD (IX+n), n 2832 dd_instructions[0x36] = function() 2833 { 2834 pc = (pc + 1) & 0xffff; 2835 var offset = get_signed_offset_byte(core.mem_read(pc)); 2836 pc = (pc + 1) & 0xffff; 2837 core.mem_write((ix + offset) & 0xffff, core.mem_read(pc)); 2838 }; 2839 // 0x39 : ADD IX, SP 2840 dd_instructions[0x39] = function() 2841 { 2842 do_ix_add(sp); 2843 }; 2844 // 0x44 : LD B, IXH (Undocumented) 2845 dd_instructions[0x44] = function() 2846 { 2847 b = (ix >>> 8) & 0xff; 2848 }; 2849 // 0x45 : LD B, IXL (Undocumented) 2850 dd_instructions[0x45] = function() 2851 { 2852 b = ix & 0xff; 2853 }; 2854 // 0x46 : LD B, (IX+n) 2855 dd_instructions[0x46] = function() 2856 { 2857 pc = (pc + 1) & 0xffff; 2858 var offset = get_signed_offset_byte(core.mem_read(pc)); 2859 b = core.mem_read((ix + offset) & 0xffff); 2860 }; 2861 // 0x4c : LD C, IXH (Undocumented) 2862 dd_instructions[0x4c] = function() 2863 { 2864 c = (ix >>> 8) & 0xff; 2865 }; 2866 // 0x4d : LD C, IXL (Undocumented) 2867 dd_instructions[0x4d] = function() 2868 { 2869 c = ix & 0xff; 2870 }; 2871 // 0x4e : LD C, (IX+n) 2872 dd_instructions[0x4e] = function() 2873 { 2874 pc = (pc + 1) & 0xffff; 2875 var offset = get_signed_offset_byte(core.mem_read(pc)); 2876 c = core.mem_read((ix + offset) & 0xffff); 2877 }; 2878 // 0x54 : LD D, IXH (Undocumented) 2879 dd_instructions[0x54] = function() 2880 { 2881 d = (ix >>> 8) & 0xff; 2882 }; 2883 // 0x55 : LD D, IXL (Undocumented) 2884 dd_instructions[0x55] = function() 2885 { 2886 d = ix & 0xff; 2887 }; 2888 // 0x56 : LD D, (IX+n) 2889 dd_instructions[0x56] = function() 2890 { 2891 pc = (pc + 1) & 0xffff; 2892 var offset = get_signed_offset_byte(core.mem_read(pc)); 2893 d = core.mem_read((ix + offset) & 0xffff); 2894 }; 2895 // 0x5c : LD E, IXH (Undocumented) 2896 dd_instructions[0x5c] = function() 2897 { 2898 e = (ix >>> 8) & 0xff; 2899 }; 2900 // 0x5d : LD E, IXL (Undocumented) 2901 dd_instructions[0x5d] = function() 2902 { 2903 e = ix & 0xff; 2904 }; 2905 // 0x5e : LD E, (IX+n) 2906 dd_instructions[0x5e] = function() 2907 { 2908 pc = (pc + 1) & 0xffff; 2909 var offset = get_signed_offset_byte(core.mem_read(pc)); 2910 e = core.mem_read((ix + offset) & 0xffff); 2911 }; 2912 // 0x60 : LD IXH, B (Undocumented) 2913 dd_instructions[0x60] = function() 2914 { 2915 ix = (ix & 0xff) | (b << 8); 2916 }; 2917 // 0x61 : LD IXH, C (Undocumented) 2918 dd_instructions[0x61] = function() 2919 { 2920 ix = (ix & 0xff) | (c << 8); 2921 }; 2922 // 0x62 : LD IXH, D (Undocumented) 2923 dd_instructions[0x62] = function() 2924 { 2925 ix = (ix & 0xff) | (d << 8); 2926 }; 2927 // 0x63 : LD IXH, E (Undocumented) 2928 dd_instructions[0x63] = function() 2929 { 2930 ix = (ix & 0xff) | (e << 8); 2931 }; 2932 // 0x64 : LD IXH, IXH (Undocumented) 2933 dd_instructions[0x64] = function() 2934 { 2935 // No-op. 2936 }; 2937 // 0x65 : LD IXH, IXL (Undocumented) 2938 dd_instructions[0x65] = function() 2939 { 2940 ix = (ix & 0xff) | ((ix & 0xff) << 8); 2941 }; 2942 // 0x66 : LD H, (IX+n) 2943 dd_instructions[0x66] = function() 2944 { 2945 pc = (pc + 1) & 0xffff; 2946 var offset = get_signed_offset_byte(core.mem_read(pc)); 2947 h = core.mem_read((ix + offset) & 0xffff); 2948 }; 2949 // 0x67 : LD IXH, A (Undocumented) 2950 dd_instructions[0x67] = function() 2951 { 2952 ix = (ix & 0xff) | (a << 8); 2953 }; 2954 // 0x68 : LD IXL, B (Undocumented) 2955 dd_instructions[0x68] = function() 2956 { 2957 ix = (ix & 0xff00) | b; 2958 }; 2959 // 0x69 : LD IXL, C (Undocumented) 2960 dd_instructions[0x69] = function() 2961 { 2962 ix = (ix & 0xff00) | c; 2963 }; 2964 // 0x6a : LD IXL, D (Undocumented) 2965 dd_instructions[0x6a] = function() 2966 { 2967 ix = (ix & 0xff00) | d; 2968 }; 2969 // 0x6b : LD IXL, E (Undocumented) 2970 dd_instructions[0x6b] = function() 2971 { 2972 ix = (ix & 0xff00) | e; 2973 }; 2974 // 0x6c : LD IXL, IXH (Undocumented) 2975 dd_instructions[0x6c] = function() 2976 { 2977 ix = (ix & 0xff00) | (ix >>> 8); 2978 }; 2979 // 0x6d : LD IXL, IXL (Undocumented) 2980 dd_instructions[0x6d] = function() 2981 { 2982 // No-op. 2983 }; 2984 // 0x6e : LD L, (IX+n) 2985 dd_instructions[0x6e] = function() 2986 { 2987 pc = (pc + 1) & 0xffff; 2988 var offset = get_signed_offset_byte(core.mem_read(pc)); 2989 l = core.mem_read((ix + offset) & 0xffff); 2990 }; 2991 // 0x6f : LD IXL, A (Undocumented) 2992 dd_instructions[0x6f] = function() 2993 { 2994 ix = (ix & 0xff00) | a; 2995 }; 2996 // 0x70 : LD (IX+n), B 2997 dd_instructions[0x70] = function() 2998 { 2999 pc = (pc + 1) & 0xffff; 3000 var offset = get_signed_offset_byte(core.mem_read(pc)); 3001 core.mem_write((ix + offset) & 0xffff, b); 3002 }; 3003 // 0x71 : LD (IX+n), C 3004 dd_instructions[0x71] = function() 3005 { 3006 pc = (pc + 1) & 0xffff; 3007 var offset = get_signed_offset_byte(core.mem_read(pc)); 3008 core.mem_write((ix + offset) & 0xffff, c); 3009 }; 3010 // 0x72 : LD (IX+n), D 3011 dd_instructions[0x72] = function() 3012 { 3013 pc = (pc + 1) & 0xffff; 3014 var offset = get_signed_offset_byte(core.mem_read(pc)); 3015 core.mem_write((ix + offset) & 0xffff, d); 3016 }; 3017 // 0x73 : LD (IX+n), E 3018 dd_instructions[0x73] = function() 3019 { 3020 pc = (pc + 1) & 0xffff; 3021 var offset = get_signed_offset_byte(core.mem_read(pc)); 3022 core.mem_write((ix + offset) & 0xffff, e); 3023 }; 3024 // 0x74 : LD (IX+n), H 3025 dd_instructions[0x74] = function() 3026 { 3027 pc = (pc + 1) & 0xffff; 3028 var offset = get_signed_offset_byte(core.mem_read(pc)); 3029 core.mem_write((ix + offset) & 0xffff, h); 3030 }; 3031 // 0x75 : LD (IX+n), L 3032 dd_instructions[0x75] = function() 3033 { 3034 pc = (pc + 1) & 0xffff; 3035 var offset = get_signed_offset_byte(core.mem_read(pc)); 3036 core.mem_write((ix + offset) & 0xffff, l); 3037 }; 3038 // 0x77 : LD (IX+n), A 3039 dd_instructions[0x77] = function() 3040 { 3041 pc = (pc + 1) & 0xffff; 3042 var offset = get_signed_offset_byte(core.mem_read(pc)); 3043 core.mem_write((ix + offset) & 0xffff, a); 3044 }; 3045 // 0x7c : LD A, IXH (Undocumented) 3046 dd_instructions[0x7c] = function() 3047 { 3048 a = (ix >>> 8) & 0xff; 3049 }; 3050 // 0x7d : LD A, IXL (Undocumented) 3051 dd_instructions[0x7d] = function() 3052 { 3053 a = ix & 0xff; 3054 }; 3055 // 0x7e : LD A, (IX+n) 3056 dd_instructions[0x7e] = function() 3057 { 3058 pc = (pc + 1) & 0xffff; 3059 var offset = get_signed_offset_byte(core.mem_read(pc)); 3060 a = core.mem_read((ix + offset) & 0xffff); 3061 }; 3062 // 0x84 : ADD A, IXH (Undocumented) 3063 dd_instructions[0x84] = function() 3064 { 3065 do_add((ix >>> 8) & 0xff); 3066 }; 3067 // 0x85 : ADD A, IXL (Undocumented) 3068 dd_instructions[0x85] = function() 3069 { 3070 do_add(ix & 0xff); 3071 }; 3072 // 0x86 : ADD A, (IX+n) 3073 dd_instructions[0x86] = function() 3074 { 3075 pc = (pc + 1) & 0xffff; 3076 var offset = get_signed_offset_byte(core.mem_read(pc)); 3077 do_add(core.mem_read((ix + offset) & 0xffff)); 3078 }; 3079 // 0x8c : ADC A, IXH (Undocumented) 3080 dd_instructions[0x8c] = function() 3081 { 3082 do_adc((ix >>> 8) & 0xff); 3083 }; 3084 // 0x8d : ADC A, IXL (Undocumented) 3085 dd_instructions[0x8d] = function() 3086 { 3087 do_adc(ix & 0xff); 3088 }; 3089 // 0x8e : ADC A, (IX+n) 3090 dd_instructions[0x8e] = function() 3091 { 3092 pc = (pc + 1) & 0xffff; 3093 var offset = get_signed_offset_byte(core.mem_read(pc)); 3094 do_adc(core.mem_read((ix + offset) & 0xffff)); 3095 }; 3096 // 0x94 : SUB IXH (Undocumented) 3097 dd_instructions[0x94] = function() 3098 { 3099 do_sub((ix >>> 8) & 0xff); 3100 }; 3101 // 0x95 : SUB IXL (Undocumented) 3102 dd_instructions[0x95] = function() 3103 { 3104 do_sub(ix & 0xff); 3105 }; 3106 // 0x96 : SUB A, (IX+n) 3107 dd_instructions[0x96] = function() 3108 { 3109 pc = (pc + 1) & 0xffff; 3110 var offset = get_signed_offset_byte(core.mem_read(pc)); 3111 do_sub(core.mem_read((ix + offset) & 0xffff)); 3112 }; 3113 // 0x9c : SBC IXH (Undocumented) 3114 dd_instructions[0x9c] = function() 3115 { 3116 do_sbc((ix >>> 8) & 0xff); 3117 }; 3118 // 0x9d : SBC IXL (Undocumented) 3119 dd_instructions[0x9d] = function() 3120 { 3121 do_sbc(ix & 0xff); 3122 }; 3123 // 0x9e : SBC A, (IX+n) 3124 dd_instructions[0x9e] = function() 3125 { 3126 pc = (pc + 1) & 0xffff; 3127 var offset = get_signed_offset_byte(core.mem_read(pc)); 3128 do_sbc(core.mem_read((ix + offset) & 0xffff)); 3129 }; 3130 // 0xa4 : AND IXH (Undocumented) 3131 dd_instructions[0xa4] = function() 3132 { 3133 do_and((ix >>> 8) & 0xff); 3134 }; 3135 // 0xa5 : AND IXL (Undocumented) 3136 dd_instructions[0xa5] = function() 3137 { 3138 do_and(ix & 0xff); 3139 }; 3140 // 0xa6 : AND A, (IX+n) 3141 dd_instructions[0xa6] = function() 3142 { 3143 pc = (pc + 1) & 0xffff; 3144 var offset = get_signed_offset_byte(core.mem_read(pc)); 3145 do_and(core.mem_read((ix + offset) & 0xffff)); 3146 }; 3147 // 0xac : XOR IXH (Undocumented) 3148 dd_instructions[0xac] = function() 3149 { 3150 do_xor((ix >>> 8) & 0xff); 3151 }; 3152 // 0xad : XOR IXL (Undocumented) 3153 dd_instructions[0xad] = function() 3154 { 3155 do_xor(ix & 0xff); 3156 }; 3157 // 0xae : XOR A, (IX+n) 3158 dd_instructions[0xae] = function() 3159 { 3160 pc = (pc + 1) & 0xffff; 3161 var offset = get_signed_offset_byte(core.mem_read(pc)); 3162 do_xor(core.mem_read((ix + offset) & 0xffff)); 3163 }; 3164 // 0xb4 : OR IXH (Undocumented) 3165 dd_instructions[0xb4] = function() 3166 { 3167 do_or((ix >>> 8) & 0xff); 3168 }; 3169 // 0xb5 : OR IXL (Undocumented) 3170 dd_instructions[0xb5] = function() 3171 { 3172 do_or(ix & 0xff); 3173 }; 3174 // 0xb6 : OR A, (IX+n) 3175 dd_instructions[0xb6] = function() 3176 { 3177 pc = (pc + 1) & 0xffff; 3178 var offset = get_signed_offset_byte(core.mem_read(pc)); 3179 do_or(core.mem_read((ix + offset) & 0xffff)); 3180 }; 3181 // 0xbc : CP IXH (Undocumented) 3182 dd_instructions[0xbc] = function() 3183 { 3184 do_cp((ix >>> 8) & 0xff); 3185 }; 3186 // 0xbd : CP IXL (Undocumented) 3187 dd_instructions[0xbd] = function() 3188 { 3189 do_cp(ix & 0xff); 3190 }; 3191 // 0xbe : CP A, (IX+n) 3192 dd_instructions[0xbe] = function() 3193 { 3194 pc = (pc + 1) & 0xffff; 3195 var offset = get_signed_offset_byte(core.mem_read(pc)); 3196 do_cp(core.mem_read((ix + offset) & 0xffff)); 3197 }; 3198 // 0xcb : CB Prefix (IX bit instructions) 3199 dd_instructions[0xcb] = function() 3200 { 3201 pc = (pc + 1) & 0xffff; 3202 var offset = get_signed_offset_byte(core.mem_read(pc)); 3203 pc = (pc + 1) & 0xffff; 3204 var opcode = core.mem_read(pc), value; 3205 3206 // As with the "normal" CB prefix, we implement the DDCB prefix 3207 // by decoding the opcode directly, rather than using a table. 3208 if (opcode < 0x40) 3209 { 3210 // Shift and rotate instructions. 3211 var ddcb_functions = [do_rlc, do_rrc, do_rl, do_rr, 3212 do_sla, do_sra, do_sll, do_srl]; 3213 3214 // Most of the opcodes in this range are not valid, 3215 // so we map this opcode onto one of the ones that is. 3216 var func = ddcb_functions[(opcode & 0x38) >>> 3], 3217 value = func( core.mem_read((ix + offset) & 0xffff)); 3218 3219 core.mem_write((ix + offset) & 0xffff, value); 3220 } 3221 else 3222 { 3223 var bit_number = (opcode & 0x38) >>> 3; 3224 3225 if (opcode < 0x80) 3226 { 3227 // BIT 3228 flags.N = 0; 3229 flags.H = 1; 3230 flags.Z = !(core.mem_read((ix + offset) & 0xffff) & (1 << bit_number)) ? 1 : 0; 3231 flags.P = flags.Z; 3232 flags.S = ((bit_number === 7) && !flags.Z) ? 1 : 0; 3233 } 3234 else if (opcode < 0xc0) 3235 { 3236 // RES 3237 value = core.mem_read((ix + offset) & 0xffff) & ~(1 << bit_number) & 0xff; 3238 core.mem_write((ix + offset) & 0xffff, value); 3239 } 3240 else 3241 { 3242 // SET 3243 value = core.mem_read((ix + offset) & 0xffff) | (1 << bit_number); 3244 core.mem_write((ix + offset) & 0xffff, value); 3245 } 3246 } 3247 3248 // This implements the undocumented shift, RES, and SET opcodes, 3249 // which write their result to memory and also to an 8080 register. 3250 if (value !== undefined) 3251 { 3252 if ((opcode & 0x07) === 0) 3253 b = value; 3254 else if ((opcode & 0x07) === 1) 3255 c = value; 3256 else if ((opcode & 0x07) === 2) 3257 d = value; 3258 else if ((opcode & 0x07) === 3) 3259 e = value; 3260 else if ((opcode & 0x07) === 4) 3261 h = value; 3262 else if ((opcode & 0x07) === 5) 3263 l = value; 3264 // 6 is the documented opcode, which doesn't set a register. 3265 else if ((opcode & 0x07) === 7) 3266 a = value; 3267 } 3268 3269 cycle_counter += cycle_counts_cb[opcode] + 8; 3270 }; 3271 // 0xe1 : POP IX 3272 dd_instructions[0xe1] = function() 3273 { 3274 ix = pop_word(); 3275 }; 3276 // 0xe3 : EX (SP), IX 3277 dd_instructions[0xe3] = function() 3278 { 3279 var temp = ix; 3280 ix = core.mem_read(sp); 3281 ix |= core.mem_read((sp + 1) & 0xffff) << 8; 3282 core.mem_write(sp, temp & 0xff); 3283 core.mem_write((sp + 1) & 0xffff, (temp >>> 8) & 0xff); 3284 }; 3285 // 0xe5 : PUSH IX 3286 dd_instructions[0xe5] = function() 3287 { 3288 push_word(ix); 3289 }; 3290 // 0xe9 : JP (IX) 3291 dd_instructions[0xe9] = function() 3292 { 3293 pc = (ix - 1) & 0xffff; 3294 }; 3295 // 0xf9 : LD SP, IX 3296 dd_instructions[0xf9] = function() 3297 { 3298 sp = ix; 3299 }; 3300 3301 3302 /////////////////////////////////////////////////////////////////////////////// 3303 /// These tables contain the number of T cycles used for each instruction. 3304 /// In a few special cases, such as conditional control flow instructions, 3305 /// additional cycles might be added to these values. 3306 /// The total number of cycles is the return value of run_instruction(). 3307 /////////////////////////////////////////////////////////////////////////////// 3308 let cycle_counts = [ 3309 4, 10, 7, 6, 4, 4, 7, 4, 4, 11, 7, 6, 4, 4, 7, 4, 3310 8, 10, 7, 6, 4, 4, 7, 4, 12, 11, 7, 6, 4, 4, 7, 4, 3311 7, 10, 16, 6, 4, 4, 7, 4, 7, 11, 16, 6, 4, 4, 7, 4, 3312 7, 10, 13, 6, 11, 11, 10, 4, 7, 11, 13, 6, 4, 4, 7, 4, 3313 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3314 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3315 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3316 7, 7, 7, 7, 7, 7, 4, 7, 4, 4, 4, 4, 4, 4, 7, 4, 3317 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3318 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3319 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3320 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4, 3321 5, 10, 10, 10, 10, 11, 7, 11, 5, 10, 10, 0, 10, 17, 7, 11, 3322 5, 10, 10, 11, 10, 11, 7, 11, 5, 4, 10, 11, 10, 0, 7, 11, 3323 5, 10, 10, 19, 10, 11, 7, 11, 5, 4, 10, 4, 10, 0, 7, 11, 3324 5, 10, 10, 4, 10, 11, 7, 11, 5, 6, 10, 4, 10, 0, 7, 11 3325 ]; 3326 3327 let cycle_counts_ed = [ 3328 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3330 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3331 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3332 12, 12, 15, 20, 8, 14, 8, 9, 12, 12, 15, 20, 8, 14, 8, 9, 3333 12, 12, 15, 20, 8, 14, 8, 9, 12, 12, 15, 20, 8, 14, 8, 9, 3334 12, 12, 15, 20, 8, 14, 8, 18, 12, 12, 15, 20, 8, 14, 8, 18, 3335 12, 12, 15, 20, 8, 14, 8, 0, 12, 12, 15, 20, 8, 14, 8, 0, 3336 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3337 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3338 16, 16, 16, 16, 0, 0, 0, 0, 16, 16, 16, 16, 0, 0, 0, 0, 3339 16, 16, 16, 16, 0, 0, 0, 0, 16, 16, 16, 16, 0, 0, 0, 0, 3340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3341 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3343 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 3344 ]; 3345 3346 let cycle_counts_cb = [ 3347 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3348 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3349 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3350 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3351 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 3352 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 3353 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 3354 8, 8, 8, 8, 8, 8, 12, 8, 8, 8, 8, 8, 8, 8, 12, 8, 3355 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3356 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3357 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3358 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3359 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3360 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3361 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8, 3362 8, 8, 8, 8, 8, 8, 15, 8, 8, 8, 8, 8, 8, 8, 15, 8 3363 ]; 3364 3365 let cycle_counts_dd = [ 3366 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 3367 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 3368 0, 14, 20, 10, 8, 8, 11, 0, 0, 15, 20, 10, 8, 8, 11, 0, 3369 0, 0, 0, 0, 23, 23, 19, 0, 0, 15, 0, 0, 0, 0, 0, 0, 3370 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 3371 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 3372 8, 8, 8, 8, 8, 8, 19, 8, 8, 8, 8, 8, 8, 8, 19, 8, 3373 19, 19, 19, 19, 19, 19, 0, 19, 0, 0, 0, 0, 8, 8, 19, 0, 3374 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 3375 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 3376 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 3377 0, 0, 0, 0, 8, 8, 19, 0, 0, 0, 0, 0, 8, 8, 19, 0, 3378 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3380 0, 14, 0, 23, 0, 15, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 3381 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0 3382 ]; 3383 3384 // There's tons of stuff in this object, 3385 // but only these three functions are the public API. 3386 return { 3387 getState, 3388 setState, 3389 reset, 3390 run_instruction, 3391 interrupt, 3392 // Ajouts pour l'émulateur ZX81 : registre I (jeu de caractères), 3393 // PC et IFF1 lisibles/modifiables sans passer par getState(). 3394 get_i() { return i; }, 3395 get_pc() { return pc; }, 3396 set_pc(v) { pc = v & 0xffff; }, 3397 get_iff1() { return iff1; }, 3398 is_halted() { return halted; } 3399 }; 3400 } 3401 3402 export { Z80 };