3 * Native method handling for unix_impl (emulation).
10 #include "constants.h"
11 #include "specialsignatures.h"
12 #include "specialclasses.h"
18 #include "configure.h"
19 #include "interpreter.h"
20 #include "exceptions.h"
21 #include "platform_config.h"
25 #define MAJOR_VERSION 0
26 #define MINOR_VERSION 6
30 #define push_word( a) push_word_cur( a)
31 #define push_ref( a) push_ref_cur( a)
33 static TWOBYTES gSensorValue = 0;
35 static char* sensorReadTypes[3] = {
41 static char *sensorSetTypes[5] = {
49 #define DISPLAY_WIDTH 100
50 #define DISPLAY_DEPTH 8
55 unsigned char display[DISPLAY_DEPTH+1][DISPLAY_WIDTH];
56 } __attribute__((packed)) display_array;
58 static char display_init()
60 // Initialise the array parameters so that the display can
61 // be memory mapped into the Java address space
62 display_array.hdr.flags.arrays.isArray = 1;
63 // NOTE This object must always be marked, otherwise very, very bad
64 // things will happen!
65 display_array.hdr.flags.arrays.mark = 1;
66 display_array.hdr.flags.arrays.length = 200;
67 display_array.hdr.flags.arrays.isAllocated = 1;
68 display_array.hdr.flags.arrays.type = T_INT;
69 display_array.hdr.monitorCount = 0;
70 display_array.hdr.threadId = 0;
73 static char *getSensorMode(byte code)
75 static char smBuffer[256];
77 strcpy(smBuffer, "mode = ");
80 case 0x00: strcat(smBuffer, "RAW"); break;
81 case 0x20: strcat(smBuffer, "BOOLEAN"); break;
82 case 0x40: strcat(smBuffer, "EDGE"); break;
83 case 0x60: strcat(smBuffer, "PULSE"); break;
84 case 0x80: strcat(smBuffer, "PERCENT"); break;
85 case 0xA0: strcat(smBuffer, "DEGC"); break;
86 case 0xC0: strcat(smBuffer, "DEGF"); break;
87 case 0xE0: strcat(smBuffer, "ANGLE"); break;
88 default: sprintf(smBuffer, "mode = INVALID (0x%1X)", code & 0xF0); break;
91 sprintf(&smBuffer[strlen(smBuffer)], ", slope = %d", code & 0x0F);
95 extern int verbose; /* If non-zero, generates verbose output. */
98 char *get_meaning(STACKWORD *);
100 void dump_flag(Object *obj)
102 if (is_allocated(obj))
106 printf("Ready for the garbage\n");
108 else if (is_array(obj))
110 printf("Array, type=%d, length=%d\n", get_element_type(obj), get_array_length(obj));
114 printf("Class index = %d\n", get_na_class_index(obj));
119 printf ("Free block, length=%d\n", get_free_length(obj));
123 * Object/block flags.
125 * -- bits 0-14: Size of free block in words.
126 * -- bit 15 : Zero (not allocated).
128 * -- bits 0-7 : Class index.
129 * -- bits 8-12: Unused.
130 * -- bit 13 : Garbage collection mark.
131 * -- bit 14 : Zero (not an array).
132 * -- bit 15 : One (allocated).
134 * -- bits 0-8 : Array length (0-527).
135 * -- bits 9-12: Element type.
136 * -- bit 13 : Garbage collection mark.
137 * -- bit 14 : One (is an array).
138 * -- bit 15 : One (allocated).
142 char* string2chp(String* s)
151 obj = word2obj(get_word_4_ns((byte*)(&(s->characters))));
152 pA = jchar_array(obj);
153 length = get_array_length(obj);
154 ret = malloc(length+1);
155 for (i=0; i<length; i++)
165 boolean debug_uncaught_exception(Object * exception,
166 const Thread * thread,
167 const MethodRecord * methodRecord,
168 const MethodRecord * rootMethod,
169 byte * pc, int exceptionFrame)
175 * NOTE: The technique is not the same as that used in TinyVM.
177 void dispatch_native (TWOBYTES signature, STACKWORD *paramBase)
179 ClassRecord *classRecord;
184 monitor_wait((Object*) word2ptr(paramBase[0]), 0);
187 monitor_wait((Object*) word2ptr(paramBase[0]), paramBase[2]);
190 monitor_notify((Object*) word2ptr(paramBase[0]), false);
193 monitor_notify((Object*) word2ptr(paramBase[0]), true);
196 init_thread ((Thread *) word2ptr(paramBase[0]));
199 schedule_request( REQUEST_SWITCH_THREAD);
202 sleep_thread (paramBase[1]);
203 schedule_request( REQUEST_SWITCH_THREAD);
205 case getPriority_4_5I:
206 push_word (get_thread_priority ((Thread*)word2ptr(paramBase[0])));
208 case setPriority_4I_5V:
210 STACKWORD p = (STACKWORD)paramBase[1];
211 if (p > MAX_PRIORITY || p < MIN_PRIORITY)
212 throw_exception(illegalArgumentException);
214 set_thread_priority ((Thread*)word2ptr(paramBase[0]), p);
217 case currentThread_4_5Ljava_3lang_3Thread_2:
218 push_ref(ptr2ref(currentThread));
221 interrupt_thread((Thread*)word2ptr(paramBase[0]));
223 case interrupted_4_5Z:
225 JBYTE i = currentThread->interruptState != INTERRUPT_CLEARED;
226 currentThread->interruptState = INTERRUPT_CLEARED;
230 case isInterrupted_4_5Z:
231 push_word(((Thread*)word2ptr(paramBase[0]))->interruptState
232 != INTERRUPT_CLEARED);
234 case setDaemon_4Z_5V:
235 ((Thread*)word2ptr(paramBase[0]))->daemon = (JBYTE)paramBase[1];
238 push_word(((Thread*)word2ptr(paramBase[0]))->daemon);
241 join_thread((Thread*)word2ptr(paramBase[0]));
244 join_thread((Thread*)word2obj(paramBase[0]));
247 schedule_request(REQUEST_EXIT);
249 case currentTimeMillis_4_5J:
251 push_word (get_sys_time());
254 set_poller(word2ptr(paramBase[0]));
256 case readSensorValue_4I_5I:
257 // Parameters: int portId
262 printf("Reading sensor %d, returned value %d\n",paramBase[0], sensors[paramBase[0]].value);
263 push_word (sensors[paramBase[0]].value);
265 case setADTypeById_4II_5V:
270 printf("Setting sensor %d to AD type %d\n",paramBase[0], paramBase[1]);
272 case setPowerTypeById_4II_5V:
277 printf("Setting sensor %d to power type %d\n",paramBase[0], paramBase[1]);
279 case freeMemory_4_5J:
281 push_word (getHeapFree());
283 case totalMemory_4_5J:
285 push_word (getHeapSize());
287 case test_4Ljava_3lang_3String_2Z_5V:
290 printf("%s\n",string2chp((String*)word2ptr(paramBase[0])));
291 throw_exception(error);
294 case testEQ_4Ljava_3lang_3String_2II_5V:
295 if (paramBase[1] != paramBase[2])
297 printf("%s: expected %ld, got %ld\n",string2chp((String*)word2ptr(paramBase[0])), paramBase[1], paramBase[2]);
298 throw_exception(error);
301 case floatToIntBits_4F_5I: // Fall through
302 case intBitsToFloat_4I_5F:
303 push_word (paramBase[0]);
305 case drawString_4Ljava_3lang_3String_2II_5V:
307 byte *p = word2ptr(paramBase[0]);
309 //printf("displayString: pointer is %x\n",p);
310 //printf("Object size is %d\n",sizeof(Object));
311 Object *charArray = (Object *) word2ptr(get_word_4_ns(p + HEADER_SIZE));
312 //printf("Chars is %x\n",charArray);
313 len = charArray->flags.arrays.length;
314 //printf("length is %d\n",len);
317 char *chars = ((char *) charArray) + HEADER_SIZE;
318 //printf("chars is %x\n",chars);
319 for(i=0;i<len;i++) buff[i] = chars[i+i];
325 printf("drawString called with parameters %s, %d, %d\n",buff,paramBase[1],paramBase[2]);
329 case drawInt_4III_5V:
334 printf("drawInt called with parameters %d, %d, %d\n",paramBase[0],paramBase[1],paramBase[2]);
336 case drawInt_4IIII_5V:
341 printf("drawInt called with parameters %d, %d, %d, %d\n",paramBase[0],paramBase[1],paramBase[2],paramBase[3]);
348 printf("Displayed Refreshed\n");
355 printf("Display cleared\n");
357 case setDisplay_4_1I_5V:
362 printf("Display set\n");
364 case getDisplay_4_5_1B:
369 printf("Get display\n");
371 push_word((STACKWORD) ptr2word(&display_array));
373 case setAutoRefresh_4I_5V:
378 printf("Set autodisplay to %d\n", paramBase[0]);
380 case bitBlt_4_1BIIII_1BIIIIIII_5V:
382 Object *src = word2ptr(paramBase[0]);
383 Object *dst = word2ptr(paramBase[5]);
388 printf("bitBlt called\n");
391 case getSystemFont_4_5_1B:
396 printf("getSystemFont called\n");
399 case getVoltageMilliVolt_4_5I:
404 printf("getVoltageMillivolts returning 9999\n");
407 case getButtons_4_5I:
411 printf("readButtons returning 0\n");
415 case getTachoCountById_4I_5I:
420 printf("getTachoCount on Motor %d returning 0\n", paramBase[0]);
423 case controlMotorById_4III_5V:
428 printf("controlMotor called with parameters %d, %d, %d\n",paramBase[0],paramBase[1],paramBase[2]);
430 case resetTachoCountById_4I_5V:
435 printf("resetTachoCount on Motor %d \n", paramBase[0]);
437 case i2cEnableById_4I_5V:
442 printf("i2cEnableById\n");
444 case i2cDisableById_4I_5V:
449 printf("i2cDisableById\n");
451 case i2cBusyById_4I_5I:
456 printf("i2cBusyById\n");
459 case i2cStartById_4IIII_1BII_5I:
461 Object *p = word2ptr(paramBase[4]);
462 byte *byteArray = (((byte *) p) + HEADER_SIZE);
467 printf("i2cStart called with parameters %d, %d, %d, %d %x, %d, %d\n",
478 case playFreq_4III_5V:
483 printf("playFreq with freq = %d, duration = %d, volume = %d\n", paramBase[0], paramBase[1], paramBase[2]);
485 case btSend_4_1BI_5V:
487 Object *p = word2ptr(paramBase[0]);
488 byte *byteArray = (((byte *) p) + HEADER_SIZE);
493 printf("btSend called with parameters %x, %d\n", byteArray, paramBase[1]);
496 case btReceive_4_1B_5V:
498 Object *p = word2ptr(paramBase[0]);
499 byte *byteArray = (((byte *) p) + HEADER_SIZE);
504 printf("btReceive called with parameter %x\n", byteArray);
507 case btGetBC4CmdMode_4_5I:
512 printf("btGetBC4CmdMode returning 1\n");
515 case btSetArmCmdMode_4I_5V:
520 printf("btSetArmCmdMode\n");
522 case btStartADConverter_4_5V:
527 printf("btStartAdConverter\n");
529 case btSetResetLow_4_5V:
534 printf("btSetResetLow\n");
536 case btSetResetHigh_4_5V:
541 printf("btSetResetHigh\n");
543 case btWrite_4_1BII_5I:
545 Object *p = word2ptr(paramBase[0]);
546 byte *byteArray = (((byte *) p) + HEADER_SIZE);
551 printf("btWrite called with parameters %x, %d, %d\n", byteArray,paramBase[1],paramBase[2]);
554 case btRead_4_1BII_5I:
556 Object *p = word2ptr(paramBase[0]);
557 byte *byteArray = (((byte *) p) + HEADER_SIZE);
562 printf("btRead called with parameters %x, %d, %d\n", byteArray,paramBase[1],paramBase[2]);
569 printf("btPending called\n");
573 case usbRead_4_1BII_5I:
575 Object *p = word2ptr(paramBase[0]);
576 byte *byteArray = (((byte *) p) + HEADER_SIZE);
580 printf("usbReceive called with parameters %x, %d\n", byteArray, paramBase[1]);
585 case usbWrite_4_1BII_5I:
587 Object *p = word2ptr(paramBase[0]);
588 byte *byteArray = (((byte *) p) + HEADER_SIZE);
592 printf("usbWrite called with parameters %x, %d\n", byteArray, paramBase[1]);
601 case usbEnable_4I_5V:
606 printf("usbEnable called\n");;
610 case usbDisable_4_5V:
615 printf("usbDisable called\n");;
623 printf("udpReset called\n");
626 case usbSetSerialNo_4Ljava_3lang_3String_2_5V:
628 byte *p = word2ptr(paramBase[0]);
630 Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));
632 len = get_array_length(charArray);
636 printf("udpSetSerial called\n");
640 case usbSetName_4Ljava_3lang_3String_2_5V:
642 byte *p = word2ptr(paramBase[0]);
644 Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p)));
646 len = get_array_length(charArray);
650 printf("udpSetName called\n");
654 case writePage_4_1BI_5V:
656 Object *p = word2ptr(paramBase[0]);
657 unsigned long *intArray = (unsigned long *) (((byte *) p) + HEADER_SIZE);
661 printf("writePage called with parameters %x, %d\n", intArray, paramBase[1]);
665 case readPage_4_1BI_5V:
668 Object *p = word2ptr(paramBase[0]);
669 unsigned long *intArray = (unsigned long *) (((byte *) p) + HEADER_SIZE);
673 printf("readPage called with parameters %x, %d\n", intArray, paramBase[1]);
681 printf("exec called\n");
684 case playSample_4IIIII_5V:
689 printf("Playing sound sample\n");
696 printf("Sound getTime called\n");
699 case getDataAddress_4Ljava_3lang_3Object_2_5I:
704 printf("Data address is %x\n",ptr2word (((byte *) word2ptr (paramBase[0])) + HEADER_SIZE));
711 printf("Collecting garbage\n");
714 push_word (sys_diagn(paramBase[0], paramBase[1]));
721 printf("Shutting down\n");
723 case arraycopy_4Ljava_3lang_3Object_2ILjava_3lang_3Object_2II_5V:
725 Object *p1 = word2ptr(paramBase[0]);
726 Object *p2 = word2ptr(paramBase[2]);
727 arraycopy(p1, paramBase[1], p2, paramBase[3], paramBase[4]);
730 case executeProgram_4I_5V:
733 ClassRecord *classRecord;
734 classRecord = get_class_record (get_entry_class (paramBase[0]));
735 // Initialize top word with fake parameter for main():
736 set_top_ref_cur (JNULL);
737 // Push stack frame for main method:
738 mRec= find_method (classRecord, main_4_1Ljava_3lang_3String_2_5V);
739 dispatch_special (mRec, curPc);
740 dispatch_static_initializer (classRecord, curPc);
748 printf("Set debug\n");
751 push_word(*((unsigned long *)(paramBase[0])));
753 case eventOptions_4II_5I:
759 printf("Debug event options\n");
763 case suspendThread_4Ljava_3lang_3Object_2_5V:
764 suspend_thread(ref2ptr(paramBase[0]));
766 case resumeThread_4Ljava_3lang_3Object_2_5V:
767 resume_thread(ref2ptr(paramBase[0]));
769 case getProgramExecutionsCount_4_5I:
770 push_word(gProgramExecutions);
772 case getFirmwareRevision_4_5I:
775 case getFirmwareMajorVersion_4_5I:
776 push_word((STACKWORD) MAJOR_VERSION);
778 case getFirmwareMinorVersion_4_5I:
779 push_word((STACKWORD) MINOR_VERSION);
783 printf("Received bad native method code: %d\n", signature);
785 throw_exception (noSuchMethodError);