-
Notifications
You must be signed in to change notification settings - Fork 1
/
Memory.cpp
74 lines (59 loc) · 1.35 KB
/
Memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include "Memory.h"
#include <assert.h>
#include "Cartridge.h"
#include "Video.h"
Memory::Memory( const Cartridge &cartridge, Video &video )
: cartridge( cartridge )
, video( video )
{
map = new byte[ 64_KB ];
}
Memory::~Memory()
{
delete[] map;
}
void Memory::Reset()
{
memset( map, 0x00, 64_KB );
MapCartridge( cartridge );
}
byte Memory::Read( word address ) const
{
if ( address >= 0x2000 && address <= 0x2007 )
{
/* PPU memory */
return video.Read( address );
}
else
{
return map[ address ];
}
}
void Memory::Write( word address, byte 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( const Cartridge &cartridge )
{
/* 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 );
}