114 lines
8.2 KiB
Swift
114 lines
8.2 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
|
|
final class DebuggerMMUBridgeTests: XCTestCase {
|
|
@MainActor func testBankedGuardBridgeFreezesModulesAndPreservesManualPoints() async {
|
|
let controller = VCCMachineController()
|
|
_ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) }
|
|
_ = await command(controller.loadDiagnostic)
|
|
_ = await command { controller.debuggerWriteMemory(address: 0x4000, data: Data([0x12,0x20,0xfe]), completion: $0) }
|
|
_ = await command { controller.debuggerSetPC(0x4000, completion: $0) }
|
|
_ = await command { controller.debuggerSetBreakpoints([0x4567], completion: $0) }
|
|
let initial = await capture(controller, includePhysical: false)
|
|
let physical = Int(initial.mmu.effective[2].sourceAddress / 8192)
|
|
let pages = [["logicalPage": 2, "physicalPage": physical]]
|
|
let mutableBytes = NSMutableData(data: Data([0x12]))
|
|
let module: [String: Any] = ["id": "resident", "pages": pages,
|
|
"addresses": [0x4000,0x4001], "bytes": [mutableBytes, NSData(data: Data([0x20,0xfe]))], "breakpoints": [0x4000]]
|
|
let armed = await command {
|
|
controller.debuggerArmBankedDevelopmentBreakpoints([0x4000], modules: [module], completion: $0)
|
|
mutableBytes.mutableBytes.storeBytes(of: UInt8(0x39), as: UInt8.self)
|
|
}
|
|
XCTAssertNil(armed.errorMessage)
|
|
XCTAssertTrue(armed.debuggerBankedDevelopment); XCTAssertTrue(armed.debuggerDevelopmentAttached)
|
|
XCTAssertEqual(armed.generation, initial.machine.generation)
|
|
XCTAssertEqual(armed.debuggerProgramRevision, initial.machine.debuggerProgramRevision)
|
|
let invalidPages: [[String: Any]] = [["logicalPage": 2.5, "physicalPage": physical]]
|
|
let invalid: [String: Any] = ["id": "invalid", "pages": invalidPages,
|
|
"addresses": [0x4000], "bytes": [Data([0x12])]]
|
|
let rejected = await command { controller.debuggerArmBankedDevelopmentBreakpoints([0x4000], modules: [invalid], completion: $0) }
|
|
XCTAssertNotNil(rejected.errorMessage); XCTAssertTrue(rejected.debuggerBankedDevelopment)
|
|
XCTAssertTrue(rejected.debuggerDevelopmentAttached, "Invalid replacements must retain the verified existing attachment")
|
|
for points: Any in [[0x4001], [0x4000,0x4000], [true], [16384.5], "4000"] {
|
|
var invalidQualification = module
|
|
invalidQualification["breakpoints"] = points
|
|
let definition = invalidQualification
|
|
let invalid = await command { controller.debuggerArmBankedDevelopmentBreakpoints([0x4000], modules: [definition], completion: $0) }
|
|
XCTAssertNotNil(invalid.errorMessage)
|
|
XCTAssertTrue(invalid.debuggerDevelopmentAttached, "Invalid point qualification preserves the current source connection")
|
|
}
|
|
let stale = await command {
|
|
controller.debuggerStepBankedSource(mode: 0, addresses: [0x4000,0x4001], locations: [1,2],
|
|
bytes: [Data([0x12]),Data([0x20,0xfe])], moduleIdentifiers: ["resident","resident"],
|
|
generation: armed.generation, programRevision: armed.debuggerProgramRevision,
|
|
mappingFingerprint: armed.debuggerMappingFingerprint &+ 1, programCounter: 0x4000, completion: $0)
|
|
}
|
|
XCTAssertNotNil(stale.errorMessage); XCTAssertEqual(stale.state, .paused)
|
|
let cleared = await command(controller.debuggerClearDevelopmentBreakpoints)
|
|
XCTAssertFalse(cleared.debuggerBankedDevelopment); XCTAssertFalse(cleared.debuggerDevelopmentAttached)
|
|
let capture = await capture(controller, includePhysical: false)
|
|
XCTAssertEqual(capture.breakpoints.map(\.uint16Value), [0x4567])
|
|
_ = await command(controller.shutdown)
|
|
}
|
|
@MainActor private func command(_ operation: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot {
|
|
await withCheckedContinuation { continuation in operation { continuation.resume(returning: $0) } }
|
|
}
|
|
@MainActor private func capture(_ controller: VCCMachineController, physical: UInt32 = 0,
|
|
includePhysical: Bool = true) async -> VCCDebuggerSnapshot {
|
|
await withCheckedContinuation { continuation in
|
|
controller.debuggerSnapshot(address: 0x8000, followPC: false, memoryAddress: 0x8000,
|
|
physicalAddress: physical, physicalMemory: includePhysical) {
|
|
continuation.resume(returning: $0)
|
|
}
|
|
}
|
|
}
|
|
@MainActor func testPhysicalCaptureMatchesMappedRAMWithoutROMOverlayForAllCapacities() async {
|
|
for use6309 in [false, true] {
|
|
for (ram, capacity) in [0x20000, 0x80000, 0x200000, 0x800000].enumerated() {
|
|
let controller = VCCMachineController()
|
|
_ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) }
|
|
_ = await command { controller.configureMachine(use6309: use6309, ram: ram, overclock: 1, frameInterval: 1, fastForward: false, completion: $0) }
|
|
let loaded = await command { controller.loadDiagnostic(use6309: use6309, completion: $0) }
|
|
let before = await capture(controller, includePhysical: false)
|
|
XCTAssertEqual(before.mmu.ramByteCount, UInt32(capacity))
|
|
XCTAssertEqual(before.mmu.task0.count, 8); XCTAssertEqual(before.mmu.task1.count, 8)
|
|
XCTAssertEqual(before.mmu.effective.count, 8); XCTAssertFalse(before.mmu.enabled)
|
|
XCTAssertTrue(before.physicalMemory.isEmpty)
|
|
XCTAssertEqual(before.memory[0], 0x7c, "CPU $8000 is the diagnostic ROM")
|
|
XCTAssertEqual(before.mmu.effective[4].source, 1)
|
|
let physicalROM = await capture(controller, physical: before.mmu.effective[4].ramAddress)
|
|
XCTAssertEqual(physicalROM.physicalMemory[0], 0, "Physical RAM beneath the ROM is not the ROM image")
|
|
_ = await command { controller.debuggerWriteMemory(address: 0x4000, data: Data([0x5a]), completion: $0) }
|
|
let physicalRAM = await capture(controller, physical: before.mmu.effective[2].ramAddress)
|
|
XCTAssertEqual(physicalRAM.physicalMemory[0], 0x5a)
|
|
XCTAssertEqual(physicalRAM.machine.cycles, loaded.cycles)
|
|
XCTAssertEqual(physicalRAM.machine.generation, loaded.generation)
|
|
XCTAssertEqual(physicalRAM.machine.state, .paused)
|
|
XCTAssertEqual(physicalROM.physicalMemory[0], 0, "Owned captures don't change after later writes")
|
|
_ = await command(controller.shutdown)
|
|
}
|
|
}
|
|
}
|
|
@MainActor func testPhysicalEndAndRAMResizeRecoverCursorWithoutChangingSession() async {
|
|
let controller = VCCMachineController()
|
|
_ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) }
|
|
_ = await command { controller.configureMachine(use6309: false, ram: 3, overclock: 1, frameInterval: 1, fastForward: false, completion: $0) }
|
|
_ = await command(controller.loadDiagnostic)
|
|
let last = await capture(controller, physical: 0x7fffff)
|
|
XCTAssertEqual(last.physicalAddress, 0x7fffff); XCTAssertEqual(last.physicalMemory.count, 1)
|
|
let changed = await command { controller.configureMachine(use6309: false, ram: 0, overclock: 1, frameInterval: 1, fastForward: false, completion: $0) }
|
|
let recovered = await capture(controller, physical: 0x7fffff)
|
|
XCTAssertNil(recovered.machine.errorMessage)
|
|
XCTAssertEqual(recovered.physicalAddress, 0x1ff00); XCTAssertEqual(recovered.physicalMemory.count, 256)
|
|
XCTAssertEqual(recovered.machine.generation, changed.generation)
|
|
XCTAssertEqual(recovered.mmu.ramByteCount, 0x20000)
|
|
_ = await command(controller.resume)
|
|
let running = await capture(controller)
|
|
XCTAssertEqual(running.machine.state, .running); XCTAssertEqual(running.machine.generation, changed.generation)
|
|
_ = await command(controller.shutdown)
|
|
let closed = await capture(controller)
|
|
XCTAssertEqual(closed.machine.state, .closed); XCTAssertEqual(closed.mmu.ramByteCount, 0)
|
|
XCTAssertTrue(closed.physicalMemory.isEmpty); XCTAssertTrue(closed.mmu.effective.isEmpty)
|
|
}
|
|
}
|