142 lines
6.8 KiB
Swift
142 lines
6.8 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import AppKit
|
|
import Combine
|
|
|
|
@MainActor
|
|
final class GraphicsPreviewModel: ObservableObject {
|
|
enum Source: String, CaseIterable { case current = "Current Video Page", address = "At Address" }
|
|
enum Mode: Int, CaseIterable {
|
|
case sg4, pmode0, pmode1, pmode2, pmode3, pmode4, hscreen1, hscreen2, hscreen3, hscreen4
|
|
var label: String {
|
|
if self == .sg4 { return "Text / SG4" }
|
|
if rawValue <= Self.pmode4.rawValue { return "PMODE \(rawValue - 1)" }
|
|
return "HSCREEN \(rawValue - 5)"
|
|
}
|
|
var supportsColorSet: Bool { rawValue >= Self.pmode0.rawValue && rawValue <= Self.pmode4.rawValue }
|
|
}
|
|
struct Request: Equatable {
|
|
var source = Source.current
|
|
var physical = false
|
|
var address: UInt32 = 0x0e00
|
|
var mode = Mode.pmode1
|
|
var alternate = false
|
|
}
|
|
struct Pixel: Equatable { var x = 0; var y = 0 }
|
|
struct Frame {
|
|
let capture: VCCDebugGraphicsCapture
|
|
let image: CGImage
|
|
let highlights: CGImage?
|
|
let changes: Int?
|
|
}
|
|
@Published var request = Request()
|
|
@Published var addressText = "0E00"
|
|
@Published var addressError: String?
|
|
@Published private(set) var frame: Frame?
|
|
@Published private(set) var error: String?
|
|
@Published var selection = Pixel()
|
|
private var epoch: UInt64 = 0
|
|
|
|
func readyToStep(machine: MachineModel) -> Bool {
|
|
if error != nil { return true }
|
|
guard let snapshot = frame?.capture.machine else { return false }
|
|
return snapshot.state == .paused && snapshot.generation == machine.basicDebug.generation &&
|
|
snapshot.cycles == machine.basicDebug.cycles
|
|
}
|
|
|
|
func inspectAddress(_ address: UInt32, physical: Bool) {
|
|
addressText = DebuggerModel.hex(address, width: physical ? 6 : 4)
|
|
addressError = nil
|
|
request.physical = physical
|
|
request.address = address
|
|
request.source = .address
|
|
}
|
|
|
|
func applyAddress() {
|
|
guard let value = DebuggerModel.hexadecimalAddress(addressText, maximum: request.physical ? 0x7fffff : 0xffff) else {
|
|
addressError = request.physical ? "Enter a physical address from $000000 to $7FFFFF." : "Enter a CPU address from $0000 to $FFFF."
|
|
return
|
|
}
|
|
inspectAddress(value, physical: request.physical)
|
|
}
|
|
|
|
private static func image(_ rgba: Data, width: Int, height: Int) -> CGImage? {
|
|
guard width > 0, height > 0, rgba.count == width * height * 4,
|
|
let provider = CGDataProvider(data: rgba as CFData) else { return nil }
|
|
return CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 32,
|
|
bytesPerRow: width * 4, space: CGColorSpaceCreateDeviceRGB(),
|
|
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue),
|
|
provider: provider, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
|
|
}
|
|
|
|
// A translucent overlay preserves the colors underneath. Work is bounded
|
|
// by one native raster and is only performed for an observed single step.
|
|
static func differences(_ before: Data, _ after: Data) -> (Data, Int) {
|
|
guard before.count == after.count, after.count.isMultiple(of: 4) else { return (Data(), 0) }
|
|
var mask = Data(count: after.count)
|
|
var changes = 0
|
|
before.withUnsafeBytes { (old: UnsafeRawBufferPointer) in
|
|
after.withUnsafeBytes { (new: UnsafeRawBufferPointer) in
|
|
mask.withUnsafeMutableBytes { (out: UnsafeMutableRawBufferPointer) in
|
|
for i in stride(from: 0, to: new.count, by: 4) {
|
|
if old[i] != new[i] || old[i+1] != new[i+1] || old[i+2] != new[i+2] || old[i+3] != new[i+3] {
|
|
out[i] = 255; out[i+1] = 94; out[i+2] = 190; out[i+3] = 210
|
|
changes += 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return (mask, changes)
|
|
}
|
|
|
|
func poll(machine: MachineModel) async {
|
|
let intent = request
|
|
epoch &+= 1
|
|
let token = epoch
|
|
frame = nil
|
|
error = nil
|
|
var lastRead: (epoch: UInt64, snapshot: VCCMachineSnapshot)?
|
|
while !Task.isCancelled, token == epoch, intent == request {
|
|
// Repeated polling of an unchanged paused machine needs no extra
|
|
// bitmap. Commands increment inputEpoch, including paused RAM edits.
|
|
if machine.canQuickLoad, machine.state == .running || lastRead?.snapshot.state != .paused ||
|
|
lastRead?.epoch != machine.inputEpoch || lastRead?.snapshot.cycles != machine.basicDebug.cycles ||
|
|
lastRead?.snapshot.generation != machine.basicDebug.generation {
|
|
let inputEpoch = machine.inputEpoch
|
|
let sample = await machine.readGraphicsPreview(currentPage: intent.source == .current,
|
|
address: intent.address, physical: intent.physical, mode: intent.mode.rawValue,
|
|
alternatePalette: intent.mode.supportsColorSet && intent.alternate)
|
|
guard !Task.isCancelled, epoch == token, request == intent else { return }
|
|
if let sample, inputEpoch == machine.inputEpoch, sample.machine.generation == machine.basicDebug.generation {
|
|
lastRead = (inputEpoch, sample.machine)
|
|
accept(sample, step: machine.lastDebuggerStep)
|
|
}
|
|
}
|
|
do { try await Task.sleep(for: .milliseconds(250)) } catch { return }
|
|
}
|
|
}
|
|
|
|
private func accept(_ sample: VCCDebugGraphicsCapture, step: (generation: UInt64, before: UInt64, after: UInt64)?) {
|
|
if let message = sample.errorMessage {
|
|
error = message; frame = nil
|
|
return
|
|
}
|
|
guard let bitmap = Self.image(sample.rgba, width: sample.width, height: sample.height) else {
|
|
error = "This page could not be rendered."; frame = nil
|
|
return
|
|
}
|
|
var highlights: CGImage?
|
|
var changes: Int?
|
|
if let previous = frame, let step, previous.capture.machine.state == .paused, sample.machine.state == .paused,
|
|
sample.machine.generation == step.generation, sample.machine.cycles == step.after,
|
|
previous.capture.machine.cycles == step.before, sample.hasSameLayout(as: previous.capture) {
|
|
let (mask, count) = Self.differences(previous.capture.rgba, sample.rgba)
|
|
changes = count
|
|
if count > 0 { highlights = Self.image(mask, width: sample.width, height: sample.height) }
|
|
}
|
|
if selection.x >= sample.width || selection.y >= sample.height { selection = Pixel() }
|
|
error = nil
|
|
frame = Frame(capture: sample, image: bitmap, highlights: highlights, changes: changes)
|
|
}
|
|
}
|