Skip to content

Commit

Permalink
Create reset functionality in the debugger to restart the emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonazan2 committed Oct 6, 2019
1 parent 5a8c1d0 commit c8f86c7
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Debugger/CpuDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
30 changes: 29 additions & 1 deletion Debugger/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
}

Expand Down Expand Up @@ -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() ) );
Expand All @@ -110,17 +117,38 @@ DebuggerUpdateResult Debugger::Update( float deltaMilliseconds, u32 cycles )
}
}

if ( reset )
{
reset = false;
return DebuggerUpdateResult::RESET;
}

return DebuggerUpdateResult::CONTINUE;
}

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;
Expand Down
2 changes: 2 additions & 0 deletions Debugger/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class Debugger

GLFWwindow *window;
DebuggerMode mode;
bool reset;

void ComposeView( u32 cycles );
void ComposeEmulatorControlView();
void Render();
bool ShouldCloseWindow() const;
};
14 changes: 10 additions & 4 deletions Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 37 additions & 13 deletions Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) |
| | |
+---------------------+---------------------------------------------------------+
*/


Expand All @@ -28,15 +44,23 @@ class Memory
Memory( const Cartridge &cartridge, Video &video );
~Memory();

void Reset();

/* Memory management */
byte Read( word address ) const;
void Write( word address, byte data );

const byte *const GetMemoryMap() const;

private:

byte *map;
Video &video;
/* Associated NES systems */
const Cartridge &cartridge;
Video &video;

/* NES memory map */
byte *map;


void MapCartridge( const Cartridge &cartridge );
};
23 changes: 21 additions & 2 deletions Video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions Video.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,21 @@ 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;
byte Read( word address ) const;
void Write( word address, byte data );

/* Frame buffer */
byte* GetFrameBuffer() const;
RGB* GetFrameBuffer() const;

private:

Expand All @@ -69,7 +75,7 @@ class Video
byte *memory;

/* Frame buffer */
byte *frameBuffer;
RGB *frameBuffer;


void MapCartridgeCHRToPPU();
Expand Down
21 changes: 20 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "Video.h"
#include "Debugger/Debugger.h"

#include <assert.h>

static constexpr u32 AVERAGE_CYCLES_PER_FRAME = 29780;

int main(int argc, char** argv)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c8f86c7

Please sign in to comment.