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

Request to Erik and/or Andy

Erik, Andy,

A while ago, I read in one of the threads an answer from one of you (if I remember well) regarding the abstraction of the compiler datatypes.

One of you was giving examples of how, for instance, abstract the 'unsigned char' by defining UCHAR, the 'char' by defining SCHAR, and so on in a separate header file. This all to make your code less compiler dependent.

I want to apply this solution, because I find it a nice one (separation between your code and typical compiler properties makes your code as much as possible portable).

The problem is: I simply can't find back anymore this thread.

Is there any chance one of you still know this?

Of course, if other people reading this message would know it, I would be equally thankful! ;-)

--Geert

  • is this what you were looking for:

    http://www.keil.com/forum/docs/thread2472.asp

    Note that I would use "U8" rather then "UCHAR" because it explicitly & unambiguously tells you the size of the type.
    And it's shorter to type! ;-)

    I would also use #defines for al compiler-specific keyword extensions; eg,

    #if defined __C51__
    // Keil C51
    #define DATA data
    #define XDATA xdata
    #define AT(adr) _at_ adr
    //etc
    #elif defined __BORLANDC__
    // Borland C
    #define DATA
    #define XDATA
    #define AT(adr)
    //etc
    #else
    #error Unknown Compiler
    #endif

    Thus I can compile my code under Borland - which usually gives better diagnostics - if I want to.

    Is that sufficient for you?

  • Indeed, Andy

    This is exactly what I was looking for.

    Thanks very much for the prompt answer and thanks for the extra input too!

    Best regards,

    --Geert

  • OK, here is the whole symphony

    Global Variables are defined in a .h file included in every module, one module #defines DATACT.

    #define U1    bit
    #define UB8   unsigned char   bdata
    #define U8    unsigned char
    #define S8    signed   char
    #define UI8   unsigned char   idata
    #define UX8   unsigned char   xdata
    #define UC8   unsigned char   code
    #define U16   unsigned short
    #define S16   signed   short
    #define UI16  unsigned short  idata
    #define UX16  unsigned short  xdata
    #define UC16  unsigned short  code
    #define U32   unsigned long
    #define UX32  unsigned long   xdata
    #define UI32  unsigned long   idata
    #define UC32  unsigned long  code
    
                                                // pointer in data in
    #define U8DI  unsigned char   idata * data  // data       idata
    #define U8DX  unsigned char   xdata * data  // data       xdata
    #define U8IX  unsigned char   xdata * idata // idata      xdata
    #define U8IC  unsigned char   code  * idata // idata      code
    #define U8DC  unsigned char   code  * data  // data       code
    #define U8XC  unsigned char   code  * xdata // xdata      code
    #define U8CC  unsigned char   code  * code  // code       code
    
    #define U16DX unsigned short  xdata * data  // data       xdata
    #define U16IX unsigned short  xdata * idata // idata      xdata
    #define U16CC unsigned short  code  * code  // code       code
    
    #ifdef DATACT
    #define DOX
    #else // !DATACT
    #define DOX extern
    #endif // DATACT
    
    #define UB  DOX   U1      // bit
    #define UC  DOX   U8      // unsigned char
    #define UI  DOX   U16     // unsigned short
    #define UL  DOX   U32     // unsigned long
    
    #define UBC DOX   UB8     // unsigned char bdata
    #define UCI DOX   UI8     // unsigned char idata
    #define UCX DOX   UX8     // unsigned char xdata
    //#define UXR DOX   UX8     // unsigned char xdata
    //#define UCC DOX   UC8     // unsigned char code
    #define UII DOX   UI16    // unsigned short idata
    #define UIX DOX   UX16    // unsigned short xdata
    //#define UIC DOX   UC16    // unsigned short code
    #define ULI DOX   UI32
    #define ULX DOX   UX32
    #define UCDI  DOX U8DI    // unsigned char  idata * data
    #define UCDX  DOX U8DX    // unsigned char  xdata * data
    #define UCIX  DOX U8IX    // unsigned char  xdata * idata
    #define UCCX  DOX U8CC    // unsigned char   code * code
    #define UCDC  DOX U8DC    // unsigned char   code * data
    #define UIDX  DOX U16DX   // unsigned short xdata * data
    #define UIIX  DOX U16IX   // unsigned short xdata * idata
    #define UIDC  DOX U8DC    // unsigned short  code * data
    
    
    
    #ifdef DATACT
    #define UXRG(name,addr) UX8 name _at_ addr
    #else
    #define UXRG(name,addr) extern UX8 name
    #endif
    
    #define FALSE 0
    #define TRUE  ~FALSE
    
    
    


    Erik

  • Superb!

    Thanks both of you for your valuable input!

    --Geert