\documentclass[11pt]{article}
\usepackage{pdflscape}
\newif\ifpdf
\ifx\pdfoutput\undefined
    \pdffalse           % we are not running PDFLaTeX
\else
    \pdfoutput=1        % we are running PDFLaTeX
    \pdftrue
\fi
%----------------------------------------------------------------------
\ifpdf
    \usepackage[pdftex,
        letterpaper=true,
        colorlinks=true,
        urlcolor=rltblue,               % \href{...}{...}
        anchorcolor=rltbrightblue,
        filecolor=rltgreen,             % \href*{...}
        linkcolor=rltred,               % \ref{...} and \pageref{...}
        menucolor=webdarkblue,
        citecolor=webbrightgreen,
        pdftitle={EE 308 Spring 2009},
        pdfauthor={Bill Rison, New Mexico Tech},
        pagebackref,
        pdfpagemode=None,
        bookmarksopen=true]{hyperref}
    \pdfcompresslevel=9
    \usepackage[pdftex]{graphicx}
    \usepackage{thumbpdf}
\else
    \usepackage[dvips,
        letterpaper=true,
        colorlinks=true,
        urlcolor=rltblue,               % \href{...}{...}
        anchorcolor=rltbrightblue,
        filecolor=rltgreen,             % \href*{...}
        linkcolor=rltred,               % \ref{...} and \pageref{...}
        menucolor=webdarkblue,
        citecolor=webbrightgreen]{hyperref}
    \usepackage{graphicx}
\fi

\usepackage{color}
\definecolor{rltbrightred}{rgb}{1,0,0}
\definecolor{rltred}{rgb}{0.75,0,0}
\definecolor{rltdarkred}{rgb}{0.5,0,0}
%
\definecolor{rltbrightgreen}{rgb}{0,0.75,0}
\definecolor{rltgreen}{rgb}{0,0.5,0}
\definecolor{rltdarkgreen}{rgb}{0,0,0.25}
%
\definecolor{rltbrightblue}{rgb}{0,0,1}
\definecolor{rltblue}{rgb}{0,0,0.75}
\definecolor{rltdarkblue}{rgb}{0,0,0.5}

\definecolor{webred}{rgb}{0.5,.25,0}
\definecolor{webblue}{rgb}{0,0,0.75}
\definecolor{webgreen}{rgb}{0,0.5,0}


\usepackage{fancyheadings}
%\setlength{\parskip}{2ex}
%\setlength{\parindent}{2em}
\hoffset=-55pt
\voffset=-60pt
\textheight=9in
\textwidth=6.5in
\parindent=2em
\parskip=10pt
\pagestyle{fancy}
\lhead{\bfseries EE 308}
\chead{\bfseries }
\rhead{\bfseries Spring 2009}
\lfoot{\bfseries }
\cfoot{\thepage}
\rfoot{\bfseries }
\setlength{\headrulewidth}{0.5pt}

\begin{document}\label{start}

\begin{large}

\begin{center}
{\bf A Simple MC9S12 Program}
\end{center}

\begin{itemize}
\item All programs and data must be placed in memory between address
{\tt 0x1000} and {\tt 0x3BFF}.  For our programs we will put the
first instruction at {\tt 0x2000}, and the first data byte at {\tt
0x1000}
\item Consider the following program:
\begin{large}
\begin{verbatim}
ldaa $1000 ; Put contents of memory at 0x1000 into A
inca       ; Add one to A
staa $1001 ; Store the result into memory at 0x1001 
swi        ; End program
\end{verbatim}
\end{large}
\item If the first instruction is at address {\tt 0x2000}, the following
bytes in memory will tell the MC9S12 to execute the above program:
{\tt
\begin{tabular}{l|l|l}
Address & Value & Instruction \\ \hline
0x2000 & B6 & {\tt ldaa \$1000}\\
0x2001 & 10 \\
0x2002 & 00 \\
0x2003 & 42 & {\tt inca}\\
0x2004 & 7A &{\tt staa \$1001}\\
0x2005 & 10 \\
0x2006 & 01 \\
0x2007 & 3F & {\tt swi}\\
\end{tabular}
}
\item If the contents of address {\tt 0x1000} were {\tt 0xA2}, the
program would put an {\tt 0xA3} into address {\tt 0x1001}.
\end{itemize}


\newpage
\begin{center}
{\bf A Simple Assembly Language Program.}
\end{center}
\begin{itemize}
\item It is difficult for humans to remember the numbers (op codes) for
computer instructions.  It is also hard for us to keep track of the
addresses of numerous data values.  Instead we use words called
mnemonics to represent instructions, and labels to represent addresses,
and let a computer programmer called an assembler to convert our program
to binary numbers (machine code).
\item Here is an assembly language program to implement the previous
program:
\begin{large}
\begin{verbatim}
prog     equ      $2000  ; Start program at 0x2000
data     equ      $1000  ; Data value at 0x1000

         org      prog

         ldaa     input
         inca
         staa     result
         swi

         org      data
input:   dc.b     $A2
result:  ds.b     1
\end{verbatim}
\end{large}
\item We would put this code into a file and give it a name, such as
{\tt main.s}.   (Assembly language programs usually have the extension {\tt
.s} or {\tt .asm}.)
\item Note that {\tt equ}, {\tt org}, {\tt dc.b} and {\tt ds.b} are not
instructions for the MC9S12 but are directives to the assembler which make
it possible for us to write assembly language programs.  The are called
assembler directives or psuedo-ops.  For example the psuedo-op {\tt org}
tells the assembler that the starting address (origin) of our program
should be {\tt 0x2000}.
\end{itemize}

\newpage
\begin{center}
{\bf Assembling an Assembly Language Program} 
\end{center}

\begin{itemize}
\item A computer program called an assembler can convert an assembly
language program into machine code.
\item The assembler we use in class is a part of CodeWarrior
\item The assembler will produce a file called {\tt main.lst}, which shows the
machine code generated.
\begin{large}
\begin{verbatim}
Freescale HC12-Assembler 
(c) Copyright Freescale 1987-2009

 Abs. Rel.   Loc    Obj. code   Source line
 ---- ----   ------ ---------   -----------
    1    1          0000 2000   prog     equ      $2000  ; Start program at 0x2000
    2    2          0000 1000   data     equ      $1000  ; Data value at 0x1000
    3    3                      
    4    4                               org      prog
    5    5                      
    6    6  a002000 B610 00              ldaa     input
    7    7  a002003 42                   inca
    8    8  a002004 7A10 01              staa     result
    9    9  a002007 3F                   swi
   10   10                      
   11   11                               org      data
   12   12  a001000 A2          input:   dc.b     $A2
   13   13  a001001             result:  ds.b     1
\end{verbatim}
\end{large}
\item This will produce a file called {\tt Project.abs.s19} which we can load
into the MC9S12.
\begin{verbatim}
S0630000433A5C446F63756D656E747320616E642053657474696E67735C7269736F6E5C4D7920446F63756D656E74735C41636164656D69635C45453330385C53707231305C737570705C3130303132355C70315C62696E5C50726F6A6563742E6162732A
S1051000A20048
S10B2000B61000427A10013F02
S9030000FC
\end{verbatim}


\newpage

\item This will produce a file called {\tt Project.abs.s19} which we can load
into the MC9S12.
\begin{verbatim}
S0630000433A5C446F63756D656E747320616E642053657474696E67735C7269736F6E5C4D7920446F63756D656E74735C41636164656D69635C45453330385C53707231305C737570705C3130303132355C70315C62696E5C50726F6A6563742E6162732A
S1051000A20048
S10B2000B61000427A10013F02
S9030000FC
\end{verbatim}

\item 
The first line of the S19 file starts with a S0:  the S0 indicates that it is
the first line.
\begin{itemize}
\item The first line from CodeWarrior is too long for the DBug-12 monitor.
You will need to delete the first line before loading the file into the
MC9S12.
\end{itemize}
\item 
The last line of the S19 file starts with a S9:  the S9 indicates that it is
the last line.
\item The other lines begin with a S1:  the S1 indcates these lines are data
to be loaded into the MC9S12 memory.

\item Here is the second line (with some spaces added):\\

\verb+S1 0B 2000 B6 1000 42 7A 1001 3F 02+

\item On the second line, the \verb+S1+ is followed by a \verb+0B+.  This
tells the loader that there this line has 11 (\verb+0x0B+) bytes of data
follow.  
\item The count \verb+0B+ is followed by \verb+2000+.  This tells the loader
that the data should be put into memory starting with address \verb+0x2000+.
\item The next 16 hex numbers \verb+B61000427A10013F+ are the 8 bytes
to be loaded into memory.  You should be able to find these bytes in the {\tt
test.lst} file.
\item The last two hex numbers, \verb+0x02+, is a one byte checksum, which the
loader can use to make sure the data was loaded correctly.
\end{itemize}

\newpage

\begin{large}
\begin{verbatim}
Freescale HC12-Assembler 
(c) Copyright Freescale 1987-2009

 Abs. Rel.   Loc    Obj. code   Source line
 ---- ----   ------ ---------   -----------
    1    1          0000 2000   prog     equ      $2000  ; Start program at 0x2000
    2    2          0000 1000   data     equ      $1000  ; Data value at 0x1000
    3    3                      
    4    4                               org      prog
    5    5                      
    6    6  a002000 B610 00              ldaa     input
    7    7  a002003 42                   inca
    8    8  a002004 7A10 01              staa     result
    9    9  a002007 3F                   swi
   10   10                      
   11   11                               org      data
   12   12  a001000 A2          input:   dc.b     $A2
   13   13  a001001             result:  ds.b     1
\end{verbatim}
\end{large}

\noindent What will program do?

\begin{itemize}
\item  \verb+ldaa input   :+ Load contents of \verb+0x1000+ into {\bf A}\\
       \verb+              + (\verb+0xA2+ into {\bf A})
\item  \verb+inca         :+ Increment {\bf A}\\ 
       \verb+              + (\verb^0xA2 + 1 = 0xA3 -> ^{\bf{A}})
\item  \verb+staa result  :+ Store contents of {\bf A} to address \verb+0x1001+\\
       \verb+              + (\verb+0xA3 -> adress 0x1001+)
\item  \verb+swi          :+ Software interrupt \\
       \verb+              + (Return control to DBug-12 Monitor)
\end{itemize}


\newpage

{\bf \noindent Simple Programs for the MC9S12}

\begin{center}
\underline{A simple MC9S12 program fragment}

\includegraphics[width=3.5in]{02_prog_1}

\vspace{0.5in}

\underline{A simple MC9S12 program with assembler directives}

\includegraphics[width=4in]{02_prog_2}

\end{center}

\newpage

\begin{center}
\underline{MC9S12 Programming Model --- The registers inside the 
MC9S12 CPU the programmer needs to know about}

\includegraphics[width=7in]{prog_model}

\end{center}

\newpage

\begin{center}
\underline{How the MC9S12 executes a simple program}

\includegraphics[width=7in]{02_simple_prog}

\vspace{0.5in}

\underline{Things you need to know to write MC9S12 assembly language programs}

\includegraphics[width=5in]{03_HC12_Programming}

\end{center}

\newpage

{\bf \noindent Addressing Modes for the MC9S12}

\begin{itemize}
\item Most MC9S12 instructions operate on memory
\item The address of the data an instruction operates on is called the {\it
effective address} of that instruction.
\item Each instruction has information which tells the MC9S12 the address of the
data in memory it operates on.
\item The {\it addressing mode} of the instruction tells the MC9S12 how to
figure out the effective address for the instruction.
\item Each MC9S12 instructions consists of a one or two byte {\it op code}
which tells the MC9S12 what to do and what addressing mode to use, followed,
when necessary by one or more bytes which tell the MC9S12 how to deterime the
effective address.
\begin{itemize}
\item All two-byte op codes begin with an \$18.
\end{itemize}
\item For example, the {\tt LDAA} instruction has 4 different op codes
({\tt{86, 96, A6, B6}}), one for each of the 4 different addressing modes
({\tt{IMM, DIR, EXT, IDX}}).
\end{itemize}

\begin{center}
\includegraphics[width=7in]{ldaa}

\end{center}

\newpage

\begin{center}
\underline{The MC9S12 has 6 addressing modes}

\includegraphics[width=7in]{04_addr_modes_1}

\end{center}

\newpage

\begin{center}
\underline{The {\it Inherent} (INH) addressing mode}

\includegraphics[width=7in]{05_inh}

\end{center}

\newpage

\begin{center}
\underline{The {\it Extended} (EXT) addressing mode}

\includegraphics[width=7in]{06_ext}

\end{center}

\newpage

\begin{center}
\underline{The {\it Direct} (DIR) addressing mode}

\includegraphics[width=7in]{07_dir}

\end{center}

\newpage

\begin{center}
\underline{The {\it Immediate} (IMM) addressing mode}

\includegraphics[width=7in]{08_imm}

\end{center}

\newpage

\begin{center}
\underline{The {\it Indexed} (IDX, IDX1, IDX2) addressing mode}

\includegraphics[width=7in]{09_idx}

\end{center}

\newpage

\begin{center}
\underline{Different types of indexed addressing modes}\\
\underline{(Note:  We will not discuss indirect indexed mode)}

\includegraphics[width=6in]{10_idx_modes}

\end{center}

\underline{The data books list three different types of indexed modes:}
\begin{itemize}
\item Table 4.2 of the {\bf Core Users Guide} shows details
\item {\bf IDX}:  One byte used to specify address
\begin{itemize}
\item Called the postbyte
\item Tells which register to use
\item Tells whether to use autoincrement or autodecrement
\item Tells offset to use
\end{itemize}
\item {\bf IDX1}: Two bytes used to specify address
\begin{itemize}
\item First byte called the postbyte
\item Second byte called the extension
\item Postbyte tells which register to use, and sign of offset
\item Extension tells size of offset
\end{itemize}
\item {\bf IDX2}: Three bytes used to specify address
\begin{itemize}
\item First byte called the postbyte
\item Next two bytes called the extension
\item Postbyte tells which register to use
\item Extension tells size of offset
\end{itemize}

\end{itemize}

%\newpage

%\begin{center}

\includegraphics[width=7in]{table_3_2}

%\end{center}

\newpage

\begin{center}
\underline{The {\it Relative} (REL) addressing mode}

\includegraphics[width=7in]{11_rel}

\end{center}

\newpage

\begin{center}
\underline{Summary of MC9S12 addressing modes}

\includegraphics[width=7in]{12_adr_modes}

\end{center}

\underline{A few instructions have two effective addresses:}
\begin{itemize}
\item {\tt MOVB \$2000,\$3000} \hspace{1em}  Move byte from address {\tt \$2000} to {\tt
\$3000}
\item {\tt MOVW 0,X,0,Y} \hspace{1em} Move word from address pointed to by {\tt X} to
address pointed to by {\tt Y}
\end{itemize}


\end{large}

\end{document}
