changeset 186:02e25abe2dcd

Cleanup VINT code and fix bug in which VINT cycle would be set incorrectly after a VDP control port write
author Mike Pavone <pavone@retrodev.com>
date Sun, 13 Jan 2013 16:11:28 -0800
parents b204fbed4efe
children 8e138da572ab
files blastem.c
diffstat 1 files changed, 16 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/blastem.c	Sun Jan 13 15:55:43 2013 -0800
+++ b/blastem.c	Sun Jan 13 16:11:28 2013 -0800
@@ -104,6 +104,20 @@
 
 #define VINT_CYCLE ((MCLKS_LINE * 226)/MCLKS_PER_68K)
 
+void adjust_int_cycle(m68k_context * context, vdp_context * v_context)
+{
+	if (!(v_context->regs[REG_MODE_2] & 0x20 && ((context->status & 0x7) < 6)) || context->current_cycle >= VINT_CYCLE) {
+		context->int_cycle = CYCLE_NEVER;
+		context->target_cycle = context->sync_cycle;
+	} else if (context->int_cycle > VINT_CYCLE) {
+		context->int_cycle = VINT_CYCLE;
+		context->int_num = 6;
+		if (context->int_cycle < context->sync_cycle) {
+			context->target_cycle = context->int_cycle;
+		}
+	}
+}
+
 m68k_context * sync_components(m68k_context * context)
 {
 	//TODO: Handle sync targets smaller than a single frame
@@ -121,38 +135,11 @@
 		if (mclks) {
 			vdp_run_context(v_context, mclks);
 		}
-		if (v_context->regs[REG_MODE_2] & 0x20 && ((context->status & 0x7) < 6)) {
-			if (context->int_cycle > VINT_CYCLE) {
-				context->int_cycle = VINT_CYCLE;
-				context->int_num = 6;
-				if (context->int_cycle < context->sync_cycle) {
-					context->target_cycle = context->int_cycle;
-				}
-			}
-		} else {
-			context->int_cycle = 0xFFFFFFFF;
-			context->target_cycle = context->sync_cycle;
-		}
 	} else {
 		//printf("running VDP for %d cycles\n", mclks - v_context->cycles);
 		vdp_run_context(v_context, mclks);
-		if (v_context->regs[REG_MODE_2] & 0x20 && ((context->status & 0x7) < 6)) {
-			if (context->int_cycle > VINT_CYCLE) {
-				context->int_cycle = VINT_CYCLE;
-				context->int_num = 6;
-				if (context->int_cycle < context->sync_cycle && context->int_cycle < context->current_cycle) {
-					context->target_cycle = context->int_cycle;
-				}
-			}
-			if (context->int_cycle <= context->current_cycle) {
-				context->int_cycle = CYCLE_NEVER;
-				context->target_cycle = context->sync_cycle;
-			}
-		} else {
-			context->int_cycle = CYCLE_NEVER;
-			context->target_cycle = context->sync_cycle;
-		}
 	}
+	adjust_int_cycle(context, v_context);
 	return context;
 }
 
@@ -197,18 +184,7 @@
 				}
 				context->current_cycle = v_context->cycles / MCLKS_PER_68K;
 			} else {
-				if (v_context->regs[REG_MODE_2] & 0x20 && ((context->status & 0x7) < 6)) {
-					if (context->int_cycle > VINT_CYCLE) {
-						context->int_cycle = VINT_CYCLE;
-						context->int_num = 6;
-						if (context->int_cycle < context->sync_cycle) {
-							context->target_cycle = context->int_cycle;
-						}
-					}
-				} else {
-					context->int_cycle = 0xFFFFFFFF;
-					context->target_cycle = context->sync_cycle;
-				}
+				adjust_int_cycle(context, v_context);
 			}
 		} else {
 			printf("Illegal write to HV Counter port %X\n", vdp_port);