]> Shamusworld >> Repos - virtualjaguar/blob - src/vj.cpp
74a5629850f64d497c206cc117e1844b8933593a
[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
163                 | SDL_INIT_CDROM | SDL_INIT_NOPARACHUTE) < 0)
164         {
165                 WriteLog("VJ: Could not initialize the SDL library: %s\n", SDL_GetError());
166                 return -1;
167         }
168         WriteLog("VJ: SDL successfully initialized.\n");
169
170 WriteLog("Initializing memory subsystem...\n");
171         InitMemory();
172 WriteLog("Initializing version...\n");
173         InitVersion();
174         version_display(log_get());
175 WriteLog("Initializing jaguar subsystem...\n");
176         jaguar_init();
177
178         // Get the BIOS ROM
179 //      if (vjs.useJaguarBIOS)
180 // What would be nice here would be a way to check if the BIOS was loaded so that we
181 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
182 WriteLog("About to attempt to load BIOSes...\n");
183         BIOSLoaded = (JaguarLoadROM(jaguar_bootRom, vjs.jagBootPath) == 0x20000 ? true : false);
184         WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
185         CDBIOSLoaded = (JaguarLoadROM(jaguar_CDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
186         WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
187
188         SET32(jaguar_mainRam, 0, 0x00200000);                   // Set top of stack...
189
190 WriteLog("Initializing video subsystem...\n");
191         InitVideo();
192 WriteLog("Initializing GUI subsystem...\n");
193         InitGUI();
194
195         // Get the cartridge ROM (if passed in)
196         // Now with crunchy GUI goodness!
197 //      JaguarLoadCart(jaguar_mainRom, (haveCart ? argv[1] : vjs.ROMPath));
198 //Need to find a better way to handle this crap...
199 WriteLog("About to start GUI...\n");
200         GUIMain();
201
202 /*      jaguar_reset();
203         
204         totalFrames = 0;
205         startTime = clock();
206         nNormalLast = 0;                                                                        // Last value of timeGetTime()
207         nNormalFrac = 0;                                                                        // Extra fraction we did
208         nNormalLast = SDL_GetTicks();                                           //timeGetTime();
209
210         while (!finished)
211         {
212 #ifdef SPEED_CONTROL
213                 nTime = SDL_GetTicks() - nNormalLast;                   // calcule le temps écoulé depuis le dernier affichage
214                                                                                                                 // nTime est en mili-secondes.
215                 // détermine le nombre de trames à passer + 1
216                 nCount = (nTime * 600 - nNormalFrac) / 10000;
217
218                 // si le nombre de trames à passer + 1 est nul ou négatif,
219                 // ne rien faire pendant 2 ms
220                 if (nCount <= 0) 
221                 { 
222                         //Sleep(2); 
223                         //SDL_Delay(1);
224                 } // No need to do anything for a bit
225                 else
226                 {
227                         nNormalFrac += nCount * 10000;                          // 
228                         nNormalLast += nNormalFrac / 600;                       // add the duration of nNormalFrac frames
229                         nNormalFrac %= 600;                                                     // 
230
231                         // Pas plus de 9 (10-1) trames non affichées 
232                         if (nCount > 10)
233                                 nCount = 10;
234                         for(int i=0; i<nCount-1; i++)
235                                 jaguar_exec(backbuffer, false);
236 #endif
237             // Set up new backbuffer with new pixels and data
238                         JaguarExecute(backbuffer, true);
239                         totalFrames++;
240 //WriteLog("Frame #%u...\n", totalFrames);
241 //extern bool doDSPDis;
242 //if (totalFrames == 373)
243 //      doDSPDis = true;
244
245                         // Some QnD GUI stuff here...
246                         if (showGUI)
247                         {
248                                 extern uint32 gpu_pc, dsp_pc;
249                                 DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
250                                 DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
251                         }
252
253                         // Simple frameskip
254                         if (nFrame == nFrameskip)
255                         {
256                                 RenderBackbuffer();
257                                 nFrame = 0;
258                         }
259                         else
260                                 nFrame++;
261
262                         joystick_exec();
263
264 #ifdef SPEED_CONTROL
265                 }
266 #endif
267         }*/
268
269 //This is no longer accurate...!
270 //      int elapsedTime = clock() - startTime;
271 //      int fps = (1000 * totalFrames) / elapsedTime;
272 //      WriteLog("VJ: Ran at an average of %i FPS.\n", fps);
273
274         jaguar_done();
275         VersionDone();
276         MemoryDone();
277         VideoDone();
278         log_done();     
279
280         // Free SDL components last...!
281         SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_CDROM);
282         SDL_Quit();
283
284     return 0;
285 }