Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

#1 2011-01-19 23:26:16

Nyarlathotep
New member
Registered: 2011-01-13
Posts: 3

Register Bits

Hiii

I'm trying to write some sound examples in C with GBDK.
Pandocs/GBSOUND.txt/etc. all give great detailed information about the sound registers but the notation confuses me. I've tried looking it up for myself but I don't know what it's called.

eg:

Code:

[b]for NR52_REG (sound on/off)[/b]

 Bit 7 - All sound on/off  (0: stop all sound circuits) (Read/Write)
 Bit 3 - Sound 4 ON flag (Read Only)
 Bit 2 - Sound 3 ON flag (Read Only)
 Bit 1 - Sound 2 ON flag (Read Only)
 Bit 0 - Sound 1 ON flag (Read Only)

Now following from examples i've found, I know what I can set the power on using NR52_REG = 0x80U and off with NR52_REG = 0x00U, however this doesn't let me understand the notation of "Bit 7", "Bit 3" and "Bit 6-4" for other registers, etc.

If someone could point me in the right direction or give brief examples of writing to bits in C, i'd be v. happy :]

Offline

 

#2 2011-01-20 06:54:18

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 249

Re: Register Bits

It sounds like you need to learn a bit about binary and hexadecimal numbers. The concept is explained in a lot of places. But here's a cheat sheet for the bits and hexadecimal numbers. If bit 6 is 1 and the other bits are 0, that's a hexadecimal value of 0x40, for example. If you need to combine several bits, you can do so with the bitwise "or" operator, which in C is the "pipe" character, | . For example, say you want to combine bits 3 and 4. Then you could write (0x08U | 0x10U). (The U is there for a good reason BTW, which I won't go into here. Make sure it's there.) You could also calculate the value manually and insert it, in this case 0x18U.

Furthermore, the bitwise operators can be used on data that is not constant. Say you want to retrigger the sound in channel 1 without changing the frequency. Then you could do something like:

NR14_REG = NR14_REG | 0x80U;

Code:

Bit    7  6  5  4 | 3  2  1  0

Hex  
0x01   0  0  0  0 | 0  0  0  1
0x02   0  0  0  0 | 0  0  1  0
0x04   0  0  0  0 | 0  1  0  0
0x08   0  0  0  0 | 1  0  0  0
0x10   0  0  0  1 | 0  0  0  0  
0x20   0  0  1  0 | 0  0  0  0  
0x40   0  1  0  0 | 0  0  0  0  
0x80   1  0  0  0 | 0  0  0  0

Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#3 2011-01-20 08:32:43

Nyarlathotep
New member
Registered: 2011-01-13
Posts: 3

Re: Register Bits

Ahkay I get the concept now. I (believe) I understand hexdecimal numbers but never made the connection between bits and looking at it from a binary point of view. It's remarkably similar to the cg_nomip console command in quake3 in that it uses a bitmask to determine which weapons aren't picmipped.

The bitmasking cheat sheet above also explains the 'Bit 7-6' notation in documents, it just means bits 6 and 7 are used to determine, for example, the wave duty cycle.

Code:

 00: 12.5% ( _-------_-------_------- )
 01: 25%   ( __------__------__------ )
 10: 50%   ( ____----____----____---- ) (normal)
 11: 75%   ( ______--______--______-- )

Previously from random examples I saw 0x80U = 50% (= 128), I thought .'. 25% = 0x40U and 12.5% = 0x20U.. now I know that's not quite so tongue

Prior to doing sound code examples I looked into bitmasking, which'll have greater use now that you've mentioned this.

Finally, I think I understand the importance of the U on the end of a hex number - unsigned constants/vars will be faster to process.

I think i've got it now!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson