]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
ef6ea79772d992bf0c0184773e7439e0750bffe2
[virtualjaguar] / src / vj.cpp
1 //
2 // Virtual Jaguar Emulator
3 //
4 // Original codebase by Cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes/enhancements by James L. Hammons and Adam Green
7 //
8
9 #ifdef __GCCUNIX__
10 #include <unistd.h>
11 #endif
12
13 //#include <dirent.h>                                                                   // POSIX, but should compile with linux & mingw...
14 #include <time.h>
15 #include <SDL.h>
16 #include "jaguar.h"
17 //#include "crc32.h"
18 //#include "zlib.h"
19 //#include "unzip.h"
20 #include "video.h"
21 #include "gui.h"
22 #include "sdlemu_opengl.h"
23 #include "settings.h"                                                           // Pull in "vjs" struct
24
25 // Uncomment this for speed control (?)
26 //#define SPEED_CONTROL
27
28 // Private function prototypes
29
30 //uint32 JaguarLoadROM(uint8 *, char *);
31 //void JaguarLoadCart(uint8 *, char *);
32 //int gzfilelength(gzFile gd);
33
34 // External variables
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[MAX_PATH];
47
48 //These should go into video.cpp...
49 //And they will!
50 //SDL_Surface * surface, * mainSurface;
51 //int16 * backbuffer = NULL;
52 //SDL_Joystick * joystick;
53 //Uint32 mainSurfaceFlags = SDL_SWSURFACE;
54
55 bool finished = false;
56 bool showGUI = false;
57 bool showMessage = false;
58 uint32 showMessageTimeout;
59 char messageBuffer[200];
60
61 //
62 // The main emulator loop (what else?)
63 //
64 //Maybe we should move the video stuff to TOM? Makes more sense to put it there...
65 //Actually, it would probably be better served in VIDEO.CPP... !!! FIX !!! [DONE]
66 uint32 totalFrames;//temp, so we can grab this from elsewhere...
67 int main(int argc, char * argv[])
68 {
69         uint32 startTime;//, totalFrames;//, endTime;//, w, h;
70         uint32 nNormalLast = 0;
71         int32 nNormalFrac = 0; 
72     int32 nFrameskip = 0;                                                       // Default: Show every frame
73     int32 nFrame = 0;                                                           // No. of Frame
74
75         printf("Virtual Jaguar GCC/SDL Portable Jaguar Emulator v1.0.7\n");
76         printf("Based upon Virtual Jaguar core v1.0.0 by Cal2 of Potato emulation.\n");
77         printf("Written by Niels Wagenaar (Linux/WIN32), Caz (BeOS),\n");
78         printf("James L. Hammons (WIN32) and Adam Green (MacOS)\n");
79         printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
80
81         bool haveCart = false;                                                  // Assume there is no cartridge...!
82
83         log_init("vj.log");
84         LoadVJSettings();                                                               // Get config file settings...
85
86         // Check the switches... ;-)
87         // NOTE: Command line switches can override any config file settings, thus the
88         //       proliferation of the noXXX switches. ;-)
89
90         for(int i=1; i<argc || argv[i]!=NULL; i++)
91         {
92                 // This would be the most likely place to do the cart loading...
93                 if (argv[i][0] != '-')
94                         haveCart = true;                                                // It looks like we have a cartridge!
95
96                 if (!strcmp(argv[i], "-joystick")) 
97                         vjs.useJoystick = true;
98
99                 if (!strcmp(argv[i], "-joyport"))
100                 {
101                         vjs.joyport = atoi(argv[++i]) + 1;
102                         if (vjs.joyport > 3)
103                                 vjs.joyport = 3;
104                 }
105
106                 if (!strcmp(argv[i], "-frameskip"))
107                 {
108                         nFrameskip = atoi(argv[++i]) + 1;
109                         if (nFrameskip > 10)
110                                 nFrameskip = 10;
111 #ifdef SPEED_CONTROL
112                         nFrameskip = 0;
113 #endif
114                 }
115
116                 if (!strcmp(argv[i], "-bios"))
117                         vjs.useJaguarBIOS = true;
118
119                 if (!strcmp(argv[i], "-nobios"))
120                         vjs.useJaguarBIOS = false;
121
122                 if (!strcmp(argv[i], "-dsp"))
123                         vjs.DSPEnabled = true;
124
125                 if (!strcmp(argv[i], "-nodsp"))
126                         vjs.DSPEnabled = false;
127
128                 if (!strcmp(argv[i], "-pipeline"))
129                         vjs.usePipelinedDSP = true;
130
131                 if (!strcmp(argv[i], "-nopipeline"))
132                         vjs.usePipelinedDSP = false;
133
134                 if (!strcmp(argv[i], "-gl"))
135                         vjs.useOpenGL = true;
136
137                 if (!strcmp(argv[i], "-nogl"))
138                         vjs.useOpenGL = false;
139
140                 if (!strcmp(argv[i], "-fullscreen")) 
141                         vjs.fullscreen = true;
142
143                 if (!strcmp(argv[i], "-window")) 
144                         vjs.fullscreen = false;
145
146                 if (!strcmp(argv[i], "-pal"))
147                         vjs.hardwareTypeNTSC = false;
148
149                 if (!strcmp(argv[i], "-ntsc"))
150                         vjs.hardwareTypeNTSC = true;
151
152                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
153                 {
154                     printf("Usage: \n\n");
155                         printf("vj [romfile] [switches]\n");
156                         printf("  -? or -help     : Display usage and switches                \n");
157                         printf("  -frameskip 1-10 : Enable frameskip 1 - 10 (default: none)   \n");
158                         printf("  -joystick       : Enable joystick/gamepad                   \n");
159                         printf("  -joyport 0-3    : Select desired joystick port              \n");
160                         printf("  -bios           : Boot cart using Jaguar BIOS ROM           \n");
161                         printf("  -nobios         : Boot cart without using Jaguar BIOS ROM   \n");
162                         printf("  -dsp            : Force VJ to use the DSP                   \n");
163                         printf("  -nodsp          : Force VJ to run without the DSP           \n");
164                         printf("  -pipeline       : Use the DSP pipelined core                \n");
165                         printf("  -nopipeline     : Use the DSP non-pipelined core            \n");
166                         printf("  -gl             : Use OpenGL rendering                      \n");
167                         printf("  -nogl           : Use old non-OpenGL rendering              \n");
168                         printf("  -fullscreen     : Enable fullscreen mode (default: windowed)\n");
169                         printf("  -window         : Enable windowed mode                      \n");
170                         printf("  -pal            : Force VJ to PAL mode (default: NTSC)      \n");
171                         printf("  -ntsc           : Force VJ to NTSC mode                     \n");
172                         printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
173                         printf("with the Jaguar BIOS.\n");
174                         return 1;
175                 }
176     }
177
178         memory_init();
179         version_init();
180         version_display(log_get());
181         jaguar_init();
182
183         // Get the BIOS ROM
184         if (vjs.useJaguarBIOS)
185                 JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath);
186
187         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
188
189         InitVideo();
190         InitGUI();
191
192         // Get the cartridge ROM (if passed in)
193         // Now with crunchy GUI goodness!
194 //      JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : vjs.ROMPath));
195 //Need to find a better way to handle this crap...
196         GUIMain();
197
198 /*      jaguar_reset();
199         
200         totalFrames = 0;
201         startTime = clock();
202         nNormalLast = 0;                                                                        // Last value of timeGetTime()
203         nNormalFrac = 0;                                                                        // Extra fraction we did
204         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
205
206         while (!finished)
207         {
208 #ifdef SPEED_CONTROL
209                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
210                                                                                                                 // nTime est en mili-secondes.
211                 // détermine le nombre de trames à passer + 1
212                 nCount = (nTime * 600 - nNormalFrac) / 10000;
213
214                 // si le nombre de trames à passer + 1 est nul ou négatif,
215                 // ne rien faire pendant 2 ms
216                 if (nCount <= 0) 
217                 { 
218                         //Sleep(2); 
219                         //SDL_Delay(1);
220                 } // No need to do anything for a bit
221                 else
222                 {
223                         nNormalFrac += nCount * 10000;                          // 
224                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
225                         nNormalFrac %= 600;                                                     // 
226
227                         // Pas plus de 9 (10-1) trames non affichées 
228                         if (nCount > 10)
229                                 nCount = 10;
230                         for(int i=0; i<nCount-1; i++)
231                                 jaguar_exec(backbuffer, false);
232 #endif
233             // Set up new backbuffer with new pixels and data
234                         JaguarExecute(backbuffer, true);
235                         totalFrames++;
236 //WriteLog("Frame #%u...\n", totalFrames);
237 //extern bool doDSPDis;
238 //if (totalFrames == 373)
239 //      doDSPDis = true;
240
241                         // Some QnD GUI stuff here...
242                         if (showGUI)
243                         {
244                                 extern uint32 gpu_pc, dsp_pc;
245                                 DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
246                                 DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
247                         }
248
249                         // Simple frameskip
250                         if (nFrame == nFrameskip)
251                         {
252                                 RenderBackbuffer();
253                                 nFrame = 0;
254                         }
255                         else
256                                 nFrame++;
257
258                         joystick_exec();
259
260 #ifdef SPEED_CONTROL
261                 }
262 #endif
263         }*/
264
265         int elapsedTime = clock() - startTime;
266         int fps = (1000 * totalFrames) / elapsedTime;
267         WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
268
269         jaguar_done();
270         version_done();
271         memory_done();
272         VideoDone();                                                                    // Free SDL components last...!
273         log_done();     
274
275     return 0;
276 }