2015-07-16 23:53:53 +00:00
|
|
|
//
|
2015-07-17 00:40:46 +00:00
|
|
|
// Atari2600Document.swift
|
2015-07-16 23:53:53 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/07/2015.
|
|
|
|
// Copyright © 2015 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
class Atari2600Document: MachineDocument {
|
|
|
|
|
|
|
|
// MARK: NSDocument overrides
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
self.intendedCyclesPerSecond = 1194720
|
|
|
|
}
|
2015-07-16 23:53:53 +00:00
|
|
|
|
|
|
|
override func windowControllerDidLoadNib(aController: NSWindowController) {
|
|
|
|
super.windowControllerDidLoadNib(aController)
|
2016-01-05 04:12:47 +00:00
|
|
|
atari2600.view = openGLView
|
2016-02-05 03:28:50 +00:00
|
|
|
// openGLView.frameBounds = CGRectMake(0.1, 0.1, 0.8, 0.8)
|
2015-07-16 23:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override class func autosavesInPlace() -> Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override var windowNibName: String? {
|
|
|
|
// Returns the nib file name of the document
|
|
|
|
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this property and override -makeWindowControllers instead.
|
|
|
|
return "Atari2600Document"
|
|
|
|
}
|
|
|
|
|
2016-01-05 04:44:36 +00:00
|
|
|
private var atari2600 = CSAtari2600()
|
2015-07-16 23:53:53 +00:00
|
|
|
override func dataOfType(typeName: String) throws -> NSData {
|
|
|
|
// Insert code here to write your document to data of the specified type. If outError != nil, ensure that you create and set an appropriate error when returning nil.
|
|
|
|
// You can also choose to override fileWrapperOfType:error:, writeToURL:ofType:error:, or writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
|
|
|
|
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
override func readFromData(data: NSData, ofType typeName: String) throws {
|
2016-01-05 04:12:47 +00:00
|
|
|
atari2600.setROM(data)
|
2015-07-16 23:53:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 01:15:10 +00:00
|
|
|
override func close() {
|
|
|
|
super.close()
|
2015-08-19 00:33:24 +00:00
|
|
|
openGLView.invalidate()
|
2015-07-28 01:15:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
// MARK: MachineDocument overrides
|
2015-07-19 14:54:19 +00:00
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
override func runForNumberOfCycles(numberOfCycles: Int32) {
|
|
|
|
atari2600.runForNumberOfCycles(numberOfCycles)
|
2015-07-17 02:14:40 +00:00
|
|
|
}
|
2015-08-19 00:33:24 +00:00
|
|
|
|
|
|
|
// MARK: CSOpenGLViewResponderDelegate
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
private func inputForKey(event: NSEvent) -> Atari2600DigitalInput? {
|
2015-08-19 00:33:24 +00:00
|
|
|
switch event.keyCode {
|
|
|
|
case 123: return Atari2600DigitalInputJoy1Left
|
|
|
|
case 126: return Atari2600DigitalInputJoy1Up
|
|
|
|
case 124: return Atari2600DigitalInputJoy1Right
|
|
|
|
case 125: return Atari2600DigitalInputJoy1Down
|
|
|
|
case 0: return Atari2600DigitalInputJoy1Fire
|
|
|
|
default: print("\(event.keyCode)"); return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
override func keyDown(event: NSEvent) {
|
|
|
|
super.keyDown(event)
|
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
if let input = inputForKey(event) {
|
2016-01-05 04:12:47 +00:00
|
|
|
atari2600.setState(true, forDigitalInput: input)
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
2015-08-19 00:58:05 +00:00
|
|
|
|
|
|
|
if event.keyCode == 36 {
|
2016-01-05 04:12:47 +00:00
|
|
|
atari2600.setResetLineEnabled(true)
|
2015-08-19 00:58:05 +00:00
|
|
|
}
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 04:40:43 +00:00
|
|
|
override func keyUp(event: NSEvent) {
|
|
|
|
super.keyUp(event)
|
|
|
|
|
2015-08-19 00:33:24 +00:00
|
|
|
if let input = inputForKey(event) {
|
2016-01-05 04:12:47 +00:00
|
|
|
atari2600.setState(false, forDigitalInput: input)
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
2015-08-19 00:58:05 +00:00
|
|
|
|
|
|
|
if event.keyCode == 36 {
|
2016-01-05 04:12:47 +00:00
|
|
|
atari2600.setResetLineEnabled(false)
|
2015-08-19 00:58:05 +00:00
|
|
|
}
|
2015-08-19 00:33:24 +00:00
|
|
|
}
|
2015-07-16 23:53:53 +00:00
|
|
|
}
|