added documentation to cartridge

This commit is contained in:
Lauchmelder 2022-02-28 16:43:35 +01:00
parent 377a9a3741
commit 7c424b3137
No known key found for this signature in database
GPG key ID: C2403C69D78F011D
2 changed files with 39 additions and 25 deletions

View file

@ -8,7 +8,7 @@
#include "mappers/Mapper000.hpp" #include "mappers/Mapper000.hpp"
Cartridge::Cartridge(Bus* bus) : Cartridge::Cartridge(Bus* bus) :
bus(bus) bus(bus), mapper(nullptr)
{ {
} }
@ -17,36 +17,19 @@ Cartridge::~Cartridge()
delete mapper; delete mapper;
} }
Byte Cartridge::ReadCPU(Word addr)
{
return mapper->ReadCPU(addr);
}
Byte Cartridge::ReadPPU(Word addr)
{
return mapper->ReadPPU(addr);
}
void Cartridge::WriteCPU(Word addr, Byte val)
{
mapper->WriteCPU(addr, val);
}
void Cartridge::WritePPU(Word addr, Byte val)
{
mapper->WritePPU(addr, val);
}
void Cartridge::Load(std::string path) void Cartridge::Load(std::string path)
{ {
// Try to load file from disk
std::ifstream file(path, std::ios::binary); std::ifstream file(path, std::ios::binary);
if (!file) if (!file)
throw std::runtime_error("Failed to open file " + path); throw std::runtime_error("Failed to open file " + path);
// Read header into (temporary) structure
LOG_CORE_INFO("Extracting header"); LOG_CORE_INFO("Extracting header");
Header header; Header header;
file.read((char*)&header, sizeof(Header)); file.read((char*)&header, sizeof(Header));
// Figure out which mapper the cartridge uses and create a mapper object
uint8_t mapperNumber = (header.MapperHi & 0xF0) | (header.MapperLo >> 4); uint8_t mapperNumber = (header.MapperHi & 0xF0) | (header.MapperLo >> 4);
LOG_CORE_INFO("Cartridge requires Mapper {0:d}", mapperNumber); LOG_CORE_INFO("Cartridge requires Mapper {0:d}", mapperNumber);
switch (mapperNumber) switch (mapperNumber)

View file

@ -8,6 +8,9 @@
class Bus; class Bus;
/**
* @brief iNES ROM header.
*/
struct Header struct Header
{ {
Byte Signature[4]; Byte Signature[4];
@ -21,19 +24,47 @@ struct Header
Byte Padding[5]; Byte Padding[5];
}; };
/**
* @brief Represents a cartridge and handles CPU/PPU read/writes.
*/
class Cartridge class Cartridge
{ {
public: public:
/**
* @brief Add a cartridge to the Bus.
*/
Cartridge(Bus* bus); Cartridge(Bus* bus);
~Cartridge(); ~Cartridge();
Byte ReadCPU(Word addr); /**
Byte ReadPPU(Word addr); * @brief Read from the CPU.
void WriteCPU(Word addr, Byte val); */
void WritePPU(Word addr, Byte val); inline Byte ReadCPU(Word addr) { return mapper->ReadCPU(addr); }
/**
* @brief Read from the PPU.
*/
inline Byte ReadPPU(Word addr) { return mapper->ReadPPU(addr); }
/**
* @brief Wrote from the CPU.
*/
inline void WriteCPU(Word addr, Byte val) { mapper->WriteCPU(addr, val); }
/**
* @brief Write from the PPU.
*/
inline void WritePPU(Word addr, Byte val) { mapper->WritePPU(addr, val); }
/**
* @brief Load an iNES file from disk.
*/
void Load(std::string path); void Load(std::string path);
/**
* @brief Returns the Mapper used by the cartridge.
*/
inline Mapper* GetMapper() { return mapper; } inline Mapper* GetMapper() { return mapper; }
private: private: