From: Toni Wilen Date: Sun, 9 Jul 2023 17:20:26 +0000 (+0300) Subject: Randomize emulator startup state by default, configurable random seed. X-Git-Tag: 5.1.0~165 X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=62bace6406c30995b14948085efa9f02c8036330;p=francis%2Fwinuae.git Randomize emulator startup state by default, configurable random seed. --- diff --git a/cfgfile.cpp b/cfgfile.cpp index 887a7fa4..35df82f5 100644 --- a/cfgfile.cpp +++ b/cfgfile.cpp @@ -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("agnussize"), agnussize, p->cs_agnussize); 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)) { 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("keyboard_handshake"), &p->cs_kbhandshake, 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; if (cfgfile_strval(option, value, _T("comp_trustbyte"), &p->comptrustbyte, compmode, 0) diff --git a/include/options.h b/include/options.h index 5c13517b..b83c9028 100644 --- a/include/options.h +++ b/include/options.h @@ -729,6 +729,7 @@ struct uae_prefs { int cs_denisemodel; bool cs_memorypatternfill; bool cs_ipldelay; + uae_u32 seed; struct boardromconfig expansionboard[MAX_EXPANSION_BOARDS]; diff --git a/include/uae.h b/include/uae.h index 94ef91bb..600b7bc9 100644 --- a/include/uae.h +++ b/include/uae.h @@ -86,9 +86,9 @@ extern void fetch_inputfilepath(TCHAR *out, int size); extern void fetch_datapath(TCHAR *out, int size); extern void fetch_rompath(TCHAR *out, int size); extern void fetch_videopath(TCHAR *out, int size); -extern uae_u32 uaerand (void); -extern uae_u32 uaesrand (uae_u32 seed); -extern uae_u32 uaerandgetseed (void); +extern uae_u32 uaerand(void); +extern uae_u32 uaesetrandseed(uae_u32 seed); +extern uae_u32 uaerandgetseed(void); /* the following prototypes should probably be moved somewhere else */ diff --git a/inputrecord.cpp b/inputrecord.cpp index 92deb2c2..117fc79b 100644 --- a/inputrecord.cpp +++ b/inputrecord.cpp @@ -393,32 +393,32 @@ int inprec_open (const TCHAR *fname, const TCHAR *statefilename) header_end2 = 0; if (input_play) { uae_u32 id; - zfile_fseek (inprec_zf, 0, SEEK_END); + zfile_fseek(inprec_zf, 0, SEEK_END); 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); - zfile_fread (inprec_buffer, inprec_size, 1, inprec_zf); + zfile_fread(inprec_buffer, inprec_size, 1, inprec_zf); inprec_plastptr = inprec_buffer; id = inprec_pu32(); if (id != 'UAE\0') { - inprec_close (true); + inprec_close(true); return 0; } - int v = inprec_pu8 (); + int v = inprec_pu8(); if (v != 3) { - inprec_close (true); + inprec_close(true); return 0; } - inprec_pu8 (); - inprec_pu8 (); - inprec_pu8 (); + inprec_pu8(); + inprec_pu8(); + inprec_pu8(); seed = inprec_pu32(); - seed = uaesrand (seed); - vsync_counter = inprec_pu32 (); - hsync_counter = inprec_pu32 (); - i = inprec_pu32 (); + seed = uaesetrandseed(seed); + vsync_counter = inprec_pu32(); + hsync_counter = inprec_pu32(); + i = inprec_pu32(); while (i-- > 0) - inprec_pu8 (); + inprec_pu8(); header_end = addrdiff(inprec_plastptr, inprec_buffer); inprec_pstr (savestate_fname); if (savestate_fname[0]) { @@ -466,17 +466,17 @@ int inprec_open (const TCHAR *fname, const TCHAR *statefilename) header_end2 = addrdiff(inprec_plastptr, inprec_buffer); findlast (); } else if (input_record) { - seed = uaesrand (seed); - inprec_buffer = inprec_p = xmalloc (uae_u8, inprec_size); - inprec_ru32 ('UAE\0'); - inprec_ru8 (3); - inprec_ru8 (UAEMAJOR); - inprec_ru8 (UAEMINOR); - inprec_ru8 (UAESUBREV); - inprec_ru32 (seed); - inprec_ru32 (vsync_counter); - inprec_ru32 (hsync_counter); - inprec_ru32 (0); // extra header size + seed = uaesetrandseed(seed); + inprec_buffer = inprec_p = xmalloc(uae_u8, inprec_size); + inprec_ru32('UAE\0'); + inprec_ru8(3); + inprec_ru8(UAEMAJOR); + inprec_ru8(UAEMINOR); + inprec_ru8(UAESUBREV); + inprec_ru32(seed); + inprec_ru32(vsync_counter); + inprec_ru32(hsync_counter); + inprec_ru32(0); // extra header size flush (); header_end2 = header_end = zfile_ftell32(inprec_zf); } else { @@ -494,7 +494,7 @@ int inprec_open (const TCHAR *fname, const TCHAR *statefilename) void inprec_startup (void) { - uaesrand (seed); + uaesetrandseed(seed); } bool inprec_prepare_record (const TCHAR *statefilename) diff --git a/main.cpp b/main.cpp index 2860d494..5e33db41 100644 --- a/main.cpp +++ b/main.cpp @@ -71,9 +71,8 @@ TCHAR warning_buffer[256]; TCHAR optionsfile[256]; static uae_u32 randseed; -static int oldhcounter; -static uae_u32 xorshiftstate = 1; +static uae_u32 xorshiftstate; static uae_u32 xorshift32(void) { uae_u32 x = xorshiftstate; @@ -84,23 +83,35 @@ static uae_u32 xorshift32(void) return xorshiftstate; } -uae_u32 uaesrand(uae_u32 seed) -{ - oldhcounter = -1; - randseed = seed; - return randseed; -} uae_u32 uaerand(void) { - if (oldhcounter != hsync_counter) { - xorshiftstate = (hsync_counter ^ randseed) | 1; - oldhcounter = hsync_counter; + if (xorshiftstate == 0) { + xorshiftstate = randseed; + if (!xorshiftstate) { + randseed = 1; + xorshiftstate = 1; + } } uae_u32 r = xorshift32(); return r; } + 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; } @@ -1183,6 +1194,12 @@ static int real_main2 (int argc, TCHAR **argv) #ifdef RETROPLATFORM rp_fixup_options (&currprefs); #endif + if (currprefs.seed == 0) { + uae_u32 t = getlocaltime(); + uaesetrandseed(t); + } else { + uaesetrandseed(currprefs.seed); + } copy_prefs(&currprefs, &changed_prefs); target_run (); /* force sound settings change */ diff --git a/newcpu.cpp b/newcpu.cpp index 0a26cedf..ca4faccf 100644 --- a/newcpu.cpp +++ b/newcpu.cpp @@ -6626,10 +6626,24 @@ void m68k_go (int may_quit) } cpu_hardreset = false; cpu_keyboardreset = false; - hardboot = 0; event_wait = true; 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 if (restored) { restored = 0;