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.
|
|
|
|
|
*
|
|
|
|
|
* Version: @(#)disc_random.c 1.0.0 2017/05/30
|
|
|
|
|
*
|
|
|
|
|
* Author: Miran Grca, <mgrca8@gmail.com>
|
|
|
|
|
* Copyright 2016-2017 Miran Grca.
|
|
|
|
|
*/
|
2016-09-07 20:59:08 +02:00
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2017-05-05 01:49:42 +02:00
|
|
|
#include <stdint.h>
|
2016-09-07 20:59:08 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
|
|
#include "disc_random.h"
|
|
|
|
|
|
2016-09-07 21:09:45 +02:00
|
|
|
uint32_t preconst = 0x6ED9EBA1;
|
2016-09-07 20:59:08 +02:00
|
|
|
|
|
|
|
|
static __inline__ uint32_t rotl32c (uint32_t x, uint32_t n)
|
|
|
|
|
{
|
|
|
|
|
assert (n<32);
|
|
|
|
|
return (x<<n) | (x>>(-n&31));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __inline__ uint32_t rotr32c (uint32_t x, uint32_t n)
|
|
|
|
|
{
|
|
|
|
|
assert (n<32);
|
|
|
|
|
return (x>>n) | (x<<(-n&31));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define ROTATE_LEFT rotl32c
|
|
|
|
|
|
|
|
|
|
#define ROTATE_RIGHT rotr32c
|
|
|
|
|
|
|
|
|
|
static __inline__ unsigned long long rdtsc(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned hi, lo;
|
2017-05-05 01:49:42 +02:00
|
|
|
#ifdef __MSC__
|
|
|
|
|
__asm {
|
|
|
|
|
rdtsc
|
|
|
|
|
mov hi, edx ; EDX:EAX is already standard return!!
|
|
|
|
|
mov lo, eax
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
|
|
|
|
|
#endif
|
2016-09-07 20:59:08 +02:00
|
|
|
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 21:09:45 +02:00
|
|
|
static uint32_t RDTSC(void)
|
2016-09-07 20:59:08 +02:00
|
|
|
{
|
2016-09-07 21:09:45 +02:00
|
|
|
return (uint32_t) (rdtsc());
|
2016-09-07 20:59:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void disc_random_twist(uint32_t *val)
|
|
|
|
|
{
|
|
|
|
|
*val = ROTATE_LEFT(*val, rand() % 32);
|
|
|
|
|
*val ^= 0x5A827999;
|
|
|
|
|
*val = ROTATE_RIGHT(*val, rand() % 32);
|
|
|
|
|
*val ^= 0x4ED32706;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t disc_random_generate()
|
|
|
|
|
{
|
|
|
|
|
uint16_t r = 0;
|
|
|
|
|
r = (rand() ^ ROTATE_LEFT(preconst, rand() % 32)) % 256;
|
|
|
|
|
disc_random_twist(&preconst);
|
|
|
|
|
return (r & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void disc_random_init()
|
|
|
|
|
{
|
2016-09-07 21:09:45 +02:00
|
|
|
uint32_t seed = RDTSC();
|
2016-09-07 20:59:08 +02:00
|
|
|
srand(seed);
|
|
|
|
|
return;
|
|
|
|
|
}
|