<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <!--Converted with LaTeX2HTML 2008 (1.71) original version by: Nikos Drakos, CBLU, University of Leeds * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan * with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --> <HTML> <HEAD> <TITLE>3.12.1 MCS51/DS390 Startup Code</TITLE> <META NAME="description" CONTENT="3.12.1 MCS51/DS390 Startup Code"> <META NAME="keywords" CONTENT="sdccman"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <META NAME="Generator" CONTENT="LaTeX2HTML v2008"> <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> <LINK REL="STYLESHEET" HREF="sdccman.css"> <LINK REL="next" HREF="node79.html"> <LINK REL="previous" HREF="node77.html"> <LINK REL="up" HREF="node77.html"> <LINK REL="next" HREF="node79.html"> </HEAD> <BODY > <!--Navigation Panel--> <A NAME="tex2html1850" HREF="node79.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1844" HREF="node77.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1838" HREF="node77.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1846" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1848" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1851" HREF="node79.html">3.12.2 HC08 Startup Code</A> <B> Up:</B> <A NAME="tex2html1845" HREF="node77.html">3.12 Startup Code</A> <B> Previous:</B> <A NAME="tex2html1839" HREF="node77.html">3.12 Startup Code</A> <B> <A NAME="tex2html1847" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1849" HREF="node191.html">Index</A></B> <BR> <BR> <!--End of Navigation Panel--> <H2><A NAME="SECTION004121000000000000000"> 3.12.1 MCS51/DS390 Startup Code</A> </H2> <P> The compiler triggers the linker to link certain initialization modules from the runtime library<A NAME="1844"></A> called crt<something>. Only the necessary ones are linked, for instance crtxstack.asm (GSINIT1, GSINIT5) is not linked unless the --xstack option is used. These modules are highly entangled by the use of special segments/areas, but a common layout is shown below: <BLOCKQUOTE> <TT><B><FONT SIZE="-1">(main.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area HOME (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__interrupt_vect:</FONT></TT> <BR><TT><FONT SIZE="-1"> ljmp __sdcc_gsinit_startup</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtstart.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT0 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__sdcc_gsinit_startup::</FONT></TT> <BR><TT><FONT SIZE="-1"> mov sp,#__start__stack - 1</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtxstack.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT1 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__sdcc_init_xstack::</FONT></TT> <BR><TT><FONT SIZE="-1">; Need to initialize in GSINIT1 in case the user's __sdcc_external_startup uses the xstack.</FONT></TT> <BR><TT><FONT SIZE="-1"> mov __XPAGE,#(__start__xstack >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> mov _spx,#__start__xstack</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtstart.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT2 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1"> lcall __sdcc_external_startup</FONT></TT> <BR><TT><FONT SIZE="-1"> mov a,dpl</FONT></TT> <BR><TT><FONT SIZE="-1"> jz __sdcc_init_data</FONT></TT> <BR><TT><FONT SIZE="-1"> ljmp __sdcc_program_startup</FONT></TT> <BR><TT><FONT SIZE="-1">__sdcc_init_data:</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtxinit.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT3 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__mcs51_genXINIT::</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r1,#l_XINIT</FONT></TT> <BR><TT><FONT SIZE="-1"> mov a,r1</FONT></TT> <BR><TT><FONT SIZE="-1"> orl a,#(l_XINIT >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> jz 00003$</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r2,#((l_XINIT+255) >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> mov dptr,#s_XINIT</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r0,#s_XISEG</FONT></TT> <BR><TT><FONT SIZE="-1"> mov __XPAGE,#(s_XISEG >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1">00001$: clr a</FONT></TT> <BR><TT><FONT SIZE="-1"> movc a,@a+dptr</FONT></TT> <BR><TT><FONT SIZE="-1"> movx @r0,a</FONT></TT> <BR><TT><FONT SIZE="-1"> inc dptr</FONT></TT> <BR><TT><FONT SIZE="-1"> inc r0</FONT></TT> <BR><TT><FONT SIZE="-1"> cjne r0,#0,00002$</FONT></TT> <BR><TT><FONT SIZE="-1"> inc __XPAGE</FONT></TT> <BR><TT><FONT SIZE="-1">00002$: djnz r1,00001$</FONT></TT> <BR><TT><FONT SIZE="-1"> djnz r2,00001$</FONT></TT> <BR><TT><FONT SIZE="-1"> mov __XPAGE,#0xFF</FONT></TT> <BR><TT><FONT SIZE="-1">00003$:</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtclear.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT4 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__mcs51_genRAMCLEAR::</FONT></TT> <BR><TT><FONT SIZE="-1"> clr a</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r0,#(l_IRAM-1)</FONT></TT> <BR><TT><FONT SIZE="-1">00004$: mov @r0,a</FONT></TT> <BR><TT><FONT SIZE="-1"> djnz r0,00004$</FONT></TT> <BR><TT><FONT SIZE="-1">; _mcs51_genRAMCLEAR() end</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtxclear.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT4 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__mcs51_genXRAMCLEAR::</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r0,#l_PSEG</FONT></TT> <BR><TT><FONT SIZE="-1"> mov a,r0</FONT></TT> <BR><TT><FONT SIZE="-1"> orl a,#(l_PSEG >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> jz 00006$</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r1,#s_PSEG</FONT></TT> <BR><TT><FONT SIZE="-1"> mov __XPAGE,#(s_PSEG >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> clr a</FONT></TT> <BR><TT><FONT SIZE="-1">00005$: movx @r1,a</FONT></TT> <BR><TT><FONT SIZE="-1"> inc r1</FONT></TT> <BR><TT><FONT SIZE="-1"> djnz r0,00005$</FONT></TT> <BR><TT><FONT SIZE="-1">00006$:</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r0,#l_XSEG</FONT></TT> <BR><TT><FONT SIZE="-1"> mov a,r0</FONT></TT> <BR><TT><FONT SIZE="-1"> orl a,#(l_XSEG >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> jz 00008$</FONT></TT> <BR><TT><FONT SIZE="-1"> mov r1,#((l_XSEG + 255) >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> mov dptr,#s_XSEG</FONT></TT> <BR><TT><FONT SIZE="-1"> clr a</FONT></TT> <BR><TT><FONT SIZE="-1">00007$: movx @dptr,a</FONT></TT> <BR><TT><FONT SIZE="-1"> inc dptr</FONT></TT> <BR><TT><FONT SIZE="-1"> djnz r0,00007$</FONT></TT> <BR><TT><FONT SIZE="-1"> djnz r1,00007$</FONT></TT> <BR><TT><FONT SIZE="-1">00008$:</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(crtxstack.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT5 (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">; Need to initialize in GSINIT5 because __mcs51_genXINIT modifies __XPAGE</FONT></TT> <BR><TT><FONT SIZE="-1">; and __mcs51_genRAMCLEAR modifies _spx.</FONT></TT> <BR><TT><FONT SIZE="-1"> mov __XPAGE,#(__start__xstack >> 8)</FONT></TT> <BR><TT><FONT SIZE="-1"> mov _spx,#__start__xstack</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(application modules)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSINIT (CODE)</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><B><FONT SIZE="-1">(main.asm)</FONT></B></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> <P> <BLOCKQUOTE><TT><FONT SIZE="-1"> .area GSFINAL (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1"> ljmp __sdcc_program_startup</FONT></TT> <BR><TT><FONT SIZE="-1">;----------------------------</FONT></TT> <BR><TT><FONT SIZE="-1">; Home</FONT></TT> <BR><TT><FONT SIZE="-1">;----------------------------</FONT></TT> <BR><TT><FONT SIZE="-1"> .area HOME (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1"> .area CSEG (CODE)</FONT></TT> <BR><TT><FONT SIZE="-1">__sdcc_program_startup:</FONT></TT> <BR><TT><FONT SIZE="-1"> lcall _main</FONT></TT> <BR><TT><FONT SIZE="-1">; return from main will lock up</FONT></TT> <BR><TT><FONT SIZE="-1"> sjmp .</FONT></TT></BLOCKQUOTE> <P> <BLOCKQUOTE> </BLOCKQUOTE> One of these modules (crtstart.asm) contains a call to the C routine <I>_sdcc_external_startup()<A NAME="2073"></A></I> at the start of the CODE area. This routine is also in the runtime library<A NAME="2074"></A> and returns 0 by default. If this routine returns a non-zero value, the static & global variable initialization will be skipped and the function main will be invoked. Otherwise static & global variables will be initialized before the function main is invoked. You could add an <I>_sdcc_external_startup()</I> routine to your program to override the default if you need to setup hardware or perform some other critical operation prior to static & global variable initialization<A NAME="2076"></A>. On some mcs51 variants xdata<A NAME="2077"></A> memory has to be explicitly enabled before it can be accessed or if the watchdog<A NAME="2078"></A> needs to be disabled, this is the place to do it. The startup code clears all internal data memory, 256 bytes by default, but from 0 to n-1 if <I>--iram-size<A NAME="2079"></A>n</I> is used. (recommended for Chipcon CC1010). <P> See also the compiler options <I>--no-xinit</I>-<I>opt</I><A NAME="2082"></A>, <I>--main-return</I><A NAME="2084"></A> and section <A HREF="node103.html#sub:MCS51-variants">4.1</A> about MCS51-variants. <BR> <P> While these initialization modules are meant as generic startup code there might be the need for customization. Let's assume the return value of <I>_sdcc_external_startup()</I> in <I>crtstart.asm</I> should not be checked (or <I>_sdcc_external_startup()</I> should not be called at all). The recommended way would be to copy <I>crtstart.asm</I> (f.e. from <TT><A NAME="tex2html34" HREF="http://sdcc.svn.sourceforge.net/viewvc/*checkout*/sdcc/trunk/sdcc/device/lib/mcs51/crtstart.asm">http://sdcc.svn.sourceforge.net/viewvc/*checkout*/sdcc/trunk/sdcc/device/lib/mcs51/crtstart.asm</A></TT>) into the source directory, adapt it there, then assemble it with <I>sdas8051 -plosgff</I><A NAME="tex2html35" HREF="footnode.html#foot5828"><SUP>3.5</SUP></A><I> crtstart.asm</I> and when linking your project explicitly specify <I>crtstart.rel</I>. As a bonus a listing of the relocated object file <I>crtstart.rst</I> is generated. <P> <P><P> <BR> <P> <HR> <!--Navigation Panel--> <A NAME="tex2html1850" HREF="node79.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1844" HREF="node77.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1838" HREF="node77.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1846" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1848" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1851" HREF="node79.html">3.12.2 HC08 Startup Code</A> <B> Up:</B> <A NAME="tex2html1845" HREF="node77.html">3.12 Startup Code</A> <B> Previous:</B> <A NAME="tex2html1839" HREF="node77.html">3.12 Startup Code</A> <B> <A NAME="tex2html1847" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1849" HREF="node191.html">Index</A></B> <!--End of Navigation Panel--> <ADDRESS> 2011-03-20 </ADDRESS> </BODY> </HTML>