what this macro do ?

This is a discussion on what this macro do ? within the C Programming forums, part of the General Programming Boards category; howdy~ here is a macro which i dunno what it means: Code: #define KEYS *(volatile u16*) 0x4000130 i'd appreciate if ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    what this macro do ?

    howdy~

    here is a macro which i dunno what it means:
    Code:
    #define KEYS	*(volatile u16*) 0x4000130
    i'd appreciate if anyone could explain it abit(especially volatile keyword), tks.
    Never end on learning~

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    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
    -----------------------------------------------

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    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~

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Some number (a memory address).
    Code:
    #define KEYS	*(volatile u16*) 0x4000130
    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.

    [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:
    Code:
    while ( KEYS == 0 )
    {
       /* wait for keypress */
    }
    /* handle 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.
    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.*

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    tks Dave_Sinkula,

    in fact the code is composed of these two lines:
    Code:
    #define KEYS	*(volatile u16*) 0x4000130 
    #define keyPressed(key) (~KEYS & key)
    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.
    Never end on learning~

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not the address itself. You're dereferencing a pointer, which gives you the value of whatever it points at.
    Code:
    int *ptr, value;
    
    value = 10;
    ptr = & value;
    
    display( *ptr ); /* Displays "10", which is the value of what is being pointed at. */
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by black
    tks Dave_Sinkula,

    in fact the code is composed of these two lines:
    Code:
    #define KEYS	*(volatile u16*) 0x4000130 
    #define keyPressed(key) (~KEYS & key)
    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.
    Not the address itself -- the value there -- remember, KEYS is:
    The u16 value located at address 0x4000130.
    KEYS 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.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21