215 lines
13 KiB
Swift
215 lines
13 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
|
|
final class AssemblyBuildServiceTests: XCTestCase {
|
|
private var root: URL!
|
|
private var runtime: URL!
|
|
private var service: AssemblyBuildService { AssemblyBuildService(toolchain: runtime) }
|
|
private let first = """
|
|
org $3f00
|
|
start lda #$8f
|
|
sta $0400
|
|
hold bra hold
|
|
end start
|
|
"""
|
|
|
|
override func setUpWithError() throws {
|
|
root = FileManager.default.temporaryDirectory.appendingPathComponent("Assembly tests " + UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
|
runtime = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
|
|
.appendingPathComponent("LocalResources/ToolchainBundle")
|
|
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: runtime.appendingPathComponent("bin/lwasm").path),
|
|
"Stage the bundled LWTools runtime for assembly workflow tests.")
|
|
}
|
|
|
|
override func tearDownWithError() throws {
|
|
if let root { try? FileManager.default.removeItem(at: root) }
|
|
}
|
|
|
|
private func write(_ text: String, _ path: String) throws {
|
|
let url = root.appendingPathComponent(path)
|
|
try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
|
|
try Data(text.utf8).write(to: url)
|
|
}
|
|
|
|
func testStarterImageSymbolLocationsDiagnosticsAndTargetOverride() async throws {
|
|
let result = try await service.build(.single(source: first))
|
|
XCTAssertEqual(result.entry, 0x3f00)
|
|
XCTAssertEqual(result.ranges, [0x3f00...0x3f06])
|
|
XCTAssertEqual(result.binaryData, Data([0,0,7,0x3f,0,0x86,0x8f,0xb7,4,0,0x20,0xfe,0xff,0,0,0x3f,0]))
|
|
XCTAssertEqual(result.instructions.map(\.line), [2,3,4])
|
|
XCTAssertEqual(result.instructions.map(\.address), [0x3f00,0x3f02,0x3f05])
|
|
XCTAssertTrue(result.symbols.contains("start EQU $3F00"))
|
|
let extended = first.replacingOccurrences(of: "lda #$8f", with: "ldw #$1234")
|
|
let hd = try await service.build(.single(source: extended, cpu: .hd6309))
|
|
XCTAssertEqual(hd.instructions.first?.bytes, [0x10,0x86,0x12,0x34])
|
|
for text in [extended, " pragma 6309\n" + extended] {
|
|
do {
|
|
_ = try await service.build(.single(source: text))
|
|
XCTFail("MC6809 target accepted an HD6309 operation")
|
|
} catch let error as AssemblyBuildFailure {
|
|
XCTAssertEqual(error.diagnostics.first?.path, "main.asm")
|
|
XCTAssertEqual(error.diagnostics.first?.line, 2)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testPhysicalIncludesWithCollidingNamesOverlaysAndMacroExpansion() async throws {
|
|
let name = "same_long_include_name.inc"
|
|
try write(" org $3f00\n include 'a/" + name + "'\n include 'b/" + name + "'\n end $3f00", "main.asm")
|
|
try write(" include 'defs.inc'\n lda #one\n fcb $86,1\n", "a/" + name)
|
|
try write("one equ 1", "defs.inc")
|
|
try write(" ldb #2\n", "b/" + name)
|
|
try write("unrelated", "other.txt")
|
|
let main = root.appendingPathComponent("main.asm")
|
|
let saved = try AssemblyProjectSnapshot.readSource(at: main)
|
|
XCTAssertEqual(saved.files.count, 4)
|
|
let draft = try AssemblyProjectSnapshot.readSource(at: main, overlays: ["b/" + name: " ldb #3"])
|
|
XCTAssertNotEqual(saved, draft)
|
|
let result = try await service.build(draft)
|
|
XCTAssertEqual(result.instructions.map(\.path), ["a/" + name, "b/" + name])
|
|
XCTAssertEqual(result.instructions.map(\.bytes), [[0x86,1], [0xc6,3]])
|
|
XCTAssertEqual(result.instructions.map(\.address), [0x3f00,0x3f04])
|
|
let macros = " org $3f00\nload macro\n lda #1\n endm\n load\n fcb $86,1\nhold bra hold\n end $3f00"
|
|
let expanded = try await service.build(.single(source: macros))
|
|
XCTAssertEqual(expanded.instructions.map(\.line), [7])
|
|
|
|
try write(" nop", "twice.inc")
|
|
let repeated = " org $3f00\n include 'twice.inc'\n include 'twice.inc'\n end $3f00"
|
|
let twice = try await service.build(AssemblyProjectSnapshot.readSource(at: main, overlays: ["main.asm": repeated]))
|
|
XCTAssertTrue(twice.instructions.isEmpty, "A single include line expanded at two addresses needs disambiguation.")
|
|
}
|
|
|
|
func testLinkedSubroutineUsesFinalPlacementAndRelocatedOperands() async throws {
|
|
try write(#"{"main":"main.asm","modules":["lib/sub.asm"],"linkScript":"layout.link"}"#, AssemblyProjectSnapshot.configurationFilename)
|
|
try write(" section code\n export start\n import sub\nstart jsr sub\nhold bra hold\n endsection", "main.asm")
|
|
try write(" section code\n export sub\n include 'body.inc'\n endsection", "lib/sub.asm")
|
|
try write("sub incb\n rts", "lib/body.inc")
|
|
try write("section code load 3f00\nentry start\n", "layout.link")
|
|
let result = try await service.build(AssemblyProjectSnapshot.read(directory: root))
|
|
XCTAssertEqual(result.entry, 0x3f00)
|
|
XCTAssertEqual(result.instructions.first?.bytes, [0xbd,0x3f,0x05])
|
|
XCTAssertEqual(result.instructions.filter { $0.path == "lib/body.inc" }.map(\.address), [0x3f05,0x3f06])
|
|
XCTAssertTrue(result.symbols.contains("sub (../out/module1.o) = 3F05"))
|
|
}
|
|
|
|
func testAbsoluteAndBinaryIncludesCannotReadOutsideSnapshot() async throws {
|
|
try write(" nop", "outside.asm")
|
|
for directive in ["include", "includebin"] {
|
|
let line = " " + directive + " \"" + root.appendingPathComponent("outside.asm").path + "\""
|
|
let source = " org $3f00\n" + line + "\n end $3f00"
|
|
do {
|
|
_ = try await service.build(.single(source: source))
|
|
XCTFail("External include succeeded.")
|
|
} catch { XCTAssertTrue(error.localizedDescription.contains("leaves the project snapshot"), error.localizedDescription) }
|
|
}
|
|
}
|
|
|
|
func testCustomMakeUsesOverlayAndDiscardsPriorProduct() async throws {
|
|
try write(first, "main.asm")
|
|
try write("stale", "program.bin")
|
|
let makefile = "all:\n\t$(LWASM) $(ASFLAGS) --decb -o program.bin main.asm\n"
|
|
try write(makefile, "Custom.mk")
|
|
try write(#"{"makefile":"Custom.mk","target":"all","binary":"program.bin"}"#, AssemblyProjectSnapshot.configurationFilename)
|
|
let snap = try AssemblyProjectSnapshot.read(directory: root, overlays: ["main.asm": first.replacingOccurrences(of: "#$8f", with: "#$8a")])
|
|
let result = try await service.build(snap)
|
|
XCTAssertEqual(result.binaryData[6], 0x8a)
|
|
XCTAssertTrue(result.instructions.isEmpty)
|
|
XCTAssertEqual(try String(contentsOf: root.appendingPathComponent("program.bin")), "stale")
|
|
XCTAssertEqual(try String(contentsOf: root.appendingPathComponent("Custom.mk")), makefile)
|
|
}
|
|
|
|
func testCancellingCustomRecipeLeavesNextBuildIndependent() async throws {
|
|
try write(first, "main.asm")
|
|
let marker = root.appendingPathComponent("started")
|
|
try write("all:\n\t@touch '" + marker.path + "'\n\t@sleep 60\n", "Makefile")
|
|
try write(#"{"makefile":"Makefile","binary":"program.bin"}"#, AssemblyProjectSnapshot.configurationFilename)
|
|
let project = try AssemblyProjectSnapshot.read(directory: root)
|
|
let buildService = service
|
|
let build = Task { try await buildService.build(project) }
|
|
defer { build.cancel() }
|
|
for _ in 0..<200 {
|
|
if FileManager.default.fileExists(atPath: marker.path) { break }
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
XCTAssertTrue(FileManager.default.fileExists(atPath: marker.path))
|
|
build.cancel()
|
|
do {
|
|
_ = try await build.value
|
|
XCTFail("A cancelled recipe returned an artifact.")
|
|
} catch is CancellationError {}
|
|
let fresh = try await buildService.build(.single(source: first))
|
|
XCTAssertEqual(fresh.instructions.count, 3)
|
|
XCTAssertFalse(FileManager.default.fileExists(atPath: root.appendingPathComponent("program.bin").path))
|
|
}
|
|
|
|
func testDiskExportContainsExactMachineLanguageEntry() async throws {
|
|
let artifact = try await service.build(.single(source: first))
|
|
let disk = try await service.diskImage(for: artifact, name: "first")
|
|
XCTAssertEqual(disk.count, 35 * 18 * 256)
|
|
let directory = 17 * 18 * 256 + 2 * 256
|
|
XCTAssertEqual(String(decoding: disk[directory..<(directory + 11)], as: UTF8.self), "FIRST BIN")
|
|
XCTAssertEqual(disk[directory + 11], 2) // Machine language
|
|
XCTAssertEqual(disk[directory + 12], 0) // Binary
|
|
let granule = Int(disk[directory + 13]), offset = (granule + (granule >= 34 ? 2 : 0)) * 9 * 256
|
|
let size = Int(disk[directory + 14]) * 256 + Int(disk[directory + 15])
|
|
XCTAssertEqual(size, artifact.binaryData.count)
|
|
XCTAssertEqual(disk.subdata(in: offset..<(offset + size)), artifact.binaryData)
|
|
}
|
|
|
|
@MainActor func testDiskBASICLoadMAndExecOnBothProcessors() async throws {
|
|
let roms = runtime.deletingLastPathComponent().appendingPathComponent("ROMs")
|
|
let system = roms.appendingPathComponent("coco3.rom"), diskROM = roms.appendingPathComponent("disk11.rom")
|
|
try XCTSkipUnless(FileManager.default.fileExists(atPath: system.path) && FileManager.default.fileExists(atPath: diskROM.path),
|
|
"Stage the licensed CoCo 3 and Disk BASIC ROMs to execute the exported disk.")
|
|
let rom = try Data(contentsOf: system), dos = try Data(contentsOf: diskROM)
|
|
let build = try await service.build(.single(source: first))
|
|
let disk = try await service.diskImage(for: build, name: "FIRST")
|
|
func command(_ body: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot {
|
|
await withCheckedContinuation { c in body { c.resume(returning: $0) } }
|
|
}
|
|
for cpu in [false, true] {
|
|
let controller = VCCMachineController()
|
|
do {
|
|
_ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) }
|
|
_ = await command { controller.loadDiskROM(dos, name: "Disk BASIC", completion: $0) }
|
|
_ = await command { controller.configureCartridges([2,0,0,0,2], roms: Array(repeating: Data(), count: 5),
|
|
names: Array(repeating: "", count: 5), multiPak: true, physicalSlot: 4, completion: $0) }
|
|
_ = await command { controller.mountDisk(disk, drive: 0, name: "FIRST.dsk", readOnly: true, completion: $0) }
|
|
_ = await command { controller.loadROM(rom, use6309: cpu, completion: $0) }
|
|
_ = await command(controller.resume)
|
|
var ready = false
|
|
for _ in 0..<250 {
|
|
let text: String? = await withCheckedContinuation { c in controller.copyScreenText { text, _ in c.resume(returning: text) } }
|
|
if text?.contains("OK") == true { ready = true; break }
|
|
try await Task.sleep(nanoseconds: 40_000_000)
|
|
}
|
|
XCTAssertTrue(ready)
|
|
let boot = await command(controller.snapshot)
|
|
for _ in 0..<100 {
|
|
if await command(controller.snapshot).frames >= boot.frames + 18 { break }
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
let paste = try CocoPaste("LOADM\"FIRST\":EXEC\n", mode: .text)
|
|
_ = await command { controller.pasteChords(paste.chords.map { NSNumber(value: $0) }, completion: $0) }
|
|
var finished = false
|
|
for _ in 0..<300 {
|
|
if await command(controller.snapshot).programCounter == 0x3f05 { finished = true; break }
|
|
try await Task.sleep(nanoseconds: 40_000_000)
|
|
}
|
|
XCTAssertTrue(finished, "The exported program should reach its hold loop after LOADM/EXEC.")
|
|
_ = await command(controller.pause)
|
|
let cell: VCCDebugMemoryCapture = await withCheckedContinuation { c in
|
|
controller.debuggerMemory(address: 0x400, count: 1, physical: false, logicalAddress: 0x400,
|
|
os9: false, decode: false) { c.resume(returning: $0) }
|
|
}
|
|
XCTAssertEqual(cell.bytes.first, 0x8f)
|
|
_ = await command(controller.shutdown)
|
|
} catch {
|
|
_ = await command(controller.shutdown)
|
|
throw error
|
|
}
|
|
}
|
|
}
|
|
}
|