Cleanup, better symbol handling, improves LTO

This commit is contained in:
Lior Halphon 2021-11-07 13:39:18 +02:00
parent fbf1bb7f98
commit 18e7a3f4fa
22 changed files with 108 additions and 106 deletions

View File

@ -4,9 +4,6 @@
#include <assert.h>
#include "gb.h"
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
static const uint8_t duties[] = {
0, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 1,

View File

@ -3,8 +3,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include "gb_struct_def.h"
#include "defs.h"
#ifdef GB_INTERNAL
/* Speed = 1 / Length (in seconds) */
@ -172,14 +171,14 @@ void GB_set_interference_volume(GB_gameboy_t *gb, double volume);
void GB_apu_set_sample_callback(GB_gameboy_t *gb, GB_sample_callback_t callback);
#ifdef GB_INTERNAL
bool GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index);
void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value);
uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg);
void GB_apu_div_event(GB_gameboy_t *gb);
void GB_apu_div_secondary_event(GB_gameboy_t *gb);
void GB_apu_init(GB_gameboy_t *gb);
void GB_apu_run(GB_gameboy_t *gb);
void GB_apu_update_cycles_per_sample(GB_gameboy_t *gb);
bool internal GB_apu_is_DAC_enabled(GB_gameboy_t *gb, unsigned index);
void internal GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value);
uint8_t internal GB_apu_read(GB_gameboy_t *gb, uint8_t reg);
void internal GB_apu_div_event(GB_gameboy_t *gb);
void internal GB_apu_div_secondary_event(GB_gameboy_t *gb);
void internal GB_apu_init(GB_gameboy_t *gb);
void internal GB_apu_run(GB_gameboy_t *gb);
void internal GB_apu_update_cycles_per_sample(GB_gameboy_t *gb);
#endif
#endif /* apu_h */

View File

@ -1,7 +1,7 @@
#ifndef camera_h
#define camera_h
#include <stdint.h>
#include "gb_struct_def.h"
#include "defs.h"
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);

View File

@ -1,6 +1,6 @@
#ifndef cheats_h
#define cheats_h
#include "gb_struct_def.h"
#include "defs.h"
#define GB_CHEAT_ANY_BANK 0xFFFF
@ -20,7 +20,7 @@ int GB_save_cheats(GB_gameboy_t *gb, const char *path);
#ifdef GB_DISABLE_CHEATS
#define GB_apply_cheat(...)
#else
void GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value);
void internal GB_apply_cheat(GB_gameboy_t *gb, uint16_t address, uint8_t *value);
#endif
#endif

View File

@ -2,7 +2,7 @@
#define debugger_h
#include <stdbool.h>
#include <stdint.h>
#include "gb_struct_def.h"
#include "defs.h"
#include "symbol_hash.h"
@ -17,14 +17,14 @@
#define GB_debugger_add_symbol(gb, bank, address, symbol) ((void)bank, (void)address, (void)symbol)
#else
void GB_debugger_run(GB_gameboy_t *gb);
void GB_debugger_handle_async_commands(GB_gameboy_t *gb);
void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr);
void GB_debugger_ret_hook(GB_gameboy_t *gb);
void GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value);
void GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr);
const GB_bank_symbol_t *GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr);
void GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol);
void internal GB_debugger_run(GB_gameboy_t *gb);
void internal GB_debugger_handle_async_commands(GB_gameboy_t *gb);
void internal GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr);
void internal GB_debugger_ret_hook(GB_gameboy_t *gb);
void internal GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value);
void internal GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr);
const GB_bank_symbol_t *internal GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr);
void internal GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol);
#endif /* GB_DISABLE_DEBUGGER */
#endif

44
Core/defs.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef defs_h
#define defs_h
#ifdef GB_INTERNAL
// "Keyword" definitions
#define likely(x) __builtin_expect((x), 1)
#define unlikely(x) __builtin_expect((x), 0)
#define internal __attribute__((visibility("hidden")))
#if __clang__
#define unrolled _Pragma("unroll")
#elif __GNUC__ >= 8
#define unrolled _Pragma("GCC unroll 8")
#else
#define unrolled
#endif
/* Todo: similar macros are everywhere, clean this up and remove direct calls to bswap */
#ifdef GB_BIG_ENDIAN
#define LE16(x) __builtin_bswap16(x)
#define LE32(x) __builtin_bswap32(x)
#define LE64(x) __builtin_bswap64(x)
#define BE16(x) (x)
#define BE32(x) (x)
#define BE64(x) (x)
#else
#define LE16(x) (x)
#define LE32(x) (x)
#define LE64(x) (x)
#define BE16(x) __builtin_bswap16(x)
#define BE32(x) __builtin_bswap32(x)
#define BE64(x) __builtin_bswap64(x)
#endif
#endif
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
#define __builtin_bswap16(x) ({ typeof(x) _x = (x); _x >> 8 | _x << 8; })
#endif
struct GB_gameboy_s;
typedef struct GB_gameboy_s GB_gameboy_t;
#endif

View File

@ -6,10 +6,10 @@
#include <stdint.h>
#ifdef GB_INTERNAL
void GB_display_run(GB_gameboy_t *gb, uint8_t cycles);
void GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index);
void GB_STAT_update(GB_gameboy_t *gb);
void GB_lcd_off(GB_gameboy_t *gb);
void internal GB_display_run(GB_gameboy_t *gb, uint8_t cycles);
void internal GB_palette_changed(GB_gameboy_t *gb, bool background_palette, uint8_t index);
void internal GB_STAT_update(GB_gameboy_t *gb);
void internal GB_lcd_off(GB_gameboy_t *gb);
enum {
GB_OBJECT_PRIORITY_X,

View File

@ -5,7 +5,7 @@
#include <stdint.h>
#include <time.h>
#include "gb_struct_def.h"
#include "defs.h"
#include "save_state.h"
#include "apu.h"
@ -35,17 +35,6 @@
#define GB_MODEL_PAL_BIT 0x40
#define GB_MODEL_NO_SFC_BIT 0x80
#ifdef GB_INTERNAL
#if __clang__
#define unrolled _Pragma("unroll")
#elif __GNUC__ >= 8
#define unrolled _Pragma("GCC unroll 8")
#else
#define unrolled
#endif
#endif
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define GB_BIG_ENDIAN
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
@ -54,28 +43,6 @@
#error Unable to detect endianess
#endif
#ifdef GB_INTERNAL
/* Todo: similar macros are everywhere, clean this up and remove direct calls to bswap */
#ifdef GB_BIG_ENDIAN
#define LE16(x) __builtin_bswap16(x)
#define LE32(x) __builtin_bswap32(x)
#define LE64(x) __builtin_bswap64(x)
#define BE16(x) (x)
#define BE32(x) (x)
#define BE64(x) (x)
#else
#define LE16(x) (x)
#define LE32(x) (x)
#define LE64(x) (x)
#define BE16(x) __builtin_bswap16(x)
#define BE32(x) __builtin_bswap32(x)
#define BE64(x) __builtin_bswap64(x)
#endif
#endif
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
#define __builtin_bswap16(x) ({ typeof(x) _x = (x); _x >> 8 | _x << 8; })
#endif
typedef struct {
struct GB_color_s {
@ -889,7 +856,7 @@ void GB_get_rom_title(GB_gameboy_t *gb, char *title);
uint32_t GB_get_rom_crc32(GB_gameboy_t *gb);
#ifdef GB_INTERNAL
void GB_borrow_sgb_border(GB_gameboy_t *gb);
void internal GB_borrow_sgb_border(GB_gameboy_t *gb);
#endif
#endif /* GB_h */

View File

@ -1,5 +0,0 @@
#ifndef gb_struct_def_h
#define gb_struct_def_h
struct GB_gameboy_s;
typedef struct GB_gameboy_s GB_gameboy_t;
#endif

View File

@ -1,6 +1,6 @@
#ifndef joypad_h
#define joypad_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdbool.h>
typedef enum {
@ -20,6 +20,6 @@ void GB_set_key_state_for_player(GB_gameboy_t *gb, GB_key_t index, unsigned play
void GB_icd_set_joyp(GB_gameboy_t *gb, uint8_t value);
#ifdef GB_INTERNAL
void GB_update_joyp(GB_gameboy_t *gb);
void internal GB_update_joyp(GB_gameboy_t *gb);
#endif
#endif /* joypad_h */

View File

@ -1,6 +1,6 @@
#ifndef MBC_h
#define MBC_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdbool.h>
typedef struct {
@ -26,8 +26,8 @@ typedef struct {
#ifdef GB_INTERNAL
extern const GB_cartridge_t GB_cart_defs[256];
void GB_update_mbc_mappings(GB_gameboy_t *gb);
void GB_configure_cart(GB_gameboy_t *gb);
void internal GB_update_mbc_mappings(GB_gameboy_t *gb);
void internal GB_configure_cart(GB_gameboy_t *gb);
#endif
#endif /* MBC_h */

View File

@ -1,6 +1,6 @@
#ifndef memory_h
#define memory_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdint.h>
typedef uint8_t (*GB_read_memory_callback_t)(GB_gameboy_t *gb, uint16_t addr, uint8_t data);
@ -9,9 +9,9 @@ void GB_set_read_memory_callback(GB_gameboy_t *gb, GB_read_memory_callback_t cal
uint8_t GB_read_memory(GB_gameboy_t *gb, uint16_t addr);
void GB_write_memory(GB_gameboy_t *gb, uint16_t addr, uint8_t value);
#ifdef GB_INTERNAL
void GB_dma_run(GB_gameboy_t *gb);
void GB_hdma_run(GB_gameboy_t *gb);
void GB_trigger_oam_bug(GB_gameboy_t *gb, uint16_t address);
void internal GB_dma_run(GB_gameboy_t *gb);
void internal GB_hdma_run(GB_gameboy_t *gb);
void internal GB_trigger_oam_bug(GB_gameboy_t *gb, uint16_t address);
#endif
#endif /* memory_h */

View File

@ -2,7 +2,7 @@
#define printer_h
#include <stdint.h>
#include <stdbool.h>
#include "gb_struct_def.h"
#include "defs.h"
#define GB_PRINTER_MAX_COMMAND_LENGTH 0x280
#define GB_PRINTER_DATA_SIZE 0x280

View File

@ -2,11 +2,11 @@
#define rewind_h
#include <stdbool.h>
#include "gb_struct_def.h"
#include "defs.h"
#ifdef GB_INTERNAL
void GB_rewind_push(GB_gameboy_t *gb);
void GB_rewind_free(GB_gameboy_t *gb);
void internal GB_rewind_push(GB_gameboy_t *gb);
void internal GB_rewind_free(GB_gameboy_t *gb);
#endif
bool GB_rewind_pop(GB_gameboy_t *gb);
void GB_set_rewind_length(GB_gameboy_t *gb, double seconds);

View File

@ -1,7 +1,7 @@
#ifndef rumble_h
#define rumble_h
#include "gb_struct_def.h"
#include "defs.h"
typedef enum {
GB_RUMBLE_DISABLED,
@ -10,7 +10,7 @@ typedef enum {
} GB_rumble_mode_t;
#ifdef GB_INTERNAL
void GB_handle_rumble(GB_gameboy_t *gb);
void internal GB_handle_rumble(GB_gameboy_t *gb);
#endif
void GB_set_rumble_mode(GB_gameboy_t *gb, GB_rumble_mode_t mode);

View File

@ -36,8 +36,8 @@ static inline uint32_t state_magic(void)
}
/* For internal in-memory save states (rewind, debugger) that do not need BESS */
size_t GB_get_save_state_size_no_bess(GB_gameboy_t *gb);
void GB_save_state_to_buffer_no_bess(GB_gameboy_t *gb, uint8_t *buffer);
size_t internal GB_get_save_state_size_no_bess(GB_gameboy_t *gb);
void internal GB_save_state_to_buffer_no_bess(GB_gameboy_t *gb, uint8_t *buffer);
#endif
#endif /* save_state_h */

View File

@ -1,6 +1,6 @@
#ifndef sgb_h
#define sgb_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdint.h>
#include <stdbool.h>
@ -58,9 +58,9 @@ struct GB_sgb_s {
uint8_t received_header[0x54];
};
void GB_sgb_write(GB_gameboy_t *gb, uint8_t value);
void GB_sgb_render(GB_gameboy_t *gb);
void GB_sgb_load_default_data(GB_gameboy_t *gb);
void internal GB_sgb_write(GB_gameboy_t *gb, uint8_t value);
void internal GB_sgb_render(GB_gameboy_t *gb);
void internal GB_sgb_load_default_data(GB_gameboy_t *gb);
#endif

View File

@ -1,11 +1,11 @@
#ifndef sm83_cpu_h
#define sm83_cpu_h
#include "gb_struct_def.h"
#include "defs.h"
#include <stdint.h>
void GB_cpu_disassemble(GB_gameboy_t *gb, uint16_t pc, uint16_t count);
#ifdef GB_INTERNAL
void GB_cpu_run(GB_gameboy_t *gb);
void internal GB_cpu_run(GB_gameboy_t *gb);
#endif
#endif /* sm83_cpu_h */

View File

@ -27,12 +27,12 @@ typedef struct {
} GB_reversed_symbol_map_t;
#ifdef GB_INTERNAL
void GB_reversed_map_add_symbol(GB_reversed_symbol_map_t *map, uint16_t bank, GB_bank_symbol_t *symbol);
const GB_symbol_t *GB_reversed_map_find_symbol(GB_reversed_symbol_map_t *map, const char *name);
GB_bank_symbol_t *GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const char *name);
const GB_bank_symbol_t *GB_map_find_symbol(GB_symbol_map_t *map, uint16_t addr);
GB_symbol_map_t *GB_map_alloc(void);
void GB_map_free(GB_symbol_map_t *map);
void internal GB_reversed_map_add_symbol(GB_reversed_symbol_map_t *map, uint16_t bank, GB_bank_symbol_t *symbol);
const GB_symbol_t *internal GB_reversed_map_find_symbol(GB_reversed_symbol_map_t *map, const char *name);
GB_bank_symbol_t *internal GB_map_add_symbol(GB_symbol_map_t *map, uint16_t addr, const char *name);
const GB_bank_symbol_t *internal GB_map_find_symbol(GB_symbol_map_t *map, uint16_t addr);
GB_symbol_map_t *internal GB_map_alloc(void);
void internal GB_map_free(GB_symbol_map_t *map);
#endif
#endif /* symbol_hash_h */

View File

@ -1,13 +1,13 @@
#ifndef timing_h
#define timing_h
#include "gb_struct_def.h"
#include "defs.h"
#ifdef GB_INTERNAL
void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles);
void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac);
bool GB_timing_sync_turbo(GB_gameboy_t *gb); /* Returns true if should skip frame */
void GB_timing_sync(GB_gameboy_t *gb);
void GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value);
void internal GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles);
void internal GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac);
bool internal GB_timing_sync_turbo(GB_gameboy_t *gb); /* Returns true if should skip frame */
void internal GB_timing_sync(GB_gameboy_t *gb);
void internal GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value);
enum {
GB_TIMA_RUNNING = 0,

View File

@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include "gb_struct_def.h"
#include "defs.h"
typedef struct {

View File

@ -175,7 +175,7 @@ CFLAGS += -O3 -DNDEBUG
STRIP := strip
ifeq ($(PLATFORM),Darwin)
LDFLAGS += -Wl,-exported_symbols_list,$(NULL)
STRIP := -@true
STRIP := strip -x
endif
ifneq ($(PLATFORM),windows32)
LDFLAGS += -flto