Prehistorik Man intro music [solved]

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Prehistorik Man intro music [solved]

Post by zeroone »

At the very beginning of the intro music to Prehistorik Man, my emulator outputs an enduring high pitched tone from both Square channels. Eventually, the tone disappears and the music seems normal after that point. Below, I logged the writes to the APU control registers and the Square 2 registers.

FF26 80 (NR52 CONTROL_POWER_LENGTH_STATUSES)
FF24 77 (NR50 CONTROL_LEFT_RIGHT_VOLUME)
FF25 53 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

Audio is enabled and output volumes are maxed out.
Wave and Square 1 are enabled on the left and both Square channels are enabled on the right.

FF16 8F (NR21 SQUARE2_DUTY_LENGTH)
FF17 F0 (NR22 SQUARE2_VOLUME_ENVELOPE)
FF19 80 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)

Square 2 duty is set to the 50% pattern and Square 2 length is set to 64 - 15 = 49.
The starting volume for the volume envelope is set to 15. And, the period is set to 0, which is interpreted as 8. The envelope delta is set to negative.
Finally, the tone is triggered with length disabled (why did they even bother setting the length?). Meaning, it's an enduring tone until a register write shuts it off. If the envelope volume drops to 0, that doesn't fully kill the tone...

FF17 18 (NR22 SQUARE2_VOLUME_ENVELOPE)

The starting volume is changed to 1, which shouldn't affect the currently playing volume. The period remains unchanged. However, the envelope delta toggled from minus to plus; even if the envelope volume dropped to 0, it can now rise back to max.

FF18 F4 (NR23 SQUARE2_FREQUENCY_LSB)
FF19 01 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)

The frequency of the tone is adjusted.

FF25 77 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

The noise channel is muted.

FF18 F4 (NR23 SQUARE2_FREQUENCY_LSB)
FF19 01 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)
FF25 77 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

That pattern repeats again and again for a while.

FF18 F4 (NR23 SQUARE2_FREQUENCY_LSB)
FF19 01 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)
FF25 77 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

...

Anyway, from what I understand of the APU, the enduring Square tones should be present. What am I interpreting incorrectly?
Last edited by zeroone on Fri Dec 08, 2017 7:45 am, edited 1 time in total.
nitro2k01
Posts: 252
Joined: Sat Aug 28, 2010 9:01 am

Re: Prehistorik Man intro music

Post by nitro2k01 »

I'm looking at this and I agree that I think something would be playing on channels 1 and 2. Perhaps something from the obscure behavior list applies and make those channels go silent. But let me stop you and say that if the tone is high pitched, it's likely some other kind of bug in your code. The value $01F4 written to NRx3 and NRx4 corresponds to about 85 Hz, or between E3 and F3 in pitch. This is a bass note, NOT a high pitched tone.
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Prehistorik Man intro music

Post by zeroone »

nitro2k01 wrote:I'm looking at this and I agree that I think something would be playing on channels 1 and 2. Perhaps something from the obscure behavior list applies and make those channels go silent. But let me stop you and say that if the tone is high pitched, it's likely some other kind of bug in your code. The value $01F4 written to NRx3 and NRx4 corresponds to about 85 Hz, or between E3 and F3 in pitch. This is a bass note, NOT a high pitched tone.
I did notice this bullet point:

"Extra length clocking occurs when writing to NRx4 when the frame sequencer's next step is one that doesn't clock the length counter. In this case, if the length counter was PREVIOUSLY disabled and now enabled and the length counter is not zero, it is decremented. If this decrement makes it zero and trigger is clear, the channel is disabled. On the CGB-02, the length counter only has to have been disabled before; the current length enable state doesn't matter. This breaks at least one game (Prehistorik Man), and was fixed on CGB-04 and CGB-05."

But, I already had that logic. From the register writes above, is this related?

Edit: Also, you are correct that the tone is not high pitched as I suggested. It just sounds very annoying, similar to a buzzer.
Last edited by zeroone on Sat Dec 02, 2017 10:57 pm, edited 2 times in total.
SuperWill24
Posts: 33
Joined: Sat May 13, 2017 7:54 pm

Re: Prehistorik Man intro music

Post by SuperWill24 »

zeroone wrote:At the very beginning of the intro music to Prehistorik Man, my emulator outputs an enduring high pitched tone from both Square channels. Eventually, the tone disappears and the music seems normal after that point. Below, I logged the writes to the APU control registers and the Square 2 registers.

FF26 80 (NR52 CONTROL_POWER_LENGTH_STATUSES)
FF24 77 (NR50 CONTROL_LEFT_RIGHT_VOLUME)
FF25 53 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

Audio is enabled and output volumes are maxed out.
Wave and Square 1 are enabled on the left and both Square channels are enabled on the right.

FF16 8F (NR21 SQUARE2_DUTY_LENGTH)
FF17 F0 (NR22 SQUARE2_VOLUME_ENVELOPE)
FF19 80 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)

Square 2 duty is set to the 50% pattern and Square 2 length is set to 64 - 15 = 49.
The starting volume for the volume envelope is set to 15. And, the period is set to 0, which is interpreted as 8. The envelope delta is set to negative.
Finally, the tone is triggered with length disabled (why did they even bother setting the length?). Meaning, it's an enduring tone until a register write shuts it off. If the envelope volume drops to 0, that doesn't fully kill the tone...

FF17 18 (NR22 SQUARE2_VOLUME_ENVELOPE)

The starting volume is changed to 1, which shouldn't affect the currently playing volume. The period remains unchanged. However, the envelope delta toggled from minus to plus; even if the envelope volume dropped to 0, it can now rise back to max.

FF18 F4 (NR23 SQUARE2_FREQUENCY_LSB)
FF19 01 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)

The frequency of the tone is adjusted.

FF25 77 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

The noise channel is muted.

FF18 F4 (NR23 SQUARE2_FREQUENCY_LSB)
FF19 01 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)
FF25 77 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

That pattern repeats again and again for a while.

FF18 F4 (NR23 SQUARE2_FREQUENCY_LSB)
FF19 01 (NR24 SQUARE2_TRIGGER_FREQUENCY_MSB)
FF25 77 (NR51 CONTROL_LEFT_RIGHT_ENABLES)

...

Anyway, from what I understand of the APU, the enduring Square tones should be present. What am I interpreting incorrectly?
Prehistorik Man probably uses the same sound engine as The Blues Brothers: Jukebox Adventure and Little Indian in Big City (both also by Titus). Many emulators seem to have trouble emulating this, because I remember that Jukebox Adventure sounded horribly wrong on a number of emulators (such as VisualBoyAdvance), similar to what is described here. However, it would sound correct on BGB. Another issue that many emulators have is that they don't get past the intro, showing an "invalid opcode" or something like that because they don't support the intro's impressive scrolling effect.
Alyosha_TAS
Posts: 173
Joined: Wed Jun 15, 2016 11:49 am

Re: Prehistorik Man intro music

Post by Alyosha_TAS »

I had the exact same problem with my emulator.

It is fixed via the effects described in the 'obscure behaviour' section here:
"Zombie" mode: the volume can be manually altered while a channel is playing by writing to NRx2. Behavior depends on the old and new values of NRx2, and whether the envlope has stopped automatic updates. The CGB-02 and CGB-04 are the most consistent:

If the old envelope period was zero and the envelope is still doing automatic updates, volume is incremented by 1, otherwise if the envelope was in subtract mode, volume is incremented by 2.
If the mode was changed (add to subtract or subtract to add), volume is set to 16-volume.
Only the low 4 bits of volume are kept after the above operations.

Other models behave differently, especially the DMG units which have crazy behavior in some cases. The only useful consistent behavior is using add mode with a period of zero in order to increment the volume by 1. That is, write $V8 to NRx2 to set the initial volume to V before triggering the channel, then write $08 to NRx2 to increment the volume as the sound plays (repeat 15 times to decrement the volume by 1). This allows manual volume control on all units tested.
Gambatte implements this as so:

Code: Select all

bool EnvelopeUnit::nr2Change(const unsigned newNr2) {
	if (!(nr2 & 7) && counter != COUNTER_DISABLED)
		++volume;
	else if (!(nr2 & 8))
		volume += 2;
	
	if ((nr2 ^ newNr2) & 8)
		volume = 0x10 - volume;
	
	volume &= 0xF;
	
	nr2 = newNr2;
	
	return !(newNr2 & 0xF8);
}
Notice how the volume itself is directly effected by writes to NRx2 while playing.

I'm not sure how physically realistic that is, or how it changes precisely between GB and CGB, but implementing that logic fixed the problem completely for me.
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Prehistorik Man intro music

Post by zeroone »

@Alyosha_TAS Thanks for the info. Zombie mode is an obscure behavior that I have yet to implement. I'll plug this in when I get a chance. Hopefully, it solves the problem :)
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Prehistorik Man intro music

Post by zeroone »

Alyosha_TAS wrote:I'm not sure how physically realistic that is, or how it changes precisely between GB and CGB, but implementing that logic fixed the problem completely for me.
Thanks for your help. The audio issue was resolved by introducing this extra logic. Apparently, "zombie" mode is a real thing.
Post Reply