Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement APU [WIP] #24

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.vs/
.vscode/
build/
*.DS_Store
cmake-build-debug/
.idea/
src/.vscode/
CMakeSettings.json
run.sh
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON")

find_package(SDL2 REQUIRED)

if (DEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
endif()

add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} SDL2::SDL2 SDL2::SDL2main)
add_subdirectory(src)
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(SOURCES
gameBoy.cpp
mmap.cpp
graphics.cpp
sound.cpp

# -------
# Header Files
Expand All @@ -13,6 +14,7 @@ set(SOURCES
mmap.h
types.h
graphics.h
sound.h
)

target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
Expand Down
20 changes: 10 additions & 10 deletions src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ CPU::CPU()
// TODO: check the initial state of IME
IMEFlag = -1;

IMEReg = false;
}
IMEReg = false;
}

// NOP just adds 4 cycles
// Does nothing
int CPU::NOP()
{
reg_PC.dat += 1;
debugPrint("NOP\n");
return 4;
}
// NOP just adds 4 cycles
// Does nothing
int CPU::NOP()
{
reg_PC.dat += 1;
debugPrint("NOP\n");
return 4;
}

// LD BC, u16
// Loads a 16 bit immediate value into BC
Expand Down
24 changes: 21 additions & 3 deletions src/gameBoy.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "cpu.h"
#include "gameBoy.h"

#include <chrono>

int GBE::s_Cycles;

GBE::GBE()
Expand All @@ -15,6 +17,10 @@ GBE::GBE()
// Initialize the Graphics
gbe_graphics = new PPU();

// Initialize the APU
gbe_sound = new APU();


// Unify the CPU and MemoryMap
gbe_cpu->setMemory(gbe_mMap);

Expand All @@ -24,14 +30,21 @@ GBE::GBE()
// Unify the PPU and MmeoryMap
gbe_graphics->setMemoryMap(gbe_mMap);

// Unify the CPU and MemoryMap
gbe_sound->setMemoryMap(gbe_mMap);

// SDL_INIT_AUDIO and SDL_INIT_VIDEO both are initilised in graphics.cpp
// Don't change ordering of gbe_graphics and gbe_sound
gbe_graphics->init();

gbe_sound->init();

// Open the Boot ROM
if ((bootROM = fopen("../src/dmg_boot.gb", "rb")) == NULL)
printf("boot rom file not opened");

// Open the Game ROM
if ((gameROM = fopen("../tests/pkmnred.gb", "rb")) == NULL)
if ((gameROM = fopen("../tests/dmg_sound/rom_singles/01-registers.gb", "rb")) == NULL)
printf("game rom file not opened");

// Set the Boot ROM
Expand Down Expand Up @@ -98,7 +111,6 @@ GBE::GBE()
gbe_mMap->debugWriteMemory(0x133, 0x3E);

executeBootROM();

update();
}

Expand All @@ -111,14 +123,20 @@ void GBE::update()
{
// Execute the next instruction
s_Cycles += gbe_cpu->executeNextInstruction();

// update the DIV and TIMA timers
gbe_cpu->updateTimers(s_Cycles);
gbe_graphics->executePPU(s_Cycles);
// this runs at a freq of around 27 * freq(DIV) = 442368 Hz
// this is probably enough to implement APU
gbe_sound->stepAPU(s_Cycles);
gbe_sound->test(s_Cycles);
s_Cycles = 0;
s_Cycles += gbe_cpu->performInterrupt();
// printf("s_Cycles after: %d\n\n", s_Cycles);
gbe_graphics->pollEvents();
}

// printf("a: %lld\n", a);
}

void GBE::executeBootROM()
Expand Down
4 changes: 4 additions & 0 deletions src/gameBoy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cpu.h"
#include "mmap.h"
#include "graphics.h"
#include "sound.h"

// GBE stands for GameBoyEmulator

Expand All @@ -28,6 +29,9 @@ class GBE
// Pointer to the Graphics
PPU* gbe_graphics;

// Pointer to the Sound
APU* gbe_sound;

// File pointer for Boot ROM
FILE* bootROM;

Expand Down
7 changes: 6 additions & 1 deletion src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,41 @@ PPU::PPU()
bool PPU::init()
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return false;
}

// Set hint to use hardware acceleration
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Hardware Acceleration not enabled! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return false;
}

// Set hint for VSync
if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
{
printf("VSync not enabled! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return false;
}

// Create window and renderer
if (!(window = SDL_CreateWindow("GameBoy Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN)))
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return false;
}

if (!(renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)))
{
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return false;
}

Expand Down
Loading
Loading