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