view java/src/com/aurellem/gb/Gb.java @ 96:cb487c4ce5c0

added write-memory!, which allows me to write any value to the gameboy's memory-mapped rom or ram.
author Robert McIntyre <rlm@mit.edu>
date Sun, 11 Mar 2012 23:27:19 -0500
parents 4c60ebca1a9d
children 2090bcb78f44
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 int ntick();
33 public static boolean tick(){
34 return (1 == ntick());
35 }
37 public static native void nstep(int keymask);
39 public static void step(int keymask){
40 if (-1 == keymask) {step();}
41 else {nstep(keymask);}}
43 public static native void shutdown();
45 public static native long saveState(ByteBuffer buffer, int size);
47 public static native void loadState(ByteBuffer buffer, int size);
49 public static final int MAX_SAVE_SIZE = 20000;
51 public static ByteBuffer createDirectByteBuffer(int capacity){
52 byte[] zeros = new byte[capacity];
53 ByteBuffer buf =
54 ByteBuffer.allocateDirect(capacity)
55 .order(ByteOrder.nativeOrder());
56 buf.put(zeros);
57 buf.clear();
58 return buf;
59 }
61 public static ByteBuffer saveBuffer(){
62 return createDirectByteBuffer(MAX_SAVE_SIZE);
63 }
65 public static ByteBuffer saveState(){
66 ByteBuffer buf = saveBuffer();
68 saveState(buf, buf.capacity());
70 // determine the extent of the saved data
71 int position = buf.capacity() - 1;
72 for (int i = position; i > 0; i--){
73 if (0 != buf.get(i)){
74 position = i;
75 break;
76 }}
77 byte[] saveArray = new byte[position];
78 ByteBuffer save = createDirectByteBuffer(position);
79 buf.get(saveArray, 0 , position);
80 save.put(saveArray);
81 save.rewind();
82 return save;
83 }
85 public static void loadState(ByteBuffer saveState){
86 loadState(saveState, MAX_SAVE_SIZE);
87 }
89 public static native int getROMSize();
90 public static native int getRAMSize();
93 public static final int WRAM_SIZE = 0x8000;
95 public static final int VRAM_SIZE = 0x4000;
97 public static final int RAM_SIZE = 0x8000;
99 public static final int ROM_SIZE = 0x100000;
101 public static final int NUM_REGISTERS = 27;
103 public static final int GB_MEMORY = 0x10000;
105 public static native void getMemory(int[] store);
107 public static native void writeMemory(int[] newMemory);
109 public static native void getRAM(int[] store);
111 public static native void getROM(int[] store);
113 public static native void getWRAM(int[] store);
115 public static native void getVRAM(int[] store);
117 public static native void getRegisters(int[] store);
118 }