Browse Source

Emulator: Make read/write function more generic

master
Riyyi 2 years ago
parent
commit
c83d322d0c
  1. 49
      src/cpu.cpp
  2. 11
      src/cpu.h

49
src/cpu.cpp

@ -82,8 +82,8 @@ void CPU::update()
void CPU::add() void CPU::add()
{ {
uint8_t opcode = consumeMemory(); uint8_t opcode = read(m_pc++);
uint8_t immediate = consumeMemory(); uint8_t immediate = read(m_pc++);
switch (opcode) { switch (opcode) {
case 0xc6: case 0xc6:
// ADD A,d8 // ADD A,d8
@ -108,7 +108,7 @@ void CPU::add()
void CPU::ld8() void CPU::ld8()
{ {
uint8_t opcode = consumeMemory(); uint8_t opcode = read(m_pc++);
switch (opcode) { switch (opcode) {
case 0x3e: case 0x3e:
m_wait_cycles += 8; m_wait_cycles += 8;
@ -121,7 +121,7 @@ void CPU::ld8()
void CPU::ldh8() void CPU::ldh8()
{ {
uint8_t opcode = consumeMemory(); uint8_t opcode = read(m_pc++);
switch (opcode) { switch (opcode) {
case 0xe0: case 0xe0:
// LD ($ff00 + n),A == LDH (n),A // LD ($ff00 + n),A == LDH (n),A
@ -133,7 +133,7 @@ void CPU::ldh8()
// LD A,($ff00 + n) == LDH A,(n) // LD A,($ff00 + n) == LDH A,(n)
// Put value at address (0xff00 + next byte in memory) into A: // Put value at address (0xff00 + next byte in memory) into A:
m_wait_cycles += 12; m_wait_cycles += 12;
m_a = readFf(); m_a = readFf(immediate8());
break; break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
@ -142,20 +142,20 @@ void CPU::ldh8()
void CPU::ld16() void CPU::ld16()
{ {
uint8_t opcode = consumeMemory(); uint8_t opcode = read(m_pc++);
switch (opcode) { switch (opcode) {
case 0x01: { case 0x01: {
m_wait_cycles += 12; m_wait_cycles += 12;
setBc(immediate16()); write(bc(), immediate16());
break; break;
} }
case 0x11: case 0x11:
m_wait_cycles += 12; m_wait_cycles += 12;
setDe(immediate16()); write(de(), immediate16());
break; break;
case 0x21: case 0x21:
m_wait_cycles += 12; m_wait_cycles += 12;
setHl(immediate16()); write(hl(), immediate16());
break; break;
case 0x31: { case 0x31: {
m_wait_cycles += 12; m_wait_cycles += 12;
@ -185,7 +185,7 @@ void CPU::ld16()
void CPU::jp16() void CPU::jp16()
{ {
uint8_t opcode = consumeMemory(); uint8_t opcode = read(m_pc++);
switch (opcode) { switch (opcode) {
case 0xc3: case 0xc3:
m_wait_cycles += 16; m_wait_cycles += 16;
@ -203,35 +203,22 @@ uint8_t CPU::peekMemory(int32_t offset) const
return Emu::the().readMemory(m_pc + offset); 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) uint32_t CPU::read(uint32_t address)
{
Emu::the().writeMemory(immediate8() | (0xff << 8), value);
}
uint32_t CPU::readFf()
{ {
return Emu::the().readMemory(immediate8() | (0xff << 8)); return Emu::the().readMemory(address);
} }
void CPU::setBc(uint32_t value) void CPU::writeFf(uint32_t value)
{
m_b = value >> 8;
m_c = value;
}
void CPU::setDe(uint32_t value)
{ {
m_d = value >> 8; Emu::the().writeMemory(read(m_pc++) | (0xff << 8), value);
m_e = value;
} }
void CPU::setHl(uint32_t value) uint32_t CPU::readFf(uint32_t address)
{ {
m_h = value >> 8; return Emu::the().readMemory(address | (0xff << 8));
m_l = value;
} }

11
src/cpu.h

@ -57,17 +57,14 @@ public:
private: private:
uint8_t peekMemory(int32_t offset = 0) const; uint8_t peekMemory(int32_t offset = 0) const;
uint8_t consumeMemory();
uint32_t immediate8() { return consumeMemory(); }
uint32_t immediate16() { return consumeMemory() | (consumeMemory() << 8); } uint32_t immediate16() { return consumeMemory() | (consumeMemory() << 8); }
void writeFf(uint32_t value); void write(uint32_t address, uint32_t value);
uint32_t readFf(); uint32_t read(uint32_t address);
void setBc(uint32_t value); void writeFf(uint32_t value);
void setDe(uint32_t value); uint32_t readFf(uint32_t address);
void setHl(uint32_t value);
uint32_t bc() const { return m_b << 8 | m_c; } uint32_t bc() const { return m_b << 8 | m_c; }
uint32_t de() const { return m_b << 8 | m_c; } uint32_t de() const { return m_b << 8 | m_c; }

Loading…
Cancel
Save