]> Shamusworld >> Repos - virtualjaguar/blob - vj.cpp
132c7a705e06745c6203d7e72ef5f6ec64166446
[virtualjaguar] / 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 //This is done here, so that we get valid numbers from TOM... !!! FIX !!!
160         jaguar_reset();
161
162         // Set up the backbuffer
163 //      int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
164         backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
165         memset(backbuffer, 0x22, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
166
167         // Set up SDL library
168         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
169         {
170                 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
171                 exit(1);
172         }
173
174         // Let's get proper info about the platform we're running on...
175         const SDL_VideoInfo * info = SDL_GetVideoInfo();
176
177         if (!info)
178         {
179                 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
180                 exit(1);
181         }
182
183         if (info->hw_available)
184                 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
185
186         if (info->blit_hw)
187                 mainSurfaceFlags |= SDL_HWACCEL;
188
189         if (fullscreen)
190                 mainSurfaceFlags |= SDL_FULLSCREEN;
191
192         mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
193
194         if (mainSurface == NULL)
195         {
196                 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
197                 exit(1);
198         }
199
200         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
201
202         // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
203         surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
204                 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
205
206         if (surface == NULL)
207         {
208                 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
209                 exit(1);
210         }
211
212         // Initialize Joystick support under SDL
213         if (useJoystick)
214         {
215                 if (SDL_NumJoysticks() <= 0)
216                 {
217                         useJoystick = false;
218                         printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
219                 }
220                 else
221                 {
222                         if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
223                         {
224                                 useJoystick = false;
225                                 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
226                         }
227                         else
228                                 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
229                 }
230         }
231
232         // Get the cartridge ROM (if passed in)
233 //      if (haveCart)
234 //              JaguarLoadCart(jaguar_mainRom, argv[1]);
235         // Now with crunchy GUI goodness!
236         JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
237
238 //Do this again??? Hmm... This is not very nice.
239 //Maybe it's not necessary??? Seems to be, at least for PD ROMs... !!! FIX !!!
240         jaguar_reset();
241         
242         totalFrames = 0;
243         startTime = clock();
244         nNormalLast = 0;                                                                        // Last value of timeGetTime()
245         nNormalFrac = 0;                                                                        // Extra fraction we did
246         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
247
248         while (!finished)
249         {
250 #ifdef SPEED_CONTROL
251                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
252                                                                                                                 // nTime est en mili-secondes.
253                 // détermine le nombre de trames à passer + 1
254                 nCount = (nTime * 600 - nNormalFrac) / 10000;
255
256                 // si le nombre de trames à passer + 1 est nul ou négatif,
257                 // ne rien faire pendant 2 ms
258                 if (nCount <= 0) 
259                 { 
260                         //Sleep(2); 
261                         //SDL_Delay(1);
262                 } // No need to do anything for a bit
263                 else
264                 {
265                         nNormalFrac += nCount * 10000;                          // 
266                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
267                         nNormalFrac %= 600;                                                     // 
268
269                         // Pas plus de 9 (10-1) trames non affichées 
270                         if (nCount > 10)
271                                 nCount = 10;
272                         for(int i=0; i<nCount-1; i++)
273                                 jaguar_exec(backbuffer, false);
274 #endif
275             // Setting up new backbuffer with new pixels and data
276                         JaguarExecute(backbuffer, true);
277                         totalFrames++;
278
279                         // GUI stuff here...
280                         if (showGUI)
281                         {
282                                 extern uint32 gpu_pc, dsp_pc;
283                                 DrawText(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
284                                 DrawText(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
285                         }
286
287                         // Simple frameskip
288                         if (nFrame == nFrameskip)
289                         {
290                                 BlitBackbuffer();
291                                 nFrame = 0;
292                         }
293                         else
294                                 nFrame++;
295
296                         joystick_exec();
297                         
298 #ifdef SPEED_CONTROL
299                 }
300 #endif
301         }
302
303         int elapsedTime = clock() - startTime;
304         int fps = (1000 * totalFrames) / elapsedTime;
305         fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
306
307         jaguar_done();
308         version_done();
309         memory_done();
310         log_done();     
311
312         SDL_JoystickClose(joystick);
313         SDL_FreeSurface(surface);
314         SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
315         SDL_Quit();
316
317     return 0;
318 }
319
320 //
321 // Generic ROM loading
322 //
323 uint32 JaguarLoadROM(uint8 * rom, char * path)
324 {
325         uint32 romSize = 0;
326
327         char * ext = strrchr(path, '.');
328         if (ext != NULL)
329         {
330                 WriteLog("VJ: Loading %s...", path);
331
332                 if (stricmp(ext, ".zip") == 0)
333                 {
334                         // Handle ZIP file loading here...
335                         WriteLog("(ZIPped)...");
336
337                         if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
338                         {
339                                 WriteLog("Failed!\n");
340                                 return 0;
341                         }
342                 }
343                 else
344                 {
345                         FILE * fp = fopen(path, "rb");
346
347                         if (fp == NULL)
348                         {
349                                 WriteLog("Failed!\n");
350                                 return 0;
351                         }
352
353                         fseek(fp, 0, SEEK_END);
354                         romSize = ftell(fp);
355                         fseek(fp, 0, SEEK_SET);
356                         fread(rom, 1, romSize, fp);
357                         fclose(fp);
358                 }
359
360                 WriteLog("OK (%i bytes)\n", romSize);
361         }
362
363         return romSize;
364 }
365
366 //
367 // Jaguar cartridge ROM loading
368 //
369 void JaguarLoadCart(uint8 * mem, char * path)
370 {
371         uint32 romSize = JaguarLoadROM(mem, path);
372
373         if (romSize == 0)
374         {
375                 char newPath[2048];
376                 WriteLog("VJ: Trying GUI...\n");
377
378 //This is not *nix friendly for some reason...
379 //              if (!UserSelectFile(path, newPath))
380                 if (!UserSelectFile((path == "" ? (char *)"." : path), newPath))
381                 {
382                         WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
383                         log_done();
384                         exit(0);
385                 }
386
387                 romSize = JaguarLoadROM(mem, newPath);
388
389                 if (romSize == 0)
390                 {
391                         WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
392                         log_done();
393                         exit(0);
394                 }
395         }
396
397         jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
398         WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
399         eeprom_init();
400 }