2 // Virtual Jaguar Emulator
4 // Original codebase by Cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes/enhancements by James L. Hammons and Adam Green
13 #include <dirent.h> // POSIX, but should compile with linux & mingw...
22 #include "sdlemu_opengl.h" // For testing only... ;-)
24 // Uncomment this for speed control
25 //#define SPEED_CONTROL
27 // Private function prototypes
30 uint32 JaguarLoadROM(uint8 *, char *);
31 void JaguarLoadCart(uint8 *, char *);
32 int gzfilelength(gzFile gd);
36 extern uint8 * jaguar_mainRam;
37 extern uint8 * jaguar_bootRom;
38 extern uint8 * jaguar_mainRom;
42 static char * jaguar_bootRom_path = "./bios/jagboot.rom";
43 //static char *jaguar_bootRom_path="c:/jaguarEmu/newload.img";
44 //static char *jaguar_bootRom_path="./bios/JagOS.bin";
45 char * jaguar_eeproms_path = "./eeproms/";
46 char jaguar_boot_dir[1024];
48 SDL_Surface * surface, * mainSurface;
49 int16 * backbuffer = NULL;
50 SDL_Joystick * joystick;
51 Uint32 mainSurfaceFlags = SDL_SWSURFACE;
53 bool finished = false;
54 bool fullscreen = false;
55 bool hardwareTypeNTSC = true; // Set to false for PAL
57 bool useJoystick = false;
58 int32 nJoyport = 0; // Joystick port
60 //These two should be local!
61 // And now they are! ;-)
62 bool jaguar_use_bios = false; // Default is now to USE the BIOS
63 bool dsp_enabled = false;
65 // Keybindings in order of L, R, U, D, C, B, A, Op, Pa, 0-9, #, *
66 uint16 p1KeyBindings[21] = {};
67 uint16 p2KeyBindings[21] = {};
69 bool showMessage = false;
70 uint32 showMessageTimeout;
71 char messageBuffer[200];
73 bool useOpenGL = true; // ! ;-)
76 // The main emulator loop (what else?)
78 //Maybe we should move the video stuff to TOM? Makes more sense to put it there...
79 uint32 totalFrames;//temp, so we can grab this from elsewhere...
80 int main(int argc, char * argv[])
82 uint32 startTime;//, totalFrames;//, endTime;//, w, h;
83 uint32 nNormalLast = 0;
84 int32 nNormalFrac = 0;
85 int32 nFrameskip = 0; // Default: Show every frame
86 int32 nFrame = 0; // No. of Frame
88 printf("Virtual Jaguar/SDL v1.0.6 (GCC/SDL Port)\n");
89 printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
90 printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
91 printf("Portions massaged by James L. Hammons (WIN32)\n");
92 printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
94 // BIOS is now ON by default--use the -nobios switch to turn it off!
95 jaguar_use_bios = true;
96 bool haveCart = false; // Assume there is no cartridge...!
98 // Checking the switches ;)
100 for(int i=1; i<argc || argv[i]!=NULL; i++)
102 // This would be the most likely place to do the cart loading...
103 if (argv[i][0] != '-')
104 haveCart = true; // It looks like we have a cartridge!
106 if (!strcmp(argv[i], "-fullscreen"))
109 //We *don't* need this option!
110 /* if (!strcmp(argv[i], "-window"))
111 // console.option("windowed output");
112 fullscreen = false;*/
114 if (!strcmp(argv[i], "-joystick"))
117 if (!strcmp(argv[i], "-joyport"))
119 nJoyport = atoi(argv[++i]) + 1;
124 if (!strcmp(argv[i], "-frameskip"))
126 nFrameskip = atoi(argv[++i]) + 1;
134 if (!strcmp(argv[i], "-nobios"))
135 jaguar_use_bios = false;
137 if (!strcmp(argv[i], "-dspon"))
140 if (!strcmp(argv[i], "-pal"))
141 hardwareTypeNTSC = false;
143 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
145 printf("Usage: \n\n");
146 printf("vj [romfile] [switches]\n");
147 printf(" -? or -help : Display usage and switches \n");
148 printf(" -fullscreen : Enable fullscreen mode (default: windowed)\n");
149 printf(" -frameskip 1-10 : Enable frameskip 1 - 10 (default: none) \n");
150 printf(" -joystick : Enable joystick/gamepad \n");
151 printf(" -joyport 0-3 : Select desired joystick port \n");
152 printf(" -nobios : Boot cart without using Jaguar BIOS ROM \n");
153 printf(" -dspon : Force VJ to use the DSP \n");
154 printf(" -pal : Force VJ to PAL mode (default is NTSC) \n");
155 printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
156 printf("with the Jaguar BIOS.\n");
161 getcwd(jaguar_boot_dir, 1024);
165 version_display(log_get());
170 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
172 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
174 //This is done here, so that we get valid numbers from TOM... !!! FIX !!!
177 // Set up the backbuffer
178 // int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
179 backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
180 memset(backbuffer, 0x22, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
184 // Get the cartridge ROM (if passed in)
186 // JaguarLoadCart(jaguar_mainRom, argv[1]);
187 // Now with crunchy GUI goodness!
188 JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
190 //Do this again??? Hmm... This is not very nice.
191 //Maybe it's not necessary??? Seems to be, at least for PD ROMs... !!! FIX !!!
196 nNormalLast = 0; // Last value of timeGetTime()
197 nNormalFrac = 0; // Extra fraction we did
198 nNormalLast = SDL_GetTicks(); //timeGetTime();
203 nTime = SDL_GetTicks() - nNormalLast; // calcule le temps écoulé depuis le dernier affichage
204 // nTime est en mili-secondes.
205 // détermine le nombre de trames à passer + 1
206 nCount = (nTime * 600 - nNormalFrac) / 10000;
208 // si le nombre de trames à passer + 1 est nul ou négatif,
209 // ne rien faire pendant 2 ms
214 } // No need to do anything for a bit
217 nNormalFrac += nCount * 10000; //
218 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
219 nNormalFrac %= 600; //
221 // Pas plus de 9 (10-1) trames non affichées
224 for(int i=0; i<nCount-1; i++)
225 jaguar_exec(backbuffer, false);
227 // Setting up new backbuffer with new pixels and data
228 JaguarExecute(backbuffer, true);
234 extern uint32 gpu_pc, dsp_pc;
235 DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
236 DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
240 if (nFrame == nFrameskip)
255 int elapsedTime = clock() - startTime;
256 int fps = (1000 * totalFrames) / elapsedTime;
257 fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
265 sdlemu_close_opengl();
266 SDL_JoystickClose(joystick);
267 SDL_FreeSurface(surface);
268 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
275 // Prime SDL and create surfaces
279 // Set up SDL library
280 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
282 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
286 // Get proper info about the platform we're running on...
287 const SDL_VideoInfo * info = SDL_GetVideoInfo();
291 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
297 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF | SDL_OPENGL;
298 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
302 if (info->hw_available)
303 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
306 mainSurfaceFlags |= SDL_HWACCEL;
310 mainSurfaceFlags |= SDL_FULLSCREEN;
312 // mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
313 //Testing purposes... :-)
314 mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth()*2, tom_getVideoModeHeight()*2, 16, mainSurfaceFlags);
316 if (mainSurface == NULL)
318 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
322 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
324 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
325 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
326 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
330 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
335 // sdlemu_init_opengl(surface, 1/*method*/, 1/*size*/, 0/*texture type (linear, nearest)*/);
336 //Testing purposes... :-)
337 sdlemu_init_opengl(surface, 1/*method*/, 2/*size*/, 0/*texture type (linear, nearest)*/);
339 // Initialize Joystick support under SDL
342 if (SDL_NumJoysticks() <= 0)
345 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
349 if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
352 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
355 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
361 // Generic ROM loading
363 uint32 JaguarLoadROM(uint8 * rom, char * path)
367 char * ext = strrchr(path, '.');
370 WriteLog("VJ: Loading %s...", path);
372 if (stricmp(ext, ".zip") == 0)
374 // Handle ZIP file loading here...
375 WriteLog("(ZIPped)...");
377 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
379 WriteLog("Failed!\n");
385 /* FILE * fp = fopen(path, "rb");
389 WriteLog("Failed!\n");
393 fseek(fp, 0, SEEK_END);
395 fseek(fp, 0, SEEK_SET);
396 fread(rom, 1, romSize, fp);
399 gzFile fp = gzopen(path, "rb");
403 WriteLog("Failed!\n");
407 //fseek(fp, 0, SEEK_END);
408 romSize = gzfilelength(fp);//ftell(fp);
409 gzseek(fp, 0, SEEK_SET);
410 gzread(fp, rom, romSize);
414 WriteLog("OK (%i bytes)\n", romSize);
421 // Jaguar cartridge ROM loading
423 void JaguarLoadCart(uint8 * mem, char * path)
425 uint32 romSize = JaguarLoadROM(mem, path);
430 WriteLog("VJ: Trying GUI...\n");
432 //This is not *nix friendly for some reason...
433 // if (!UserSelectFile(path, newPath))
434 if (!UserSelectFile((path == "" ? (char *)"." : path), newPath))
436 WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
441 romSize = JaguarLoadROM(mem, newPath);
445 WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
451 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
452 WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
457 // Get the length of a (possibly) gzipped file
459 int gzfilelength(gzFile gd)
461 int size = 0, length = 0;
462 unsigned char buffer[0x10000];
468 // Read in chunks until EOF
469 size = gzread(gd, buffer, 0x10000);