diff --git a/Debugger/Debugger.cpp b/Debugger/Debugger.cpp index 8498304..65fc17d 100644 --- a/Debugger/Debugger.cpp +++ b/Debugger/Debugger.cpp @@ -11,7 +11,6 @@ #include "ImguiWrapper/imgui_impl_glfw_gl3.h" -#include "../CpuTypes.h" #include "../Video.h" @@ -24,13 +23,6 @@ Debugger::Debugger( Cpu *cpu, Memory *memory, Video *video ) { } -Debugger::~Debugger() -{ - delete leftPatternTableBuffer; - delete rightPatternTableBuffer; -} - - void Debugger::StartDebugger() { glfwInit(); @@ -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); } @@ -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() @@ -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; - } - } -} \ No newline at end of file diff --git a/Debugger/Debugger.h b/Debugger/Debugger.h index b887a3f..60507eb 100644 --- a/Debugger/Debugger.h +++ b/Debugger/Debugger.h @@ -3,6 +3,8 @@ #include "../Types.h" #include "../Cpu.h" #include "CpuDebugger.h" +#include "VideoDebugger.h" + enum class DebuggerMode : byte { @@ -19,9 +21,7 @@ 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 ); @@ -29,8 +29,6 @@ class Debugger private: - using ImTextureID = void *; - /* Systems */ Cpu *cpu; Memory *memory; @@ -38,19 +36,11 @@ class Debugger /* 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 ); }; \ No newline at end of file diff --git a/Debugger/VideoDebugger.cpp b/Debugger/VideoDebugger.cpp new file mode 100644 index 0000000..30408e1 --- /dev/null +++ b/Debugger/VideoDebugger.cpp @@ -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 ]; + } +} \ No newline at end of file diff --git a/Debugger/VideoDebugger.h b/Debugger/VideoDebugger.h new file mode 100644 index 0000000..5255f4a --- /dev/null +++ b/Debugger/VideoDebugger.h @@ -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(); +}; \ No newline at end of file diff --git a/Memory.cpp b/Memory.cpp index 148ec7e..6860d8e 100644 --- a/Memory.cpp +++ b/Memory.cpp @@ -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 ); } @@ -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 ); diff --git a/Memory.h b/Memory.h index d16321f..3df1cab 100644 --- a/Memory.h +++ b/Memory.h @@ -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; diff --git a/PaletteColors.h b/PaletteColors.h new file mode 100644 index 0000000..4a575bc --- /dev/null +++ b/PaletteColors.h @@ -0,0 +1,77 @@ +#pragma once + +#include +#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 } + } +}; diff --git a/Types.h b/Types.h index 8cc2ac4..17d5b80 100644 --- a/Types.h +++ b/Types.h @@ -27,7 +27,7 @@ struct RGB byte green; byte blue; - bool isEqual(RGB other) const + bool isEqual( RGB other ) const { return red == other.red && green == other.green && blue == other.blue; } diff --git a/Video.h b/Video.h index 8dc5de1..ae35a19 100644 --- a/Video.h +++ b/Video.h @@ -23,7 +23,9 @@ | | | | 0x3000 - 0x3EFF | Mirrors of 0x2000 0x2EFF | | | | - | 0x3F00 - 0x3F1F | Palette RAM indexes | + | 0x3F00 - 0x3F0F | BG Palette RAM indexes | + | | | + | 0x3F10 - 0x3F1F | Sprite Palette RAM indexes | | | | | 0x3F20 - 0x3FFF | Mirrors of 0x3F00 0x3F1F | | | | @@ -48,7 +50,7 @@ class Video /* PPU memory layout */ byte *memory; - + void MapCartridgeCHRToPPU(); }; \ No newline at end of file