Check-in [e77018c3ea]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add configuration ROM to RatufaCoat; Correct potential memory leak in RatufaCoat; Implement Java instruction write types in RatufaCoat
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:e77018c3eaf4037377542b8b5d04ff62b7e52e28
User & Date: stephanie.gawroriski 2019-06-14 15:37:53
Context
2019-06-14
18:18
Remove note strings in the bootstrap. check-in: 48adcac4bf user: stephanie.gawroriski tags: trunk
15:37
Add configuration ROM to RatufaCoat; Correct potential memory leak in RatufaCoat; Implement Java instruction write types in RatufaCoat check-in: e77018c3ea user: stephanie.gawroriski tags: trunk
15:01
Write of SummerCoat's config ROM area. check-in: 895f1a5e6b user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ratufacoat/sjmerc.c.

17
18
19
20
21
22
23



24
25
26
27
28
29
30
31
32







































33
34
35
36
37
38
39
...
458
459
460
461
462
463
464






465
466
467
468
469
470
471
...
717
718
719
720
721
722
723
724
725

























































726
727

728
729
730
731
732
733
734
...
739
740
741
742
743
744
745



















746
747
748
749
750
751
752
....
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284

1285














1286
1287
1288
1289
1290
1291
1292
....
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
....
1949
1950
1951
1952
1953
1954
1955

1956
1957
1958
1959
1960
1961
1962





















































































































































1963
1964
1965
1966
1967
1968
1969

1970
1971
1972
1973
1974
1975
1976
1977
1978

1979
1980
1981
1982
1983



1984
1985
1986
1987
1988
1989
1990
....
1997
1998
1999
2000
2001
2002
2003



2004
2005
2006
2007
2008
2009
2010
....
2021
2022
2023
2024
2025
2026
2027


2028
2029
2030
2031
2032
2033
2034
....
2039
2040
2041
2042
2043
2044
2045


2046
2047
2048
2049
2050
2051
2052
....
2073
2074
2075
2076
2077
2078
2079

2080
2081
2082
2083
2084
2085
2086






2087
2088
2089
2090
/** Default RAM size. */
#if defined(SJME_IS_DOS)
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(65536)
#else
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(16777216)
#endif




/** Magic number for ROMs. */
#define SJME_ROM_MAGIC_NUMBER SJME_JINT_C(0x58455223)

/** Magic number for JARs. */
#define SJME_JAR_MAGIC_NUMBER SJME_JINT_C(0x00456570)

/** Check magic for BootRAM. */
#define SJME_BOOTRAM_CHECK_MAGIC SJME_JINT_C(0xFFFFFFFF)








































/** Maximum CPU registers. */
#define SJME_MAX_REGISTERS SJME_JINT_C(64)

/** Maximum system call arguments. */
#define SJME_MAX_SYSCALLARGS SJME_JINT_C(8)

/** The zero register. */
................................................................................
	
	/** ROM. */
	void* rom;
	
	/** The size of ROM. */
	sjme_jint romsize;
	






	/** Preset ROM. */
	void* presetrom;
	
	/* Native functions. */
	sjme_nativefuncs* nativefuncs;
	
	/** Linearly fair CPU execution engine. */
................................................................................
				((rv >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((rv << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((rv << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));
#endif
			return rv;
	}
}

/**

























































 * Read Java value from memory and increment pointer.
 *

 * @param size The size of value to read.
 * @param ptr The pointer to read from.
 * @return The resulting value.
 * @since 2019/06/08
 */
sjme_jint sjme_memjreadp(sjme_jvm* jvm, sjme_jint size, void** ptr)
{
................................................................................
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET(*ptr, size);
	
	/* Return result. */
	return rv;
}




















/**
 * Decodes a variable unsigned int operation argument.
 *
 * @param ptr The pointer to read from.
 * @return The resulting decoded value.
 * @since 2019/06/09
................................................................................
								break;
						}
					}
					
					/* Store value */
					else
					{
						if (error != NULL)
							*error = SJME_ERROR_NOJAVAWRITE;
						

						return cycles;














					}
				}
				break;
				
				/* Atomic decrement and get. */
			case SJME_OP_ATOMIC_INT_DECREMENT_AND_GET:
				{
................................................................................
	/* Bootstrap entry arguments. */
	/* (int __rambase, int __ramsize, int __rombase, int __romsize, */
	/* int __confbase, int __confsize) */
	cpu->r[SJME_ARGBASE_REGISTER + 0] = SJME_POINTER_TO_JINT(ram);
	cpu->r[SJME_ARGBASE_REGISTER + 1] = ramsize;
	cpu->r[SJME_ARGBASE_REGISTER + 2] = SJME_POINTER_TO_JINT(rom);
	cpu->r[SJME_ARGBASE_REGISTER + 3] = jvm->romsize;
	cpu->r[SJME_ARGBASE_REGISTER + 4] = 0;
	cpu->r[SJME_ARGBASE_REGISTER + 5] = 0;
	
	
	/* Address where the BootRAM is read from. */
	rp = SJME_POINTER_OFFSET(bootjar, bootoff);
	
	/* Copy initial base memory bytes, which is pure big endian. */
	byteram = (sjme_jbyte*)ram;
	n = sjme_memjreadp(jvm, 4, &rp);
................................................................................
			/* Free CPU state. */
			sjme_free(oldcpu);
		}
	}
	
	/* Delete major JVM data areas. */
	sjme_free(jvm->ram);

	if (jvm->presetrom == NULL)
		sjme_free(jvm->rom);
	
	/* Destroyed okay. */
	return 1;
}






















































































































































/** Creates a new instance of the JVM. */
sjme_jvm* sjme_jvmnew(sjme_jvmoptions* options, sjme_nativefuncs* nativefuncs,
	sjme_jint* error)
{
	sjme_jvmoptions nulloptions;
	void* ram;
	void* rom;

	sjme_jvm* rv;
	sjme_jint i, l, romsize;
	
	/* We need native functions. */
	if (nativefuncs == NULL)
		return NULL;
	
	/* Allocate VM state. */
	rv = sjme_malloc(sizeof(*rv));

	if (rv == NULL)
	{
		if (error != NULL)
			*error = SJME_ERROR_NOMEMORY;
		



		return NULL;
	}
	
	/* If there were no options specified, just use a null set. */
	if (options == NULL)
	{
		memset(&nulloptions, 0, sizeof(nulloptions));
................................................................................
	
	/* Allocate RAM. */
	ram = sjme_malloc(options->ramsize);
	if (ram == NULL)
	{
		if (error != NULL)
			*error = SJME_ERROR_NOMEMORY;



		
		return NULL;
	}
	
	/* Needed by the VM. */
	rv->ram = ram;
	rv->ramsize = options->ramsize;
................................................................................
		rom = sjme_loadrom(nativefuncs, &romsize, error);
		
		/* Could not load the ROM? */
		if (rom == NULL)
		{
			sjme_free(rv);
			sjme_free(ram);


			return NULL;
		}
	}
	
	/* If we are copying from the preset ROM, duplicate it. */
	if (options->presetrom != NULL && options->copyrom != 0)
	{
................................................................................
		rom = sjme_malloc(options->romsize);
		if (rom == NULL)
		{
			if (error != NULL)
				*error = SJME_ERROR_NOMEMORY;
			
			sjme_free(ram);


			return NULL;
		}
		
		/* Copy large chunks at a time. */
		for (i = 0; i < options->romsize;)
		{
			/* Byte left to move? */
................................................................................
	rv->romsize = romsize;
	
	/* Initialize the BootRAM and boot the CPU. */
	if (sjme_initboot(rom, ram, options->ramsize, rv, error) == 0)
	{
		sjme_free(rv);
		sjme_free(ram);

		
		/* If a pre-set ROM is not being used, make sure it gets cleared. */
		if (options->presetrom)
			sjme_free(rom);
		
		return NULL;
	}






	
	/* The JVM is ready to use. */
	return rv;
}







>
>
>









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>







 









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
<







 







>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>









>
|




>
>
>







 







>
>
>







 







>
>







 







>
>







 







>







>
>
>
>
>
>




17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
...
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
...
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
....
1400
1401
1402
1403
1404
1405
1406
1407

1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
....
1964
1965
1966
1967
1968
1969
1970
1971
1972

1973
1974
1975
1976
1977
1978
1979
....
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
....
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
....
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
....
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
....
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
/** Default RAM size. */
#if defined(SJME_IS_DOS)
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(65536)
#else
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(16777216)
#endif

/** Default size of configuration ROM. */
#define SJME_DEFAULT_CONF_SIZE SJME_JINT_C(65536)

/** Magic number for ROMs. */
#define SJME_ROM_MAGIC_NUMBER SJME_JINT_C(0x58455223)

/** Magic number for JARs. */
#define SJME_JAR_MAGIC_NUMBER SJME_JINT_C(0x00456570)

/** Check magic for BootRAM. */
#define SJME_BOOTRAM_CHECK_MAGIC SJME_JINT_C(0xFFFFFFFF)

/** End of configuration. */
#define SJME_CONFIG_END SJME_JINT_C(0)

/** Java VM Version. */
#define SJME_CONFIG_JAVA_VM_VERSION SJME_JINT_C(1)

/** Java VM Name. */
#define SJME_CONFIG_JAVA_VM_NAME SJME_JINT_C(2)

/** Java VM Vendor. */
#define SJME_CONFIG_JAVA_VM_VENDOR SJME_JINT_C(3)

/** Java VM E-Mail. */
#define SJME_CONFIG_JAVA_VM_EMAIL SJME_JINT_C(4)

/** Java VM URL. */
#define SJME_CONFIG_JAVA_VM_URL SJME_JINT_C(5)

/** The guest depth. */
#define SJME_CONFIG_GUEST_DEPTH SJME_JINT_C(6)

/** Main class. */
#define SJME_CONFIG_MAIN_CLASS SJME_JINT_C(7)

/** Main program arguments. */
#define SJME_CONFIG_MAIN_ARGUMENTS SJME_JINT_C(8)

/** Is this a MIDlet? */
#define SJME_CONFIG_IS_MIDLET SJME_JINT_C(9)

/** Define system propertly. */
#define SJME_CONFIG_DEFINE_PROPERTY SJME_JINT_C(10)

/** Classpath to use. */
#define SJME_CONFIG_CLASS_PATH SJME_JINT_C(11)

/** Number of available options. */
#define SJME_CONFIG_NUM_OPTIONS SJME_JINT_C(12)

/** Maximum CPU registers. */
#define SJME_MAX_REGISTERS SJME_JINT_C(64)

/** Maximum system call arguments. */
#define SJME_MAX_SYSCALLARGS SJME_JINT_C(8)

/** The zero register. */
................................................................................
	
	/** ROM. */
	void* rom;
	
	/** The size of ROM. */
	sjme_jint romsize;
	
	/** Configuration ROM. */
	void* config;
	
	/** Size of of the configuration ROM. */
	sjme_jint configsize;
	
	/** Preset ROM. */
	void* presetrom;
	
	/* Native functions. */
	sjme_nativefuncs* nativefuncs;
	
	/** Linearly fair CPU execution engine. */
................................................................................
				((rv >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((rv << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((rv << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));
#endif
			return rv;
	}
}

/**
 * Writes a Java value to memory.
 *
 * @param jvm JVM pointer.
 * @param size The size of the value to write.
 * @param ptr The pointer to read to.
 * @param off The offset.
 * @param value The value to write.
 * @since 2019/06/14
 */
void sjme_memjwrite(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off,
	sjme_jint value)
{
	/* Get the true pointer. */
	ptr = SJME_POINTER_OFFSET(ptr, off);
	
	/* Check against JVM areas. */
	if (jvm != NULL)
	{
		/* Ignore writes to ROM. */
		if (ptr >= jvm->rom &&
			ptr < SJME_POINTER_OFFSET(jvm->rom, jvm->romsize))
			return;
	}
	
	/* Write value to memory. */
	switch (size)
	{
			/* Byte */
		case 1:
			*((sjme_jbyte*)ptr) = (sjme_jbyte)value;
			break;
		
			/* Short */
		case 2:
#if defined(SJME_LITTLE_ENDIAN)
			value = (value & SJME_JINT_C(0xFFFF0000)) |
				(((value << SJME_JINT_C(8)) & SJME_JINT_C(0xFF00)) |
				((value >> SJME_JINT_C(8)) & SJME_JINT_C(0x00FF)));
#endif
			*((sjme_jshort*)ptr) = (sjme_jshort)value;
			break;
			
			/* Integer */
		case 4:
		default:
#if defined(SJME_LITTLE_ENDIAN)
			value = (((value >> SJME_JINT_C(24)) & SJME_JINT_C(0x000000FF)) |
				((value >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((value << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((value << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));
#endif
			*((sjme_jint*)ptr) = value;
			break;
	}
}

/**
 * Read Java value from memory and increment pointer.
 *
 * @param jvm The JVM.
 * @param size The size of value to read.
 * @param ptr The pointer to read from.
 * @return The resulting value.
 * @since 2019/06/08
 */
sjme_jint sjme_memjreadp(sjme_jvm* jvm, sjme_jint size, void** ptr)
{
................................................................................
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET(*ptr, size);
	
	/* Return result. */
	return rv;
}

/**
 * Write Java value to memory and increment pointer.
 *
 * @param jvm The JVM.
 * @param size The size of value to write.
 * @param ptr The pointer to read from.
 * @param value The value to write.
 * @since 2019/06/14
 */
void sjme_memjwritep(sjme_jvm* jvm, sjme_jint size, void** ptr,
	sjme_jint value)
{
	/* Write pointer value. */
	sjme_memjwrite(jvm, size, *ptr, 0, value);
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET(*ptr, size);
}

/**
 * Decodes a variable unsigned int operation argument.
 *
 * @param ptr The pointer to read from.
 * @return The resulting decoded value.
 * @since 2019/06/09
................................................................................
								break;
						}
					}
					
					/* Store value */
					else
					{
						switch (op & SJME_MEM_DATATYPE_MASK)

						{	
							case SJME_DATATYPE_BYTE:
								sjme_memjwrite(jvm, 1, tempp, ib, r[ic]);
								break;
								
							case SJME_DATATYPE_CHARACTER:
							case SJME_DATATYPE_SHORT:
								sjme_memjwrite(jvm, 2, tempp, ib, r[ic]);
								break;
								
							case SJME_DATATYPE_OBJECT:
							case SJME_DATATYPE_INTEGER:
							case SJME_DATATYPE_FLOAT:
							default:
								sjme_memjwrite(jvm, 4, tempp, ib, r[ic]);
								break;
						}
					}
				}
				break;
				
				/* Atomic decrement and get. */
			case SJME_OP_ATOMIC_INT_DECREMENT_AND_GET:
				{
................................................................................
	/* Bootstrap entry arguments. */
	/* (int __rambase, int __ramsize, int __rombase, int __romsize, */
	/* int __confbase, int __confsize) */
	cpu->r[SJME_ARGBASE_REGISTER + 0] = SJME_POINTER_TO_JINT(ram);
	cpu->r[SJME_ARGBASE_REGISTER + 1] = ramsize;
	cpu->r[SJME_ARGBASE_REGISTER + 2] = SJME_POINTER_TO_JINT(rom);
	cpu->r[SJME_ARGBASE_REGISTER + 3] = jvm->romsize;
	cpu->r[SJME_ARGBASE_REGISTER + 4] = SJME_POINTER_TO_JINT(jvm->config);
	cpu->r[SJME_ARGBASE_REGISTER + 5] = jvm->configsize;

	
	/* Address where the BootRAM is read from. */
	rp = SJME_POINTER_OFFSET(bootjar, bootoff);
	
	/* Copy initial base memory bytes, which is pure big endian. */
	byteram = (sjme_jbyte*)ram;
	n = sjme_memjreadp(jvm, 4, &rp);
................................................................................
			/* Free CPU state. */
			sjme_free(oldcpu);
		}
	}
	
	/* Delete major JVM data areas. */
	sjme_free(jvm->ram);
	sjme_free(jvm->config);
	if (jvm->presetrom == NULL)
		sjme_free(jvm->rom);
	
	/* Destroyed okay. */
	return 1;
}

/** Initializes the configuration space. */
void sjme_configinit(void* conf, sjme_jint confsize, sjme_jvm* jvm,
	sjme_jvmoptions* options, sjme_nativefuncs* nativefuncs, sjme_jint* error)
{
#define SJME_CONFIG_FORMAT_INTEGER SJME_JINT_C(1)
#define SJME_CONFIG_FORMAT_KEYVALUE SJME_JINT_C(2)
#define SJME_CONFIG_FORMAT_STRING SJME_JINT_C(3)
#define SJME_CONFIG_FORMAT_STRINGS SJME_JINT_C(4)
	void* wp;
	void* basep;
	void* sizep;
	sjme_jint opt, format, iv, it;
	char* sa;
	char* sb;
	
	/* Write pointer starts at the base area. */
	wp = conf;
	
	/* Go through all possible options to make a value. */
	for (opt = SJME_JINT_C(1); opt < SJME_CONFIG_NUM_OPTIONS; opt++)
	{
		/* Used to specify the format. */
		format = 0;
		
		/* Reset. */
		sa = NULL;
		sb = NULL;
		iv = 0;
		
		/* Depends on the option. */
		switch (opt)
		{
			/* Java VM Version. */
			case SJME_CONFIG_JAVA_VM_VERSION:
				format = SJME_CONFIG_FORMAT_STRING;
				sa = "0.3.0";
				break;
			
			/* Java VM Name. */
			case SJME_CONFIG_JAVA_VM_NAME:
				format = SJME_CONFIG_FORMAT_STRING;
				sa = "SquirrelJME RatufaCoat";
				break;
			
			/* Java VM Vendor. */
			case SJME_CONFIG_JAVA_VM_VENDOR:
				format = SJME_CONFIG_FORMAT_STRING;
				sa = "Stephanie Gawroriski";
				break;
			
			/* Java VM E-Mail. */
			case SJME_CONFIG_JAVA_VM_EMAIL:
				format = SJME_CONFIG_FORMAT_STRING;
				sa = "xerthesquirrel@gmail.com";
				break;
			
			/* Java VM URL. */
			case SJME_CONFIG_JAVA_VM_URL:
				format = SJME_CONFIG_FORMAT_STRING;
				sa = "https://squirreljme.cc/";
				break;
			
			/* The guest depth. */
			case SJME_CONFIG_GUEST_DEPTH:
				break;
			
			/* Main class. */
			case SJME_CONFIG_MAIN_CLASS:
				break;
			
			/* Main program arguments. */
			case SJME_CONFIG_MAIN_ARGUMENTS:
				break;
			
			/* Is this a MIDlet? */
			case SJME_CONFIG_IS_MIDLET:
				break;
			
			/* Define system propertly. */
			case SJME_CONFIG_DEFINE_PROPERTY:
				break;
			
			/* Classpath to use. */
			case SJME_CONFIG_CLASS_PATH:
				break;
			
				/* Unknown, ignore. */
			default:
				continue;
		}
		
		/* No known way to write this? */
		if (format == 0)
			continue;
		
		/* Write option key. */
		sjme_memjwritep(jvm, 2, &wp, opt);
		
		/* Store size location for later write. */
		sizep = wp;
		sjme_memjwritep(jvm, 2, &wp, 0);
		
		/* Base write pointer. */
		basep = wp;
		
		/* Depends on the format. */
		switch (format)
		{
				/* Integer. */
			case SJME_CONFIG_FORMAT_INTEGER:
				sjme_memjwritep(jvm, 4, &wp, iv);
				break;
			
				/* Key/value pair. */
			case SJME_CONFIG_FORMAT_KEYVALUE:
				break;
			
				/* String value. */
			case SJME_CONFIG_FORMAT_STRING:
				{
					// Record string length
					int iv = strlen(sa);
					sjme_memjwritep(jvm, 2, &wp, iv);
					
					// Record characters
					for (it = 0; it < iv; it++)
						sjme_memjwritep(jvm, 1, &wp, (sjme_jint)sa[it]);
				}
				break;
			
				/* Multiple strings. */
			case SJME_CONFIG_FORMAT_STRINGS:
				break;
		}
		
		/* Write to the actual size! */
		sjme_memjwrite(jvm, 2, sizep, 0,
			SJME_POINTER_TO_JINT(wp) - SJME_POINTER_TO_JINT(basep));
	}
	
	/* Write end of config. */
	sjme_memjwritep(jvm, 2, &wp, SJME_CONFIG_END);
	
#undef SJME_CONFIG_FORMAT_INTEGER
#undef SJME_CONFIG_FORMAT_KEYVALUE
#undef SJME_CONFIG_FORMAT_STRING
#undef SJME_CONFIG_FORMAT_STRINGS
}

/** Creates a new instance of the JVM. */
sjme_jvm* sjme_jvmnew(sjme_jvmoptions* options, sjme_nativefuncs* nativefuncs,
	sjme_jint* error)
{
	sjme_jvmoptions nulloptions;
	void* ram;
	void* rom;
	void* conf;
	sjme_jvm* rv;
	sjme_jint i, l, romsize;
	
	/* We need native functions. */
	if (nativefuncs == NULL)
		return NULL;
	
	/* Allocate VM state. */
	rv = sjme_malloc(sizeof(*rv));
	conf = sjme_malloc(SJME_DEFAULT_CONF_SIZE);
	if (rv == NULL || conf == NULL)
	{
		if (error != NULL)
			*error = SJME_ERROR_NOMEMORY;
		
		sjme_free(rv);
		sjme_free(conf);
		
		return NULL;
	}
	
	/* If there were no options specified, just use a null set. */
	if (options == NULL)
	{
		memset(&nulloptions, 0, sizeof(nulloptions));
................................................................................
	
	/* Allocate RAM. */
	ram = sjme_malloc(options->ramsize);
	if (ram == NULL)
	{
		if (error != NULL)
			*error = SJME_ERROR_NOMEMORY;
			
		sjme_free(rv);
		sjme_free(conf);
		
		return NULL;
	}
	
	/* Needed by the VM. */
	rv->ram = ram;
	rv->ramsize = options->ramsize;
................................................................................
		rom = sjme_loadrom(nativefuncs, &romsize, error);
		
		/* Could not load the ROM? */
		if (rom == NULL)
		{
			sjme_free(rv);
			sjme_free(ram);
			sjme_free(conf);
			
			return NULL;
		}
	}
	
	/* If we are copying from the preset ROM, duplicate it. */
	if (options->presetrom != NULL && options->copyrom != 0)
	{
................................................................................
		rom = sjme_malloc(options->romsize);
		if (rom == NULL)
		{
			if (error != NULL)
				*error = SJME_ERROR_NOMEMORY;
			
			sjme_free(ram);
			sjme_free(conf);
			
			return NULL;
		}
		
		/* Copy large chunks at a time. */
		for (i = 0; i < options->romsize;)
		{
			/* Byte left to move? */
................................................................................
	rv->romsize = romsize;
	
	/* Initialize the BootRAM and boot the CPU. */
	if (sjme_initboot(rom, ram, options->ramsize, rv, error) == 0)
	{
		sjme_free(rv);
		sjme_free(ram);
		sjme_free(conf);
		
		/* If a pre-set ROM is not being used, make sure it gets cleared. */
		if (options->presetrom)
			sjme_free(rom);
		
		return NULL;
	}
	
	/* Initialize configuration space. */
	rv->config = conf;
	rv->configsize = SJME_DEFAULT_CONF_SIZE;
	sjme_configinit(conf, SJME_DEFAULT_CONF_SIZE, rv, options, nativefuncs,
		error);
	
	/* The JVM is ready to use. */
	return rv;
}

Changes to runt/apis/cldc-compact/cc/squirreljme/jvm/ConfigRomType.java.

59
60
61
62
63
64
65
66



67

	/** Define system propertly. */
	public static final short DEFINE_PROPERTY =
		10;
	
	/** Classpath to use. */
	public static final short CLASS_PATH =
		11;
}












|
>
>
>
|
>
59
60
61
62
63
64
65
66
67
68
69
70
71
	/** Define system propertly. */
	public static final short DEFINE_PROPERTY =
		10;
	
	/** Classpath to use. */
	public static final short CLASS_PATH =
		11;
	
	/** Number of available options. */
	public static final short NUM_OPTIONS =
		12;
}

Changes to runt/libs/summercoat-vm/cc/squirreljme/vm/summercoat/ConfigRomWriter.java.

53
54
55
56
57
58
59




























60
61
62
63
64
65
66
..
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
			throw new IOException("AE0e " + len);
		
		// Key, value, and the data
		__dos.writeShort(__opt);
		__dos.writeShort(len);
		__dos.write(__b, 0, len);
	}




























	
	/**
	 * Writes key/value.
	 *
	 * @param __dos The output stream.
	 * @param __opt The option to write.
	 * @param __k The key.
................................................................................
		// Write data area
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream xdos = new DataOutputStream(baos))
		{
			xdos.writeUTF(__k);
			xdos.writeUTF(__v);
			
			// Write in
			ConfigRomWriter.writeData(__dos, __opt, baos.toByteArray());
		}
	}
	
	/**
	 * Writes the specified integer.
	 *
	 * @param __dos The output stream.
	 * @param __opt The option to write.
	 * @param __v The value to write.
	 * @throws IOException On write errors.
	 * @throws NullPointerException On null arguments.
	 * @since 2019/06/14
	 */
	public static final void writeInteger(DataOutputStream __dos, int __opt,
		int __v)
		throws IOException, NullPointerException
	{
		if (__dos == null)
			throw new NullPointerException("NARG");
		
		// Write data area
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream xdos = new DataOutputStream(baos))
		{
			xdos.writeInt(__v);
			
			// Write in
			ConfigRomWriter.writeData(__dos, __opt, baos.toByteArray());
		}
	}
	
	/**
	 * Writes the specified string.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
...
107
108
109
110
111
112
113




























114
115
116
117
118
119
120
			throw new IOException("AE0e " + len);
		
		// Key, value, and the data
		__dos.writeShort(__opt);
		__dos.writeShort(len);
		__dos.write(__b, 0, len);
	}
	
	/**
	 * Writes the specified integer.
	 *
	 * @param __dos The output stream.
	 * @param __opt The option to write.
	 * @param __v The value to write.
	 * @throws IOException On write errors.
	 * @throws NullPointerException On null arguments.
	 * @since 2019/06/14
	 */
	public static final void writeInteger(DataOutputStream __dos, int __opt,
		int __v)
		throws IOException, NullPointerException
	{
		if (__dos == null)
			throw new NullPointerException("NARG");
		
		// Write data area
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream xdos = new DataOutputStream(baos))
		{
			xdos.writeInt(__v);
			
			// Write in
			ConfigRomWriter.writeData(__dos, __opt, baos.toByteArray());
		}
	}
	
	/**
	 * Writes key/value.
	 *
	 * @param __dos The output stream.
	 * @param __opt The option to write.
	 * @param __k The key.
................................................................................
		// Write data area
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DataOutputStream xdos = new DataOutputStream(baos))
		{
			xdos.writeUTF(__k);
			xdos.writeUTF(__v);
			




























			// Write in
			ConfigRomWriter.writeData(__dos, __opt, baos.toByteArray());
		}
	}
	
	/**
	 * Writes the specified string.

Changes to runt/libs/summercoat-vm/cc/squirreljme/vm/summercoat/SummerCoatFactory.java.

214
215
216
217
218
219
220



221
222
223
224
225
226
227
						ConfigRomType.DEFINE_PROPERTY,
						e.getKey(), e.getValue());
				}
			
			// Class path
			ConfigRomWriter.writeStrings(dos, ConfigRomType.CLASS_PATH,
				SummerCoatFactory.classPathToStringArray(__cp));



		}
		
		// {@squirreljme.error AE0d Could not write to configuration ROM.}
		catch (IOException e)
		{
			throw new VMException("AE0d", e);
		}







>
>
>







214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
						ConfigRomType.DEFINE_PROPERTY,
						e.getKey(), e.getValue());
				}
			
			// Class path
			ConfigRomWriter.writeStrings(dos, ConfigRomType.CLASS_PATH,
				SummerCoatFactory.classPathToStringArray(__cp));
			
			// End
			dos.writeShort(ConfigRomType.END);
		}
		
		// {@squirreljme.error AE0d Could not write to configuration ROM.}
		catch (IOException e)
		{
			throw new VMException("AE0d", e);
		}