]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Added memory browser, fixed documentation, M68000 exception explanations.
[virtualjaguar] / src / gui / mainwin.cpp
1 //
2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
3 // by James Hammons
4 // (C) 2009 Underground Software
5 //
6 // JLH = James Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  12/23/2009  Created this file
11 // JLH  12/20/2010  Added settings, menus & toolbars
12 // JLH  07/05/2011  Added CD BIOS functionality to GUI
13 //
14
15 // FIXED:
16 //
17 // - Add dbl click/enter to select in cart list, ESC to dimiss [DONE]
18 // - Autoscan/autoload all available BIOS from 'software' folder [DONE]
19 // - Add 1 key jumping in cartridge list (press 'R', jumps to carts starting with 'R', etc) [DONE]
20 // - Controller configuration [DONE]
21 //
22 // STILL TO BE DONE:
23 //
24 // - Remove SDL dependencies (sound, mainly) from Jaguar core lib
25 // - Fix inconsistency with trailing slashes in paths (eeproms needs one, software doesn't)
26 //
27 // SFDX CODE: 9XF9TUHFM2359
28
29 // Uncomment this for debugging...
30 //#define DEBUG
31 //#define DEBUGFOO                      // Various tool debugging...
32 //#define DEBUGTP                               // Toolpalette debugging...
33
34 #include "mainwin.h"
35
36 #include "SDL.h"
37 #include "app.h"
38 #include "glwidget.h"
39 #include "about.h"
40 #include "help.h"
41 #include "settings.h"
42 #include "filepicker.h"
43 #include "configdialog.h"
44 #include "generaltab.h"
45 #include "version.h"
46 #include "debug/memorybrowser.h"
47
48 #include "dac.h"
49 #include "jaguar.h"
50 #include "tom.h"
51 #include "log.h"
52 #include "file.h"
53 #include "jagbios.h"
54 #include "jagcdbios.h"
55 #include "jagstub2bios.h"
56 #include "joystick.h"
57
58 // According to SebRmv, this header isn't seen on Arch Linux either... :-/
59 //#ifdef __GCCWIN32__
60 // Apparently on win32, usleep() is not pulled in by the usual suspects.
61 #include <unistd.h>
62 //#endif
63
64 // The way BSNES controls things is by setting a timer with a zero
65 // timeout, sleeping if not emulating anything. Seems there has to be a
66 // better way.
67
68 // It has a novel approach to plugging-in/using different video/audio/input
69 // methods, can we do something similar or should we just use the built-in
70 // QOpenGL?
71
72 // We're going to try to use the built-in OpenGL support and see how it goes.
73 // We'll make the VJ core modular so that it doesn't matter what GUI is in
74 // use, we can drop it in anywhere and use it as-is.
75
76 //MainWin::MainWin(QString filenameToRun): running(true), powerButtonOn(false),
77 MainWin::MainWin(bool autoRun): running(true), powerButtonOn(false),
78         showUntunedTankCircuit(true), cartridgeLoaded(false), CDActive(false),
79         //, alpineLoadSuccessful(false),
80 //      pauseForFileSelector(false), loadAndGo(false), plzDontKillMyComputer(false)
81         pauseForFileSelector(false), loadAndGo(autoRun), plzDontKillMyComputer(false)
82 {
83         for(int i=0; i<8; i++)
84                 keyHeld[i] = false;
85
86         videoWidget = new GLWidget(this);
87         setCentralWidget(videoWidget);
88         setWindowIcon(QIcon(":/res/vj-icon.png"));
89
90         QString title = QString(tr("Virtual Jaguar " VJ_RELEASE_VERSION ));
91
92         if (vjs.hardwareTypeAlpine)
93                 title += QString(tr(" - Alpine Mode"));
94
95         setWindowTitle(title);
96
97         aboutWin = new AboutWindow(this);
98         helpWin = new HelpWindow(this);
99         filePickWin = new FilePickerWindow(this);
100         memBrowseWin = new MemoryBrowserWindow(this);
101
102     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
103     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
104
105         setUnifiedTitleAndToolBarOnMac(true);
106
107         // Create actions
108
109         quitAppAct = new QAction(tr("E&xit"), this);
110 //      quitAppAct->setShortcuts(QKeySequence::Quit);
111 //      quitAppAct->setShortcut(QKeySequence(tr("Alt+x")));
112         quitAppAct->setShortcut(QKeySequence(tr("Ctrl+q")));
113         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
114         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
115
116         powerGreen.addFile(":/res/power-off.png", QSize(), QIcon::Normal, QIcon::Off);
117         powerGreen.addFile(":/res/power-on-green.png", QSize(), QIcon::Normal, QIcon::On);
118         powerRed.addFile(":/res/power-off.png", QSize(), QIcon::Normal, QIcon::Off);
119         powerRed.addFile(":/res/power-on-red.png", QSize(), QIcon::Normal, QIcon::On);
120
121 //      powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
122         powerAct = new QAction(powerGreen, tr("&Power"), this);
123         powerAct->setStatusTip(tr("Powers Jaguar on/off"));
124         powerAct->setCheckable(true);
125         powerAct->setChecked(false);
126 //      powerAct->setDisabled(true);
127         connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
128
129         QIcon pauseIcon;
130         pauseIcon.addFile(":/res/pause-off", QSize(), QIcon::Normal, QIcon::Off);
131         pauseIcon.addFile(":/res/pause-on", QSize(), QIcon::Normal, QIcon::On);
132 //      pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
133         pauseAct = new QAction(pauseIcon, tr("Pause"), this);
134         pauseAct->setStatusTip(tr("Toggles the running state"));
135         pauseAct->setCheckable(true);
136         pauseAct->setDisabled(true);
137         pauseAct->setShortcut(QKeySequence(tr("Esc")));
138         connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
139
140         zoomActs = new QActionGroup(this);
141
142         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
143         x1Act->setStatusTip(tr("Set window zoom to 100%"));
144         x1Act->setCheckable(true);
145         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
146
147         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
148         x2Act->setStatusTip(tr("Set window zoom to 200%"));
149         x2Act->setCheckable(true);
150         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
151
152         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
153         x3Act->setStatusTip(tr("Set window zoom to 300%"));
154         x3Act->setCheckable(true);
155         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
156
157         tvTypeActs = new QActionGroup(this);
158
159         ntscAct = new QAction(QIcon(":/res/ntsc.png"), tr("NTSC"), tvTypeActs);
160         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
161         ntscAct->setCheckable(true);
162         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
163
164         palAct = new QAction(QIcon(":/res/pal.png"), tr("PAL"), tvTypeActs);
165         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
166         palAct->setCheckable(true);
167         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
168
169         blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
170         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
171         blurAct->setCheckable(true);
172         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
173
174         aboutAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&About..."), this);
175         aboutAct->setStatusTip(tr("Blatant self-promotion"));
176         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
177
178         helpAct = new QAction(QIcon(":/res/vj-icon.png"), tr("&Contents..."), this);
179         helpAct->setStatusTip(tr("Help is available, if you should need it"));
180         connect(helpAct, SIGNAL(triggered()), this, SLOT(ShowHelpWin()));
181
182         filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
183         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
184         filePickAct->setShortcut(QKeySequence(tr("Ctrl+i")));
185         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
186
187         configAct = new QAction(QIcon(":/res/wrench.png"), tr("&Configure"), this);
188         configAct->setStatusTip(tr("Configure options for Virtual Jaguar"));
189         configAct->setShortcut(QKeySequence(tr("Ctrl+c")));
190         connect(configAct, SIGNAL(triggered()), this, SLOT(Configure()));
191
192         useCDAct = new QAction(QIcon(":/res/compact-disc.png"), tr("&Use CD Unit"), this);
193         useCDAct->setStatusTip(tr("Use Jaguar Virtual CD unit"));
194 //      useCDAct->setShortcut(QKeySequence(tr("Ctrl+c")));
195         useCDAct->setCheckable(true);
196         connect(useCDAct, SIGNAL(triggered()), this, SLOT(ToggleCDUsage()));
197
198         frameAdvanceAct = new QAction(QIcon(":/res/frame-advance.png"), tr("&Frame Advance"), this);
199         frameAdvanceAct->setShortcut(QKeySequence(tr("F7")));
200         connect(frameAdvanceAct, SIGNAL(triggered()), this, SLOT(FrameAdvance()));
201
202         // Debugger Actions
203         memBrowseAct = new QAction(QIcon(":/res/generic.png"), tr("Memory Browser"), this);
204         memBrowseAct->setStatusTip(tr("Shows the Jaguar memory browser window"));
205 //      memBrowseAct->setCheckable(true);
206         connect(memBrowseAct, SIGNAL(triggered()), this, SLOT(ShowMemoryBrowserWin()));
207
208         // Misc. connections...
209         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
210         connect(filePickWin, SIGNAL(FilePickerHiding()), this, SLOT(Unpause()));
211
212         // Create menus & toolbars
213
214         fileMenu = menuBar()->addMenu(tr("&Jaguar"));
215         fileMenu->addAction(powerAct);
216         fileMenu->addAction(pauseAct);
217         fileMenu->addAction(frameAdvanceAct);
218         fileMenu->addAction(filePickAct);
219         fileMenu->addAction(useCDAct);
220         fileMenu->addAction(configAct);
221         fileMenu->addAction(quitAppAct);
222
223         if (vjs.hardwareTypeAlpine)
224         {
225                 debugMenu = menuBar()->addMenu(tr("&Debug"));
226                 debugMenu->addAction(memBrowseAct);
227         }
228
229         helpMenu = menuBar()->addMenu(tr("&Help"));
230         helpMenu->addAction(helpAct);
231         helpMenu->addAction(aboutAct);
232
233         toolbar = addToolBar(tr("Stuff"));
234         toolbar->addAction(powerAct);
235         toolbar->addAction(pauseAct);
236         toolbar->addAction(filePickAct);
237         toolbar->addAction(useCDAct);
238         toolbar->addSeparator();
239         toolbar->addAction(x1Act);
240         toolbar->addAction(x2Act);
241         toolbar->addAction(x3Act);
242         toolbar->addSeparator();
243         toolbar->addAction(ntscAct);
244         toolbar->addAction(palAct);
245         toolbar->addSeparator();
246         toolbar->addAction(blurAct);
247
248         if (vjs.hardwareTypeAlpine)
249         {
250                 debugbar = addToolBar(tr("&Debug"));
251                 debugbar->addAction(memBrowseAct);
252         }
253
254         //      Create status bar
255         statusBar()->showMessage(tr("Ready"));
256
257         ReadSettings();
258
259         // Do this in case original size isn't correct (mostly for the first-run case)
260         ResizeMainWindow();
261
262         // Set up timer based loop for animation...
263         timer = new QTimer(this);
264         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
265 //This is only correct for PAL. !!! FIX !!! [DONE]
266 //Use timer->setInterval( int msec ) to fix this on the fly
267 //      timer->start(20);
268         // This isn't very accurate for NTSC: This is early by 40 msec per frame.
269         // This is because it's discarding the 0.6666... on the end of the fraction.
270         // Alas, 6 doesn't divide cleanly into 10. :-P
271         timer->start(vjs.hardwareTypeNTSC ? 16 : 20);
272
273         // We set this initially, to make VJ behave somewhat as it would if no
274         // cart were inserted and the BIOS was set as active...
275         jaguarCartInserted = true;
276         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
277         WriteLog("VJ: Initializing jaguar subsystem...\n");
278         JaguarInit();
279         memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Use the stock BIOS
280
281         // Check for filename passed in on the command line...
282 //      if (!filenameToRun.isEmpty())
283         if (autoRun)
284         {
285 //              loadAndGo = true;
286                 // Attempt to load/run the file the user passed in...
287 //              LoadSoftware(filenameToRun);
288 ////            memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Use the stock BIOS
289                 // Prevent the file scanner from running...
290                 return;
291         }
292
293         // Load up the default ROM if in Alpine mode:
294         if (vjs.hardwareTypeAlpine)
295         {
296                 bool romLoaded = JaguarLoadFile(vjs.alpineROMPath);
297
298                 // If regular load failed, try just a straight file load
299                 // (Dev only! I don't want people to start getting lazy with their releases again! :-P)
300                 if (!romLoaded)
301                         romLoaded = AlpineLoadFile(vjs.alpineROMPath);
302
303                 if (romLoaded)
304                         WriteLog("Alpine Mode: Successfully loaded file \"%s\".\n", vjs.alpineROMPath);
305                 else
306                         WriteLog("Alpine Mode: Unable to load file \"%s\"!\n", vjs.alpineROMPath);
307
308                 // Attempt to load/run the ABS file...
309                 LoadSoftware(vjs.absROMPath);
310                 memcpy(jagMemSpace + 0xE00000, jaguarDevBootROM2, 0x20000);     // Use the stub BIOS
311                 // Prevent the scanner from running...
312                 return;
313         }
314 //      else
315 //              memcpy(jagMemSpace + 0xE00000, jaguarBootROM, 0x20000); // Otherwise, use the stock BIOS
316
317         // Run the scanner if nothing passed in and *not* Alpine mode...
318         // NB: Really need to look into caching the info scanned in here...
319         filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
320 }
321
322
323 void MainWin::LoadFile(QString file)
324 {
325         LoadSoftware(file);
326 }
327
328
329 void MainWin::SyncUI(void)
330 {
331         // Set toolbar buttons/menus based on settings read in (sync the UI)...
332         blurAct->setChecked(vjs.glFilter);
333         x1Act->setChecked(zoomLevel == 1);
334         x2Act->setChecked(zoomLevel == 2);
335         x3Act->setChecked(zoomLevel == 3);
336 //      running = powerAct->isChecked();
337         ntscAct->setChecked(vjs.hardwareTypeNTSC);
338         palAct->setChecked(!vjs.hardwareTypeNTSC);
339         powerAct->setIcon(vjs.hardwareTypeNTSC ? powerRed : powerGreen);
340 }
341
342
343 void MainWin::closeEvent(QCloseEvent * event)
344 {
345         JaguarDone();
346 // This should only be done by the config dialog
347 //      WriteSettings();
348         WriteUISettings();
349         event->accept(); // ignore() if can't close for some reason
350 }
351
352
353 void MainWin::keyPressEvent(QKeyEvent * e)
354 {
355         // We ignore the Alt key for now, since it causes problems with the GUI
356         if (e->key() == Qt::Key_Alt)
357         {
358                 e->accept();
359                 return;
360         }
361
362         HandleKeys(e, true);
363 }
364
365
366 void MainWin::keyReleaseEvent(QKeyEvent * e)
367 {
368         // We ignore the Alt key for now, since it causes problems with the GUI
369         if (e->key() == Qt::Key_Alt)
370         {
371                 e->accept();
372                 return;
373         }
374
375         HandleKeys(e, false);
376 }
377
378
379 void MainWin::HandleKeys(QKeyEvent * e, bool state)
380 {
381         enum { P1LEFT = 0, P1RIGHT, P1UP, P1DOWN, P2LEFT, P2RIGHT, P2UP, P2DOWN };
382         // We kill bad key combos here, before they can get to the emulator...
383         // This also kills the illegal instruction problem that cropped up in Rayman!
384         // May want to do this by killing the old one instead of ignoring the new one...
385         // Seems to work better that way...
386
387 // The problem with this approach is that it causes bad results because it doesn't do
388 // any checking of previous states. Need to come up with something better because this
389 // causes problems where the keyboard acts as if it were unresponsive. :-P
390 #if 0
391         if ((e->key() == vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
392                 || (e->key() == vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
393                 || (e->key() == vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
394                 || (e->key() == vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U]))
395                 return;
396 #else
397 #if 0
398         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L] && joypad_0_buttons[BUTTON_R])
399                 joypad_0_buttons[BUTTON_R] = 0;
400         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R] && joypad_0_buttons[BUTTON_L])
401                 joypad_0_buttons[BUTTON_L] = 0;
402         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U] && joypad_0_buttons[BUTTON_D])
403                 joypad_0_buttons[BUTTON_D] = 0;
404         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D] && joypad_0_buttons[BUTTON_U])
405                 joypad_0_buttons[BUTTON_U] = 0;
406
407         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L] && joypad_1_buttons[BUTTON_R])
408                 joypad_1_buttons[BUTTON_R] = 0;
409         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R] && joypad_1_buttons[BUTTON_L])
410                 joypad_1_buttons[BUTTON_L] = 0;
411         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U] && joypad_1_buttons[BUTTON_D])
412                 joypad_1_buttons[BUTTON_D] = 0;
413         if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D] && joypad_1_buttons[BUTTON_U])
414                 joypad_1_buttons[BUTTON_U] = 0;
415 #else
416 //hrm, this still has sticky state problems... Ugh!
417         // First, settle key states...
418         if (e->key() == (int)vjs.p1KeyBindings[BUTTON_L])
419                 keyHeld[P1LEFT] = state;
420         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_R])
421                 keyHeld[P1RIGHT] = state;
422         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_U])
423                 keyHeld[P1UP] = state;
424         else if (e->key() == (int)vjs.p1KeyBindings[BUTTON_D])
425                 keyHeld[P1DOWN] = state;
426         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_L])
427                 keyHeld[P2LEFT] = state;
428         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_R])
429                 keyHeld[P2RIGHT] = state;
430         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_U])
431                 keyHeld[P2UP] = state;
432         else if (e->key() == (int)vjs.p2KeyBindings[BUTTON_D])
433                 keyHeld[P2DOWN] = state;
434
435         // Next, check for conflicts and bail out if there are any...
436         if ((keyHeld[P1LEFT] && keyHeld[P1RIGHT])
437                 || (keyHeld[P1UP] && keyHeld[P1DOWN])
438                 || (keyHeld[P2LEFT] && keyHeld[P2RIGHT])
439                 || (keyHeld[P2UP] && keyHeld[P2DOWN]))
440                 return;
441 #endif
442 #endif
443
444         // No bad combos exist, let's stuff the emulator key buffers...!
445         for(int i=BUTTON_FIRST; i<=BUTTON_LAST; i++)
446         {
447                 if (e->key() == (int)vjs.p1KeyBindings[i])
448 //                      joypad_0_buttons[i] = (uint8)state;
449                         joypad_0_buttons[i] = (state ? 0x01 : 0x00);
450
451 // Pad #2 is screwing up pad #1. Prolly a problem in joystick.cpp...
452 // So let's try to fix it there. :-P [DONE]
453                 if (e->key() == (int)vjs.p2KeyBindings[i])
454 //                      joypad_1_buttons[i] = (uint8)state;
455                         joypad_1_buttons[i] = (state ? 0x01 : 0x00);
456         }
457 }
458
459
460 void MainWin::Open(void)
461 {
462 }
463
464
465 void MainWin::Configure(void)
466 {
467         // Call the configuration dialog and update settings
468         ConfigDialog dlg(this);
469         //ick.
470         dlg.generalTab->useUnknownSoftware->setChecked(allowUnknownSoftware);
471
472         if (dlg.exec() == false)
473                 return;
474
475         QString before = vjs.ROMPath;
476         QString alpineBefore = vjs.alpineROMPath;
477         QString absBefore = vjs.absROMPath;
478 //      bool audioBefore = vjs.audioEnabled;
479         bool audioBefore = vjs.DSPEnabled;
480         dlg.UpdateVJSettings();
481         QString after = vjs.ROMPath;
482         QString alpineAfter = vjs.alpineROMPath;
483         QString absAfter = vjs.absROMPath;
484 //      bool audioAfter = vjs.audioEnabled;
485         bool audioAfter = vjs.DSPEnabled;
486
487         bool allowOld = allowUnknownSoftware;
488         //ick.
489         allowUnknownSoftware = dlg.generalTab->useUnknownSoftware->isChecked();
490
491         // We rescan the "software" folder if the user either changed the path or
492         // checked/unchecked the "Allow unknown files" option in the config dialog.
493         if ((before != after) || (allowOld != allowUnknownSoftware))
494                 filePickWin->ScanSoftwareFolder(allowUnknownSoftware);
495
496         // If the "Alpine" ROM is changed, then let's load it...
497         if (alpineBefore != alpineAfter)
498         {
499                 if (!JaguarLoadFile(vjs.alpineROMPath) && !AlpineLoadFile(vjs.alpineROMPath))
500                 {
501                         // Oh crap, we couldn't get the file! Alert the media!
502                         QMessageBox msg;
503                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.alpineROMPath));
504                         msg.setIcon(QMessageBox::Warning);
505                         msg.exec();
506                 }
507         }
508
509         // If the "ABS" ROM is changed, then let's load it...
510         if (absBefore != absAfter)
511         {
512                 if (!JaguarLoadFile(vjs.absROMPath))
513                 {
514                         // Oh crap, we couldn't get the file! Alert the media!
515                         QMessageBox msg;
516                         msg.setText(QString(tr("Could not load file \"%1\"!")).arg(vjs.absROMPath));
517                         msg.setIcon(QMessageBox::Warning);
518                         msg.exec();
519                 }
520         }
521
522         // If the "Enable DSP" checkbox changed, then we have to re-init the DAC,
523         // since it's running in the host audio IRQ...
524         if (audioBefore != audioAfter)
525         {
526                 DACDone();
527                 DACInit();
528         }
529
530         // Just in case we crash before a clean exit...
531         WriteSettings();
532 }
533
534
535 //
536 // Here's the main emulator loop
537 //
538 void MainWin::Timer(void)
539 {
540         if (!running)
541                 return;
542
543         if (showUntunedTankCircuit)
544         {
545                 // Some machines can't handle this, so we give them the option to disable it. :-)
546                 if (!plzDontKillMyComputer)
547                 {
548                 // Random hash & trash
549                 // We try to simulate an untuned tank circuit here... :-)
550                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
551                 {
552                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
553                         {
554                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x]
555                                         = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;
556                         }
557                 }
558                 }
559         }
560         else
561         {
562                 // Otherwise, run the Jaguar simulation
563                 JaguarExecuteNew();
564         }
565
566         videoWidget->updateGL();
567 }
568
569
570 void MainWin::TogglePowerState(void)
571 {
572         powerButtonOn = !powerButtonOn;
573         running = true;
574
575         // With the power off, we simulate white noise on the screen. :-)
576         if (!powerButtonOn)
577         {
578                 useCDAct->setDisabled(false);
579                 palAct->setDisabled(false);
580                 ntscAct->setDisabled(false);
581                 pauseAct->setChecked(false);
582                 pauseAct->setDisabled(true);
583                 showUntunedTankCircuit = true;
584                 // This is just in case the ROM we were playing was in a narrow or wide field mode,
585                 // so the untuned tank sim doesn't look wrong. :-)
586                 TOMReset();
587         }
588         else
589         {
590                 useCDAct->setDisabled(true);
591                 palAct->setDisabled(true);
592                 ntscAct->setDisabled(true);
593                 pauseAct->setChecked(false);
594                 pauseAct->setDisabled(false);
595                 showUntunedTankCircuit = false;
596
597                 // Otherwise, we prepare for running regular software...
598                 if (CDActive)
599                 {
600 // Should check for cartridgeLoaded here as well...!
601 // We can clear it when toggling CDActive on, so that when we power cycle it does the
602 // expected thing. Otherwise, if we use the file picker to insert a cart, we expect
603 // to run the cart! Maybe have a RemoveCart function that only works if the CD unit
604 // is active?
605                         setWindowTitle(QString("Virtual Jaguar " VJ_RELEASE_VERSION
606                                 " - Now playing: Jaguar CD"));
607                 }
608
609                 WriteLog("GUI: Resetting Jaguar...\n");
610                 JaguarReset();
611         }
612 }
613
614
615 void MainWin::ToggleRunState(void)
616 {
617         running = !running;
618
619         if (!running)
620         {
621                 for(uint32_t i=0; i<(uint32_t)(videoWidget->textureWidth * 256); i++)
622                 {
623                         uint32_t pixel = videoWidget->buffer[i];
624                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
625                         pixel = ((r + g + b) / 3) & 0x00FF;
626                         videoWidget->buffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
627                 }
628
629                 videoWidget->updateGL();
630         }
631 }
632
633
634 void MainWin::SetZoom100(void)
635 {
636         zoomLevel = 1;
637         ResizeMainWindow();
638 }
639
640
641 void MainWin::SetZoom200(void)
642 {
643         zoomLevel = 2;
644         ResizeMainWindow();
645 }
646
647
648 void MainWin::SetZoom300(void)
649 {
650         zoomLevel = 3;
651         ResizeMainWindow();
652 }
653
654
655 void MainWin::SetNTSC(void)
656 {
657         powerAct->setIcon(powerRed);
658         timer->setInterval(16);
659         vjs.hardwareTypeNTSC = true;
660         ResizeMainWindow();
661         WriteSettings();
662 }
663
664
665 void MainWin::SetPAL(void)
666 {
667         powerAct->setIcon(powerGreen);
668         timer->setInterval(20);
669         vjs.hardwareTypeNTSC = false;
670         ResizeMainWindow();
671         WriteSettings();
672 }
673
674
675 void MainWin::ToggleBlur(void)
676 {
677         vjs.glFilter = !vjs.glFilter;
678         WriteSettings();
679 }
680
681
682 void MainWin::ShowAboutWin(void)
683 {
684         aboutWin->show();
685 }
686
687
688 void MainWin::ShowHelpWin(void)
689 {
690         helpWin->show();
691 }
692
693
694 void MainWin::InsertCart(void)
695 {
696         // If the emulator is running, we pause it here and unpause it later
697         // if we dismiss the file selector without choosing anything
698         if (running && powerButtonOn)
699         {
700                 ToggleRunState();
701                 pauseForFileSelector = true;
702         }
703
704         filePickWin->show();
705 }
706
707
708 void MainWin::Unpause(void)
709 {
710         // Here we unpause the emulator if it was paused when we went into the file selector
711         if (pauseForFileSelector)
712         {
713                 pauseForFileSelector = false;
714
715                 // Some nutter might have unpaused while in the file selector, so check for that
716                 if (!running)
717                         ToggleRunState();
718         }
719 }
720
721
722 void MainWin::LoadSoftware(QString file)
723 {
724         running = false;                                                        // Prevent bad things(TM) from happening...
725         pauseForFileSelector = false;                           // Reset the file selector pause flag
726         SET32(jaguarMainRAM, 0, 0x00200000);            // Set top of stack...
727         cartridgeLoaded = JaguarLoadFile(file.toAscii().data());
728
729         char * biosPointer = jaguarBootROM;
730
731         if (vjs.hardwareTypeAlpine)
732                 biosPointer = jaguarDevBootROM2;
733
734         memcpy(jagMemSpace + 0xE00000, biosPointer, 0x20000);
735
736         powerAct->setDisabled(false);
737         powerAct->setChecked(true);
738         powerButtonOn = false;
739         TogglePowerState();
740
741         if (!vjs.hardwareTypeAlpine && !loadAndGo)
742         {
743                 QString newTitle = QString("Virtual Jaguar " VJ_RELEASE_VERSION " - Now playing: %1")
744                         .arg(filePickWin->GetSelectedPrettyName());
745                 setWindowTitle(newTitle);
746         }
747 }
748
749
750 void MainWin::ToggleCDUsage(void)
751 {
752         CDActive = !CDActive;
753
754 #if 0
755         if (CDActive)
756         {
757                 powerAct->setDisabled(false);
758         }
759         else
760         {
761                 powerAct->setDisabled(true);
762         }
763 #else
764         // Set up the Jaguar CD for execution, otherwise, clear memory
765         if (CDActive)
766                 memcpy(jagMemSpace + 0x800000, jaguarCDBootROM, 0x40000);
767         else
768                 memset(jagMemSpace + 0x800000, 0xFF, 0x40000);
769 #endif
770 }
771
772
773 void MainWin::FrameAdvance(void)
774 {
775 //printf("Frame Advance...\n");
776         // Execute 1 frame, then exit (only useful in Pause mode)
777         JaguarExecuteNew();
778         videoWidget->updateGL();
779 }
780
781
782 void MainWin::ShowMemoryBrowserWin(void)
783 {
784         memBrowseWin->show();
785         memBrowseWin->RefreshContents();
786 }
787
788
789 void MainWin::ResizeMainWindow(void)
790 {
791         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
792         show();
793
794         for(int i=0; i<2; i++)
795         {
796                 resize(0, 0);
797                 usleep(2000);
798                 QApplication::processEvents();
799         }
800 }
801
802
803 #warning "!!! Need to check the window geometry to see if the positions are legal !!!"
804 // i.e., someone could drag it to another screen, close it, then disconnect that screen
805 void MainWin::ReadSettings(void)
806 {
807         QSettings settings("Underground Software", "Virtual Jaguar");
808         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
809         QSize size = settings.value("size", QSize(400, 400)).toSize();
810         resize(size);
811         move(pos);
812         pos = settings.value("cartLoadPos", QPoint(200, 200)).toPoint();
813         filePickWin->move(pos);
814
815         zoomLevel = settings.value("zoom", 2).toInt();
816         allowUnknownSoftware = settings.value("showUnknownSoftware", false).toBool();
817
818         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
819         vjs.joyport          = settings.value("joyport", 0).toInt();
820         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
821         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
822         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
823         vjs.GPUEnabled       = settings.value("GPUEnabled", true).toBool();
824         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
825         vjs.audioEnabled     = settings.value("audioEnabled", true).toBool();
826         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
827         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
828         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
829         vjs.glFilter         = settings.value("glFilterType", 1).toInt();
830         vjs.renderType       = settings.value("renderType", 0).toInt();
831         vjs.allowWritesToROM = settings.value("writeROM", false).toBool();
832 //      strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
833 //      strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
834         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms/").toString().toAscii().data());
835         strcpy(vjs.ROMPath, settings.value("ROMs", "./software/").toString().toAscii().data());
836         strcpy(vjs.alpineROMPath, settings.value("DefaultROM", "").toString().toAscii().data());
837         strcpy(vjs.absROMPath, settings.value("DefaultABS", "").toString().toAscii().data());
838 WriteLog("MainWin: Paths\n");
839 //WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
840 //WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
841 WriteLog("   EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
842 WriteLog("      ROMPath = \"%s\"\n", vjs.ROMPath);
843 WriteLog("AlpineROMPath = \"%s\"\n", vjs.alpineROMPath);
844 WriteLog("   absROMPath = \"%s\"\n", vjs.absROMPath);
845 WriteLog("Pipelined DSP = %s\n", (vjs.usePipelinedDSP ? "ON" : "off"));
846
847         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
848         vjs.p1KeyBindings[BUTTON_U] = settings.value("p1k_up", Qt::Key_S).toInt();
849         vjs.p1KeyBindings[BUTTON_D] = settings.value("p1k_down", Qt::Key_X).toInt();
850         vjs.p1KeyBindings[BUTTON_L] = settings.value("p1k_left", Qt::Key_A).toInt();
851         vjs.p1KeyBindings[BUTTON_R] = settings.value("p1k_right", Qt::Key_D).toInt();
852         vjs.p1KeyBindings[BUTTON_C] = settings.value("p1k_c", Qt::Key_J).toInt();
853         vjs.p1KeyBindings[BUTTON_B] = settings.value("p1k_b", Qt::Key_K).toInt();
854         vjs.p1KeyBindings[BUTTON_A] = settings.value("p1k_a", Qt::Key_L).toInt();
855         vjs.p1KeyBindings[BUTTON_OPTION] = settings.value("p1k_option", Qt::Key_O).toInt();
856         vjs.p1KeyBindings[BUTTON_PAUSE] = settings.value("p1k_pause", Qt::Key_P).toInt();
857         vjs.p1KeyBindings[BUTTON_0] = settings.value("p1k_0", Qt::Key_0).toInt();
858         vjs.p1KeyBindings[BUTTON_1] = settings.value("p1k_1", Qt::Key_1).toInt();
859         vjs.p1KeyBindings[BUTTON_2] = settings.value("p1k_2", Qt::Key_2).toInt();
860         vjs.p1KeyBindings[BUTTON_3] = settings.value("p1k_3", Qt::Key_3).toInt();
861         vjs.p1KeyBindings[BUTTON_4] = settings.value("p1k_4", Qt::Key_4).toInt();
862         vjs.p1KeyBindings[BUTTON_5] = settings.value("p1k_5", Qt::Key_5).toInt();
863         vjs.p1KeyBindings[BUTTON_6] = settings.value("p1k_6", Qt::Key_6).toInt();
864         vjs.p1KeyBindings[BUTTON_7] = settings.value("p1k_7", Qt::Key_7).toInt();
865         vjs.p1KeyBindings[BUTTON_8] = settings.value("p1k_8", Qt::Key_8).toInt();
866         vjs.p1KeyBindings[BUTTON_9] = settings.value("p1k_9", Qt::Key_9).toInt();
867         vjs.p1KeyBindings[BUTTON_d] = settings.value("p1k_pound", Qt::Key_Minus).toInt();
868         vjs.p1KeyBindings[BUTTON_s] = settings.value("p1k_star", Qt::Key_Equal).toInt();
869
870         vjs.p2KeyBindings[BUTTON_U] = settings.value("p2k_up", Qt::Key_Up).toInt();
871         vjs.p2KeyBindings[BUTTON_D] = settings.value("p2k_down", Qt::Key_Down).toInt();
872         vjs.p2KeyBindings[BUTTON_L] = settings.value("p2k_left", Qt::Key_Left).toInt();
873         vjs.p2KeyBindings[BUTTON_R] = settings.value("p2k_right", Qt::Key_Right).toInt();
874         vjs.p2KeyBindings[BUTTON_C] = settings.value("p2k_c", Qt::Key_Z).toInt();
875         vjs.p2KeyBindings[BUTTON_B] = settings.value("p2k_b", Qt::Key_X).toInt();
876         vjs.p2KeyBindings[BUTTON_A] = settings.value("p2k_a", Qt::Key_C).toInt();
877         vjs.p2KeyBindings[BUTTON_OPTION] = settings.value("p2k_option", Qt::Key_Apostrophe).toInt();
878         vjs.p2KeyBindings[BUTTON_PAUSE] = settings.value("p2k_pause", Qt::Key_Return).toInt();
879         vjs.p2KeyBindings[BUTTON_0] = settings.value("p2k_0", Qt::Key_0).toInt();
880         vjs.p2KeyBindings[BUTTON_1] = settings.value("p2k_1", Qt::Key_1).toInt();
881         vjs.p2KeyBindings[BUTTON_2] = settings.value("p2k_2", Qt::Key_2).toInt();
882         vjs.p2KeyBindings[BUTTON_3] = settings.value("p2k_3", Qt::Key_3).toInt();
883         vjs.p2KeyBindings[BUTTON_4] = settings.value("p2k_4", Qt::Key_4).toInt();
884         vjs.p2KeyBindings[BUTTON_5] = settings.value("p2k_5", Qt::Key_5).toInt();
885         vjs.p2KeyBindings[BUTTON_6] = settings.value("p2k_6", Qt::Key_6).toInt();
886         vjs.p2KeyBindings[BUTTON_7] = settings.value("p2k_7", Qt::Key_7).toInt();
887         vjs.p2KeyBindings[BUTTON_8] = settings.value("p2k_8", Qt::Key_8).toInt();
888         vjs.p2KeyBindings[BUTTON_9] = settings.value("p2k_9", Qt::Key_9).toInt();
889         vjs.p2KeyBindings[BUTTON_d] = settings.value("p2k_pound", Qt::Key_Slash).toInt();
890         vjs.p2KeyBindings[BUTTON_s] = settings.value("p2k_star", Qt::Key_Asterisk).toInt();
891 }
892
893
894 void MainWin::WriteSettings(void)
895 {
896         QSettings settings("Underground Software", "Virtual Jaguar");
897         settings.setValue("pos", pos());
898         settings.setValue("size", size());
899         settings.setValue("cartLoadPos", filePickWin->pos());
900
901         settings.setValue("zoom", zoomLevel);
902         settings.setValue("showUnknownSoftware", allowUnknownSoftware);
903
904         settings.setValue("useJoystick", vjs.useJoystick);
905         settings.setValue("joyport", vjs.joyport);
906         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
907         settings.setValue("frameSkip", vjs.frameSkip);
908         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
909         settings.setValue("GPUEnabled", vjs.GPUEnabled);
910         settings.setValue("DSPEnabled", vjs.DSPEnabled);
911         settings.setValue("audioEnabled", vjs.audioEnabled);
912         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
913         settings.setValue("fullscreen", vjs.fullscreen);
914         settings.setValue("useOpenGL", vjs.useOpenGL);
915         settings.setValue("glFilterType", vjs.glFilter);
916         settings.setValue("renderType", vjs.renderType);
917         settings.setValue("writeROM", vjs.allowWritesToROM);
918         settings.setValue("JagBootROM", vjs.jagBootPath);
919         settings.setValue("CDBootROM", vjs.CDBootPath);
920         settings.setValue("EEPROMs", vjs.EEPROMPath);
921         settings.setValue("ROMs", vjs.ROMPath);
922         settings.setValue("DefaultROM", vjs.alpineROMPath);
923         settings.setValue("DefaultABS", vjs.absROMPath);
924
925         settings.setValue("p1k_up", vjs.p1KeyBindings[BUTTON_U]);
926         settings.setValue("p1k_down", vjs.p1KeyBindings[BUTTON_D]);
927         settings.setValue("p1k_left", vjs.p1KeyBindings[BUTTON_L]);
928         settings.setValue("p1k_right", vjs.p1KeyBindings[BUTTON_R]);
929         settings.setValue("p1k_c", vjs.p1KeyBindings[BUTTON_C]);
930         settings.setValue("p1k_b", vjs.p1KeyBindings[BUTTON_B]);
931         settings.setValue("p1k_a", vjs.p1KeyBindings[BUTTON_A]);
932         settings.setValue("p1k_option", vjs.p1KeyBindings[BUTTON_OPTION]);
933         settings.setValue("p1k_pause", vjs.p1KeyBindings[BUTTON_PAUSE]);
934         settings.setValue("p1k_0", vjs.p1KeyBindings[BUTTON_0]);
935         settings.setValue("p1k_1", vjs.p1KeyBindings[BUTTON_1]);
936         settings.setValue("p1k_2", vjs.p1KeyBindings[BUTTON_2]);
937         settings.setValue("p1k_3", vjs.p1KeyBindings[BUTTON_3]);
938         settings.setValue("p1k_4", vjs.p1KeyBindings[BUTTON_4]);
939         settings.setValue("p1k_5", vjs.p1KeyBindings[BUTTON_5]);
940         settings.setValue("p1k_6", vjs.p1KeyBindings[BUTTON_6]);
941         settings.setValue("p1k_7", vjs.p1KeyBindings[BUTTON_7]);
942         settings.setValue("p1k_8", vjs.p1KeyBindings[BUTTON_8]);
943         settings.setValue("p1k_9", vjs.p1KeyBindings[BUTTON_9]);
944         settings.setValue("p1k_pound", vjs.p1KeyBindings[BUTTON_d]);
945         settings.setValue("p1k_star", vjs.p1KeyBindings[BUTTON_s]);
946
947         settings.setValue("p2k_up", vjs.p2KeyBindings[BUTTON_U]);
948         settings.setValue("p2k_down", vjs.p2KeyBindings[BUTTON_D]);
949         settings.setValue("p2k_left", vjs.p2KeyBindings[BUTTON_L]);
950         settings.setValue("p2k_right", vjs.p2KeyBindings[BUTTON_R]);
951         settings.setValue("p2k_c", vjs.p2KeyBindings[BUTTON_C]);
952         settings.setValue("p2k_b", vjs.p2KeyBindings[BUTTON_B]);
953         settings.setValue("p2k_a", vjs.p2KeyBindings[BUTTON_A]);
954         settings.setValue("p2k_option", vjs.p2KeyBindings[BUTTON_OPTION]);
955         settings.setValue("p2k_pause", vjs.p2KeyBindings[BUTTON_PAUSE]);
956         settings.setValue("p2k_0", vjs.p2KeyBindings[BUTTON_0]);
957         settings.setValue("p2k_1", vjs.p2KeyBindings[BUTTON_1]);
958         settings.setValue("p2k_2", vjs.p2KeyBindings[BUTTON_2]);
959         settings.setValue("p2k_3", vjs.p2KeyBindings[BUTTON_3]);
960         settings.setValue("p2k_4", vjs.p2KeyBindings[BUTTON_4]);
961         settings.setValue("p2k_5", vjs.p2KeyBindings[BUTTON_5]);
962         settings.setValue("p2k_6", vjs.p2KeyBindings[BUTTON_6]);
963         settings.setValue("p2k_7", vjs.p2KeyBindings[BUTTON_7]);
964         settings.setValue("p2k_8", vjs.p2KeyBindings[BUTTON_8]);
965         settings.setValue("p2k_9", vjs.p2KeyBindings[BUTTON_9]);
966         settings.setValue("p2k_pound", vjs.p2KeyBindings[BUTTON_d]);
967         settings.setValue("p2k_star", vjs.p2KeyBindings[BUTTON_s]);
968 }
969
970
971 void MainWin::WriteUISettings(void)
972 {
973         QSettings settings("Underground Software", "Virtual Jaguar");
974         settings.setValue("pos", pos());
975         settings.setValue("size", size());
976         settings.setValue("cartLoadPos", filePickWin->pos());
977
978         settings.setValue("zoom", zoomLevel);
979 }