2 // Virtual Jaguar Emulator
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
13 #include <dirent.h> // POSIX, but should compile with linux & mingw...
22 #include "sdlemu_opengl.h"
23 #include "settings.h" // Pull in "vjs" struct
25 // Uncomment this for speed control (?)
26 //#define SPEED_CONTROL
28 // Private function prototypes
30 uint32 JaguarLoadROM(uint8 *, char *);
31 void JaguarLoadCart(uint8 *, char *);
32 int gzfilelength(gzFile gd);
36 extern uint8 * jaguar_mainRam;
37 extern uint8 * jaguar_bootRom;
38 extern uint8 * jaguar_mainRom;
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];
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;
54 bool finished = false;
56 bool showMessage = false;
57 uint32 showMessageTimeout;
58 char messageBuffer[200];
61 // The main emulator loop (what else?)
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[])
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
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");
80 bool haveCart = false; // Assume there is no cartridge...!
83 LoadVJSettings(); // Get config file settings...
85 // Check the switches... ;-)
86 // NOTE: Command line switches can override any config file settings, thus the
87 // proliferation of the noXXX switches. ;-)
89 for(int i=1; i<argc || argv[i]!=NULL; i++)
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!
95 if (!strcmp(argv[i], "-joystick"))
96 vjs.useJoystick = true;
98 if (!strcmp(argv[i], "-joyport"))
100 vjs.joyport = atoi(argv[++i]) + 1;
105 if (!strcmp(argv[i], "-frameskip"))
107 nFrameskip = atoi(argv[++i]) + 1;
115 if (!strcmp(argv[i], "-bios"))
116 vjs.useJaguarBIOS = true;
118 if (!strcmp(argv[i], "-nobios"))
119 vjs.useJaguarBIOS = false;
121 if (!strcmp(argv[i], "-dsp"))
122 vjs.DSPEnabled = true;
124 if (!strcmp(argv[i], "-nodsp"))
125 vjs.DSPEnabled = false;
127 if (!strcmp(argv[i], "-pipeline"))
128 vjs.usePipelinedDSP = true;
130 if (!strcmp(argv[i], "-nopipeline"))
131 vjs.usePipelinedDSP = false;
133 if (!strcmp(argv[i], "-gl"))
134 vjs.useOpenGL = true;
136 if (!strcmp(argv[i], "-nogl"))
137 vjs.useOpenGL = false;
139 if (!strcmp(argv[i], "-fullscreen"))
140 vjs.fullscreen = true;
142 if (!strcmp(argv[i], "-window"))
143 vjs.fullscreen = false;
145 if (!strcmp(argv[i], "-pal"))
146 vjs.hardwareTypeNTSC = false;
148 if (!strcmp(argv[i], "-ntsc"))
149 vjs.hardwareTypeNTSC = true;
151 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
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");
177 // getcwd(jaguar_boot_dir, 1024);
180 version_display(log_get());
184 if (vjs.useJaguarBIOS)
185 JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath);
187 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
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));
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));
207 nNormalLast = 0; // Last value of timeGetTime()
208 nNormalFrac = 0; // Extra fraction we did
209 nNormalLast = SDL_GetTicks(); //timeGetTime();
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;
219 // si le nombre de trames à passer + 1 est nul ou négatif,
220 // ne rien faire pendant 2 ms
225 } // No need to do anything for a bit
228 nNormalFrac += nCount * 10000; //
229 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
230 nNormalFrac %= 600; //
232 // Pas plus de 9 (10-1) trames non affichées
235 for(int i=0; i<nCount-1; i++)
236 jaguar_exec(backbuffer, false);
238 // Set up new backbuffer with new pixels and data
239 JaguarExecute(backbuffer, true);
241 //WriteLog("Frame #%u...\n", totalFrames);
242 //extern bool doDSPDis;
243 //if (totalFrames == 373)
246 // Some QnD GUI stuff here...
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);
255 if (nFrame == nFrameskip)
270 int elapsedTime = clock() - startTime;
271 int fps = (1000 * totalFrames) / elapsedTime;
272 WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
277 VideoDone(); // Free SDL components last...!
284 // Generic ROM loading
286 uint32 JaguarLoadROM(uint8 * rom, char * path)
290 char * ext = strrchr(path, '.');
293 WriteLog("VJ: Loading \"%s\"...", path);
295 if (stricmp(ext, ".zip") == 0)
297 // Handle ZIP file loading here...
298 WriteLog("(ZIPped)...");
300 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
302 WriteLog("Failed!\n");
308 /* FILE * fp = fopen(path, "rb");
312 WriteLog("Failed!\n");
316 fseek(fp, 0, SEEK_END);
318 fseek(fp, 0, SEEK_SET);
319 fread(rom, 1, romSize, fp);
322 gzFile fp = gzopen(path, "rb");
326 WriteLog("Failed!\n");
330 romSize = gzfilelength(fp);
331 gzseek(fp, 0, SEEK_SET);
332 gzread(fp, rom, romSize);
336 WriteLog("OK (%i bytes)\n", romSize);
343 // Jaguar cartridge ROM loading
345 void JaguarLoadCart(uint8 * mem, char * path)
347 uint32 romSize = JaguarLoadROM(mem, path);
352 WriteLog("VJ: Trying GUI...\n");
354 //This is not *nix friendly for some reason...
355 // if (!UserSelectFile(path, newPath))
356 if (!UserSelectFile((strlen(path) == 0 ? (char *)"." : path), newPath))
358 WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
363 romSize = JaguarLoadROM(mem, newPath);
367 WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
373 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
374 WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
379 // Get the length of a (possibly) gzipped file
381 int gzfilelength(gzFile gd)
383 int size = 0, length = 0;
384 unsigned char buffer[0x10000];
390 // Read in chunks until EOF
391 size = gzread(gd, buffer, 0x10000);