annotate src/sdl/TestEmu.cpp @ 34:6bde5b67a2b8

saving progress
author Robert McIntyre <rlm@mit.edu>
date Mon, 05 Mar 2012 01:58:52 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
rlm@1 2 // Copyright (C) 1999-2003 Forgotten
rlm@1 3 // Copyright (C) 2004 Forgotten and the VBA development team
rlm@1 4
rlm@1 5 // This program is free software; you can redistribute it and/or modify
rlm@1 6 // it under the terms of the GNU General Public License as published by
rlm@1 7 // the Free Software Foundation; either version 2, or(at your option)
rlm@1 8 // any later version.
rlm@1 9 //
rlm@1 10 // This program is distributed in the hope that it will be useful,
rlm@1 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@1 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
rlm@1 13 // GNU General Public License for more details.
rlm@1 14 //
rlm@1 15 // You should have received a copy of the GNU General Public License
rlm@1 16 // along with this program; if not, write to the Free Software Foundation,
rlm@1 17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
rlm@1 18
rlm@1 19 #include <stdarg.h>
rlm@1 20 #include <stdlib.h>
rlm@1 21 #include <stdio.h>
rlm@1 22 #include <string.h>
rlm@1 23 #include <sys/types.h>
rlm@1 24 #include <sys/stat.h>
rlm@1 25
rlm@1 26 #include "AutoBuild.h"
rlm@1 27
rlm@1 28 #include "SDL.h"
rlm@1 29 #include "debugger.h"
rlm@1 30 #include "common/System.h"
rlm@1 31 #include "common/unzip.h"
rlm@1 32 #include "common/Util.h"
rlm@1 33 #include "gba/GBA.h"
rlm@1 34 #include "gba/GBAGlobals.h"
rlm@1 35 #include "gba/GBASound.h"
rlm@1 36 #include "gb/GB.h"
rlm@1 37 #include "gb/gbGlobals.h"
rlm@1 38
rlm@1 39 #ifndef WIN32
rlm@1 40 # include <unistd.h>
rlm@1 41 # define GETCWD getcwd
rlm@1 42 #else // WIN32
rlm@1 43 # include <direct.h>
rlm@1 44 # define GETCWD _getcwd
rlm@1 45 #endif // WIN32
rlm@1 46
rlm@1 47 #ifdef MMX
rlm@1 48 extern "C" bool cpu_mmx;
rlm@1 49 #endif
rlm@1 50 extern bool8 soundEcho;
rlm@1 51 extern bool8 soundLowPass;
rlm@1 52 extern bool8 soundReverse;
rlm@1 53
rlm@1 54 extern void remoteInit();
rlm@1 55 extern void remoteCleanUp();
rlm@1 56 extern void remoteStubMain();
rlm@1 57 extern void remoteStubSignal(int,int);
rlm@1 58 extern void remoteOutput(char *, u32);
rlm@1 59 extern void remoteSetProtocol(int);
rlm@1 60 extern void remoteSetPort(int);
rlm@1 61 extern void debuggerOutput(char *, u32);
rlm@1 62
rlm@1 63 struct EmulatedSystem emulator;
rlm@1 64
rlm@1 65 int systemRedShift = 0;
rlm@1 66 int systemBlueShift = 16;
rlm@1 67 int systemGreenShift = 8;
rlm@1 68 int systemColorDepth = 32;
rlm@1 69 int systemDebug = 0;
rlm@1 70 int systemVerbose = 0;
rlm@1 71 int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
rlm@1 72
rlm@1 73 int sensorX = 2047;
rlm@1 74 int sensorY = 2047;
rlm@1 75 bool sensorOn = false;
rlm@1 76
rlm@1 77 int cartridgeType = 3;
rlm@1 78 int captureFormat = 0;
rlm@1 79
rlm@1 80 int emulating = 0;
rlm@1 81 int RGB_LOW_BITS_MASK=0x821;
rlm@1 82 int systemFrameSkip = 0;
rlm@1 83 u32 systemColorMap32[0x10000];
rlm@1 84 u16 systemColorMap16[0x10000];
rlm@1 85 u16 systemGbPalette[24];
rlm@1 86 char filename[2048];
rlm@1 87 char biosFileName[2048];
rlm@1 88 char captureDir[2048];
rlm@1 89 char saveDir[2048];
rlm@1 90 char batteryDir[2048];
rlm@1 91
rlm@1 92 int throttle = 0;
rlm@1 93
rlm@1 94 bool paused = false;
rlm@1 95 bool debugger = true;
rlm@1 96 bool debuggerStub = false;
rlm@1 97 bool systemSoundOn = false;
rlm@1 98 bool removeIntros = false;
rlm@1 99 int sdlFlashSize = 0;
rlm@1 100 int sdlAutoIPS = 1;
rlm@1 101 int sdlRtcEnable = 0;
rlm@1 102 int sdlAgbPrint = 0;
rlm@1 103
rlm@1 104 int sdlDefaultJoypad = 0;
rlm@1 105
rlm@1 106 u16 motion[4] = {
rlm@1 107 SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2
rlm@1 108 };
rlm@1 109
rlm@1 110 extern void debuggerSignal(int,int);
rlm@1 111
rlm@1 112 void (*dbgMain)() = debuggerMain;
rlm@1 113 void (*dbgSignal)(int,int) = debuggerSignal;
rlm@1 114 void (*dbgOutput)(char *, u32) = debuggerOutput;
rlm@1 115
rlm@1 116 char *sdlGetFilename(char *name)
rlm@1 117 {
rlm@1 118 static char filebuffer[2048];
rlm@1 119
rlm@1 120 int len = strlen(name);
rlm@1 121
rlm@1 122 char *p = name + len - 1;
rlm@1 123
rlm@1 124 while(true) {
rlm@1 125 if(*p == '/' ||
rlm@1 126 *p == '\\') {
rlm@1 127 p++;
rlm@1 128 break;
rlm@1 129 }
rlm@1 130 len--;
rlm@1 131 p--;
rlm@1 132 if(len == 0)
rlm@1 133 break;
rlm@1 134 }
rlm@1 135
rlm@1 136 if(len == 0)
rlm@1 137 strcpy(filebuffer, name);
rlm@1 138 else
rlm@1 139 strcpy(filebuffer, p);
rlm@1 140 return filebuffer;
rlm@1 141 }
rlm@1 142
rlm@1 143 void usage(char *cmd)
rlm@1 144 {
rlm@1 145 printf("%s file-name\n",cmd);
rlm@1 146 }
rlm@1 147
rlm@1 148 int main(int argc, char **argv)
rlm@1 149 {
rlm@1 150 fprintf(stderr,"VisualBoyAdvance-Test version %s\n", VERSION);
rlm@1 151
rlm@1 152 captureDir[0] = 0;
rlm@1 153 saveDir[0] = 0;
rlm@1 154 batteryDir[0] = 0;
rlm@1 155
rlm@1 156 char buffer[1024];
rlm@1 157
rlm@1 158 systemFrameSkip = frameSkip = 2;
rlm@1 159 gbBorderOn = 0;
rlm@1 160
rlm@1 161 parseDebug = true;
rlm@1 162
rlm@1 163 if(!debuggerStub) {
rlm@1 164 if(argc <= 1) {
rlm@1 165 systemMessage(0,"Missing image name");
rlm@1 166 usage(argv[0]);
rlm@1 167 exit(-1);
rlm@1 168 }
rlm@1 169 }
rlm@1 170
rlm@1 171 for(int i = 0; i < 24;) {
rlm@1 172 systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10);
rlm@1 173 systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10);
rlm@1 174 systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10);
rlm@1 175 systemGbPalette[i++] = 0;
rlm@1 176 }
rlm@1 177
rlm@1 178 if(argc == 2) {
rlm@1 179 char *szFile = argv[optind];
rlm@1 180 bool failed = false;
rlm@1 181 if(utilIsZipFile(szFile)) {
rlm@1 182 unzFile unz = unzOpen(szFile);
rlm@1 183
rlm@1 184 if(unz == NULL) {
rlm@1 185 systemMessage(0, "Cannot open file %s", szFile);
rlm@1 186 exit(-1);
rlm@1 187 }
rlm@1 188 int r = unzGoToFirstFile(unz);
rlm@1 189
rlm@1 190 if(r != UNZ_OK) {
rlm@1 191 unzClose(unz);
rlm@1 192 systemMessage(0, "Bad ZIP file %s", szFile);
rlm@1 193 exit(-1);
rlm@1 194 }
rlm@1 195
rlm@1 196 bool found = false;
rlm@1 197
rlm@1 198 unz_file_info info;
rlm@1 199
rlm@1 200 while(true) {
rlm@1 201 r = unzGetCurrentFileInfo(unz,
rlm@1 202 &info,
rlm@1 203 buffer,
rlm@1 204 sizeof(buffer),
rlm@1 205 NULL,
rlm@1 206 0,
rlm@1 207 NULL,
rlm@1 208 0);
rlm@1 209
rlm@1 210 if(r != UNZ_OK) {
rlm@1 211 unzClose(unz);
rlm@1 212 systemMessage(0,"Bad ZIP file %s", szFile);
rlm@1 213 exit(-1);
rlm@1 214 }
rlm@1 215
rlm@1 216 if(utilIsGBImage(buffer)) {
rlm@1 217 found = true;
rlm@1 218 cartridgeType = 1;
rlm@1 219 break;
rlm@1 220 }
rlm@1 221 if(utilIsGBAImage(buffer)) {
rlm@1 222 found = true;
rlm@1 223 cartridgeType = 0;
rlm@1 224 break;
rlm@1 225 }
rlm@1 226
rlm@1 227 r = unzGoToNextFile(unz);
rlm@1 228
rlm@1 229 if(r != UNZ_OK)
rlm@1 230 break;
rlm@1 231 }
rlm@1 232
rlm@1 233 if(!found) {
rlm@1 234 unzClose(unz);
rlm@1 235 systemMessage(0, "No image found on ZIP file %s", szFile);
rlm@1 236 exit(-1);
rlm@1 237 }
rlm@1 238
rlm@1 239 unzClose(unz);
rlm@1 240 }
rlm@1 241
rlm@1 242 if(utilIsGBImage(szFile) || cartridgeType == 1) {
rlm@1 243 failed = !gbLoadRom(szFile);
rlm@1 244 cartridgeType = 1;
rlm@1 245 emulator = GBSystem;
rlm@1 246 } else if(utilIsGBAImage(szFile) || cartridgeType == 0) {
rlm@1 247 failed = !CPULoadRom(szFile);
rlm@1 248 cartridgeType = 0;
rlm@1 249 emulator = GBASystem;
rlm@1 250
rlm@1 251 //CPUInit(biosFileName, useBios);
rlm@1 252 CPUInit();
rlm@1 253 CPUReset();
rlm@1 254 } else {
rlm@1 255 systemMessage(0, "Unknown file type %s", szFile);
rlm@1 256 exit(-1);
rlm@1 257 }
rlm@1 258
rlm@1 259 if(failed) {
rlm@1 260 systemMessage(0, "Failed to load file %s", szFile);
rlm@1 261 exit(-1);
rlm@1 262 }
rlm@1 263 strcpy(filename, szFile);
rlm@1 264 char *p = strrchr(filename, '.');
rlm@1 265
rlm@1 266 if(p)
rlm@1 267 *p = 0;
rlm@1 268 } else {
rlm@1 269 cartridgeType = 0;
rlm@1 270 strcpy(filename, "gnu_stub");
rlm@1 271 rom = (u8 *)malloc(0x2000000);
rlm@1 272 workRAM = (u8 *)calloc(1, 0x40000);
rlm@1 273 bios = (u8 *)calloc(1,0x4000);
rlm@1 274 internalRAM = (u8 *)calloc(1,0x8000);
rlm@1 275 paletteRAM = (u8 *)calloc(1,0x400);
rlm@1 276 vram = (u8 *)calloc(1, 0x20000);
rlm@1 277 oam = (u8 *)calloc(1, 0x400);
rlm@1 278 pix = (u8 *)calloc(1, 4 * 240 * 160);
rlm@1 279 ioMem = (u8 *)calloc(1, 0x400);
rlm@1 280
rlm@1 281 emulator = GBASystem;
rlm@1 282
rlm@1 283 //CPUInit(biosFileName, useBios);
rlm@1 284 CPUInit();
rlm@1 285 CPUReset();
rlm@1 286 }
rlm@1 287
rlm@1 288 if(debuggerStub)
rlm@1 289 remoteInit();
rlm@1 290
rlm@1 291 if(cartridgeType == 0) {
rlm@1 292 } else if (cartridgeType == 1) {
rlm@1 293 if(gbBorderOn) {
rlm@1 294 gbBorderLineSkip = 256;
rlm@1 295 gbBorderColumnSkip = 48;
rlm@1 296 gbBorderRowSkip = 40;
rlm@1 297 } else {
rlm@1 298 gbBorderLineSkip = 160;
rlm@1 299 gbBorderColumnSkip = 0;
rlm@1 300 gbBorderRowSkip = 0;
rlm@1 301 }
rlm@1 302 } else {
rlm@1 303 }
rlm@1 304
rlm@1 305 for(int i = 0; i < 0x10000; i++) {
rlm@1 306 systemColorMap32[i] = ((i & 0x1f) << systemRedShift) |
rlm@1 307 (((i & 0x3e0) >> 5) << systemGreenShift) |
rlm@1 308 (((i & 0x7c00) >> 10) << systemBlueShift);
rlm@1 309 }
rlm@1 310
rlm@1 311 emulating = 1;
rlm@1 312 soundInit();
rlm@1 313
rlm@1 314 while(emulating) {
rlm@1 315 if(!paused) {
rlm@1 316 if(debugger && emulator.emuHasDebugger)
rlm@1 317 dbgMain();
rlm@1 318 else
rlm@1 319 emulator.emuMain(emulator.emuCount);
rlm@1 320 }
rlm@1 321 }
rlm@1 322 emulating = 0;
rlm@1 323 fprintf(stderr,"Shutting down\n");
rlm@1 324 remoteCleanUp();
rlm@1 325 soundShutdown();
rlm@1 326
rlm@1 327 if(gbRom != NULL || rom != NULL) {
rlm@1 328 emulator.emuCleanUp();
rlm@1 329 }
rlm@1 330
rlm@1 331 return 0;
rlm@1 332 }
rlm@1 333
rlm@1 334 void systemMessage(int num, const char *msg, ...)
rlm@1 335 {
rlm@1 336 char buffer[2048];
rlm@1 337 va_list valist;
rlm@1 338
rlm@1 339 va_start(valist, msg);
rlm@1 340 vsprintf(buffer, msg, valist);
rlm@1 341
rlm@1 342 fprintf(stderr, "%s\n", buffer);
rlm@1 343 va_end(valist);
rlm@1 344 }
rlm@1 345
rlm@1 346 void systemDrawScreen()
rlm@1 347 {
rlm@1 348 }
rlm@1 349
rlm@1 350 bool systemReadJoypads()
rlm@1 351 {
rlm@1 352 return true;
rlm@1 353 }
rlm@1 354
rlm@1 355 u32 systemReadJoypad(int,bool)
rlm@1 356 {
rlm@1 357 return 0;
rlm@1 358 }
rlm@1 359
rlm@1 360 void systemShowSpeed(int speed)
rlm@1 361 {
rlm@1 362 }
rlm@1 363
rlm@1 364 void system10Frames(int rate)
rlm@1 365 {
rlm@1 366 }
rlm@1 367
rlm@1 368 void systemFrame(int)
rlm@1 369 {
rlm@1 370 }
rlm@1 371
rlm@1 372 void systemSetTitle(const char *title)
rlm@1 373 {
rlm@1 374 }
rlm@1 375
rlm@1 376 int systemScreenCapture(int a)
rlm@1 377 {
rlm@1 378 char buffer[2048];
rlm@1 379
rlm@1 380 if(captureFormat) {
rlm@1 381 if(captureDir[0])
rlm@1 382 sprintf(buffer, "%s/%s%02d.bmp", captureDir, sdlGetFilename(filename), a);
rlm@1 383 else
rlm@1 384 sprintf(buffer, "%s%02d.bmp", filename, a);
rlm@1 385
rlm@1 386 emulator.emuWriteBMP(buffer);
rlm@1 387 } else {
rlm@1 388 if(captureDir[0])
rlm@1 389 sprintf(buffer, "%s/%s%02d.png", captureDir, sdlGetFilename(filename), a);
rlm@1 390 else
rlm@1 391 sprintf(buffer, "%s%02d.png", filename, a);
rlm@1 392 emulator.emuWritePNG(buffer);
rlm@1 393 }
rlm@1 394
rlm@1 395 systemScreenMessage("Screen capture");
rlm@1 396 }
rlm@1 397
rlm@1 398 u32 systemReadJoypadExtended()
rlm@1 399 {
rlm@1 400 return 0;
rlm@1 401 }
rlm@1 402
rlm@1 403 void systemWriteDataToSoundBuffer()
rlm@1 404 {
rlm@1 405 }
rlm@1 406
rlm@1 407 bool systemSoundInit()
rlm@1 408 {
rlm@1 409 return true;
rlm@1 410 }
rlm@1 411
rlm@1 412 void systemSoundShutdown()
rlm@1 413 {
rlm@1 414 }
rlm@1 415
rlm@1 416 void systemSoundPause()
rlm@1 417 {
rlm@1 418 }
rlm@1 419
rlm@1 420 void systemSoundResume()
rlm@1 421 {
rlm@1 422 }
rlm@1 423
rlm@1 424 void systemSoundReset()
rlm@1 425 {
rlm@1 426 }
rlm@1 427
rlm@1 428 static int ticks = 0;
rlm@1 429
rlm@1 430 u32 systemGetClock()
rlm@1 431 {
rlm@1 432 return ticks++;
rlm@1 433 }
rlm@1 434
rlm@1 435 void systemUpdateMotionSensor()
rlm@1 436 {
rlm@1 437 }
rlm@1 438
rlm@1 439 int systemGetSensorX()
rlm@1 440 {
rlm@1 441 return 0;
rlm@1 442 }
rlm@1 443
rlm@1 444 int systemGetSensorY()
rlm@1 445 {
rlm@1 446 return 0;
rlm@1 447 }
rlm@1 448
rlm@1 449 void systemGbPrint(u8 *data,int pages,int feed,int palette, int contrast)
rlm@1 450 {
rlm@1 451 }
rlm@1 452
rlm@1 453 void systemScreenMessage(const char *msg, int slot, int duration, const char *colorList)
rlm@1 454 {
rlm@1 455 }
rlm@1 456
rlm@1 457 bool systemCanChangeSoundQuality()
rlm@1 458 {
rlm@1 459 return false;
rlm@1 460 }
rlm@1 461
rlm@1 462 bool systemPauseOnFrame()
rlm@1 463 {
rlm@1 464 return false;
rlm@1 465 }
rlm@1 466
rlm@1 467 void systemGbBorderOn()
rlm@1 468 {
rlm@1 469 }
rlm@1 470
rlm@1 471 bool sdlCheckJoyKey(int)
rlm@1 472 {
rlm@1 473 return true;
rlm@1 474 }
rlm@1 475
rlm@1 476 u16 checksumBIOS()
rlm@1 477 {
rlm@1 478 bool hasBIOS = false;
rlm@1 479 u8 * tempBIOS;
rlm@1 480 if(useBios)
rlm@1 481 {
rlm@1 482 tempBIOS = (u8 *)malloc(0x4000);
rlm@1 483 int size = 0x4000;
rlm@1 484 if(utilLoad(biosFileName,
rlm@1 485 utilIsGBABios,
rlm@1 486 tempBIOS,
rlm@1 487 size)) {
rlm@1 488 if(size == 0x4000)
rlm@1 489 hasBIOS = true;
rlm@1 490 }
rlm@1 491 }
rlm@1 492
rlm@1 493 u16 biosCheck = 0;
rlm@1 494 if(hasBIOS) {
rlm@1 495 for(int i = 0; i < 0x4000; i += 4)
rlm@1 496 biosCheck += *((u32 *)&tempBIOS[i]);
rlm@1 497 free(tempBIOS);
rlm@1 498 }
rlm@1 499
rlm@1 500 return biosCheck;
rlm@1 501 }