2016-06-18 17:29:11 +00:00
|
|
|
#ifndef GB_h
|
|
|
|
#define GB_h
|
2016-03-30 20:07:55 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2017-01-13 19:27:37 +00:00
|
|
|
#include "printer.h"
|
2016-03-30 20:07:55 +00:00
|
|
|
#include "apu.h"
|
2016-06-11 11:52:09 +00:00
|
|
|
#include "save_struct.h"
|
2016-07-13 20:00:50 +00:00
|
|
|
#include "symbol_hash.h"
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
|
2016-09-20 17:04:38 +00:00
|
|
|
#define GB_STRUCT_VERSION 10
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
GB_REGISTER_AF,
|
|
|
|
GB_REGISTER_BC,
|
|
|
|
GB_REGISTER_DE,
|
|
|
|
GB_REGISTER_HL,
|
|
|
|
GB_REGISTER_SP,
|
|
|
|
GB_REGISTERS_16_BIT /* Count */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Todo: Actually use these! */
|
|
|
|
enum {
|
|
|
|
GB_CARRY_FLAG = 16,
|
|
|
|
GB_HALF_CARRY_FLAG = 32,
|
|
|
|
GB_SUBSTRACT_FLAG = 64,
|
|
|
|
GB_ZERO_FLAG = 128,
|
|
|
|
};
|
|
|
|
|
2016-07-20 22:03:13 +00:00
|
|
|
#define GB_MAX_IR_QUEUE 256
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
enum {
|
|
|
|
/* Joypad and Serial */
|
|
|
|
GB_IO_JOYP = 0x00, // Joypad (R/W)
|
|
|
|
GB_IO_SB = 0x01, // Serial transfer data (R/W)
|
|
|
|
GB_IO_SC = 0x02, // Serial Transfer Control (R/W)
|
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
/* Timers */
|
|
|
|
GB_IO_DIV = 0x04, // Divider Register (R/W)
|
|
|
|
GB_IO_TIMA = 0x05, // Timer counter (R/W)
|
|
|
|
GB_IO_TMA = 0x06, // Timer Modulo (R/W)
|
|
|
|
GB_IO_TAC = 0x07, // Timer Control (R/W)
|
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
GB_IO_IF = 0x0f, // Interrupt Flag (R/W)
|
|
|
|
|
|
|
|
/* Sound */
|
|
|
|
GB_IO_NR10 = 0x10, // Channel 1 Sweep register (R/W)
|
|
|
|
GB_IO_NR11 = 0x11, // Channel 1 Sound length/Wave pattern duty (R/W)
|
|
|
|
GB_IO_NR12 = 0x12, // Channel 1 Volume Envelope (R/W)
|
|
|
|
GB_IO_NR13 = 0x13, // Channel 1 Frequency lo (Write Only)
|
|
|
|
GB_IO_NR14 = 0x14, // Channel 1 Frequency hi (R/W)
|
|
|
|
GB_IO_NR21 = 0x16, // Channel 2 Sound Length/Wave Pattern Duty (R/W)
|
|
|
|
GB_IO_NR22 = 0x17, // Channel 2 Volume Envelope (R/W)
|
|
|
|
GB_IO_NR23 = 0x18, // Channel 2 Frequency lo data (W)
|
|
|
|
GB_IO_NR24 = 0x19, // Channel 2 Frequency hi data (R/W)
|
|
|
|
GB_IO_NR30 = 0x1a, // Channel 3 Sound on/off (R/W)
|
|
|
|
GB_IO_NR31 = 0x1b, // Channel 3 Sound Length
|
|
|
|
GB_IO_NR32 = 0x1c, // Channel 3 Select output level (R/W)
|
|
|
|
GB_IO_NR33 = 0x1d, // Channel 3 Frequency's lower data (W)
|
|
|
|
GB_IO_NR34 = 0x1e, // Channel 3 Frequency's higher data (R/W)
|
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
GB_IO_NR41 = 0x20, // Channel 4 Sound Length (R/W)
|
|
|
|
GB_IO_NR42 = 0x21, // Channel 4 Volume Envelope (R/W)
|
|
|
|
GB_IO_NR43 = 0x22, // Channel 4 Polynomial Counter (R/W)
|
|
|
|
GB_IO_NR44 = 0x23, // Channel 4 Counter/consecutive, Inital (R/W)
|
|
|
|
GB_IO_NR50 = 0x24, // Channel control / ON-OFF / Volume (R/W)
|
|
|
|
GB_IO_NR51 = 0x25, // Selection of Sound output terminal (R/W)
|
|
|
|
GB_IO_NR52 = 0x26, // Sound on/off
|
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
GB_IO_WAV_START = 0x30, // Wave pattern start
|
|
|
|
GB_IO_WAV_END = 0x3f, // Wave pattern end
|
|
|
|
|
|
|
|
/* Graphics */
|
|
|
|
GB_IO_LCDC = 0x40, // LCD Control (R/W)
|
|
|
|
GB_IO_STAT = 0x41, // LCDC Status (R/W)
|
|
|
|
GB_IO_SCY = 0x42, // Scroll Y (R/W)
|
|
|
|
GB_IO_SCX = 0x43, // Scroll X (R/W)
|
|
|
|
GB_IO_LY = 0x44, // LCDC Y-Coordinate (R)
|
|
|
|
GB_IO_LYC = 0x45, // LY Compare (R/W)
|
|
|
|
GB_IO_DMA = 0x46, // DMA Transfer and Start Address (W)
|
|
|
|
GB_IO_BGP = 0x47, // BG Palette Data (R/W) - Non CGB Mode Only
|
|
|
|
GB_IO_OBP0 = 0x48, // Object Palette 0 Data (R/W) - Non CGB Mode Only
|
|
|
|
GB_IO_OBP1 = 0x49, // Object Palette 1 Data (R/W) - Non CGB Mode Only
|
|
|
|
GB_IO_WY = 0x4a, // Window Y Position (R/W)
|
|
|
|
GB_IO_WX = 0x4b, // Window X Position minus 7 (R/W)
|
2016-04-05 20:21:51 +00:00
|
|
|
// Has some undocumented compatibility flags written at boot.
|
|
|
|
// Unfortunately it is not readable or writable after boot has finished, so research of this
|
|
|
|
// register is quite limited. The value written to this register, however, can be controlled
|
|
|
|
// in some cases.
|
|
|
|
GB_IO_DMG_EMULATION = 0x4c,
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
/* General CGB features */
|
|
|
|
GB_IO_KEY1 = 0x4d, // CGB Mode Only - Prepare Speed Switch
|
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
GB_IO_VBK = 0x4f, // CGB Mode Only - VRAM Bank
|
|
|
|
GB_IO_BIOS = 0x50, // Write to disable the BIOS mapping
|
|
|
|
|
|
|
|
/* CGB DMA */
|
|
|
|
GB_IO_HDMA1 = 0x51, // CGB Mode Only - New DMA Source, High
|
|
|
|
GB_IO_HDMA2 = 0x52, // CGB Mode Only - New DMA Source, Low
|
|
|
|
GB_IO_HDMA3 = 0x53, // CGB Mode Only - New DMA Destination, High
|
|
|
|
GB_IO_HDMA4 = 0x54, // CGB Mode Only - New DMA Destination, Low
|
|
|
|
GB_IO_HDMA5 = 0x55, // CGB Mode Only - New DMA Length/Mode/Start
|
|
|
|
|
|
|
|
/* IR */
|
|
|
|
GB_IO_RP = 0x56, // CGB Mode Only - Infrared Communications Port
|
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
/* CGB Paletts */
|
|
|
|
GB_IO_BGPI = 0x68, // CGB Mode Only - Background Palette Index
|
|
|
|
GB_IO_BGPD = 0x69, // CGB Mode Only - Background Palette Data
|
|
|
|
GB_IO_OBPI = 0x6a, // CGB Mode Only - Sprite Palette Index
|
|
|
|
GB_IO_OBPD = 0x6b, // CGB Mode Only - Sprite Palette Data
|
|
|
|
|
2016-04-05 20:21:51 +00:00
|
|
|
// 1 is written for DMG ROMs on a CGB. Does not appear to have an effect.
|
|
|
|
GB_IO_DMG_EMULATION_INDICATION = 0x6c, // (FEh) Bit 0 (Read/Write)
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
/* Missing */
|
|
|
|
|
|
|
|
GB_IO_SVBK = 0x70, // CGB Mode Only - WRAM Bank
|
|
|
|
GB_IO_UNKNOWN2 = 0x72, // (00h) - Bit 0-7 (Read/Write)
|
|
|
|
GB_IO_UNKNOWN3 = 0x73, // (00h) - Bit 0-7 (Read/Write)
|
|
|
|
GB_IO_UNKNOWN4 = 0x74, // (00h) - Bit 0-7 (Read/Write) - CGB Mode Only
|
|
|
|
GB_IO_UNKNOWN5 = 0x75, // (8Fh) - Bit 4-6 (Read/Write)
|
|
|
|
GB_IO_PCM_12 = 0x76, // Channels 1 and 2 amplitudes
|
|
|
|
GB_IO_PCM_34 = 0x77, // Channels 3 and 4 amplitudes
|
|
|
|
GB_IO_UNKNOWN8 = 0x7F, // Unknown, write only
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LCDC_PERIOD 70224
|
|
|
|
#define CPU_FREQUENCY 0x400000
|
|
|
|
#define DIV_CYCLES (0x100)
|
2016-10-21 21:49:32 +00:00
|
|
|
#define INTERNAL_DIV_CYCLES (0x40000)
|
2016-03-30 20:07:55 +00:00
|
|
|
#define FRAME_LENGTH 16742706 // in nanoseconds
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
GB_LOG_BOLD = 1,
|
|
|
|
GB_LOG_DASHED_UNDERLINE = 2,
|
|
|
|
GB_LOG_UNDERLINE = 4,
|
|
|
|
GB_LOG_UNDERLINE_MASK = GB_LOG_DASHED_UNDERLINE | GB_LOG_UNDERLINE
|
2016-06-18 17:29:11 +00:00
|
|
|
} GB_log_attributes;
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
struct GB_gameboy_s;
|
|
|
|
typedef struct GB_gameboy_s GB_gameboy_t;
|
|
|
|
typedef void (*GB_vblank_callback_t)(GB_gameboy_t *gb);
|
2016-06-18 17:29:11 +00:00
|
|
|
typedef void (*GB_log_callback_t)(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes);
|
2016-03-30 20:07:55 +00:00
|
|
|
typedef char *(*GB_input_callback_t)(GB_gameboy_t *gb);
|
2016-06-18 17:29:11 +00:00
|
|
|
typedef uint32_t (*GB_rgb_encode_callback_t)(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b);
|
2016-07-20 22:03:13 +00:00
|
|
|
typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool on, long cycles_since_last_update);
|
2016-10-02 14:14:58 +00:00
|
|
|
typedef uint8_t (*GB_camera_get_pixel_callback_t)(GB_gameboy_t *gb, uint8_t x, uint8_t y);
|
|
|
|
typedef void (*GB_camera_update_request_callback_t)(GB_gameboy_t *gb);
|
2016-10-22 12:37:03 +00:00
|
|
|
typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, bool rumble_on);
|
2016-11-11 23:58:53 +00:00
|
|
|
typedef void (*GB_serial_transfer_start_callback_t)(GB_gameboy_t *gb, uint8_t byte_to_send);
|
|
|
|
typedef uint8_t (*GB_serial_transfer_end_callback_t)(GB_gameboy_t *gb);
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
enum {
|
2016-07-09 11:45:25 +00:00
|
|
|
GB_NO_MBC,
|
|
|
|
GB_MBC1,
|
|
|
|
GB_MBC2,
|
|
|
|
GB_MBC3,
|
|
|
|
GB_MBC5,
|
2017-02-16 19:07:35 +00:00
|
|
|
GB_HUC1, /* Todo: HUC1 features are not emulated. Should be unified with the CGB IR sensor API. */
|
2016-10-17 15:51:43 +00:00
|
|
|
GB_HUC3,
|
2016-03-30 20:07:55 +00:00
|
|
|
} mbc_type;
|
2016-10-01 23:15:03 +00:00
|
|
|
enum {
|
|
|
|
GB_STANDARD_MBC,
|
2016-10-17 15:51:43 +00:00
|
|
|
GB_CAMERA,
|
2016-10-01 23:15:03 +00:00
|
|
|
} mbc_subtype;
|
2016-03-30 20:07:55 +00:00
|
|
|
bool has_ram;
|
|
|
|
bool has_battery;
|
|
|
|
bool has_rtc;
|
|
|
|
bool has_rumble;
|
|
|
|
} GB_cartridge_t;
|
|
|
|
|
2016-07-20 22:03:13 +00:00
|
|
|
typedef struct {
|
|
|
|
bool state;
|
|
|
|
long delay;
|
|
|
|
} GB_ir_queue_item_t;
|
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
struct GB_breakpoint_s;
|
|
|
|
struct GB_watchpoint_s;
|
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
/* When state saving, each section is dumped independently of other sections.
|
|
|
|
This allows adding data to the end of the section without worrying about future compatibility.
|
2016-06-18 17:29:11 +00:00
|
|
|
Some other changes might be "safe" as well.
|
|
|
|
This struct is not packed, but dumped sections exclusively use types that have the same alignment in both 32 and 64
|
|
|
|
bit platforms. */
|
|
|
|
|
|
|
|
/* We make sure bool is 1 for cross-platform save state compatibility. */
|
|
|
|
/* Todo: We might want to typedef our own bool if this prevents SameBoy from working on specific platforms. */
|
|
|
|
_Static_assert(sizeof(bool) == 1, "sizeof(bool) != 1");
|
2016-06-11 11:52:09 +00:00
|
|
|
|
2016-08-06 10:56:29 +00:00
|
|
|
enum {
|
|
|
|
GB_TIMA_RUNNING = 0,
|
|
|
|
GB_TIMA_RELOADING = 1,
|
|
|
|
GB_TIMA_RELOADED = 2
|
|
|
|
};
|
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
typedef struct GB_gameboy_s {
|
|
|
|
GB_SECTION(header,
|
2016-06-18 17:29:11 +00:00
|
|
|
/* The magic makes sure a state file is:
|
|
|
|
- Indeed a SameBoy state file.
|
|
|
|
- Has the same endianess has the current platform. */
|
|
|
|
uint32_t magic;
|
|
|
|
/* The version field makes sure we don't load save state files with a completely different structure.
|
|
|
|
This happens when struct fields are removed/resized in an backward incompatible manner. */
|
|
|
|
uint32_t version;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
GB_SECTION(core_state,
|
|
|
|
/* Registers */
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t pc;
|
2016-10-21 21:49:32 +00:00
|
|
|
union {
|
|
|
|
uint16_t registers[GB_REGISTERS_16_BIT];
|
|
|
|
struct {
|
|
|
|
uint16_t af,
|
|
|
|
bc,
|
|
|
|
de,
|
|
|
|
hl,
|
|
|
|
sp;
|
|
|
|
};
|
|
|
|
struct {
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
uint8_t a, f,
|
|
|
|
b, c,
|
|
|
|
d, e,
|
|
|
|
h, l;
|
|
|
|
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
uint8_t f, a,
|
|
|
|
c, b,
|
|
|
|
e, d,
|
|
|
|
l, h;
|
|
|
|
#else
|
|
|
|
#error Unable to detect endianess
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2016-08-05 13:36:38 +00:00
|
|
|
uint8_t ime;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t interrupt_enable;
|
|
|
|
uint8_t cgb_ram_bank;
|
2016-06-11 11:52:09 +00:00
|
|
|
|
|
|
|
/* CPU and General Hardware Flags*/
|
|
|
|
bool cgb_mode;
|
|
|
|
bool is_cgb;
|
|
|
|
bool cgb_double_speed;
|
|
|
|
bool halted;
|
|
|
|
bool stopped;
|
2016-06-18 17:29:11 +00:00
|
|
|
bool boot_rom_finished;
|
2016-08-05 13:36:38 +00:00
|
|
|
bool ime_toggle; /* ei (and di in CGB) have delayed effects.*/
|
2016-09-21 22:51:09 +00:00
|
|
|
bool halt_bug;
|
2016-07-18 19:05:11 +00:00
|
|
|
|
2017-01-13 19:27:37 +00:00
|
|
|
/* Misc state */
|
2016-07-18 19:05:11 +00:00
|
|
|
bool infrared_input;
|
2017-01-13 19:27:37 +00:00
|
|
|
GB_printer_t printer;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-08-03 20:31:10 +00:00
|
|
|
/* DMA and HDMA */
|
|
|
|
GB_SECTION(dma,
|
2016-06-11 11:52:09 +00:00
|
|
|
bool hdma_on;
|
|
|
|
bool hdma_on_hblank;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t hdma_steps_left;
|
|
|
|
uint16_t hdma_cycles;
|
|
|
|
uint16_t hdma_current_src, hdma_current_dest;
|
2016-08-03 20:31:10 +00:00
|
|
|
|
|
|
|
uint8_t dma_steps_left;
|
|
|
|
uint8_t dma_current_dest;
|
|
|
|
uint16_t dma_current_src;
|
2016-08-06 10:57:38 +00:00
|
|
|
int16_t dma_cycles;
|
2016-08-06 11:24:43 +00:00
|
|
|
bool is_dma_restarting;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* MBC */
|
|
|
|
GB_SECTION(mbc,
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t mbc_rom_bank;
|
|
|
|
uint8_t mbc_ram_bank;
|
|
|
|
uint32_t mbc_ram_size;
|
2016-06-11 11:52:09 +00:00
|
|
|
bool mbc_ram_enable;
|
2016-07-09 14:34:55 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint8_t bank_low:5;
|
|
|
|
uint8_t bank_high:3;
|
|
|
|
uint8_t mode:1;
|
|
|
|
} mbc1;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t rom_bank:4;
|
|
|
|
} mbc2;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t rom_bank:7;
|
|
|
|
uint8_t padding:1;
|
|
|
|
uint8_t ram_bank:4;
|
|
|
|
} mbc3;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t rom_bank_low;
|
|
|
|
uint8_t rom_bank_high:1;
|
|
|
|
uint8_t ram_bank:4;
|
|
|
|
} mbc5;
|
2017-02-16 19:07:35 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t bank_low:6;
|
|
|
|
uint8_t bank_high:3;
|
|
|
|
uint8_t mode:1;
|
|
|
|
} huc1;
|
2016-10-17 15:51:43 +00:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t rom_bank;
|
|
|
|
uint8_t ram_bank;
|
|
|
|
} huc3;
|
2016-07-09 14:34:55 +00:00
|
|
|
};
|
|
|
|
uint16_t mbc_rom0_bank; /* For some MBC1 wirings. */
|
2016-10-02 00:40:11 +00:00
|
|
|
bool camera_registers_mapped;
|
2016-10-02 14:14:58 +00:00
|
|
|
uint8_t camera_registers[0x36];
|
2016-10-22 12:37:03 +00:00
|
|
|
bool rumble_state;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* HRAM and HW Registers */
|
|
|
|
GB_SECTION(hram,
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t hram[0xFFFF - 0xFF80];
|
|
|
|
uint8_t io_registers[0x80];
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* Timing */
|
|
|
|
GB_SECTION(timing,
|
2016-06-18 17:29:11 +00:00
|
|
|
int64_t last_vblank;
|
|
|
|
uint32_t display_cycles;
|
|
|
|
uint32_t div_cycles;
|
2016-08-05 14:22:12 +00:00
|
|
|
GB_PADDING(uint32_t, tima_cycles);
|
2016-08-03 20:31:10 +00:00
|
|
|
GB_PADDING(uint32_t, dma_cycles);
|
2016-09-12 22:21:47 +00:00
|
|
|
GB_aligned_double apu_sample_cycles;
|
2016-08-06 10:56:29 +00:00
|
|
|
uint8_t tima_reload_state; /* After TIMA overflows, it becomes 0 for 4 cycles before actually reloading. */
|
2016-09-05 19:13:28 +00:00
|
|
|
uint16_t serial_cycles;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
/* APU */
|
|
|
|
GB_SECTION(apu,
|
|
|
|
GB_apu_t apu;
|
|
|
|
);
|
|
|
|
|
|
|
|
/* RTC */
|
|
|
|
GB_SECTION(rtc,
|
|
|
|
union {
|
|
|
|
struct {
|
2016-08-21 19:33:57 +00:00
|
|
|
uint8_t seconds;
|
|
|
|
uint8_t minutes;
|
|
|
|
uint8_t hours;
|
|
|
|
uint8_t days;
|
|
|
|
uint8_t high;
|
2016-06-11 11:52:09 +00:00
|
|
|
};
|
2016-08-21 19:33:57 +00:00
|
|
|
uint8_t data[5];
|
|
|
|
} rtc_real, rtc_latched;
|
2016-06-11 11:52:09 +00:00
|
|
|
time_t last_rtc_second;
|
2016-08-21 19:33:57 +00:00
|
|
|
bool rtc_latch;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* Video Display */
|
|
|
|
GB_SECTION(video,
|
2016-06-18 17:29:11 +00:00
|
|
|
uint32_t vram_size; // Different between CGB and DMG
|
|
|
|
uint8_t cgb_vram_bank;
|
|
|
|
uint8_t oam[0xA0];
|
|
|
|
uint8_t background_palletes_data[0x40];
|
|
|
|
uint8_t sprite_palletes_data[0x40];
|
2016-06-11 11:52:09 +00:00
|
|
|
uint32_t background_palletes_rgb[0x20];
|
|
|
|
uint32_t sprite_palletes_rgb[0x20];
|
2016-06-18 17:29:11 +00:00
|
|
|
int16_t previous_lcdc_x;
|
|
|
|
uint8_t padding;
|
2016-06-11 11:52:09 +00:00
|
|
|
bool effective_window_enabled;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t effective_window_y;
|
2016-06-11 11:52:09 +00:00
|
|
|
bool stat_interrupt_line;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t effective_scx;
|
2016-06-11 11:52:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* Unsaved data. This includes all pointers, as well as everything that shouldn't be on a save state */
|
|
|
|
|
|
|
|
/* ROM */
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t *rom;
|
|
|
|
uint32_t rom_size;
|
2016-03-30 20:07:55 +00:00
|
|
|
const GB_cartridge_t *cartridge_type;
|
2016-07-09 16:25:13 +00:00
|
|
|
enum {
|
|
|
|
GB_STANDARD_MBC1_WIRING,
|
|
|
|
GB_MBC1M_WIRING,
|
|
|
|
} mbc1_wiring;
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
/* Various RAMs */
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t *ram;
|
|
|
|
uint8_t *vram;
|
|
|
|
uint8_t *mbc_ram;
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
/* I/O */
|
|
|
|
uint32_t *screen;
|
2016-06-10 12:28:50 +00:00
|
|
|
GB_sample_t *audio_buffer;
|
2016-06-11 11:52:09 +00:00
|
|
|
bool keys[8];
|
|
|
|
|
|
|
|
/* Audio Specific */
|
2016-03-30 20:07:55 +00:00
|
|
|
unsigned int buffer_size;
|
|
|
|
unsigned int sample_rate;
|
|
|
|
unsigned int audio_position;
|
|
|
|
bool audio_stream_started; // detects first copy request to minimize lag
|
2016-06-11 11:52:09 +00:00
|
|
|
volatile bool audio_copy_in_progress;
|
2016-09-20 22:59:43 +00:00
|
|
|
volatile bool apu_lock;
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
/* Callbacks */
|
|
|
|
void *user_data;
|
2016-03-30 20:07:55 +00:00
|
|
|
GB_log_callback_t log_callback;
|
|
|
|
GB_input_callback_t input_callback;
|
2016-07-17 21:39:43 +00:00
|
|
|
GB_input_callback_t async_input_callback;
|
2016-03-30 20:07:55 +00:00
|
|
|
GB_rgb_encode_callback_t rgb_encode_callback;
|
2016-06-11 11:52:09 +00:00
|
|
|
GB_vblank_callback_t vblank_callback;
|
2016-07-18 19:05:11 +00:00
|
|
|
GB_infrared_callback_t infrared_callback;
|
2016-10-02 14:14:58 +00:00
|
|
|
GB_camera_get_pixel_callback_t camera_get_pixel_callback;
|
|
|
|
GB_camera_update_request_callback_t camera_update_request_callback;
|
2016-10-22 12:37:03 +00:00
|
|
|
GB_rumble_callback_t rumble_callback;
|
2016-11-11 23:58:53 +00:00
|
|
|
GB_serial_transfer_start_callback_t serial_transfer_start_callback;
|
|
|
|
GB_serial_transfer_end_callback_t serial_transfer_end_callback;
|
2016-07-20 22:03:13 +00:00
|
|
|
/* IR */
|
|
|
|
long cycles_since_ir_change;
|
|
|
|
long cycles_since_input_ir_change;
|
|
|
|
GB_ir_queue_item_t ir_queue[GB_MAX_IR_QUEUE];
|
|
|
|
size_t ir_queue_length;
|
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
/*** Debugger ***/
|
2016-09-06 21:37:11 +00:00
|
|
|
volatile bool debug_stopped, debug_disable;
|
2016-03-30 20:07:55 +00:00
|
|
|
bool debug_fin_command, debug_next_command;
|
2016-07-06 21:29:25 +00:00
|
|
|
|
|
|
|
/* Breakpoints */
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t n_breakpoints;
|
2016-07-01 15:24:21 +00:00
|
|
|
struct GB_breakpoint_s *breakpoints;
|
2016-07-06 21:29:25 +00:00
|
|
|
|
2016-08-09 19:48:53 +00:00
|
|
|
/* SLD (Todo: merge with backtrace) */
|
2016-04-10 20:22:54 +00:00
|
|
|
bool stack_leak_detection;
|
2016-07-06 21:29:25 +00:00
|
|
|
int debug_call_depth;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t sp_for_call_depth[0x200]; /* Should be much more than enough */
|
|
|
|
uint16_t addr_for_call_depth[0x200];
|
2016-07-06 21:29:25 +00:00
|
|
|
|
2016-08-09 19:48:53 +00:00
|
|
|
/* Backtrace */
|
|
|
|
unsigned int backtrace_size;
|
|
|
|
uint16_t backtrace_sps[0x200];
|
|
|
|
struct {
|
|
|
|
uint16_t bank;
|
|
|
|
uint16_t addr;
|
|
|
|
} backtrace_returns[0x200];
|
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
/* Watchpoints */
|
|
|
|
uint16_t n_watchpoints;
|
|
|
|
struct GB_watchpoint_s *watchpoints;
|
|
|
|
|
2016-07-15 11:31:27 +00:00
|
|
|
/* Symbol tables */
|
2016-07-14 17:46:00 +00:00
|
|
|
GB_symbol_map_t *bank_symbols[0x200];
|
2016-07-15 11:31:27 +00:00
|
|
|
GB_reversed_symbol_map_t reversed_symbol_map;
|
2016-06-11 11:52:09 +00:00
|
|
|
|
2016-09-16 08:58:31 +00:00
|
|
|
/* Ticks command */
|
|
|
|
unsigned long debugger_ticks;
|
|
|
|
|
2016-06-11 11:52:09 +00:00
|
|
|
/* Misc */
|
|
|
|
bool turbo;
|
2016-09-03 00:39:32 +00:00
|
|
|
bool turbo_dont_skip;
|
|
|
|
bool disable_rendering;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint32_t ram_size; // Different between CGB and DMG
|
|
|
|
uint8_t boot_rom[0x900];
|
2016-07-17 21:39:43 +00:00
|
|
|
bool vblank_just_occured; // For slow operations involving syscalls; these should only run once per vblank
|
2016-04-10 20:22:54 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
} GB_gameboy_t;
|
|
|
|
|
|
|
|
#ifndef __printflike
|
|
|
|
/* Missing from Linux headers. */
|
|
|
|
#define __printflike(fmtarg, firstvararg) \
|
|
|
|
__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
|
|
|
|
#endif
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
void GB_init(GB_gameboy_t *gb);
|
|
|
|
void GB_init_cgb(GB_gameboy_t *gb);
|
|
|
|
void GB_free(GB_gameboy_t *gb);
|
|
|
|
int GB_load_boot_rom(GB_gameboy_t *gb, const char *path);
|
|
|
|
int GB_load_rom(GB_gameboy_t *gb, const char *path);
|
|
|
|
int GB_save_battery(GB_gameboy_t *gb, const char *path);
|
|
|
|
void GB_load_battery(GB_gameboy_t *gb, const char *path);
|
|
|
|
int GB_save_state(GB_gameboy_t *gb, const char *path);
|
|
|
|
int GB_load_state(GB_gameboy_t *gb, const char *path);
|
|
|
|
void GB_run(GB_gameboy_t *gb);
|
|
|
|
void GB_set_pixels_output(GB_gameboy_t *gb, uint32_t *output);
|
|
|
|
void GB_set_vblank_callback(GB_gameboy_t *gb, GB_vblank_callback_t callback);
|
|
|
|
void GB_set_log_callback(GB_gameboy_t *gb, GB_log_callback_t callback);
|
|
|
|
void GB_log(GB_gameboy_t *gb, const char *fmt, ...) __printflike(2, 3);
|
|
|
|
void GB_attributed_log(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, ...) __printflike(3, 4);
|
|
|
|
void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback);
|
2016-07-17 21:39:43 +00:00
|
|
|
void GB_set_async_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback);
|
2016-06-18 17:29:11 +00:00
|
|
|
void GB_set_sample_rate(GB_gameboy_t *gb, unsigned int sample_rate);
|
|
|
|
void GB_set_rgb_encode_callback(GB_gameboy_t *gb, GB_rgb_encode_callback_t callback);
|
2016-07-18 19:05:11 +00:00
|
|
|
void GB_set_infrared_callback(GB_gameboy_t *gb, GB_infrared_callback_t callback);
|
|
|
|
void GB_set_infrared_input(GB_gameboy_t *gb, bool state);
|
2016-07-20 22:03:13 +00:00
|
|
|
void GB_queue_infrared_input(GB_gameboy_t *gb, bool state, long cycles_after_previous_change);
|
2016-10-22 12:37:03 +00:00
|
|
|
void GB_set_rumble_callback(GB_gameboy_t *gb, GB_rumble_callback_t callback);
|
2016-11-11 23:58:53 +00:00
|
|
|
|
|
|
|
/* These APIs are used when using internal clock */
|
|
|
|
void GB_set_serial_transfer_start_callback(GB_gameboy_t *gb, GB_serial_transfer_start_callback_t callback);
|
|
|
|
void GB_set_serial_transfer_end_callback(GB_gameboy_t *gb, GB_serial_transfer_end_callback_t callback);
|
|
|
|
|
|
|
|
/* These APIs are used when using external clock */
|
|
|
|
uint8_t GB_serial_get_data(GB_gameboy_t *gb);
|
|
|
|
void GB_serial_set_data(GB_gameboy_t *gb, uint8_t data);
|
|
|
|
|
2017-01-13 19:27:37 +00:00
|
|
|
void GB_disconnect_serial(GB_gameboy_t *gb);
|
2016-06-18 17:29:11 +00:00
|
|
|
#endif /* GB_h */
|