// SPDX-License-Identifier: GPL-3.0-or-later import Foundation import XCTest final class AssemblyReferenceTests: XCTestCase { private let catalog = AssemblyReference.shared private var root: URL { URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() .deletingLastPathComponent().deletingLastPathComponent() } func testVocabularyAgainstBundledAssemblerAndDecoder() throws { func matches(_ expression: String, _ file: String) throws -> [[String]] { let text = try String(contentsOf: root.appendingPathComponent(file), encoding: .utf8) as NSString let regex = try NSRegularExpression(pattern: expression) return regex.matches(in: text as String, range: NSRange(location: 0, length: text.length)).map { match in (1.. [String] { catalog.completions(in: source, at: (source as NSString).length, target6309: hd) } XCTAssertEqual(catalog.search("BHS", target6309: false, category: nil).first?.name, "BCC") XCTAssertTrue(catalog.search("TFM", target6309: false, category: .instructions).isEmpty) XCTAssertTrue(catalog.search(",X+", target6309: nil, category: .addressing).contains { $0.name == "Indexed" }) XCTAssertTrue(catalog.search("copy a byte", target6309: false, category: nil).contains { $0.name == "LDA" }) XCTAssertEqual(complete(" tfr a,b"), ["B"]) XCTAssertFalse(complete(" tfr a,").contains("X")) XCTAssertTrue(complete(" lda ,", true).contains("W")) XCTAssertFalse(complete(" lda ,").contains("W")) XCTAssertTrue(complete("save fcb 0\n sta #s").isEmpty) XCTAssertTrue(complete(" lda E,").isEmpty) XCTAssertFalse(complete(" lda A,", true).contains("W")) XCTAssertFalse(complete(" pshs ").contains("S")) XCTAssertTrue(complete(" pshu ").contains("S")) XCTAssertEqual(complete(" tfm x-,y", true), ["Y-"]) XCTAssertTrue(complete(" fcc /ld").isEmpty) XCTAssertTrue(complete(" lda #1 ; ld").isEmpty) XCTAssertTrue(complete(" lda #1 ld").isEmpty) XCTAssertTrue(complete(" i\u{0301} ; ld").isEmpty) XCTAssertEqual(complete("myTable fcb 1\n lda myT"), ["myTable"]) XCTAssertEqual(catalog.topic(in: "start lda ,x+", at: 10, target6309: false), "LDA") XCTAssertNil(catalog.topic(in: " ldw #1", at: 10, target6309: false)) let lesson = AssemblyReferenceLesson.all[0] XCTAssertNotEqual(lesson.request.id, lesson.request.id) } /// All teaching listings use one DECB load block and an entry at $3F00. private func assemble(_ lesson: AssemblyReferenceLesson, hd: Bool, folder: URL) throws -> (Int32, Data) { let candidates = [Bundle.main.resourceURL?.appendingPathComponent("Toolchain/bin/lwasm"), root.appendingPathComponent("platforms/macos/LocalResources/ToolchainBundle/bin/lwasm"), root.appendingPathComponent(".build/toolchain/install/bin/lwasm")].compactMap { $0 } guard let tool = candidates.first(where: { FileManager.default.isExecutableFile(atPath: $0.path) }) else { throw XCTSkip("A local or bundled LWASM is required to assemble the reference lessons.") } let source = folder.appendingPathComponent(lesson.id + ".asm") let output = folder.appendingPathComponent(UUID().uuidString + ".bin") try lesson.source.write(to: source, atomically: true, encoding: .utf8) let process = Process() process.executableURL = tool process.arguments = [hd ? "--6309" : "--6809","--format=decb","--output=" + output.path,source.path] try process.run() process.waitUntilExit() return (process.terminationStatus, (try? Data(contentsOf: output)) ?? Data()) } @MainActor func testAssembledLessonsExecuteOnDeclaredProcessorsAndModes() async throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } func command(_ operation: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot { await withCheckedContinuation { cont in operation { cont.resume(returning: $0) } } } for lesson in AssemblyReferenceLesson.all { if lesson.hd { XCTAssertNotEqual(try assemble(lesson, hd: false, folder: folder).0, 0) } for hd in (lesson.hd ? [true] : [false,true]) { let (status, bin) = try assemble(lesson, hd: hd, folder: folder) XCTAssertEqual(status, 0) guard bin.count >= 10 else { XCTFail(lesson.id); continue } XCTAssertEqual(Array(bin.prefix(1)), [0]) XCTAssertEqual(Array(bin[3...4]), [0x3F,0]) XCTAssertEqual(Array(bin.suffix(5)), [0xFF,0,0,0x3F,0]) let code = Data(bin.dropFirst(5).dropLast(5)) XCTAssertEqual(code.count, Int(bin[1]) * 256 + Int(bin[2])) if lesson.id == "cell" { XCTAssertEqual(Array(code), [0x86,0x8F,0xB7,4,0,0x20,0xFE]) } for native in (hd ? [false,true] : [false]) { let controller = VCCMachineController() _ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) } _ = await command { controller.loadDiagnostic(use6309: hd, completion: $0) } if native { _ = await command { controller.debuggerWriteMemory(address: 0x3E00, data: Data([0x11,0x3D,1]), completion: $0) } _ = await command { controller.debuggerSetPC(0x3E00, completion: $0) } _ = await command(controller.debuggerStep) } _ = await command { controller.debuggerWriteMemory(address: 0x3F00, data: code, completion: $0) } _ = await command { controller.debuggerSetPC(0x3F00, completion: $0) } let steps = ["cell":2,"count":7,"call":6,"page":2,"signed":4,"word":1,"transfer":4][lesson.id]! for _ in 0..