Length Counter Clarifications

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
breathermachine
Posts: 20
Joined: Wed Oct 21, 2015 10:28 pm

Length Counter Clarifications

Post by breathermachine »

Hello!
I've got some questions about the Length Counter that I can't seem to resolve by reading PanDocs, GBSSOUND.txt, http://gbdev.gg8.se/wiki/articles/Gameb ... d_hardware(Wiki) and other sources.

Q1: Is the "enabled flag" that's mentioned in the Wiki the same as the length enable flag in bit 6 of NRx4?

Q2: I currently have the following logic for outputting the current waveform. Is this logic correct?
(I have removed other details such as duty, LFSR, DAC power, etc. to make it simple.)

Code: Select all

    if (length != 0 || !length_enable)
        output current_volume
    else
        output 0
Q2.5: I'm using the same logic for NR52's status bits. (1 if length != 0 || !length_enable, 0 otherwise.) Is this also correct?

Q3: The Wiki mentioned that when a channel is triggered it becomes enabled. Does this mean enabling length_enable (bit 6 of NRx4) OR the "enabled flag" that's mentioned in the Wiki?

EDIT: Add Q4.
Q4: During a trigger event, if the length counter is 0, it is set to either 64(square and noise) or 256(wave). Aren't 63 or 255 the correct values?

Thank you!
binji
Posts: 11
Joined: Thu Jun 16, 2016 11:53 am

Re: Length Counter Clarifications

Post by binji »

OK, I'm not 100% on these answers, but my emulator does pass blargg's dmg_sound test, so I'll give it a shot:
Q1: Is the "enabled flag" that's mentioned in the Wiki the same as the length enable flag in bit 6 of NRx4?
No, I don't think so. It describes it as an "internal enabled flag", which implies its value is not exposed to the user. In my emulator, I use the NR52 status bit for this.
Q2: I currently have the following logic for outputting the current waveform. Is this logic correct?
This seems like it should work, but I don't do it this way. Since we need to update the status bit anyway, I clear it when the length becomes zero. Then I check it to determine whether I need to produce a sample for that channel. This way I don't ouptut sound if the sweep causes the status to be disabled either.
Q2.5: I'm using the same logic for NR52's status bits. (1 if length != 0 || !length_enable, 0 otherwise.) Is this also correct?
Same as above, I manipulate the status bit independently. A few things can affect this, length counter, DAC enabled, sweep overflow, and writing to NR10 (in a specific case).
Q3: The Wiki mentioned that when a channel is triggered it becomes enabled. Does this mean enabling length_enable (bit 6 of NRx4) OR the "enabled flag" that's mentioned in the Wiki?
I think it just means that the channel will start producing sound, so yeah the "enabled flag". You can trigger a channel without enabling the length counter, for example.
Q4: During a trigger event, if the length counter is 0, it is set to either 64(square and noise) or 256(wave). Aren't 63 or 255 the correct values?
It seems like 64 and 256 are correct. But there is a case where it will be 63 or 255, see under "Obscure Behavior" on that wiki:
If a channel is triggered when the frame sequencer's next step is one that doesn't clock the length counter and the length counter is now enabled and length is being set to 64 (256 for wave channel) because it was previously zero, it is set to 63 instead (255 for wave channel).
Post Reply