From 74ae760730c5307bfe0c38a38a18e998d4a8d867 Mon Sep 17 00:00:00 2001 From: tudnai Date: Tue, 21 Jun 2022 21:53:10 -0700 Subject: [PATCH] Pause / Resume with VSYNC --- .gitignore | 2 ++ A2Mac/ViewController.swift | 44 ++++++++++++++++++-------------------- src/cpu/6502.h | 10 +++++---- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 5066240..480cf79 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ Resources/rom/Downloads/APPLE_IIe_ROM_KRK1.zip Resources/rom/Downloads/APPLE_IIe_ROM_KRK2.zip Resources/rom/Downloads/apple_iie_rom.zip Resources/rom/Downloads/077-0026-0027 for IIe.zip + +**/Contents/** diff --git a/A2Mac/ViewController.swift b/A2Mac/ViewController.swift index cbe894d..7fece3f 100644 --- a/A2Mac/ViewController.swift +++ b/A2Mac/ViewController.swift @@ -208,7 +208,7 @@ class ViewController: NSViewController { var workItem : DispatchWorkItem? = nil; @IBAction func PowerOn(_ sender: Any) { - upd.suspend() + CVDisplayLinkStop(displayLink!) cpuState = cpuState_inited; spkr_stopAll() @@ -239,7 +239,7 @@ class ViewController: NSViewController { m6502_ColdReset( Bundle.main.resourcePath! + "/rom/", ViewController.romFileName ) cpuState = cpuState_running; - self.upd.resume() + CVDisplayLinkStart(self.displayLink!) } //------------------------------------------------------------ @@ -267,7 +267,7 @@ class ViewController: NSViewController { @IBAction func PowerOff(_ sender: Any) { - upd.suspend() + CVDisplayLinkStop(displayLink!) cpuState = cpuState_inited; spkr_stopAll() @@ -290,12 +290,12 @@ class ViewController: NSViewController { switch ( cpuState ) { case cpuState_halted: - upd.resume() + CVDisplayLinkStart(displayLink!) cpuState = cpuState_running break case cpuState_running: - upd.suspend() + CVDisplayLinkStop(displayLink!) cpuState = cpuState_halted break @@ -337,6 +337,8 @@ class ViewController: NSViewController { let lineEndChars = ViewController.lineEndChars var frameCnt = 0 + var flashInverted = false + // let spaceChar : Character = "\u{E17F}" // let blockChar : Character = "\u{E07F}" // static let spaceChar : Character = " " @@ -465,7 +467,7 @@ class ViewController: NSViewController { if ( cpuMode == cpuMode_eco ) { cpuState = cpuState_running; - upd.resume() + CVDisplayLinkStart(displayLink!) } // print("keyDown") @@ -714,11 +716,18 @@ class ViewController: NSViewController { self.frameCnt += 1 if ( self.frameCnt == fps / video_fps_divider / 2 ) { - ViewController.charConvTbl = ViewController.charConvTblFlashOn + if !flashInverted { + ViewController.charConvTbl = ViewController.charConvTblFlashOn + flashInverted = true + } } else if ( self.frameCnt >= fps / video_fps_divider ) { self.frameCnt = 0 - ViewController.charConvTbl = ViewController.charConvTblFlashOff + + if flashInverted { + ViewController.charConvTbl = ViewController.charConvTblFlashOff + flashInverted = false + } } } @@ -1008,13 +1017,13 @@ class ViewController: NSViewController { case cpuState_halting: cpuState = cpuState_halted - // video rendering + // last video rendering before halt Render() break case cpuState_halted: - upd.suspend() + CVDisplayLinkStop(displayLink!) break default: @@ -1034,17 +1043,6 @@ class ViewController: NSViewController { // } - var upd = RepeatingTimer(timeInterval: 1) - -// func newUpdateTimer( timeInterval : Double ) { -// upd.kill() -// upd = RepeatingTimer(timeInterval: timeInterval) -// upd.eventHandler = { -// self.Update() -// } -// upd.resume() -// } - // Kelvin Sherlock's fix to avoid uninstalled font problems override func awakeFromNib() { @@ -1144,7 +1142,7 @@ class ViewController: NSViewController { let unsafeSelf: UnsafeMutableRawPointer = Unmanaged.passUnretained(self).toOpaque() CVDisplayLinkSetOutputCallback(displayLink!, displayLinkOutputCallback, unsafeSelf) - CVDisplayLinkStart(displayLink!) +// CVDisplayLinkStart(displayLink!) } @@ -1399,7 +1397,7 @@ class ViewController: NSViewController { cpuMode = cpuMode_eco fps = DEFAULT_FPS - video_fps_divider = DEF_VIDEO_DIV + video_fps_divider = ECO_VIDEO_DIV break case "Game": diff --git a/src/cpu/6502.h b/src/cpu/6502.h index 4bc935e..2825a7c 100644 --- a/src/cpu/6502.h +++ b/src/cpu/6502.h @@ -187,11 +187,13 @@ extern double mips; extern double mhz; #define DEFAULT_FPS 60U -#define DEF_VIDEO_DIV 1U -#define DEF_SPKR_DIV 1U +#define DEF_VIDEO_DIV 2U +#define DEF_SPKR_DIV 2U -#define GAME_FPS 180U // 480U -#define GAME_VIDEO_DIV (GAME_FPS / DEFAULT_FPS) +#define ECO_VIDEO_DIV 4U + +#define GAME_FPS 60U // 180U // 480U +#define GAME_VIDEO_DIV 1U // (GAME_FPS / DEFAULT_FPS) extern unsigned int video_fps_divider; extern unsigned int fps;