]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
Changes mainly to support the removal of SDLptc.h
[virtualjaguar] / src / vj.cpp
1 //
2 // Virtual Jaguar Emulator
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James L. Hammons
7 //
8
9 // Added by SDLEMU (http://sdlemu.ngemu.com)
10 // Added for GCC UNIX compatibility
11 #ifdef __GCCUNIX__
12 #include <unistd.h>
13 #endif  // __GCCUNIX__
14
15 #include <time.h>
16 #include <SDL.h>
17 //#include "SDLptc.h"                   // Ick! Aargh! Bleah!
18 #include "jaguar.h"
19 #include "crc32.h"
20 #include "unzip.h"
21
22 // Uncomment this for speed control
23 //#define SPEED_CONTROL
24
25 // Private function prototypes
26
27 uint32 JaguarLoadROM(uint8 *, char *);
28 void JaguarLoadCart(uint8 *, char *);
29
30 // External variables
31
32 //These two should be local!
33 extern bool jaguar_use_bios;
34 extern bool dsp_enabled;
35
36 extern uint8 * jaguar_mainRam;
37 extern uint8 * jaguar_bootRom;
38 extern uint8 * jaguar_mainRom;
39
40 // Various paths
41
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];
47
48 SDL_Surface * surface, * mainSurface;
49 SDL_Joystick * joystick;
50 Uint32 mainSurfaceFlags = SDL_SWSURFACE;
51
52 //Console console;
53 //Surface * surface;
54 //Format format(16, 0x007C00, 0x00003E0, 0x0000001F);
55 bool finished = false;
56 bool fullscreen = false;
57 bool hardwareTypeNTSC = true;                   // Set to false for PAL
58
59 bool useJoystick = false;
60
61
62 // Added/changed by SDLEMU http://sdlemu.ngemu.com
63
64 uint32 totalFrames;//so we can grab this from elsewhere...
65 int main(int argc, char * argv[])
66 {
67         uint32 startTime;//, totalFrames;//, endTime;//, w, h;
68 //      int32 * vs;
69         uint32 nNormalLast = 0;
70         int32 nNormalFrac = 0; 
71 //      int32 i = 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
77
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");
83
84 //    console.option("windowed output");
85
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...!
89
90         // Checking the switches ;)
91
92         for(int i=1; i<argc || argv[i]!=NULL; i++)
93         {
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!
97
98                 if (!strcmp(argv[i], "-fullscreen")) 
99 //              {
100                         fullscreen = true;
101 //                      console.option("fullscreen output");
102 //              }
103
104 //We *don't* need this option!
105 /*              if (!strcmp(argv[i], "-window")) 
106 //                      console.option("windowed output");
107                         fullscreen = false;*/
108
109                 if (!strcmp(argv[i], "-joystick")) 
110 //                      console.option("joystick enabled");
111                         useJoystick = true;
112
113                 if (!strcmp(argv[i], "-joyport"))
114                 {
115                         nJoyport = atoi(argv[++i]) + 1;
116                         if (nJoyport > 3)
117                                 nJoyport = 3;
118                 }
119
120                 if (!strcmp(argv[i], "-frameskip"))
121                 {
122                         nFrameskip = atoi(argv[++i]) + 1;
123                         if (nFrameskip > 10)
124                                 nFrameskip = 10;
125 #ifdef SPEED_CONTROL
126                         nFrameskip = 0;
127 #endif
128                 }
129
130                 if (!strcmp(argv[i], "-nobios"))
131                         jaguar_use_bios = false;
132
133                 if (!strcmp(argv[i], "-dspon"))
134                         dsp_enabled = 1;
135
136                 if (!strcmp(argv[i], "-pal"))
137                         hardwareTypeNTSC = false;
138
139                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
140                 {
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");
153                         return 1;
154                 }
155     }
156
157         getcwd(jaguar_boot_dir, 1024);
158         log_init("vj.log");
159         memory_init();
160         version_init();
161         version_display(log_get());
162         jaguar_init();
163
164         // Get the BIOS ROM
165         if (jaguar_use_bios)
166                 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
167
168         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
169
170         // Get the cartridge ROM (if passed in)
171         if (haveCart)
172                 JaguarLoadCart(jaguar_mainRom, argv[1]);
173
174         jaguar_reset();
175         
176         // Set up the backbuffer
177         int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
178         memset(backbuffer, 0xAA, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
179
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)
182         {
183                 WriteLog("VJ: Could not initialize the SDL library: %s", SDL_GetError());
184                 exit(1);
185         }
186
187         // Let's get proper info about the platform we're running on.
188         const SDL_VideoInfo * info = SDL_GetVideoInfo();
189
190         if (!info)
191         {
192                 WriteLog("VJ: SDL is unable to get the video query: %s\n", SDL_GetError());
193                 exit(1);
194         }
195
196         if (info->hw_available)
197                 mainSurfaceFlags = SDL_HWSURFACE | SDL_HWPALETTE | SDL_DOUBLEBUF;
198
199         if (info->blit_hw)
200                 mainSurfaceFlags |= SDL_HWACCEL;
201
202         if (fullscreen)
203                 mainSurfaceFlags |= SDL_FULLSCREEN;
204
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);
208
209         if (mainSurface == NULL)
210         {
211                 WriteLog("VJ: SDL is unable to set the video mode: %s\n", SDL_GetError());
212                 exit(1);
213         }
214
215         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
216
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);
222         if (surface == NULL)
223         {
224                 WriteLog("VJ: Could not create primary SDL surface: %s", SDL_GetError());
225                 exit(1);
226         }
227
228         // Initialize Joystick support under SDL
229 //      if (console.JoyEnabled() == 1)
230         if (useJoystick)
231         {
232                 if (SDL_NumJoysticks() <= 0)
233                 {
234 //              console.option("joystick disabled");
235                         useJoystick = false;
236                         printf("No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
237                 }
238                 else
239                 {
240 //                      if ((console.joystick = SDL_JoystickOpen(nJoyport)) == 0)
241                         if ((joystick = SDL_JoystickOpen(nJoyport)) == 0)
242                         {
243 //                              console.option("joystick disabled");
244                                 useJoystick = false;
245                                 printf("Unable to open a Joystick on port: %d\n", (int)nJoyport);
246                         }
247                         else
248                                 printf("Using: %s\n", SDL_JoystickName(nJoyport));
249                 }
250         }
251
252         // Open the display and start emulating some 3l337 Atari Jaguar games :P
253 //      console.open("Virtual Jaguar", tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
254
255         totalFrames = 0;
256         startTime = clock();
257         nNormalLast = 0;                                                                        // Last value of timeGetTime()
258         nNormalFrac = 0;                                                                        // Extra fraction we did
259         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
260
261         while (!finished)
262         {
263 #ifdef SPEED_CONTROL
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;
268
269                 // si le nombre de trames à passer + 1 est nul ou négatif,
270                 // ne rien faire pendant 2 ms
271                 if (nCount <= 0) 
272                 { 
273                         //Sleep(2); 
274                         //SDL_Delay(1);
275                 } // No need to do anything for a bit
276                 else
277                 {
278                         nNormalFrac += nCount * 10000;                          // 
279                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
280                         nNormalFrac %= 600;                                                     // 
281
282                         // Pas plus de 9 (10-1) trames non affichées 
283                         if (nCount > 10)
284                                 nCount = 10;
285                         for(int i=0; i<nCount-1; i++)
286                                 jaguar_exec(backbuffer, false);
287 #endif
288             // Setting up new backbuffer with new pixels and data
289 //                      jaguar_exec(backbuffer, true);
290                         JaguarExecute(backbuffer, true);
291                         totalFrames++;
292
293                         // Simple frameskip
294                         if (nFrame == nFrameskip)
295                         {
296 //                              int32 * vs = (int32 *)surface->lock();
297                                 if (SDL_MUSTLOCK(surface))
298                                         while (SDL_LockSurface(surface) < 0)
299                                                 SDL_Delay(10);
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;
313 //                              console.update();
314                             SDL_Flip(mainSurface);      
315 //                              nupdates = 0;
316
317                                 nFrame = 0;
318                         }
319                         else
320                                 nFrame++;
321
322                         joystick_exec();
323                         
324 #ifdef SPEED_CONTROL
325                 }
326 #endif
327         }
328
329         int elapsedTime = clock() - startTime;
330         int fps = (1000 * totalFrames) / elapsedTime;
331         fprintf(log_get(), "Statistics: %i FPS\n", fps);
332
333         jaguar_done();
334         version_done();
335         memory_done();
336         log_done();     
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);
341         SDL_Quit();
342
343     return 0;
344 }
345
346 //
347 // Generic ROM loading
348 //
349 uint32 JaguarLoadROM(uint8 * rom, char * path)
350 {
351         uint32 romSize;
352
353         WriteLog("JagEm: Loading %s...", path);
354
355         char * ext = strrchr(path, '.');
356         if (strcmpi(ext, ".zip") == 0)
357         {
358                 // Handle ZIP file loading here...
359                 WriteLog("(ZIPped)...");
360
361                 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
362                 {
363                         WriteLog("Failed!\n");
364                         log_done();
365                         exit(0);
366                 }
367         }
368         else
369         {
370                 FILE * fp = fopen(path, "rb");
371
372                 if (fp == NULL)
373                 {
374                         WriteLog("Failed!\n");
375                         log_done();
376                         exit(0);
377                 }
378
379                 fseek(fp, 0, SEEK_END);
380                 romSize = ftell(fp);
381                 fseek(fp, 0, SEEK_SET);
382                 fread(rom, 1, romSize, fp);
383                 fclose(fp);
384         }
385
386         WriteLog("OK (%i bytes)\n", romSize);
387         return romSize;
388 }
389
390 //
391 // Jaguar cartridge ROM loading
392 //
393 void JaguarLoadCart(uint8 * mem, char * path)
394 {
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);
398         eeprom_init();
399 }