Don’t use libc’s random/rand
This commit is contained in:
parent
5a04054145
commit
66b814a226
34
Core/gb.c
34
Core/gb.c
@ -9,8 +9,10 @@
|
|||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include "random.h"
|
||||||
#include "gb.h"
|
#include "gb.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef DISABLE_REWIND
|
#ifdef DISABLE_REWIND
|
||||||
#define GB_rewind_free(...)
|
#define GB_rewind_free(...)
|
||||||
#define GB_rewind_push(...)
|
#define GB_rewind_push(...)
|
||||||
@ -565,7 +567,7 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
case GB_MODEL_CGB_E:
|
case GB_MODEL_CGB_E:
|
||||||
case GB_MODEL_AGB: /* Unverified */
|
case GB_MODEL_AGB: /* Unverified */
|
||||||
for (unsigned i = 0; i < gb->ram_size; i++) {
|
for (unsigned i = 0; i < gb->ram_size; i++) {
|
||||||
gb->ram[i] = (random() & 0xFF);
|
gb->ram[i] = GB_random();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -573,12 +575,12 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
case GB_MODEL_SGB_NTSC: /* Unverified*/
|
case GB_MODEL_SGB_NTSC: /* Unverified*/
|
||||||
case GB_MODEL_SGB_PAL: /* Unverified */
|
case GB_MODEL_SGB_PAL: /* Unverified */
|
||||||
for (unsigned i = 0; i < gb->ram_size; i++) {
|
for (unsigned i = 0; i < gb->ram_size; i++) {
|
||||||
gb->ram[i] = (random() & 0xFF);
|
gb->ram[i] = GB_random();
|
||||||
if (i & 0x100) {
|
if (i & 0x100) {
|
||||||
gb->ram[i] &= random();
|
gb->ram[i] &= GB_random();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
gb->ram[i] |= random();
|
gb->ram[i] |= GB_random();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -586,7 +588,7 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
case GB_MODEL_SGB2:
|
case GB_MODEL_SGB2:
|
||||||
for (unsigned i = 0; i < gb->ram_size; i++) {
|
for (unsigned i = 0; i < gb->ram_size; i++) {
|
||||||
gb->ram[i] = 0x55;
|
gb->ram[i] = 0x55;
|
||||||
gb->ram[i] ^= random() & random() & random();
|
gb->ram[i] ^= GB_random() & GB_random() & GB_random();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -596,7 +598,7 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
gb->ram[i] = 0;
|
gb->ram[i] = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
gb->ram[i] = (random() | random() | random() | random()) & 0xFF;
|
gb->ram[i] = GB_random() | GB_random() | GB_random() | GB_random();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -609,7 +611,7 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
case GB_MODEL_CGB_E:
|
case GB_MODEL_CGB_E:
|
||||||
case GB_MODEL_AGB:
|
case GB_MODEL_AGB:
|
||||||
for (unsigned i = 0; i < sizeof(gb->hram); i++) {
|
for (unsigned i = 0; i < sizeof(gb->hram); i++) {
|
||||||
gb->hram[i] = (random() & 0xFF);
|
gb->hram[i] = GB_random();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -619,10 +621,10 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
case GB_MODEL_SGB2:
|
case GB_MODEL_SGB2:
|
||||||
for (unsigned i = 0; i < sizeof(gb->hram); i++) {
|
for (unsigned i = 0; i < sizeof(gb->hram); i++) {
|
||||||
if (i & 1) {
|
if (i & 1) {
|
||||||
gb->hram[i] = random() | random() | random();
|
gb->hram[i] = GB_random() | GB_random() | GB_random();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
gb->hram[i] = random() & random() & random();
|
gb->hram[i] = GB_random() & GB_random() & GB_random();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -642,10 +644,10 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
case GB_MODEL_SGB2:
|
case GB_MODEL_SGB2:
|
||||||
for (unsigned i = 0; i < 8; i++) {
|
for (unsigned i = 0; i < 8; i++) {
|
||||||
if (i & 2) {
|
if (i & 2) {
|
||||||
gb->oam[i] = random() & random() & random();
|
gb->oam[i] = GB_random() & GB_random() & GB_random();
|
||||||
}
|
}
|
||||||
else {
|
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++) {
|
for (unsigned i = 8; i < sizeof(gb->oam); i++) {
|
||||||
@ -669,10 +671,10 @@ static void reset_ram(GB_gameboy_t *gb)
|
|||||||
uint8_t temp;
|
uint8_t temp;
|
||||||
for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) {
|
for (unsigned i = 0; i < GB_IO_WAV_END - GB_IO_WAV_START; i++) {
|
||||||
if (i & 1) {
|
if (i & 1) {
|
||||||
temp = random() & random() & random();
|
temp = GB_random() & GB_random() & GB_random();
|
||||||
}
|
}
|
||||||
else {
|
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] = temp >> 4;
|
||||||
gb->apu.wave_channel.wave_form[i * 2 + 1] = temp & 0xF;
|
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++) {
|
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)) {
|
if (GB_is_cgb(gb)) {
|
||||||
for (unsigned i = 0; i < 64; i++) {
|
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->background_palettes_data[i] = GB_random(); /* Doesn't really matter as the boot ROM overrides it anyway*/
|
||||||
gb->sprite_palettes_data[i] = random() & 0xFF;
|
gb->sprite_palettes_data[i] = GB_random();
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < 32; i++) {
|
for (unsigned i = 0; i < 32; i++) {
|
||||||
GB_palette_changed(gb, true, i * 2);
|
GB_palette_changed(gb, true, i * 2);
|
||||||
|
38
Core/random.c
Normal file
38
Core/random.c
Normal 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
12
Core/random.h
Normal 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 */
|
@ -1,4 +1,5 @@
|
|||||||
#include "gb.h"
|
#include "gb.h"
|
||||||
|
#include "random.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#define INTRO_ANIMATION_LENGTH 200
|
#define INTRO_ANIMATION_LENGTH 200
|
||||||
@ -682,7 +683,7 @@ static double fm_sweep(double phase)
|
|||||||
}
|
}
|
||||||
static double random_double(void)
|
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)
|
bool GB_sgb_render_jingle(GB_gameboy_t *gb, GB_sample_t *dest, size_t count)
|
||||||
|
@ -16,12 +16,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <Core/gb.h>
|
#include <Core/gb.h>
|
||||||
|
#include <Core/random.h>
|
||||||
/* Disable all randomness during automatic tests */
|
|
||||||
long random(void)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool running = false;
|
static bool running = false;
|
||||||
static char *filename;
|
static char *filename;
|
||||||
@ -263,6 +258,8 @@ int main(int argc, char **argv)
|
|||||||
bool dmg = false;
|
bool dmg = false;
|
||||||
const char *boot_rom_path = NULL;
|
const char *boot_rom_path = NULL;
|
||||||
|
|
||||||
|
GB_random_set_enabled(false);
|
||||||
|
|
||||||
for (unsigned i = 1; i < argc; i++) {
|
for (unsigned i = 1; i < argc; i++) {
|
||||||
if (strcmp(argv[i], "--dmg") == 0) {
|
if (strcmp(argv[i], "--dmg") == 0) {
|
||||||
fprintf(stderr, "Using DMG mode\n");
|
fprintf(stderr, "Using DMG mode\n");
|
||||||
|
@ -243,7 +243,6 @@ else
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
TARGET := $(TARGET_NAME)_libretro.dll
|
TARGET := $(TARGET_NAME)_libretro.dll
|
||||||
SHARED := -shared -static-libgcc -static-libstdc++ -s -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined
|
SHARED := -shared -static-libgcc -static-libstdc++ -s -Wl,--version-script=$(CORE_DIR)/libretro/link.T -Wl,--no-undefined
|
||||||
CFLAGS += -Drandom=rand
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
TARGET := $(CORE_DIR)/build/bin/$(TARGET)
|
TARGET := $(CORE_DIR)/build/bin/$(TARGET)
|
||||||
|
Loading…
Reference in New Issue
Block a user