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={8032 8051 ansi c compiler CPU DS390
11 embedded GPL HC08 manual mcs51 microcontroller PIC Z80},
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 cvs
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:
207 char (8 bits, 1 byte),
210 short and int (16 bits, 2 bytes),
213 long (32 bit, 4 bytes)
220 The compiler also allows
222 inline assembler code
224 to be embedded anywhere in a function.
225 In addition, routines developed in assembly can also be called.
229 SDCC also provides an option (-
239 -cyclomatic) to report the relative complexity of a function.
240 These functions can then be further optimized, or hand coded in assembly
246 SDCC also comes with a companion source level debugger SDCDB, the debugger
247 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
252 The latest version can be downloaded from
253 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
263 Please note: the compiler will probably always be some steps ahead of this
268 \begin_inset LatexCommand \index{Status of documentation}
278 Obviously this has pros and cons
287 All packages used in this compiler system are
295 ; source code for all the sub-packages (pre-processor, assemblers, linkers
296 etc) is distributed with the package.
297 This documentation is maintained using a freeware word processor (LyX).
299 This program is free software; you can redistribute it and/or modify it
300 under the terms of the GNU General Public License
301 \begin_inset LatexCommand \index{GNU General Public License, GPL}
305 as published by the Free Software Foundation; either version 2, or (at
306 your option) any later version.
307 This program is distributed in the hope that it will be useful, but WITHOUT
308 ANY WARRANTY; without even the implied warranty
309 \begin_inset LatexCommand \index{warranty}
313 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
314 See the GNU General Public License for more details.
315 You should have received a copy of the GNU General Public License along
316 with this program; if not, write to the Free Software Foundation, 59 Temple
317 Place - Suite 330, Boston, MA 02111-1307, USA.
318 In other words, you are welcome to use, share and improve this program.
319 You are forbidden to forbid anyone else to use, share and improve what
321 Help stamp out software-hoarding!
324 Typographic conventions
325 \begin_inset LatexCommand \index{Typographic conventions}
332 Throughout this manual, we will use the following convention.
333 Commands you have to type in are printed in
341 Code samples are printed in
346 Interesting items and new terms are printed in
351 Compatibility with previous versions
352 \begin_inset LatexCommand \index{Compatibility with previous versions}
359 This version has numerous bug fixes compared with the previous version.
360 But we also introduced some incompatibilities with older versions.
361 Not just for the fun of it, but to make the compiler more stable, efficient
363 \begin_inset LatexCommand \index{ANSI-compliance}
368 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
372 for ANSI-Compliance).
378 short is now equivalent to int (16 bits), it used to be equivalent to char
379 (8 bits) which is not ANSI compliant.
382 the default directory for gcc-builds where include, library and documentation
383 files are stored is now in /usr/local/share.
386 char type parameters to vararg functions are casted to int unless explicitly
403 will push a as an int and as a char resp.
416 -regextend has been removed.
429 -noregparms has been removed.
442 -stack-after-data has been removed.
446 \begin_inset LatexCommand \index{bit}
451 \begin_inset LatexCommand \index{sbit}
456 \begin_inset LatexCommand \index{\_\_sbit}
460 types now consistently behave like the C99 _Bool type with respect to type
462 \begin_inset LatexCommand \index{type conversion}
467 \begin_inset LatexCommand \index{type promotion}
472 The most common incompatibility resulting from this change is related to
474 \begin_inset LatexCommand \index{Bit toggling}
488 b = ~b; /* equivalent to b=1 instead of toggling b */
492 b = !b; /* toggles b */
496 In previous versions, both forms would have toggled the bit.
501 <pending: more incompatibilities?>
507 What do you need before you start installation of SDCC? A computer, and
509 The preferred method of installation is to compile SDCC from source using
511 For Windows some pre-compiled binary distributions are available for your
513 You should have some experience with command line tools and compiler use.
519 The SDCC home page at
520 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
524 is a great place to find distribution sets.
525 You can also find links to the user mailing lists that offer help or discuss
526 SDCC with other SDCC users.
527 Web links to other SDCC related sites can also be found here.
528 This document can be found in the DOC directory of the source package as
530 A pdf version of this document is available at
531 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
536 Some of the other tools (simulator and assembler) included with SDCC contain
537 their own documentation and can be found in the source distribution.
538 If you want the latest unreleased software, the complete source package
539 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
542 Wishes for the future
545 There are (and always will be) some things that could be done.
546 Here are some I can think of:
553 char KernelFunction3(char p) at 0x340;
561 \begin_inset LatexCommand \index{code banking (not supported)}
571 If you can think of some more, please see the section
572 \begin_inset LatexCommand \ref{sub:Requesting-Features}
576 about filing feature requests
577 \begin_inset LatexCommand \index{Requesting features}
582 \begin_inset LatexCommand \index{Feature request}
592 \begin_inset LatexCommand \index{Installation}
599 For most users it is sufficient to skip to either section
600 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
605 \begin_inset LatexCommand \ref{sub:Windows-Install}
610 More detailled instructions follow below.
614 \begin_inset LatexCommand \index{Options SDCC configuration}
621 The install paths, search paths and other options are defined when running
623 The defaults can be overridden by:
625 \labelwidthstring 00.00.0000
637 -prefix see table below
639 \labelwidthstring 00.00.0000
651 -exec_prefix see table below
653 \labelwidthstring 00.00.0000
665 -bindir see table below
667 \labelwidthstring 00.00.0000
679 -datadir see table below
681 \labelwidthstring 00.00.0000
683 docdir environment variable, see table below
685 \labelwidthstring 00.00.0000
687 include_dir_suffix environment variable, see table below
689 \labelwidthstring 00.00.0000
691 lib_dir_suffix environment variable, see table below
693 \labelwidthstring 00.00.0000
695 sdccconf_h_dir_separator environment variable, either / or
700 This character will only be used in sdccconf.h; don't forget it's a C-header,
701 therefore a double-backslash is needed there.
703 \labelwidthstring 00.00.0000
715 -disable-mcs51-port Excludes the Intel mcs51 port
717 \labelwidthstring 00.00.0000
729 -disable-gbz80-port Excludes the Gameboy gbz80 port
731 \labelwidthstring 00.00.0000
743 -disable-z80-port Excludes the z80 port
745 \labelwidthstring 00.00.0000
757 -disable-avr-port Excludes the AVR port
759 \labelwidthstring 00.00.0000
771 -disable-ds390-port Excludes the DS390 port
773 \labelwidthstring 00.00.0000
785 -disable-hc08-port Excludes the HC08 port
787 \labelwidthstring 00.00.0000
799 -disable-pic-port Excludes the PIC port
801 \labelwidthstring 00.00.0000
813 -disable-xa51-port Excludes the XA51 port
815 \labelwidthstring 00.00.0000
827 -disable-ucsim Disables configuring and building of ucsim
829 \labelwidthstring 00.00.0000
841 -disable-device-lib-build Disables automatically building device libraries
843 \labelwidthstring 00.00.0000
855 -disable-packihx Disables building packihx
857 \labelwidthstring 00.00.0000
869 -enable-libgc Use the Bohem memory allocator.
870 Lower runtime footprint.
873 Furthermore the environment variables CC, CFLAGS, ...
874 the tools and their arguments can be influenced.
875 Please see `configure -
885 -help` and the man/info pages of `configure` for details.
889 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
890 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
891 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
892 At the moment it's not possible to change the default settings (it was
893 simply never required).
897 These configure options are compiled into the binaries, and can only be
898 changed by rerunning 'configure' and recompiling SDCC.
899 The configure options are written in
903 to distinguish them from run time environment variables (see section search
909 \begin_inset Quotes sld
913 \begin_inset Quotes srd
916 are used by the SDCC team to build the official Win32 binaries.
917 The SDCC team uses Mingw32 to build the official Windows binaries, because
924 a gcc compiler and last but not least
927 the binaries can be built by cross compiling on Sourceforge's compile farm.
930 See the examples, how to pass the Win32 settings to 'configure'.
931 The other Win32 builds using Borland, VC or whatever don't use 'configure',
932 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
944 <lyxtabular version="3" rows="8" columns="3">
946 <column alignment="block" valignment="top" leftline="true" width="0in">
947 <column alignment="block" valignment="top" leftline="true" width="0in">
948 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
949 <row topline="true" bottomline="true">
950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1005 <row topline="true">
1006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1037 <row topline="true">
1038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1075 <row topline="true">
1076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1109 <row topline="true">
1110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1147 <row topline="true">
1148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1175 <row topline="true" bottomline="true">
1176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1212 'configure' also computes relative paths.
1213 This is needed for full relocatability of a binary package and to complete
1214 search paths (see section search paths below):
1220 \begin_inset Tabular
1221 <lyxtabular version="3" rows="4" columns="3">
1223 <column alignment="block" valignment="top" leftline="true" width="0in">
1224 <column alignment="block" valignment="top" leftline="true" width="0in">
1225 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1226 <row topline="true" bottomline="true">
1227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1252 <row topline="true" bottomline="true">
1253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1280 <row bottomline="true">
1281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1308 <row bottomline="true">
1309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1360 \begin_inset Quotes srd
1364 \begin_inset Quotes srd
1378 \begin_inset Quotes srd
1382 \begin_inset Quotes srd
1410 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1419 \begin_inset Quotes srd
1422 i586-mingw32msvc-gcc
1423 \begin_inset Quotes srd
1427 \begin_inset Quotes srd
1430 i586-mingw32msvc-g++
1431 \begin_inset Quotes srd
1439 \begin_inset Quotes srd
1442 i586-mingw32msvc-ranlib
1443 \begin_inset Quotes srd
1451 \begin_inset Quotes srd
1454 i586-mingw32msvc-strip
1455 \begin_inset Quotes srd
1473 \begin_inset Quotes srd
1477 \begin_inset Quotes srd
1495 \begin_inset Quotes srd
1499 \begin_inset Quotes srd
1507 \begin_inset Quotes srd
1511 \begin_inset Quotes srd
1519 \begin_inset Quotes srd
1523 \begin_inset Quotes srd
1531 \begin_inset Quotes srd
1535 \begin_inset Quotes srd
1542 sdccconf_h_dir_separator=
1543 \begin_inset Quotes srd
1555 \begin_inset Quotes srd
1572 -disable-device-lib-build
1600 -host=i586-mingw32msvc -
1610 -build=unknown-unknown-linux-gnu
1614 \begin_inset Quotes sld
1618 \begin_inset Quotes srd
1621 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1630 \begin_inset Quotes srd
1634 \begin_inset Quotes srd
1642 \begin_inset Quotes srd
1646 \begin_inset Quotes srd
1664 \begin_inset Quotes srd
1668 \begin_inset Quotes srd
1686 \begin_inset Quotes srd
1690 \begin_inset Quotes srd
1698 \begin_inset Quotes srd
1702 \begin_inset Quotes srd
1710 \begin_inset Quotes srd
1714 \begin_inset Quotes srd
1722 \begin_inset Quotes srd
1726 \begin_inset Quotes srd
1733 sdccconf_h_dir_separator=
1734 \begin_inset Quotes srd
1746 \begin_inset Quotes srd
1766 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1777 -C' turns on caching, which gives a little bit extra speed.
1778 However if options are changed, it can be necessary to delete the config.cache
1783 \begin_inset LatexCommand \label{sub:Install-paths}
1788 \begin_inset LatexCommand \index{Install paths}
1794 \added_space_top medskip \align center
1796 \begin_inset Tabular
1797 <lyxtabular version="3" rows="5" columns="4">
1799 <column alignment="center" valignment="top" leftline="true" width="0">
1800 <column alignment="center" valignment="top" leftline="true" width="0">
1801 <column alignment="center" valignment="top" leftline="true" width="0">
1802 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1803 <row topline="true" bottomline="true">
1804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1845 <row topline="true">
1846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1885 <row topline="true">
1886 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1901 $DATADIR/ $INCLUDE_DIR_SUFFIX
1904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1909 /usr/local/share/sdcc/include
1912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1925 <row topline="true">
1926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1941 $DATADIR/$LIB_DIR_SUFFIX
1944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1949 /usr/local/share/sdcc/lib
1952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1965 <row topline="true" bottomline="true">
1966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1989 /usr/local/share/sdcc/doc
1992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2014 *compiler, preprocessor, assembler, and linker
2020 is auto-appended by the compiler, e.g.
2021 small, large, z80, ds390 etc
2024 The install paths can still be changed during `make install` with e.g.:
2027 make install prefix=$(HOME)/local/sdcc
2030 Of course this doesn't change the search paths compiled into the binaries.
2034 Moreover the install path can be changed by defining DESTDIR
2035 \begin_inset LatexCommand \index{DESTDIR}
2042 make install DESTDIR=$(HOME)/sdcc.rpm/
2045 Please note that DESTDIR must have a trailing slash!
2049 \begin_inset LatexCommand \label{sub:Search-Paths}
2054 \begin_inset LatexCommand \index{Search path}
2061 Some search paths or parts of them are determined by configure variables
2066 , see section above).
2067 Further search paths are determined by environment variables during runtime.
2070 The paths searched when running the compiler are as follows (the first catch
2076 Binary files (preprocessor, assembler and linker)
2082 \begin_inset Tabular
2083 <lyxtabular version="3" rows="4" columns="3">
2085 <column alignment="block" valignment="top" leftline="true" width="0in">
2086 <column alignment="block" valignment="top" leftline="true" width="0in">
2087 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2088 <row topline="true" bottomline="true">
2089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2114 <row topline="true">
2115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2144 <row topline="true">
2145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2150 Path of argv[0] (if available)
2153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2161 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2170 <row topline="true" bottomline="true">
2171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2179 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2187 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2212 \begin_inset Tabular
2213 <lyxtabular version="3" rows="6" columns="3">
2215 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2216 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2217 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2218 <row topline="true" bottomline="true">
2219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2244 <row topline="true">
2245 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2300 <row topline="true">
2301 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2326 <row topline="true">
2327 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2341 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2364 <row topline="true">
2365 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2446 <row topline="true" bottomline="true">
2447 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2463 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2468 /usr/local/share/sdcc/
2473 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2501 -nostdinc disables the last two search paths.
2511 With the exception of
2512 \begin_inset Quotes sld
2526 \begin_inset Quotes srd
2533 is auto-appended by the compiler (e.g.
2534 small, large, z80, ds390 etc.).
2541 \begin_inset Tabular
2542 <lyxtabular version="3" rows="6" columns="3">
2544 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2545 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2546 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2547 <row topline="true" bottomline="true">
2548 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2556 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2573 <row topline="true">
2574 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2592 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2629 <row topline="true">
2630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2642 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2669 <row topline="true">
2670 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2681 $LIB_DIR_SUFFIX/<model>
2684 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2698 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2715 <row topline="true">
2716 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2731 $LIB_DIR_SUFFIX/<model>
2734 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2787 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2843 <row topline="true" bottomline="true">
2844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2853 $LIB_DIR_SUFFIX/<model>
2856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2861 /usr/local/share/sdcc/
2868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2886 Don't delete any of the stray spaces in the table above without checking
2887 the HTML output (last line)!
2903 -nostdlib disables the last two search paths.
2907 \begin_inset LatexCommand \index{Building SDCC}
2914 Building SDCC on Linux
2915 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2924 Download the source package
2926 either from the SDCC CVS repository or from the nightly snapshots
2928 , it will be named something like sdcc
2939 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2948 Bring up a command line terminal, such as xterm.
2953 Unpack the file using a command like:
2956 "tar -xvzf sdcc.src.tar.gz
2961 , this will create a sub-directory called sdcc with all of the sources.
2964 Change directory into the main SDCC directory, for example type:
2981 This configures the package for compilation on your system.
2997 All of the source packages will compile, this can take a while.
3013 This copies the binary executables, the include files, the libraries and
3014 the documentation to the install directories.
3015 Proceed with section
3016 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3023 Building SDCC on OSX 2.x
3026 Follow the instruction for Linux.
3030 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3031 )) fails to compile SDCC.
3032 Fortunately there's also gcc 2.9.x installed, which works fine.
3033 This compiler can be selected by running 'configure' with:
3036 ./configure CC=gcc2 CXX=g++2
3039 Cross compiling SDCC on Linux for Windows
3042 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3043 See section 'Configure Options'.
3046 Building SDCC on Windows
3049 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3051 They use Unix-sockets, which are not available on Win32.
3054 Building SDCC using Cygwin and Mingw32
3057 For building and installing a Cygwin executable follow the instructions
3063 \begin_inset Quotes sld
3067 \begin_inset Quotes srd
3070 Win32-binary can be built, which will not need the Cygwin-DLL.
3071 For the necessary 'configure' options see section 'configure options' or
3072 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3076 In order to install Cygwin on Windows download setup.exe from
3077 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3083 \begin_inset Quotes sld
3086 default text file type
3087 \begin_inset Quotes srd
3091 \begin_inset Quotes sld
3095 \begin_inset Quotes srd
3098 and download/install at least the following packages.
3099 Some packages are selected by default, others will be automatically selected
3100 because of dependencies with the manually selected packages.
3101 Never deselect these packages!
3110 gcc ; version 3.x is fine, no need to use the old 2.9x
3113 binutils ; selected with gcc
3119 rxvt ; a nice console, which makes life much easier under windoze (see below)
3122 man ; not really needed for building SDCC, but you'll miss it sooner or
3126 less ; not really needed for building SDCC, but you'll miss it sooner or
3130 cvs ; only if you use CVS access
3133 If you want to develop something you'll need:
3136 python ; for the regression tests
3139 gdb ; the gnu debugger, together with the nice GUI
3140 \begin_inset Quotes sld
3144 \begin_inset Quotes srd
3150 openssh ; to access the CF or commit changes
3153 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3154 use autoconf-stable!
3157 rxvt is a nice console with history.
3158 Replace in your cygwin.bat the line
3177 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3180 -bg black -fg white -geometry 100x65 -e bash -
3193 Text selected with the mouse is automatically copied to the clipboard, pasting
3194 works with shift-insert.
3198 The other good tip is to make sure you have no //c/-style paths anywhere,
3199 use /cygdrive/c/ instead.
3200 Using // invokes a network lookup which is very slow.
3202 \begin_inset Quotes sld
3206 \begin_inset Quotes srd
3209 is too long, you can change it with e.g.
3215 SDCC sources use the unix line ending LF.
3216 Life is much easier, if you store the source tree on a drive which is mounted
3218 And use an editor which can handle LF-only line endings.
3219 Make sure not to commit files with windows line endings.
3220 The tabulator spacing
3221 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3225 used in the project is 8.
3226 Although a tabulator spacing of 8 is a sensible choice for programmers
3227 (it's a power of 2 and allows to display 8/16 bit signed variables without
3228 loosing columns) the plan is to move towards using only spaces in the source.
3231 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3236 Download the source package
3238 either from the SDCC CVS repository or from the
3239 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3245 , it will be named something like sdcc
3252 SDCC is distributed with all the projects, workspaces, and files you need
3253 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3254 doesn't build under MSVC).
3255 The workspace name is 'sdcc.dsw'.
3256 Please note that as it is now, all the executables are created in a folder
3260 Once built you need to copy the executables from sdcc
3264 bin before running SDCC.
3269 WARNING: Visual studio is very picky with line terminations; it expects
3270 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3271 If you are getting a message such as "This makefile was not generated by
3272 Developer Studio etc.
3274 \begin_inset Quotes srd
3277 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3278 need to convert the Unix style line endings to DOS style line endings.
3279 To do so you can use the
3280 \begin_inset Quotes sld
3284 \begin_inset Quotes srd
3287 utility freely available on the internet.
3288 Doug Hawkins reported in the sdcc-user list that this works:
3296 SDCC> unix2dos sdcc.dsw
3302 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3306 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3307 flex.exe, and gawk.exe.
3308 One good place to get them is
3309 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3317 Download the file UnxUtils
3318 \begin_inset LatexCommand \index{UnxUtils}
3323 Now you have to install the utilities and setup MSVC so it can locate the
3325 Here there are two alternatives (choose one!):
3332 a) Extract UnxUtils.zip to your C:
3334 hard disk PRESERVING the original paths, otherwise bison won't work.
3335 (If you are using WinZip make certain that 'Use folder names' is selected)
3339 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3340 in 'Show directories for:' select 'Executable files', and in the directories
3341 window add a new path: 'C:
3351 (As a side effect, you get a bunch of Unix utilities that could be useful,
3352 such as diff and patch.)
3359 This one avoids extracting a bunch of files you may not use, but requires
3364 a) Create a directory were to put the tools needed, or use a directory already
3372 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3373 to such directory WITHOUT preserving the original paths.
3374 (If you are using WinZip make certain that 'Use folder names' is not selected)
3378 c) Rename bison.exe to '_bison.exe'.
3382 d) Create a batch file 'bison.bat' in 'C:
3386 ' and add these lines:
3406 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3410 Steps 'c' and 'd' are needed because bison requires by default that the
3411 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3412 '/usr/local/share/' I think.
3413 So it is necessary to tell bison where those files are located if they
3414 are not in such directory.
3415 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3419 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3420 in 'Show directories for:' select 'Executable files', and in the directories
3421 window add a new path: 'c:
3424 Note that you can use any other path instead of 'c:
3426 util', even the path where the Visual C++ tools are, probably: 'C:
3430 Microsoft Visual Studio
3435 So you don't have to execute step 'e' :)
3439 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3440 the executables from sdcc
3444 bin, and you can compile using SDCC.
3447 Building SDCC Using Borland
3450 From the sdcc directory, run the command "make -f Makefile.bcc".
3451 This should regenerate all the .exe files in the bin directory except for
3452 sdcdb.exe (which currently doesn't build under Borland C++).
3455 If you modify any source files and need to rebuild, be aware that the dependenci
3456 es may not be correctly calculated.
3457 The safest option is to delete all .obj files and run the build again.
3458 From a Cygwin BASH prompt, this can easily be done with the command (be
3459 sure you are in the sdcc directory):
3469 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3471 ) -print -exec rm {}
3480 or on Windows NT/2000/XP from the command prompt with the command:
3487 del /s *.obj *.lib *.rul
3490 from the sdcc directory.
3493 Windows Install Using a ZIP Package
3496 Download the binary zip package from
3497 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3501 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3502 This should unpack to a group of sub-directories.
3503 An example directory structure after unpacking the mingw32 package is:
3508 bin for the executables, c:
3516 lib for the include and libraries.
3519 Adjust your environment variable PATH to include the location of the bin
3520 directory or start sdcc using the full path.
3523 Windows Install Using the Setup Program
3524 \begin_inset LatexCommand \label{sub:Windows-Install}
3531 Download the setup program
3533 sdcc-x.y.z-setup.exe
3535 for an official release from
3538 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3542 or a setup program for one of the snapshots
3544 sdcc_yyyymmdd_setup.exe
3547 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3552 A windows typical installer will guide you through the installation process.
3555 Building the Documentation
3558 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3559 as changing into the doc directory and typing
3563 \begin_inset Quotes srd
3567 \begin_inset Quotes srd
3574 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3577 \begin_inset LatexCommand \url{http://www.lyx.org}
3581 as editor this is straightforward.
3582 Prebuilt documentation in html and pdf format is available from
3583 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3590 Reading the Documentation
3593 Currently reading the document in pdf format is recommended, as for unknown
3594 reason the hyperlinks are working there whereas in the html version they
3601 If you should know why please drop us a note
3607 You'll find the pdf version
3608 \begin_inset LatexCommand \index{PDF version of this document}
3613 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3621 \begin_inset LatexCommand \index{HTML version of this document}
3626 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3632 This documentation is in some aspects different from a commercial documentation:
3636 It tries to document SDCC for several processor architectures in one document
3637 (commercially these probably would be separate documents/products).
3639 \begin_inset LatexCommand \index{Status of documentation}
3643 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3645 Z80, PIC14, PIC16 and HC08.
3648 There are many references pointing away from this documentation.
3649 Don't let this distract you.
3651 was a reference like
3652 \begin_inset LatexCommand \url{http://www.opencores.org}
3656 together with a statement
3657 \begin_inset Quotes sld
3660 some processors which are targetted by SDCC can be implemented in a
3677 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3682 \begin_inset Quotes srd
3685 we expect you to have a quick look there and come back.
3686 If you read this you are on the right track.
3689 Some sections attribute more space to problems, restrictions and warnings
3690 than to the solution.
3693 The installation section and the section about the debugger is intimidating.
3696 There are still lots of typos and there are more different writing styles
3700 Testing the SDCC Compiler
3701 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3708 The first thing you should do after installing your SDCC compiler is to
3724 \begin_inset LatexCommand \index{version}
3731 at the prompt, and the program should run and tell you the version.
3732 If it doesn't run, or gives a message about not finding sdcc program, then
3733 you need to check over your installation.
3734 Make sure that the sdcc bin directory is in your executable search path
3735 defined by the PATH environment setting (
3740 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3747 Install trouble-shooting for suggestions
3750 Make sure that the sdcc program is in the bin folder, if not perhaps something
3751 did not install correctly.
3759 is commonly installed as described in section
3760 \begin_inset Quotes sld
3763 Install and search paths
3764 \begin_inset Quotes srd
3773 Make sure the compiler works on a very simple example.
3774 Type in the following test.c program using your favorite
3800 Compile this using the following command:
3809 If all goes well, the compiler will generate a test.asm and test.rel file.
3810 Congratulations, you've just compiled your first program with SDCC.
3811 We used the -c option to tell SDCC not to link the generated code, just
3812 to keep things simple for this step.
3820 The next step is to try it with the linker.
3830 If all goes well the compiler will link with the libraries and produce
3831 a test.ihx output file.
3836 (no test.ihx, and the linker generates warnings), then the problem is most
3845 usr/local/share/sdcc/lib directory
3852 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3859 Install trouble-shooting for suggestions).
3867 The final test is to ensure
3875 header files and libraries.
3876 Edit test.c and change it to the following:
3893 strcpy(str1, "testing");
3900 Compile this by typing
3907 This should generate a test.ihx output file, and it should give no warnings
3908 such as not finding the string.h file.
3909 If it cannot find the string.h file, then the problem is that
3913 cannot find the /usr/local/share/sdcc/include directory
3920 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3927 Install trouble-shooting section for suggestions).
3945 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3949 to find exactly where SDCC is looking for the include and lib files.
3952 Install Trouble-shooting
3953 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3958 \begin_inset LatexCommand \index{Install trouble-shooting}
3965 If SDCC does not build correctly
3968 A thing to try is starting from scratch by unpacking the .tgz source package
3969 again in an empty directory.
3977 ./configure 2>&1 | tee configure.log
3991 make 2>&1 | tee make.log
3998 If anything goes wrong, you can review the log files to locate the problem.
3999 Or a relevant part of this can be attached to an email that could be helpful
4000 when requesting help from the mailing list.
4004 \begin_inset Quotes sld
4008 \begin_inset Quotes srd
4015 \begin_inset Quotes sld
4019 \begin_inset Quotes srd
4022 command is a script that analyzes your system and performs some configuration
4023 to ensure the source package compiles on your system.
4024 It will take a few minutes to run, and will compile a few tests to determine
4025 what compiler features are installed.
4029 \begin_inset Quotes sld
4033 \begin_inset Quotes srd
4039 This runs the GNU make tool, which automatically compiles all the source
4040 packages into the final installed binary executables.
4044 \begin_inset Quotes sld
4048 \begin_inset Quotes erd
4054 This will install the compiler, other executables libraries and include
4055 files into the appropriate directories.
4057 \begin_inset LatexCommand \ref{sub:Install-paths}
4063 \begin_inset LatexCommand \ref{sub:Search-Paths}
4068 about install and search paths.
4070 On most systems you will need super-user privileges to do this.
4076 SDCC is not just a compiler, but a collection of tools by various developers.
4077 These include linkers, assemblers, simulators and other components.
4078 Here is a summary of some of the components.
4079 Note that the included simulator and assembler have separate documentation
4080 which you can find in the source package in their respective directories.
4081 As SDCC grows to include support for other processors, other packages from
4082 various developers are included and may have their own sets of documentation.
4086 You might want to look at the files which are installed in <installdir>.
4087 At the time of this writing, we find the following programs for gcc-builds:
4091 In <installdir>/bin:
4094 sdcc - The compiler.
4097 sdcpp - The C preprocessor.
4100 asx8051 - The assembler for 8051 type processors.
4107 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4110 aslink -The linker for 8051 type processors.
4117 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4120 s51 - The ucSim 8051 simulator.
4123 sdcdb - The source debugger.
4126 packihx - A tool to pack (compress) Intel hex files.
4129 In <installdir>/share/sdcc/include
4135 In <installdir>/share/sdcc/lib
4138 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4142 In <installdir>/share/sdcc/doc
4148 As development for other processors proceeds, this list will expand to include
4149 executables to support processors like AVR, PIC, etc.
4155 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4156 the assembler and linkage editor.
4159 sdcpp - The C-Preprocessor
4163 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4167 is a modified version of the GNU preprocessor.
4168 The C preprocessor is used to pull in #include sources, process #ifdef
4169 statements, #defines and so on.
4180 - The Assemblers and Linkage Editors
4183 This is retargettable assembler & linkage editor, it was developed by Alan
4185 John Hartman created the version for 8051, and I (Sandeep) have made some
4186 enhancements and bug fixes for it to work properly with SDCC.
4193 \begin_inset LatexCommand \index{s51}
4197 is a freeware, opensource simulator developed by Daniel Drotos.
4198 The simulator is built as part of the build process.
4199 For more information visit Daniel's web site at:
4200 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4205 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4209 sdcdb - Source Level Debugger
4213 \begin_inset LatexCommand \index{sdcdb (debugger)}
4217 is the companion source level debugger.
4218 More about sdcdb in section
4219 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4224 The current version of the debugger uses Daniel's Simulator S51
4225 \begin_inset LatexCommand \index{s51}
4229 , but can be easily changed to use other simulators.
4239 Single Source File Projects
4242 For single source file 8051 projects the process is very simple.
4243 Compile your programs with the following command
4246 "sdcc sourcefile.c".
4250 This will compile, assemble and link your source file.
4251 Output files are as follows:
4255 \begin_inset LatexCommand \index{<file>.asm}
4260 \begin_inset LatexCommand \index{Assembler source}
4264 file created by the compiler
4268 \begin_inset LatexCommand \index{<file>.lst}
4273 \begin_inset LatexCommand \index{Assembler listing}
4277 file created by the Assembler
4281 \begin_inset LatexCommand \index{<file>.rst}
4286 \begin_inset LatexCommand \index{Assembler listing}
4290 file updated with linkedit information, created by linkage editor
4294 \begin_inset LatexCommand \index{<file>.sym}
4299 \begin_inset LatexCommand \index{Symbol listing}
4303 for the sourcefile, created by the assembler
4307 \begin_inset LatexCommand \index{<file>.rel}
4312 \begin_inset LatexCommand \index{<file>.o}
4317 \begin_inset LatexCommand \index{Object file}
4321 created by the assembler, input to Linkage editor
4325 \begin_inset LatexCommand \index{<file>.map}
4330 \begin_inset LatexCommand \index{Memory map}
4334 for the load module, created by the Linker
4338 \begin_inset LatexCommand \index{<file>.mem}
4342 - A file with a summary of the memory usage
4346 \begin_inset LatexCommand \index{<file>.ihx}
4350 - The load module in Intel hex format
4351 \begin_inset LatexCommand \index{Intel hex format}
4355 (you can select the Motorola S19 format
4356 \begin_inset LatexCommand \index{Motorola S19 format}
4371 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4376 If you need another format you might want to use
4383 \begin_inset LatexCommand \index{objdump (tool)}
4394 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4399 Both formats are documented in the documentation of srecord
4400 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4408 \begin_inset LatexCommand \index{<file>.adb}
4412 - An intermediate file containing debug information needed to create the
4424 \begin_inset LatexCommand \index{-\/-debug}
4432 \begin_inset LatexCommand \index{<file>.cdb}
4436 - An optional file (with -
4446 -debug) containing debug information.
4447 The format is documented in cdbfileformat.pdf
4452 \begin_inset LatexCommand \index{<file> (no extension)}
4456 An optional AOMF or AOMF51
4457 \begin_inset LatexCommand \index{AOMF, AOMF51}
4461 file containing debug information (generated with option -
4488 ormat is commonly used by third party tools (debuggers
4489 \begin_inset LatexCommand \index{Debugger}
4493 , simulators, emulators)
4497 \begin_inset LatexCommand \index{<file>.dump*}
4501 - Dump file to debug the compiler it self (generated with option -
4511 -dumpall) (see section
4512 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4518 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4524 \begin_inset Quotes sld
4527 Anatomy of the compiler
4528 \begin_inset Quotes srd
4534 Projects with Multiple Source Files
4537 SDCC can compile only ONE file at a time.
4538 Let us for example assume that you have a project containing the following
4543 foo1.c (contains some functions)
4545 foo2.c (contains some more functions)
4547 foomain.c (contains more functions and the function main)
4555 The first two files will need to be compiled separately with the commands:
4587 Then compile the source file containing the
4592 \begin_inset LatexCommand \index{Linker}
4596 the files together with the following command:
4604 foomain.c\SpecialChar ~
4605 foo1.rel\SpecialChar ~
4610 \begin_inset LatexCommand \index{<file>.rel}
4622 can be separately compiled as well:
4633 sdcc foomain.rel foo1.rel foo2.rel
4640 The file containing the
4655 file specified in the command line, since the linkage editor processes
4656 file in the order they are presented to it.
4657 The linker is invoked from SDCC using a script file with extension .lnk
4658 \begin_inset LatexCommand \index{<file>.lnk}
4663 You can view this file to troubleshoot linking problems such as those arising
4664 from missing libraries.
4667 Projects with Additional Libraries
4668 \begin_inset LatexCommand \index{Libraries}
4675 Some reusable routines may be compiled into a library, see the documentation
4676 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4680 \begin_inset LatexCommand \index{<file>.lib}
4687 Libraries created in this manner can be included in the command line.
4688 Make sure you include the -L <library-path> option to tell the linker where
4689 to look for these files if they are not in the current directory.
4690 Here is an example, assuming you have the source file
4702 (if that is not the same as your current project):
4709 sdcc foomain.c foolib.lib -L mylib
4720 must be an absolute path name.
4724 The most efficient way to use libraries is to keep separate modules in separate
4726 The lib file now should name all the modules.rel
4727 \begin_inset LatexCommand \index{<file>.rel}
4732 For an example see the standard library file
4736 in the directory <installdir>/share/lib/small.
4739 Using sdcclib to Create and Manage Libraries
4740 \begin_inset LatexCommand \index{sdcclib}
4747 Alternatively, instead of having a .rel file for each entry on the library
4748 file as described in the preceding section, sdcclib can be used to embed
4749 all the modules belonging to such library in the library file itself.
4750 This results in a larger library file, but it greatly reduces the number
4751 of disk files accessed by the linker.
4752 Additionally, the packed library file contains an index of all include
4753 modules and symbols that significantly speeds up the linking process.
4754 To display a list of options supported by sdcclib type:
4763 \begin_inset LatexCommand \index{sdcclib}
4774 To create a new library file, start by compiling all the required modules.
4812 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4814 The next step is to add the .rel files to the library file:
4822 sdcclib libint.lib _divsint.rel
4825 \begin_inset LatexCommand \index{sdcclib}
4835 sdcclib libint.lib _divuint.rel
4841 sdcclib libint.lib _modsint.rel
4847 sdcclib libint.lib _moduint.rel
4853 sdcclib libint.lib _mulint.rel
4860 If the file already exists in the library, it will be replaced.
4861 To see what modules and symbols are included in the library, options -s
4862 and -m are available.
4870 sdcclib -s libint.lib
4873 \begin_inset LatexCommand \index{sdcclib}
4983 If the source files are compiled using -
4994 \begin_inset LatexCommand \index{-\/-debug}
4998 , the corresponding debug information file .adb will be include in the library
5000 The library files created with sdcclib are plain text files, so they can
5001 be viewed with a text editor.
5002 It is not recomended to modify a library file created with sdcclib using
5003 a text editor, as there are file indexes numbers located accross the file
5004 used by the linker to quickly locate the required module to link.
5005 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5006 it can be safely deleted, since all the information required for linking
5007 is embedded in the library file itself.
5008 Library files created using sdcclib are used as described in the preceding
5012 Command Line Options
5013 \begin_inset LatexCommand \index{Command Line Options}
5020 Processor Selection Options
5021 \begin_inset LatexCommand \index{Options processor selection}
5026 \begin_inset LatexCommand \index{Processor selection options}
5032 \labelwidthstring 00.00.0000
5037 \begin_inset LatexCommand \index{-mmcs51}
5043 Generate code for the Intel MCS51
5044 \begin_inset LatexCommand \index{MCS51}
5048 family of processors.
5049 This is the default processor target.
5051 \labelwidthstring 00.00.0000
5056 \begin_inset LatexCommand \index{-mds390}
5062 Generate code for the Dallas DS80C390
5063 \begin_inset LatexCommand \index{DS80C390}
5069 \labelwidthstring 00.00.0000
5074 \begin_inset LatexCommand \index{-mds400}
5080 Generate code for the Dallas DS80C400
5081 \begin_inset LatexCommand \index{DS80C400}
5087 \labelwidthstring 00.00.0000
5092 \begin_inset LatexCommand \index{-mhc08}
5098 Generate code for the Freescale/Motorola HC08
5099 \begin_inset LatexCommand \index{HC08}
5103 family of processors.
5105 \labelwidthstring 00.00.0000
5110 \begin_inset LatexCommand \index{-mz80}
5116 Generate code for the Zilog Z80
5117 \begin_inset LatexCommand \index{Z80}
5121 family of processors.
5123 \labelwidthstring 00.00.0000
5128 \begin_inset LatexCommand \index{-mgbz80}
5134 Generate code for the GameBoy Z80
5135 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5139 processor (Not actively maintained).
5141 \labelwidthstring 00.00.0000
5146 \begin_inset LatexCommand \index{-mavr}
5152 Generate code for the Atmel AVR
5153 \begin_inset LatexCommand \index{AVR}
5157 processor (In development, not complete).
5158 AVR users should probably have a look at winavr
5159 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5164 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5171 I think it is fair to direct users there for now.
5172 Open source is also about avoiding unnecessary work .
5173 But I didn't find the 'official' link.
5175 \labelwidthstring 00.00.0000
5180 \begin_inset LatexCommand \index{-mpic14}
5186 Generate code for the Microchip PIC 14
5187 \begin_inset LatexCommand \index{PIC14}
5191 -bit processors (p16f84 and variants.
5192 In development, not complete).
5195 p16f627 p16f628 p16f84 p16f873 p16f877?
5197 \labelwidthstring 00.00.0000
5202 \begin_inset LatexCommand \index{-mpic16}
5208 Generate code for the Microchip PIC 16
5209 \begin_inset LatexCommand \index{PIC16}
5213 -bit processors (p18f452 and variants.
5214 In development, not complete).
5216 \labelwidthstring 00.00.0000
5222 Generate code for the Toshiba TLCS-900H
5223 \begin_inset LatexCommand \index{TLCS-900H}
5227 processor (Not maintained, not complete).
5229 \labelwidthstring 00.00.0000
5234 \begin_inset LatexCommand \index{-mxa51}
5240 Generate code for the Phillips XA51
5241 \begin_inset LatexCommand \index{XA51}
5245 processor (Not maintained, not complete).
5248 Preprocessor Options
5249 \begin_inset LatexCommand \index{Options preprocessor}
5254 \begin_inset LatexCommand \index{Preprocessor options}
5259 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5265 \labelwidthstring 00.00.0000
5270 \begin_inset LatexCommand \index{-I<path>}
5276 The additional location where the pre processor will look for <..h> or
5277 \begin_inset Quotes eld
5281 \begin_inset Quotes erd
5286 \labelwidthstring 00.00.0000
5291 \begin_inset LatexCommand \index{-D<macro[=value]>}
5297 Command line definition of macros.
5298 Passed to the preprocessor.
5300 \labelwidthstring 00.00.0000
5305 \begin_inset LatexCommand \index{-M}
5311 Tell the preprocessor to output a rule suitable for make describing the
5312 dependencies of each object file.
5313 For each source file, the preprocessor outputs one make-rule whose target
5314 is the object file name for that source file and whose dependencies are
5315 all the files `#include'd in it.
5316 This rule may be a single line or may be continued with `
5318 '-newline if it is long.
5319 The list of rules is printed on standard output instead of the preprocessed
5322 \begin_inset LatexCommand \index{-E}
5328 \labelwidthstring 00.00.0000
5333 \begin_inset LatexCommand \index{-C}
5339 Tell the preprocessor not to discard comments.
5340 Used with the `-E' option.
5342 \labelwidthstring 00.00.0000
5347 \begin_inset LatexCommand \index{-MM}
5358 Like `-M' but the output mentions only the user header files included with
5360 \begin_inset Quotes eld
5364 System header files included with `#include <file>' are omitted.
5366 \labelwidthstring 00.00.0000
5371 \begin_inset LatexCommand \index{-Aquestion(answer)}
5377 Assert the answer answer for question, in case it is tested with a preprocessor
5378 conditional such as `#if #question(answer)'.
5379 `-A-' disables the standard assertions that normally describe the target
5382 \labelwidthstring 00.00.0000
5387 \begin_inset LatexCommand \index{-Umacro}
5393 Undefine macro macro.
5394 `-U' options are evaluated after all `-D' options, but before any `-include'
5395 and `-imacros' options.
5397 \labelwidthstring 00.00.0000
5402 \begin_inset LatexCommand \index{-dM}
5408 Tell the preprocessor to output only a list of the macro definitions that
5409 are in effect at the end of preprocessing.
5410 Used with the `-E' option.
5412 \labelwidthstring 00.00.0000
5417 \begin_inset LatexCommand \index{-dD}
5423 Tell the preprocessor to pass all macro definitions into the output, in
5424 their proper sequence in the rest of the output.
5426 \labelwidthstring 00.00.0000
5431 \begin_inset LatexCommand \index{-dN}
5442 Like `-dD' except that the macro arguments and contents are omitted.
5443 Only `#define name' is included in the output.
5445 \labelwidthstring 00.00.0000
5450 preprocessorOption[,preprocessorOption]
5453 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5458 Pass the preprocessorOption to the preprocessor
5463 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5468 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5469 Collection (gcc), if you need more dedicated options please refer to the
5471 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5479 \begin_inset LatexCommand \index{Options linker}
5484 \begin_inset LatexCommand \index{Linker options}
5490 \labelwidthstring 00.00.0000
5510 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5515 \begin_inset LatexCommand \index{-L -\/-lib-path}
5522 <absolute path to additional libraries> This option is passed to the linkage
5523 editor's additional libraries
5524 \begin_inset LatexCommand \index{Libraries}
5529 The path name must be absolute.
5530 Additional library files may be specified in the command line.
5531 See section Compiling programs for more details.
5533 \labelwidthstring 00.00.0000
5550 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5555 <Value> The start location of the external ram
5556 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5560 , default value is 0.
5561 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5571 -xram-loc 0x8000 or -
5583 \labelwidthstring 00.00.0000
5600 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5605 <Value> The start location of the code
5606 \begin_inset LatexCommand \index{code}
5610 segment, default value 0.
5611 Note when this option is used the interrupt vector table is also relocated
5612 to the given address.
5613 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5623 -code-loc 0x8000 or -
5635 \labelwidthstring 00.00.0000
5652 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5657 <Value> By default the stack
5658 \begin_inset LatexCommand \index{stack}
5662 is placed after the data segment.
5663 Using this option the stack can be placed anywhere in the internal memory
5665 The value entered can be in Hexadecimal or Decimal format, e.g.
5676 -stack-loc 0x20 or -
5687 Since the sp register is incremented before a push or call, the initial
5688 sp will be set to one byte prior the provided value.
5689 The provided value should not overlap any other memory areas such as used
5690 register banks or the data segment and with enough space for the current
5708 \begin_inset LatexCommand \index{-\/-pack-iram}
5712 option (which is now a default setting) will override this setting, so
5713 you should also specify the
5729 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5733 option if you need to manually place the stack.
5735 \labelwidthstring 00.00.0000
5752 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5757 <Value> The start location of the internal ram data
5758 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5763 The value entered can be in Hexadecimal or Decimal format, eg.
5785 (By default, the start location of the internal ram data segment is set
5786 as low as possible in memory, taking into account the used register banks
5787 and the bit segment at address 0x20.
5788 For example if register banks 0 and 1 are used without bit variables, the
5789 data segment will be set, if -
5799 -data-loc is not used, to location 0x10.)
5801 \labelwidthstring 00.00.0000
5818 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5823 <Value> The start location of the indirectly addressable internal ram
5824 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5828 of the 8051, default value is 0x80.
5829 The value entered can be in Hexadecimal or Decimal format, eg.
5840 -idata-loc 0x88 or -
5852 \labelwidthstring 00.00.0000
5869 <Value> The start location of the bit
5870 \begin_inset LatexCommand \index{bit}
5874 addressable internal ram of the 8051.
5880 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5883 \labelwidthstring 00.00.0000
5898 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5907 The linker output (final object code) is in Intel Hex format.
5908 \begin_inset LatexCommand \index{Intel hex format}
5912 This is the default option.
5913 The format itself is documented in the documentation of srecord
5914 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5920 \labelwidthstring 00.00.0000
5935 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5944 The linker output (final object code) is in Motorola S19 format
5945 \begin_inset LatexCommand \index{Motorola S19 format}
5950 The format itself is documented in the documentation of srecord.
5952 \labelwidthstring 00.00.0000
5967 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5976 The linker output (final object code) is in ELF format
5977 \begin_inset LatexCommand \index{ELF format}
5982 (Currently only supported for the HC08 processors)
5984 \labelwidthstring 00.00.0000
5989 linkOption[,linkOption]
5992 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5997 Pass the linkOption to the linker.
5998 See file sdcc/as/doc/asxhtm.html for more on linker options.
6002 \begin_inset LatexCommand \index{Options MCS51}
6007 \begin_inset LatexCommand \index{MCS51 options}
6013 \labelwidthstring 00.00.0000
6028 \begin_inset LatexCommand \index{-\/-model-small}
6039 Generate code for Small Model programs, see section Memory Models for more
6041 This is the default model.
6043 \labelwidthstring 00.00.0000
6058 \begin_inset LatexCommand \index{-\/-model-large}
6064 Generate code for Large model programs, see section Memory Models for more
6066 If this option is used all source files in the project have to be compiled
6069 \labelwidthstring 00.00.0000
6084 \begin_inset LatexCommand \index{-\/-xstack}
6090 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6091 variables and passing parameters.
6093 \begin_inset LatexCommand \ref{sub:External-Stack}
6098 External Stack for more details.
6100 \labelwidthstring 00.00.0000
6118 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6122 Causes the linker to check if the internal ram usage is within limits of
6125 \labelwidthstring 00.00.0000
6143 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6147 Causes the linker to check if the external ram usage is within limits of
6150 \labelwidthstring 00.00.0000
6168 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6172 Causes the linker to check if the code memory usage is within limits of
6175 \labelwidthstring 00.00.0000
6193 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6197 Causes the linker to check if there is at minimum <Value> bytes for stack.
6199 \labelwidthstring 00.00.0000
6217 \begin_inset LatexCommand \index{-\/-pack-iram}
6221 Causes the linker to use unused register banks for data variables and pack
6222 data, idata and stack together.
6223 This is the default now.
6225 \labelwidthstring 00.00.0000
6243 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6247 Causes the linker to use old style for allocating memory areas.
6250 DS390 / DS400 Options
6251 \begin_inset LatexCommand \index{Options DS390}
6256 \begin_inset LatexCommand \index{DS390 options}
6262 \labelwidthstring 00.00.0000
6279 \begin_inset LatexCommand \index{-\/-model-flat24}
6289 Generate 24-bit flat mode code.
6290 This is the one and only that the ds390 code generator supports right now
6291 and is default when using
6296 See section Memory Models for more details.
6298 \labelwidthstring 00.00.0000
6313 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6319 disable interrupts during ESP:SP updates.
6321 \labelwidthstring 00.00.0000
6338 \begin_inset LatexCommand \index{-\/-stack-10bit}
6342 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6343 This is the one and only that the ds390 code generator supports right now
6344 and is default when using
6349 In this mode, the stack is located in the lower 1K of the internal RAM,
6350 which is mapped to 0x400000.
6351 Note that the support is incomplete, since it still uses a single byte
6352 as the stack pointer.
6353 This means that only the lower 256 bytes of the potential 1K stack space
6354 will actually be used.
6355 However, this does allow you to reclaim the precious 256 bytes of low RAM
6356 for use for the DATA and IDATA segments.
6357 The compiler will not generate any code to put the processor into 10 bit
6359 It is important to ensure that the processor is in this mode before calling
6360 any re-entrant functions compiled with this option.
6361 In principle, this should work with the
6374 \begin_inset LatexCommand \index{-\/-stack-auto}
6380 option, but that has not been tested.
6381 It is incompatible with the
6394 \begin_inset LatexCommand \index{-\/-xstack}
6401 It also only makes sense if the processor is in 24 bit contiguous addressing
6414 -model-flat24 option
6418 \labelwidthstring 00.00.0000
6433 \begin_inset LatexCommand \index{-\/-stack-probe}
6439 insert call to function __stack_probe at each function prologue.
6441 \labelwidthstring 00.00.0000
6456 \begin_inset LatexCommand \index{-\/-tini-libid}
6462 <nnnn> LibraryID used in -mTININative.
6465 \labelwidthstring 00.00.0000
6480 \begin_inset LatexCommand \index{-\/-use-accelerator}
6486 generate code for DS390 Arithmetic Accelerator.
6491 \begin_inset LatexCommand \index{Options Z80}
6496 \begin_inset LatexCommand \index{Z80 options}
6502 \labelwidthstring 00.00.0000
6519 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6529 Force a called function to always save BC.
6531 \labelwidthstring 00.00.0000
6548 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6552 When linking, skip the standard crt0.o object file.
6553 You must provide your own crt0.o for your system when linking.
6557 Optimization Options
6558 \begin_inset LatexCommand \index{Options optimization}
6563 \begin_inset LatexCommand \index{Optimization options}
6569 \labelwidthstring 00.00.0000
6584 \begin_inset LatexCommand \index{-\/-nogcse}
6590 Will not do global subexpression elimination, this option may be used when
6591 the compiler creates undesirably large stack/data spaces to store compiler
6601 \begin_inset LatexCommand \index{sloc (spill location)}
6606 A warning message will be generated when this happens and the compiler
6607 will indicate the number of extra bytes it allocated.
6608 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6610 \begin_inset LatexCommand \index{\#pragma nogcse}
6614 can be used to turn off global subexpression elimination
6615 \begin_inset LatexCommand \index{Subexpression elimination}
6619 for a given function only.
6621 \labelwidthstring 00.00.0000
6636 \begin_inset LatexCommand \index{-\/-noinvariant}
6642 Will not do loop invariant optimizations, this may be turned off for reasons
6643 explained for the previous option.
6644 For more details of loop optimizations performed see Loop Invariants in
6646 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6651 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6653 \begin_inset LatexCommand \index{\#pragma noinvariant}
6657 can be used to turn off invariant optimizations for a given function only.
6659 \labelwidthstring 00.00.0000
6674 \begin_inset LatexCommand \index{-\/-noinduction}
6680 Will not do loop induction optimizations, see section strength reduction
6682 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6684 \begin_inset LatexCommand \index{\#pragma noinduction}
6688 can be used to turn off induction optimizations for a given function only.
6690 \labelwidthstring 00.00.0000
6705 \begin_inset LatexCommand \index{-\/-nojtbound}
6716 Will not generate boundary condition check when switch statements
6717 \begin_inset LatexCommand \index{switch statement}
6721 are implemented using jump-tables.
6723 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6728 Switch Statements for more details.
6729 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6731 \begin_inset LatexCommand \index{\#pragma nojtbound}
6735 can be used to turn off boundary checking for jump tables for a given function
6738 \labelwidthstring 00.00.0000
6753 \begin_inset LatexCommand \index{-\/-noloopreverse}
6762 Will not do loop reversal
6763 \begin_inset LatexCommand \index{Loop reversing}
6769 \labelwidthstring 00.00.0000
6786 \begin_inset LatexCommand \index{-\/-nolabelopt }
6790 Will not optimize labels (makes the dumpfiles more readable).
6792 \labelwidthstring 00.00.0000
6807 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6813 Will not memcpy initialized data from code space into xdata space.
6814 This saves a few bytes in code space if you don't have initialized data
6815 \begin_inset LatexCommand \index{Variable initialization}
6821 \labelwidthstring 00.00.0000
6836 \begin_inset LatexCommand \index{-\/-nooverlay}
6842 The compiler will not overlay parameters and local variables of any function,
6843 see section Parameters and local variables for more details.
6845 \labelwidthstring 00.00.0000
6860 \begin_inset LatexCommand \index{-\/-no-peep}
6866 Disable peep-hole optimization.
6868 \labelwidthstring 00.00.0000
6885 \begin_inset LatexCommand \index{-\/-peep-file}
6890 <filename> This option can be used to use additional rules to be used by
6891 the peep hole optimizer.
6893 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6898 Peep Hole optimizations for details on how to write these rules.
6900 \labelwidthstring 00.00.0000
6915 \begin_inset LatexCommand \index{-\/-peep-asm}
6921 Pass the inline assembler code through the peep hole optimizer.
6922 This can cause unexpected changes to inline assembler code, please go through
6923 the peephole optimizer
6924 \begin_inset LatexCommand \index{Peephole optimizer}
6928 rules defined in the source file tree '<target>/peeph.def' before using
6931 \labelwidthstring 00.00.0000
6946 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6952 The compiler will optimize code generation towards fast code, possibly
6953 at the expense of code size.
6955 \labelwidthstring 00.00.0000
6970 \begin_inset LatexCommand \index{-\/-opt-code-size}
6976 The compiler will optimize code generation towards compact code, possibly
6977 at the expense of code speed.
6981 \begin_inset LatexCommand \index{Options other}
6987 \labelwidthstring 00.00.0000
7003 \begin_inset LatexCommand \index{-\/-compile-only}
7008 \begin_inset LatexCommand \index{-c -\/-compile-only}
7014 will compile and assemble the source, but will not call the linkage editor.
7016 \labelwidthstring 00.00.0000
7035 \begin_inset LatexCommand \index{-\/-c1mode}
7041 reads the preprocessed source from standard input and compiles it.
7042 The file name for the assembler output must be specified using the -o option.
7044 \labelwidthstring 00.00.0000
7049 \begin_inset LatexCommand \index{-E}
7055 Run only the C preprocessor.
7056 Preprocess all the C source files specified and output the results to standard
7059 \labelwidthstring 00.00.0000
7065 \begin_inset LatexCommand \index{-o <path/file>}
7071 The output path resp.
7072 file where everything will be placed.
7073 If the parameter is a path, it must have a trailing slash (or backslash
7074 for the Windows binaries) to be recognized as a path.
7077 \labelwidthstring 00.00.0000
7092 \begin_inset LatexCommand \index{-\/-stack-auto}
7103 All functions in the source file will be compiled as
7108 \begin_inset LatexCommand \index{reentrant}
7113 the parameters and local variables will be allocated on the stack
7114 \begin_inset LatexCommand \index{stack}
7120 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7124 Parameters and Local Variables for more details.
7125 If this option is used all source files in the project should be compiled
7127 It automatically implies --int-long-reent and --float-reent.
7130 \labelwidthstring 00.00.0000
7145 \begin_inset LatexCommand \index{-\/-callee-saves}
7149 function1[,function2][,function3]....
7152 The compiler by default uses a caller saves convention for register saving
7153 across function calls, however this can cause unnecessary register pushing
7154 & popping when calling small functions from larger functions.
7155 This option can be used to switch the register saving convention for the
7156 function names specified.
7157 The compiler will not save registers when calling these functions, no extra
7158 code will be generated at the entry & exit (function prologue
7161 \begin_inset LatexCommand \index{function prologue}
7170 \begin_inset LatexCommand \index{function epilogue}
7176 ) for these functions to save & restore the registers used by these functions,
7177 this can SUBSTANTIALLY reduce code & improve run time performance of the
7179 In the future the compiler (with inter procedural analysis) will be able
7180 to determine the appropriate scheme to use for each function call.
7181 DO NOT use this option for built-in functions such as _mulint..., if this
7182 option is used for a library function the appropriate library function
7183 needs to be recompiled with the same option.
7184 If the project consists of multiple source files then all the source file
7185 should be compiled with the same -
7195 -callee-saves option string.
7196 Also see #pragma\SpecialChar ~
7198 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7204 \labelwidthstring 00.00.0000
7219 \begin_inset LatexCommand \index{-\/-debug}
7228 When this option is used the compiler will generate debug information.
7229 The debug information collected in a file with .cdb extension can be used
7231 For more information see documentation for SDCDB.
7232 Another file with no extension contains debug information in AOMF or AOMF51
7233 \begin_inset LatexCommand \index{AOMF, AOMF51}
7237 format which is commonly used by third party tools.
7239 \labelwidthstring 00.00.0000
7244 \begin_inset LatexCommand \index{-S}
7255 Stop after the stage of compilation proper; do not assemble.
7256 The output is an assembler code file for the input file specified.
7258 \labelwidthstring 00.00.0000
7273 \begin_inset LatexCommand \index{-\/-int-long-reent}
7279 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7280 Note by default these libraries are compiled as non-reentrant.
7281 See section Installation for more details.
7283 \labelwidthstring 00.00.0000
7298 \begin_inset LatexCommand \index{-\/-cyclomatic}
7307 This option will cause the compiler to generate an information message for
7308 each function in the source file.
7309 The message contains some
7313 information about the function.
7314 The number of edges and nodes the compiler detected in the control flow
7315 graph of the function, and most importantly the
7317 cyclomatic complexity
7318 \begin_inset LatexCommand \index{Cyclomatic complexity}
7324 see section on Cyclomatic Complexity for more details.
7326 \labelwidthstring 00.00.0000
7341 \begin_inset LatexCommand \index{-\/-float-reent}
7347 Floating point library is compiled as reentrant
7348 \begin_inset LatexCommand \index{reentrant}
7353 See section Installation for more details.
7355 \labelwidthstring 00.00.0000
7370 \begin_inset LatexCommand \index{-\/-main-return}
7376 This option can be used if the code generated is called by a monitor program
7377 or if the main routine includes an endless loop.
7378 This option might result in slightly smaller code and save two bytes of
7380 The return from the 'main'
7381 \begin_inset LatexCommand \index{main return}
7385 function will return to the function calling main.
7386 The default setting is to lock up i.e.
7393 \labelwidthstring 00.00.0000
7408 \begin_inset LatexCommand \index{-\/-nostdinc}
7414 This will prevent the compiler from passing on the default include path
7415 to the preprocessor.
7417 \labelwidthstring 00.00.0000
7432 \begin_inset LatexCommand \index{-\/-nostdlib}
7438 This will prevent the compiler from passing on the default library
7439 \begin_inset LatexCommand \index{Libraries}
7445 \labelwidthstring 00.00.0000
7460 \begin_inset LatexCommand \index{-\/-verbose}
7466 Shows the various actions the compiler is performing.
7468 \labelwidthstring 00.00.0000
7473 \begin_inset LatexCommand \index{-V}
7479 Shows the actual commands the compiler is executing.
7481 \labelwidthstring 00.00.0000
7496 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7502 Hides your ugly and inefficient c-code from the asm file, so you can always
7503 blame the compiler :)
7505 \labelwidthstring 00.00.0000
7520 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7526 Will not include peep-hole comments in the generated files.
7528 \labelwidthstring 00.00.0000
7543 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7549 Include i-codes in the asm file.
7550 Sounds like noise but is most helpful for debugging the compiler itself.
7552 \labelwidthstring 00.00.0000
7567 \begin_inset LatexCommand \index{-\/-less-pedantic}
7573 Disable some of the more pedantic warnings
7574 \begin_inset LatexCommand \index{Warnings}
7578 (jwk burps: please be more specific here, please!).
7580 \labelwidthstring 00.00.0000
7594 -disable-warning\SpecialChar ~
7596 \begin_inset LatexCommand \index{-\/-disable-warning}
7602 Disable specific warning with number <nnnn>.
7604 \labelwidthstring 00.00.0000
7619 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7625 Display the directories in the compiler's search path
7627 \labelwidthstring 00.00.0000
7642 \begin_inset LatexCommand \index{-\/-vc}
7648 Display errors and warnings using MSVC style, so you can use SDCC with
7651 \labelwidthstring 00.00.0000
7666 \begin_inset LatexCommand \index{-\/-use-stdout}
7672 Send errors and warnings to stdout instead of stderr.
7674 \labelwidthstring 00.00.0000
7679 asmOption[,asmOption]
7682 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7687 Pass the asmOption to the assembler
7688 \begin_inset LatexCommand \index{Options assembler}
7693 \begin_inset LatexCommand \index{Assembler options}
7698 See file sdcc/as/doc/asxhtm.html for assembler options.cd
7700 \labelwidthstring 00.00.0000
7715 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7721 Generally follow the C89 standard, but allow SDCC features that conflict
7722 with the standard (default).
7724 \labelwidthstring 00.00.0000
7739 \begin_inset LatexCommand \index{-\/-std-c89}
7745 Follow the C89 standard and disable SDCC features that conflict with the
7748 \labelwidthstring 00.00.0000
7763 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7769 Generally follow the C99 standard, but allow SDCC features that conflict
7770 with the standard (incomplete support).
7772 \labelwidthstring 00.00.0000
7787 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7793 Follow the C99 standard and disable SDCC features that conflict with the
7794 standard (incomplete support).
7796 \labelwidthstring 00.00.0000
7808 a SDCC compiler option but if you want
7812 warnings you can use a separate tool dedicated to syntax checking like
7814 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
7819 \begin_inset LatexCommand \index{lint (syntax checking tool)}
7824 \begin_inset LatexCommand \url{http://www.splint.org}
7829 To make your source files parseable by splint you will have to include
7835 \begin_inset LatexCommand \index{splint (syntax checking tool)}
7839 in your source file and add brackets around extended keywords (like
7842 \begin_inset Quotes sld
7855 \begin_inset Quotes srd
7863 \begin_inset Quotes sld
7866 __interrupt\SpecialChar ~
7868 \begin_inset Quotes srd
7876 Splint has an excellent on line manual at
7877 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
7881 and it's capabilities go beyond pure syntax checking.
7882 You'll need to tell splint the location of SDCC's include files so a typical
7883 command line could look like this:
7887 splint\SpecialChar ~
7889 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
7894 Intermediate Dump Options
7895 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7900 \begin_inset LatexCommand \index{Options intermediate dump}
7905 \begin_inset LatexCommand \index{Intermediate dump options}
7912 The following options are provided for the purpose of retargetting and debugging
7914 They provide a means to dump the intermediate code (iCode
7915 \begin_inset LatexCommand \index{iCode}
7919 ) generated by the compiler in human readable form at various stages of
7920 the compilation process.
7921 More on iCodes see chapter
7922 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7927 \begin_inset Quotes srd
7930 The anatomy of the compiler
7931 \begin_inset Quotes srd
7936 \labelwidthstring 00.00.0000
7951 \begin_inset LatexCommand \index{-\/-dumpraw}
7957 This option will cause the compiler to dump the intermediate code into
7960 <source filename>.dumpraw
7962 just after the intermediate code has been generated for a function, i.e.
7963 before any optimizations are done.
7965 \begin_inset LatexCommand \index{Basic blocks}
7969 at this stage ordered in the depth first number, so they may not be in
7970 sequence of execution.
7972 \labelwidthstring 00.00.0000
7987 \begin_inset LatexCommand \index{-\/-dumpgcse}
7993 Will create a dump of iCode's, after global subexpression elimination
7994 \begin_inset LatexCommand \index{Global subexpression elimination}
8000 <source filename>.dumpgcse.
8002 \labelwidthstring 00.00.0000
8017 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8023 Will create a dump of iCode's, after deadcode elimination
8024 \begin_inset LatexCommand \index{Dead-code elimination}
8030 <source filename>.dumpdeadcode.
8032 \labelwidthstring 00.00.0000
8047 \begin_inset LatexCommand \index{-\/-dumploop}
8056 Will create a dump of iCode's, after loop optimizations
8057 \begin_inset LatexCommand \index{Loop optimization}
8063 <source filename>.dumploop.
8065 \labelwidthstring 00.00.0000
8080 \begin_inset LatexCommand \index{-\/-dumprange}
8089 Will create a dump of iCode's, after live range analysis
8090 \begin_inset LatexCommand \index{Live range analysis}
8096 <source filename>.dumprange.
8098 \labelwidthstring 00.00.0000
8113 \begin_inset LatexCommand \index{-\/-dumlrange}
8119 Will dump the life ranges
8120 \begin_inset LatexCommand \index{Live range analysis}
8126 \labelwidthstring 00.00.0000
8141 \begin_inset LatexCommand \index{-\/-dumpregassign}
8150 Will create a dump of iCode's, after register assignment
8151 \begin_inset LatexCommand \index{Register assignment}
8157 <source filename>.dumprassgn.
8159 \labelwidthstring 00.00.0000
8174 \begin_inset LatexCommand \index{-\/-dumplrange}
8180 Will create a dump of the live ranges of iTemp's
8182 \labelwidthstring 00.00.0000
8197 \begin_inset LatexCommand \index{-\/-dumpall}
8208 Will cause all the above mentioned dumps to be created.
8211 Redirecting output on Windows Shells
8214 By default SDCC writes it's error messages to
8215 \begin_inset Quotes sld
8219 \begin_inset Quotes srd
8223 To force all messages to
8224 \begin_inset Quotes sld
8228 \begin_inset Quotes srd
8252 \begin_inset LatexCommand \index{-\/-use-stdout}
8257 Additionally, if you happen to have visual studio installed in your windows
8258 machine, you can use it to compile your sources using a custom build and
8274 \begin_inset LatexCommand \index{-\/-vc}
8279 Something like this should work:
8323 -model-large -c $(InputPath)
8326 Environment variables
8327 \begin_inset LatexCommand \index{Environment variables}
8334 SDCC recognizes the following environment variables:
8336 \labelwidthstring 00.00.0000
8341 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8347 SDCC installs a signal handler
8348 \begin_inset LatexCommand \index{signal handler}
8352 to be able to delete temporary files after an user break (^C) or an exception.
8353 If this environment variable is set, SDCC won't install the signal handler
8354 in order to be able to debug SDCC.
8356 \labelwidthstring 00.00.0000
8363 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8369 Path, where temporary files will be created.
8370 The order of the variables is the search order.
8371 In a standard *nix environment these variables are not set, and there's
8372 no need to set them.
8373 On Windows it's recommended to set one of them.
8375 \labelwidthstring 00.00.0000
8380 \begin_inset LatexCommand \index{SDCC\_HOME}
8387 \begin_inset LatexCommand \ref{sub:Install-paths}
8393 \begin_inset Quotes sld
8397 \begin_inset Quotes srd
8402 \labelwidthstring 00.00.0000
8407 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8414 \begin_inset LatexCommand \ref{sub:Search-Paths}
8420 \begin_inset Quotes sld
8424 \begin_inset Quotes srd
8429 \labelwidthstring 00.00.0000
8434 \begin_inset LatexCommand \index{SDCC\_LIB}
8441 \begin_inset LatexCommand \ref{sub:Search-Paths}
8447 \begin_inset Quotes sld
8451 \begin_inset Quotes srd
8457 There are some more environment variables recognized by SDCC, but these
8458 are solely used for debugging purposes.
8459 They can change or disappear very quickly, and will never be documented.
8462 Storage Class Language Extensions
8465 MCS51/DS390 Storage Class
8466 \begin_inset LatexCommand \index{Storage class}
8473 In addition to the ANSI storage classes SDCC allows the following MCS51
8474 specific storage classes:
8475 \layout Subsubsection
8478 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8483 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8488 \begin_inset LatexCommand \index{near (storage class)}
8493 \begin_inset LatexCommand \index{\_\_near (storage class)}
8504 storage class for the Small Memory model (
8512 can be used synonymously).
8513 Variables declared with this storage class will be allocated in the directly
8514 addressable portion of the internal RAM of a 8051, e.g.:
8519 data unsigned char test_data;
8522 Writing 0x01 to this variable generates the assembly code:
8527 75*00 01\SpecialChar ~
8533 \layout Subsubsection
8536 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8541 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8546 \begin_inset LatexCommand \index{far (storage class)}
8551 \begin_inset LatexCommand \index{\_\_far (storage class)}
8558 Variables declared with this storage class will be placed in the external
8564 storage class for the Large Memory model, e.g.:
8569 xdata unsigned char test_xdata;
8572 Writing 0x01 to this variable generates the assembly code:
8577 90s00r00\SpecialChar ~
8606 \layout Subsubsection
8609 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8614 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8621 Variables declared with this storage class will be allocated into the indirectly
8622 addressable portion of the internal ram of a 8051, e.g.:
8627 idata unsigned char test_idata;
8630 Writing 0x01 to this variable generates the assembly code:
8659 Please note, the first 128 byte of idata physically access the same RAM
8661 The original 8051 had 128 byte idata memory, nowadays most devices have
8662 256 byte idata memory.
8664 \begin_inset LatexCommand \index{stack}
8668 is located in idata memory.
8669 \layout Subsubsection
8672 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8677 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8684 Paged xdata access is just as straightforward as using the other addressing
8686 It is typically located at the start of xdata and has a maximum size of
8688 The following example writes 0x01 to the pdata variable.
8689 Please note, pdata access physically accesses xdata memory.
8690 The high byte of the address is determined by port P2
8691 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8695 (or in case of some 8051 variants by a separate Special Function Register,
8697 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8706 pdata unsigned char test_pdata;
8709 Writing 0x01 to this variable generates the assembly code:
8753 \begin_inset LatexCommand \index{-\/-xstack}
8757 option is used the pdata memory area is followed by the xstack memory area
8758 and the sum of their sizes is limited to 256 bytes.
8759 \layout Subsubsection
8762 \begin_inset LatexCommand \index{code}
8767 \begin_inset LatexCommand \index{\_\_code}
8774 'Variables' declared with this storage class will be placed in the code
8780 code unsigned char test_code;
8783 Read access to this variable generates the assembly code:
8788 90s00r6F\SpecialChar ~
8791 mov dptr,#_test_code
8820 indexed arrays of characters in code memory can be accessed efficiently:
8825 code char test_array[] = {'c','h','e','a','p'};
8828 Read access to this array using an 8-bit unsigned index generates the assembly
8845 90s00r41\SpecialChar ~
8848 mov dptr,#_test_array
8863 \layout Subsubsection
8866 \begin_inset LatexCommand \index{bit}
8871 \begin_inset LatexCommand \index{\_\_bit}
8878 This is a data-type and a storage class specifier.
8879 When a variable is declared as a bit, it is allocated into the bit addressable
8880 memory of 8051, e.g.:
8888 Writing 1 to this variable generates the assembly code:
8904 The bit addressable memory consists of 128 bits which are located from 0x20
8905 to 0x2f in data memory.
8908 Apart from this 8051 specific storage class most architectures support ANSI-C
8910 \begin_inset LatexCommand \index{bitfields}
8920 Not really meant as examples, but nevertheless showing what bitfields are
8921 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8925 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8926 signed modifier are implemented as unsigned.
8927 \layout Subsubsection
8930 \begin_inset LatexCommand \index{sfr}
8935 \begin_inset LatexCommand \index{\_\_sfr}
8940 \begin_inset LatexCommand \index{\_\_sbit}
8947 Like the bit keyword,
8951 signifies both a data-type and storage class, they are used to describe
8972 variables of a 8051, eg:
8978 \begin_inset LatexCommand \index{at}
8983 \begin_inset LatexCommand \index{\_\_at}
8987 0x80 P0;\SpecialChar ~
8988 /* special function register P0 at location 0x80 */
8990 sbit at 0xd7 CY; /* CY (Carry Flag
8991 \begin_inset LatexCommand \index{Flags}
8996 \begin_inset LatexCommand \index{Carry flag}
9003 Special function registers which are located on an address dividable by
9004 8 are bit-addressable, an
9008 addresses a specific bit within these sfr.
9009 \layout Subsubsection
9012 \begin_inset LatexCommand \index{Pointer}
9016 to MCS51/DS390 specific memory spaces
9019 SDCC allows (via language extensions) pointers to explicitly point to any
9020 of the memory spaces
9021 \begin_inset LatexCommand \index{Memory model}
9026 In addition to the explicit pointers, the compiler uses (by default) generic
9027 pointers which can be used to point to any of the memory spaces.
9031 Pointer declaration examples:
9036 /* pointer physically in internal ram pointing to object in external ram
9039 xdata unsigned char * data p;
9043 /* pointer physically in external ram pointing to object in internal ram
9046 data unsigned char * xdata p;
9050 /* pointer physically in code rom pointing to data in xdata space */
9052 xdata unsigned char * code p;
9056 /* pointer physically in code space pointing to data in code space */
9058 code unsigned char * code p;
9062 /* the following is a generic pointer physically located in xdata space
9068 Well you get the idea.
9073 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9086 The highest order byte of the
9090 pointers contains the data space information.
9091 Assembler support routines are called whenever data is stored or retrieved
9097 These are useful for developing reusable library
9098 \begin_inset LatexCommand \index{Libraries}
9103 Explicitly specifying the pointer type will generate the most efficient
9105 \layout Subsubsection
9107 Notes on MCS51 memory
9108 \begin_inset LatexCommand \index{MCS51 memory}
9115 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9116 RAM memory which is structured as follows:
9120 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9123 - Bytes 20-2F - 16 bytes to hold 128 bit
9124 \begin_inset LatexCommand \index{bit}
9130 - Bytes 30-7F - 80 bytes for general purpose use.
9135 Additionally some members of the MCS51 family may have up to 128 bytes of
9136 additional, indirectly addressable, internal RAM memory (
9141 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9146 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9151 Furthermore, some chips may have some built in external memory (
9156 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9161 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9165 ) which should not be confused with the internal, directly addressable RAM
9171 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9176 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9181 Sometimes this built in
9185 memory has to be activated before using it (you can probably find this
9186 information on the datasheet of the microcontroller your are using, see
9188 \begin_inset LatexCommand \ref{sub:Startup-Code}
9196 Normally SDCC will only use the first bank
9197 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9201 of registers (register bank 0), but it is possible to specify that other
9202 banks of registers (keyword
9209 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9214 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9220 ) should be used in interrupt
9221 \begin_inset LatexCommand \index{interrupt}
9226 \begin_inset LatexCommand \index{\_\_interrupt}
9231 By default, the compiler will place the stack after the last byte of allocated
9232 memory for variables.
9233 For example, if the first 2 banks of registers are used, and only four
9238 variables, it will position the base of the internal stack at address 20
9240 This implies that as the stack
9241 \begin_inset LatexCommand \index{stack}
9245 grows, it will use up the remaining register banks, and the 16 bytes used
9246 by the 128 bit variables, and 80 bytes for general purpose use.
9247 If any bit variables are used, the data variables will be placed in unused
9248 register banks and after the byte holding the last bit variable.
9249 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9254 variables will be placed starting from address 0x10 to 0x20 and continue
9267 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9271 to specify the start address of the
9286 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9290 to specify the size of the total internal RAM (
9302 By default the 8051 linker will place the stack after the last byte of (i)data
9315 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9319 allows you to specify the start of the stack, i.e.
9320 you could start it after any data in the general purpose area.
9321 If your microcontroller has additional indirectly addressable internal
9326 ) you can place the stack on it.
9327 You may also need to use -
9338 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9342 to set the start address of the external RAM (
9357 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9361 to specify its size.
9362 Same goes for the code memory, using -
9373 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9388 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9393 If in doubt, don't specify any options and see if the resulting memory
9394 layout is appropriate, then you can adjust it.
9397 The linker generates two files with memory allocation information.
9398 The first, with extension .map
9399 \begin_inset LatexCommand \index{<file>.map}
9403 shows all the variables and segments.
9404 The second with extension .mem
9405 \begin_inset LatexCommand \index{<file>.mem}
9409 shows the final memory layout.
9410 The linker will complain either if memory segments overlap, there is not
9411 enough memory, or there is not enough space for stack.
9412 If you get any linking warnings and/or errors related to stack or segments
9413 allocation, take a look at either the .map or .mem files to find out what
9415 The .mem file may even suggest a solution to the problem.
9418 Z80/Z180 Storage Class
9419 \begin_inset LatexCommand \index{Storage class}
9424 \layout Subsubsection
9427 \begin_inset LatexCommand \index{sfr}
9432 \begin_inset LatexCommand \index{\_\_sfr}
9436 (in/out to 8-bit addresses)
9440 \begin_inset LatexCommand \index{Z80}
9444 family has separate address spaces for memory and
9454 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9458 is accessed with special instructions, e.g.:
9463 sfr at 0x78 IoPort;\SpecialChar ~
9465 /* define a var in I/O space at 78h called IoPort */
9469 Writing 0x01 to this variable generates the assembly code:
9489 \layout Subsubsection
9492 \begin_inset LatexCommand \index{sfr}
9497 \begin_inset LatexCommand \index{\_\_sfr}
9501 (in/out to 16-bit addresses)
9508 is used to support 16 bit addresses in I/O memory e.g.:
9514 \begin_inset LatexCommand \index{at}
9519 \begin_inset LatexCommand \index{\_\_at}
9526 Writing 0x01 to this variable generates the assembly code:
9531 01 23 01\SpecialChar ~
9551 \layout Subsubsection
9554 \begin_inset LatexCommand \index{sfr}
9559 \begin_inset LatexCommand \index{\_\_sfr}
9563 (in0/out0 to 8 bit addresses on Z180
9564 \begin_inset LatexCommand \index{Z180}
9569 \begin_inset LatexCommand \index{HD64180}
9576 The compiler option -
9586 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9588 \begin_inset LatexCommand \index{\#pragma portmode}
9592 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9602 If you include the file z180.h this will be set automatically.
9606 \begin_inset LatexCommand \index{Storage class}
9611 \layout Subsubsection
9614 \begin_inset LatexCommand \index{data (hc08 storage class)}
9619 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9626 The data storage class declares a variable that resides in the first 256
9627 bytes of memory (the direct page).
9628 The HC08 is most efficient at accessing variables (especially pointers)
9630 \layout Subsubsection
9633 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9638 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9645 The xdata storage class declares a variable that can reside anywhere in
9647 This is the default if no storage class is specified.
9652 \begin_inset LatexCommand \index{Absolute addressing}
9659 Data items can be assigned an absolute address with the
9662 \begin_inset LatexCommand \index{at}
9667 \begin_inset LatexCommand \index{\_\_at}
9673 keyword, in addition to a storage class, e.g.:
9679 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9684 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9689 \begin_inset LatexCommand \index{at}
9694 \begin_inset LatexCommand \index{\_\_at}
9698 0x7ffe unsigned int chksum;
9701 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9702 of the external ram.
9707 reserve any space for variables declared in this way (they are implemented
9708 with an equate in the assembler).
9709 Thus it is left to the programmer to make sure there are no overlaps with
9710 other variables that are declared without the absolute address.
9711 The assembler listing file (.lst
9712 \begin_inset LatexCommand \index{<file>.lst}
9716 ) and the linker output files (.rst
9717 \begin_inset LatexCommand \index{<file>.rst}
9722 \begin_inset LatexCommand \index{<file>.map}
9726 ) are good places to look for such overlaps.
9727 Variables with an absolute address are
9732 \begin_inset LatexCommand \index{Variable initialization}
9739 In case of memory mapped I/O devices the keyword
9743 has to be used to tell the compiler that accesses might not be removed:
9749 \begin_inset LatexCommand \index{volatile}
9754 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9759 \begin_inset LatexCommand \index{at}
9763 0x8000 unsigned char PORTA_8255;
9766 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9771 \begin_inset LatexCommand \index{Aligned array}
9778 starts at a block (256 byte) boundary
9779 \begin_inset LatexCommand \index{block boundary}
9784 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9790 Absolute addresses can be specified for variables in all storage classes,
9797 \begin_inset LatexCommand \index{bit}
9802 \begin_inset LatexCommand \index{at}
9809 The above example will allocate the variable at offset 0x02 in the bit-addressab
9811 There is no real advantage to assigning absolute addresses to variables
9812 in this manner, unless you want strict control over all the variables allocated.
9813 One possible use would be to write hardware portable code.
9814 For example, if you have a routine that uses one or more of the microcontroller
9815 I/O pins, and such pins are different for two different hardwares, you
9816 can declare the I/O pins in your routine using:
9822 \begin_inset LatexCommand \index{volatile}
9826 bit MOSI;\SpecialChar ~
9830 /* master out, slave in */
9832 extern volatile bit MISO;\SpecialChar ~
9836 /* master in, slave out */
9838 extern volatile bit MCLK;\SpecialChar ~
9846 /* Input and Output of a byte on a 3-wire serial bus.
9851 If needed adapt polarity of clock, polarity of data and bit order
9856 unsigned char spi_io(unsigned char out_byte)
9880 MOSI = out_byte & 0x80;
9910 /* _asm nop _endasm; */\SpecialChar ~
9918 /* for slow peripherals */
9969 Then, someplace in the code for the first hardware you would use
9975 \begin_inset LatexCommand \index{at}
9980 \begin_inset LatexCommand \index{\_\_at}
9984 0x80 MOSI;\SpecialChar ~
9988 /* I/O port 0, bit 0 */
9990 bit at 0x81 MISO;\SpecialChar ~
9994 /* I/O port 0, bit 1 */
9996 bit at 0x82 MCLK;\SpecialChar ~
10000 /* I/O port 0, bit 2 */
10003 Similarly, for the second hardware you would use
10008 bit at 0x83 MOSI;\SpecialChar ~
10012 /* I/O port 0, bit 3 */
10014 bit at 0x91 MISO;\SpecialChar ~
10018 /* I/O port 1, bit 1 */
10021 \begin_inset LatexCommand \index{bit}
10025 at 0x92 MCLK;\SpecialChar ~
10029 /* I/O port 1, bit 2 */
10032 and you can use the same hardware dependent routine without changes, as
10033 for example in a library.
10034 This is somehow similar to sbit, but only one absolute address has to be
10035 specified in the whole project.
10039 \begin_inset LatexCommand \index{Parameters}
10044 \begin_inset LatexCommand \index{function parameter}
10049 \begin_inset LatexCommand \index{local variables}
10054 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10061 Automatic (local) variables and parameters to functions can either be placed
10062 on the stack or in data-space.
10063 The default action of the compiler is to place these variables in the internal
10064 RAM (for small model) or external RAM (for large model).
10065 This in fact makes them similar to
10068 \begin_inset LatexCommand \index{static}
10074 so by default functions are non-reentrant
10075 \begin_inset LatexCommand \index{reentrant}
10084 They can be placed on the stack
10085 \begin_inset LatexCommand \index{stack}
10102 \begin_inset LatexCommand \index{-\/-stack-auto}
10110 #pragma\SpecialChar ~
10114 \begin_inset LatexCommand \index{\#pragma stackauto}
10121 \begin_inset LatexCommand \index{reentrant}
10127 keyword in the function declaration, e.g.:
10132 unsigned char foo(char i) reentrant
10146 Since stack space on 8051 is limited, the
10164 option should be used sparingly.
10165 Note that the reentrant keyword just means that the parameters & local
10166 variables will be allocated to the stack, it
10170 mean that the function is register bank
10171 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10180 \begin_inset LatexCommand \index{local variables}
10184 can be assigned storage classes and absolute
10185 \begin_inset LatexCommand \index{Absolute addressing}
10194 unsigned char foo()
10202 xdata unsigned char i;
10215 \begin_inset LatexCommand \index{at}
10219 0x31 unsigned char j;
10231 In the above example the variable
10235 will be allocated in the external ram,
10239 in bit addressable space and
10258 or when a function is declared as
10262 this should only be done for static variables.
10266 \begin_inset LatexCommand \index{function parameter}
10270 however are not allowed any storage class
10271 \begin_inset LatexCommand \index{Storage class}
10275 , (storage classes for parameters will be ignored), their allocation is
10276 governed by the memory model in use, and the reentrancy options.
10280 \begin_inset LatexCommand \label{sub:Overlaying}
10285 \begin_inset LatexCommand \index{Overlaying}
10293 \begin_inset LatexCommand \index{reentrant}
10297 functions SDCC will try to reduce internal ram space usage by overlaying
10298 parameters and local variables of a function (if possible).
10299 Parameters and local variables
10300 \begin_inset LatexCommand \index{local variables}
10304 of a function will be allocated to an overlayable segment if the function
10307 no other function calls and the function is non-reentrant and the memory
10309 \begin_inset LatexCommand \index{Memory model}
10316 If an explicit storage class
10317 \begin_inset LatexCommand \index{Storage class}
10321 is specified for a local variable, it will NOT be overlayed.
10324 Note that the compiler (not the linkage editor) makes the decision for overlayin
10326 Functions that are called from an interrupt service routine should be preceded
10327 by a #pragma\SpecialChar ~
10329 \begin_inset LatexCommand \index{\#pragma nooverlay}
10333 if they are not reentrant.
10336 Also note that the compiler does not do any processing of inline assembler
10337 code, so the compiler might incorrectly assign local variables and parameters
10338 of a function into the overlay segment if the inline assembler code calls
10339 other c-functions that might use the overlay.
10340 In that case the #pragma\SpecialChar ~
10341 nooverlay should be used.
10344 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10346 \begin_inset LatexCommand \index{Multiplication}
10351 \begin_inset LatexCommand \index{Division}
10355 will NOT be overlayed since these are implemented using external functions,
10364 \begin_inset LatexCommand \index{\#pragma nooverlay}
10370 void set_error(unsigned char errcd)
10386 void some_isr () interrupt
10387 \begin_inset LatexCommand \index{interrupt}
10417 In the above example the parameter
10425 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10427 not present, this could cause unpredictable runtime behavior when called
10428 from an interrupt service routine.
10429 The #pragma\SpecialChar ~
10430 nooverlay ensures that the parameters and local variables for
10431 the function are NOT overlayed.
10434 Interrupt Service Routines
10435 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10442 General Information
10457 outines to be coded in C, with some extended keywords.
10462 void timer_isr (void) interrupt 1 using 1
10476 The optional number following the
10479 \begin_inset LatexCommand \index{interrupt}
10484 \begin_inset LatexCommand \index{\_\_interrupt}
10490 keyword is the interrupt number this routine will service.
10491 When present, the compiler will insert a call to this routine in the interrupt
10492 vector table for the interrupt number specified.
10493 If you have multiple source files in your project, interrupt service routines
10494 can be present in any of them, but a prototype of the isr MUST be present
10495 or included in the file that contains the function
10503 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10508 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10514 keyword can be used to tell the compiler to use the specified register
10515 bank (8051 specific) when generating code for this function.
10521 Interrupt service routines open the door for some very interesting bugs:
10523 If an interrupt service routine changes variables which are accessed by
10524 other functions these variables have to be declared
10529 \begin_inset LatexCommand \index{volatile}
10537 If the access to these variables is not
10540 \begin_inset LatexCommand \index{atomic}
10547 the processor needs more than one instruction for the access and could
10548 be interrupted while accessing the variable) the interrupt must be disabled
10549 during the access to avoid inconsistent data.
10550 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10551 and should be protected by disabling interrupts.
10552 You're not automatically on the safe side if you use 8 bit variables though.
10553 We need an example here: f.e.
10554 on the 8051 the harmless looking
10555 \begin_inset Quotes srd
10560 flags\SpecialChar ~
10565 \begin_inset Quotes sld
10574 \begin_inset Quotes srd
10579 flags\SpecialChar ~
10584 \begin_inset Quotes sld
10587 from within an interrupt routine might get lost if the interrupt occurs
10590 \begin_inset Quotes sld
10595 counter\SpecialChar ~
10600 \begin_inset Quotes srd
10603 is not atomic on the 8051 even if
10607 is located in data memory.
10608 Bugs like these are hard to reproduce and can cause a lot of trouble.
10612 The return address and the registers used in the interrupt service routine
10613 are saved on the stack
10614 \begin_inset LatexCommand \index{stack}
10618 so there must be sufficient stack space.
10619 If there isn't variables or registers (or even the return address itself)
10626 \begin_inset LatexCommand \index{stack overflow}
10630 is most likely to happen if the interrupt occurs during the
10631 \begin_inset Quotes sld
10635 \begin_inset Quotes srd
10638 subroutine when the stack is already in use for f.e.
10639 many return addresses.
10642 A special note here, int (16 bit) and long (32 bit) integer division
10643 \begin_inset LatexCommand \index{Division}
10648 \begin_inset LatexCommand \index{Multiplication}
10653 \begin_inset LatexCommand \index{Modulus}
10658 \begin_inset LatexCommand \index{Floating point support}
10662 operations are implemented using external support routines developed in
10664 If an interrupt service routine needs to do any of these operations then
10665 the support routines (as mentioned in a following section) will have to
10666 be recompiled using the
10679 \begin_inset LatexCommand \index{-\/-stack-auto}
10685 option and the source file will need to be compiled using the
10700 \begin_inset LatexCommand \index{-\/-int-long-reent}
10707 Calling other functions from an interrupt service routine is not recommended,
10708 avoid it if possible.
10709 Note that when some function is called from an interrupt service routine
10710 it should be preceded by a #pragma\SpecialChar ~
10712 \begin_inset LatexCommand \index{\#pragma nooverlay}
10716 if it is not reentrant.
10717 Furthermore nonreentrant functions should not be called from the main program
10718 while the interrupt service routine might be active.
10724 \begin_inset LatexCommand \ref{sub:Overlaying}
10729 about Overlaying and section
10730 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10735 about Functions using private register banks.
10738 MCS51/DS390 Interrupt Service Routines
10741 Interrupt numbers and the corresponding address & descriptions for the Standard
10742 8051/8052 are listed below.
10743 SDCC will automatically adjust the interrupt vector table to the maximum
10744 interrupt number specified.
10750 \begin_inset Tabular
10751 <lyxtabular version="3" rows="7" columns="3">
10753 <column alignment="center" valignment="top" leftline="true" width="0in">
10754 <column alignment="center" valignment="top" leftline="true" width="0in">
10755 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10756 <row topline="true" bottomline="true">
10757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10782 <row topline="true">
10783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10799 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10808 <row topline="true">
10809 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10817 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10825 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10834 <row topline="true">
10835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10851 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10860 <row topline="true">
10861 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10877 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10886 <row topline="true">
10887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10912 <row topline="true" bottomline="true">
10913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10921 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10929 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10947 If the interrupt service routine is defined without
10950 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10955 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10961 a register bank or with register bank 0 (
10965 0), the compiler will save the registers used by itself on the stack upon
10966 entry and restore them at exit, however if such an interrupt service routine
10967 calls another function then the entire register bank will be saved on the
10969 This scheme may be advantageous for small interrupt service routines which
10970 have low register usage.
10973 If the interrupt service routine is defined to be using a specific register
10978 & psw are saved and restored, if such an interrupt service routine calls
10979 another function (using another register bank) then the entire register
10980 bank of the called function will be saved on the stack.
10981 This scheme is recommended for larger interrupt service routines.
10984 HC08 Interrupt Service Routines
10987 Since the number of interrupts available is chip specific and the interrupt
10988 vector table always ends at the last byte of memory, the interrupt numbers
10989 corresponds to the interrupt vectors in reverse order of address.
10990 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10991 2 will use the interrupt vector at 0xfffa, and so on.
10992 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10993 this way; instead see section
10994 \begin_inset LatexCommand \ref{sub:Startup-Code}
10998 for details on customizing startup.
11001 Z80 Interrupt Service Routines
11004 The Z80 uses several different methods for determining the correct interrupt
11005 vector depending on the hardware implementation.
11006 Therefore, SDCC ignores the optional interrupt number and does not attempt
11007 to generate an interrupt vector table.
11010 By default, SDCC generates code for a maskable interrupt, which uses an
11011 RETI instruction to return from the interrupt.
11012 To write an interrupt handler for the non-maskable interrupt, which needs
11013 an RETN instruction instead, add the
11022 void nmi_isr (void) critical interrupt
11036 Enabling and Disabling Interrupts
11039 Critical Functions and Critical Statements
11042 A special keyword may be associated with a block or a function declaring
11048 SDCC will generate code to disable all interrupts
11049 \begin_inset LatexCommand \index{interrupt}
11053 upon entry to a critical function and restore the interrupt enable to the
11054 previous state before returning.
11055 Nesting critical functions will need one additional byte on the stack
11056 \begin_inset LatexCommand \index{stack}
11065 int foo () critical
11066 \begin_inset LatexCommand \index{critical}
11071 \begin_inset LatexCommand \index{\_\_critical}
11096 The critical attribute maybe used with other attributes like
11106 may also be used to disable interrupts more locally:
11114 More than one statement could have been included in the block.
11117 Enabling and Disabling Interrupts directly
11121 \begin_inset LatexCommand \index{interrupt}
11125 can also be disabled and enabled directly (8051):
11130 EA = 0;\SpecialChar ~
11193 EA = 1;\SpecialChar ~
11260 On other architectures which have seperate opcodes for enabling and disabling
11261 interrupts you might want to make use of defines with inline assembly
11262 \begin_inset LatexCommand \index{Assembler routines}
11272 \begin_inset LatexCommand \index{\_asm}
11281 \begin_inset LatexCommand \index{\_endasm}
11290 #define SEI _asm\SpecialChar ~
11302 Note: it is sometimes sufficient to disable only a specific interrupt source
11304 a timer or serial interrupt by manipulating an
11307 \begin_inset LatexCommand \index{interrupt mask}
11317 Usually the time during which interrupts are disabled should be kept as
11319 This minimizes both
11324 \begin_inset LatexCommand \index{interrupt latency}
11328 (the time between the occurrence of the interrupt and the execution of
11329 the first code in the interrupt routine) and
11334 \begin_inset LatexCommand \index{interrupt jitter}
11338 (the difference between the shortest and the longest interrupt latency).
11339 These really are something different, f.e.
11340 a serial interrupt has to be served before its buffer overruns so it cares
11341 for the maximum interrupt latency, whereas it does not care about jitter.
11342 On a loudspeaker driven via a digital to analog converter which is fed
11343 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11344 a much smaller jitter will be very audible.
11347 You can reenable interrupts within an interrupt routine and on some architecture
11348 s you can make use of two (or more) levels of
11350 interrupt priorities
11353 \begin_inset LatexCommand \index{interrupt priority}
11358 On some architectures which don't support interrupt priorities these can
11359 be implemented by manipulating the interrupt mask and reenabling interrupts
11360 within the interrupt routine.
11361 Check there is sufficient space on the stack
11362 \begin_inset LatexCommand \index{stack}
11366 and don't add complexity unless you have to.
11371 \begin_inset LatexCommand \index{semaphore}
11375 locking (mcs51/ds390)
11378 Some architectures (mcs51/ds390) have an atomic
11379 \begin_inset LatexCommand \index{atomic}
11392 These type of instructions are typically used in preemptive multitasking
11393 systems, where a routine f.e.
11394 claims the use of a data structure ('acquires a lock
11395 \begin_inset LatexCommand \index{lock}
11399 on it'), makes some modifications and then releases the lock when the data
11400 structure is consistent again.
11401 The instruction may also be used if interrupt and non-interrupt code have
11402 to compete for a resource.
11403 With the atomic bit test and clear instruction interrupts
11404 \begin_inset LatexCommand \index{interrupt}
11408 don't have to be disabled for the locking operation.
11412 SDCC generates this instruction if the source follows this pattern:
11418 \begin_inset LatexCommand \index{volatile}
11422 bit resource_is_free;
11426 if (resource_is_free)
11436 resource_is_free=0;
11449 resource_is_free=1;
11456 Note, mcs51 and ds390 support only an atomic
11457 \begin_inset LatexCommand \index{atomic}
11465 instruction (as opposed to atomic bit test and
11470 Functions using private register banks
11471 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11478 Some architectures have support for quickly changing register sets.
11479 SDCC supports this feature with the
11482 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11487 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11493 attribute (which tells the compiler to use a register bank
11494 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11498 other than the default bank zero).
11499 It should only be applied to
11502 \begin_inset LatexCommand \index{interrupt}
11508 functions (see footnote below).
11509 This will in most circumstances make the generated ISR code more efficient
11510 since it will not have to save registers on the stack.
11517 attribute will have no effect on the generated code for a
11521 function (but may occasionally be useful anyway
11527 possible exception: if a function is called ONLY from 'interrupt' functions
11528 using a particular bank, it can be declared with the same 'using' attribute
11529 as the calling 'interrupt' functions.
11530 For instance, if you have several ISRs using bank one, and all of them
11531 call memcpy(), it might make sense to create a specialized version of memcpy()
11532 'using 1', since this would prevent the ISR from having to save bank zero
11533 to the stack on entry and switch to bank zero before calling the function
11540 (pending: I don't think this has been done yet)
11547 function using a non-zero bank will assume that it can trash that register
11548 bank, and will not save it.
11549 Since high-priority interrupts
11550 \begin_inset LatexCommand \index{interrupts}
11555 \begin_inset LatexCommand \index{interrupt priority}
11559 can interrupt low-priority ones on the 8051 and friends, this means that
11560 if a high-priority ISR
11564 a particular bank occurs while processing a low-priority ISR
11568 the same bank, terrible and bad things can happen.
11569 To prevent this, no single register bank should be
11573 by both a high priority and a low priority ISR.
11574 This is probably most easily done by having all high priority ISRs use
11575 one bank and all low priority ISRs use another.
11576 If you have an ISR which can change priority at runtime, you're on your
11577 own: I suggest using the default bank zero and taking the small performance
11581 It is most efficient if your ISR calls no other functions.
11582 If your ISR must call other functions, it is most efficient if those functions
11583 use the same bank as the ISR (see note 1 below); the next best is if the
11584 called functions use bank zero.
11585 It is very inefficient to call a function using a different, non-zero bank
11591 \begin_inset LatexCommand \label{sub:Startup-Code}
11596 \begin_inset LatexCommand \index{Startup code}
11603 MCS51/DS390 Startup Code
11606 The compiler inserts a call to the C routine
11608 _sdcc_external_startup()
11609 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11618 at the start of the CODE area.
11619 This routine is in the runtime library
11620 \begin_inset LatexCommand \index{Runtime library}
11625 By default this routine returns 0, if this routine returns a non-zero value,
11626 the static & global variable initialization will be skipped and the function
11627 main will be invoked.
11628 Otherwise static & global variables will be initialized before the function
11632 _sdcc_external_startup()
11634 routine to your program to override the default if you need to setup hardware
11635 or perform some other critical operation prior to static & global variable
11637 \begin_inset LatexCommand \index{Variable initialization}
11642 On some mcs51 variants xdata
11643 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11647 memory has to be explicitly enabled before it can be accessed or if the
11648 watchdog needs to be disabled, this is the place to do it.
11649 The startup code clears all internal data memory, 256 bytes by default,
11650 but from 0 to n-1 if
11663 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11670 (recommended for Chipcon CC1010).
11673 See also the compiler option
11692 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11697 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11702 about MCS51-variants.
11708 The HC08 startup code follows the same scheme as the MCS51 startup code.
11714 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11715 from sdcc/device/lib/z80/crt0.s.
11716 If you need a different startup code you can use the compiler option
11737 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11741 and provide your own crt0.o.
11745 Inline Assembler Code
11746 \begin_inset LatexCommand \index{Assembler routines}
11753 A Step by Step Introduction
11754 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11761 Starting from a small snippet of c-code this example shows for the MCS51
11762 how to use inline assembly, access variables, a function parameter and
11763 an array in xdata memory.
11764 The example uses an MCS51 here but is easily adapted for other architectures.
11765 This is a buffer routine which should be optimized:
11772 \begin_inset LatexCommand \index{far (storage class)}
11777 \begin_inset LatexCommand \index{\_\_far (storage class)}
11782 \begin_inset LatexCommand \index{at}
11787 \begin_inset LatexCommand \index{\_\_at}
11792 \begin_inset LatexCommand \index{Aligned array}
11798 unsigned char head,tail;
11802 void to_buffer( unsigned char c )
11810 if( head != tail-1 )
11820 buf[ head++ ] = c;\SpecialChar ~
11824 /* access to a 256 byte aligned array */
11829 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11830 then a corresponding buffer.asm file is generated.
11831 We define a new function
11835 in file buffer.c in which we cut and paste the generated code, removing
11836 unwanted comments and some ':'.
11838 \begin_inset Quotes sld
11842 \begin_inset Quotes srd
11846 \begin_inset Quotes sld
11850 \begin_inset Quotes srd
11853 to the beginning and the end of the function body:
11859 /* With a cut and paste from the .asm file, we have something to start with.
11864 The function is not yet OK! (registers aren't saved) */
11866 void to_buffer_asm( unsigned char c )
11875 \begin_inset LatexCommand \index{\_asm}
11880 \begin_inset LatexCommand \index{\_\_asm}
11894 ;buffer.c if( head != tail-1 )
11942 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11943 \begin_inset LatexCommand \index{Aligned array}
12008 \begin_inset LatexCommand \index{\_endasm}
12013 \begin_inset LatexCommand \index{\_\_endasm}
12022 The new file buffer.c should compile with only one warning about the unreferenced
12023 function argument 'c'.
12024 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12025 (1) and finally have:
12031 unsigned char far at 0x7f00 buf[0x100];
12033 unsigned char head,tail;
12035 #define USE_ASSEMBLY (1)
12043 void to_buffer( unsigned char c )
12051 if( head != tail-1 )
12071 void to_buffer( unsigned char c )
12079 c; // to avoid warning: unreferenced function argument
12086 \begin_inset LatexCommand \index{\_asm}
12091 \begin_inset LatexCommand \index{\_\_asm}
12105 ; save used registers here.
12116 ; If we were still using r2,r3 we would have to push them here.
12119 ; if( head != tail-1 )
12162 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12186 ; buf[ head++ ] = c;
12197 a,dpl \SpecialChar ~
12204 ; dpl holds lower byte of function argument
12215 dpl,_head \SpecialChar ~
12218 ; buf is 0x100 byte aligned so head can be used directly
12260 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12272 ; restore used registers here
12279 \begin_inset LatexCommand \index{\_endasm}
12284 \begin_inset LatexCommand \index{\_\_endasm}
12295 The inline assembler code can contain any valid code understood by the assembler
12296 , this includes any assembler directives and comment lines
12302 The assembler does not like some characters like ':' or ''' in comments.
12303 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12304 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12309 \begin_inset LatexCommand \index{Assembler documentation}
12317 The compiler does not do any validation of the code within the
12320 \begin_inset LatexCommand \index{\_asm}
12325 \begin_inset LatexCommand \index{\_\_asm}
12333 \begin_inset LatexCommand \index{\_endasm}
12338 \begin_inset LatexCommand \index{\_\_endasm}
12347 Specifically it will not know which registers are used and thus register
12349 \begin_inset LatexCommand \index{push/pop}
12353 has to be done manually.
12357 It is recommended that each assembly instruction (including labels) be placed
12358 in a separate line (as the example shows).
12372 \begin_inset LatexCommand \index{-\/-peep-asm}
12378 command line option is used, the inline assembler code will be passed through
12379 the peephole optimizer
12380 \begin_inset LatexCommand \index{Peephole optimizer}
12385 There are only a few (if any) cases where this option makes sense, it might
12386 cause some unexpected changes in the inline assembler code.
12387 Please go through the peephole optimizer rules defined in file
12391 before using this option.
12395 \begin_inset LatexCommand \label{sub:Naked-Functions}
12400 \begin_inset LatexCommand \index{Naked functions}
12407 A special keyword may be associated with a function declaring it as
12410 \begin_inset LatexCommand \index{\_naked}
12415 \begin_inset LatexCommand \index{\_\_naked}
12426 function modifier attribute prevents the compiler from generating prologue
12427 \begin_inset LatexCommand \index{function prologue}
12432 \begin_inset LatexCommand \index{function epilogue}
12436 code for that function.
12437 This means that the user is entirely responsible for such things as saving
12438 any registers that may need to be preserved, selecting the proper register
12439 bank, generating the
12443 instruction at the end, etc.
12444 Practically, this means that the contents of the function must be written
12445 in inline assembler.
12446 This is particularly useful for interrupt functions, which can have a large
12447 (and often unnecessary) prologue/epilogue.
12448 For example, compare the code generated by these two functions:
12454 \begin_inset LatexCommand \index{volatile}
12458 data unsigned char counter;
12462 void simpleInterrupt(void) interrupt
12463 \begin_inset LatexCommand \index{interrupt}
12468 \begin_inset LatexCommand \index{\_\_interrupt}
12486 void nakedInterrupt(void) interrupt 2 _naked
12495 \begin_inset LatexCommand \index{\_asm}
12500 \begin_inset LatexCommand \index{\_\_asm}
12517 _counter ; does not change flags, no need to save psw
12529 ; MUST explicitly include ret or reti in _naked function.
12536 \begin_inset LatexCommand \index{\_endasm}
12541 \begin_inset LatexCommand \index{\_\_endasm}
12550 For an 8051 target, the generated simpleInterrupt looks like:
12691 whereas nakedInterrupt looks like:
12706 _counter ; does not change flags, no need to save psw
12724 ; MUST explicitly include ret or reti in _naked function
12727 The related directive #pragma exclude
12728 \begin_inset LatexCommand \index{\#pragma exclude}
12732 allows a more fine grained control over pushing & popping
12733 \begin_inset LatexCommand \index{push/pop}
12740 While there is nothing preventing you from writing C code inside a
12744 function, there are many ways to shoot yourself in the foot doing this,
12745 and it is recommended that you stick to inline assembler.
12748 Use of Labels within Inline Assembler
12751 SDCC allows the use of in-line assembler with a few restrictions regarding
12753 In older versions of the compiler all labels defined within inline assembler
12762 where nnnn is a number less than 100 (which implies a limit of utmost 100
12763 inline assembler labels
12777 \begin_inset LatexCommand \index{\_asm}
12782 \begin_inset LatexCommand \index{\_\_asm}
12812 \begin_inset LatexCommand \index{\_endasm}
12817 \begin_inset LatexCommand \index{\_\_endasm}
12824 Inline assembler code cannot reference any C-Labels, however it can reference
12826 \begin_inset LatexCommand \index{Labels}
12830 defined by the inline assembler, e.g.:
12855 ; some assembler code
12875 /* some more c code */
12877 clabel:\SpecialChar ~
12879 /* inline assembler cannot reference this label */
12891 $0003: ;label (can be referenced by inline assembler only)
12898 \begin_inset LatexCommand \index{\_endasm}
12903 \begin_inset LatexCommand \index{\_\_endasm}
12913 /* some more c code */
12918 In other words inline assembly code can access labels defined in inline
12919 assembly within the scope of the function.
12920 The same goes the other way, i.e.
12921 labels defines in inline assembly can not be accessed by C statements.
12924 Interfacing with Assembler Code
12925 \begin_inset LatexCommand \index{Assembler routines}
12932 Global Registers used for Parameter Passing
12933 \begin_inset LatexCommand \index{Parameter passing}
12940 The compiler always uses the global registers
12943 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12948 \begin_inset LatexCommand \index{DPTR}
12953 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12962 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12968 to pass the first parameter to a routine.
12969 The second parameter onwards is either allocated on the stack (for reentrant
12980 -stack-auto is used) or in data / xdata memory (depending on the memory
12985 Assembler Routine (non-reentrant)
12988 In the following example
12989 \begin_inset LatexCommand \index{reentrant}
12994 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12998 the function c_func calls an assembler routine asm_func, which takes two
13000 \begin_inset LatexCommand \index{function parameter}
13009 extern int asm_func(unsigned char, unsigned char);
13013 int c_func (unsigned char i, unsigned char j)
13021 return asm_func(i,j);
13035 return c_func(10,9);
13040 The corresponding assembler function is:
13045 .globl _asm_func_PARM_2
13146 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13163 Note here that the return values
13164 \begin_inset LatexCommand \index{return value}
13168 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13170 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13171 b' & 'acc' for four byte values.
13174 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13175 the parameter number starting from 1, and counting from the left.
13176 The first parameter is passed in
13177 \begin_inset Quotes eld
13181 \begin_inset Quotes erd
13184 for a one byte parameter,
13185 \begin_inset Quotes eld
13189 \begin_inset Quotes erd
13193 \begin_inset Quotes eld
13197 \begin_inset Quotes erd
13200 for three bytes and
13201 \begin_inset Quotes eld
13205 \begin_inset Quotes erd
13208 for a four bytes parameter.
13209 The variable name for the second parameter will be _<function_name>_PARM_2.
13213 Assemble the assembler routine with the following command:
13220 asx8051 -losg asmfunc.asm
13227 Then compile and link the assembler routine to the C source file with the
13235 sdcc cfunc.c asmfunc.rel
13238 Assembler Routine (reentrant)
13242 \begin_inset LatexCommand \index{reentrant}
13247 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13251 the second parameter
13252 \begin_inset LatexCommand \index{function parameter}
13256 onwards will be passed on the stack, the parameters are pushed from right
13258 after the call the leftmost parameter will be on the top of the stack.
13259 Here is an example:
13264 extern int asm_func(unsigned char, unsigned char);
13268 int c_func (unsigned char i, unsigned char j) reentrant
13276 return asm_func(i,j);
13290 return c_func(10,9);
13295 The corresponding assembler routine is:
13395 The compiling and linking procedure remains the same, however note the extra
13396 entry & exit linkage required for the assembler code, _bp is the stack
13397 frame pointer and is used to compute the offset into the stack for parameters
13398 and local variables.
13402 \begin_inset LatexCommand \index{int (16 bit)}
13407 \begin_inset LatexCommand \index{long (32 bit)}
13414 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13415 multiplication and modulus operations are implemented by support routines.
13416 These support routines are all developed in ANSI-C to facilitate porting
13417 to other MCUs, although some model specific assembler optimizations are
13419 The following files contain the described routines, all of them can be
13420 found in <installdir>/share/sdcc/lib.
13426 \begin_inset Tabular
13427 <lyxtabular version="3" rows="11" columns="2">
13429 <column alignment="center" valignment="top" leftline="true" width="0">
13430 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13431 <row topline="true" bottomline="true">
13432 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13453 <row topline="true">
13454 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13462 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13467 16 bit multiplication
13471 <row topline="true">
13472 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13480 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13485 signed 16 bit division (calls _divuint)
13489 <row topline="true">
13490 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13503 unsigned 16 bit division
13507 <row topline="true">
13508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13516 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13521 signed 16 bit modulus (calls _moduint)
13525 <row topline="true">
13526 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13534 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13539 unsigned 16 bit modulus
13543 <row topline="true">
13544 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13552 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13557 32 bit multiplication
13561 <row topline="true">
13562 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13570 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13575 signed 32 division (calls _divulong)
13579 <row topline="true">
13580 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13588 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13593 unsigned 32 division
13597 <row topline="true">
13598 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13606 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13611 signed 32 bit modulus (calls _modulong)
13615 <row topline="true" bottomline="true">
13616 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13624 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13629 unsigned 32 bit modulus
13642 Since they are compiled as
13647 \begin_inset LatexCommand \index{reentrant}
13652 \begin_inset LatexCommand \index{interrupt}
13656 service routines should not do any of the above operations.
13657 If this is unavoidable then the above routines will need to be compiled
13671 \begin_inset LatexCommand \index{-\/-stack-auto}
13677 option, after which the source program will have to be compiled with
13690 \begin_inset LatexCommand \index{-\/-int-long-reent}
13697 Notice that you don't have to call these routines directly.
13698 The compiler will use them automatically every time an integer operation
13702 Floating Point Support
13703 \begin_inset LatexCommand \index{Floating point support}
13710 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13711 floating point support routines are derived from gcc's floatlib.c and consist
13712 of the following routines:
13720 \begin_inset Tabular
13721 <lyxtabular version="3" rows="17" columns="2">
13723 <column alignment="center" valignment="top" leftline="true" width="0">
13724 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13725 <row topline="true" bottomline="true">
13726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13743 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13752 <row topline="true">
13753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13784 add floating point numbers
13788 <row topline="true">
13789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13820 subtract floating point numbers
13824 <row topline="true">
13825 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13856 divide floating point numbers
13860 <row topline="true">
13861 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13892 multiply floating point numbers
13896 <row topline="true">
13897 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13928 convert floating point to unsigned char
13932 <row topline="true">
13933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13950 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13964 convert floating point to signed char
13968 <row topline="true">
13969 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14000 convert floating point to unsigned int
14004 <row topline="true">
14005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14036 convert floating point to signed int
14040 <row topline="true">
14041 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14067 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14081 convert floating point to unsigned long
14085 <row topline="true">
14086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14117 convert floating point to signed long
14121 <row topline="true">
14122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14139 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14153 convert unsigned char to floating point
14157 <row topline="true">
14158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14175 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14189 convert char to floating point number
14193 <row topline="true">
14194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14211 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14225 convert unsigned int to floating point
14229 <row topline="true">
14230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14247 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14261 convert int to floating point numbers
14265 <row topline="true">
14266 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14283 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14297 convert unsigned long to floating point number
14301 <row topline="true" bottomline="true">
14302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14333 convert long to floating point number
14346 These support routines are developed in ANSI-C so there is room for space
14347 and speed improvement
14353 The floating point routines for the mcs51 are implemented in assembler
14357 Note if all these routines are used simultaneously the data space might
14359 For serious floating point usage the large model might be needed.
14360 Also notice that you don't have to call this routines directly.
14361 The compiler will use them automatically every time a floating point operation
14366 \begin_inset LatexCommand \index{Libraries}
14375 <pending: this is messy and incomplete - a little more information is in
14376 sdcc/doc/libdoc.txt
14381 Compiler support routines (_gptrget, _mulint etc.)
14384 Stdclib functions (puts, printf, strcat etc.)
14385 \layout Subsubsection
14391 \begin_inset LatexCommand \index{<stdio.h>}
14395 As usual on embedded systems you have to provide your own
14398 \begin_inset LatexCommand \index{getchar()}
14407 \begin_inset LatexCommand \index{putchar()}
14414 SDCC does not know whether the system connects to a serial line with or
14415 without handshake, LCD, keyboard or other device.
14416 You'll find examples for serial routines f.e.
14417 in sdcc/device/lib.
14423 \begin_inset LatexCommand \index{printf()}
14433 does not support float (except on ds390).
14434 To enable this recompile it with the option
14447 \begin_inset LatexCommand \index{USE\_FLOATS}
14453 on the command line.
14467 \begin_inset LatexCommand \index{-\/-model-large}
14473 for the mcs51 port, since this uses a lot of memory.
14476 If you're short on memory you might want to use
14479 \begin_inset LatexCommand \index{printf\_small()}
14494 For the mcs51 there additionally are assembly versions
14497 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14506 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14515 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14521 which should fit the requirements of many embedded systems (printf_fast()
14522 can be customized by unsetting #defines to
14526 support long variables and field widths).
14529 Math functions (sin, pow, sqrt etc.)
14536 \begin_inset LatexCommand \index{Libraries}
14540 included in SDCC should have a license at least as liberal as the GNU Lesser
14541 General Public License
14542 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14553 license statements for the libraries are missing.
14554 sdcc/device/lib/ser_ir.c
14558 come with a GPL (as opposed to LGPL) License - this will not be liberal
14559 enough for many embedded programmers.
14562 If you have ported some library or want to share experience about some code
14564 falls into any of these categories Busses (I
14565 \begin_inset Formula $^{\textrm{2}}$
14568 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14569 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14570 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14571 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14576 would certainly like to hear about it.
14577 Programmers coding for embedded systems are not especially famous for being
14578 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14579 e these references are very valuable.
14580 Let's help to create a climate where information is shared.
14586 MCS51 Memory Models
14587 \begin_inset LatexCommand \index{Memory model}
14592 \begin_inset LatexCommand \index{MCS51 memory model}
14597 \layout Subsubsection
14602 SDCC allows two memory models for MCS51 code,
14611 Modules compiled with different memory models should
14615 be combined together or the results would be unpredictable.
14616 The library routines supplied with the compiler are compiled as both small
14618 The compiled library modules are contained in separate directories as small
14619 and large so that you can link to either set.
14623 When the large model is used all variables declared without a storage class
14624 will be allocated into the external ram, this includes all parameters and
14625 local variables (for non-reentrant
14626 \begin_inset LatexCommand \index{reentrant}
14631 When the small model is used variables without storage class are allocated
14632 in the internal ram.
14635 Judicious usage of the processor specific storage classes
14636 \begin_inset LatexCommand \index{Storage class}
14640 and the 'reentrant' function type will yield much more efficient code,
14641 than using the large model.
14642 Several optimizations are disabled when the program is compiled using the
14643 large model, it is therefore recommended that the small model be used unless
14644 absolutely required.
14645 \layout Subsubsection
14648 \begin_inset LatexCommand \label{sub:External-Stack}
14653 \begin_inset LatexCommand \index{stack}
14658 \begin_inset LatexCommand \index{External stack (mcs51)}
14665 The external stack (-
14676 \begin_inset LatexCommand \index{-\/-xstack}
14680 ) is located in pdata
14681 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14685 memory (usually at the start of the external ram segment) and uses all
14686 unused space in pdata (max.
14698 -xstack option is used to compile the program, the parameters and local
14700 \begin_inset LatexCommand \index{local variables}
14704 of all reentrant functions are allocated in this area.
14705 This option is provided for programs with large stack space requirements.
14706 When used with the -
14717 \begin_inset LatexCommand \index{-\/-stack-auto}
14721 option, all parameters and local variables are allocated on the external
14722 stack (note: support libraries will need to be recompiled with the same
14724 There is a predefined target in the library makefile).
14727 The compiler outputs the higher order address byte of the external ram segment
14729 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14734 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14738 ), therefore when using the External Stack option, this port
14742 be used by the application program.
14746 \begin_inset LatexCommand \index{Memory model}
14751 \begin_inset LatexCommand \index{DS390 memory model}
14758 The only model supported is Flat 24
14759 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14764 This generates code for the 24 bit contiguous addressing mode of the Dallas
14766 In this mode, up to four meg of external RAM or code space can be directly
14768 See the data sheets at www.dalsemi.com for further information on this part.
14772 Note that the compiler does not generate any code to place the processor
14773 into 24 bitmode (although
14777 in the ds390 libraries will do that for you).
14783 \begin_inset LatexCommand \index{Tinibios (DS390)}
14787 , the boot loader or similar code must ensure that the processor is in 24
14788 bit contiguous addressing mode before calling the SDCC startup code.
14806 option, variables will by default be placed into the XDATA segment.
14811 Segments may be placed anywhere in the 4 meg address space using the usual
14823 Note that if any segments are located above 64K, the -r flag must be passed
14824 to the linker to generate the proper segment relocations, and the Intel
14825 HEX output format must be used.
14826 The -r flag can be passed to the linker by using the option
14830 on the SDCC command line.
14831 However, currently the linker can not handle code segments > 64k.
14835 \begin_inset LatexCommand \index{Pragmas}
14842 SDCC supports the following #pragma directives:
14846 \begin_inset LatexCommand \index{\#pragma save}
14850 - this will save all current options to the save/restore stack.
14851 See #pragma\SpecialChar ~
14856 \begin_inset LatexCommand \index{\#pragma restore}
14860 - will restore saved options from the last save.
14861 saves & restores can be nested.
14862 SDCC uses a save/restore stack: save pushes current options to the stack,
14863 restore pulls current options from the stack.
14864 See #pragma\SpecialChar ~
14871 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14876 \begin_inset LatexCommand \index{function prologue}
14880 function1[,function2[,function3...]] - The compiler by default uses a caller
14881 saves convention for register saving across function calls, however this
14882 can cause unnecessary register pushing & popping
14883 \begin_inset LatexCommand \index{push/pop}
14887 when calling small functions from larger functions.
14888 This option can be used to switch off the register saving convention for
14889 the function names specified.
14890 The compiler will not save registers when calling these functions, extra
14891 code need to be manually inserted at the entry & exit for these functions
14892 to save & restore the registers used by these functions, this can SUBSTANTIALLY
14893 reduce code & improve run time performance of the generated code.
14894 In the future the compiler (with inter procedural analysis) may be able
14895 to determine the appropriate scheme to use for each function call.
14906 -callee-saves command line option is used, the function names specified
14907 in #pragma\SpecialChar ~
14909 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14913 is appended to the list of functions specified in the command line.
14917 \begin_inset LatexCommand \index{\#pragma exclude}
14921 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
14922 of pairs of push/pop
14923 \begin_inset LatexCommand \index{push/pop}
14932 \begin_inset LatexCommand \index{interrupt}
14945 The directive should be placed immediately before the ISR function definition
14946 and it affects ALL ISR functions following it.
14947 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
14948 exclude\SpecialChar ~
14950 \begin_inset LatexCommand \index{\#pragma exclude}
14955 See also the related keyword _naked
14956 \begin_inset LatexCommand \index{\_naked}
14961 \begin_inset LatexCommand \index{\_\_naked}
14969 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
14973 - the compiler will not warn you anymore for obvious mistakes, you'r on
14977 disable_warning <nnnn>
14978 \begin_inset LatexCommand \index{\#pragma disable\_warning}
14982 - the compiler will not warn you anymore about warning number <nnnn>.
14986 \begin_inset LatexCommand \index{\#pragma nogcse}
14990 - will stop global common subexpression elimination.
14994 \begin_inset LatexCommand \index{\#pragma noinduction}
14998 - will stop loop induction optimizations.
15002 \begin_inset LatexCommand \index{\#pragma noinvariant}
15006 - will not do loop invariant optimizations.
15007 For more details see Loop Invariants in section
15008 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15016 \begin_inset LatexCommand \index{\#pragma noiv}
15020 - Do not generate interrupt
15021 \begin_inset LatexCommand \index{interrupt}
15025 vector table entries for all ISR functions defined after the pragma.
15026 This is useful in cases where the interrupt vector table must be defined
15027 manually, or when there is a secondary, manually defined interrupt vector
15029 for the autovector feature of the Cypress EZ-USB FX2).
15030 More elegantly this can be achieved by obmitting the optional interrupt
15031 number after the interrupt keyword, see section
15032 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15041 \begin_inset LatexCommand \index{\#pragma nojtbound}
15045 - will not generate code for boundary value checking, when switch statements
15046 are turned into jump-tables (dangerous).
15047 For more details see section
15048 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15056 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15060 - Will not do loop reversal optimization
15064 \begin_inset LatexCommand \index{\#pragma nooverlay}
15068 - the compiler will not overlay the parameters and local variables of a
15073 \begin_inset LatexCommand \index{\#pragma stackauto}
15088 \begin_inset LatexCommand \index{-\/-stack-auto}
15093 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15097 Parameters and Local Variables.
15101 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15105 - The compiler will optimize code generation towards fast code, possibly
15106 at the expense of code size.
15110 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15114 - The compiler will optimize code generation towards compact code, possibly
15115 at the expense of code speed.
15119 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15123 - The compiler will attempt to generate code that is both compact and fast,
15124 as long as meeting one goal is not a detriment to the other (this is the
15130 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15134 - Generally follow the C89 standard, but allow SDCC features that conflict
15135 with the standard (default).
15139 \begin_inset LatexCommand \index{\#pragma std\_c89}
15143 - Follow the C89 standard and disable SDCC features that conflict with the
15148 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15152 - Generally follow the C99 standard, but allow SDCC features that conflict
15153 with the standard (incomplete support).
15157 \begin_inset LatexCommand \index{\#pragma std\_c99}
15161 - Follow the C99 standard and disable SDCC features that conflict with the
15162 standard (incomplete support).
15165 SDCPP supports the following #pragma directives:
15169 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15173 (+ | -) - switch _asm _endasm block preprocessing on / off.
15177 The pragma's are intended to be used to turn-on or off certain optimizations
15178 which might cause the compiler to generate extra stack / data space to
15179 store compiler generated temporary variables.
15180 This usually happens in large functions.
15181 Pragma directives should be used as shown in the following example, they
15182 are used to control options & optimizations for a given function; pragmas
15183 should be placed before and/or after a function, placing pragma's inside
15184 a function body could have unpredictable results.
15190 \begin_inset LatexCommand \index{\#pragma save}
15201 /* save the current settings */
15204 \begin_inset LatexCommand \index{\#pragma nogcse}
15213 /* turnoff global subexpression elimination */
15215 #pragma noinduction
15216 \begin_inset LatexCommand \index{\#pragma noinduction}
15220 /* turn off induction optimizations */
15243 \begin_inset LatexCommand \index{\#pragma restore}
15247 /* turn the optimizations back on */
15250 The compiler will generate a warning message when extra space is allocated.
15251 It is strongly recommended that the save and restore pragma's be used when
15252 changing options for a function.
15255 Defines Created by the Compiler
15258 The compiler creates the following #defines
15259 \begin_inset LatexCommand \index{\#defines}
15264 \begin_inset LatexCommand \index{Defines created by the compiler}
15274 \begin_inset Tabular
15275 <lyxtabular version="3" rows="10" columns="2">
15277 <column alignment="center" valignment="top" leftline="true" width="0">
15278 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15279 <row topline="true" bottomline="true">
15280 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15290 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15301 <row topline="true">
15302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15308 \begin_inset LatexCommand \index{SDCC}
15315 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15320 this Symbol is always defined
15324 <row topline="true">
15325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15331 \begin_inset LatexCommand \index{SDCC\_mcs51}
15336 \begin_inset LatexCommand \index{SDCC\_ds390}
15341 \begin_inset LatexCommand \index{SDCC\_z80}
15348 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15353 depending on the model used (e.g.: -mds390
15357 <row topline="true">
15358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15364 \begin_inset LatexCommand \index{\_\_mcs51}
15369 \begin_inset LatexCommand \index{\_\_ds390}
15374 \begin_inset LatexCommand \index{\_\_hc08}
15379 \begin_inset LatexCommand \index{\_\_z80}
15386 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15391 depending on the model used (e.g.
15396 <row topline="true">
15397 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15403 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15410 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15433 <row topline="true">
15434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15440 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15447 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15470 <row topline="true">
15471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15477 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15484 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15507 <row topline="true">
15508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15514 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15521 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15544 <row topline="true">
15545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15551 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15558 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15571 <row topline="true" bottomline="true">
15572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15578 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15605 Notes on supported Processors
15609 \begin_inset LatexCommand \label{sub:MCS51-variants}
15614 \begin_inset LatexCommand \index{MCS51 variants}
15621 MCS51 processors are available from many vendors and come in many different
15623 While they might differ considerably in respect to Special Function Registers
15624 the core MCS51 is usually not modified or is kept compatible.
15628 pdata access by SFR
15631 With the upcome of devices with internal xdata and flash memory devices
15633 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15637 as dedicated I/O port is becoming more popular.
15638 Switching the high byte for pdata
15639 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15643 access which was formerly done by port P2 is then achieved by a Special
15645 \begin_inset LatexCommand \index{sfr}
15650 In well-established MCS51 tradition the address of this
15654 is where the chip designers decided to put it.
15655 Needless to say that they didn't agree on a common name either.
15656 So that the startup code can correctly initialize xdata variables, you
15657 should define an sfr with the name _XPAGE
15660 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15666 at the appropriate location if the default, port P2, is not used for this.
15672 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15677 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15682 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15685 For more exotic implementations further customizations may be needed.
15687 \begin_inset LatexCommand \ref{sub:Startup-Code}
15691 for other possibilities.
15694 Other Features available by SFR
15697 Some MCS51 variants offer features like Double DPTR
15698 \begin_inset LatexCommand \index{DPTR}
15702 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
15703 These are currently not used for the MCS51 port.
15704 If you absolutely need them you can fall back to inline assembly or submit
15711 The DS80C400 microcontroller has a rich set of peripherals.
15712 In its built-in ROM library it includes functions to access some of the
15713 features, among them is a TCP stack with IP4 and IP6 support.
15714 Library headers (currently in beta status) and other files are provided
15718 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
15726 The Z80 and gbz80 port
15729 SDCC can target both the Zilog
15730 \begin_inset LatexCommand \index{Z80}
15734 and the Nintendo Gameboy's Z80-like gbz80
15735 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
15740 The Z80 port is passed through the same
15743 \begin_inset LatexCommand \index{Regression test}
15749 as the MCS51 and DS390 ports, so floating point support, support for long
15750 variables and bitfield support is fine.
15751 See mailing lists and forums about interrupt routines.
15754 As always, the code is the authoritative reference - see z80/ralloc.c and
15757 \begin_inset LatexCommand \index{stack}
15761 frame is similar to that generated by the IAR Z80 compiler.
15762 IX is used as the base pointer, HL and IY are used as a temporary registers,
15763 and BC and DE are available for holding variables.
15765 \begin_inset LatexCommand \index{return value}
15769 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
15771 The gbz80 port use the same set of registers for the return values, but
15772 in a different order of significance: E (one byte), DE (two bytes), or
15779 The port to the Motorola HC08
15780 \begin_inset LatexCommand \index{HC08}
15784 family has been added in October 2003, and is still undergoing some basic
15786 The code generator is complete, but the register allocation is still quite
15788 Some of the SDCC's standard C library functions have embedded non-HC08
15789 inline assembly and so are not yet usable.
15800 \begin_inset LatexCommand \index{PIC14}
15804 port still requires a major effort from the development community.
15805 However it can work for very simple code.
15808 C code and 14bit PIC code page
15809 \begin_inset LatexCommand \index{code page (pic14)}
15814 \begin_inset LatexCommand \index{RAM bank (pic14)}
15821 The linker organizes allocation for the code page and RAM banks.
15822 It does not have intimate knowledge of the code flow.
15823 It will put all the code section of a single asm file into a single code
15825 In order to make use of multiple code pages, separate asm files must be
15827 The compiler treats all functions of a single C file as being in the same
15828 code page unless it is non static.
15829 The compiler treats all local variables of a single C file as being in
15830 the same RAM bank unless it is an extern.
15834 To get the best follow these guide lines:
15837 make local functions static, as non static functions require code page selection
15841 Make local variables static as extern variables require RAM bank selection
15845 For devices that have multiple code pages it is more efficient to use the
15846 same number of files as pages, i.e.
15847 for the 16F877 use 4 separate files and i.e.
15848 for the 16F874 use 2 separate files.
15849 This way the linker can put the code for each file into different code
15850 pages and the compiler can allocate reusable variables more efficiently
15851 and there's less page selection overhead.
15852 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
15853 instruction set) use 'unsigned char' whereever possible instead of 'int'.
15856 Creating a device include file
15859 For generating a device include file use the support perl script inc2h.pl
15860 kept in directory support/script.
15866 For the interrupt function, use the keyword 'interrupt'
15867 \begin_inset LatexCommand \index{interrupt}
15871 with level number of 0 (PIC14 only has 1 interrupt so this number is only
15872 there to avoid a syntax error - it ought to be fixed).
15878 void Intr(void) interrupt 0
15884 T0IF = 0; /* Clear timer interrupt */
15889 Linking and assembling
15892 For assembling you can use either GPUTILS'
15893 \begin_inset LatexCommand \index{gputils (pic tools)}
15897 gpasm.exe or MPLAB's mpasmwin.exe.
15898 GPUTILS is available from
15899 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
15904 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
15905 If you use MPLAB and an interrupt function then the linker script file
15906 vectors section will need to be enlarged to link with mplink.
15929 sdcc -S -V -mpic14 -p16F877 $<
15943 $(PRJ).hex: $(OBJS)
15953 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
15975 sdcc -S -V -mpic14 -p16F877 $<
15985 mpasmwin /q /o $*.asm
15989 $(PRJ).hex: $(OBJS)
15999 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
16002 Please note that indentations within a
16006 have to be done with a tabulator character.
16010 \begin_inset LatexCommand \index{PIC16}
16018 \begin_inset LatexCommand \index{PIC16}
16022 port is the portion of SDCC that is responsible to produce code for the
16024 \begin_inset LatexCommand \index{Microchip}
16028 (TM) microcontrollers with 16 bit core.
16029 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16030 Currently supported devices are:
16034 \begin_inset Tabular
16035 <lyxtabular version="3" rows="4" columns="6">
16037 <column alignment="center" valignment="top" leftline="true" width="0">
16038 <column alignment="center" valignment="top" leftline="true" width="0">
16039 <column alignment="center" valignment="top" leftline="true" width="0">
16040 <column alignment="center" valignment="top" leftline="true" width="0">
16041 <column alignment="center" valignment="top" leftline="true" width="0">
16042 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16043 <row topline="true">
16044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16084 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16093 <row topline="true">
16094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16102 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16118 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16134 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16143 <row topline="true">
16144 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16176 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16193 <row topline="true" bottomline="true">
16194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16218 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16225 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16250 PIC16 port supports the standard command line arguments as supposed, with
16251 the exception of certain cases that will be mentioned in the following
16254 \labelwidthstring 00.00.0000
16266 -callee-saves See -
16278 \labelwidthstring 00.00.0000
16290 -all-callee-saves All function arguments are passed on stack by default.
16293 There is no need to specify this in the command line.
16295 \labelwidthstring 00.00.0000
16307 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16308 no local variables.
16311 Port Specific Options
16312 \begin_inset LatexCommand \index{Options PIC16}
16319 The port specific options appear after the global options in the sdcc --help
16321 \layout Subsubsection
16326 General options enable certain port features and optimizations.
16328 \labelwidthstring 00.00.0000
16340 -stack-model=[model] Used in conjuction with the command above.
16341 Defines the stack model to be used, valid stack models are :
16344 \labelwidthstring 00.00.0000
16350 Selects small stack model.
16351 8 bit stack and frame pointers.
16352 Supports 256 bytes stack size.
16354 \labelwidthstring 00.00.0000
16360 Selects large stack model.
16361 16 bit stack and frame pointers.
16362 Supports 65536 bytes stack size.
16365 \labelwidthstring 00.00.0000
16377 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16378 unitialized data variables with [kword].
16379 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16381 \labelwidthstring 00.00.0000
16393 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16394 Useful for bootloaders.
16396 \labelwidthstring 00.00.0000
16408 -asm= sets the full path and name of an external assembler to call.
16410 \labelwidthstring 00.00.0000
16422 -link= sets the full path and name of an external linker to call.
16424 \labelwidthstring 00.00.0000
16436 -mplab-comp MPLAB compatibility option.
16437 Currently only suppresses special gpasm directives.
16438 \layout Subsubsection
16440 Optimization Options
16442 \labelwidthstring 00.00.0000
16454 -optimize-goto Try to use (conditional) BRA instead of GOTO
16456 \labelwidthstring 00.00.0000
16468 -optimize-cmp Try to optimize some compares.
16470 \labelwidthstring 00.00.0000
16482 -obanksel=nn Set optimization level for inserting BANKSELs.
16487 \labelwidthstring 00.00.0000
16491 \labelwidthstring 00.00.0000
16493 1 checks previous used register and if it is the same then does not emit
16494 BANKSEL, accounts only for labels.
16496 \labelwidthstring 00.00.0000
16498 2 tries to check the location of (even different) symbols and removes BANKSELs
16499 if they are in the same bank.
16504 Important: There might be problems if the linker script has data sections
16505 across bank borders!
16507 \layout Subsubsection
16511 \labelwidthstring 00.00.0000
16523 -nodefaultlibs do not link default libraries when linking
16525 \labelwidthstring 00.00.0000
16537 -no-crt Don't link the default run-time modules
16539 \labelwidthstring 00.00.0000
16551 -use-crt= Use a custom run-time module instead of the defaults.
16552 \layout Subsubsection
16557 Debugging options enable extra debugging information in the output files.
16559 \labelwidthstring 00.00.0000
16571 -debug-xtra Similar to -
16582 \begin_inset LatexCommand \index{-\/-debug}
16586 , but dumps more information.
16588 \labelwidthstring 00.00.0000
16600 -debug-ralloc Force register allocator to dump <source>.d file with debugging
16602 <source> is the name of the file compiled.
16604 \labelwidthstring 00.00.0000
16616 -pcode-verbose Enable pcode debugging information in translation.
16618 \labelwidthstring 00.00.0000
16630 -denable-peeps Force the usage of peepholes.
16633 \labelwidthstring 00.00.0000
16645 -gstack Trace push/pops for stack pointer overflow
16647 \labelwidthstring 00.00.0000
16659 -call-tree dump call tree in .calltree file
16662 Enviromental Variables
16665 There is a number of enviromental variables that can be used when running
16666 SDCC to enable certain optimizations or force a specific program behaviour.
16667 these variables are primarily for debugging purposes so they can be enabled/dis
16671 Currently there is only two such variables available:
16673 \labelwidthstring 00.00.0000
16675 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
16676 bitfields is optimized by directly loading FSR0 with the address of the
16677 bitfield structure.
16678 Normally SDCC will cast the bitfield structure to a bitfield pointer and
16680 This step saves data ram and code space for functions that perform heavy
16683 80 bytes of code space are saved when compiling malloc.c with this option).
16686 \labelwidthstring 00.00.0000
16688 NO_REG_OPT do not perform pCode registers optimization.
16689 This should be used for debugging purposes.
16690 In some where bugs in the pcode optimizer are found, users can benefit
16691 from temporarily disabling the optimizer until the bug is fixed.
16694 Preprocessor Macros
16697 PIC16 port defines the following preprocessor macros while translating a
16702 \begin_inset Tabular
16703 <lyxtabular version="3" rows="6" columns="2">
16705 <column alignment="center" valignment="top" leftline="true" width="0">
16706 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16707 <row topline="true" bottomline="true">
16708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16725 <row topline="true">
16726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16734 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16739 Port identification
16743 <row topline="true">
16744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16762 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16767 Port identification (same as above)
16771 <row topline="true">
16772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16785 MCU Identification.
16790 is the microcontrol identification number, i.e.
16795 <row topline="true">
16796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16814 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16819 MCU Identification (same as above)
16823 <row topline="true" bottomline="true">
16824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16832 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16837 nnn = SMALL or LARGE respectively according to the stack model used
16848 In addition the following macros are defined when calling assembler:
16852 \begin_inset Tabular
16853 <lyxtabular version="3" rows="4" columns="2">
16855 <column alignment="center" valignment="top" leftline="true" width="0">
16856 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16857 <row topline="true" bottomline="true">
16858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16875 <row topline="true">
16876 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16889 MCU Identification.
16894 is the microcontrol identification number, i.e.
16899 <row topline="true">
16900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16913 nnn = SMALL or LARGE respectively according to the memory model used for
16918 <row topline="true" bottomline="true">
16919 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16927 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16932 nnn = SMALL or LARGE respectively according to the stack model used
16947 \begin_inset LatexCommand \index{PIC16}
16951 port uses the following directories for searching header files and libraries.
16955 \begin_inset Tabular
16956 <lyxtabular version="3" rows="3" columns="4">
16958 <column alignment="center" valignment="top" leftline="true" width="0">
16959 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16960 <column alignment="center" valignment="top" width="0">
16961 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16962 <row topline="true" bottomline="true">
16963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16971 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16979 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16987 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16996 <row topline="true">
16997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17002 PREFIX/sdcc/include/pic16
17005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17010 PIC16 specific headers
17013 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17030 <row topline="true" bottomline="true">
17031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17036 PREFIX/sdcc/lib/pic16
17039 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17044 PIC16 specific libraries
17047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17055 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17072 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17079 PIC16 port currently supports the following pragmas:
17081 \labelwidthstring 00.00.0000
17083 stack pragma stack forces the code generator to initialize the stack & frame
17084 pointers at a specific address.
17085 This is an adhoc solution for cases where no STACK directive is available
17086 in the linker script or gplink is not instructed to create a stack section.
17088 The stack pragma should be used only once in a project.
17089 Multiple pragmas may result in indeterminate behaviour of the program.
17095 The old format (ie.
17096 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17097 cross page boundaries (or even exceed the available data RAM) and crash
17099 Make sure that stack does not cross page boundaries when using the SMALL
17105 The format is as follows:
17108 #pragma stack bottom_address [stack_size]
17115 is the lower bound of the stack section.
17116 The stack pointer initially will point at address (bottom_address+stack_size-1).
17124 /* initializes stack of 100 bytes at RAM address 0x200 */
17127 #pragma stack 0x200 100
17130 If the stack_size field is omitted then a stack is created with the default
17132 This size might be enough for most programs, but its not enough for operations
17133 with deep function nesting or excessive stack usage.
17135 \labelwidthstring 00.00.0000
17139 This pragma is deprecated.
17140 Its use will cause a warning message to be issued.
17146 \labelwidthstring 00.00.0000
17148 code place a function symbol at static FLASH address
17156 /* place function test_func at 0x4000 */
17159 #pragma code test_func 0x4000
17163 \labelwidthstring 00.00.0000
17165 library instructs the linker to use a library module.
17170 #pragma library module_name
17177 can be any library or object file (including its path).
17178 Note that there are four reserved keywords which have special meaning.
17183 \begin_inset Tabular
17184 <lyxtabular version="3" rows="6" columns="3">
17186 <column alignment="center" valignment="top" leftline="true" width="0">
17187 <column alignment="block" valignment="top" leftline="true" width="20page%">
17188 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17189 <row topline="true" bottomline="true">
17190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17215 <row topline="true">
17216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17231 ignore all library pragmas
17234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17245 <row topline="true">
17246 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17264 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17277 <row topline="true">
17278 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17293 link the Math libarary
17296 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17309 <row topline="true">
17310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17325 link the I/O library
17328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17341 <row topline="true" bottomline="true">
17342 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17352 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17357 link the debug library
17360 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17379 * is the device number, i.e.
17380 452 for PIC18F452 MCU.
17383 This feature allows for linking with specific libraries withoug having to
17384 explicit name them in the command line.
17389 keyword will reject all modules specified by the library pragma.
17391 \labelwidthstring 00.00.0000
17393 udata pragma udata instructs the compiler to emit code so that linker will
17394 place a variable at a specific memory bank
17402 /* places variable foo at bank2 */
17405 #pragma udata bank2 foo
17411 In order for this pragma to work extra SECTION directives should be added
17412 in the .lkr script.
17413 In the following example a sample .lkr file is shown:
17418 // Sample linker script for the PIC18F452 processor
17424 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17427 CODEPAGE NAME=page START=0x2A END=0x7FFF
17430 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17433 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17436 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17439 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17442 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17447 DATABANK NAME=gpr0 START=0x80 END=0xFF
17450 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17453 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17456 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17459 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17462 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17465 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17470 SECTION NAME=CONFIG ROM=config
17475 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17478 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17481 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17484 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17487 SECTION NAME=bank4 RAM=gpr4
17490 SECTION NAME=bank5 RAM=gpr5
17493 The linker will recognise the section name set in the pragma statement and
17494 will position the variable at the memory bank set with the RAM field at
17495 the SECTION line in the linker script file.
17499 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17506 There is one main header file that can be included to the source files using
17513 This header file contains the definitions for the processor special registers,
17514 so it is necessary if the source accesses them.
17515 It can be included by adding the following line in the beginning of the
17519 #include <pic18fregs.h>
17522 The specific microcontroller is selected within the pic18fregs.h automatically,
17523 so the same source can be used with a variety of devices.
17529 The libraries that PIC16
17530 \begin_inset LatexCommand \index{PIC16}
17534 port depends on are the microcontroller device libraries which contain
17535 the symbol definitions for the microcontroller special function registers.
17536 These libraries have the format pic18fxxxx.lib, where
17540 is the microcontroller identification number.
17541 The specific library is selected automatically by the compiler at link
17542 stage according to the selected device.
17545 Libraries are created with gplib which is part of the gputils package
17546 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17551 \layout Subsubsection*
17553 Building the libraries
17556 Before using SDCC/pic16 there are some libraries that need to be compiled.
17557 This process is not done automatically by SDCC since not all users use
17558 SDCC for pic16 projects.
17559 So each user should compile the libraries separately.
17562 The steps to compile the pic16 libraries under Linux are:
17565 cd device/lib/pic16
17580 su -c 'make install' # install the libraries, you need the root password
17583 If you need to install the headers too, do:
17589 su -c 'make install' # install the headers, you need the root password
17592 There exist a special target to build the I/O libraries.
17593 This target is not automatically build because it will build the I/O library
17599 This way building will take quite a lot of time.
17600 Users are advised to edit the
17602 device/lib/pic16/pics.build
17604 file and then execute:
17613 The following memory models are supported by the PIC16 port:
17622 Memory model affects the default size of pointers within the source.
17623 The sizes are shown in the next table:
17627 \begin_inset Tabular
17628 <lyxtabular version="3" rows="3" columns="3">
17630 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17631 <column alignment="center" valignment="top" leftline="true" width="0">
17632 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17633 <row topline="true" bottomline="true">
17634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17639 Pointer sizes according to memory model
17642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17659 <row topline="true" bottomline="true">
17660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17668 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17685 <row topline="true" bottomline="true">
17686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17694 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
17702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17718 It is advisable that all sources within a project are compiled with the
17720 If one wants to override the default memory model, this can be done by
17721 declaring a pointer as
17730 Far selects large memory model's pointers, while near selects small memory
17734 The standard device libraries (see
17735 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
17739 ) contain no reference to pointers, so they can be used with both memory
17746 The stack implementation for the PIC16 port uses two indirect registers,
17749 \labelwidthstring 00.00.0000
17751 FSR1 is assigned as stack pointer
17753 \labelwidthstring 00.00.0000
17755 FSR2 is assigned as frame pointer
17758 The following stack models are supported by the PIC16 port
17779 model means that only the FSRxL byte is used to access stack and frame,
17786 uses both FSRxL and FSRxH registers.
17787 The following table shows the stack/frame pointers sizes according to stack
17788 model and the maximum space they can address:
17792 \begin_inset Tabular
17793 <lyxtabular version="3" rows="3" columns="3">
17795 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17796 <column alignment="center" valignment="top" leftline="true" width="0">
17797 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17798 <row topline="true" bottomline="true">
17799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17804 Stack & Frame pointer sizes according to stack model
17807 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17824 <row topline="true">
17825 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17841 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17850 <row topline="true" bottomline="true">
17851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17887 stack model is currently not working properly throughout the code generator.
17888 So its use is not advised.
17889 Also there are some other points that need special care:
17894 Do not create stack sections with size more than one physical bank (that
17898 Stack sections should no cross physical bank limits (i.e.
17899 #pragma stack 0x50 0x100)
17902 These limitations are caused by the fact that only FSRxL is modified when
17903 using SMALL stack model, so no more than 256 bytes of stack can be used.
17904 This problem will disappear after LARGE model is fully implemented.
17910 In addition to the standard SDCC function keywords, PIC16 port makes available
17913 \labelwidthstring 00.00.0000
17915 wparam Use the WREG to pass one byte of the first function argument.
17916 This improves speed but you may not use this for functions with arguments
17917 that are called via function pointers, otherwise the first byte of the
17918 first parameter will get lost.
17922 void func_wparam(int a) wparam
17928 /* WREG hold the lower part of a */
17931 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
17941 This keyword replaces the deprecated wparam pragma.
17943 \labelwidthstring 00.00.0000
17945 shadowregs When entering/exiting an ISR, it is possible to take advantage
17946 of the PIC18F hardware shadow registers which hold the values of WREG,
17947 STATUS and BSR registers.
17948 This can be done by adding the keyword
17956 keyword in the function's header.
17959 void isr_shadow(void) shadowregs interrupt 1
17975 instructs the code generator not to store/restore WREG, STATUS, BSR when
17976 entering/exiting the ISR.
17979 Function return values
17982 Return values from functions are placed to the appropriate registers following
17983 a modified Microchip policy optimized for SDCC.
17984 The following table shows these registers:
17988 \begin_inset Tabular
17989 <lyxtabular version="3" rows="6" columns="2">
17991 <column alignment="center" valignment="top" leftline="true" width="0">
17992 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17993 <row topline="true" bottomline="true">
17994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18007 destination register
18011 <row topline="true">
18012 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18029 <row topline="true">
18030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18038 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18047 <row topline="true">
18048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18056 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18065 <row topline="true">
18066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18079 FSR0L:PRODH:PRODL:WREG
18083 <row topline="true" bottomline="true">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18097 on stack, FSR0 points to the beginning
18111 An interrupt servive routine (ISR) is declared using the
18118 void isr(void) interrupt
18136 is the interrupt number, which for PIC18F devices can be:
18140 \begin_inset Tabular
18141 <lyxtabular version="3" rows="4" columns="3">
18143 <column alignment="center" valignment="top" leftline="true" width="0">
18144 <column alignment="center" valignment="top" leftline="true" width="0">
18145 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18146 <row topline="true" bottomline="true">
18147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18170 Interrupt Vector Address
18174 <row topline="true">
18175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18191 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18200 <row topline="true">
18201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18232 HIGH priority interrupts
18235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18244 <row topline="true" bottomline="true">
18245 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18258 LOW priority interrupts
18261 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18277 When generating assembly code for ISR the code generator places a
18283 Interrupt Vector Address
18285 which points at the genetated ISR.
18286 This single GOTO instruction is part of an automatically generated
18288 interrupt entry point
18291 The actuall ISR code is placed as normally would in the code space.
18292 Upon interrupt request, the GOTO instruction is executed which jumps to
18294 When declaring interrupt functions as _naked this GOTO instruction is
18299 The whole interrupt functions is therefore placed at the Interrupt Vector
18300 Address of the specific interrupt.
18301 This is not a problem for the LOW priority interrupts, but it is a problem
18302 for the RESET and the HIGH priority interrupts because code may be written
18303 at the next interrupt´s vector address and cause undeterminate program
18304 behaviour if that interrupt is raised.
18310 This is not a problem when
18313 this is a HIGH interrupt ISR and LOW interrupts are
18320 when the ISR is small enough not to reach the next interrupt´s vector address.
18330 is possible to be omitted.
18331 This way a function is generated similar to an ISR, but it is not assigned
18335 When entering an interrupt, currently the PIC16
18336 \begin_inset LatexCommand \index{PIC16}
18340 port automatically saves the following registers:
18352 PROD (PRODL and PRODH)
18355 FSR0 (FSR0L and FSR0H)
18358 These registers are restored upon return from the interrupt routine.
18364 NOTE that when the _naked attribute is specified for an interrupt routine,
18365 then NO registers are stored or restored.
18374 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18375 There are 3 types of generic pointers currently implemented data, code
18376 and eeprom pointers.
18377 They are differentiated by the value of the 7th and 6th bits of the upper
18382 \begin_inset Tabular
18383 <lyxtabular version="3" rows="5" columns="5">
18385 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18386 <column alignment="center" valignment="top" width="0">
18387 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18388 <column alignment="center" valignment="top" width="0">
18389 <column alignment="left" valignment="top" rightline="true" width="0">
18390 <row topline="true" bottomline="true">
18391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18415 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18420 rest of the pointer
18423 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18432 <row topline="true" bottomline="true">
18433 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18457 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18466 uuuuuu uuuuxxxx xxxxxxxx
18469 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18474 a 12-bit data pointer in data RAM memory
18478 <row bottomline="true">
18479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18512 uxxxxx xxxxxxxx xxxxxxxx
18515 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18520 a 21-bit code pointer in FLASH memory
18524 <row bottomline="true">
18525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18541 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18558 uuuuuu uuuuuuxx xxxxxxxx
18561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18566 a 10-bit eeprom pointer in EEPROM memory
18570 <row bottomline="true">
18571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18604 xxxxxx xxxxxxxx xxxxxxxx
18607 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18612 unimplemented pointer type
18623 Generic pointer are read and written with a set of library functions which
18624 read/write 1, 2, 3, 4 bytes.
18628 \layout Subsubsection
18630 Standard I/O Streams
18637 the type FILE is defined as:
18640 typedef char * FILE;
18643 This type is the stream type implemented I/O in the PIC18F devices.
18644 Also the standard input and output streams are declared in stdio.h:
18647 extern FILE * stdin;
18650 extern FILE * stdout;
18653 The FILE type is actually a generic pointer which defines one more type
18654 of generic pointers, the
18659 This new type has the format:
18663 \begin_inset Tabular
18664 <lyxtabular version="3" rows="2" columns="7">
18666 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18667 <column alignment="center" valignment="top" width="0">
18668 <column alignment="center" valignment="top" leftline="true" width="0">
18669 <column alignment="center" valignment="top" leftline="true" width="0">
18670 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18671 <column alignment="center" valignment="top" width="0">
18672 <column alignment="left" valignment="top" rightline="true" width="0">
18673 <row topline="true" bottomline="true">
18674 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18682 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18690 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18698 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18706 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18714 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18719 rest of the pointer
18722 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18731 <row topline="true" bottomline="true">
18732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18748 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18764 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18772 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18784 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18789 upper byte high nubble is 0x2n, the rest are zeroes
18800 Currently implemented there are 3 types of streams defined:
18804 \begin_inset Tabular
18805 <lyxtabular version="3" rows="4" columns="4">
18807 <column alignment="center" valignment="top" leftline="true" width="0">
18808 <column alignment="center" valignment="top" leftline="true" width="0">
18809 <column alignment="center" valignment="top" leftline="true" width="0">
18810 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18811 <row topline="true" bottomline="true">
18812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18828 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18845 <row topline="true">
18846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18877 Writes/Reads characters via the USART peripheral
18881 <row topline="true">
18882 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18913 Writes/Reads characters via the MSSP peripheral
18917 <row topline="true" bottomline="true">
18918 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18949 Writes/Reads characters via used defined functions
18960 The stream identifiers are declared as macros in the stdio.h header.
18963 In the libc library there exist the functions that are used to write to
18964 each of the above streams.
18967 \labelwidthstring 00.00.0000
18979 _stream_usart_putchar writes a character at the USART stream
18981 \labelwidthstring 00.00.0000
18993 _stream_mssp_putchar writes a character at the MSSP stream
18995 \labelwidthstring 00.00.0000
18997 putchar dummy function.
18998 This writes a character to a user specified manner.
19001 In order to increase performance
19005 is declared in stdio.h as having its parameter in WREG (it has the wparam
19007 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19008 in a user-friendly way.
19013 is the name of the variable that holds the character to print.
19014 An example follows:
19017 #include <pic18fregs.h>
19029 PORTA = c; /* dump character c to PORTA */
19042 stdout = STREAM_USER; /* this is not necessery, since stdout points
19045 * by default to STREAM_USER */
19048 printf (¨This is a printf test
19056 \layout Subsubsection
19061 PIC16 contains an implementation of the printf-family of functions.
19062 There exist the following functions:
19065 extern unsigned int sprintf(char *buf, char *fmt, ...);
19068 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19073 extern unsigned int printf(char *fmt, ...);
19076 extern unsigned int vprintf(char *fmt, va_lista ap);
19081 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19084 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19087 For sprintf and vsprintf
19091 should normally be a data pointer where the resulting string will be placed.
19092 No range checking is done so the user should allocate the necessery buffer.
19093 For fprintf and vfprintf
19097 should be a stream pointer (i.e.
19098 stdout, STREAM_MSSP, etc...).
19099 \layout Subsubsection
19104 The PIC18F family of microcontrollers supports a number of interrupt sources.
19105 A list of these interrupts is shown in the following table:
19109 \begin_inset Tabular
19110 <lyxtabular version="3" rows="11" columns="4">
19112 <column alignment="left" valignment="top" leftline="true" width="0">
19113 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19114 <column alignment="left" valignment="top" leftline="true" width="0">
19115 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19116 <row topline="true" bottomline="true">
19117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19125 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19141 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19150 <row topline="true">
19151 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19159 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19164 PORTB change interrupt
19167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19175 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19180 EEPROM/FLASH write complete interrupt
19184 <row topline="true">
19185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19193 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19198 INT0 external interrupt
19201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19214 Bus collision interrupt
19218 <row topline="true">
19219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19232 INT1 external interrupt
19235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19248 Low voltage detect interrupt
19252 <row topline="true">
19253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19261 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19266 INT2 external interrupt
19269 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19277 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19282 Parallel slave port interrupt
19286 <row topline="true">
19287 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19295 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19300 CCP1 module interrupt
19303 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19311 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19316 AD convertion complete interrupt
19320 <row topline="true">
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 CCP2 module interrupt
19337 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19345 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19350 USART receive interrupt
19354 <row topline="true">
19355 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19363 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19368 TMR0 overflow interrupt
19371 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19379 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19384 USART transmit interrupt
19388 <row topline="true">
19389 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19397 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19402 TMR1 overflow interrupt
19405 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19413 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19418 SSP receive/transmit interrupt
19422 <row topline="true">
19423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19436 TMR2 matches PR2 interrupt
19439 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19454 <row topline="true" bottomline="true">
19455 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19468 TMR3 overflow interrupt
19471 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19478 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19493 The prototypes for these names are defined in the header file
19500 In order to simplify signal handling, a number of macros is provided:
19502 \labelwidthstring 00.00.0000
19504 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19505 high priority interrupts.
19510 is the function name to use.
19512 \labelwidthstring 00.00.0000
19514 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19515 low priority interrupt.
19520 is the function name to use.
19522 \labelwidthstring 00.00.0000
19524 DEF_HANDLER(sig,handler) define a handler for signal
19528 \labelwidthstring 00.00.0000
19530 END_DEF end the declaration of the dispatch table.
19533 Additionally there are two more macros to simplify the declaration of the
19536 \labelwidthstring 00.00.0000
19540 SIGHANDLER(handler)
19542 this declares the function prototype for the
19548 \labelwidthstring 00.00.0000
19550 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19553 An example of using the macros above is shown below:
19556 #include <pic18fregs.h>
19559 #include <signal.h>
19563 DEF_INTHIGH(high_int)
19566 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
19569 DEF_HANDLER(SIG_BCOL, _bcol_handler)
19576 SIGHANDLER(_tmr0_handler)
19582 /* action to be taken when timer 0 overflows */
19589 SIGHANDLERNAKED(_bcol_handler)
19598 /* action to be taken when bus collision occurs */
19614 Special care should be taken when using the above scheme:
19617 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
19620 when declaring SIGHANDLERNAKED handler never forget to use
19624 for proper returning.
19630 Here you can find some general tips for compiling programs with SDCC/pic16.
19631 \layout Subsubsection
19636 The default stack size (that is 64 bytes) probably is enough for many programs.
19637 One must take care that when there are many levels of function nesting,
19638 or there is excessive usage of stack, its size should be extended.
19639 An example of such a case is the printf/sprintf family of functions.
19640 If you encounter problems like not being able to print integers, then you
19641 need to set the stack size around the maximum (256 for small stack model).
19642 The following diagram shows what happens when calling printf to print an
19646 printf () --> ltoa () --> ultoa () --> divschar ()
19649 It is should be understood that stack is easily consumed when calling complicate
19651 Using command line arguments like -
19661 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
19663 Other ways to reduce stack usage may exist.
19666 Debugging with SDCDB
19667 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
19672 \begin_inset LatexCommand \index{sdcdb (debugger)}
19679 SDCC is distributed with a source level debugger
19680 \begin_inset LatexCommand \index{Debugger}
19685 The debugger uses a command line interface, the command repertoire of the
19686 debugger has been kept as close to gdb
19687 \begin_inset LatexCommand \index{gdb}
19691 (the GNU debugger) as possible.
19692 The configuration and build process is part of the standard compiler installati
19693 on, which also builds and installs the debugger in the target directory
19694 specified during configuration.
19695 The debugger allows you debug BOTH at the C source and at the ASM source
19697 Sdcdb is available on Unix platforms only.
19700 Compiling for Debugging
19714 \begin_inset LatexCommand \index{-\/-debug}
19718 option must be specified for all files for which debug information is to
19720 The complier generates a .adb file for each of these files.
19721 The linker creates the .cdb
19722 \begin_inset LatexCommand \index{<file>.cdb}
19727 \begin_inset LatexCommand \index{<file>.adb}
19731 files and the address information.
19732 This .cdb is used by the debugger.
19735 How the Debugger Works
19748 -debug option is specified the compiler generates extra symbol information
19749 some of which are put into the assembler source and some are put into the
19751 Then the linker creates the .cdb file from the individual .adb files with
19752 the address information for the symbols.
19753 The debugger reads the symbolic information generated by the compiler &
19754 the address information generated by the linker.
19755 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
19756 execution is controlled by the debugger.
19757 When a command is issued for the debugger, it translates it into appropriate
19758 commands for the simulator.
19761 Starting the Debugger
19764 The debugger can be started using the following command line.
19765 (Assume the file you are debugging has the file name foo).
19779 The debugger will look for the following files.
19782 foo.c - the source file.
19785 foo.cdb - the debugger symbol information file.
19788 foo.ihx - the Intel hex format
19789 \begin_inset LatexCommand \index{Intel hex format}
19796 Command Line Options.
19809 -directory=<source file directory> this option can used to specify the directory
19811 The debugger will look into the directory list specified for source, cdb
19813 The items in the directory list must be separated by ':', e.g.
19814 if the source files can be in the directories /home/src1 and /home/src2,
19825 -directory option should be -
19835 -directory=/home/src1:/home/src2.
19836 Note there can be no spaces in the option.
19840 -cd <directory> - change to the <directory>.
19843 -fullname - used by GUI front ends.
19846 -cpu <cpu-type> - this argument is passed to the simulator please see the
19847 simulator docs for details.
19850 -X <Clock frequency > this options is passed to the simulator please see
19851 the simulator docs for details.
19854 -s <serial port file> passed to simulator see the simulator docs for details.
19857 -S <serial in,out> passed to simulator see the simulator docs for details.
19860 -k <port number> passed to simulator see the simulator docs for details.
19866 As mentioned earlier the command interface for the debugger has been deliberatel
19867 y kept as close the GNU debugger gdb, as possible.
19868 This will help the integration with existing graphical user interfaces
19869 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
19870 If you use a graphical user interface for the debugger you can skip the
19872 \layout Subsubsection*
19874 break [line | file:line | function | file:function]
19877 Set breakpoint at specified line or function:
19886 sdcdb>break foo.c:100
19888 sdcdb>break funcfoo
19890 sdcdb>break foo.c:funcfoo
19891 \layout Subsubsection*
19893 clear [line | file:line | function | file:function ]
19896 Clear breakpoint at specified line or function:
19905 sdcdb>clear foo.c:100
19907 sdcdb>clear funcfoo
19909 sdcdb>clear foo.c:funcfoo
19910 \layout Subsubsection*
19915 Continue program being debugged, after breakpoint.
19916 \layout Subsubsection*
19921 Execute till the end of the current function.
19922 \layout Subsubsection*
19927 Delete breakpoint number 'n'.
19928 If used without any option clear ALL user defined break points.
19929 \layout Subsubsection*
19931 info [break | stack | frame | registers ]
19934 info break - list all breakpoints
19937 info stack - show the function call stack.
19940 info frame - show information about the current execution frame.
19943 info registers - show content of all registers.
19944 \layout Subsubsection*
19949 Step program until it reaches a different source line.
19950 Note: pressing <return> repeats the last command.
19951 \layout Subsubsection*
19956 Step program, proceeding through subroutine calls.
19957 \layout Subsubsection*
19962 Start debugged program.
19963 \layout Subsubsection*
19968 Print type information of the variable.
19969 \layout Subsubsection*
19974 print value of variable.
19975 \layout Subsubsection*
19980 load the given file name.
19981 Note this is an alternate method of loading file for debugging.
19982 \layout Subsubsection*
19987 print information about current frame.
19988 \layout Subsubsection*
19993 Toggle between C source & assembly source.
19994 \layout Subsubsection*
19996 ! simulator command
19999 Send the string following '!' to the simulator, the simulator response is
20001 Note the debugger does not interpret the command being sent to the simulator,
20002 so if a command like 'go' is sent the debugger can loose its execution
20003 context and may display incorrect values.
20004 \layout Subsubsection*
20011 My name is Bobby Brown"
20014 Interfacing with XEmacs
20015 \begin_inset LatexCommand \index{XEmacs}
20020 \begin_inset LatexCommand \index{Emacs}
20027 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20028 sdcdb.el and sdcdbsrc.el.
20029 These two files can be found in the $(prefix)/bin directory after the installat
20031 These files need to be loaded into XEmacs for the interface to work.
20032 This can be done at XEmacs startup time by inserting the following into
20033 your '.xemacs' file (which can be found in your HOME directory):
20039 (load-file sdcdbsrc.el)
20045 .xemacs is a lisp file so the () around the command is REQUIRED.
20046 The files can also be loaded dynamically while XEmacs is running, set the
20047 environment variable 'EMACSLOADPATH' to the installation bin directory
20048 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20049 To start the interface enter the following command:
20063 You will prompted to enter the file name to be debugged.
20068 The command line options that are passed to the simulator directly are bound
20069 to default values in the file sdcdbsrc.el.
20070 The variables are listed below, these values maybe changed as required.
20073 sdcdbsrc-cpu-type '51
20076 sdcdbsrc-frequency '11059200
20079 sdcdbsrc-serial nil
20082 The following is a list of key mapping for the debugger interface.
20093 ;;key\SpecialChar ~
20107 binding\SpecialChar ~
20131 ;;---\SpecialChar ~
20145 -------\SpecialChar ~
20187 sdcdb-next-from-src\SpecialChar ~
20215 sdcdb-back-from-src\SpecialChar ~
20243 sdcdb-cont-from-src\SpecialChar ~
20253 SDCDB continue command
20271 sdcdb-step-from-src\SpecialChar ~
20299 sdcdb-whatis-c-sexp\SpecialChar ~
20309 SDCDB ptypecommand for data at
20376 sdcdbsrc-delete\SpecialChar ~
20390 SDCDB Delete all breakpoints if no arg
20439 given or delete arg (C-u arg x)
20457 sdcdbsrc-frame\SpecialChar ~
20472 SDCDB Display current frame if no arg,
20521 given or display frame arg
20588 sdcdbsrc-goto-sdcdb\SpecialChar ~
20598 Goto the SDCDB output buffer
20616 sdcdb-print-c-sexp\SpecialChar ~
20627 SDCDB print command for data at
20694 sdcdbsrc-goto-sdcdb\SpecialChar ~
20704 Goto the SDCDB output buffer
20722 sdcdbsrc-mode\SpecialChar ~
20738 Toggles Sdcdbsrc mode (turns it off)
20753 sdcdb-finish-from-src\SpecialChar ~
20761 SDCDB finish command
20776 sdcdb-break\SpecialChar ~
20794 Set break for line with point
20809 sdcdbsrc-mode\SpecialChar ~
20825 Toggle Sdcdbsrc mode
20840 sdcdbsrc-srcmode\SpecialChar ~
20863 Here are a few guidelines that will help the compiler generate more efficient
20864 code, some of the tips are specific to this compiler others are generally
20865 good programming practice.
20868 Use the smallest data type to represent your data-value.
20869 If it is known in advance that the value is going to be less than 256 then
20870 use an 'unsigned char' instead of a 'short' or 'int'.
20871 Please note, that ANSI C requires both signed and unsigned chars to be
20872 promoted to 'signed int' before doing any operation.
20874 \begin_inset LatexCommand \index{type promotion}
20878 can be omitted, if the result is the same.
20879 The effect of the promotion rules together with the sign-extension is often
20886 unsigned char uc = 0xfe;
20888 if (uc * uc < 0) /* this is true! */
20907 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
20917 (unsigned char) -12 / (signed char) -3 = ...
20920 No, the result is not 4:
20925 (int) (unsigned char) -12 / (int) (signed char) -3 =
20927 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
20929 (int) 0x00f4 / (int) 0xfffd =
20931 (int) 0x00f4 / (int) 0xfffd =
20933 (int) 244 / (int) -3 =
20935 (int) -81 = (int) 0xffaf;
20938 Don't complain, that gcc gives you a different result.
20939 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
20940 Therefore the results are different.
20943 \begin_inset Quotes sld
20947 \begin_inset Quotes srd
20953 If well-defined overflow characteristics are important and negative values
20954 are not, or if you want to steer clear of sign-extension problems when
20955 manipulating bits or bytes, use one of the corresponding unsigned types.
20956 (Beware when mixing signed and unsigned values in expressions, though.)
20958 Although character types (especially unsigned char) can be used as "tiny"
20959 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
20960 ble sign extension and increased code size.
20964 Use unsigned when it is known in advance that the value is not going to
20966 This helps especially if you are doing division or multiplication, bit-shifting
20967 or are using an array index.
20970 NEVER jump into a LOOP.
20973 Declare the variables to be local
20974 \begin_inset LatexCommand \index{local variables}
20978 whenever possible, especially loop control variables (induction).
20981 Since the compiler does not always do implicit integral promotion, the programme
20982 r should do an explicit cast when integral promotion is required.
20985 Reducing the size of division, multiplication & modulus operations can reduce
20986 code size substantially.
20987 Take the following code for example.
20993 foobar(unsigned int p1, unsigned char ch)
21001 unsigned char ch1 = p1 % ch ;
21012 For the modulus operation the variable ch will be promoted to unsigned int
21013 first then the modulus operation will be performed (this will lead to a
21014 call to support routine _moduint()), and the result will be casted to a
21016 If the code is changed to
21021 foobar(unsigned int p1, unsigned char ch)
21029 unsigned char ch1 = (unsigned char)p1 % ch ;
21040 It would substantially reduce the code generated (future versions of the
21041 compiler will be smart enough to detect such optimization opportunities).
21045 Have a look at the assembly listing to get a
21046 \begin_inset Quotes sld
21050 \begin_inset Quotes srd
21053 for the code generation.
21057 \begin_inset LatexCommand \index{Tools}
21061 included in the distribution
21065 \begin_inset Tabular
21066 <lyxtabular version="3" rows="12" columns="3">
21068 <column alignment="center" valignment="top" leftline="true" width="0pt">
21069 <column alignment="center" valignment="top" leftline="true" width="0pt">
21070 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21071 <row topline="true" bottomline="true">
21072 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21088 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21097 <row topline="true">
21098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21106 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21111 Simulator for various architectures
21114 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21123 <row topline="true">
21124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21137 header file conversion
21140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21145 sdcc/support/scripts
21149 <row topline="true">
21150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21163 header file conversion
21166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21171 sdcc/support/scripts
21175 <row topline="true">
21176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21210 <row topline="true">
21211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21245 <row topline="true">
21246 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21262 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21280 <row topline="true">
21281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21315 <row topline="true">
21316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21350 <row topline="true">
21351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21367 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21385 <row topline="true">
21386 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21402 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21420 <row topline="true" bottomline="true">
21421 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21429 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21437 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21465 \begin_inset LatexCommand \index{Documentation}
21469 included in the distribution
21473 \begin_inset Tabular
21474 <lyxtabular version="3" rows="10" columns="2">
21476 <column alignment="left" valignment="top" leftline="true" width="0">
21477 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21478 <row topline="true" bottomline="true">
21479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21492 Where to get / filename
21496 <row topline="true">
21497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21502 SDCC Compiler User Guide
21505 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21510 You're reading it right now
21514 <row topline="true">
21515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21532 <row topline="true">
21533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21539 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
21544 \begin_inset LatexCommand \index{Assembler documentation}
21548 Assemblers and ASLINK
21549 \begin_inset LatexCommand \index{aslink}
21554 \begin_inset LatexCommand \index{Linker documentation}
21561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21566 sdcc/as/doc/asxhtm.html
21570 <row topline="true">
21571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21576 SDCC regression test
21577 \begin_inset LatexCommand \index{Regression test}
21584 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21589 sdcc/doc/test_suite_spec.pdf
21593 <row topline="true">
21594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21602 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21611 <row topline="true">
21612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21617 Notes on debugging with sdcdb
21618 \begin_inset LatexCommand \index{sdcdb (debugger)}
21625 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21630 sdcc/debugger/README
21634 <row topline="true">
21635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21640 Software simulator for microcontrollers
21643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21670 <row topline="true">
21671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21676 Temporary notes on the pic16
21677 \begin_inset LatexCommand \index{PIC16}
21684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21689 sdcc/src/pic16/NOTES
21693 <row topline="true" bottomline="true">
21694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21699 SDCC internal documentation (debugging file format)
21702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21738 Related open source tools
21739 \begin_inset LatexCommand \index{Related tools}
21747 \begin_inset Tabular
21748 <lyxtabular version="3" rows="11" columns="3">
21750 <column alignment="center" valignment="top" leftline="true" width="0pt">
21751 <column alignment="block" valignment="top" leftline="true" width="30line%">
21752 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21753 <row topline="true" bottomline="true">
21754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21779 <row topline="true">
21780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21786 \begin_inset LatexCommand \index{gpsim (pic simulator)}
21793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21807 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
21815 <row topline="true">
21816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21822 \begin_inset LatexCommand \index{gputils (pic tools)}
21829 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21837 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21843 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
21851 <row topline="true">
21852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21874 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
21882 <row topline="true">
21883 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21889 \begin_inset LatexCommand \index{indent (source formatting tool)}
21896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21901 Formats C source - Master of the white spaces
21904 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21910 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
21918 <row topline="true">
21919 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21925 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
21932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21937 Object file conversion, checksumming, ...
21940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21946 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
21954 <row topline="true">
21955 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21961 \begin_inset LatexCommand \index{objdump (tool)}
21968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21973 Object file conversion, ...
21976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21981 Part of binutils (should be there anyway)
21985 <row topline="true">
21986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21992 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
21999 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22004 Source code documentation system
22007 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22013 \begin_inset LatexCommand \url{http://www.doxygen.org}
22021 <row topline="true">
22022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22035 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22038 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22044 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22052 <row topline="true">
22053 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22059 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22071 Statically checks c sources (see
22072 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22079 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22085 \begin_inset LatexCommand \url{http://www.splint.org}
22093 <row topline="true" bottomline="true">
22094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22100 \begin_inset LatexCommand \index{ddd (debugger)}
22107 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22112 Debugger, serves nicely as GUI to sdcdb
22113 \begin_inset LatexCommand \index{sdcdb (debugger)}
22120 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22126 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22143 Related documentation / recommended reading
22147 \begin_inset Tabular
22148 <lyxtabular version="3" rows="6" columns="3">
22150 <column alignment="center" valignment="top" leftline="true" width="0pt">
22151 <column alignment="block" valignment="top" leftline="true" width="30line%">
22152 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22153 <row topline="true" bottomline="true">
22154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22162 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22170 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22179 <row topline="true">
22180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22203 \begin_inset LatexCommand \index{C Reference card}
22210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22216 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22224 <row topline="true">
22225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22247 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22255 <row topline="true">
22256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22268 Latest datasheet of the target CPU
22271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22280 <row topline="true">
22281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22293 Revision history of datasheet
22296 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22305 <row topline="true" bottomline="true">
22306 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22321 Advanced Compiler Design and Implementation
22324 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22329 bookstore (very dedicated, probably read other books first)
22345 Some questions answered, some pointers given - it might be time to in turn
22353 can you solve your project with the selected microcontroller? Would you
22354 find out early or rather late that your target is too small/slow/whatever?
22355 Can you switch to a slightly better device if it doesn't fit?
22358 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22359 and/or another programming language be more adequate? Would an operating
22360 system on the target device help?
22363 if you solved the problem, will the marketing department be happy?
22366 if the marketing department is happy, will customers be happy?
22369 if you're the project manager, marketing department and maybe even the customer
22370 in one person, have you tried to see the project from the outside?
22373 is the project done if you think it is done? Or is just that other interface/pro
22374 tocol/feature/configuration/option missing? How about website, manual(s),
22375 internationali(z|s)ation, packaging, labels, 2nd source for components,
22376 electromagnetic compatability/interference, documentation for production,
22377 production test software, update mechanism, patent issues?
22380 is your project adequately positioned in that magic triangle: fame, fortune,
22384 Maybe not all answers to these questions are known and some answers may
22389 , nevertheless knowing these questions may help you to avoid burnout
22395 burnout is bad for electronic devices, programmers and motorcycle tyres
22399 Chances are you didn't want to hear some of them...
22403 \begin_inset LatexCommand \index{Support}
22410 SDCC has grown to be a large project.
22411 The compiler alone (without the preprocessor, assembler and linker) is
22412 well over 100,000 lines of code (blank stripped).
22413 The open source nature of this project is a key to its continued growth
22415 You gain the benefit and support of many active software developers and
22417 Is SDCC perfect? No, that's why we need your help.
22418 The developers take pride in fixing reported bugs.
22419 You can help by reporting the bugs and helping other SDCC users.
22420 There are lots of ways to contribute, and we encourage you to take part
22421 in making SDCC a great software package.
22425 The SDCC project is hosted on the SDCC sourceforge site at
22426 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22431 You'll find the complete set of mailing lists
22432 \begin_inset LatexCommand \index{Mailing list(s)}
22436 , forums, bug reporting system, patch submission
22437 \begin_inset LatexCommand \index{Patch submission}
22442 \begin_inset LatexCommand \index{download}
22446 area and cvs code repository
22447 \begin_inset LatexCommand \index{cvs code repository}
22455 \begin_inset LatexCommand \index{Bug reporting}
22460 \begin_inset LatexCommand \index{Reporting bugs}
22467 The recommended way of reporting bugs is using the infrastructure of the
22469 You can follow the status of bug reports there and have an overview about
22473 Bug reports are automatically forwarded to the developer mailing list and
22474 will be fixed ASAP.
22475 When reporting a bug, it is very useful to include a small test program
22476 (the smaller the better) which reproduces the problem.
22477 If you can isolate the problem by looking at the generated assembly code,
22478 this can be very helpful.
22479 Compiling your program with the -
22490 \begin_inset LatexCommand \index{-\/-dumpall}
22494 option can sometimes be useful in locating optimization problems.
22495 When reporting a bug please maker sure you:
22498 Attach the code you are compiling with SDCC.
22502 Specify the exact command you use to run SDCC, or attach your Makefile.
22506 Specify the SDCC version (type "
22512 "), your platform, and operating system.
22516 Provide an exact copy of any error message or incorrect output.
22520 Put something meaningful in the subject of your message.
22523 Please attempt to include these 5 important parts, as applicable, in all
22524 requests for support or when reporting any problems or bugs with SDCC.
22525 Though this will make your message lengthy, it will greatly improve your
22526 chance that SDCC users and developers will be able to help you.
22527 Some SDCC developers are frustrated by bug reports without code provided
22528 that they can use to reproduce and ultimately fix the problem, so please
22529 be sure to provide sample code if you are reporting a bug!
22532 Please have a short check that you are using a recent version of SDCC and
22533 the bug is not yet known.
22534 This is the link for reporting bugs:
22535 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
22542 Requesting Features
22543 \begin_inset LatexCommand \label{sub:Requesting-Features}
22548 \begin_inset LatexCommand \index{Feature request}
22553 \begin_inset LatexCommand \index{Requesting features}
22560 Like bug reports feature requests are forwarded to the developer mailing
22562 This is the link for requesting features:
22563 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
22573 Like bug reports contributed patches are forwarded to the developer mailing
22575 This is the link for submitting patches
22576 \begin_inset LatexCommand \index{Patch submission}
22581 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
22588 You need to specify some parameters to the
22592 command for the patches to be useful.
22593 If you modified more than one file a patch created f.e.
22598 \begin_inset Quotes sld
22601 diff -Naur unmodified_directory modified_directory >my_changes.patch
22602 \begin_inset Quotes srd
22608 will be fine, otherwise
22612 \begin_inset Quotes sld
22615 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
22616 \begin_inset Quotes srd
22629 These links should take you directly to the
22630 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
22640 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
22641 automated messages (mid 2003)
22645 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
22650 \begin_inset LatexCommand \index{Mailing list(s)}
22654 and forums are archived and searchable so if you are lucky someone already
22655 had a similar problem.
22656 While mails to the lists themselves are delivered promptly their web front
22657 end on sourceforge sometimes shows a severe time lag (up to several weeks),
22658 if you're seriously using SDCC please consider subscribing to the lists.
22664 You can follow the status of the cvs version
22665 \begin_inset LatexCommand \index{version}
22669 of SDCC by watching the Changelog
22670 \begin_inset LatexCommand \index{Changelog}
22674 in the cvs-repository
22679 \begin_inset LatexCommand \htmlurl{http://cvs.sf.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
22687 \begin_inset LatexCommand \index{Release policy}
22694 Historically there often were long delays between official releases and
22695 the sourceforge download area tends to get not updated at all.
22696 Excuses in the past might have referred to problems with live range analysis,
22697 but as this was fixed a while ago, the current problem is that another
22698 excuse has to be found.
22699 Kidding aside, we have to get better there! On the other hand there are
22700 daily snapshots available at
22701 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
22705 , and you can always build the very last version (hopefully with many bugs
22706 fixed, and features added) from the source code available at
22707 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
22715 \begin_inset LatexCommand \index{Examples}
22722 You'll find some small examples in the directory
22724 sdcc/device/examples/.
22727 More examples and libraries are available at
22729 The SDCC Open Knowledge Resource
22730 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
22737 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
22744 I did insert a reference to Paul's web site here although it seems rather
22745 dedicated to a specific 8032 board (I think it's okay because it f.e.
22746 shows LCD/Harddisc interface and has a free 8051 monitor.
22747 Independent 8032 board vendors face hard competition of heavily subsidized
22748 development boards anyway).
22751 Maybe we should include some links to real world applications.
22752 Preferably pointer to pointers (one for each architecture) so this stays
22757 \begin_inset LatexCommand \index{Quality control}
22764 The compiler is passed through nightly compile and build checks.
22770 \begin_inset LatexCommand \index{Regression test}
22774 check that SDCC itself compiles flawlessly on several platforms and checks
22775 the quality of the code generated by SDCC by running the code through simulator
22777 There is a separate document
22780 \begin_inset LatexCommand \index{Test suite}
22789 You'll find the test code in the directory
22791 sdcc/support/regression
22794 You can run these tests manually by running
22798 in this directory (or f.e.
22803 \begin_inset Quotes sld
22807 \begin_inset Quotes srd
22813 if you don't want to run the complete tests).
22814 The test code might also be interesting if you want to look for examples
22815 \begin_inset LatexCommand \index{Examples}
22819 checking corner cases of SDCC or if you plan to submit patches
22820 \begin_inset LatexCommand \index{Patch submission}
22827 The pic port uses a different set of regression tests, you'll find them
22830 sdcc/src/regression
22835 SDCC Technical Data
22839 \begin_inset LatexCommand \index{Optimizations}
22846 SDCC performs a host of standard optimizations in addition to some MCU specific
22851 Sub-expression Elimination
22852 \begin_inset LatexCommand \index{Subexpression elimination}
22859 The compiler does local and
22885 will be translated to
22897 Some subexpressions are not as obvious as the above example, e.g.:
22907 In this case the address arithmetic a->b[i] will be computed only once;
22908 the equivalent code in C would be.
22920 The compiler will try to keep these temporary variables in registers.
22923 Dead-Code Elimination
22924 \begin_inset LatexCommand \index{Dead-code elimination}
22945 i = 1; \SpecialChar ~
22954 global = 1;\SpecialChar ~
22967 global = 3;\SpecialChar ~
22992 \begin_inset LatexCommand \index{Copy propagation}
23048 Note: the dead stores created by this copy propagation will be eliminated
23049 by dead-code elimination.
23053 \begin_inset LatexCommand \index{Loop optimization}
23058 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23065 Two types of loop optimizations are done by SDCC
23073 of loop induction variables.
23074 In addition to the strength reduction the optimizer marks the induction
23075 variables and the register allocator tries to keep the induction variables
23076 in registers for the duration of the loop.
23077 Because of this preference of the register allocator
23078 \begin_inset LatexCommand \index{Register allocation}
23082 , loop induction optimization causes an increase in register pressure, which
23083 may cause unwanted spilling of other temporary variables into the stack
23084 \begin_inset LatexCommand \index{stack}
23089 The compiler will generate a warning message when it is forced to allocate
23090 extra space either on the stack or data space.
23091 If this extra space allocation is undesirable then induction optimization
23092 can be eliminated either for the entire source file (with -
23102 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23104 \begin_inset LatexCommand \index{\#pragma noinduction}
23117 for (i = 0 ; i < 100 ; i ++)
23133 for (i = 0; i < 100; i++)
23142 As mentioned previously some loop invariants are not as apparent, all static
23143 address computations are also moved out of the loop.
23148 \begin_inset LatexCommand \index{Strength reduction}
23152 , this optimization substitutes an expression by a cheaper expression:
23157 for (i=0;i < 100; i++)
23175 for (i=0;i< 100;i++) {
23181 ar[itemp1] = itemp2;
23198 The more expensive multiplication
23199 \begin_inset LatexCommand \index{Multiplication}
23203 is changed to a less expensive addition.
23207 \begin_inset LatexCommand \index{Loop reversing}
23214 This optimization is done to reduce the overhead of checking loop boundaries
23215 for every iteration.
23216 Some simple loops can be reversed and implemented using a
23217 \begin_inset Quotes eld
23220 decrement and jump if not zero
23221 \begin_inset Quotes erd
23225 SDCC checks for the following criterion to determine if a loop is reversible
23226 (note: more sophisticated compilers use data-dependency analysis to make
23227 this determination, SDCC uses a more simple minded analysis).
23230 The 'for' loop is of the form
23236 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23246 The <for body> does not contain
23247 \begin_inset Quotes eld
23251 \begin_inset Quotes erd
23255 \begin_inset Quotes erd
23261 All goto's are contained within the loop.
23264 No function calls within the loop.
23267 The loop control variable <sym> is not assigned any value within the loop
23270 The loop control variable does NOT participate in any arithmetic operation
23274 There are NO switch statements in the loop.
23277 Algebraic Simplifications
23280 SDCC does numerous algebraic simplifications, the following is a small sub-set
23281 of these optimizations.
23286 i = j + 0;\SpecialChar ~
23290 /* changed to: */\SpecialChar ~
23296 i /= 2;\SpecialChar ~
23303 /* changed to: */\SpecialChar ~
23309 i = j - j;\SpecialChar ~
23313 /* changed to: */\SpecialChar ~
23319 i = j / 1;\SpecialChar ~
23323 /* changed to: */\SpecialChar ~
23330 Note the subexpressions
23331 \begin_inset LatexCommand \index{Subexpression}
23335 given above are generally introduced by macro expansions or as a result
23336 of copy/constant propagation.
23339 'switch' Statements
23340 \begin_inset LatexCommand \label{sub:'switch'-Statements}
23345 \begin_inset LatexCommand \index{switch statement}
23352 SDCC can optimize switch statements to jump tables
23353 \begin_inset LatexCommand \index{jump tables}
23358 It makes the decision based on an estimate of the generated code size.
23359 SDCC is quite liberal in the requirements for jump table generation:
23362 The labels need not be in order, and the starting number need not be one
23363 or zero, the case labels are in numerical sequence or not too many case
23364 labels are missing.
23370 switch(i) {\SpecialChar ~
23401 case 4: ...\SpecialChar ~
23433 case 5: ...\SpecialChar ~
23465 case 3: ...\SpecialChar ~
23496 case 6: ...\SpecialChar ~
23528 case 7: ...\SpecialChar ~
23560 case 8: ...\SpecialChar ~
23592 case 9: ...\SpecialChar ~
23624 case 10: ...\SpecialChar ~
23655 case 11: ...\SpecialChar ~
23722 Both the above switch statements will be implemented using a jump-table.
23723 The example to the right side is slightly more efficient as the check for
23724 the lower boundary of the jump-table is not needed.
23728 The number of case labels is not larger than supported by the target architectur
23732 If the case labels are not in numerical sequence ('gaps' between cases)
23733 SDCC checks whether a jump table with additionally inserted dummy cases
23734 is still attractive.
23738 If the starting number is not zero and a check for the lower boundary of
23739 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
23744 Switch statements which have large gaps in the numeric sequence or those
23745 that have too many case labels can be split into more than one switch statement
23746 for efficient code generation, e.g.:
23826 If the above switch statement is broken down into two switch statements
23916 then both the switch statements will be implemented using jump-tables whereas
23917 the unmodified switch statement will not be.
23920 There might be reasons which SDCC cannot know about to either favour or
23921 not favour jump tables.
23922 If the target system has to be as quick for the last switch case as for
23923 the first (pro jump table), or if the switch argument is known to be zero
23924 in the majority of the cases (contra jump table).
23927 The pragma nojtbound
23928 \begin_inset LatexCommand \index{\#pragma nojtbound}
23932 can be used to turn off checking the
23945 It has no effect if a default label is supplied.
23946 Use of this pragma is dangerous: if the switch
23947 \begin_inset LatexCommand \index{switch statement}
23951 argument is not matched by a case statement the processor will happily
23955 Bit-shifting Operations
23956 \begin_inset LatexCommand \index{Bit shifting}
23963 Bit shifting is one of the most frequently used operation in embedded programmin
23965 SDCC tries to implement bit-shift operations in the most efficient way
23981 generates the following code:
23998 In general SDCC will never setup a loop if the shift count is known.
24041 \begin_inset LatexCommand \index{Bit rotation}
24048 A special case of the bit-shift operation is bit rotation
24049 \begin_inset LatexCommand \index{rotating bits}
24053 , SDCC recognizes the following expression to be a left bit-rotation:
24063 char i;\SpecialChar ~
24074 /* unsigned is needed for rotation */
24079 i = ((i << 1) | (i >> 7));
24088 will generate the following code:
24107 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24108 ns of this case will also be recognized as bit-rotation, i.e.:
24113 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24116 Nibble and Byte Swapping
24119 Other special cases of the bit-shift operations are nibble or byte swapping
24120 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24124 , SDCC recognizes the following expressions:
24147 i = ((i << 4) | (i >> 4));
24153 j = ((j << 8) | (j >> 8));
24156 and generates a swap instruction for the nibble swapping
24157 \begin_inset LatexCommand \index{Nibble swapping}
24161 or move instructions for the byte swapping
24162 \begin_inset LatexCommand \index{Byte swapping}
24168 \begin_inset Quotes sld
24172 \begin_inset Quotes srd
24175 example can be used to convert from little to big-endian or vice versa.
24176 If you want to change the endianness of a
24180 integer you have to cast to
24187 Note that SDCC stores numbers in little-endian
24193 Usually 8-bit processors don't care much about endianness.
24194 This is not the case for the standard 8051 which only has an instruction
24200 \begin_inset LatexCommand \index{DPTR}
24208 so little-endian is the more efficient byte order.
24212 \begin_inset LatexCommand \index{little-endian}
24217 \begin_inset LatexCommand \index{Endianness}
24222 lowest order first).
24226 \begin_inset LatexCommand \index{Highest Order Bit}
24233 It is frequently required to obtain the highest order bit of an integral
24234 type (long, int, short or char types).
24235 SDCC recognizes the following expression to yield the highest order bit
24236 and generates optimized code for it, e.g.:
24258 hob = (gint >> 15) & 1;
24268 will generate the following code:
24301 000A E5*01\SpecialChar ~
24328 000C 23\SpecialChar ~
24359 000D 54 01\SpecialChar ~
24386 000F F5*02\SpecialChar ~
24414 Variations of this case however will
24419 It is a standard C expression, so I heartily recommend this be the only
24420 way to get the highest order bit, (it is portable).
24421 Of course it will be recognized even if it is embedded in other expressions,
24427 xyz = gint + ((gint >> 15) & 1);
24430 will still be recognized.
24434 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
24439 \begin_inset LatexCommand \index{Peephole optimizer}
24446 The compiler uses a rule based, pattern matching and re-writing mechanism
24447 for peep-hole optimization.
24452 a peep-hole optimizer by Christopher W.
24453 Fraser (cwfraser\SpecialChar ~
24456 A default set of rules are compiled into the compiler, additional rules
24457 may be added with the
24470 \begin_inset LatexCommand \index{-\/-peep-file}
24477 The rule language is best illustrated with examples.
24501 The above rule will change the following assembly
24502 \begin_inset LatexCommand \index{Assembler routines}
24524 Note: All occurrences of a
24528 (pattern variable) must denote the same string.
24529 With the above rule, the assembly sequence:
24539 will remain unmodified.
24543 Other special case optimizations may be added by the user (via
24559 some variants of the 8051 MCU
24560 \begin_inset LatexCommand \index{MCS51 variants}
24573 The following two rules will change all
24592 replace { lcall %1 } by { acall %1 }
24594 replace { ljmp %1 } by { ajmp %1 }
24599 inline-assembler code
24601 is also passed through the peep hole optimizer, thus the peephole optimizer
24602 can also be used as an assembly level macro expander.
24603 The rules themselves are MCU dependent whereas the rule language infra-structur
24604 e is MCU independent.
24605 Peephole optimization rules for other MCU can be easily programmed using
24610 The syntax for a rule is as follows:
24615 rule := replace [ restart ] '{' <assembly sequence> '
24653 <assembly sequence> '
24671 '}' [if <functionName> ] '
24676 <assembly sequence> := assembly instruction (each instruction including
24677 labels must be on a separate line).
24681 The optimizer will apply to the rules one by one from the top in the sequence
24682 of their appearance, it will terminate when all rules are exhausted.
24683 If the 'restart' option is specified, then the optimizer will start matching
24684 the rules again from the top, this option for a rule is expensive (performance)
24685 , it is intended to be used in situations where a transformation will trigger
24686 the same rule again.
24687 An example of this (not a good one, it has side effects) is the following
24710 Note that the replace pattern cannot be a blank, but can be a comment line.
24711 Without the 'restart' option only the innermost 'pop' 'push' pair would
24712 be eliminated, i.e.:
24742 the restart option the rule will be applied again to the resulting code
24743 and then all the pop-push pairs will be eliminated to yield:
24753 A conditional function can be attached to a rule.
24754 Attaching rules are somewhat more involved, let me illustrate this with
24781 The optimizer does a look-up of a function name table defined in function
24786 in the source file SDCCpeeph.c, with the name
24791 If it finds a corresponding entry the function is called.
24792 Note there can be no parameters specified for these functions, in this
24797 is crucial, since the function
24801 expects to find the label in that particular variable (the hash table containin
24802 g the variable bindings is passed as a parameter).
24803 If you want to code more such functions, take a close look at the function
24804 labelInRange and the calling mechanism in source file SDCCpeeph.c.
24805 Currently implemented are
24807 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
24808 24bitMode, portIsDS390, 24bitModeAndPortDS390
24817 I know this whole thing is a little kludgey, but maybe some day we will
24818 have some better means.
24819 If you are looking at this file, you will see the default rules that are
24820 compiled into the compiler, you can add your own rules in the default set
24821 there if you get tired of specifying the -
24835 \begin_inset LatexCommand \index{ANSI-compliance}
24840 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
24847 Deviations from the compliance:
24850 functions are not reentrant
24851 \begin_inset LatexCommand \index{reentrant}
24855 unless explicitly declared as such or the
24868 \begin_inset LatexCommand \index{-\/-stack-auto}
24874 command line option is specified.
24877 structures and unions cannot be assigned values directly, cannot be passed
24878 as function parameters or assigned to each other and cannot be a return
24879 value from a function, e.g.:
24905 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
24916 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
24938 return rets;/* is invalid in SDCC although allowed in ANSI */
24945 \begin_inset LatexCommand \index{long long (not supported)}
24950 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
24958 \begin_inset LatexCommand \index{double (not supported)}
24962 ' precision floating point
24963 \begin_inset LatexCommand \index{Floating point support}
24970 No support for setjmp
24971 \begin_inset LatexCommand \index{setjmp (not supported)}
24976 \begin_inset LatexCommand \index{longjmp (not supported)}
24984 \begin_inset LatexCommand \index{K\&R style}
24988 function declarations are NOT allowed.
24994 foo(i,j) /* this old style of function declarations */
24996 int i,j; /* are valid in ANSI but not valid in SDCC */
25011 Certain words that are valid identifiers in the standard may be reserved
25012 words in SDCC unless the
25025 \begin_inset LatexCommand \index{-\/-std-c89}
25040 \begin_inset LatexCommand \index{-\/-std-c99}
25046 command line options are used.
25047 These may include (depending on the selected processor): 'at', 'banked',
25048 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
25049 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
25050 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
25052 Compliant equivalents of these keywords are always available in a form
25053 that begin with two underscores
25054 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
25059 '__data' instead of 'data'.
25062 Cyclomatic Complexity
25063 \begin_inset LatexCommand \index{Cyclomatic complexity}
25070 Cyclomatic complexity of a function is defined as the number of independent
25071 paths the program can take during execution of the function.
25072 This is an important number since it defines the number test cases you
25073 have to generate to validate the function.
25074 The accepted industry standard for complexity number is 10, if the cyclomatic
25075 complexity reported by SDCC exceeds 10 you should think about simplification
25076 of the function logic.
25077 Note that the complexity level is not related to the number of lines of
25078 code in a function.
25079 Large functions can have low complexity, and small functions can have large
25085 SDCC uses the following formula to compute the complexity:
25090 complexity = (number of edges in control flow graph) - (number of nodes
25091 in control flow graph) + 2;
25095 Having said that the industry standard is 10, you should be aware that in
25096 some cases it be may unavoidable to have a complexity level of less than
25098 For example if you have switch statement with more than 10 case labels,
25099 each case label adds one to the complexity level.
25100 The complexity level is by no means an absolute measure of the algorithmic
25101 complexity of the function, it does however provide a good starting point
25102 for which functions you might look at for further optimization.
25105 Retargetting for other Processors
25108 The issues for retargetting the compiler are far too numerous to be covered
25110 What follows is a brief description of each of the seven phases of the
25111 compiler and its MCU dependency.
25114 Parsing the source and building the annotated parse tree.
25115 This phase is largely MCU independent (except for the language extensions).
25116 Syntax & semantic checks are also done in this phase, along with some initial
25117 optimizations like back patching labels and the pattern matching optimizations
25118 like bit-rotation etc.
25121 The second phase involves generating an intermediate code which can be easy
25122 manipulated during the later phases.
25123 This phase is entirely MCU independent.
25124 The intermediate code generation assumes the target machine has unlimited
25125 number of registers, and designates them with the name iTemp.
25126 The compiler can be made to dump a human readable form of the code generated
25140 This phase does the bulk of the standard optimizations and is also MCU independe
25142 This phase can be broken down into several sub-phases:
25146 Break down intermediate code (iCode) into basic blocks.
25148 Do control flow & data flow analysis on the basic blocks.
25150 Do local common subexpression elimination, then global subexpression elimination
25152 Dead code elimination
25156 If loop optimizations caused any changes then do 'global subexpression eliminati
25157 on' and 'dead code elimination' again.
25160 This phase determines the live-ranges; by live range I mean those iTemp
25161 variables defined by the compiler that still survive after all the optimization
25163 Live range analysis
25164 \begin_inset LatexCommand \index{Live range analysis}
25168 is essential for register allocation, since these computation determines
25169 which of these iTemps will be assigned to registers, and for how long.
25172 Phase five is register allocation.
25173 There are two parts to this process.
25177 The first part I call 'register packing' (for lack of a better term).
25178 In this case several MCU specific expression folding is done to reduce
25183 The second part is more MCU independent and deals with allocating registers
25184 to the remaining live ranges.
25185 A lot of MCU specific code does creep into this phase because of the limited
25186 number of index registers available in the 8051.
25189 The Code generation phase is (unhappily), entirely MCU dependent and very
25190 little (if any at all) of this code can be reused for other MCU.
25191 However the scheme for allocating a homogenized assembler operand for each
25192 iCode operand may be reused.
25195 As mentioned in the optimization section the peep-hole optimizer is rule
25196 based system, which can reprogrammed for other MCUs.
25200 \begin_inset LatexCommand \index{Compiler internals}
25207 The anatomy of the compiler
25208 \begin_inset LatexCommand \label{sub:The-anatomy-of}
25217 This is an excerpt from an article published in Circuit Cellar Magazine
25223 It's a little outdated (the compiler is much more efficient now and user/develo
25224 per friendly), but pretty well exposes the guts of it all.
25230 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
25231 It is fairly easy to retarget for other 8-bit MCU.
25232 Here we take a look at some of the internals of the compiler.
25237 \begin_inset LatexCommand \index{Parsing}
25244 Parsing the input source file and creating an AST (Annotated Syntax Tree
25245 \begin_inset LatexCommand \index{Annotated syntax tree}
25250 This phase also involves propagating types (annotating each node of the
25251 parse tree with type information) and semantic analysis.
25252 There are some MCU specific parsing rules.
25253 For example the storage classes, the extended storage classes are MCU specific
25254 while there may be a xdata storage class for 8051 there is no such storage
25255 class for z80 or Atmel AVR.
25256 SDCC allows MCU specific storage class extensions, i.e.
25257 xdata will be treated as a storage class specifier when parsing 8051 C
25258 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
25263 \begin_inset LatexCommand \index{iCode}
25270 Intermediate code generation.
25271 In this phase the AST is broken down into three-operand form (iCode).
25272 These three operand forms are represented as doubly linked lists.
25273 ICode is the term given to the intermediate form generated by the compiler.
25274 ICode example section shows some examples of iCode generated for some simple
25275 C source functions.
25279 \begin_inset LatexCommand \index{Optimizations}
25286 Bulk of the target independent optimizations is performed in this phase.
25287 The optimizations include constant propagation, common sub-expression eliminati
25288 on, loop invariant code movement, strength reduction of loop induction variables
25289 and dead-code elimination.
25292 Live range analysis
25293 \begin_inset LatexCommand \index{Live range analysis}
25300 During intermediate code generation phase, the compiler assumes the target
25301 machine has infinite number of registers and generates a lot of temporary
25303 The live range computation determines the lifetime of each of these compiler-ge
25304 nerated temporaries.
25305 A picture speaks a thousand words.
25306 ICode example sections show the live range annotations for each of the
25308 It is important to note here, each iCode is assigned a number in the order
25309 of its execution in the function.
25310 The live ranges are computed in terms of these numbers.
25311 The from number is the number of the iCode which first defines the operand
25312 and the to number signifies the iCode which uses this operand last.
25315 Register Allocation
25316 \begin_inset LatexCommand \index{Register allocation}
25323 The register allocation determines the type and number of registers needed
25325 In most MCUs only a few registers can be used for indirect addressing.
25326 In case of 8051 for example the registers R0 & R1 can be used to indirectly
25327 address the internal ram and DPTR to indirectly address the external ram.
25328 The compiler will try to allocate the appropriate register to pointer variables
25330 ICode example section shows the operands annotated with the registers assigned
25332 The compiler will try to keep operands in registers as much as possible;
25333 there are several schemes the compiler uses to do achieve this.
25334 When the compiler runs out of registers the compiler will check to see
25335 if there are any live operands which is not used or defined in the current
25336 basic block being processed, if there are any found then it will push that
25337 operand and use the registers in this block, the operand will then be popped
25338 at the end of the basic block.
25342 There are other MCU specific considerations in this phase.
25343 Some MCUs have an accumulator; very short-lived operands could be assigned
25344 to the accumulator instead of a general-purpose register.
25350 Figure II gives a table of iCode operations supported by the compiler.
25351 The code generation involves translating these operations into corresponding
25352 assembly code for the processor.
25353 This sounds overly simple but that is the essence of code generation.
25354 Some of the iCode operations are generated on a MCU specific manner for
25355 example, the z80 port does not use registers to pass parameters so the
25356 SEND and RECV iCode operations will not be generated, and it also does
25357 not support JUMPTABLES.
25364 <Where is Figure II?>
25367 In the original article Figure II was announced to be downloadable on
25372 Unfortunately it never seemed to have shown up there, so: where is Figure
25377 \begin_inset LatexCommand \index{iCode}
25384 This section shows some details of iCode.
25385 The example C code does not do anything useful; it is used as an example
25386 to illustrate the intermediate code generated by the compiler.
25398 /* This function does nothing useful.
25405 for the purpose of explaining iCode */
25408 short function (data int *x)
25416 short i=10; \SpecialChar ~
25418 /* dead initialization eliminated */
25423 short sum=10; /* dead initialization eliminated */
25436 while (*x) *x++ = *p++;
25450 /* compiler detects i,j to be induction variables */
25454 for (i = 0, j = 10 ; i < 10 ; i++, j
25480 mul += i * 3; \SpecialChar ~
25482 /* this multiplication remains */
25488 gint += j * 3;\SpecialChar ~
25490 /* this multiplication changed to addition */
25504 In addition to the operands each iCode contains information about the filename
25505 and line it corresponds to in the source file.
25506 The first field in the listing should be interpreted as follows:
25511 Filename(linenumber: iCode Execution sequence number : ICode hash table
25512 key : loop depth of the iCode).
25517 Then follows the human readable form of the ICode operation.
25518 Each operand of this triplet form can be of three basic types a) compiler
25519 generated temporary b) user defined variable c) a constant value.
25520 Note that local variables and parameters are replaced by compiler generated
25523 \begin_inset LatexCommand \index{Live range analysis}
25527 are computed only for temporaries (i.e.
25528 live ranges are not computed for global variables).
25530 \begin_inset LatexCommand \index{Register allocation}
25534 are allocated for temporaries only.
25535 Operands are formatted in the following manner:
25540 Operand Name [lr live-from : live-to ] { type information } [ registers
25546 As mentioned earlier the live ranges are computed in terms of the execution
25547 sequence number of the iCodes, for example
25549 the iTemp0 is live from (i.e.
25550 first defined in iCode with execution sequence number 3, and is last used
25551 in the iCode with sequence number 5).
25552 For induction variables such as iTemp21 the live range computation extends
25553 the lifetime from the start to the end of the loop.
25555 The register allocator used the live range information to allocate registers,
25556 the same registers may be used for different temporaries if their live
25557 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
25558 iTemp17 since their live ranges do not overlap.
25559 In addition the allocator also takes into consideration the type and usage
25560 of a temporary, for example itemp6 is a pointer to near space and is used
25561 as to fetch data from (i.e.
25562 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
25563 Some short lived temporaries are allocated to special registers which have
25564 meaning to the code generator e.g.
25565 iTemp13 is allocated to a pseudo register CC which tells the back end that
25566 the temporary is used only for a conditional jump the code generation makes
25567 use of this information to optimize a compare and jump ICode.
25569 There are several loop optimizations
25570 \begin_inset LatexCommand \index{Loop optimization}
25574 performed by the compiler.
25575 It can detect induction variables iTemp21(i) and iTemp23(j).
25576 Also note the compiler does selective strength reduction
25577 \begin_inset LatexCommand \index{Strength reduction}
25582 the multiplication of an induction variable in line 18 (gint = j * 3) is
25583 changed to addition, a new temporary iTemp17 is allocated and assigned
25584 a initial value, a constant 3 is then added for each iteration of the loop.
25585 The compiler does not change the multiplication
25586 \begin_inset LatexCommand \index{Multiplication}
25590 in line 17 however since the processor does support an 8 * 8 bit multiplication.
25592 Note the dead code elimination
25593 \begin_inset LatexCommand \index{Dead-code elimination}
25597 optimization eliminated the dead assignments in line 7 & 8 to I and sum
25605 Sample.c (5:1:0:0) _entry($9) :
25610 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
25615 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
25620 Sample.c(11:4:53:0) preHeaderLbl0($11) :
25625 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
25631 Sample.c(11:6:5:1) _whilecontinue_0($1) :
25636 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
25642 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25647 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
25653 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
25659 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
25665 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
25671 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
25672 * int}[r0] + 0x2 {short}
25677 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
25682 Sample.c(11:17:21:0)_whilebreak_0($3) :
25687 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
25692 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
25697 Sample.c(15:20:54:0)preHeaderLbl1($13) :
25702 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
25707 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
25712 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
25717 Sample.c(15:24:26:1)_forcond_0($4) :
25722 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
25728 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
25733 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
25734 + ITemp21 [lr21:38]{short}[r4]
25739 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
25745 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
25746 + iTemp15 [lr29:30]{short}[r1]
25751 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
25757 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
25763 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
25769 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
25775 Sample.c(19:38:47:1) goto _forcond_0($4)
25780 Sample.c(19:39:48:0)_forbreak_0($7) :
25785 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
25786 + ITemp11 [lr19:40]{short}[r3]
25791 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
25796 Sample.c(20:42:51:0)_return($8) :
25801 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
25807 Finally the code generated for this function:
25848 ; ----------------------------------------------
25853 ; function function
25858 ; ----------------------------------------------
25868 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
25880 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
25892 ;_whilecontinue_0($1) :
25902 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
25907 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25966 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
25985 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
26032 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
26072 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
26098 ; iTemp6 [lr5:16]{_near * int}[r0] =
26103 ; iTemp6 [lr5:16]{_near * int}[r0] +
26120 ; goto _whilecontinue_0($1)
26132 ; _whilebreak_0($3) :
26142 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
26154 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
26166 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
26178 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
26197 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
26226 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
26231 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
26276 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
26281 ; iTemp21 [lr21:38]{short}[r4]
26307 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
26340 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
26345 ; iTemp15 [lr29:30]{short}[r1]
26364 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
26411 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
26458 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
26470 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
26484 cjne r5,#0xff,00104$
26496 ; goto _forcond_0($4)
26508 ; _forbreak_0($7) :
26518 ; ret iTemp24 [lr40:41]{short}
26561 A few words about basic block successors, predecessors and dominators
26564 Successors are basic blocks
26565 \begin_inset LatexCommand \index{Basic blocks}
26569 that might execute after this basic block.
26571 Predecessors are basic blocks that might execute before reaching this basic
26574 Dominators are basic blocks that WILL execute before reaching this basic
26608 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
26611 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
26614 c) domVect of [BB4] = BB1 ...
26615 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
26623 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
26633 Thanks to all the other volunteer developers who have helped with coding,
26634 testing, web-page creation, distribution sets, etc.
26635 You know who you are :-)
26642 This document was initially written by Sandeep Dutta
26645 All product names mentioned herein may be trademarks
26646 \begin_inset LatexCommand \index{Trademarks}
26650 of their respective companies.
26657 To avoid confusion, the installation and building options for SDCC itself
26658 (chapter 2) are not part of the index.
26662 \begin_inset LatexCommand \printindex{}