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], "-gl"))
128 vjs.useOpenGL = true;
130 if (!strcmp(argv[i], "-nogl"))
131 vjs.useOpenGL = false;
133 if (!strcmp(argv[i], "-fullscreen"))
134 vjs.fullscreen = true;
136 if (!strcmp(argv[i], "-window"))
137 vjs.fullscreen = false;
139 if (!strcmp(argv[i], "-pal"))
140 vjs.hardwareTypeNTSC = false;
142 if (!strcmp(argv[i], "-ntsc"))
143 vjs.hardwareTypeNTSC = true;
145 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
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");
169 // getcwd(jaguar_boot_dir, 1024);
172 version_display(log_get());
176 if (vjs.useJaguarBIOS)
177 JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath);
179 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
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));
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));
199 nNormalLast = 0; // Last value of timeGetTime()
200 nNormalFrac = 0; // Extra fraction we did
201 nNormalLast = SDL_GetTicks(); //timeGetTime();
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;
211 // si le nombre de trames à passer + 1 est nul ou négatif,
212 // ne rien faire pendant 2 ms
217 } // No need to do anything for a bit
220 nNormalFrac += nCount * 10000; //
221 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
222 nNormalFrac %= 600; //
224 // Pas plus de 9 (10-1) trames non affichées
227 for(int i=0; i<nCount-1; i++)
228 jaguar_exec(backbuffer, false);
230 // Set up new backbuffer with new pixels and data
231 JaguarExecute(backbuffer, true);
234 // Some QnD GUI stuff here...
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);
243 if (nFrame == nFrameskip)
258 int elapsedTime = clock() - startTime;
259 int fps = (1000 * totalFrames) / elapsedTime;
260 WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
265 VideoDone(); // Free SDL components last...!
272 // Generic ROM loading
274 uint32 JaguarLoadROM(uint8 * rom, char * path)
278 char * ext = strrchr(path, '.');
281 WriteLog("VJ: Loading %s...", path);
283 if (stricmp(ext, ".zip") == 0)
285 // Handle ZIP file loading here...
286 WriteLog("(ZIPped)...");
288 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
290 WriteLog("Failed!\n");
296 /* FILE * fp = fopen(path, "rb");
300 WriteLog("Failed!\n");
304 fseek(fp, 0, SEEK_END);
306 fseek(fp, 0, SEEK_SET);
307 fread(rom, 1, romSize, fp);
310 gzFile fp = gzopen(path, "rb");
314 WriteLog("Failed!\n");
318 romSize = gzfilelength(fp);
319 gzseek(fp, 0, SEEK_SET);
320 gzread(fp, rom, romSize);
324 WriteLog("OK (%i bytes)\n", romSize);
331 // Jaguar cartridge ROM loading
333 void JaguarLoadCart(uint8 * mem, char * path)
335 uint32 romSize = JaguarLoadROM(mem, path);
340 WriteLog("VJ: Trying GUI...\n");
342 //This is not *nix friendly for some reason...
343 // if (!UserSelectFile(path, newPath))
344 if (!UserSelectFile((strlen(path) == 0 ? (char *)"." : path), newPath))
346 WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
351 romSize = JaguarLoadROM(mem, newPath);
355 WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
361 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
362 WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
367 // Get the length of a (possibly) gzipped file
369 int gzfilelength(gzFile gd)
371 int size = 0, length = 0;
372 unsigned char buffer[0x10000];
378 // Read in chunks until EOF
379 size = gzread(gd, buffer, 0x10000);