// SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI import AppKit import UniformTypeIdentifiers struct MemoryToolsView: View { @ObservedObject var model: MemoryToolsModel var body: some View { VStack(alignment: .leading, spacing: 12) { HStack { Picker("Address space", selection: $model.space) { ForEach(MemoryToolsModel.Space.allCases, id: \.self) { Text($0.rawValue).tag($0) } }.frame(width: 240).accessibilityIdentifier("memory-tools-space") Spacer() Text("All addresses are hexadecimal").font(.caption).foregroundStyle(.secondary) DebuggerHelpButton(title: "Memory capture and export", text: "Physical addresses identify RAM locations, not CPU execution addresses. The assumed CPU origin controls relative branch targets. Captures are read-only; Disassemble shows up to 128 rows. Export writes the entire selected range as raw bytes, without a LOADM header.") } HStack(spacing: 12) { entry("Start", text: $model.startText, id: "memory-tools-start") entry("End (inclusive)", text: $model.endText, id: "memory-tools-end") if model.space == .physical { entry("Assumed CPU origin", text: $model.logicalText, id: "memory-tools-origin") } } HStack { Toggle("Interpret OS-9 calls and module headers", isOn: $model.os9) .toggleStyle(.checkbox).accessibilityIdentifier("memory-tools-os9") Spacer() Button { Task { await model.decode() } } label: { Label("Disassemble", systemImage: "list.bullet.rectangle") }.disabled(!model.available) .accessibilityIdentifier("memory-tools-decode") Button(action: export) { Label("Export Binary…", systemImage: "square.and.arrow.up") }.disabled(!model.available) .accessibilityIdentifier("memory-tools-export") } Divider() Text(model.caption).font(.caption).foregroundStyle(.secondary) .accessibilityIdentifier("memory-tools-caption") ScrollView([.horizontal, .vertical]) { LazyVStack(alignment: .leading, spacing: 4) { HStack(spacing: 12) { Text("Location").frame(width: 75, alignment: .leading) Text("CPU").frame(width: 45, alignment: .leading) Text("Bytes").frame(width: 160, alignment: .leading) Text("Instruction / annotation") }.foregroundStyle(.secondary) ForEach(model.rows) { row in HStack(alignment: .top, spacing: 12) { Text(DebuggerModel.hex(row.location, width: model.capturedPhysical ? 6 : 4)).frame(width: 75, alignment: .leading) Text(DebuggerModel.hex(row.logicalAddress)).frame(width: 45, alignment: .leading) Text(row.bytes.isEmpty ? "—" : row.bytes).frame(width: 160, alignment: .leading) Text(row.mnemonic).frame(width: 60, alignment: .leading) Text(row.operand) }.accessibilityElement(children: .combine) .accessibilityIdentifier("memory-tools-row-\(row.id)") .contextMenu { Button("Copy Row") { NSPasteboard.general.clearContents() NSPasteboard.general.setString("\(DebuggerModel.hex(row.location, width: 6)) \(row.bytes) \(row.mnemonic) \(row.operand)", forType: .string) } } } }.font(.system(size: 12, design: .monospaced)).padding(8) }.frame(minHeight: 180).background(Color(nsColor: .textBackgroundColor), in: RoundedRectangle(cornerRadius: 6)) if let error = model.error { Text(error).foregroundStyle(.red).font(.caption).fixedSize(horizontal: false, vertical: true) .accessibilityIdentifier("memory-tools-error") } if let message = model.saveMessage { Text(message).font(.caption).accessibilityIdentifier("memory-tools-saved") } if model.pending { ProgressView().controlSize(.small) } }.padding(16).frame(minWidth: 760, idealWidth: 900, minHeight: 420, idealHeight: 600) } private func entry(_ label: String, text: Binding, id: String) -> some View { HStack { Text(label) TextField(label, text: text).labelsHidden().textFieldStyle(.roundedBorder) .font(.system(.body, design: .monospaced)).frame(width: 90) .accessibilityLabel(label + " in hexadecimal").accessibilityIdentifier(id) } } private func export() { Task { guard let bytes = await model.exportBytes() else { return } let panel = NSSavePanel() panel.title = "Export Raw Memory" panel.nameFieldStringValue = "memory.raw" panel.allowedContentTypes = [.data] panel.begin { result in guard result == .OK, let url = panel.url else { return } Task { await model.save(bytes, to: url) } } } } }