Debian Codegraph @

src/engine/platform/sound/gb/timing.c

raw
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 GB_TAC_TRIGGER_BITS[] = {512, 8, 32, 128};
12
13 #define IR_DECAY 31500
14 #define IR_THRESHOLD 19900
15 #define IR_MAX IR_THRESHOLD * 2 + IR_DECAY
16
17 static void advance_tima_state_machine(GB_gameboy_t *gb)
18 {
19 if (gb->tima_reload_state == GB_TIMA_RELOADED) {
20 gb->tima_reload_state = GB_TIMA_RUNNING;
21 }
22 else if (gb->tima_reload_state == GB_TIMA_RELOADING) {
23 gb->io_registers[GB_IO_IF] |= 4;
24 gb->tima_reload_state = GB_TIMA_RELOADED;
25 }
26 }
27
28 static void increase_tima(GB_gameboy_t *gb)
29 {
30 gb->io_registers[GB_IO_TIMA]++;
31 if (gb->io_registers[GB_IO_TIMA] == 0) {
32 gb->io_registers[GB_IO_TIMA] = gb->io_registers[GB_IO_TMA];
33 gb->tima_reload_state = GB_TIMA_RELOADING;
34 }
35 }
36
37 static void GB_set_internal_div_counter(GB_gameboy_t *gb, uint16_t value)
38 {
39 /* TIMA increases when a specific high-bit becomes a low-bit. */
40 value &= INTERNAL_DIV_CYCLES - 1;
41 uint16_t triggers = gb->div_counter & ~value;
42 if ((gb->io_registers[GB_IO_TAC] & 4) && (triggers & GB_TAC_TRIGGER_BITS[gb->io_registers[GB_IO_TAC] & 3])) {
43 increase_tima(gb);
44 }
45
46 /* TODO: Can switching to double speed mode trigger an event? */
47 uint16_t apu_bit = gb->cgb_double_speed? 0x2000 : 0x1000;
48 if (triggers & apu_bit) {
49 GB_apu_run(gb);
50 GB_apu_div_event(gb);
51 }
52 else {
53 uint16_t secondary_triggers = ~gb->div_counter & value;
54 if (secondary_triggers & apu_bit) {
55 GB_apu_run(gb);
56 GB_apu_div_secondary_event(gb);
57 }
58 }
59 gb->div_counter = value;
60 }
61
62 static void GB_timers_run(GB_gameboy_t *gb, uint8_t cycles)
63 {
64 if (gb->stopped) {
65 if (CGB) {
66 gb->apu.apu_cycles += 4 << !gb->cgb_double_speed;
67 }
68 return;
69 }
70
71 GB_STATE_MACHINE(gb, div, cycles, 1) {
72 GB_STATE(gb, div, 1);
73 GB_STATE(gb, div, 2);
74 GB_STATE(gb, div, 3);
75 }
76
77 GB_set_internal_div_counter(gb, 0);
78 main:
79 GB_SLEEP(gb, div, 1, 3);
80 while (true) {
81 advance_tima_state_machine(gb);
82 GB_set_internal_div_counter(gb, gb->div_counter + 4);
83 gb->apu.apu_cycles += 4 << !gb->cgb_double_speed;
84 GB_SLEEP(gb, div, 2, 4);
85 }
86
87 /* Todo: This is ugly to allow compatibility with 0.11 save states. Fix me when breaking save compatibility */
88 {
89 div3:
90 /* Compensate for lack of prefetch emulation, as well as DIV's internal initial value */
91 GB_set_internal_div_counter(gb, 8);
92 goto main;
93 }
94 }
95
96 void GB_advance_cycles(GB_gameboy_t *gb, uint8_t cycles)
97 {
98 gb->apu.pcm_mask[0] = gb->apu.pcm_mask[1] = 0xFF; // Sort of hacky, but too many cross-component interactions to do it right
99
100 GB_timers_run(gb, cycles);
101
102 if (!gb->cgb_double_speed) {
103 cycles <<= 1;
104 }
105
106 gb->apu_output.sample_cycles += cycles;
107
108 GB_apu_run(gb);
109 }
110
111 /*
112 This glitch is based on the expected results of mooneye-gb rapid_toggle test.
113 This glitch happens because how TIMA is increased, see GB_set_internal_div_counter.
114 According to GiiBiiAdvance, GBC's behavior is different, but this was not tested or implemented.
115 */
116 void GB_emulate_timer_glitch(GB_gameboy_t *gb, uint8_t old_tac, uint8_t new_tac)
117 {
118 /* Glitch only happens when old_tac is enabled. */
119 if (!(old_tac & 4)) return;
120
121 unsigned old_clocks = GB_TAC_TRIGGER_BITS[old_tac & 3];
122 unsigned new_clocks = GB_TAC_TRIGGER_BITS[new_tac & 3];
123
124 /* The bit used for overflow testing must have been 1 */
125 if (gb->div_counter & old_clocks) {
126 /* And now either the timer must be disabled, or the new bit used for overflow testing be 0. */
127 if (!(new_tac & 4) || gb->div_counter & new_clocks) {
128 increase_tima(gb);
129 }
130 }
131 }