Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

myendian.h

Go to the documentation of this file.
00001 //
00002 // /home/ms/sidplay/libsidplay/include/RCS/myendian.h,v
00003 //
00004 
00005 #ifndef __MYENDIAN_H
00006   #define __MYENDIAN_H
00007 
00008 #include "compconf.h"
00009 #include "mytypes.h"
00010 
00011 // This should never be true.
00012 #if defined(LO) || defined(HI) || defined(LOLO) || defined(LOHI) || defined(HILO) || defined(HIHI)
00013   #warning Redefinition of these values can cause trouble.
00014 #endif
00015 
00016 // For optional direct memory access.
00017 // First value in memory/array = index 0.
00018 // Second value in memory/array = index 1.
00019 
00020 // For a pair of bytes/words/longwords.
00021 #undef LO
00022 #undef HI
00023 
00024 // For two pairs of bytes/words/longwords.
00025 #undef LOLO
00026 #undef LOHI
00027 #undef HILO
00028 #undef HIHI
00029 
00030 #if defined(IS_BIG_ENDIAN)
00031 // byte-order: HI..3210..LO
00032   #define LO 1
00033   #define HI 0
00034   #define LOLO 3
00035   #define LOHI 2
00036   #define HILO 1
00037   #define HIHI 0
00038 #elif defined(IS_LITTLE_ENDIAN)
00039 // byte-order: LO..0123..HI
00040   #define LO 0
00041   #define HI 1
00042   #define LOLO 0
00043   #define LOHI 1
00044   #define HILO 2
00045   #define HIHI 3
00046 #else
00047   #error Define the endianess of the CPU in ``compconf.h'' !
00048 #endif
00049 
00050 union cpuLword
00051 {
00052         uword w[2];  // single 16-bit low and high word
00053         udword l;    // complete 32-bit longword
00054 };
00055 
00056 union cpuWord
00057 {
00058         ubyte b[2];  // single 8-bit low and high byte
00059         uword w;     // complete 16-bit word
00060 };
00061 
00062 union cpuLBword
00063 {
00064         ubyte b[4];  // single 8-bit bytes
00065         udword l;    // complete 32-bit longword
00066 };
00067 
00068 inline uword readEndian(ubyte hi, ubyte lo)
00073 {
00074   return(uword)(( (uword)hi << 8 ) + (uword)lo );
00075 }
00076 
00077 inline udword readEndian(ubyte hihi, ubyte hilo, ubyte hi, ubyte lo)
00082 {
00083   return(( (udword)hihi << 24 ) + ( (udword)hilo << 16 ) + 
00084                  ( (udword)hi << 8 ) + (udword)lo );
00085 }
00086 
00087 // Read a little-endian 16-bit word from two bytes in memory.
00088 inline uword readLEword(ubyte ptr[2])
00089 {
00090 #if defined(IS_LITTLE_ENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
00091         return *((uword*)ptr);
00092 #else
00093         return readEndian(ptr[1],ptr[0]);
00094 #endif
00095 }
00096 
00097 inline void writeLEword(ubyte ptr[2], uword someWord)
00101         {
00102 #if defined(IS_LITTLE_ENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
00103         *((uword*)ptr) = someWord;
00104 #else
00105         ptr[0] = (ubyte)(someWord & 0xFF);
00106         ptr[1] = (ubyte)(someWord >> 8);
00107 #endif
00108         }
00109 
00110 
00111 
00112 inline uword readBEword(ubyte ptr[2])
00116         {
00117 #if defined(IS_BIG_ENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
00118         return (uword)*((uword*)ptr);
00119 #else
00120         return (uword)( (((uword)ptr[0])<<8) + ((uword)ptr[1]) );
00121 #endif
00122         }
00123 
00124 
00125 // Read a big-endian 32-bit word from four bytes in memory.
00126 inline udword readBEdword(ubyte ptr[4])
00127 {
00128 #if defined(IS_BIG_ENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
00129         return *((udword*)ptr);
00130 #else
00131         return ( (((udword)ptr[0])<<24) + (((udword)ptr[1])<<16)
00132                         + (((udword)ptr[2])<<8) + ((udword)ptr[3]) );
00133 #endif
00134 }
00135 
00136 
00137 inline void writeBEword(ubyte ptr[2], uword someWord)
00141         {
00142 #if defined(IS_BIG_ENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
00143         *((uword*)ptr) = someWord;
00144 #else
00145         ptr[0] = (ubyte)(someWord >> 8);
00146         ptr[1] = (ubyte)(someWord & 0xFF);
00147 #endif
00148         }
00149 
00150 inline void writeBEdword(ubyte ptr[4], udword someDword)
00154         {
00155 #if defined(IS_BIG_ENDIAN) && defined(OPTIMIZE_ENDIAN_ACCESS)
00156         *((udword*)ptr) = someDword;
00157 #else
00158         ptr[0] = (ubyte)(someDword >> 24);
00159         ptr[1] = (ubyte)((someDword>>16) & 0xFF);
00160         ptr[2] = (ubyte)((someDword>>8) & 0xFF);
00161         ptr[3] = (ubyte)(someDword & 0xFF);
00162 #endif
00163         }
00164 
00165 
00166 
00167 inline uword convertEndianess( uword intelword )
00171         {
00172         uword hi = (uword)(intelword >> 8);
00173         uword lo = (uword)(intelword & 255);
00174         return (uword)(( lo << 8 ) + hi );
00175         }
00176 
00177 // Convert 32-bit little-endian word to big-endian order or vice versa.
00178 inline udword convertEndianess( udword inteldword )
00179 {
00180         udword hihi = inteldword >> 24;
00181         udword hilo = ( inteldword >> 16 ) & 0xFF;
00182         udword hi = ( inteldword >> 8 ) & 0xFF;
00183         udword lo = inteldword & 0xFF;
00184         return(( lo << 24 ) + ( hi << 16 ) + ( hilo << 8 ) + hihi );
00185 }
00186 
00187 #endif

Generated on Tue Feb 8 04:13:58 2005 for Esidplay by doxygen 1.3.3