This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

xdata access by #define

I have to read a flash written by another program and am provided a file looking like this

// data locations
#define data1 0
#define data2 data1+0x100
...

I would like to use the file 'as is" but all my typecasting efforts have resulted in errors or wrong code.

In advance, thanks

Erik

Parents
  • "the "problems" experienced in the cited threads both relate to a proper understanding of the Compiler's internal data representation - or lack thereof!

    Thanks for clarifying. I figured my point didn't come across. Yes those threads do relate to internal representation. However, yy cautions are independent of the implementation -- strictly a "C thing". The context in which XBYTE and XWORD are used expect "array-ness", if you will, and this is a particular problem with XWORD, regardless of the target system. XBYTE[data2] accesses a byte at address 0x0100, whereas XWORD[data2] accesses a word at address 0x0200.

    I personally find the dichotomy awkward and a source of errors. When I'm thinking in terms of addresses, I want my macros to implement them as addresses/pointers, not arrays where one macro coincidentally works with addresses and the other does not. I prefer something a bit more generic, allowing me to always use addresses pointing to anything. A contrived example:

    // data locations
    #define data1 0
    #define data2 data1+0x100
    
    #define XDAT(type, addr)    (*(type xdata *)(addr))
    
    struct FOO
    {
        long l[3];
        char c;
    };
    
    unsigned char b;
    unsigned int  w;
    unsigned long l;
    
    void main(void)
    {
        b = XDAT(unsigned char, data2);
        w = XDAT(unsigned int,  data2);
        l = XDAT(unsigned long, data2);
    
        b = XDAT(struct FOO, data2).c;
    }

Reply
  • "the "problems" experienced in the cited threads both relate to a proper understanding of the Compiler's internal data representation - or lack thereof!

    Thanks for clarifying. I figured my point didn't come across. Yes those threads do relate to internal representation. However, yy cautions are independent of the implementation -- strictly a "C thing". The context in which XBYTE and XWORD are used expect "array-ness", if you will, and this is a particular problem with XWORD, regardless of the target system. XBYTE[data2] accesses a byte at address 0x0100, whereas XWORD[data2] accesses a word at address 0x0200.

    I personally find the dichotomy awkward and a source of errors. When I'm thinking in terms of addresses, I want my macros to implement them as addresses/pointers, not arrays where one macro coincidentally works with addresses and the other does not. I prefer something a bit more generic, allowing me to always use addresses pointing to anything. A contrived example:

    // data locations
    #define data1 0
    #define data2 data1+0x100
    
    #define XDAT(type, addr)    (*(type xdata *)(addr))
    
    struct FOO
    {
        long l[3];
        char c;
    };
    
    unsigned char b;
    unsigned int  w;
    unsigned long l;
    
    void main(void)
    {
        b = XDAT(unsigned char, data2);
        w = XDAT(unsigned int,  data2);
        l = XDAT(unsigned long, data2);
    
        b = XDAT(struct FOO, data2).c;
    }

Children
No data