ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/Emulator.mm
Revision: 1.6
Committed: 2003-03-26T23:04:46Z (21 years, 2 months ago) by nigel
Branch: MAIN
CVS Tags: nigel-build-13
Changes since 1.5: +38 -11 lines
Log Message:
Fixes for safe Restart()ing, some tidying up

File Contents

# User Rev Content
1 nigel 1.1 /*
2     * Emulator.mm - Class whose actions are attached to GUI widgets in a window,
3     * used to control a single Basilisk II emulated Macintosh.
4     *
5 nigel 1.6 * $Id: Emulator.mm,v 1.5 2003/03/26 01:45:31 nigel Exp $
6 nigel 1.1 *
7     * Basilisk II (C) 1997-2002 Christian Bauer
8     *
9     * This program is free software; you can redistribute it and/or modify
10     * it under the terms of the GNU General Public License as published by
11     * the Free Software Foundation; either version 2 of the License, or
12     * (at your option) any later version.
13     *
14     * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18     *
19     * You should have received a copy of the GNU General Public License
20     * along with this program; if not, write to the Free Software
21     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22     */
23    
24     #import "Emulator.h"
25     #import "EmulatorView.h"
26    
27     @implementation Emulator
28    
29     #import "sysdeps.h" // Types used in Basilisk C++ code
30    
31     #import "main_macosx.h" // Prototypes for QuitEmuNoExit() and InitEmulator()
32     #import "misc_macosx.h" // Some other prototypes
33     #import "video_macosx.h" // Some window/view globals
34    
35     #import <adb.h>
36     #import <main.h>
37     #import <prefs.h>
38     #import <timer.h>
39    
40 nigel 1.4 #undef check() // memory.h defines a check macro, clashes with an OS X one?
41 nigel 1.1 #import <cpu_emulation.h>
42    
43 nigel 1.4 #define DEBUG 0
44 nigel 1.1 #import <debug.h>
45    
46     // NSWindow method, which is invoked via delegation
47    
48     - (BOOL) windowShouldClose: (id)sender
49     {
50     if ( uaeCreated )
51     {
52     NSLog(@"windowShouldClose returning NO");
53     return NO; // Should initiate poweroff and return NSTerminateLater ?
54     }
55    
56     NSLog(@"windowShouldClose returning YES");
57     return YES;
58     }
59    
60     // Default methods
61    
62     - (Emulator *) init
63     {
64     int frameSkip;
65    
66     self = [super init];
67    
68     running = NO; // Save churn when application loads
69     // running = YES;
70     uaeCreated = NO;
71    
72     frameSkip = PrefsFindInt32("frameskip");
73     if ( frameSkip )
74     redrawDelay = frameSkip / 60.0;
75     else
76     redrawDelay = 0.0;
77    
78     // We do this so that we can work out if we are in full screen mode:
79     parse_screen_prefs(PrefsFindString("screen"));
80    
81     [self createThreads];
82    
83     return self;
84     }
85    
86     - (void) awakeFromNib
87     {
88     the_win = win; // Set global for access by Basilisk C++ code
89    
90    
91     [win setDelegate: self]; // Enable windowShouldClose calling
92    
93     // Try to speed up everything
94     //[win setHasShadow: NO]; // This causes view & window to now be drawn correctly
95     [win useOptimizedDrawing: YES];
96    
97     [win makeKeyAndOrderFront:self];
98    
99     if ( redrawDelay )
100     [speed setFloatValue: 1.0 / redrawDelay];
101     else
102     [speed setFloatValue: 60.0];
103    
104    
105     if ( runOrPause == nil )
106     NSLog(@"%s - runOrPause button pointer is nil!", __PRETTY_FUNCTION__);
107    
108     [self runUpdate];
109     }
110    
111    
112     // Helpers which other classes use to access our private stuff
113    
114     - (BOOL) isRunning { return running; }
115     - (BOOL) uaeCreated { return uaeCreated; }
116     - (EmulatorView *) screen { return screen; }
117     - (NSSlider *) speed { return speed; }
118     - (NSWindow *) window { return win; }
119    
120    
121     // Update some UI elements
122    
123     - (void) runUpdate
124     {
125     if ( running )
126     [runOrPause setState: NSOnState]; // Running. Change button label to 'Pause'
127     else
128     [runOrPause setState: NSOffState]; // Paused. Change button label to 'Run'
129    
130     [win setDocumentEdited: uaeCreated]; // Set the little dimple in the close button
131     }
132    
133    
134     // Methods invoked by buttons & menu items
135    
136     - (IBAction) Benchmark: (id)sender;
137     {
138     BOOL wasRunning = running;
139    
140     if ( running )
141     [self Suspend: self];
142     [screen benchmark];
143     if ( wasRunning )
144     [self Resume: self];
145     }
146    
147 nigel 1.6 #ifdef NIGEL
148     - (IBAction) EjectCD: (id)sender;
149     {
150     NSString *path;
151     const char *cdrom = PrefsFindString("cdrom");
152    
153     if ( cdrom )
154     {
155     #include <sys/param.h>
156     #define KERNEL
157     #include <sys/mount.h>
158    
159     struct statfs buf;
160     if ( fsstat(path, &buf) < 0 )
161     return;
162    
163     path = [NSString stringWithCString: cdrom];
164    
165     [[NSWorkspace sharedWorkspace] unmountAndEjectDeviceAtPath: path];
166     // [path release];
167     }
168     }
169     #endif
170    
171 nigel 1.1 - (IBAction) Interrupt: (id)sender;
172     {
173 nigel 1.3 WarningSheet (@"Interrupt action not yet supported", win);
174 nigel 1.1 }
175    
176     - (IBAction) PowerKey: (id)sender;
177     {
178     if ( uaeCreated ) // If Mac has started
179     {
180     ADBKeyDown(0x7f); // Send power key, which is also
181     ADBKeyUp(0x7f); // called ADB_RESET or ADB_POWER
182     }
183     else
184     {
185     running = YES; // Start emulator
186     [self runUpdate];
187     [self Resume: nil];
188     }
189     }
190    
191     - (IBAction) Restart: (id)sender
192     {
193     if ( running )
194     // reset680x0();
195     {
196     uaeCreated = NO;
197     [redraw suspend];
198     NSLog (@"%s - uae_cpu reset not yet supported, will try to fake it",
199     __PRETTY_FUNCTION__);
200    
201 nigel 1.6 [screen clear];
202     [screen display];
203 nigel 1.1
204     [emul terminate]; QuitEmuNoExit();
205    
206 nigel 1.6
207     // OK. We have killed & cleaned up. Now, start afresh:
208     #include <sys.h>
209     int argc = 0;
210     char **argv;
211    
212     PrefsInit(argc, argv);
213     SysInit();
214    
215 nigel 1.4 emul = [NNThread new];
216 nigel 1.1 [emul perform:@selector(emulThread) of:self];
217     [emul start];
218    
219     if ( display_type != DISPLAY_SCREEN )
220     [redraw resume];
221     }
222     }
223    
224     - (IBAction) Resume: (id)sender
225     {
226     [RTC resume];
227     [emul resume];
228     if ( display_type != DISPLAY_SCREEN )
229     [redraw resume];
230     [tick resume];
231     [xPRAM resume];
232     }
233    
234 nigel 1.2 - (IBAction) ScreenHideShow: (NSButton *)sender;
235     {
236     WarningSheet(@"Nigel doesn't know how to shrink or grow this window",
237     @"Maybe you can grab the source code and have a go yourself?",
238     nil, win);
239     }
240    
241 nigel 1.1 - (IBAction) Snapshot: (id) sender
242     {
243     if ( screen == nil || uaeCreated == NO )
244     WarningSheet(@"The emulator has not yet started.",
245     @"There is no screen output to snapshot",
246 nigel 1.3 nil, win);
247 nigel 1.1 else
248     {
249     NSData *TIFFdata;
250    
251     [self Suspend: self];
252    
253     TIFFdata = [screen TIFFrep];
254     if ( TIFFdata == nil )
255     NSLog(@"%s - Unable to convert Basilisk screen to a TIFF representation",
256     __PRETTY_FUNCTION__);
257     else
258     {
259     NSSavePanel *sp = [NSSavePanel savePanel];
260    
261     [sp setRequiredFileType:@"tiff"];
262    
263     if ( [sp runModalForDirectory:NSHomeDirectory()
264     file:@"B2-screen-snapshot.tiff"] == NSOKButton )
265     if ( ! [TIFFdata writeToFile:[sp filename] atomically:YES] )
266     NSLog(@"%s - Could not write TIFF data to file @%",
267     __PRETTY_FUNCTION__, [sp filename]);
268    
269     }
270     if ( running )
271     [self Resume: self];
272     }
273     }
274    
275     - (IBAction) SpeedChange: (NSSlider *)sender
276     {
277     float frequency = [sender floatValue];
278    
279     [redraw suspend];
280    
281     if ( frequency == 0.0 )
282     redrawDelay = 0.0;
283     else
284     {
285     frequencyToTickDelay(frequency);
286    
287     redrawDelay = 1.0 / frequency;
288    
289     [redraw changeIntervalTo: (int)(redrawDelay * 1e6)
290     units: NNmicroSeconds];
291     if ( running && display_type != DISPLAY_SCREEN )
292     [redraw resume];
293     }
294     }
295    
296     - (IBAction) Suspend: (id)sender
297     {
298     [RTC suspend];
299     [emul suspend];
300     [redraw suspend];
301     [tick suspend];
302     [xPRAM suspend];
303     }
304    
305     - (IBAction) ToggleState: (NSButton *)sender
306     {
307     running = [sender state]; // State of the toggled NSButton
308     if ( running )
309     [self Resume: nil];
310     else
311     [self Suspend: nil];
312     }
313    
314     - (IBAction) Terminate: (id)sender;
315     {
316     [self exitThreads];
317     [win performClose: self];
318     }
319    
320     #include <xpram.h>
321    
322     #define XPRAM_SIZE 256
323    
324     uint8 lastXPRAM[XPRAM_SIZE]; // Copy of PRAM
325    
326     - (IBAction) ZapPRAM: (id)sender;
327     {
328     memset(XPRAM, 0, XPRAM_SIZE);
329     memset(lastXPRAM, 0, XPRAM_SIZE);
330     ZapPRAM();
331     }
332    
333     //
334     // Threads, Timers and stuff to manage them:
335     //
336    
337     - (void) createThreads
338     {
339     #ifdef USE_PTHREADS
340 nigel 1.3 // Make UI threadsafe:
341     [NSThread detachNewThreadSelector:(SEL)"" toTarget:nil withObject:nil];
342 nigel 1.1 //emul = [[NNThread alloc] initWithAutoReleasePool];
343     #endif
344 nigel 1.4 emul = [NNThread new];
345     RTC = [NNTimer new];
346     redraw = [NNTimer new];
347     tick = [NNTimer new];
348     xPRAM = [NNTimer new];
349 nigel 1.1
350     [emul perform:@selector(emulThread) of:self];
351     [RTC repeat:@selector(RTCinterrupt) of:self
352     every:1
353     units:NNseconds];
354     [redraw repeat:@selector(redrawScreen) of:self
355     every:(int)(1000*redrawDelay)
356     units:NNmilliSeconds];
357     [tick repeat:@selector(tickInterrupt) of:self
358     every:16625
359     units:NNmicroSeconds];
360     [xPRAM repeat:@selector(xPRAMbackup) of:self
361     every:60
362     units:NNseconds];
363    
364     if ( running ) // Start emulator, then threads in most economical order
365     {
366     [emul start];
367     [xPRAM start];
368     [RTC start];
369     if ( display_type != DISPLAY_SCREEN )
370     [redraw start];
371     [tick start];
372     }
373     }
374    
375     - (void) exitThreads
376     {
377     running = NO;
378     [emul terminate]; [emul release]; emul = nil;
379     [tick invalidate]; [tick release]; tick = nil;
380     [redraw invalidate]; [redraw release]; redraw = nil;
381     [RTC invalidate]; [RTC release]; RTC = nil;
382     [xPRAM invalidate]; [xPRAM release]; xPRAM = nil;
383     }
384    
385     - (void) emulThread
386     {
387 nigel 1.4 NSAutoreleasePool *pool = [NSAutoreleasePool new];
388 nigel 1.1
389 nigel 1.5 if ( ! InitEmulator() )
390     {
391     [redraw suspend]; // Stop the barberpole
392 nigel 1.1
393 nigel 1.5 ErrorSheet(@"Cannot start Emulator", @"", @"Quit", win);
394     }
395 nigel 1.1 else
396     {
397     memcpy(lastXPRAM, XPRAM, XPRAM_SIZE);
398    
399     uaeCreated = YES; // Enable timers to access emulated Mac's memory
400    
401 nigel 1.3 while ( screen == nil ) // If we are still loading from Nib?
402 nigel 1.1 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];
403    
404 nigel 1.5 [self runUpdate]; // Set the window close gadget to dimpled
405 nigel 1.1
406     Start680x0(); // Start 68k and jump to ROM boot routine
407    
408     puts ("Emulator exited normally");
409     }
410    
411     [pool release];
412 nigel 1.5 QuitEmulator();
413 nigel 1.1 }
414    
415     - (void) RTCinterrupt
416     {
417     if ( uaeCreated )
418     WriteMacInt32 (0x20c, TimerDateTime() ); // Update MacOS time
419     }
420    
421     - (void) redrawScreen
422     {
423     if ( display_type == DISPLAY_SCREEN )
424     {
425 nigel 1.6 NSLog(@"We are in fullscreen mode - why was redrawScreen() called?");
426 nigel 1.1 return;
427     }
428     [barberPole animate:self]; // wobble the pole
429     [screen setNeedsDisplay: YES]; // redisplay next time through runLoop
430     // Or, use a direct method. e.g.
431 nigel 1.6 // [screen display] or [screen cgDrawInto: ...];
432 nigel 1.1 }
433    
434     #include <main.h> // For #define INTFLAG_60HZ
435     #include <rom_patches.h> // For ROMVersion
436     #include "macos_util_macosx.h" // For HasMacStarted()
437    
438     - (void) tickInterrupt
439     {
440     if ( ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted() )
441     {
442     SetInterruptFlag (INTFLAG_60HZ);
443     TriggerInterrupt ();
444     }
445     }
446    
447     - (void) xPRAMbackup
448     {
449     if ( uaeCreated &&
450     memcmp(lastXPRAM, XPRAM, XPRAM_SIZE) ) // if PRAM changed from copy
451     {
452     memcpy (lastXPRAM, XPRAM, XPRAM_SIZE); // re-copy
453     SaveXPRAM (); // and save to disk
454     }
455     }
456    
457 nigel 1.2 @end