/* vim: set filetype=c:
 *
 * random.c86  -  Simple random number generator
 *
 * Copyright (C) 2003-2007 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: random.c86,v 1.4 2007/01/06 18:30:51 gkminix Exp $
 */


/* Include target system C headers */
#include <common.h86>
#include <i386/misc.h86>
#include <i386/time.h86>



/*
 * The random value gets computed by multiplying a 32-bit seed value
 * with RANDMUL and then adding RANDADD. Due to rounding this gives
 * a resulting number which jumps around the 32-bit number which might
 * seem as random (but isn't of course). However, this is absolutely
 * sufficient for the bootrom purposes.
 */
#define RANDMUL	0x015A4E35
#define RANDADD	0x5DA0



/*
 * Variables local to this module
 */
static unsigned long seed = 0;			/* Seed value */



/*
 * Generate a random nunber
 */
unsigned long random(unsigned long mask)
{
  /* Setup seed */
  if (seed == 0)
	seed = get_ticks();

  /* Compute new seed and return value */
  seed = ((seed * RANDMUL) + RANDADD) >> 1;
  return(seed & mask);
}

