1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // --------------------------------------------------------------------------*/
11 * SquirrelJME RatufaCoat Header.
17 #ifndef SJME_hGRATUFACOATSJMERCHSJMERCH
18 #define SJME_hGRATUFACOATSJMERCHSJMERCH
20 /** Standard Includes. */
27 /** Version string. */
28 #define SQUIRRELJME_VERSION_STRING "0.3.0"
30 /** Is this a 64-bit system? */
31 #if !defined(SJME_BITS)
32 #if defined(_LP64) || defined(__LP64__) || defined(__x86_64__) || \
33 defined(_M_X64) || defined(_M_AMD64) || defined(__aarch64__) || \
34 defined(__ia64__) || defined(__ia64) || defined(_M_IA64) || \
35 defined(__itanium__) || defined(__powerpc64__) || \
36 defined(__ppc64__) || defined(_ARCH_PPC64) || defined(_ARCH_PPC64)
43 /** Possibly detect endianess. */
44 #if !defined(SJME_BIG_ENDIAN) && !defined(SJME_LITTLE_ENDIAN)
45 /** Defined by the system? */
46 #if !defined(SJME_BIG_ENDIAN)
47 #if defined(MSB_FIRST) || \
48 (defined(WORDS_BIGENDIAN) && WORDS_BIGENDIAN != 0)
49 #define SJME_BIG_ENDIAN
53 /** Just set little endian if no endianess was defined */
54 #if !defined(SJME_BIG_ENDIAN) && !defined(SJME_LITTLE_ENDIAN)
55 #define SJME_LITTLE_ENDIAN
58 /** If both are defined, just set big endian. */
59 #if defined(SJME_BIG_ENDIAN) && defined(SJME_LITTLE_ENDIAN)
60 #undef SJME_LITTLE_ENDIAN
65 #if defined(__linux__) || defined(__gnu_linux__)
66 #define SJME_IS_LINUX 1
70 #if defined(MSDOS) || defined(_MSDOS) || defined(__MSDOS__) || defined(__DOS__)
77 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
78 (defined(__WATCOMC__) && __WATCOMC__ >= 1270) || \
79 (defined(_MSC_VER) && _MSC_VER >= 1600) || \
80 (defined(__GNUC__) && __GNUC__ >= 4) || \
81 (defined(PSP) || defined(PS2))
85 #elif defined(_MSC_VER) && _MSC_VER < 1600
86 typedef signed __int8
int8_t;
87 typedef signed __int16
int16_t;
88 typedef signed __int32
int32_t;
90 typedef unsigned __int8
uint8_t;
91 typedef unsigned __int16
uint16_t;
92 typedef unsigned __int32
uint32_t;
98 #define UINT8_C(x) x##U
99 #define UINT16_C(x) x##U
100 #define UINT32_C(x) x##U
103 #elif defined(__palmos__)
104 #include <PalmTypes.h>
107 typedef Int16
int16_t;
108 typedef Int32
int32_t;
110 typedef UInt8
uint8_t;
111 typedef UInt16
uint16_t;
112 typedef UInt32
uint32_t;
114 typedef int32_t intptr_t;
115 typedef uint32_t uintptr_t;
119 #define INT32_C(x) x##L
121 #define UINT8_C(x) x##U
122 #define UINT16_C(x) x##U
123 #define UINT32_C(x) x##UL
125 #define SIZE_MAX UINT32_C(0xFFFFFFFF)
127 /** Guessed otherwise. */
129 #if defined(SCHAR_MAX) && SCHAR_MAX == 127
130 typedef signed char int8_t;
135 #if defined(INT_MAX) && INT_MAX == 32767
136 typedef signed int int16_t;
137 typedef unsigned int uint16_t;
140 #define UINT16_C(x) x##U
141 #elif defined(SHORT_MAX) && SHORT_MAX == 32767
142 typedef signed short int16_t;
143 typedef unsigned short uint16_t;
146 #define UINT16_C(x) x##U
149 #if defined(INT_MAX) && INT_MAX > 32767
150 typedef signed int int32_t;
151 typedef unsigned int uint32_t;
154 #elif defined(LONG_MAX) && LONG_MAX > 32767
155 typedef signed long int32_t;
156 typedef unsigned long uint32_t;
158 #define INT32_C(x) x##L
159 #define UINT32_C(x) x##UL
165 #ifndef SJME_CXX_IS_EXTERNED
166 #define SJME_CXX_IS_EXTERNED
167 #define SJME_cXRATUFACOATSJMERCHSJMERCH
170 #endif /** #ifdef SJME_CXX_IS_EXTERNED */
171 #endif /** #ifdef __cplusplus */
173 /****************************************************************************/
175 /** {@code byte} type. */
176 typedef int8_t sjme_jbyte
;
178 /** {@code short} type. */
179 typedef int16_t sjme_jshort
;
181 /** {@code char} type. */
182 typedef uint16_t sjme_jchar
;
184 /** {@code int} type. */
185 typedef int32_t sjme_jint
;
187 /** Unsigned {@code int} type. */
188 typedef uint32_t sjme_juint
;
190 /** Constant value macros. */
191 #define SJME_JBYTE_C(x) INT8_C(x)
192 #define SJME_JSHORT_C(x) INT16_C(x)
193 #define SJME_JCHAR_C(x) UINT16_C(x)
194 #define SJME_JINT_C(x) INT32_C(x)
195 #define SJME_JUINT_C(x) UINT32_C(x)
197 /** Maximum values. */
198 #define SJME_JINT_MAX_VALUE INT32_C(0x7FFFFFFF)
200 /** Unsigned short mask. */
201 #define SJME_JINT_USHORT_MASK INT32_C(0xFFFF)
203 /** Pointer conversion. */
204 #define SJME_POINTER_TO_JINT(x) ((sjme_jint)((uintptr_t)(x)))
205 #define SJME_JINT_TO_POINTER(x) ((void*)((uintptr_t)((sjme_jint)(x))))
207 #define SJME_POINTER_TO_JMEM(x) (((uintptr_t)(x)))
208 #define SJME_JMEM_TO_POINTER(x) ((void*)((uintptr_t)((x))))
211 #define SJME_POINTER_OFFSET(p, o) SJME_JINT_TO_POINTER( \
212 SJME_POINTER_TO_JINT(p) + ((sjme_jint)(o)))
214 /** Pointer math, no precision loss. */
215 #define SJME_POINTER_OFFSET_LONG(p, o) SJME_JMEM_TO_POINTER( \
216 SJME_POINTER_TO_JMEM(p) + ((uintptr_t)(o)))
218 /** Standard C format for arguments. */
219 #define SJME_JVMARG_FORMAT_STDC SJME_JINT_C(1)
221 /** Open file for reading. */
222 #define SJME_OPENMODE_READ SJME_JINT_C(1)
224 /** Open file for read and writing. */
225 #define SJME_OPENMODE_READWRITE SJME_JINT_C(2)
227 /** Open file for read and writing, create new file or truncate existing. */
228 #define SJME_OPENMODE_READWRITETRUNCATE SJME_JINT_C(3)
231 #define SJME_ERROR_NONE SJME_JINT_C(0)
233 /** Unknown error. */
234 #define SJME_ERROR_UNKNOWN SJME_JINT_C(-1)
236 /** File does not exist. */
237 #define SJME_ERROR_NOSUCHFILE SJME_JINT_C(-2)
239 /** Invalid argument. */
240 #define SJME_ERROR_INVALIDARG SJME_JINT_C(-3)
242 /** End of file reached. */
243 #define SJME_ERROR_ENDOFFILE SJME_JINT_C(-4)
245 /** No memory available. */
246 #define SJME_ERROR_NOMEMORY SJME_JINT_C(-5)
248 /** No native ROM file specified. */
249 #define SJME_ERROR_NONATIVEROM SJME_JINT_C(-6)
251 /** No support for files. */
252 #define SJME_ERROR_NOFILES SJME_JINT_C(-7)
254 /** Invalid ROM magic number. */
255 #define SJME_ERROR_INVALIDROMMAGIC SJME_JINT_C(-8)
257 /** Invalid JAR magic number. */
258 #define SJME_ERROR_INVALIDJARMAGIC SJME_JINT_C(-9)
260 /** Invalid end of BootRAM. */
261 #define SJME_ERROR_INVALIDBOOTRAMEND SJME_JINT_C(-10)
263 /** Invalid BootRAM seed. */
264 #define SJME_ERROR_INVALIDBOOTRAMSEED SJME_JINT_C(-11)
266 /** CPU hit breakpoint. */
267 #define SJME_ERROR_CPUBREAKPOINT SJME_JINT_C(-12)
269 /** Cannot write Java values. */
270 #define SJME_ERROR_NOJAVAWRITE SJME_JINT_C(-13)
273 #define SJME_ERROR_READERROR SJME_JINT_C(-14)
275 /** Early end of file reached. */
276 #define SJME_ERROR_EARLYEOF SJME_JINT_C(-15)
278 /** Virtual machine not ready. */
279 #define SJME_ERROR_JVMNOTREADY SJME_JINT_C(-16)
281 /** The virtual machine has exited, supervisor boot okay. */
282 #define SJME_ERROR_JVMEXIT_SUV_OKAY SJME_JINT_C(-17)
284 /** The virtual machine has exited, the supervisor did not flag! */
285 #define SJME_ERROR_JVMEXIT_SUV_FAIL SJME_JINT_C(-18)
287 /** Thread returned at the top-most frame and not through a system call. */
288 #define SJME_ERROR_THREADRETURN SJME_JINT_C(-19)
290 /** Bad memory access. */
291 #define SJME_ERROR_BADADDRESS SJME_JINT_C(-20)
293 /** Invalid CPU operation. */
294 #define SJME_ERROR_INVALIDOP SJME_JINT_C(-21)
296 /** Could not initialize the VMM. */
297 #define SJME_ERROR_VMMNEWFAIL SJME_JINT_C(-22)
300 #define SJME_ERROR_INVALIDSIZE SJME_JINT_C(-23)
302 /** Address resolution error. */
303 #define SJME_ERROR_ADDRRESFAIL SJME_JINT_C(-24)
305 /** Invalid memory type. */
306 #define SJME_ERROR_INVALIDMEMTYPE SJME_JINT_C(-25)
308 /** Register value overflowed. */
309 #define SJME_ERROR_REGISTEROVERFLOW SJME_JINT_C(-26)
311 /** Could not map address. */
312 #define SJME_ERROR_VMMMAPFAIL SJME_JINT_C(-27)
314 /** VMM Type: Byte. */
315 #define SJME_VMMTYPE_BYTE SJME_JINT_C(-1)
317 /** VMM Type: Short. */
318 #define SJME_VMMTYPE_SHORT SJME_JINT_C(-2)
320 /** VMM Type: Integer. */
321 #define SJME_VMMTYPE_INTEGER SJME_JINT_C(-3)
323 /** VMM Type: Java Short. */
324 #define SJME_VMMTYPE_JAVASHORT SJME_JINT_C(-4)
326 /** VMM Type: Java Integer. */
327 #define SJME_VMMTYPE_JAVAINTEGER SJME_JINT_C(-5)
329 /** Pixel format: Integer RGB888. */
330 #define SJME_PIXELFORMAT_INTEGER_RGB888 SJME_JINT_C(0)
332 /** Pixel format: Byte Indexed. */
333 #define SJME_PIXELFORMAT_BYTE_INDEXED SJME_JINT_C(1)
335 /** Pixel format: Short RGB565. */
336 #define SJME_PIXELFORMAT_SHORT_RGB565 SJME_JINT_C(2)
338 /** Pixel format: Packed 1-bit. */
339 #define SJME_PIXELFORMAT_PACKED_ONE SJME_JINT_C(3)
341 /** Pixel format: Packed 2-bit. */
342 #define SJME_PIXELFORMAT_PACKED_TWO SJME_JINT_C(4)
344 /** Pixel format: Packed 4-bit. */
345 #define SJME_PIXELFORMAT_PACKED_FOUR SJME_JINT_C(5)
347 /** This represents an error. */
348 typedef struct sjme_error
353 /** The value of it. */
358 * Virtual memory information.
362 typedef struct sjme_vmem sjme_vmem
;
365 * Virtual memory pointer.
369 typedef sjme_jint sjme_vmemptr
;
372 * Virtual memory mapping.
376 typedef struct sjme_vmemmap
378 /** The real memory pointer. */
381 /** The fake memory pointer. */
384 /** The memory region size. */
387 /** Banked access function. */
388 uintptr_t (*bank
)(sjme_jint
* offset
);
392 * Java virtual machine arguments.
396 typedef struct sjme_jvmargs
398 /** The format of the arguments. */
401 /** Arguments that can be used. */
407 /** Argument count. */
417 * Options used to initialize the virtual machine.
421 typedef struct sjme_jvmoptions
423 /** The amount of RAM to allocate, 0 is default. */
426 /** Preset ROM pointer, does not need loading? */
429 /** Preset ROM size. */
432 /** If non-zero then the ROM needs to be copied (address unsafe). */
435 /** Command line arguments sent to the VM. */
440 * SQF Font information.
444 typedef struct sjme_sqf
446 /** The pixel height of the font. */
447 sjme_jint pixelheight
;
449 /** The ascent of the font. */
452 /** The descent of the font. */
455 /** The bytes per scanline. */
456 sjme_jint bytesperscan
;
458 /** Widths for each character. */
459 sjme_jbyte
* charwidths
;
461 /** Which characters are valid? */
462 sjme_jbyte
* isvalidchar
;
464 /** Which characters make up the bitmap? */
469 * Represents the framebuffer for SquirrelJME.
473 typedef struct sjme_framebuffer
481 /** Screen height. */
484 /** Scanline length. */
487 /** Scanline length in bytes. */
488 sjme_jint scanlenbytes
;
490 /** The number of bits per pixel. */
491 sjme_jint bitsperpixel
;
493 /** The number of available pixels. */
496 /** The pixel format used. */
499 /** Flush the framebuffer. */
504 * This represents the name of a file in native form, system dependent.
508 typedef struct sjme_nativefilename sjme_nativefilename
;
511 * Represents an open file.
515 typedef struct sjme_nativefile sjme_nativefile
;
518 * Instance of the JVM.
522 typedef struct sjme_jvm sjme_jvm
;
525 * Native functions available for the JVM to use.
529 typedef struct sjme_nativefuncs
531 /** Current monotonic nano-seconds, returns low nanos. */
532 sjme_jint (*nanotime
)(sjme_jint
* hi
);
534 /** Current system clock in Java time, returns low time. */
535 sjme_jint (*millitime
)(sjme_jint
* hi
);
537 /** The filename to use for the native ROM. */
538 sjme_nativefilename
* (*nativeromfile
)(void);
540 /** Converts the Java char sequence to native filename. */
541 sjme_nativefilename
* (*nativefilename
)(sjme_jint len
, sjme_jchar
* chars
);
543 /** Frees the specified filename. */
544 void (*freefilename
)(sjme_nativefilename
* filename
);
546 /** Opens the specified file. */
547 sjme_nativefile
* (*fileopen
)(sjme_nativefilename
* filename
,
548 sjme_jint mode
, sjme_error
* error
);
550 /** Closes the specified file. */
551 void (*fileclose
)(sjme_nativefile
* file
, sjme_error
* error
);
553 /** Returns the size of the file. */
554 sjme_jint (*filesize
)(sjme_nativefile
* file
, sjme_error
* error
);
556 /** Reads part of a file. */
557 sjme_jint (*fileread
)(sjme_nativefile
* file
, void* dest
, sjme_jint len
,
560 /** Writes single byte to standard output. */
561 sjme_jint (*stdout_write
)(sjme_jint b
);
563 /** Writes single byte to standard error. */
564 sjme_jint (*stderr_write
)(sjme_jint b
);
566 /** Obtains the framebuffer. */
567 sjme_framebuffer
* (*framebuffer
)(void);
569 /** Returns information on where to load optional JAR from. */
570 sjme_jint (*optional_jar
)(void** ptr
, sjme_jint
* size
);
574 * Allocates the given number of bytes.
576 * @param size The number of bytes to allocate.
579 void* sjme_malloc(sjme_jint size
);
582 * Frees the given pointer.
584 * @param p The pointer to free.
587 void sjme_free(void* p
);
590 * Sets the error code.
592 * @param error The error to set.
593 * @param code The error code.
594 * @param value The error value.
597 void sjme_seterror(sjme_error
* error
, sjme_jint code
, sjme_jint value
);
600 * Executes code running within the JVM.
602 * @param jvm The JVM to execute.
603 * @param error JVM execution error.
604 * @param cycles The number of cycles to execute for.
605 * @return Non-zero if the JVM is resuming, otherwise zero on its exit.
608 sjme_jint
sjme_jvmexec(sjme_jvm
* jvm
, sjme_error
* error
, sjme_jint cycles
);
611 * Destroys the virtual machine instance.
613 * @param jvm The JVM to destroy.
614 * @param error The error state.
615 * @return Non-zero if successful.
618 sjme_jint
sjme_jvmdestroy(sjme_jvm
* jvm
, sjme_error
* error
);
621 * Creates a new instance of a SquirrelJME JVM.
623 * @param args Arguments to the JVM.
624 * @param options Options used to initialize the JVM.
625 * @param nativefuncs Native functions used in the JVM.
626 * @param error Error flag.
627 * @return The resulting JVM or {@code NULL} if it could not be created.
630 sjme_jvm
* sjme_jvmnew(sjme_jvmoptions
* options
, sjme_nativefuncs
* nativefuncs
,
634 * Creates a new virtual memory manager.
636 * @param error The error state.
637 * @return The virtual memory manager.
640 sjme_vmem
* sjme_vmmnew(sjme_error
* error
);
643 * Virtually maps the given region of memory.
645 * @param vmem The virtual memory to map to.
646 * @param at The address to map to, if possible to map there.
647 * @param ptr The region to map.
648 * @param size The number of bytes to map.
649 * @param error The error state.
650 * @return The memory mapping information, returns {@code NULL} on error.
653 sjme_vmemmap
* sjme_vmmmap(sjme_vmem
* vmem
, sjme_jint at
, void* ptr
,
654 sjme_jint size
, sjme_error
* error
);
657 * Resolves the given memory pointer.
659 * @param vmem The virtual memory.
660 * @param ptr The pointer to resolve.
661 * @param off The offset.
662 * @param error The error.
665 void* sjme_vmmresolve(sjme_vmem
* vmem
, sjme_vmemptr ptr
, sjme_jint off
,
669 * Convert size to Java type.
671 * @param type The input size.
672 * @param error The error state.
673 * @return The resulting type.
676 sjme_jint
sjme_vmmsizetojavatype(sjme_jint size
, sjme_error
* error
);
679 * Convert size to type.
681 * @param type The input size.
682 * @param error The error state.
683 * @return The resulting type.
686 sjme_jint
sjme_vmmsizetotype(sjme_jint size
, sjme_error
* error
);
689 * Reads from virtual memory.
691 * @param vmem Virtual memory.
692 * @param type The type to utilize.
693 * @param ptr The pointer address.
694 * @param off The offset.
695 * @param error The error state.
696 * @return The read value.
699 sjme_jint
sjme_vmmread(sjme_vmem
* vmem
, sjme_jint type
, sjme_vmemptr ptr
,
700 sjme_jint off
, sjme_error
* error
);
703 * Reads from virtual memory.
705 * @param vmem Virtual memory.
706 * @param type The type to utilize.
707 * @param ptr The pointer address, is incremented accordingly.
708 * @param error The error state.
709 * @return The read value.
712 sjme_jint
sjme_vmmreadp(sjme_vmem
* vmem
, sjme_jint type
, sjme_vmemptr
* ptr
,
716 * Write to virtual memory.
718 * @param vmem Virtual memory.
719 * @param type The type to utilize.
720 * @param ptr The pointer address.
721 * @param off The offset.
722 * @param val The value to write.
723 * @param error The error state.
726 void sjme_vmmwrite(sjme_vmem
* vmem
, sjme_jint type
, sjme_vmemptr ptr
,
727 sjme_jint off
, sjme_jint val
, sjme_error
* error
);
730 * Write to virtual memory.
732 * @param vmem Virtual memory.
733 * @param type The type to utilize.
734 * @param ptr The pointer address, is incremented accordingly.
735 * @param val The value to write.
736 * @param error The error state.
739 void sjme_vmmwritep(sjme_vmem
* vmem
, sjme_jint type
, sjme_vmemptr
* ptr
,
740 sjme_jint val
, sjme_error
* error
);
743 * Atomically reads, checks, and then sets the value.
745 * @param vmem Virtual memory.
746 * @param check The check value.
747 * @param set The set value.
748 * @param ptr The pointer address.
749 * @param off The offset.
750 * @param error The error state.
751 * @return The value which was read.
754 sjme_jint
sjme_vmmatomicintcheckgetandset(sjme_vmem
* vmem
, sjme_jint check
,
755 sjme_jint set
, sjme_vmemptr ptr
, sjme_jint off
, sjme_error
* error
);
758 * Atomically increments and integer and then gets its value.
760 * @param vmem Virtual memory.
761 * @param ptr The pointer address.
762 * @param off The offset.
763 * @param add The value to add.
764 * @param error The error state.
765 * @return The value after the addition.
768 sjme_jint
sjme_vmmatomicintaddandget(sjme_vmem
* vmem
,
769 sjme_vmemptr ptr
, sjme_jint off
, sjme_jint add
, sjme_error
* error
);
771 /****************************************************************************/
775 #ifdef SJME_cXRATUFACOATSJMERCHSJMERCH
777 #undef SJME_cXRATUFACOATSJMERCHSJMERCH
778 #undef SJME_CXX_IS_EXTERNED
779 #endif /** #ifdef SJME_cXRATUFACOATSJMERCHSJMERCH */
780 #endif /** #ifdef __cplusplus */
783 #endif /* #ifndef SJME_hGRATUFACOATSJMERCHSJMERCH */