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