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
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;
52 bool finished = false;
53 bool fullscreen = false;
54 bool hardwareTypeNTSC = true; // Set to false for PAL
56 bool useJoystick = false;
60 // Added/changed by SDLEMU http://sdlemu.ngemu.com
62 uint32 totalFrames;//so we can grab this from elsewhere...
63 int main(int argc, char * argv[])
65 uint32 startTime;//, totalFrames;//, endTime;//, w, h;
66 uint32 nNormalLast = 0;
67 int32 nNormalFrac = 0;
68 int32 nFrameskip = 0; // Default: Show every frame
69 int32 nFrame = 0; // No. of Frame
70 int32 nJoyport = 0; // Joystick port
72 printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
73 printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
74 printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
75 printf("Portions massaged by James L. Hammons (WIN32)\n");
76 printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
78 // BIOS is now ON by default--use the -nobios switch to turn it off!
79 jaguar_use_bios = true;
80 bool haveCart = false; // Assume there is no cartridge...!
82 // Checking the switches ;)
84 for(int i=1; i<argc || argv[i]!=NULL; i++)
86 // This would be the most likely place to do the cart loading...
87 if (argv[i][0] != '-')
88 haveCart = true; // It looks like we have a cartridge!
90 if (!strcmp(argv[i], "-fullscreen"))
93 //We *don't* need this option!
94 /* if (!strcmp(argv[i], "-window"))
95 // console.option("windowed output");
98 if (!strcmp(argv[i], "-joystick"))
101 if (!strcmp(argv[i], "-joyport"))
103 nJoyport = atoi(argv[++i]) + 1;
108 if (!strcmp(argv[i], "-frameskip"))
110 nFrameskip = atoi(argv[++i]) + 1;
118 if (!strcmp(argv[i], "-nobios"))
119 jaguar_use_bios = false;
121 if (!strcmp(argv[i], "-dspon"))
124 if (!strcmp(argv[i], "-pal"))
125 hardwareTypeNTSC = false;
127 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
129 printf("Usage: \n\n");
130 printf("vj [romfile] [switches]\n");
131 printf(" -? or -help : Display usage and switches \n");
132 printf(" -fullscreen : Enable fullscreen mode (windowed default)\n");
133 printf(" -frameskip 1-10 : Enable frameskip 1 (default) - 10 \n");
134 printf(" -joystick : Enable joystick/gamepad \n");
135 printf(" -joyport 0-3 : Select desired joystick port \n");
136 printf(" -nobios : Boot cart without using Jaguar BIOS ROM \n");
137 printf(" -dspon : Force VJ to use the DSP \n");
138 printf(" -pal : Force VJ to PAL mode (default is NTSC) \n");
139 printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
140 printf("with the Jaguar BIOS.\n");
145 getcwd(jaguar_boot_dir, 1024);
149 version_display(log_get());
154 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
156 SET32(jaguar_mainRam, 0, 0x00200000); // Set top of stack...
158 // Get the cartridge ROM (if passed in)
160 JaguarLoadCart(jaguar_mainRom, argv[1]);
164 // Set up the backbuffer
165 int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
166 memset(backbuffer, 0xAA, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
168 // Set up SDL library
169 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) < 0)
171 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
175 // Let's get proper info about the platform we're running on...
176 const SDL_VideoInfo * info = SDL_GetVideoInfo();
180 WriteLog("VJ: SDL is unable to get the video info: %s\n", SDL_GetError());
184 if (info->hw_available)
185 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
188 mainSurfaceFlags |= SDL_HWACCEL;
191 mainSurfaceFlags |= SDL_FULLSCREEN;
193 // Note: mainSurface is *never* used again!
194 //Not true--had to look at what's what here... It's the primary surface...
195 mainSurface = SDL_SetVideoMode(tom_getVideoModeWidth(), tom_getVideoModeHeight(), 16, mainSurfaceFlags);
197 if (mainSurface == NULL)
199 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
203 SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
205 // Create the primary SDL display (16 BPP, 5/5/5 RGB format)
206 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, tom_getVideoModeWidth(),
207 tom_getVideoModeHeight(), 16, 0x7C00, 0x03E0, 0x001F, 0);
211 WriteLog("VJ: Could not create primary SDL surface: %s\n", SDL_GetError());
215 // Initialize Joystick support under SDL
218 if (SDL_NumJoysticks() <= 0)
221 printf("VJ: No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
225 if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
228 printf("VJ: Unable to open a Joystick on port: %d\n", (int)nJoyport);
231 printf("VJ: Using: %s\n", SDL_JoystickName(nJoyport));
237 nNormalLast = 0; // Last value of timeGetTime()
238 nNormalFrac = 0; // Extra fraction we did
239 nNormalLast = SDL_GetTicks(); //timeGetTime();
244 nTime = SDL_GetTicks() - nNormalLast; // calcule le temps écoulé depuis le dernier affichage
245 // nTime est en mili-secondes.
246 // détermine le nombre de trames à passer + 1
247 nCount = (nTime * 600 - nNormalFrac) / 10000;
249 // si le nombre de trames à passer + 1 est nul ou négatif,
250 // ne rien faire pendant 2 ms
255 } // No need to do anything for a bit
258 nNormalFrac += nCount * 10000; //
259 nNormalLast += nNormalFrac / 600; // add the duration of nNormalFrac frames
260 nNormalFrac %= 600; //
262 // Pas plus de 9 (10-1) trames non affichées
265 for(int i=0; i<nCount-1; i++)
266 jaguar_exec(backbuffer, false);
268 // Setting up new backbuffer with new pixels and data
269 JaguarExecute(backbuffer, true);
275 extern uint32 gpu_pc;
276 extern uint32 dsp_pc;
277 DrawText(backbuffer, 8, 0, "Friendly GUI brought to you by JLH ;-)");
278 DrawText(backbuffer, 8, 8, "GPU PC: %08X", gpu_pc);
279 DrawText(backbuffer, 8, 16, "DSP PC: %08X", dsp_pc);
283 if (nFrame == nFrameskip)
285 if (SDL_MUSTLOCK(surface))
286 while (SDL_LockSurface(surface) < 0)
289 memcpy(surface->pixels, backbuffer, tom_width * tom_height * 2);
291 if (SDL_MUSTLOCK(surface))
292 SDL_UnlockSurface(surface);
294 SDL_Rect srcrect, dstrect;
295 srcrect.x = srcrect.y = 0, srcrect.w = surface->w, srcrect.h = surface->h;
296 dstrect.x = dstrect.y = 0, dstrect.w = surface->w, dstrect.h = surface->h;
297 SDL_BlitSurface(surface, &srcrect, mainSurface, &dstrect);
298 SDL_Flip(mainSurface);
311 int elapsedTime = clock() - startTime;
312 int fps = (1000 * totalFrames) / elapsedTime;
313 fprintf(log_get(), "VJ: Ran at an average of %i FPS.\n", fps);
320 SDL_JoystickClose(joystick);
321 SDL_FreeSurface(surface);
322 SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
329 // Generic ROM loading
331 uint32 JaguarLoadROM(uint8 * rom, char * path)
335 WriteLog("JagEm: Loading %s...", path);
337 char * ext = strrchr(path, '.');
338 if (strcmpi(ext, ".zip") == 0)
340 // Handle ZIP file loading here...
341 WriteLog("(ZIPped)...");
343 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
345 WriteLog("Failed!\n");
352 FILE * fp = fopen(path, "rb");
356 WriteLog("Failed!\n");
361 fseek(fp, 0, SEEK_END);
363 fseek(fp, 0, SEEK_SET);
364 fread(rom, 1, romSize, fp);
368 WriteLog("OK (%i bytes)\n", romSize);
373 // Jaguar cartridge ROM loading
375 void JaguarLoadCart(uint8 * mem, char * path)
377 uint32 romsize = JaguarLoadROM(mem, path);
378 jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romsize);
379 WriteLog( "CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);