Tuesday, April 5, 2011

can anyone help me with this macro?

Anyone can explain in details what the following macro does?

#define write_XDATA(address,value) (((char *)0x010000) [address]=value)

thx!

From stackoverflow
  • It assigns value to the byte at memory location 0x10000 + address. It's easier to grok if you separate it out a bit:

    char* buf = (char *)0x010000;
    buf[address]=value;
    

    (Though of course you have no choice but to mash it all together in a macro.)

    sharptooth : Not necessarily. Why could you not write a function that accepts a `char` and a `size_t` and writes that `char` at address 0x010000 + size_t?
    Marcelo Cantos : Sorry, my last statement was ambiguous. What I meant was, "...in a macro, you have no choice...".
  • It maps addresses to real addresses using an offset and then writes to it. XDATA is probably a term taken over from the 8051-processor.

  • You use it:

    write_XDATA( Address, Value );
    

    and it is expanded:

    ((char*)0x010000)[Address]=Value;
    

    which is equivalent to the following:

    char* baseAddress = (char*)0x010000;
    *(baseAddress + Address) = Value;
    

    so basically it writes a byte stored in Value at the address 0x010000 + Address.

  • I don't know how much details you want to hear, but the macro expands itself to what you have just written -

    macro parameters address and value are put into address and value placeholders in the macro expansion definition (((char *)0x010000) [address]=value)

  • This macro on address + 0x010000 saving one byte of value.

  • That's most probably part of a program designed to run on an embedded platform. It's used to do memory mapped IO.

    The base address of the register-map is 0x010000. It writes value to the memory location 0x010000+address.

    The use of the square brackets [] works because of the equivalence of array-addressing and pointer arithmetic in C.

0 comments:

Post a Comment