diff java/src/com/aurellem/gb/Gb.java @ 76:d7c38ce83421

working on disk-backup for save-states
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 19:48:54 -0600
parents 4699c7bab77d
children ad6ebe886a21
line wrap: on
line diff
     1.1 --- a/java/src/com/aurellem/gb/Gb.java	Thu Mar 08 06:01:09 2012 -0600
     1.2 +++ b/java/src/com/aurellem/gb/Gb.java	Thu Mar 08 19:48:54 2012 -0600
     1.3 @@ -32,24 +32,49 @@
     1.4  
     1.5      public static native void shutdown();
     1.6  
     1.7 -    public static native void saveState(ByteBuffer buffer, int size);
     1.8 +    public static native long saveState(ByteBuffer buffer, int size);
     1.9  
    1.10      public static native void loadState(ByteBuffer buffer, int size);
    1.11  
    1.12 -    public static final int SAVE_SIZE = 90000;
    1.13 +    public static final int MAX_SAVE_SIZE = 90000;
    1.14  
    1.15 -    public static ByteBuffer saveState(){
    1.16 +    public static ByteBuffer createDirectByteBuffer(int capacity){
    1.17 +	byte[] zeros = new byte[capacity];
    1.18  	ByteBuffer buf = 
    1.19 -	    ByteBuffer.allocateDirect(SAVE_SIZE)
    1.20 +	    ByteBuffer.allocateDirect(capacity)
    1.21  	              .order(ByteOrder.nativeOrder());
    1.22 +	buf.put(zeros);
    1.23  	buf.clear();
    1.24 -	saveState(buf, SAVE_SIZE);
    1.25 -	buf.flip();
    1.26  	return buf;
    1.27      }
    1.28  
    1.29 +    public static ByteBuffer saveBuffer(){
    1.30 +	return createDirectByteBuffer(MAX_SAVE_SIZE);
    1.31 +    }
    1.32 +
    1.33 +    public static ByteBuffer saveState(){
    1.34 +	ByteBuffer buf = saveBuffer();
    1.35 +	
    1.36 +	saveState(buf, buf.capacity());
    1.37 +	
    1.38 +	// determine the extent of the saved data
    1.39 +	int position = buf.capacity() - 1;
    1.40 +	for (int i = position; i > 0; i--){
    1.41 +	    if (0 != buf.get(i)){
    1.42 +		position = i;
    1.43 +		break;
    1.44 +	    }}
    1.45 +	System.out.println("Position: " + position);
    1.46 +	byte[] saveArray = new byte[position];
    1.47 +	ByteBuffer save = createDirectByteBuffer(position);
    1.48 +	buf.get(saveArray, 0 , position);
    1.49 +	save.put(saveArray);
    1.50 +	save.rewind();
    1.51 +	return save;
    1.52 +    }
    1.53 +
    1.54      public static void loadState(ByteBuffer saveState){
    1.55 -	loadState(saveState, SAVE_SIZE);
    1.56 +	loadState(saveState, MAX_SAVE_SIZE);
    1.57      }
    1.58  
    1.59      public static native int getROMSize();