]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
9dbfe023f6077b66a4129694e70e331b53fda5f0
[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 int16 * backbuffer = NULL;
51 SDL_Joystick * joystick;
52 Uint32 mainSurfaceFlags = SDL_SWSURFACE;
53
54 bool finished = false;
55 bool fullscreen = false;
56 bool hardwareTypeNTSC = true;                   // Set to false for PAL
57
58 bool useJoystick = false;
59 bool showGUI = false;
60
61 // Added/changed by SDLEMU http://sdlemu.ngemu.com
62
63 uint32 totalFrames;//temp, so we can grab this from elsewhere...
64 int main(int argc, char * argv[])
65 {
66         uint32 startTime;//, totalFrames;//, endTime;//, w, h;
67         uint32 nNormalLast = 0;
68         int32 nNormalFrac = 0; 
69     int32 nFrameskip = 0;                                                               // Default: Show every frame
70     int32 nFrame = 0;                                                                   // No. of Frame
71     int32 nJoyport = 0;                                                                 // Joystick port
72
73         printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
74         printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
75         printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
76         printf("Portions massaged by James L. Hammons (WIN32)\n");
77         printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
78
79         // BIOS is now ON by default--use the -nobios switch to turn it off!
80         jaguar_use_bios = true;
81         bool haveCart = false;                                                                  // Assume there is no cartridge...!
82
83         // Checking the switches ;)
84
85         for(int i=1; i<argc || argv[i]!=NULL; i++)
86         {
87                 // This would be the most likely place to do the cart loading...
88                 if (argv[i][0] != '-')
89                         haveCart = true;                                                                // It looks like we have a cartridge!
90
91                 if (!strcmp(argv[i], "-fullscreen")) 
92                         fullscreen = true;
93
94 //We *don't* need this option!
95 /*              if (!strcmp(argv[i], "-window")) 
96 //                      console.option("windowed output");
97                         fullscreen = false;*/
98
99                 if (!strcmp(argv[i], "-joystick")) 
100                         useJoystick = true;
101
102                 if (!strcmp(argv[i], "-joyport"))
103                 {
104                         nJoyport = atoi(argv[++i]) + 1;
105                         if (nJoyport > 3)
106                                 nJoyport = 3;
107                 }
108
109                 if (!strcmp(argv[i], "-frameskip"))
110                 {
111                         nFrameskip = atoi(argv[++i]) + 1;
112                         if (nFrameskip > 10)
113                                 nFrameskip = 10;
114 #ifdef SPEED_CONTROL
115                         nFrameskip = 0;
116 #endif
117                 }
118
119                 if (!strcmp(argv[i], "-nobios"))
120                         jaguar_use_bios = false;
121
122                 if (!strcmp(argv[i], "-dspon"))
123                         dsp_enabled = 1;
124
125                 if (!strcmp(argv[i], "-pal"))
126                         hardwareTypeNTSC = false;
127
128                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
129                 {
130                     printf("Usage: \n\n");
131                         printf("vj [romfile] [switches]\n");
132                         printf("  -? or -help     : Display usage and switches               \n");
133                         printf("  -fullscreen     : Enable fullscreen mode (windowed default)\n");
134                         printf("  -frameskip 1-10 : Enable frameskip 1 (default) - 10        \n");
135                         printf("  -joystick       : Enable joystick/gamepad                  \n");
136                         printf("  -joyport   0-3  : Select desired joystick port             \n");
137                         printf("  -nobios         : Boot cart without using Jaguar BIOS ROM  \n");
138                         printf("  -dspon          : Force VJ to use the DSP                  \n");
139                         printf("  -pal            : Force VJ to PAL mode (default is NTSC)   \n");
140                         printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
141                         printf("with the Jaguar BIOS.\n");
142                         return 1;
143                 }
144     }
145
146         getcwd(jaguar_boot_dir, 1024);
147         log_init("vj.log");
148         memory_init();
149         version_init();
150         version_display(log_get());
151         jaguar_init();
152
153         // Get the BIOS ROM
154         if (jaguar_use_bios)
155                 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
156
157         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
158
159         jaguar_reset();
160
161         // Set up the backbuffer
162 //      int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
163         backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
164         memset(backbuffer, 0x22, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
165
166         // Set up SDL library
167         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
168         {
169                 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
170                 exit(1);
171         }
172
173         // Let's get proper info about the platform we're running on...
174         const SDL_VideoInfo * info = SDL_GetVideoInfo();
175
176         if (!info)
177         {
178                 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
179                 exit(1);
180         }
181
182         if (info->hw_available)
183                 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
184
185         if (info->blit_hw)
186                 mainSurfaceFlags |= SDL_HWACCEL;
187
188         if (fullscreen)
189                 mainSurfaceFlags |= SDL_FULLSCREEN;
190
191         // Note: mainSurface is *never* used again!
192         //Not true--had to look at what's what here... It's the primary surface...
193         mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
194
195         if (mainSurface == NULL)
196         {
197                 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
198                 exit(1);
199         }
200
201         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
202
203         // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
204         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
205                 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
206
207         if (surface == NULL)
208         {
209                 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
210                 exit(1);
211         }
212
213         // Initialize Joystick support under SDL
214         if (useJoystick)
215         {
216                 if (SDL_NumJoysticks() <= 0)
217                 {
218                         useJoystick = false;
219                         printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
220                 }
221                 else
222                 {
223                         if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
224                         {
225                                 useJoystick = false;
226                                 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
227                         }
228                         else
229                                 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
230                 }
231         }
232
233         // Get the cartridge ROM (if passed in)
234 //      if (haveCart)
235 //              JaguarLoadCart(jaguar_mainRom, argv[1]);
236         // Now with crunchy GUI goodness!
237         JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)"."));
238
239 //Do this again??? Hmm... This is not very nice.
240 //Maybe it's not necessary??? Seems to be, at least for PD ROMs... !!! FIX !!!
241         jaguar_reset();
242         
243         totalFrames = 0;
244         startTime = clock();
245         nNormalLast = 0;                                                                        // Last value of timeGetTime()
246         nNormalFrac = 0;                                                                        // Extra fraction we did
247         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
248
249         while (!finished)
250         {
251 #ifdef SPEED_CONTROL
252                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
253                                                                                                                 // nTime est en mili-secondes.
254                 // détermine le nombre de trames à passer + 1
255                 nCount = (nTime * 600 - nNormalFrac) / 10000;
256
257                 // si le nombre de trames à passer + 1 est nul ou négatif,
258                 // ne rien faire pendant 2 ms
259                 if (nCount <= 0) 
260                 { 
261                         //Sleep(2); 
262                         //SDL_Delay(1);
263                 } // No need to do anything for a bit
264                 else
265                 {
266                         nNormalFrac += nCount * 10000;                          // 
267                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
268                         nNormalFrac %= 600;                                                     // 
269
270                         // Pas plus de 9 (10-1) trames non affichées 
271                         if (nCount > 10)
272                                 nCount = 10;
273                         for(int i=0; i<nCount-1; i++)
274                                 jaguar_exec(backbuffer, false);
275 #endif
276             // Setting up new backbuffer with new pixels and data
277                         JaguarExecute(backbuffer, true);
278                         totalFrames++;
279
280                         // GUI stuff here...
281                         if (showGUI)
282                         {
283                                 extern uint32 gpu_pc, dsp_pc;
284                                 DrawText(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
285                                 DrawText(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
286                         }
287
288                         // Simple frameskip
289                         if (nFrame == nFrameskip)
290                         {
291                                 BlitBackbuffer();
292                                 nFrame = 0;
293                         }
294                         else
295                                 nFrame++;
296
297                         joystick_exec();
298                         
299 #ifdef SPEED_CONTROL
300                 }
301 #endif
302         }
303
304         int elapsedTime = clock() - startTime;
305         int fps = (1000 * totalFrames) / elapsedTime;
306         fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
307
308         jaguar_done();
309         version_done();
310         memory_done();
311         log_done();     
312
313         SDL_JoystickClose(joystick);
314         SDL_FreeSurface(surface);
315         SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
316         SDL_Quit();
317
318     return 0;
319 }
320
321 //
322 // Generic ROM loading
323 //
324 uint32 JaguarLoadROM(uint8 * rom, char * path)
325 {
326         uint32 romSize = 0;
327
328         char * ext = strrchr(path, '.');
329         if (ext != NULL)
330         {
331                 WriteLog("VJ: Loading %s...", path);
332
333                 if (strcmpi(ext, ".zip") == 0)
334                 {
335                         // Handle ZIP file loading here...
336                         WriteLog("(ZIPped)...");
337
338                         if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
339                         {
340                                 WriteLog("Failed!\n");
341                                 return 0;
342                         }
343                 }
344                 else
345                 {
346                         FILE * fp = fopen(path, "rb");
347
348                         if (fp == NULL)
349                         {
350                                 WriteLog("Failed!\n");
351                                 return 0;
352                         }
353
354                         fseek(fp, 0, SEEK_END);
355                         romSize = ftell(fp);
356                         fseek(fp, 0, SEEK_SET);
357                         fread(rom, 1, romSize, fp);
358                         fclose(fp);
359                 }
360
361                 WriteLog("OK (%i bytes)\n", romSize);
362         }
363
364         return romSize;
365 }
366
367 //
368 // Jaguar cartridge ROM loading
369 //
370 void JaguarLoadCart(uint8 * mem, char * path)
371 {
372         uint32 romSize = JaguarLoadROM(mem, path);
373
374         if (romSize == 0)
375         {
376                 char newPath[2048];
377                 WriteLog("VJ: Trying GUI...\n");
378
379                 if (!UserSelectFile(path, newPath))
380                 {
381                         WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
382                         log_done();
383                         exit(0);
384                 }
385
386                 romSize = JaguarLoadROM(mem, newPath);
387
388                 if (romSize == 0)
389                 {
390                         WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
391                         log_done();
392                         exit(0);
393                 }
394         }
395
396         jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
397         WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
398         eeprom_init();
399 }