view java/src/com/aurellem/gb/Gb.java @ 80:ad6ebe886a21

worked out size of save files and reduced MAX_SAVE_SIZE to a more reasonable value
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 22:29:11 -0600
parents d7c38ce83421
children db8e0a563c8e
line wrap: on
line source
1 package com.aurellem.gb;
3 import java.nio.ByteBuffer;
4 import java.nio.IntBuffer;
5 import java.nio.ByteOrder;
7 public class Gb {
10 public Gb(){}
13 /**
14 * Hello World! This is just to test the native interface.
15 */
16 public static native void sayHello();
18 /**
19 * Run the emulator on a given rom
20 * @param rom - the name of the rom.
21 */
22 public static native void startEmulator(String rom);
25 public static void loadVBA(){
26 System.loadLibrary("vba");
27 }
29 public static native void step();
31 public static native void step(int keymask);
33 public static native void shutdown();
35 public static native long saveState(ByteBuffer buffer, int size);
37 public static native void loadState(ByteBuffer buffer, int size);
39 public static final int MAX_SAVE_SIZE = 20000;
41 public static ByteBuffer createDirectByteBuffer(int capacity){
42 byte[] zeros = new byte[capacity];
43 ByteBuffer buf =
44 ByteBuffer.allocateDirect(capacity)
45 .order(ByteOrder.nativeOrder());
46 buf.put(zeros);
47 buf.clear();
48 return buf;
49 }
51 public static ByteBuffer saveBuffer(){
52 return createDirectByteBuffer(MAX_SAVE_SIZE);
53 }
55 public static ByteBuffer saveState(){
56 ByteBuffer buf = saveBuffer();
58 saveState(buf, buf.capacity());
60 // determine the extent of the saved data
61 int position = buf.capacity() - 1;
62 for (int i = position; i > 0; i--){
63 if (0 != buf.get(i)){
64 position = i;
65 break;
66 }}
67 System.out.println("Position: " + position);
68 byte[] saveArray = new byte[position];
69 ByteBuffer save = createDirectByteBuffer(position);
70 buf.get(saveArray, 0 , position);
71 save.put(saveArray);
72 save.rewind();
73 return save;
74 }
76 public static void loadState(ByteBuffer saveState){
77 loadState(saveState, MAX_SAVE_SIZE);
78 }
80 public static native int getROMSize();
81 public static native int getRAMSize();
84 public static final int WRAM_SIZE = 0x8000;
86 public static final int VRAM_SIZE = 0x4000;
88 public static final int NUM_REGISTERS = 27;
90 public static native void getRAM(int[] store);
92 public static native void getROM(int[] store);
94 public static native void getWRAM(int[] store);
96 public static native void getVRAM(int[] store);
98 public static native void getRegisters(int[] store);
99 }