changeset 92:1ff2c546f5ad

added tick(), which allows one to step through each opcode of gameboy
author Robert McIntyre <rlm@mit.edu>
date Sun, 11 Mar 2012 19:07:31 -0500
parents 613353b7e012
children 4c60ebca1a9d
files java/src/com/aurellem/gb/Gb.java src/clojure/clojure.cpp src/common/System.h src/gb/GB.cpp src/gb/GB.h src/gb/gbCodes.h src/gb/gbCodesCB.h src/gba/GBA.cpp src/gba/GBA.h src/sdl/Drive.h src/sdl/SDL.cpp
diffstat 11 files changed, 62 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/java/src/com/aurellem/gb/Gb.java	Sun Mar 11 13:53:27 2012 -0500
     1.2 +++ b/java/src/com/aurellem/gb/Gb.java	Sun Mar 11 19:07:31 2012 -0500
     1.3 @@ -27,6 +27,12 @@
     1.4      }
     1.5  
     1.6      public static native void step();
     1.7 +    
     1.8 +    public static native int ntick();
     1.9 +
    1.10 +    public static boolean tick(){
    1.11 +	return (1 == ntick());
    1.12 +    }
    1.13  
    1.14      public static native void nstep(int keymask);
    1.15  
     2.1 --- a/src/clojure/clojure.cpp	Sun Mar 11 13:53:27 2012 -0500
     2.2 +++ b/src/clojure/clojure.cpp	Sun Mar 11 19:07:31 2012 -0500
     2.3 @@ -61,8 +61,16 @@
     2.4    step(keymask);
     2.5  }
     2.6  
     2.7 +/*
     2.8 + * Class:     com_aurellem_gb_Gb
     2.9 + * Method:    ntick
    2.10 + * Signature: ()I
    2.11 + */
    2.12 +JNIEXPORT jint JNICALL Java_com_aurellem_gb_Gb_ntick
    2.13 +(JNIEnv *env, jclass clazz){
    2.14 +  return tick();
    2.15  
    2.16 -
    2.17 +}
    2.18  
    2.19  
    2.20  /*
     3.1 --- a/src/common/System.h	Sun Mar 11 13:53:27 2012 -0500
     3.2 +++ b/src/common/System.h	Sun Mar 11 19:07:31 2012 -0500
     3.3 @@ -13,7 +13,7 @@
     3.4  struct EmulatedSystem
     3.5  {
     3.6  	// main emulation function
     3.7 -	void (*emuMain)(int);
     3.8 +	int (*emuMain)(int);
     3.9  	// reset emulator
    3.10  	void (*emuReset)(bool);
    3.11  	// clean up memory
     4.1 --- a/src/gb/GB.cpp	Sun Mar 11 13:53:27 2012 -0500
     4.2 +++ b/src/gb/GB.cpp	Sun Mar 11 19:07:31 2012 -0500
     4.3 @@ -15,7 +15,7 @@
     4.4  #include "../common/Util.h"
     4.5  #include "../common/System.h"
     4.6  #include "../common/movie.h"
     4.7 -#include "../common/vbalua.h"
     4.8 +// #include "../common/vbalua.h"
     4.9  
    4.10  #ifdef __GNUC__
    4.11  #define _stricmp strcasecmp
    4.12 @@ -1280,7 +1280,7 @@
    4.13  void gbWriteMemory(register u16 address, register u8 value)
    4.14  {
    4.15    gbWriteMemoryWrapped(address, value);
    4.16 -  CallRegisteredLuaMemHook(address, 1, value, LUAMEMHOOK_WRITE);
    4.17 +  //CallRegisteredLuaMemHook(address, 1, value, LUAMEMHOOK_WRITE);
    4.18  }
    4.19  
    4.20  u8 gbReadMemory(register u16 address)
    4.21 @@ -3164,8 +3164,9 @@
    4.22    return true;
    4.23  }
    4.24  
    4.25 -void gbEmulate(int ticksToStop)
    4.26 +int gbEmulate(int ticksToStop)
    4.27  {
    4.28 +  int rlm_count = 0;
    4.29    //printf("RLM: Inside the GB!\n");
    4.30    gbRegister tempRegister;
    4.31    u8		   tempValue;
    4.32 @@ -3232,8 +3233,8 @@
    4.33      }
    4.34  
    4.35  
    4.36 -  for (;; )
    4.37 -    {
    4.38 +  //for (;; )
    4.39 +  // {
    4.40  #ifndef FINAL_VERSION
    4.41        if (systemDebug)
    4.42  	{
    4.43 @@ -3276,7 +3277,8 @@
    4.44        else
    4.45  	{
    4.46  	  opcode = gbReadOpcode(PC.W);
    4.47 -	  CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC);
    4.48 +	  //printf("RLM: calling mem Hook ; %07d\n", rlm_count++);
    4.49 +	  //CallRegisteredLuaMemHook(PC.W, 1, opcode, LUAMEMHOOK_EXEC);
    4.50  	  PC.W++;
    4.51  
    4.52  	  if (IFF & 0x100)
    4.53 @@ -3304,7 +3306,7 @@
    4.54  	}
    4.55  
    4.56        if (!emulating)
    4.57 -	return;
    4.58 +	return 1;
    4.59  
    4.60        if (gbDmaTicks)
    4.61  	{
    4.62 @@ -3774,31 +3776,31 @@
    4.63  		  if ((gbInterrupt & 1) && (register_IE & 1))
    4.64  		    {
    4.65  		      gbVblank_interrupt();
    4.66 -		      continue;
    4.67 +		      return newFrame;
    4.68  		    }
    4.69  
    4.70  		  if ((gbInterrupt & 2) && (register_IE & 2))
    4.71  		    {
    4.72  		      gbLcd_interrupt();
    4.73 -		      continue;
    4.74 +		      return newFrame;
    4.75  		    }
    4.76  
    4.77  		  if ((gbInterrupt & 4) && (register_IE & 4))
    4.78  		    {
    4.79  		      gbTimer_interrupt();
    4.80 -		      continue;
    4.81 +		      return newFrame;
    4.82  		    }
    4.83  
    4.84  		  if ((gbInterrupt & 8) && (register_IE & 8))
    4.85  		    {
    4.86  		      gbSerial_interrupt();
    4.87 -		      continue;
    4.88 +		      return newFrame;
    4.89  		    }
    4.90  
    4.91  		  if ((gbInterrupt & 16) && (register_IE & 16))
    4.92  		    {
    4.93  		      gbJoypad_interrupt();
    4.94 -		      continue;
    4.95 +		      return newFrame;
    4.96  		    }
    4.97  		}
    4.98  	    }
    4.99 @@ -3814,12 +3816,12 @@
   4.100  	{
   4.101  	  // old timing code
   4.102  	  if (ticksToStop > 0)
   4.103 -	    continue;
   4.104 +	    return newFrame;
   4.105  	}
   4.106        else
   4.107  	{
   4.108  	  if (!newFrame && (register_LCDC & 0x80) != 0)
   4.109 -	    continue;
   4.110 +	    return newFrame;
   4.111  	}
   4.112  
   4.113        if (!(register_LCDC & 0x80))
   4.114 @@ -3862,14 +3864,15 @@
   4.115  	}
   4.116  
   4.117        // makes sure frames are really divided across input sampling boundaries which occur at a constant rate
   4.118 -      if (newFrame || useOldFrameTiming)
   4.119 -	{
   4.120 +      //if (newFrame || useOldFrameTiming)
   4.121 +      //	{
   4.122  	  ///			extern void VBAOnEnteringFrameBoundary();
   4.123  	  ///			VBAOnEnteringFrameBoundary();
   4.124  
   4.125 -	  break;
   4.126 -	}
   4.127 -    }
   4.128 +      //  break;
   4.129 +      //	}
   4.130 +      // RLM removing for loop }
   4.131 +  return newFrame;
   4.132  }
   4.133  
   4.134  
   4.135 @@ -3964,7 +3967,7 @@
   4.136      gbWriteSaveState,
   4.137      // emuReadStateFromStream
   4.138      gbReadSaveStateFromStream,
   4.139 -    // emuWriteStateToStream
   4.140 +    // emuwritestatetostream
   4.141      gbWriteSaveStateToStream,
   4.142      // emuReadMemState
   4.143      gbReadMemSaveState,
     5.1 --- a/src/gb/GB.h	Sun Mar 11 13:53:27 2012 -0500
     5.2 +++ b/src/gb/GB.h	Sun Mar 11 19:07:31 2012 -0500
     5.3 @@ -22,7 +22,7 @@
     5.4  } gbRegister;
     5.5  
     5.6  extern bool gbLoadRom(const char *);
     5.7 -extern void gbEmulate(int);
     5.8 +extern int gbEmulate(int);
     5.9  extern bool gbIsGameboyRom(const char *);
    5.10  extern void gbSoundReset();
    5.11  extern void gbSoundSetQuality(int);
     6.1 --- a/src/gb/gbCodes.h	Sun Mar 11 13:53:27 2012 -0500
     6.2 +++ b/src/gb/gbCodes.h	Sun Mar 11 19:07:31 2012 -0500
     6.3 @@ -1385,4 +1385,4 @@
     6.4     systemMessage(0, N_("Unknown opcode %02x at %04x"),
     6.5                   gbReadOpcode(PC.W-1),PC.W-1);
     6.6     emulating = false;
     6.7 -   return;
     6.8 +   return 1; 
     7.1 --- a/src/gb/gbCodesCB.h	Sun Mar 11 13:53:27 2012 -0500
     7.2 +++ b/src/gb/gbCodesCB.h	Sun Mar 11 19:07:31 2012 -0500
     7.3 @@ -1266,4 +1266,4 @@
     7.4     systemMessage(0, N_("Unknown opcode %02x at %04x"),
     7.5                   gbReadOpcode(PC.W-1),PC.W-1);
     7.6     emulating = false;
     7.7 -   return;
     7.8 +   return 1;
     8.1 --- a/src/gba/GBA.cpp	Sun Mar 11 13:53:27 2012 -0500
     8.2 +++ b/src/gba/GBA.cpp	Sun Mar 11 19:07:31 2012 -0500
     8.3 @@ -3746,7 +3746,7 @@
     8.4  extern void winlog(const char *, ...);
     8.5  #endif
     8.6  
     8.7 -void CPULoop(int _ticks)
     8.8 +void CPULoop2(int _ticks)
     8.9  {
    8.10  	int32 ticks = _ticks;
    8.11  	int32 clockTicks;
    8.12 @@ -4501,6 +4501,14 @@
    8.13  	}
    8.14  }
    8.15  
    8.16 +
    8.17 +// RLM: 
    8.18 +int CPULoop(int _ticks){
    8.19 +  CPULoop2(_ticks);
    8.20 +  return 1;
    8.21 +}
    8.22 +
    8.23 +
    8.24  struct EmulatedSystem GBASystem =
    8.25  {
    8.26  	// emuMain
     9.1 --- a/src/gba/GBA.h	Sun Mar 11 13:53:27 2012 -0500
     9.2 +++ b/src/gba/GBA.h	Sun Mar 11 19:07:31 2012 -0500
     9.3 @@ -86,7 +86,7 @@
     9.4  extern bool CPULoadBios(const char *, bool);
     9.5  extern void CPUInit();
     9.6  extern void CPUReset(bool userReset = false);
     9.7 -extern void CPULoop(int);
     9.8 +extern int CPULoop(int);
     9.9  extern void CPUCheckDMA(int, int);
    9.10  #ifdef PROFILING
    9.11  extern void cpuProfil(char *buffer, int, u32, int);
    10.1 --- a/src/sdl/Drive.h	Sun Mar 11 13:53:27 2012 -0500
    10.2 +++ b/src/sdl/Drive.h	Sun Mar 11 19:07:31 2012 -0500
    10.3 @@ -5,3 +5,5 @@
    10.4  void step(int keymask);
    10.5  
    10.6  void shutdown();
    10.7 +
    10.8 +int tick();
    11.1 --- a/src/sdl/SDL.cpp	Sun Mar 11 13:53:27 2012 -0500
    11.2 +++ b/src/sdl/SDL.cpp	Sun Mar 11 19:07:31 2012 -0500
    11.3 @@ -2062,30 +2062,18 @@
    11.4    SDL_Quit();
    11.5  }
    11.6  
    11.7 +int tick () {
    11.8 +  return theEmulator.emuMain(theEmulator.emuCount);
    11.9 +}
   11.10  
   11.11  void step () {
   11.12    if(!paused && active) {
   11.13 -    if(debugger && theEmulator.emuHasDebugger)
   11.14 -      dbgMain();
   11.15 -    else {
   11.16 -      //printf("RLM: emulator main\n");
   11.17 -      theEmulator.emuMain(theEmulator.emuCount);
   11.18 -      //printf("RLM: emulator main called\n");
   11.19 -      if(rewindSaveNeeded && rewindMemory && theEmulator.emuWriteMemState) {
   11.20 -	rewindCount++;
   11.21 -	if(rewindCount > 8)
   11.22 -	  rewindCount = 8;
   11.23 -	if(theEmulator.emuWriteMemState &&
   11.24 -	   theEmulator.emuWriteMemState(&rewindMemory[rewindPos*REWIND_SIZE], 
   11.25 -					REWIND_SIZE)) {
   11.26 -	  rewindPos = ++rewindPos & 7;
   11.27 -	  if(rewindCount == 8)
   11.28 -	    rewindTopPos = ++rewindTopPos & 7;
   11.29 -	}
   11.30 -      }
   11.31 -
   11.32 -      rewindSaveNeeded = false;
   11.33 +    //printf("RLM: emulator main\n");
   11.34 +    int frameComplete = 0;
   11.35 +    while (!(frameComplete)){
   11.36 +      frameComplete = theEmulator.emuMain(theEmulator.emuCount);
   11.37      }
   11.38 +    //printf("RLM: emulator main called\n");
   11.39    } else {
   11.40      SDL_Delay(500);
   11.41    }