Randomize emulator startup state by default, configurable random seed.

This commit is contained in:
Toni Wilen 2023-07-09 20:20:26 +03:00
parent af1a4f3ee6
commit 62bace6406
6 changed files with 78 additions and 42 deletions

View File

@ -2755,6 +2755,9 @@ void cfgfile_save_options (struct zfile *f, struct uae_prefs *p, int type)
cfgfile_dwrite_strarr(f, _T("agnusmodel"), agnusmodel, p->cs_agnusmodel); cfgfile_dwrite_strarr(f, _T("agnusmodel"), agnusmodel, p->cs_agnusmodel);
cfgfile_dwrite_strarr(f, _T("agnussize"), agnussize, p->cs_agnussize); cfgfile_dwrite_strarr(f, _T("agnussize"), agnussize, p->cs_agnussize);
cfgfile_dwrite_strarr(f, _T("denisemodel"), denisemodel, p->cs_denisemodel); cfgfile_dwrite_strarr(f, _T("denisemodel"), denisemodel, p->cs_denisemodel);
if (p->seed) {
cfgfile_write(f, _T("rndseed"), _T("%d"), p->seed);
}
if (is_board_enabled(p, ROMTYPE_CD32CART, 0)) { if (is_board_enabled(p, ROMTYPE_CD32CART, 0)) {
cfgfile_dwrite_bool(f, _T("cd32fmv"), true); cfgfile_dwrite_bool(f, _T("cd32fmv"), true);
@ -5971,7 +5974,8 @@ static int cfgfile_parse_hardware (struct uae_prefs *p, const TCHAR *option, TCH
|| cfgfile_intval(option, value, _T("genlock_mix"), &p->genlock_mix, 1) || cfgfile_intval(option, value, _T("genlock_mix"), &p->genlock_mix, 1)
|| cfgfile_intval(option, value, _T("keyboard_handshake"), &p->cs_kbhandshake, 1) || cfgfile_intval(option, value, _T("keyboard_handshake"), &p->cs_kbhandshake, 1)
|| cfgfile_intval(option, value, _T("eclockphase"), &p->cs_eclockphase, 1) || cfgfile_intval(option, value, _T("eclockphase"), &p->cs_eclockphase, 1)
|| cfgfile_intval(option, value, _T("chipset_rtc_adjust"), &p->cs_rtc_adjust, 1)) || cfgfile_intval(option, value, _T("chipset_rtc_adjust"), &p->cs_rtc_adjust, 1)
|| cfgfile_intval(option, value, _T("rndseed"), &p->seed, 1))
return 1; return 1;
if (cfgfile_strval(option, value, _T("comp_trustbyte"), &p->comptrustbyte, compmode, 0) if (cfgfile_strval(option, value, _T("comp_trustbyte"), &p->comptrustbyte, compmode, 0)

View File

@ -729,6 +729,7 @@ struct uae_prefs {
int cs_denisemodel; int cs_denisemodel;
bool cs_memorypatternfill; bool cs_memorypatternfill;
bool cs_ipldelay; bool cs_ipldelay;
uae_u32 seed;
struct boardromconfig expansionboard[MAX_EXPANSION_BOARDS]; struct boardromconfig expansionboard[MAX_EXPANSION_BOARDS];

View File

@ -86,9 +86,9 @@ extern void fetch_inputfilepath(TCHAR *out, int size);
extern void fetch_datapath(TCHAR *out, int size); extern void fetch_datapath(TCHAR *out, int size);
extern void fetch_rompath(TCHAR *out, int size); extern void fetch_rompath(TCHAR *out, int size);
extern void fetch_videopath(TCHAR *out, int size); extern void fetch_videopath(TCHAR *out, int size);
extern uae_u32 uaerand (void); extern uae_u32 uaerand(void);
extern uae_u32 uaesrand (uae_u32 seed); extern uae_u32 uaesetrandseed(uae_u32 seed);
extern uae_u32 uaerandgetseed (void); extern uae_u32 uaerandgetseed(void);
/* the following prototypes should probably be moved somewhere else */ /* the following prototypes should probably be moved somewhere else */

View File

@ -393,32 +393,32 @@ int inprec_open (const TCHAR *fname, const TCHAR *statefilename)
header_end2 = 0; header_end2 = 0;
if (input_play) { if (input_play) {
uae_u32 id; uae_u32 id;
zfile_fseek (inprec_zf, 0, SEEK_END); zfile_fseek(inprec_zf, 0, SEEK_END);
inprec_size = zfile_ftell32(inprec_zf); inprec_size = zfile_ftell32(inprec_zf);
zfile_fseek (inprec_zf, 0, SEEK_SET); zfile_fseek(inprec_zf, 0, SEEK_SET);
inprec_buffer = inprec_p = xmalloc (uae_u8, inprec_size); inprec_buffer = inprec_p = xmalloc (uae_u8, inprec_size);
zfile_fread (inprec_buffer, inprec_size, 1, inprec_zf); zfile_fread(inprec_buffer, inprec_size, 1, inprec_zf);
inprec_plastptr = inprec_buffer; inprec_plastptr = inprec_buffer;
id = inprec_pu32(); id = inprec_pu32();
if (id != 'UAE\0') { if (id != 'UAE\0') {
inprec_close (true); inprec_close(true);
return 0; return 0;
} }
int v = inprec_pu8 (); int v = inprec_pu8();
if (v != 3) { if (v != 3) {
inprec_close (true); inprec_close(true);
return 0; return 0;
} }
inprec_pu8 (); inprec_pu8();
inprec_pu8 (); inprec_pu8();
inprec_pu8 (); inprec_pu8();
seed = inprec_pu32(); seed = inprec_pu32();
seed = uaesrand (seed); seed = uaesetrandseed(seed);
vsync_counter = inprec_pu32 (); vsync_counter = inprec_pu32();
hsync_counter = inprec_pu32 (); hsync_counter = inprec_pu32();
i = inprec_pu32 (); i = inprec_pu32();
while (i-- > 0) while (i-- > 0)
inprec_pu8 (); inprec_pu8();
header_end = addrdiff(inprec_plastptr, inprec_buffer); header_end = addrdiff(inprec_plastptr, inprec_buffer);
inprec_pstr (savestate_fname); inprec_pstr (savestate_fname);
if (savestate_fname[0]) { if (savestate_fname[0]) {
@ -466,17 +466,17 @@ int inprec_open (const TCHAR *fname, const TCHAR *statefilename)
header_end2 = addrdiff(inprec_plastptr, inprec_buffer); header_end2 = addrdiff(inprec_plastptr, inprec_buffer);
findlast (); findlast ();
} else if (input_record) { } else if (input_record) {
seed = uaesrand (seed); seed = uaesetrandseed(seed);
inprec_buffer = inprec_p = xmalloc (uae_u8, inprec_size); inprec_buffer = inprec_p = xmalloc(uae_u8, inprec_size);
inprec_ru32 ('UAE\0'); inprec_ru32('UAE\0');
inprec_ru8 (3); inprec_ru8(3);
inprec_ru8 (UAEMAJOR); inprec_ru8(UAEMAJOR);
inprec_ru8 (UAEMINOR); inprec_ru8(UAEMINOR);
inprec_ru8 (UAESUBREV); inprec_ru8(UAESUBREV);
inprec_ru32 (seed); inprec_ru32(seed);
inprec_ru32 (vsync_counter); inprec_ru32(vsync_counter);
inprec_ru32 (hsync_counter); inprec_ru32(hsync_counter);
inprec_ru32 (0); // extra header size inprec_ru32(0); // extra header size
flush (); flush ();
header_end2 = header_end = zfile_ftell32(inprec_zf); header_end2 = header_end = zfile_ftell32(inprec_zf);
} else { } else {
@ -494,7 +494,7 @@ int inprec_open (const TCHAR *fname, const TCHAR *statefilename)
void inprec_startup (void) void inprec_startup (void)
{ {
uaesrand (seed); uaesetrandseed(seed);
} }
bool inprec_prepare_record (const TCHAR *statefilename) bool inprec_prepare_record (const TCHAR *statefilename)

View File

@ -71,9 +71,8 @@ TCHAR warning_buffer[256];
TCHAR optionsfile[256]; TCHAR optionsfile[256];
static uae_u32 randseed; static uae_u32 randseed;
static int oldhcounter;
static uae_u32 xorshiftstate = 1; static uae_u32 xorshiftstate;
static uae_u32 xorshift32(void) static uae_u32 xorshift32(void)
{ {
uae_u32 x = xorshiftstate; uae_u32 x = xorshiftstate;
@ -84,23 +83,35 @@ static uae_u32 xorshift32(void)
return xorshiftstate; return xorshiftstate;
} }
uae_u32 uaesrand(uae_u32 seed)
{
oldhcounter = -1;
randseed = seed;
return randseed;
}
uae_u32 uaerand(void) uae_u32 uaerand(void)
{ {
if (oldhcounter != hsync_counter) { if (xorshiftstate == 0) {
xorshiftstate = (hsync_counter ^ randseed) | 1; xorshiftstate = randseed;
oldhcounter = hsync_counter; if (!xorshiftstate) {
randseed = 1;
xorshiftstate = 1;
}
} }
uae_u32 r = xorshift32(); uae_u32 r = xorshift32();
return r; return r;
} }
uae_u32 uaerandgetseed(void) uae_u32 uaerandgetseed(void)
{ {
if (!randseed) {
randseed = 1;
xorshiftstate = 1;
}
return randseed;
}
uae_u32 uaesetrandseed(uae_u32 seed)
{
if (!seed) {
seed = 1;
}
randseed = seed;
xorshiftstate = seed;
return randseed; return randseed;
} }
@ -1183,6 +1194,12 @@ static int real_main2 (int argc, TCHAR **argv)
#ifdef RETROPLATFORM #ifdef RETROPLATFORM
rp_fixup_options (&currprefs); rp_fixup_options (&currprefs);
#endif #endif
if (currprefs.seed == 0) {
uae_u32 t = getlocaltime();
uaesetrandseed(t);
} else {
uaesetrandseed(currprefs.seed);
}
copy_prefs(&currprefs, &changed_prefs); copy_prefs(&currprefs, &changed_prefs);
target_run (); target_run ();
/* force sound settings change */ /* force sound settings change */

View File

@ -6626,10 +6626,24 @@ void m68k_go (int may_quit)
} }
cpu_hardreset = false; cpu_hardreset = false;
cpu_keyboardreset = false; cpu_keyboardreset = false;
hardboot = 0;
event_wait = true; event_wait = true;
unset_special(SPCFLAG_MODE_CHANGE); unset_special(SPCFLAG_MODE_CHANGE);
if (!restored && hardboot) {
uae_u32 s = uaerandgetseed();
uaesetrandseed(s);
write_log("rndseed = %08x (%u)\n", s, s);
// add random delay before CPU starts
int t = uaerand() & 0x7fff;
while (t > 255) {
x_do_cycles(255 * CYCLE_UNIT);
t -= 255;
}
x_do_cycles(t * CYCLE_UNIT);
}
hardboot = 0;
#ifdef SAVESTATE #ifdef SAVESTATE
if (restored) { if (restored) {
restored = 0; restored = 0;