Starting to parse the Rom's header

This commit is contained in:
AnonymusRaccoon
2020-01-30 17:36:14 +01:00
parent a366d29638
commit 0bb1965073
4 changed files with 61 additions and 11 deletions
+1
View File
@@ -75,4 +75,5 @@ add_executable(ComSquare
sources/Memory/IRectangleMemory.hpp
sources/DSP/DSP.cpp
sources/DSP/DSP.hpp
sources/Exceptions/InvalidAcction.hpp
)
+26 -4
View File
@@ -8,6 +8,7 @@
#include "Cartridge.hpp"
#include "../Exceptions/InvalidAddress.hpp"
#include "../Exceptions/InvalidRom.hpp"
#include "../Exceptions/InvalidAcction.hpp"
namespace ComSquare::Cartridge
{
@@ -20,9 +21,10 @@ namespace ComSquare::Cartridge
if (!rom)
throw InvalidRomException("Could not open the rom file at " + romPath + ". " + strerror(errno));
this->_size = size;
this->_data = new unsigned char[size];
this->_data = new uint8_t[size];
std::memset(this->_data, 0, size);
fread(this->_data, 1, size, rom);
this->_loadHeader();
} catch (InvalidRomException &ex) {
std::cerr << "Invalid Rom Error: " << ex.what() << std::endl;
}
@@ -52,8 +54,28 @@ namespace ComSquare::Cartridge
void Cartridge::write_internal(uint24_t addr, uint8_t data)
{
if (addr >= this->_size)
throw InvalidAddress("Cartridge write", addr);
this->_data[addr] = data;
(void)addr;
(void)data;
throw InvalidAcction("Witting to the ROM is not allowed.");
}
uint32_t Cartridge::_getHeaderAddress()
{
return 0x81c0;
}
bool Cartridge::_loadHeader()
{
unsigned headerAddress = this->_getHeaderAddress();
char name[22];
std::memcpy(name, &this->_data[headerAddress], 21);
name[21] = '\0';
this->header.gameName = std::string(name);
ADDMAPPINGMODE(this->_data[headerAddress + 21] & 0x10u ? FastRom : SlowRom);
ADDMAPPINGMODE(this->_data[headerAddress + 21] & 0x1u ? HiRom : LoRom);
if (this->_data[headerAddress + 21] & 0x2u || this->_data[headerAddress + 210] & 0x4u)
ADDMAPPINGMODE(ExRom);
return headerAddress & 0x200u;
}
}
+12 -7
View File
@@ -12,14 +12,13 @@
namespace ComSquare::Cartridge
{
#define ADDMAPPINGMODE(flag) (this->header.mappingMode = static_cast<MappingMode>(this->header.mappingMode | (flag)))
enum MappingMode {
LowRom,
HiRom,
ExLowRom,
EwHiRom,
SA1Rom, // unimplemented
FastLoRom,
FastHiRom
LoRom = 1u << 0u,
HiRom = 1u << 1u,
SlowRom = 1u << 2u,
FastRom = 1u << 3u,
ExRom = 1u << 4u,
};
struct Header
@@ -56,6 +55,12 @@ namespace ComSquare::Cartridge
//! @param romPath The path of the rom to get info from.
//! @return The size of the rom.
static size_t getRomSize(const std::string &romPath);
//! @brief Set the public variable header by parsing the header in the ROM.
//! @return True if this cartridge has a SCM header, false otherwise.
bool _loadHeader();
//! @brief Get the address of the header.
//! @return The address of this cartridge header.
uint32_t _getHeaderAddress();
public:
//! @brief Load a rom from it's path.
explicit Cartridge(const std::string &romPath);
+22
View File
@@ -0,0 +1,22 @@
//
// Created by anonymus-raccoon on 1/30/20.
//
#ifndef COMSQUARE_INVALIDACCTION_HPP
#define COMSQUARE_INVALIDACCTION_HPP
#include <exception>
#include <string>
namespace ComSquare
{
//! @brief Exception thrown when someone tries to load an invalid rom.
class InvalidAcction : std::exception {
private:
std::string _msg;
public:
explicit InvalidAcction(const std::string &msg) : _msg(msg) {}
const char *what() const noexcept override { return this->_msg.c_str(); }
};
}
#endif //COMSQUARE_INVALIDACCTION_HPP