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
17 //#include "SDLptc.h" // Ick! Aargh! Bleah!
22 // Uncomment this for speed control
23 //#define SPEED_CONTROL
25 // Private function prototypes
27 uint32 JaguarLoadROM(uint8 *, char *);
28 void JaguarLoadCart(uint8 *, char *);
32 //These two should be local!
33 extern bool jaguar_use_bios;
34 extern bool dsp_enabled;
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[1024];
48 SDL_Surface * surface, * mainSurface;
49 SDL_Joystick * joystick;
50 Uint32 mainSurfaceFlags = SDL_SWSURFACE;
54 //Format format(16, 0x007C00, 0x00003E0, 0x0000001F);
55 bool finished = false;
56 bool fullscreen = false;
57 bool hardwareTypeNTSC = true; // Set to false for PAL
59 bool useJoystick = false;
62 // Added/changed by SDLEMU http://sdlemu.ngemu.com
64 uint32 totalFrames;//so we can grab this from elsewhere...
65 int main(int argc, char * argv[])
67 uint32 startTime;//, totalFrames;//, endTime;//, w, h;
69 uint32 nNormalLast = 0;
70 int32 nNormalFrac = 0;
72 //unused int32 nTime = 0;
73 //unused int32 nCount = 0;
74 int32 nFrameskip = 0; // Default: Show every frame
75 int32 nFrame = 0; // No. of Frame
76 int32 nJoyport = 0; // Joystick port
78 printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
79 printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
80 printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
81 printf("Portions massaged by James L. Hammons (WIN32)\n");
82 printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
84 // console.option("windowed output");
86 // BIOS is now ON by default--use the -nobios switch to turn it off!
87 jaguar_use_bios = true;
88 bool haveCart = false; // Assume there is no cartridge...!
90 // Checking the switches ;)
92 for(int i=1; i<argc || argv[i]!=NULL; i++)
94 // This would be the most likely place to do the cart loading...
95 if (argv[i][0] != '-')
96 haveCart = true; // It looks like we have a cartridge!
98 if (!strcmp(argv[i], "-fullscreen"))
101 // console.option("fullscreen output");
104 //We *don't* need this option!
105 /* if (!strcmp(argv[i], "-window"))
106 // console.option("windowed output");
107 fullscreen = false;*/
109 if (!strcmp(argv[i], "-joystick"))
110 // console.option("joystick enabled");
113 if (!strcmp(argv[i], "-joyport"))
115 nJoyport = atoi(argv[++i]) + 1;
120 if (!strcmp(argv[i], "-frameskip"))
122 nFrameskip = atoi(argv[++i]) + 1;
130 if (!strcmp(argv[i], "-nobios"))
131 jaguar_use_bios = false;
133 if (!strcmp(argv[i], "-dspon"))
136 if (!strcmp(argv[i], "-pal"))
137 hardwareTypeNTSC = false;
139 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
141 printf("Usage: \n\n");
142 printf("vj [romfile] [switches]\n");
143 printf(" -? or -help : Display usage and switches \n");
144 printf(" -fullscreen : Enable fullscreen mode (windowed default)\n");
145 printf(" -frameskip 1-10 : Enable frameskip 1 (default) - 10 \n");
146 printf(" -joystick : Enable joystick/gamepad \n");
147 printf(" -joyport 0-3 : Select desired joystick port \n");
148 printf(" -nobios : Boot cart without using Jaguar BIOS ROM \n");
149 printf(" -dspon : Force VJ to use the DSP \n");
150 printf(" -pal : Force VJ to PAL mode (default is NTSC) \n");
151 printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
152 printf("with the Jaguar BIOS.\n");
157 getcwd(jaguar_boot_dir, 1024);
161 version_display(log_get());
166 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
168 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
170 // Get the cartridge ROM (if passed in)
172 JaguarLoadCart(jaguar_mainRom, argv[1]);
176 // Set up the backbuffer
177 int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
178 memset(backbuffer, 0xAA, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
180 // Set up SDL library
181 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
183 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
187 // Let's get proper info about the platform we're running on.
188 const SDL_VideoInfo * info = SDL_GetVideoInfo();
192 WriteLog("VJ: SDL is unable to get the video query: %s\n", SDL_GetError());
196 if (info->hw_available)
197 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
200 mainSurfaceFlags |= SDL_HWACCEL;
203 mainSurfaceFlags |= SDL_FULLSCREEN;
205 // Note: mainSurface is *never* used again!
206 //Not true--had to look at what's what here...
207 mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
209 if (mainSurface == NULL)
211 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
215 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
217 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
218 // surface = new Surface(tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
219 //Format format(16, 0x007C00, 0x00003E0, 0x0000001F);
220 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
221 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
224 WriteLog("VJ: Could not create primary SDL surface: %s", SDL_GetError());
228 // Initialize Joystick support under SDL
229 // if (console.JoyEnabled() == 1)
232 if (SDL_NumJoysticks() <= 0)
234 // console.option("joystick disabled");
236 printf("No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
240 // if ((console.joystick = SDL_JoystickOpen(nJoyport)) == 0)
241 if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
243 // console.option("joystick disabled");
245 printf("Unable to open a Joystick on port: %d\n", (int)nJoyport);
248 printf("Using: %s\n", SDL_JoystickName(nJoyport));
252 // Open the display and start emulating some 3l337 Atari Jaguar games :P
253 // console.open("Virtual Jaguar", tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
257 nNormalLast = 0; // Last value of timeGetTime()
258 nNormalFrac = 0; // Extra fraction we did
259 nNormalLast = SDL_GetTicks(); //timeGetTime();
264 nTime = SDL_GetTicks() - nNormalLast; // calcule le temps écoulé depuis le dernier affichage
265 // nTime est en mili-secondes.
266 // détermine le nombre de trames à passer + 1
267 nCount = (nTime * 600 - nNormalFrac) / 10000;
269 // si le nombre de trames à passer + 1 est nul ou négatif,
270 // ne rien faire pendant 2 ms
275 } // No need to do anything for a bit
278 nNormalFrac += nCount * 10000; //
279 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
280 nNormalFrac %= 600; //
282 // Pas plus de 9 (10-1) trames non affichées
285 for(int i=0; i<nCount-1; i++)
286 jaguar_exec(backbuffer, false);
288 // Setting up new backbuffer with new pixels and data
289 // jaguar_exec(backbuffer, true);
290 JaguarExecute(backbuffer, true);
294 if (nFrame == nFrameskip)
296 // int32 * vs = (int32 *)surface->lock();
297 if (SDL_MUSTLOCK(surface))
298 while (SDL_LockSurface(surface) < 0)
300 // uint8 * vs = (Uint8 *)surface->pixels;
301 // memcpy(vs, backbuffer, tom_width * tom_height * 2);
302 memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
303 // surface->unlock();
304 if (SDL_MUSTLOCK(surface))
305 SDL_UnlockSurface(surface);
306 // surface->copy(console);
307 SDL_Rect srcrect, dstrect;
308 srcrect.x = srcrect.y = 0, srcrect.w = surface->w, srcrect.h = surface->h;
309 dstrect.x = dstrect.y = 0, dstrect.w = surface->w, dstrect.h = surface->h;
310 // SDL_LowerBlit(surface, &srcrect, dst.surface, &dstrect);
311 SDL_BlitSurface(surface, &srcrect, mainSurface, &dstrect);
312 // dst.updates[dst.nupdates++] = dstrect;
314 SDL_Flip(mainSurface);
329 int elapsedTime = clock() - startTime;
330 int fps = (1000 * totalFrames) / elapsedTime;
331 fprintf(log_get(), "Statistics: %i FPS\n", fps);
337 // console.close(); // Close SDL items as last!
338 SDL_JoystickClose(joystick);
339 SDL_FreeSurface(surface);
340 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
347 // Generic ROM loading
349 uint32 JaguarLoadROM(uint8 * rom, char * path)
353 WriteLog("JagEm: Loading %s...", path);
355 char * ext = strrchr(path, '.');
356 if (strcmpi(ext, ".zip") == 0)
358 // Handle ZIP file loading here...
359 WriteLog("(ZIPped)...");
361 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
363 WriteLog("Failed!\n");
370 FILE * fp = fopen(path, "rb");
374 WriteLog("Failed!\n");
379 fseek(fp, 0, SEEK_END);
381 fseek(fp, 0, SEEK_SET);
382 fread(rom, 1, romSize, fp);
386 WriteLog("OK (%i bytes)\n", romSize);
391 // Jaguar cartridge ROM loading
393 void JaguarLoadCart(uint8 * mem, char * path)
395 uint32 romsize = JaguarLoadROM(mem, path);
396 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romsize);
397 WriteLog( "CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);