2 // Virtual Jaguar Emulator
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James L. Hammons
9 // Added by SDLEMU (http://sdlemu.ngemu.com)
10 // Added for GCC UNIX compatibility
15 #include <dirent.h> // POSIX, but should compile with linux & mingw...
23 // Uncomment this for speed control
24 //#define SPEED_CONTROL
26 // Private function prototypes
28 uint32 JaguarLoadROM(uint8 *, char *);
29 void JaguarLoadCart(uint8 *, char *);
33 //These two should be local!
34 extern bool jaguar_use_bios;
35 extern bool dsp_enabled;
37 extern uint8 * jaguar_mainRam;
38 extern uint8 * jaguar_bootRom;
39 extern uint8 * jaguar_mainRom;
43 static char * jaguar_bootRom_path = "./bios/jagboot.rom";
44 //static char *jaguar_bootRom_path="c:/jaguarEmu/newload.img";
45 //static char *jaguar_bootRom_path="./bios/JagOS.bin";
46 char * jaguar_eeproms_path = "./eeproms/";
47 char jaguar_boot_dir[1024];
49 SDL_Surface * surface, * mainSurface;
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;
60 int16 * backbuffer = NULL;
62 // Added/changed by SDLEMU http://sdlemu.ngemu.com
64 uint32 totalFrames;//so we can grab this from elsewhere...
65 int main(int argc, char * argv[])
68 /*DIR * dp = opendir("../../ROMs/");
70 printf("Could not open directory!\n");
74 while ((de = readdir(dp)) != NULL)
76 char * ext = strrchr(de->d_name, '.');
77 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
78 printf("--> %s\n", de->d_name);
84 uint32 startTime;//, totalFrames;//, endTime;//, w, h;
85 uint32 nNormalLast = 0;
86 int32 nNormalFrac = 0;
87 int32 nFrameskip = 0; // Default: Show every frame
88 int32 nFrame = 0; // No. of Frame
89 int32 nJoyport = 0; // Joystick port
91 printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
92 printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
93 printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
94 printf("Portions massaged by James L. Hammons (WIN32)\n");
95 printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
97 // BIOS is now ON by default--use the -nobios switch to turn it off!
98 jaguar_use_bios = true;
99 bool haveCart = false; // Assume there is no cartridge...!
101 // Checking the switches ;)
103 for(int i=1; i<argc || argv[i]!=NULL; i++)
105 // This would be the most likely place to do the cart loading...
106 if (argv[i][0] != '-')
107 haveCart = true; // It looks like we have a cartridge!
109 if (!strcmp(argv[i], "-fullscreen"))
112 //We *don't* need this option!
113 /* if (!strcmp(argv[i], "-window"))
114 // console.option("windowed output");
115 fullscreen = false;*/
117 if (!strcmp(argv[i], "-joystick"))
120 if (!strcmp(argv[i], "-joyport"))
122 nJoyport = atoi(argv[++i]) + 1;
127 if (!strcmp(argv[i], "-frameskip"))
129 nFrameskip = atoi(argv[++i]) + 1;
137 if (!strcmp(argv[i], "-nobios"))
138 jaguar_use_bios = false;
140 if (!strcmp(argv[i], "-dspon"))
143 if (!strcmp(argv[i], "-pal"))
144 hardwareTypeNTSC = false;
146 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
148 printf("Usage: \n\n");
149 printf("vj [romfile] [switches]\n");
150 printf(" -? or -help : Display usage and switches \n");
151 printf(" -fullscreen : Enable fullscreen mode (windowed default)\n");
152 printf(" -frameskip 1-10 : Enable frameskip 1 (default) - 10 \n");
153 printf(" -joystick : Enable joystick/gamepad \n");
154 printf(" -joyport 0-3 : Select desired joystick port \n");
155 printf(" -nobios : Boot cart without using Jaguar BIOS ROM \n");
156 printf(" -dspon : Force VJ to use the DSP \n");
157 printf(" -pal : Force VJ to PAL mode (default is NTSC) \n");
158 printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
159 printf("with the Jaguar BIOS.\n");
164 getcwd(jaguar_boot_dir, 1024);
168 version_display(log_get());
173 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
175 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
179 // Set up the backbuffer
180 // int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
181 backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
182 memset(backbuffer, 0x22, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
184 // Set up SDL library
185 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
187 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
191 // Let's get proper info about the platform we're running on...
192 const SDL_VideoInfo * info = SDL_GetVideoInfo();
196 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
200 if (info->hw_available)
201 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
204 mainSurfaceFlags |= SDL_HWACCEL;
207 mainSurfaceFlags |= SDL_FULLSCREEN;
209 // Note: mainSurface is *never* used again!
210 //Not true--had to look at what's what here... It's the primary surface...
211 mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
213 if (mainSurface == NULL)
215 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
219 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
221 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
222 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
223 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
227 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
231 // Initialize Joystick support under SDL
234 if (SDL_NumJoysticks() <= 0)
237 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
241 if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
244 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
247 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
251 // Get the cartridge ROM (if passed in)
253 // JaguarLoadCart(jaguar_mainRom, argv[1]);
254 // Now with crunchy GUI goodness!
255 JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)"."));
257 //Do this again??? Hmm... This is not very nice.
258 //Maybe it's not necessary??? Seems to be, at least for PD ROMs... !!! FIX !!!
263 nNormalLast = 0; // Last value of timeGetTime()
264 nNormalFrac = 0; // Extra fraction we did
265 nNormalLast = SDL_GetTicks(); //timeGetTime();
270 nTime = SDL_GetTicks() - nNormalLast; // calcule le temps écoulé depuis le dernier affichage
271 // nTime est en mili-secondes.
272 // détermine le nombre de trames à passer + 1
273 nCount = (nTime * 600 - nNormalFrac) / 10000;
275 // si le nombre de trames à passer + 1 est nul ou négatif,
276 // ne rien faire pendant 2 ms
281 } // No need to do anything for a bit
284 nNormalFrac += nCount * 10000; //
285 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
286 nNormalFrac %= 600; //
288 // Pas plus de 9 (10-1) trames non affichées
291 for(int i=0; i<nCount-1; i++)
292 jaguar_exec(backbuffer, false);
294 // Setting up new backbuffer with new pixels and data
295 JaguarExecute(backbuffer, true);
301 extern uint32 gpu_pc;
302 extern uint32 dsp_pc;
303 DrawText(backbuffer, 8, 0, true, "Friendly GUI brought to you by JLH ;-)");
304 DrawText(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
305 DrawText(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
309 if (nFrame == nFrameskip)
311 if (SDL_MUSTLOCK(surface))
312 while (SDL_LockSurface(surface) < 0)
315 memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
317 if (SDL_MUSTLOCK(surface))
318 SDL_UnlockSurface(surface);
320 SDL_Rect srcrect, dstrect;
321 srcrect.x = srcrect.y = 0, srcrect.w = surface->w, srcrect.h = surface->h;
322 dstrect.x = dstrect.y = 0, dstrect.w = surface->w, dstrect.h = surface->h;
323 SDL_BlitSurface(surface, &srcrect, mainSurface, &dstrect);
324 SDL_Flip(mainSurface);
337 int elapsedTime = clock() - startTime;
338 int fps = (1000 * totalFrames) / elapsedTime;
339 fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
346 SDL_JoystickClose(joystick);
347 SDL_FreeSurface(surface);
348 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
355 // Very very crude GUI file selector
357 uint32 DoROMFind(uint8 * mem, char * path)
359 char cartName[2048]; // Really, should be MAX_PATH or something like it
361 DIR * dp = opendir(path);
365 WriteLog("VJ: Could not open directory \"%s\"!\nAborting!\n", path);
372 while ((de = readdir(dp)) != NULL)
374 char * ext = strrchr(de->d_name, '.');
376 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
384 WriteLog("Found no ROM files!\nAborting!\n");
389 char * names = (char *)malloc(numFiles * 2048);
392 WriteLog("Could not allocate memory for %u files!\nAborting!\n", numFiles);
400 while ((de = readdir(dp)) != NULL)
402 char * ext = strrchr(de->d_name, '.');
404 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
405 strcpy(&names[i++ * 2048], de->d_name);
409 // Main GUI selection loop
413 if (numFiles > 1) // Only go GUI if more than one possibility!
416 uint32 limit = (numFiles > 24 ? 24 : numFiles);
420 while (SDL_PollEvent(&event))
423 memset(backbuffer, 0x11, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
424 uint32 startFile = (cursor >= limit ? cursor - limit + 1 : 0);
425 for(uint32 i=0; i<limit; i++)
427 bool invert = (cursor == startFile + i ? true : false);
429 // Guarantee that we clip our strings to fit in the screen...
430 memcpy(buf, &names[(startFile + i) * 2048], 38);
432 DrawText(backbuffer, 0, i*8, invert, " %s ", buf);
436 if (SDL_MUSTLOCK(surface))
437 while (SDL_LockSurface(surface) < 0)
440 // memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
441 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
443 if (SDL_MUSTLOCK(surface))
444 SDL_UnlockSurface(surface);
446 SDL_Rect srcrect, dstrect;
447 srcrect.x = srcrect.y = 0, srcrect.w = surface->w, srcrect.h = surface->h;
448 dstrect.x = dstrect.y = 0, dstrect.w = surface->w, dstrect.h = surface->h;
449 SDL_BlitSurface(surface, &srcrect, mainSurface, &dstrect);
450 SDL_Flip(mainSurface);
453 if (event.type == SDL_KEYDOWN)
455 switch (event.key.keysym.sym)
462 if (cursor != numFiles - 1)
473 strcpy(cartName, path);
474 if (path[strlen(path) - 1] != '/')
475 strcat(cartName, "/");
476 strcat(cartName, &names[cursor * 2048]);
479 uint32 romSize = JaguarLoadROM(mem, cartName);
489 // Generic ROM loading
491 uint32 JaguarLoadROM(uint8 * rom, char * path)
495 char * ext = strrchr(path, '.');
498 WriteLog("VJ: Loading %s...", path);
500 if (strcmpi(ext, ".zip") == 0)
502 // Handle ZIP file loading here...
503 WriteLog("(ZIPped)...");
505 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
507 WriteLog("Failed!\n");
515 FILE * fp = fopen(path, "rb");
519 WriteLog("Failed!\n");
525 fseek(fp, 0, SEEK_END);
527 fseek(fp, 0, SEEK_SET);
528 fread(rom, 1, romSize, fp);
532 WriteLog("OK (%i bytes)\n", romSize);
539 // Jaguar cartridge ROM loading
541 void JaguarLoadCart(uint8 * mem, char * path)
543 uint32 romSize = JaguarLoadROM(mem, path);
547 WriteLog("VJ: Trying GUI...\n");
548 romSize = DoROMFind(mem, path);
551 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
552 WriteLog( "CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);