home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / sdl / TestEmu.cpp < prev   
Encoding:
C/C++ Source or Header  |  2004-05-20  |  8.7 KB  |  449 lines

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