GameBoy Emulator 1
Game Boy emulator core and tooling
Loading...
Searching...
No Matches
readROM.hpp
1#pragma once
2
3#include "cartridge.hpp"
4#include <iostream>
5#include <cstring>
6#include <fstream>
7#include <vector>
8#include <stdexcept>
9#include <iterator>
10
11inline std::vector<u8> load_rom(const char* _filepath){
12 std::ifstream file(_filepath, std::ios::binary);
13
14 if (!file){
15 throw std::runtime_error("Error opening file");
16 }
17
18 std::vector<u8> rom(
19 (std::istreambuf_iterator(file)),
20 std::istreambuf_iterator<char>()
21 );
22 std::cout << "ROM Size: " << (rom.size()/1024) << "K" << std::endl;
23
24 if(rom.empty()){
25 throw std::runtime_error("ROM file is empty");
26 }
27
28 if(rom.size() < 0x150){
29 throw std::runtime_error("ROM too small");
30 }
31
32 rom_header _header{};
33 std::memcpy(&_header, &rom[0x100], sizeof(rom_header));
34
35 /* avoid Null-terminator error */
36 char title[17]{};
37 std::memcpy(title, _header.title, 16);
38
39 std::cout << "Title: " << " " << title << std::endl;
40 std::cout << "Type: 0x" << std::hex << static_cast<int>(_header.type) << "\n";
41 std::cout << "ROM: 0x" << std::hex << static_cast<int>(_header.rom_size) << "\n";
42 std::cout << "RAM: 0x" << std::hex << static_cast<int>(_header.ram_size) << "\n";
43
44 u8 sum = 0;
45
46 for (int i = 0x134; i <= 0x14C; i++) {
47 sum = sum - rom[i] - 1;
48 }
49
50 if (sum == _header.checksum)
51 std::cout << "Header OK\n" << std::endl;
52 else
53 std::cout << "Bad header\n" << std::endl;
54
55 return rom; // success
56}