]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
Added new GUI ROM selection routines :-)
[virtualjaguar] / src / vj.cpp
1 //
2 // Virtual Jaguar Emulator
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James L. Hammons
7 //
8
9 // Added by SDLEMU (http://sdlemu.ngemu.com)
10 // Added for GCC UNIX compatibility
11 #ifdef __GCCUNIX__
12 #include <unistd.h>
13 #endif  // __GCCUNIX__
14
15 #include <dirent.h>                                     // POSIX, but should compile with linux & mingw...
16 #include <time.h>
17 #include <SDL.h>
18 #include "jaguar.h"
19 #include "crc32.h"
20 #include "unzip.h"
21 #include "gui.h"
22
23 // Uncomment this for speed control
24 //#define SPEED_CONTROL
25
26 // Private function prototypes
27
28 uint32 JaguarLoadROM(uint8 *, char *);
29 void JaguarLoadCart(uint8 *, char *);
30
31 // External variables
32
33 //These two should be local!
34 extern bool jaguar_use_bios;
35 extern bool dsp_enabled;
36
37 extern uint8 * jaguar_mainRam;
38 extern uint8 * jaguar_bootRom;
39 extern uint8 * jaguar_mainRom;
40
41 // Various paths
42
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];
48
49 SDL_Surface * surface, * mainSurface;
50 SDL_Joystick * joystick;
51 Uint32 mainSurfaceFlags = SDL_SWSURFACE;
52
53 bool finished = false;
54 bool fullscreen = false;
55 bool hardwareTypeNTSC = true;                   // Set to false for PAL
56
57 bool useJoystick = false;
58 bool showGUI = false;
59
60 int16 * backbuffer = NULL;
61
62 // Added/changed by SDLEMU http://sdlemu.ngemu.com
63
64 uint32 totalFrames;//so we can grab this from elsewhere...
65 int main(int argc, char * argv[])
66 {
67
68 /*DIR * dp = opendir("../../ROMs/");
69 if (dp == NULL)
70         printf("Could not open directory!\n");
71 else
72 {
73         dirent * de;
74         while ((de = readdir(dp)) != NULL)
75         {
76                 char * ext = strrchr(de->d_name, '.');
77                 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
78                         printf("--> %s\n", de->d_name);
79         }
80
81         closedir(dp);
82 }*/
83
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
90
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");
96
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...!
100
101         // Checking the switches ;)
102
103         for(int i=1; i<argc || argv[i]!=NULL; i++)
104         {
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!
108
109                 if (!strcmp(argv[i], "-fullscreen")) 
110                         fullscreen = true;
111
112 //We *don't* need this option!
113 /*              if (!strcmp(argv[i], "-window")) 
114 //                      console.option("windowed output");
115                         fullscreen = false;*/
116
117                 if (!strcmp(argv[i], "-joystick")) 
118                         useJoystick = true;
119
120                 if (!strcmp(argv[i], "-joyport"))
121                 {
122                         nJoyport = atoi(argv[++i]) + 1;
123                         if (nJoyport > 3)
124                                 nJoyport = 3;
125                 }
126
127                 if (!strcmp(argv[i], "-frameskip"))
128                 {
129                         nFrameskip = atoi(argv[++i]) + 1;
130                         if (nFrameskip > 10)
131                                 nFrameskip = 10;
132 #ifdef SPEED_CONTROL
133                         nFrameskip = 0;
134 #endif
135                 }
136
137                 if (!strcmp(argv[i], "-nobios"))
138                         jaguar_use_bios = false;
139
140                 if (!strcmp(argv[i], "-dspon"))
141                         dsp_enabled = 1;
142
143                 if (!strcmp(argv[i], "-pal"))
144                         hardwareTypeNTSC = false;
145
146                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
147                 {
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");
160                         return 1;
161                 }
162     }
163
164         getcwd(jaguar_boot_dir, 1024);
165         log_init("vj.log");
166         memory_init();
167         version_init();
168         version_display(log_get());
169         jaguar_init();
170
171         // Get the BIOS ROM
172         if (jaguar_use_bios)
173                 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
174
175         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
176
177         jaguar_reset();
178
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));
183
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)
186         {
187                 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
188                 exit(1);
189         }
190
191         // Let's get proper info about the platform we're running on...
192         const SDL_VideoInfo * info = SDL_GetVideoInfo();
193
194         if (!info)
195         {
196                 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
197                 exit(1);
198         }
199
200         if (info->hw_available)
201                 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
202
203         if (info->blit_hw)
204                 mainSurfaceFlags |= SDL_HWACCEL;
205
206         if (fullscreen)
207                 mainSurfaceFlags |= SDL_FULLSCREEN;
208
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);
212
213         if (mainSurface == NULL)
214         {
215                 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
216                 exit(1);
217         }
218
219         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
220
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);
224
225         if (surface == NULL)
226         {
227                 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
228                 exit(1);
229         }
230
231         // Initialize Joystick support under SDL
232         if (useJoystick)
233         {
234                 if (SDL_NumJoysticks() <= 0)
235                 {
236                         useJoystick = false;
237                         printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
238                 }
239                 else
240                 {
241                         if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
242                         {
243                                 useJoystick = false;
244                                 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
245                         }
246                         else
247                                 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
248                 }
249         }
250
251         // Get the cartridge ROM (if passed in)
252 //      if (haveCart)
253 //              JaguarLoadCart(jaguar_mainRom, argv[1]);
254         // Now with crunchy GUI goodness!
255         JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)"."));
256
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 !!!
259         jaguar_reset();
260         
261         totalFrames = 0;
262         startTime = clock();
263         nNormalLast = 0;                                                                        // Last value of timeGetTime()
264         nNormalFrac = 0;                                                                        // Extra fraction we did
265         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
266
267         while (!finished)
268         {
269 #ifdef SPEED_CONTROL
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;
274
275                 // si le nombre de trames Ã  passer + 1 est nul ou négatif,
276                 // ne rien faire pendant 2 ms
277                 if (nCount <= 0) 
278                 { 
279                         //Sleep(2); 
280                         //SDL_Delay(1);
281                 } // No need to do anything for a bit
282                 else
283                 {
284                         nNormalFrac += nCount * 10000;                          // 
285                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
286                         nNormalFrac %= 600;                                                     // 
287
288                         // Pas plus de 9 (10-1) trames non affichées 
289                         if (nCount > 10)
290                                 nCount = 10;
291                         for(int i=0; i<nCount-1; i++)
292                                 jaguar_exec(backbuffer, false);
293 #endif
294             // Setting up new backbuffer with new pixels and data
295                         JaguarExecute(backbuffer, true);
296                         totalFrames++;
297
298                         // GUI stuff here...
299                         if (showGUI)
300                         {
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);
306                         }
307
308                         // Simple frameskip
309                         if (nFrame == nFrameskip)
310                         {
311                                 if (SDL_MUSTLOCK(surface))
312                                         while (SDL_LockSurface(surface) < 0)
313                                                 SDL_Delay(10);
314
315                                 memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
316
317                                 if (SDL_MUSTLOCK(surface))
318                                         SDL_UnlockSurface(surface);
319
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);      
325                                 nFrame = 0;
326                         }
327                         else
328                                 nFrame++;
329
330                         joystick_exec();
331                         
332 #ifdef SPEED_CONTROL
333                 }
334 #endif
335         }
336
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);
340
341         jaguar_done();
342         version_done();
343         memory_done();
344         log_done();     
345
346         SDL_JoystickClose(joystick);
347         SDL_FreeSurface(surface);
348         SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
349         SDL_Quit();
350
351     return 0;
352 }
353
354 //
355 // Very very crude GUI file selector
356 //
357 uint32 DoROMFind(uint8 * mem, char * path)
358 {
359         char cartName[2048];    // Really, should be MAX_PATH or something like it
360         uint32 numFiles = 0;
361         DIR * dp = opendir(path);
362
363         if (dp == NULL)
364         {
365                 WriteLog("VJ: Could not open directory \"%s\"!\nAborting!\n", path);
366                 log_done();
367                 exit(0);
368         }
369         else
370         {
371                 dirent * de;
372                 while ((de = readdir(dp)) != NULL)
373                 {
374                         char * ext = strrchr(de->d_name, '.');
375                         if (ext != NULL)
376                                 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
377                                         numFiles++;
378                 }
379                 closedir(dp);
380         }
381
382         if (numFiles == 0)
383         {
384                 WriteLog("Found no ROM files!\nAborting!\n");
385                 log_done();
386                 exit(0);
387         }
388
389         char * names = (char *)malloc(numFiles * 2048);
390         if (names == NULL)
391         {
392                 WriteLog("Could not allocate memory for %u files!\nAborting!\n", numFiles);
393                 log_done();
394                 exit(0);
395         }
396
397         int i = 0;
398         dp = opendir(path);
399         dirent * de;
400         while ((de = readdir(dp)) != NULL)
401         {
402                 char * ext = strrchr(de->d_name, '.');
403                 if (ext != NULL)
404                         if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
405                                 strcpy(&names[i++ * 2048], de->d_name);
406         }
407         closedir(dp);
408
409         // Main GUI selection loop
410
411         uint32 cursor = 0;
412
413         if (numFiles > 1)       // Only go GUI if more than one possibility!
414         {
415         bool done = false;
416         uint32 limit = (numFiles > 24 ? 24 : numFiles);
417         SDL_Event event;
418         while (!done)
419         
420         while (SDL_PollEvent(&event))
421         {
422                 // Draw the GUI...
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++)
426                 {
427                         bool invert = (cursor == startFile + i ? true : false);
428                         char buf[41];
429                         // Guarantee that we clip our strings to fit in the screen...
430                         memcpy(buf, &names[(startFile + i) * 2048], 38);
431                         buf[38] = 0;
432                         DrawText(backbuffer, 0, i*8, invert, " %s ", buf);
433                 }
434
435
436                                 if (SDL_MUSTLOCK(surface))
437                                         while (SDL_LockSurface(surface) < 0)
438                                                 SDL_Delay(10);
439
440 //                              memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
441                                 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
442
443                                 if (SDL_MUSTLOCK(surface))
444                                         SDL_UnlockSurface(surface);
445
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);      
451
452
453                 if (event.type == SDL_KEYDOWN)
454                 {
455                         switch (event.key.keysym.sym)
456                         {
457                         case SDLK_UP:
458                                 if (cursor != 0)
459                                         cursor--;
460                                 break;
461                         case SDLK_DOWN:
462                                 if (cursor != numFiles - 1)
463                                         cursor++;
464                                 break;
465                         case SDLK_RETURN:
466                                 done = true;
467                                 break;
468                         }
469                 }
470         }
471         }
472
473         strcpy(cartName, path);
474         if (path[strlen(path) - 1] != '/')
475                 strcat(cartName, "/");
476         strcat(cartName, &names[cursor * 2048]);
477         free(names);
478
479         uint32 romSize = JaguarLoadROM(mem, cartName);
480         if (romSize == 0)
481         {
482                 log_done();
483                 exit(0);
484         }
485         return romSize;
486 }
487
488 //
489 // Generic ROM loading
490 //
491 uint32 JaguarLoadROM(uint8 * rom, char * path)
492 {
493         uint32 romSize = 0;
494
495         char * ext = strrchr(path, '.');
496         if (ext != NULL)
497         {
498                 WriteLog("VJ: Loading %s...", path);
499
500                 if (strcmpi(ext, ".zip") == 0)
501                 {
502                         // Handle ZIP file loading here...
503                         WriteLog("(ZIPped)...");
504
505                         if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
506                         {
507                                 WriteLog("Failed!\n");
508 //                              log_done();
509 //                              exit(0);
510                                 return 0;
511                         }
512                 }
513                 else
514                 {
515                         FILE * fp = fopen(path, "rb");
516
517                         if (fp == NULL)
518                         {
519                                 WriteLog("Failed!\n");
520 //                              log_done();
521 //                              exit(0);
522                                 return 0;
523                         }
524
525                         fseek(fp, 0, SEEK_END);
526                         romSize = ftell(fp);
527                         fseek(fp, 0, SEEK_SET);
528                         fread(rom, 1, romSize, fp);
529                         fclose(fp);
530                 }
531
532                 WriteLog("OK (%i bytes)\n", romSize);
533         }
534
535         return romSize;
536 }
537
538 //
539 // Jaguar cartridge ROM loading
540 //
541 void JaguarLoadCart(uint8 * mem, char * path)
542 {
543         uint32 romSize = JaguarLoadROM(mem, path);
544
545         if (romSize == 0)
546         {
547                 WriteLog("VJ: Trying GUI...\n");
548                 romSize = DoROMFind(mem, path);
549         }
550
551         jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
552         WriteLog( "CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
553         eeprom_init();
554 }