/*-------------------------------------------------------------------------
   Rand function copy right information

   Copyright (C) 2006-2008. Lin, Rongrong <woody@palmmicro.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-------------------------------------------------------------------------*/

#include "type.h"
#include "core.h"
#include "apps.h"

UCHAR holdrand;

/*
volatile UCHAR iLastVar;

#define LAST_VAR_VAL	0x55

const UCHAR _cStackOverflow[] = "Stack overflow!";

void CheckStack()
{
	if (iLastVar != LAST_VAR_VAL)
	{
		LcdDisplay(_cStackOverflow, 0);
		LcdDisplay(_cStackOverflow, 1);
		UdpDebugString(_cStackOverflow);
	}
}
*/

void srand(UCHAR seed)
{
	holdrand = seed;
//	iLastVar = LAST_VAR_VAL;
}

UCHAR _CalcRand(UCHAR iVal) _naked
{
	iVal;
	_asm
	push	ix
	ld		ix,#0
	add		ix,sp
	ld		b,4(ix)		; A random number seed ¡ª make the result "more random"
	ld		a,r
    add		a,b
    ld		l,a
	pop	ix
	ret
	_endasm;
}

UCHAR rand()
{
	UCHAR iVal;
	iVal = _CalcRand(holdrand);
	holdrand += iVal;
	return iVal;
}

void rand_array(PCHAR pDst, UCHAR iLen)
{
	do
	{
		*pDst ++ = rand();
		iLen --;
	} while (iLen);
}
