diff --git a/Core/apu.c b/Core/apu.c index bd00d49..3706599 100644 --- a/Core/apu.c +++ b/Core/apu.c @@ -710,7 +710,7 @@ uint8_t GB_apu_read(GB_gameboy_t *gb, uint8_t reg) return gb->io_registers[reg] | read_mask[reg - GB_IO_NR10]; } -static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb, uint8_t nr43) +static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb) { uint16_t effective_counter = gb->apu.noise_channel.counter; /* Ladies and gentlemen, I present you the holy grail glitch of revision detection! */ @@ -720,14 +720,7 @@ static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb, uint8_t nr43 #if 0 case GB_MODEL_CGB_B: if (effective_counter & 8) { - // Verify this part - if (gb->apu.channel_4_countdown_reloaded && !(effective_counter & 1)) { - uint16_t lower_bits = ~(effective_counter & ~(effective_counter - 1)) & 0xF; - effective_counter = (effective_counter & ~0xF) | lower_bits; - } - else { - effective_counter |= 0xF; - } + effective_counter |= 0xE; // Seems to me F under some circumstances? } if (effective_counter & 0x80) { effective_counter |= 0xFF; @@ -763,16 +756,7 @@ static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb, uint8_t nr43 // case GB_MODEL_CGB_A: case GB_MODEL_CGB_C: if (effective_counter & 8) { - if (gb->apu.channel_4_countdown_reloaded && !(effective_counter & 1)) { - uint16_t lower_bits = ~(effective_counter & ~(effective_counter - 1)) & 0xF; - effective_counter = (effective_counter & ~0xF) | lower_bits; - if (!(nr43 >> 4)) { - effective_counter &= ~1; - } - } - else { - effective_counter |= 0xE; - } + effective_counter |= 0xE; } if (effective_counter & 0x80) { effective_counter |= 0xFF; @@ -836,9 +820,6 @@ static inline uint16_t effective_channel4_counter(GB_gameboy_t *gb, uint8_t nr43 */ break; } - if (gb->model <= GB_MODEL_CGB_C && gb->apu.channel_4_countdown_reloaded && (nr43 >> 4)) { - effective_counter = (effective_counter & effective_counter - 1) | (effective_counter & 1); - } return effective_counter; } @@ -1200,22 +1181,16 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR43: { gb->apu.noise_channel.narrow = value & 8; - uint16_t effective_counter = effective_channel4_counter(gb, value); + uint16_t effective_counter = effective_channel4_counter(gb); bool old_bit = (effective_counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1; gb->io_registers[GB_IO_NR43] = value; bool new_bit = (effective_counter >> (gb->io_registers[GB_IO_NR43] >> 4)) & 1; if (gb->apu.channel_4_countdown_reloaded) { unsigned divisor = (gb->io_registers[GB_IO_NR43] & 0x07) << 2; if (!divisor) divisor = 2; - if (gb->model > GB_MODEL_CGB_C) { - gb->apu.noise_channel.counter_countdown = - divisor + (divisor == 2? 0 : (uint8_t[]){2, 1, 0, 3}[(gb->apu.noise_channel.alignment) & 3]); - } - else { - gb->apu.noise_channel.counter_countdown = - divisor + (divisor == 2? 0 : (uint8_t[]){2, 1, 4, 3}[(gb->apu.noise_channel.alignment) & 3]); - } - gb->apu.channel_4_delta = 0; + gb->apu.noise_channel.counter_countdown = + divisor + (divisor == 2? 0 : (uint8_t[]){2, 1, 0, 3}[(gb->apu.noise_channel.alignment) & 3]); + gb->apu.channel_4_delta = 0; } /* Step LFSR */ if (new_bit && (!old_bit || gb->model <= GB_MODEL_CGB_C)) { @@ -1263,6 +1238,15 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) } } + /* TODO: These two are quite weird. Verify further */ + if (gb->model <= GB_MODEL_CGB_C && gb->cgb_double_speed) { + if (!(gb->io_registers[GB_IO_NR43] & 0xF0) && (gb->io_registers[GB_IO_NR43] & 0x07)) { + gb->apu.noise_channel.counter_countdown -= 1; + } + else if ((gb->io_registers[GB_IO_NR43] & 0xF0) && !(gb->io_registers[GB_IO_NR43] & 0x07)) { + gb->apu.noise_channel.counter_countdown += 1; + } + } gb->apu.noise_channel.current_volume = gb->io_registers[GB_IO_NR42] >> 4;