#pragma once

#include <common.h>

typedef struct {
    u8 entry[4];
    u8 logo[0x30];

    char title[16];
    u16 new_lic_code;
    u8 sgb_flag;
    u8 type;
    u8 rom_size;
    u8 ram_size;
    u8 dest_code;
    u8 lic_code;
    u8 version;
    u8 checksum;
    u16 global_checksum;
} rom_header;

typedef struct {
    char filename[1024];
    u32 rom_size;
    u8 *rom_data;
    rom_header *header;
    bool is_multicart;

    //mbc1 data
    bool ram_enabled;
    bool ram_banking;

    u8 *rom_bank_x;
    u8 *rom_bank_x2;
    u8 banking_mode;

    u8 rom_bank_value;
    u8 rom_bank_value_2;
    u8 ram_bank_value;

    u8 *ram_bank;
    u8 *ram_banks[16];

    //battery
    bool battery;
    bool need_save;
} cart_context;

typedef struct {
    bool ram_enabled;
    bool ram_banking;
    u8 banking_mode;
    u8 rom_bank_value;
    u8 rom_bank_value_2;
    u8 ram_bank_value;
    u8 ram_banks[16][0x2000];
    bool need_save;
} cart_state;

void cart_load_state(const cart_state*);
void cart_save_state(cart_state*);
bool cart_init();

bool cart_load(char *cart);

rom_header *get_rom_header();

cart_context *cart_get_context();

u8 cart_read(u16 address);
void cart_write(u16 address, u8 value);

bool cart_need_save();
bool cart_battery_load();
bool cart_battery_save();

u8 cart_get_rom_bank();
