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