changeset 680:4996369f1463

Some small synchronization improvements that do not seem to fix anything
author Michael Pavone <pavone@retrodev.com>
date Sun, 04 Jan 2015 23:05:37 -0800
parents 7dd83295193a
children e26640daf1ae
files blastem.c vdp.c vdp.h
diffstat 3 files changed, 55 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/blastem.c	Sun Jan 04 12:25:33 2015 -0800
+++ b/blastem.c	Sun Jan 04 23:05:37 2015 -0800
@@ -210,41 +210,45 @@
 	z80_context * z_context = gen->z80;
 	uint32_t mclks = context->current_cycle;
 	sync_z80(z_context, mclks);
+	sync_sound(gen, mclks);
 	if (mclks >= mclk_target) {
-		sync_sound(gen, mclks);
-		gen->ym->current_cycle -= mclk_target;
-		gen->psg->cycles -= mclk_target;
-		if (gen->ym->write_cycle != CYCLE_NEVER) {
-			gen->ym->write_cycle = gen->ym->write_cycle >= mclk_target ? gen->ym->write_cycle - mclk_target : 0;
-		}
 		vdp_run_context(v_context, mclk_target);
-		//printf("reached frame end | MCLK Cycles: %d, Target: %d, VDP cycles: %d\n", mclks, mclk_target, v_context->cycles);
+		if (vdp_is_frame_over(v_context)) {
+			//printf("reached frame end | MCLK Cycles: %d, Target: %d, VDP cycles: %d\n", mclks, mclk_target, v_context->cycles);
 
-		if (!headless) {
-			break_on_sync |= wait_render_frame(v_context, frame_limit);
-		} else if(exit_after){
-			--exit_after;
-			if (!exit_after) {
-				exit(0);
+			if (!headless) {
+				break_on_sync |= wait_render_frame(v_context, frame_limit);
+			} else if(exit_after){
+				--exit_after;
+				if (!exit_after) {
+					exit(0);
+				}
 			}
+			frame++;
+			mclks -= mclk_target;
+			vdp_adjust_cycles(v_context, mclk_target);
+			io_adjust_cycles(gen->ports, context->current_cycle, mclk_target);
+			io_adjust_cycles(gen->ports+1, context->current_cycle, mclk_target);
+			io_adjust_cycles(gen->ports+2, context->current_cycle, mclk_target);
+			context->current_cycle -= mclk_target;
+			z80_adjust_cycles(z_context, mclk_target);
+			gen->ym->current_cycle -= mclk_target;
+			gen->psg->cycles -= mclk_target;
+			if (gen->ym->write_cycle != CYCLE_NEVER) {
+				gen->ym->write_cycle = gen->ym->write_cycle >= mclk_target ? gen->ym->write_cycle - mclk_target : 0;
+			}
+			if (mclks) {
+				vdp_run_context(v_context, mclks);
+			}
+			mclk_target = vdp_cycles_to_frame_end(v_context);
+			context->sync_cycle = mclk_target;
+		} else {
+			vdp_run_context(v_context, mclks);
+			mclk_target = vdp_cycles_to_frame_end(v_context);
 		}
-		frame++;
-		mclks -= mclk_target;
-		vdp_adjust_cycles(v_context, mclk_target);
-		io_adjust_cycles(gen->ports, context->current_cycle, mclk_target);
-		io_adjust_cycles(gen->ports+1, context->current_cycle, mclk_target);
-		io_adjust_cycles(gen->ports+2, context->current_cycle, mclk_target);
-		context->current_cycle -= mclk_target;
-		z80_adjust_cycles(z_context, mclk_target);
-		if (mclks) {
-			vdp_run_context(v_context, mclks);
-		}
-		mclk_target = vdp_cycles_to_frame_end(v_context);
-		context->sync_cycle = mclk_target;
 	} else {
 		//printf("running VDP for %d cycles\n", mclks - v_context->cycles);
 		vdp_run_context(v_context, mclks);
-		sync_sound(gen, mclks);
 	}
 	if (context->int_ack) {
 		vdp_int_ack(v_context, context->int_ack);
@@ -284,7 +288,6 @@
 		int blocked;
 		uint32_t before_cycle = v_context->cycles;
 		if (vdp_port < 4) {
-			gen->bus_busy = 1;
 			while (vdp_data_port_write(v_context, value) < 0) {
 				while(v_context->flags & FLAG_DMA_RUN) {
 					vdp_run_dma_done(v_context, mclk_target);
@@ -296,7 +299,6 @@
 				//context->current_cycle = v_context->cycles;
 			}
 		} else if(vdp_port < 8) {
-			gen->bus_busy = 1;
 			blocked = vdp_control_port_write(v_context, value);
 			if (blocked) {
 				while (blocked) {
@@ -323,19 +325,16 @@
 		if (v_context->cycles != before_cycle) {
 			//printf("68K paused for %d (%d) cycles at cycle %d (%d) for write\n", v_context->cycles - context->current_cycle, v_context->cycles - before_cycle, context->current_cycle, before_cycle);
 			context->current_cycle = v_context->cycles;
+			//Lock the Z80 out of the bus until the VDP access is complete
+			gen->bus_busy = 1;
+			sync_z80(gen->z80, v_context->cycles);
+			gen->bus_busy = 0;
 		}
 	} else if (vdp_port < 0x18) {
-		sync_sound(gen, context->current_cycle);
 		psg_write(gen->psg, value);
 	} else {
 		//TODO: Implement undocumented test register(s)
 	}
-	if (gen->bus_busy)
-	{
-		//Lock the Z80 out of the bus until the VDP access is complete
-		sync_z80(gen->z80, v_context->cycles);
-		gen->bus_busy = 0;
-	}
 	return context;
 }
 
@@ -402,6 +401,11 @@
 	if (v_context->cycles != before_cycle) {
 		//printf("68K paused for %d (%d) cycles at cycle %d (%d) for read\n", v_context->cycles - context->current_cycle, v_context->cycles - before_cycle, context->current_cycle, before_cycle);
 		context->current_cycle = v_context->cycles;
+		//Lock the Z80 out of the bus until the VDP access is complete
+		genesis_context *gen = context->system;
+		gen->bus_busy = 1;
+		sync_z80(gen->z80, v_context->cycles);
+		gen->bus_busy = 0;
 	}
 	return value;
 }
--- a/vdp.c	Sun Jan 04 12:25:33 2015 -0800
+++ b/vdp.c	Sun Jan 04 23:05:37 2015 -0800
@@ -1627,7 +1627,7 @@
 					if (!context->double_res) {
 						context->framebuf = context->oddbuf;
 					}
-				}
+					}
 				context->cd &= 0x3C;
 			}
 		} else {
@@ -1892,7 +1892,7 @@
 	return MCLKS_LINE * (lines - 1) + vdp_cycles_next_line(context);
 }
 
-uint32_t vdp_cycles_to_frame_end(vdp_context * context)
+uint32_t vdp_frame_end_line(vdp_context * context)
 {
 	uint32_t frame_end;
 	if (context->flags2 & FLAG2_REGION_PAL) {
@@ -1908,7 +1908,18 @@
 			frame_end = NTSC_INACTIVE_START + 8;
 		}
 	}
-	return context->cycles + vdp_cycles_to_line(context, frame_end);
+	return frame_end;
+}
+
+uint32_t vdp_cycles_to_frame_end(vdp_context * context)
+{
+	return context->cycles + vdp_cycles_to_line(context, vdp_frame_end_line(context));
+}
+
+uint8_t vdp_is_frame_over(vdp_context * context)
+{
+	uint32_t frame_end = vdp_frame_end_line(context);
+	return context->vcounter >= frame_end && context->vcounter < (frame_end + 8);
 }
 
 uint32_t vdp_next_hint(vdp_context * context)
--- a/vdp.h	Sun Jan 04 12:25:33 2015 -0800
+++ b/vdp.h	Sun Jan 04 23:05:37 2015 -0800
@@ -194,6 +194,7 @@
 void vdp_print_reg_explain(vdp_context * context);
 void latch_mode(vdp_context * context);
 uint32_t vdp_cycles_to_frame_end(vdp_context * context);
+uint8_t vdp_is_frame_over(vdp_context * context);
 
 extern int32_t color_map[1 << 12];