From 76e880161106f8b7725dc6fc580752b5c280cf65 Mon Sep 17 00:00:00 2001 From: Luigi Thirty Date: Sat, 29 Jul 2017 16:11:14 -0400 Subject: [PATCH] the terminal works --- FruitMachine/AppleI/AppleI.swift | 10 +++++++- FruitMachine/AppleI/AppleScreenView.swift | 15 +++++++++++ FruitMachine/AppleI/PIA.swift | 13 ++++++++++ FruitMachine/AppleI/Video/PIAOverrides.swift | 27 +++++++++++++------- FruitMachine/MainViewController.swift | 24 ++++++++++++----- 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/FruitMachine/AppleI/AppleI.swift b/FruitMachine/AppleI/AppleI.swift index d062221..9ce61cb 100644 --- a/FruitMachine/AppleI/AppleI.swift +++ b/FruitMachine/AppleI/AppleI.swift @@ -33,9 +33,13 @@ class AppleI: NSObject { override init() { super.init() - emulatorView.wantsLayer = true + emuScreenLayer.shouldRasterize = true emuScreenLayer.delegate = emulatorViewDelegate emuScreenLayer.frame = emulatorView.bounds + + //emulatorView.layer = emuScreenLayer + emulatorView.wantsLayer = true + emuScreenLayer.setNeedsDisplay() emulatorView.layer?.addSublayer(emuScreenLayer) @@ -54,8 +58,11 @@ class AppleI: NSObject { CPU.sharedInstance.memoryInterface.write_overrides.append(PIAOverrides.writeDSP) CPU.sharedInstance.memoryInterface.read_overrides.append(PIAOverrides.readDSP) + CPU.sharedInstance.memoryInterface.write_overrides.append(PIAOverrides.writeDSPCR) + CPU.sharedInstance.memoryInterface.read_overrides.append(PIAOverrides.readKBD) CPU.sharedInstance.memoryInterface.read_overrides.append(PIAOverrides.readKBDCR) + } func runFrame() { @@ -68,6 +75,7 @@ class AppleI: NSObject { emulatorViewDelegate.putCharacterPixels(charPixels: cg.getCharacterPixels(charIndex: character), pixelPosition: emulatorViewDelegate.getPixelOffset(charCellIndex: cellNum)) } + emulatorView.setNeedsDisplay(emulatorView.frame) emulatorView.display() } } diff --git a/FruitMachine/AppleI/AppleScreenView.swift b/FruitMachine/AppleI/AppleScreenView.swift index 002aab1..3b74d1b 100644 --- a/FruitMachine/AppleI/AppleScreenView.swift +++ b/FruitMachine/AppleI/AppleScreenView.swift @@ -10,9 +10,24 @@ import Cocoa class AppleScreenView: NSView { + override var acceptsFirstResponder: Bool { + return true + } + + override func becomeFirstResponder() -> Bool { + return true + } + + override func resignFirstResponder() -> Bool { + return true + } + override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. + + layer?.sublayers![0].setNeedsDisplay() + layer?.sublayers![0].display() } } diff --git a/FruitMachine/AppleI/PIA.swift b/FruitMachine/AppleI/PIA.swift index 5139edc..c4e35ae 100644 --- a/FruitMachine/AppleI/PIA.swift +++ b/FruitMachine/AppleI/PIA.swift @@ -9,6 +9,11 @@ import Cocoa class PIA: NSObject { + enum PIAMode { + case DDR + case Output + } + var data: UInt8 var control: UInt8 @@ -16,5 +21,13 @@ class PIA: NSObject { data = 0x00 control = 0x00 } + + func getMode() -> PIAMode { + if((control & 0x04) == 0x04) { + return .Output + } else { + return .DDR + } + } } diff --git a/FruitMachine/AppleI/Video/PIAOverrides.swift b/FruitMachine/AppleI/Video/PIAOverrides.swift index 114c609..8c0356a 100644 --- a/FruitMachine/AppleI/Video/PIAOverrides.swift +++ b/FruitMachine/AppleI/Video/PIAOverrides.swift @@ -11,16 +11,25 @@ import Cocoa class PIAOverrides: NSObject { static let writeDSP = WriteOverride(start: 0xD012, end: 0xD012, writeAnyway: false, action: PIAOverrides.actionWriteDSP) static func actionWriteDSP(terminal: AnyObject, byte: UInt8?) -> UInt8? { - //TODO: implement actual 6520 PIA behavior + + let pia = AppleI.sharedInstance.pia["display"]! - //Writing to DSP sets DSP.7 - AppleI.sharedInstance.pia["display"]!.data = byte! | 0x80 - - //Output our character to the terminal - AppleI.sharedInstance.terminal.putCharacter(charIndex: byte!) - - AppleI.sharedInstance.pia["display"]!.data = byte! & ~(0x80) - return nil; + if(pia.getMode() == .Output) { + //Writing to DSP sets DSP.7 + AppleI.sharedInstance.pia["display"]!.data = byte! | 0x80 + + //Output our character to the terminal + AppleI.sharedInstance.terminal.putCharacter(charIndex: byte!) + + AppleI.sharedInstance.pia["display"]!.data = byte! & ~(0x80) + } + return nil + } + + static let writeDSPCR = WriteOverride(start: 0xD013, end: 0xD013, writeAnyway: false, action: PIAOverrides.actionWriteDSPCR) + static func actionWriteDSPCR(terminal: AnyObject, byte: UInt8?) -> UInt8? { + AppleI.sharedInstance.pia["display"]?.control = byte! + return nil } static let readDSP = ReadOverride(start: 0xD012, end: 0xD012, readAnyway: false, action: PIAOverrides.actionReadDSP) diff --git a/FruitMachine/MainViewController.swift b/FruitMachine/MainViewController.swift index ed8a382..ccf1976 100644 --- a/FruitMachine/MainViewController.swift +++ b/FruitMachine/MainViewController.swift @@ -26,24 +26,34 @@ class MainViewController: NSViewController { // Do view setup here. self.view.addSubview(computer.emulatorView) - computer.emulatorView.display() self.frameTimer = Timer.scheduledTimer(timeInterval: 1/60, target: self, selector: #selector(runEmulation), userInfo: nil, repeats: true) //runEmulation() } @objc func runEmulation() { - AppleI.sharedInstance.runFrame() - computer.emulatorView.setNeedsDisplay(computer.emulatorView.frame) - computer.emulatorView.layer!.setNeedsDisplay(computer.emulatorView.layer!.frame) - computer.emulatorView.display() + computer.runFrame() } override func keyDown(with event: NSEvent) { - let character = event.characters?.first + let c = returnChar(theEvent: event) - computer.pia["keyboard"]?.data = 0x41 + computer.pia["keyboard"]?.data = UInt8((c?.asciiValue)! & 0x000000FF) computer.pia["keyboard"]?.control |= 0x80 } + + private func returnChar(theEvent: NSEvent) -> Character?{ + let s: String = theEvent.characters! + for char in s{ + return char + } + return nil + } } + +extension Character { + var asciiValue: UInt32? { + return String(self).unicodeScalars.filter{$0.isASCII}.first?.value + } +}