Implement class for cartridge and rudimentary ROM inspector

This commit is contained in:
madmaurice 2023-08-31 23:53:20 +02:00
parent 0c52f9d2e0
commit 6e7d06299a
6 changed files with 86 additions and 1 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
/vgbc /vgbc
/libemu.a /libemu.a
/vgbc.test /vgbc.test
/vgbc.inspect

View file

@ -5,6 +5,7 @@ modules := memory/device \
memory/register \ memory/register \
memory/bank \ memory/bank \
cpu/cpu \ cpu/cpu \
cpu/decoder cpu/decoder \
cartridge/cartridge
CXX_FLAGS := -I $(CURDIR) CXX_FLAGS := -I $(CURDIR)

View file

@ -34,3 +34,7 @@ vgbc: main.o libemu.a
vgbc.test: $(test-objs) libemu.a vgbc.test: $(test-objs) libemu.a
@echo " ==> link $@"; \ @echo " ==> link $@"; \
g++ $(CXX_FLAGS) -o $@ $^ g++ $(CXX_FLAGS) -o $@ $^
vgbc.inspect: cartridge/inspector.o libemu.a
@echo " ==> link $@"; \
g++ $(CXX_FLAGS) -o $@ $^

22
cartridge/cartridge.cpp Normal file
View file

@ -0,0 +1,22 @@
#include <cartridge/cartridge.h>
#include <cstring>
Cartridge::Cartridge(std::ifstream& file)
{
_size = file.seekg(0, std::ios_base::end).tellg();
file.seekg(0, std::ios_base::beg);
_data = new u8[_size];
file.read(reinterpret_cast<char*>(_data), _size);
std::strncpy(_title, (char*)&_data[TITLE], 16);
_title[16] = '\0';
}
u8 Cartridge::operator[](size_t addr)
{
if(addr >= _size)
return 0xFFu;
return _data[addr];
}

37
cartridge/cartridge.h Normal file
View file

@ -0,0 +1,37 @@
#pragma once
#include <misc/types.h>
#include <fstream>
enum CartridgeType : u8 {
ROM_ONLY = 0x00,
MBC1 = 0x01,
MBC1_RAM = 0x02,
MBC1_RAM_BATTERY = 0x03,
};
class Cartridge {
private:
u8* _data;
size_t _size;
char _title[17];
enum Offsets {
TITLE = 0x0134,
MANU_CODE = 0x013F,
};
public:
Cartridge(std::ifstream& file);
u8 operator[](size_t addr);
inline
const char* title()
{ return _title; }
inline
size_t size()
{ return _size; }
};

20
cartridge/inspector.cpp Normal file
View file

@ -0,0 +1,20 @@
#include <iostream>
#include <fstream>
#include <cartridge/cartridge.h>
int main(int argc, char** argv)
{
if(argc < 2) return 1;
std::ifstream file;
file.open(argv[1]);
Cartridge cart(file);
std::cout << std::hex << "Path: " << argv[1] << " (size " << cart.size() << ")" << std::endl;
std::cout << "title: " << cart.title() << std::endl;
std::cout << std::hex << "cgb flag: " << (int)cart[0x143] << std::endl;
std::cout << std::hex << "type: " << (int)cart[0x147] << std::endl;
return 0;
}