]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
Randomize emulator startup state by default, configurable random seed.
authorToni Wilen <twilen@winuae.net>
Sun, 9 Jul 2023 17:20:26 +0000 (20:20 +0300)
committerToni Wilen <twilen@winuae.net>
Sun, 9 Jul 2023 17:20:26 +0000 (20:20 +0300)
cfgfile.cpp
include/options.h
include/uae.h
inputrecord.cpp
main.cpp
newcpu.cpp

index 887a7fa41f16a611334d09a055e3a87ecb78e69c..35df82f56aed1e2cab4ac530161803e8364daac3 100644 (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("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)
index 5c13517bb2b6e1ce31a70f0b659c0b4519594571..b83c90282356b5c1f85bba2d2e495ddc9cc8c551 100644 (file)
@@ -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];
 
index 94ef91bb8171a3ba8b84dafa3fb9ca8a684ca510..600b7bc933bf80dde477e6572b9602421bcbaac4 100644 (file)
@@ -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 */
 
index 92deb2c2bc9474e7c08bf2bbfcbd5a9b00a4ac1d..117fc79bfc9654f0c586e4e55217498a4550fda6 100644 (file)
@@ -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)
index 2860d4942a589ea39448f75c8fafd634f6c9cdec..5e33db414b1568c9558d4e888d324cd7ee169805 100644 (file)
--- 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 */
index 0a26cedf0be4a352db007a6455a34fef20dd23c6..ca4faccf153a9a26429cb18b16f661f733fee10a 100644 (file)
@@ -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;