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