Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ class CPU
void parse_opcode(Opcode code);
void debug();

private:


Memory* memory;

private:
const int
FLAG_ZERO = 0b10000000,
FLAG_SUB = 0b01000000,
Expand Down
117 changes: 117 additions & 0 deletions src/debugger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include"debugger.h"

debugger::debugger()
{
window.create(sf::VideoMode(1000, 600), "Debugger");
ImGui::SFML::Init(window);
}

debugger::~debugger()
{
ImGui::SFML::Shutdown();
window.close();
}

void debugger::update(CPU& cpu)
{
//handle input
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event);
if (event.type == sf::Event::Closed)
window.close();
}

ImGui::SFML::Update(window,deltaClock.restart());

window.clear(sf::Color(15, 34, 50));

renderCpuState(cpu);
renderWRAMState(cpu);
renderVRAMState(cpu);

ImGui::SFML::Render(window);
window.display();
}

void debugger::renderCpuState(CPU& cpu) const
{
//set window pos and size
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(500, 200));
ImGui::StyleColorsLight();

ImGui::Begin("CPU state", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse); //no window resize and mov;

ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Program Counter: ");
ImGui::SameLine();
ImGui::Text("%04X", cpu.reg_PC);

ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Stack Pointer: ");
ImGui::SameLine();
ImGui::Text("%04X", cpu.reg_SP);

ImGui::NewLine();

ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Registers");
//all registers
ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Reg_A: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_A);
ImGui::SameLine();
ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), " Reg_B: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_B);

ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Reg_C: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_C);
ImGui::SameLine();
ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), " Reg_D: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_D);

ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Reg_E: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_E);
ImGui::SameLine();
ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), " Reg_H: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_H);

ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), "Reg_L: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_L);
ImGui::SameLine();
ImGui::TextColored(ImVec4(0.3f, 0.2f, 1.0f, 1.0f), " Reg_F: ");
ImGui::SameLine();
ImGui::Text("%02X", cpu.reg_F);


ImGui::End();
}

void debugger::renderWRAMState(CPU& cpu)
{
ImGui::SetNextWindowPos(ImVec2(0, 200));
ImGui::SetNextWindowSize(ImVec2(500, 400));
ImGui::StyleColorsLight();

ImGui::Begin("WRAM State", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse);
memEditor.ReadOnly = true;
memEditor.DrawContents(cpu.memory->WRAM.data(), 8192);
ImGui::End();
}

void debugger::renderVRAMState(CPU& cpu)
{
ImGui::SetNextWindowPos(ImVec2(500, 0));
ImGui::SetNextWindowSize(ImVec2(500, 600));
ImGui::StyleColorsLight();

ImGui::Begin("VRAM State", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse);
memEditor.ReadOnly = true;
memEditor.DrawContents(cpu.memory->VRAM.data(), 8192);
ImGui::End();
}
27 changes: 27 additions & 0 deletions src/debugger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include<SFML\Window.hpp>
#include<SFML\Graphics.hpp>
#include<imgui.h>
#include<imgui-SFML.h>
#include<imgui_memory_editor.h>
#include"cpu.h"

class debugger
{
public:

sf::RenderWindow window;
sf::Clock deltaClock;
MemoryEditor memEditor;

debugger();
~debugger();

void update(CPU& cpu);

void renderCpuState(CPU& cpu) const;

void renderWRAMState(CPU& cpu);

void renderVRAMState(CPU& cpu);
};
6 changes: 6 additions & 0 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void Emulator::run()
time = time.Zero;
//cout << display.scanlines_rendered << endl;
display.scanlines_rendered = 0;

//debugger
if (debugWindow.window.isOpen())
{
debugWindow.update(cpu);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "cpu.h"
#include "memory.h"
#include "display.h"
#include "debugger.h"

typedef sf::Keyboard::Key Key;

Expand All @@ -19,6 +20,8 @@ class Emulator
Memory memory;
Display display;

debugger debugWindow;

private:

float framerate = 60;
Expand Down
19 changes: 6 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
#include "cpu.h"
#include "display.h"

int main(int argc, char *args[])
int main(int argc, char **argv)
{
if (argc != 2)
{
std::cout<< "Usage: " << argv[0] << "<ROM>\n";
}
Emulator emulator;

//string name = "cpu/cpu_instrs";
//string name = "instr_timing";

//emulator.memory.load_rom("tests/" + name + ".gb");
//emulator.memory.load_rom("roms/Dr. Mario.gb");
//emulator.memory.load_rom("roms/kirby.gb");
//emulator.memory.load_rom("roms/tetris.gb");
//emulator.memory.load_rom("roms/minesweeper.gb");
//emulator.memory.load_rom("roms/Super Mario Land.gb");
//emulator.memory.load_rom("roms/cASTELIAN.gb");
//emulator.memory.load_rom("roms/Serpent.gb");
emulator.memory.load_rom("roms/yupferris.gb");
emulator.memory.load_rom(argv[1]);

emulator.run();

Expand Down
2 changes: 1 addition & 1 deletion src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Memory::load_rom(std::string location)
Byte cart = buffer[0x0147];
cout << "Cartridge Type: " << cart_types[cart] << endl;

delete controller;
//delete controller;

// Assign memory controller based on cartridge specification
switch (cart)
Expand Down
10 changes: 5 additions & 5 deletions src/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ class Memory
// Dynamic Memory Controller
MemoryController* controller;

void do_dma_transfer();
Byte get_joypad_state();

public:

// Memory Regions
vector<Byte> VRAM; // $8000 - $9FFF, 8kB Video RAM
vector<Byte> OAM; // $FE00 - $FEA0, OAM Sprite RAM
vector<Byte> WRAM; // $C000 - $DFFF, 8kB Working RAM
vector<Byte> ZRAM; // $FF80 - $FFFF, 128 bytes of RAM

void do_dma_transfer();
Byte get_joypad_state();

public:

MemoryRegister
P1,
DIV, TIMA, TMA, TAC,
Expand Down