]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
Minor cleanups
[virtualjaguar] / src / vj.cpp
1 //
2 // Virtual Jaguar Emulator
3 //
4 // Original codebase by David Raingeard (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>                                                                     // Is this necessary anymore?
11 #endif
12
13 #include <time.h>
14 #include <SDL.h>
15 #include "jaguar.h"
16 #include "video.h"
17 #include "gui.h"
18 #include "sdlemu_opengl.h"
19 #include "settings.h"                                                           // Pull in "vjs" struct
20
21 // Uncomment this for speed control (?)
22 //#define SPEED_CONTROL
23
24 // Uncomment this to use built-in BIOS/CD-ROM BIOS
25 // You'll need a copy of jagboot.h & jagcd.h for this to work...!
26 //#define USE_BUILT_IN_BIOS
27
28 #ifdef USE_BUILT_IN_BIOS
29 #include "jagboot.h"
30 #include "jagcd.h"
31 #endif
32
33 // Private function prototypes
34
35 // External variables
36
37 extern uint8 * jaguar_mainRam;
38 extern uint8 * jaguar_mainRom;
39 extern uint8 * jaguar_bootRom;
40 extern uint8 * jaguar_CDBootROM;
41
42 // Global variables (export capable)
43 //should these even be here anymore?
44
45 bool finished = false;
46 bool showGUI = false;
47 bool showMessage = false;
48 uint32 showMessageTimeout;
49 char messageBuffer[200];
50 bool BIOSLoaded = false;
51 bool CDBIOSLoaded = false;
52
53 //
54 // The main emulator loop (what else?)
55 //
56 //Maybe we should move the video stuff to TOM? Makes more sense to put it there...
57 //Actually, it would probably be better served in VIDEO.CPP... !!! FIX !!! [DONE]
58 uint32 totalFrames;//temp, so we can grab this from elsewhere...
59 int main(int argc, char * argv[])
60 {
61 //      uint32 startTime;//, totalFrames;//, endTime;//, w, h;
62 //      uint32 nNormalLast = 0;
63 //      int32 nNormalFrac = 0; 
64         int32 nFrameskip = 0;                                                   // Default: Show every frame
65 //      int32 nFrame = 0;                                                               // No. of Frame
66
67         printf("Virtual Jaguar GCC/SDL Portable Jaguar Emulator v1.0.8\n");
68         printf("Based upon Virtual Jaguar core v1.0.0 by David Raingeard.\n");
69         printf("Written by Niels Wagenaar (Linux/WIN32), Carwin Jones (BeOS),\n");
70         printf("James L. Hammons (WIN32) and Adam Green (MacOS)\n");
71         printf("Contact: http://sdlemu.ngemu.com/ | sdlemu@ngemu.com\n");
72
73         bool haveCart = false;                                                  // Assume there is no cartridge...!
74
75         log_init("vj.log");
76         LoadVJSettings();                                                               // Get config file settings...
77
78         // Check the switches... ;-)
79         // NOTE: Command line switches can override any config file settings, thus the
80         //       proliferation of the noXXX switches. ;-)
81
82         for(int i=1; i<argc || argv[i]!=NULL; i++)
83         {
84                 // This would be the most likely place to do the cart loading...
85                 if (argv[i][0] != '-')
86                         haveCart = true;                                                // It looks like we have a cartridge!
87
88                 if (!strcmp(argv[i], "-joystick")) 
89                         vjs.useJoystick = true;
90
91                 if (!strcmp(argv[i], "-joyport"))
92                 {
93                         vjs.joyport = atoi(argv[++i]) + 1;
94                         if (vjs.joyport > 3)
95                                 vjs.joyport = 3;
96                 }
97
98                 if (!strcmp(argv[i], "-frameskip"))
99                 {
100                         nFrameskip = atoi(argv[++i]) + 1;
101                         if (nFrameskip > 10)
102                                 nFrameskip = 10;
103 #ifdef SPEED_CONTROL
104                         nFrameskip = 0;
105 #endif
106                 }
107
108                 if (!strcmp(argv[i], "-bios"))
109                         vjs.useJaguarBIOS = true;
110
111                 if (!strcmp(argv[i], "-nobios"))
112                         vjs.useJaguarBIOS = false;
113
114                 if (!strcmp(argv[i], "-dsp"))
115                         vjs.DSPEnabled = true;
116
117                 if (!strcmp(argv[i], "-nodsp"))
118                         vjs.DSPEnabled = false;
119
120                 if (!strcmp(argv[i], "-pipeline"))
121                         vjs.usePipelinedDSP = true;
122
123                 if (!strcmp(argv[i], "-nopipeline"))
124                         vjs.usePipelinedDSP = false;
125
126                 if (!strcmp(argv[i], "-gl"))
127                         vjs.useOpenGL = true;
128
129                 if (!strcmp(argv[i], "-nogl"))
130                         vjs.useOpenGL = false;
131
132                 if (!strcmp(argv[i], "-fullscreen")) 
133                         vjs.fullscreen = true;
134
135                 if (!strcmp(argv[i], "-window")) 
136                         vjs.fullscreen = false;
137
138                 if (!strcmp(argv[i], "-pal"))
139                         vjs.hardwareTypeNTSC = false;
140
141                 if (!strcmp(argv[i], "-ntsc"))
142                         vjs.hardwareTypeNTSC = true;
143
144                 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "-?"))
145                 {
146                     printf("Usage: \n\n");
147                         printf("vj [romfile] [switches]\n");
148                         printf("  -? or -help     : Display usage and switches                \n");
149                         printf("  -frameskip 1-10 : Enable frameskip 1 - 10 (default: none)   \n");
150                         printf("  -joystick       : Enable joystick/gamepad                   \n");
151                         printf("  -joyport 0-3    : Select desired joystick port              \n");
152                         printf("  -bios           : Boot cart using Jaguar BIOS ROM           \n");
153                         printf("  -nobios         : Boot cart without using Jaguar BIOS ROM   \n");
154                         printf("  -dsp            : Force VJ to use the DSP                   \n");
155                         printf("  -nodsp          : Force VJ to run without the DSP           \n");
156                         printf("  -pipeline       : Use the DSP pipelined core                \n");
157                         printf("  -nopipeline     : Use the DSP non-pipelined core            \n");
158                         printf("  -gl             : Use OpenGL rendering                      \n");
159                         printf("  -nogl           : Use old non-OpenGL rendering              \n");
160                         printf("  -fullscreen     : Enable fullscreen mode (default: windowed)\n");
161                         printf("  -window         : Enable windowed mode                      \n");
162                         printf("  -pal            : Force VJ to PAL mode (default: NTSC)      \n");
163                         printf("  -ntsc           : Force VJ to NTSC mode                     \n");
164                         printf("\nInvoking Virtual Jagaur with no ROM file will cause it to boot up\n");
165                         printf("with the VJ GUI.\n");
166                         return 1;
167                 }
168     }
169
170         // Set up SDL library
171         if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
172 //              | SDL_INIT_CDROM) < 0)
173 //              | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE) < 0)
174         {
175                 WriteLog("VJ: Could not initialize the SDL library: %s\n", SDL_GetError());
176                 return -1;
177         }
178
179         WriteLog("VJ: SDL successfully initialized.\n");
180
181 WriteLog("Initializing memory subsystem...\n");
182         InitMemory();
183 WriteLog("Initializing version...\n");
184         InitVersion();
185         version_display(log_get());
186 WriteLog("Initializing jaguar subsystem...\n");
187         jaguar_init();
188
189         // Get the BIOS ROM
190 #ifdef USE_BUILT_IN_BIOS
191         WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
192         memcpy(jaguar_bootRom, jagBootROM, 0x20000);
193         memcpy(jaguar_CDBootROM, jagCDROM, 0x40000);
194         BIOSLoaded = CDBIOSLoaded = true;
195 #else
196 //      if (vjs.useJaguarBIOS)
197 // What would be nice here would be a way to check if the BIOS was loaded so that we
198 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
199 WriteLog("About to attempt to load BIOSes...\n");
200         BIOSLoaded = (JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath) == 0x20000 ? true : false);
201         WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
202         CDBIOSLoaded = (JaguarLoadROM(jaguar_CDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
203         WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
204 #endif
205
206         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
207
208 WriteLog("Initializing video subsystem...\n");
209         InitVideo();
210 WriteLog("Initializing GUI subsystem...\n");
211         InitGUI();
212
213         // Get the cartridge ROM (if passed in)
214         // Now with crunchy GUI goodness!
215 //      JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : vjs.ROMPath));
216 //Need to find a better way to handle this crap...
217 WriteLog("About to start GUI...\n");
218 //      GUIMain();
219         GUIMain(haveCart ? argv[1] : NULL);
220
221 /*      jaguar_reset();
222         
223         totalFrames = 0;
224         startTime = clock();
225         nNormalLast = 0;                                                                        // Last value of timeGetTime()
226         nNormalFrac = 0;                                                                        // Extra fraction we did
227         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
228
229         while (!finished)
230         {
231 #ifdef SPEED_CONTROL
232                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
233                                                                                                                 // nTime est en mili-secondes.
234                 // détermine le nombre de trames à passer + 1
235                 nCount = (nTime * 600 - nNormalFrac) / 10000;
236
237                 // si le nombre de trames à passer + 1 est nul ou négatif,
238                 // ne rien faire pendant 2 ms
239                 if (nCount <= 0) 
240                 { 
241                         //Sleep(2); 
242                         //SDL_Delay(1);
243                 } // No need to do anything for a bit
244                 else
245                 {
246                         nNormalFrac += nCount * 10000;                          // 
247                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
248                         nNormalFrac %= 600;                                                     // 
249
250                         // Pas plus de 9 (10-1) trames non affichées 
251                         if (nCount > 10)
252                                 nCount = 10;
253                         for(int i=0; i<nCount-1; i++)
254                                 jaguar_exec(backbuffer, false);
255 #endif
256             // Set up new backbuffer with new pixels and data
257                         JaguarExecute(backbuffer, true);
258                         totalFrames++;
259 //WriteLog("Frame #%u...\n", totalFrames);
260 //extern bool doDSPDis;
261 //if (totalFrames == 373)
262 //      doDSPDis = true;
263
264                         // Some QnD GUI stuff here...
265                         if (showGUI)
266                         {
267                                 extern uint32 gpu_pc, dsp_pc;
268                                 DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
269                                 DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
270                         }
271
272                         // Simple frameskip
273                         if (nFrame == nFrameskip)
274                         {
275                                 RenderBackbuffer();
276                                 nFrame = 0;
277                         }
278                         else
279                                 nFrame++;
280
281                         joystick_exec();
282
283 #ifdef SPEED_CONTROL
284                 }
285 #endif
286         }*/
287
288 //This is no longer accurate...!
289 //      int elapsedTime = clock() - startTime;
290 //      int fps = (1000 * totalFrames) / elapsedTime;
291 //      WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
292
293         jaguar_done();
294         VersionDone();
295         MemoryDone();
296         VideoDone();
297         log_done();     
298
299         // Free SDL components last...!
300 //      SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_CDROM);
301         SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER);
302         SDL_Quit();
303
304     return 0;
305 }