1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-25 16:31:42 +00:00

Ensured there's no overflow while changing base.

This commit is contained in:
Thomas Harte 2015-07-19 10:54:19 -04:00
parent 412e02fdd8
commit 12746b35e3

View File

@ -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))