4-bit Samples on the Pulse Channels

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

Moderator: Moderators

Post Reply
User avatar
za909
Posts: 206
Joined: Fri Jan 24, 2014 9:05 am
Location: Hungary

4-bit Samples on the Pulse Channels

Post by za909 » Thu May 09, 2019 5:33 am

Hi there,

I've had yet another idea to cram more into my DMC IRQ-based PCM music player, since a scheduler meant to handle game logic tasks during a wait inside the IRQ handler proved to be of very little added efficiency to a game. Since I need to wait more than 100 cycles / IRQ, I thought I could use a trick to continously reset the phase of a pulse channel by writing the high period register to continously output the first 1-step of the 75% duty waveform, letting me use the channel as a 4-bit DAC via the volume register. I would use this to play sound effects without having to software mix them with the music being played via $4011.
However, while the wave sequence is reset when the write happens, the timer is not from what I gather? That of course means that no matter what, I will get a "cut" in the sound every 2049 APU cycles (when the divider underflows and reloads from the period), which when aligned the worst, should persist for the time between two phase resets. This should be 432 CPU cycles, or possibly more (432 + 512 ± 1) if an OAM DMA happens to block the IRQ at the worst possible time as well.

Is there any way to ensure that the waveform remains completely flatlined, volume changes excluded (e.g.: by changing back-and-forth between two duty cycles at the precise moment) to make this work without added distortion?

User avatar
Bregalad
Posts: 7872
Joined: Fri Nov 12, 2004 2:49 pm
Location: Chexbres, VD, Switzerland

Re: 4-bit Samples on the Pulse Channels

Post by Bregalad » Thu May 09, 2019 10:12 am

I think if you write to $4003/$4007 the phase will reset, so doing this "constantly" will ensure a DC output, leading to the effect you want. I'm not sure of the phase, either duty cycles %00, %01, %10 will work and %11 won't work, or it's the oposite. This is actually the only difference between duty cycles %01 and %11. Someone more knownledgeable about the APU, or experiment on real hardware, should be able to answer this question.

The reason this "trick" was never experimented used is because this has basically no advantage over using $4011. Even mixing different things into $4011 should be as fast as using the pulse channels for this, as it's a simple addition operation.

lidnariq
Posts: 9294
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: 4-bit Samples on the Pulse Channels

Post by lidnariq » Thu May 09, 2019 11:07 am

za909 wrote:Is there any way to ensure that the waveform remains completely flatlined, volume changes excluded (e.g.: by changing back-and-forth between two duty cycles at the precise moment) to make this work without added distortion?
No, there's no "stop the pulse channel" to get the same DAC that people have tricked out of the other PSGs.

The best I think you'll manage is to set the pulse channel to its maximum frequency, and hope that people won't be bothered by the 12.4kHz modulation noise, or as you found, having to write to the phase reset not less often than every 2049 APU cycles.

User avatar
za909
Posts: 206
Joined: Fri Jan 24, 2014 9:05 am
Location: Hungary

Re: 4-bit Samples on the Pulse Channels

Post by za909 » Fri May 10, 2019 1:11 am

Bregalad wrote:I think if you write to $4003/$4007 the phase will reset, so doing this "constantly" will ensure a DC output, leading to the effect you want. I'm not sure of the phase, either duty cycles %00, %01, %10 will work and %11 won't work, or it's the oposite. This is actually the only difference between duty cycles %01 and %11. Someone more knownledgeable about the APU, or experiment on real hardware, should be able to answer this question.

The reason this "trick" was never experimented used is because this has basically no advantage over using $4011. Even mixing different things into $4011 should be as fast as using the pulse channels for this, as it's a simple addition operation.
Yes, the %11 duty will play a bit sequence of 1 0 0 1 1 1 1 1, all the others start with a 0 (so the click heard when crossing an 8-bit period boundary is much more noticeable here than it is for the other duty cycles). It has its advantages over %01 though, depending on what you want, like a slightly heavier "attack", creating a "dirtier" sound when switching back-and-forth between %10 and %11, better bass properties depending on your sound system, and it can create a tinny, high-pass filtered sound when combining it with a detuned %01 pulse from the other channel.

Mixing into $4011 should work if the extra addition does not delay my first write too much. Unlike the music, which I update twice in the IRQ handler (first right after the IRQ fires, and then after waiting enough time to be right in-between two IRQs), the sound effects should be fine even with one update, at 4.1kHz. Does it make sense to have a fixed restriction on the range of the music to accomodate the additional 4-bit sound, or do these things typically not add up to cause clipping (or clamping if I adjust the mix in that case)?

lidnariq
Posts: 9294
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: 4-bit Samples on the Pulse Channels

Post by lidnariq » Fri May 10, 2019 10:47 am

To the best of my knowledge, the NES's audio path doesn't clip, even when all five channels are emitting their maximum value simultaneously.

Post Reply