This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

[APU-sound] frequency in registers $4002 and $4003 problem

[APU-sound] frequency in registers $4002 and $4003 problem
by on (#41185)
Hi, I've wrote this routine to make a sound, following the various docs about the NES APU:

Code:
// don't take it literally, it's part of a longer code.
byte volume, length;
word tone;
addr( 0x4000 ) = 0x10 | ( 0xF & volume );
addr( 0x4002 ) = (byte) tone;
addr( 0x4003 ) = ( length <<3 ) | ( tone >>5 );


the $4002 register, and the lowest 3 bits of $4003, are supposed to hold an 11 bit number (0..2047) which is inversely proportional to the frequency ( at least that's what I understood )

So, tell me if I'm wrong, but the higher the value, the lower the frequency, thus the pitch of the sound.
I made a program that increments this value and expected to hear a sound whose pitch would get constantly lower. But it doesn't, it goes lower but at certain points it jumps to a higher pitch, and then continues going lower.
Is the code correct? can you spot an error?
Thanks!

PS: I quote from Everynes - Nocash NES Specs:
Quote:
4002h - APU Frequency Channel 1 (Rectangle)
0-7 Lower 8 bits of wavelength (upper 3 bits in Register 3 (4003h))
...
4003h - APU Length Channel 1 (Rectangle)
2-0 Upper 3 bits of wavelength (unused on noise channel)
7-3 Length counter load register (5bit value, see below)

by on (#41187)
No, that pitch jumping isn't normal.

Code:
addr( 0x4003 ) = ( length <<3 ) | ( tone >>5 );

Looks like your problem,
Code:
addr( 0x4003 ) = ( length <<3 ) | ( tone >>8 );

Looks like it'd fix it, seeing as how the lower three bits of 4003 act like what theoretically would be bits 8, 9, and 10 of $4002.

by on (#41188)
Of course! silly of me. 8!
Thanks! works great.

PS: surprisingly, this works great in jNES, but my PMP only sounds when this value is less than or equal to 1023, so it only makes a sound when bit 2 of $4003 is 0.

by on (#41189)
Sweep Unit. See this thread:

http://nesdev.com/bbs/viewtopic.php?t=4681

by on (#41190)
That was it. Thanks!