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 static TWOBYTES gSensorValue = 0;
27 static char* sensorReadTypes[3] = {
33 static char *sensorSetTypes[5] = {
41 static char *getSensorMode(byte code)
43 static char smBuffer[256];
45 strcpy(smBuffer, "mode = ");
48 case 0x00: strcat(smBuffer, "RAW"); break;
49 case 0x20: strcat(smBuffer, "BOOLEAN"); break;
50 case 0x40: strcat(smBuffer, "EDGE"); break;
51 case 0x60: strcat(smBuffer, "PULSE"); break;
52 case 0x80: strcat(smBuffer, "PERCENT"); break;
53 case 0xA0: strcat(smBuffer, "DEGC"); break;
54 case 0xC0: strcat(smBuffer, "DEGF"); break;
55 case 0xE0: strcat(smBuffer, "ANGLE"); break;
56 default: sprintf(smBuffer, "mode = INVALID (0x%1X)", code & 0xF0); break;
59 sprintf(&smBuffer[strlen(smBuffer)], ", slope = %d", code & 0x0F);
63 extern int verbose; /* If non-zero, generates verbose output. */
66 char *get_meaning(STACKWORD *);
68 void dump_flag(Object *obj)
70 if (is_allocated(obj))
74 printf("Ready for the garbage\n");
76 else if (is_array(obj))
78 printf("Array, type=%d, length=%d\n", get_element_type(obj), get_array_length(obj));
82 printf("Class index = %d\n", get_na_class_index(obj));
87 printf ("Free block, length=%d\n", get_free_length(obj));
93 * -- bits 0-14: Size of free block in words.
94 * -- bit 15 : Zero (not allocated).
96 * -- bits 0-7 : Class index.
97 * -- bits 8-12: Unused.
98 * -- bit 13 : Garbage collection mark.
99 * -- bit 14 : Zero (not an array).
100 * -- bit 15 : One (allocated).
102 * -- bits 0-8 : Array length (0-527).
103 * -- bits 9-12: Element type.
104 * -- bit 13 : Garbage collection mark.
105 * -- bit 14 : One (is an array).
106 * -- bit 15 : One (allocated).
110 char* string2chp(String* s)
119 obj = word2obj(get_word((byte*)(&(s->characters)), 4));
120 pA = jchar_array(obj);
121 length = get_array_length(obj);
122 ret = malloc(length+1);
123 for (i=0; i<length; i++)
134 * NOTE: The technique is not the same as that used in TinyVM.
136 void dispatch_native (TWOBYTES signature, STACKWORD *paramBase)
138 ClassRecord *classRecord;
143 monitor_wait((Object*) word2ptr(paramBase[0]), 0);
146 monitor_wait((Object*) word2ptr(paramBase[0]), paramBase[2]);
149 monitor_notify((Object*) word2ptr(paramBase[0]), false);
152 monitor_notify((Object*) word2ptr(paramBase[0]), true);
155 init_thread ((Thread *) word2ptr(paramBase[0]));
158 schedule_request( REQUEST_SWITCH_THREAD);
161 sleep_thread (paramBase[1]);
162 schedule_request( REQUEST_SWITCH_THREAD);
164 case getPriority_4_5I:
165 push_word (get_thread_priority ((Thread*)word2ptr(paramBase[0])));
167 case setPriority_4I_5V:
169 STACKWORD p = (STACKWORD)paramBase[1];
170 if (p > MAX_PRIORITY || p < MIN_PRIORITY)
171 throw_exception(illegalArgumentException);
173 set_thread_priority ((Thread*)word2ptr(paramBase[0]), p);
176 case currentThread_4_5Ljava_3lang_3Thread_2:
177 push_ref(ptr2ref(currentThread));
180 interrupt_thread((Thread*)word2ptr(paramBase[0]));
182 case interrupted_4_5Z:
184 JBYTE i = currentThread->interruptState != INTERRUPT_CLEARED;
185 currentThread->interruptState = INTERRUPT_CLEARED;
189 case isInterrupted_4_5Z:
190 push_word(((Thread*)word2ptr(paramBase[0]))->interruptState
191 != INTERRUPT_CLEARED);
193 case setDaemon_4Z_5V:
194 ((Thread*)word2ptr(paramBase[0]))->daemon = (JBYTE)paramBase[1];
197 push_word(((Thread*)word2ptr(paramBase[0]))->daemon);
200 join_thread((Thread*)word2ptr(paramBase[0]));
203 join_thread((Thread*)word2obj(paramBase[0]));
206 schedule_request(REQUEST_EXIT);
208 case currentTimeMillis_4_5J:
210 push_word (get_sys_time());
213 set_poller(word2ptr(paramBase[0]));
215 case readSensorValue_4I_5I:
216 // Parameters: int portId
221 printf("Reading sensor %ld, returned value %d\n",sensors[paramBase[0]].value);
222 push_word (sensors[paramBase[0]].value);
224 case setADTypeById_4II_5V:
229 printf("Setting sensor %d to AD type %d\n",paramBase[0], paramBase[1]);
231 case setPowerTypeById_4II_5V:
236 printf("Setting sensor %d to power type %d\n",paramBase[0], paramBase[1]);
238 case freeMemory_4_5J:
240 push_word (getHeapFree());
242 case totalMemory_4_5J:
244 push_word (getHeapSize());
246 case test_4Ljava_3lang_3String_2Z_5V:
249 printf("%s\n",string2chp((String*)word2ptr(paramBase[0])));
250 throw_exception(error);
253 case testEQ_4Ljava_3lang_3String_2II_5V:
254 if (paramBase[1] != paramBase[2])
256 printf("%s: expected %ld, got %ld\n",string2chp((String*)word2ptr(paramBase[0])), paramBase[1], paramBase[2]);
257 throw_exception(error);
260 case floatToIntBits_4F_5I: // Fall through
261 case intBitsToFloat_4I_5F:
262 push_word (paramBase[0]);
264 case drawString_4Ljava_3lang_3String_2II_5V:
266 byte *p = word2ptr(paramBase[0]);
268 //printf("displayString: pointer is %x\n",p);
269 //printf("Object size is %d\n",sizeof(Object));
270 Object *charArray = (Object *) word2ptr(get_word(p + HEADER_SIZE, 4));
271 //printf("Chars is %x\n",charArray);
272 len = charArray->flags.arrays.length;
273 //printf("length is %d\n",len);
276 char *chars = ((char *) charArray) + HEADER_SIZE;
277 //printf("chars is %x\n",chars);
278 for(i=0;i<len;i++) buff[i] = chars[i+i];
284 printf("drawString called with parameters %s,%d,%d\n",buff,paramBase[1],paramBase[2]);
288 case drawInt_4III_5V:
293 printf("drawInt called with parameters %d, %d, %d\n",paramBase[0],paramBase[1],paramBase[2]);
295 case drawInt_4IIII_5V:
300 printf("drawInt called with parameters %d, %d, %d, %d\n",paramBase[0],paramBase[1],paramBase[2],paramBase[3]);
302 case controlMotorById_4III_5V:
307 printf("controlMotor called with parameters %d,%d,%d\n",paramBase[0],paramBase[1],paramBase[2]);
314 printf("Displayed Refreshed\n");
321 printf("Display cleared\n");
323 case setDisplay_4_1I_5V:
328 printf("Display set\n");
330 case getVoltageMilliVolt_4_5I:
335 printf("getVoltageMillivolts returning 9999\n");
338 case readButtons_4_5I:
343 printf("readButtons returning 0\n");
346 case getTachoCountById_4I_5I:
351 printf("getTachoCount on Motor %d returning 0\n", paramBase[0]);
354 case resetTachoCountById_4I_5V:
359 printf("resetTachoCount on Motor %d \n", paramBase[0]);
361 case i2cEnableById_4I_5V:
366 printf("i2cEnableById\n");
368 case i2cDisableById_4I_5V:
373 printf("i2cDisableById\n");
375 case i2cBusyById_4I_5I:
380 printf("i2cBusyById\n");
383 case i2cStartById_4IIII_1BII_5I:
385 Object *p = word2ptr(paramBase[4]);
386 byte *byteArray = (((byte *) p) + HEADER_SIZE);
391 printf("i2cStart called with parameters %d, %d, %d, %d %x, %d, %d\n",
402 case playTone_4II_5V:
407 printf("playTone with tone = %d, duration = %d\n", paramBase[0], paramBase[1]);
409 case btSend_4_1BI_5V:
411 Object *p = word2ptr(paramBase[0]);
412 byte *byteArray = (((byte *) p) + HEADER_SIZE);
417 printf("btSend with parameter &d\n", byteArray);
420 case btReceive_4_1B_5V:
422 Object *p = word2ptr(paramBase[0]);
423 byte *byteArray = (((byte *) p) + HEADER_SIZE);
427 printf("btReceive called with parameter %x\n", byteArray);
431 case btGetCmdMode_4_5I:
436 printf("btGetCmdMode returning 1\n");
439 case btSetCmdMode_4I_5V:
444 printf("btSetCmdMode\n");
446 case btStartADConverter_4_5V:
448 case usbRead_4_1BI_5I:
450 Object *p = word2ptr(paramBase[0]);
451 byte *byteArray = (((byte *) p) + HEADER_SIZE);
455 printf("usbReceive called with parameters %x, %d\n", byteArray, paramBase[1]);
460 case usbWrite_4_1BI_5V:
462 Object *p = word2ptr(paramBase[0]);
463 byte *byteArray = (((byte *) p) + HEADER_SIZE);
467 printf("usbWrite called with parameters %x, %d\n", byteArray, paramBase[1]);
471 case writePage_4_1BI_5V:
473 Object *p = word2ptr(paramBase[0]);
474 unsigned long *intArray = (unsigned long *) (((byte *) p) + HEADER_SIZE);
478 printf("writePage called with parameters %x, %d\n", intArray, paramBase[1]);
482 case readPage_4_1BI_5V:
485 Object *p = word2ptr(paramBase[0]);
486 unsigned long *intArray = (unsigned long *) (((byte *) p) + HEADER_SIZE);
490 printf("readPage called with parameters %x, %d\n", intArray, paramBase[1]);
498 printf("exec called\n");
505 printf("udpReset called\n");
508 case playSample_4IIII_5V:
513 printf("Playing sound sample\n");
517 printf("Received bad native method code: %d\n", signature);
519 throw_exception (noSuchMethodError);