diff --git a/src/cpu.cpp b/src/cpu.cpp index 09ebffc..a0b85ed 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -82,8 +82,8 @@ void CPU::update() void CPU::add() { - uint8_t opcode = consumeMemory(); - uint8_t immediate = consumeMemory(); + uint8_t opcode = read(m_pc++); + uint8_t immediate = read(m_pc++); switch (opcode) { case 0xc6: // ADD A,d8 @@ -108,7 +108,7 @@ void CPU::add() void CPU::ld8() { - uint8_t opcode = consumeMemory(); + uint8_t opcode = read(m_pc++); switch (opcode) { case 0x3e: m_wait_cycles += 8; @@ -121,7 +121,7 @@ void CPU::ld8() void CPU::ldh8() { - uint8_t opcode = consumeMemory(); + uint8_t opcode = read(m_pc++); switch (opcode) { case 0xe0: // LD ($ff00 + n),A == LDH (n),A @@ -133,7 +133,7 @@ void CPU::ldh8() // 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(); + m_a = readFf(immediate8()); break; default: VERIFY_NOT_REACHED(); @@ -142,20 +142,20 @@ void CPU::ldh8() void CPU::ld16() { - uint8_t opcode = consumeMemory(); + uint8_t opcode = read(m_pc++); switch (opcode) { case 0x01: { m_wait_cycles += 12; - setBc(immediate16()); + write(bc(), immediate16()); break; } case 0x11: m_wait_cycles += 12; - setDe(immediate16()); + write(de(), immediate16()); break; case 0x21: m_wait_cycles += 12; - setHl(immediate16()); + write(hl(), immediate16()); break; case 0x31: { m_wait_cycles += 12; @@ -185,7 +185,7 @@ void CPU::ld16() void CPU::jp16() { - uint8_t opcode = consumeMemory(); + uint8_t opcode = read(m_pc++); switch (opcode) { case 0xc3: m_wait_cycles += 16; @@ -203,35 +203,22 @@ uint8_t CPU::peekMemory(int32_t offset) const return Emu::the().readMemory(m_pc + offset); } -uint8_t CPU::consumeMemory() +void CPU::write(uint32_t address, uint32_t value) { - return Emu::the().readMemory(m_pc++); + Emu::the().writeMemory(address, value); } -void CPU::writeFf(uint32_t value) -{ - Emu::the().writeMemory(immediate8() | (0xff << 8), value); -} - -uint32_t CPU::readFf() +uint32_t CPU::read(uint32_t address) { - return Emu::the().readMemory(immediate8() | (0xff << 8)); + return Emu::the().readMemory(address); } -void CPU::setBc(uint32_t value) -{ - m_b = value >> 8; - m_c = value; -} - -void CPU::setDe(uint32_t value) +void CPU::writeFf(uint32_t value) { - m_d = value >> 8; - m_e = value; + Emu::the().writeMemory(read(m_pc++) | (0xff << 8), value); } -void CPU::setHl(uint32_t value) +uint32_t CPU::readFf(uint32_t address) { - m_h = value >> 8; - m_l = value; + return Emu::the().readMemory(address | (0xff << 8)); } diff --git a/src/cpu.h b/src/cpu.h index 6dd3c41..786930b 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -57,17 +57,14 @@ public: private: uint8_t peekMemory(int32_t offset = 0) const; - uint8_t consumeMemory(); - uint32_t immediate8() { return consumeMemory(); } uint32_t immediate16() { return consumeMemory() | (consumeMemory() << 8); } - void writeFf(uint32_t value); - uint32_t readFf(); + void write(uint32_t address, uint32_t value); + uint32_t read(uint32_t address); - void setBc(uint32_t value); - void setDe(uint32_t value); - void setHl(uint32_t value); + void writeFf(uint32_t value); + uint32_t readFf(uint32_t address); uint32_t bc() const { return m_b << 8 | m_c; } uint32_t de() const { return m_b << 8 | m_c; }