diff --git a/Mini vMac.xcodeproj/project.pbxproj b/Mini vMac.xcodeproj/project.pbxproj index d8a7a0a..b2fbcc2 100644 --- a/Mini vMac.xcodeproj/project.pbxproj +++ b/Mini vMac.xcodeproj/project.pbxproj @@ -8,8 +8,7 @@ /* Begin PBXBuildFile section */ 283422D21CF8EF8C0088B634 /* MacPlus4M.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 283422CA1CF8EF8C0088B634 /* MacPlus4M.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - 283422EE1CF8F2210088B634 /* MYOSGLUE.m in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; - 283422F41CF8F55E0088B634 /* Emulator.m in Sources */ = {isa = PBXBuildFile; fileRef = 283422F21CF8F5400088B634 /* Emulator.m */; }; + 283422EE1CF8F2210088B634 /* MYOSGLUE.m in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */; }; 283423601CF9C1050088B634 /* GLOBGLUE.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423341CF9C0F10088B634 /* GLOBGLUE.c */; }; 283423611CF9C10B0088B634 /* IWMEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423371CF9C0F10088B634 /* IWMEMDEV.c */; }; 283423631CF9C1320088B634 /* KBRDEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423391CF9C0F10088B634 /* KBRDEMDV.c */; }; @@ -25,8 +24,7 @@ 2834236D1CF9C1320088B634 /* SNDEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423531CF9C0F10088B634 /* SNDEMDEV.c */; }; 2834236E1CF9C1320088B634 /* SONYEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423551CF9C0F10088B634 /* SONYEMDV.c */; }; 2834236F1CF9C1320088B634 /* VIAEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 2834235B1CF9C0F10088B634 /* VIAEMDEV.c */; }; - 283423861CF9C4CB0088B634 /* Emulator.m in Sources */ = {isa = PBXBuildFile; fileRef = 283422F21CF8F5400088B634 /* Emulator.m */; }; - 283423881CF9C4CB0088B634 /* MYOSGLUE.m in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 283423881CF9C4CB0088B634 /* MYOSGLUE.m in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */; }; 283423A31CF9C5880088B634 /* MacII.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 283423961CF9C4CB0088B634 /* MacII.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 283423D21CF9C66D0088B634 /* ADBEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423231CF9C0F10088B634 /* ADBEMDEV.c */; }; 283423D31CF9C66D0088B634 /* ASCEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423271CF9C0F10088B634 /* ASCEMDEV.c */; }; @@ -60,9 +58,8 @@ 289710BE1CFB11BF0089D463 /* SCSIEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423511CF9C0F10088B634 /* SCSIEMDV.c */; }; 289710BF1CFB11BF0089D463 /* SCCEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 2834234A1CF9C0F10088B634 /* SCCEMDEV.c */; }; 289710C01CFB11BF0089D463 /* SCRNEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 2834234C1CF9C0F10088B634 /* SCRNEMDV.c */; }; - 289710C11CFB11BF0089D463 /* Emulator.m in Sources */ = {isa = PBXBuildFile; fileRef = 283422F21CF8F5400088B634 /* Emulator.m */; }; 289710C21CFB11BF0089D463 /* IWMEMDEV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423371CF9C0F10088B634 /* IWMEMDEV.c */; }; - 289710C31CFB11BF0089D463 /* MYOSGLUE.m in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + 289710C31CFB11BF0089D463 /* MYOSGLUE.m in Sources */ = {isa = PBXBuildFile; fileRef = 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */; }; 289710C41CFB11BF0089D463 /* MOUSEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 2834233F1CF9C0F10088B634 /* MOUSEMDV.c */; }; 289710C51CFB11BF0089D463 /* GLOBGLUE.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423341CF9C0F10088B634 /* GLOBGLUE.c */; }; 289710C61CFB11BF0089D463 /* KBRDEMDV.c in Sources */ = {isa = PBXBuildFile; fileRef = 283423391CF9C0F10088B634 /* KBRDEMDV.c */; }; @@ -185,8 +182,6 @@ 283422D71CF8EFD80088B634 /* EmulatorProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EmulatorProtocol.h; sourceTree = ""; }; 283422E71CF8F1C80088B634 /* CNFGGLOB.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CNFGGLOB.h; sourceTree = ""; }; 283422E91CF8F1C80088B634 /* EMCONFIG.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EMCONFIG.h; sourceTree = ""; }; - 283422F11CF8F5400088B634 /* Emulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Emulator.h; sourceTree = ""; }; - 283422F21CF8F5400088B634 /* Emulator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Emulator.m; sourceTree = ""; }; 283423221CF9C0F10088B634 /* ACTVCODE.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = ACTVCODE.h; sourceTree = ""; }; 283423231CF9C0F10088B634 /* ADBEMDEV.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; lineEnding = 0; path = ADBEMDEV.c; sourceTree = ""; }; 283423241CF9C0F10088B634 /* ADBEMDEV.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = ADBEMDEV.h; sourceTree = ""; }; @@ -426,8 +421,6 @@ children = ( 283423211CF9C0F10088B634 /* mnvm_core */, 283422D71CF8EFD80088B634 /* EmulatorProtocol.h */, - 283422F11CF8F5400088B634 /* Emulator.h */, - 283422F21CF8F5400088B634 /* Emulator.m */, 28CE8ECB1CD4CDC500FE25A8 /* MYOSGLUE.m */, 289710B71CFB0DC70089D463 /* CNFGGLOB.h */, ); @@ -974,7 +967,6 @@ 2834236C1CF9C1320088B634 /* SCSIEMDV.c in Sources */, 2834236A1CF9C1320088B634 /* SCCEMDEV.c in Sources */, 2834236B1CF9C1320088B634 /* SCRNEMDV.c in Sources */, - 283422F41CF8F55E0088B634 /* Emulator.m in Sources */, 283423611CF9C10B0088B634 /* IWMEMDEV.c in Sources */, 283422EE1CF8F2210088B634 /* MYOSGLUE.m in Sources */, 283423661CF9C1320088B634 /* MOUSEMDV.c in Sources */, @@ -993,7 +985,6 @@ files = ( 283423DF1CF9C66D0088B634 /* SONYEMDV.c in Sources */, 283423DE1CF9C66D0088B634 /* SCSIEMDV.c in Sources */, - 283423861CF9C4CB0088B634 /* Emulator.m in Sources */, 283423DD1CF9C66D0088B634 /* SCRNEMDV.c in Sources */, 283423DB1CF9C66D0088B634 /* RTCEMDEV.c in Sources */, 283423881CF9C4CB0088B634 /* MYOSGLUE.m in Sources */, @@ -1024,7 +1015,6 @@ 289710BE1CFB11BF0089D463 /* SCSIEMDV.c in Sources */, 289710BF1CFB11BF0089D463 /* SCCEMDEV.c in Sources */, 289710C01CFB11BF0089D463 /* SCRNEMDV.c in Sources */, - 289710C11CFB11BF0089D463 /* Emulator.m in Sources */, 289710C21CFB11BF0089D463 /* IWMEMDEV.c in Sources */, 289710C31CFB11BF0089D463 /* MYOSGLUE.m in Sources */, 289710C41CFB11BF0089D463 /* MOUSEMDV.c in Sources */, diff --git a/Mini vMac/Emulator.h b/Mini vMac/Emulator.h deleted file mode 100644 index a4805a4..0000000 --- a/Mini vMac/Emulator.h +++ /dev/null @@ -1,16 +0,0 @@ -// -// Emulator.h -// Mini vMac -// -// Created by Jesús A. Álvarez on 27/05/2016. -// Copyright © 2016 namedfork. All rights reserved. -// - -#import -#import "EmulatorProtocol.h" - -@interface Emulator : NSObject - -- (void)updateScreen:(CGImageRef)screenImage; - -@end diff --git a/Mini vMac/Emulator.m b/Mini vMac/Emulator.m deleted file mode 100644 index 2b900dc..0000000 --- a/Mini vMac/Emulator.m +++ /dev/null @@ -1,157 +0,0 @@ -// -// Emulator.m -// Mini vMac -// -// Created by Jesús A. Álvarez on 27/05/2016. -// Copyright © 2016 namedfork. All rights reserved. -// - -#import "Emulator.h" -#include "SYSDEPNS.h" -#include "ENDIANAC.h" -#include "MYOSGLUE.h" - -IMPORTPROC RunEmulator(void); -IMPORTFUNC blnr GetSpeedStopped(void); -IMPORTPROC SetSpeedStopped(blnr stopped); -IMPORTPROC SetMouseButton(blnr down); -IMPORTPROC SetMouseLoc(ui4r h, ui4r v); -IMPORTPROC SetMouseDelta(ui4r dh, ui4r dv); -IMPORTFUNC blnr Sony_Insert1(NSString *filePath, blnr silentfail); -IMPORTFUNC blnr Sony_IsInserted(NSString *filePath); -IMPORTPROC SetKeyState(int key, blnr down); - -static Emulator *sharedEmulator = nil; -static dispatch_once_t onceToken; - -@implementation Emulator - -@synthesize dataPath; - -+ (instancetype)sharedEmulator { - dispatch_once(&onceToken, ^{ - sharedEmulator = [self new]; - }); - return sharedEmulator; -} - -- (instancetype)init { - if ((self = [super init])) { - dispatch_once(&onceToken, ^{ - sharedEmulator = self; - }); - } - return self; -} -- (void)run { - SpeedValue = [[NSUserDefaults standardUserDefaults] integerForKey:@"speedValue"]; - if (SpeedValue > 3) { - SpeedValue = 3; - } - RunEmulator(); -} - -- (NSInteger)initialSpeed { - return WantInitSpeedValue; -} - -- (NSBundle *)bundle { - return [NSBundle bundleForClass:self.class]; -} - -- (NSInteger)speed { - return SpeedValue; -} - -- (void)setSpeed:(NSInteger)speed { - SpeedValue = speed; -} - -- (BOOL)isRunning { - return !GetSpeedStopped(); -} - -- (void)setRunning:(BOOL)running { - SetSpeedStopped(running ? falseblnr : trueblnr); -} - -- (void)interrupt { - WantMacInterrupt = trueblnr; -} - -- (void)reset { - WantMacReset = trueblnr; -} - -#pragma mark - Screen - -@synthesize screenLayer; - -- (CGSize)screenSize { - return CGSizeMake(vMacScreenWidth, vMacScreenHeight); -} - -- (void)updateScreen:(CGImageRef)screenImage { - screenLayer.contents = (__bridge id)screenImage; -} - -#pragma mark - Disk - -@synthesize insertDiskNotification, ejectDiskNotification; - -- (BOOL)anyDiskInserted { - return AnyDiskInserted(); -} - -- (BOOL)isDiskInserted:(NSString *)path { - return Sony_IsInserted(path); -} - -- (BOOL)insertDisk:(NSString *)path { - return Sony_Insert1(path, false); -} - -- (NSString *)insertDiskNotification { - return @"didInsertDisk"; -} - -- (NSString *)ejectDiskNotification { - return @"didEjectDisk"; -} - -#pragma mark - Keyboard - -- (int)translateScanCode:(int)scancode { - switch (scancode) { - case 54: return 59; // left control - case 59: return 70; // arrow left - case 60: return 66; // arrow right - case 61: return 72; // arrow down - case 62: return 77; // arrow up - default: return scancode; - } -} - -- (void)keyDown:(int)scancode { - SetKeyState([self translateScanCode:scancode], true); -} - -- (void)keyUp:(int)scancode { - SetKeyState([self translateScanCode:scancode], false); -} - -#pragma mark - Mouse - -- (void)setMouseX:(NSInteger)x Y:(NSInteger)y { - SetMouseLoc(x, y); -} - -- (void)moveMouseX:(NSInteger)x Y:(NSInteger)y { - SetMouseDelta(x, y); -} - -- (void)setMouseButton:(BOOL)down { - SetMouseButton(down); -} - -@end diff --git a/Mini vMac/MYOSGLUE.m b/Mini vMac/MYOSGLUE.m index 7dc69a9..5fae777 100644 --- a/Mini vMac/MYOSGLUE.m +++ b/Mini vMac/MYOSGLUE.m @@ -31,7 +31,15 @@ #include "ENDIANAC.h" #include "MYOSGLUE.h" #include "STRCONST.h" -#import "Emulator.h" +#import "EmulatorProtocol.h" + +@interface Emulator : NSObject + +- (void)updateScreen:(CGImageRef)screenImage; + +@end + +static Emulator *sharedEmulator = nil; #pragma mark - some simple utilities @@ -59,7 +67,7 @@ LOCALFUNC blnr dbglog_open0(void) { #if dbglog_ToStdErr return trueblnr; #else - NSString *myLogPath = [MyDataPath stringByAppendingPathComponent:@"dbglog.txt"]; + NSString *myLogPath = [sharedEmulator.dataPath stringByAppendingPathComponent:@"dbglog.txt"]; const char *path = [myLogPath fileSystemRepresentation]; dbglog_File = fopen(path, "w"); @@ -152,24 +160,6 @@ LOCALPROC Screen_UnInit(void) { } } -LOCALVAR NSString *MyDataPath = nil; - -LOCALFUNC blnr InitCocoaStuff(void) { - MyDataPath = [Emulator sharedEmulator].dataPath; - if (MyDataPath) { - [MyDataPath retain]; - } - - Screen_Init(); - return trueblnr; -} - -LOCALPROC UnInitCocoaStuff(void) { - [MyDataPath release]; - MyDataPath = nil; - Screen_UnInit(); -} - #pragma mark - Parameter Buffers #if IncludePbufs @@ -349,15 +339,13 @@ LOCALFUNC tMacErr CopyBytesToPbuf(const char *x, ui5r L, tPbuf *r) { #if IncludeSonyGetName || IncludeHostTextClipExchange LOCALFUNC tMacErr NSStringToRomanPbuf(NSString *string, tPbuf *r) { tMacErr v = mnvm_miscErr; - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSData *d0 = [string dataUsingEncoding:NSMacOSRomanStringEncoding]; - const void *s = [d0 bytes]; - NSUInteger L = [d0 length]; - - v = CopyBytesToPbuf(s, (ui5r)L, r); - - [pool release]; - + @autoreleasepool { + NSData *d0 = [string dataUsingEncoding:NSMacOSRomanStringEncoding]; + const void *s = [d0 bytes]; + NSUInteger L = [d0 length]; + + v = CopyBytesToPbuf(s, (ui5r)L, r); + } return v; } #endif @@ -488,13 +476,8 @@ LOCALFUNC tMacErr vSonyEject0(tDrive Drive_No, blnr deleteit) { NSString *filePath = DriveNames[Drive_No]; if (NULL != filePath) { if (deleteit) { - NSAutoreleasePool *pool = - [[NSAutoreleasePool alloc] init]; - const char *s = [filePath fileSystemRepresentation]; - remove(s); - [pool release]; + remove(filePath.fileSystemRepresentation); } - [filePath release]; DriveNames[Drive_No] = NULL; /* not really needed */ } } @@ -530,11 +513,10 @@ GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r) { tMacErr v = mnvm_miscErr; NSString *filePath = DriveNames[Drive_No]; if (NULL != filePath) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSString *s0 = [filePath lastPathComponent]; - v = NSStringToRomanPbuf(s0, r); - - [pool release]; + @autoreleasepool { + NSString *s0 = [filePath lastPathComponent]; + v = NSStringToRomanPbuf(s0, r); + } } return v; @@ -563,7 +545,7 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked, NSString *filePath) { DiskInsertNotify(Drive_No, locked); #if IncludeSonyGetName || IncludeSonyNew - DriveNames[Drive_No] = [filePath retain]; + DriveNames[Drive_No] = filePath.copy; #endif IsOk = trueblnr; @@ -611,7 +593,7 @@ GLOBALFUNC blnr Sony_Insert1(NSString *filePath, blnr silentfail) { LOCALFUNC blnr Sony_Insert2(char *s) { NSString *sPath; - if (!FindNamedChildFilePath(MyDataPath, s, &sPath)) { + if (!FindNamedChildFilePath(sharedEmulator.dataPath, s, &sPath)) { return falseblnr; } else { return Sony_Insert1(sPath, trueblnr); @@ -712,7 +694,7 @@ LOCALFUNC tMacErr LoadMacRomFrom(NSString *parentPath) { LOCALFUNC blnr LoadMacRom(void) { tMacErr err; - if (mnvm_fnfErr == (err = LoadMacRomFrom(MyDataPath))) { + if (mnvm_fnfErr == (err = LoadMacRomFrom(sharedEmulator.dataPath))) { } if (mnvm_noErr != err) { @@ -734,19 +716,11 @@ LOCALFUNC blnr LoadMacRom(void) { #if IncludeHostTextClipExchange GLOBALFUNC tMacErr HTCEexport(tPbuf i) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSData *d = [NSData dataWithBytes:PbufDat[i] length:PbufSize[i]]; - NSString *ss = [[[NSString alloc] - initWithData:d - encoding:NSMacOSRomanStringEncoding] - autorelease]; - UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; - pasteboard.string = ss; - - PbufDispose(i); - - [pool release]; - + @autoreleasepool { + UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; + pasteboard.string = [[NSString alloc] initWithBytes:PbufDat[i] length:PbufSize[i] encoding:NSMacOSRomanStringEncoding]; + PbufDispose(i); + } return mnvm_noErr; } #endif @@ -754,13 +728,12 @@ GLOBALFUNC tMacErr HTCEexport(tPbuf i) { #if IncludeHostTextClipExchange GLOBALFUNC tMacErr HTCEimport(tPbuf *r) { tMacErr err = mnvm_miscErr; - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; - if (pasteboard.string != nil) { - err = NSStringToRomanPbuf(pasteboard.string, r); + @autoreleasepool { + UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; + if (pasteboard.string != nil) { + err = NSStringToRomanPbuf(pasteboard.string, r); + } } - [pool release]; - return err; } #endif @@ -842,26 +815,6 @@ LOCALFUNC blnr InitLocationDat(void) { return trueblnr; } -#pragma mark - Mouse - -GLOBALPROC SetMouseButton(blnr down) { - MyMouseButtonSet(down); -} - -GLOBALPROC SetMouseLoc(ui4r h, ui4r v) { - MyMousePositionSet(h, v); -} - -GLOBALPROC SetMouseDelta(ui4r dh, ui4r dv) { - MyMousePositionSetDelta(dh, dv); -} - -#pragma mark - Keyboard - -GLOBALPROC SetKeyState(int key, blnr down) { - Keyboard_UpdateKeyMap(key, down); -} - #pragma mark - Video Out #if 0 != vMacScreenDepth && vMacScreenDepth < 4 @@ -1584,25 +1537,17 @@ LOCALPROC EnterSpeedStopped(void) { #endif } -GLOBALFUNC blnr GetSpeedStopped(void) { - return SpeedStopped; -} - -GLOBALPROC SetSpeedStopped(blnr stopped) { - SpeedStopped = stopped; -} - LOCALPROC MacMsgDisplayOn() { if (SavedBriefMsg != nullpr) { NSString *title = NSStringCreateFromSubstCStr(SavedBriefMsg, falseblnr); NSString *message = NSStringCreateFromSubstCStr(SavedLongMsg, falseblnr); if ([UIAlertController class]) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; - blnr wasStopped = CurSpeedStopped; + blnr wasStopped = SpeedStopped; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { - SetSpeedStopped(wasStopped); + SpeedStopped = wasStopped; }]]; - SetSpeedStopped(trueblnr); + SpeedStopped = trueblnr; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; } else { // fallback for iOS 7 @@ -1618,7 +1563,7 @@ LOCALFUNC blnr InitOSGLU(void) { blnr IsOk = falseblnr; @autoreleasepool { if (AllocMyMemory()) - if (InitCocoaStuff()) + if (Screen_Init()) #if dbglog_HAVE if (dbglog_open()) #endif @@ -1660,7 +1605,7 @@ LOCALPROC UnInitOSGLU(void) { #endif CheckSavedMacMsg(); - UnInitCocoaStuff(); + Screen_UnInit(); UnallocMyMemory(); } @@ -1702,53 +1647,180 @@ GLOBALFUNC blnr ExtraTimeNotOver(void) { } GLOBALPROC WaitForNextTick(void) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSRunLoop *mainRunLoop = [NSRunLoop mainRunLoop]; - NSDate *until = [NSDate distantPast]; -label_retry: - [mainRunLoop runMode:NSDefaultRunLoopMode beforeDate:until]; - - CheckForSavedTasks(); - - if (ForceMacOff) { - goto label_exit; - } - - if (CurSpeedStopped) { - DoneWithDrawingForTick(); - until = [NSDate distantFuture]; - goto label_retry; - } - - if (ExtraTimeNotOver()) { - until = [NSDate dateWithTimeIntervalSinceReferenceDate:NextTickChangeTime]; - goto label_retry; - } - - if (CheckDateTime()) { + @autoreleasepool { + NSRunLoop *mainRunLoop = [NSRunLoop mainRunLoop]; + NSDate *until = [NSDate distantPast]; + label_retry: + [mainRunLoop runMode:NSDefaultRunLoopMode beforeDate:until]; + + CheckForSavedTasks(); + + if (ForceMacOff) { + return; + } + + if (CurSpeedStopped) { + DoneWithDrawingForTick(); + until = [NSDate distantFuture]; + goto label_retry; + } + + if (ExtraTimeNotOver()) { + until = [NSDate dateWithTimeIntervalSinceReferenceDate:NextTickChangeTime]; + goto label_retry; + } + + if (CheckDateTime()) { #if MySoundEnabled - MySound_SecondNotify(); + MySound_SecondNotify(); #endif #if EnableDemoMsg - DemoModeSecondNotify(); + DemoModeSecondNotify(); +#endif + } + + OnTrueTime = TrueEmulatedTime; + +#if dbglog_TimeStuff + dbglog_writelnNum("WaitForNextTick, OnTrueTime", OnTrueTime); #endif } - - OnTrueTime = TrueEmulatedTime; - -#if dbglog_TimeStuff - dbglog_writelnNum("WaitForNextTick, OnTrueTime", OnTrueTime); -#endif - -label_exit: - [pool release]; } -GLOBALPROC RunEmulator(void) { - ZapOSGLUVars(); +#pragma mark - Objective-C Interface +static dispatch_once_t onceToken; + +@implementation Emulator + +@synthesize dataPath; + ++ (instancetype)sharedEmulator { + dispatch_once(&onceToken, ^{ + sharedEmulator = [self new]; + }); + return sharedEmulator; +} + +- (instancetype)init { + if ((self = [super init])) { + dispatch_once(&onceToken, ^{ + sharedEmulator = self; + }); + } + return self; +} + +- (void)run { + ZapOSGLUVars(); + if (InitOSGLU()) { ProgramMain(); } UnInitOSGLU(); } + +- (NSInteger)initialSpeed { + return WantInitSpeedValue; +} + +- (NSBundle *)bundle { + return [NSBundle bundleForClass:self.class]; +} + +- (NSInteger)speed { + return SpeedValue; +} + +- (void)setSpeed:(NSInteger)speed { + SpeedValue = speed; +} + +- (BOOL)isRunning { + return !SpeedStopped; +} + +- (void)setRunning:(BOOL)running { + SpeedStopped = !running; +} + +- (void)interrupt { + WantMacInterrupt = trueblnr; +} + +- (void)reset { + WantMacReset = trueblnr; +} + +#pragma mark - Screen + +@synthesize screenLayer; + +- (CGSize)screenSize { + return CGSizeMake(vMacScreenWidth, vMacScreenHeight); +} + +- (void)updateScreen:(CGImageRef)screenImage { + screenLayer.contents = (__bridge id)screenImage; +} + +#pragma mark - Disk + +@synthesize insertDiskNotification, ejectDiskNotification; + +- (BOOL)anyDiskInserted { + return AnyDiskInserted(); +} + +- (BOOL)isDiskInserted:(NSString *)path { + return Sony_IsInserted(path); +} + +- (BOOL)insertDisk:(NSString *)path { + return Sony_Insert1(path, false); +} + +- (NSString *)insertDiskNotification { + return @"didInsertDisk"; +} + +- (NSString *)ejectDiskNotification { + return @"didEjectDisk"; +} + +#pragma mark - Keyboard + +- (int)translateScanCode:(int)scancode { + switch (scancode) { + case 54: return 59; // left control + case 59: return 70; // arrow left + case 60: return 66; // arrow right + case 61: return 72; // arrow down + case 62: return 77; // arrow up + default: return scancode; + } +} + +- (void)keyDown:(int)scancode { + Keyboard_UpdateKeyMap([self translateScanCode:scancode], 1); +} + +- (void)keyUp:(int)scancode { + Keyboard_UpdateKeyMap([self translateScanCode:scancode], 0); +} + +#pragma mark - Mouse + +- (void)setMouseX:(NSInteger)x Y:(NSInteger)y { + MyMousePositionSet(x, y); +} + +- (void)moveMouseX:(NSInteger)x Y:(NSInteger)y { + MyMousePositionSetDelta(x, y); +} + +- (void)setMouseButton:(BOOL)down { + MyMouseButtonSet(down); +} + +@end