2017-05-30 03:38:38 +02:00
|
|
|
/*
|
|
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the 86Box distribution.
|
|
|
|
|
*
|
|
|
|
|
* A better random number generation, used for floppy weak bits
|
|
|
|
|
* and network MAC address generation.
|
|
|
|
|
*
|
2020-03-25 00:46:02 +02:00
|
|
|
*
|
2017-05-30 03:38:38 +02:00
|
|
|
*
|
|
|
|
|
* Author: Miran Grca, <mgrca8@gmail.com>
|
2018-10-02 22:54:28 +02:00
|
|
|
* Copyright 2016-2018 Miran Grca.
|
2017-05-30 03:38:38 +02:00
|
|
|
*/
|
2017-05-05 01:49:42 +02:00
|
|
|
#include <stdint.h>
|
2016-09-07 20:59:08 +02:00
|
|
|
#include <stdlib.h>
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/random.h>
|
2016-09-07 20:59:08 +02:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
#if !(defined(__i386__) || defined(__x86_64__))
|
|
|
|
|
# include <time.h>
|
2019-03-20 02:36:13 +08:00
|
|
|
#endif
|
2016-09-07 20:59:08 +02:00
|
|
|
|
2016-09-07 21:09:45 +02:00
|
|
|
uint32_t preconst = 0x6ED9EBA1;
|
2016-09-07 20:59:08 +02:00
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
static __inline uint32_t
|
|
|
|
|
rotl32c(uint32_t x, uint32_t n)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2017-09-04 01:52:29 -04:00
|
|
|
#if 0
|
2016-09-07 20:59:08 +02:00
|
|
|
assert (n<32);
|
2017-09-04 01:52:29 -04:00
|
|
|
#endif
|
2022-09-18 17:11:43 -04:00
|
|
|
return (x << n) | (x >> (-n & 31));
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
static __inline uint32_t
|
|
|
|
|
rotr32c(uint32_t x, uint32_t n)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2017-09-04 01:52:29 -04:00
|
|
|
#if 0
|
2016-09-07 20:59:08 +02:00
|
|
|
assert (n<32);
|
2017-09-04 01:52:29 -04:00
|
|
|
#endif
|
2022-09-18 17:11:43 -04:00
|
|
|
return (x >> n) | (x << (-n & 31));
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
#define ROTATE_LEFT rotl32c
|
2016-09-07 20:59:08 +02:00
|
|
|
|
|
|
|
|
#define ROTATE_RIGHT rotr32c
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
static __inline unsigned long long
|
|
|
|
|
rdtsc(void)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2016-09-07 20:59:08 +02:00
|
|
|
unsigned hi, lo;
|
2022-09-18 17:11:43 -04:00
|
|
|
# ifdef _MSC_VER
|
2017-05-05 01:49:42 +02:00
|
|
|
__asm {
|
|
|
|
|
rdtsc
|
|
|
|
|
mov hi, edx ; EDX:EAX is already standard return!!
|
|
|
|
|
mov lo, eax
|
|
|
|
|
}
|
2022-09-18 17:11:43 -04:00
|
|
|
# else
|
|
|
|
|
__asm__ __volatile__("rdtsc"
|
|
|
|
|
: "=a"(lo), "=d"(hi));
|
|
|
|
|
# endif
|
|
|
|
|
return ((unsigned long long) lo) | (((unsigned long long) hi) << 32);
|
2017-05-05 01:49:42 +02:00
|
|
|
#else
|
2022-09-18 17:11:43 -04:00
|
|
|
return time(NULL);
|
2019-03-20 02:36:13 +08:00
|
|
|
#endif
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
static uint32_t
|
|
|
|
|
RDTSC(void)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
return (uint32_t) (rdtsc());
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
static void
|
|
|
|
|
random_twist(uint32_t *val)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
*val = ROTATE_LEFT(*val, rand() % 32);
|
|
|
|
|
*val ^= 0x5A827999;
|
|
|
|
|
*val = ROTATE_RIGHT(*val, rand() % 32);
|
|
|
|
|
*val ^= 0x4ED32706;
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
uint8_t
|
|
|
|
|
random_generate(void)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
uint16_t r = 0;
|
|
|
|
|
r = (RDTSC() ^ ROTATE_LEFT(preconst, rand() % 32)) % 256;
|
|
|
|
|
random_twist(&preconst);
|
|
|
|
|
return (r & 0xff);
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:11:43 -04:00
|
|
|
void
|
|
|
|
|
random_init(void)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2022-09-18 17:11:43 -04:00
|
|
|
uint32_t seed = RDTSC();
|
|
|
|
|
srand(seed);
|
|
|
|
|
return;
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|