]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
5c6fb61e25b3d3be5d914dab7827a705decf7095
[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 //
10 // Important info:
11 //
12 //                           NTSC       PAL
13 // GPU/DSP/video clock rate  26.590906  26.593900
14 // 68000 clock rate          13.295453  13.296950
15 // (clock rates in MHz)
16 //
17
18 // Added by SDLEMU (http://sdlemu.ngemu.com)
19 // Added for GCC UNIX compatibility
20 #ifdef __GCCUNIX__
21 #include <unistd.h>
22 #endif  // __GCCUNIX__
23
24 #include <time.h>
25 #include <SDL.h>
26 #include "SDLptc.h"
27 #include "jaguar.h"
28 #include "crc32.h"
29 #include "unzip.h"
30
31 // Uncomment this for speed control
32 //#define SPEED_CONTROL
33
34 //
35 // Private function prototypes
36 //
37
38 uint32 JaguarLoadROM(uint8 *, char *);
39 void JaguarLoadCart(uint8 *, char *);
40
41 //
42 // Various paths
43 //
44
45 //static char  *jaguar_bootRom_path="c:/jaguarEmu/newload.img";
46 static char * jaguar_bootRom_path = "./bios/jagboot.rom";
47 //static char  *jaguar_bootRom_path="./bios/JagOS.bin";
48 char * jaguar_eeproms_path = "./eeproms/";
49 char jaguar_boot_dir[1024];
50 //static char romLoadDialog_filePath[1024];
51
52
53 Console console;
54 Surface * surface;
55 Format format(16, 0x007C00, 0x00003E0, 0x0000001F);
56 bool finished = false;
57 bool fullscreen = false;
58 bool hardwareTypeNTSC = true;                   // Set to false for PAL
59
60 //
61 // External variables
62 //
63
64 extern bool jaguar_use_bios;
65 extern bool dsp_enabled;
66 extern uint8 * jaguar_mainRam;
67 extern uint8 * jaguar_bootRom;
68 extern uint8 * jaguar_mainRom;
69
70
71 void main_screen_switch(void)
72 {
73         fullscreen = !fullscreen;
74         if (fullscreen)
75                 console.option("fullscreen output");
76         else
77                 console.option("windowed output");
78         console.close();
79         console.open("Virtual Jaguar", tom_width, tom_height, format);
80 }
81
82 // Added/changed by SDLEMU http://sdlemu.ngemu.com
83
84 uint32 totalFrames;//so we can grab this from somewhere else...
85 int main(int argc, char * argv[])
86 {
87         uint32 startTime;//, totalFrames;//, endTime;//, w, h;
88 //      int32 * vs;
89         uint32 nNormalLast = 0;
90         int32 nNormalFrac = 0; 
91 //      int32 i = 0;
92 //unused        int32 nTime = 0;
93 //unused        int32 nCount = 0;
94     int32 nFrameskip = 0;                                                               // Default: Show every frame
95     int32 nFrame = 0;                                                                   // No. of Frame
96     int32 nJoyport = 0;                                                                 // Joystick port
97
98         printf("Virtual Jaguar/SDL v1.0.5 (GCC/SDL Port)\n");
99         printf("Based upon Virtual Jaguar core v1.0.0 by cal2 of Potato emulation.\n");
100         printf("Written by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)\n");
101         printf("Portions massaged by James L. Hammons (WIN32)\n");
102         printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
103
104     console.option("windowed output");
105
106         // BIOS is now ON by default--use the -nobios switch to turn it off!
107         jaguar_use_bios = true;
108         bool haveCart = false;                                                                  // Assume there is no cartridge...!
109
110         // Checking the switches ;)
111
112         for(int i=1; i<argc || argv[i]!=NULL; i++)
113         {
114                 // This would be the most likely place to do the cart loading...
115                 if (argv[i][0] != '-')
116                         haveCart = true;                                                                // It looks like we have a cartridge!
117
118                 if (!strcmp(argv[i], "-fullscreen")) 
119                 {
120                         fullscreen = true;
121                         console.option("fullscreen output");
122                 }
123
124                 if (!strcmp(argv[i], "-window")) 
125                         console.option("windowed output");
126
127                 if (!strcmp(argv[i], "-joystick")) 
128                         console.option("joystick enabled");
129
130                 if (!strcmp(argv[i], "-joyport"))
131                 {
132                         nJoyport = atoi(argv[++i]) + 1;
133                         if (nJoyport > 3)
134                                 nJoyport = 3;
135                 }
136
137                 if (!strcmp(argv[i], "-frameskip"))
138                 {
139                         nFrameskip = atoi(argv[++i]) + 1;
140                         if (nFrameskip > 10)
141                                 nFrameskip = 10;
142 #ifdef SPEED_CONTROL
143                         nFrameskip = 0;
144 #endif
145                 }
146
147                 if (!strcmp(argv[i], "-nobios"))
148                         jaguar_use_bios = false;
149
150                 if (!strcmp(argv[i], "-dspon"))
151                         dsp_enabled = 1;
152
153                 if (!strcmp(argv[i], "-pal"))
154                         hardwareTypeNTSC = false;
155
156                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
157                 {
158                     printf("Usage: \n\n");
159                         printf("vj [romfile] [switches]\n");
160                         printf("  -? or -help     : Display usage and switches              \n");
161                         printf("  -fullscreen     : Enable fullscreen mode                  \n");
162                         printf("  -window         : Enable windowed mode (default)          \n");
163                         printf("  -frameskip 1-10 : Enable frameskip 1 (default) - 10       \n");
164                         printf("  -joystick       : Enable joystick/gamepad                 \n");
165                         printf("  -joyport   0-3  : Select desired joystick port            \n");
166                         printf("  -nobios         : Boot cart without using Jaguar BIOS ROM \n");
167                         printf("  -dspon          : Force VJ to use the DSP                 \n");
168                         printf("  -pal            : Force VJ to PAL mode (default is NTSC)  \n");
169                         printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
170                         printf("with the Jaguar BIOS.\n");
171                         return true;
172                 }
173     }
174
175         getcwd(jaguar_boot_dir, 1024);
176         log_init("vj.log");
177         memory_init();
178         version_init();
179         version_display(log_get());
180         jaguar_init();
181
182         // Get the BIOS ROM
183         if (jaguar_use_bios)
184                 JaguarLoadROM(jaguar_bootRom, jaguar_bootRom_path);
185
186         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
187
188         // Get the cartridge ROM (if passed in)
189         if (haveCart)
190         {
191                 JaguarLoadCart(jaguar_mainRom, argv[1]);
192                 eeprom_init();
193         }
194
195         jaguar_reset();
196         
197         // Setting up the backbuffer
198         int16 * backbuffer = (int16 *)malloc(845 * 525 * sizeof(int16));
199         memset(backbuffer, 0xAA, tom_getVideoModeWidth() * tom_getVideoModeHeight() * sizeof(int16));
200
201         // Setting up the primary SDL display
202         surface = new Surface(tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
203
204         // Initialize Joystick support under SDL
205         if (console.JoyEnabled() == 1)
206         {
207                 if (SDL_NumJoysticks() <= 0)
208                 {
209                 console.option("joystick disabled");
210                         printf("No joystick(s) or joypad(s) detected on your system. Using keyboard...\n");
211                 }
212                 else
213                 {
214                         if ((console.joystick = SDL_JoystickOpen(nJoyport)) == 0)
215                         {
216                                 console.option("joystick disabled");
217                                 printf("Unable to open a Joystick on port: %d\n", (int)nJoyport);
218                         }
219                         else
220                                 printf("Using: %s\n", SDL_JoystickName(nJoyport));
221                 }
222         }
223
224         // Open the display and start emulating some 3l337 Atari Jaguar games :P
225         console.open("Virtual Jaguar", tom_getVideoModeWidth(), tom_getVideoModeHeight(), format);
226         
227         totalFrames = 0;
228         startTime = clock();
229         nNormalLast = 0;                                                                        // Last value of timeGetTime()
230         nNormalFrac = 0;                                                                        // Extra fraction we did
231         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
232
233         while (!finished)
234         {
235 #ifdef SPEED_CONTROL
236                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
237                                                                                                                 // nTime est en mili-secondes.
238                 // détermine le nombre de trames à passer + 1
239                 nCount = (nTime * 600 - nNormalFrac) / 10000;
240
241                 // si le nombre de trames à passer + 1 est nul ou négatif,
242                 // ne rien faire pendant 2 ms
243                 if (nCount <= 0) 
244                 { 
245                         //Sleep(2); 
246                         //SDL_Delay(1);
247                 } // No need to do anything for a bit
248                 else
249                 {
250                         nNormalFrac += nCount * 10000;                          // 
251                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
252                         nNormalFrac %= 600;                                                     // 
253
254                         // Pas plus de 9 (10-1) trames non affichées 
255                         if (nCount > 10)
256                                 nCount = 10;
257                         for(int i=0; i<nCount-1; i++)
258                                 jaguar_exec(backbuffer, false);
259 #endif
260             // Setting up new backbuffer with new pixels and data
261 //                      jaguar_exec(backbuffer, true);
262                         JaguarExecute(backbuffer, true);
263                         totalFrames++;
264
265                         // Simple frameskip
266                         if (nFrame == nFrameskip)
267                         {
268                                 int32 * vs = (int32 *)surface->lock();
269                                 memcpy(vs, backbuffer, tom_width * tom_height * 2);
270                                 surface->unlock();
271                                 surface->copy(console);
272                                 console.update();
273                                 nFrame = 0;
274                         }
275                         else
276                                 nFrame++;
277
278                         joystick_exec();
279                         
280 #ifdef SPEED_CONTROL
281                 }
282 #endif
283         }
284
285         int elapsedTime = clock() - startTime;
286         int fps = (1000 * totalFrames) / elapsedTime;
287         fprintf(log_get(), "Statistics: %i FPS\n", fps);
288         
289         if (console.JoyEnabled() == 1) {}
290  
291         jaguar_done();
292         version_done();
293         memory_done();
294         log_done();     
295         console.close();                                                                        // Close SDL items as last!
296
297     return 0;
298 }
299
300 //
301 // Generic ROM loading
302 //
303 uint32 JaguarLoadROM(uint8 * rom, char * path)
304 {
305         uint32 romSize;
306
307         WriteLog("JagEm: Loading %s...", path);
308
309         char * ext = strrchr(path, '.');
310         if (strcmpi(ext, ".zip") == 0)
311         {
312                 // Handle ZIP file loading here...
313                 WriteLog("(ZIPped)...");
314
315                 if (load_zipped_file(0, 0, path, NULL, &rom, &romSize) == -1)
316                 {
317                         WriteLog("Failed!\n");
318                         log_done();
319                         exit(0);
320                 }
321         }
322         else
323         {
324                 FILE * fp = fopen(path, "rb");
325
326                 if (fp == NULL)
327                 {
328                         WriteLog("Failed!\n");
329                         log_done();
330                         exit(0);
331                 }
332
333                 fseek(fp, 0, SEEK_END);
334                 romSize = ftell(fp);
335                 fseek(fp, 0, SEEK_SET);
336                 fread(rom, 1, romSize, fp);
337                 fclose(fp);
338         }
339
340         WriteLog("OK (%i bytes)\n", romSize);
341         return romSize;
342 }
343
344 //
345 // Jaguar cartridge ROM loading
346 //
347 void JaguarLoadCart(uint8 * mem, char * path)
348 {
349         uint32 romsize = JaguarLoadROM(mem, path);
350         jaguar_mainRom_crc32 = crc32_calcCheckSum(jaguar_mainRom, romsize);
351         WriteLog( "CRC: %08X\n", (unsigned int)jaguar_mainRom_crc32);
352 }