]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/vj.cpp
New video subsystem supports OpenGL!
[virtualjaguar] / src / vj.cpp
index 376c3e2a1e73aa4ba66585d36579b8474ba46501..32d7e3797cc20b601c05d78f12754ddc403fb291 100644 (file)
@@ -1,38 +1,38 @@
 //
 // Virtual Jaguar Emulator
 //
-// by cal2
+// Original codebase by Cal2
 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
-// Cleanups/fixes by James L. Hammons
+// Cleanups/fixes/enhancements by James L. Hammons and Adam Green
 //
 
-// Added by SDLEMU (http://sdlemu.ngemu.com)
-// Added for GCC UNIX compatibility
 #ifdef __GCCUNIX__
 #include <unistd.h>
-#endif // __GCCUNIX__
+#endif
 
+#include <dirent.h>                                                                    // POSIX, but should compile with linux & mingw...
 #include <time.h>
 #include <SDL.h>
-//#include "SDLptc.h"                  // Ick! Aargh! Bleah!
 #include "jaguar.h"
 #include "crc32.h"
+#include "zlib.h"
 #include "unzip.h"
+#include "video.h"
+#include "gui.h"
+#include "sdlemu_opengl.h"                                                     // For testing only... ;-)
 
 // Uncomment this for speed control
 //#define SPEED_CONTROL
 
 // Private function prototypes
 
+void InitSDL(void);
 uint32 JaguarLoadROM(uint8 *, char *);
 void JaguarLoadCart(uint8 *, char *);
+int gzfilelength(gzFile gd);
 
 // External variables
 
-//These two should be local!
-extern bool jaguar_use_bios;
-extern bool dsp_enabled;
-
 extern uint8 * jaguar_mainRam;
 extern uint8 * jaguar_bootRom;
 extern uint8 * jaguar_mainRom;
@@ -46,46 +46,54 @@ char * jaguar_eeproms_path = "./eeproms/";
 char jaguar_boot_dir[1024];
 
 SDL_Surface * surface, * mainSurface;
+int16 * backbuffer = NULL;
 SDL_Joystick * joystick;
 Uint32 mainSurfaceFlags = SDL_SWSURFACE;
 
-//Console console;
-//Surface * surface;
-//Format format(16, 0x007C00, 0x00003E0, 0x0000001F);
 bool finished = false;
 bool fullscreen = false;
-bool hardwareTypeNTSC = true;                  // Set to false for PAL
+bool hardwareTypeNTSC = true;                                          // Set to false for PAL
 
 bool useJoystick = false;
+int32 nJoyport = 0;                                                                    // Joystick port
+bool showGUI = false;
+//These two should be local!
+// And now they are! ;-)
+bool jaguar_use_bios = false;                                          // Default is now to USE the BIOS
+bool dsp_enabled = false;
+
+// Keybindings in order of L, R, U, D, C, B, A, Op, Pa, 0-9, #, *
+uint16 p1KeyBindings[21] = {};
+uint16 p2KeyBindings[21] = {};
 
+bool showMessage = false;
+uint32 showMessageTimeout;
+char messageBuffer[200];
 
-// Added/changed by SDLEMU http://sdlemu.ngemu.com
+bool useOpenGL = true;                                                         // ! ;-)
 
-uint32 totalFrames;//so we can grab this from elsewhere...
+//
+// The main emulator loop (what else?)
+//
+//Maybe we should move the video stuff to TOM? Makes more sense to put it there...
+uint32 totalFrames;//temp, so we can grab this from elsewhere...
 int main(int argc, char * argv[])
 {
        uint32 startTime;//, totalFrames;//, endTime;//, w, h;
-//     int32 * vs;
        uint32 nNormalLast = 0;
        int32 nNormalFrac = 0; 
-//     int32 i = 0;
-//unused       int32 nTime = 0;
-//unused       int32 nCount = 0;
     int32 nFrameskip = 0;                                                              // Default: Show every frame
     int32 nFrame = 0;                                                                  // No. of Frame
-    int32 nJoyport = 0;                                                                        // Joystick port
 
-       printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
+       printf("Virtual Jaguar/SDL v1.0.6 (GCC/SDL Port)\n");
        printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
        printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
        printf("Portions massaged by James L. Hammons (WIN32)\n");
        printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
 
-//    console.option("windowed output");
-
        // BIOS is now ON by default--use the -nobios switch to turn it off!
        jaguar_use_bios = true;
-       bool haveCart = false;                                                                  // Assume there is no cartridge...!
+       bool haveCart = false;                                                          // Assume there is no cartridge...!
 
        // Checking the switches ;)
 
@@ -96,10 +104,7 @@ int main(int argc, char * argv[])
                        haveCart = true;                                                                // It looks like we have a cartridge!
 
                if (!strcmp(argv[i], "-fullscreen")) 
-//             {
                        fullscreen = true;
-//                     console.option("fullscreen output");
-//             }
 
 //We *don't* need this option!
 /*             if (!strcmp(argv[i], "-window")) 
@@ -107,7 +112,6 @@ int main(int argc, char * argv[])
                        fullscreen = false;*/
 
                if (!strcmp(argv[i], "-joystick")) 
-//                     console.option("joystick enabled");
                        useJoystick = true;
 
                if (!strcmp(argv[i], "-joyport"))
@@ -140,14 +144,14 @@ int main(int argc, char * argv[])
                {
                    printf("Usage: \n\n");
                        printf("vj [romfile] [switches]\n");
-                       printf("  -? or -help     : Display usage and switches               \n");
-                       printf("  -fullscreen     : Enable fullscreen mode (windowed default)\n");
-                       printf("  -frameskip 1-10 : Enable frameskip 1 (default) - 10        \n");
-                       printf("  -joystick       : Enable joystick/gamepad                  \n");
-                       printf("  -joyport   0-3  : Select desired joystick port             \n");
-                       printf("  -nobios         : Boot cart without using Jaguar BIOS ROM  \n");
-                       printf("  -dspon          : Force VJ to use the DSP                  \n");
-                       printf("  -pal            : Force VJ to PAL mode (default is NTSC)   \n");
+                       printf("  -? or -help     : Display usage and switches                \n");
+                       printf("  -fullscreen     : Enable fullscreen mode (default: windowed)\n");
+                       printf("  -frameskip 1-10 : Enable frameskip 1 - 10 (default: none)   \n");
+                       printf("  -joystick       : Enable joystick/gamepad                   \n");
+                       printf("  -joyport   0-3  : Select desired joystick port              \n");
+                       printf("  -nobios         : Boot cart without using Jaguar BIOS ROM   \n");
+                       printf("  -dspon          : Force VJ to use the DSP                   \n");
+                       printf("  -pal            : Force VJ to PAL mode (default is NTSC)    \n");
                        printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
                        printf("with the Jaguar BIOS.\n");
                        return 1;
@@ -167,91 +171,26 @@ int main(int argc, char * argv[])
 
        SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
 
-       // Get the cartridge ROM (if passed in)
-       if (haveCart)
-               JaguarLoadCart(jaguar_mainRom, argv[1]);
-
+//This is done here, so that we get valid numbers from TOM... !!! FIX !!!
        jaguar_reset();
-       
-       // Set up the backbuffer
-       int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
-       memset(backbuffer, 0xAA, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
-
-       // Set up SDL library
-       if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
-       {
-               WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
-               exit(1);
-       }
-
-       // Let's get proper info about the platform we're running on.
-       const SDL_VideoInfo * info = SDL_GetVideoInfo();
-
-       if (!info)
-       {
-               WriteLog("VJ: SDL is unable to get the video query: %s\n", SDL_GetError());
-               exit(1);
-       }
-
-       if (info->hw_available)
-               mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
-
-       if (info->blit_hw)
-               mainSurfaceFlags |= SDL_HWACCEL;
-
-       if (fullscreen)
-               mainSurfaceFlags |= SDL_FULLSCREEN;
-
-       // Note: mainSurface is *never* used again!
-       //Not true--had to look at what's what here...
-       mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
 
-       if (mainSurface == NULL)
-       {
-               WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
-               exit(1);
-       }
-
-       SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
-
-       // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
-//     surface = new Surface(tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
-//Format format(16, 0x007C00, 0x00003E0, 0x0000001F);
-       surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
-               tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
-       if (surface == NULL)
-       {
-               WriteLog("VJ: Could not create primary SDL surface: %s", SDL_GetError());
-               exit(1);
-       }
+       // Set up the backbuffer
+//     int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
+       backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
+       memset(backbuffer, 0x22, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
 
-       // Initialize Joystick support under SDL
-//     if (console.JoyEnabled() == 1)
-       if (useJoystick)
-       {
-               if (SDL_NumJoysticks() <= 0)
-               {
-//             console.option("joystick disabled");
-                       useJoystick = false;
-                       printf("No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
-               }
-               else
-               {
-//                     if ((console.joystick = SDL_JoystickOpen(nJoyport)) == 0)
-                       if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
-                       {
-//                             console.option("joystick disabled");
-                               useJoystick = false;
-                               printf("Unable to open a Joystick on port: %d\n", (int)nJoyport);
-                       }
-                       else
-                               printf("Using: %s\n", SDL_JoystickName(nJoyport));
-               }
-       }
+       InitSDL();
 
-       // Open the display and start emulating some 3l337 Atari Jaguar games :P
-//     console.open("Virtual Jaguar", tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
+       // Get the cartridge ROM (if passed in)
+//     if (haveCart)
+//             JaguarLoadCart(jaguar_mainRom, argv[1]);
+       // Now with crunchy GUI goodness!
+       JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
 
+//Do this again??? Hmm... This is not very nice.
+//Maybe it's not necessary??? Seems to be, at least for PD ROMs... !!! FIX !!!
+       jaguar_reset();
+       
        totalFrames = 0;
        startTime = clock();
        nNormalLast = 0;                                                                        // Last value of timeGetTime()
@@ -286,41 +225,28 @@ int main(int argc, char * argv[])
                                jaguar_exec(backbuffer, false);
 #endif
             // Setting up new backbuffer with new pixels and data
-//                     jaguar_exec(backbuffer, true);
                        JaguarExecute(backbuffer, true);
                        totalFrames++;
 
+                       // GUI stuff here...
+                       if (showGUI)
+                       {
+                               extern uint32 gpu_pc, dsp_pc;
+                               DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
+                               DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
+                       }
+
                        // Simple frameskip
                        if (nFrame == nFrameskip)
                        {
-//                             int32 * vs = (int32 *)surface->lock();
-                               if (SDL_MUSTLOCK(surface))
-                                       while (SDL_LockSurface(surface) < 0)
-                                               SDL_Delay(10);
-//                             uint8 * vs = (Uint8 *)surface->pixels;
-//                             memcpy(vs, backbuffer, tom_width * tom_height * 2);
-                               memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
-//                             surface->unlock();
-                               if (SDL_MUSTLOCK(surface))
-                                       SDL_UnlockSurface(surface);
-//                             surface->copy(console);
-                               SDL_Rect srcrect, dstrect;
-                               srcrect.x = srcrect.y = 0, srcrect.w = surface->w, srcrect.h = surface->h;
-                               dstrect.x = dstrect.y = 0, dstrect.w = surface->w, dstrect.h = surface->h;
-//                             SDL_LowerBlit(surface, &srcrect, dst.surface, &dstrect);
-                               SDL_BlitSurface(surface, &srcrect, mainSurface, &dstrect);
-//                             dst.updates[dst.nupdates++] = dstrect;
-//                             console.update();
-                           SDL_Flip(mainSurface);      
-//                             nupdates = 0;
-
+                               RenderBackbuffer();
                                nFrame = 0;
                        }
                        else
                                nFrame++;
 
                        joystick_exec();
-                       
+
 #ifdef SPEED_CONTROL
                }
 #endif
@@ -328,13 +254,15 @@ int main(int argc, char * argv[])
 
        int elapsedTime = clock() - startTime;
        int fps = (1000 * totalFrames) / elapsedTime;
-       fprintf(log_get(), "Statistics: %i FPS\n", fps);
+       fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
 
        jaguar_done();
        version_done();
        memory_done();
        log_done();     
-//     console.close();                                                                        // Close SDL items as last!
+
+       if (useOpenGL)
+               sdlemu_close_opengl();
        SDL_JoystickClose(joystick);
        SDL_FreeSurface(surface);
        SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
@@ -344,46 +272,148 @@ int main(int argc, char * argv[])
 }
 
 //
-// Generic ROM loading
+// Prime SDL and create surfaces
 //
-uint32 JaguarLoadROM(uint8 * rom, char * path)
+void InitSDL(void)
 {
-       uint32 romSize;
+       // Set up SDL library
+       if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
+       {
+               WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
+               exit(1);
+       }
 
-       WriteLog("JagEm: Loading %s...", path);
+       // Get proper info about the platform we're running on...
+       const SDL_VideoInfo * info = SDL_GetVideoInfo();
 
-       char * ext = strrchr(path, '.');
-       if (strcmpi(ext, ".zip") == 0)
+       if (!info)
+       {
+               WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
+               exit(1);
+       }
+
+       if (useOpenGL)
+       {
+               mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_OPENGL;
+               SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+       }
+       else
        {
-               // Handle ZIP file loading here...
-               WriteLog("(ZIPped)...");
+               if (info->hw_available)
+                       mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
 
-               if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
+               if (info->blit_hw)
+                       mainSurfaceFlags |= SDL_HWACCEL;
+       }
+
+       if (fullscreen)
+               mainSurfaceFlags |= SDL_FULLSCREEN;
+
+//     mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
+//Testing purposes... :-)
+       mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth()*2, tom_getVideoModeHeight()*2, 16, mainSurfaceFlags);
+
+       if (mainSurface == NULL)
+       {
+               WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
+               exit(1);
+       }
+
+       SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
+
+       // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
+       surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
+               tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
+
+       if (surface == NULL)
+       {
+               WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
+               exit(1);
+       }
+
+       if (useOpenGL)
+//             sdlemu_init_opengl(surface, 1/*method*/, 1/*size*/, 0/*texture type (linear, nearest)*/);
+//Testing purposes... :-)
+               sdlemu_init_opengl(surface, 1/*method*/, 2/*size*/, 0/*texture type (linear, nearest)*/);
+
+       // Initialize Joystick support under SDL
+       if (useJoystick)
+       {
+               if (SDL_NumJoysticks() <= 0)
                {
-                       WriteLog("Failed!\n");
-                       log_done();
-                       exit(0);
+                       useJoystick = false;
+                       printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
+               }
+               else
+               {
+                       if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
+                       {
+                               useJoystick = false;
+                               printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
+                       }
+                       else
+                               printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
                }
        }
-       else
+}
+
+//
+// Generic ROM loading
+//
+uint32 JaguarLoadROM(uint8 * rom, char * path)
+{
+       uint32 romSize = 0;
+
+       char * ext = strrchr(path, '.');
+       if (ext != NULL)
        {
-               FILE * fp = fopen(path, "rb");
+               WriteLog("VJ: Loading %s...", path);
 
-               if (fp == NULL)
+               if (stricmp(ext, ".zip") == 0)
                {
-                       WriteLog("Failed!\n");
-                       log_done();
-                       exit(0);
+                       // Handle ZIP file loading here...
+                       WriteLog("(ZIPped)...");
+
+                       if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
+                       {
+                               WriteLog("Failed!\n");
+                               return 0;
+                       }
                }
+               else
+               {
+/*                     FILE * fp = fopen(path, "rb");
 
-               fseek(fp, 0, SEEK_END);
-               romSize = ftell(fp);
-               fseek(fp, 0, SEEK_SET);
-               fread(rom, 1, romSize, fp);
-               fclose(fp);
+                       if (fp == NULL)
+                       {
+                               WriteLog("Failed!\n");
+                               return 0;
+                       }
+
+                       fseek(fp, 0, SEEK_END);
+                       romSize = ftell(fp);
+                       fseek(fp, 0, SEEK_SET);
+                       fread(rom, 1, romSize, fp);
+                       fclose(fp);*/
+
+                       gzFile fp = gzopen(path, "rb");
+
+                       if (fp == NULL)
+                       {
+                               WriteLog("Failed!\n");
+                               return 0;
+                       }
+
+                       //fseek(fp, 0, SEEK_END);
+                       romSize = gzfilelength(fp);//ftell(fp);
+                       gzseek(fp, 0, SEEK_SET);
+                       gzread(fp, rom, romSize);
+                       gzclose(fp);
+               }
+
+               WriteLog("OK (%i bytes)\n", romSize);
        }
 
-       WriteLog("OK (%i bytes)\n", romSize);
        return romSize;
 }
 
@@ -392,8 +422,59 @@ uint32 JaguarLoadROM(uint8 * rom, char * path)
 //
 void JaguarLoadCart(uint8 * mem, char * path)
 {
-       uint32 romsize = JaguarLoadROM(mem, path);
-       jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romsize);
-       WriteLog( "CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
+       uint32 romSize = JaguarLoadROM(mem, path);
+
+       if (romSize == 0)
+       {
+               char newPath[2048];
+               WriteLog("VJ: Trying GUI...\n");
+
+//This is not *nix friendly for some reason...
+//             if (!UserSelectFile(path, newPath))
+               if (!UserSelectFile((path == "" ? (char *)"." : path), newPath))
+               {
+                       WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
+                       log_done();
+                       exit(0);
+               }
+
+               romSize = JaguarLoadROM(mem, newPath);
+
+               if (romSize == 0)
+               {
+                       WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
+                       log_done();
+                       exit(0);
+               }
+       }
+
+       jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
+       WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
        eeprom_init();
 }
+
+//
+// Get the length of a (possibly) gzipped file
+//
+int gzfilelength(gzFile gd)
+{
+   int size = 0, length = 0;
+   unsigned char buffer[0x10000];
+
+   gzrewind(gd);
+
+   do
+   {
+      // Read in chunks until EOF
+      size = gzread(gd, buffer, 0x10000);
+
+      if (size <= 0)
+       break;
+
+      length += size;
+   }
+   while (!gzeof(gd));
+
+   gzrewind(gd);
+   return length;
+}