-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cartridge.cpp
136 lines (110 loc) · 2.11 KB
/
Cartridge.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Cartridge.h"
Cartridge::Cartridge(const std::string& sFileName)
{
struct sHeader
{
char name[4];
uint8_t prg_rom_chunks;
uint8_t chr_rom_chunks;
uint8_t mapper1;
uint8_t mapper2;
uint8_t prg_ram_size;
uint8_t tv_system1;
uint8_t tv_system2;
char unused[5];
} header;
std::ifstream file;
file.open(sFileName, std::ifstream::binary);
if (file.is_open())
{
file.read((char*)& header, sizeof(header));
if (header.mapper1 & 0x04)
{
file.seekg(512, std::ios::cur);
}
nMapperID = ((header.mapper2 >> 4) << 4) | (header.mapper1 >> 4);
mirror = (header.mapper1 & 0x01) ? VERTICAL : HORIZONTAL;
uint8_t nFileType = 1;
if (nFileType == 0)
{
}
if (nFileType == 1)
{
nPRGBanks = header.prg_rom_chunks;
vPRGmem.resize(nPRGBanks * 16384);
file.read((char*)vPRGmem.data(), vPRGmem.size());
nCHRBanks = header.chr_rom_chunks;
vCHRmem.resize(nCHRBanks * 8192);
file.read((char*)vCHRmem.data(), vCHRmem.size());
}
if (nFileType == 2)
{
}
switch (nMapperID)
{
case 0:
pMapper = std::make_shared<Mapper_000>(nPRGBanks, nCHRBanks);
break;
}
file.close();
}
}
Cartridge::~Cartridge()
{
}
bool Cartridge::ImageValid()
{
return bImageValid;
}
bool Cartridge::cpuRead(uint16_t address, uint8_t& data)
{
uint32_t mapper_address = 0x00;
if (pMapper->cpuMapRead(address, mapper_address))
{
data = vPRGmem[mapper_address];
return true;
}
else
{
return false;
}
}
bool Cartridge::cpuWrite(uint16_t address, uint8_t data)
{
uint32_t mapper_address = 0;
if (pMapper->cpuMapWrite(address, mapper_address))
{
vPRGmem[mapper_address] = data;
return true;
}
else
{
return false;
}
}
bool Cartridge::ppuRead(uint16_t address, uint8_t& data)
{
uint32_t mapper_address = 0;
if (pMapper->ppuMapRead(address, mapper_address))
{
data = vCHRmem[mapper_address];
return true;
}
else
{
return false;
}
}
bool Cartridge::ppuWrite(uint16_t address, uint8_t data)
{
uint32_t mapper_address = 0;
if (pMapper->ppuMapRead(address, mapper_address))
{
vCHRmem[mapper_address] = data;
return true;
}
else
{
return false;
}
}