219 lines
14 KiB
Swift
219 lines
14 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
import AppKit
|
||
import SwiftUI
|
||
import UniformTypeIdentifiers
|
||
|
||
struct AssemblyDevelopmentView: View {
|
||
@ObservedObject var model: AssemblyDevelopmentModel
|
||
@Environment(\.openWindow) private var openWindow
|
||
@StateObject private var finder = SourceFind()
|
||
@StateObject private var pool = AssemblyEditorPool()
|
||
@State private var details = false
|
||
@State private var detailPane = "Listing"
|
||
@State private var diskExport = false
|
||
@State private var diskName = "FIRST"
|
||
@State private var diskBoot = false
|
||
@State private var settingsHelp = false
|
||
private var locked: Bool { model.working || model.commanding }
|
||
|
||
var body: some View {
|
||
VStack(alignment: .leading, spacing: 0) {
|
||
HStack {
|
||
Label("Assembly Development", systemImage: "cpu").font(.title2.bold())
|
||
Spacer()
|
||
Button(action: reference) { Label("Quick Reference", systemImage: "book.closed") }
|
||
.keyboardShortcut("?", modifiers: [.command, .option]).accessibilityIdentifier("assembly-reference")
|
||
Button { openWindow(id: "debugger") } label: { Label("Debugger", systemImage: "ladybug") }
|
||
.help("Inspect registers, flags, memory, trace and Graphics Preview")
|
||
}.padding(.horizontal, 14).padding(.vertical, 10)
|
||
Divider()
|
||
HStack {
|
||
Menu("File") {
|
||
Button("New Source") { model.openExample(title: "Untitled", source: "", target6309: false) }
|
||
Button(AssemblyWorkspaceDocument.starterTitle) {
|
||
model.openExample(title: AssemblyWorkspaceDocument.starterTitle, source: AssemblyWorkspaceDocument.starterSource, target6309: false)
|
||
}
|
||
Divider()
|
||
Button("Local Assistant Access…") { MCPAssembly.shared.show(model, editors: pool) }
|
||
Button("Open Source…") { open(folder: false) }
|
||
Button("Open Project Folder…") { open(folder: true) }
|
||
Button("Project Settings…") { settingsHelp = true }.disabled(!model.document.isProject)
|
||
Divider()
|
||
Button("Save Source", action: save).keyboardShortcut("s")
|
||
Button("Save a Copy…") { saveCopy() }
|
||
Button("Reload from Disk") { model.reload() }.disabled(model.document.directory == nil)
|
||
Button("Close Document") { model.closeDocument() }
|
||
}.fixedSize().accessibilityIdentifier("assembly-file")
|
||
Picker("Document", selection: Binding(get: { model.activeID }, set: model.select)) {
|
||
ForEach(model.documents) { doc in
|
||
Text(doc.title + (doc.buffers.contains(where: \.dirty) ? " •" : "")).tag(doc.id)
|
||
}
|
||
}.labelsHidden().accessibilityLabel("Assembly document").accessibilityIdentifier("assembly-document")
|
||
Spacer()
|
||
Picker("Target", selection: Binding(get: { model.cpu }, set: model.setCPU)) {
|
||
Text("MC6809").tag(AssemblyCPU.mc6809)
|
||
Text("HD6309").tag(AssemblyCPU.hd6309)
|
||
}.fixedSize().accessibilityIdentifier("assembly-cpu")
|
||
}.disabled(model.commanding).padding(.horizontal, 14).padding(.vertical, 6)
|
||
HStack(spacing: 8) {
|
||
Button { model.perform(.assemble) } label: { Label("Assemble", systemImage: "hammer") }
|
||
.keyboardShortcut("b").accessibilityIdentifier("assembly-assemble").disabled(locked)
|
||
Button { model.perform(.run) } label: { Label("Run", systemImage: "play.fill") }
|
||
.keyboardShortcut("r").buttonStyle(.borderedProminent).accessibilityIdentifier("assembly-run").disabled(locked)
|
||
Button { model.perform(.debug) } label: { Label("Debug from Entry", systemImage: "playpause") }
|
||
.accessibilityIdentifier("assembly-debug-entry").disabled(locked)
|
||
if model.working && !model.commanding {
|
||
Button("Cancel") { model.cancelBuild() }.accessibilityIdentifier("assembly-cancel")
|
||
}
|
||
Spacer()
|
||
Menu("Export") {
|
||
Button("Export Binary…") { export(disk: false) }.accessibilityIdentifier("assembly-export-binary")
|
||
Button("Export Disk Image…") { diskBoot = false; diskExport = true }.accessibilityIdentifier("assembly-export-disk")
|
||
}.disabled(locked).fixedSize().accessibilityIdentifier("assembly-export")
|
||
}.padding(.horizontal, 14).padding(.vertical, 5)
|
||
Divider()
|
||
HStack(spacing: 8) {
|
||
if model.document.paths.count > 1 || model.document.isProject {
|
||
Menu(model.buffer.path) {
|
||
ForEach(model.document.paths, id: \.self) { path in Button(path) { model.selectFile(path) } }
|
||
}.fixedSize().accessibilityLabel("Project files")
|
||
} else { Label(model.buffer.path, systemImage: "doc.text") }
|
||
Text("Line \(model.cursorLine)").foregroundStyle(.secondary)
|
||
Spacer()
|
||
SourceFindButton(finder: finder)
|
||
Menu("Labels") {
|
||
Button("Go to Definition", action: model.goToDefinition)
|
||
Divider()
|
||
ForEach(Array(model.labelLocations.prefix(500).enumerated()), id: \.offset) { _, item in
|
||
Button("\(item.0) · \(item.1.path):\(item.1.line)") { model.selectFile(item.1.path, line: item.1.line) }
|
||
}
|
||
}.fixedSize()
|
||
Button { pool.complete(model.buffer.id) } label: { Label("Complete", systemImage: "text.badge.plus") }
|
||
.help("Complete at the caret (Control–Space)").accessibilityIdentifier("assembly-complete")
|
||
Button { model.toggleBreakpoint() } label: {
|
||
Label(model.document.breakpoints.contains(.init(path: model.buffer.path, line: model.cursorLine)) ? "Remove Stop" : "Set Stop",
|
||
systemImage: "record.circle")
|
||
}.disabled(locked).help("Toggle a breakpoint before this source instruction").accessibilityIdentifier("assembly-breakpoint")
|
||
if !model.document.breakpoints.isEmpty {
|
||
Menu("Stops (\(model.document.breakpoints.count))") {
|
||
Button("Clear All Stops", action: model.clearStops)
|
||
Divider()
|
||
ForEach(model.document.breakpoints.sorted { ($0.path, $0.line) < ($1.path, $1.line) }, id: \.self) { stop in
|
||
Button("\(stop.path):\(stop.line)") { model.selectFile(stop.path, line: stop.line) }
|
||
}
|
||
}.fixedSize()
|
||
}
|
||
}.font(.callout).padding(.horizontal, 14).padding(.vertical, 5)
|
||
SourceFindBar(finder: finder, source: model.buffer.text, readOnly: false)
|
||
AssemblyEditor(finder: finder, model: model, pool: pool, lookup: reference).frame(minHeight: 130)
|
||
if model.document.title == AssemblyWorkspaceDocument.starterTitle {
|
||
Text("Run, then press 1 for Earth or 2 for Mars in the emulator. After the greeting, you can type BASIC commands at the OK prompt.")
|
||
.font(.callout).foregroundStyle(.secondary).padding(.horizontal, 14).padding(.top, 6)
|
||
} else if model.document.title == "First Program — Change a Screen Cell" {
|
||
Text("In Disk BASIC’s 32-column display, this is POKE &H0400,&H8F. Step LDA to change A, then STA to change the first screen cell.")
|
||
.font(.callout).foregroundStyle(.secondary).padding(.horizontal, 14).padding(.top, 6)
|
||
}
|
||
HStack {
|
||
Button("Pause", action: model.pause).disabled(!model.attached || model.paused || locked).accessibilityIdentifier("assembly-pause")
|
||
Button("Step") { model.control(step: true) }.disabled(!model.paused || locked).accessibilityIdentifier("assembly-step")
|
||
Button("Continue") { model.control(step: false) }.disabled(!model.paused || locked).accessibilityIdentifier("assembly-continue")
|
||
Text(model.execution).font(.system(.caption, design: .monospaced)).textSelection(.enabled)
|
||
.accessibilityIdentifier("assembly-execution")
|
||
Spacer()
|
||
}.padding(.horizontal, 14).padding(.vertical, 7)
|
||
Divider()
|
||
Text(model.message).font(.callout).textSelection(.enabled).accessibilityIdentifier("assembly-status")
|
||
.padding(.horizontal, 14).padding(.top, 7)
|
||
if !model.diagnostics.isEmpty {
|
||
ScrollView {
|
||
VStack(alignment: .leading) {
|
||
ForEach(Array(model.diagnostics.enumerated()), id: \.offset) { _, item in
|
||
Button("\(item.path):\(item.line) \(item.message)") { model.selectFile(item.path, line: item.line) }
|
||
.buttonStyle(.link).accessibilityIdentifier("assembly-diagnostic")
|
||
}
|
||
}.frame(maxWidth: .infinity, alignment: .leading)
|
||
}.frame(maxHeight: 74).padding(.horizontal, 14)
|
||
}
|
||
DisclosureGroup("Build details", isExpanded: $details) {
|
||
Picker("Build details", selection: $detailPane) {
|
||
ForEach(["Listing", "Symbols", "Log"], id: \.self) { Text($0) }
|
||
}.pickerStyle(.segmented).labelsHidden().frame(maxWidth: 320)
|
||
ScrollView([.vertical, .horizontal]) {
|
||
Text(detailPane == "Log" ? model.buildLog : (detailPane == "Listing" ? model.artifact?.listing : model.artifact?.symbols) ?? "")
|
||
.font(.system(.caption, design: .monospaced)).textSelection(.enabled)
|
||
}.frame(maxWidth: .infinity, minHeight: 45, maxHeight: 150, alignment: .topLeading)
|
||
}.padding(.horizontal, 14).padding(.vertical, 7)
|
||
}.frame(minWidth: 690, minHeight: 420)
|
||
.focusedSceneValue(\.sourceFind, finder)
|
||
.onDisappear { MCPAssembly.shared.close(for: model) }
|
||
.onChange(of: model.displayRequest) { _, _ in
|
||
openWindow(id: "machine")
|
||
DispatchQueue.main.async { model.machine.focusDisplay() }
|
||
}
|
||
.onChange(of: model.documents.map { $0.buffers.map(\.id) }) { _, ids in pool.forget(except: Set(ids.joined())) }
|
||
.task {
|
||
while !Task.isCancelled {
|
||
model.refresh()
|
||
try? await Task.sleep(nanoseconds: 350_000_000)
|
||
}
|
||
}
|
||
.sheet(isPresented: $diskExport) {
|
||
VStack(alignment: .leading, spacing: 14) {
|
||
Text("Export Disk Image").font(.headline)
|
||
TextField("Disk BASIC filename", text: $diskName).accessibilityIdentifier("assembly-disk-name")
|
||
Text("Use 1–8 letters or digits, without an extension.").font(.caption).foregroundStyle(.secondary)
|
||
Toggle("Install DOS bootloader", isOn: $diskBoot).accessibilityIdentifier("assembly-export-dos")
|
||
if let name = BASICExportName.normalized(diskName) {
|
||
Text(DevelopmentDiskProgram.binary(name).instructions(dos: diskBoot))
|
||
.fixedSize(horizontal: false, vertical: true).accessibilityIdentifier("assembly-export-commands")
|
||
}
|
||
HStack {
|
||
Spacer(); Button("Cancel") { diskExport = false }
|
||
Button("Choose Location…") {
|
||
diskExport = false; DispatchQueue.main.async { export(disk: true) }
|
||
}.keyboardShortcut(.defaultAction).disabled(BASICExportName.normalized(diskName) == nil)
|
||
}
|
||
}.padding(20).frame(width: 440)
|
||
}
|
||
.sheet(isPresented: $settingsHelp) {
|
||
VStack(alignment: .leading, spacing: 12) {
|
||
Text("Project Settings").font(.title2.bold())
|
||
Text("Edit vccplus-assembly.json in the source editor. Assemble validates and uses your changes. Save writes them to the project.")
|
||
Text("Set main to the entry source. For linked objects, add modules (additional source paths) and linkScript (the LWLINK script path).")
|
||
Text("To run an existing Makefile, set makefile, target and binary to the recipe, target name and output path. Each fresh build asks before executing the recipe.")
|
||
HStack {
|
||
Spacer(); Button("Cancel") { settingsHelp = false }
|
||
Button("Edit Settings") { settingsHelp = false; model.editSettings() }.keyboardShortcut(.defaultAction)
|
||
}
|
||
}.padding(20).frame(width: 460)
|
||
}
|
||
}
|
||
private func reference() {
|
||
let extended = model.cpu == .hd6309
|
||
let term = AssemblyReference.shared.topic(in: model.buffer.text, at: model.cursor, target6309: extended)
|
||
AssemblyReferenceSelection.shared.show(term ?? "", target6309: extended)
|
||
openWindow(id: "assembly-reference")
|
||
}
|
||
private func open(folder: Bool) {
|
||
let panel = NSOpenPanel(); panel.canChooseDirectories = folder; panel.canChooseFiles = !folder
|
||
panel.allowsMultipleSelection = false
|
||
if !folder { panel.allowedContentTypes = ["asm", "s", "lwasm", "inc"].compactMap { UTType(filenameExtension: $0) } }
|
||
guard panel.runModal() == .OK, let url = panel.url else { return }
|
||
model.open(url, folder: folder)
|
||
}
|
||
private func save() { if model.document.directory == nil { saveCopy() } else { model.save() } }
|
||
private func saveCopy() {
|
||
let panel = NSSavePanel()
|
||
panel.nameFieldStringValue = (model.buffer.path as NSString).lastPathComponent
|
||
guard panel.runModal() == .OK, let url = panel.url else { return }; model.save(to: url)
|
||
}
|
||
private func export(disk: Bool) {
|
||
let panel = NSSavePanel()
|
||
let name = BASICExportName.normalized(diskName) ?? "FIRST"
|
||
if disk { panel.message = DevelopmentDiskProgram.binary(name).instructions(dos: diskBoot) }
|
||
panel.nameFieldStringValue = disk ? "\(name).dsk" : (model.document.mainPath as NSString).lastPathComponent.components(separatedBy: ".").dropLast().joined(separator: ".") + ".bin"
|
||
guard panel.runModal() == .OK, let url = panel.url else { return }
|
||
model.perform(disk ? .exportDisk(url, name, dosBoot: diskBoot) : .exportBinary(url))
|
||
}
|
||
}
|