From e236dcd7a6928d05d27225f1e32eeb1f91e4a734 Mon Sep 17 00:00:00 2001 From: Luigi Thirty Date: Tue, 8 Aug 2017 03:00:16 -0400 Subject: [PATCH] working on prodos block support --- FruitMachine/AppleII/AppleIIBase.swift | 4 +- .../Peripherals/DiskII/DiskImage.swift | 88 +++++++++++++++++-- FruitMachine/M6502/CPU.swift | 3 +- 3 files changed, 87 insertions(+), 8 deletions(-) diff --git a/FruitMachine/AppleII/AppleIIBase.swift b/FruitMachine/AppleII/AppleIIBase.swift index 3e96f8d..c3ca3db 100644 --- a/FruitMachine/AppleII/AppleIIBase.swift +++ b/FruitMachine/AppleII/AppleIIBase.swift @@ -108,9 +108,9 @@ class AppleIIBase: NSObject, EmulatedSystem { let slot6 = defaults.string(forKey: "a2_Peripherals_Slot6") if(slot6 == "Disk II") { backplane[6] = DiskII(slot: 6, romPath: "/Users/luigi/apple2/341-0027-a.p5") - - let drive = backplane[6]! as! DiskII } + + (backplane[6] as! DiskII).attachDiskImage(imagePath: "/Users/luigi/apple2/Prodos_2_4_1.po") } func doColdReset() { diff --git a/FruitMachine/AppleII/Peripherals/DiskII/DiskImage.swift b/FruitMachine/AppleII/Peripherals/DiskII/DiskImage.swift index 68e8bd3..5e8f1a1 100644 --- a/FruitMachine/AppleII/Peripherals/DiskII/DiskImage.swift +++ b/FruitMachine/AppleII/Peripherals/DiskII/DiskImage.swift @@ -38,10 +38,10 @@ class Dos33Image: DiskImageFormat { //Find the track in our disk. let trackOffset = trackNum * Dos33Image.BYTES_PER_TRACK //Find the sector in this track. - let sectorOffset = SECTOR_ORDER[sectorNum] * Dos33Image.BYTES_PER_SECTOR + let sectorOffset = SECTOR_ORDER[sectorNum] * BYTES_PER_SECTOR let offset = trackOffset + sectorOffset - return Array(imageData[offset ..< offset + Dos33Image.BYTES_PER_SECTOR]) + return Array(imageData[offset ..< offset + BYTES_PER_SECTOR]) } } @@ -63,6 +63,55 @@ class ProdosImage: DiskImageFormat { return Array(imageData[offset ..< offset + BYTES_PER_SECTOR]) } + + static func readBlock(imageData: [UInt8], blockNum: Int) -> [UInt8] { + var blockData = [UInt8]() + + /* Find the track number. */ + let track = blockNum / 8 + + /* Find the sector numbers. */ + let blockOffset8 = blockNum % 8 + var sector1 = 0 + var sector2 = 0 + + switch blockOffset8 { + case 0: + sector1 = 0 + sector2 = 2 + case 1: + sector1 = 4 + sector2 = 6 + case 2: + sector1 = 8 + sector2 = 0xA + case 3: + sector1 = 0xC + sector2 = 0xE + + case 4: + sector1 = 1 + sector2 = 3 + case 5: + sector1 = 5 + sector2 = 7 + case 6: + sector1 = 9 + sector2 = 0xB + case 7: + sector1 = 0xD + sector2 = 0xF + default: + print("should never happen") + } + + /* First half of the interleaved block. */ + blockData.append(contentsOf: readTrackAndSector(imageData: imageData, trackNum: track, sectorNum: sector1)) + /* Second half of the interleaved block. */ + blockData.append(contentsOf: readTrackAndSector(imageData: imageData, trackNum: track, sectorNum: sector2)) + + return blockData + } } class DiskImage: NSObject { @@ -105,9 +154,29 @@ class DiskImage: NSObject { image = ProdosImage() for track in 0.. [UInt8]? { @@ -192,7 +261,16 @@ class DiskImage: NSObject { //Data Field encodedData.append(contentsOf: dataPrologue) //343 bytes: 342-byte sector + 1-byte checksum - encodedData.append(contentsOf: EncodeSectorSixAndTwo(sector: Dos33Image.readTrackAndSector(imageData: imageData, trackNum: index, sectorNum: sectorNum))) + if(image is Dos33Image) { + encodedData.append(contentsOf: EncodeSectorSixAndTwo(sector: Dos33Image.readTrackAndSector(imageData: imageData, trackNum: index, sectorNum: sectorNum))) + } + else if(image is ProdosImage){ + /* TODO: A .PO image is stored by 512-blocks which are not contiguous on the disk. Need to adapt this to handle blocks. */ + /* + encodedData.append(contentsOf: EncodeSectorSixAndTwo(sector: ProdosImage.readTrackAndSector(imageData: imageData, trackNum: index, sectorNum: sectorNum))) + */ + } + encodedData.append(contentsOf: dataEpilogue) //Gap2 - 20 bytes diff --git a/FruitMachine/M6502/CPU.swift b/FruitMachine/M6502/CPU.swift index b5c03e8..600cbb9 100644 --- a/FruitMachine/M6502/CPU.swift +++ b/FruitMachine/M6502/CPU.swift @@ -283,7 +283,8 @@ final class CPU: NSObject { do { try executeNextInstruction() } catch CPUExceptions.invalidInstruction { - isRunning = false + print("Invalid instruction at \(program_counter.asHexString())") + coldReset() } catch { print(error) }