From bd8403ee64a73f7a88d3cb076902503300367b34 Mon Sep 17 00:00:00 2001 From: tudnai Date: Mon, 31 Oct 2022 17:43:43 -0700 Subject: [PATCH] - Attempt to do better disassembly scroll syncchronization - main.sync replaced to .async to avoid invalid opcode error --- .../xcdebugger/Expressions.xcexplist | 36 ++++++++ A2Mac/DebuggerViewController.swift | 86 ++++++++++++++++--- A2Mac/DebuggerWindowController.swift | 3 + A2Mac/ViewController.swift | 24 +++--- A2iOS/ViewController.swift | 2 +- 5 files changed, 126 insertions(+), 25 deletions(-) diff --git a/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist b/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist index f3896aa..b546aa3 100644 --- a/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist +++ b/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist @@ -627,6 +627,12 @@ + + + + + + + + + + + + + + @@ -765,6 +785,14 @@ + + + + + + @@ -773,6 +801,14 @@ + + + + + + diff --git a/A2Mac/DebuggerViewController.swift b/A2Mac/DebuggerViewController.swift index d37dfb4..69e1286 100644 --- a/A2Mac/DebuggerViewController.swift +++ b/A2Mac/DebuggerViewController.swift @@ -63,13 +63,21 @@ class DebuggerViewController: NSViewController, NSTextFieldDelegate { // Stack_Display.scroll(Stack_Display.enclosingScrollView!.visibleRect, by: NSSize(width: 0, height: event.scrollingDeltaY) ) // } - var scrollTo = Stack_Display.visibleRect.origin + var scrollTo = Disass_Display.visibleRect.origin let lineSpacing = CGFloat(1.5) - let lineHeight = Stack_Display.font!.pointSize * lineSpacing + let lineHeight = Disass_Display.font!.pointSize * lineSpacing // print("lineHeight:", lineHeight, "fontSize:", Stack_Display.font?.pointSize) - scrollTo.y = round( (scrollTo.y + round(event.scrollingDeltaY) * lineHeight) / lineHeight) * lineHeight - Stack_Display.scroll(scrollTo) + let y1 = round( (scrollTo.y + round(event.scrollingDeltaY) * lineHeight) / lineHeight) * lineHeight + let y2 = round( scrollTo.y / lineHeight + event.scrollingDeltaY ) * lineHeight + + if y1 != y2 { + print("NOT EQ", y1, y2) + } + + scrollTo.y = y1 + + Disass_Display.scroll(scrollTo) } @@ -186,40 +194,94 @@ N V - B D I Z C } - let disass_addr_max : UInt16 = 50 + let disass_addr_min : UInt16 = 320 + let disass_addr_max : UInt16 = 170 var disass_addr : UInt16 = 0 + var line_number = 0 + var current_line_number = 0 + let lines_to_disass = 300 + + + func get_scroll_line(view: DisplayView) -> Int { + let scrollPos = view.visibleRect.origin.y + let lineSpacing = CGFloat(1.5) + let lineHeight = view.font!.pointSize * lineSpacing + + return Int(scrollPos / lineHeight) + } + + + func scroll_to(view: DisplayView, line: Int) { + var scrollTo = view.visibleRect.origin + let lineSpacing = CGFloat(1.5) + let lineHeight = view.font!.pointSize * lineSpacing + + scrollTo.y = CGFloat(line) * lineHeight + + view.scroll(scrollTo) + } + func DisplayDisassembly() { let m6502_saved = m6502 var disass = "" + line_number = 0 + current_line_number = 0 + if m6502.PC > disass_addr && m6502.PC < disass_addr + disass_addr_max { m6502.PC = disass_addr } else { disass_addr = m6502.PC + m6502.PC -= disass_addr_min + 20 + + // try to sync disassembly code + while m6502.PC < disass_addr - disass_addr_min { + m6502_Disass_1_Instr() +// line_number += 1 + } + + // hopefully instruction address is in sync + disass_addr = m6502.PC } -// m6502.PC = 0xFF3A - - for _ in 1...35 { - let current_line = m6502.PC == m6502_saved.PC + // normal disassembly + for _ in 1...lines_to_disass { + let isCurrentLine = m6502.PC == m6502_saved.PC m6502_Disass_1_Instr() + line_number += 1 - var line = String(cString: disassemblyLine( current_line )!) + var line = String(cString: disassemblyLine( isCurrentLine )!) - if current_line { + if isCurrentLine { line = invertLine(line: line) + current_line_number = line_number } disass += line + "\n" } DispatchQueue.main.async { +// let isEmpty = self.Disass_Display.string.isEmpty self.Disass_Display.string = disass - } + let currentScrollLine = self.get_scroll_line(view: self.Disass_Display) + 1 + if self.current_line_number <= currentScrollLine || self.current_line_number > currentScrollLine + 35 { + self.scroll_to(view: self.Disass_Display, line: self.current_line_number - 5) + // at the beginning it takes a while to fill up the buffer -- maybe allocation issue? + if currentScrollLine == 1 { + // so we need to scroll a bit later when the string is already populated + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + self.scroll_to(view: self.Disass_Display, line: self.current_line_number - 5) + } + } + } + } + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + // your code here + } m6502 = m6502_saved } diff --git a/A2Mac/DebuggerWindowController.swift b/A2Mac/DebuggerWindowController.swift index 4700dc8..b325c2b 100644 --- a/A2Mac/DebuggerWindowController.swift +++ b/A2Mac/DebuggerWindowController.swift @@ -143,6 +143,7 @@ class DebuggerWindowController: NSWindowController, NSWindowDelegate { } // TODO: Update Screen and speaker etc + ViewController.current?.Update() } @@ -154,6 +155,7 @@ class DebuggerWindowController: NSWindowController, NSWindowDelegate { } // TODO: Update Screen and speaker etc + ViewController.current?.Update() } @@ -179,6 +181,7 @@ class DebuggerWindowController: NSWindowController, NSWindowDelegate { } // TODO: Update Screen and speaker etc + ViewController.current?.Update() } diff --git a/A2Mac/ViewController.swift b/A2Mac/ViewController.swift index 2ad7153..c2ca0f0 100644 --- a/A2Mac/ViewController.swift +++ b/A2Mac/ViewController.swift @@ -1162,7 +1162,7 @@ class ViewController: NSViewController { // Rendering is happening in the main thread, which has two implications: // 1. We can update UI elements // 2. it is independent of the simulation, de that is running in the background thread while we are busy with rendering... - DispatchQueue.main.sync { + DispatchQueue.main.async { self.UpdateText() self.UpdateCPUspeed() @@ -1229,31 +1229,31 @@ class ViewController: NSViewController { func diskButtonUpdate() { - DispatchQueue.main.sync { + DispatchQueue.main.async { // Disk Motor LED - if ( frameCounter % DEF_DRV_LED_DIV == 0 ) { + if ( self.frameCounter % DEF_DRV_LED_DIV == 0 ) { if spkr_is_disk_motor_playing() { - if disk1_led.isHidden { - disk1_led.isHidden = false + if self.disk1_led.isHidden { + self.disk1_led.isHidden = false } } else { - if !disk1_led.isHidden { - disk1_led.isHidden = true + if !self.disk1_led.isHidden { + self.disk1_led.isHidden = true } } } // Disk Loaded - if ( frameCounter % DEF_DRV_LED_DIV == 0 ) { + if ( self.frameCounter % DEF_DRV_LED_DIV == 0 ) { if woz_is_loaded() > 0 { - if disk1_closed.isHidden { - disk1_closed.isHidden = false + if self.disk1_closed.isHidden { + self.disk1_closed.isHidden = false } } else { - if !disk1_closed.isHidden { - disk1_closed.isHidden = true + if !self.disk1_closed.isHidden { + self.disk1_closed.isHidden = true } } } diff --git a/A2iOS/ViewController.swift b/A2iOS/ViewController.swift index 1cfe4a3..eeef89c 100644 --- a/A2iOS/ViewController.swift +++ b/A2iOS/ViewController.swift @@ -701,7 +701,7 @@ class ViewController: UIViewController { // Rendering is happening in the main thread, which has two implications: // 1. We can update UI elements // 2. it is independent of the simulation, de that is running in the background thread while we are busy with rendering... - DispatchQueue.main.sync { + DispatchQueue.main.async { var unicodeTextString : String = "" var fromLines = 0