view java/src/com/aurellem/gb/Gb.java @ 331:6ec288064d49

improved do-save-corruption
author Robert McIntyre <rlm@mit.edu>
date Fri, 06 Apr 2012 12:18:58 -0500
parents 893c753f8088
children 09b3bc0b71b5
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);
24 public static void loadVBA(){
25 System.loadLibrary("vba");
26 }
28 public static native void step();
30 public static native int ntick();
32 public static boolean tick(){
33 return (1 == ntick());
34 }
36 public static native void nstep(int keymask);
38 public static void step(int keymask){
39 if (-1 == keymask) {step();}
40 else {nstep(keymask);}}
42 public static native void shutdown();
44 public static native long saveState(ByteBuffer buffer, int size);
46 public static native void loadState(ByteBuffer buffer, int size);
48 public static final int MAX_SAVE_SIZE = 20000;
50 public static ByteBuffer createDirectByteBuffer(int capacity){
51 byte[] zeros = new byte[capacity];
52 ByteBuffer buf =
53 ByteBuffer.allocateDirect(capacity)
54 .order(ByteOrder.nativeOrder());
55 buf.put(zeros);
56 buf.clear();
57 return buf;
58 }
60 public static ByteBuffer saveBuffer(){
61 return createDirectByteBuffer(MAX_SAVE_SIZE);
62 }
64 public static ByteBuffer saveState(){
65 ByteBuffer buf = saveBuffer();
67 saveState(buf, buf.capacity());
69 // determine the extent of the saved data
70 int position = buf.capacity() - 1;
71 for (int i = position; i > 0; i--){
72 if (0 != buf.get(i)){
73 position = i;
74 break;
75 }}
76 byte[] saveArray = new byte[position];
77 ByteBuffer save = createDirectByteBuffer(position);
78 buf.get(saveArray, 0 , position);
79 save.put(saveArray);
80 save.rewind();
81 return save;
82 }
84 public static void loadState(ByteBuffer saveState){
85 loadState(saveState, MAX_SAVE_SIZE);
86 }
88 public static native int getROMSize();
89 public static native int getRAMSize();
92 public static final int WRAM_SIZE = 0x8000;
94 public static final int VRAM_SIZE = 0x4000;
96 public static final int RAM_SIZE = 0x8000;
98 public static final int ROM_SIZE = 0x100000;
100 public static final int NUM_REGISTERS = 29;
102 public static final int GB_MEMORY = 0x10000;
104 public static native void getMemory(int[] store);
106 public static native void writeMemory(int[] newMemory);
108 public static native void getRAM(int[] store);
110 public static native void getROM(int[] store);
112 public static native void writeROM(int[] newROM);
114 public static native void getWRAM(int[] store);
116 public static native void getVRAM(int[] store);
118 public static native void getRegisters(int[] store);
120 public static native void writeRegisters(int[] newRegisters);
122 }