ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/Emulator.mm
Revision: 1.3
Committed: 2002-05-30T12:36:17Z (22 years ago) by nigel
Branch: MAIN
Changes since 1.2: +8 -9 lines
Log Message:
Minor tidyup

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.3 * $Id: Emulator.mm,v 1.2 2002/05/23 12:48:38 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 nigel 1.3 WarningSheet (@"Interrupt action not yet supported", win);
155 nigel 1.1 }
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 nigel 1.3 nil, win);
220 nigel 1.1 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 nigel 1.3 // Make UI threadsafe:
314     [NSThread detachNewThreadSelector:(SEL)"" toTarget:nil withObject:nil];
315 nigel 1.1 //emul = [[NNThread alloc] initWithAutoReleasePool];
316     #endif
317     emul = [[NNThread alloc] init];
318     RTC = [[NNTimer alloc] init];
319     redraw = [[NNTimer alloc] init];
320     tick = [[NNTimer alloc] init];
321     xPRAM = [[NNTimer alloc] init];
322    
323     [emul perform:@selector(emulThread) of:self];
324     [RTC repeat:@selector(RTCinterrupt) of:self
325     every:1
326     units:NNseconds];
327     [redraw repeat:@selector(redrawScreen) of:self
328     every:(int)(1000*redrawDelay)
329     units:NNmilliSeconds];
330     [tick repeat:@selector(tickInterrupt) of:self
331     every:16625
332     units:NNmicroSeconds];
333     [xPRAM repeat:@selector(xPRAMbackup) of:self
334     every:60
335     units:NNseconds];
336    
337     if ( running ) // Start emulator, then threads in most economical order
338     {
339     [emul start];
340     [xPRAM start];
341     [RTC start];
342     if ( display_type != DISPLAY_SCREEN )
343     [redraw start];
344     [tick start];
345     }
346     }
347    
348     - (void) exitThreads
349     {
350     running = NO;
351     [emul terminate]; [emul release]; emul = nil;
352     [tick invalidate]; [tick release]; tick = nil;
353     [redraw invalidate]; [redraw release]; redraw = nil;
354     [RTC invalidate]; [RTC release]; RTC = nil;
355     [xPRAM invalidate]; [xPRAM release]; xPRAM = nil;
356     if ( uaeCreated )
357     QuitEmuNoExit();
358     }
359    
360     - (void) emulThread
361     {
362     extern uint8 *RAMBaseHost, *ROMBaseHost;
363     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
364    
365     InitEmulator();
366    
367     if ( RAMBaseHost == NULL || ROMBaseHost == NULL )
368 nigel 1.3 ErrorSheet(@"Cannot start Emulator",
369     @"Emulator memory not allocated", nil, win);
370 nigel 1.1 else
371     {
372     memcpy(lastXPRAM, XPRAM, XPRAM_SIZE);
373    
374     uaeCreated = YES; // Enable timers to access emulated Mac's memory
375    
376 nigel 1.3 while ( screen == nil ) // If we are still loading from Nib?
377 nigel 1.1 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow: 1.0]];
378    
379     // [screen readyToDraw];
380     [self runUpdate];
381    
382     Start680x0(); // Start 68k and jump to ROM boot routine
383    
384     puts ("Emulator exited normally");
385     }
386    
387     running = NO;
388     uaeCreated = NO;
389     [self runUpdate]; // Update button & dimple
390     [pool release];
391     [self exitThreads];
392     }
393    
394     - (void) RTCinterrupt
395     {
396     if ( uaeCreated )
397     WriteMacInt32 (0x20c, TimerDateTime() ); // Update MacOS time
398     }
399    
400     - (void) redrawScreen
401     {
402     if ( display_type == DISPLAY_SCREEN )
403     {
404     NSLog(@"Why was redrawScreen() called?");
405     return;
406     }
407     [barberPole animate:self]; // wobble the pole
408     [screen setNeedsDisplay: YES]; // redisplay next time through runLoop
409     // Or, use a direct method. e.g.
410     // [screen cgDrawInto: ...];
411     }
412    
413     #include <main.h> // For #define INTFLAG_60HZ
414     #include <rom_patches.h> // For ROMVersion
415     #include "macos_util_macosx.h" // For HasMacStarted()
416    
417     - (void) tickInterrupt
418     {
419     if ( ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted() )
420     {
421     SetInterruptFlag (INTFLAG_60HZ);
422     TriggerInterrupt ();
423     }
424     }
425    
426     - (void) xPRAMbackup
427     {
428     if ( uaeCreated &&
429     memcmp(lastXPRAM, XPRAM, XPRAM_SIZE) ) // if PRAM changed from copy
430     {
431     memcpy (lastXPRAM, XPRAM, XPRAM_SIZE); // re-copy
432     SaveXPRAM (); // and save to disk
433     }
434     }
435    
436 nigel 1.2 @end