From acdefbb4fb847000853d2bee15dae4855d46d902 Mon Sep 17 00:00:00 2001 From: Luigi Thirty Date: Mon, 31 Jul 2017 16:33:42 -0400 Subject: [PATCH] change from ARGB32 to BE555 --- FruitMachine/AppleI/AppleI.swift | 6 ++-- FruitMachine/AppleI/AppleIBitmapDisplay.swift | 28 ++++++++++--------- FruitMachine/AppleI/Video/BitmapPixels.swift | 13 +++++++++ FruitMachine/FruitMachine.storyboard | 6 +++- FruitMachine/MainViewController.swift | 7 +++-- 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/FruitMachine/AppleI/AppleI.swift b/FruitMachine/AppleI/AppleI.swift index 5d3cde3..5366df3 100644 --- a/FruitMachine/AppleI/AppleI.swift +++ b/FruitMachine/AppleI/AppleI.swift @@ -63,6 +63,8 @@ class AppleI: NSObject { } func runFrame() { + let startTime = CFAbsoluteTimeGetCurrent() + CPU.sharedInstance.cycles = 0 CPU.sharedInstance.cyclesInBatch = AppleI.CYCLES_PER_BATCH CPU.sharedInstance.runCyclesBatch() @@ -76,7 +78,7 @@ class AppleI: NSObject { emulatorView.setNeedsDisplay(emulatorView.frame) emulatorView.display() - //emuGLView.setNeedsDisplay(emuGLView.frame) - //emuGLView.display() + let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime + print("Time elapsed for runFrame: \(timeElapsed) s.") } } diff --git a/FruitMachine/AppleI/AppleIBitmapDisplay.swift b/FruitMachine/AppleI/AppleIBitmapDisplay.swift index 1f8695f..6f73949 100644 --- a/FruitMachine/AppleI/AppleIBitmapDisplay.swift +++ b/FruitMachine/AppleI/AppleIBitmapDisplay.swift @@ -13,7 +13,7 @@ class AppleIBitmapDisplay: NSObject, CALayerDelegate { static let PIXEL_HEIGHT = 192 /* Pixel data stuff. */ - let bitmapInfo: CGBitmapInfo = [.byteOrder32Big, CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipFirst.rawValue)] + let bitmapInfo: CGBitmapInfo = [.byteOrder16Big, CGBitmapInfo(rawValue: CGImageAlphaInfo.noneSkipFirst.rawValue)] var pixels: CVPixelBuffer? @@ -21,18 +21,22 @@ class AppleIBitmapDisplay: NSObject, CALayerDelegate { let bufferWidth: Int let bufferHeight: Int + var renderedImage: CGImage? + override init() { - _ = CVPixelBufferCreate(kCFAllocatorDefault, AppleIBitmapDisplay.PIXEL_WIDTH, AppleIBitmapDisplay.PIXEL_HEIGHT, OSType(k32ARGBPixelFormat), nil, &pixels) + _ = CVPixelBufferCreate(kCFAllocatorDefault, AppleIBitmapDisplay.PIXEL_WIDTH, AppleIBitmapDisplay.PIXEL_HEIGHT, OSType(k16BE555PixelFormat), nil, &pixels) sourceRowBytes = CVPixelBufferGetBytesPerRow(pixels!) bufferWidth = CVPixelBufferGetWidth(pixels!) bufferHeight = CVPixelBufferGetHeight(pixels!) + + renderedImage = nil } func putCharacterPixels(charPixels: [UInt8], pixelPosition: CGPoint) { CVPixelBufferLockBaseAddress(pixels!, CVPixelBufferLockFlags(rawValue: 0)) let pixelBase = CVPixelBufferGetBaseAddress(pixels!) - let buf = pixelBase?.assumingMemoryBound(to: BitmapPixelsARGB32.PixelData.self) + let buf = pixelBase?.assumingMemoryBound(to: BitmapPixelsBE555.PixelData.self) //Calculate the offset to reach the desired position. let baseOffset = (Int(pixelPosition.y) * AppleIBitmapDisplay.PIXEL_WIDTH) + Int(pixelPosition.x) @@ -41,7 +45,7 @@ class AppleIBitmapDisplay: NSObject, CALayerDelegate { let offsetY = AppleIBitmapDisplay.PIXEL_WIDTH * charY for charX in 0..<8 { - buf![baseOffset + offsetY + 7 - charX] = (charPixels[charY] & UInt8(1 << charX)) > 0 ? BitmapPixelsARGB32.ARGBWhite : BitmapPixelsARGB32.ARGBBlack + buf![baseOffset + offsetY + 7 - charX] = (charPixels[charY] & UInt8(1 << charX)) > 0 ? BitmapPixelsBE555.ARGBWhite : BitmapPixelsBE555.ARGBBlack } } @@ -58,25 +62,23 @@ class AppleIBitmapDisplay: NSObject, CALayerDelegate { /* Draw the screen. */ func draw(_ layer: CALayer, in ctx: CGContext) { - let bounds = layer.bounds - CVPixelBufferLockBaseAddress(pixels!, CVPixelBufferLockFlags.readOnly) let pixelBase = CVPixelBufferGetBaseAddress(pixels!) let pixelRef = CGDataProvider(dataInfo: nil, data: pixelBase!, size: sourceRowBytes * bufferHeight, releaseData: releaseMaskImagePixelData) - let renderedImage = CGImage(width: AppleIBitmapDisplay.PIXEL_WIDTH, + renderedImage = CGImage(width: AppleIBitmapDisplay.PIXEL_WIDTH, height: AppleIBitmapDisplay.PIXEL_HEIGHT, - bitsPerComponent: Int(BitmapPixelsARGB32.bitsPerComponent), //8 - bitsPerPixel: Int(BitmapPixelsARGB32.bitsPerPixel), //32 - bytesPerRow: AppleIBitmapDisplay.PIXEL_WIDTH * Int(MemoryLayout.size), - space: BitmapPixelsARGB32.colorSpace, //RGB - bitmapInfo: bitmapInfo, //ARGB32 + bitsPerComponent: Int(BitmapPixelsBE555.bitsPerComponent), //5 + bitsPerPixel: Int(BitmapPixelsBE555.bitsPerPixel), //16 + bytesPerRow: AppleIBitmapDisplay.PIXEL_WIDTH * Int(MemoryLayout.size), + space: BitmapPixelsBE555.colorSpace, //RGB + bitmapInfo: bitmapInfo, //BE555 provider: pixelRef!, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent) - ctx.draw(renderedImage!, in: bounds) + ctx.draw(renderedImage!, in: layer.bounds) CVPixelBufferUnlockBaseAddress(pixels!, CVPixelBufferLockFlags(rawValue: 0)) } diff --git a/FruitMachine/AppleI/Video/BitmapPixels.swift b/FruitMachine/AppleI/Video/BitmapPixels.swift index e532772..8f4ea50 100644 --- a/FruitMachine/AppleI/Video/BitmapPixels.swift +++ b/FruitMachine/AppleI/Video/BitmapPixels.swift @@ -37,3 +37,16 @@ class BitmapPixelsARGB32 : NSObject { static let ARGBWhite = PixelData(a: 255, r: 200, g: 200, b: 200) static let ARGBBlack = PixelData(a: 255, r: 0, g: 0, b: 0) } + +class BitmapPixelsBE555 : NSObject { + struct PixelData { + var data: UInt16 = 0 + } + + static let bitsPerComponent: UInt8 = 5 + static let bitsPerPixel: UInt = 16 + static let colorSpace = CGColorSpaceCreateDeviceRGB() + + static let ARGBWhite = PixelData(data: 0b1111111101111111) + static let ARGBBlack = PixelData(data: 0b0000000000000000) +} diff --git a/FruitMachine/FruitMachine.storyboard b/FruitMachine/FruitMachine.storyboard index ba8a136..7b6d113 100644 --- a/FruitMachine/FruitMachine.storyboard +++ b/FruitMachine/FruitMachine.storyboard @@ -23,7 +23,11 @@ - + + + + + diff --git a/FruitMachine/MainViewController.swift b/FruitMachine/MainViewController.swift index ac182c9..55159fb 100644 --- a/FruitMachine/MainViewController.swift +++ b/FruitMachine/MainViewController.swift @@ -8,8 +8,6 @@ import Cocoa import CoreGraphics -import OpenGL -import GLKit class MainViewController: NSViewController { @@ -24,7 +22,6 @@ class MainViewController: NSViewController { super.viewDidLoad() preferencesWindowController = PreferencesWindowController() - preferencesWindowController.loadWindow() // Do view setup here. self.view.addSubview(computer.emulatorView) @@ -67,6 +64,10 @@ class MainViewController: NSViewController { debuggerWindowController = debuggerStoryboard.instantiateInitialController() as! DebuggerWindowController debuggerWindowController.showWindow(self) } + + @IBAction func showPreferences(_ sender: Any) { + preferencesWindowController.loadWindow() + } } extension Character {