From 2a234fabfd6e8e65c0be7d04b256351982c140eb Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 22 Aug 2022 13:12:27 +0200 Subject: [PATCH] Emulator: Add opcode LDH or LD 0xff00 --- src/cpu.cpp | 31 +++++++++++++++++++++++++++++++ src/cpu.h | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/src/cpu.cpp b/src/cpu.cpp index db42fa2..230e9cd 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -111,6 +111,27 @@ void CPU::ld8() } } +void CPU::ldh8() +{ + uint8_t opcode = consumeMemory(); + switch (opcode) { + case 0xe0: + // LD ($ff00 + n),A == LDH (n),A + // Put value in A into address (0xff00 + next byte in memory): + m_wait_cycles += 12; + writeFf(m_a); + break; + case 0xf0: + // LD A,($ff00 + n) == LDH A,(n) + // Put value at address (0xff00 + next byte in memory) into A: + m_wait_cycles += 12; + m_a = readFf(); + break; + default: + VERIFY_NOT_REACHED(); + } +} + void CPU::ld16() { uint8_t opcode = consumeMemory(); @@ -179,6 +200,16 @@ uint8_t CPU::consumeMemory() return Emu::the().readMemory(m_pc++); } +void CPU::writeFf(uint32_t value) +{ + Emu::the().writeMemory(immediate8() | (0xff << 8), value); +} + +uint32_t CPU::readFf() +{ + return Emu::the().readMemory(immediate8() | (0xff << 8)); +} + void CPU::setBc(uint32_t value) { m_b = value >> 8; diff --git a/src/cpu.h b/src/cpu.h index 16b491c..835ffc2 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -39,6 +39,7 @@ public: // 8-bit void ld8(); + void ldh8(); // 16-bit void ld16(); @@ -61,6 +62,9 @@ private: uint32_t immediate8() { return consumeMemory(); } uint32_t immediate16() { return consumeMemory() | (consumeMemory() << 8); } + void writeFf(uint32_t value); + uint32_t readFf(); + void setBc(uint32_t value); void setDe(uint32_t value); void setHl(uint32_t value);