-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiNES.cpp
More file actions
57 lines (45 loc) · 1.58 KB
/
iNES.cpp
File metadata and controls
57 lines (45 loc) · 1.58 KB
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
#include "iNES.h"
#include <filesystem>
#include <fstream>
iNES::iNES(const char *path) {
if (!std::filesystem::exists(path)) {
throw std::runtime_error("File does not exist");
}
auto fileSize = std::filesystem::file_size(path);
if (fileSize < MinRomSize) {
throw std::runtime_error("File is too small");
}
if (fileSize > MaxRomSize) {
throw std::runtime_error("File is too large");
}
// Read the header
std::ifstream file(path, std::ios::binary);
file.read(reinterpret_cast<char *>(&header), sizeof(header));
VERIFY(file, "Failed to read header");
if (header.hasTrainer()) {
file.read(reinterpret_cast<char *>(&trainer), sizeof(trainer));
VERIFY(file, "Failed to read trainer");
}
if (header.prgRomChunks == 0) {
throw std::runtime_error("PRG ROM size is zero");
}
// Read the PRG ROM
auto prgRomSize = header.prgRomChunks * PrgRomChunkSize;
prgRom.resize(prgRomSize);
file.read(reinterpret_cast<char*>(prgRom.data()), prgRomSize);
VERIFY(file, "Failed to read PRG ROM");
// Read the CHR ROM
if (header.chrRomChunks > 0) {
auto chrRomSize = header.chrRomChunks * ChrRomChunkSize;
chrRom.resize(chrRomSize);
file.read(reinterpret_cast<char*>(chrRom.data()), chrRomSize);
VERIFY(file, "Failed to read CHR ROM");
}
// TODO: Read the PlayChoice-10 data
if (header.HasPlayChoice10Data()) {
throw std::runtime_error("PlayChoice-10 data is not supported");
}
}
iNES::~iNES() {
SPDLOG_TRACE("iNES destructor");
}