Skip to content

Commit

Permalink
Map NROM cartridge into memory, set the PC to the reset vector at the…
Browse files Browse the repository at this point in the history
… beginning of the emulation
  • Loading branch information
Jonazan2 committed May 30, 2019
1 parent 0d1ad19 commit 440a798
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 11 deletions.
20 changes: 15 additions & 5 deletions Cartridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool Cartridge::TryLoad( const char* romFile )

bool Cartridge::TryLoadHeader()
{
static constexpr ui64 ROM_CONSTANT_HEADER = 0x4E45531A;
static constexpr u64 ROM_CONSTANT_HEADER = 0x4E45531A;

if ( rom == nullptr )
{
Expand All @@ -67,7 +67,7 @@ bool Cartridge::TryLoadHeader()
return false;
}

ui64 constant = 0;
u64 constant = 0;
for ( byte i = 0x00; i <= 0x03; ++i )
{
constant <<= 8;
Expand All @@ -79,8 +79,8 @@ bool Cartridge::TryLoadHeader()
return false;
}

header.prgRomSizeKB = static_cast<ui32>( rom[ 0x04 ] ) * 16;
header.chrRomSizeKB = static_cast<ui32>( rom[ 0x05 ] ) * 8;
header.prgRomSizeKB = static_cast< u32 >( rom[ 0x04 ] ) * 16;
header.chrRomSizeKB = static_cast< u32 >( rom[ 0x05 ] ) * 8;

/* Flags 6 */
header.mirroringType = MirroringType( rom[ 0x06 ] & 0b0000'0001 );
Expand Down Expand Up @@ -108,7 +108,17 @@ void Cartridge::PrintDetails() const
<< "Contains PRG RAM: " << header.hasPRGRam << "\n"
<< "Contains 512B trainer: " << header.has512BTrainer << "\n"
<< "Ignores Mirroring: " << header.ignoreMirroring << "\n"
<< "Mapper: " << static_cast< ui32 >( header.mapper );
<< "Mapper: " << static_cast< u32 >( header.mapper );

std::cout << std::endl;
}

const Cartridge::Header& Cartridge::GetHeader() const
{
return header;
}

const byte* const Cartridge::GetRom() const
{
return rom;
}
8 changes: 5 additions & 3 deletions Cartridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class Cartridge

struct Header
{
ui32 prgRomSizeKB;
ui32 chrRomSizeKB;
u32 prgRomSizeKB;
u32 chrRomSizeKB;
Cartridge::MirroringType mirroringType;
bool hasPersistentMemory;
bool hasPRGRam;
Expand All @@ -53,10 +53,12 @@ class Cartridge

void PrintDetails() const;
bool IsLoaded() const;
const Header& GetHeader() const;
const byte * const GetRom() const;

private:

ui32 cartridgeSize;
u32 cartridgeSize;
const char* romFileName;
byte* rom;
bool isLoaded;
Expand Down
29 changes: 29 additions & 0 deletions Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@

#include "Cpu.h"

#include "Memory.h"

Cpu::Cpu( Memory *memory )
: memory( memory )
{
Reset();
}

void Cpu::Reset()
{
/* Load the reset vector into the PC register */
PC.low = memory->Read( 0xFFFC );
PC.hi = memory->Read( 0xFFFD );

/* Power up state of registers */
stackPointer = 0xFD;
pRegister = 0x34;
accumulator = 0x00;
xRegisterIndex = 0x00;
yRegisterIndex = 0x00;
}

word Cpu::Update()
{
const byte opcode = GetNextOpcode();
const byte instruction = ( ( ( opcode & 0b1110'0000 ) >> 3 ) | ( opcode & 0b0000'0011 ) );
const byte addressingMode = ( opcode & 0b0001'1100 ) >> 2;
return 0;
}

Expand All @@ -28,3 +53,7 @@ word Cpu::GetAbsoluteStackAddress() const
return 0x0100 + stackPointer;
}

byte Cpu::GetNextOpcode() const
{
return memory->Read( PC.value );
}
11 changes: 10 additions & 1 deletion Cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "Types.h"


class Memory;

class Cpu
{
public:
Expand All @@ -20,7 +23,9 @@ class Cpu
};


Cpu() = default;
Cpu( Memory *memory );

void Reset();

word Update();

Expand All @@ -38,8 +43,12 @@ class Cpu
byte xRegisterIndex;
byte yRegisterIndex;

/* Systems */
Memory *memory;

/* Stack operations */
word GetAbsoluteStackAddress() const;

/* Opcode handling */
byte GetNextOpcode() const;
};
48 changes: 48 additions & 0 deletions Memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "Memory.h"

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


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

MapCartridge( cartridge );
}

Memory::~Memory()
{
delete map;
}

byte Memory::Read( word address ) const
{
return map[ address ];
}

void Memory::Write( word address, byte data )
{
map[ address ] = data;
}

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

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

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 );

}
38 changes: 38 additions & 0 deletions Memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "Types.h"


/*
* 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 )
*/


class Cartridge;

class Memory
{
public:

Memory( Cartridge *cartridge );
~Memory();

byte Read( word address ) const;
void Write( word address, byte data );

private:

byte *map;

void MapCartridge( Cartridge *cartridge );
};
15 changes: 13 additions & 2 deletions Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
using byte = unsigned char;
using word = unsigned short;

using ui64 = unsigned long long;
using u64 = unsigned long long;
using i64 = long long;

using ui32 = unsigned int;
using u32 = unsigned int;
using i32 = int;

union Register
Expand All @@ -18,3 +18,14 @@ union Register
};
word value;
};


namespace
{

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

}
5 changes: 5 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <iostream>

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

int main(int argc, char** argv)
{
Expand All @@ -13,6 +15,9 @@ int main(int argc, char** argv)
Cartridge cartridge( argv[1] );
if ( cartridge.IsLoaded() )
{
Memory memory( &cartridge );
Cpu cpu( &memory );
cpu.Update();
cartridge.PrintDetails();
return 0;
}
Expand Down

0 comments on commit 440a798

Please sign in to comment.