changeset 1556:075df0844baa

Randomize soft reset timing and fix silly bug that was accidentally clearing IO state on soft reset
author Michael Pavone <pavone@retrodev.com>
date Fri, 30 Mar 2018 22:01:05 -0700
parents 6ce36c3f250b
children a0fbb1e90533
files genesis.c genesis.h
diffstat 2 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/genesis.c	Fri Mar 30 00:37:08 2018 -0700
+++ b/genesis.c	Fri Mar 30 22:01:05 2018 -0700
@@ -29,7 +29,7 @@
 
 //TODO: Figure out the exact value for this
 #define LINES_NTSC 262
-#define LINES_PAL 312
+#define LINES_PAL 313
 
 #define MAX_SOUND_CYCLES 100000	
 
@@ -308,6 +308,11 @@
 	sync_z80(z_context, mclks);
 	sync_sound(gen, mclks);
 	vdp_run_context(v_context, mclks);
+	if (mclks >= gen->reset_cycle) {
+		gen->reset_requested = 1;
+		context->should_return = 1;
+		gen->reset_cycle = CYCLE_NEVER;
+	}
 	if (v_context->frame != last_frame_num) {
 		//printf("reached frame end %d | MCLK Cycles: %d, Target: %d, VDP cycles: %d, vcounter: %d, hslot: %d\n", last_frame_num, mclks, gen->frame_end, v_context->cycles, v_context->vcounter, v_context->hslot);
 		last_frame_num = v_context->frame;
@@ -331,6 +336,9 @@
 			if (gen->ym->write_cycle != CYCLE_NEVER) {
 				gen->ym->write_cycle = gen->ym->write_cycle >= deduction ? gen->ym->write_cycle - deduction : 0;
 			}
+			if (gen->reset_cycle != CYCLE_NEVER) {
+				gen->reset_cycle -= deduction;
+			}
 		}
 	}
 	gen->frame_end = vdp_cycles_to_frame_end(v_context);
@@ -345,6 +353,9 @@
 		context->sync_cycle = context->current_cycle + 1;
 	}
 	adjust_int_cycle(context, v_context);
+	if (gen->reset_cycle < context->target_cycle) {
+		context->target_cycle = gen->reset_cycle;
+	}
 	if (address) {
 		if (gen->header.enter_debugger) {
 			gen->header.enter_debugger = 0;
@@ -1039,6 +1050,7 @@
 	{
 		if (gen->reset_requested) {
 			gen->reset_requested = 0;
+			gen->m68k->should_return = 0;
 			z80_assert_reset(gen->z80, gen->m68k->current_cycle);
 			z80_clear_busreq(gen->z80, gen->m68k->current_cycle);
 			ym_reset(gen->ym);
@@ -1163,8 +1175,13 @@
 static void soft_reset(system_header *system)
 {
 	genesis_context *gen = (genesis_context *)system;
-	gen->m68k->should_return = 1;
-	gen->reset_requested = 1;
+	if (gen->reset_cycle == CYCLE_NEVER) {
+		double random = (double)rand()/(double)RAND_MAX;
+		gen->reset_cycle = gen->m68k->current_cycle + random * MCLKS_LINE * (gen->version_reg & HZ50 ? LINES_PAL : LINES_NTSC);
+		if (gen->reset_cycle < gen->m68k->target_cycle) {
+			gen->m68k->target_cycle = gen->reset_cycle;
+		}
+	}
 }
 
 static void free_genesis(system_header *system)
--- a/genesis.h	Fri Mar 30 00:37:08 2018 -0700
+++ b/genesis.h	Fri Mar 30 22:01:05 2018 -0700
@@ -44,6 +44,7 @@
 	uint32_t        max_cycles;
 	uint32_t        int_latency_prev1;
 	uint32_t        int_latency_prev2;
+	uint32_t        reset_cycle;
 	uint8_t         bank_regs[8];
 	uint16_t        mapper_start_index;
 	uint8_t         mapper_type;