Page 1 of 1

Blargg's NES APU libraries writing to a register

Posted: Sun May 11, 2008 12:31 pm
by Laserbeak43
Hi,
I'm using blargg's NES sound APU library in visual studio 2005. I'm still learning reading his and brad's docs as well as getting assistance on #nesdev(thanks blargg, kev and thefox).

I'm having a bit of trouble with understanding some write register functions and the logic behind them.
To start, this binary operation.

Code: Select all

apu.write_register( 0x4000, 0xb0/*176*/ | volume );
now looking at this code, i see that 0xb0 is 176 and i added a comment as a reminder. then in order to break that down into binary i open up pcalc and cheat a bit: 10110000. then, i deal with the volume which with a little debugging, i've found is 14 at its highest level 000001110.
so after ORing them i get:

Code: Select all

10110000
00001110
------------
10111110
which is 190(correct?). so i'm writting 10111110 to register 0x4000
Blargg's APU Doc wrote:$4000/4 ddle nnnn duty, loop env/disable length, env disable, vol/env
period
so i'm writting
10 to the duty register?
11 to loop envelope/disable length?(not sure how this works yet)
1110 to the volume envelope?

I must admit, that writting this has cleared up a bit of this, but i'm still not sure of the result. is my answer correct? or backwards(little-endian needed)? is this affecting ONLY the volume?

thanks.

Posted: Sun May 11, 2008 5:10 pm
by blargg
The point of using hexadecimal is it directly translates to binary. 0xB0 tells the values of the two groups of four bits. B = %1011, 0 = %0000, so 0xB0 = %10110000. Then when you OR in the volume, as long as it's 15 or less (0x0F or less), it'll only affect the low 4 bits.

As for interpretation of 0xB0 + 14 you get %10 for duty (square wave), loop envelope and disable length counter, disable envelope (so you get a fixed volume), volume = 14.

Posted: Sun May 11, 2008 9:01 pm
by Laserbeak43
blargg wrote:The point of using hexadecimal is it directly translates to binary. 0xB0 tells the values of the two groups of four bits. B = %1011, 0 = %0000, so 0xB0 = %10110000. Then when you OR in the volume, as long as it's 15 or less (0x0F or less), it'll only affect the low 4 bits.

As for interpretation of 0xB0 + 14 you get %10 for duty (square wave), loop envelope and disable length counter, disable envelope (so you get a fixed volume), volume = 14.
pretty clever :D , but,
- %10 duty cycle? i thought there was only 1/8 1/4 1/2 and 3/4.
- loop envelope is enabled and the length counter disabled?
- and the regulare amp envelope is disabled right?

Posted: Sun May 11, 2008 10:54 pm
by Laserbeak43
Laserbeak43 wrote:
blargg wrote:The point of using hexadecimal is it directly translates to binary. 0xB0 tells the values of the two groups of four bits. B = %1011, 0 = %0000, so 0xB0 = %10110000. Then when you OR in the volume, as long as it's 15 or less (0x0F or less), it'll only affect the low 4 bits.

As for interpretation of 0xB0 + 14 you get %10 for duty (square wave), loop envelope and disable length counter, disable envelope (so you get a fixed volume), volume = 14.
pretty clever :D , but,
- %10 duty cycle? i thought there was only 1/8 1/4 1/2 and 3/4.
- loop envelope is enabled and the length counter disabled?
- and the regular amp envelope is disabled right?
-heh, sorry been out of the binary game for a while. didn't realize that the %10 was actually 2. so yeah then that's a 50% duty cycle.

-the loop envelope IS enabled and the length counter disabled.

-and like you said, the volume's envelope is disabled.