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