From bf8bd66389251640d9ee441a44d7b0e5a3ed5bd0 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 28 Aug 2022 22:09:15 +0200 Subject: [PATCH] Emulator: Implement 8-bit ADD opcodes --- src/cpu.cpp | 50 +++++++++++++++++++++++++++++++++++++++----------- src/cpu.h | 2 +- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/cpu.cpp b/src/cpu.cpp index 8dc7164..561f726 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -192,6 +192,14 @@ void CPU::update() case 0x7d: ldr8(); break; case 0x7e: ldr8(); break; case 0x7f: ldr8(); break; + case 0x80: add8(); break; + case 0x81: add8(); break; + case 0x82: add8(); break; + case 0x83: add8(); break; + case 0x84: add8(); break; + case 0x85: add8(); break; + case 0x86: add8(); break; + case 0x87: add8(); break; case 0xa0: and8(); break; case 0xa1: and8(); break; case 0xa2: and8(); break; @@ -214,7 +222,7 @@ void CPU::update() case 0xc3: jp16(); break; case 0xc4: call(); break; case 0xc5: push(); break; - case 0xc6: addi8(); break; + case 0xc6: add8(); break; case 0xc7: rst(); break; case 0xcb: prefix(); break; case 0xcc: call(); break; @@ -269,25 +277,45 @@ void CPU::update() // ------------------------------------- -void CPU::addi8() +void CPU::add8() { - uint8_t opcode = pcRead(); - uint8_t immediate = pcRead(); - switch (opcode) { - case 0xc6: // ADD A,i8, flags: Z 0 H C - m_wait_cycles += 8; + auto add = [this](uint8_t register_) -> void { + // ADD A,r8, flags: Z 0 H C + m_wait_cycles += 4; // Set flags m_nf = 0; - m_hf = isCarry(m_a, immediate, 0x10); - m_cf = isCarry(m_a, immediate, 0x100); + m_hf = isCarry(m_a, register_, 0x10); + m_cf = isCarry(m_a, register_, 0x100); - // A = A + r - m_a = (m_a + immediate) & 0xff; + // Add the value in r8 to A + m_a = (m_a + register_) & 0xff; // Zero flag m_zf = m_a == 0; + }; + + uint8_t opcode = pcRead(); + switch (opcode) { + case 0x80: /* ADD A,B */ add(m_b); break; + case 0x81: /* ADD A,C */ add(m_c); break; + case 0x82: /* ADD A,D */ add(m_d); break; + case 0x83: /* ADD A,E */ add(m_e); break; + case 0x84: /* ADD A,H */ add(m_h); break; + case 0x85: /* ADD A,L */ add(m_l); break; + case 0x86: /* ADD A,(HL) */ { + m_wait_cycles += 4; // + 4 = 8 total + + add(read(hl())); break; + } + case 0x87: /* ADD A,A */ add(m_a); break; + case 0xc6: /* ADD A,i8 */ { + m_wait_cycles += 4; // + 4 = 8 total + + add(pcRead()); + break; + } default: VERIFY_NOT_REACHED(); } diff --git a/src/cpu.h b/src/cpu.h index 7d6fb3a..b505dae 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -28,7 +28,7 @@ public: // Arithmetic and Logic Instructions // 8-bit - void addi8(); + void add8(); void and8(); void dec8(); void inc8();