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