Home | History | Annotate | Download | only in b_BasicEm
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /* ---- includes ----------------------------------------------------------- */
     18 
     19 #include "b_BasicEm/Functions.h"
     20 #include "b_BasicEm/Context.h"
     21 
     22 /* ---- related objects  --------------------------------------------------- */
     23 
     24 /* ---- typedefs ----------------------------------------------------------- */
     25 
     26 /* ---- constants ---------------------------------------------------------- */
     27 
     28 /* ---- globals   ---------------------------------------------------------- */
     29 
     30 /* ------------------------------------------------------------------------- */
     31 
     32 /* ========================================================================= */
     33 /*                                                                           */
     34 /* ---- \ghd{ external functions } ----------------------------------------- */
     35 /*                                                                           */
     36 /* ========================================================================= */
     37 
     38 /* ------------------------------------------------------------------------- */
     39 
     40 uint16 bbs_swapBytes( uint16 valA )
     41 {
     42 	return ( ( valA >> 8 ) & 0x00FF ) | ( ( valA << 8 ) & 0xFF00 );
     43 }
     44 
     45 /* ------------------------------------------------------------------------- */
     46 
     47 uint32 bbs_memWrite32( const void* ptrA,
     48 					   uint16* memPtrA )
     49 {
     50 	uint32 valL = *( uint32* )ptrA;
     51 
     52 	#ifdef HW_BIG_ENDIAN
     53 		*memPtrA++ = bbs_swapBytes( ( uint16 )( ( valL >> 0  ) & 0xFFFF ) );
     54 		*memPtrA++ = bbs_swapBytes( ( uint16 )( ( valL >> 16 ) & 0xFFFF ) );
     55 	#else
     56 		*memPtrA++ = ( valL >> 0  ) & 0xFFFF;
     57 		*memPtrA++ = ( valL >> 16 ) & 0xFFFF;
     58 	#endif
     59 
     60 
     61 	return bbs_SIZEOF16( uint32 );
     62 }
     63 
     64 /* ------------------------------------------------------------------------- */
     65 
     66 uint32 bbs_memRead32( void* ptrA,
     67 					  const uint16* memPtrA )
     68 {
     69 	uint32 valL = 0;
     70 
     71 	#ifdef HW_BIG_ENDIAN
     72 		valL |= ( ( uint32 )bbs_swapBytes( *memPtrA++ ) << 0  );
     73 		valL |= ( ( uint32 )bbs_swapBytes( *memPtrA++ ) << 16 );
     74 	#else
     75 		valL |= ( ( uint32 )*memPtrA++ << 0  );
     76 		valL |= ( ( uint32 )*memPtrA++ << 16 );
     77 	#endif
     78 
     79 	*( uint32* )ptrA = valL;
     80 
     81 	return bbs_SIZEOF16( uint32 );
     82 }
     83 
     84 /* ------------------------------------------------------------------------- */
     85 
     86 uint32 bbs_memPeek32( const uint16* memPtrA )
     87 {
     88 	uint32 valL = 0;
     89 
     90 	#ifdef HW_BIG_ENDIAN
     91 		valL |= ( ( uint32 )bbs_swapBytes( *memPtrA++ ) << 0  );
     92 		valL |= ( ( uint32 )bbs_swapBytes( *memPtrA++ ) << 16 );
     93 	#else
     94 		valL |= ( ( uint32 )*memPtrA++ << 0  );
     95 		valL |= ( ( uint32 )*memPtrA++ << 16 );
     96 	#endif
     97 
     98 	return valL;
     99 }
    100 
    101 /* ------------------------------------------------------------------------- */
    102 
    103 uint32 bbs_memWrite16( const void* ptrA,
    104 					   uint16* memPtrA )
    105 {
    106 	#ifdef HW_BIG_ENDIAN
    107 		*memPtrA++ = bbs_swapBytes( *( uint16* )ptrA );
    108 	#else
    109 		*memPtrA++ = *( uint16* )ptrA;
    110 	#endif
    111 	return bbs_SIZEOF16( uint16 );
    112 }
    113 
    114 /* ------------------------------------------------------------------------- */
    115 
    116 uint32 bbs_memRead16( void* ptrA,
    117 					  const uint16* memPtrA )
    118 {
    119 	#ifdef HW_BIG_ENDIAN
    120 		*( uint16* )ptrA = bbs_swapBytes( *memPtrA++ );
    121 	#else
    122 		*( uint16* )ptrA = *memPtrA++;
    123 	#endif
    124 
    125 	return bbs_SIZEOF16( uint16 );
    126 }
    127 
    128 /* ------------------------------------------------------------------------- */
    129 
    130 uint32 bbs_memWrite32Arr( struct bbs_Context* cpA,
    131 						  const void* ptrA,
    132 						  uint32 sizeA, uint16* memPtrA )
    133 {
    134 	uint32 iL;
    135 	const uint32* srcL = ( uint32* )ptrA;
    136 
    137 	if( bbs_Context_error( cpA ) ) return 0;
    138 
    139 	for( iL = 0; iL < sizeA; iL++ )
    140 	{
    141 		memPtrA += bbs_memWrite32( srcL++, memPtrA );
    142 	}
    143 
    144 	return sizeA * bbs_SIZEOF16( uint32 );
    145 }
    146 
    147 /* ------------------------------------------------------------------------- */
    148 
    149 uint32 bbs_memRead32Arr( struct bbs_Context* cpA,
    150 						 void* ptrA,
    151 						 uint32 sizeA,
    152 						 const uint16* memPtrA )
    153 {
    154 	uint32 iL;
    155 	uint32* dstL = ( uint32* )ptrA;
    156 
    157 	if( bbs_Context_error( cpA ) ) return 0;
    158 
    159 	for( iL = 0; iL < sizeA; iL++ )
    160 	{
    161 		memPtrA += bbs_memRead32( dstL++, memPtrA );
    162 	}
    163 
    164 	return sizeA * bbs_SIZEOF16( uint32 );
    165 }
    166 
    167 /* ------------------------------------------------------------------------- */
    168 
    169 uint32 bbs_memWrite16Arr( struct bbs_Context* cpA,
    170 						  const void* ptrA,
    171 						  uint32 sizeA,
    172 						  uint16* memPtrA )
    173 {
    174 	uint32 iL;
    175 	const uint16* srcL = ( uint16* )ptrA;
    176 
    177 	if( bbs_Context_error( cpA ) ) return 0;
    178 
    179 	for( iL = 0; iL < sizeA; iL++ )
    180 	{
    181 		memPtrA += bbs_memWrite16( srcL++, memPtrA );
    182 	}
    183 
    184 	return sizeA * bbs_SIZEOF16( uint16 );
    185 }
    186 
    187 /* ------------------------------------------------------------------------- */
    188 
    189 uint32 bbs_memRead16Arr( struct bbs_Context* cpA,
    190 						 void* ptrA,
    191 						 uint32 sizeA,
    192 						 const uint16* memPtrA )
    193 {
    194 	uint32 iL;
    195 	uint16* dstL = ( uint16* )ptrA;
    196 
    197 	if( bbs_Context_error( cpA ) ) return 0;
    198 
    199 	for( iL = 0; iL < sizeA; iL++ )
    200 	{
    201 		memPtrA += bbs_memRead16( dstL++, memPtrA );
    202 	}
    203 
    204 	return sizeA * bbs_SIZEOF16( uint16 );
    205 }
    206 
    207 /* ------------------------------------------------------------------------- */
    208 
    209 uint32 bbs_memWriteUInt32( uint32 valA,
    210 						   uint16* memPtrA )
    211 {
    212 	#ifdef HW_BIG_ENDIAN
    213 		*memPtrA++ = bbs_swapBytes( ( uint16 )( ( valA >> 0  ) & 0xFFFF ) );
    214 		*memPtrA++ = bbs_swapBytes( ( uint16 )( ( valA >> 16 ) & 0xFFFF ) );
    215 	#else
    216 		*memPtrA++ = ( valA >> 0  ) & 0xFFFF;
    217 		*memPtrA++ = ( valA >> 16 ) & 0xFFFF;
    218 	#endif
    219 
    220 	return bbs_SIZEOF16( valA );
    221 }
    222 
    223 /* ------------------------------------------------------------------------- */
    224 
    225 uint32 bbs_memWriteUInt16( uint16 valA,
    226 						   uint16* memPtrA )
    227 {
    228 	#ifdef HW_BIG_ENDIAN
    229 		*memPtrA++ = bbs_swapBytes( valA );
    230 	#else
    231 		*memPtrA++ = valA;
    232 	#endif
    233 
    234 	return bbs_SIZEOF16( valA );
    235 }
    236 
    237 /* ------------------------------------------------------------------------- */
    238 
    239 uint32 bbs_memReadVersion32( struct bbs_Context* cpA,
    240 							 uint32* versionPtrA,
    241 							 uint32 refVersionA,
    242 							 const uint16* memPtrA )
    243 {
    244 	if( bbs_Context_error( cpA ) ) return 0;
    245 
    246 	bbs_memRead32( versionPtrA, memPtrA );
    247 	if( *versionPtrA > refVersionA )
    248 	{
    249 		bbs_ERR0( bbs_ERR_WRONG_VERSION, "uint32 bbs_memReadVersion32( .... ):\n"
    250 			       "Data format is newer than software or corrupt\n" );
    251 	}
    252 	return bbs_SIZEOF16( uint32 );
    253 }
    254 
    255 /* ------------------------------------------------------------------------- */
    256 
    257 uint16 bbs_memCheckSum16( const uint16* memPtrA, uint32 sizeA )
    258 {
    259 	uint32 iL;
    260 	uint16 sumL = 0;
    261 	for( iL = 0; iL < sizeA; iL++ )
    262 	{
    263 		#ifdef HW_BIG_ENDIAN
    264 			sumL += bbs_swapBytes( memPtrA[ iL ] );
    265 		#else
    266 			sumL += memPtrA[ iL ];
    267 		#endif
    268 	}
    269 
    270 	return sumL;
    271 }
    272 
    273 /* ------------------------------------------------------------------------- */
    274 
    275