Skip to content

Commit 4c57a76

Browse files
committed
Refine and comment CGB-0 PCM12 behavior
1 parent 10801af commit 4c57a76

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Core/apu.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,21 @@ static void tick_square_envelope(GB_gameboy_t *gb, GB_channel_t index)
541541
gb->apu.pcm_mask[0] &= gb->apu.square_channels[GB_SQUARE_1].current_volume | 0xF1;
542542
}
543543
else {
544-
gb->apu.pcm_mask[0] &= (gb->apu.square_channels[GB_SQUARE_2].current_volume << 4) | (gb->model == GB_MODEL_CGB_0? 0x3F : 0x1F);
544+
/* Note: CGB-0 behavior is instance specific and non-deterministic. Emulated behavior follows my CGB-0,
545+
except that my CGB-0 sometimes yields "1" for 8->7 and 4->3 transitions. */
546+
uint8_t mask;
547+
if (unlikely(gb->model == GB_MODEL_CGB_0)) {
548+
if (gb->apu.square_channels[GB_SQUARE_2].current_volume == 1 && (gb->io_registers[GB_IO_NR22] & 8)) {
549+
mask = 0x1F;
550+
}
551+
else {
552+
mask = 0x3F;
553+
}
554+
}
555+
else {
556+
mask = 0x3F;
557+
}
558+
gb->apu.pcm_mask[0] &= (gb->apu.square_channels[GB_SQUARE_2].current_volume << 4) | mask;
545559
}
546560
}
547561

0 commit comments

Comments
 (0)