3 * Entry source file for TinyVM emulator.
11 #include "constants.h"
15 #include "specialclasses.h"
16 #include "specialsignatures.h"
19 #include "interpreter.h"
20 #include "exceptions.h"
25 #include "platform_hooks.h"
27 #define MEMORY_SIZE 8192 /* 16 Kb */
28 #define EXTRA_MEMORY_SIZE 1536 /* 3 Kb */
33 struct timeval gStart;
34 struct itimerval itimer =
40 int verbose = 0; /* If 1, print descriptive strings. */
42 int last_sys_time; /* to generate ticks */
43 int last_ad_time; /* to generate sensor reads */
45 void handle_uncaught_exception (Object *exception,
47 const MethodRecord *methodRecord,
48 const MethodRecord *rootMethod,
51 printf ("*** UNCAUGHT EXCEPTION/ERROR: \n");
52 printf ("-- Exception class : %u\n", (unsigned) get_class_index (exception));
53 printf ("-- Thread : %u\n", (unsigned) thread->threadId);
54 printf ("-- Method signature : %u\n", (unsigned) methodRecord->signatureId);
55 printf ("-- Root method sig. : %u\n", (unsigned) rootMethod->signatureId);
56 printf ("-- Bytecode offset : %u\n", (unsigned) pc -
57 (int) get_code_ptr(methodRecord));
60 void switch_thread_hook()
65 FOURBYTES sys_time = 0;
68 void timer_handler(int signo)
70 gettimeofday(&now, 0);
71 // 11/17/01: Jose commented out:
72 // 03/29/02: Paul put back in. Regression tests will fail otherwise.
73 timersub(&now, &gStart, &now);
74 sys_time = now.tv_sec*1000 + now.tv_usec/1000;
75 signal(SIGALRM, timer_handler);
78 FOURBYTES get_sys_time_impl()
91 #if EXECUTE_FROM_FLASH
93 MasterRecord *mrec = get_master_record();
94 int staticSize = mrec->staticStateLength;
95 int statusSize = (mrec->lastClass + 1) * sizeof( classStatusBase[0]);
97 staticSize = (staticSize + 1) & ~(1);
98 statusSize = (statusSize + 3) & ~(3);
100 classStaticStateBase = malloc( staticSize * 2);
101 classStatusBase = malloc( statusSize);
103 memset( classStatusBase, 0, statusSize);
107 // Initialize binary image state
116 size = EXTRA_MEMORY_SIZE;
117 region = (byte *) malloc (size * sizeof (TWOBYTES));
118 memory_add_region (region, region + size * sizeof (TWOBYTES));
121 region = (byte *) malloc (size * sizeof (TWOBYTES));
122 memory_add_region (region, region + size * sizeof (TWOBYTES));
125 // Initialize exceptions
127 // Create the boot thread (bootThread is a special global)
128 bootThread = (Thread *) new_object_for_class (JAVA_LANG_THREAD);
131 printf ("Created bootThread: %d. Initializing...\n", (int) bootThread);
135 for (count = 0; count < 100; count++)
141 TWOBYTES numNodes, biggest, freeMem;
142 scan_memory (&numNodes, &biggest, &freeMem);
143 printf ("nodes = %d\n", (int) numNodes);
144 printf ("biggest = %d\n", (int) biggest);
145 printf ("freeMem = %d\n", (int) freeMem);
150 if (!init_thread (bootThread))
152 printf ("Unable to initialize threading module.\n");
155 // Execute the bytecode interpreter
156 set_program_number (0);
158 printf ("Engine starting.\n");
161 // Engine returns when all non-daemon threads are dead
163 printf ("Engine finished.\n");
171 /***************************************************************************
173 * Parses command line. Format is:
174 * argv[0] [-v] bin_file
177 * -v Verbose mode. Prints text output rather than raw output.
179 *--------------------------------------------------------------------------
180 * To go into man page:
181 * Name: emu-lejosrun - Emulate lejos RCX code in Unix
183 * Synosis: emu-lejosrun [-v] bin_file
185 * Description: Executes a binary file created by the lejos compiler within
186 * Unix rather than in the RCX environment. The Java byte-codes
187 * are executed here, and their actions are listed rather than
188 * executed as they would be on the real RCX device.
190 * Options: -v Verbose mode. Normally the output is printed in raw
191 * mode. The actual hex values are printed. Using this
192 * option displays more user-friendly output.
193 *--------------------------------------------------------------------------
194 ***************************************************************************/
195 int main (int argc, char *argv[])
198 char *file = argv[argc - 1];
200 /* Process any options. */
201 while(--argc > 0 && (*++argv)[0] == '-')
202 while((c = *++argv[0]) != 0)
208 printf("%s: unknown option %c\n",
214 printf ("%s runs a binary dumped by the linker.\n", argv[0]);
215 printf ("Use: %s [-v] binary_file\n", argv[0]);
219 printf ("Reading binary %s\n", file);
222 if (gettimeofday(&gStart, NULL))
223 perror("main: gettimeofday");
224 timer_handler (SIGALRM);
225 if (setitimer(ITIMER_REAL, &itimer, null) < 0)
227 printf ("Failed to set interval timer\n");
232 last_ad_time = get_sys_time();
235 itimer.it_value.tv_usec = 0;
236 setitimer(ITIMER_REAL, &itimer, null);
237 signal(SIGALRM, SIG_DFL);