<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://sdcc.sourceforge.net/mediawiki/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://sdcc.sourceforge.net/mediawiki/index.php?title=Using_SDCC_to_develop_Native_Java_functions&amp;feed=atom&amp;action=history</id>
		<title>Using SDCC to develop Native Java functions - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://sdcc.sourceforge.net/mediawiki/index.php?title=Using_SDCC_to_develop_Native_Java_functions&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://sdcc.sourceforge.net/mediawiki/index.php?title=Using_SDCC_to_develop_Native_Java_functions&amp;action=history"/>
		<updated>2013-05-24T05:07:05Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.20.2</generator>

	<entry>
		<id>http://sdcc.sourceforge.net/mediawiki/index.php?title=Using_SDCC_to_develop_Native_Java_functions&amp;diff=52&amp;oldid=prev</id>
		<title>Borutr: Created page with &quot;&lt;pre&gt; Using SDCC to develop Native Java functions -------------------------------------------  Prerequisites --------------  1) Download the latest compiler sources from http:...&quot;</title>
		<link rel="alternate" type="text/html" href="http://sdcc.sourceforge.net/mediawiki/index.php?title=Using_SDCC_to_develop_Native_Java_functions&amp;diff=52&amp;oldid=prev"/>
				<updated>2012-12-04T16:29:47Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;pre&amp;gt; Using SDCC to develop Native Java functions -------------------------------------------  Prerequisites --------------  1) Download the latest compiler sources from http:...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
Using SDCC to develop Native Java functions&lt;br /&gt;
-------------------------------------------&lt;br /&gt;
&lt;br /&gt;
Prerequisites&lt;br /&gt;
--------------&lt;br /&gt;
&lt;br /&gt;
1) Download the latest compiler sources from http://sdcc.sourceforge.net (Subversion download), build &amp;amp; &lt;br /&gt;
   install the compiler.&lt;br /&gt;
2) Download &amp;amp; install tini development kit. SDCC uses 'a390' assembler to generate Native libraries,&lt;br /&gt;
   it is NOT distributed with the compiler. Tested with Version 1.02d.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Small Example&lt;br /&gt;
-------------&lt;br /&gt;
&lt;br /&gt;
Hello.java :-&lt;br /&gt;
&lt;br /&gt;
import com.dalsemi.comm.*;&lt;br /&gt;
import com.dalsemi.system.*;&lt;br /&gt;
&lt;br /&gt;
public class Hello&lt;br /&gt;
{&lt;br /&gt;
	public static native int method1(int i,int j);    &lt;br /&gt;
	static void main(String args[])&lt;br /&gt;
	{	&lt;br /&gt;
		System.out.println(&amp;quot;Hello Started&amp;quot;);&lt;br /&gt;
		try {&lt;br /&gt;
		    System.loadLibrary(&amp;quot;myn.tlib&amp;quot;);&lt;br /&gt;
		    System.out.println(&amp;quot;Load Success&amp;quot;);       &lt;br /&gt;
		    System.out.println(&amp;quot;Native method1 returned &amp;quot; + method1(200,100));	    &lt;br /&gt;
		} &lt;br /&gt;
		catch (Throwable t) {&lt;br /&gt;
		      System.out.println(t);&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myn.c :-&lt;br /&gt;
&lt;br /&gt;
long Native_method1() _JavaNative&lt;br /&gt;
{&lt;br /&gt;
    long l = NatLib_LoadInt(0);&lt;br /&gt;
    long k = NatLib_LoadInt(1);&lt;br /&gt;
    return l-k;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Before you start compiling make sure&lt;br /&gt;
a) 'macro' &amp;amp; 'a390' are in the PATH&lt;br /&gt;
b) The files tini.inc, ds80c390.inc, tinimacro.inc &amp;amp; apiequ.inc are in the SAME directory as&lt;br /&gt;
   the C file.&lt;br /&gt;
&lt;br /&gt;
&amp;gt; javac -bootclasspath &amp;lt;path to&amp;gt;/tiniclasses.jar Hello.java&lt;br /&gt;
&amp;gt; java -cp &amp;lt;path to&amp;gt;/tini.jar TINIConvertor -f Hello.class -o Hello.tini -d &amp;lt;path to&amp;gt;/tini.db&lt;br /&gt;
&amp;gt; sdcc -mTININative myn.c&lt;br /&gt;
&lt;br /&gt;
Load Hello.tini &amp;amp; myn.tlib into the TINI board then&lt;br /&gt;
&lt;br /&gt;
TINI /&amp;gt; java Hello.tini&lt;br /&gt;
Hello Started&lt;br /&gt;
Load Success&lt;br /&gt;
Native method1 returned 100&lt;br /&gt;
&lt;br /&gt;
TINI /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Details you MUST know&lt;br /&gt;
---------------------&lt;br /&gt;
SDCC has a completely different and incompatible parameter passing and register usage&lt;br /&gt;
than the TINI java environment. The Native API has been implemented using the __builtin, &lt;br /&gt;
or intrinsic function support in SDCC . Each of the Native API functions are mapped to an &lt;br /&gt;
SDCC intrinsic function, the code generator for the intrinsic function takes care of mapping &lt;br /&gt;
the registers and parameters to the TINI Java environment's expectations .&lt;br /&gt;
&lt;br /&gt;
The _JavaNative keyword is used to map the return value from a Native function to R4:R0, by &lt;br /&gt;
default SDCC uses DPTR:b:a to return a value.&lt;br /&gt;
&lt;br /&gt;
	Type Mapping&lt;br /&gt;
	------------&lt;br /&gt;
&lt;br /&gt;
	SDCC can support types that are upto 4 bytes (32 bits).&lt;br /&gt;
&lt;br /&gt;
	Java				SDCC&lt;br /&gt;
	----				----&lt;br /&gt;
	char				char&lt;br /&gt;
	short				short/int&lt;br /&gt;
	int				long&lt;br /&gt;
	long				NOT SUPPORTED&lt;br /&gt;
	double				NOT SUPPORTED&lt;br /&gt;
&lt;br /&gt;
	_bpx &amp;amp; _ap&lt;br /&gt;
	---------&lt;br /&gt;
	SDCC requires a 16 bit frame pointer to access local variables (on the stack), and&lt;br /&gt;
	function parameters . In the TININative environment _bpx is mapped to R7_B3:R6_B3 &lt;br /&gt;
	(register R7:R6 in bank3). The compiler also uses AP as a temp register, for the&lt;br /&gt;
	TINI environment this is mapped to R5_B3.&lt;br /&gt;
&lt;br /&gt;
Limitations&lt;br /&gt;
-----------&lt;br /&gt;
&lt;br /&gt;
The TININative environment does not have a linker. Multiplication &amp;amp; Division of 16 &amp;amp; 32 bit&lt;br /&gt;
numbers are implemented in SDCC as library functions this implies that , division &amp;amp; multiplication&lt;br /&gt;
of these numbers are not supported in the TININative environment. The compiler transforms div/mul by&lt;br /&gt;
power of 2 to shifts . For other mul/divs there are two ways around .&lt;br /&gt;
&lt;br /&gt;
a) Copy the library function from the library to your code . The sources can be found in&lt;br /&gt;
   sdcc/device/lib.&lt;br /&gt;
b) Use the --use-accelerator option, with this option the compiler will generate code to use&lt;br /&gt;
   the on-chip arithmetic accelerator for 16 bit multiplication &amp;amp; division &amp;amp; modulus. &lt;br /&gt;
   NOTE The compiler will disable interrupts during this operation to prevent corruption of&lt;br /&gt;
	MA &amp;amp; MB registers. MUL/DIV/MOD of unsigned quantities are more efficient than signed&lt;br /&gt;
	quantities.&lt;br /&gt;
&lt;br /&gt;
API Mappings&lt;br /&gt;
------------&lt;br /&gt;
As mentioned earlier the Native APIs are implemented using compiler intrinsic functions, at this&lt;br /&gt;
time only the following functions have been mapped (more will be mapped in the future). Some&lt;br /&gt;
Native API calls return multiple values, I haven't found a good way to handle this yet, in most&lt;br /&gt;
cases these return a HANDLE and a pointer to ABSOLUTE memory in DPTR, in such cases SDCC will&lt;br /&gt;
return ONLY the HANDLE and ignore the POINTER value. The HANDLE can then be used with MM_Deref to&lt;br /&gt;
obtain the POINTER value again (I hope my assumption is correct here).&lt;br /&gt;
&lt;br /&gt;
SDCC Prototype					   Native API&lt;br /&gt;
--------------					   ----------&lt;br /&gt;
char NatLib_LoadByte (char parmnum);		   NatLib_LoadPrimitive&lt;br /&gt;
int  NatLib_LoadShort(char parmnum);		   NatLib_LoadPrimitive&lt;br /&gt;
long NatLib_LoadInt  (char parmnum);		   NatLib_LoadPrimitive&lt;br /&gt;
char *NatLib_LoadPointer (char parmnum);	   NatLib_LoadPointer&lt;br /&gt;
&lt;br /&gt;
/* in the following cases the compiler will fill in the pointer to LibraryID when required*/&lt;br /&gt;
/* NatLib_Get* return mutiple values return value is HANDLE , pointer is ignored */&lt;br /&gt;
  &lt;br /&gt;
char NatLib_InstallImmutableStateBlock(void *state_block,int handle);&lt;br /&gt;
					           NatLib_InstallImmutableStateBlock&lt;br /&gt;
char NatLib_InstallEphemeralStateBlock(void *state_block,int handle);&lt;br /&gt;
					           NatLib_InstallEphemeralStateBlock&lt;br /&gt;
void NatLib_RemoveImmutableStateBlock ();	   NatLib_RemoveImmutableStateBlock&lt;br /&gt;
void NatLib_RemoveEphemeralStateBlock ();	   NatLib_RemoveEphemeralStateBlock&lt;br /&gt;
int  NatLib_GetImmutableStateBlock();              NatLib_GetImmutableStateBlock&lt;br /&gt;
int  NatLib_GetEphemeralStateBlock();              NatLib_GetEphemeralStateBlock&lt;br /&gt;
&lt;br /&gt;
int MM_XMalloc (long size);			   MM_XMalloc	/* returns HANDLE */&lt;br /&gt;
int MM_Malloc (int size);			   MM_Malloc    /* return HANDLE  */&lt;br /&gt;
int MM_ApplicationMalloc ( int size );		   MM_ApplicationMalloc /* returns HANDLE */&lt;br /&gt;
int MM_Free (int handle);			   MM_Free&lt;br /&gt;
char *MM_Deref (int handle);			   MM_Deref&lt;br /&gt;
char MM_UnrestrictedPersist(int handle);           MM_UnrestrictedPersist&lt;br /&gt;
&lt;br /&gt;
char System_ExecJavaProcess(char *image,int handle-to-processname)&lt;br /&gt;
						   System_ExecJavaProcess&lt;br /&gt;
void System_GetRTCRegisters(char *regsavearea)     System_GetRTCRegisters&lt;br /&gt;
void System_SetRTCRegisters(char *regsavearea)     System_SetRTCRegisters&lt;br /&gt;
void System_ThreadSleep(long timeout)              System_ThreadSleep&lt;br /&gt;
void System_ThreadSleep_ExitCriticalSection(long timeout)              System_ThreadSleep_ExitCriticalSection&lt;br /&gt;
void System_ThreadResume(char threadid,char processid)		       System_ThreadResume&lt;br /&gt;
void System_SaveJavaThreadState()                  System_SaveJavaThreadState&lt;br /&gt;
void System_RestoreJavaThreadState()               System_RestoreJavaThreadState&lt;br /&gt;
void System_ProcessSleep(long timeout)             System_ProcessSleep&lt;br /&gt;
void System_ProcessSleep_ExitCriticalSection(long timeout)             System_ProcessSleep_ExitCriticalSection&lt;br /&gt;
void System_ProcessYield()                         System_ProcessYield&lt;br /&gt;
void System_ProcessSuspend()                       System_ProcessSuspend&lt;br /&gt;
void System_ProcessResume(char processid)          System_ProcessResume&lt;br /&gt;
char System_RegisterPoll((void *)(funcpointer)())  System_RegisterPoll&lt;br /&gt;
char System_RemovePoll((void *)(funcpointer)())    System_RemovePoll&lt;br /&gt;
char System_GetCurrentThreadId()		   System_GetCurrentThreadId&lt;br /&gt;
char System_GetCurrentProcessId()		   System_GetCurrentProcessId&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Some Notes&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
The register convention mapping causes a lot of push &amp;amp; pops to be generated. &lt;br /&gt;
The source for the built in functions can be found in file src/ds390/gen.c.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Borutr</name></author>	</entry>

	</feed>