// SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI import AppKit import UniformTypeIdentifiers struct TraceView: View { @ObservedObject var model: TraceModel var body: some View { VStack(alignment: .leading, spacing: 0) { controls.padding(12) Divider() HStack { Text(model.snapshot?.status ?? "No trace").fontWeight(.semibold).accessibilityIdentifier("trace-status") Text("\(model.snapshot?.total ?? 0) / \(model.snapshot?.limit ?? 0) samples").monospacedDigit().foregroundStyle(.secondary) Spacer() Button { model.page(false) } label: { Label("Previous", systemImage: "chevron.left") }.disabled((model.snapshot?.offset ?? 0) == 0) Button { model.page(true) } label: { Label("Next", systemImage: "chevron.right") }.disabled((model.snapshot?.offset ?? 0) + 256 >= (model.snapshot?.total ?? 0)) TextField("Sample", text: $model.sampleText).frame(width: 70).onSubmit(model.go) .accessibilityLabel("Trace sample number").accessibilityIdentifier("trace-sample") Button("Go", action: model.go) }.padding(10) Divider() ScrollView([.vertical, .horizontal]) { LazyVStack(alignment: .leading, spacing: 0) { HStack(spacing: 12) { Text("Sample").frame(width: 60, alignment: .leading) Text("Master clock").frame(width: 110, alignment: .leading) Text("Event").frame(width: 110, alignment: .leading) Text("PC").frame(width: 48, alignment: .leading) Text("Instruction / detail").frame(width: 310, alignment: .leading) }.foregroundStyle(.secondary).padding(.vertical, 5) ForEach(model.snapshot?.records ?? [], id: \.index) { row in Button { model.choose(row) } label: { HStack(spacing: 12) { Text("\(row.index + 1)").frame(width: 60, alignment: .leading) Text("\(row.clock)").frame(width: 110, alignment: .leading) Text(row.kind).frame(width: 110, alignment: .leading) Text(DebuggerModel.hex(row.address)).frame(width: 48, alignment: .leading) Text(row.instruction.isEmpty ? row.detail : row.instruction).frame(width: 310, alignment: .leading) }.padding(.vertical, 4).contentShape(Rectangle()) }.buttonStyle(.plain) .background(model.selection?.index == row.index ? Color.accentColor.opacity(0.16) : Color.clear) .accessibilityIdentifier("trace-row-\(row.index)") } }.font(.system(size: 11, design: .monospaced)).padding(.horizontal, 12) }.frame(minHeight: 100, maxHeight: .infinity).accessibilityIdentifier("trace-rows") Divider() VStack(alignment: .leading, spacing: 5) { if let row = model.selection { Text("Sample \(row.index + 1) · frame \(row.frame) · \(row.cycles) instruction cycles · \(row.detail)").font(.caption) Text("Before: " + row.before).font(.system(size: 11, design: .monospaced)) Text("After: " + row.after).font(.system(size: 11, design: .monospaced)) } else { Text("Select a sample to inspect its before/after registers.").foregroundStyle(.secondary).font(.caption) } HStack { Button { model.mark(true) } label: { Label("Mark A", systemImage: "flag") }.disabled(model.selection == nil).accessibilityIdentifier("trace-mark-a") Text(model.markA.map { "#\($0.index + 1)" } ?? "—") Button { model.mark(false) } label: { Label("Mark B", systemImage: "flag.fill") }.disabled(model.selection == nil).accessibilityIdentifier("trace-mark-b") Text(model.markB.map { "#\($0.index + 1)" } ?? "—") if let difference = model.markDifference { Text(difference).font(.caption).accessibilityIdentifier("trace-difference") } } }.padding(10) Divider() HStack { Text("Export samples").font(.caption) TextField("First", text: $model.firstExport).frame(width: 65).accessibilityLabel("First trace sample to export") Text("through").font(.caption) TextField("Last", text: $model.lastExport).frame(width: 65).accessibilityLabel("Last trace sample to export, blank for all") Button(action: export) { Label("Export CSV…", systemImage: "square.and.arrow.up") }.disabled(!model.available || (model.snapshot?.total ?? 0) == 0).accessibilityIdentifier("trace-export") Spacer() }.padding(10) if let message = model.saveMessage { Text(message).font(.caption).foregroundStyle(.secondary).padding(10).accessibilityIdentifier("trace-save-message") } if let error = model.error { Text(error).foregroundStyle(.red).font(.caption).padding(10).accessibilityIdentifier("trace-error") } HStack { Text("Start New replaces the previous capture; Clear removes it.") .font(.caption).foregroundStyle(.secondary) Spacer() DebuggerHelpButton(title: "Execution trace", text: "Trace controls never resume or pause the machine. Start replaces the previous capture; Clear removes it. Addresses are logical hexadecimal. Captured bytes and states are historical.") }.padding(.horizontal, 10).padding(.bottom, 8) }.textFieldStyle(.roundedBorder).task { await model.poll() } } private var controls: some View { VStack(alignment: .leading, spacing: 8) { HStack { Label("Capture", systemImage: "waveform.path").font(.headline) Spacer() Button(action: model.start) { Label("Start New", systemImage: "record.circle") }.disabled(!model.available).accessibilityIdentifier("trace-start") Button(action: model.stop) { Label("Stop", systemImage: "stop.fill") }.disabled(!model.available || model.snapshot?.armed != true).accessibilityIdentifier("trace-stop") Button(action: model.clear) { Label("Clear", systemImage: "xmark.circle") }.disabled(!model.available).accessibilityIdentifier("trace-clear") } HStack { Text("Sample limit").font(.caption) TextField("10000", text: $model.maximum).frame(width: 80).accessibilityLabel("Maximum trace samples").accessibilityIdentifier("trace-limit") Toggle("Screen events", isOn: $model.screen).accessibilityIdentifier("trace-screen") Toggle("Scheduler events", isOn: $model.scheduler).accessibilityIdentifier("trace-scheduler") } HStack { Text("Start at").font(.caption) TextField("Immediately (blank)", text: $model.starts).accessibilityLabel("Trace start addresses").accessibilityIdentifier("trace-start-addresses") Text("Stop before").font(.caption) TextField("Limit or Stop (blank)", text: $model.stops).accessibilityLabel("Trace stop addresses").accessibilityIdentifier("trace-stop-addresses") } } } private func export() { model.export { text in guard let text else { return } let panel = NSSavePanel(); panel.title = "Export Execution Trace" panel.allowedContentTypes = [.commaSeparatedText]; panel.nameFieldStringValue = "VCC Trace.csv" panel.begin { response in guard response == .OK, let url = panel.url else { return } Task { await model.save(text, to: url) } } } } }