2016-03-30 20:07:55 +00:00
|
|
|
#ifndef timing_h
|
|
|
|
#define timing_h
|
2021-11-07 11:39:18 +00:00
|
|
|
#include "defs.h"
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2021-12-11 00:51:21 +00:00
|
|
|
typedef enum {
|
|
|
|
GB_RTC_MODE_SYNC_TO_HOST,
|
|
|
|
GB_RTC_MODE_ACCURATE,
|
|
|
|
} GB_rtc_mode_t;
|
|
|
|
|
|
|
|
/* RTC emulation mode */
|
|
|
|
void GB_set_rtc_mode(GB_gameboy_t *gb, GB_rtc_mode_t mode);
|
|
|
|
|
|
|
|
/* Speed multiplier for the RTC, mostly for TAS syncing */
|
|
|
|
void GB_set_rtc_multiplier(GB_gameboy_t *gb, double multiplier);
|
|
|
|
|
2017-04-17 17:16:17 +00:00
|
|
|
#ifdef GB_INTERNAL
|
2021-11-07 12:13:52 +00:00
|
|
|
internal void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles);
|
|
|
|
internal void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac);
|
|
|
|
internal bool GB_timing_sync_turbo(GB_gameboy_t *gb); /* Returns true if should skip frame */
|
|
|
|
internal void GB_timing_sync(GB_gameboy_t *gb);
|
|
|
|
internal void GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value);
|
2017-04-17 17:16:17 +00:00
|
|
|
enum {
|
|
|
|
GB_TIMA_RUNNING = 0,
|
|
|
|
GB_TIMA_RELOADING = 1,
|
|
|
|
GB_TIMA_RELOADED = 2
|
|
|
|
};
|
2018-02-23 11:16:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define GB_SLEEP(gb, unit, state, cycles) do {\
|
2018-02-24 22:48:45 +00:00
|
|
|
(gb)->unit##_cycles -= (cycles) * __state_machine_divisor; \
|
2021-12-25 23:47:59 +00:00
|
|
|
if (unlikely((gb)->unit##_cycles <= 0)) {\
|
2018-02-23 11:16:05 +00:00
|
|
|
(gb)->unit##_state = state;\
|
|
|
|
return;\
|
|
|
|
unit##state:; \
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
2021-12-25 23:47:59 +00:00
|
|
|
#define GB_BATCHPOINT(gb, unit, state, cycles) do {\
|
|
|
|
unit##state:; \
|
|
|
|
if (likely(__state_machine_allow_batching && (gb)->unit##_cycles < (cycles * 2))) {\
|
|
|
|
(gb)->unit##_state = state;\
|
|
|
|
return;\
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GB_BATCHED_CYCLES(gb, unit) ((gb)->unit##_cycles / __state_machine_divisor)
|
|
|
|
|
2018-02-24 22:48:45 +00:00
|
|
|
#define GB_STATE_MACHINE(gb, unit, cycles, divisor) \
|
|
|
|
static const int __state_machine_divisor = divisor;\
|
2018-02-23 11:16:05 +00:00
|
|
|
(gb)->unit##_cycles += cycles; \
|
2020-05-16 20:27:17 +00:00
|
|
|
if ((gb)->unit##_cycles <= 0) {\
|
2018-02-23 11:16:05 +00:00
|
|
|
return;\
|
|
|
|
}\
|
|
|
|
switch ((gb)->unit##_state)
|
2017-04-17 17:16:17 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-25 23:47:59 +00:00
|
|
|
#define GB_BATCHABLE_STATE_MACHINE(gb, unit, cycles, divisor, allow_batching) \
|
|
|
|
const bool __state_machine_allow_batching = (allow_batching); \
|
|
|
|
GB_STATE_MACHINE(gb, unit, cycles, divisor)
|
|
|
|
|
2018-02-23 11:16:05 +00:00
|
|
|
#define GB_STATE(gb, unit, state) case state: goto unit##state
|
|
|
|
|
2018-02-23 13:33:44 +00:00
|
|
|
#define GB_UNIT(unit) int32_t unit##_cycles, unit##_state
|
2018-02-23 11:16:05 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
#endif /* timing_h */
|