2 // Virtual Jaguar Emulator
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James L. Hammons
9 // Added by SDLEMU (http://sdlemu.ngemu.com)
10 // Added for GCC UNIX compatibility
15 #include <dirent.h> // POSIX, but should compile with linux & mingw...
23 // Uncomment this for speed control
24 //#define SPEED_CONTROL
26 // Private function prototypes
28 uint32 JaguarLoadROM(uint8 *, char *);
29 void JaguarLoadCart(uint8 *, char *);
33 //These two should be local!
34 extern bool jaguar_use_bios;
35 extern bool dsp_enabled;
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;
55 bool fullscreen = false;
56 bool hardwareTypeNTSC = true; // Set to false for PAL
58 bool useJoystick = false;
61 // Added/changed by SDLEMU http://sdlemu.ngemu.com
63 uint32 totalFrames;//temp, so we can grab this from elsewhere...
64 int main(int argc, char * argv[])
66 uint32 startTime;//, totalFrames;//, endTime;//, w, h;
67 uint32 nNormalLast = 0;
68 int32 nNormalFrac = 0;
69 int32 nFrameskip = 0; // Default: Show every frame
70 int32 nFrame = 0; // No. of Frame
71 int32 nJoyport = 0; // Joystick port
73 printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
74 printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
75 printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
76 printf("Portions massaged by James L. Hammons (WIN32)\n");
77 printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
79 // BIOS is now ON by default--use the -nobios switch to turn it off!
80 jaguar_use_bios = true;
81 bool haveCart = false; // Assume there is no cartridge...!
83 // Checking the switches ;)
85 for(int i=1; i<argc || argv[i]!=NULL; i++)
87 // This would be the most likely place to do the cart loading...
88 if (argv[i][0] != '-')
89 haveCart = true; // It looks like we have a cartridge!
91 if (!strcmp(argv[i], "-fullscreen"))
94 //We *don't* need this option!
95 /* if (!strcmp(argv[i], "-window"))
96 // console.option("windowed output");
99 if (!strcmp(argv[i], "-joystick"))
102 if (!strcmp(argv[i], "-joyport"))
104 nJoyport = atoi(argv[++i]) + 1;
109 if (!strcmp(argv[i], "-frameskip"))
111 nFrameskip = atoi(argv[++i]) + 1;
119 if (!strcmp(argv[i], "-nobios"))
120 jaguar_use_bios = false;
122 if (!strcmp(argv[i], "-dspon"))
125 if (!strcmp(argv[i], "-pal"))
126 hardwareTypeNTSC = false;
128 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
130 printf("Usage: \n\n");
131 printf("vj [romfile] [switches]\n");
132 printf(" -? or -help : Display usage and switches \n");
133 printf(" -fullscreen : Enable fullscreen mode (windowed default)\n");
134 printf(" -frameskip 1-10 : Enable frameskip 1 (default) - 10 \n");
135 printf(" -joystick : Enable joystick/gamepad \n");
136 printf(" -joyport 0-3 : Select desired joystick port \n");
137 printf(" -nobios : Boot cart without using Jaguar BIOS ROM \n");
138 printf(" -dspon : Force VJ to use the DSP \n");
139 printf(" -pal : Force VJ to PAL mode (default is NTSC) \n");
140 printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
141 printf("with the Jaguar BIOS.\n");
146 getcwd(jaguar_boot_dir, 1024);
150 version_display(log_get());
155 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
157 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
159 //This is done here, so that we get valid numbers from TOM... !!! FIX !!!
162 // Set up the backbuffer
163 // int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
164 backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
165 memset(backbuffer, 0x22, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
167 // Set up SDL library
168 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
170 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
174 // Let's get proper info about the platform we're running on...
175 const SDL_VideoInfo * info = SDL_GetVideoInfo();
179 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
183 if (info->hw_available)
184 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
187 mainSurfaceFlags |= SDL_HWACCEL;
190 mainSurfaceFlags |= SDL_FULLSCREEN;
192 mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
194 if (mainSurface == NULL)
196 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
200 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
202 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
203 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
204 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
208 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
212 // Initialize Joystick support under SDL
215 if (SDL_NumJoysticks() <= 0)
218 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
222 if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
225 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
228 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
232 // Get the cartridge ROM (if passed in)
234 // JaguarLoadCart(jaguar_mainRom, argv[1]);
235 // Now with crunchy GUI goodness!
236 JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : (char *)""));
238 //Do this again??? Hmm... This is not very nice.
239 //Maybe it's not necessary??? Seems to be, at least for PD ROMs... !!! FIX !!!
244 nNormalLast = 0; // Last value of timeGetTime()
245 nNormalFrac = 0; // Extra fraction we did
246 nNormalLast = SDL_GetTicks(); //timeGetTime();
251 nTime = SDL_GetTicks() - nNormalLast; // calcule le temps écoulé depuis le dernier affichage
252 // nTime est en mili-secondes.
253 // détermine le nombre de trames à passer + 1
254 nCount = (nTime * 600 - nNormalFrac) / 10000;
256 // si le nombre de trames à passer + 1 est nul ou négatif,
257 // ne rien faire pendant 2 ms
262 } // No need to do anything for a bit
265 nNormalFrac += nCount * 10000; //
266 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
267 nNormalFrac %= 600; //
269 // Pas plus de 9 (10-1) trames non affichées
272 for(int i=0; i<nCount-1; i++)
273 jaguar_exec(backbuffer, false);
275 // Setting up new backbuffer with new pixels and data
276 JaguarExecute(backbuffer, true);
282 extern uint32 gpu_pc, dsp_pc;
283 DrawText(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
284 DrawText(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
288 if (nFrame == nFrameskip)
303 int elapsedTime = clock() - startTime;
304 int fps = (1000 * totalFrames) / elapsedTime;
305 fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
312 SDL_JoystickClose(joystick);
313 SDL_FreeSurface(surface);
314 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
321 // Generic ROM loading
323 uint32 JaguarLoadROM(uint8 * rom, char * path)
327 char * ext = strrchr(path, '.');
330 WriteLog("VJ: Loading %s...", path);
332 if (stricmp(ext, ".zip") == 0)
334 // Handle ZIP file loading here...
335 WriteLog("(ZIPped)...");
337 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
339 WriteLog("Failed!\n");
345 FILE * fp = fopen(path, "rb");
349 WriteLog("Failed!\n");
353 fseek(fp, 0, SEEK_END);
355 fseek(fp, 0, SEEK_SET);
356 fread(rom, 1, romSize, fp);
360 WriteLog("OK (%i bytes)\n", romSize);
367 // Jaguar cartridge ROM loading
369 void JaguarLoadCart(uint8 * mem, char * path)
371 uint32 romSize = JaguarLoadROM(mem, path);
376 WriteLog("VJ: Trying GUI...\n");
378 //This is not *nix friendly for some reason...
379 // if (!UserSelectFile(path, newPath))
380 if (!UserSelectFile((path == "" ? (char *)"." : path), newPath))
382 WriteLog("VJ: Could not find valid ROM in directory \"%s\"...\nAborting!\n", path);
387 romSize = JaguarLoadROM(mem, newPath);
391 WriteLog("VJ: Could not load ROM from file \"%s\"...\nAborting!\n", newPath);
397 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romSize);
398 WriteLog("CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);