Don’t use libc’s random/rand

This commit is contained in:
Lior Halphon 2019-06-14 16:49:41 +03:00
parent 5a04054145
commit 66b814a226
6 changed files with 73 additions and 24 deletions

View File

@ -9,8 +9,10 @@
#include <sys/select.h>
#include <unistd.h>
#endif
#include "random.h"
#include "gb.h"
#ifdef DISABLE_REWIND
#define GB_rewind_free(...)
#define GB_rewind_push(...)
@ -565,7 +567,7 @@ static void reset_ram(GB_gameboy_t *gb)
case GB_MODEL_CGB_E:
case GB_MODEL_AGB: /* Unverified */
for (unsigned i = 0; i < gb->ram_size; i++) {
gb->ram[i] = (random() & 0xFF);
gb->ram[i] = GB_random();
}
break;
@ -573,12 +575,12 @@ static void reset_ram(GB_gameboy_t *gb)
case GB_MODEL_SGB_NTSC: /* Unverified*/
case GB_MODEL_SGB_PAL: /* Unverified */
for (unsigned i = 0; i < gb->ram_size; i++) {
gb->ram[i] = (random() & 0xFF);
gb->ram[i] = GB_random();
if (i & 0x100) {
gb->ram[i] &= random();
gb->ram[i] &= GB_random();
}
else {
gb->ram[i] |= random();
gb->ram[i] |= GB_random();
}
}
break;
@ -586,7 +588,7 @@ static void reset_ram(GB_gameboy_t *gb)
case GB_MODEL_SGB2:
for (unsigned i = 0; i < gb->ram_size; i++) {
gb->ram[i] = 0x55;
gb->ram[i] ^= random() & random() & random();
gb->ram[i] ^= GB_random() & GB_random() & GB_random();
}
break;
@ -596,7 +598,7 @@ static void reset_ram(GB_gameboy_t *gb)
gb->ram[i] = 0;
}
else {
gb->ram[i] = (random() | random() | random() | random()) & 0xFF;
gb->ram[i] = GB_random() | GB_random() | GB_random() | GB_random();
}
}
break;
@ -609,7 +611,7 @@ static void reset_ram(GB_gameboy_t *gb)
case GB_MODEL_CGB_E:
case GB_MODEL_AGB:
for (unsigned i = 0; i < sizeof(gb->hram); i++) {
gb->hram[i] = (random() & 0xFF);
gb->hram[i] = GB_random();
}
break;
@ -619,10 +621,10 @@ static void reset_ram(GB_gameboy_t *gb)
case GB_MODEL_SGB2:
for (unsigned i = 0; i < sizeof(gb->hram); i++) {
if (i & 1) {
gb->hram[i] = random() | random() | random();
gb->hram[i] = GB_random() | GB_random() | GB_random();
}
else {
gb->hram[i] = random() & random() & random();
gb->hram[i] = GB_random() & GB_random() & GB_random();
}
}
break;
@ -642,10 +644,10 @@ static void reset_ram(GB_gameboy_t *gb)
case GB_MODEL_SGB2:
for (unsigned i = 0; i < 8; i++) {
if (i & 2) {
gb->oam[i] = random() & random() & random();
gb->oam[i] = GB_random() & GB_random() & GB_random();
}
else {
gb->oam[i] = random() | random() | random();
gb->oam[i] = GB_random() | GB_random() | GB_random();
}
}
for (unsigned i = 8; i < sizeof(gb->oam); i++) {
@ -669,10 +671,10 @@ static void reset_ram(GB_gameboy_t *gb)
uint8_t temp;
for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) {
if (i & 1) {
temp = random() & random() & random();
temp = GB_random() & GB_random() & GB_random();
}
else {
temp = random() | random() | random();
temp = GB_random() | GB_random() | GB_random();
}
gb->apu.wave_channel.wave_form[i * 2] = temp >> 4;
gb->apu.wave_channel.wave_form[i * 2 + 1] = temp & 0xF;
@ -684,13 +686,13 @@ static void reset_ram(GB_gameboy_t *gb)
}
for (unsigned i = 0; i < sizeof(gb->extra_oam); i++) {
gb->extra_oam[i] = (random() & 0xFF);
gb->extra_oam[i] = GB_random();
}
if (GB_is_cgb(gb)) {
for (unsigned i = 0; i < 64; i++) {
gb->background_palettes_data[i] = random() & 0xFF; /* Doesn't really matter as the boot ROM overrides it anyway*/
gb->sprite_palettes_data[i] = random() & 0xFF;
gb->background_palettes_data[i] = GB_random(); /* Doesn't really matter as the boot ROM overrides it anyway*/
gb->sprite_palettes_data[i] = GB_random();
}
for (unsigned i = 0; i < 32; i++) {
GB_palette_changed(gb, true, i * 2);

38
Core/random.c Normal file
View File

@ -0,0 +1,38 @@
#include "random.h"
#include <time.h>
static uint64_t seed;
static bool enabled = true;
uint8_t GB_random(void)
{
if (!enabled) return 0;
seed *= 0x27BB2EE687B0B0FDL;
seed += 0xB504F32D;
return seed >> 56;
}
uint32_t GB_random32(void)
{
GB_random();
return seed >> 32;
}
void GB_random_seed(uint64_t new_seed)
{
seed = new_seed;
}
void GB_random_set_enabled(bool enable)
{
enabled = enable;
}
static void __attribute__((constructor)) init_seed(void)
{
seed = time(NULL);
for (unsigned i = 64; i--;) {
GB_random();
}
}

12
Core/random.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef random_h
#define random_h
#include <stdint.h>
#include <stdbool.h>
uint8_t GB_random(void);
uint32_t GB_random32(void);
void GB_random_seed(uint64_t seed);
void GB_random_set_enabled(bool enable);
#endif /* random_h */

View File

@ -1,4 +1,5 @@
#include "gb.h"
#include "random.h"
#include <math.h>
#define INTRO_ANIMATION_LENGTH 200
@ -682,7 +683,7 @@ static double fm_sweep(double phase)
}
static double random_double(void)
{
return ((random() % 0x10001) - 0x8000) / (double) 0x8000;
return ((signed)(GB_random32() % 0x10001) - 0x8000) / (double) 0x8000;
}
bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count)

View File

@ -16,12 +16,7 @@
#endif
#include <Core/gb.h>
/* Disable all randomness during automatic tests */
long random(void)
{
return 0;
}
#include <Core/random.h>
static bool running = false;
static char *filename;
@ -262,6 +257,8 @@ int main(int argc, char **argv)
bool dmg = false;
const char *boot_rom_path = NULL;
GB_random_set_enabled(false);
for (unsigned i = 1; i < argc; i++) {
if (strcmp(argv[i], "--dmg") == 0) {

View File

@ -243,7 +243,6 @@ else
CC = gcc
TARGET := $(TARGET_NAME)_libretro.dll
SHARED := -shared -static-libgcc -static-libstdc++ -s -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined
CFLAGS += -Drandom=rand
endif
TARGET := $(CORE_DIR)/build/bin/$(TARGET)