Skip to content

Commit

Permalink
Move VRAM and OAM into dedicated object (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
kremi151 committed Aug 15, 2020
1 parent 575fc96 commit f7ab4f3
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 27 deletions.
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(SOURCES
source/memory/memory.cpp
source/memory/mbc1.cpp
source/memory/mbc_none.cpp
source/memory/ppu_memory.cpp
source/instructions/instruction_context.cpp
source/instructions/alu.cpp
source/instructions/loads.cpp
Expand Down Expand Up @@ -65,6 +66,7 @@ set(HEADERS
source/memory/mbc.h
source/memory/mbc1.h
source/memory/mbc_none.h
source/memory/ppu_memory.h
source/instructions/instruction_context.h
source/instructions/alu.h
source/instructions/loads.h
Expand Down
5 changes: 3 additions & 2 deletions core/source/emulator/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ Emulator::Emulator(GameBoyType gbType, const Controller::ControllersPtr& control
: cartridge(new Cartridge)
, controllers(controllers)
, ioRegisters(controllers)
, memory(new Memory(cartridge, controllers, ioRegisters))
, ppuMemory()
, memory(new Memory(cartridge, controllers, ioRegisters, ppuMemory))
, cpu(std::make_shared<CPU>(gbType, memory, ioRegisters))
, ppu(memory, cpu, controllers, ioRegisters)
, ppu(memory, cpu, controllers, ioRegisters, ppuMemory)
{
}

Expand Down
2 changes: 2 additions & 0 deletions core/source/emulator/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cartridge/cartridge.h>
#include <controllers/controllers.h>
#include <memory/memory.h>
#include <memory/ppu_memory.h>
#include <memory>

namespace FunkyBoy {
Expand All @@ -40,6 +41,7 @@ namespace FunkyBoy {
Controller::ControllersPtr controllers;

io_registers ioRegisters;
PPUMemory ppuMemory;
MemoryPtr memory;
CPUPtr cpu;
PPU ppu;
Expand Down
3 changes: 2 additions & 1 deletion core/source/emulator/ppu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@

using namespace FunkyBoy;

PPU::PPU(FunkyBoy::MemoryPtr memory, CPUPtr cpu, Controller::ControllersPtr controllers, const io_registers& ioRegisters)
PPU::PPU(FunkyBoy::MemoryPtr memory, CPUPtr cpu, Controller::ControllersPtr controllers, const io_registers& ioRegisters, const PPUMemory &ppuMemory)
: memory(std::move(memory))
, cpu(std::move(cpu))
, controllers(std::move(controllers))
, ioRegisters(ioRegisters)
, ppuMemory(ppuMemory)
, gpuMode(GPUMode::GPUMode_2)
, modeClocks(0)
, scanLineBuffer(new u8[FB_GB_DISPLAY_WIDTH])
Expand Down
4 changes: 3 additions & 1 deletion core/source/emulator/ppu.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <controllers/controllers.h>
#include <emulator/cpu.h>
#include <emulator/io_registers.h>
#include <memory/ppu_memory.h>
#include <util/gpumode.h>
#include <util/typedefs.h>

Expand All @@ -32,6 +33,7 @@ namespace FunkyBoy {
CPUPtr cpu;
Controller::ControllersPtr controllers;
io_registers ioRegisters;
PPUMemory ppuMemory;

GPUMode gpuMode;

Expand All @@ -41,7 +43,7 @@ namespace FunkyBoy {

void renderScanline(u8 ly);
public:
PPU(MemoryPtr memory, CPUPtr cpu, Controller::ControllersPtr controllers, const io_registers& ioRegisters);
PPU(MemoryPtr memory, CPUPtr cpu, Controller::ControllersPtr controllers, const io_registers& ioRegisters, const PPUMemory &ppuMemory);
~PPU();

ret_code doClocks(u8 clocks);
Expand Down
23 changes: 6 additions & 17 deletions core/source/memory/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,22 @@ using namespace FunkyBoy;

#define FB_INTERNAL_RAM_BANK_SIZE (4 * 1024)

Memory::Memory(std::shared_ptr<Cartridge> cartridge, Controller::ControllersPtr controllers, const io_registers& ioRegisters)
Memory::Memory(std::shared_ptr<Cartridge> cartridge, Controller::ControllersPtr controllers, const io_registers& ioRegisters, const PPUMemory &ppuMemory)
: cartridge(std::move(cartridge))
, controllers(std::move(controllers))
, ioRegisters(ioRegisters)
, ppuMemory(ppuMemory)
, interruptEnableRegister(0)
, dmaStarted(false)
{
vram = new u8[6144]{};
bgMapData1 = new u8[1024]{};
bgMapData2 = new u8[1024]{};
internalRam = new u8[8 * FB_INTERNAL_RAM_BANK_SIZE]{};
oam = new u8[160]{};
hram = new u8[127]{};

dynamicRamBank = internalRam + FB_INTERNAL_RAM_BANK_SIZE;
}

Memory::~Memory() {
delete[] vram;
delete[] bgMapData1;
delete[] bgMapData2;
delete[] internalRam;
delete[] oam;
delete[] hram;
}

Expand All @@ -68,12 +61,8 @@ u8* Memory::getMemoryAddress(FunkyBoy::memory_address offset) {
FB_MEMORY_NIBBLE_RANGE(7):
return cartridge->mbc->getROMMemoryAddress(offset, cartridge->rom);
FB_MEMORY_NIBBLE_RANGE(8):
case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
return vram + (offset - 0x8000);
case 0x98: case 0x99: case 0x9A: case 0x9B:
return bgMapData1 + (offset - 0x9800);
case 0x9C: case 0x9D: case 0x9E: case 0x9F:
return bgMapData2 + (offset - 0x9C00);
FB_MEMORY_NIBBLE_RANGE(9):
return &ppuMemory.getVRAMByte(offset - 0x8000);
FB_MEMORY_NIBBLE_RANGE(A):
FB_MEMORY_NIBBLE_RANGE(B):
return cartridge->mbc->getRAMMemoryAddress(offset - 0xA000, cartridge->ram);
Expand All @@ -89,7 +78,7 @@ u8* Memory::getMemoryAddress(FunkyBoy::memory_address offset) {
return dynamicRamBank + (offset - 0xF000);
case 0xFE: {
if (offset < 0xFEA0) {
return oam + (offset - 0xFE00);
return &ppuMemory.getOAMByte(offset - 0xFE00);
} else {
// Not usable
return nullptr;
Expand Down Expand Up @@ -233,7 +222,7 @@ void Memory::doDMA() {
if (!dmaStarted) {
return;
}
*(oam + dmaLsb) = read8BitsAt(Util::compose16Bits(dmaLsb, dmaMsb));
ppuMemory.getOAMByte(dmaLsb) = read8BitsAt(Util::compose16Bits(dmaLsb, dmaMsb));
if (++dmaLsb > 0x9F) {
dmaStarted = false;
}
Expand Down
8 changes: 3 additions & 5 deletions core/source/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cartridge/cartridge.h>
#include <controllers/controllers.h>
#include <emulator/io_registers.h>
#include <memory/ppu_memory.h>

namespace FunkyBoy {

Expand All @@ -29,12 +30,9 @@ namespace FunkyBoy {
CartridgePtr cartridge;
Controller::ControllersPtr controllers;
io_registers ioRegisters;
PPUMemory ppuMemory;

u8 *vram; // Character RAM
u8 *bgMapData1;
u8 *bgMapData2;
u8 *internalRam;
u8 *oam;
u8 *hram;
u8 interruptEnableRegister;

Expand All @@ -49,7 +47,7 @@ namespace FunkyBoy {
bool interceptWrite(memory_address offset, u8 &val);
bool interceptReadAt(memory_address offset, u8 *out);
public:
Memory(CartridgePtr cartridge, Controller::ControllersPtr controllers, const io_registers& ioRegisters);
Memory(CartridgePtr cartridge, Controller::ControllersPtr controllers, const io_registers& ioRegisters, const PPUMemory &ppuMemory);
~Memory();

Memory(const Memory &other) = delete;
Expand Down
50 changes: 50 additions & 0 deletions core/source/memory/ppu_memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2020 Michel Kremer (kremi151)
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ppu_memory.h"

using namespace FunkyBoy;

PPUMemory::PPUMemory()
: vram(new u8[8192]{})
, oam(new u8[160]{})
, ptrCounter(new u16(1))
{
}

PPUMemory::PPUMemory(const PPUMemory &other)
: vram(other.vram)
, oam(other.oam)
, ptrCounter(other.ptrCounter)
{
(*ptrCounter)++;
}

PPUMemory::~PPUMemory() {
if (--(*ptrCounter) < 1) {
delete[] vram;
delete[] oam;
delete ptrCounter;
}
}

u8 &PPUMemory::getVRAMByte(memory_address vramOffset) {
return *(vram + vramOffset);
}

u8 &PPUMemory::getOAMByte(memory_address oamOffset) {
return *(oam + oamOffset);
}
42 changes: 42 additions & 0 deletions core/source/memory/ppu_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2020 Michel Kremer (kremi151)
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FB_CORE_MEMORY_PPU_MEMORY_H
#define FB_CORE_MEMORY_PPU_MEMORY_H

#include <util/typedefs.h>

namespace FunkyBoy {

class PPUMemory {
private:
u8 *vram;
u8 *oam;
u16 *ptrCounter;
public:
PPUMemory();
PPUMemory(const PPUMemory &other);
~PPUMemory();

PPUMemory &operator=(const PPUMemory &other) = delete;

u8 &getVRAMByte(memory_address vramOffset);
u8 &getOAMByte(memory_address oamOffset);
};

}

#endif //FB_CORE_MEMORY_PPU_MEMORY_H
3 changes: 2 additions & 1 deletion test/source/unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ FunkyBoy::MemoryPtr createMemory() {
auto controllers = std::make_shared<FunkyBoy::Controller::Controllers>();
FunkyBoy::CartridgePtr cartridge(new FunkyBoy::Cartridge);
FunkyBoy::io_registers io(controllers);
return std::make_shared<FunkyBoy::Memory>(cartridge, controllers, io);
FunkyBoy::PPUMemory ppuMemory;
return std::make_shared<FunkyBoy::Memory>(cartridge, controllers, io, ppuMemory);
}

TEST(test16BitReadWrite) {
Expand Down

0 comments on commit f7ab4f3

Please sign in to comment.