view src/clojure/clojure.cpp @ 551:b69a3dba8045

condensed gameboy program into a contigous sequence of bytes.
author Robert McIntyre <rlm@mit.edu>
date Thu, 30 Aug 2012 11:19:52 -0500
parents fa7676dbf6f2
children
line wrap: on
line source
1 #include "com_aurellem_gb_Gb.h"
2 #include "../sdl/Drive.h"
3 #include "../gb/GB.h"
6 #include <string.h>
8 #define UNUSED(x) (void)(x)
11 /*
12 * Class: com_aurellem_gb_Gb
13 * Method: sayHello
14 * Signature: ()V
15 */
16 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_sayHello
17 (JNIEnv *env, jclass clazz){
18 UNUSED(env);UNUSED(clazz);
19 printf("Hello from GB\n");
20 }
22 /*
23 * Class: com_aurellem_gb_Gb
24 * Method: startEmulator
25 * Signature: (Ljava/lang/String;)V
26 */
27 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_startEmulator
28 (JNIEnv *env, jclass clazz, jstring str){
29 UNUSED(clazz);
31 const char *_romName = env->GetStringUTFChars(str, 0);
32 size_t len = strlen(_romName);
34 char romName[len + 1];
36 strcpy(romName, _romName);
38 char* arguments[] = {"vba-rlm", romName};
39 runVBA(2, arguments);
40 }
45 /*
46 * Class: com_aurellem_gb_Gb
47 * Method: step
48 * Signature: ()V
49 */
50 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_step
51 (JNIEnv *env, jclass clazz){
52 step();
53 }
55 /*
56 * Class: com_aurellem_gb_Gb
57 * Method: step
58 * Signature: (I)V
59 */
60 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_nstep
61 (JNIEnv *env, jclass clazz, jint keymask){
62 step(keymask);
63 }
65 /*
66 * Class: com_aurellem_gb_Gb
67 * Method: ntick
68 * Signature: ()I
69 */
70 JNIEXPORT jint JNICALL Java_com_aurellem_gb_Gb_ntick
71 (JNIEnv *env, jclass clazz){
72 return tick();
74 }
77 /*
78 * Class: com_aurellem_gb_Gb
79 * Method: shutdown
80 * Signature: ()V
81 */
82 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_shutdown
83 (JNIEnv *env, jclass clazz){
84 shutdown();
86 }
88 /*
89 * Class: com_aurellem_gb_Gb
90 * Method: saveState
91 * Signature: (Ljava/nio/ByteBuffer;I)J
92 */
93 JNIEXPORT jlong JNICALL Java_com_aurellem_gb_Gb_saveState
94 (JNIEnv *env, jclass clazz, jobject buffer, jint size){
95 char* buffer_address =
96 ((char*) env->GetDirectBufferAddress(buffer));
97 long limit = gbWriteMemSaveStatePos(buffer_address, size);
98 return limit;
99 }
101 /*
102 * Class: com_aurellem_gb_Gb
103 * Method: loadState
104 * Signature: (Ljava/nio/ByteBuffer;)V
105 */
106 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_loadState
107 (JNIEnv *env, jclass clazz, jobject buffer, jint size){
108 char* buffer_address =
109 ((char*) env->GetDirectBufferAddress(buffer));
110 gbReadMemSaveState(buffer_address, size);
111 }
114 /*
115 * Class: com_aurellem_gb_Gb
116 * Method: getROMSize
117 * Signature: ()I
118 */
119 JNIEXPORT jint JNICALL Java_com_aurellem_gb_Gb_getROMSize
120 (JNIEnv *env, jclass clazz){
121 return getRomSize();
122 }
124 /*
125 * Class: com_aurellem_gb_Gb
126 * Method: getRAMSize
127 * Signature: ()I
128 */
129 JNIEXPORT jint JNICALL Java_com_aurellem_gb_Gb_getRAMSize
130 (JNIEnv *env, jclass clazz){
131 return getRamSize();
132 }
135 /*
136 * Class: com_aurellem_gb_Gb
137 * Method: getMemory
138 * Signature: ([I)V
139 */
140 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getMemory
141 (JNIEnv *env, jclass clazz, jintArray arr){
142 jint *mem_store = env->GetIntArrayElements(arr, 0);
143 storeMemory(mem_store);
144 env->ReleaseIntArrayElements(arr, mem_store, 0);
145 }
147 /*
148 * Class: com_aurellem_gb_Gb
149 * Method: writeMemory
150 * Signature: ([I)V
151 */
152 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_writeMemory
153 (JNIEnv *env, jclass clazz, jintArray arr){
154 jint *new_memory = env->GetIntArrayElements(arr, 0);
155 writeMemory(new_memory);
156 env->ReleaseIntArrayElements(arr, new_memory, 0);
157 }
160 /*
161 * Class: com_aurellem_gb_Gb
162 * Method: getRAM
163 * Signature: ([I)V
164 */
165 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getRAM
166 (JNIEnv *env, jclass clazz, jintArray arr){
167 jint *ram_store = env->GetIntArrayElements(arr, 0);
168 storeRam(ram_store);
169 env->ReleaseIntArrayElements(arr, ram_store, 0);
170 }
173 /*
174 * Class: com_aurellem_gb_Gb
175 * Method: getROM
176 * Signature: ([I)V
177 */
178 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getROM
179 (JNIEnv *env, jclass clazz, jintArray arr){
180 jint *rom_store = env->GetIntArrayElements(arr, 0);
181 storeRom(rom_store);
182 env->ReleaseIntArrayElements(arr, rom_store, 0);
183 }
186 /*
187 * Class: com_aurellem_gb_Gb
188 * Method: writeROM
189 * Signature: ([I)V
190 */
191 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_writeROM
192 (JNIEnv *env, jclass clazz, jintArray arr){
193 jint *new_rom = env->GetIntArrayElements(arr, 0);
194 writeRom(new_rom);
195 env->ReleaseIntArrayElements(arr, new_rom, 0);
196 }
200 /*
201 * Class: com_aurellem_gb_Gb
202 * Method: getWRAM
203 * Signature: ([I)V
204 */
205 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getWRAM
206 (JNIEnv *env, jclass clazz, jintArray arr){
207 jint *ram_store = env->GetIntArrayElements(arr, 0);
208 storeWRam(ram_store);
209 env->ReleaseIntArrayElements(arr, ram_store, 0);
210 }
213 /*
214 * Class: com_aurellem_gb_Gb
215 * Method: getVRAM
216 * Signature: ([I)V
217 */
218 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getVRAM
219 (JNIEnv *env, jclass clazz, jintArray arr){
220 jint *ram_store = env->GetIntArrayElements(arr, 0);
221 storeVRam(ram_store);
222 env->ReleaseIntArrayElements(arr, ram_store, 0);
223 }
226 /*
227 * Class: com_aurellem_gb_Gb
228 * Method: getRegisters
229 * Signature: ([I)V
230 */
231 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getRegisters
232 (JNIEnv *env, jclass clazz, jintArray arr){
233 jint *registers_store = env->GetIntArrayElements(arr, 0);
234 storeRegisters(registers_store);
235 env->ReleaseIntArrayElements(arr, registers_store, 0);
236 }
239 /*
240 * Class: com_aurellem_gb_Gb
241 * Method: writeRegisters
242 * Signature: ([I)V
243 */
244 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_writeRegisters
245 (JNIEnv *env, jclass clazz, jintArray arr){
246 jint *new_registers = env->GetIntArrayElements(arr, 0);
247 setRegisters(new_registers);
248 env->ReleaseIntArrayElements(arr, new_registers, 0);
249 }
252 int intensity[32] = {
253 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x5e, 0x6c, 0x7a, 0x88, 0x94,
254 0xa0, 0xae, 0xb7, 0xbf, 0xc6, 0xce, 0xd3, 0xd9, 0xdf, 0xe3, 0xe7,
255 0xeb, 0xef, 0xf3, 0xf6, 0xf9, 0xfb, 0xfd, 0xfe, 0xff, 0xff };
257 int influence[3][3] =
258 {
259 {16,4,4},
260 {8,16,8},
261 {0,8,16}
262 };
264 int* translateRGB(int* rgb, int* store){
266 int m[3][3];
267 int i,j;
269 for (i=0;i<3;i++){
270 for (j=0;j<3;j++){
271 m[i][j] = (intensity[rgb[i]>>3]*influence[i][j]) >> 5;}}
273 for (i=0;i<3;i++)
274 {
275 if (m[0][i]>m[1][i])
276 {
277 j=m[0][i];
278 m[0][i]=m[1][i];
279 m[1][i]=j;
280 }
282 if (m[1][i]>m[2][i])
283 {
284 j=m[1][i];
285 m[1][i]=m[2][i];
286 m[2][i]=j;
287 }
289 if (m[0][i]>m[1][i])
290 {
291 j=m[0][i];
292 m[0][i]=m[1][i];
293 m[1][i]=j;
294 }
296 store[i]=(((m[0][i]+m[1][i]*2+m[2][i]*4)*5) >> 4)+32;
297 }
298 return store;
299 }
301 /*
302 * Class: com_aurellem_gb_Gb
303 * Method: translateRGB
304 * Signature: ([I[I)V
305 */
306 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_translateRGB
307 (JNIEnv *env, jclass clazz, jintArray rgb, jintArray store){
308 jint *RGB_Arr = env->GetIntArrayElements(rgb, 0);
309 jint *store_Arr = env->GetIntArrayElements(store,0);
310 translateRGB(RGB_Arr, store_Arr);
311 env->ReleaseIntArrayElements(rgb, RGB_Arr, 0);
312 env->ReleaseIntArrayElements(store, store_Arr, 0);
313 }
316 /*
317 * Class: com_aurellem_gb_Gb
318 * Method: getPixels
319 * Signature: ([I)V
320 */
321 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getPixels
322 (JNIEnv *env, jclass clazz, jintArray arr){
323 jint *pixel_store = env->GetIntArrayElements(arr, 0);
324 getPixels32(pixel_store);
325 env->ReleaseIntArrayElements(arr, pixel_store, 0);
326 }
328 /*
329 * Class: com_aurellem_gb_Gb
330 * Method: nwritePNG
331 * Signature: (Ljava/lang/String;)V
332 */
333 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_nwritePNG
334 (JNIEnv *env, jclass clazz, jstring filename){
335 const char *_filename = env->GetStringUTFChars(filename, 0);
336 gbWritePNGFile(_filename);
337 }
341 /*
342 * Class: com_aurellem_gb_Gb
343 * Method: readMemory
344 * Signature: (I)I
345 */
346 JNIEXPORT jint JNICALL Java_com_aurellem_gb_Gb_readMemory
347 (JNIEnv *env, jclass clazz, jint address){
348 return (jint) gbReadMemory((u16) address);
349 }
351 /*
352 * Class: com_aurellem_gb_Gb
353 * Method: getFrameSound
354 * Signature: ([B)V
355 */
356 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getFrameSound
357 (JNIEnv *env, jclass clazz, jbyteArray arr){
358 jbyte *sound_store = env->GetByteArrayElements(arr, 0);
359 int i;
362 u8* soundBytes = (u8*) soundFrameSound;
363 for (i = 0; i < 44100*2; i++){
364 sound_store[i] = (jbyte) soundBytes[i];
365 }
367 /*
368 u8* soundBytes = (u8*) soundFinalWave;
369 for (i = 0; i < 1470*2 ; i++){
370 sound_store[i] = (jbyte) soundBytes[i];
371 }
372 */
374 env->ReleaseByteArrayElements(arr, sound_store, 0);
375 }
378 /*
379 * Class: com_aurellem_gb_Gb
380 * Method: getSoundFrameWritten
381 * Signature: ()I
382 */
383 JNIEXPORT jint JNICALL Java_com_aurellem_gb_Gb_getSoundFrameWritten
384 (JNIEnv *env, jclass clazz){
385 return soundFrameSoundWritten;
386 }
388 /*
389 * Class: com_aurellem_gb_Gb
390 * Method: setSoundFrameWritten
391 * Signature: (I)V
392 */
393 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_setSoundFrameWritten
394 (JNIEnv *env, jclass clazz , jint newSoundFrameWritten){
395 soundFrameSoundWritten = newSoundFrameWritten;
396 }
399 /*
400 * Class: com_aurellem_gb_Gb
401 * Method: getFrameSound2
402 * Signature: ([B)V
403 */
404 JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_getFrameSound2
405 (JNIEnv *env, jclass clazz, jbyteArray arr){
406 setbuf(stdout, NULL);
408 jbyte *sound_store = env->GetByteArrayElements(arr, 0);
409 int i;
411 for (i = 0; i < 1470*2; i++){
412 sound_store[i] = (jbyte) soundCopyBuffer[i];
413 }
415 env->ReleaseByteArrayElements(arr, sound_store, 0);
417 }