From c8f86c7330437e64930ac370a1f8f4be0be12c7c Mon Sep 17 00:00:00 2001 From: Jonathan Maldonado Contreras Date: Sun, 6 Oct 2019 18:52:36 +0100 Subject: [PATCH] Create reset functionality in the debugger to restart the emulator --- Debugger/CpuDebugger.cpp | 2 +- Debugger/Debugger.cpp | 30 +++++++++++++++++++++++- Debugger/Debugger.h | 2 ++ Memory.cpp | 14 +++++++---- Memory.h | 50 +++++++++++++++++++++++++++++----------- Video.cpp | 23 ++++++++++++++++-- Video.h | 10 ++++++-- main.cpp | 21 ++++++++++++++++- 8 files changed, 128 insertions(+), 24 deletions(-) diff --git a/Debugger/CpuDebugger.cpp b/Debugger/CpuDebugger.cpp index b90ebed..6762ec4 100644 --- a/Debugger/CpuDebugger.cpp +++ b/Debugger/CpuDebugger.cpp @@ -13,7 +13,7 @@ CpuDebugger::CpuDebugger() void CpuDebugger::ComposeView( Cpu &cpu, Memory &memory, u32 cycles, DebuggerMode& mode ) { - ImGui::SetNextWindowPos( ImVec2( 0, 0 ) ); + ImGui::SetNextWindowPos( ImVec2( 0, 100 ) ); ImGui::Begin( "Cpu" ); { ImGui::Columns( 2 ); diff --git a/Debugger/Debugger.cpp b/Debugger/Debugger.cpp index aeadb7c..d7fc302 100644 --- a/Debugger/Debugger.cpp +++ b/Debugger/Debugger.cpp @@ -18,8 +18,9 @@ Debugger::Debugger( Cpu *cpu, Memory *memory, Video *video ) : cpu( cpu ) , memory( memory ) , video( video ) - , mode( DebuggerMode::IDLE ) , window( nullptr ) + , mode( DebuggerMode::IDLE ) + , reset( false ) { } @@ -89,6 +90,12 @@ DebuggerUpdateResult Debugger::Update( float deltaMilliseconds, u32 cycles ) return DebuggerUpdateResult::QUIT; } + if ( reset ) + { + reset = false; + return DebuggerUpdateResult::RESET; + } + if ( elapsed.count() < 16.6f ) { std::this_thread::sleep_for( std::chrono::duration< float, std::milli > ( 16.6F - elapsed.count() ) ); @@ -110,6 +117,12 @@ DebuggerUpdateResult Debugger::Update( float deltaMilliseconds, u32 cycles ) } } + if ( reset ) + { + reset = false; + return DebuggerUpdateResult::RESET; + } + return DebuggerUpdateResult::CONTINUE; } @@ -117,10 +130,25 @@ void Debugger::ComposeView( u32 cycles ) { glfwPollEvents(); ImGuiGLFW::NewFrame(); + + ComposeEmulatorControlView(); cpuDebugger.ComposeView( *cpu, *memory, cycles, mode ); videoDebugger.ComposeView( cycles, *video ); } +void Debugger::ComposeEmulatorControlView() +{ + ImGui::SetNextWindowPos( ImVec2( 0, 0 ) ); + ImGui::Begin( "PatNes Control" ); + + if ( ImGui::Button( "Reset" ) ) + { + reset = true; + } + + ImGui::End(); +} + void Debugger::Render() { i32 width, height; diff --git a/Debugger/Debugger.h b/Debugger/Debugger.h index fc59075..9be6250 100644 --- a/Debugger/Debugger.h +++ b/Debugger/Debugger.h @@ -48,8 +48,10 @@ class Debugger GLFWwindow *window; DebuggerMode mode; + bool reset; void ComposeView( u32 cycles ); + void ComposeEmulatorControlView(); void Render(); bool ShouldCloseWindow() const; }; \ No newline at end of file diff --git a/Memory.cpp b/Memory.cpp index 3a0d73e..a4ea653 100644 --- a/Memory.cpp +++ b/Memory.cpp @@ -8,17 +8,23 @@ Memory::Memory( const Cartridge &cartridge, Video &video ) - : video( video ) + : cartridge( cartridge ) + , video( video ) { map = new byte[ 64_KB ]; - memset( map, 0x00, 64_KB ); - MapCartridge( cartridge ); } Memory::~Memory() { - delete map; + delete[] map; +} + +void Memory::Reset() +{ + memset( map, 0x00, 64_KB ); + + MapCartridge( cartridge ); } byte Memory::Read( word address ) const diff --git a/Memory.h b/Memory.h index 9f2b000..d5106de 100644 --- a/Memory.h +++ b/Memory.h @@ -4,17 +4,33 @@ /* - * MEMORY MAP - * - * 0x0000 - 0x07FF ( 2KB internal RAM ) - * 0x0800 - 0x0FFF ( Mirrors of RAM ) - * 0x1000 - 0x17FF ( Mirrors of RAM ) - * 0x1800 - 0x1FFF ( Mirrors of RAM ) - * 0x2000 - 0x2007 ( NES PPU registers ) - * 0x2008 - 0x3FFF ( Mirrors of PPU registers, repeat every 8 bytes ) - * 0x4000 - 0x4017 ( NES APU & IO registers ) - * 0x4018 - 0x401F ( APU and UI Functionality that is usually disabled ) - * 0x4020 - 0xFFFF ( Cartridge and mapper registers ) + + +-------------------------------------------------------------------------------+ + | | + | NES Memory map | + | | + +---------------------+---------------------------------------------------------+ + | | | + | 0x0000 - 0x07FF | ( 2KB internal RAM ) | + | | | + | 0x0800 - 0x0FFF | ( Mirrors of RAM ) | + | | | + | 0x1000 - 0x17FF | ( Mirrors of RAM ) | + | | | + | 0x1800 - 0x1FFF | ( Mirrors of RAM ) | + | | | + | 0x2000 - 0x2007 | ( NES PPU registers ) | + | | | + | 0x2008 - 0x3FFF | ( Mirrors of PPU registers, repeat every 8 bytes ) | + | | | + | 0x4000 - 0x4017 | ( NES APU & IO registers ) | + | | | + | 0x4018 - 0x401F | ( APU and UI Functionality that is usually disabled ) | + | | | + | 0x4020 - 0xFFFF | ( Cartridge and mapper registers ) | + | | | + +---------------------+---------------------------------------------------------+ + */ @@ -28,6 +44,9 @@ class Memory Memory( const Cartridge &cartridge, Video &video ); ~Memory(); + void Reset(); + + /* Memory management */ byte Read( word address ) const; void Write( word address, byte data ); @@ -35,8 +54,13 @@ class Memory private: - byte *map; - Video &video; + /* Associated NES systems */ + const Cartridge &cartridge; + Video &video; + + /* NES memory map */ + byte *map; + void MapCartridge( const Cartridge &cartridge ); }; \ No newline at end of file diff --git a/Video.cpp b/Video.cpp index df8a963..668c3d6 100644 --- a/Video.cpp +++ b/Video.cpp @@ -10,8 +10,23 @@ Video::Video( Cartridge *cartridge ) : cartridge( cartridge ) { memory = new byte[ 16_KB ]; + frameBuffer = new RGB[ NES_VIDEO_RESOLUTION ]; + + Reset(); +} + +Video::~Video() +{ + delete[] memory; + delete[] frameBuffer; +} + + +void Video::Reset() +{ memset( memory, 0x00, 16_KB ); - MapCartridgeCHRToPPU(); + memset( frameBuffer, 0x00, NES_VIDEO_RESOLUTION ); + MapCartridgeCHRToPPU(); } void Video::MapCartridgeCHRToPPU() @@ -25,10 +40,14 @@ void Video::MapCartridgeCHRToPPU() const u32 dataSize = header.chrRomSizeKB * 1_KB; memcpy( memory, &cartridgeRom[ offset ], dataSize ); - memory[ PPUSTATUS_REGISTER ] = 0b1010'0000; } +RGB* Video::GetFrameBuffer() const +{ + return frameBuffer; +} + const byte * const Video::GetPPUMemory() const { return memory; diff --git a/Video.h b/Video.h index b117296..4ce58bd 100644 --- a/Video.h +++ b/Video.h @@ -39,7 +39,13 @@ class Cartridge; class Video { public: + + static constexpr u32 NES_VIDEO_RESOLUTION = 256 * 240; + Video( Cartridge *memory ); + ~Video(); + + void Reset(); /* PPU memory management */ const byte * const GetPPUMemory() const; @@ -47,7 +53,7 @@ class Video void Write( word address, byte data ); /* Frame buffer */ - byte* GetFrameBuffer() const; + RGB* GetFrameBuffer() const; private: @@ -69,7 +75,7 @@ class Video byte *memory; /* Frame buffer */ - byte *frameBuffer; + RGB *frameBuffer; void MapCartridgeCHRToPPU(); diff --git a/main.cpp b/main.cpp index 16096d1..68cc28e 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,8 @@ #include "Video.h" #include "Debugger/Debugger.h" +#include + static constexpr u32 AVERAGE_CYCLES_PER_FRAME = 29780; int main(int argc, char** argv) @@ -40,7 +42,24 @@ int main(int argc, char** argv) { currentCycles += cpu.Update(); DebuggerUpdateResult result = debugger.Update( 0.f, currentCycles ); - quit = ( result == DebuggerUpdateResult::QUIT ); + + switch ( result ) + { + case DebuggerUpdateResult::QUIT: + { + quit = true; + } + break; + + case DebuggerUpdateResult::RESET: + { + memory.Reset(); + video.Reset(); + cpu.Reset(); + currentCycles = 0; + } + break; + } } return 0;