Skip to content

Commit

Permalink
Add ppu const addresses and fix some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonazan2 committed Oct 6, 2019
1 parent c93f3c7 commit c345255
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 39 deletions.
2 changes: 2 additions & 0 deletions Cartridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ bool Cartridge::TryLoad( const char* romFile )
{
return TryLoadHeader();
}

return false;
}

bool Cartridge::TryLoadHeader()
Expand Down
1 change: 1 addition & 0 deletions Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ short Cpu::ExecuteMappableInstruction( byte opcode )
default:
{
assert( false && "How did I get here?. The show where instructions say...How did I get here?");
return 0;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Debugger/ImguiWrapper/imgui_impl_glfw_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <GLFW/glfw3native.h>
#endif

#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS 1
#include <unordered_map>


Expand Down
33 changes: 24 additions & 9 deletions Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include <assert.h>
#include "Cartridge.h"
#include "Video.h"


Memory::Memory( Cartridge *cartridge )
Memory::Memory( const Cartridge &cartridge, Video &video )
: video( video )
{
map = new byte[ 64_KB ];
memset( map, 0x00, 64_KB );
Expand All @@ -21,33 +23,46 @@ Memory::~Memory()

byte Memory::Read( word address ) const
{
return map[ address ];
if ( address >= 0x2000 && address <= 0x2007 )
{
/* PPU memory */
return video.Read( address );
}
else
{
return map[ address ];
}
}

void Memory::Write( word address, byte data )
{
map[ address ] = data;
if ( address >= 0x2000 && address <= 0x2007 )
{
/* PPU memory */
video.Write( address, data );
}
else
{
map[ address ] = data;
}
}

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

void Memory::MapCartridge( Cartridge *cartridge )
void Memory::MapCartridge( const Cartridge &cartridge )
{
assert( cartridge != nullptr );

/* For now only support NROM with PRG ROM of 16KB and no ram */
Cartridge::Header header = cartridge->GetHeader();
Cartridge::Header header = cartridge.GetHeader();
assert( header.mapper == 0x00 && header.prgRomSizeKB == 16 && !header.hasPRGRam);

const byte * const rom = cartridge->GetRom();
const byte * const rom = cartridge.GetRom();

/* Map the PRG ROM to 0x8000 */
memcpy(&map[0x8000], &rom[0x0010], 16_KB );

/* Mirror the PRG ROM in 0xC000 */
memcpy(&map[0xC000], &rom[0x0010], 16_KB );

}
8 changes: 5 additions & 3 deletions Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@


class Cartridge;
class Video;

class Memory
{
public:

Memory( Cartridge *cartridge );
Memory( const Cartridge &cartridge, Video &video );
~Memory();

byte Read( word address ) const;
Expand All @@ -34,7 +35,8 @@ class Memory

private:

byte *map;
byte *map;
Video &video;

void MapCartridge( Cartridge *cartridge );
void MapCartridge( const Cartridge &cartridge );
};
8 changes: 2 additions & 6 deletions Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ struct RGB

constexpr RGB PINK = { 0xFF, 0x00, 0x80 };

/* Operators */

namespace
{
/* Operators */

u64 operator"" _KB( u64 size )
constexpr u64 operator"" _KB( u64 size )
{
return size * 1024;
}

}
17 changes: 15 additions & 2 deletions Video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Video::Video( Cartridge *cartridge )
{
memory = new byte[ 16_KB ];
memset( memory, 0x00, 16_KB );
MapCartridgeCHRToPPU();
MapCartridgeCHRToPPU();
}

void Video::MapCartridgeCHRToPPU()
Expand All @@ -24,9 +24,22 @@ void Video::MapCartridgeCHRToPPU()
const u32 offset = (header.prgRomSizeKB * 1_KB) + 0x0010;
const u32 dataSize = header.chrRomSizeKB * 1_KB;
memcpy( memory, &cartridgeRom[ offset ], dataSize );


memory[ PPUSTATUS_REGISTER ] = 0b1010'0000;
}

const byte * const Video::GetPPUMemory() const
{
return memory;
}
}

byte Video::Read( word address ) const
{
return memory[ address ];
}

void Video::Write( word address, byte data )
{
memory[ address ] = data;
}
20 changes: 20 additions & 0 deletions Video.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,36 @@ class Video
public:
Video( Cartridge *memory );

/* PPU memory management */
const byte * const GetPPUMemory() const;
byte Read( word address ) const;
void Write( word address, byte data );

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

private:

/* PPU Register addresses */
static constexpr word PPUCTRL_REGISTER = 0x2000;
static constexpr word PPUMASK_REGISTER = 0x2001;
static constexpr word PPUSTATUS_REGISTER = 0x2002;
static constexpr word OAMA_REGISTER = 0x2003;
static constexpr word OAMADATA_REGISTER = 0x2004;
static constexpr word PPUSCROLL_REGISTER = 0x2005;
static constexpr word PPUADDR_REGISTER = 0x2006;
static constexpr word PPUDATA_ADDRESS = 0x2007;


/* Associated Systems */
Cartridge *cartridge;

/* PPU memory layout */
byte *memory;

/* Frame buffer */
byte *frameBuffer;


void MapCartridgeCHRToPPU();
};
36 changes: 18 additions & 18 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ int main(int argc, char** argv)
return -1;
}


Cartridge cartridge( argv[1] );
if ( cartridge.IsLoaded() )
if ( !cartridge.IsLoaded() )
{
cartridge.PrintDetails();
std::cout << "The cartridge couldn't be loaded";
return -1;
}

Memory memory( &cartridge );
Cpu cpu( &memory );
Video video( &cartridge );

Debugger debugger( &cpu, &memory, &video );
debugger.StartDebugger();
cartridge.PrintDetails();

/* Run the first frame for now */
u32 currentCycles = 0;
while ( currentCycles < AVERAGE_CYCLES_PER_FRAME )
{
currentCycles += cpu.Update();
debugger.Update(0.f, currentCycles);
}
Video video( &cartridge );
Memory memory( cartridge, video );
Cpu cpu( &memory );

return 0;
Debugger debugger( &cpu, &memory, &video );
debugger.StartDebugger();

/* Run a few frames for now */
u32 currentCycles = 0;
while ( currentCycles < AVERAGE_CYCLES_PER_FRAME )
{
currentCycles += cpu.Update();
debugger.Update(0.f, currentCycles);
}

std::cout << "The cartridge couldn't be loaded";
return -1;
return 0;
}

0 comments on commit c345255

Please sign in to comment.