1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
5 \pdfoptionpdfminorversion=3
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={68hc08 8032 8051 ansi c compiler CPU DS390 embedded free Freescale GPL HC08 Intel manual Maxim mcs51 Microchip microcontroller open source PIC Z80 Zilog},
11 pdfpagemode=UseOutlines,
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by Subversion
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
136 designed for 8 bit Microprocessors.
137 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
139 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
143 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
144 Zilog Z80 based MCUs.
145 It can be retargetted for other microprocessors, support for Microchip
146 PIC, Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargettable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
209 <lyxtabular version="3" rows="8" columns="5">
211 <column alignment="center" valignment="top" leftline="true" width="0">
212 <column alignment="center" valignment="top" leftline="true" width="0">
213 <column alignment="center" valignment="top" leftline="true" width="0">
214 <column alignment="center" valignment="top" leftline="true" width="0">
215 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
216 <row topline="true" bottomline="true">
217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
401 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
426 <row topline="true" bottomline="true">
427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
456 -2.147.483.648, +2.147.483.647
459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
468 <row topline="true" bottomline="true">
469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
508 <row topline="true" bottomline="true">
509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
554 The compiler also allows
556 inline assembler code
558 to be embedded anywhere in a function.
559 In addition, routines developed in assembly can also be called.
563 SDCC also provides an option (-
573 -cyclomatic) to report the relative complexity of a function.
574 These functions can then be further optimized, or hand coded in assembly
580 SDCC also comes with a companion source level debugger SDCDB, the debugger
581 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
582 SDCDB and ucSim are currently not available on Win32 platforms.
587 The latest version can be downloaded from
588 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
598 Please note: the compiler will probably always be some steps ahead of this
603 \begin_inset LatexCommand \index{Status of documentation}
613 Obviously this has pros and cons
622 All packages used in this compiler system are
630 ; source code for all the sub-packages (pre-processor, assemblers, linkers
631 etc) is distributed with the package.
632 This documentation is maintained using a freeware word processor (LyX).
634 This program is free software; you can redistribute it and/or modify it
635 under the terms of the GNU General Public License
636 \begin_inset LatexCommand \index{GNU General Public License, GPL}
640 as published by the Free Software Foundation; either version 2, or (at
641 your option) any later version.
642 This program is distributed in the hope that it will be useful, but WITHOUT
643 ANY WARRANTY; without even the implied warranty
644 \begin_inset LatexCommand \index{warranty}
648 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
649 See the GNU General Public License for more details.
650 You should have received a copy of the GNU General Public License along
651 with this program; if not, write to the Free Software Foundation, 59 Temple
652 Place - Suite 330, Boston, MA 02111-1307, USA.
653 In other words, you are welcome to use, share and improve this program.
654 You are forbidden to forbid anyone else to use, share and improve what
656 Help stamp out software-hoarding!
659 Typographic conventions
660 \begin_inset LatexCommand \index{Typographic conventions}
667 Throughout this manual, we will use the following convention.
668 Commands you have to type in are printed in
676 Code samples are printed in
681 Interesting items and new terms are printed in
687 \begin_inset LatexCommand \label{sec:Compatibility-with-previous}
691 with previous versions
692 \begin_inset LatexCommand \index{Compatibility with previous versions}
699 This version has numerous bug fixes compared with the previous version.
700 But we also introduced some incompatibilities with older versions.
701 Not just for the fun of it, but to make the compiler more stable, efficient
703 \begin_inset LatexCommand \index{ANSI-compliance}
708 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
712 for ANSI-Compliance).
718 short is now equivalent to int (16 bits), it used to be equivalent to char
719 (8 bits) which is not ANSI compliant.
722 the default directory for gcc-builds where include, library and documentation
723 files are stored is now in /usr/local/share.
726 char type parameters to vararg functions are casted to int unless explicitly
743 will push a as an int and as a char resp.
756 -regextend has been removed.
769 -noregparms has been removed.
782 -stack-after-data has been removed.
786 \begin_inset LatexCommand \index{bit}
791 \begin_inset LatexCommand \index{sbit}
796 \begin_inset LatexCommand \index{\_\_sbit}
800 types now consistently behave like the C99 _Bool type with respect to type
802 \begin_inset LatexCommand \index{type conversion}
807 \begin_inset LatexCommand \index{type promotion}
812 The most common incompatibility resulting from this change is related to
814 \begin_inset LatexCommand \index{Bit toggling}
828 b = ~b; /* equivalent to b=1 instead of toggling b */
829 \begin_inset Marginal
844 b = !b; /* toggles b */
848 In previous versions, both forms would have toggled the bit.
853 <pending: more incompatibilities?>
859 What do you need before you start installation of SDCC? A computer, and
861 The preferred method of installation is to compile SDCC from source using
863 For Windows some pre-compiled binary distributions are available for your
865 You should have some experience with command line tools and compiler use.
871 The SDCC home page at
872 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
876 is a great place to find distribution sets.
877 You can also find links to the user mailing lists that offer help or discuss
878 SDCC with other SDCC users.
879 Web links to other SDCC related sites can also be found here.
880 This document can be found in the DOC directory of the source package as
882 A pdf version of this document is available at
883 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
888 Some of the other tools (simulator and assembler) included with SDCC contain
889 their own documentation and can be found in the source distribution.
890 If you want the latest unreleased software, the complete source package
891 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
895 Wishes for the future
898 There are (and always will be) some things that could be done.
899 Here are some I can think of:
906 char KernelFunction3(char p) at 0x340;
914 \begin_inset LatexCommand \index{code banking (limited support)}
924 If you can think of some more, please see the section
925 \begin_inset LatexCommand \ref{sub:Requesting-Features}
929 about filing feature requests
930 \begin_inset LatexCommand \index{Requesting features}
935 \begin_inset LatexCommand \index{Feature request}
945 \begin_inset LatexCommand \index{Installation}
952 For most users it is sufficient to skip to either section
953 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
958 \begin_inset LatexCommand \ref{sub:Windows-Install}
963 More detailled instructions follow below.
967 \begin_inset LatexCommand \index{Options SDCC configuration}
974 The install paths, search paths and other options are defined when running
976 The defaults can be overridden by:
978 \labelwidthstring 00.00.0000
990 -prefix see table below
992 \labelwidthstring 00.00.0000
1004 -exec_prefix see table below
1006 \labelwidthstring 00.00.0000
1018 -bindir see table below
1020 \labelwidthstring 00.00.0000
1032 -datadir see table below
1034 \labelwidthstring 00.00.0000
1036 docdir environment variable, see table below
1038 \labelwidthstring 00.00.0000
1040 include_dir_suffix environment variable, see table below
1042 \labelwidthstring 00.00.0000
1044 lib_dir_suffix environment variable, see table below
1046 \labelwidthstring 00.00.0000
1048 sdccconf_h_dir_separator environment variable, either / or
1053 This character will only be used in sdccconf.h; don't forget it's a C-header,
1054 therefore a double-backslash is needed there.
1056 \labelwidthstring 00.00.0000
1068 -disable-mcs51-port Excludes the Intel mcs51 port
1070 \labelwidthstring 00.00.0000
1082 -disable-gbz80-port Excludes the Gameboy gbz80 port
1084 \labelwidthstring 00.00.0000
1096 -disable-z80-port Excludes the z80 port
1098 \labelwidthstring 00.00.0000
1110 -disable-avr-port Excludes the AVR port
1112 \labelwidthstring 00.00.0000
1124 -disable-ds390-port Excludes the DS390 port
1126 \labelwidthstring 00.00.0000
1138 -disable-hc08-port Excludes the HC08 port
1140 \labelwidthstring 00.00.0000
1152 -disable-pic-port Excludes the PIC port
1154 \labelwidthstring 00.00.0000
1166 -disable-xa51-port Excludes the XA51 port
1168 \labelwidthstring 00.00.0000
1180 -disable-ucsim Disables configuring and building of ucsim
1182 \labelwidthstring 00.00.0000
1194 -disable-device-lib Disables automatically building device libraries
1196 \labelwidthstring 00.00.0000
1208 -disable-packihx Disables building packihx
1210 \labelwidthstring 00.00.0000
1222 -enable-doc Build pdf, html and txt files from the lyx sources
1224 \labelwidthstring 00.00.0000
1236 -enable-libgc Use the Bohem memory allocator.
1237 Lower runtime footprint.
1240 Furthermore the environment variables CC, CFLAGS, ...
1241 the tools and their arguments can be influenced.
1242 Please see `configure -
1252 -help` and the man/info pages of `configure` for details.
1256 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
1257 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
1258 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
1259 At the moment it's not possible to change the default settings (it was
1260 simply never required).
1264 These configure options are compiled into the binaries, and can only be
1265 changed by rerunning 'configure' and recompiling SDCC.
1266 The configure options are written in
1270 to distinguish them from run time environment variables (see section search
1276 \begin_inset Quotes sld
1280 \begin_inset Quotes srd
1283 are used by the SDCC team to build the official Win32 binaries.
1284 The SDCC team uses Mingw32 to build the official Windows binaries, because
1291 a gcc compiler and last but not least
1294 the binaries can be built by cross compiling on Sourceforge's compile farm.
1297 See the examples, how to pass the Win32 settings to 'configure'.
1298 The other Win32 builds using Borland, VC or whatever don't use 'configure',
1299 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
1310 \begin_inset Tabular
1311 <lyxtabular version="3" rows="8" columns="3">
1313 <column alignment="block" valignment="top" leftline="true" width="0in">
1314 <column alignment="block" valignment="top" leftline="true" width="0in">
1315 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1316 <row topline="true" bottomline="true">
1317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1342 <row topline="true">
1343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1353 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1361 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1372 <row topline="true">
1373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1393 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1404 <row topline="true">
1405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1415 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1427 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1442 <row topline="true">
1443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1465 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1476 <row topline="true">
1477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1514 <row topline="true">
1515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1542 <row topline="true" bottomline="true">
1543 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1579 'configure' also computes relative paths.
1580 This is needed for full relocatability of a binary package and to complete
1581 search paths (see section search paths below):
1587 \begin_inset Tabular
1588 <lyxtabular version="3" rows="4" columns="3">
1590 <column alignment="block" valignment="top" leftline="true" width="0in">
1591 <column alignment="block" valignment="top" leftline="true" width="0in">
1592 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1593 <row topline="true" bottomline="true">
1594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1602 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1619 <row topline="true" bottomline="true">
1620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1638 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1647 <row bottomline="true">
1648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1658 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1666 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1675 <row bottomline="true">
1676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1727 \begin_inset Quotes srd
1731 \begin_inset Quotes srd
1745 \begin_inset Quotes srd
1749 \begin_inset Quotes srd
1777 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1786 \begin_inset Quotes srd
1789 i586-mingw32msvc-gcc
1790 \begin_inset Quotes srd
1794 \begin_inset Quotes srd
1797 i586-mingw32msvc-g++
1798 \begin_inset Quotes srd
1806 \begin_inset Quotes srd
1809 i586-mingw32msvc-ranlib
1810 \begin_inset Quotes srd
1818 \begin_inset Quotes srd
1821 i586-mingw32msvc-strip
1822 \begin_inset Quotes srd
1840 \begin_inset Quotes srd
1844 \begin_inset Quotes srd
1862 \begin_inset Quotes srd
1866 \begin_inset Quotes srd
1874 \begin_inset Quotes srd
1878 \begin_inset Quotes srd
1886 \begin_inset Quotes srd
1890 \begin_inset Quotes srd
1898 \begin_inset Quotes srd
1902 \begin_inset Quotes srd
1909 sdccconf_h_dir_separator=
1910 \begin_inset Quotes srd
1922 \begin_inset Quotes srd
1967 -host=i586-mingw32msvc -
1977 -build=unknown-unknown-linux-gnu
1981 \begin_inset Quotes sld
1985 \begin_inset Quotes srd
1988 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1997 \begin_inset Quotes srd
2001 \begin_inset Quotes srd
2009 \begin_inset Quotes srd
2013 \begin_inset Quotes srd
2031 \begin_inset Quotes srd
2035 \begin_inset Quotes srd
2053 \begin_inset Quotes srd
2057 \begin_inset Quotes srd
2065 \begin_inset Quotes srd
2069 \begin_inset Quotes srd
2077 \begin_inset Quotes srd
2081 \begin_inset Quotes srd
2089 \begin_inset Quotes srd
2093 \begin_inset Quotes srd
2100 sdccconf_h_dir_separator=
2101 \begin_inset Quotes srd
2113 \begin_inset Quotes srd
2133 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2144 -C' turns on caching, which gives a little bit extra speed.
2145 However if options are changed, it can be necessary to delete the config.cache
2150 \begin_inset LatexCommand \label{sub:Install-paths}
2155 \begin_inset LatexCommand \index{Install paths}
2161 \added_space_top medskip \align center
2163 \begin_inset Tabular
2164 <lyxtabular version="3" rows="5" columns="4">
2166 <column alignment="left" valignment="top" leftline="true" width="0">
2167 <column alignment="left" valignment="top" leftline="true" width="0">
2168 <column alignment="left" valignment="top" leftline="true" width="0">
2169 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
2170 <row topline="true" bottomline="true">
2171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2212 <row topline="true">
2213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2231 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2239 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2252 <row topline="true">
2253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2268 $DATADIR/ $INCLUDE_DIR_SUFFIX
2271 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2276 /usr/local/share/sdcc/include
2279 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2292 <row topline="true">
2293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2308 $DATADIR/$LIB_DIR_SUFFIX
2311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2316 /usr/local/share/sdcc/lib
2319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2332 <row topline="true" bottomline="true">
2333 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2356 /usr/local/share/sdcc/doc
2359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2381 *compiler, preprocessor, assembler, and linker
2387 is auto-appended by the compiler, e.g.
2388 small, large, z80, ds390 etc
2391 The install paths can still be changed during `make install` with e.g.:
2394 make install prefix=$(HOME)/local/sdcc
2397 Of course this doesn't change the search paths compiled into the binaries.
2401 Moreover the install path can be changed by defining DESTDIR
2402 \begin_inset LatexCommand \index{DESTDIR}
2409 make install DESTDIR=$(HOME)/sdcc.rpm/
2412 Please note that DESTDIR must have a trailing slash!
2416 \begin_inset LatexCommand \label{sub:Search-Paths}
2421 \begin_inset LatexCommand \index{Search path}
2428 Some search paths or parts of them are determined by configure variables
2433 , see section above).
2434 Further search paths are determined by environment variables during runtime.
2437 The paths searched when running the compiler are as follows (the first catch
2443 Binary files (preprocessor, assembler and linker)
2449 \begin_inset Tabular
2450 <lyxtabular version="3" rows="4" columns="3">
2452 <column alignment="block" valignment="top" leftline="true" width="0in">
2453 <column alignment="block" valignment="top" leftline="true" width="0in">
2454 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2455 <row topline="true" bottomline="true">
2456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2464 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2481 <row topline="true">
2482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2511 <row topline="true">
2512 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2517 Path of argv[0] (if available)
2520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2537 <row topline="true" bottomline="true">
2538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2554 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2579 \begin_inset Tabular
2580 <lyxtabular version="3" rows="6" columns="3">
2582 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2583 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2584 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2585 <row topline="true" bottomline="true">
2586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2602 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2611 <row topline="true">
2612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2648 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2667 <row topline="true">
2668 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2693 <row topline="true">
2694 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2708 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2731 <row topline="true">
2732 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2750 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2813 <row topline="true" bottomline="true">
2814 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2830 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2835 /usr/local/share/sdcc/
2840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2868 -nostdinc disables the last two search paths.
2878 With the exception of
2879 \begin_inset Quotes sld
2893 \begin_inset Quotes srd
2900 is auto-appended by the compiler (e.g.
2901 small, large, z80, ds390 etc.).
2908 \begin_inset Tabular
2909 <lyxtabular version="3" rows="6" columns="3">
2911 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2912 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2913 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2914 <row topline="true" bottomline="true">
2915 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2923 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2931 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2940 <row topline="true">
2941 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2996 <row topline="true">
2997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3009 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3036 <row topline="true">
3037 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3048 $LIB_DIR_SUFFIX/<model>
3051 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3065 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3082 <row topline="true">
3083 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3098 $LIB_DIR_SUFFIX/<model>
3101 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3210 <row topline="true" bottomline="true">
3211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3220 $LIB_DIR_SUFFIX/<model>
3223 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3228 /usr/local/share/sdcc/
3235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3253 Don't delete any of the stray spaces in the table above without checking
3254 the HTML output (last line)!
3270 -nostdlib disables the last two search paths.
3274 \begin_inset LatexCommand \index{Building SDCC}
3281 Building SDCC on Linux
3282 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3291 Download the source package
3293 either from the SDCC Subversion repository or from the nightly snapshots
3295 , it will be named something like sdcc
3306 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3315 Bring up a command line terminal, such as xterm.
3320 Unpack the file using a command like:
3323 "tar -xvzf sdcc.src.tar.gz
3328 , this will create a sub-directory called sdcc with all of the sources.
3331 Change directory into the main SDCC directory, for example type:
3348 This configures the package for compilation on your system.
3364 All of the source packages will compile, this can take a while.
3380 This copies the binary executables, the include files, the libraries and
3381 the documentation to the install directories.
3382 Proceed with section
3383 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3390 Building SDCC on OSX 2.x
3393 Follow the instruction for Linux.
3397 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3398 )) fails to compile SDCC.
3399 Fortunately there's also gcc 2.9.x installed, which works fine.
3400 This compiler can be selected by running 'configure' with:
3403 ./configure CC=gcc2 CXX=g++2
3406 Cross compiling SDCC on Linux for Windows
3409 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3410 See section 'Configure Options'.
3413 Building SDCC on Windows
3416 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3418 They use Unix-sockets, which are not available on Win32.
3421 Building SDCC using Cygwin and Mingw32
3424 For building and installing a Cygwin executable follow the instructions
3430 \begin_inset Quotes sld
3434 \begin_inset Quotes srd
3437 Win32-binary can be built, which will not need the Cygwin-DLL.
3438 For the necessary 'configure' options see section 'configure options' or
3439 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3443 In order to install Cygwin on Windows download setup.exe from
3444 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3450 \begin_inset Quotes sld
3453 default text file type
3454 \begin_inset Quotes srd
3458 \begin_inset Quotes sld
3462 \begin_inset Quotes srd
3465 and download/install at least the following packages.
3466 Some packages are selected by default, others will be automatically selected
3467 because of dependencies with the manually selected packages.
3468 Never deselect these packages!
3477 gcc ; version 3.x is fine, no need to use the old 2.9x
3480 binutils ; selected with gcc
3486 rxvt ; a nice console, which makes life much easier under windoze (see below)
3489 man ; not really needed for building SDCC, but you'll miss it sooner or
3493 less ; not really needed for building SDCC, but you'll miss it sooner or
3497 svn ; only if you use Subversion access
3500 If you want to develop something you'll need:
3503 python ; for the regression tests
3506 gdb ; the gnu debugger, together with the nice GUI
3507 \begin_inset Quotes sld
3511 \begin_inset Quotes srd
3517 openssh ; to access the CF or commit changes
3520 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3521 use autoconf-stable!
3524 rxvt is a nice console with history.
3525 Replace in your cygwin.bat the line
3544 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3547 -bg black -fg white -geometry 100x65 -e bash -
3560 Text selected with the mouse is automatically copied to the clipboard, pasting
3561 works with shift-insert.
3565 The other good tip is to make sure you have no //c/-style paths anywhere,
3566 use /cygdrive/c/ instead.
3567 Using // invokes a network lookup which is very slow.
3569 \begin_inset Quotes sld
3573 \begin_inset Quotes srd
3576 is too long, you can change it with e.g.
3582 SDCC sources use the unix line ending LF.
3583 Life is much easier, if you store the source tree on a drive which is mounted
3585 And use an editor which can handle LF-only line endings.
3586 Make sure not to commit files with windows line endings.
3587 The tabulator spacing
3588 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3592 used in the project is 8.
3593 Although a tabulator spacing of 8 is a sensible choice for programmers
3594 (it's a power of 2 and allows to display 8/16 bit signed variables without
3595 loosing columns) the plan is to move towards using only spaces in the source.
3598 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3603 Download the source package
3605 either from the SDCC Subversion repository or from the
3606 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3612 , it will be named something like sdcc
3619 SDCC is distributed with all the projects, workspaces, and files you need
3620 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3621 doesn't build under MSVC).
3622 The workspace name is 'sdcc.dsw'.
3623 Please note that as it is now, all the executables are created in a folder
3627 Once built you need to copy the executables from sdcc
3631 bin before running SDCC.
3636 WARNING: Visual studio is very picky with line terminations; it expects
3637 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3638 When using the Subversion repository it's easiest to configure the svn
3639 client to convert automatically for you.
3640 If however you are getting a message such as "This makefile was not generated
3641 by Developer Studio etc.
3643 \begin_inset Quotes srd
3646 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3647 need to convert the Unix style line endings to DOS style line endings.
3648 To do so you can use the
3649 \begin_inset Quotes sld
3653 \begin_inset Quotes srd
3656 utility freely available on the internet.
3657 Doug Hawkins reported in the sdcc-user list that this works:
3665 SDCC> unix2dos sdcc.dsw
3671 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3675 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3676 flex.exe, and gawk.exe.
3677 One good place to get them is
3678 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3686 Download the file UnxUtils
3687 \begin_inset LatexCommand \index{UnxUtils}
3692 Now you have to install the utilities and setup MSVC so it can locate the
3694 Here there are two alternatives (choose one!):
3701 a) Extract UnxUtils.zip to your C:
3703 hard disk PRESERVING the original paths, otherwise bison won't work.
3704 (If you are using WinZip make certain that 'Use folder names' is selected)
3708 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3709 in 'Show directories for:' select 'Executable files', and in the directories
3710 window add a new path: 'C:
3720 (As a side effect, you get a bunch of Unix utilities that could be useful,
3721 such as diff and patch.)
3728 This one avoids extracting a bunch of files you may not use, but requires
3733 a) Create a directory were to put the tools needed, or use a directory already
3741 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3742 to such directory WITHOUT preserving the original paths.
3743 (If you are using WinZip make certain that 'Use folder names' is not selected)
3747 c) Rename bison.exe to '_bison.exe'.
3751 d) Create a batch file 'bison.bat' in 'C:
3755 ' and add these lines:
3775 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3779 Steps 'c' and 'd' are needed because bison requires by default that the
3780 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3781 '/usr/local/share/' I think.
3782 So it is necessary to tell bison where those files are located if they
3783 are not in such directory.
3784 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3788 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3789 in 'Show directories for:' select 'Executable files', and in the directories
3790 window add a new path: 'c:
3793 Note that you can use any other path instead of 'c:
3795 util', even the path where the Visual C++ tools are, probably: 'C:
3799 Microsoft Visual Studio
3804 So you don't have to execute step 'e' :)
3808 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3809 the executables from sdcc
3813 bin, and you can compile using SDCC.
3816 Building SDCC Using Borland
3819 From the sdcc directory, run the command "make -f Makefile.bcc".
3820 This should regenerate all the .exe files in the bin directory except for
3821 sdcdb.exe (which currently doesn't build under Borland C++).
3824 If you modify any source files and need to rebuild, be aware that the dependenci
3825 es may not be correctly calculated.
3826 The safest option is to delete all .obj files and run the build again.
3827 From a Cygwin BASH prompt, this can easily be done with the command (be
3828 sure you are in the sdcc directory):
3838 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3840 ) -print -exec rm {}
3849 or on Windows NT/2000/XP from the command prompt with the command:
3856 del /s *.obj *.lib *.rul
3859 from the sdcc directory.
3862 Windows Install Using a ZIP Package
3865 Download the binary zip package from
3866 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3870 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3871 This should unpack to a group of sub-directories.
3872 An example directory structure after unpacking the mingw32 package is:
3877 bin for the executables, c:
3885 lib for the include and libraries.
3888 Adjust your environment variable PATH to include the location of the bin
3889 directory or start sdcc using the full path.
3892 Windows Install Using the Setup Program
3893 \begin_inset LatexCommand \label{sub:Windows-Install}
3900 Download the setup program
3902 sdcc-x.y.z-setup.exe
3904 for an official release from
3907 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3911 or a setup program for one of the snapshots
3913 sdcc-yyyymmdd-xxxx-setup.exe
3916 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3921 A windows typical installer will guide you through the installation process.
3925 \begin_inset LatexCommand \index{VPATH}
3932 SDCC supports the VPATH feature provided by configure and make.
3933 It allows to separate the source and build trees.
3965 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3966 # extract source to directory sdcc
3971 mkdir sdcc.build\SpecialChar ~
3980 # put output in sdcc.build
3990 ../sdcc/configure\SpecialChar ~
3998 # configure is doing all the magic!
4010 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
4011 It automagically computes the variables srcdir, top_srcdir and top_buildir
4017 the generated files will be in ~/sdcc.build, while the source files stay
4020 This is not only usefull for building different binaries, e.g.
4021 when cross compiling.
4022 It also gives you a much better overview in the source tree when all the
4023 generated files are not scattered between the source files.
4024 And the best thing is: if you want to change a file you can leave the original
4025 file untouched in the source directory.
4026 Simply copy it to the build directory, edit it, enter `make clean`, `rm
4027 Makefile.dep` and `make`.
4032 will do the rest for you!
4035 Building the Documentation
4048 -enable-doc to the configure arguments to build the documentation together
4049 with all the other stuff.
4050 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4051 dvips and makeindex) to get the job done.
4052 Another possibility is to change to the doc directory and to type
4056 \begin_inset Quotes srd
4060 \begin_inset Quotes srd
4067 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4070 \begin_inset LatexCommand \url{http://www.lyx.org}
4074 as editor is straightforward.
4075 Prebuilt documentation in html and pdf format is available from
4076 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4083 Reading the Documentation
4086 Currently reading the document in pdf format is recommended, as for unknown
4087 reason the hyperlinks are working there whereas in the html version they
4094 If you should know why please drop us a note
4100 You'll find the pdf version
4101 \begin_inset LatexCommand \index{PDF version of this document}
4106 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4114 \begin_inset LatexCommand \index{HTML version of this document}
4119 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4125 This documentation is in some aspects different from a commercial documentation:
4129 It tries to document SDCC for several processor architectures in one document
4130 (commercially these probably would be separate documents/products).
4132 \begin_inset LatexCommand \index{Status of documentation}
4136 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4138 Z80, PIC14, PIC16 and HC08.
4141 There are many references pointing away from this documentation.
4142 Don't let this distract you.
4144 was a reference like
4145 \begin_inset LatexCommand \url{http://www.opencores.org}
4149 together with a statement
4150 \begin_inset Quotes sld
4153 some processors which are targetted by SDCC can be implemented in a
4170 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4175 \begin_inset Quotes srd
4179 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4184 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4189 \begin_inset Quotes sld
4192 have you ever heard of an open source compiler that compiles a subset of
4194 \begin_inset Quotes srd
4197 we expect you to have a quick look there and come back.
4198 If you read this you are on the right track.
4201 Some sections attribute more space to problems, restrictions and warnings
4202 than to the solution.
4205 The installation section and the section about the debugger is intimidating.
4208 There are still lots of typos and there are more different writing styles
4212 Testing the SDCC Compiler
4213 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4220 The first thing you should do after installing your SDCC compiler is to
4236 \begin_inset LatexCommand \index{version}
4243 at the prompt, and the program should run and output its version like:
4248 SDCC : mcs51/z80/avr/ds390/pic16/pic14/ds400/hc08 2.5.6 #4169 (May 8 2006)
4252 If it doesn't run, or gives a message about not finding sdcc program, then
4253 you need to check over your installation.
4254 Make sure that the sdcc bin directory is in your executable search path
4255 defined by the PATH environment setting (
4260 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4267 Install trouble-shooting for suggestions
4270 Make sure that the sdcc program is in the bin folder, if not perhaps something
4271 did not install correctly.
4279 is commonly installed as described in section
4280 \begin_inset Quotes sld
4283 Install and search paths
4284 \begin_inset Quotes srd
4293 Make sure the compiler works on a very simple example.
4294 Type in the following test.c program using your favorite
4320 Compile this using the following command:
4329 If all goes well, the compiler will generate a test.asm and test.rel file.
4330 Congratulations, you've just compiled your first program with SDCC.
4331 We used the -c option to tell SDCC not to link the generated code, just
4332 to keep things simple for this step.
4340 The next step is to try it with the linker.
4350 If all goes well the compiler will link with the libraries and produce
4351 a test.ihx output file.
4356 (no test.ihx, and the linker generates warnings), then the problem is most
4365 usr/local/share/sdcc/lib directory
4372 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4379 Install trouble-shooting for suggestions).
4387 The final test is to ensure
4395 header files and libraries.
4396 Edit test.c and change it to the following:
4413 strcpy(str1, "testing");
4420 Compile this by typing
4427 This should generate a test.ihx output file, and it should give no warnings
4428 such as not finding the string.h file.
4429 If it cannot find the string.h file, then the problem is that
4433 cannot find the /usr/local/share/sdcc/include directory
4440 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4447 Install trouble-shooting section for suggestions).
4465 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4469 to find exactly where SDCC is looking for the include and lib files.
4472 Install Trouble-shooting
4473 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4478 \begin_inset LatexCommand \index{Install trouble-shooting}
4485 If SDCC does not build correctly
4488 A thing to try is starting from scratch by unpacking the .tgz source package
4489 again in an empty directory.
4497 ./configure 2>&1 | tee configure.log
4511 make 2>&1 | tee make.log
4518 If anything goes wrong, you can review the log files to locate the problem.
4519 Or a relevant part of this can be attached to an email that could be helpful
4520 when requesting help from the mailing list.
4524 \begin_inset Quotes sld
4528 \begin_inset Quotes srd
4535 \begin_inset Quotes sld
4539 \begin_inset Quotes srd
4542 command is a script that analyzes your system and performs some configuration
4543 to ensure the source package compiles on your system.
4544 It will take a few minutes to run, and will compile a few tests to determine
4545 what compiler features are installed.
4549 \begin_inset Quotes sld
4553 \begin_inset Quotes srd
4559 This runs the GNU make tool, which automatically compiles all the source
4560 packages into the final installed binary executables.
4564 \begin_inset Quotes sld
4568 \begin_inset Quotes erd
4574 This will install the compiler, other executables libraries and include
4575 files into the appropriate directories.
4577 \begin_inset LatexCommand \ref{sub:Install-paths}
4583 \begin_inset LatexCommand \ref{sub:Search-Paths}
4588 about install and search paths.
4590 On most systems you will need super-user privileges to do this.
4596 SDCC is not just a compiler, but a collection of tools by various developers.
4597 These include linkers, assemblers, simulators and other components.
4598 Here is a summary of some of the components.
4599 Note that the included simulator and assembler have separate documentation
4600 which you can find in the source package in their respective directories.
4601 As SDCC grows to include support for other processors, other packages from
4602 various developers are included and may have their own sets of documentation.
4606 You might want to look at the files which are installed in <installdir>.
4607 At the time of this writing, we find the following programs for gcc-builds:
4611 In <installdir>/bin:
4614 sdcc - The compiler.
4617 sdcpp - The C preprocessor.
4620 asx8051 - The assembler for 8051 type processors.
4627 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4630 aslink -The linker for 8051 type processors.
4637 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4640 s51 - The ucSim 8051 simulator.
4641 Not available on Win32 platforms.
4644 sdcdb - The source debugger.
4645 Not available on Win32 platforms.
4648 packihx - A tool to pack (compress) Intel hex files.
4651 In <installdir>/share/sdcc/include
4657 In <installdir>/share/sdcc/lib
4660 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4664 In <installdir>/share/sdcc/doc
4670 As development for other processors proceeds, this list will expand to include
4671 executables to support processors like AVR, PIC, etc.
4677 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4678 the assembler and linkage editor.
4681 sdcpp - The C-Preprocessor
4685 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4689 is a modified version of the GNU preprocessor.
4690 The C preprocessor is used to pull in #include sources, process #ifdef
4691 statements, #defines and so on.
4702 - The Assemblers and Linkage Editors
4705 This is retargettable assembler & linkage editor, it was developed by Alan
4707 John Hartman created the version for 8051, and I (Sandeep) have made some
4708 enhancements and bug fixes for it to work properly with SDCC.
4715 \begin_inset LatexCommand \index{s51}
4719 is a freeware, opensource simulator developed by Daniel Drotos.
4720 The simulator is built as part of the build process.
4721 For more information visit Daniel's web site at:
4722 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4727 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4729 S51 is currently not available on Win32 platfors.
4732 sdcdb - Source Level Debugger
4736 \begin_inset LatexCommand \index{sdcdb (debugger)}
4740 is the companion source level debugger.
4741 More about sdcdb in section
4742 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4747 The current version of the debugger uses Daniel's Simulator S51
4748 \begin_inset LatexCommand \index{s51}
4752 , but can be easily changed to use other simulators.
4753 Sdcdb is currently not available on Win32 platfors.
4762 Single Source File Projects
4765 For single source file 8051 projects the process is very simple.
4766 Compile your programs with the following command
4769 "sdcc sourcefile.c".
4773 This will compile, assemble and link your source file.
4774 Output files are as follows:
4778 \begin_inset LatexCommand \index{<file>.asm}
4783 \begin_inset LatexCommand \index{Assembler source}
4787 file created by the compiler
4791 \begin_inset LatexCommand \index{<file>.lst}
4796 \begin_inset LatexCommand \index{Assembler listing}
4800 file created by the Assembler
4804 \begin_inset LatexCommand \index{<file>.rst}
4809 \begin_inset LatexCommand \index{Assembler listing}
4813 file updated with linkedit information, created by linkage editor
4817 \begin_inset LatexCommand \index{<file>.sym}
4822 \begin_inset LatexCommand \index{Symbol listing}
4826 for the sourcefile, created by the assembler
4830 \begin_inset LatexCommand \index{<file>.rel}
4835 \begin_inset LatexCommand \index{<file>.o}
4840 \begin_inset LatexCommand \index{Object file}
4844 created by the assembler, input to Linkage editor
4848 \begin_inset LatexCommand \index{<file>.map}
4853 \begin_inset LatexCommand \index{Memory map}
4857 for the load module, created by the Linker
4861 \begin_inset LatexCommand \index{<file>.mem}
4865 - A file with a summary of the memory usage
4869 \begin_inset LatexCommand \index{<file>.ihx}
4873 - The load module in Intel hex format
4874 \begin_inset LatexCommand \index{Intel hex format}
4878 (you can select the Motorola S19 format
4879 \begin_inset LatexCommand \index{Motorola S19 format}
4894 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4899 If you need another format you might want to use
4906 \begin_inset LatexCommand \index{objdump (tool)}
4917 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4922 Both formats are documented in the documentation of srecord
4923 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4931 \begin_inset LatexCommand \index{<file>.adb}
4935 - An intermediate file containing debug information needed to create the
4947 \begin_inset LatexCommand \index{-\/-debug}
4955 \begin_inset LatexCommand \index{<file>.cdb}
4959 - An optional file (with -
4969 -debug) containing debug information.
4970 The format is documented in cdbfileformat.pdf
4975 \begin_inset LatexCommand \index{<file> (no extension)}
4979 An optional AOMF or AOMF51
4980 \begin_inset LatexCommand \index{AOMF, AOMF51}
4985 \begin_inset LatexCommand \label{OMF file}
4989 file containing debug information (generated with option -
5016 ormat is commonly used by third party tools (debuggers
5017 \begin_inset LatexCommand \index{Debugger}
5021 , simulators, emulators)
5025 \begin_inset LatexCommand \index{<file>.dump*}
5029 - Dump file to debug the compiler it self (generated with option -
5039 -dumpall) (see section
5040 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5046 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5052 \begin_inset Quotes sld
5055 Anatomy of the compiler
5056 \begin_inset Quotes srd
5062 Projects with Multiple Source Files
5065 SDCC can compile only ONE file at a time.
5066 Let us for example assume that you have a project containing the following
5071 foo1.c (contains some functions)
5073 foo2.c (contains some more functions)
5075 foomain.c (contains more functions and the function main)
5083 The first two files will need to be compiled separately with the commands:
5115 Then compile the source file containing the
5120 \begin_inset LatexCommand \index{Linker}
5124 the files together with the following command:
5132 foomain.c\SpecialChar ~
5133 foo1.rel\SpecialChar ~
5138 \begin_inset LatexCommand \index{<file>.rel}
5150 can be separately compiled as well:
5161 sdcc foomain.rel foo1.rel foo2.rel
5168 The file containing the
5183 file specified in the command line, since the linkage editor processes
5184 file in the order they are presented to it.
5185 The linker is invoked from SDCC using a script file with extension .lnk
5186 \begin_inset LatexCommand \index{<file>.lnk}
5191 You can view this file to troubleshoot linking problems such as those arising
5192 from missing libraries.
5195 Projects with Additional Libraries
5196 \begin_inset LatexCommand \index{Libraries}
5203 Some reusable routines may be compiled into a library, see the documentation
5204 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5208 \begin_inset LatexCommand \index{<file>.lib}
5215 Libraries created in this manner can be included in the command line.
5216 Make sure you include the -L <library-path> option to tell the linker where
5217 to look for these files if they are not in the current directory.
5218 Here is an example, assuming you have the source file
5230 (if that is not the same as your current project):
5237 sdcc foomain.c foolib.lib -L mylib
5248 must be an absolute path name.
5252 The most efficient way to use libraries is to keep separate modules in separate
5254 The lib file now should name all the modules.rel
5255 \begin_inset LatexCommand \index{<file>.rel}
5260 For an example see the standard library file
5264 in the directory <installdir>/share/lib/small.
5267 Using sdcclib to Create and Manage Libraries
5268 \begin_inset LatexCommand \index{sdcclib}
5275 Alternatively, instead of having a .rel file for each entry on the library
5276 file as described in the preceding section, sdcclib can be used to embed
5277 all the modules belonging to such library in the library file itself.
5278 This results in a larger library file, but it greatly reduces the number
5279 of disk files accessed by the linker.
5280 Additionally, the packed library file contains an index of all include
5281 modules and symbols that significantly speeds up the linking process.
5282 To display a list of options supported by sdcclib type:
5291 \begin_inset LatexCommand \index{sdcclib}
5302 To create a new library file, start by compiling all the required modules.
5340 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5342 The next step is to add the .rel files to the library file:
5350 sdcclib libint.lib _divsint.rel
5353 \begin_inset LatexCommand \index{sdcclib}
5363 sdcclib libint.lib _divuint.rel
5369 sdcclib libint.lib _modsint.rel
5375 sdcclib libint.lib _moduint.rel
5381 sdcclib libint.lib _mulint.rel
5388 If the file already exists in the library, it will be replaced.
5389 To see what modules and symbols are included in the library, options -s
5390 and -m are available.
5398 sdcclib -s libint.lib
5401 \begin_inset LatexCommand \index{sdcclib}
5511 If the source files are compiled using -
5522 \begin_inset LatexCommand \index{-\/-debug}
5526 , the corresponding debug information file .adb will be include in the library
5528 The library files created with sdcclib are plain text files, so they can
5529 be viewed with a text editor.
5530 It is not recomended to modify a library file created with sdcclib using
5531 a text editor, as there are file indexes numbers located accross the file
5532 used by the linker to quickly locate the required module to link.
5533 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5534 it can be safely deleted, since all the information required for linking
5535 is embedded in the library file itself.
5536 Library files created using sdcclib are used as described in the preceding
5540 Command Line Options
5541 \begin_inset LatexCommand \index{Command Line Options}
5548 Processor Selection Options
5549 \begin_inset LatexCommand \index{Options processor selection}
5554 \begin_inset LatexCommand \index{Processor selection options}
5560 \labelwidthstring 00.00.0000
5565 \begin_inset LatexCommand \index{-mmcs51}
5571 Generate code for the Intel MCS51
5572 \begin_inset LatexCommand \index{MCS51}
5576 family of processors.
5577 This is the default processor target.
5579 \labelwidthstring 00.00.0000
5584 \begin_inset LatexCommand \index{-mds390}
5590 Generate code for the Dallas DS80C390
5591 \begin_inset LatexCommand \index{DS80C390}
5597 \labelwidthstring 00.00.0000
5602 \begin_inset LatexCommand \index{-mds400}
5608 Generate code for the Dallas DS80C400
5609 \begin_inset LatexCommand \index{DS80C400}
5615 \labelwidthstring 00.00.0000
5620 \begin_inset LatexCommand \index{-mhc08}
5626 Generate code for the Freescale/Motorola HC08
5627 \begin_inset LatexCommand \index{HC08}
5631 family of processors.
5633 \labelwidthstring 00.00.0000
5638 \begin_inset LatexCommand \index{-mz80}
5644 Generate code for the Zilog Z80
5645 \begin_inset LatexCommand \index{Z80}
5649 family of processors.
5651 \labelwidthstring 00.00.0000
5656 \begin_inset LatexCommand \index{-mgbz80}
5662 Generate code for the GameBoy Z80
5663 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5667 processor (Not actively maintained).
5669 \labelwidthstring 00.00.0000
5674 \begin_inset LatexCommand \index{-mavr}
5680 Generate code for the Atmel AVR
5681 \begin_inset LatexCommand \index{AVR}
5685 processor (In development, not complete).
5686 AVR users should probably have a look at winavr
5687 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5692 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5699 I think it is fair to direct users there for now.
5700 Open source is also about avoiding unnecessary work .
5701 But I didn't find the 'official' link.
5703 \labelwidthstring 00.00.0000
5708 \begin_inset LatexCommand \index{-mpic14}
5714 Generate code for the Microchip PIC 14
5715 \begin_inset LatexCommand \index{PIC14}
5719 -bit processors (p16f84 and variants.
5720 In development, not complete).
5723 p16f627 p16f628 p16f84 p16f873 p16f877?
5725 \labelwidthstring 00.00.0000
5730 \begin_inset LatexCommand \index{-mpic16}
5736 Generate code for the Microchip PIC 16
5737 \begin_inset LatexCommand \index{PIC16}
5741 -bit processors (p18f452 and variants.
5742 In development, not complete).
5744 \labelwidthstring 00.00.0000
5750 Generate code for the Toshiba TLCS-900H
5751 \begin_inset LatexCommand \index{TLCS-900H}
5755 processor (Not maintained, not complete).
5757 \labelwidthstring 00.00.0000
5762 \begin_inset LatexCommand \index{-mxa51}
5768 Generate code for the Phillips XA51
5769 \begin_inset LatexCommand \index{XA51}
5773 processor (Not maintained, not complete).
5776 Preprocessor Options
5777 \begin_inset LatexCommand \index{Options preprocessor}
5782 \begin_inset LatexCommand \index{Preprocessor options}
5787 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5793 \labelwidthstring 00.00.0000
5798 \begin_inset LatexCommand \index{-I<path>}
5804 The additional location where the pre processor will look for <..h> or
5805 \begin_inset Quotes eld
5809 \begin_inset Quotes erd
5814 \labelwidthstring 00.00.0000
5819 \begin_inset LatexCommand \index{-D<macro[=value]>}
5825 Command line definition of macros.
5826 Passed to the preprocessor.
5828 \labelwidthstring 00.00.0000
5833 \begin_inset LatexCommand \index{-M}
5839 Tell the preprocessor to output a rule suitable for make describing the
5840 dependencies of each object file.
5841 For each source file, the preprocessor outputs one make-rule whose target
5842 is the object file name for that source file and whose dependencies are
5843 all the files `#include'd in it.
5844 This rule may be a single line or may be continued with `
5846 '-newline if it is long.
5847 The list of rules is printed on standard output instead of the preprocessed
5850 \begin_inset LatexCommand \index{-E}
5856 \labelwidthstring 00.00.0000
5861 \begin_inset LatexCommand \index{-C}
5867 Tell the preprocessor not to discard comments.
5868 Used with the `-E' option.
5870 \labelwidthstring 00.00.0000
5875 \begin_inset LatexCommand \index{-MM}
5886 Like `-M' but the output mentions only the user header files included with
5888 \begin_inset Quotes eld
5892 System header files included with `#include <file>' are omitted.
5894 \labelwidthstring 00.00.0000
5899 \begin_inset LatexCommand \index{-Aquestion(answer)}
5905 Assert the answer answer for question, in case it is tested with a preprocessor
5906 conditional such as `#if #question(answer)'.
5907 `-A-' disables the standard assertions that normally describe the target
5910 \labelwidthstring 00.00.0000
5915 \begin_inset LatexCommand \index{-Umacro}
5921 Undefine macro macro.
5922 `-U' options are evaluated after all `-D' options, but before any `-include'
5923 and `-imacros' options.
5925 \labelwidthstring 00.00.0000
5930 \begin_inset LatexCommand \index{-dM}
5936 Tell the preprocessor to output only a list of the macro definitions that
5937 are in effect at the end of preprocessing.
5938 Used with the `-E' option.
5940 \labelwidthstring 00.00.0000
5945 \begin_inset LatexCommand \index{-dD}
5951 Tell the preprocessor to pass all macro definitions into the output, in
5952 their proper sequence in the rest of the output.
5954 \labelwidthstring 00.00.0000
5959 \begin_inset LatexCommand \index{-dN}
5970 Like `-dD' except that the macro arguments and contents are omitted.
5971 Only `#define name' is included in the output.
5973 \labelwidthstring 00.00.0000
5978 preprocessorOption[,preprocessorOption]
5981 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5986 Pass the preprocessorOption to the preprocessor
5991 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5996 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5997 Collection (gcc), if you need more dedicated options please refer to the
5999 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
6007 \begin_inset LatexCommand \index{Options linker}
6012 \begin_inset LatexCommand \index{Linker options}
6018 \labelwidthstring 00.00.0000
6038 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6043 \begin_inset LatexCommand \index{-L -\/-lib-path}
6050 <absolute path to additional libraries> This option is passed to the linkage
6051 editor's additional libraries
6052 \begin_inset LatexCommand \index{Libraries}
6057 The path name must be absolute.
6058 Additional library files may be specified in the command line.
6059 See section Compiling programs for more details.
6061 \labelwidthstring 00.00.0000
6078 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6083 <Value> The start location of the external ram
6084 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6088 , default value is 0.
6089 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6099 -xram-loc 0x8000 or -
6111 \labelwidthstring 00.00.0000
6128 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6133 <Value> The start location of the code
6134 \begin_inset LatexCommand \index{code}
6138 segment, default value 0.
6139 Note when this option is used the interrupt vector table is also relocated
6140 to the given address.
6141 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6151 -code-loc 0x8000 or -
6163 \labelwidthstring 00.00.0000
6180 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6185 <Value> By default the stack
6186 \begin_inset LatexCommand \index{stack}
6190 is placed after the data segment.
6191 Using this option the stack can be placed anywhere in the internal memory
6193 The value entered can be in Hexadecimal or Decimal format, e.g.
6204 -stack-loc 0x20 or -
6215 Since the sp register is incremented before a push or call, the initial
6216 sp will be set to one byte prior the provided value.
6217 The provided value should not overlap any other memory areas such as used
6218 register banks or the data segment and with enough space for the current
6236 \begin_inset LatexCommand \index{-\/-pack-iram}
6240 option (which is now a default setting) will override this setting, so
6241 you should also specify the
6257 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6261 option if you need to manually place the stack.
6263 \labelwidthstring 00.00.0000
6280 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6285 <Value> By default the external stack
6286 \begin_inset LatexCommand \index{xstack}
6290 is placed after the pdata segment.
6291 Using this option the xstack can be placed anywhere in the external memory
6293 The value entered can be in Hexadecimal or Decimal format, e.g.
6304 -xstack-loc 0x8000 or -
6315 The provided value should not overlap any other memory areas such as the
6316 pdata or xdata segment and with enough space for the current application.
6318 \labelwidthstring 00.00.0000
6335 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6340 <Value> The start location of the internal ram data
6341 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6346 The value entered can be in Hexadecimal or Decimal format, eg.
6368 (By default, the start location of the internal ram data segment is set
6369 as low as possible in memory, taking into account the used register banks
6370 and the bit segment at address 0x20.
6371 For example if register banks 0 and 1 are used without bit variables, the
6372 data segment will be set, if -
6382 -data-loc is not used, to location 0x10.)
6384 \labelwidthstring 00.00.0000
6401 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6406 <Value> The start location of the indirectly addressable internal ram
6407 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6411 of the 8051, default value is 0x80.
6412 The value entered can be in Hexadecimal or Decimal format, eg.
6423 -idata-loc 0x88 or -
6435 \labelwidthstring 00.00.0000
6452 <Value> The start location of the bit
6453 \begin_inset LatexCommand \index{bit}
6457 addressable internal ram of the 8051.
6463 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6466 \labelwidthstring 00.00.0000
6481 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6490 The linker output (final object code) is in Intel Hex format.
6491 \begin_inset LatexCommand \index{Intel hex format}
6495 This is the default option.
6496 The format itself is documented in the documentation of srecord
6497 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6503 \labelwidthstring 00.00.0000
6518 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6527 The linker output (final object code) is in Motorola S19 format
6528 \begin_inset LatexCommand \index{Motorola S19 format}
6533 The format itself is documented in the documentation of srecord.
6535 \labelwidthstring 00.00.0000
6550 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6559 The linker output (final object code) is in ELF format
6560 \begin_inset LatexCommand \index{ELF format}
6565 (Currently only supported for the HC08 processors)
6567 \labelwidthstring 00.00.0000
6572 linkOption[,linkOption]
6575 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6580 Pass the linkOption to the linker.
6581 See file sdcc/as/doc/asxhtm.html for more on linker options.
6585 \begin_inset LatexCommand \index{Options MCS51}
6590 \begin_inset LatexCommand \index{MCS51 options}
6596 \labelwidthstring 00.00.0000
6611 \begin_inset LatexCommand \index{-\/-model-small}
6622 Generate code for Small Model programs, see section Memory Models for more
6624 This is the default model.
6626 \labelwidthstring 00.00.0000
6641 \begin_inset LatexCommand \index{-\/-model-medium}
6647 Generate code for Medium model programs, see section Memory Models for
6649 If this option is used all source files in the project have to be compiled
6651 It must also be used when invoking the linker.
6653 \labelwidthstring 00.00.0000
6668 \begin_inset LatexCommand \index{-\/-model-large}
6674 Generate code for Large model programs, see section Memory Models for more
6676 If this option is used all source files in the project have to be compiled
6678 It must also be used when invoking the linker.
6680 \labelwidthstring 00.00.0000
6695 \begin_inset LatexCommand \index{-\/-xstack}
6701 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6702 variables and passing parameters.
6704 \begin_inset LatexCommand \ref{sub:External-Stack}
6709 External Stack for more details.
6711 \labelwidthstring 00.00.0000
6729 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6733 Causes the linker to check if the internal ram usage is within limits of
6736 \labelwidthstring 00.00.0000
6754 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6758 Causes the linker to check if the external ram usage is within limits of
6761 \labelwidthstring 00.00.0000
6779 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6783 Causes the linker to check if the code memory usage is within limits of
6786 \labelwidthstring 00.00.0000
6804 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6808 Causes the linker to check if there is at minimum <Value> bytes for stack.
6810 \labelwidthstring 00.00.0000
6828 \begin_inset LatexCommand \index{-\/-pack-iram}
6832 Causes the linker to use unused register banks for data variables and pack
6833 data, idata and stack together.
6834 This is the default now.
6836 \labelwidthstring 00.00.0000
6854 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6858 Causes the linker to use old style for allocating memory areas.
6861 DS390 / DS400 Options
6862 \begin_inset LatexCommand \index{Options DS390}
6867 \begin_inset LatexCommand \index{DS390 options}
6873 \labelwidthstring 00.00.0000
6890 \begin_inset LatexCommand \index{-\/-model-flat24}
6900 Generate 24-bit flat mode code.
6901 This is the one and only that the ds390 code generator supports right now
6902 and is default when using
6907 See section Memory Models for more details.
6909 \labelwidthstring 00.00.0000
6924 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6930 disable interrupts during ESP:SP updates.
6932 \labelwidthstring 00.00.0000
6949 \begin_inset LatexCommand \index{-\/-stack-10bit}
6953 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6954 This is the one and only that the ds390 code generator supports right now
6955 and is default when using
6960 In this mode, the stack is located in the lower 1K of the internal RAM,
6961 which is mapped to 0x400000.
6962 Note that the support is incomplete, since it still uses a single byte
6963 as the stack pointer.
6964 This means that only the lower 256 bytes of the potential 1K stack space
6965 will actually be used.
6966 However, this does allow you to reclaim the precious 256 bytes of low RAM
6967 for use for the DATA and IDATA segments.
6968 The compiler will not generate any code to put the processor into 10 bit
6970 It is important to ensure that the processor is in this mode before calling
6971 any re-entrant functions compiled with this option.
6972 In principle, this should work with the
6985 \begin_inset LatexCommand \index{-\/-stack-auto}
6991 option, but that has not been tested.
6992 It is incompatible with the
7005 \begin_inset LatexCommand \index{-\/-xstack}
7012 It also only makes sense if the processor is in 24 bit contiguous addressing
7025 -model-flat24 option
7029 \labelwidthstring 00.00.0000
7044 \begin_inset LatexCommand \index{-\/-stack-probe}
7050 insert call to function __stack_probe at each function prologue.
7052 \labelwidthstring 00.00.0000
7067 \begin_inset LatexCommand \index{-\/-tini-libid}
7073 <nnnn> LibraryID used in -mTININative.
7076 \labelwidthstring 00.00.0000
7091 \begin_inset LatexCommand \index{-\/-use-accelerator}
7097 generate code for DS390 Arithmetic Accelerator.
7102 \begin_inset LatexCommand \index{Options Z80}
7107 \begin_inset LatexCommand \index{Z80 options}
7113 \labelwidthstring 00.00.0000
7130 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
7140 Force a called function to always save BC.
7142 \labelwidthstring 00.00.0000
7159 \begin_inset LatexCommand \index{-\/-no-std-crt0}
7163 When linking, skip the standard crt0.o object file.
7164 You must provide your own crt0.o for your system when linking.
7168 Optimization Options
7169 \begin_inset LatexCommand \index{Options optimization}
7174 \begin_inset LatexCommand \index{Optimization options}
7180 \labelwidthstring 00.00.0000
7195 \begin_inset LatexCommand \index{-\/-nogcse}
7201 Will not do global subexpression elimination, this option may be used when
7202 the compiler creates undesirably large stack/data spaces to store compiler
7212 \begin_inset LatexCommand \index{sloc (spill location)}
7217 A warning message will be generated when this happens and the compiler
7218 will indicate the number of extra bytes it allocated.
7219 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7221 \begin_inset LatexCommand \index{\#pragma nogcse}
7225 can be used to turn off global subexpression elimination
7226 \begin_inset LatexCommand \index{Subexpression elimination}
7230 for a given function only.
7232 \labelwidthstring 00.00.0000
7247 \begin_inset LatexCommand \index{-\/-noinvariant}
7253 Will not do loop invariant optimizations, this may be turned off for reasons
7254 explained for the previous option.
7255 For more details of loop optimizations performed see Loop Invariants in
7257 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7262 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7264 \begin_inset LatexCommand \index{\#pragma noinvariant}
7268 can be used to turn off invariant optimizations for a given function only.
7270 \labelwidthstring 00.00.0000
7285 \begin_inset LatexCommand \index{-\/-noinduction}
7291 Will not do loop induction optimizations, see section strength reduction
7293 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7295 \begin_inset LatexCommand \index{\#pragma noinduction}
7299 can be used to turn off induction optimizations for a given function only.
7301 \labelwidthstring 00.00.0000
7316 \begin_inset LatexCommand \index{-\/-nojtbound}
7327 Will not generate boundary condition check when switch statements
7328 \begin_inset LatexCommand \index{switch statement}
7332 are implemented using jump-tables.
7334 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7339 Switch Statements for more details.
7340 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7342 \begin_inset LatexCommand \index{\#pragma nojtbound}
7346 can be used to turn off boundary checking for jump tables for a given function
7349 \labelwidthstring 00.00.0000
7364 \begin_inset LatexCommand \index{-\/-noloopreverse}
7373 Will not do loop reversal
7374 \begin_inset LatexCommand \index{Loop reversing}
7380 \labelwidthstring 00.00.0000
7397 \begin_inset LatexCommand \index{-\/-nolabelopt }
7401 Will not optimize labels (makes the dumpfiles more readable).
7403 \labelwidthstring 00.00.0000
7418 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7424 Will not memcpy initialized data from code space into xdata space.
7425 This saves a few bytes in code space if you don't have initialized data
7426 \begin_inset LatexCommand \index{Variable initialization}
7432 \labelwidthstring 00.00.0000
7447 \begin_inset LatexCommand \index{-\/-nooverlay}
7453 The compiler will not overlay parameters and local variables of any function,
7454 see section Parameters and local variables for more details.
7456 \labelwidthstring 00.00.0000
7471 \begin_inset LatexCommand \index{-\/-no-peep}
7477 Disable peep-hole optimization with built-in rules.
7479 \labelwidthstring 00.00.0000
7496 \begin_inset LatexCommand \index{-\/-peep-file}
7501 <filename> This option can be used to use additional rules to be used by
7502 the peep hole optimizer.
7504 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7509 Peep Hole optimizations for details on how to write these rules.
7511 \labelwidthstring 00.00.0000
7526 \begin_inset LatexCommand \index{-\/-peep-asm}
7532 Pass the inline assembler code through the peep hole optimizer.
7533 This can cause unexpected changes to inline assembler code, please go through
7534 the peephole optimizer
7535 \begin_inset LatexCommand \index{Peephole optimizer}
7539 rules defined in the source file tree '<target>/peeph.def' before using
7542 \labelwidthstring 00.00.0000
7557 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7563 The compiler will optimize code generation towards fast code, possibly
7564 at the expense of code size.
7566 \labelwidthstring 00.00.0000
7581 \begin_inset LatexCommand \index{-\/-opt-code-size}
7587 The compiler will optimize code generation towards compact code, possibly
7588 at the expense of code speed.
7592 \begin_inset LatexCommand \index{Options other}
7598 \labelwidthstring 00.00.0000
7614 \begin_inset LatexCommand \index{-\/-compile-only}
7619 \begin_inset LatexCommand \index{-c -\/-compile-only}
7625 will compile and assemble the source, but will not call the linkage editor.
7627 \labelwidthstring 00.00.0000
7646 \begin_inset LatexCommand \index{-\/-c1mode}
7652 reads the preprocessed source from standard input and compiles it.
7653 The file name for the assembler output must be specified using the -o option.
7655 \labelwidthstring 00.00.0000
7660 \begin_inset LatexCommand \index{-E}
7666 Run only the C preprocessor.
7667 Preprocess all the C source files specified and output the results to standard
7670 \labelwidthstring 00.00.0000
7676 \begin_inset LatexCommand \index{-o <path/file>}
7682 The output path resp.
7683 file where everything will be placed.
7684 If the parameter is a path, it must have a trailing slash (or backslash
7685 for the Windows binaries) to be recognized as a path.
7688 \labelwidthstring 00.00.0000
7703 \begin_inset LatexCommand \index{-\/-stack-auto}
7714 All functions in the source file will be compiled as
7719 \begin_inset LatexCommand \index{reentrant}
7724 the parameters and local variables will be allocated on the stack
7725 \begin_inset LatexCommand \index{stack}
7731 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7735 Parameters and Local Variables for more details.
7736 If this option is used all source files in the project should be compiled
7738 It automatically implies --int-long-reent and --float-reent.
7741 \labelwidthstring 00.00.0000
7756 \begin_inset LatexCommand \index{-\/-callee-saves}
7760 function1[,function2][,function3]....
7763 The compiler by default uses a caller saves convention for register saving
7764 across function calls, however this can cause unnecessary register pushing
7765 & popping when calling small functions from larger functions.
7766 This option can be used to switch the register saving convention for the
7767 function names specified.
7768 The compiler will not save registers when calling these functions, no extra
7769 code will be generated at the entry & exit (function prologue
7772 \begin_inset LatexCommand \index{function prologue}
7781 \begin_inset LatexCommand \index{function epilogue}
7787 ) for these functions to save & restore the registers used by these functions,
7788 this can SUBSTANTIALLY reduce code & improve run time performance of the
7790 In the future the compiler (with inter procedural analysis) will be able
7791 to determine the appropriate scheme to use for each function call.
7792 DO NOT use this option for built-in functions such as _mulint..., if this
7793 option is used for a library function the appropriate library function
7794 needs to be recompiled with the same option.
7795 If the project consists of multiple source files then all the source file
7796 should be compiled with the same -
7806 -callee-saves option string.
7807 Also see #pragma\SpecialChar ~
7809 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7815 \labelwidthstring 00.00.0000
7830 \begin_inset LatexCommand \index{-\/-debug}
7839 When this option is used the compiler will generate debug information.
7840 The debug information collected in a file with .cdb extension can be used
7842 For more information see documentation for SDCDB.
7843 Another file with no extension contains debug information in AOMF or AOMF51
7844 \begin_inset LatexCommand \index{AOMF, AOMF51}
7848 format which is commonly used by third party tools.
7850 \labelwidthstring 00.00.0000
7855 \begin_inset LatexCommand \index{-S}
7866 Stop after the stage of compilation proper; do not assemble.
7867 The output is an assembler code file for the input file specified.
7869 \labelwidthstring 00.00.0000
7884 \begin_inset LatexCommand \index{-\/-int-long-reent}
7890 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7891 Note by default these libraries are compiled as non-reentrant.
7892 See section Installation for more details.
7894 \labelwidthstring 00.00.0000
7909 \begin_inset LatexCommand \index{-\/-cyclomatic}
7918 This option will cause the compiler to generate an information message for
7919 each function in the source file.
7920 The message contains some
7924 information about the function.
7925 The number of edges and nodes the compiler detected in the control flow
7926 graph of the function, and most importantly the
7928 cyclomatic complexity
7929 \begin_inset LatexCommand \index{Cyclomatic complexity}
7935 see section on Cyclomatic Complexity for more details.
7937 \labelwidthstring 00.00.0000
7952 \begin_inset LatexCommand \index{-\/-float-reent}
7958 Floating point library is compiled as reentrant
7959 \begin_inset LatexCommand \index{reentrant}
7964 See section Installation for more details.
7966 \labelwidthstring 00.00.0000
7981 \begin_inset LatexCommand \index{-\/-main-return}
7987 This option can be used if the code generated is called by a monitor program
7988 or if the main routine includes an endless loop.
7989 This option might result in slightly smaller code and save two bytes of
7991 The return from the 'main'
7992 \begin_inset LatexCommand \index{main return}
7996 function will return to the function calling main.
7997 The default setting is to lock up i.e.
8004 \labelwidthstring 00.00.0000
8019 \begin_inset LatexCommand \index{-\/-nostdinc}
8025 This will prevent the compiler from passing on the default include path
8026 to the preprocessor.
8028 \labelwidthstring 00.00.0000
8043 \begin_inset LatexCommand \index{-\/-nostdlib}
8049 This will prevent the compiler from passing on the default library
8050 \begin_inset LatexCommand \index{Libraries}
8056 \labelwidthstring 00.00.0000
8071 \begin_inset LatexCommand \index{-\/-verbose}
8077 Shows the various actions the compiler is performing.
8079 \labelwidthstring 00.00.0000
8084 \begin_inset LatexCommand \index{-V}
8090 Shows the actual commands the compiler is executing.
8092 \labelwidthstring 00.00.0000
8107 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8113 Hides your ugly and inefficient c-code from the asm file, so you can always
8114 blame the compiler :)
8116 \labelwidthstring 00.00.0000
8131 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8137 Will not include peep-hole comments in the generated files.
8139 \labelwidthstring 00.00.0000
8154 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8160 Include i-codes in the asm file.
8161 Sounds like noise but is most helpful for debugging the compiler itself.
8163 \labelwidthstring 00.00.0000
8178 \begin_inset LatexCommand \index{-\/-less-pedantic}
8184 Disable some of the more pedantic warnings
8185 \begin_inset LatexCommand \index{Warnings}
8189 (jwk burps: please be more specific here, please!).
8191 \labelwidthstring 00.00.0000
8205 -disable-warning\SpecialChar ~
8207 \begin_inset LatexCommand \index{-\/-disable-warning}
8213 Disable specific warning with number <nnnn>.
8215 \labelwidthstring 00.00.0000
8230 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8236 Display the directories in the compiler's search path
8238 \labelwidthstring 00.00.0000
8253 \begin_inset LatexCommand \index{-\/-vc}
8259 Display errors and warnings using MSVC style, so you can use SDCC with
8262 \labelwidthstring 00.00.0000
8277 \begin_inset LatexCommand \index{-\/-use-stdout}
8283 Send errors and warnings to stdout instead of stderr.
8285 \labelwidthstring 00.00.0000
8290 asmOption[,asmOption]
8293 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8298 Pass the asmOption to the assembler
8299 \begin_inset LatexCommand \index{Options assembler}
8304 \begin_inset LatexCommand \index{Assembler options}
8309 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8311 \labelwidthstring 00.00.0000
8326 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8332 Generally follow the C89 standard, but allow SDCC features that conflict
8333 with the standard (default).
8335 \labelwidthstring 00.00.0000
8350 \begin_inset LatexCommand \index{-\/-std-c89}
8356 Follow the C89 standard and disable SDCC features that conflict with the
8359 \labelwidthstring 00.00.0000
8374 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8380 Generally follow the C99 standard, but allow SDCC features that conflict
8381 with the standard (incomplete support).
8383 \labelwidthstring 00.00.0000
8398 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8404 Follow the C99 standard and disable SDCC features that conflict with the
8405 standard (incomplete support).
8407 \labelwidthstring 00.00.0000
8424 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8429 <Name> The name to be used for the code
8430 \begin_inset LatexCommand \index{code}
8434 segment, default CSEG.
8435 This is useful if you need to tell the compiler to put the code in a special
8436 segment so you can later on tell the linker to put this segment in a special
8438 Can be used for instance when using bank switching to put the code in a
8441 \labelwidthstring 00.00.0000
8458 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8463 <Name> The name to be used for the const
8464 \begin_inset LatexCommand \index{code}
8468 segment, default CONST.
8469 This is useful if you need to tell the compiler to put the const data in
8470 a special segment so you can later on tell the linker to put this segment
8471 in a special place in memory.
8472 Can be used for instance when using bank switching to put the const data
8475 \labelwidthstring 00.00.0000
8487 a SDCC compiler option but if you want
8491 warnings you can use a separate tool dedicated to syntax checking like
8493 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8498 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8503 \begin_inset LatexCommand \url{http://www.splint.org}
8508 To make your source files parseable by splint you will have to include
8514 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8518 in your source file and add brackets around extended keywords (like
8521 \begin_inset Quotes sld
8534 \begin_inset Quotes srd
8542 \begin_inset Quotes sld
8545 __interrupt\SpecialChar ~
8547 \begin_inset Quotes srd
8555 Splint has an excellent on line manual at
8556 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8560 and it's capabilities go beyond pure syntax checking.
8561 You'll need to tell splint the location of SDCC's include files so a typical
8562 command line could look like this:
8566 splint\SpecialChar ~
8568 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8573 Intermediate Dump Options
8574 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8579 \begin_inset LatexCommand \index{Options intermediate dump}
8584 \begin_inset LatexCommand \index{Intermediate dump options}
8591 The following options are provided for the purpose of retargetting and debugging
8593 They provide a means to dump the intermediate code (iCode
8594 \begin_inset LatexCommand \index{iCode}
8598 ) generated by the compiler in human readable form at various stages of
8599 the compilation process.
8600 More on iCodes see chapter
8601 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8606 \begin_inset Quotes srd
8609 The anatomy of the compiler
8610 \begin_inset Quotes srd
8615 \labelwidthstring 00.00.0000
8630 \begin_inset LatexCommand \index{-\/-dumpraw}
8636 This option will cause the compiler to dump the intermediate code into
8639 <source filename>.dumpraw
8641 just after the intermediate code has been generated for a function, i.e.
8642 before any optimizations are done.
8644 \begin_inset LatexCommand \index{Basic blocks}
8648 at this stage ordered in the depth first number, so they may not be in
8649 sequence of execution.
8651 \labelwidthstring 00.00.0000
8666 \begin_inset LatexCommand \index{-\/-dumpgcse}
8672 Will create a dump of iCode's, after global subexpression elimination
8673 \begin_inset LatexCommand \index{Global subexpression elimination}
8679 <source filename>.dumpgcse.
8681 \labelwidthstring 00.00.0000
8696 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8702 Will create a dump of iCode's, after deadcode elimination
8703 \begin_inset LatexCommand \index{Dead-code elimination}
8709 <source filename>.dumpdeadcode.
8711 \labelwidthstring 00.00.0000
8726 \begin_inset LatexCommand \index{-\/-dumploop}
8735 Will create a dump of iCode's, after loop optimizations
8736 \begin_inset LatexCommand \index{Loop optimization}
8742 <source filename>.dumploop.
8744 \labelwidthstring 00.00.0000
8759 \begin_inset LatexCommand \index{-\/-dumprange}
8768 Will create a dump of iCode's, after live range analysis
8769 \begin_inset LatexCommand \index{Live range analysis}
8775 <source filename>.dumprange.
8777 \labelwidthstring 00.00.0000
8792 \begin_inset LatexCommand \index{-\/-dumlrange}
8798 Will dump the life ranges
8799 \begin_inset LatexCommand \index{Live range analysis}
8805 \labelwidthstring 00.00.0000
8820 \begin_inset LatexCommand \index{-\/-dumpregassign}
8829 Will create a dump of iCode's, after register assignment
8830 \begin_inset LatexCommand \index{Register assignment}
8836 <source filename>.dumprassgn.
8838 \labelwidthstring 00.00.0000
8853 \begin_inset LatexCommand \index{-\/-dumplrange}
8859 Will create a dump of the live ranges of iTemp's
8861 \labelwidthstring 00.00.0000
8876 \begin_inset LatexCommand \index{-\/-dumpall}
8887 Will cause all the above mentioned dumps to be created.
8890 Redirecting output on Windows Shells
8893 By default SDCC writes it's error messages to
8894 \begin_inset Quotes sld
8898 \begin_inset Quotes srd
8902 To force all messages to
8903 \begin_inset Quotes sld
8907 \begin_inset Quotes srd
8931 \begin_inset LatexCommand \index{-\/-use-stdout}
8936 Additionally, if you happen to have visual studio installed in your windows
8937 machine, you can use it to compile your sources using a custom build and
8953 \begin_inset LatexCommand \index{-\/-vc}
8958 Something like this should work:
9002 -model-large -c $(InputPath)
9005 Environment variables
9006 \begin_inset LatexCommand \index{Environment variables}
9013 SDCC recognizes the following environment variables:
9015 \labelwidthstring 00.00.0000
9020 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
9026 SDCC installs a signal handler
9027 \begin_inset LatexCommand \index{signal handler}
9031 to be able to delete temporary files after an user break (^C) or an exception.
9032 If this environment variable is set, SDCC won't install the signal handler
9033 in order to be able to debug SDCC.
9035 \labelwidthstring 00.00.0000
9042 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9048 Path, where temporary files will be created.
9049 The order of the variables is the search order.
9050 In a standard *nix environment these variables are not set, and there's
9051 no need to set them.
9052 On Windows it's recommended to set one of them.
9054 \labelwidthstring 00.00.0000
9059 \begin_inset LatexCommand \index{SDCC\_HOME}
9066 \begin_inset LatexCommand \ref{sub:Install-paths}
9072 \begin_inset Quotes sld
9076 \begin_inset Quotes srd
9081 \labelwidthstring 00.00.0000
9086 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9093 \begin_inset LatexCommand \ref{sub:Search-Paths}
9099 \begin_inset Quotes sld
9103 \begin_inset Quotes srd
9108 \labelwidthstring 00.00.0000
9113 \begin_inset LatexCommand \index{SDCC\_LIB}
9120 \begin_inset LatexCommand \ref{sub:Search-Paths}
9126 \begin_inset Quotes sld
9130 \begin_inset Quotes srd
9136 There are some more environment variables recognized by SDCC, but these
9137 are solely used for debugging purposes.
9138 They can change or disappear very quickly, and will never be documented.
9141 Storage Class Language Extensions
9144 MCS51/DS390 Storage Class
9145 \begin_inset LatexCommand \index{Storage class}
9152 In addition to the ANSI storage classes SDCC allows the following MCS51
9153 specific storage classes:
9154 \layout Subsubsection
9157 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9162 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9167 \begin_inset LatexCommand \index{near (storage class)}
9172 \begin_inset LatexCommand \index{\_\_near (storage class)}
9183 storage class for the Small Memory model (
9191 or the more ANSI-C compliant forms
9199 can be used synonymously).
9200 Variables declared with this storage class will be allocated in the directly
9201 addressable portion of the internal RAM of a 8051, e.g.:
9206 __data unsigned char test_data;
9209 Writing 0x01 to this variable generates the assembly code:
9214 75*00 01\SpecialChar ~
9220 \layout Subsubsection
9223 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9228 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9233 \begin_inset LatexCommand \index{far (storage class)}
9238 \begin_inset LatexCommand \index{\_\_far (storage class)}
9245 Variables declared with this storage class will be placed in the external
9251 storage class for the Large Memory model, e.g.:
9256 __xdata unsigned char test_xdata;
9259 Writing 0x01 to this variable generates the assembly code:
9264 90s00r00\SpecialChar ~
9293 \layout Subsubsection
9296 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9301 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9308 Variables declared with this storage class will be allocated into the indirectly
9309 addressable portion of the internal ram of a 8051, e.g.:
9314 __idata unsigned char test_idata;
9317 Writing 0x01 to this variable generates the assembly code:
9346 Please note, the first 128 byte of idata physically access the same RAM
9348 The original 8051 had 128 byte idata memory, nowadays most devices have
9349 256 byte idata memory.
9351 \begin_inset LatexCommand \index{stack}
9355 is located in idata memory.
9356 \layout Subsubsection
9359 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9364 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9371 Paged xdata access is just as straightforward as using the other addressing
9373 It is typically located at the start of xdata and has a maximum size of
9375 The following example writes 0x01 to the pdata variable.
9376 Please note, pdata access physically accesses xdata memory.
9377 The high byte of the address is determined by port P2
9378 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9382 (or in case of some 8051 variants by a separate Special Function Register,
9384 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9393 storage class for the Medium Memory model, e.g.:
9398 __pdata unsigned char test_pdata;
9401 Writing 0x01 to this variable generates the assembly code:
9445 \begin_inset LatexCommand \index{-\/-xstack}
9449 option is used the pdata memory area is followed by the xstack memory area
9450 and the sum of their sizes is limited to 256 bytes.
9451 \layout Subsubsection
9454 \begin_inset LatexCommand \index{code}
9459 \begin_inset LatexCommand \index{\_\_code}
9466 'Variables' declared with this storage class will be placed in the code
9472 __code unsigned char test_code;
9475 Read access to this variable generates the assembly code:
9480 90s00r6F\SpecialChar ~
9483 mov dptr,#_test_code
9512 indexed arrays of characters in code memory can be accessed efficiently:
9517 __code char test_array[] = {'c','h','e','a','p'};
9520 Read access to this array using an 8-bit unsigned index generates the assembly
9537 90s00r41\SpecialChar ~
9540 mov dptr,#_test_array
9555 \layout Subsubsection
9558 \begin_inset LatexCommand \index{bit}
9563 \begin_inset LatexCommand \index{\_\_bit}
9570 This is a data-type and a storage class specifier.
9571 When a variable is declared as a bit, it is allocated into the bit addressable
9572 memory of 8051, e.g.:
9580 Writing 1 to this variable generates the assembly code:
9596 The bit addressable memory consists of 128 bits which are located from 0x20
9597 to 0x2f in data memory.
9600 Apart from this 8051 specific storage class most architectures support ANSI-C
9602 \begin_inset LatexCommand \index{bitfields}
9612 Not really meant as examples, but nevertheless showing what bitfields are
9613 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9617 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9618 signed modifier are implemented as unsigned.
9619 \layout Subsubsection
9622 \begin_inset LatexCommand \index{sfr}
9627 \begin_inset LatexCommand \index{\_\_sfr}
9632 \begin_inset LatexCommand \index{sfr16}
9637 \begin_inset LatexCommand \index{\_\_sfr16}
9642 \begin_inset LatexCommand \index{sfr32}
9647 \begin_inset LatexCommand \index{\_\_sfr32}
9652 \begin_inset LatexCommand \index{\_\_sbit}
9659 Like the bit keyword,
9661 sfr / sfr16 / sfr32 / sbit
9663 signify both a data-type and storage class, they are used to describe the
9684 variables of a 8051, eg:
9690 \begin_inset LatexCommand \index{at}
9695 \begin_inset LatexCommand \index{\_\_at}
9699 (0x80) P0;\SpecialChar ~
9700 /* special function register P0 at location 0x80 */
9702 /* 16 bit special function register combination for timer 0 */
9704 /* with the high byte at location 0x8C and the low byte at location 0x8A
9708 \begin_inset LatexCommand \index{at}
9713 \begin_inset LatexCommand \index{\_\_at}
9719 __sbit __at (0xd7) CY; /* CY (Carry Flag
9720 \begin_inset LatexCommand \index{Flags}
9725 \begin_inset LatexCommand \index{Carry flag}
9732 Special function registers which are located on an address dividable by
9733 8 are bit-addressable, an
9737 addresses a specific bit within these sfr.
9739 16 Bit and 32 bit special function register combinations which require a
9740 certain access order are better not declared using
9749 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9750 this is not guaranteed.
9751 \layout Subsubsection
9754 \begin_inset LatexCommand \index{Pointer}
9758 to MCS51/DS390 specific memory spaces
9761 SDCC allows (via language extensions) pointers to explicitly point to any
9762 of the memory spaces
9763 \begin_inset LatexCommand \index{Memory model}
9768 In addition to the explicit pointers, the compiler uses (by default) generic
9769 pointers which can be used to point to any of the memory spaces.
9773 Pointer declaration examples:
9778 /* pointer physically in internal ram pointing to object in external ram
9781 __xdata unsigned char * __data p;
9785 /* pointer physically in external ram pointing to object in internal ram
9788 __data unsigned char * __xdata p;
9792 /* pointer physically in code rom pointing to data in xdata space */
9794 __xdata unsigned char * __code p;
9798 /* pointer physically in code space pointing to data in code space */
9800 __code unsigned char * __code p;
9804 /* the following is a generic pointer physically located in xdata space
9811 /* the following is a function pointer physically located in data space
9814 char (* __data fp)(void);
9817 Well you get the idea.
9822 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9835 The highest order byte of the
9839 pointers contains the data space information.
9840 Assembler support routines are called whenever data is stored or retrieved
9846 These are useful for developing reusable library
9847 \begin_inset LatexCommand \index{Libraries}
9852 Explicitly specifying the pointer type will generate the most efficient
9854 \layout Subsubsection
9856 Notes on MCS51 memory
9857 \begin_inset LatexCommand \index{MCS51 memory}
9864 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9865 RAM memory which is structured as follows:
9869 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9872 - Bytes 20-2F - 16 bytes to hold 128 bit
9873 \begin_inset LatexCommand \index{bit}
9879 - Bytes 30-7F - 80 bytes for general purpose use.
9884 Additionally some members of the MCS51 family may have up to 128 bytes of
9885 additional, indirectly addressable, internal RAM memory (
9890 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9895 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9900 Furthermore, some chips may have some built in external memory (
9905 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9910 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9914 ) which should not be confused with the internal, directly addressable RAM
9920 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9925 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9930 Sometimes this built in
9934 memory has to be activated before using it (you can probably find this
9935 information on the datasheet of the microcontroller your are using, see
9937 \begin_inset LatexCommand \ref{sub:Startup-Code}
9945 Normally SDCC will only use the first bank
9946 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9950 of registers (register bank 0), but it is possible to specify that other
9951 banks of registers (keyword
9958 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9963 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9969 ) should be used in interrupt
9970 \begin_inset LatexCommand \index{interrupt}
9975 \begin_inset LatexCommand \index{\_\_interrupt}
9980 By default, the compiler will place the stack after the last byte of allocated
9981 memory for variables.
9982 For example, if the first 2 banks of registers are used, and only four
9987 variables, it will position the base of the internal stack at address 20
9989 This implies that as the stack
9990 \begin_inset LatexCommand \index{stack}
9994 grows, it will use up the remaining register banks, and the 16 bytes used
9995 by the 128 bit variables, and 80 bytes for general purpose use.
9996 If any bit variables are used, the data variables will be placed in unused
9997 register banks and after the byte holding the last bit variable.
9998 For example, if register banks 0 and 1 are used, and there are 9 bit variables
10003 variables will be placed starting from address 0x10 to 0x20 and continue
10016 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
10020 to specify the start address of the
10035 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10039 to specify the size of the total internal RAM (
10053 By default the 8051 linker will place the stack after the last byte of (i)data
10066 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10070 allows you to specify the start of the stack, i.e.
10071 you could start it after any data in the general purpose area.
10072 If your microcontroller has additional indirectly addressable internal
10077 ) you can place the stack on it.
10078 You may also need to use -
10089 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10093 to set the start address of the external RAM (
10108 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10112 to specify its size.
10113 Same goes for the code memory, using -
10124 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10139 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10144 If in doubt, don't specify any options and see if the resulting memory
10145 layout is appropriate, then you can adjust it.
10148 The linker generates two files with memory allocation information.
10149 The first, with extension .map
10150 \begin_inset LatexCommand \index{<file>.map}
10154 shows all the variables and segments.
10155 The second with extension .mem
10156 \begin_inset LatexCommand \index{<file>.mem}
10160 shows the final memory layout.
10161 The linker will complain either if memory segments overlap, there is not
10162 enough memory, or there is not enough space for stack.
10163 If you get any linking warnings and/or errors related to stack or segments
10164 allocation, take a look at either the .map or .mem files to find out what
10166 The .mem file may even suggest a solution to the problem.
10169 Z80/Z180 Storage Class
10170 \begin_inset LatexCommand \index{Storage class}
10174 Language Extensions
10175 \layout Subsubsection
10178 \begin_inset LatexCommand \index{sfr}
10183 \begin_inset LatexCommand \index{\_\_sfr}
10187 (in/out to 8-bit addresses)
10191 \begin_inset LatexCommand \index{Z80}
10195 family has separate address spaces for memory and
10205 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10209 is accessed with special instructions, e.g.:
10214 sfr at 0x78 IoPort;\SpecialChar ~
10216 /* define a var in I/O space at 78h called IoPort */
10220 Writing 0x01 to this variable generates the assembly code:
10225 3E 01\SpecialChar ~
10233 D3 78\SpecialChar ~
10240 \layout Subsubsection
10243 \begin_inset LatexCommand \index{sfr}
10248 \begin_inset LatexCommand \index{\_\_sfr}
10252 (in/out to 16-bit addresses)
10259 is used to support 16 bit addresses in I/O memory e.g.:
10265 \begin_inset LatexCommand \index{at}
10270 \begin_inset LatexCommand \index{\_\_at}
10277 Writing 0x01 to this variable generates the assembly code:
10282 01 23 01\SpecialChar ~
10287 3E 01\SpecialChar ~
10295 ED 79\SpecialChar ~
10302 \layout Subsubsection
10305 \begin_inset LatexCommand \index{sfr}
10310 \begin_inset LatexCommand \index{\_\_sfr}
10314 (in0/out0 to 8 bit addresses on Z180
10315 \begin_inset LatexCommand \index{Z180}
10320 \begin_inset LatexCommand \index{HD64180}
10327 The compiler option -
10337 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
10339 \begin_inset LatexCommand \index{\#pragma portmode}
10343 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10353 If you include the file z180.h this will be set automatically.
10357 \begin_inset LatexCommand \index{Storage class}
10361 Language Extensions
10362 \layout Subsubsection
10365 \begin_inset LatexCommand \index{data (hc08 storage class)}
10370 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10377 The data storage class declares a variable that resides in the first 256
10378 bytes of memory (the direct page).
10379 The HC08 is most efficient at accessing variables (especially pointers)
10381 \layout Subsubsection
10384 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10389 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10396 The xdata storage class declares a variable that can reside anywhere in
10398 This is the default if no storage class is specified.
10402 Absolute Addressing
10403 \begin_inset LatexCommand \index{Absolute addressing}
10410 Data items can be assigned an absolute address with the
10413 \begin_inset LatexCommand \index{at}
10418 \begin_inset LatexCommand \index{\_\_at}
10424 keyword, in addition to a storage class, e.g.:
10430 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10435 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10440 \begin_inset LatexCommand \index{at}
10445 \begin_inset LatexCommand \index{\_\_at}
10449 (0x7ffe) unsigned int chksum;
10457 __xdata __at (0x7ffe) unsigned int chksum;
10460 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10461 of the external ram.
10466 reserve any space for variables declared in this way
10467 \begin_inset Marginal
10478 (they are implemented with an equate in the assembler).
10479 Thus it is left to the programmer to make sure there are no overlaps with
10480 other variables that are declared without the absolute address.
10481 The assembler listing file (.lst
10482 \begin_inset LatexCommand \index{<file>.lst}
10486 ) and the linker output files (.rst
10487 \begin_inset LatexCommand \index{<file>.rst}
10492 \begin_inset LatexCommand \index{<file>.map}
10496 ) are good places to look for such overlaps.
10497 Variables with an absolute address are
10500 \begin_inset Marginal
10514 \begin_inset LatexCommand \index{Variable initialization}
10521 In case of memory mapped I/O devices the keyword
10525 has to be used to tell the compiler that accesses might not be removed:
10531 \begin_inset LatexCommand \index{volatile}
10536 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10541 \begin_inset LatexCommand \index{at}
10545 (0x8000) unsigned char PORTA_8255;
10548 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10553 \begin_inset LatexCommand \index{Aligned array}
10560 starts at a block (256 byte) boundary
10561 \begin_inset LatexCommand \index{block boundary}
10566 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10572 Absolute addresses can be specified for variables in all storage classes,
10579 \begin_inset LatexCommand \index{bit}
10584 \begin_inset LatexCommand \index{at}
10591 The above example will allocate the variable at offset 0x02 in the bit-addressab
10593 There is no real advantage to assigning absolute addresses to variables
10594 in this manner, unless you want strict control over all the variables allocated.
10595 One possible use would be to write hardware portable code.
10596 For example, if you have a routine that uses one or more of the microcontroller
10597 I/O pins, and such pins are different for two different hardwares, you
10598 can declare the I/O pins in your routine using:
10604 \begin_inset LatexCommand \index{volatile}
10608 __bit MOSI;\SpecialChar ~
10612 /* master out, slave in */
10614 extern volatile __bit MISO;\SpecialChar ~
10618 /* master in, slave out */
10620 extern volatile __bit MCLK;\SpecialChar ~
10628 /* Input and Output of a byte on a 3-wire serial bus.
10633 If needed adapt polarity of clock, polarity of data and bit order
10638 unsigned char spi_io(unsigned char out_byte)
10662 MOSI = out_byte & 0x80;
10692 /* _asm nop _endasm; */\SpecialChar ~
10700 /* for slow peripherals */
10751 Then, someplace in the code for the first hardware you would use
10757 \begin_inset LatexCommand \index{at}
10762 \begin_inset LatexCommand \index{\_\_at}
10766 (0x80) MOSI;\SpecialChar ~
10770 /* I/O port 0, bit 0 */
10772 __bit __at (0x81) MISO;\SpecialChar ~
10776 /* I/O port 0, bit 1 */
10778 __bit __at (0x82) MCLK;\SpecialChar ~
10782 /* I/O port 0, bit 2 */
10785 Similarly, for the second hardware you would use
10790 __bit __at (0x83) MOSI;\SpecialChar ~
10794 /* I/O port 0, bit 3 */
10796 __bit __at (0x91) MISO;\SpecialChar ~
10800 /* I/O port 1, bit 1 */
10803 \begin_inset LatexCommand \index{bit}
10807 __at (0x92) MCLK;\SpecialChar ~
10811 /* I/O port 1, bit 2 */
10814 and you can use the same hardware dependent routine without changes, as
10815 for example in a library.
10816 This is somehow similar to sbit, but only one absolute address has to be
10817 specified in the whole project.
10821 \begin_inset LatexCommand \index{Parameters}
10826 \begin_inset LatexCommand \index{function parameter}
10831 \begin_inset LatexCommand \index{local variables}
10836 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10843 Automatic (local) variables and parameters to functions can either be placed
10844 on the stack or in data-space.
10845 The default action of the compiler is to place these variables in the internal
10846 RAM (for small model) or external RAM (for large model).
10847 This in fact makes them similar to
10850 \begin_inset LatexCommand \index{static}
10856 so by default functions are non-reentrant
10857 \begin_inset LatexCommand \index{reentrant}
10866 They can be placed on the stack
10867 \begin_inset LatexCommand \index{stack}
10884 \begin_inset LatexCommand \index{-\/-stack-auto}
10892 #pragma\SpecialChar ~
10896 \begin_inset LatexCommand \index{\#pragma stackauto}
10903 \begin_inset LatexCommand \index{reentrant}
10909 keyword in the function declaration, e.g.:
10914 unsigned char foo(char i) __reentrant
10928 Since stack space on 8051 is limited, the
10946 option should be used sparingly.
10947 Note that the reentrant keyword just means that the parameters & local
10948 variables will be allocated to the stack, it
10952 mean that the function is register bank
10953 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10962 \begin_inset LatexCommand \index{local variables}
10966 can be assigned storage classes and absolute
10967 \begin_inset LatexCommand \index{Absolute addressing}
10976 unsigned char foo()
10984 __xdata unsigned char i;
10997 \begin_inset LatexCommand \index{at}
11001 (0x31) unsigned char j;
11013 In the above example the variable
11017 will be allocated in the external ram,
11021 in bit addressable space and
11040 or when a function is declared as
11044 this should only be done for static variables.
11048 \begin_inset LatexCommand \index{function parameter}
11052 however are not allowed any storage class
11053 \begin_inset LatexCommand \index{Storage class}
11057 , (storage classes for parameters will be ignored), their allocation is
11058 governed by the memory model in use, and the reentrancy options.
11061 It is however allowed to use bit parameters in reentrant functions and also
11062 non-static local bit variables are supported.
11063 Efficient use is limited to 8 semi-bitregisters in bit space.
11064 They are pushed and popped to stack as a single byte just like the normal
11069 \begin_inset LatexCommand \label{sub:Overlaying}
11074 \begin_inset LatexCommand \index{Overlaying}
11082 \begin_inset LatexCommand \index{reentrant}
11086 functions SDCC will try to reduce internal ram space usage by overlaying
11087 parameters and local variables of a function (if possible).
11088 Parameters and local variables
11089 \begin_inset LatexCommand \index{local variables}
11093 of a function will be allocated to an overlayable segment if the function
11096 no other function calls and the function is non-reentrant and the memory
11098 \begin_inset LatexCommand \index{Memory model}
11105 If an explicit storage class
11106 \begin_inset LatexCommand \index{Storage class}
11110 is specified for a local variable, it will NOT be overlayed.
11113 Note that the compiler (not the linkage editor) makes the decision for overlayin
11115 Functions that are called from an interrupt service routine
11116 \begin_inset Marginal
11126 should be preceded by a #pragma\SpecialChar ~
11128 \begin_inset LatexCommand \index{\#pragma nooverlay}
11132 if they are not reentrant.
11135 Also note that the compiler does not do any processing of inline assembler
11136 code, so the compiler might incorrectly assign local variables and parameters
11137 of a function into the overlay segment if the inline assembler code calls
11138 other c-functions that might use the overlay.
11139 In that case the #pragma\SpecialChar ~
11140 nooverlay should be used.
11143 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11145 \begin_inset LatexCommand \index{Multiplication}
11150 \begin_inset LatexCommand \index{Division}
11154 will NOT be overlayed since these are implemented using external functions,
11163 \begin_inset LatexCommand \index{\#pragma nooverlay}
11169 void set_error(unsigned char errcd)
11185 void some_isr () __interrupt
11186 \begin_inset LatexCommand \index{interrupt}
11216 In the above example the parameter
11224 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11226 not present, this could cause unpredictable runtime behavior when called
11227 from an interrupt service routine.
11228 The #pragma\SpecialChar ~
11229 nooverlay ensures that the parameters and local variables for
11230 the function are NOT overlayed.
11233 Interrupt Service Routines
11234 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11241 General Information
11256 outines to be coded in C, with some extended keywords.
11261 void timer_isr (void) __interrupt (1) __using (1)
11275 The optional number following the
11278 \begin_inset LatexCommand \index{interrupt}
11283 \begin_inset LatexCommand \index{\_\_interrupt}
11289 keyword is the interrupt number this routine will service.
11290 When present, the compiler will insert a call to this routine in the interrupt
11291 vector table for the interrupt number specified.
11292 If you have multiple source files in your project, interrupt service routines
11293 can be present in any of them, but a prototype of the isr MUST be present
11294 or included in the file that contains the function
11302 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11307 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11313 keyword can be used to tell the compiler to use the specified register
11314 bank (8051 specific) when generating code for this function.
11320 Interrupt service routines open the door for some very interesting bugs:
11322 If an interrupt service routine changes variables which are accessed by
11323 other functions these variables have to be declared
11328 \begin_inset LatexCommand \index{volatile}
11336 If the access to these variables is not
11339 \begin_inset LatexCommand \index{atomic}
11346 the processor needs more than one instruction for the access and could
11347 be interrupted while accessing the variable) the interrupt must be disabled
11348 during the access to avoid inconsistent data.
11349 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
11350 and should be protected by disabling interrupts.
11351 You're not automatically on the safe side if you use 8 bit variables though.
11352 We need an example here: f.e.
11353 on the 8051 the harmless looking
11354 \begin_inset Quotes srd
11359 flags\SpecialChar ~
11364 \begin_inset Quotes sld
11373 \begin_inset Quotes srd
11378 flags\SpecialChar ~
11383 \begin_inset Quotes sld
11386 from within an interrupt routine might get lost if the interrupt occurs
11389 \begin_inset Quotes sld
11394 counter\SpecialChar ~
11399 \begin_inset Quotes srd
11402 is not atomic on the 8051 even if
11406 is located in data memory.
11407 Bugs like these are hard to reproduce and can cause a lot of trouble.
11411 The return address and the registers used in the interrupt service routine
11412 are saved on the stack
11413 \begin_inset LatexCommand \index{stack}
11417 so there must be sufficient stack space.
11418 If there isn't variables or registers (or even the return address itself)
11425 \begin_inset LatexCommand \index{stack overflow}
11429 is most likely to happen if the interrupt occurs during the
11430 \begin_inset Quotes sld
11434 \begin_inset Quotes srd
11437 subroutine when the stack is already in use for f.e.
11438 many return addresses.
11441 A special note here, int (16 bit) and long (32 bit) integer division
11442 \begin_inset LatexCommand \index{Division}
11447 \begin_inset LatexCommand \index{Multiplication}
11452 \begin_inset LatexCommand \index{Modulus}
11457 \begin_inset LatexCommand \index{Floating point support}
11461 operations are implemented using external support routines developed in
11463 If an interrupt service routine needs to do any of these operations then
11464 the support routines (as mentioned in a following section) will have to
11465 be recompiled using the
11478 \begin_inset LatexCommand \index{-\/-stack-auto}
11484 option and the source file will need to be compiled using the
11499 \begin_inset LatexCommand \index{-\/-int-long-reent}
11504 Note, the type promotion
11505 \begin_inset LatexCommand \index{type promotion}
11509 required by ANSI C can cause 16 bit routines to be used without the programmer
11514 Calling other functions from an interrupt service routine is not recommended,
11515 avoid it if possible.
11516 Note that when some function is called from an interrupt service routine
11517 it should be preceded by a #pragma\SpecialChar ~
11519 \begin_inset LatexCommand \index{\#pragma nooverlay}
11523 if it is not reentrant.
11524 Furthermore nonreentrant functions should not be called from the main program
11525 while the interrupt service routine might be active.
11526 They also must not be called from low priority interrupt service routines
11527 while a high priority interrupt service routine might be active.
11528 You could use semaphores or make the function
11532 if all parameters are passed in registers.
11537 \begin_inset LatexCommand \ref{sub:Overlaying}
11542 about Overlaying and section
11543 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11548 about Functions using private register banks.
11551 MCS51/DS390 Interrupt Service Routines
11554 Interrupt numbers and the corresponding address & descriptions for the Standard
11555 8051/8052 are listed below.
11556 SDCC will automatically adjust the interrupt vector table to the maximum
11557 interrupt number specified.
11563 \begin_inset Tabular
11564 <lyxtabular version="3" rows="9" columns="3">
11566 <column alignment="center" valignment="top" leftline="true" width="0in">
11567 <column alignment="left" valignment="top" leftline="true" width="0in">
11568 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
11569 <row topline="true" bottomline="true">
11570 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11578 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11586 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11595 <row topline="true">
11596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11604 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11612 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11621 <row topline="true">
11622 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11638 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11647 <row topline="true">
11648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11656 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11664 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11673 <row topline="true">
11674 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11682 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11690 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11699 <row topline="true">
11700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11725 <row topline="true">
11726 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11742 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11751 <row topline="true">
11752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11760 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11776 <row topline="true" bottomline="true">
11777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11792 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11810 If the interrupt service routine is defined without
11813 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11818 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11824 a register bank or with register bank 0 (
11828 0), the compiler will save the registers used by itself on the stack upon
11829 entry and restore them at exit, however if such an interrupt service routine
11830 calls another function then the entire register bank will be saved on the
11832 This scheme may be advantageous for small interrupt service routines which
11833 have low register usage.
11836 If the interrupt service routine is defined to be using a specific register
11841 & psw are saved and restored, if such an interrupt service routine calls
11842 another function (using another register bank) then the entire register
11843 bank of the called function will be saved on the stack.
11844 This scheme is recommended for larger interrupt service routines.
11847 HC08 Interrupt Service Routines
11850 Since the number of interrupts available is chip specific and the interrupt
11851 vector table always ends at the last byte of memory, the interrupt numbers
11852 corresponds to the interrupt vectors in reverse order of address.
11853 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11854 2 will use the interrupt vector at 0xfffa, and so on.
11855 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11856 this way; instead see section
11857 \begin_inset LatexCommand \ref{sub:Startup-Code}
11861 for details on customizing startup.
11864 Z80 Interrupt Service Routines
11867 The Z80 uses several different methods for determining the correct interrupt
11868 vector depending on the hardware implementation.
11869 Therefore, SDCC ignores the optional interrupt number and does not attempt
11870 to generate an interrupt vector table.
11873 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11874 instruction to return from the interrupt.
11875 To write an interrupt handler for the non-maskable interrupt, which needs
11876 a RETN instruction instead, add the
11885 void nmi_isr (void) critical interrupt
11899 However if you need to create a non-interruptable interrupt service routine
11900 you would also require the
11905 To distinguish between this and an nmi_isr you must provide an interrupt
11909 Enabling and Disabling Interrupts
11912 Critical Functions and Critical Statements
11915 A special keyword may be associated with a block or a function declaring
11921 SDCC will generate code to disable all interrupts
11922 \begin_inset LatexCommand \index{interrupt}
11926 upon entry to a critical function and restore the interrupt enable to the
11927 previous state before returning.
11928 Nesting critical functions will need one additional byte on the stack
11929 \begin_inset LatexCommand \index{stack}
11938 int foo () __critical
11939 \begin_inset LatexCommand \index{critical}
11944 \begin_inset LatexCommand \index{\_\_critical}
11969 The critical attribute maybe used with other attributes like
11979 may also be used to disable interrupts more locally:
11987 More than one statement could have been included in the block.
11990 Enabling and Disabling Interrupts directly
11994 \begin_inset LatexCommand \index{interrupt}
11998 can also be disabled and enabled directly (8051):
12003 EA = 0;\SpecialChar ~
12066 EA = 1;\SpecialChar ~
12133 On other architectures which have seperate opcodes for enabling and disabling
12134 interrupts you might want to make use of defines with inline assembly
12135 \begin_inset LatexCommand \index{Assembler routines}
12145 \begin_inset LatexCommand \index{\_asm}
12154 \begin_inset LatexCommand \index{\_endasm}
12163 #define SEI _asm\SpecialChar ~
12175 Note: it is sometimes sufficient to disable only a specific interrupt source
12177 a timer or serial interrupt by manipulating an
12180 \begin_inset LatexCommand \index{interrupt mask}
12190 Usually the time during which interrupts are disabled should be kept as
12192 This minimizes both
12197 \begin_inset LatexCommand \index{interrupt latency}
12201 (the time between the occurrence of the interrupt and the execution of
12202 the first code in the interrupt routine) and
12207 \begin_inset LatexCommand \index{interrupt jitter}
12211 (the difference between the shortest and the longest interrupt latency).
12212 These really are something different, f.e.
12213 a serial interrupt has to be served before its buffer overruns so it cares
12214 for the maximum interrupt latency, whereas it does not care about jitter.
12215 On a loudspeaker driven via a digital to analog converter which is fed
12216 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12217 a much smaller jitter will be very audible.
12220 You can reenable interrupts within an interrupt routine and on some architecture
12221 s you can make use of two (or more) levels of
12223 interrupt priorities
12226 \begin_inset LatexCommand \index{interrupt priority}
12231 On some architectures which don't support interrupt priorities these can
12232 be implemented by manipulating the interrupt mask and reenabling interrupts
12233 within the interrupt routine.
12234 Check there is sufficient space on the stack
12235 \begin_inset LatexCommand \index{stack}
12239 and don't add complexity unless you have to.
12244 \begin_inset LatexCommand \index{semaphore}
12248 locking (mcs51/ds390)
12251 Some architectures (mcs51/ds390) have an atomic
12252 \begin_inset LatexCommand \index{atomic}
12265 These type of instructions are typically used in preemptive multitasking
12266 systems, where a routine f.e.
12267 claims the use of a data structure ('acquires a lock
12268 \begin_inset LatexCommand \index{lock}
12272 on it'), makes some modifications and then releases the lock when the data
12273 structure is consistent again.
12274 The instruction may also be used if interrupt and non-interrupt code have
12275 to compete for a resource.
12276 With the atomic bit test and clear instruction interrupts
12277 \begin_inset LatexCommand \index{interrupt}
12281 don't have to be disabled for the locking operation.
12285 SDCC generates this instruction if the source follows this pattern:
12291 \begin_inset LatexCommand \index{volatile}
12295 bit resource_is_free;
12299 if (resource_is_free)
12309 resource_is_free=0;
12322 resource_is_free=1;
12329 Note, mcs51 and ds390 support only an atomic
12330 \begin_inset LatexCommand \index{atomic}
12338 instruction (as opposed to atomic bit test and
12343 Functions using private register banks
12344 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12351 Some architectures have support for quickly changing register sets.
12352 SDCC supports this feature with the
12355 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12360 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12366 attribute (which tells the compiler to use a register bank
12367 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12371 other than the default bank zero).
12372 It should only be applied to
12375 \begin_inset LatexCommand \index{interrupt}
12381 functions (see footnote below).
12382 This will in most circumstances make the generated ISR code more efficient
12383 since it will not have to save registers on the stack.
12390 attribute will have no effect on the generated code for a
12394 function (but may occasionally be useful anyway
12400 possible exception: if a function is called ONLY from 'interrupt' functions
12401 using a particular bank, it can be declared with the same 'using' attribute
12402 as the calling 'interrupt' functions.
12403 For instance, if you have several ISRs using bank one, and all of them
12404 call memcpy(), it might make sense to create a specialized version of memcpy()
12405 'using 1', since this would prevent the ISR from having to save bank zero
12406 to the stack on entry and switch to bank zero before calling the function
12413 (pending: I don't think this has been done yet)
12420 function using a non-zero bank will assume that it can trash that register
12421 bank, and will not save it.
12422 Since high-priority interrupts
12423 \begin_inset LatexCommand \index{interrupts}
12428 \begin_inset LatexCommand \index{interrupt priority}
12432 can interrupt low-priority ones on the 8051 and friends, this means that
12433 if a high-priority ISR
12437 a particular bank occurs while processing a low-priority ISR
12441 the same bank, terrible and bad things can happen.
12442 To prevent this, no single register bank should be
12446 by both a high priority and a low priority ISR.
12447 This is probably most easily done by having all high priority ISRs use
12448 one bank and all low priority ISRs use another.
12449 If you have an ISR which can change priority at runtime, you're on your
12450 own: I suggest using the default bank zero and taking the small performance
12454 It is most efficient if your ISR calls no other functions.
12455 If your ISR must call other functions, it is most efficient if those functions
12456 use the same bank as the ISR (see note 1 below); the next best is if the
12457 called functions use bank zero.
12458 It is very inefficient to call a function using a different, non-zero bank
12464 \begin_inset LatexCommand \label{sub:Startup-Code}
12469 \begin_inset LatexCommand \index{Startup code}
12476 MCS51/DS390 Startup Code
12479 The compiler inserts a call to the C routine
12481 _sdcc_external_startup()
12482 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12491 at the start of the CODE area.
12492 This routine is in the runtime library
12493 \begin_inset LatexCommand \index{Runtime library}
12498 By default this routine returns 0, if this routine returns a non-zero value,
12499 the static & global variable initialization will be skipped and the function
12500 main will be invoked.
12501 Otherwise static & global variables will be initialized before the function
12505 _sdcc_external_startup()
12507 routine to your program to override the default if you need to setup hardware
12508 or perform some other critical operation prior to static & global variable
12510 \begin_inset LatexCommand \index{Variable initialization}
12515 On some mcs51 variants xdata
12516 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12520 memory has to be explicitly enabled before it can be accessed or if the
12521 watchdog needs to be disabled, this is the place to do it.
12522 The startup code clears all internal data memory, 256 bytes by default,
12523 but from 0 to n-1 if
12536 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12543 (recommended for Chipcon CC1010).
12546 See also the compiler option
12565 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12570 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12575 about MCS51-variants.
12581 The HC08 startup code follows the same scheme as the MCS51 startup code.
12587 On the Z80 the startup code is inserted by linking with crt0.o which is generated
12588 from sdcc/device/lib/z80/crt0.s.
12589 If you need a different startup code you can use the compiler option
12610 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12614 and provide your own crt0.o.
12618 Inline Assembler Code
12619 \begin_inset LatexCommand \index{Assembler routines}
12626 A Step by Step Introduction
12627 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12634 Starting from a small snippet of c-code this example shows for the MCS51
12635 how to use inline assembly, access variables, a function parameter and
12636 an array in xdata memory.
12637 The example uses an MCS51 here but is easily adapted for other architectures.
12638 This is a buffer routine which should be optimized:
12644 unsigned char __far
12645 \begin_inset LatexCommand \index{far (storage class)}
12650 \begin_inset LatexCommand \index{\_\_far (storage class)}
12655 \begin_inset LatexCommand \index{at}
12660 \begin_inset LatexCommand \index{\_\_at}
12664 (0x7f00) buf[0x100];
12665 \begin_inset LatexCommand \index{Aligned array}
12671 unsigned char head, tail;
12675 void to_buffer( unsigned char c )
12683 if( head != (unsigned char)(tail-1) )\SpecialChar ~
12689 \begin_inset LatexCommand \index{promotion to signed int}
12694 \begin_inset LatexCommand \index{type promotion}
12699 \begin_inset Marginal
12720 buf[ head++ ] = c;\SpecialChar ~
12736 /* access to a 256 byte aligned array */
12741 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12742 then a corresponding buffer.asm file is generated.
12743 We define a new function
12747 in file buffer.c in which we cut and paste the generated code, removing
12748 unwanted comments and some ':'.
12750 \begin_inset Quotes sld
12754 \begin_inset Quotes srd
12758 \begin_inset Quotes sld
12762 \begin_inset Quotes srd
12765 to the beginning and the end of the function body:
12771 /* With a cut and paste from the .asm file, we have something to start with.
12776 The function is not yet OK! (registers aren't saved) */
12778 void to_buffer_asm( unsigned char c )
12787 \begin_inset LatexCommand \index{\_asm}
12792 \begin_inset LatexCommand \index{\_\_asm}
12806 ;buffer.c if( head != (unsigned char)(tail-1) )
12854 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12855 \begin_inset LatexCommand \index{Aligned array}
12920 \begin_inset LatexCommand \index{\_endasm}
12925 \begin_inset LatexCommand \index{\_\_endasm}
12934 The new file buffer.c should compile with only one warning about the unreferenced
12935 function argument 'c'.
12936 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12937 (1) and finally have:
12943 unsigned char __far __at(0x7f00) buf[0x100];
12945 unsigned char head, tail;
12947 #define USE_ASSEMBLY (1)
12955 void to_buffer( unsigned char c )
12963 if( head != (unsigned char)(tail-1) )
12983 void to_buffer( unsigned char c )
12991 c; // to avoid warning: unreferenced function argument
12998 \begin_inset LatexCommand \index{\_asm}
13003 \begin_inset LatexCommand \index{\_\_asm}
13017 ; save used registers here.
13028 ; If we were still using r2,r3 we would have to push them here.
13031 ; if( head != (unsigned char)(tail-1) )
13074 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
13098 ; buf[ head++ ] = c;
13109 a,dpl \SpecialChar ~
13116 ; dpl holds lower byte of function argument
13127 dpl,_head \SpecialChar ~
13130 ; buf is 0x100 byte aligned so head can be used directly
13172 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13184 ; restore used registers here
13191 \begin_inset LatexCommand \index{\_endasm}
13196 \begin_inset LatexCommand \index{\_\_endasm}
13207 The inline assembler code can contain any valid code understood by the assembler
13208 , this includes any assembler directives and comment lines
13214 The assembler does not like some characters like ':' or ''' in comments.
13215 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13216 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13221 \begin_inset LatexCommand \index{Assembler documentation}
13229 The compiler does not do any validation of the code within the
13232 \begin_inset LatexCommand \index{\_asm}
13237 \begin_inset LatexCommand \index{\_\_asm}
13245 \begin_inset LatexCommand \index{\_endasm}
13250 \begin_inset LatexCommand \index{\_\_endasm}
13259 Specifically it will not know which registers are used and thus register
13261 \begin_inset LatexCommand \index{push/pop}
13265 has to be done manually.
13269 It is recommended that each assembly instruction (including labels) be placed
13270 in a separate line (as the example shows).
13284 \begin_inset LatexCommand \index{-\/-peep-asm}
13290 command line option is used, the inline assembler code will be passed through
13291 the peephole optimizer
13292 \begin_inset LatexCommand \index{Peephole optimizer}
13297 There are only a few (if any) cases where this option makes sense, it might
13298 cause some unexpected changes in the inline assembler code.
13299 Please go through the peephole optimizer rules defined in file
13303 before using this option.
13307 \begin_inset LatexCommand \label{sub:Naked-Functions}
13312 \begin_inset LatexCommand \index{Naked functions}
13319 A special keyword may be associated with a function declaring it as
13322 \begin_inset LatexCommand \index{\_naked}
13327 \begin_inset LatexCommand \index{\_\_naked}
13338 function modifier attribute prevents the compiler from generating prologue
13339 \begin_inset LatexCommand \index{function prologue}
13344 \begin_inset LatexCommand \index{function epilogue}
13348 code for that function.
13349 This means that the user is entirely responsible for such things as saving
13350 any registers that may need to be preserved, selecting the proper register
13351 bank, generating the
13355 instruction at the end, etc.
13356 Practically, this means that the contents of the function must be written
13357 in inline assembler.
13358 This is particularly useful for interrupt functions, which can have a large
13359 (and often unnecessary) prologue/epilogue.
13360 For example, compare the code generated by these two functions:
13366 \begin_inset LatexCommand \index{volatile}
13370 data unsigned char counter;
13374 void simpleInterrupt(void) __interrupt
13375 \begin_inset LatexCommand \index{interrupt}
13380 \begin_inset LatexCommand \index{\_\_interrupt}
13398 void nakedInterrupt(void) __interrupt (2) __naked
13407 \begin_inset LatexCommand \index{\_asm}
13412 \begin_inset LatexCommand \index{\_\_asm}
13429 _counter ; does not change flags, no need to save psw
13441 ; MUST explicitly include ret or reti in _naked function.
13448 \begin_inset LatexCommand \index{\_endasm}
13453 \begin_inset LatexCommand \index{\_\_endasm}
13462 For an 8051 target, the generated simpleInterrupt looks like:
13471 example, recent versions of SDCC generate
13473 the same code for simpleInterrupt() and nakedInterrupt()!
13613 whereas nakedInterrupt looks like:
13628 _counter ; does not change flags, no need to save psw
13646 ; MUST explicitly include ret or reti in _naked function
13649 The related directive #pragma exclude
13650 \begin_inset LatexCommand \index{\#pragma exclude}
13654 allows a more fine grained control over pushing & popping
13655 \begin_inset LatexCommand \index{push/pop}
13662 While there is nothing preventing you from writing C code inside a
13666 function, there are many ways to shoot yourself in the foot doing this,
13667 and it is recommended that you stick to inline assembler.
13670 Use of Labels within Inline Assembler
13673 SDCC allows the use of in-line assembler with a few restrictions regarding
13675 In older versions of the compiler all labels defined within inline assembler
13684 where nnnn is a number less than 100 (which implies a limit of utmost 100
13685 inline assembler labels
13699 \begin_inset LatexCommand \index{\_asm}
13704 \begin_inset LatexCommand \index{\_\_asm}
13734 \begin_inset LatexCommand \index{\_endasm}
13739 \begin_inset LatexCommand \index{\_\_endasm}
13746 Inline assembler code cannot reference any C-Labels, however it can reference
13748 \begin_inset LatexCommand \index{Labels}
13752 defined by the inline assembler, e.g.:
13777 ; some assembler code
13797 /* some more c code */
13799 clabel:\SpecialChar ~
13801 /* inline assembler cannot reference this label */
13813 $0003: ;label (can be referenced by inline assembler only)
13820 \begin_inset LatexCommand \index{\_endasm}
13825 \begin_inset LatexCommand \index{\_\_endasm}
13835 /* some more c code */
13840 In other words inline assembly code can access labels defined in inline
13841 assembly within the scope of the function.
13842 The same goes the other way, i.e.
13843 labels defines in inline assembly can not be accessed by C statements.
13846 Interfacing with Assembler Code
13847 \begin_inset LatexCommand \index{Assembler routines}
13854 Global Registers used for Parameter Passing
13855 \begin_inset LatexCommand \index{Parameter passing}
13862 The compiler always uses the global registers
13865 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13870 \begin_inset LatexCommand \index{DPTR}
13875 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13884 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13890 to pass the first parameter to a routine.
13891 The second parameter onwards is either allocated on the stack (for reentrant
13902 -stack-auto is used) or in data / xdata memory (depending on the memory
13907 Assembler Routine (non-reentrant)
13910 In the following example
13911 \begin_inset LatexCommand \index{reentrant}
13916 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13920 the function c_func calls an assembler routine asm_func, which takes two
13922 \begin_inset LatexCommand \index{function parameter}
13931 extern int asm_func(unsigned char, unsigned char);
13935 int c_func (unsigned char i, unsigned char j)
13943 return asm_func(i,j);
13957 return c_func(10,9);
13962 The corresponding assembler function is:
13967 .globl _asm_func_PARM_2
14068 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14085 Note here that the return values
14086 \begin_inset LatexCommand \index{return value}
14090 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
14092 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
14093 b' & 'acc' for four byte values.
14096 The parameter naming convention is _<function_name>_PARM_<n>, where n is
14097 the parameter number starting from 1, and counting from the left.
14098 The first parameter is passed in
14099 \begin_inset Quotes eld
14103 \begin_inset Quotes erd
14106 for a one byte parameter,
14107 \begin_inset Quotes eld
14111 \begin_inset Quotes erd
14115 \begin_inset Quotes eld
14119 \begin_inset Quotes erd
14122 for three bytes and
14123 \begin_inset Quotes eld
14127 \begin_inset Quotes erd
14130 for a four bytes parameter.
14131 The variable name for the second parameter will be _<function_name>_PARM_2.
14135 Assemble the assembler routine with the following command:
14142 asx8051 -losg asmfunc.asm
14149 Then compile and link the assembler routine to the C source file with the
14157 sdcc cfunc.c asmfunc.rel
14160 Assembler Routine (reentrant)
14164 \begin_inset LatexCommand \index{reentrant}
14169 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
14173 the second parameter
14174 \begin_inset LatexCommand \index{function parameter}
14178 onwards will be passed on the stack, the parameters are pushed from right
14180 after the call the leftmost parameter will be on the top of the stack.
14181 Here is an example:
14186 extern int asm_func(unsigned char, unsigned char);
14190 int c_func (unsigned char i, unsigned char j) reentrant
14198 return asm_func(i,j);
14212 return c_func(10,9);
14217 The corresponding assembler routine is:
14317 The compiling and linking procedure remains the same, however note the extra
14318 entry & exit linkage required for the assembler code, _bp is the stack
14319 frame pointer and is used to compute the offset into the stack for parameters
14320 and local variables.
14324 \begin_inset LatexCommand \index{int (16 bit)}
14329 \begin_inset LatexCommand \index{long (32 bit)}
14336 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14337 multiplication and modulus operations are implemented by support routines.
14338 These support routines are all developed in ANSI-C to facilitate porting
14339 to other MCUs, although some model specific assembler optimizations are
14341 The following files contain the described routines, all of them can be
14342 found in <installdir>/share/sdcc/lib.
14348 \begin_inset Tabular
14349 <lyxtabular version="3" rows="11" columns="2">
14351 <column alignment="left" valignment="top" leftline="true" width="0">
14352 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14353 <row topline="true" bottomline="true">
14354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14364 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14375 <row topline="true">
14376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14384 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14389 16 bit multiplication
14393 <row topline="true">
14394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14402 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14407 signed 16 bit division (calls _divuint)
14411 <row topline="true">
14412 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14420 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14425 unsigned 16 bit division
14429 <row topline="true">
14430 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14438 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14443 signed 16 bit modulus (calls _moduint)
14447 <row topline="true">
14448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14456 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14461 unsigned 16 bit modulus
14465 <row topline="true">
14466 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14479 32 bit multiplication
14483 <row topline="true">
14484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14497 signed 32 division (calls _divulong)
14501 <row topline="true">
14502 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14510 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14515 unsigned 32 division
14519 <row topline="true">
14520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14533 signed 32 bit modulus (calls _modulong)
14537 <row topline="true" bottomline="true">
14538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14551 unsigned 32 bit modulus
14564 Since they are compiled as
14569 \begin_inset LatexCommand \index{reentrant}
14574 \begin_inset LatexCommand \index{interrupt}
14578 service routines should not do any of the above operations.
14579 If this is unavoidable then the above routines will need to be compiled
14593 \begin_inset LatexCommand \index{-\/-stack-auto}
14599 option, after which the source program will have to be compiled with
14612 \begin_inset LatexCommand \index{-\/-int-long-reent}
14619 Notice that you don't have to call these routines directly.
14620 The compiler will use them automatically every time an integer operation
14624 Floating Point Support
14625 \begin_inset LatexCommand \index{Floating point support}
14632 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
14633 floating point support routines are derived from gcc's floatlib.c and consist
14634 of the following routines:
14642 \begin_inset Tabular
14643 <lyxtabular version="3" rows="17" columns="2">
14645 <column alignment="left" valignment="top" leftline="true" width="0">
14646 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14647 <row topline="true" bottomline="true">
14648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14674 <row topline="true">
14675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14692 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14706 add floating point numbers
14710 <row topline="true">
14711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14742 subtract floating point numbers
14746 <row topline="true">
14747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14764 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14778 divide floating point numbers
14782 <row topline="true">
14783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14814 multiply floating point numbers
14818 <row topline="true">
14819 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14850 convert floating point to unsigned char
14854 <row topline="true">
14855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14886 convert floating point to signed char
14890 <row topline="true">
14891 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14922 convert floating point to unsigned int
14926 <row topline="true">
14927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14958 convert floating point to signed int
14962 <row topline="true">
14963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14989 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15003 convert floating point to unsigned long
15007 <row topline="true">
15008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15025 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15039 convert floating point to signed long
15043 <row topline="true">
15044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15075 convert unsigned char to floating point
15079 <row topline="true">
15080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15097 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15111 convert char to floating point number
15115 <row topline="true">
15116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15147 convert unsigned int to floating point
15151 <row topline="true">
15152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15169 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15183 convert int to floating point numbers
15187 <row topline="true">
15188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15219 convert unsigned long to floating point number
15223 <row topline="true" bottomline="true">
15224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15255 convert long to floating point number
15268 These support routines are developed in ANSI-C so there is room for space
15269 and speed improvement
15275 These floating point routines (
15279 sinf(), cosf(), ...) for the mcs51 are implemented in assembler.
15284 Note if all these routines are used simultaneously the data space might
15286 For serious floating point usage the large model might be needed.
15287 Also notice that you don't have to call this routines directly.
15288 The compiler will use them automatically every time a floating point operation
15293 \begin_inset LatexCommand \index{Libraries}
15302 <pending: this is messy and incomplete - a little more information is in
15303 sdcc/doc/libdoc.txt
15308 Compiler support routines (_gptrget, _mulint etc.)
15311 Stdclib functions (puts, printf, strcat etc.)
15312 \layout Subsubsection
15318 \begin_inset LatexCommand \index{<stdio.h>}
15322 As usual on embedded systems you have to provide your own
15325 \begin_inset LatexCommand \index{getchar()}
15334 \begin_inset LatexCommand \index{putchar()}
15341 SDCC does not know whether the system connects to a serial line with or
15342 without handshake, LCD, keyboard or other device.
15356 You'll find examples for serial routines f.e.
15357 in sdcc/device/lib.
15363 \begin_inset LatexCommand \index{printf()}
15373 does not support float (except on ds390).
15374 To enable this recompile it with the option
15387 \begin_inset LatexCommand \index{USE\_FLOATS}
15393 on the command line.
15407 \begin_inset LatexCommand \index{-\/-model-large}
15413 for the mcs51 port, since this uses a lot of memory.
15416 If you're short on memory you might want to use
15419 \begin_inset LatexCommand \index{printf\_small()}
15434 For the mcs51 there additionally are assembly versions
15437 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15443 (subset of printf using less than 270 bytes) and
15446 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15455 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15461 (floating-point aware version of printf_fast) which should fit the requirements
15462 of many embedded systems (printf_fast() can be customized by unsetting
15467 support long variables and field widths).
15470 Math functions (sinf, powf, sqrtf etc.)
15471 \layout Subsubsection
15476 See definitions in file <math.h>.
15483 \begin_inset LatexCommand \index{Libraries}
15487 included in SDCC should have a license at least as liberal as the GNU Lesser
15488 General Public License
15489 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
15500 license statements for the libraries are missing.
15501 sdcc/device/lib/ser_ir.c
15505 come with a GPL (as opposed to LGPL) License - this will not be liberal
15506 enough for many embedded programmers.
15509 If you have ported some library or want to share experience about some code
15511 falls into any of these categories Busses (I
15512 \begin_inset Formula $^{\textrm{2}}$
15515 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
15516 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
15517 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
15518 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
15523 would certainly like to hear about it.
15524 Programmers coding for embedded systems are not especially famous for being
15525 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
15526 e these references are very valuable.
15527 Let's help to create a climate where information is shared.
15533 MCS51 Memory Models
15534 \begin_inset LatexCommand \index{Memory model}
15539 \begin_inset LatexCommand \index{MCS51 memory model}
15544 \layout Subsubsection
15546 Small, Medium and Large
15549 SDCC allows three memory models for MCS51 code,
15558 Modules compiled with different memory models should
15562 be combined together or the results would be unpredictable.
15563 The library routines supplied with the compiler are compiled as small,
15565 The compiled library modules are contained in separate directories as small,
15566 medium and large so that you can link to the appropriate set.
15569 When the medium or large model is used all variables declared without a
15570 storage class will be allocated into the external ram, this includes all
15571 parameters and local variables (for non-reentrant
15572 \begin_inset LatexCommand \index{reentrant}
15577 When the small model is used variables without storage class are allocated
15578 in the internal ram.
15581 Judicious usage of the processor specific storage classes
15582 \begin_inset LatexCommand \index{Storage class}
15586 and the 'reentrant' function type will yield much more efficient code,
15587 than using the large model.
15588 Several optimizations are disabled when the program is compiled using the
15589 large model, it is therefore recommended that the small model be used unless
15590 absolutely required.
15591 \layout Subsubsection
15594 \begin_inset LatexCommand \label{sub:External-Stack}
15599 \begin_inset LatexCommand \index{stack}
15604 \begin_inset LatexCommand \index{External stack (mcs51)}
15611 The external stack (-
15622 \begin_inset LatexCommand \index{-\/-xstack}
15626 ) is located in pdata
15627 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15631 memory (usually at the start of the external ram segment) and uses all
15632 unused space in pdata (max.
15644 -xstack option is used to compile the program, the parameters and local
15646 \begin_inset LatexCommand \index{local variables}
15650 of all reentrant functions are allocated in this area.
15651 This option is provided for programs with large stack space requirements.
15652 When used with the -
15663 \begin_inset LatexCommand \index{-\/-stack-auto}
15667 option, all parameters and local variables are allocated on the external
15668 stack (note: support libraries will need to be recompiled with the same
15670 There is a predefined target in the library makefile).
15673 The compiler outputs the higher order address byte of the external ram segment
15675 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15680 \begin_inset LatexCommand \ref{sub:MCS51-variants}
15684 ), therefore when using the External Stack option, this port
15688 be used by the application program.
15692 \begin_inset LatexCommand \index{Memory model}
15697 \begin_inset LatexCommand \index{DS390 memory model}
15704 The only model supported is Flat 24
15705 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15710 This generates code for the 24 bit contiguous addressing mode of the Dallas
15712 In this mode, up to four meg of external RAM or code space can be directly
15714 See the data sheets at www.dalsemi.com for further information on this part.
15718 Note that the compiler does not generate any code to place the processor
15719 into 24 bitmode (although
15723 in the ds390 libraries will do that for you).
15729 \begin_inset LatexCommand \index{Tinibios (DS390)}
15733 , the boot loader or similar code must ensure that the processor is in 24
15734 bit contiguous addressing mode before calling the SDCC startup code.
15752 option, variables will by default be placed into the XDATA segment.
15757 Segments may be placed anywhere in the 4 meg address space using the usual
15769 Note that if any segments are located above 64K, the -r flag must be passed
15770 to the linker to generate the proper segment relocations, and the Intel
15771 HEX output format must be used.
15772 The -r flag can be passed to the linker by using the option
15776 on the SDCC command line.
15777 However, currently the linker can not handle code segments > 64k.
15781 \begin_inset LatexCommand \index{Pragmas}
15788 SDCC supports the following #pragma directives:
15792 \begin_inset LatexCommand \index{\#pragma save}
15796 - this will save all current options to the save/restore stack.
15797 See #pragma\SpecialChar ~
15802 \begin_inset LatexCommand \index{\#pragma restore}
15806 - will restore saved options from the last save.
15807 saves & restores can be nested.
15808 SDCC uses a save/restore stack: save pushes current options to the stack,
15809 restore pulls current options from the stack.
15810 See #pragma\SpecialChar ~
15817 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15822 \begin_inset LatexCommand \index{function prologue}
15826 function1[,function2[,function3...]] - The compiler by default uses a caller
15827 saves convention for register saving across function calls, however this
15828 can cause unnecessary register pushing & popping
15829 \begin_inset LatexCommand \index{push/pop}
15833 when calling small functions from larger functions.
15834 This option can be used to switch off the register saving convention for
15835 the function names specified.
15836 The compiler will not save registers when calling these functions, extra
15837 code need to be manually inserted at the entry & exit for these functions
15838 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15839 reduce code & improve run time performance of the generated code.
15840 In the future the compiler (with inter procedural analysis) may be able
15841 to determine the appropriate scheme to use for each function call.
15852 -callee-saves command line option is used, the function names specified
15853 in #pragma\SpecialChar ~
15855 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15859 is appended to the list of functions specified in the command line.
15863 \begin_inset LatexCommand \index{\#pragma exclude}
15867 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15868 of pairs of push/pop
15869 \begin_inset LatexCommand \index{push/pop}
15878 \begin_inset LatexCommand \index{interrupt}
15891 The directive should be placed immediately before the ISR function definition
15892 and it affects ALL ISR functions following it.
15893 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15894 exclude\SpecialChar ~
15896 \begin_inset LatexCommand \index{\#pragma exclude}
15901 See also the related keyword _naked
15902 \begin_inset LatexCommand \index{\_naked}
15907 \begin_inset LatexCommand \index{\_\_naked}
15915 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15919 - the compiler will not warn you anymore for obvious mistakes, you'r on
15923 disable_warning <nnnn>
15924 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15928 - the compiler will not warn you anymore about warning number <nnnn>.
15932 \begin_inset LatexCommand \index{\#pragma nogcse}
15936 - will stop global common subexpression elimination.
15940 \begin_inset LatexCommand \index{\#pragma noinduction}
15944 - will stop loop induction optimizations.
15948 \begin_inset LatexCommand \index{\#pragma noinvariant}
15952 - will not do loop invariant optimizations.
15953 For more details see Loop Invariants in section
15954 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15962 \begin_inset LatexCommand \index{\#pragma noiv}
15966 - Do not generate interrupt
15967 \begin_inset LatexCommand \index{interrupt}
15971 vector table entries for all ISR functions defined after the pragma.
15972 This is useful in cases where the interrupt vector table must be defined
15973 manually, or when there is a secondary, manually defined interrupt vector
15975 for the autovector feature of the Cypress EZ-USB FX2).
15976 More elegantly this can be achieved by obmitting the optional interrupt
15977 number after the interrupt keyword, see section
15978 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15987 \begin_inset LatexCommand \index{\#pragma nojtbound}
15991 - will not generate code for boundary value checking, when switch statements
15992 are turned into jump-tables (dangerous).
15993 For more details see section
15994 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
16002 \begin_inset LatexCommand \index{\#pragma noloopreverse}
16006 - Will not do loop reversal optimization
16010 \begin_inset LatexCommand \index{\#pragma nooverlay}
16014 - the compiler will not overlay the parameters and local variables of a
16019 \begin_inset LatexCommand \index{\#pragma stackauto}
16034 \begin_inset LatexCommand \index{-\/-stack-auto}
16039 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
16043 Parameters and Local Variables.
16047 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
16051 - The compiler will optimize code generation towards fast code, possibly
16052 at the expense of code size.
16056 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
16060 - The compiler will optimize code generation towards compact code, possibly
16061 at the expense of code speed.
16065 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
16069 - The compiler will attempt to generate code that is both compact and fast,
16070 as long as meeting one goal is not a detriment to the other (this is the
16076 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
16080 - Generally follow the C89 standard, but allow SDCC features that conflict
16081 with the standard (default).
16085 \begin_inset LatexCommand \index{\#pragma std\_c89}
16089 - Follow the C89 standard and disable SDCC features that conflict with the
16094 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
16098 - Generally follow the C99 standard, but allow SDCC features that conflict
16099 with the standard (incomplete support).
16103 \begin_inset LatexCommand \index{\#pragma std\_c99}
16107 - Follow the C99 standard and disable SDCC features that conflict with the
16108 standard (incomplete support).
16112 \begin_inset LatexCommand \index{\#pragma codeseg}
16116 - Use this name (max.
16117 8 characters) for the code segment.
16132 \begin_inset LatexCommand \index{\#pragma constseg}
16136 - Use this name (max.
16137 8 characters) for the const segment.
16151 SDCPP supports the following #pragma directives:
16155 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16159 (+ | -) - switch _asm _endasm block preprocessing on / off.
16163 The pragma's are intended to be used to turn-on or off certain optimizations
16164 which might cause the compiler to generate extra stack / data space to
16165 store compiler generated temporary variables.
16166 This usually happens in large functions.
16167 Pragma directives should be used as shown in the following example, they
16168 are used to control options & optimizations for a given function; pragmas
16169 should be placed before and/or after a function, placing pragma's inside
16170 a function body could have unpredictable results.
16176 \begin_inset LatexCommand \index{\#pragma save}
16187 /* save the current settings */
16190 \begin_inset LatexCommand \index{\#pragma nogcse}
16199 /* turnoff global subexpression elimination */
16201 #pragma noinduction
16202 \begin_inset LatexCommand \index{\#pragma noinduction}
16206 /* turn off induction optimizations */
16229 \begin_inset LatexCommand \index{\#pragma restore}
16233 /* turn the optimizations back on */
16236 The compiler will generate a warning message when extra space is allocated.
16237 It is strongly recommended that the save and restore pragma's be used when
16238 changing options for a function.
16241 Defines Created by the Compiler
16244 The compiler creates the following #defines
16245 \begin_inset LatexCommand \index{\#defines}
16250 \begin_inset LatexCommand \index{Defines created by the compiler}
16260 \begin_inset Tabular
16261 <lyxtabular version="3" rows="11" columns="2">
16263 <column alignment="left" valignment="top" leftline="true" width="0">
16264 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
16265 <row topline="true" bottomline="true">
16266 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16276 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16287 <row topline="true">
16288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16294 \begin_inset LatexCommand \index{SDCC}
16301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16307 Since version 2.5.6 the version number as an int (ex.
16312 <row topline="true">
16313 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16319 \begin_inset LatexCommand \index{SDCC\_mcs51}
16324 \begin_inset LatexCommand \index{SDCC\_ds390}
16329 \begin_inset LatexCommand \index{SDCC\_z80}
16336 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16341 depending on the model used (e.g.: -mds390
16345 <row topline="true">
16346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16352 \begin_inset LatexCommand \index{\_\_mcs51}
16357 \begin_inset LatexCommand \index{\_\_ds390}
16362 \begin_inset LatexCommand \index{\_\_hc08}
16367 \begin_inset LatexCommand \index{\_\_z80}
16374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16379 depending on the model used (e.g.
16384 <row topline="true">
16385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16391 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16421 <row topline="true">
16422 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16428 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16435 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16458 <row topline="true">
16459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16465 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
16472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16495 <row topline="true">
16496 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16502 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
16509 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16532 <row topline="true">
16533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16539 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
16546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16569 <row topline="true">
16570 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16576 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16583 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16596 <row topline="true" bottomline="true">
16597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16603 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16630 Notes on supported Processors
16634 \begin_inset LatexCommand \label{sub:MCS51-variants}
16639 \begin_inset LatexCommand \index{MCS51 variants}
16646 MCS51 processors are available from many vendors and come in many different
16648 While they might differ considerably in respect to Special Function Registers
16649 the core MCS51 is usually not modified or is kept compatible.
16653 pdata access by SFR
16656 With the upcome of devices with internal xdata and flash memory devices
16658 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16662 as dedicated I/O port is becoming more popular.
16663 Switching the high byte for pdata
16664 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16668 access which was formerly done by port P2 is then achieved by a Special
16670 \begin_inset LatexCommand \index{sfr}
16675 In well-established MCS51 tradition the address of this
16679 is where the chip designers decided to put it.
16680 Needless to say that they didn't agree on a common name either.
16681 So that the startup code can correctly initialize xdata variables, you
16682 should define an sfr with the name _XPAGE
16685 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16691 at the appropriate location if the default, port P2, is not used for this.
16697 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16702 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16707 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16710 For more exotic implementations further customizations may be needed.
16712 \begin_inset LatexCommand \ref{sub:Startup-Code}
16716 for other possibilities.
16719 Other Features available by SFR
16722 Some MCS51 variants offer features like Double DPTR
16723 \begin_inset LatexCommand \index{DPTR}
16727 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16728 These are currently not used for the MCS51 port.
16729 If you absolutely need them you can fall back to inline assembly or submit
16736 The DS80C400 microcontroller has a rich set of peripherals.
16737 In its built-in ROM library it includes functions to access some of the
16738 features, among them is a TCP stack with IP4 and IP6 support.
16739 Library headers (currently in beta status) and other files are provided
16743 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16751 The Z80 and gbz80 port
16754 SDCC can target both the Zilog
16755 \begin_inset LatexCommand \index{Z80}
16759 and the Nintendo Gameboy's Z80-like gbz80
16760 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16765 The Z80 port is passed through the same
16768 \begin_inset LatexCommand \index{Regression test}
16774 as the MCS51 and DS390 ports, so floating point support, support for long
16775 variables and bitfield support is fine.
16776 See mailing lists and forums about interrupt routines.
16779 As always, the code is the authoritative reference - see z80/ralloc.c and
16782 \begin_inset LatexCommand \index{stack}
16786 frame is similar to that generated by the IAR Z80 compiler.
16787 IX is used as the base pointer, HL and IY are used as a temporary registers,
16788 and BC and DE are available for holding variables.
16790 \begin_inset LatexCommand \index{return value}
16794 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16796 The gbz80 port use the same set of registers for the return values, but
16797 in a different order of significance: E (one byte), DE (two bytes), or
16804 The port to the Motorola HC08
16805 \begin_inset LatexCommand \index{HC08}
16809 family has been added in October 2003, and is still undergoing some basic
16811 The code generator is complete, but the register allocation is still quite
16813 Some of the SDCC's standard C library functions have embedded non-HC08
16814 inline assembly and so are not yet usable.
16825 \begin_inset LatexCommand \index{PIC14}
16829 port still requires a major effort from the development community.
16830 However it can work for very simple code.
16833 C code and 14bit PIC code page
16834 \begin_inset LatexCommand \index{code page (pic14)}
16839 \begin_inset LatexCommand \index{RAM bank (pic14)}
16846 The linker organizes allocation for the code page and RAM banks.
16847 It does not have intimate knowledge of the code flow.
16848 It will put all the code section of a single asm file into a single code
16850 In order to make use of multiple code pages, separate asm files must be
16852 The compiler treats all functions of a single C file as being in the same
16853 code page unless it is non static.
16854 The compiler treats all local variables of a single C file as being in
16855 the same RAM bank unless it is an extern.
16859 To get the best follow these guide lines:
16862 make local functions static, as non static functions require code page selection
16866 Make local variables static as extern variables require RAM bank selection
16870 For devices that have multiple code pages it is more efficient to use the
16871 same number of files as pages, i.e.
16872 for the 16F877 use 4 separate files and i.e.
16873 for the 16F874 use 2 separate files.
16874 This way the linker can put the code for each file into different code
16875 pages and the compiler can allocate reusable variables more efficiently
16876 and there's less page selection overhead.
16877 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16878 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16881 Creating a device include file
16884 For generating a device include file use the support perl script inc2h.pl
16885 kept in directory support/script.
16891 For the interrupt function, use the keyword 'interrupt'
16892 \begin_inset LatexCommand \index{interrupt}
16896 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16897 there to avoid a syntax error - it ought to be fixed).
16903 void Intr(void) interrupt 0
16909 T0IF = 0; /* Clear timer interrupt */
16914 Linking and assembling
16917 For assembling you can use either GPUTILS'
16918 \begin_inset LatexCommand \index{gputils (pic tools)}
16922 gpasm.exe or MPLAB's mpasmwin.exe.
16923 GPUTILS is available from
16924 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16929 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16930 If you use MPLAB and an interrupt function then the linker script file
16931 vectors section will need to be enlarged to link with mplink.
16954 sdcc -S -V -mpic14 -p16F877 $<
16968 $(PRJ).hex: $(OBJS)
16978 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17000 sdcc -S -V -mpic14 -p16F877 $<
17010 mpasmwin /q /o $*.asm
17014 $(PRJ).hex: $(OBJS)
17024 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17027 Please note that indentations within a
17031 have to be done with a tabulator character.
17034 Command-line options
17037 Besides the switches common to all SDCC backends, the PIC14 port accepts
17038 the following options (for an updated list see sdcc -
17050 \labelwidthstring 00.00.0000
17062 -debug-extra emit debug info in assembly output
17064 \labelwidthstring 00.00.0000
17076 -no-pcode-opt disable (slightly faulty) optimization on pCode
17080 \layout Subsubsection
17082 error: missing definition for symbol
17083 \begin_inset Quotes sld
17087 \begin_inset Quotes srd
17093 The PIC14 port uses library routines to provide more complex operations
17094 like multiplication, division/modulus and (generic) pointer dereferencing.
17095 In order to add these routines to your project, you must link with PIC14's
17101 For single source file projects this is done automatically, more complex
17106 to the linker's arguments.
17107 Make sure you also add an include path for the library (using the -I switch
17109 \layout Subsubsection
17111 Processor mismatch in file
17112 \begin_inset Quotes sld
17116 \begin_inset Quotes srd
17122 This warning can usually be ignored due to the very good compatibility amongst
17123 14 bit PIC devices.
17126 You might also consider recompiling the library for your specific device
17127 by changing the ARCH=p16f877 (default target) entry in
17129 device/lib/pic/Makefile.in
17133 device/lib/pic/Makefile
17135 to reflect your device.
17136 This might even improve performance for smaller devices as unneccesary
17137 BANKSELs migth be removed.
17141 \layout Subsubsection
17146 Currently, data can only be initialized if it resides in the source file
17152 Data in other source files will silently
17160 \begin_inset LatexCommand \index{PIC16}
17168 \begin_inset LatexCommand \index{PIC16}
17172 port is the portion of SDCC that is responsible to produce code for the
17174 \begin_inset LatexCommand \index{Microchip}
17178 (TM) microcontrollers with 16 bit core.
17179 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17180 Currently supported devices are:
17184 \begin_inset Tabular
17185 <lyxtabular version="3" rows="4" columns="6">
17187 <column alignment="center" valignment="top" leftline="true" width="0">
17188 <column alignment="center" valignment="top" leftline="true" width="0">
17189 <column alignment="center" valignment="top" leftline="true" width="0">
17190 <column alignment="center" valignment="top" leftline="true" width="0">
17191 <column alignment="center" valignment="top" leftline="true" width="0">
17192 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17193 <row topline="true">
17194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17218 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17226 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17243 <row topline="true">
17244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17268 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17276 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17293 <row topline="true">
17294 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17343 <row topline="true" bottomline="true">
17344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17352 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17360 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17400 PIC16 port supports the standard command line arguments as supposed, with
17401 the exception of certain cases that will be mentioned in the following
17404 \labelwidthstring 00.00.0000
17416 -callee-saves See -
17428 \labelwidthstring 00.00.0000
17440 -all-callee-saves All function arguments are passed on stack by default.
17443 There is no need to specify this in the command line.
17445 \labelwidthstring 00.00.0000
17457 -fommit-frame-pointer Frame pointer will be omitted when the function uses
17458 no local variables.
17461 Port Specific Options
17462 \begin_inset LatexCommand \index{Options PIC16}
17469 The port specific options appear after the global options in the sdcc --help
17471 \layout Subsubsection
17476 General options enable certain port features and optimizations.
17478 \labelwidthstring 00.00.0000
17490 -stack-model=[model] Used in conjuction with the command above.
17491 Defines the stack model to be used, valid stack models are :
17494 \labelwidthstring 00.00.0000
17500 Selects small stack model.
17501 8 bit stack and frame pointers.
17502 Supports 256 bytes stack size.
17504 \labelwidthstring 00.00.0000
17510 Selects large stack model.
17511 16 bit stack and frame pointers.
17512 Supports 65536 bytes stack size.
17515 \labelwidthstring 00.00.0000
17527 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
17528 unitialized data variables with [kword].
17529 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
17531 \labelwidthstring 00.00.0000
17543 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
17544 Useful for bootloaders.
17546 \labelwidthstring 00.00.0000
17558 -asm= sets the full path and name of an external assembler to call.
17560 \labelwidthstring 00.00.0000
17572 -link= sets the full path and name of an external linker to call.
17574 \labelwidthstring 00.00.0000
17586 -mplab-comp MPLAB compatibility option.
17587 Currently only suppresses special gpasm directives.
17588 \layout Subsubsection
17590 Optimization Options
17592 \labelwidthstring 00.00.0000
17604 -optimize-goto Try to use (conditional) BRA instead of GOTO
17606 \labelwidthstring 00.00.0000
17618 -optimize-cmp Try to optimize some compares.
17620 \labelwidthstring 00.00.0000
17632 -optimize-df Analyze the dataflow of the generated code and improve it.
17634 \labelwidthstring 00.00.0000
17646 -obanksel=nn Set optimization level for inserting BANKSELs.
17651 \labelwidthstring 00.00.0000
17655 \labelwidthstring 00.00.0000
17657 1 checks previous used register and if it is the same then does not emit
17658 BANKSEL, accounts only for labels.
17660 \labelwidthstring 00.00.0000
17662 2 tries to check the location of (even different) symbols and removes BANKSELs
17663 if they are in the same bank.
17668 Important: There might be problems if the linker script has data sections
17669 across bank borders!
17671 \layout Subsubsection
17675 \labelwidthstring 00.00.0000
17687 -nodefaultlibs do not link default libraries when linking
17689 \labelwidthstring 00.00.0000
17701 -no-crt Don't link the default run-time modules
17703 \labelwidthstring 00.00.0000
17715 -use-crt= Use a custom run-time module instead of the defaults.
17716 \layout Subsubsection
17721 Debugging options enable extra debugging information in the output files.
17723 \labelwidthstring 00.00.0000
17735 -debug-xtra Similar to -
17746 \begin_inset LatexCommand \index{-\/-debug}
17750 , but dumps more information.
17752 \labelwidthstring 00.00.0000
17764 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17766 <source> is the name of the file compiled.
17768 \labelwidthstring 00.00.0000
17780 -pcode-verbose Enable pcode debugging information in translation.
17782 \labelwidthstring 00.00.0000
17794 -denable-peeps Force the usage of peepholes.
17797 \labelwidthstring 00.00.0000
17809 -gstack Trace push/pops for stack pointer overflow
17811 \labelwidthstring 00.00.0000
17823 -call-tree dump call tree in .calltree file
17826 Enviromental Variables
17829 There is a number of enviromental variables that can be used when running
17830 SDCC to enable certain optimizations or force a specific program behaviour.
17831 these variables are primarily for debugging purposes so they can be enabled/dis
17835 Currently there is only two such variables available:
17837 \labelwidthstring 00.00.0000
17839 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17840 bitfields is optimized by directly loading FSR0 with the address of the
17841 bitfield structure.
17842 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17844 This step saves data ram and code space for functions that perform heavy
17847 80 bytes of code space are saved when compiling malloc.c with this option).
17850 \labelwidthstring 00.00.0000
17852 NO_REG_OPT do not perform pCode registers optimization.
17853 This should be used for debugging purposes.
17854 In some where bugs in the pcode optimizer are found, users can benefit
17855 from temporarily disabling the optimizer until the bug is fixed.
17858 Preprocessor Macros
17861 PIC16 port defines the following preprocessor macros while translating a
17866 \begin_inset Tabular
17867 <lyxtabular version="3" rows="6" columns="2">
17869 <column alignment="center" valignment="top" leftline="true" width="0">
17870 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17871 <row topline="true" bottomline="true">
17872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17889 <row topline="true">
17890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17898 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17903 Port identification
17907 <row topline="true">
17908 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17926 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17931 Port identification (same as above)
17935 <row topline="true">
17936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17949 MCU Identification.
17954 is the microcontrol identification number, i.e.
17959 <row topline="true">
17960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17978 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17983 MCU Identification (same as above)
17987 <row topline="true" bottomline="true">
17988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18001 nnn = SMALL or LARGE respectively according to the stack model used
18012 In addition the following macros are defined when calling assembler:
18016 \begin_inset Tabular
18017 <lyxtabular version="3" rows="4" columns="2">
18019 <column alignment="center" valignment="top" leftline="true" width="0">
18020 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18021 <row topline="true" bottomline="true">
18022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18030 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18039 <row topline="true">
18040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18048 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18053 MCU Identification.
18058 is the microcontrol identification number, i.e.
18063 <row topline="true">
18064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18077 nnn = SMALL or LARGE respectively according to the memory model used for
18082 <row topline="true" bottomline="true">
18083 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18091 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18096 nnn = SMALL or LARGE respectively according to the stack model used
18111 \begin_inset LatexCommand \index{PIC16}
18115 port uses the following directories for searching header files and libraries.
18119 \begin_inset Tabular
18120 <lyxtabular version="3" rows="3" columns="4">
18122 <column alignment="center" valignment="top" leftline="true" width="0">
18123 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18124 <column alignment="center" valignment="top" width="0">
18125 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18126 <row topline="true" bottomline="true">
18127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18143 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18151 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18160 <row topline="true">
18161 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18166 PREFIX/sdcc/include/pic16
18169 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18174 PIC16 specific headers
18177 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18185 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18194 <row topline="true" bottomline="true">
18195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18200 PREFIX/sdcc/lib/pic16
18203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18208 PIC16 specific libraries
18211 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18219 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18236 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18243 PIC16 port currently supports the following pragmas:
18245 \labelwidthstring 00.00.0000
18247 stack pragma stack forces the code generator to initialize the stack & frame
18248 pointers at a specific address.
18249 This is an adhoc solution for cases where no STACK directive is available
18250 in the linker script or gplink is not instructed to create a stack section.
18252 The stack pragma should be used only once in a project.
18253 Multiple pragmas may result in indeterminate behaviour of the program.
18259 The old format (ie.
18260 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18261 cross page boundaries (or even exceed the available data RAM) and crash
18263 Make sure that stack does not cross page boundaries when using the SMALL
18269 The format is as follows:
18272 #pragma stack bottom_address [stack_size]
18279 is the lower bound of the stack section.
18280 The stack pointer initially will point at address (bottom_address+stack_size-1).
18288 /* initializes stack of 100 bytes at RAM address 0x200 */
18291 #pragma stack 0x200 100
18294 If the stack_size field is omitted then a stack is created with the default
18296 This size might be enough for most programs, but its not enough for operations
18297 with deep function nesting or excessive stack usage.
18299 \labelwidthstring 00.00.0000
18303 This pragma is deprecated.
18304 Its use will cause a warning message to be issued.
18310 \labelwidthstring 00.00.0000
18312 code place a function symbol at static FLASH address
18320 /* place function test_func at 0x4000 */
18323 #pragma code test_func 0x4000
18327 \labelwidthstring 00.00.0000
18329 library instructs the linker to use a library module.
18334 #pragma library module_name
18341 can be any library or object file (including its path).
18342 Note that there are four reserved keywords which have special meaning.
18347 \begin_inset Tabular
18348 <lyxtabular version="3" rows="6" columns="3">
18350 <column alignment="center" valignment="top" leftline="true" width="0">
18351 <column alignment="block" valignment="top" leftline="true" width="20page%">
18352 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18353 <row topline="true" bottomline="true">
18354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18370 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18379 <row topline="true">
18380 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18395 ignore all library pragmas
18398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18409 <row topline="true">
18410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18420 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18428 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18441 <row topline="true">
18442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18452 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18457 link the Math libarary
18460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18473 <row topline="true">
18474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18489 link the I/O library
18492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18505 <row topline="true" bottomline="true">
18506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18521 link the debug library
18524 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18543 * is the device number, i.e.
18544 452 for PIC18F452 MCU.
18547 This feature allows for linking with specific libraries withoug having to
18548 explicit name them in the command line.
18553 keyword will reject all modules specified by the library pragma.
18555 \labelwidthstring 00.00.0000
18557 udata pragma udata instructs the compiler to emit code so that linker will
18558 place a variable at a specific memory bank
18566 /* places variable foo at bank2 */
18569 #pragma udata bank2 foo
18575 In order for this pragma to work extra SECTION directives should be added
18576 in the .lkr script.
18577 In the following example a sample .lkr file is shown:
18582 // Sample linker script for the PIC18F452 processor
18588 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18591 CODEPAGE NAME=page START=0x2A END=0x7FFF
18594 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18597 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18600 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18603 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18606 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18611 DATABANK NAME=gpr0 START=0x80 END=0xFF
18614 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18617 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18620 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18623 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18626 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18629 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18634 SECTION NAME=CONFIG ROM=config
18639 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18642 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18645 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18648 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18651 SECTION NAME=bank4 RAM=gpr4
18654 SECTION NAME=bank5 RAM=gpr5
18657 The linker will recognise the section name set in the pragma statement and
18658 will position the variable at the memory bank set with the RAM field at
18659 the SECTION line in the linker script file.
18663 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18670 There is one main header file that can be included to the source files using
18677 This header file contains the definitions for the processor special registers,
18678 so it is necessary if the source accesses them.
18679 It can be included by adding the following line in the beginning of the
18683 #include <pic18fregs.h>
18686 The specific microcontroller is selected within the pic18fregs.h automatically,
18687 so the same source can be used with a variety of devices.
18693 The libraries that PIC16
18694 \begin_inset LatexCommand \index{PIC16}
18698 port depends on are the microcontroller device libraries which contain
18699 the symbol definitions for the microcontroller special function registers.
18700 These libraries have the format pic18fxxxx.lib, where
18704 is the microcontroller identification number.
18705 The specific library is selected automatically by the compiler at link
18706 stage according to the selected device.
18709 Libraries are created with gplib which is part of the gputils package
18710 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18715 \layout Subsubsection*
18717 Building the libraries
18720 Before using SDCC/pic16 there are some libraries that need to be compiled.
18721 This process is not done automatically by SDCC since not all users use
18722 SDCC for pic16 projects.
18723 So each user should compile the libraries separately.
18726 The steps to compile the pic16 libraries under Linux are:
18729 cd device/lib/pic16
18744 su -c 'make install' # install the libraries, you need the root password
18747 If you need to install the headers too, do:
18753 su -c 'make install' # install the headers, you need the root password
18756 There exist a special target to build the I/O libraries.
18757 This target is not automatically build because it will build the I/O library
18763 This way building will take quite a lot of time.
18764 Users are advised to edit the
18766 device/lib/pic16/pics.build
18768 file and then execute:
18777 The following memory models are supported by the PIC16 port:
18786 Memory model affects the default size of pointers within the source.
18787 The sizes are shown in the next table:
18791 \begin_inset Tabular
18792 <lyxtabular version="3" rows="3" columns="3">
18794 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18795 <column alignment="center" valignment="top" leftline="true" width="0">
18796 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18797 <row topline="true" bottomline="true">
18798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18803 Pointer sizes according to memory model
18806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18814 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18823 <row topline="true" bottomline="true">
18824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18832 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18849 <row topline="true" bottomline="true">
18850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18858 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18882 It is advisable that all sources within a project are compiled with the
18884 If one wants to override the default memory model, this can be done by
18885 declaring a pointer as
18894 Far selects large memory model's pointers, while near selects small memory
18898 The standard device libraries (see
18899 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18903 ) contain no reference to pointers, so they can be used with both memory
18910 The stack implementation for the PIC16 port uses two indirect registers,
18913 \labelwidthstring 00.00.0000
18915 FSR1 is assigned as stack pointer
18917 \labelwidthstring 00.00.0000
18919 FSR2 is assigned as frame pointer
18922 The following stack models are supported by the PIC16 port
18943 model means that only the FSRxL byte is used to access stack and frame,
18950 uses both FSRxL and FSRxH registers.
18951 The following table shows the stack/frame pointers sizes according to stack
18952 model and the maximum space they can address:
18956 \begin_inset Tabular
18957 <lyxtabular version="3" rows="3" columns="3">
18959 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18960 <column alignment="center" valignment="top" leftline="true" width="0">
18961 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18962 <row topline="true" bottomline="true">
18963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18968 Stack & Frame pointer sizes according to stack model
18971 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18988 <row topline="true">
18989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19014 <row topline="true" bottomline="true">
19015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19023 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19051 stack model is currently not working properly throughout the code generator.
19052 So its use is not advised.
19053 Also there are some other points that need special care:
19058 Do not create stack sections with size more than one physical bank (that
19062 Stack sections should no cross physical bank limits (i.e.
19063 #pragma stack 0x50 0x100)
19066 These limitations are caused by the fact that only FSRxL is modified when
19067 using SMALL stack model, so no more than 256 bytes of stack can be used.
19068 This problem will disappear after LARGE model is fully implemented.
19074 In addition to the standard SDCC function keywords, PIC16 port makes available
19077 \labelwidthstring 00.00.0000
19079 wparam Use the WREG to pass one byte of the first function argument.
19080 This improves speed but you may not use this for functions with arguments
19081 that are called via function pointers, otherwise the first byte of the
19082 first parameter will get lost.
19086 void func_wparam(int a) wparam
19092 /* WREG hold the lower part of a */
19095 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19105 This keyword replaces the deprecated wparam pragma.
19107 \labelwidthstring 00.00.0000
19109 shadowregs When entering/exiting an ISR, it is possible to take advantage
19110 of the PIC18F hardware shadow registers which hold the values of WREG,
19111 STATUS and BSR registers.
19112 This can be done by adding the keyword
19120 keyword in the function's header.
19123 void isr_shadow(void) shadowregs interrupt 1
19139 instructs the code generator not to store/restore WREG, STATUS, BSR when
19140 entering/exiting the ISR.
19143 Function return values
19146 Return values from functions are placed to the appropriate registers following
19147 a modified Microchip policy optimized for SDCC.
19148 The following table shows these registers:
19152 \begin_inset Tabular
19153 <lyxtabular version="3" rows="6" columns="2">
19155 <column alignment="center" valignment="top" leftline="true" width="0">
19156 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19157 <row topline="true" bottomline="true">
19158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19171 destination register
19175 <row topline="true">
19176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19193 <row topline="true">
19194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19211 <row topline="true">
19212 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19220 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19229 <row topline="true">
19230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19243 FSR0L:PRODH:PRODL:WREG
19247 <row topline="true" bottomline="true">
19248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19261 on stack, FSR0 points to the beginning
19275 An interrupt servive routine (ISR) is declared using the
19282 void isr(void) interrupt
19300 is the interrupt number, which for PIC18F devices can be:
19304 \begin_inset Tabular
19305 <lyxtabular version="3" rows="4" columns="3">
19307 <column alignment="center" valignment="top" leftline="true" width="0">
19308 <column alignment="center" valignment="top" leftline="true" width="0">
19309 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19310 <row topline="true" bottomline="true">
19311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19334 Interrupt Vector Address
19338 <row topline="true">
19339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19347 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19355 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19364 <row topline="true">
19365 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19396 HIGH priority interrupts
19399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19408 <row topline="true" bottomline="true">
19409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19422 LOW priority interrupts
19425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19441 When generating assembly code for ISR the code generator places a
19447 Interrupt Vector Address
19449 which points at the genetated ISR.
19450 This single GOTO instruction is part of an automatically generated
19452 interrupt entry point
19455 The actuall ISR code is placed as normally would in the code space.
19456 Upon interrupt request, the GOTO instruction is executed which jumps to
19458 When declaring interrupt functions as _naked this GOTO instruction is
19463 The whole interrupt functions is therefore placed at the Interrupt Vector
19464 Address of the specific interrupt.
19465 This is not a problem for the LOW priority interrupts, but it is a problem
19466 for the RESET and the HIGH priority interrupts because code may be written
19467 at the next interrupt´s vector address and cause undeterminate program
19468 behaviour if that interrupt is raised.
19474 This is not a problem when
19477 this is a HIGH interrupt ISR and LOW interrupts are
19484 when the ISR is small enough not to reach the next interrupt´s vector address.
19494 is possible to be omitted.
19495 This way a function is generated similar to an ISR, but it is not assigned
19499 When entering an interrupt, currently the PIC16
19500 \begin_inset LatexCommand \index{PIC16}
19504 port automatically saves the following registers:
19516 PROD (PRODL and PRODH)
19519 FSR0 (FSR0L and FSR0H)
19522 These registers are restored upon return from the interrupt routine.
19528 NOTE that when the _naked attribute is specified for an interrupt routine,
19529 then NO registers are stored or restored.
19538 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
19539 There are 3 types of generic pointers currently implemented data, code
19540 and eeprom pointers.
19541 They are differentiated by the value of the 7th and 6th bits of the upper
19546 \begin_inset Tabular
19547 <lyxtabular version="3" rows="5" columns="5">
19549 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19550 <column alignment="center" valignment="top" width="0">
19551 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19552 <column alignment="center" valignment="top" width="0">
19553 <column alignment="left" valignment="top" rightline="true" width="0">
19554 <row topline="true" bottomline="true">
19555 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19571 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19584 rest of the pointer
19587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19596 <row topline="true" bottomline="true">
19597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19613 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19630 uuuuuu uuuuxxxx xxxxxxxx
19633 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19638 a 12-bit data pointer in data RAM memory
19642 <row bottomline="true">
19643 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19659 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19676 uxxxxx xxxxxxxx xxxxxxxx
19679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19684 a 21-bit code pointer in FLASH memory
19688 <row bottomline="true">
19689 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19713 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19722 uuuuuu uuuuuuxx xxxxxxxx
19725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19730 a 10-bit eeprom pointer in EEPROM memory
19734 <row bottomline="true">
19735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19759 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19768 xxxxxx xxxxxxxx xxxxxxxx
19771 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19776 unimplemented pointer type
19787 Generic pointer are read and written with a set of library functions which
19788 read/write 1, 2, 3, 4 bytes.
19792 \layout Subsubsection
19794 Standard I/O Streams
19801 the type FILE is defined as:
19804 typedef char * FILE;
19807 This type is the stream type implemented I/O in the PIC18F devices.
19808 Also the standard input and output streams are declared in stdio.h:
19811 extern FILE * stdin;
19814 extern FILE * stdout;
19817 The FILE type is actually a generic pointer which defines one more type
19818 of generic pointers, the
19823 This new type has the format:
19827 \begin_inset Tabular
19828 <lyxtabular version="3" rows="2" columns="7">
19830 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19831 <column alignment="center" valignment="top" width="0">
19832 <column alignment="center" valignment="top" leftline="true" width="0">
19833 <column alignment="center" valignment="top" leftline="true" width="0">
19834 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19835 <column alignment="center" valignment="top" width="0">
19836 <column alignment="left" valignment="top" rightline="true" width="0">
19837 <row topline="true" bottomline="true">
19838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19862 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19870 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19883 rest of the pointer
19886 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19895 <row topline="true" bottomline="true">
19896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19920 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19928 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19948 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19953 upper byte high nubble is 0x2n, the rest are zeroes
19964 Currently implemented there are 3 types of streams defined:
19968 \begin_inset Tabular
19969 <lyxtabular version="3" rows="4" columns="4">
19971 <column alignment="center" valignment="top" leftline="true" width="0">
19972 <column alignment="center" valignment="top" leftline="true" width="0">
19973 <column alignment="center" valignment="top" leftline="true" width="0">
19974 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19975 <row topline="true" bottomline="true">
19976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20009 <row topline="true">
20010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20041 Writes/Reads characters via the USART peripheral
20045 <row topline="true">
20046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20077 Writes/Reads characters via the MSSP peripheral
20081 <row topline="true" bottomline="true">
20082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20108 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20113 Writes/Reads characters via used defined functions
20124 The stream identifiers are declared as macros in the stdio.h header.
20127 In the libc library there exist the functions that are used to write to
20128 each of the above streams.
20131 \labelwidthstring 00.00.0000
20143 _stream_usart_putchar writes a character at the USART stream
20145 \labelwidthstring 00.00.0000
20157 _stream_mssp_putchar writes a character at the MSSP stream
20159 \labelwidthstring 00.00.0000
20161 putchar dummy function.
20162 This writes a character to a user specified manner.
20165 In order to increase performance
20169 is declared in stdio.h as having its parameter in WREG (it has the wparam
20171 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
20172 in a user-friendly way.
20177 is the name of the variable that holds the character to print.
20178 An example follows:
20181 #include <pic18fregs.h>
20193 PORTA = c; /* dump character c to PORTA */
20206 stdout = STREAM_USER; /* this is not necessery, since stdout points
20209 * by default to STREAM_USER */
20212 printf (¨This is a printf test
20220 \layout Subsubsection
20225 PIC16 contains an implementation of the printf-family of functions.
20226 There exist the following functions:
20229 extern unsigned int sprintf(char *buf, char *fmt, ...);
20232 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20237 extern unsigned int printf(char *fmt, ...);
20240 extern unsigned int vprintf(char *fmt, va_lista ap);
20245 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20248 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20251 For sprintf and vsprintf
20255 should normally be a data pointer where the resulting string will be placed.
20256 No range checking is done so the user should allocate the necessery buffer.
20257 For fprintf and vfprintf
20261 should be a stream pointer (i.e.
20262 stdout, STREAM_MSSP, etc...).
20263 \layout Subsubsection
20268 The PIC18F family of microcontrollers supports a number of interrupt sources.
20269 A list of these interrupts is shown in the following table:
20273 \begin_inset Tabular
20274 <lyxtabular version="3" rows="11" columns="4">
20276 <column alignment="left" valignment="top" leftline="true" width="0">
20277 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20278 <column alignment="left" valignment="top" leftline="true" width="0">
20279 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20280 <row topline="true" bottomline="true">
20281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20289 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20305 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20314 <row topline="true">
20315 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20323 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20328 PORTB change interrupt
20331 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20339 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20344 EEPROM/FLASH write complete interrupt
20348 <row topline="true">
20349 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20357 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20362 INT0 external interrupt
20365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20373 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20378 Bus collision interrupt
20382 <row topline="true">
20383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20391 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20396 INT1 external interrupt
20399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20412 Low voltage detect interrupt
20416 <row topline="true">
20417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20430 INT2 external interrupt
20433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20441 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20446 Parallel slave port interrupt
20450 <row topline="true">
20451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20464 CCP1 module interrupt
20467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20475 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20480 AD convertion complete interrupt
20484 <row topline="true">
20485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20498 CCP2 module interrupt
20501 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20509 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20514 USART receive interrupt
20518 <row topline="true">
20519 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20527 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20532 TMR0 overflow interrupt
20535 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20543 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20548 USART transmit interrupt
20552 <row topline="true">
20553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20566 TMR1 overflow interrupt
20569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20577 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20582 SSP receive/transmit interrupt
20586 <row topline="true">
20587 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20600 TMR2 matches PR2 interrupt
20603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20618 <row topline="true" bottomline="true">
20619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20632 TMR3 overflow interrupt
20635 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20642 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20657 The prototypes for these names are defined in the header file
20664 In order to simplify signal handling, a number of macros is provided:
20666 \labelwidthstring 00.00.0000
20668 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20669 high priority interrupts.
20674 is the function name to use.
20676 \labelwidthstring 00.00.0000
20678 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20679 low priority interrupt.
20684 is the function name to use.
20686 \labelwidthstring 00.00.0000
20688 DEF_HANDLER(sig,handler) define a handler for signal
20692 \labelwidthstring 00.00.0000
20694 END_DEF end the declaration of the dispatch table.
20697 Additionally there are two more macros to simplify the declaration of the
20700 \labelwidthstring 00.00.0000
20704 SIGHANDLER(handler)
20706 this declares the function prototype for the
20712 \labelwidthstring 00.00.0000
20714 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20717 An example of using the macros above is shown below:
20720 #include <pic18fregs.h>
20723 #include <signal.h>
20727 DEF_INTHIGH(high_int)
20730 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20733 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20740 SIGHANDLER(_tmr0_handler)
20746 /* action to be taken when timer 0 overflows */
20753 SIGHANDLERNAKED(_bcol_handler)
20762 /* action to be taken when bus collision occurs */
20778 Special care should be taken when using the above scheme:
20781 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20784 when declaring SIGHANDLERNAKED handler never forget to use
20788 for proper returning.
20794 Here you can find some general tips for compiling programs with SDCC/pic16.
20795 \layout Subsubsection
20800 The default stack size (that is 64 bytes) probably is enough for many programs.
20801 One must take care that when there are many levels of function nesting,
20802 or there is excessive usage of stack, its size should be extended.
20803 An example of such a case is the printf/sprintf family of functions.
20804 If you encounter problems like not being able to print integers, then you
20805 need to set the stack size around the maximum (256 for small stack model).
20806 The following diagram shows what happens when calling printf to print an
20810 printf () --> ltoa () --> ultoa () --> divschar ()
20813 It is should be understood that stack is easily consumed when calling complicate
20815 Using command line arguments like -
20825 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20827 Other ways to reduce stack usage may exist.
20833 There are several approaches to debugging your code.
20834 This chapter is meant to show your options and to give detail on some of
20839 When writing your code:
20842 write your code with debugging in mind (avoid duplicating code, put conceptually
20843 similar variables into structs, use structured code, have strategic points
20844 within your code where all variables are consistent, ...)
20847 run a syntax-checking tool like splint
20848 \begin_inset LatexCommand \index{splint (syntax checking tool)}
20853 \begin_inset LatexCommand \index{lint (syntax checking tool)}
20868 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
20875 for the high level code use a C-compiler (like f.e.
20876 GCC) to compile run and debug the code on your host.
20888 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
20892 ) on howto handle syntax extensions like __xdata, __at(), ...
20896 use another C-compiler to compile code for your target.
20897 Always an option but not recommended:) And not very likely to help you.
20898 If you seriously consider walking this path you should at least occasionally
20899 check portability of your code.
20900 Most commercial compiler vendors will offer an evaluation version so you
20901 can test compile your code or snippets of your code.
20904 Debugging on a simulator:
20907 there is a separate section about SDCDB (section
20908 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
20915 or (8051 specific) use a freeware/commercial simulator which interfaces
20917 \begin_inset LatexCommand \index{AOMF, AOMF51}
20922 \begin_inset LatexCommand \ref{OMF file}
20926 ) optionally generated by SDCC.
20929 Debugging On-target:
20932 use a MCU port pin to serially output debug data to the RS232 port of your
20934 You'll probably want some level shifting device typically involving a MAX232
20936 If the hardware serial port of the MCU is not available search for 'Software
20937 UART' in your favourite search machine.
20940 use an on-target monitor.
20941 In this context a monitor is a small program which usually accepts commands
20942 via a serial line and allows to set program counter, to single step through
20943 a program and read/write memory locations.
20944 For the 8051 good examples of monitors are paulmon and cmon51 (see section
20946 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
20953 toggle MCU port pins at strategic points within your code and use an oscilloscop
20957 digital oscilloscope
20960 \begin_inset LatexCommand \index{oscilloscope}
20964 with deep trace memory is really helpful especially if you have to debug
20965 a realtime application.
20966 If you need to monitor more pins than your oscilloscope provides you can
20967 sometimes get away with a small R-2R network.
20968 On a single channel oscilloscope you could f.e.
20969 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
20971 \begin_inset Formula $\Omega$
20974 resistor and the other one by a 5\SpecialChar ~
20976 \begin_inset Formula $\Omega$
20979 resistor to the oscilloscope probe (check output drive capability of the
20980 pins you want to monitor).
20981 If you need to monitor many more pins a
21001 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21006 Usually very expensive.
21007 And very nice to have too.
21008 And usually locks you (for years...) to the devices the ICE can emulate.
21012 use a remote debugger.
21013 In most 8-bit systems the symbol information is not available on the target,
21014 and a complete debugger is too bulky for the target system.
21015 Therefore usually a debugger on the host system connects to an on-target
21016 debugging stub which accepts only primitive commands.
21019 Terms to enter into your favourite search engine could be 'remote debugging',
21020 'gdb stub' or 'inferior debugger'.
21024 use an on target hardware debugger.
21025 Some of the more modern MCUs include hardware support for setting break
21026 points and monitoring/changing variables by using dedicated hardware pins.
21027 This facility doesn't require additional code to run on the target and
21032 doesn't affect runtime behaviour until a breakpoint is hit.
21033 For the mcs51 most hardware debuggers use the AOMF
21034 \begin_inset LatexCommand \index{AOMF, AOMF51}
21039 \begin_inset LatexCommand \ref{OMF file}
21050 if you are not familiar with any of the following terms you're likely to
21051 run into problems rather sooner than later:
21068 As an embedded programmer you
21072 to know them so why not look them up
21076 you have problems?)
21079 tell someone else about your problem (actually this is a surprisingly effective
21080 means to hunt down the bug even if the listener is not familiar with your
21082 As 'failure to communicate' is probably one of the job-induced deformations
21083 of an embedded programmer this is highly encouraged.
21086 Debugging with SDCDB
21087 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21092 \begin_inset LatexCommand \index{sdcdb (debugger)}
21099 SDCC is distributed with a source level debugger
21100 \begin_inset LatexCommand \index{Debugger}
21105 The debugger uses a command line interface, the command repertoire of the
21106 debugger has been kept as close to gdb
21107 \begin_inset LatexCommand \index{gdb}
21111 (the GNU debugger) as possible.
21112 The configuration and build process is part of the standard compiler installati
21113 on, which also builds and installs the debugger in the target directory
21114 specified during configuration.
21115 The debugger allows you debug BOTH at the C source and at the ASM source
21117 Sdcdb is currently not available on Win32 platforms.
21120 Compiling for Debugging
21134 \begin_inset LatexCommand \index{-\/-debug}
21138 option must be specified for all files for which debug information is to
21140 The compiler generates a .adb file for each of these files.
21141 The linker creates the .cdb
21142 \begin_inset LatexCommand \index{<file>.cdb}
21147 \begin_inset LatexCommand \index{<file>.adb}
21151 files and the address information.
21152 This .cdb is used by the debugger.
21155 How the Debugger Works
21168 -debug option is specified the compiler generates extra symbol information
21169 some of which are put into the assembler source and some are put into the
21171 Then the linker creates the .cdb file from the individual .adb files with
21172 the address information for the symbols.
21173 The debugger reads the symbolic information generated by the compiler &
21174 the address information generated by the linker.
21175 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
21176 execution is controlled by the debugger.
21177 When a command is issued for the debugger, it translates it into appropriate
21178 commands for the simulator.
21179 (Currently sdcdb only connects to the simulator but
21184 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
21188 is an effort to connect directly to the hardware.)
21191 Starting the Debugger SDCDB
21194 The debugger can be started using the following command line.
21195 (Assume the file you are debugging has the file name foo).
21209 The debugger will look for the following files.
21212 foo.c - the source file.
21215 foo.cdb - the debugger symbol information file.
21218 foo.ihx - the Intel hex format
21219 \begin_inset LatexCommand \index{Intel hex format}
21226 SDCDB Command Line Options
21239 -directory=<source file directory> this option can used to specify the directory
21241 The debugger will look into the directory list specified for source, cdb
21243 The items in the directory list must be separated by ':', e.g.
21244 if the source files can be in the directories /home/src1 and /home/src2,
21255 -directory option should be -
21265 -directory=/home/src1:/home/src2.
21266 Note there can be no spaces in the option.
21270 -cd <directory> - change to the <directory>.
21273 -fullname - used by GUI front ends.
21276 -cpu <cpu-type> - this argument is passed to the simulator please see the
21277 simulator docs for details.
21280 -X <Clock frequency > this options is passed to the simulator please see
21281 the simulator docs for details.
21284 -s <serial port file> passed to simulator see the simulator docs for details.
21287 -S <serial in,out> passed to simulator see the simulator docs for details.
21290 -k <port number> passed to simulator see the simulator docs for details.
21293 SDCDB Debugger Commands
21296 As mentioned earlier the command interface for the debugger has been deliberatel
21297 y kept as close the GNU debugger gdb, as possible.
21298 This will help the integration with existing graphical user interfaces
21299 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
21300 If you use a graphical user interface for the debugger you can skip this
21302 \layout Subsubsection*
21304 break [line | file:line | function | file:function]
21307 Set breakpoint at specified line or function:
21316 sdcdb>break foo.c:100
21318 sdcdb>break funcfoo
21320 sdcdb>break foo.c:funcfoo
21321 \layout Subsubsection*
21323 clear [line | file:line | function | file:function ]
21326 Clear breakpoint at specified line or function:
21335 sdcdb>clear foo.c:100
21337 sdcdb>clear funcfoo
21339 sdcdb>clear foo.c:funcfoo
21340 \layout Subsubsection*
21345 Continue program being debugged, after breakpoint.
21346 \layout Subsubsection*
21351 Execute till the end of the current function.
21352 \layout Subsubsection*
21357 Delete breakpoint number 'n'.
21358 If used without any option clear ALL user defined break points.
21359 \layout Subsubsection*
21361 info [break | stack | frame | registers ]
21364 info break - list all breakpoints
21367 info stack - show the function call stack.
21370 info frame - show information about the current execution frame.
21373 info registers - show content of all registers.
21374 \layout Subsubsection*
21379 Step program until it reaches a different source line.
21380 Note: pressing <return> repeats the last command.
21381 \layout Subsubsection*
21386 Step program, proceeding through subroutine calls.
21387 \layout Subsubsection*
21392 Start debugged program.
21393 \layout Subsubsection*
21398 Print type information of the variable.
21399 \layout Subsubsection*
21404 print value of variable.
21405 \layout Subsubsection*
21410 load the given file name.
21411 Note this is an alternate method of loading file for debugging.
21412 \layout Subsubsection*
21417 print information about current frame.
21418 \layout Subsubsection*
21423 Toggle between C source & assembly source.
21424 \layout Subsubsection*
21426 ! simulator command
21429 Send the string following '!' to the simulator, the simulator response is
21431 Note the debugger does not interpret the command being sent to the simulator,
21432 so if a command like 'go' is sent the debugger can loose its execution
21433 context and may display incorrect values.
21434 \layout Subsubsection*
21441 My name is Bobby Brown"
21444 Interfacing SDCDB with DDD
21447 The screenshot was converted from png to eps with:
21448 \begin_inset Quotes sld
21451 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
21452 \begin_inset Quotes srd
21455 which produces a pretty compact eps file which is free from compression
21459 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
21460 as this broke the build system on Sourceforge (pdf-file was broken).
21466 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
21472 shows a screenshot of a debugging session with DDD
21473 \begin_inset LatexCommand \index{DDD (debugger)}
21477 (Unix only) on a simulated 8032.
21478 The debugging session might not run as smoothly as the screenshot suggests.
21479 The debugger allows setting of breakpoints, displaying and changing variables,
21480 single stepping through C and assembler code.
21483 The source was compiled with
21506 -debug ddd_example.c
21519 and DDD was invoked with
21526 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
21529 Interfacing SDCDB with XEmacs
21530 \begin_inset LatexCommand \index{XEmacs}
21535 \begin_inset LatexCommand \index{Emacs}
21542 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
21543 sdcdb.el and sdcdbsrc.el.
21544 These two files can be found in the $(prefix)/bin directory after the installat
21546 These files need to be loaded into XEmacs for the interface to work.
21547 This can be done at XEmacs startup time by inserting the following into
21548 your '.xemacs' file (which can be found in your HOME directory):
21554 (load-file sdcdbsrc.el)
21560 .xemacs is a lisp file so the () around the command is REQUIRED.
21561 The files can also be loaded dynamically while XEmacs is running, set the
21562 environment variable 'EMACSLOADPATH' to the installation bin directory
21563 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
21564 To start the interface enter the following command:
21578 You will prompted to enter the file name to be debugged.
21583 The command line options that are passed to the simulator directly are bound
21584 to default values in the file sdcdbsrc.el.
21585 The variables are listed below, these values maybe changed as required.
21588 sdcdbsrc-cpu-type '51
21591 sdcdbsrc-frequency '11059200
21594 sdcdbsrc-serial nil
21597 The following is a list of key mapping for the debugger interface.
21608 ;;key\SpecialChar ~
21622 binding\SpecialChar ~
21646 ;;---\SpecialChar ~
21660 -------\SpecialChar ~
21702 sdcdb-next-from-src\SpecialChar ~
21730 sdcdb-back-from-src\SpecialChar ~
21758 sdcdb-cont-from-src\SpecialChar ~
21768 SDCDB continue command
21786 sdcdb-step-from-src\SpecialChar ~
21814 sdcdb-whatis-c-sexp\SpecialChar ~
21824 SDCDB ptypecommand for data at
21891 sdcdbsrc-delete\SpecialChar ~
21905 SDCDB Delete all breakpoints if no arg
21954 given or delete arg (C-u arg x)
21972 sdcdbsrc-frame\SpecialChar ~
21987 SDCDB Display current frame if no arg,
22036 given or display frame arg
22103 sdcdbsrc-goto-sdcdb\SpecialChar ~
22113 Goto the SDCDB output buffer
22131 sdcdb-print-c-sexp\SpecialChar ~
22142 SDCDB print command for data at
22209 sdcdbsrc-goto-sdcdb\SpecialChar ~
22219 Goto the SDCDB output buffer
22237 sdcdbsrc-mode\SpecialChar ~
22253 Toggles Sdcdbsrc mode (turns it off)
22268 sdcdb-finish-from-src\SpecialChar ~
22276 SDCDB finish command
22291 sdcdb-break\SpecialChar ~
22309 Set break for line with point
22324 sdcdbsrc-mode\SpecialChar ~
22340 Toggle Sdcdbsrc mode
22355 sdcdbsrc-srcmode\SpecialChar ~
22378 Here are a few guidelines that will help the compiler generate more efficient
22379 code, some of the tips are specific to this compiler others are generally
22380 good programming practice.
22383 Use the smallest data type to represent your data-value.
22384 If it is known in advance that the value is going to be less than 256 then
22385 use an 'unsigned char' instead of a 'short' or 'int'.
22386 Please note, that ANSI C requires both signed and unsigned chars to be
22387 promoted to 'signed int'
22388 \begin_inset LatexCommand \index{promotion to signed int}
22392 before doing any operation.
22394 \begin_inset LatexCommand \index{type promotion}
22399 \begin_inset LatexCommand \label{type promotion}
22403 can be omitted, if the result is the same.
22404 The effect of the promotion rules together with the sign-extension is often
22411 unsigned char uc = 0xfe;
22413 if (uc * uc < 0) /* this is true! */
22432 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
22442 (unsigned char) -12 / (signed char) -3 = ...
22445 No, the result is not 4:
22450 (int) (unsigned char) -12 / (int) (signed char) -3 =
22452 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
22454 (int) 0x00f4 / (int) 0xfffd =
22456 (int) 0x00f4 / (int) 0xfffd =
22458 (int) 244 / (int) -3 =
22460 (int) -81 = (int) 0xffaf;
22463 Don't complain, that gcc gives you a different result.
22464 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
22465 Therefore the results are different.
22468 \begin_inset Quotes sld
22472 \begin_inset Quotes srd
22478 If well-defined overflow characteristics are important and negative values
22479 are not, or if you want to steer clear of sign-extension problems when
22480 manipulating bits or bytes, use one of the corresponding unsigned types.
22481 (Beware when mixing signed and unsigned values in expressions, though.)
22483 Although character types (especially unsigned char) can be used as "tiny"
22484 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
22485 ble sign extension and increased code size.
22489 Use unsigned when it is known in advance that the value is not going to
22491 This helps especially if you are doing division or multiplication, bit-shifting
22492 or are using an array index.
22495 NEVER jump into a LOOP.
22498 Declare the variables to be local
22499 \begin_inset LatexCommand \index{local variables}
22503 whenever possible, especially loop control variables (induction).
22506 Since the compiler does not always do implicit integral promotion, the programme
22507 r should do an explicit cast when integral promotion is required.
22510 Reducing the size of division, multiplication & modulus operations can reduce
22511 code size substantially.
22512 Take the following code for example.
22518 foobar(unsigned int p1, unsigned char ch)
22526 unsigned char ch1 = p1 % ch ;
22537 For the modulus operation the variable ch will be promoted to unsigned int
22538 first then the modulus operation will be performed (this will lead to a
22539 call to support routine _moduint()), and the result will be casted to a
22541 If the code is changed to
22546 foobar(unsigned int p1, unsigned char ch)
22554 unsigned char ch1 = (unsigned char)p1 % ch ;
22565 It would substantially reduce the code generated (future versions of the
22566 compiler will be smart enough to detect such optimization opportunities).
22570 Have a look at the assembly listing to get a
22571 \begin_inset Quotes sld
22575 \begin_inset Quotes srd
22578 for the code generation.
22581 Porting code from or to other compilers
22584 check whether endianness of the compilers differs and adapt where needed.
22587 check the device specific header files for compiler specific syntax.
22588 Eventually include the file <compiler.h
22589 \begin_inset LatexCommand \index{compiler.h (include file)}
22593 > to allow using common header files.
22596 check whether the startup code contains the correct initialization (watchdog,
22600 check whether the sizes of short, int, long match.
22603 check if some 16 or 32 bit hardware registers require a specific addressing
22604 order (least significant or most significant byte first) and adapt if needed
22613 relate to time and not to lower/upper memory location here, so this is
22618 the same as endianness).
22621 check whether the keyword
22625 is used where needed.
22626 The compilers might differ in their optimization characteristics (as different
22627 versions of the same compiler might also use more clever optimizations
22628 this is good idea anyway).
22631 check that the compilers are not told to supress warnings.
22634 check and convert compiler specific extensions (interrupts, memory areas,
22638 check for differences in type promotion (especially check for math operations
22639 on char variables and for the use of the ~\SpecialChar ~
22640 operator on bit variables.
22642 \begin_inset LatexCommand \ref{type promotion}
22647 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
22654 check the assembly code generated for interrupt routines (f.e.
22655 for calls to possibly non-reentrant library functions).
22658 check whether timing loops result in proper timing (or preferably consider
22659 a rewrite of the code with timer based delays instead).
22662 check for differences in printf parameters (some compilers push (va_arg
22663 \begin_inset LatexCommand \index{va\_arg}
22667 ) char variables as integers others as char).
22670 check the resulting memory layout.
22674 \begin_inset LatexCommand \index{Tools}
22678 included in the distribution
22682 \begin_inset Tabular
22683 <lyxtabular version="3" rows="12" columns="3">
22685 <column alignment="left" valignment="top" leftline="true" width="0pt">
22686 <column alignment="left" valignment="top" leftline="true" width="0pt">
22687 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22688 <row topline="true" bottomline="true">
22689 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22714 <row topline="true">
22715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22723 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22728 Simulator for various architectures
22731 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22740 <row topline="true">
22741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22754 header file conversion
22757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22762 sdcc/support/scripts
22766 <row topline="true">
22767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22780 header file conversion
22783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22788 sdcc/support/scripts
22792 <row topline="true">
22793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22827 <row topline="true">
22828 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22844 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22862 <row topline="true">
22863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22871 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22879 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22897 <row topline="true">
22898 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22932 <row topline="true">
22933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22941 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22949 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22967 <row topline="true">
22968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23002 <row topline="true">
23003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23011 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23019 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23037 <row topline="true" bottomline="true">
23038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23082 \begin_inset LatexCommand \index{Documentation}
23086 included in the distribution
23090 \begin_inset Tabular
23091 <lyxtabular version="3" rows="10" columns="2">
23093 <column alignment="left" valignment="top" leftline="true" width="0">
23094 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23095 <row topline="true" bottomline="true">
23096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23109 Where to get / filename
23113 <row topline="true">
23114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23119 SDCC Compiler User Guide
23122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23127 You're reading it right now
23131 <row topline="true">
23132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23149 <row topline="true">
23150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23156 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
23161 \begin_inset LatexCommand \index{Assembler documentation}
23165 Assemblers and ASLINK
23166 \begin_inset LatexCommand \index{aslink}
23171 \begin_inset LatexCommand \index{Linker documentation}
23178 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23183 sdcc/as/doc/asxhtm.html
23187 <row topline="true">
23188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23193 SDCC regression test
23194 \begin_inset LatexCommand \index{Regression test}
23201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23206 sdcc/doc/test_suite_spec.pdf
23210 <row topline="true">
23211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23219 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23228 <row topline="true">
23229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23234 Notes on debugging with sdcdb
23235 \begin_inset LatexCommand \index{sdcdb (debugger)}
23242 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23247 sdcc/debugger/README
23251 <row topline="true">
23252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23257 Software simulator for microcontrollers
23260 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23287 <row topline="true">
23288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23293 Temporary notes on the pic16
23294 \begin_inset LatexCommand \index{PIC16}
23301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23306 sdcc/src/pic16/NOTES
23310 <row topline="true" bottomline="true">
23311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23316 SDCC internal documentation (debugging file format)
23319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23355 Related open source tools
23356 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
23361 \begin_inset LatexCommand \index{Related tools}
23369 \begin_inset Tabular
23370 <lyxtabular version="3" rows="14" columns="3">
23372 <column alignment="left" valignment="top" leftline="true" width="0pt">
23373 <column alignment="block" valignment="top" leftline="true" width="30line%">
23374 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23375 <row topline="true" bottomline="true">
23376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23401 <row topline="true">
23402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23408 \begin_inset LatexCommand \index{gpsim (pic simulator)}
23415 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23423 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23429 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
23437 <row topline="true">
23438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23444 \begin_inset LatexCommand \index{gputils (pic tools)}
23451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23465 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
23473 <row topline="true">
23474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23490 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23496 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
23504 <row topline="true">
23505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23518 Tools for Silicon Laboratories JTAG debug adapter, partly based on sdcdb
23522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23528 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
23536 <row topline="true">
23537 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23543 \begin_inset LatexCommand \index{indent (source formatting tool)}
23550 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23555 Formats C source - Master of the white spaces
23558 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23564 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
23572 <row topline="true">
23573 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23579 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
23586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23591 Object file conversion, checksumming, ...
23594 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23600 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
23608 <row topline="true">
23609 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23615 \begin_inset LatexCommand \index{objdump (tool)}
23622 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23627 Object file conversion, ...
23630 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23635 Part of binutils (should be there anyway)
23639 <row topline="true">
23640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23653 8051 monitor (hex up-/download, single step, disassemble)
23656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23662 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
23670 <row topline="true">
23671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23677 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
23684 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23689 Source code documentation system
23692 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23698 \begin_inset LatexCommand \url{http://www.doxygen.org}
23706 <row topline="true">
23707 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23720 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
23723 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23729 \begin_inset LatexCommand \url{http://www.kdevelop.org}
23737 <row topline="true">
23738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23751 8051 monitor (hex up-/download, single step, disassemble)
23754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23760 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
23768 <row topline="true">
23769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23775 \begin_inset LatexCommand \index{splint (syntax checking tool)}
23782 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23787 Statically checks c sources (see
23788 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
23795 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23801 \begin_inset LatexCommand \url{http://www.splint.org}
23809 <row topline="true" bottomline="true">
23810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23816 \begin_inset LatexCommand \index{ddd (debugger)}
23823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23828 Debugger, serves nicely as GUI to sdcdb
23829 \begin_inset LatexCommand \index{sdcdb (debugger)}
23836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23842 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
23859 Related documentation / recommended reading
23863 \begin_inset Tabular
23864 <lyxtabular version="3" rows="8" columns="3">
23866 <column alignment="left" valignment="top" leftline="true" width="0pt">
23867 <column alignment="block" valignment="top" leftline="true" width="30line%">
23868 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23869 <row topline="true" bottomline="true">
23870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23886 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23895 <row topline="true">
23896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23919 \begin_inset LatexCommand \index{C Reference card}
23926 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23932 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
23940 <row topline="true">
23941 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23957 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23963 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
23971 <row topline="true">
23972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23980 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23986 \begin_inset Quotes sld
23990 \begin_inset Quotes srd
23996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24004 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24012 <row topline="true">
24013 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24027 \begin_inset Quotes sld
24030 Extensions for Embedded C
24031 \begin_inset Quotes srd
24037 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24045 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
24053 <row topline="true">
24054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24061 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24066 Latest datasheet of the target CPU
24069 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24078 <row topline="true">
24079 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24091 Revision history of datasheet
24094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24103 <row topline="true" bottomline="true">
24104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24119 Advanced Compiler Design and Implementation
24122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24127 bookstore (very dedicated, probably read other books first)
24143 Some questions answered, some pointers given - it might be time to in turn
24151 can you solve your project with the selected microcontroller? Would you
24152 find out early or rather late that your target is too small/slow/whatever?
24153 Can you switch to a slightly better device if it doesn't fit?
24156 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
24157 and/or another programming language be more adequate? Would an operating
24158 system on the target device help?
24161 if you solved the problem, will the marketing department be happy?
24164 if the marketing department is happy, will customers be happy?
24167 if you're the project manager, marketing department and maybe even the customer
24168 in one person, have you tried to see the project from the outside?
24171 is the project done if you think it is done? Or is just that other interface/pro
24172 tocol/feature/configuration/option missing? How about website, manual(s),
24173 internationali(z|s)ation, packaging, labels, 2nd source for components,
24174 electromagnetic compatability/interference, documentation for production,
24175 production test software, update mechanism, patent issues?
24178 is your project adequately positioned in that magic triangle: fame, fortune,
24182 Maybe not all answers to these questions are known and some answers may
24187 , nevertheless knowing these questions may help you to avoid burnout
24193 burnout is bad for electronic devices, programmers and motorcycle tyres
24197 Chances are you didn't want to hear some of them...
24201 \begin_inset LatexCommand \index{Support}
24208 SDCC has grown to be a large project.
24209 The compiler alone (without the preprocessor, assembler and linker) is
24210 well over 100,000 lines of code (blank stripped).
24211 The open source nature of this project is a key to its continued growth
24213 You gain the benefit and support of many active software developers and
24215 Is SDCC perfect? No, that's why we need your help.
24216 The developers take pride in fixing reported bugs.
24217 You can help by reporting the bugs and helping other SDCC users.
24218 There are lots of ways to contribute, and we encourage you to take part
24219 in making SDCC a great software package.
24223 The SDCC project is hosted on the SDCC sourceforge site at
24224 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
24229 You'll find the complete set of mailing lists
24230 \begin_inset LatexCommand \index{Mailing list(s)}
24234 , forums, bug reporting system, patch submission
24235 \begin_inset LatexCommand \index{Patch submission}
24240 \begin_inset LatexCommand \index{download}
24244 area and Subversion code repository
24245 \begin_inset LatexCommand \index{Subversion code repository}
24253 \begin_inset LatexCommand \index{Bug reporting}
24258 \begin_inset LatexCommand \index{Reporting bugs}
24265 The recommended way of reporting bugs is using the infrastructure of the
24267 You can follow the status of bug reports there and have an overview about
24271 Bug reports are automatically forwarded to the developer mailing list and
24272 will be fixed ASAP.
24273 When reporting a bug, it is very useful to include a small test program
24274 (the smaller the better) which reproduces the problem.
24275 If you can isolate the problem by looking at the generated assembly code,
24276 this can be very helpful.
24277 Compiling your program with the -
24288 \begin_inset LatexCommand \index{-\/-dumpall}
24292 option can sometimes be useful in locating optimization problems.
24293 When reporting a bug please maker sure you:
24296 Attach the code you are compiling with SDCC.
24300 Specify the exact command you use to run SDCC, or attach your Makefile.
24304 Specify the SDCC version (type "
24310 "), your platform, and operating system.
24314 Provide an exact copy of any error message or incorrect output.
24318 Put something meaningful in the subject of your message.
24321 Please attempt to include these 5 important parts, as applicable, in all
24322 requests for support or when reporting any problems or bugs with SDCC.
24323 Though this will make your message lengthy, it will greatly improve your
24324 chance that SDCC users and developers will be able to help you.
24325 Some SDCC developers are frustrated by bug reports without code provided
24326 that they can use to reproduce and ultimately fix the problem, so please
24327 be sure to provide sample code if you are reporting a bug!
24330 Please have a short check that you are using a recent version of SDCC and
24331 the bug is not yet known.
24332 This is the link for reporting bugs:
24333 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
24340 Requesting Features
24341 \begin_inset LatexCommand \label{sub:Requesting-Features}
24346 \begin_inset LatexCommand \index{Feature request}
24351 \begin_inset LatexCommand \index{Requesting features}
24358 Like bug reports feature requests are forwarded to the developer mailing
24360 This is the link for requesting features:
24361 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24371 Like bug reports contributed patches are forwarded to the developer mailing
24373 This is the link for submitting patches
24374 \begin_inset LatexCommand \index{Patch submission}
24379 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
24386 You need to specify some parameters to the
24390 command for the patches to be useful.
24391 If you modified more than one file a patch created f.e.
24396 \begin_inset Quotes sld
24399 diff -Naur unmodified_directory modified_directory >my_changes.patch
24400 \begin_inset Quotes srd
24406 will be fine, otherwise
24410 \begin_inset Quotes sld
24413 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
24414 \begin_inset Quotes srd
24427 These links should take you directly to the
24428 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
24438 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
24439 automated messages (mid 2003)
24443 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
24448 \begin_inset LatexCommand \index{Mailing list(s)}
24452 and forums are archived and searchable so if you are lucky someone already
24453 had a similar problem.
24454 While mails to the lists themselves are delivered promptly their web front
24455 end on sourceforge sometimes shows a severe time lag (up to several weeks),
24456 if you're seriously using SDCC please consider subscribing to the lists.
24462 You can follow the status of the Subversion version
24463 \begin_inset LatexCommand \index{version}
24467 of SDCC by watching the Changelog
24468 \begin_inset LatexCommand \index{Changelog}
24472 in the Subversion repository
24475 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
24482 Subversion Source Code Repository
24491 or the filenames of the snapshot versions of SDCC include date and its
24493 \begin_inset LatexCommand \index{Subversion}
24498 Subversion allows to download the source of recent or previous versions
24500 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
24504 (by number or by date).
24505 An on-line source code browser and detailled instructions are also available
24507 SDCC versions starting from 1999 up to now are available (currently the
24508 versions prior to the conversion from cvs to Subversion (April 2006) are
24509 either by accessible by Subversion or by cvs).
24513 \begin_inset LatexCommand \index{Release policy}
24520 Historically there often were long delays between official releases and
24521 the sourceforge download area tends to get not updated at all.
24522 Excuses in the past might have referred to problems with live range analysis,
24523 but as this was fixed a while ago, the current problem is that another
24524 excuse has to be found.
24525 Kidding aside, we have to get better there! On the other hand there are
24526 daily snapshots available at
24527 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
24531 , and you can always build the very last version (hopefully with many bugs
24532 fixed, and features added) from the source code available at
24533 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
24541 \begin_inset LatexCommand \index{Examples}
24548 You'll find some small examples in the directory
24550 sdcc/device/examples/.
24553 More examples and libraries are available at
24555 The SDCC Open Knowledge Resource
24556 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
24563 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
24570 I did insert a reference to Paul's web site here although it seems rather
24571 dedicated to a specific 8032 board (I think it's okay because it f.e.
24572 shows LCD/Harddisc interface and has a free 8051 monitor.
24573 Independent 8032 board vendors face hard competition of heavily subsidized
24574 development boards anyway).
24577 Maybe we should include some links to real world applications.
24578 Preferably pointer to pointers (one for each architecture) so this stays
24583 \begin_inset LatexCommand \index{Quality control}
24590 The compiler is passed through nightly compile and build checks.
24596 \begin_inset LatexCommand \index{Regression test}
24600 check that SDCC itself compiles flawlessly on several platforms and checks
24601 the quality of the code generated by SDCC by running the code through simulator
24603 There is a separate document
24606 \begin_inset LatexCommand \index{Test suite}
24615 You'll find the test code in the directory
24617 sdcc/support/regression
24620 You can run these tests manually by running
24624 in this directory (or f.e.
24629 \begin_inset Quotes sld
24633 \begin_inset Quotes srd
24639 if you don't want to run the complete tests).
24640 The test code might also be interesting if you want to look for examples
24641 \begin_inset LatexCommand \index{Examples}
24645 checking corner cases of SDCC or if you plan to submit patches
24646 \begin_inset LatexCommand \index{Patch submission}
24653 The pic port uses a different set of regression tests, you'll find them
24656 sdcc/src/regression
24661 Use of SDCC in Education
24674 the phrase "use in education" might evoke the association "
24678 fit for use in education".
24679 This connotation is not intended but nevertheless risked as the licensing
24680 of SDCC makes it difficult to offer educational discounts
24684 If your rationales are to:
24687 give students a chance to understand the
24691 steps of code generation
24694 have a curriculum that can be extended for years.
24695 Then you could use an fpga board as target and your curriculum will seamlessly
24696 extend from logic synthesis (
24697 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
24702 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
24706 ), over assembly programming, to C to FPGA compilers (
24707 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
24714 be able to insert excursions about skills like using a revision control
24715 system, submitting/applying patches, using a type-setting (as opposed to
24716 word-processing) engine LyX/LaTeX, using
24717 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
24722 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
24726 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
24727 Source Software, CPU simulation, compiler regression tests
24728 \begin_inset LatexCommand \index{Regression test}
24735 And if there should be a shortage of ideas then you can always point students
24736 to the ever-growing feature request list
24737 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24744 not tie students to a specific host platform and instead allow them to use
24749 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
24751 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
24758 not encourage students to use illegal copies of educational software
24761 be immune to licensing/availability/price changes of the chosen tool chain
24764 be able to change to a new target platform without having to adopt a new
24768 have complete control over and insight into the tool chain
24771 make your students aware about the pros and cons of open source software
24775 give back to the public as you are probably at least partially publically
24779 give students a chance to publically prove their skills and to possibly
24780 see a world wide impact
24783 then SDCC is probably among the first choices.
24784 Well, probably SDCC might be the only choice.
24787 SDCC Technical Data
24791 \begin_inset LatexCommand \index{Optimizations}
24798 SDCC performs a host of standard optimizations in addition to some MCU specific
24803 Sub-expression Elimination
24804 \begin_inset LatexCommand \index{Subexpression elimination}
24811 The compiler does local and
24837 will be translated to
24849 Some subexpressions are not as obvious as the above example, e.g.:
24859 In this case the address arithmetic a->b[i] will be computed only once;
24860 the equivalent code in C would be.
24872 The compiler will try to keep these temporary variables in registers.
24875 Dead-Code Elimination
24876 \begin_inset LatexCommand \index{Dead-code elimination}
24897 i = 1; \SpecialChar ~
24906 global = 1;\SpecialChar ~
24919 global = 3;\SpecialChar ~
24944 \begin_inset LatexCommand \index{Copy propagation}
25000 Note: the dead stores created by this copy propagation will be eliminated
25001 by dead-code elimination.
25005 \begin_inset LatexCommand \index{Loop optimization}
25010 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
25017 Two types of loop optimizations are done by SDCC
25025 of loop induction variables.
25026 In addition to the strength reduction the optimizer marks the induction
25027 variables and the register allocator tries to keep the induction variables
25028 in registers for the duration of the loop.
25029 Because of this preference of the register allocator
25030 \begin_inset LatexCommand \index{Register allocation}
25034 , loop induction optimization causes an increase in register pressure, which
25035 may cause unwanted spilling of other temporary variables into the stack
25036 \begin_inset LatexCommand \index{stack}
25041 The compiler will generate a warning message when it is forced to allocate
25042 extra space either on the stack or data space.
25043 If this extra space allocation is undesirable then induction optimization
25044 can be eliminated either for the entire source file (with -
25054 -noinduction option) or for a given function only using #pragma\SpecialChar ~
25056 \begin_inset LatexCommand \index{\#pragma noinduction}
25069 for (i = 0 ; i < 100 ; i ++)
25085 for (i = 0; i < 100; i++)
25094 As mentioned previously some loop invariants are not as apparent, all static
25095 address computations are also moved out of the loop.
25100 \begin_inset LatexCommand \index{Strength reduction}
25104 , this optimization substitutes an expression by a cheaper expression:
25109 for (i=0;i < 100; i++)
25127 for (i=0;i< 100;i++) {
25133 ar[itemp1] = itemp2;
25150 The more expensive multiplication
25151 \begin_inset LatexCommand \index{Multiplication}
25155 is changed to a less expensive addition.
25159 \begin_inset LatexCommand \index{Loop reversing}
25166 This optimization is done to reduce the overhead of checking loop boundaries
25167 for every iteration.
25168 Some simple loops can be reversed and implemented using a
25169 \begin_inset Quotes eld
25172 decrement and jump if not zero
25173 \begin_inset Quotes erd
25177 SDCC checks for the following criterion to determine if a loop is reversible
25178 (note: more sophisticated compilers use data-dependency analysis to make
25179 this determination, SDCC uses a more simple minded analysis).
25182 The 'for' loop is of the form
25188 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
25198 The <for body> does not contain
25199 \begin_inset Quotes eld
25203 \begin_inset Quotes erd
25207 \begin_inset Quotes erd
25213 All goto's are contained within the loop.
25216 No function calls within the loop.
25219 The loop control variable <sym> is not assigned any value within the loop
25222 The loop control variable does NOT participate in any arithmetic operation
25226 There are NO switch statements in the loop.
25229 Algebraic Simplifications
25232 SDCC does numerous algebraic simplifications, the following is a small sub-set
25233 of these optimizations.
25238 i = j + 0;\SpecialChar ~
25242 /* changed to: */\SpecialChar ~
25248 i /= 2;\SpecialChar ~
25255 /* changed to: */\SpecialChar ~
25261 i = j - j;\SpecialChar ~
25265 /* changed to: */\SpecialChar ~
25271 i = j / 1;\SpecialChar ~
25275 /* changed to: */\SpecialChar ~
25282 Note the subexpressions
25283 \begin_inset LatexCommand \index{Subexpression}
25287 given above are generally introduced by macro expansions or as a result
25288 of copy/constant propagation.
25291 'switch' Statements
25292 \begin_inset LatexCommand \label{sub:'switch'-Statements}
25297 \begin_inset LatexCommand \index{switch statement}
25304 SDCC can optimize switch statements to jump tables
25305 \begin_inset LatexCommand \index{jump tables}
25310 It makes the decision based on an estimate of the generated code size.
25311 SDCC is quite liberal in the requirements for jump table generation:
25314 The labels need not be in order, and the starting number need not be one
25315 or zero, the case labels are in numerical sequence or not too many case
25316 labels are missing.
25322 switch(i) {\SpecialChar ~
25353 case 4: ...\SpecialChar ~
25385 case 5: ...\SpecialChar ~
25417 case 3: ...\SpecialChar ~
25448 case 6: ...\SpecialChar ~
25480 case 7: ...\SpecialChar ~
25512 case 8: ...\SpecialChar ~
25544 case 9: ...\SpecialChar ~
25576 case 10: ...\SpecialChar ~
25607 case 11: ...\SpecialChar ~
25674 Both the above switch statements will be implemented using a jump-table.
25675 The example to the right side is slightly more efficient as the check for
25676 the lower boundary of the jump-table is not needed.
25680 The number of case labels is not larger than supported by the target architectur
25684 If the case labels are not in numerical sequence ('gaps' between cases)
25685 SDCC checks whether a jump table with additionally inserted dummy cases
25686 is still attractive.
25690 If the starting number is not zero and a check for the lower boundary of
25691 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
25696 Switch statements which have large gaps in the numeric sequence or those
25697 that have too many case labels can be split into more than one switch statement
25698 for efficient code generation, e.g.:
25778 If the above switch statement is broken down into two switch statements
25868 then both the switch statements will be implemented using jump-tables whereas
25869 the unmodified switch statement will not be.
25872 There might be reasons which SDCC cannot know about to either favour or
25873 not favour jump tables.
25874 If the target system has to be as quick for the last switch case as for
25875 the first (pro jump table), or if the switch argument is known to be zero
25876 in the majority of the cases (contra jump table).
25879 The pragma nojtbound
25880 \begin_inset LatexCommand \index{\#pragma nojtbound}
25884 can be used to turn off checking the
25897 It has no effect if a default label is supplied.
25898 Use of this pragma is dangerous: if the switch
25899 \begin_inset LatexCommand \index{switch statement}
25903 argument is not matched by a case statement the processor will happily
25907 Bit-shifting Operations
25908 \begin_inset LatexCommand \index{Bit shifting}
25915 Bit shifting is one of the most frequently used operation in embedded programmin
25917 SDCC tries to implement bit-shift operations in the most efficient way
25933 generates the following code:
25950 In general SDCC will never setup a loop if the shift count is known.
25993 \begin_inset LatexCommand \index{Bit rotation}
26000 A special case of the bit-shift operation is bit rotation
26001 \begin_inset LatexCommand \index{rotating bits}
26005 , SDCC recognizes the following expression to be a left bit-rotation:
26015 char i;\SpecialChar ~
26026 /* unsigned is needed for rotation */
26031 i = ((i << 1) | (i >> 7));
26040 will generate the following code:
26059 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
26060 ns of this case will also be recognized as bit-rotation, i.e.:
26065 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
26068 Nibble and Byte Swapping
26071 Other special cases of the bit-shift operations are nibble or byte swapping
26072 \begin_inset LatexCommand \index{swapping nibbles/bytes}
26076 , SDCC recognizes the following expressions:
26099 i = ((i << 4) | (i >> 4));
26105 j = ((j << 8) | (j >> 8));
26108 and generates a swap instruction for the nibble swapping
26109 \begin_inset LatexCommand \index{Nibble swapping}
26113 or move instructions for the byte swapping
26114 \begin_inset LatexCommand \index{Byte swapping}
26120 \begin_inset Quotes sld
26124 \begin_inset Quotes srd
26127 example can be used to convert from little to big-endian or vice versa.
26128 If you want to change the endianness of a
26132 integer you have to cast to
26139 Note that SDCC stores numbers in little-endian
26145 Usually 8-bit processors don't care much about endianness.
26146 This is not the case for the standard 8051 which only has an instruction
26152 \begin_inset LatexCommand \index{DPTR}
26160 so little-endian is the more efficient byte order.
26164 \begin_inset LatexCommand \index{little-endian}
26169 \begin_inset LatexCommand \index{Endianness}
26174 lowest order first).
26178 \begin_inset LatexCommand \index{Highest Order Bit}
26183 \begin_inset LatexCommand \index{Any Order Bit}
26190 It is frequently required to obtain the highest order bit of an integral
26191 type (long, int, short or char types).
26192 Also obtaining any other order bit is not uncommon.
26193 SDCC recognizes the following expressions to yield the highest order bit
26194 and generates optimized code for it, e.g.:
26207 unsigned char hob1, aob1;
26211 bit hob2, hob3, aob2, aob3;
26220 hob1 = (gint >> 15) & 1;
26224 hob2 = (gint >> 15) & 1;
26228 hob3 = gint & 0x8000;
26232 aob1 = (gint >> 9) & 1;
26236 aob2 = (gint >> 8) & 1;
26240 aob3 = gint & 0x0800;
26250 will generate the following code:
26283 000A E5*01\SpecialChar ~
26310 000C 23\SpecialChar ~
26341 000D 54 01\SpecialChar ~
26368 000F F5*02\SpecialChar ~
26423 0011 E5*01\SpecialChar ~
26450 0013 33\SpecialChar ~
26480 0014 92*00\SpecialChar ~
26535 0016 E5*01\SpecialChar ~
26562 0018 33\SpecialChar ~
26592 0019 92*01\SpecialChar ~
26647 001B E5*01\SpecialChar ~
26674 001D 03\SpecialChar ~
26705 001E 54 01\SpecialChar ~
26732 0020 F5*03\SpecialChar ~
26787 0022 E5*01\SpecialChar ~
26814 0024 13\SpecialChar ~
26844 0025 92*02\SpecialChar ~
26899 0027 E5*01\SpecialChar ~
26926 0029 A2 E3\SpecialChar ~
26953 002B 92*03\SpecialChar ~
26981 Other variations of these cases however will
26986 They are standard C expressions, so I heartily recommend these be the only
26987 way to get the highest order bit, (it is portable).
26988 Of course it will be recognized even if it is embedded in other expressions,
26994 xyz = gint + ((gint >> 15) & 1);
26997 will still be recognized.
27001 \begin_inset LatexCommand \index{Higher Order Byte}
27005 / Higher Order Word
27006 \begin_inset LatexCommand \index{Higher Order Word}
27013 It is also frequently required to obtain a higher order byte or word of
27014 a larger integral type (long, int or short types).
27015 SDCC recognizes the following expressions to yield the higher order byte
27016 or word and generates optimized code for it, e.g.:
27023 unsigned long int glong;
27031 unsigned char hob1, hob2;
27035 unsigned int how1, how2;
27044 hob1 = (gint >> 8) & 0xFF;
27048 hob2 = glong >> 24;
27052 how1 = (glong >> 16) & 0xFFFF;
27066 will generate the following code:
27099 0037 85*01*06\SpecialChar ~
27121 _foo_hob1_1_1,(_gint + 1)
27151 003A 85*05*07\SpecialChar ~
27173 _foo_hob2_1_1,(_glong + 3)
27203 003D 85*04*08\SpecialChar ~
27225 _foo_how1_1_1,(_glong + 2)
27227 0040 85*05*09\SpecialChar ~
27249 (_foo_how1_1_1 + 1),(_glong + 3)
27251 0043 85*03*0A\SpecialChar ~
27273 _foo_how2_1_1,(_glong + 1)
27275 0046 85*04*0B\SpecialChar ~
27297 (_foo_how2_1_1 + 1),(_glong + 2)
27300 Again, variations of these cases may
27305 They are standard C expressions, so I heartily recommend these be the only
27306 way to get the higher order byte/word, (it is portable).
27307 Of course it will be recognized even if it is embedded in other expressions,
27313 xyz = gint + ((gint >> 8) & 0xFF);
27316 will still be recognized.
27320 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
27325 \begin_inset LatexCommand \index{Peephole optimizer}
27332 The compiler uses a rule based, pattern matching and re-writing mechanism
27333 for peep-hole optimization.
27338 a peep-hole optimizer by Christopher W.
27339 Fraser (cwfraser\SpecialChar ~
27342 A default set of rules are compiled into the compiler, additional rules
27343 may be added with the
27356 \begin_inset LatexCommand \index{-\/-peep-file}
27363 The rule language is best illustrated with examples.
27387 The above rule will change the following assembly
27388 \begin_inset LatexCommand \index{Assembler routines}
27410 Note: All occurrences of a
27414 (pattern variable) must denote the same string.
27415 With the above rule, the assembly sequence:
27425 will remain unmodified.
27429 Other special case optimizations may be added by the user (via
27445 some variants of the 8051 MCU
27446 \begin_inset LatexCommand \index{MCS51 variants}
27459 The following two rules will change all
27478 replace { lcall %1 } by { acall %1 }
27480 replace { ljmp %1 } by { ajmp %1 }
27485 inline-assembler code
27487 is also passed through the peep hole optimizer, thus the peephole optimizer
27488 can also be used as an assembly level macro expander.
27489 The rules themselves are MCU dependent whereas the rule language infra-structur
27490 e is MCU independent.
27491 Peephole optimization rules for other MCU can be easily programmed using
27496 The syntax for a rule is as follows:
27501 rule := replace [ restart ] '{' <assembly sequence> '
27539 <assembly sequence> '
27557 '}' [if <functionName> ] '
27562 <assembly sequence> := assembly instruction (each instruction including
27563 labels must be on a separate line).
27567 The optimizer will apply to the rules one by one from the top in the sequence
27568 of their appearance, it will terminate when all rules are exhausted.
27569 If the 'restart' option is specified, then the optimizer will start matching
27570 the rules again from the top, this option for a rule is expensive (performance)
27571 , it is intended to be used in situations where a transformation will trigger
27572 the same rule again.
27573 An example of this (not a good one, it has side effects) is the following
27596 Note that the replace pattern cannot be a blank, but can be a comment line.
27597 Without the 'restart' option only the innermost 'pop' 'push' pair would
27598 be eliminated, i.e.:
27628 the restart option the rule will be applied again to the resulting code
27629 and then all the pop-push pairs will be eliminated to yield:
27639 A conditional function can be attached to a rule.
27640 Attaching rules are somewhat more involved, let me illustrate this with
27667 The optimizer does a look-up of a function name table defined in function
27672 in the source file SDCCpeeph.c, with the name
27677 If it finds a corresponding entry the function is called.
27678 Note there can be no parameters specified for these functions, in this
27683 is crucial, since the function
27687 expects to find the label in that particular variable (the hash table containin
27688 g the variable bindings is passed as a parameter).
27689 If you want to code more such functions, take a close look at the function
27690 labelInRange and the calling mechanism in source file SDCCpeeph.c.
27691 Currently implemented are
27693 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
27694 24bitMode, portIsDS390, 24bitModeAndPortDS390
27703 I know this whole thing is a little kludgey, but maybe some day we will
27704 have some better means.
27705 If you are looking at this file, you will see the default rules that are
27706 compiled into the compiler, you can add your own rules in the default set
27707 there if you get tired of specifying the -
27721 \begin_inset LatexCommand \index{ANSI-compliance}
27726 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
27733 The latest publically available version of the standard
27735 ISO/IEC 9899 - Programming languages - C
27737 should be available at:
27738 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
27747 Deviations from the compliance:
27750 functions are not reentrant
27751 \begin_inset LatexCommand \index{reentrant}
27755 unless explicitly declared as such or the
27768 \begin_inset LatexCommand \index{-\/-stack-auto}
27774 command line option is specified.
27777 structures and unions cannot be assigned values directly, cannot be passed
27778 as function parameters or assigned to each other and cannot be a return
27779 value from a function, e.g.:
27805 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
27816 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
27838 return rets;/* is invalid in SDCC although allowed in ANSI */
27844 initialization of structure arrays must be fully braced.
27850 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
27852 struct s { char x } a[] = {{1}, {2}}; /* OK */
27857 \begin_inset LatexCommand \index{long long (not supported)}
27862 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
27870 \begin_inset LatexCommand \index{double (not supported)}
27874 ' precision floating point
27875 \begin_inset LatexCommand \index{Floating point support}
27883 \begin_inset LatexCommand \index{K\&R style}
27887 function declarations are NOT allowed.
27893 foo(i,j) /* this old style of function declarations */
27895 int i,j; /* are valid in ANSI but not valid in SDCC */
27910 Most enhancements in C99 are not supported, f.e.:
27919 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
27926 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
27930 Certain words that are valid identifiers in the standard may be reserved
27931 words in SDCC unless the
27944 \begin_inset LatexCommand \index{-\/-std-c89}
27959 \begin_inset LatexCommand \index{-\/-std-c99}
27965 command line options are used.
27966 These may include (depending on the selected processor): 'at', 'banked',
27967 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
27968 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
27969 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
27971 Compliant equivalents of these keywords are always available in a form
27972 that begin with two underscores
27973 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
27978 '__data' instead of 'data'.
27981 Cyclomatic Complexity
27982 \begin_inset LatexCommand \index{Cyclomatic complexity}
27989 Cyclomatic complexity of a function is defined as the number of independent
27990 paths the program can take during execution of the function.
27991 This is an important number since it defines the number test cases you
27992 have to generate to validate the function.
27993 The accepted industry standard for complexity number is 10, if the cyclomatic
27994 complexity reported by SDCC exceeds 10 you should think about simplification
27995 of the function logic.
27996 Note that the complexity level is not related to the number of lines of
27997 code in a function.
27998 Large functions can have low complexity, and small functions can have large
28004 SDCC uses the following formula to compute the complexity:
28009 complexity = (number of edges in control flow graph) - (number of nodes
28010 in control flow graph) + 2;
28014 Having said that the industry standard is 10, you should be aware that in
28015 some cases it be may unavoidable to have a complexity level of less than
28017 For example if you have switch statement with more than 10 case labels,
28018 each case label adds one to the complexity level.
28019 The complexity level is by no means an absolute measure of the algorithmic
28020 complexity of the function, it does however provide a good starting point
28021 for which functions you might look at for further optimization.
28024 Retargetting for other Processors
28027 The issues for retargetting the compiler are far too numerous to be covered
28029 What follows is a brief description of each of the seven phases of the
28030 compiler and its MCU dependency.
28033 Parsing the source and building the annotated parse tree.
28034 This phase is largely MCU independent (except for the language extensions).
28035 Syntax & semantic checks are also done in this phase, along with some initial
28036 optimizations like back patching labels and the pattern matching optimizations
28037 like bit-rotation etc.
28040 The second phase involves generating an intermediate code which can be easy
28041 manipulated during the later phases.
28042 This phase is entirely MCU independent.
28043 The intermediate code generation assumes the target machine has unlimited
28044 number of registers, and designates them with the name iTemp.
28045 The compiler can be made to dump a human readable form of the code generated
28059 This phase does the bulk of the standard optimizations and is also MCU independe
28061 This phase can be broken down into several sub-phases:
28065 Break down intermediate code (iCode) into basic blocks.
28067 Do control flow & data flow analysis on the basic blocks.
28069 Do local common subexpression elimination, then global subexpression elimination
28071 Dead code elimination
28075 If loop optimizations caused any changes then do 'global subexpression eliminati
28076 on' and 'dead code elimination' again.
28079 This phase determines the live-ranges; by live range I mean those iTemp
28080 variables defined by the compiler that still survive after all the optimization
28082 Live range analysis
28083 \begin_inset LatexCommand \index{Live range analysis}
28087 is essential for register allocation, since these computation determines
28088 which of these iTemps will be assigned to registers, and for how long.
28091 Phase five is register allocation.
28092 There are two parts to this process.
28096 The first part I call 'register packing' (for lack of a better term).
28097 In this case several MCU specific expression folding is done to reduce
28102 The second part is more MCU independent and deals with allocating registers
28103 to the remaining live ranges.
28104 A lot of MCU specific code does creep into this phase because of the limited
28105 number of index registers available in the 8051.
28108 The Code generation phase is (unhappily), entirely MCU dependent and very
28109 little (if any at all) of this code can be reused for other MCU.
28110 However the scheme for allocating a homogenized assembler operand for each
28111 iCode operand may be reused.
28114 As mentioned in the optimization section the peep-hole optimizer is rule
28115 based system, which can reprogrammed for other MCUs.
28119 \begin_inset LatexCommand \index{Compiler internals}
28126 The anatomy of the compiler
28127 \begin_inset LatexCommand \label{sub:The-anatomy-of}
28136 This is an excerpt from an article published in Circuit Cellar Magazine
28142 It's a little outdated (the compiler is much more efficient now and user/develo
28143 per friendly), but pretty well exposes the guts of it all.
28149 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
28150 It is fairly easy to retarget for other 8-bit MCU.
28151 Here we take a look at some of the internals of the compiler.
28156 \begin_inset LatexCommand \index{Parsing}
28163 Parsing the input source file and creating an AST (Annotated Syntax Tree
28164 \begin_inset LatexCommand \index{Annotated syntax tree}
28169 This phase also involves propagating types (annotating each node of the
28170 parse tree with type information) and semantic analysis.
28171 There are some MCU specific parsing rules.
28172 For example the storage classes, the extended storage classes are MCU specific
28173 while there may be a xdata storage class for 8051 there is no such storage
28174 class for z80 or Atmel AVR.
28175 SDCC allows MCU specific storage class extensions, i.e.
28176 xdata will be treated as a storage class specifier when parsing 8051 C
28177 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
28182 \begin_inset LatexCommand \index{iCode}
28189 Intermediate code generation.
28190 In this phase the AST is broken down into three-operand form (iCode).
28191 These three operand forms are represented as doubly linked lists.
28192 ICode is the term given to the intermediate form generated by the compiler.
28193 ICode example section shows some examples of iCode generated for some simple
28194 C source functions.
28198 \begin_inset LatexCommand \index{Optimizations}
28205 Bulk of the target independent optimizations is performed in this phase.
28206 The optimizations include constant propagation, common sub-expression eliminati
28207 on, loop invariant code movement, strength reduction of loop induction variables
28208 and dead-code elimination.
28211 Live range analysis
28212 \begin_inset LatexCommand \index{Live range analysis}
28219 During intermediate code generation phase, the compiler assumes the target
28220 machine has infinite number of registers and generates a lot of temporary
28222 The live range computation determines the lifetime of each of these compiler-ge
28223 nerated temporaries.
28224 A picture speaks a thousand words.
28225 ICode example sections show the live range annotations for each of the
28227 It is important to note here, each iCode is assigned a number in the order
28228 of its execution in the function.
28229 The live ranges are computed in terms of these numbers.
28230 The from number is the number of the iCode which first defines the operand
28231 and the to number signifies the iCode which uses this operand last.
28234 Register Allocation
28235 \begin_inset LatexCommand \index{Register allocation}
28242 The register allocation determines the type and number of registers needed
28244 In most MCUs only a few registers can be used for indirect addressing.
28245 In case of 8051 for example the registers R0 & R1 can be used to indirectly
28246 address the internal ram and DPTR to indirectly address the external ram.
28247 The compiler will try to allocate the appropriate register to pointer variables
28249 ICode example section shows the operands annotated with the registers assigned
28251 The compiler will try to keep operands in registers as much as possible;
28252 there are several schemes the compiler uses to do achieve this.
28253 When the compiler runs out of registers the compiler will check to see
28254 if there are any live operands which is not used or defined in the current
28255 basic block being processed, if there are any found then it will push that
28256 operand and use the registers in this block, the operand will then be popped
28257 at the end of the basic block.
28261 There are other MCU specific considerations in this phase.
28262 Some MCUs have an accumulator; very short-lived operands could be assigned
28263 to the accumulator instead of a general-purpose register.
28269 Figure II gives a table of iCode operations supported by the compiler.
28270 The code generation involves translating these operations into corresponding
28271 assembly code for the processor.
28272 This sounds overly simple but that is the essence of code generation.
28273 Some of the iCode operations are generated on a MCU specific manner for
28274 example, the z80 port does not use registers to pass parameters so the
28275 SEND and RECV iCode operations will not be generated, and it also does
28276 not support JUMPTABLES.
28283 <Where is Figure II?>
28286 In the original article Figure II was announced to be downloadable on
28291 Unfortunately it never seemed to have shown up there, so: where is Figure
28296 \begin_inset LatexCommand \index{iCode}
28303 This section shows some details of iCode.
28304 The example C code does not do anything useful; it is used as an example
28305 to illustrate the intermediate code generated by the compiler.
28317 /* This function does nothing useful.
28324 for the purpose of explaining iCode */
28327 short function (data int *x)
28335 short i=10; \SpecialChar ~
28337 /* dead initialization eliminated */
28342 short sum=10; /* dead initialization eliminated */
28355 while (*x) *x++ = *p++;
28369 /* compiler detects i,j to be induction variables */
28373 for (i = 0, j = 10 ; i < 10 ; i++, j
28399 mul += i * 3; \SpecialChar ~
28401 /* this multiplication remains */
28407 gint += j * 3;\SpecialChar ~
28409 /* this multiplication changed to addition */
28423 In addition to the operands each iCode contains information about the filename
28424 and line it corresponds to in the source file.
28425 The first field in the listing should be interpreted as follows:
28430 Filename(linenumber: iCode Execution sequence number : ICode hash table
28431 key : loop depth of the iCode).
28436 Then follows the human readable form of the ICode operation.
28437 Each operand of this triplet form can be of three basic types a) compiler
28438 generated temporary b) user defined variable c) a constant value.
28439 Note that local variables and parameters are replaced by compiler generated
28442 \begin_inset LatexCommand \index{Live range analysis}
28446 are computed only for temporaries (i.e.
28447 live ranges are not computed for global variables).
28449 \begin_inset LatexCommand \index{Register allocation}
28453 are allocated for temporaries only.
28454 Operands are formatted in the following manner:
28459 Operand Name [lr live-from : live-to ] { type information } [ registers
28465 As mentioned earlier the live ranges are computed in terms of the execution
28466 sequence number of the iCodes, for example
28468 the iTemp0 is live from (i.e.
28469 first defined in iCode with execution sequence number 3, and is last used
28470 in the iCode with sequence number 5).
28471 For induction variables such as iTemp21 the live range computation extends
28472 the lifetime from the start to the end of the loop.
28474 The register allocator used the live range information to allocate registers,
28475 the same registers may be used for different temporaries if their live
28476 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
28477 iTemp17 since their live ranges do not overlap.
28478 In addition the allocator also takes into consideration the type and usage
28479 of a temporary, for example itemp6 is a pointer to near space and is used
28480 as to fetch data from (i.e.
28481 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
28482 Some short lived temporaries are allocated to special registers which have
28483 meaning to the code generator e.g.
28484 iTemp13 is allocated to a pseudo register CC which tells the back end that
28485 the temporary is used only for a conditional jump the code generation makes
28486 use of this information to optimize a compare and jump ICode.
28488 There are several loop optimizations
28489 \begin_inset LatexCommand \index{Loop optimization}
28493 performed by the compiler.
28494 It can detect induction variables iTemp21(i) and iTemp23(j).
28495 Also note the compiler does selective strength reduction
28496 \begin_inset LatexCommand \index{Strength reduction}
28501 the multiplication of an induction variable in line 18 (gint = j * 3) is
28502 changed to addition, a new temporary iTemp17 is allocated and assigned
28503 a initial value, a constant 3 is then added for each iteration of the loop.
28504 The compiler does not change the multiplication
28505 \begin_inset LatexCommand \index{Multiplication}
28509 in line 17 however since the processor does support an 8 * 8 bit multiplication.
28511 Note the dead code elimination
28512 \begin_inset LatexCommand \index{Dead-code elimination}
28516 optimization eliminated the dead assignments in line 7 & 8 to I and sum
28524 Sample.c (5:1:0:0) _entry($9) :
28529 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
28534 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
28539 Sample.c(11:4:53:0) preHeaderLbl0($11) :
28544 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
28550 Sample.c(11:6:5:1) _whilecontinue_0($1) :
28555 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
28561 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28566 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
28572 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
28578 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
28584 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
28590 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
28591 * int}[r0] + 0x2 {short}
28596 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
28601 Sample.c(11:17:21:0)_whilebreak_0($3) :
28606 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
28611 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
28616 Sample.c(15:20:54:0)preHeaderLbl1($13) :
28621 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
28626 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
28631 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
28636 Sample.c(15:24:26:1)_forcond_0($4) :
28641 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
28647 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28652 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
28653 + ITemp21 [lr21:38]{short}[r4]
28658 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
28664 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
28665 + iTemp15 [lr29:30]{short}[r1]
28670 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
28676 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
28682 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
28688 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
28694 Sample.c(19:38:47:1) goto _forcond_0($4)
28699 Sample.c(19:39:48:0)_forbreak_0($7) :
28704 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
28705 + ITemp11 [lr19:40]{short}[r3]
28710 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
28715 Sample.c(20:42:51:0)_return($8) :
28720 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
28726 Finally the code generated for this function:
28767 ; ----------------------------------------------
28772 ; function function
28777 ; ----------------------------------------------
28787 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
28799 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
28811 ;_whilecontinue_0($1) :
28821 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
28826 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28885 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
28904 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
28951 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
28991 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
29017 ; iTemp6 [lr5:16]{_near * int}[r0] =
29022 ; iTemp6 [lr5:16]{_near * int}[r0] +
29039 ; goto _whilecontinue_0($1)
29051 ; _whilebreak_0($3) :
29061 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29073 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29085 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29097 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29116 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29145 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
29150 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29195 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
29200 ; iTemp21 [lr21:38]{short}[r4]
29226 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
29259 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
29264 ; iTemp15 [lr29:30]{short}[r1]
29283 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
29330 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
29377 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
29389 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
29403 cjne r5,#0xff,00104$
29415 ; goto _forcond_0($4)
29427 ; _forbreak_0($7) :
29437 ; ret iTemp24 [lr40:41]{short}
29480 A few words about basic block successors, predecessors and dominators
29483 Successors are basic blocks
29484 \begin_inset LatexCommand \index{Basic blocks}
29488 that might execute after this basic block.
29490 Predecessors are basic blocks that might execute before reaching this basic
29493 Dominators are basic blocks that WILL execute before reaching this basic
29527 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
29530 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
29533 c) domVect of [BB4] = BB1 ...
29534 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
29542 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
29552 Thanks to all the other volunteer developers who have helped with coding,
29553 testing, web-page creation, distribution sets, etc.
29554 You know who you are :-)
29561 This document was initially written by Sandeep Dutta
29564 All product names mentioned herein may be trademarks
29565 \begin_inset LatexCommand \index{Trademarks}
29569 of their respective companies.
29576 To avoid confusion, the installation and building options for SDCC itself
29577 (chapter 2) are not part of the index.
29581 \begin_inset LatexCommand \printindex{}