| 0 |      1 | // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|  |      2 | // All rights reserved.
 | 
|  |      3 | // This component and the accompanying materials are made available
 | 
|  |      4 | // under the terms of the License "Eclipse Public License v1.0"
 | 
|  |      5 | // which accompanies this distribution, and is available
 | 
|  |      6 | // at the URL "http://www.eclipse.org/legal/epl-v10.html".
 | 
|  |      7 | //
 | 
|  |      8 | // Initial Contributors:
 | 
|  |      9 | // Nokia Corporation - initial contribution.
 | 
|  |     10 | //
 | 
|  |     11 | // Contributors:
 | 
|  |     12 | //
 | 
|  |     13 | // Description:
 | 
|  |     14 | // e32test\nkernsa\testutils.cpp
 | 
|  |     15 | // 
 | 
|  |     16 | //
 | 
|  |     17 | 
 | 
|  |     18 | #include <nktest/nkutils.h>
 | 
|  |     19 | 
 | 
|  |     20 | extern "C" {
 | 
|  |     21 | TUint32 random(TUint32* aSeed)
 | 
|  |     22 | 	{
 | 
|  |     23 | 	TUint32 x = aSeed[0];
 | 
|  |     24 | 	TUint32 r3 = x >> 1;
 | 
|  |     25 | 	r3 |= (aSeed[1] << 31);
 | 
|  |     26 | 	aSeed[1] = x & 1;
 | 
|  |     27 | 	r3 ^= (x << 12);
 | 
|  |     28 | 	x = r3 ^ (r3 >> 20);
 | 
|  |     29 | 	aSeed[0] = x;
 | 
|  |     30 | 	return x;
 | 
|  |     31 | 	}
 | 
|  |     32 | 
 | 
|  |     33 | void setup_block(TUint32* aBlock, TInt aNumWords)
 | 
|  |     34 | 	{
 | 
|  |     35 | 	TUint32 seed[2];
 | 
|  |     36 | 	seed[0] = aBlock[0];
 | 
|  |     37 | 	seed[1] = 0;
 | 
|  |     38 | 	TInt i;
 | 
|  |     39 | 	for (i=1; i<aNumWords; ++i)
 | 
|  |     40 | 		*++aBlock = random(seed);
 | 
|  |     41 | 	}
 | 
|  |     42 | 
 | 
|  |     43 | void setup_block_cpu(TUint32* aBlock, TInt aNumWords)
 | 
|  |     44 | 	{
 | 
|  |     45 | 	TUint32 seed[2];
 | 
|  |     46 | 	seed[0] = aBlock[0];
 | 
|  |     47 | 	seed[1] = 0;
 | 
|  |     48 | 	aBlock[1] = NKern::CurrentCpu();
 | 
|  |     49 | 	TInt i;
 | 
|  |     50 | 	for (i=2; i<aNumWords; ++i)
 | 
|  |     51 | 		aBlock[i] = random(seed) ^ NKern::CurrentCpu();
 | 
|  |     52 | 	}
 | 
|  |     53 | 
 | 
|  |     54 | TBool verify_block(const TUint32* aBlock, TInt aNumWords)
 | 
|  |     55 | 	{
 | 
|  |     56 | 	TUint32 seed[2];
 | 
|  |     57 | 	seed[0] = aBlock[0];
 | 
|  |     58 | 	seed[1] = 0;
 | 
|  |     59 | 	TInt i;
 | 
|  |     60 | 	for (i=1; i<aNumWords; ++i)
 | 
|  |     61 | 		{
 | 
|  |     62 | 		TUint32 x = random(seed);
 | 
|  |     63 | 		if (aBlock[i] != x)
 | 
|  |     64 | 			{
 | 
|  |     65 | 			KPrintf("Verify block failed: expected %08x got %08x", x, aBlock[i]);
 | 
|  |     66 | 			return FALSE;
 | 
|  |     67 | 			}
 | 
|  |     68 | 		}
 | 
|  |     69 | 	return TRUE;
 | 
|  |     70 | 	}
 | 
|  |     71 | 
 | 
|  |     72 | TBool verify_block_no_trace(const TUint32* aBlock, TInt aNumWords)
 | 
|  |     73 | 	{
 | 
|  |     74 | 	TUint32 seed[2];
 | 
|  |     75 | 	seed[0] = aBlock[0];
 | 
|  |     76 | 	seed[1] = 0;
 | 
|  |     77 | 	TInt i;
 | 
|  |     78 | 	for (i=1; i<aNumWords; ++i)
 | 
|  |     79 | 		{
 | 
|  |     80 | 		TUint32 x = random(seed);
 | 
|  |     81 | 		if (aBlock[i] != x)
 | 
|  |     82 | 			return FALSE;
 | 
|  |     83 | 		}
 | 
|  |     84 | 	return TRUE;
 | 
|  |     85 | 	}
 | 
|  |     86 | 
 | 
|  |     87 | TInt verify_block_cpu(const TUint32* aBlock, TInt aNumWords)
 | 
|  |     88 | 	{
 | 
|  |     89 | 	TUint32 seed[2];
 | 
|  |     90 | 	seed[0] = aBlock[0];
 | 
|  |     91 | 	seed[1] = 0;
 | 
|  |     92 | 	TUint32 cpu = aBlock[1];
 | 
|  |     93 | 	TInt i;
 | 
|  |     94 | 	for (i=2; i<aNumWords; ++i)
 | 
|  |     95 | 		{
 | 
|  |     96 | 		TUint32 x = random(seed);
 | 
|  |     97 | 		TUint32 y = aBlock[i] ^ x;
 | 
|  |     98 | 		if (y != cpu)
 | 
|  |     99 | 			{
 | 
|  |    100 | 			KPrintf("Verify block with CPU failed: expected %08x got %08x (XOR %08x)", x, aBlock[i], y);
 | 
|  |    101 | 			return -1;
 | 
|  |    102 | 			}
 | 
|  |    103 | 		}
 | 
|  |    104 | 	return cpu;
 | 
|  |    105 | 	}
 | 
|  |    106 | 
 | 
|  |    107 | TInt verify_block_cpu_no_trace(const TUint32* aBlock, TInt aNumWords)
 | 
|  |    108 | 	{
 | 
|  |    109 | 	TUint32 seed[2];
 | 
|  |    110 | 	seed[0] = aBlock[0];
 | 
|  |    111 | 	seed[1] = 0;
 | 
|  |    112 | 	TUint32 cpu = aBlock[1];
 | 
|  |    113 | 	TInt i;
 | 
|  |    114 | 	for (i=2; i<aNumWords; ++i)
 | 
|  |    115 | 		{
 | 
|  |    116 | 		TUint32 x = random(seed);
 | 
|  |    117 | 		TUint32 y = aBlock[i] ^ x;
 | 
|  |    118 | 		if (y != cpu)
 | 
|  |    119 | 			return -1;
 | 
|  |    120 | 		}
 | 
|  |    121 | 	return cpu;
 | 
|  |    122 | 	}
 | 
|  |    123 | }
 | 
|  |    124 | 
 | 
|  |    125 | 
 | 
|  |    126 | CircBuf* CircBuf::New(TInt aSlots)
 | 
|  |    127 | 	{
 | 
|  |    128 | 	CircBuf* p = new CircBuf();
 | 
|  |    129 | 	p->iSlotCount = aSlots;
 | 
|  |    130 | 	p->iGetIndex = 0;
 | 
|  |    131 | 	p->iPutIndex = 0;
 | 
|  |    132 | 	p->iBufBase = (TUint32*)malloc(aSlots*sizeof(TUint32));
 | 
|  |    133 | 	if (!p->iBufBase)
 | 
|  |    134 | 		{
 | 
|  |    135 | 		delete p;
 | 
|  |    136 | 		p = 0;
 | 
|  |    137 | 		}
 | 
|  |    138 | 	return p;
 | 
|  |    139 | 	}
 | 
|  |    140 | 
 | 
|  |    141 | CircBuf::CircBuf()
 | 
|  |    142 | 	:	iLock(TSpinLock::EOrderGenericIrqLow1)
 | 
|  |    143 | 	{
 | 
|  |    144 | 	}
 | 
|  |    145 | 
 | 
|  |    146 | CircBuf::~CircBuf()
 | 
|  |    147 | 	{
 | 
|  |    148 | 	free(iBufBase);
 | 
|  |    149 | 	}
 | 
|  |    150 | 
 | 
|  |    151 | TInt CircBuf::TryGet(TUint32& aOut)
 | 
|  |    152 | 	{
 | 
|  |    153 | 	TInt r = KErrUnderflow;
 | 
|  |    154 | 	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
 | 
|  |    155 | 	if (iGetIndex != iPutIndex)
 | 
|  |    156 | 		{
 | 
|  |    157 | 		aOut = iBufBase[iGetIndex++];
 | 
|  |    158 | 		if (iGetIndex == iSlotCount)
 | 
|  |    159 | 			iGetIndex = 0;
 | 
|  |    160 | 		r = KErrNone;
 | 
|  |    161 | 		}
 | 
|  |    162 | 	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
 | 
|  |    163 | 	return r;
 | 
|  |    164 | 	}
 | 
|  |    165 | 
 | 
|  |    166 | TInt CircBuf::TryPut(TUint32 aIn)
 | 
|  |    167 | 	{
 | 
|  |    168 | 	TInt r = KErrOverflow;
 | 
|  |    169 | 	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
 | 
|  |    170 | 	TInt nextPut = iPutIndex + 1;
 | 
|  |    171 | 	if (nextPut == iSlotCount)
 | 
|  |    172 | 		nextPut = 0;
 | 
|  |    173 | 	if (iGetIndex != nextPut)
 | 
|  |    174 | 		{
 | 
|  |    175 | 		iBufBase[iPutIndex] = aIn;
 | 
|  |    176 | 		iPutIndex = nextPut;
 | 
|  |    177 | 		r = KErrNone;
 | 
|  |    178 | 		}
 | 
|  |    179 | 	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
 | 
|  |    180 | 	return r;
 | 
|  |    181 | 	}
 | 
|  |    182 | 
 | 
|  |    183 | TUint32 CircBuf::Get()
 | 
|  |    184 | 	{
 | 
|  |    185 | 	TUint32 x;
 | 
|  |    186 | 	while(TryGet(x)!=KErrNone)
 | 
|  |    187 | 		NKern::Sleep(1);
 | 
|  |    188 | 	return x;
 | 
|  |    189 | 	}
 | 
|  |    190 | 
 | 
|  |    191 | void CircBuf::Put(TUint32 aIn)
 | 
|  |    192 | 	{
 | 
|  |    193 | 	while(TryPut(aIn)!=KErrNone)
 | 
|  |    194 | 		NKern::Sleep(1);
 | 
|  |    195 | 	}
 | 
|  |    196 | 
 | 
|  |    197 | void CircBuf::Reset()
 | 
|  |    198 | 	{
 | 
|  |    199 | 	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
 | 
|  |    200 | 	iPutIndex = iGetIndex = 0;
 | 
|  |    201 | 	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
 | 
|  |    202 | 	}
 | 
|  |    203 | 
 | 
|  |    204 | TInt CircBuf::Count()
 | 
|  |    205 | 	{
 | 
|  |    206 | 	TInt irq = __SPIN_LOCK_IRQSAVE(iLock);
 | 
|  |    207 | 	TInt r = iPutIndex - iGetIndex;
 | 
|  |    208 | 	if (r < 0)
 | 
|  |    209 | 		r += iSlotCount;
 | 
|  |    210 | 	__SPIN_UNLOCK_IRQRESTORE(iLock,irq);
 | 
|  |    211 | 	return r;
 | 
|  |    212 | 	}
 |