| 1 |
|
| #include "gb.h" |
| 2 |
|
| #ifdef _WIN32 |
| 3 |
|
| #ifndef _WIN32_WINNT |
| 4 |
|
| #define _WIN32_WINNT 0x0500 |
| 5 |
|
| #endif |
| 6 |
|
| #include <windows.h> |
| 7 |
|
| #else |
| 8 |
|
| #include <sys/time.h> |
| 9 |
|
| #endif |
| 10 |
|
| |
| 11 |
|
| static const unsigned TAC_TRIGGER_BITS[] = {512, 8, 32, 128}; |
| 12 |
|
| |
| 13 |
|
| #ifndef GB_DISABLE_TIMEKEEPING |
| 14 |
0 |
| static int64_t get_nanoseconds(void) |
| 15 |
|
| { |
| 16 |
|
| #ifndef _WIN32 |
| 17 |
|
| struct timeval now; |
| 18 |
0 |
| gettimeofday(&now, NULL); |
| 19 |
0 |
| return (now.tv_usec) * 1000 + now.tv_sec * 1000000000L; |
| 20 |
|
| #else |
| 21 |
|
| FILETIME time; |
| 22 |
|
| GetSystemTimeAsFileTime(&time); |
| 23 |
|
| return (((int64_t)time.dwHighDateTime << 32) | time.dwLowDateTime) * 100L; |
| 24 |
|
| #endif |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
0 |
| static void nsleep(uint64_t nanoseconds) |
| 28 |
|
| { |
| 29 |
|
| #ifndef _WIN32 |
| 30 |
0 |
| struct timespec sleep = {0, nanoseconds}; |
| 31 |
0 |
| nanosleep(&sleep, NULL); |
| 32 |
|
| #else |
| 33 |
|
| HANDLE timer; |
| 34 |
|
| LARGE_INTEGER time; |
| 35 |
|
| timer = CreateWaitableTimer(NULL, true, NULL); |
| 36 |
|
| time.QuadPart = -(nanoseconds / 100L); |
| 37 |
|
| SetWaitableTimer(timer, &time, 0, NULL, NULL, false); |
| 38 |
|
| WaitForSingleObject(timer, INFINITE); |
| 39 |
|
| CloseHandle(timer); |
| 40 |
|
| #endif |
| 41 |
0 |
| } |
| 42 |
|
| |
| 43 |
291 |
| bool GB_timing_sync_turbo(GB_gameboy_t *gb) |
| 44 |
|
| { |
| 45 |
|
| #ifndef GB_DISABLE_DEBUGGER |
| 46 |
|
| if (unlikely(gb->backstep_instructions)) return false; |
| 47 |
|
| #endif |
| 48 |
291 |
▶ | if (!gb->turbo_dont_skip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gb->turbo_dont_skip) { |
0 |
291 |
291 |
|
| 49 |
0 |
| int64_t nanoseconds = get_nanoseconds(); |
| 50 |
0 |
▶ | if (nanoseconds <= gb->last_render + 1000000000 / 60) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nanoseconds <= gb->last_render + 1000000000 / 60) { |
0 |
0 |
0 |
|
| 51 |
0 |
| return true; |
| 52 |
|
| } |
| 53 |
0 |
| gb->last_render = nanoseconds; |
| 54 |
|
| } |
| 55 |
291 |
| return false; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
491 |
| void GB_timing_sync(GB_gameboy_t *gb) |
| 59 |
|
| { |
| 60 |
|
| #ifndef GB_DISABLE_DEBUGGER |
| 61 |
|
| if (unlikely(gb->backstep_instructions)) return; |
| 62 |
|
| #endif |
| 63 |
|
| /* Prevent syncing if not enough time has passed.*/ |
| 64 |
491 |
▶ | if (gb->cycles_since_last_sync < LCDC_PERIOD / 3) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cycles_since_last_sync < LCDC_PERIOD / 3) return; |
200 |
291 |
491 |
|
| 65 |
|
| |
| 66 |
|
| unsigned target_clock_rate; |
| 67 |
291 |
▶ | if (gb->turbo) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->turbo) { |
291 |
0 |
291 |
|
| 68 |
291 |
▶ | if (gb->turbo_cap_multiplier) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->turbo_cap_multiplier) { |
0 |
291 |
291 |
|
| 69 |
0 |
| target_clock_rate = GB_get_clock_rate(gb) * gb->turbo_cap_multiplier; |
| 70 |
|
| } |
| 71 |
|
| else { |
| 72 |
291 |
| gb->cycles_since_last_sync = 0; |
| 73 |
291 |
▶ | if (gb->update_input_hint_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->update_input_hint_callback) { |
0 |
291 |
291 |
|
| 74 |
0 |
| gb->update_input_hint_callback(gb); |
| 75 |
|
| } |
| 76 |
291 |
| return; |
| 77 |
|
| } |
| 78 |
|
| } |
| 79 |
|
| else { |
| 80 |
0 |
| target_clock_rate = GB_get_clock_rate(gb); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
0 |
| uint64_t target_nanoseconds = gb->cycles_since_last_sync * 1000000000LL / 2 / target_clock_rate; /* / 2 because we use 8MHz units */ |
| 84 |
0 |
| int64_t nanoseconds = get_nanoseconds(); |
| 85 |
0 |
| int64_t time_to_sleep = target_nanoseconds + gb->last_sync - nanoseconds; |
| 86 |
0 |
▶ | if (time_to_sleep > 0 && time_to_sleep < LCDC_PERIOD * 1200000000LL / target_clock_rate) { // +20% to be more forgiving |
Branch: 2 branches
| Branch | True | False | Total |
| if (time_to_sleep > 0 && time_to_sleep < LCDC_PERIOD * 1200000000LL / target_clock_rate) { // +20% to be more forgiving |
0 |
0 |
0 |
| if (time_to_sleep > 0 && time_to_sleep < LCDC_PERIOD * 1200000000LL / target_clock_rate) { // +20% to be more forgiving |
0 |
0 |
0 |
|
| 87 |
0 |
| nsleep(time_to_sleep); |
| 88 |
0 |
| gb->last_sync += target_nanoseconds; |
| 89 |
|
| } |
| 90 |
|
| else { |
| 91 |
0 |
▶ | if (time_to_sleep < 0 && -time_to_sleep < LCDC_PERIOD * 1200000000LL / target_clock_rate) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (time_to_sleep < 0 && -time_to_sleep < LCDC_PERIOD * 1200000000LL / target_clock_rate) { |
0 |
0 |
0 |
| if (time_to_sleep < 0 && -time_to_sleep < LCDC_PERIOD * 1200000000LL / target_clock_rate) { |
0 |
0 |
0 |
|
| 92 |
|
| // We're running a bit too slow, but the difference is small enough, |
| 93 |
|
| // just skip this sync and let it even out |
| 94 |
0 |
| return; |
| 95 |
|
| } |
| 96 |
0 |
| gb->last_sync = nanoseconds; |
| 97 |
|
| } |
| 98 |
|
| |
| 99 |
0 |
| gb->cycles_since_last_sync = 0; |
| 100 |
0 |
▶ | if (gb->update_input_hint_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->update_input_hint_callback) { |
0 |
0 |
0 |
|
| 101 |
0 |
| gb->update_input_hint_callback(gb); |
| 102 |
|
| } |
| 103 |
|
| } |
| 104 |
|
| #else |
| 105 |
|
| |
| 106 |
|
| bool GB_timing_sync_turbo(GB_gameboy_t *gb) |
| 107 |
|
| { |
| 108 |
|
| return false; |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
|
| void GB_timing_sync(GB_gameboy_t *gb) |
| 112 |
|
| { |
| 113 |
|
| #ifndef GB_DISABLE_DEBUGGER |
| 114 |
|
| if (unlikely(gb->backstep_instructions)) return; |
| 115 |
|
| #endif |
| 116 |
|
| if (gb->cycles_since_last_sync < LCDC_PERIOD / 3) return; |
| 117 |
|
| gb->cycles_since_last_sync = 0; |
| 118 |
|
| |
| 119 |
|
| gb->cycles_since_last_sync = 0; |
| 120 |
|
| if (gb->update_input_hint_callback) { |
| 121 |
|
| gb->update_input_hint_callback(gb); |
| 122 |
|
| } |
| 123 |
|
| return; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| #endif |
| 127 |
|
| |
| 128 |
|
| #define IR_DECAY 31500 |
| 129 |
|
| #define IR_WARMUP 19900 |
| 130 |
|
| #define IR_THRESHOLD 240 |
| 131 |
|
| #define IR_MAX IR_THRESHOLD * 2 + IR_DECAY + 268 |
| 132 |
|
| |
| 133 |
5043155 |
| static void ir_run(GB_gameboy_t *gb, uint32_t cycles) |
| 134 |
|
| { |
| 135 |
|
| /* TODO: the way this thing works makes the CGB IR port behave inaccurately when used together with HUC1/3 IR ports*/ |
| 136 |
5043155 |
▶ | if ((gb->model > GB_MODEL_CGB_E || !gb->cgb_mode) && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) return; |
Branch: 4 branches
| Branch | True | False | Total |
| if ((gb->model > GB_MODEL_CGB_E || !gb->cgb_mode) && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) return; |
5043155 |
0 |
5043155 |
| if ((gb->model > GB_MODEL_CGB_E || !gb->cgb_mode) && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) return; |
522091 |
4521064 |
5043155 |
| if ((gb->model > GB_MODEL_CGB_E || !gb->cgb_mode) && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) return; |
522091 |
0 |
522091 |
| if ((gb->model > GB_MODEL_CGB_E || !gb->cgb_mode) && gb->cartridge_type->mbc_type != GB_HUC1 && gb->cartridge_type->mbc_type != GB_HUC3) return; |
522091 |
0 |
522091 |
|
| 137 |
13563192 |
| bool is_sensing = (gb->io_registers[GB_IO_RP] & 0xC0) == 0xC0 || |
| 138 |
9042128 |
▶ | (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) || |
Branch: 3 branches
| Branch | True | False | Total |
| (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) || |
4521064 |
0 |
4521064 |
| (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) || |
0 |
4521064 |
4521064 |
| (gb->cartridge_type->mbc_type == GB_HUC1 && gb->huc1.ir_mode) || |
0 |
0 |
0 |
|
| 139 |
4521064 |
▶ | (gb->cartridge_type->mbc_type == GB_HUC3 && gb->huc3.mode == 0xE); |
Branch: 2 branches
| Branch | True | False | Total |
| (gb->cartridge_type->mbc_type == GB_HUC3 && gb->huc3.mode == 0xE); |
0 |
4521064 |
4521064 |
| (gb->cartridge_type->mbc_type == GB_HUC3 && gb->huc3.mode == 0xE); |
0 |
0 |
0 |
|
| 140 |
4521064 |
▶ | if (is_sensing && (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (is_sensing && (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1))) { |
0 |
4521064 |
4521064 |
| if (is_sensing && (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1))) { |
0 |
0 |
0 |
| if (is_sensing && (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1))) { |
0 |
0 |
0 |
| if (is_sensing && (gb->infrared_input || gb->cart_ir || (gb->io_registers[GB_IO_RP] & 1))) { |
0 |
0 |
0 |
|
| 141 |
0 |
| gb->ir_sensor += cycles; |
| 142 |
0 |
▶ | if (gb->ir_sensor > IR_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->ir_sensor > IR_MAX) { |
0 |
0 |
0 |
|
| 143 |
0 |
| gb->ir_sensor = IR_MAX; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
0 |
▶ | gb->effective_ir_input = gb->ir_sensor >= IR_WARMUP + IR_THRESHOLD && gb->ir_sensor <= IR_WARMUP + IR_THRESHOLD + IR_DECAY; |
Branch: 2 branches
| Branch | True | False | Total |
| gb->effective_ir_input = gb->ir_sensor >= IR_WARMUP + IR_THRESHOLD && gb->ir_sensor <= IR_WARMUP + IR_THRESHOLD + IR_DECAY; |
0 |
0 |
0 |
| gb->effective_ir_input = gb->ir_sensor >= IR_WARMUP + IR_THRESHOLD && gb->ir_sensor <= IR_WARMUP + IR_THRESHOLD + IR_DECAY; |
0 |
0 |
0 |
|
| 147 |
|
| } |
| 148 |
|
| else { |
| 149 |
4521064 |
▶ | unsigned target = is_sensing? IR_WARMUP : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned target = is_sensing? IR_WARMUP : 0; |
0 |
4521064 |
4521064 |
|
| 150 |
4521064 |
▶ | if (gb->ir_sensor < target) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->ir_sensor < target) { |
0 |
4521064 |
4521064 |
|
| 151 |
0 |
| gb->ir_sensor += cycles; |
| 152 |
|
| } |
| 153 |
4521064 |
▶ | else if (gb->ir_sensor <= target + cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (gb->ir_sensor <= target + cycles) { |
4521064 |
0 |
4521064 |
|
| 154 |
4521064 |
| gb->ir_sensor = target; |
| 155 |
|
| } |
| 156 |
|
| else { |
| 157 |
0 |
| gb->ir_sensor -= cycles; |
| 158 |
|
| } |
| 159 |
4521064 |
| gb->effective_ir_input = false; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
5045279 |
| static void advance_tima_state_machine(GB_gameboy_t *gb) |
| 165 |
|
| { |
| 166 |
5045279 |
▶ | if (gb->tima_reload_state == GB_TIMA_RELOADED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->tima_reload_state == GB_TIMA_RELOADED) { |
0 |
5045279 |
5045279 |
|
| 167 |
0 |
| gb->tima_reload_state = GB_TIMA_RUNNING; |
| 168 |
|
| } |
| 169 |
5045279 |
▶ | else if (gb->tima_reload_state == GB_TIMA_RELOADING) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (gb->tima_reload_state == GB_TIMA_RELOADING) { |
0 |
5045279 |
5045279 |
|
| 170 |
0 |
| gb->io_registers[GB_IO_IF] |= 4; |
| 171 |
0 |
| gb->tima_reload_state = GB_TIMA_RELOADED; |
| 172 |
|
| } |
| 173 |
5045279 |
| } |
| 174 |
|
| |
| 175 |
0 |
| static void increase_tima(GB_gameboy_t *gb) |
| 176 |
|
| { |
| 177 |
0 |
| gb->io_registers[GB_IO_TIMA]++; |
| 178 |
0 |
▶ | if (gb->io_registers[GB_IO_TIMA] == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->io_registers[GB_IO_TIMA] == 0) { |
0 |
0 |
0 |
|
| 179 |
0 |
| gb->io_registers[GB_IO_TIMA] = gb->io_registers[GB_IO_TMA]; |
| 180 |
0 |
| gb->tima_reload_state = GB_TIMA_RELOADING; |
| 181 |
|
| } |
| 182 |
0 |
| } |
| 183 |
|
| |
| 184 |
78826 |
| void GB_serial_master_edge(GB_gameboy_t *gb) |
| 185 |
|
| { |
| 186 |
78826 |
▶ | if (gb->printer_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->printer_callback) { |
0 |
78826 |
78826 |
|
| 187 |
0 |
| unsigned ticks = 1 << gb->serial_mask; |
| 188 |
0 |
▶ | if (unlikely((gb->printer.command_state || gb->printer.bits_received))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (unlikely((gb->printer.command_state || gb->printer.bits_received))) { |
0 |
0 |
0 |
| if (unlikely((gb->printer.command_state || gb->printer.bits_received))) { |
0 |
0 |
0 |
|
| 189 |
0 |
| gb->printer.idle_time +=ticks; |
| 190 |
|
| } |
| 191 |
0 |
▶ | if (unlikely(gb->printer.time_remaining)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->printer.time_remaining)) { |
0 |
0 |
0 |
|
| 192 |
0 |
▶ | if (gb->printer.time_remaining <= ticks) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->printer.time_remaining <= ticks) { |
0 |
0 |
0 |
|
| 193 |
0 |
| gb->printer.time_remaining = 0; |
| 194 |
0 |
▶ | if (gb->printer_done_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->printer_done_callback) { |
0 |
0 |
0 |
|
| 195 |
0 |
| gb->printer_done_callback(gb); |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
|
| else { |
| 199 |
0 |
| gb->printer.time_remaining -= ticks; |
| 200 |
|
| } |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
78826 |
| gb->serial_master_clock ^= true; |
| 205 |
|
| |
| 206 |
78826 |
▶ | if (!gb->serial_master_clock && (gb->io_registers[GB_IO_SC] & 0x81) == 0x81) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!gb->serial_master_clock && (gb->io_registers[GB_IO_SC] & 0x81) == 0x81) { |
39411 |
39415 |
78826 |
| if (!gb->serial_master_clock && (gb->io_registers[GB_IO_SC] & 0x81) == 0x81) { |
0 |
39411 |
39411 |
|
| 207 |
0 |
| gb->serial_count++; |
| 208 |
0 |
▶ | if (gb->serial_count == 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->serial_count == 8) { |
0 |
0 |
0 |
|
| 209 |
0 |
| gb->serial_count = 0; |
| 210 |
0 |
| gb->io_registers[GB_IO_SC] &= ~0x80; |
| 211 |
0 |
| gb->io_registers[GB_IO_IF] |= 8; |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
0 |
| gb->io_registers[GB_IO_SB] <<= 1; |
| 215 |
|
| |
| 216 |
0 |
▶ | if (gb->serial_transfer_bit_end_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->serial_transfer_bit_end_callback) { |
0 |
0 |
0 |
|
| 217 |
0 |
| gb->io_registers[GB_IO_SB] |= gb->serial_transfer_bit_end_callback(gb); |
| 218 |
|
| } |
| 219 |
|
| else { |
| 220 |
0 |
| gb->io_registers[GB_IO_SB] |= 1; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
0 |
▶ | if (gb->serial_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->serial_count) { |
0 |
0 |
0 |
|
| 224 |
|
| /* Still more bits to send */ |
| 225 |
0 |
▶ | if (gb->serial_transfer_bit_start_callback) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->serial_transfer_bit_start_callback) { |
0 |
0 |
0 |
|
| 226 |
0 |
| gb->serial_transfer_bit_start_callback(gb, gb->io_registers[GB_IO_SB] & 0x80); |
| 227 |
|
| } |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| } |
| 231 |
78826 |
| } |
| 232 |
|
| |
| 233 |
|
| |
| 234 |
5045295 |
| void GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value) |
| 235 |
|
| { |
| 236 |
|
| /* TIMA increases when a specific high-bit becomes a low-bit. */ |
| 237 |
5045295 |
| uint16_t triggers = gb->div_counter & ~value; |
| 238 |
5045295 |
▶ | if ((gb->io_registers[GB_IO_TAC] & 4) && unlikely(triggers & TAC_TRIGGER_BITS[gb->io_registers[GB_IO_TAC] & 3])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((gb->io_registers[GB_IO_TAC] & 4) && unlikely(triggers & TAC_TRIGGER_BITS[gb->io_registers[GB_IO_TAC] & 3])) { |
0 |
5045295 |
5045295 |
| if ((gb->io_registers[GB_IO_TAC] & 4) && unlikely(triggers & TAC_TRIGGER_BITS[gb->io_registers[GB_IO_TAC] & 3])) { |
0 |
0 |
0 |
|
| 239 |
0 |
| increase_tima(gb); |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
5045295 |
▶ | if (unlikely(triggers & gb->serial_mask)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(triggers & gb->serial_mask)) { |
78826 |
4966469 |
5045295 |
|
| 243 |
78826 |
| GB_serial_master_edge(gb); |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
|
| /* TODO: Can switching to double speed mode trigger an event? */ |
| 247 |
5045295 |
▶ | uint16_t apu_bit = gb->cgb_double_speed? 0x2000 : 0x1000; |
Branch: 1 branch
| Branch | True | False | Total |
| uint16_t apu_bit = gb->cgb_double_speed? 0x2000 : 0x1000; |
0 |
5045295 |
5045295 |
|
| 248 |
5045295 |
▶ | if (unlikely(triggers & apu_bit)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(triggers & apu_bit)) { |
2459 |
5042836 |
5045295 |
|
| 249 |
2459 |
| GB_apu_div_event(gb); |
| 250 |
|
| } |
| 251 |
|
| else { |
| 252 |
5042836 |
| uint16_t secondary_triggers = ~gb->div_counter & value; |
| 253 |
5042836 |
▶ | if (unlikely(secondary_triggers & apu_bit)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(secondary_triggers & apu_bit)) { |
2462 |
5040374 |
5042836 |
|
| 254 |
2462 |
| GB_apu_div_secondary_event(gb); |
| 255 |
|
| } |
| 256 |
|
| } |
| 257 |
5045295 |
| gb->div_counter = value; |
| 258 |
5045295 |
| } |
| 259 |
|
| |
| 260 |
5043155 |
| static void timers_run(GB_gameboy_t *gb, uint8_t cycles) |
| 261 |
|
| { |
| 262 |
5043155 |
▶ | if (gb->stopped) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->stopped) { |
0 |
5043155 |
5043155 |
|
| 263 |
0 |
▶ | if (GB_is_cgb(gb)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GB_is_cgb(gb)) { |
0 |
0 |
0 |
|
| 264 |
0 |
| gb->apu.apu_cycles += 1 << !gb->cgb_double_speed; |
| 265 |
|
| } |
| 266 |
0 |
| gb->apu_output.sample_cycles += (gb->apu_output.sample_rate << !gb->cgb_double_speed) << 1; |
| 267 |
0 |
| return; |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
5043155 |
▶ | GB_STATE_MACHINE(gb, div, cycles, 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| GB_STATE_MACHINE(gb, div, cycles, 1) { |
31 |
5043124 |
5043155 |
|
| 271 |
0 |
| GB_STATE(gb, div, 1); |
| 272 |
5043115 |
| GB_STATE(gb, div, 2); |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
9 |
▶ | GB_SLEEP(gb, div, 1, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| GB_SLEEP(gb, div, 1, 3); |
0 |
9 |
9 |
|
| 276 |
|
| while (true) { |
| 277 |
5045279 |
| advance_tima_state_machine(gb); |
| 278 |
5045279 |
| GB_set_internal_div_counter(gb, gb->div_counter + 4); |
| 279 |
5045279 |
| gb->apu.apu_cycles += 1 << !gb->cgb_double_speed; |
| 280 |
5045279 |
| gb->apu_output.sample_cycles += (gb->apu_output.sample_rate << !gb->cgb_double_speed) << 1; |
| 281 |
10088394 |
▶ | GB_SLEEP(gb, div, 2, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| GB_SLEEP(gb, div, 2, 4); |
5043124 |
2155 |
5045279 |
|
| 282 |
5045270 |
▶ | if (unlikely(gb->apu.pending_envelope_tick)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->apu.pending_envelope_tick)) { |
0 |
5045270 |
5045270 |
|
| 283 |
0 |
| GB_apu_delayed_envelope_tick(gb); |
| 284 |
|
| } |
| 285 |
|
| } |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
0 |
| void GB_set_rtc_mode(GB_gameboy_t *gb, GB_rtc_mode_t mode) |
| 289 |
|
| { |
| 290 |
0 |
▶ | if (gb->rtc_mode != mode) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_mode != mode) { |
0 |
0 |
0 |
|
| 291 |
0 |
| gb->rtc_mode = mode; |
| 292 |
0 |
| gb->rtc_cycles = 0; |
| 293 |
0 |
| gb->last_rtc_second = time(NULL); |
| 294 |
|
| } |
| 295 |
0 |
| } |
| 296 |
|
| |
| 297 |
|
| |
| 298 |
0 |
| void GB_set_rtc_multiplier(GB_gameboy_t *gb, double multiplier) |
| 299 |
|
| { |
| 300 |
0 |
▶ | if (multiplier == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (multiplier == 1) { |
0 |
0 |
0 |
|
| 301 |
0 |
| gb->rtc_second_length = 0; |
| 302 |
0 |
| return; |
| 303 |
|
| } |
| 304 |
|
| |
| 305 |
0 |
| gb->rtc_second_length = GB_get_unmultiplied_clock_rate(gb) * 2 * multiplier; |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
0 |
| void GB_rtc_set_time(GB_gameboy_t *gb, uint64_t current_time) |
| 309 |
|
| { |
| 310 |
0 |
▶ | if (gb->cartridge_type->mbc_type == GB_HUC3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_HUC3) { |
0 |
0 |
0 |
|
| 311 |
0 |
▶ | while (gb->last_rtc_second / 60 < current_time / 60) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (gb->last_rtc_second / 60 < current_time / 60) { |
0 |
0 |
0 |
|
| 312 |
0 |
| gb->last_rtc_second += 60; |
| 313 |
0 |
| gb->huc3.minutes++; |
| 314 |
0 |
▶ | if (gb->huc3.minutes == 60 * 24) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->huc3.minutes == 60 * 24) { |
0 |
0 |
0 |
|
| 315 |
0 |
| gb->huc3.days++; |
| 316 |
0 |
| gb->huc3.minutes = 0; |
| 317 |
|
| } |
| 318 |
|
| } |
| 319 |
0 |
| return; |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
0 |
| bool running = false; |
| 323 |
0 |
▶ | if (gb->cartridge_type->mbc_type == GB_TPP1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_TPP1) { |
0 |
0 |
0 |
|
| 324 |
0 |
| running = gb->tpp1_mr4 & 0x4; |
| 325 |
|
| } |
| 326 |
|
| else { |
| 327 |
0 |
| running = (gb->rtc_real.high & 0x40) == 0; |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
0 |
▶ | if (!running) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!running) return; |
0 |
0 |
0 |
|
| 331 |
|
| |
| 332 |
0 |
▶ | while (gb->last_rtc_second + 60 * 60 * 24 < current_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (gb->last_rtc_second + 60 * 60 * 24 < current_time) { |
0 |
0 |
0 |
|
| 333 |
0 |
| gb->last_rtc_second += 60 * 60 * 24; |
| 334 |
0 |
▶ | if (gb->cartridge_type->mbc_type == GB_TPP1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_TPP1) { |
0 |
0 |
0 |
|
| 335 |
0 |
▶ | if (++gb->rtc_real.tpp1.weekday == 7) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.tpp1.weekday == 7) { |
0 |
0 |
0 |
|
| 336 |
0 |
| gb->rtc_real.tpp1.weekday = 0; |
| 337 |
0 |
▶ | if (++gb->rtc_real.tpp1.weeks == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.tpp1.weeks == 0) { |
0 |
0 |
0 |
|
| 338 |
0 |
| gb->tpp1_mr4 |= 8; /* Overflow bit */ |
| 339 |
|
| } |
| 340 |
|
| } |
| 341 |
|
| } |
| 342 |
0 |
▶ | else if (++gb->rtc_real.days == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (++gb->rtc_real.days == 0) { |
0 |
0 |
0 |
|
| 343 |
0 |
▶ | if (gb->rtc_real.high & 1) { /* Bit 8 of days*/ |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_real.high & 1) { /* Bit 8 of days*/ |
0 |
0 |
0 |
|
| 344 |
0 |
| gb->rtc_real.high |= 0x80; /* Overflow bit */ |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
0 |
| gb->rtc_real.high ^= 1; |
| 348 |
|
| } |
| 349 |
|
| } |
| 350 |
|
| |
| 351 |
0 |
▶ | while (gb->last_rtc_second < current_time) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (gb->last_rtc_second < current_time) { |
0 |
0 |
0 |
|
| 352 |
0 |
| gb->last_rtc_second++; |
| 353 |
0 |
▶ | if (++gb->rtc_real.seconds != 60) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.seconds != 60) continue; |
0 |
0 |
0 |
|
| 354 |
0 |
| gb->rtc_real.seconds = 0; |
| 355 |
|
| |
| 356 |
0 |
▶ | if (++gb->rtc_real.minutes != 60) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.minutes != 60) continue; |
0 |
0 |
0 |
|
| 357 |
0 |
| gb->rtc_real.minutes = 0; |
| 358 |
|
| |
| 359 |
0 |
▶ | if (gb->cartridge_type->mbc_type == GB_TPP1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type == GB_TPP1) { |
0 |
0 |
0 |
|
| 360 |
0 |
▶ | if (++gb->rtc_real.tpp1.hours != 24) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.tpp1.hours != 24) continue; |
0 |
0 |
0 |
|
| 361 |
0 |
| gb->rtc_real.tpp1.hours = 0; |
| 362 |
0 |
▶ | if (++gb->rtc_real.tpp1.weekday != 7) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.tpp1.weekday != 7) continue; |
0 |
0 |
0 |
|
| 363 |
0 |
| gb->rtc_real.tpp1.weekday = 0; |
| 364 |
0 |
▶ | if (++gb->rtc_real.tpp1.weeks == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.tpp1.weeks == 0) { |
0 |
0 |
0 |
|
| 365 |
0 |
| gb->tpp1_mr4 |= 8; /* Overflow bit */ |
| 366 |
|
| } |
| 367 |
|
| } |
| 368 |
|
| else { |
| 369 |
0 |
▶ | if (++gb->rtc_real.hours != 24) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.hours != 24) continue; |
0 |
0 |
0 |
|
| 370 |
0 |
| gb->rtc_real.hours = 0; |
| 371 |
|
| |
| 372 |
0 |
▶ | if (++gb->rtc_real.days != 0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (++gb->rtc_real.days != 0) continue; |
0 |
0 |
0 |
|
| 373 |
|
| |
| 374 |
0 |
▶ | if (gb->rtc_real.high & 1) { /* Bit 8 of days*/ |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_real.high & 1) { /* Bit 8 of days*/ |
0 |
0 |
0 |
|
| 375 |
0 |
| gb->rtc_real.high |= 0x80; /* Overflow bit */ |
| 376 |
|
| } |
| 377 |
|
| |
| 378 |
0 |
| gb->rtc_real.high ^= 1; |
| 379 |
|
| } |
| 380 |
|
| } |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
5043155 |
| static void rtc_run(GB_gameboy_t *gb, uint8_t cycles) |
| 384 |
|
| { |
| 385 |
5043155 |
▶ | if (likely(gb->cartridge_type->mbc_type != GB_HUC3 && !gb->cartridge_type->has_rtc)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (likely(gb->cartridge_type->mbc_type != GB_HUC3 && !gb->cartridge_type->has_rtc)) return; |
5043155 |
0 |
5043155 |
| if (likely(gb->cartridge_type->mbc_type != GB_HUC3 && !gb->cartridge_type->has_rtc)) return; |
5043155 |
0 |
5043155 |
|
| 386 |
0 |
| gb->rtc_cycles += cycles; |
| 387 |
0 |
| time_t current_time = 0; |
| 388 |
0 |
▶ | uint32_t rtc_second_length = unlikely(gb->rtc_second_length)? gb->rtc_second_length : GB_get_unmultiplied_clock_rate(gb) * 2; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t rtc_second_length = unlikely(gb->rtc_second_length)? gb->rtc_second_length : GB_get_unmultiplied_clock_rate(gb) * 2; |
0 |
0 |
0 |
|
| 389 |
|
| |
| 390 |
0 |
| switch (gb->rtc_mode) { |
| 391 |
0 |
| case GB_RTC_MODE_SYNC_TO_HOST: |
| 392 |
|
| // Sync in a 1/32s resolution |
| 393 |
0 |
▶ | if (gb->rtc_cycles < GB_get_unmultiplied_clock_rate(gb) / 16) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_cycles < GB_get_unmultiplied_clock_rate(gb) / 16) return; |
0 |
0 |
0 |
|
| 394 |
0 |
| gb->rtc_cycles -= GB_get_unmultiplied_clock_rate(gb) / 16; |
| 395 |
0 |
| current_time = time(NULL); |
| 396 |
0 |
| break; |
| 397 |
0 |
| case GB_RTC_MODE_ACCURATE: |
| 398 |
0 |
▶ | if (gb->cartridge_type->mbc_type != GB_HUC3 && (gb->rtc_real.high & 0x40)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (gb->cartridge_type->mbc_type != GB_HUC3 && (gb->rtc_real.high & 0x40)) { |
0 |
0 |
0 |
| if (gb->cartridge_type->mbc_type != GB_HUC3 && (gb->rtc_real.high & 0x40)) { |
0 |
0 |
0 |
|
| 399 |
0 |
| gb->rtc_cycles -= cycles; |
| 400 |
0 |
| return; |
| 401 |
|
| } |
| 402 |
0 |
▶ | if (gb->rtc_cycles < rtc_second_length) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->rtc_cycles < rtc_second_length) return; |
0 |
0 |
0 |
|
| 403 |
0 |
| gb->rtc_cycles -= rtc_second_length; |
| 404 |
0 |
| current_time = gb->last_rtc_second + 1; |
| 405 |
0 |
| break; |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
0 |
| GB_rtc_set_time(gb, current_time); |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
5043155 |
| static void camera_run(GB_gameboy_t *gb, uint8_t cycles) |
| 412 |
|
| { |
| 413 |
|
| /* Do we have a camera? */ |
| 414 |
5043155 |
▶ | if (likely(gb->cartridge_type->mbc_type != GB_CAMERA)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(gb->cartridge_type->mbc_type != GB_CAMERA)) return; |
5043155 |
0 |
5043155 |
|
| 415 |
|
| |
| 416 |
|
| /* The camera mapper uses the PHI pin to clock itself */ |
| 417 |
|
| |
| 418 |
|
| /* PHI does not run in halt nor stop mode */ |
| 419 |
0 |
▶ | if (unlikely(gb->halted || gb->stopped)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (unlikely(gb->halted || gb->stopped)) return; |
0 |
0 |
0 |
| if (unlikely(gb->halted || gb->stopped)) return; |
0 |
0 |
0 |
|
| 420 |
|
| |
| 421 |
|
| /* Only every other PHI is used (as the camera wants a 512KiHz clock) */ |
| 422 |
0 |
| gb->camera_alignment += cycles; |
| 423 |
|
| |
| 424 |
|
| /* Is the camera processing an image? */ |
| 425 |
0 |
▶ | if (likely(gb->camera_countdown == 0)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(gb->camera_countdown == 0)) return; |
0 |
0 |
0 |
|
| 426 |
|
| |
| 427 |
0 |
| gb->camera_countdown -= cycles; |
| 428 |
0 |
▶ | if (gb->camera_countdown <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->camera_countdown <= 0) { |
0 |
0 |
0 |
|
| 429 |
0 |
| gb->camera_countdown = 0; |
| 430 |
0 |
| GB_camera_updated(gb); |
| 431 |
|
| } |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| |
| 435 |
5043155 |
| void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles) |
| 436 |
|
| { |
| 437 |
5043155 |
▶ | if (unlikely(gb->speed_switch_countdown)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->speed_switch_countdown)) { |
0 |
5043155 |
5043155 |
|
| 438 |
0 |
▶ | if (gb->speed_switch_countdown == cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->speed_switch_countdown == cycles) { |
0 |
0 |
0 |
|
| 439 |
0 |
| gb->cgb_double_speed ^= true; |
| 440 |
0 |
| gb->speed_switch_countdown = 0; |
| 441 |
|
| } |
| 442 |
0 |
▶ | else if (gb->speed_switch_countdown > cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (gb->speed_switch_countdown > cycles) { |
0 |
0 |
0 |
|
| 443 |
0 |
| gb->speed_switch_countdown -= cycles; |
| 444 |
|
| } |
| 445 |
|
| else { |
| 446 |
0 |
| uint8_t old_cycles = gb->speed_switch_countdown; |
| 447 |
0 |
| cycles -= old_cycles; |
| 448 |
0 |
| gb->speed_switch_countdown = 0; |
| 449 |
0 |
| GB_advance_cycles(gb, old_cycles); |
| 450 |
0 |
| gb->cgb_double_speed ^= true; |
| 451 |
|
| } |
| 452 |
|
| } |
| 453 |
5043155 |
| gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF; // Sort of hacky, but too many cross-component interactions to do it right |
| 454 |
|
| // Affected by speed boost |
| 455 |
5043155 |
| gb->dma_cycles = cycles; |
| 456 |
|
| |
| 457 |
5043155 |
| timers_run(gb, cycles); |
| 458 |
5043155 |
| camera_run(gb, cycles); |
| 459 |
|
| |
| 460 |
5043155 |
▶ | if (unlikely(gb->speed_switch_halt_countdown)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->speed_switch_halt_countdown)) { |
0 |
5043155 |
5043155 |
|
| 461 |
0 |
| gb->speed_switch_halt_countdown -= cycles; |
| 462 |
0 |
▶ | if (gb->speed_switch_halt_countdown <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->speed_switch_halt_countdown <= 0) { |
0 |
0 |
0 |
|
| 463 |
0 |
| gb->speed_switch_halt_countdown = 0; |
| 464 |
0 |
| gb->halted = false; |
| 465 |
|
| } |
| 466 |
|
| } |
| 467 |
|
| |
| 468 |
|
| #ifndef GB_DISABLE_DEBUGGER |
| 469 |
|
| gb->debugger_ticks += cycles; |
| 470 |
|
| #endif |
| 471 |
|
| |
| 472 |
5043155 |
▶ | if (gb->speed_switch_freeze) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->speed_switch_freeze) { |
0 |
5043155 |
5043155 |
|
| 473 |
0 |
▶ | if (gb->speed_switch_freeze >= cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->speed_switch_freeze >= cycles) { |
0 |
0 |
0 |
|
| 474 |
0 |
| gb->speed_switch_freeze -= cycles; |
| 475 |
0 |
| return; |
| 476 |
|
| } |
| 477 |
0 |
| cycles -= gb->speed_switch_freeze; |
| 478 |
0 |
| gb->speed_switch_freeze = 0; |
| 479 |
|
| } |
| 480 |
|
| |
| 481 |
5043155 |
▶ | if (unlikely(!gb->cgb_double_speed)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(!gb->cgb_double_speed)) { |
5043155 |
0 |
5043155 |
|
| 482 |
5043155 |
| cycles <<= 1; |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
|
| #ifndef GB_DISABLE_DEBUGGER |
| 486 |
|
| gb->absolute_debugger_ticks += cycles; |
| 487 |
|
| *((gb->halted || gb->stopped)? &gb->current_frame_idle_cycles : &gb->current_frame_busy_cycles) += cycles; |
| 488 |
|
| *((gb->halted || gb->stopped)? &gb->current_second_idle_cycles : &gb->current_second_busy_cycles) += cycles; |
| 489 |
|
| #endif |
| 490 |
|
| |
| 491 |
|
| // Not affected by speed boost |
| 492 |
5043155 |
▶ | if (likely(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (likely(gb->io_registers[GB_IO_LCDC] & GB_LCDC_ENABLE)) { |
3872376 |
1170779 |
5043155 |
|
| 493 |
3872376 |
| gb->double_speed_alignment += cycles; |
| 494 |
|
| } |
| 495 |
5043155 |
| gb->cycles_since_last_sync += cycles; |
| 496 |
5043155 |
| gb->cycles_since_run += cycles; |
| 497 |
|
| |
| 498 |
5043155 |
| gb->rumble_on_cycles += gb->rumble_strength & 3; |
| 499 |
5043155 |
| gb->rumble_off_cycles += (gb->rumble_strength & 3) ^ 3; |
| 500 |
|
| |
| 501 |
5043155 |
▶ | if (unlikely(gb->data_bus_decay_countdown)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(gb->data_bus_decay_countdown)) { |
5024324 |
18831 |
5043155 |
|
| 502 |
5024324 |
▶ | if (gb->data_bus_decay_countdown <= cycles) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->data_bus_decay_countdown <= cycles) { |
1574310 |
3450014 |
5024324 |
|
| 503 |
1574310 |
| gb->data_bus_decay_countdown = 0; |
| 504 |
1574310 |
| gb->data_bus = 0xFF; |
| 505 |
|
| } |
| 506 |
|
| else { |
| 507 |
3450014 |
| gb->data_bus_decay_countdown -= cycles; |
| 508 |
|
| } |
| 509 |
|
| } |
| 510 |
|
| |
| 511 |
5043155 |
| GB_joypad_run(gb, cycles); |
| 512 |
5043155 |
| GB_apu_run(gb, false); |
| 513 |
5043155 |
| GB_display_run(gb, cycles, false); |
| 514 |
5043155 |
▶ | if (unlikely(!gb->stopped)) { // TODO: Verify what happens in STOP mode |
Branch: 1 branch
| Branch | True | False | Total |
| if (unlikely(!gb->stopped)) { // TODO: Verify what happens in STOP mode |
5043155 |
0 |
5043155 |
|
| 515 |
5043155 |
| GB_dma_run(gb); |
| 516 |
|
| } |
| 517 |
5043155 |
| ir_run(gb, cycles); |
| 518 |
5043155 |
| rtc_run(gb, cycles); |
| 519 |
|
| } |
| 520 |
|
| |
| 521 |
|
| /* |
| 522 |
|
| This glitch is based on the expected results of mooneye-gb rapid_toggle test. |
| 523 |
|
| This glitch happens because how TIMA is increased, see GB_set_internal_div_counter. |
| 524 |
|
| According to GiiBiiAdvance, GBC's behavior is different, but this was not tested or implemented. |
| 525 |
|
| */ |
| 526 |
0 |
| void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac) |
| 527 |
|
| { |
| 528 |
|
| /* Glitch only happens when old_tac is enabled. */ |
| 529 |
0 |
▶ | if (!(old_tac & 4)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(old_tac & 4)) return; |
0 |
0 |
0 |
|
| 530 |
|
| |
| 531 |
0 |
| unsigned old_clocks = TAC_TRIGGER_BITS[old_tac & 3]; |
| 532 |
0 |
| unsigned new_clocks = TAC_TRIGGER_BITS[new_tac & 3]; |
| 533 |
|
| |
| 534 |
|
| /* The bit used for overflow testing must have been 1 */ |
| 535 |
0 |
▶ | if (gb->div_counter & old_clocks) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gb->div_counter & old_clocks) { |
0 |
0 |
0 |
|
| 536 |
|
| /* And now either the timer must be disabled, or the new bit used for overflow testing be 0. */ |
| 537 |
0 |
▶ | if (!(new_tac & 4) || !(gb->div_counter & new_clocks)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(new_tac & 4) || !(gb->div_counter & new_clocks)) { |
0 |
0 |
0 |
| if (!(new_tac & 4) || !(gb->div_counter & new_clocks)) { |
0 |
0 |
0 |
|
| 538 |
0 |
| increase_tima(gb); |
| 539 |
|
| } |
| 540 |
|
| } |
| 541 |
|
| } |