diff src/sdl/TestEmu.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sdl/TestEmu.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,501 @@
     1.4 +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
     1.5 +// Copyright (C) 1999-2003 Forgotten
     1.6 +// Copyright (C) 2004 Forgotten and the VBA development team
     1.7 +
     1.8 +// This program is free software; you can redistribute it and/or modify
     1.9 +// it under the terms of the GNU General Public License as published by
    1.10 +// the Free Software Foundation; either version 2, or(at your option)
    1.11 +// any later version.
    1.12 +//
    1.13 +// This program is distributed in the hope that it will be useful,
    1.14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +// GNU General Public License for more details.
    1.17 +//
    1.18 +// You should have received a copy of the GNU General Public License
    1.19 +// along with this program; if not, write to the Free Software Foundation,
    1.20 +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.21 +
    1.22 +#include <stdarg.h>
    1.23 +#include <stdlib.h>
    1.24 +#include <stdio.h>
    1.25 +#include <string.h>
    1.26 +#include <sys/types.h>
    1.27 +#include <sys/stat.h>
    1.28 +
    1.29 +#include "AutoBuild.h"
    1.30 +
    1.31 +#include "SDL.h"
    1.32 +#include "debugger.h"
    1.33 +#include "common/System.h"
    1.34 +#include "common/unzip.h"
    1.35 +#include "common/Util.h"
    1.36 +#include "gba/GBA.h"
    1.37 +#include "gba/GBAGlobals.h"
    1.38 +#include "gba/GBASound.h"
    1.39 +#include "gb/GB.h"
    1.40 +#include "gb/gbGlobals.h"
    1.41 +
    1.42 +#ifndef WIN32
    1.43 +# include <unistd.h>
    1.44 +# define GETCWD getcwd
    1.45 +#else // WIN32
    1.46 +# include <direct.h>
    1.47 +# define GETCWD _getcwd
    1.48 +#endif // WIN32
    1.49 +
    1.50 +#ifdef MMX
    1.51 +extern "C" bool cpu_mmx;
    1.52 +#endif
    1.53 +extern bool8 soundEcho;
    1.54 +extern bool8 soundLowPass;
    1.55 +extern bool8 soundReverse;
    1.56 +
    1.57 +extern void remoteInit();
    1.58 +extern void remoteCleanUp();
    1.59 +extern void remoteStubMain();
    1.60 +extern void remoteStubSignal(int,int);
    1.61 +extern void remoteOutput(char *, u32);
    1.62 +extern void remoteSetProtocol(int);
    1.63 +extern void remoteSetPort(int);
    1.64 +extern void debuggerOutput(char *, u32);
    1.65 +
    1.66 +struct EmulatedSystem emulator;
    1.67 +
    1.68 +int systemRedShift = 0;
    1.69 +int systemBlueShift = 16;
    1.70 +int systemGreenShift = 8;
    1.71 +int systemColorDepth = 32;
    1.72 +int systemDebug = 0;
    1.73 +int systemVerbose = 0;
    1.74 +int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
    1.75 +
    1.76 +int sensorX = 2047;
    1.77 +int sensorY = 2047;
    1.78 +bool sensorOn = false;
    1.79 +
    1.80 +int cartridgeType = 3;
    1.81 +int captureFormat = 0;
    1.82 +
    1.83 +int emulating = 0;
    1.84 +int RGB_LOW_BITS_MASK=0x821;
    1.85 +int systemFrameSkip = 0;
    1.86 +u32 systemColorMap32[0x10000];
    1.87 +u16 systemColorMap16[0x10000];
    1.88 +u16 systemGbPalette[24];
    1.89 +char filename[2048];
    1.90 +char biosFileName[2048];
    1.91 +char captureDir[2048];
    1.92 +char saveDir[2048];
    1.93 +char batteryDir[2048];
    1.94 +
    1.95 +int throttle = 0;
    1.96 +
    1.97 +bool paused = false;
    1.98 +bool debugger = true;
    1.99 +bool debuggerStub = false;
   1.100 +bool systemSoundOn = false;
   1.101 +bool removeIntros = false;
   1.102 +int sdlFlashSize = 0;
   1.103 +int sdlAutoIPS = 1;
   1.104 +int sdlRtcEnable = 0;
   1.105 +int sdlAgbPrint = 0;
   1.106 +
   1.107 +int sdlDefaultJoypad = 0;
   1.108 +
   1.109 +u16 motion[4] = {
   1.110 +  SDLK_KP4, SDLK_KP6, SDLK_KP8, SDLK_KP2
   1.111 +};
   1.112 +
   1.113 +extern void debuggerSignal(int,int);
   1.114 +
   1.115 +void (*dbgMain)() = debuggerMain;
   1.116 +void (*dbgSignal)(int,int) = debuggerSignal;
   1.117 +void (*dbgOutput)(char *, u32) = debuggerOutput;
   1.118 +
   1.119 +char *sdlGetFilename(char *name)
   1.120 +{
   1.121 +  static char filebuffer[2048];
   1.122 +
   1.123 +  int len = strlen(name);
   1.124 +  
   1.125 +  char *p = name + len - 1;
   1.126 +  
   1.127 +  while(true) {
   1.128 +    if(*p == '/' ||
   1.129 +       *p == '\\') {
   1.130 +      p++;
   1.131 +      break;
   1.132 +    }
   1.133 +    len--;
   1.134 +    p--;
   1.135 +    if(len == 0)
   1.136 +      break;
   1.137 +  }
   1.138 +  
   1.139 +  if(len == 0)
   1.140 +    strcpy(filebuffer, name);
   1.141 +  else
   1.142 +    strcpy(filebuffer, p);
   1.143 +  return filebuffer;
   1.144 +}
   1.145 +
   1.146 +void usage(char *cmd)
   1.147 +{
   1.148 +  printf("%s file-name\n",cmd);
   1.149 +}
   1.150 +
   1.151 +int main(int argc, char **argv)
   1.152 +{
   1.153 +  fprintf(stderr,"VisualBoyAdvance-Test version %s\n", VERSION);
   1.154 +
   1.155 +  captureDir[0] = 0;
   1.156 +  saveDir[0] = 0;
   1.157 +  batteryDir[0] = 0;
   1.158 +  
   1.159 +  char buffer[1024];
   1.160 +
   1.161 +  systemFrameSkip = frameSkip = 2;
   1.162 +  gbBorderOn = 0;
   1.163 +
   1.164 +  parseDebug = true;
   1.165 +
   1.166 +  if(!debuggerStub) {
   1.167 +    if(argc <= 1) {
   1.168 +      systemMessage(0,"Missing image name");
   1.169 +      usage(argv[0]);
   1.170 +      exit(-1);
   1.171 +    }
   1.172 +  }
   1.173 +
   1.174 +  for(int i = 0; i < 24;) {
   1.175 +    systemGbPalette[i++] = (0x1f) | (0x1f << 5) | (0x1f << 10);
   1.176 +    systemGbPalette[i++] = (0x15) | (0x15 << 5) | (0x15 << 10);
   1.177 +    systemGbPalette[i++] = (0x0c) | (0x0c << 5) | (0x0c << 10);
   1.178 +    systemGbPalette[i++] = 0;
   1.179 +  }
   1.180 +
   1.181 +  if(argc == 2) {
   1.182 +    char *szFile = argv[optind];
   1.183 +    bool failed = false;
   1.184 +    if(utilIsZipFile(szFile)) {
   1.185 +      unzFile unz = unzOpen(szFile);
   1.186 +      
   1.187 +      if(unz == NULL) {
   1.188 +        systemMessage(0, "Cannot open file %s", szFile);
   1.189 +        exit(-1);
   1.190 +      }
   1.191 +      int r = unzGoToFirstFile(unz);
   1.192 +      
   1.193 +      if(r != UNZ_OK) {
   1.194 +        unzClose(unz);
   1.195 +        systemMessage(0, "Bad ZIP file %s", szFile);
   1.196 +        exit(-1);
   1.197 +      }
   1.198 +      
   1.199 +      bool found = false;
   1.200 +      
   1.201 +      unz_file_info info;
   1.202 +      
   1.203 +      while(true) {
   1.204 +        r = unzGetCurrentFileInfo(unz,
   1.205 +                                  &info,
   1.206 +                                  buffer,
   1.207 +                                  sizeof(buffer),
   1.208 +                                  NULL,
   1.209 +                                  0,
   1.210 +                                  NULL,
   1.211 +                                  0);
   1.212 +        
   1.213 +        if(r != UNZ_OK) {
   1.214 +          unzClose(unz);
   1.215 +          systemMessage(0,"Bad ZIP file %s", szFile);
   1.216 +          exit(-1);
   1.217 +        }
   1.218 +        
   1.219 +        if(utilIsGBImage(buffer)) {
   1.220 +          found = true;
   1.221 +          cartridgeType = 1;
   1.222 +          break;
   1.223 +        }
   1.224 +        if(utilIsGBAImage(buffer)) {
   1.225 +          found = true;
   1.226 +          cartridgeType = 0;
   1.227 +          break;
   1.228 +        }
   1.229 +        
   1.230 +        r = unzGoToNextFile(unz);
   1.231 +        
   1.232 +        if(r != UNZ_OK)
   1.233 +          break;
   1.234 +      }
   1.235 +      
   1.236 +      if(!found) {
   1.237 +        unzClose(unz);
   1.238 +        systemMessage(0, "No image found on ZIP file %s", szFile);
   1.239 +        exit(-1);
   1.240 +      }
   1.241 +      
   1.242 +      unzClose(unz);
   1.243 +    }
   1.244 +    
   1.245 +    if(utilIsGBImage(szFile) || cartridgeType == 1) {
   1.246 +      failed = !gbLoadRom(szFile);
   1.247 +      cartridgeType = 1;
   1.248 +      emulator = GBSystem;
   1.249 +    } else if(utilIsGBAImage(szFile) || cartridgeType == 0) {
   1.250 +      failed = !CPULoadRom(szFile);
   1.251 +      cartridgeType = 0;
   1.252 +      emulator = GBASystem;
   1.253 +
   1.254 +      //CPUInit(biosFileName, useBios);
   1.255 +      CPUInit();
   1.256 +      CPUReset();
   1.257 +    } else {
   1.258 +      systemMessage(0, "Unknown file type %s", szFile);
   1.259 +      exit(-1);
   1.260 +    }
   1.261 +    
   1.262 +    if(failed) {
   1.263 +      systemMessage(0, "Failed to load file %s", szFile);
   1.264 +      exit(-1);
   1.265 +    }
   1.266 +    strcpy(filename, szFile);
   1.267 +    char *p = strrchr(filename, '.');
   1.268 +    
   1.269 +    if(p)
   1.270 +      *p = 0;
   1.271 +  } else {
   1.272 +    cartridgeType = 0;
   1.273 +    strcpy(filename, "gnu_stub");
   1.274 +    rom = (u8 *)malloc(0x2000000);
   1.275 +    workRAM = (u8 *)calloc(1, 0x40000);
   1.276 +    bios = (u8 *)calloc(1,0x4000);
   1.277 +    internalRAM = (u8 *)calloc(1,0x8000);
   1.278 +    paletteRAM = (u8 *)calloc(1,0x400);
   1.279 +    vram = (u8 *)calloc(1, 0x20000);
   1.280 +    oam = (u8 *)calloc(1, 0x400);
   1.281 +    pix = (u8 *)calloc(1, 4 * 240 * 160);
   1.282 +    ioMem = (u8 *)calloc(1, 0x400);
   1.283 +
   1.284 +    emulator = GBASystem;
   1.285 +    
   1.286 +    //CPUInit(biosFileName, useBios);
   1.287 +    CPUInit();
   1.288 +    CPUReset();    
   1.289 +  }
   1.290 +  
   1.291 +  if(debuggerStub) 
   1.292 +    remoteInit();
   1.293 +  
   1.294 +  if(cartridgeType == 0) {
   1.295 +  } else if (cartridgeType == 1) {
   1.296 +    if(gbBorderOn) {
   1.297 +      gbBorderLineSkip = 256;
   1.298 +      gbBorderColumnSkip = 48;
   1.299 +      gbBorderRowSkip = 40;
   1.300 +    } else {      
   1.301 +      gbBorderLineSkip = 160;
   1.302 +      gbBorderColumnSkip = 0;
   1.303 +      gbBorderRowSkip = 0;
   1.304 +    }      
   1.305 +  } else {
   1.306 +  }
   1.307 +
   1.308 +  for(int i = 0; i < 0x10000; i++) {
   1.309 +    systemColorMap32[i] = ((i & 0x1f) << systemRedShift) |
   1.310 +      (((i & 0x3e0) >> 5) << systemGreenShift) |
   1.311 +      (((i & 0x7c00) >> 10) << systemBlueShift);  
   1.312 +  }
   1.313 +
   1.314 +  emulating = 1;
   1.315 +  soundInit();
   1.316 +  
   1.317 +  while(emulating) {
   1.318 +    if(!paused) {
   1.319 +      if(debugger && emulator.emuHasDebugger)
   1.320 +        dbgMain();
   1.321 +      else
   1.322 +        emulator.emuMain(emulator.emuCount);
   1.323 +    }
   1.324 +  }
   1.325 +  emulating = 0;
   1.326 +  fprintf(stderr,"Shutting down\n");
   1.327 +  remoteCleanUp();
   1.328 +  soundShutdown();
   1.329 +
   1.330 +  if(gbRom != NULL || rom != NULL) {
   1.331 +    emulator.emuCleanUp();
   1.332 +  }
   1.333 +
   1.334 +  return 0;
   1.335 +}
   1.336 +
   1.337 +void systemMessage(int num, const char *msg, ...)
   1.338 +{
   1.339 +  char buffer[2048];
   1.340 +  va_list valist;
   1.341 +  
   1.342 +  va_start(valist, msg);
   1.343 +  vsprintf(buffer, msg, valist);
   1.344 +  
   1.345 +  fprintf(stderr, "%s\n", buffer);
   1.346 +  va_end(valist);
   1.347 +}
   1.348 +
   1.349 +void systemDrawScreen()
   1.350 +{
   1.351 +}
   1.352 +
   1.353 +bool systemReadJoypads()
   1.354 +{
   1.355 +  return true;
   1.356 +}
   1.357 +
   1.358 +u32 systemReadJoypad(int,bool)
   1.359 +{
   1.360 +  return 0;
   1.361 +}
   1.362 +
   1.363 +void systemShowSpeed(int speed)
   1.364 +{
   1.365 +}
   1.366 +
   1.367 +void system10Frames(int rate)
   1.368 +{
   1.369 +}
   1.370 +
   1.371 +void systemFrame(int)
   1.372 +{
   1.373 +}
   1.374 +
   1.375 +void systemSetTitle(const char *title)
   1.376 +{
   1.377 +}
   1.378 +
   1.379 +int systemScreenCapture(int a)
   1.380 +{
   1.381 +  char buffer[2048];
   1.382 +
   1.383 +  if(captureFormat) {
   1.384 +    if(captureDir[0])
   1.385 +      sprintf(buffer, "%s/%s%02d.bmp", captureDir, sdlGetFilename(filename), a);
   1.386 +    else
   1.387 +      sprintf(buffer, "%s%02d.bmp", filename, a);
   1.388 +
   1.389 +    emulator.emuWriteBMP(buffer);
   1.390 +  } else {
   1.391 +    if(captureDir[0])
   1.392 +      sprintf(buffer, "%s/%s%02d.png", captureDir, sdlGetFilename(filename), a);
   1.393 +    else
   1.394 +      sprintf(buffer, "%s%02d.png", filename, a);
   1.395 +    emulator.emuWritePNG(buffer);
   1.396 +  }
   1.397 +
   1.398 +  systemScreenMessage("Screen capture");
   1.399 +}
   1.400 +
   1.401 +u32 systemReadJoypadExtended()
   1.402 +{
   1.403 +  return 0;
   1.404 +}
   1.405 +
   1.406 +void systemWriteDataToSoundBuffer()
   1.407 +{
   1.408 +}
   1.409 +
   1.410 +bool systemSoundInit()
   1.411 +{
   1.412 +  return true;
   1.413 +}
   1.414 +
   1.415 +void systemSoundShutdown()
   1.416 +{
   1.417 +}
   1.418 +
   1.419 +void systemSoundPause()
   1.420 +{
   1.421 +}
   1.422 +
   1.423 +void systemSoundResume()
   1.424 +{
   1.425 +}
   1.426 +
   1.427 +void systemSoundReset()
   1.428 +{
   1.429 +}
   1.430 +
   1.431 +static int ticks = 0;
   1.432 +
   1.433 +u32 systemGetClock()
   1.434 +{
   1.435 +  return ticks++;
   1.436 +}
   1.437 +
   1.438 +void systemUpdateMotionSensor()
   1.439 +{
   1.440 +}
   1.441 +
   1.442 +int systemGetSensorX()
   1.443 +{
   1.444 +  return 0;
   1.445 +}
   1.446 +
   1.447 +int systemGetSensorY()
   1.448 +{
   1.449 +  return 0;
   1.450 +}
   1.451 +
   1.452 +void systemGbPrint(u8 *data,int pages,int feed,int palette, int contrast)
   1.453 +{
   1.454 +}
   1.455 +
   1.456 +void systemScreenMessage(const char *msg, int slot, int duration, const char *colorList)
   1.457 +{
   1.458 +}
   1.459 +
   1.460 +bool systemCanChangeSoundQuality()
   1.461 +{
   1.462 +  return false;
   1.463 +}
   1.464 +
   1.465 +bool systemPauseOnFrame()
   1.466 +{
   1.467 +  return false;
   1.468 +}
   1.469 +
   1.470 +void systemGbBorderOn()
   1.471 +{
   1.472 +}
   1.473 +
   1.474 +bool sdlCheckJoyKey(int)
   1.475 +{
   1.476 +  return true;
   1.477 +}
   1.478 +
   1.479 +u16 checksumBIOS()
   1.480 +{
   1.481 +	bool hasBIOS = false;
   1.482 +	u8 * tempBIOS;
   1.483 +	if(useBios)
   1.484 +	{
   1.485 +		tempBIOS = (u8 *)malloc(0x4000);
   1.486 +		int size = 0x4000;
   1.487 +		if(utilLoad(biosFileName,
   1.488 +					utilIsGBABios,
   1.489 +					tempBIOS,
   1.490 +					size)) {
   1.491 +		if(size == 0x4000)
   1.492 +			hasBIOS = true;
   1.493 +		}
   1.494 +	}
   1.495 +
   1.496 +	u16 biosCheck = 0;
   1.497 +	if(hasBIOS) {
   1.498 +		for(int i = 0; i < 0x4000; i += 4)
   1.499 +			biosCheck += *((u32 *)&tempBIOS[i]);
   1.500 +		free(tempBIOS);
   1.501 +	}
   1.502 +
   1.503 +	return biosCheck;
   1.504 +}