2016-01-05 04:40:43 +00:00
|
|
|
//
|
|
|
|
// ElectronDocument.swift
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 03/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2016-01-14 03:38:59 +00:00
|
|
|
import AudioToolbox
|
2016-01-05 04:40:43 +00:00
|
|
|
|
|
|
|
class ElectronDocument: MachineDocument {
|
|
|
|
|
2016-02-13 03:30:06 +00:00
|
|
|
private var electron: CSElectron! = CSElectron()
|
2016-01-05 04:40:43 +00:00
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
self.intendedCyclesPerSecond = 2000000
|
2016-01-07 04:14:36 +00:00
|
|
|
|
|
|
|
if let osPath = NSBundle.mainBundle().pathForResource("os", ofType: "rom") {
|
|
|
|
electron.setOSROM(NSData(contentsOfFile: osPath)!)
|
|
|
|
}
|
|
|
|
if let basicPath = NSBundle.mainBundle().pathForResource("basic", ofType: "rom") {
|
|
|
|
electron.setBASICROM(NSData(contentsOfFile: basicPath)!)
|
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 01:34:22 +00:00
|
|
|
override func windowControllerDidLoadNib(aController: NSWindowController) {
|
|
|
|
super.windowControllerDidLoadNib(aController)
|
|
|
|
electron.view = openGLView
|
2016-01-15 01:33:22 +00:00
|
|
|
electron.audioQueue = self.audioQueue
|
2016-01-10 01:34:22 +00:00
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
|
|
|
|
override var windowNibName: String? {
|
|
|
|
return "ElectronDocument"
|
|
|
|
}
|
|
|
|
|
2016-01-18 21:46:41 +00:00
|
|
|
override func readFromURL(url: NSURL, ofType typeName: String) throws {
|
|
|
|
print(url)
|
|
|
|
print(typeName)
|
2016-01-20 03:05:34 +00:00
|
|
|
|
|
|
|
if let pathExtension = url.pathExtension {
|
|
|
|
switch pathExtension.lowercaseString {
|
|
|
|
case "uef":
|
|
|
|
electron.openUEFAtURL(url)
|
|
|
|
return
|
|
|
|
default: break;
|
|
|
|
}
|
2016-01-18 21:46:41 +00:00
|
|
|
}
|
2016-01-20 03:05:34 +00:00
|
|
|
|
|
|
|
let fileWrapper = try NSFileWrapper(URL: url, options: NSFileWrapperReadingOptions(rawValue: 0))
|
|
|
|
try self.readFromFileWrapper(fileWrapper, ofType: typeName)
|
2016-01-18 21:46:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
override func readFromData(data: NSData, ofType typeName: String) throws {
|
2016-01-13 03:34:26 +00:00
|
|
|
electron.setROM(data, slot: 15)
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 03:30:06 +00:00
|
|
|
override func close() {
|
|
|
|
objc_sync_enter(self)
|
|
|
|
electron.sync()
|
|
|
|
openGLView.invalidate()
|
|
|
|
openGLView.openGLContext!.makeCurrentContext()
|
|
|
|
electron = nil
|
|
|
|
super.close()
|
|
|
|
objc_sync_exit(self)
|
|
|
|
}
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
// MARK: CSOpenGLViewDelegate
|
|
|
|
override func runForNumberOfCycles(numberOfCycles: Int32) {
|
2016-02-13 03:30:06 +00:00
|
|
|
objc_sync_enter(self)
|
|
|
|
electron?.runForNumberOfCycles(numberOfCycles)
|
|
|
|
objc_sync_exit(self)
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 04:05:47 +00:00
|
|
|
override func openGLView(view: CSCathodeRayView, drawViewOnlyIfDirty onlyIfDirty: Bool) {
|
|
|
|
electron.drawViewForPixelSize(view.backingSize, onlyIfDirty: onlyIfDirty)
|
2016-02-05 03:28:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
// MARK: CSOpenGLViewResponderDelegate
|
2016-01-12 00:48:31 +00:00
|
|
|
override func keyDown(event: NSEvent) {
|
|
|
|
electron.setKey(event.keyCode, isPressed: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func keyUp(event: NSEvent) {
|
|
|
|
electron.setKey(event.keyCode, isPressed: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func flagsChanged(newModifiers: NSEvent) {
|
|
|
|
electron.setKey(kVK_Shift, isPressed: newModifiers.modifierFlags.contains(.ShiftKeyMask))
|
|
|
|
electron.setKey(kVK_Control, isPressed: newModifiers.modifierFlags.contains(.ControlKeyMask))
|
|
|
|
electron.setKey(kVK_Command, isPressed: newModifiers.modifierFlags.contains(.CommandKeyMask))
|
|
|
|
}
|
2016-01-05 04:40:43 +00:00
|
|
|
}
|