diff --git a/.gitignore b/.gitignore index b6ea3c3..b7a2d4f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /vgbc /libemu.a /vgbc.test +/vgbc.inspect diff --git a/Makeconf b/Makeconf index 086f66a..d17ba72 100644 --- a/Makeconf +++ b/Makeconf @@ -5,6 +5,7 @@ modules := memory/device \ memory/register \ memory/bank \ cpu/cpu \ - cpu/decoder + cpu/decoder \ + cartridge/cartridge CXX_FLAGS := -I $(CURDIR) diff --git a/Makefile b/Makefile index 97d6d50..9f872e7 100644 --- a/Makefile +++ b/Makefile @@ -34,3 +34,7 @@ vgbc: main.o libemu.a vgbc.test: $(test-objs) libemu.a @echo " ==> link $@"; \ g++ $(CXX_FLAGS) -o $@ $^ + +vgbc.inspect: cartridge/inspector.o libemu.a + @echo " ==> link $@"; \ + g++ $(CXX_FLAGS) -o $@ $^ diff --git a/cartridge/cartridge.cpp b/cartridge/cartridge.cpp new file mode 100644 index 0000000..e126daf --- /dev/null +++ b/cartridge/cartridge.cpp @@ -0,0 +1,22 @@ +#include +#include + +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(_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]; +} diff --git a/cartridge/cartridge.h b/cartridge/cartridge.h new file mode 100644 index 0000000..2364aaa --- /dev/null +++ b/cartridge/cartridge.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include + +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; } +}; diff --git a/cartridge/inspector.cpp b/cartridge/inspector.cpp new file mode 100644 index 0000000..2bf6daa --- /dev/null +++ b/cartridge/inspector.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +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; +}