/******************************************************************************\ ** ** This file is part of the Hades GBA Emulator, and is made available under ** the terms of the GNU General Public License version 2. ** ** Copyright (C) 2021-2026 - The Hades Authors ** \******************************************************************************/ #pragma once #define GBA_SCREEN_WIDTH 240 #define GBA_SCREEN_HEIGHT 160 #define GBA_SCREEN_REAL_WIDTH 308 #define GBA_SCREEN_REAL_HEIGHT 228 #define GBA_CYCLES_PER_PIXEL 4 #define GBA_CYCLES_PER_FRAME (CYCLES_PER_PIXEL * GBA_SCREEN_REAL_WIDTH * GBA_SCREEN_REAL_HEIGHT) #define GBA_CYCLES_PER_SECOND ((uint64_t)(16 * 1024 * 1024)) #include "hades.h" #include "gba/channel.h" #include "gba/core.h" #include "gba/scheduler.h" #include "gba/memory.h" #include "gba/ppu.h" #include "gba/apu.h" #include "gba/io.h" #include "gba/gpio.h" #include "gba/debugger.h" #include "gba/cheat.h" enum gba_states { GBA_STATE_STOP = 0, GBA_STATE_PAUSE, GBA_STATE_RUN, }; enum gba_run_modes { GBA_RUN_MODE_NORMAL, #ifdef WITH_DEBUGGER GBA_RUN_MODE_FRAME, GBA_RUN_MODE_TRACE, GBA_RUN_MODE_STEP_IN, GBA_RUN_MODE_STEP_OVER, #endif }; enum gba_keys { KEY_A, KEY_B, KEY_L, KEY_R, KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_START, KEY_SELECT, KEY_MAX, KEY_MIN = KEY_A, }; struct gba_cheat_raw { bool enabled; enum { RAW_CHEAT_KIND_PARV3, RAW_CHEAT_KIND_GAMESHARK, RAW_CHEAT_KIND_CODEBREAKER, RAW_CHEAT_KIND_MAX, } kind; struct { bool finished; bool success; char *error; } compilation; char name[64]; char code[4096]; }; struct gba_shared_data { // The emulator's screen, as built by the PPU each frame. struct { uint32_t data[GBA_SCREEN_WIDTH * GBA_SCREEN_HEIGHT]; pthread_mutex_t lock; } framebuffer; // The game's backup storage. // There's no lock behind this data because struct { uint8_t *data; size_t size; atomic_bool dirty; // Set to true when `data` is modified. } backup_storage; // The frame counter, used for FPS calculations. atomic_uint frame_counter; // Audio ring buffer. struct apu_rbuffer audio_rbuffer; pthread_mutex_t audio_rbuffer_mutex; }; /* ** Settings that can be altered while the game is running. */ struct gba_settings { // Fast forward bool fast_forward; // Speed. 0.5 = 30fps, 1 = 60fps, 2 = 120fps, etc. // Can't be <= 0.0 unless `fast_forward` is true. float speed; // Enable the emulation of the prefetch buffer bool prefetch_buffer; // Enable frame skipping bool enable_frame_skipping; // How many frame to skip uint32_t frame_skip_counter; struct { bool enable_bg_layers[4]; bool enable_oam; } ppu; struct { bool enable_psg_channels[4]; bool enable_fifo_channels[2]; } apu; }; struct game_entry { char *code; enum backup_storage_types storage; enum gpio_device_types gpio; bool mirror; char *title; }; struct idle_loop_entry { char const *code; uint32_t addr; bool thumb; }; struct gba { bool exit; // The current state of the GBA enum gba_states state; // The "run mode" of the gba (how it should behave when running and for how long). enum gba_run_modes run_mode; // The channel used to communicate with the frontend struct channels channels; // Shared data with the frontend, mainly the framebuffer and audio channels. struct gba_shared_data shared_data; // A set of settings the frontend can update during the emulator's execution (speed, etc.) struct gba_settings settings; // The different components of the GBA struct core core; struct scheduler scheduler; struct memory memory; struct ppu ppu; struct apu apu; struct io io; struct gpio gpio; struct debugger debugger; struct cheats cheats; }; /* ** The initial configuration to run a game on the emulator thread. ** ** This structure contains the BIOS data, the game's ROM, its save data and other information needed ** by the emulator thread to start running the game. * ** The application thread keeps ownership of the pointers contained in a launch config. ** The emulator thread will copy their content. ** ** Therefore, those pointers must remain valid until the emulator thread sends a NOTIFICATION_RESET. */ struct launch_config { // The game ROM and its size struct { uint8_t *data; size_t size; } rom; // The BIOS and its size struct { uint8_t *data; size_t size; } bios; // True if the BIOS should be skipped bool skip_bios; // Set to the frontend's audio frequency. // Can be 0 if the frontend has no audio. uint32_t audio_frequency; // GPIO device attached to the cartridge. enum gpio_device_types gpio_device_type; // Enable ROM memory mirroring bool rom_mirroring; // Idle loop elimination struct { bool enabled; uint32_t addr; bool thumb; } idle_loop; // The kind of storage type to use. struct { enum backup_storage_types type; uint8_t *data; size_t size; } backup_storage; // Initial value for all runtime-settings (speed, etc.) struct gba_settings settings; // Cheats struct { struct gba_cheat_raw *list; size_t len; } cheats; }; struct notification; enum notification_kind; /* ** The following functions are part of the public API of libgba. ** ** TODO FIXME: Make a public header for libgba. */ /* source/gba/gba.c */ struct gba *gba_create(void); void gba_run(struct gba *gba); void gba_delete(struct gba *gba); void gba_shared_framebuffer_lock(struct gba *gba); void gba_shared_framebuffer_release(struct gba *gba); void gba_shared_audio_rbuffer_lock(struct gba *gba); void gba_shared_audio_rbuffer_release(struct gba *gba); uint32_t gba_shared_audio_rbuffer_pop_sample(struct gba *gba); uint32_t gba_shared_reset_frame_counter(struct gba *gba); void gba_delete_notification(struct notification const *notif); /* source/gba/db.c */ struct game_entry *db_lookup_game(uint8_t const game_code[3]); struct game_entry *db_autodetect_game_features(uint8_t const *rom, size_t rom_size); struct idle_loop_entry const *db_lookup_idle_loop(uint8_t const game_code[4]); /* source/gba/cheat/cheat.c */ bool cheat_parse_and_compile(struct cheat_bin *bin, struct gba_cheat_raw *raw); bool cheat_parse(struct gba_cheat_raw *raw); /* ** The following functions are *NOT* part of the public API of libgba. */ /* source/gba/gba.c */ void gba_send_notification(struct gba *gba, enum notification_kind notif); void gba_state_pause(struct gba *); void gba_send_notification_raw(struct gba *gba, struct event_header const *notif_header);