From 12746b35e31cca72e04de6a7eecb633c805024ee Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 19 Jul 2015 10:54:19 -0400 Subject: [PATCH] Ensured there's no overflow while changing base. --- OSBindings/Mac/Clock Signal/Atari2600Document.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/OSBindings/Mac/Clock Signal/Atari2600Document.swift b/OSBindings/Mac/Clock Signal/Atari2600Document.swift index b42b2bfea..cc92d4d87 100644 --- a/OSBindings/Mac/Clock Signal/Atari2600Document.swift +++ b/OSBindings/Mac/Clock Signal/Atari2600Document.swift @@ -50,7 +50,14 @@ class Atari2600Document: NSDocument, CSOpenGLViewDelegate { private var lastCycleCount: Int64? func openGLView(view: CSOpenGLView!, didUpdateToTime time: CVTimeStamp) { - let cycleCount = (1194720 * time.videoTime) / Int64(time.videoTimeScale) + // this slightly elaborate dance is to avoid overflow + let intendedCyclesPerSecond: Int64 = 1194720 + let videoTimeScale64 = Int64(time.videoTimeScale) + + let cycleCountLow = ((time.videoTime % videoTimeScale64) * intendedCyclesPerSecond) / videoTimeScale64 + let cycleCountHigh = (time.videoTime / videoTimeScale64) * intendedCyclesPerSecond + + let cycleCount = cycleCountLow + cycleCountHigh if let lastCycleCount = lastCycleCount { let elapsedTime = cycleCount - lastCycleCount atari2600!.runForNumberOfCycles(Int32(elapsedTime))