$4015 length counter and linear counter

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
rainwarrior
Posts: 7680
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

$4015 length counter and linear counter

Post by rainwarrior » Fri Mar 23, 2012 9:58 pm

I can't find any description of this in apu_ref.txt or any of the wiki articles, but every emulator I've checked sets the length counter of a channel to 0 when its $4015 enable bit is written with a 0. As far as I can tell this is correct; I discovered I was missing this behaviour from Legend of Zelda.NSF track 7 which halts the squares and triangle by writing enable with 0 then with 1 in the same frame.

The question I have is whether setting the triangle enable bit to 0 also clears its linear counter. There is differing behaviour among the emulators I've looked at:

NotSoFato: just length counter
Nestopia: just length counter
VirtuaNES: also resets linear counter, clears halt flag
NezPlug++: also resets linear counter, clears halt flag

If I am thinking about this correctly, clearing the halt flag is unnecessary, since the channel will be off until the length counter is reloaded ($400B) which will set halt anyway.

But... does anyone know offhand whether the linear counter should be cleared?

If not, I can probably rig up a hardware test on my PowerPak but it'll require a bit of thought.

User avatar
rainwarrior
Posts: 7680
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Post by rainwarrior » Fri Mar 23, 2012 10:01 pm

Actually, never mind, I don't think it matters.

The channel will be silent until the length counter is reset ($400B) which will set the halt flag and cause the linear counter to reload anyway. So it won't matter if the linear counter is cleared or not. (I guess there could potentially be an extra up to a 1/4 frame of non-silence?)


Edit: I finally found a small note on the APU Length Counter article that mentions it. I've added clarification, and also added the information to the APU Status article. It also -is- in apu_ref.txt in the length counter section, I just didn't notice it (and the number 4015 doesn't appear near the information so it evaded searches).

User avatar
rainwarrior
Posts: 7680
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Post by rainwarrior » Sun Mar 25, 2012 1:05 pm

Okay, I rigged up what I think is an appropriate test. lctest.zip (attached)

I believe that the linear counter is NOT set to 0 when the enable bit is cleared. Testing this NSF on a PowerPak there was no ~1/4-frame gap, but on some emulators where the linear counter is set to 0 (like VirtuaNSF) there is a visible gap of the expected length. I'm not sure exactly where the frame sequencer kicks in when using the PowerPak, but I presume that I would have seen a gap of some amount (beyond the couple of cycles I intentionally kill to make the transition visible), and I did not over multiple tests. (...unless I was unlucky enough to have the frame sequencer happen during those few cycles.)

So... only reset the length counter. Don't reset the linear counter. I think.
Attachments
lctest.zip
(1.87 KiB) Downloaded 30 times
Last edited by rainwarrior on Mon Aug 07, 2017 2:57 pm, edited 1 time in total.

User avatar
Disch
Posts: 1849
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch » Sun Mar 25, 2012 5:10 pm

afaik $4015 has zero impact on the linear counter. Frankly I would be extremely surprised if it did affect it because it has no reason to.

I wouldn't even bother checking against ancient emulators like NEZplug and VirtuaNES. They are so old and outdated that they are very likely wrong.

Even NotSoFatso, which is a lot more recent than them, is still very old and outdated.

User avatar
rainwarrior
Posts: 7680
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Post by rainwarrior » Sun Mar 25, 2012 11:46 pm

In general, I go by the wiki and docs as much as I can, but I have a habit of looking at other emulators just to check up on my implementation. (Like in this case, I missed the bit about zeroing the length counter, which I only realized because I saw it in other emulators.)

Anyhow, yeah I would have been really surprised if the linear counter was zeroed. Why bother with the extra circuitry?

(BTW, NEZPlug is long dead, but NEZPlug++ is still actively maintained.)

Post Reply