Skip to content

Commit

Permalink
Extract video debugger from Debugger class into its own class. Add fi…
Browse files Browse the repository at this point in the history
…le with the NES color palette in RGB. Display the NES Palette in the video debugger
  • Loading branch information
Jonazan2 committed Aug 28, 2019
1 parent 9f9c3f7 commit e623793
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 87 deletions.
70 changes: 2 additions & 68 deletions Debugger/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "ImguiWrapper/imgui_impl_glfw_gl3.h"

#include "../CpuTypes.h"
#include "../Video.h"


Expand All @@ -24,13 +23,6 @@ Debugger::Debugger( Cpu *cpu, Memory *memory, Video *video )
{
}

Debugger::~Debugger()
{
delete leftPatternTableBuffer;
delete rightPatternTableBuffer;
}


void Debugger::StartDebugger()
{
glfwInit();
Expand Down Expand Up @@ -61,14 +53,7 @@ void Debugger::StartDebugger()

ImGuiGLFW::Init( window, true );
ImGui::StyleColorsDark();

leftPatternTableBuffer = new RGB[ 128 * 128 ];
ImGuiGLFW::Texture leftPatternTexture = { 0, 128, 128, leftPatternTableBuffer };
leftPatternTableTextureID = ImGuiGLFW::CreateTexture( leftPatternTexture );

rightPatternTableBuffer = new RGB[ 128 * 128 ];
ImGuiGLFW::Texture rightPatternTexture = { 0, 128, 128, rightPatternTableBuffer };
rightPatternTableTextureID = ImGuiGLFW::CreateTexture( rightPatternTexture );
videoDebugger.CreateTextures();

Update(0.f,0);
}
Expand Down Expand Up @@ -122,18 +107,7 @@ void Debugger::ComposeView( u32 cycles )
ImGuiGLFW::NewFrame();

cpuDebugger.ComposeView( *cpu, *memory, cycles, mode );

UpdatePatternTable( 0x0000, leftPatternTableBuffer );
ImGui::SetNextWindowSize( ImVec2( 560, 560 ), ImGuiCond_FirstUseEver );
ImGui::Begin( "VRAM Left" );
ImGui::Image( leftPatternTableTextureID, ImVec2( 512, 512 ) );
ImGui::End();

UpdatePatternTable( 0x1000, rightPatternTableBuffer );
ImGui::SetNextWindowSize( ImVec2( 560, 560 ), ImGuiCond_FirstUseEver );
ImGui::Begin( "VRAM Right" );
ImGui::Image( rightPatternTableTextureID, ImVec2( 512, 512 ) );
ImGui::End();
videoDebugger.ComposeView( cycles, *video );
}

void Debugger::Render()
Expand All @@ -146,43 +120,3 @@ void Debugger::Render()
ImGuiGLFW::RenderDrawLists( ImGui::GetDrawData() );
glfwSwapBuffers( window );
}


void Debugger::UpdatePatternTable( word address, RGB *buffer )
{
constexpr static RGB palette[4] = { { 255,255,255 },{ 0xCC,0xCC,0xCC },{ 0x77,0x77,0x77 }, { 0x0,0x0,0x0 } };

const byte * const ppuMemory = video->GetPPUMemory();
assert( buffer != nullptr );
assert( ppuMemory != nullptr );

u32 vramPosition = 0;
for ( u32 tile = 0; tile < 256; ++tile )
{
const u32 tileAddress = (tile * 0x10) + address;

u32 localvramPosition = vramPosition;
for ( byte row = 0; row < 8; ++row )
{
const byte firstByte = ppuMemory[ tileAddress + row ];
const byte secondByte = ppuMemory[ tileAddress + row + 0x07 ];

for ( byte column = 0; column < 8; ++column)
{
byte mask = 0x01 << column;
const byte value = ((secondByte & mask) >> column) << 1;
const byte value2 = (firstByte & mask) >> column;
const byte finalValue = value | value2;

buffer[ localvramPosition + (7 - column) ] = palette[ finalValue ];
}
localvramPosition += 128;
}
vramPosition += 8;

if ( tile > 0 && (tile + 1) % 16 == 0)
{
vramPosition += 128 * 7;
}
}
}
18 changes: 4 additions & 14 deletions Debugger/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "../Types.h"
#include "../Cpu.h"
#include "CpuDebugger.h"
#include "VideoDebugger.h"


enum class DebuggerMode : byte
{
Expand All @@ -19,38 +21,26 @@ class Debugger
{
public:
Debugger( Cpu *cpu, Memory *memory, Video *video );
Debugger(Debugger &) = delete;
~Debugger();

Debugger( Debugger & ) = delete;

void StartDebugger();
void Update( float deltaMilliseconds, u32 cycles );
void CloseDebugger();

private:

using ImTextureID = void *;

/* Systems */
Cpu *cpu;
Memory *memory;
Video *video;

/* Specific Debuggers */
CpuDebugger cpuDebugger;

/* Textures */
ImTextureID leftPatternTableTextureID;
RGB *leftPatternTableBuffer;

ImTextureID rightPatternTableTextureID;
RGB *rightPatternTableBuffer;
VideoDebugger videoDebugger;

GLFWwindow *window;
DebuggerMode mode;

void ComposeView( u32 cycles );
void Render();

void UpdatePatternTable( word address, RGB *buffer );
};
96 changes: 96 additions & 0 deletions Debugger/VideoDebugger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "VideoDebugger.h"

#include "ImguiWrapper/imgui_impl_glfw_gl3.h"
#include "../Video.h"
#include "../PaletteColors.h"


VideoDebugger::~VideoDebugger()
{
delete leftPatternTableBuffer;
delete rightPatternTableBuffer;
delete nesPaletteTextureBuffer;
}

void VideoDebugger::CreateTextures()
{
leftPatternTableBuffer = new RGB[ 128 * 128 ];
ImGuiGLFW::Texture leftPatternTexture = { 0, 128, 128, leftPatternTableBuffer };
leftPatternTableTextureID = ImGuiGLFW::CreateTexture( leftPatternTexture );

rightPatternTableBuffer = new RGB[ 128 * 128 ];
ImGuiGLFW::Texture rightPatternTexture = { 0, 128, 128, rightPatternTableBuffer };
rightPatternTableTextureID = ImGuiGLFW::CreateTexture( rightPatternTexture );

nesPaletteTextureBuffer = new RGB[ 64 ];
ImGuiGLFW::Texture paletteTexture = { 0, 16, 4, nesPaletteTextureBuffer };
nesPaletteTextureID = ImGuiGLFW::CreateTexture( paletteTexture );
GenerateNesPaletteTexture();
}

void VideoDebugger::ComposeView( u32 cycles, Video &video )
{
UpdatePatternTable( video, 0x0000, leftPatternTableBuffer );
ImGui::SetNextWindowSize( ImVec2( 560, 560 ), ImGuiCond_FirstUseEver );
ImGui::Begin( "VRAM Left" );
ImGui::Image( leftPatternTableTextureID, ImVec2( 512, 512 ) );
ImGui::End();

UpdatePatternTable( video, 0x1000, rightPatternTableBuffer );
ImGui::SetNextWindowSize( ImVec2( 560, 560 ), ImGuiCond_FirstUseEver );
ImGui::Begin( "VRAM Right" );
ImGui::Image( rightPatternTableTextureID, ImVec2( 512, 512 ) );
ImGui::End();

ImGui::SetNextWindowSize( ImVec2( 260, 70 ), ImGuiCond_FirstUseEver );
ImGui::Begin( "NES Palette" );
ImGui::Image( nesPaletteTextureID, ImVec2( 256, 64 ) );
ImGui::End();
}

void VideoDebugger::UpdatePatternTable( Video &video, word address, RGB *buffer )
{
constexpr static RGB palette[4] = { { 255,255,255 },{ 0xCC,0xCC,0xCC },{ 0x77,0x77,0x77 }, { 0x0,0x0,0x0 } };

const byte * const ppuMemory = video.GetPPUMemory();
assert( buffer != nullptr );
assert( ppuMemory != nullptr );

u32 vramPosition = 0;
for ( u32 tile = 0; tile < 256; ++tile )
{
const u32 tileAddress = (tile * 0x10) + address;

u32 localvramPosition = vramPosition;
for ( byte row = 0; row < 8; ++row )
{
const byte firstByte = ppuMemory[ tileAddress + row ];
const byte secondByte = ppuMemory[ tileAddress + row + 0x07 ];

for ( byte column = 0; column < 8; ++column)
{
byte mask = 0x01 << column;
const byte value = ((secondByte & mask) >> column) << 1;
const byte value2 = (firstByte & mask) >> column;
const byte finalValue = value | value2;

buffer[ localvramPosition + (7 - column) ] = palette[ finalValue ];
}
localvramPosition += 128;
}
vramPosition += 8;

if ( tile > 0 && (tile + 1) % 16 == 0)
{
vramPosition += 128 * 7;
}
}
}

void VideoDebugger::GenerateNesPaletteTexture()
{
for ( byte color = 0; color < 64; ++color )
{
nesPaletteTextureBuffer[ color ] = NES_PALETTE_COLORS[ color ];
}
}
32 changes: 32 additions & 0 deletions Debugger/VideoDebugger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "../Types.h"

class Video;

class VideoDebugger
{
public:
VideoDebugger() = default;
~VideoDebugger();

void CreateTextures();
void ComposeView( u32 cycles, Video &video );

private:
using ImTextureID = void *;

/* Textures */
ImTextureID leftPatternTableTextureID;
RGB *leftPatternTableBuffer;

ImTextureID rightPatternTableTextureID;
RGB *rightPatternTableBuffer;

ImTextureID nesPaletteTextureID;
RGB *nesPaletteTextureBuffer;


void UpdatePatternTable( Video &video, word address, RGB *buffer );
void GenerateNesPaletteTexture();
};
9 changes: 7 additions & 2 deletions Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

Memory::Memory( Cartridge *cartridge )
{
map = new byte[ 0x10000 ];
memset( map, 0, 0x10000 );
map = new byte[ 64_KB ];
memset( map, 0x00, 64_KB );

MapCartridge( cartridge );
}
Expand All @@ -29,6 +29,11 @@ void Memory::Write( word address, byte data )
map[ address ] = data;
}

const byte *const Memory::GetMemoryMap() const
{
return map;
}

void Memory::MapCartridge( Cartridge *cartridge )
{
assert( cartridge != nullptr );
Expand Down
2 changes: 2 additions & 0 deletions Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Memory
byte Read( word address ) const;
void Write( word address, byte data );

const byte *const GetMemoryMap() const;

private:

byte *map;
Expand Down
77 changes: 77 additions & 0 deletions PaletteColors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include <array>
#include "Types.h"

static constexpr std::array< RGB, 64 > NES_PALETTE_COLORS =
{
{
{ 0x7C, 0x7C, 0x7C },
{ 0x00, 0x00, 0xFC },
{ 0x00, 0x00, 0xBC },
{ 0x44, 0x28, 0xBC },
{ 0x94, 0x00, 0x84 },
{ 0xA8, 0x00, 0x20 },
{ 0xA8, 0x10, 0x00 },
{ 0x88, 0x14, 0x00 },
{ 0x50, 0x30, 0x00 },
{ 0x00, 0x78, 0x00 },
{ 0x00, 0x68, 0x00 },
{ 0x00, 0x58, 0x00 },
{ 0x00, 0x40, 0x58 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },

{ 0xBC, 0xBC, 0xBC },
{ 0x00, 0x78, 0xF8 },
{ 0x00, 0x58, 0xF8 },
{ 0x68, 0x44, 0xFC },
{ 0xD8, 0x00, 0xCC },
{ 0xE4, 0x00, 0x58 },
{ 0xF8, 0x38, 0x00 },
{ 0xE4, 0x5C, 0x10 },
{ 0xAC, 0x7C, 0x00 },
{ 0x00, 0xB8, 0x00 },
{ 0x00, 0xA8, 0x00 },
{ 0x00, 0xA8, 0x44 },
{ 0x00, 0x88, 0x88 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },

{ 0xF8, 0xF8, 0xF8 },
{ 0x3C, 0xBC, 0xFC },
{ 0x68, 0x88, 0xFC },
{ 0x98, 0x78, 0xF8 },
{ 0xF8, 0x78, 0xF8 },
{ 0xF8, 0x58, 0x98 },
{ 0xF8, 0x78, 0x58 },
{ 0xFC, 0xA0, 0x44 },
{ 0xF8, 0xB8, 0x00 },
{ 0xB8, 0xF8, 0x18 },
{ 0x58, 0xD8, 0x54 },
{ 0x58, 0xF8, 0x98 },
{ 0x00, 0xE8, 0xD8 },
{ 0x78, 0x78, 0x78 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },

{ 0xFC, 0xFC, 0xFC },
{ 0xA4, 0xE4, 0xFC },
{ 0xB8, 0xB8, 0xF8 },
{ 0xD8, 0xB8, 0xF8 },
{ 0xF8, 0xB8, 0xF8 },
{ 0xF8, 0xA4, 0xC0 },
{ 0xF0, 0xD0, 0xB0 },
{ 0xFC, 0xE0, 0xA8 },
{ 0xF8, 0xD8, 0x78 },
{ 0xD8, 0xF8, 0x78 },
{ 0xB8, 0xF8, 0xB8 },
{ 0xB8, 0xF8, 0xD8 },
{ 0x00, 0xFC, 0xFC },
{ 0xF8, 0xD8, 0xF8 },
{ 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 }
}
};
Loading

0 comments on commit e623793

Please sign in to comment.