]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
New switches galore!
[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], "-gl"))
128                         vjs.useOpenGL = true;
129
130                 if (!strcmp(argv[i], "-nogl"))
131                         vjs.useOpenGL = false;
132
133                 if (!strcmp(argv[i], "-fullscreen")) 
134                         vjs.fullscreen = true;
135
136                 if (!strcmp(argv[i], "-window")) 
137                         vjs.fullscreen = false;
138
139                 if (!strcmp(argv[i], "-pal"))
140                         vjs.hardwareTypeNTSC = false;
141
142                 if (!strcmp(argv[i], "-ntsc"))
143                         vjs.hardwareTypeNTSC = true;
144
145                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
146                 {
147                     printf("Usage: \n\n");
148                         printf("vj [romfile] [switches]\n");
149                         printf("  -? or -help     : Display usage and switches                \n");
150                         printf("  -frameskip 1-10 : Enable frameskip 1 - 10 (default: none)   \n");
151                         printf("  -joystick       : Enable joystick/gamepad                   \n");
152                         printf("  -joyport 0-3    : Select desired joystick port              \n");
153                         printf("  -bios           : Boot cart using Jaguar BIOS ROM           \n");
154                         printf("  -nobios         : Boot cart without using Jaguar BIOS ROM   \n");
155                         printf("  -dsp            : Force VJ to use the DSP                   \n");
156                         printf("  -nodsp          : Force VJ to run without the DSP           \n");
157                         printf("  -gl             : Use OpenGL rendering                      \n");
158                         printf("  -nogl           : Use old non-OpenGL rendering              \n");
159                         printf("  -fullscreen     : Enable fullscreen mode (default: windowed)\n");
160                         printf("  -window         : Enable windowed mode                      \n");
161                         printf("  -pal            : Force VJ to PAL mode (default: NTSC)      \n");
162                         printf("  -ntsc           : Force VJ to NTSC mode                     \n");
163                         printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
164                         printf("with the Jaguar BIOS.\n");
165                         return 1;
166                 }
167     }
168
169 //      getcwd(jaguar_boot_dir, 1024);
170         memory_init();
171         version_init();
172         version_display(log_get());
173         jaguar_init();
174
175         // Get the BIOS ROM
176         if (vjs.useJaguarBIOS)
177                 JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath);
178
179         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
180
181         // Set up the backbuffer
182 //To be safe, this should be 1280 * 625 * 2...
183 //      backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
184         backbuffer = (int16 *)malloc(1280 * 625 * sizeof(int16));
185         memset(backbuffer, 0x44, VIRTUAL_SCREEN_WIDTH * VIRTUAL_SCREEN_HEIGHT * sizeof(int16));
186
187         InitVideo();
188         InitGUI();
189
190         // Get the cartridge ROM (if passed in)
191         // Now with crunchy GUI goodness!
192 //      JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
193         JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : vjs.ROMPath));
194
195         jaguar_reset();
196         
197         totalFrames = 0;
198         startTime = clock();
199         nNormalLast = 0;                                                                        // Last value of timeGetTime()
200         nNormalFrac = 0;                                                                        // Extra fraction we did
201         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
202
203         while (!finished)
204         {
205 #ifdef SPEED_CONTROL
206                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
207                                                                                                                 // nTime est en mili-secondes.
208                 // détermine le nombre de trames à passer + 1
209                 nCount = (nTime * 600 - nNormalFrac) / 10000;
210
211                 // si le nombre de trames à passer + 1 est nul ou négatif,
212                 // ne rien faire pendant 2 ms
213                 if (nCount <= 0) 
214                 { 
215                         //Sleep(2); 
216                         //SDL_Delay(1);
217                 } // No need to do anything for a bit
218                 else
219                 {
220                         nNormalFrac += nCount * 10000;                          // 
221                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
222                         nNormalFrac %= 600;                                                     // 
223
224                         // Pas plus de 9 (10-1) trames non affichées 
225                         if (nCount > 10)
226                                 nCount = 10;
227                         for(int i=0; i<nCount-1; i++)
228                                 jaguar_exec(backbuffer, false);
229 #endif
230             // Set up new backbuffer with new pixels and data
231                         JaguarExecute(backbuffer, true);
232                         totalFrames++;
233
234                         // Some QnD GUI stuff here...
235                         if (showGUI)
236                         {
237                                 extern uint32 gpu_pc, dsp_pc;
238                                 DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
239                                 DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
240                         }
241
242                         // Simple frameskip
243                         if (nFrame == nFrameskip)
244                         {
245                                 RenderBackbuffer();
246                                 nFrame = 0;
247                         }
248                         else
249                                 nFrame++;
250
251                         joystick_exec();
252
253 #ifdef SPEED_CONTROL
254                 }
255 #endif
256         }
257
258         int elapsedTime = clock() - startTime;
259         int fps = (1000 * totalFrames) / elapsedTime;
260         WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
261
262         jaguar_done();
263         version_done();
264         memory_done();
265         VideoDone();                                                                    // Free SDL components last...!
266         log_done();     
267
268     return 0;
269 }
270
271 //
272 // Generic ROM loading
273 //
274 uint32 JaguarLoadROM(uint8 * rom, char * path)
275 {
276         uint32 romSize = 0;
277
278         char * ext = strrchr(path, '.');
279         if (ext != NULL)
280         {
281                 WriteLog("VJ: Loading %s...", path);
282
283                 if (stricmp(ext, ".zip") == 0)
284                 {
285                         // Handle ZIP file loading here...
286                         WriteLog("(ZIPped)...");
287
288                         if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
289                         {
290                                 WriteLog("Failed!\n");
291                                 return 0;
292                         }
293                 }
294                 else
295                 {
296 /*                      FILE * fp = fopen(path, "rb");
297
298                         if (fp == NULL)
299                         {
300                                 WriteLog("Failed!\n");
301                                 return 0;
302                         }
303
304                         fseek(fp, 0, SEEK_END);
305                         romSize = ftell(fp);
306                         fseek(fp, 0, SEEK_SET);
307                         fread(rom, 1, romSize, fp);
308                         fclose(fp);*/
309
310                         gzFile fp = gzopen(path, "rb");
311
312                         if (fp == NULL)
313                         {
314                                 WriteLog("Failed!\n");
315                                 return 0;
316                         }
317
318                         romSize = gzfilelength(fp);
319                         gzseek(fp, 0, SEEK_SET);
320                         gzread(fp, rom, romSize);
321                         gzclose(fp);
322                 }
323
324                 WriteLog("OK (%i bytes)\n", romSize);
325         }
326
327         return romSize;
328 }
329
330 //
331 // Jaguar cartridge ROM loading
332 //
333 void JaguarLoadCart(uint8 * mem, char * path)
334 {
335         uint32 romSize = JaguarLoadROM(mem, path);
336
337         if (romSize == 0)
338         {
339                 char newPath[2048];
340                 WriteLog("VJ: Trying GUI...\n");
341
342 //This is not *nix friendly for some reason...
343 //              if (!UserSelectFile(path, newPath))
344                 if (!UserSelectFile((strlen(path) == 0 ? (char *)"." : path), newPath))
345                 {
346                         WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
347                         log_done();
348                         exit(0);
349                 }
350
351                 romSize = JaguarLoadROM(mem, newPath);
352
353                 if (romSize == 0)
354                 {
355                         WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
356                         log_done();
357                         exit(0);
358                 }
359         }
360
361         jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
362         WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
363         eeprom_init();
364 }
365
366 //
367 // Get the length of a (possibly) gzipped file
368 //
369 int gzfilelength(gzFile gd)
370 {
371    int size = 0, length = 0;
372    unsigned char buffer[0x10000];
373
374    gzrewind(gd);
375
376    do
377    {
378       // Read in chunks until EOF
379       size = gzread(gd, buffer, 0x10000);
380
381       if (size <= 0)
382         break;
383
384       length += size;
385    }
386    while (!gzeof(gd));
387
388    gzrewind(gd);
389    return length;
390 }