]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
Changes for 1.0.6 update
[virtualjaguar] / src / vj.cpp
1 //
2 // Virtual Jaguar Emulator
3 //
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
7 //
8
9 #ifdef __GCCUNIX__
10 #include <unistd.h>
11 #endif
12
13 #include <dirent.h>                                                                     // POSIX, but should compile with linux & mingw...
14 #include <time.h>
15 #include <SDL.h>
16 #include "jaguar.h"
17 #include "crc32.h"
18 #include "zlib.h"
19 #include "unzip.h"
20 #include "video.h"
21 #include "gui.h"
22 #include "sdlemu_opengl.h"
23 #include "settings.h"                                                           // Pull in "vjs" struct
24
25 // Uncomment this for speed control (?)
26 //#define SPEED_CONTROL
27
28 // Private function prototypes
29
30 uint32 JaguarLoadROM(uint8 *, char *);
31 void JaguarLoadCart(uint8 *, char *);
32 int gzfilelength(gzFile gd);
33
34 // External variables
35
36 extern uint8 * jaguar_mainRam;
37 extern uint8 * jaguar_bootRom;
38 extern uint8 * jaguar_mainRom;
39
40 // Various paths
41
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[MAX_PATH];
47
48 //These should go into video.cpp...
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 showGUI = false;
56 bool showMessage = false;
57 uint32 showMessageTimeout;
58 char messageBuffer[200];
59
60 //
61 // The main emulator loop (what else?)
62 //
63 //Maybe we should move the video stuff to TOM? Makes more sense to put it there...
64 //Actually, it would probably be better served in VIDEO.CPP... !!! FIX !!! [DONE]
65 uint32 totalFrames;//temp, so we can grab this from elsewhere...
66 int main(int argc, char * argv[])
67 {
68         uint32 startTime;//, totalFrames;//, endTime;//, w, h;
69         uint32 nNormalLast = 0;
70         int32 nNormalFrac = 0; 
71     int32 nFrameskip = 0;                                                       // Default: Show every frame
72     int32 nFrame = 0;                                                           // No. of Frame
73
74         printf("Virtual Jaguar GCC/SDL Portable Jaguar Emulator v1.0.6\n");
75         printf("Based upon Virtual Jaguar core v1.0.0 by Cal2 of Potato emulation.\n");
76         printf("Written by Niels Wagenaar (Linux/WIN32), Caz (BeOS),\n");
77         printf("James L. Hammons (WIN32) and Adam Green (MacOS)\n");
78         printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
79
80         bool haveCart = false;                                                  // Assume there is no cartridge...!
81
82         log_init("vj.log");
83         LoadVJSettings();                                                               // Get config file settings...
84
85         // Check the switches... ;-)
86         // NOTE: Command line switches can override any config file settings, thus the
87         //       proliferation of the noXXX switches. ;-)
88
89         for(int i=1; i<argc || argv[i]!=NULL; i++)
90         {
91                 // This would be the most likely place to do the cart loading...
92                 if (argv[i][0] != '-')
93                         haveCart = true;                                                // It looks like we have a cartridge!
94
95                 if (!strcmp(argv[i], "-joystick")) 
96                         vjs.useJoystick = true;
97
98                 if (!strcmp(argv[i], "-joyport"))
99                 {
100                         vjs.joyport = atoi(argv[++i]) + 1;
101                         if (vjs.joyport > 3)
102                                 vjs.joyport = 3;
103                 }
104
105                 if (!strcmp(argv[i], "-frameskip"))
106                 {
107                         nFrameskip = atoi(argv[++i]) + 1;
108                         if (nFrameskip > 10)
109                                 nFrameskip = 10;
110 #ifdef SPEED_CONTROL
111                         nFrameskip = 0;
112 #endif
113                 }
114
115                 if (!strcmp(argv[i], "-bios"))
116                         vjs.useJaguarBIOS = true;
117
118                 if (!strcmp(argv[i], "-nobios"))
119                         vjs.useJaguarBIOS = false;
120
121                 if (!strcmp(argv[i], "-dsp"))
122                         vjs.DSPEnabled = true;
123
124                 if (!strcmp(argv[i], "-nodsp"))
125                         vjs.DSPEnabled = false;
126
127                 if (!strcmp(argv[i], "-pipeline"))
128                         vjs.usePipelinedDSP = true;
129
130                 if (!strcmp(argv[i], "-nopipeline"))
131                         vjs.usePipelinedDSP = false;
132
133                 if (!strcmp(argv[i], "-gl"))
134                         vjs.useOpenGL = true;
135
136                 if (!strcmp(argv[i], "-nogl"))
137                         vjs.useOpenGL = false;
138
139                 if (!strcmp(argv[i], "-fullscreen")) 
140                         vjs.fullscreen = true;
141
142                 if (!strcmp(argv[i], "-window")) 
143                         vjs.fullscreen = false;
144
145                 if (!strcmp(argv[i], "-pal"))
146                         vjs.hardwareTypeNTSC = false;
147
148                 if (!strcmp(argv[i], "-ntsc"))
149                         vjs.hardwareTypeNTSC = true;
150
151                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
152                 {
153                     printf("Usage: \n\n");
154                         printf("vj [romfile] [switches]\n");
155                         printf("  -? or -help     : Display usage and switches                \n");
156                         printf("  -frameskip 1-10 : Enable frameskip 1 - 10 (default: none)   \n");
157                         printf("  -joystick       : Enable joystick/gamepad                   \n");
158                         printf("  -joyport 0-3    : Select desired joystick port              \n");
159                         printf("  -bios           : Boot cart using Jaguar BIOS ROM           \n");
160                         printf("  -nobios         : Boot cart without using Jaguar BIOS ROM   \n");
161                         printf("  -dsp            : Force VJ to use the DSP                   \n");
162                         printf("  -nodsp          : Force VJ to run without the DSP           \n");
163                         printf("  -pipeline       : Use the DSP pipelined core                \n");
164                         printf("  -nopipeline     : Use the DSP non-pipelined core            \n");
165                         printf("  -gl             : Use OpenGL rendering                      \n");
166                         printf("  -nogl           : Use old non-OpenGL rendering              \n");
167                         printf("  -fullscreen     : Enable fullscreen mode (default: windowed)\n");
168                         printf("  -window         : Enable windowed mode                      \n");
169                         printf("  -pal            : Force VJ to PAL mode (default: NTSC)      \n");
170                         printf("  -ntsc           : Force VJ to NTSC mode                     \n");
171                         printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
172                         printf("with the Jaguar BIOS.\n");
173                         return 1;
174                 }
175     }
176
177 //      getcwd(jaguar_boot_dir, 1024);
178         memory_init();
179         version_init();
180         version_display(log_get());
181         jaguar_init();
182
183         // Get the BIOS ROM
184         if (vjs.useJaguarBIOS)
185                 JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath);
186
187         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
188
189         // Set up the backbuffer
190 //To be safe, this should be 1280 * 625 * 2...
191 //      backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
192         backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
193         memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(int16));
194
195         InitVideo();
196         InitGUI();
197
198         // Get the cartridge ROM (if passed in)
199         // Now with crunchy GUI goodness!
200 //      JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
201         JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : vjs.ROMPath));
202
203         jaguar_reset();
204         
205         totalFrames = 0;
206         startTime = clock();
207         nNormalLast = 0;                                                                        // Last value of timeGetTime()
208         nNormalFrac = 0;                                                                        // Extra fraction we did
209         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
210
211         while (!finished)
212         {
213 #ifdef SPEED_CONTROL
214                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
215                                                                                                                 // nTime est en mili-secondes.
216                 // détermine le nombre de trames à passer + 1
217                 nCount = (nTime * 600 - nNormalFrac) / 10000;
218
219                 // si le nombre de trames à passer + 1 est nul ou négatif,
220                 // ne rien faire pendant 2 ms
221                 if (nCount <= 0) 
222                 { 
223                         //Sleep(2); 
224                         //SDL_Delay(1);
225                 } // No need to do anything for a bit
226                 else
227                 {
228                         nNormalFrac += nCount * 10000;                          // 
229                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
230                         nNormalFrac %= 600;                                                     // 
231
232                         // Pas plus de 9 (10-1) trames non affichées 
233                         if (nCount > 10)
234                                 nCount = 10;
235                         for(int i=0; i<nCount-1; i++)
236                                 jaguar_exec(backbuffer, false);
237 #endif
238             // Set up new backbuffer with new pixels and data
239                         JaguarExecute(backbuffer, true);
240                         totalFrames++;
241 //WriteLog("Frame #%u...\n", totalFrames);
242 //extern bool doDSPDis;
243 //if (totalFrames == 373)
244 //      doDSPDis = true;
245
246                         // Some QnD GUI stuff here...
247                         if (showGUI)
248                         {
249                                 extern uint32 gpu_pc, dsp_pc;
250                                 DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
251                                 DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
252                         }
253
254                         // Simple frameskip
255                         if (nFrame == nFrameskip)
256                         {
257                                 RenderBackbuffer();
258                                 nFrame = 0;
259                         }
260                         else
261                                 nFrame++;
262
263                         joystick_exec();
264
265 #ifdef SPEED_CONTROL
266                 }
267 #endif
268         }
269
270         int elapsedTime = clock() - startTime;
271         int fps = (1000 * totalFrames) / elapsedTime;
272         WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
273
274         jaguar_done();
275         version_done();
276         memory_done();
277         VideoDone();                                                                    // Free SDL components last...!
278         log_done();     
279
280     return 0;
281 }
282
283 //
284 // Generic ROM loading
285 //
286 uint32 JaguarLoadROM(uint8 * rom, char * path)
287 {
288         uint32 romSize = 0;
289
290         char * ext = strrchr(path, '.');
291         if (ext != NULL)
292         {
293                 WriteLog("VJ: Loading \"%s\"...", path);
294
295                 if (stricmp(ext, ".zip") == 0)
296                 {
297                         // Handle ZIP file loading here...
298                         WriteLog("(ZIPped)...");
299
300                         if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
301                         {
302                                 WriteLog("Failed!\n");
303                                 return 0;
304                         }
305                 }
306                 else
307                 {
308 /*                      FILE * fp = fopen(path, "rb");
309
310                         if (fp == NULL)
311                         {
312                                 WriteLog("Failed!\n");
313                                 return 0;
314                         }
315
316                         fseek(fp, 0, SEEK_END);
317                         romSize = ftell(fp);
318                         fseek(fp, 0, SEEK_SET);
319                         fread(rom, 1, romSize, fp);
320                         fclose(fp);*/
321
322                         gzFile fp = gzopen(path, "rb");
323
324                         if (fp == NULL)
325                         {
326                                 WriteLog("Failed!\n");
327                                 return 0;
328                         }
329
330                         romSize = gzfilelength(fp);
331                         gzseek(fp, 0, SEEK_SET);
332                         gzread(fp, rom, romSize);
333                         gzclose(fp);
334                 }
335
336                 WriteLog("OK (%i bytes)\n", romSize);
337         }
338
339         return romSize;
340 }
341
342 //
343 // Jaguar cartridge ROM loading
344 //
345 void JaguarLoadCart(uint8 * mem, char * path)
346 {
347         uint32 romSize = JaguarLoadROM(mem, path);
348
349         if (romSize == 0)
350         {
351                 char newPath[2048];
352                 WriteLog("VJ: Trying GUI...\n");
353
354 //This is not *nix friendly for some reason...
355 //              if (!UserSelectFile(path, newPath))
356                 if (!UserSelectFile((strlen(path) == 0 ? (char *)"." : path), newPath))
357                 {
358                         WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
359                         log_done();
360                         exit(0);
361                 }
362
363                 romSize = JaguarLoadROM(mem, newPath);
364
365                 if (romSize == 0)
366                 {
367                         WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
368                         log_done();
369                         exit(0);
370                 }
371         }
372
373         jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
374         WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
375         eeprom_init();
376 }
377
378 //
379 // Get the length of a (possibly) gzipped file
380 //
381 int gzfilelength(gzFile gd)
382 {
383    int size = 0, length = 0;
384    unsigned char buffer[0x10000];
385
386    gzrewind(gd);
387
388    do
389    {
390       // Read in chunks until EOF
391       size = gzread(gd, buffer, 0x10000);
392
393       if (size <= 0)
394         break;
395
396       length += size;
397    }
398    while (!gzeof(gd));
399
400    gzrewind(gd);
401    return length;
402 }