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

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

Moderator: Moderators

Post Reply
User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

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

Post by Petruza »

Hi, I've wrote this routine to make a sound, following the various docs about the NES APU:

Code: Select all

// 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:
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)
Drag
Posts: 1350
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Post by Drag »

No, that pitch jumping isn't normal.

Code: Select all

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

Code: Select all

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.
User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Post by Petruza »

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.
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Post by Disch »

Sweep Unit. See this thread:

http://nesdev.com/bbs/viewtopic.php?t=4681
User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Post by Petruza »

That was it. Thanks!
Post Reply