howdy~
here is a macro which i dunno what it means:
i'd appreciate if anyone could explain it abit(especially volatile keyword), tks.Code:#define KEYS *(volatile u16*) 0x4000130
howdy~
here is a macro which i dunno what it means:
i'd appreciate if anyone could explain it abit(especially volatile keyword), tks.Code:#define KEYS *(volatile u16*) 0x4000130
Never end on learning~
The macro will return the u16 (probably an unsigned 16 bit integer) at memory location 0x4000130. The volatile keyword tells the compiler that the value may be changed by forces outside of the program itself. This makes the computer actually go and check the value at the memory address every time it needs to access it rather than just assuming that since the program hasn't changed it since last time it must still be the same as the value stored in the register from last time. I've only ever had to use this keyword in a real-time embedded DSP type program...
Hope that helps
DavT
-----------------------------------------------
tks man, i searched it within "the c++ programing language" and also found it is rarely used. what this cute macro really confused me is:
1 it has two *, which means pointer to pointer ?
2 "the value may be changed by forces outside of the program itself", here the value is just the address(0x4000130) ?
Never end on learning~
Some number (a memory address).
Interpret this number as a pointer. Specifically, a pointer to a u16.Code:#define KEYS *(volatile u16*) 0x4000130
See what the value there is (what is the u16 at 0x4000130?).Code:#define KEYS *(volatile u16*) 0x4000130
The u16 value located at address 0x4000130.Code:#define KEYS *(volatile u16*) 0x4000130
[edit]The volatile qualifier means that the value there may change. Let's say you're waiting for some key to be pressed, and this will cause the value there to be nonzero. You might have a loop to wait for a keypress:Without volatile, the compiler might generate code that only reads the value there once and then ends up looping forever because it doesn't read the memory location each time through the loop. Adding volatile forces the compiler to read the memory location each time.Code:while ( KEYS == 0 ) { /* wait for keypress */ } /* handle keypress */
Last edited by Dave_Sinkula; 05-06-2005 at 12:15 PM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
tks Dave_Sinkula,
in fact the code is composed of these two lines:
as u said it is relative to keys detection and thanks for ur reply which makes sense i think. could u please say a little bit on second line of the code above ? i cannot grasp the exactly meaning of (~KEY & key) there, in particular ~KEY, which should be the same as: ~(*(volatile u16*) 0x4000130), i just know ~101010 is actually 010101, but what we get if it is an address itself ? tks in adv.Code:#define KEYS *(volatile u16*) 0x4000130 #define keyPressed(key) (~KEYS & key)
Never end on learning~
It's not the address itself. You're dereferencing a pointer, which gives you the value of whatever it points at.
Quzah.Code:int *ptr, value; value = 10; ptr = & value; display( *ptr ); /* Displays "10", which is the value of what is being pointed at. */
Hope is the first step on the road to disappointment.
Not the address itself -- the value there -- remember, KEYS is:Originally Posted by blackKEYS is the value there. So you read this value and bit-flip it, then AND with key (likely some constant value with a single bit set) will check for an individual key.The u16 value located at address 0x4000130.
Last edited by Dave_Sinkula; 05-06-2005 at 02:12 PM. Reason: Damn! Damn you pokey fingers!!
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*