Mesen-X/Core/ROMLoader.h

104 lines
1.7 KiB
C
Raw Normal View History

2014-06-14 11:27:55 -04:00
#pragma once
#include "stdafx.h"
enum class MirroringType
{
Horizontal,
Vertical,
ScreenAOnly,
ScreenBOnly,
FourScreens,
};
2014-06-14 11:27:55 -04:00
struct NESHeader
{
char NES[4];
uint8_t ROMCount;
uint8_t VROMCount;
uint8_t Flags1;
uint8_t Flags2;
uint8_t RAMCount;
uint8_t CartType;
uint8_t Reserved[6];
uint8_t GetMapperID()
{
return (Flags2 & 0xF0) | (Flags1 >> 4);
}
MirroringType GetMirroringType()
{
if(Flags1 & 0x08) {
return MirroringType::FourScreens;
} else {
return Flags1 & 0x01 ? MirroringType::Vertical : MirroringType::Horizontal;
}
}
2014-06-14 11:27:55 -04:00
};
class ROMLoader
{
private:
NESHeader _header;
uint8_t* _prgRAM;
uint8_t* _chrRAM;
2014-06-14 11:27:55 -04:00
public:
ROMLoader(wstring filename)
2014-06-14 11:27:55 -04:00
{
ifstream romFile(filename, ios::in | ios::binary);
if(!romFile) {
2014-06-14 18:20:56 -04:00
throw std::exception("File could not be read");
2014-06-14 11:27:55 -04:00
}
romFile.read((char*)&_header, sizeof(NESHeader));
uint8_t* prgBuffer = new uint8_t[0x4000 * _header.ROMCount];
2014-06-14 11:27:55 -04:00
for(int i = 0; i < _header.ROMCount; i++) {
romFile.read((char*)prgBuffer+i*0x4000, 0x4000);
2014-06-14 11:27:55 -04:00
}
_prgRAM = prgBuffer;
2014-06-14 11:27:55 -04:00
uint8_t* chrBuffer = new uint8_t[0x2000 * _header.VROMCount];
2014-06-14 11:27:55 -04:00
for(int i = 0; i < _header.VROMCount; i++) {
romFile.read((char*)chrBuffer+i*0x2000, 0x2000);
2014-06-14 11:27:55 -04:00
}
_chrRAM = chrBuffer;
2014-06-14 11:27:55 -04:00
romFile.close();
}
uint8_t* GetPRGRam()
{
return _prgRAM;
}
uint8_t* GetCHRRam()
{
return _chrRAM;
}
uint32_t GetPRGSize()
2014-06-14 11:27:55 -04:00
{
return _header.ROMCount * 0x4000;
2014-06-14 11:27:55 -04:00
}
uint32_t GetCHRSize()
2014-06-14 11:27:55 -04:00
{
return _header.VROMCount * 0x2000;
2014-06-14 11:27:55 -04:00
}
MirroringType GetMirroringType()
2014-06-14 11:27:55 -04:00
{
return _header.GetMirroringType();
}
uint8_t GetMapperID()
{
return _header.GetMapperID();
2014-06-14 11:27:55 -04:00
}
};