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
31 uint32 JaguarLoadROM(uint8 *, char *);
32 void JaguarLoadCart(uint8 *, char *);
33 int gzfilelength(gzFile gd);
37 extern uint8 * jaguar_mainRam;
38 extern uint8 * jaguar_bootRom;
39 extern uint8 * jaguar_mainRom;
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];
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 !!!
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...!
82 LoadVJSettings(); // Get config file settings...
84 // Check the switches... ;-)
85 // NOTE: Command line switches can override any config file settings.
87 for(int i=1; i<argc || argv[i]!=NULL; i++)
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!
93 if (!strcmp(argv[i], "-fullscreen"))
94 vjs.fullscreen = true;
96 if (!strcmp(argv[i], "-joystick"))
97 vjs.useJoystick = true;
99 if (!strcmp(argv[i], "-joyport"))
101 vjs.joyport = atoi(argv[++i]) + 1;
106 if (!strcmp(argv[i], "-frameskip"))
108 nFrameskip = atoi(argv[++i]) + 1;
116 if (!strcmp(argv[i], "-bios"))
117 vjs.useJaguarBIOS = true;
119 if (!strcmp(argv[i], "-nobios"))
120 vjs.useJaguarBIOS = false;
122 if (!strcmp(argv[i], "-dsp"))
123 vjs.DSPEnabled = true;
125 if (!strcmp(argv[i], "-pal"))
126 vjs.hardwareTypeNTSC = false;
128 if (!strcmp(argv[i], "-nogl"))
129 vjs.useOpenGL = false;
131 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
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(" -dsp : 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");
151 getcwd(jaguar_boot_dir, 1024);
155 version_display(log_get());
159 if (vjs.useJaguarBIOS)
160 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
162 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
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));
172 // Get the cartridge ROM (if passed in)
173 // Now with crunchy GUI goodness!
174 JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
180 nNormalLast = 0; // Last value of timeGetTime()
181 nNormalFrac = 0; // Extra fraction we did
182 nNormalLast = SDL_GetTicks(); //timeGetTime();
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;
192 // si le nombre de trames à passer + 1 est nul ou négatif,
193 // ne rien faire pendant 2 ms
198 } // No need to do anything for a bit
201 nNormalFrac += nCount * 10000; //
202 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
203 nNormalFrac %= 600; //
205 // Pas plus de 9 (10-1) trames non affichées
208 for(int i=0; i<nCount-1; i++)
209 jaguar_exec(backbuffer, false);
211 // Set up new backbuffer with new pixels and data
212 JaguarExecute(backbuffer, true);
215 // Some QnD GUI stuff here...
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);
224 if (nFrame == nFrameskip)
239 int elapsedTime = clock() - startTime;
240 int fps = (1000 * totalFrames) / elapsedTime;
241 WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
246 VideoDone(); // Free SDL components last...!
253 // Generic ROM loading
255 uint32 JaguarLoadROM(uint8 * rom, char * path)
259 char * ext = strrchr(path, '.');
262 WriteLog("VJ: Loading %s...", path);
264 if (stricmp(ext, ".zip") == 0)
266 // Handle ZIP file loading here...
267 WriteLog("(ZIPped)...");
269 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
271 WriteLog("Failed!\n");
277 /* FILE * fp = fopen(path, "rb");
281 WriteLog("Failed!\n");
285 fseek(fp, 0, SEEK_END);
287 fseek(fp, 0, SEEK_SET);
288 fread(rom, 1, romSize, fp);
291 gzFile fp = gzopen(path, "rb");
295 WriteLog("Failed!\n");
299 romSize = gzfilelength(fp);
300 gzseek(fp, 0, SEEK_SET);
301 gzread(fp, rom, romSize);
305 WriteLog("OK (%i bytes)\n", romSize);
312 // Jaguar cartridge ROM loading
314 void JaguarLoadCart(uint8 * mem, char * path)
316 uint32 romSize = JaguarLoadROM(mem, path);
321 WriteLog("VJ: Trying GUI...\n");
323 //This is not *nix friendly for some reason...
324 // if (!UserSelectFile(path, newPath))
325 if (!UserSelectFile((path == "" ? (char *)"." : path), newPath))
327 WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
332 romSize = JaguarLoadROM(mem, newPath);
336 WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
342 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
343 WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
348 // Get the length of a (possibly) gzipped file
350 int gzfilelength(gzFile gd)
352 int size = 0, length = 0;
353 unsigned char buffer[0x10000];
359 // Read in chunks until EOF
360 size = gzread(gd, buffer, 0x10000);