ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/Emulator.mm
Revision: 1.2
Committed: 2002-05-23T12:48:38Z (22 years, 6 months ago) by nigel
Branch: MAIN
Changes since 1.1: +9 -2 lines
Log Message:
Added ScreenHideShow: action

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.2 * $Id: Emulator.mm,v 1.1 2002/03/16 04:00:09 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     #undef check() // memory.h defines a check macro, which clashes with an OS X one?
41     #import <cpu_emulation.h>
42    
43     #define DEBUG 1
44     #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 center];
98     [win makeKeyAndOrderFront:self];
99    
100     // [self resizeWinToWidth:x Height:y];
101    
102     if ( redrawDelay )
103     [speed setFloatValue: 1.0 / redrawDelay];
104     else
105     [speed setFloatValue: 60.0];
106    
107    
108     if ( runOrPause == nil )
109     NSLog(@"%s - runOrPause button pointer is nil!", __PRETTY_FUNCTION__);
110    
111     [self runUpdate];
112     }
113    
114    
115     // Helpers which other classes use to access our private stuff
116    
117     - (BOOL) isRunning { return running; }
118     - (BOOL) uaeCreated { return uaeCreated; }
119     - (EmulatorView *) screen { return screen; }
120     - (NSSlider *) speed { return speed; }
121     - (NSWindow *) window { return win; }
122    
123     //#define DEBUG 1
124     //#include <debug.h>
125    
126     // Update some UI elements
127    
128     - (void) runUpdate
129     {
130     if ( running )
131     [runOrPause setState: NSOnState]; // Running. Change button label to 'Pause'
132     else
133     [runOrPause setState: NSOffState]; // Paused. Change button label to 'Run'
134    
135     [win setDocumentEdited: uaeCreated]; // Set the little dimple in the close button
136     }
137    
138    
139     // Methods invoked by buttons & menu items
140    
141     - (IBAction) Benchmark: (id)sender;
142     {
143     BOOL wasRunning = running;
144    
145     if ( running )
146     [self Suspend: self];
147     [screen benchmark];
148     if ( wasRunning )
149     [self Resume: self];
150     }
151    
152     - (IBAction) Interrupt: (id)sender;
153     {
154     WarningSheet (@"Interrupt action not yet supported", @"", @"OK", win);
155     }
156    
157     - (IBAction) PowerKey: (id)sender;
158     {
159     if ( uaeCreated ) // If Mac has started
160     {
161     ADBKeyDown(0x7f); // Send power key, which is also
162     ADBKeyUp(0x7f); // called ADB_RESET or ADB_POWER
163     }
164     else
165     {
166     running = YES; // Start emulator
167     [self runUpdate];
168     [self Resume: nil];
169     }
170     }
171    
172     - (IBAction) Restart: (id)sender
173     {
174     if ( running )
175     // reset680x0();
176     {
177     uaeCreated = NO;
178     [redraw suspend];
179     NSLog (@"%s - uae_cpu reset not yet supported, will try to fake it",
180     __PRETTY_FUNCTION__);
181    
182     // [screen blacken];
183     [screen setNeedsDisplay: YES];
184    
185     [emul terminate]; QuitEmuNoExit();
186    
187     emul = [[NNThread alloc] init];
188     [emul perform:@selector(emulThread) of:self];
189     [emul start];
190    
191     if ( display_type != DISPLAY_SCREEN )
192     [redraw resume];
193     uaeCreated = YES;
194     }
195     }
196    
197     - (IBAction) Resume: (id)sender
198     {
199     [RTC resume];
200     [emul resume];
201     if ( display_type != DISPLAY_SCREEN )
202     [redraw resume];
203     [tick resume];
204     [xPRAM resume];
205     }
206    
207 nigel 1.2 - (IBAction) ScreenHideShow: (NSButton *)sender;
208     {
209     WarningSheet(@"Nigel doesn't know how to shrink or grow this window",
210     @"Maybe you can grab the source code and have a go yourself?",
211     nil, win);
212     }
213    
214 nigel 1.1 - (IBAction) Snapshot: (id) sender
215     {
216     if ( screen == nil || uaeCreated == NO )
217     WarningSheet(@"The emulator has not yet started.",
218     @"There is no screen output to snapshot",
219     @"OK", win);
220     else
221     {
222     NSData *TIFFdata;
223    
224     [self Suspend: self];
225    
226     TIFFdata = [screen TIFFrep];
227     if ( TIFFdata == nil )
228     NSLog(@"%s - Unable to convert Basilisk screen to a TIFF representation",
229     __PRETTY_FUNCTION__);
230     else
231     {
232     NSSavePanel *sp = [NSSavePanel savePanel];
233    
234     [sp setRequiredFileType:@"tiff"];
235    
236     if ( [sp runModalForDirectory:NSHomeDirectory()
237     file:@"B2-screen-snapshot.tiff"] == NSOKButton )
238     if ( ! [TIFFdata writeToFile:[sp filename] atomically:YES] )
239     NSLog(@"%s - Could not write TIFF data to file @%",
240     __PRETTY_FUNCTION__, [sp filename]);
241    
242     }
243     if ( running )
244     [self Resume: self];
245     }
246     }
247    
248     - (IBAction) SpeedChange: (NSSlider *)sender
249     {
250     float frequency = [sender floatValue];
251    
252     [redraw suspend];
253    
254     if ( frequency == 0.0 )
255     redrawDelay = 0.0;
256     else
257     {
258     frequencyToTickDelay(frequency);
259    
260     redrawDelay = 1.0 / frequency;
261    
262     [redraw changeIntervalTo: (int)(redrawDelay * 1e6)
263     units: NNmicroSeconds];
264     if ( running && display_type != DISPLAY_SCREEN )
265     [redraw resume];
266     }
267     }
268    
269     - (IBAction) Suspend: (id)sender
270     {
271     [RTC suspend];
272     [emul suspend];
273     [redraw suspend];
274     [tick suspend];
275     [xPRAM suspend];
276     }
277    
278     - (IBAction) ToggleState: (NSButton *)sender
279     {
280     running = [sender state]; // State of the toggled NSButton
281     if ( running )
282     [self Resume: nil];
283     else
284     [self Suspend: nil];
285     }
286    
287     - (IBAction) Terminate: (id)sender;
288     {
289     [self exitThreads];
290     [win performClose: self];
291     }
292    
293     #include <xpram.h>
294    
295     #define XPRAM_SIZE 256
296    
297     uint8 lastXPRAM[XPRAM_SIZE]; // Copy of PRAM
298    
299     - (IBAction) ZapPRAM: (id)sender;
300     {
301     memset(XPRAM, 0, XPRAM_SIZE);
302     memset(lastXPRAM, 0, XPRAM_SIZE);
303     ZapPRAM();
304     }
305    
306     //
307     // Threads, Timers and stuff to manage them:
308     //
309    
310     - (void) createThreads
311     {
312     #ifdef USE_PTHREADS
313     [NSThread detachNewThreadSelector:(SEL)"" toTarget:nil withObject:nil]; // Make UI threadsafe
314     //emul = [[NNThread alloc] initWithAutoReleasePool];
315     #endif
316     emul = [[NNThread alloc] init];
317     RTC = [[NNTimer alloc] init];
318     redraw = [[NNTimer alloc] init];
319     tick = [[NNTimer alloc] init];
320     xPRAM = [[NNTimer alloc] init];
321    
322     [emul perform:@selector(emulThread) of:self];
323     [RTC repeat:@selector(RTCinterrupt) of:self
324     every:1
325     units:NNseconds];
326     [redraw repeat:@selector(redrawScreen) of:self
327     every:(int)(1000*redrawDelay)
328     units:NNmilliSeconds];
329     [tick repeat:@selector(tickInterrupt) of:self
330     every:16625
331     units:NNmicroSeconds];
332     [xPRAM repeat:@selector(xPRAMbackup) of:self
333     every:60
334     units:NNseconds];
335    
336     if ( running ) // Start emulator, then threads in most economical order
337     {
338     [emul start];
339     [xPRAM start];
340     [RTC start];
341     if ( display_type != DISPLAY_SCREEN )
342     [redraw start];
343     [tick start];
344     }
345     }
346    
347     - (void) exitThreads
348     {
349     running = NO;
350     [emul terminate]; [emul release]; emul = nil;
351     [tick invalidate]; [tick release]; tick = nil;
352     [redraw invalidate]; [redraw release]; redraw = nil;
353     [RTC invalidate]; [RTC release]; RTC = nil;
354     [xPRAM invalidate]; [xPRAM release]; xPRAM = nil;
355     if ( uaeCreated )
356     QuitEmuNoExit();
357     }
358    
359     - (void) emulThread
360     {
361     extern uint8 *RAMBaseHost, *ROMBaseHost;
362     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
363    
364     // [screen allocBitmap]; // Do this first, because InitEmulator() calls VideoInit(), which needs the_bitmap.
365    
366     InitEmulator();
367    
368     if ( RAMBaseHost == NULL || ROMBaseHost == NULL )
369     ErrorSheet(@"Cannot start Emulator.",
370     @"Emulator memory not allocated", @"OK", win);
371     else
372     {
373     memcpy(lastXPRAM, XPRAM, XPRAM_SIZE);
374    
375     uaeCreated = YES; // Enable timers to access emulated Mac's memory
376    
377     while ( screen == nil ) // If init sets running, but we are still loading from Nib?
378     [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];
379    
380     // [screen readyToDraw];
381     [self runUpdate];
382    
383     Start680x0(); // Start 68k and jump to ROM boot routine
384    
385     puts ("Emulator exited normally");
386     }
387    
388     running = NO;
389     uaeCreated = NO;
390     [self runUpdate]; // Update button & dimple
391     [pool release];
392     [self exitThreads];
393     }
394    
395     - (void) RTCinterrupt
396     {
397     if ( uaeCreated )
398     WriteMacInt32 (0x20c, TimerDateTime() ); // Update MacOS time
399     }
400    
401     - (void) redrawScreen
402     {
403     if ( display_type == DISPLAY_SCREEN )
404     {
405     NSLog(@"Why was redrawScreen() called?");
406     return;
407     }
408     [barberPole animate:self]; // wobble the pole
409     [screen setNeedsDisplay: YES]; // redisplay next time through runLoop
410     // Or, use a direct method. e.g.
411     // [screen cgDrawInto: ...];
412     }
413    
414     #include <main.h> // For #define INTFLAG_60HZ
415     #include <rom_patches.h> // For ROMVersion
416     #include "macos_util_macosx.h" // For HasMacStarted()
417    
418     - (void) tickInterrupt
419     {
420     if ( ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted() )
421     {
422     SetInterruptFlag (INTFLAG_60HZ);
423     TriggerInterrupt ();
424     }
425     }
426    
427     - (void) xPRAMbackup
428     {
429     if ( uaeCreated &&
430     memcmp(lastXPRAM, XPRAM, XPRAM_SIZE) ) // if PRAM changed from copy
431     {
432     memcpy (lastXPRAM, XPRAM, XPRAM_SIZE); // re-copy
433     SaveXPRAM (); // and save to disk
434     }
435     }
436    
437 nigel 1.2 @end