| 1 |
#ifndef GB_h |
| 2 |
#define GB_h |
| 3 |
#include <stdbool.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdint.h> |
| 6 |
#include <time.h> |
| 7 |
|
| 8 |
#ifdef __cplusplus |
| 9 |
extern "C" { |
| 10 |
#endif |
| 11 |
|
| 12 |
#include "gb_struct_def.h" |
| 13 |
|
| 14 |
#include "apu.h" |
| 15 |
#include "timing.h" |
| 16 |
|
| 17 |
#define GB_STRUCT_VERSION 13 |
| 18 |
|
| 19 |
#define CGB (gb->model&GB_MODEL_CGB_FAMILY) |
| 20 |
|
| 21 |
#define GB_MODEL_FAMILY_MASK 0xF00 |
| 22 |
#define GB_MODEL_DMG_FAMILY 0x000 |
| 23 |
#define GB_MODEL_MGB_FAMILY 0x100 |
| 24 |
#define GB_MODEL_CGB_FAMILY 0x200 |
| 25 |
#define GB_MODEL_PAL_BIT 0x40 |
| 26 |
#define GB_MODEL_NO_SFC_BIT 0x80 |
| 27 |
|
| 28 |
#define GB_MODEL_PAL_BIT_OLD 0x1000 |
| 29 |
#define GB_MODEL_NO_SFC_BIT_OLD 0x2000 |
| 30 |
|
| 31 |
#if __clang__ |
| 32 |
#define unrolled _Pragma("unroll") |
| 33 |
#elif __GNUC__ >= 8 |
| 34 |
#define unrolled _Pragma("GCC unroll 8") |
| 35 |
#else |
| 36 |
#define unrolled |
| 37 |
#endif |
| 38 |
|
| 39 |
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 40 |
#define GB_BIG_ENDIAN |
| 41 |
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 42 |
#define GB_LITTLE_ENDIAN |
| 43 |
#else |
| 44 |
#error Unable to detect endianess |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef GB_BIG_ENDIAN |
| 48 |
#define LE16(x) __builtin_bswap16(x) |
| 49 |
#define LE32(x) __builtin_bswap32(x) |
| 50 |
#define LE64(x) __builtin_bswap64(x) |
| 51 |
#define BE16(x) (x) |
| 52 |
#define BE32(x) (x) |
| 53 |
#define BE64(x) (x) |
| 54 |
#else |
| 55 |
#define LE16(x) (x) |
| 56 |
#define LE32(x) (x) |
| 57 |
#define LE64(x) (x) |
| 58 |
#define BE16(x) __builtin_bswap16(x) |
| 59 |
#define BE32(x) __builtin_bswap32(x) |
| 60 |
#define BE64(x) __builtin_bswap64(x) |
| 61 |
#endif |
| 62 |
|
| 63 |
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8) |
| 64 |
#define __builtin_bswap16(x) ({ typeof(x) _x = (x); _x >> 8 | _x << 8; }) |
| 65 |
#endif |
| 66 |
|
| 67 |
typedef struct { |
| 68 |
struct { |
| 69 |
uint8_t r, g, b; |
| 70 |
} colors[5]; |
| 71 |
} GB_palette_t; |
| 72 |
|
| 73 |
extern const GB_palette_t GB_PALETTE_GREY; |
| 74 |
extern const GB_palette_t GB_PALETTE_DMG; |
| 75 |
extern const GB_palette_t GB_PALETTE_MGB; |
| 76 |
extern const GB_palette_t GB_PALETTE_GBL; |
| 77 |
|
| 78 |
typedef union { |
| 79 |
struct { |
| 80 |
uint8_t seconds; |
| 81 |
uint8_t minutes; |
| 82 |
uint8_t hours; |
| 83 |
uint8_t days; |
| 84 |
uint8_t high; |
| 85 |
}; |
| 86 |
struct { |
| 87 |
uint8_t seconds; |
| 88 |
uint8_t minutes; |
| 89 |
uint8_t hours:5; |
| 90 |
uint8_t weekday:3; |
| 91 |
uint8_t weeks; |
| 92 |
} tpp1; |
| 93 |
uint8_t data[5]; |
| 94 |
} GB_rtc_time_t; |
| 95 |
|
| 96 |
typedef enum { |
| 97 |
// GB_MODEL_DMG_0 = 0x000, |
| 98 |
// GB_MODEL_DMG_A = 0x001, |
| 99 |
GB_MODEL_DMG_B = 0x002, |
| 100 |
// GB_MODEL_DMG_C = 0x003, |
| 101 |
GB_MODEL_SGB = 0x004, |
| 102 |
GB_MODEL_SGB_NTSC = GB_MODEL_SGB, |
| 103 |
GB_MODEL_SGB_PAL = GB_MODEL_SGB | GB_MODEL_PAL_BIT, |
| 104 |
GB_MODEL_SGB_NTSC_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT, |
| 105 |
GB_MODEL_SGB_NO_SFC = GB_MODEL_SGB_NTSC_NO_SFC, |
| 106 |
GB_MODEL_SGB_PAL_NO_SFC = GB_MODEL_SGB | GB_MODEL_NO_SFC_BIT | GB_MODEL_PAL_BIT, |
| 107 |
// GB_MODEL_MGB = 0x100, |
| 108 |
GB_MODEL_SGB2 = 0x101, |
| 109 |
GB_MODEL_SGB2_NO_SFC = GB_MODEL_SGB2 | GB_MODEL_NO_SFC_BIT, |
| 110 |
// GB_MODEL_CGB_0 = 0x200, |
| 111 |
// GB_MODEL_CGB_A = 0x201, |
| 112 |
// GB_MODEL_CGB_B = 0x202, |
| 113 |
GB_MODEL_CGB_C = 0x203, |
| 114 |
// GB_MODEL_CGB_D = 0x204, |
| 115 |
GB_MODEL_CGB_E = 0x205, |
| 116 |
GB_MODEL_AGB = 0x206, |
| 117 |
GB_MODEL_AGB_NATIVE = 0x226, |
| 118 |
} GB_model_t; |
| 119 |
|
| 120 |
enum { |
| 121 |
GB_REGISTER_AF, |
| 122 |
GB_REGISTER_BC, |
| 123 |
GB_REGISTER_DE, |
| 124 |
GB_REGISTER_HL, |
| 125 |
GB_REGISTER_SP, |
| 126 |
GB_REGISTERS_16_BIT /* Count */ |
| 127 |
}; |
| 128 |
|
| 129 |
/* Todo: Actually use these! */ |
| 130 |
enum { |
| 131 |
GB_CARRY_FLAG = 16, |
| 132 |
GB_HALF_CARRY_FLAG = 32, |
| 133 |
GB_SUBTRACT_FLAG = 64, |
| 134 |
GB_ZERO_FLAG = 128, |
| 135 |
}; |
| 136 |
|
| 137 |
typedef enum { |
| 138 |
GB_BORDER_SGB, |
| 139 |
GB_BORDER_NEVER, |
| 140 |
GB_BORDER_ALWAYS, |
| 141 |
} GB_border_mode_t; |
| 142 |
|
| 143 |
enum { |
| 144 |
/* Joypad and Serial */ |
| 145 |
GB_IO_JOYP = 0x00, // Joypad (R/W) |
| 146 |
GB_IO_SB = 0x01, // Serial transfer data (R/W) |
| 147 |
GB_IO_SC = 0x02, // Serial Transfer Control (R/W) |
| 148 |
|
| 149 |
/* Missing */ |
| 150 |
|
| 151 |
/* Timers */ |
| 152 |
GB_IO_DIV = 0x04, // Divider Register (R/W) |
| 153 |
GB_IO_TIMA = 0x05, // Timer counter (R/W) |
| 154 |
GB_IO_TMA = 0x06, // Timer Modulo (R/W) |
| 155 |
GB_IO_TAC = 0x07, // Timer Control (R/W) |
| 156 |
|
| 157 |
/* Missing */ |
| 158 |
|
| 159 |
GB_IO_IF = 0x0f, // Interrupt Flag (R/W) |
| 160 |
|
| 161 |
/* Sound */ |
| 162 |
GB_IO_NR10 = 0x10, // Channel 1 Sweep register (R/W) |
| 163 |
GB_IO_NR11 = 0x11, // Channel 1 Sound length/Wave pattern duty (R/W) |
| 164 |
GB_IO_NR12 = 0x12, // Channel 1 Volume Envelope (R/W) |
| 165 |
GB_IO_NR13 = 0x13, // Channel 1 Frequency lo (Write Only) |
| 166 |
GB_IO_NR14 = 0x14, // Channel 1 Frequency hi (R/W) |
| 167 |
/* NR20 does not exist */ |
| 168 |
GB_IO_NR21 = 0x16, // Channel 2 Sound Length/Wave Pattern Duty (R/W) |
| 169 |
GB_IO_NR22 = 0x17, // Channel 2 Volume Envelope (R/W) |
| 170 |
GB_IO_NR23 = 0x18, // Channel 2 Frequency lo data (W) |
| 171 |
GB_IO_NR24 = 0x19, // Channel 2 Frequency hi data (R/W) |
| 172 |
GB_IO_NR30 = 0x1a, // Channel 3 Sound on/off (R/W) |
| 173 |
GB_IO_NR31 = 0x1b, // Channel 3 Sound Length |
| 174 |
GB_IO_NR32 = 0x1c, // Channel 3 Select output level (R/W) |
| 175 |
GB_IO_NR33 = 0x1d, // Channel 3 Frequency's lower data (W) |
| 176 |
GB_IO_NR34 = 0x1e, // Channel 3 Frequency's higher data (R/W) |
| 177 |
/* NR40 does not exist */ |
| 178 |
GB_IO_NR41 = 0x20, // Channel 4 Sound Length (R/W) |
| 179 |
GB_IO_NR42 = 0x21, // Channel 4 Volume Envelope (R/W) |
| 180 |
GB_IO_NR43 = 0x22, // Channel 4 Polynomial Counter (R/W) |
| 181 |
GB_IO_NR44 = 0x23, // Channel 4 Counter/consecutive, Inital (R/W) |
| 182 |
GB_IO_NR50 = 0x24, // Channel control / ON-OFF / Volume (R/W) |
| 183 |
GB_IO_NR51 = 0x25, // Selection of Sound output terminal (R/W) |
| 184 |
GB_IO_NR52 = 0x26, // Sound on/off |
| 185 |
|
| 186 |
/* Missing */ |
| 187 |
|
| 188 |
GB_IO_WAV_START = 0x30, // Wave pattern start |
| 189 |
GB_IO_WAV_END = 0x3f, // Wave pattern end |
| 190 |
|
| 191 |
/* Graphics */ |
| 192 |
GB_IO_LCDC = 0x40, // LCD Control (R/W) |
| 193 |
GB_IO_STAT = 0x41, // LCDC Status (R/W) |
| 194 |
GB_IO_SCY = 0x42, // Scroll Y (R/W) |
| 195 |
GB_IO_SCX = 0x43, // Scroll X (R/W) |
| 196 |
GB_IO_LY = 0x44, // LCDC Y-Coordinate (R) |
| 197 |
GB_IO_LYC = 0x45, // LY Compare (R/W) |
| 198 |
GB_IO_DMA = 0x46, // DMA Transfer and Start Address (W) |
| 199 |
GB_IO_BGP = 0x47, // BG Palette Data (R/W) - Non CGB Mode Only |
| 200 |
GB_IO_OBP0 = 0x48, // Object Palette 0 Data (R/W) - Non CGB Mode Only |
| 201 |
GB_IO_OBP1 = 0x49, // Object Palette 1 Data (R/W) - Non CGB Mode Only |
| 202 |
GB_IO_WY = 0x4a, // Window Y Position (R/W) |
| 203 |
GB_IO_WX = 0x4b, // Window X Position minus 7 (R/W) |
| 204 |
// Has some undocumented compatibility flags written at boot. |
| 205 |
// Unfortunately it is not readable or writable after boot has finished, so research of this |
| 206 |
// register is quite limited. The value written to this register, however, can be controlled |
| 207 |
// in some cases. |
| 208 |
GB_IO_KEY0 = 0x4c, |
| 209 |
|
| 210 |
/* General CGB features */ |
| 211 |
GB_IO_KEY1 = 0x4d, // CGB Mode Only - Prepare Speed Switch |
| 212 |
|
| 213 |
/* Missing */ |
| 214 |
|
| 215 |
GB_IO_VBK = 0x4f, // CGB Mode Only - VRAM Bank |
| 216 |
GB_IO_BANK = 0x50, // Write to disable the BIOS mapping |
| 217 |
|
| 218 |
/* CGB DMA */ |
| 219 |
GB_IO_HDMA1 = 0x51, // CGB Mode Only - New DMA Source, High |
| 220 |
GB_IO_HDMA2 = 0x52, // CGB Mode Only - New DMA Source, Low |
| 221 |
GB_IO_HDMA3 = 0x53, // CGB Mode Only - New DMA Destination, High |
| 222 |
GB_IO_HDMA4 = 0x54, // CGB Mode Only - New DMA Destination, Low |
| 223 |
GB_IO_HDMA5 = 0x55, // CGB Mode Only - New DMA Length/Mode/Start |
| 224 |
|
| 225 |
/* IR */ |
| 226 |
GB_IO_RP = 0x56, // CGB Mode Only - Infrared Communications Port |
| 227 |
|
| 228 |
/* Missing */ |
| 229 |
|
| 230 |
/* CGB Paletts */ |
| 231 |
GB_IO_BGPI = 0x68, // CGB Mode Only - Background Palette Index |
| 232 |
GB_IO_BGPD = 0x69, // CGB Mode Only - Background Palette Data |
| 233 |
GB_IO_OBPI = 0x6a, // CGB Mode Only - Sprite Palette Index |
| 234 |
GB_IO_OBPD = 0x6b, // CGB Mode Only - Sprite Palette Data |
| 235 |
GB_IO_OPRI = 0x6c, // Affects object priority (X based or index based) |
| 236 |
|
| 237 |
/* Missing */ |
| 238 |
|
| 239 |
GB_IO_SVBK = 0x70, // CGB Mode Only - WRAM Bank |
| 240 |
GB_IO_UNKNOWN2 = 0x72, // (00h) - Bit 0-7 (Read/Write) |
| 241 |
GB_IO_UNKNOWN3 = 0x73, // (00h) - Bit 0-7 (Read/Write) |
| 242 |
GB_IO_UNKNOWN4 = 0x74, // (00h) - Bit 0-7 (Read/Write) - CGB Mode Only |
| 243 |
GB_IO_UNKNOWN5 = 0x75, // (8Fh) - Bit 4-6 (Read/Write) |
| 244 |
GB_IO_PCM_12 = 0x76, // Channels 1 and 2 amplitudes |
| 245 |
GB_IO_PCM_34 = 0x77, // Channels 3 and 4 amplitudes |
| 246 |
GB_IO_UNKNOWN8 = 0x7F, // Unknown, write only |
| 247 |
}; |
| 248 |
|
| 249 |
typedef enum { |
| 250 |
GB_LOG_BOLD = 1, |
| 251 |
GB_LOG_DASHED_UNDERLINE = 2, |
| 252 |
GB_LOG_UNDERLINE = 4, |
| 253 |
GB_LOG_UNDERLINE_MASK = GB_LOG_DASHED_UNDERLINE | GB_LOG_UNDERLINE |
| 254 |
} GB_log_attributes; |
| 255 |
|
| 256 |
typedef enum { |
| 257 |
GB_BOOT_ROM_DMG0, |
| 258 |
GB_BOOT_ROM_DMG, |
| 259 |
GB_BOOT_ROM_MGB, |
| 260 |
GB_BOOT_ROM_SGB, |
| 261 |
GB_BOOT_ROM_SGB2, |
| 262 |
GB_BOOT_ROM_CGB0, |
| 263 |
GB_BOOT_ROM_CGB, |
| 264 |
GB_BOOT_ROM_AGB, |
| 265 |
} GB_boot_rom_t; |
| 266 |
|
| 267 |
typedef enum { |
| 268 |
GB_RTC_MODE_SYNC_TO_HOST, |
| 269 |
GB_RTC_MODE_ACCURATE, |
| 270 |
} GB_rtc_mode_t; |
| 271 |
|
| 272 |
#define LCDC_PERIOD 70224 |
| 273 |
#define CPU_FREQUENCY 0x400000 |
| 274 |
#define SGB_NTSC_FREQUENCY (21477272 / 5) |
| 275 |
#define SGB_PAL_FREQUENCY (21281370 / 5) |
| 276 |
#define DIV_CYCLES (0x100) |
| 277 |
#define INTERNAL_DIV_CYCLES (0x40000) |
| 278 |
|
| 279 |
#if !defined(MIN) |
| 280 |
#define MIN(a,b) (((a)<(b))?(a):(b)) |
| 281 |
#endif |
| 282 |
|
| 283 |
#if !defined(MAX) |
| 284 |
#define MAX(a,b) (((a)>(b))?(a):(b)) |
| 285 |
#endif |
| 286 |
|
| 287 |
typedef void (*GB_vblank_callback_t)(GB_gameboy_t *gb); |
| 288 |
typedef void (*GB_log_callback_t)(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes); |
| 289 |
typedef char *(*GB_input_callback_t)(GB_gameboy_t *gb); |
| 290 |
typedef uint32_t (*GB_rgb_encode_callback_t)(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b); |
| 291 |
typedef void (*GB_infrared_callback_t)(GB_gameboy_t *gb, bool on); |
| 292 |
typedef void (*GB_rumble_callback_t)(GB_gameboy_t *gb, double rumble_amplitude); |
| 293 |
typedef void (*GB_serial_transfer_bit_start_callback_t)(GB_gameboy_t *gb, bool bit_to_send); |
| 294 |
typedef bool (*GB_serial_transfer_bit_end_callback_t)(GB_gameboy_t *gb); |
| 295 |
typedef void (*GB_update_input_hint_callback_t)(GB_gameboy_t *gb); |
| 296 |
typedef void (*GB_joyp_write_callback_t)(GB_gameboy_t *gb, uint8_t value); |
| 297 |
typedef void (*GB_icd_pixel_callback_t)(GB_gameboy_t *gb, uint8_t row); |
| 298 |
typedef void (*GB_icd_hreset_callback_t)(GB_gameboy_t *gb); |
| 299 |
typedef void (*GB_icd_vreset_callback_t)(GB_gameboy_t *gb); |
| 300 |
typedef void (*GB_boot_rom_load_callback_t)(GB_gameboy_t *gb, GB_boot_rom_t type); |
| 301 |
|
| 302 |
struct GB_breakpoint_s; |
| 303 |
struct GB_watchpoint_s; |
| 304 |
|
| 305 |
typedef struct { |
| 306 |
uint8_t pixel; // Color, 0-3 |
| 307 |
uint8_t palette; // Palette, 0 - 7 (CGB); 0-1 in DMG (or just 0 for BG) |
| 308 |
uint8_t priority; // Sprite priority – 0 in DMG, OAM index in CGB |
| 309 |
bool bg_priority; // For sprite FIFO – the BG priority bit. For the BG FIFO – the CGB attributes priority bit |
| 310 |
} GB_fifo_item_t; |
| 311 |
|
| 312 |
#define GB_FIFO_LENGTH 16 |
| 313 |
typedef struct { |
| 314 |
GB_fifo_item_t fifo[GB_FIFO_LENGTH]; |
| 315 |
uint8_t read_end; |
| 316 |
uint8_t write_end; |
| 317 |
} GB_fifo_t; |
| 318 |
|
| 319 |
typedef struct { |
| 320 |
uint32_t magic; |
| 321 |
uint8_t track_count; |
| 322 |
uint8_t first_track; |
| 323 |
uint16_t load_address; |
| 324 |
uint16_t init_address; |
| 325 |
uint16_t play_address; |
| 326 |
uint16_t sp; |
| 327 |
uint8_t TMA; |
| 328 |
uint8_t TAC; |
| 329 |
char title[32]; |
| 330 |
char author[32]; |
| 331 |
char copyright[32]; |
| 332 |
} GB_gbs_header_t; |
| 333 |
|
| 334 |
typedef struct { |
| 335 |
uint8_t track_count; |
| 336 |
uint8_t first_track; |
| 337 |
char title[33]; |
| 338 |
char author[33]; |
| 339 |
char copyright[33]; |
| 340 |
} GB_gbs_info_t; |
| 341 |
|
| 342 |
/* When state saving, each section is dumped independently of other sections. |
| 343 |
This allows adding data to the end of the section without worrying about future compatibility. |
| 344 |
Some other changes might be "safe" as well. |
| 345 |
This struct is not packed, but dumped sections exclusively use types that have the same alignment in both 32 and 64 |
| 346 |
bit platforms. */ |
| 347 |
|
| 348 |
/* minimal GB_gameboy_s struct with just the APU */ |
| 349 |
struct GB_gameboy_s { |
| 350 |
/* CPU and General Hardware Flags*/ |
| 351 |
GB_model_t model; |
| 352 |
bool cgb_mode; |
| 353 |
bool cgb_double_speed; |
| 354 |
bool halted; |
| 355 |
bool stopped; |
| 356 |
|
| 357 |
uint8_t io_registers[0x80]; |
| 358 |
GB_UNIT(div); |
| 359 |
uint16_t div_counter; |
| 360 |
uint8_t tima_reload_state; |
| 361 |
GB_apu_t apu; |
| 362 |
GB_apu_output_t apu_output; |
| 363 |
}; |
| 364 |
|
| 365 |
#ifndef __printflike |
| 366 |
/* Missing from Linux headers. */ |
| 367 |
#define __printflike(fmtarg, firstvararg) \ |
| 368 |
__attribute__((__format__ (__printf__, fmtarg, firstvararg))) |
| 369 |
#endif |
| 370 |
|
| 371 |
typedef enum { |
| 372 |
GB_DIRECT_ACCESS_ROM, |
| 373 |
GB_DIRECT_ACCESS_RAM, |
| 374 |
GB_DIRECT_ACCESS_CART_RAM, |
| 375 |
GB_DIRECT_ACCESS_VRAM, |
| 376 |
GB_DIRECT_ACCESS_HRAM, |
| 377 |
GB_DIRECT_ACCESS_IO, /* Warning: Some registers can only be read/written correctly via GB_memory_read/write. */ |
| 378 |
GB_DIRECT_ACCESS_BOOTROM, |
| 379 |
GB_DIRECT_ACCESS_OAM, |
| 380 |
GB_DIRECT_ACCESS_BGP, |
| 381 |
GB_DIRECT_ACCESS_OBP, |
| 382 |
GB_DIRECT_ACCESS_IE, |
| 383 |
} GB_direct_access_t; |
| 384 |
|
| 385 |
#ifdef __cplusplus |
| 386 |
} |
| 387 |
#endif |
| 388 |
|
| 389 |
#endif /* GB_h */ |