Skip to content

Commit

Permalink
Setup mirroring of ppu registers in memory. Initialize such registers…
Browse files Browse the repository at this point in the history
… at power up properly
  • Loading branch information
Jonazan2 committed Oct 16, 2019
1 parent 025077b commit 83e00ab
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 38 deletions.
25 changes: 13 additions & 12 deletions Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
#include "Video.h"


Memory::Memory( const Cartridge &cartridge, Video &video )
Memory::Memory( const Cartridge *cartridge )
: cartridge( cartridge )
, video( video )
{
map = new byte[ 64_KB ];
Reset();
Expand All @@ -24,15 +23,15 @@ void Memory::Reset()
{
memset( map, 0x00, 64_KB );

MapCartridge( cartridge );
MapCartridge();
}

byte Memory::Read( word address ) const
{
if ( address >= 0x2000 && address <= 0x2007 )
if ( address >= 0x2000 && address <= 0x3FFF )
{
/* PPU memory */
return video.Read( address );
const word mirroredAddress = ( address % 8 ) + 0x2000;
return map[ mirroredAddress ];
}
else
{
Expand All @@ -42,10 +41,10 @@ byte Memory::Read( word address ) const

void Memory::Write( word address, byte data )
{
if ( address >= 0x2000 && address <= 0x2007 )
if ( address >= 0x2000 && address <= 0x3FFF )
{
/* PPU memory */
video.Write( address, data );
const word mirroredAddress = ( address % 8 ) + 0x2000;
map[ mirroredAddress ] = data;
}
else
{
Expand All @@ -58,13 +57,15 @@ const byte *const Memory::GetMemoryMap() const
return map;
}

void Memory::MapCartridge( const Cartridge &cartridge )
void Memory::MapCartridge()
{
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 );
Expand Down
7 changes: 3 additions & 4 deletions Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Memory
{
public:

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

void Reset();
Expand All @@ -55,12 +55,11 @@ class Memory
private:

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

/* NES memory map */
byte *map;


void MapCartridge( const Cartridge &cartridge );
void MapCartridge();
};
18 changes: 17 additions & 1 deletion Video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <assert.h>

#include "Cartridge.h"
#include "Memory.h"


Video::Video( Cartridge *cartridge )
Video::Video( Cartridge *cartridge, Memory *cpuMemory )
: cartridge( cartridge )
, cpuMemory( cpuMemory )
{
memory = new byte[ 16_KB ];
frameBuffer = new RGB[ NES_VIDEO_RESOLUTION ];
Expand All @@ -32,6 +34,15 @@ void Video::Reset()
}

MapCartridgeCHRToPPU();

cpuMemory->Write( PPUCTRL_REGISTER, 0x00 );
cpuMemory->Write( PPUMASK_REGISTER, 0x00 );
cpuMemory->Write( PPUSTATUS_REGISTER, 0b1010'0000 );
cpuMemory->Write( OAMA_REGISTER, 0x00 );
cpuMemory->Write( OAMADATA_REGISTER, 0x00 );
cpuMemory->Write( PPUSCROLL_REGISTER, 0x00 );
cpuMemory->Write( PPUADDR_REGISTER, 0x00 );
cpuMemory->Write( PPUDATA_ADDRESS, 0x00 );
}

void Video::MapCartridgeCHRToPPU()
Expand Down Expand Up @@ -67,3 +78,8 @@ void Video::Write( word address, byte data )
{
memory[ address ] = data;
}

void Video::Update( u32 cycles )
{

}
43 changes: 27 additions & 16 deletions Video.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
| | |
| 0x1000 - 0x1FFF | Pattern table 1 |
| | |
| 0x2000 - 0x23FF | Nametable 0 |
| 0x2000 - 0x23BF | Nametable 0 |
| | |
| 0x2400 - 0x27FF | Nametable 1 |
| 0x23C0 - 0x23FF | Attribute table 0 |
| | |
| 0x2400 - 0x27BF | Nametable 1 |
| | |
| 0x27C0 - 0x27FF | Attribute table 1 |
| | |
| 0x2800 - 0x2BFF | Nametable 2 |
| | |
| 0x2C00 - 0x2FFF | Nametable 3 |
| 0x2BC0 - 0x2BFF | Attribute table 2 |
| | |
| 0x2C00 - 0x2FBF | Nametable 3 |
| | |
| 0x2FC0 - 0x2FFF | Attribute table 3 |
| | |
| 0x3000 - 0x3EFF | Mirrors of 0x2000 0x2EFF |
| | |
Expand All @@ -35,18 +43,32 @@


class Cartridge;
class Memory;

class Video
{
public:

static constexpr u32 NES_VIDEO_RESOLUTION = 256 * 240;

Video( Cartridge *memory );
/* 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;


Video( Cartridge *cartridge, Memory *cpuMemory );
~Video();

void Reset();

void Update( u32 cycles );

/* PPU memory management */
const byte * const GetPPUMemory() const;
byte Read( word address ) const;
Expand All @@ -56,20 +78,9 @@ class Video
RGB* 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;
Memory *cpuMemory;

/* PPU memory layout */
byte *memory;
Expand Down
15 changes: 10 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ int main(int argc, char** argv)

cartridge.PrintDetails();

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

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

/* Run a few frames for now */
bool quit = false;
u32 currentCycles = 0;
while ( currentCycles < AVERAGE_CYCLES_PER_FRAME && !quit )
while ( !quit )
{
currentCycles += cpu.Update();
DebuggerUpdateResult result = debugger.Update( 0.f, currentCycles );
video.Update( currentCycles );

const DebuggerUpdateResult result = debugger.Update( 0.f, currentCycles );
switch ( result )
{
case DebuggerUpdateResult::QUIT:
Expand All @@ -60,6 +60,11 @@ int main(int argc, char** argv)
}
break;
}

if ( currentCycles >= AVERAGE_CYCLES_PER_FRAME )
{
currentCycles = 0;
}
}

return 0;
Expand Down

0 comments on commit 83e00ab

Please sign in to comment.