% Format: latex % $Header: C:/MICROP/56PTWS0/RCS/asmprim2.tex 1.15 1996/01/26 19:44:50 Nick Exp Nick $ % $Log: asmprim2.tex $ % Revision 1.15 1996/01/26 19:44:50 Nick % Fixed a major error: a destination operand is on the right of the % two operands. % Also added some more labels for external references. % Removed the extra space after the acronym ECB. % % Revision 1.14 1996/01/19 18:44:40 Nick % Shifted the page so that the outer margin is wider, allowing for % trimming of sheets. % Also fixed the index entries with allowbreak. % % Revision 1.13 1996/01/19 18:25:16 Nick % 1. made the last page have the pagestyle plain. % 2. updated the "reference" box with RCSDate, added further remarks. % % Revision 1.12 1996/01/19 18:05:39 Nick % Have added the Index and References to the TOC. % Have also changed the name Bibliography to References. % % Revision 1.11 1996/01/19 17:16:00 Nick % I've now added an index. Boy, what a big job! % % Revision 1.10 1996/01/18 21:46:42 Nick % I got the year wrong on the cover! Now it's 1996. % Also the pop is into the D0 register, so specified this in picture. % % Revision 1.9 1996/01/18 15:25:54 Nick % This is the version that was actually published, although the % version number is different. Better save the auxiliary file. % % Revision 1.8 1996/01/18 13:29:34 Nick % Have changed from the book to the report class. This was important % in using the openany option to allow chapters to begin on an even page % too. % Fixed a few typos, clarified a few points, including asking the % student to bring the book to the lab with them! % Fixed the section levels of the headings in the first chapter on % the 68000 hardware. % % Revision 1.7 1996/01/17 20:40:06 Nick % Have now fixed the page layout so that chapters start on the right % side. The previous version put the odd pages on the left!!! % % Revision 1.6 1996/01/17 19:54:12 Nick % This is the final(!!) version (at least for the first printing), % with the author's name and date bold and raggedleft. % % Revision 1.5 1996/01/17 19:19:19 Nick % I have now changed the headers to privide the chapter names % on the left and the section titles on the right. % I shortened the names of two chapters so that they fit, and % added a few words about program design. % Have also done spelling checking. % % Revision 1.4 1996/01/17 18:16:00 Nick % Now have divided the book into chapters. This uses more paper, but the % material is more logically divided and like is now put more with % like material. % % Revision 1.3 1996/01/17 15:21:56 Nick % This is a very major update: added sections on the stack, % many other changes too numerous to mention now. % % Revision 1.1 1996/01/15 12:48:01 Nick % Initial revision % \documentclass[% %a4paper,% a5paper,% titlepage,% twoside,% %11pt% %12pt% ]{% %article% report% %book% } \usepackage{% %a4,% vmargin,% varioref,% multicol,% explanation,% fancyhea,% rcs,% booktabs,% verbatim,% %fullpage,% emlines2,% xr,% makeidx,% acro,% cols,% key,% prog,% nbox,% amssymb,% amsmath,% theorem,% %lgrind,% %tabularx,% result,% ifthen,% nick,% lastpage% } \newboolean{smallBook} \setboolean{smallBook}{true} \newboolean{oddRight} \setboolean{oddRight}{true} \ifthenelse{\boolean{smallBook}}% {% % Half A4 size: \setpapersize{A5}% \setmarginsrb{13mm}% left For a book, the inner. {9mm}% top {20mm}% right For a book, the outer. {10mm}% bottom {12pt}% headheight---increase to stop fancyhead warn {4mm}% headsep {0pt}% footheight {8mm}% footskip }% %else {% \setmarginsrb{30mm}% left {18mm}% top {30mm}% right {10mm}% bottom {15pt}% headheight---increase to stop fancyhead warn {8mm}% headsep {0pt}% footheight {11mm}% footskip } % End of if small book ... else. % For the external references (xr.sty): %\externaldocument[enl4s:]{enl4s} % Stuff for the headers and footers on each page: \pagestyle{fancyplain} \RCS$Revision: 1.15 $ \RCS$Date: 1996/01/26 19:44:50 $ \DeclareRobustCommand{\referenceNum}{EE/27/LA/21/95} % 6, 7,..., 21. \newcommand{\revision}{ver \RCSRevision{}% ,\hspace{0.8ex} \RCSDate% } \newcommand*{\headSize}{\scriptsize} \newcommand*{\innerHead}{\textbf{\upshape\headSize A guide to using 68000 assembly language in the laboratory and at home}} \newcommand*{\outerHead}{\textbf{\normalsize\thepage}} \newcommand*{\outerRightFoot}{\tiny\referenceNum} \newcommand*{\outerLeftFoot}{\tiny\revision} \renewcommand*{\chaptermark}[1]{\markboth{#1}{#1}} \renewcommand*{\sectionmark}[1]{\markright{\thesection\ \ #1}} \ifthenelse{\boolean{oddRight}}% Odd pages are on the right: {% \lhead% [\fancyplain{}{\outerHead}]% {\fancyplain{}{\bfseries\rightmark}} \rhead% [\fancyplain{}{\bfseries\leftmark}]% {\fancyplain{}{\outerHead}} \lfoot[\fancyplain{}{\outerLeftFoot}]{} \rfoot[]{\fancyplain{}{\outerRightFoot}} }% % Else if not oddRight, ie, odd pages are on the left. {% \lhead% [\fancyplain{}{\innerHead}] {\fancyplain{}{\outerHead}} \rhead% [\fancyplain{}{\outerHead}] {\fancyplain{}{\innerHead}} \lfoot[]{\fancyplain{}{\outerLeftFoot}} \rfoot[\fancyplain{}{\outerRightFoot}]{} } \cfoot[\fancyplain{}{}]{\fancyplain{}{}} \newcommand*{\clearemptydoublepage}% {\newpage{\pagestyle{empty}\cleardoublepage}} % Stuff for the title page: \newcommand*{\HRule}{\rule{\linewidth}{1mm}} \title{\raggedleft\HRule\\[1ex] \sffamily\Huge\bfseries Using 68000 assembly language in the laboratory and at home\\[4ex] \relsize{-3}A guide to 68000 systems in the Microprocessor Applications Laboratory~D226\\[5ex] \relsize{-2}Hong Kong Technical College (Tsing Yi)\\[2ex] Department of Electrical and Communications Engineering\\[1ex] \HRule } %\author{\noindent\makebox[\columnwidth][r]{\sffamily Nick Urbanik}} \author{} \date{\raggedleft\sffamily\bfseries Nick Urbanik\\[2ex] \normalsize \RCSDate} % This is for the table of flag bits in the section on Bcc. % To get \tt letters in math mode by default. % See The LaTeX Companion, p. 208--209. \DeclareMathVersion{mathtt} \SetSymbolFont{letters}{mathtt}{OT1}{cmtt}{m}{n} \newcommand*{\Bcc}{\textnormal{\texttt{B$_{\text{CC}}$}}\xspace} \newcommand*{\DBcc}{\textnormal{\texttt{DB$_{\text{CC}}$}}\xspace} \newcommand*{\simk}{{\sffamily sim68k}\xspace} \newcommand*{\asmk}{{\sffamily asm68k}\xspace} \newcommand*{\ECB}{\acro{ECB}\xspace} \newcommand*{\ARI}{\acro{ARI}\xspace} \newcommand*{\CR}{\acro{CR}\xspace} \newcommand*{\LF}{\acro{LF}\xspace} % The size for the text in the stack picture for push and pop: \newcommand*{\bigfigTextSize}{\large} % The set up for the exercises: \theorembodyfont{\rmfamily} \newtheorem{exercise}{Exercise}[chapter] \newtheorem{example}{Example}[chapter] \theoremheaderfont{\scshape} \makeindex \newcommand*{\nn}[1]{#1n} % For index entries in footnotes. \renewcommand*{\bibname}{References} \begin{document} \index{parallel chip|see{parallel interface/\allowbreak{}timer}} \index{PIT@\PIT|see{parallel interface/\allowbreak{}timer}} % \frontmatter \pagenumbering{roman} \maketitle \thispagestyle{empty} \newpage \vspace*{\stretch{1}} Copyright \copyright{} 1996 by Nick Urbanik\\ Department of Electrical and Communications Engineering\\ Hong Kong Technical College (Tsing Yi)\\ nicku@iohk.com For use by the students and staff of the Hong Kong Technical Colleges. This booklet was typeset using \LaTeXe. \vspace*{\stretch{2}} % \clearemptydoublepage %\setcounter{page}{1} \tableofcontents %\clearemptydoublepage \listoffigures %\clearemptydoublepage \listoftables %\thispagestyle{empty} % \clearemptydoublepage % \mainmatter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Introduction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \pagenumbering{arabic} % \enlargethispage*{100cm} This lab primer introduces many new concepts that you will apply in the laboratory. Your lab sheets will refer to this primer. You need to read the parts referred to by your lab sheets \emph{before} you come into the lab. I suggest that you use a highlighter as you read, highlighting the important points in different colours so that you can find them easily when you are in the lab. There are five main topics in this little book: \begin{enumerate} \item The 68000 programmer's model: the registers and memory organisation of the 68000 \item 68000 assembly language \item \IO using the 68230 \PIT parallel chip \item How to edit, assemble, download and run your programs in the laboratory \item How to use the simulator and assembler at your home. \end{enumerate} How to use this book: \begin{enumerate} \item Bring it to each lab session \item \begin{enumerate} \item Bring a floppy disk to the laboratory \item Collect the \simk simulator and the \asmk assembler\footnote{See section~\vref{sec:simkObtain} for details of how to obtain the \simk simulator and \asmk assembler.} \item take these home or to work \item install them on a \PC \item \emph{use} them: \item work through the example in section~\vref{sec:simTut} \item read the rest of appendix~\ref{chp:sim} for details of how to use the assembler and the simulator. \end{enumerate} \item Read the sections that your lab sheet refers to \item Use the index and table of contents to locate any topic you need, then read that section. \item It is not necessary for you to read this starting from page 1 and finishing with page \pageref{LastPage}! \end{enumerate} % \clearemptydoublepage %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{The programmer's model of the 68000 \up} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The \emph{programmer's model} of a \up is the set of registers, the memory address space, the addressing modes and the instruction set. In this chapter we just look at the registers and memory space of the 68000. The \CPU is the heart of the microcomputer system. We also call the \CPU a \up. A \up\index{microprocessor!definition} is a \CPU that is made in one integrated circuit, or \IC. We will look at how it works and how to program it. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The registers in the 68000} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:registers} There are 18 special storage locations in the 68000 called \emph{registers}. Figure~\vref{fig:registers} shows the arrangement of these registers and how they can be accessed by you, the programmer. \begin{figure}[tbh] \centering \input{register.pic} \caption{Registers in the 68000. PC is \ix{program counter}% \index{registers!program counter}% . A7 is the stack pointer% \index{stack pointer}% \index{registers!stack pointer}% . All registers are 32 bits wide, except for the status register which is 16 bits wide.} \label{fig:registers} \end{figure} Registers are very special storage locations: each has its own name. They are all located inside the \up. The \CPU can move data to and from the registers \emph{much} faster than to or from memory. For that reason, we try to keep data in \CPU registers while we can. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Data registers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:dataRegisters} \index{registers!data} \index{data registers} Inside the \CPU there are eight temporary storage places that can each hold a 32-bit number. These are called the \emph{data registers}. They are called D0, D1, D2, D3, D4, D5, D6 and D7\e. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Accumulators} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% An \emph{accumulator}% \index{accumulator}% \index{registers!accumulator} is a special \CPU register which is used in nearly all arithmetic and logic instructions as one of the operands. In other \up systems, there may be only one accumulator. The 68000 can use all of the data registers as accumulators. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Address registers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:addressRegisters} There are also eight temporary storage locations to hold addresses inside the \CPU. These are called the \emph{address registers}% \index{address register}% \index{registers!address}% . They are called A0, A1, A2, \ldots, A7\e. A7 is special purpose and is called the \emph{stack pointer}% \index{stack pointer}% \index{registers!stack pointer}% . The others can be used to hold any address. You will see in section~\vref{sec:ariWDisp} and section~\vref{sec:initMacro} that we are using A6 to hold the base address of the 68230 parallel chip, so it is better not to change A6\e. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Special registers: the program counter and the stack pointer} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:pcAndSp} There are three other special registers. There is the \emph{program counter}, labelled PC\e. The \ix{program counter}% \index{registers!program counter} always holds the address of the next instruction to be executed. As soon as the \CPU fetches a word of an instruction, the program counter is automatically incremented to point to the next word of the instruction, or the next instruction itself once the fetching of the current instruction is complete. See section \vref{sec:stack} for more about the stack pointer register% \index{stack pointer}% \index{registers!stack pointer}% , A7, and the stack. The next section discusses the status register. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Flags in the status registers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:statusReg} \index{status register|(}% \index{registers!status|(}% The \emph{status register} contains five \emph{flags}. A flag is a single bit that can have the value 1 or 0. The flags are in the lowest five bits. They each have a single letter name: X = extend flag, N = negative flag, Z = zero flag, V = overflow flag and C = carry flag. These five flags are also sometimes called the \emph{condition codes}. The bottom eight bits of the status register is sometimes called the \emph{condition code register}, CCR\e. The status register is quite different from the other registers, as the \CPU treats it more as a collection of individual bits than as a single number. These individual flags are central to the way that the \CPU decides to do one action rather than another action. These five flags are set or cleared as the result of many instructions. We are particularly interested in the effect of arithmetic instructions on the flags. See the \FLIGHT manual or your text book to see what effect each instruction has on the flags. Table~\vref{tab:statusFlags} shows the five condition code flags. \begin{table}[tbh] \centering \begin{tabularx}{\textwidth}{@{}lccX@{}} \toprule \bfseries name & \bfseries letter & \bfseries bit \# & \multicolumn{1}{c}{\bfseries when set} \\ \midrule carry & C & 0 & The current instruction had a carry out of MSB (most significant bit), or if an \ix{unsigned subtraction}, a \ix{borrow}. This is equivalent to: the result of the current instruction, considered as an \emph{unsigned operation}, did not fit in the destination.\\ \hline overflow & V & 1 & The result of the current instruction, considered as a \emph{signed operation}, did not fit in the destination. With addition, overflow can only happen when adding two numbers of the same sign. In this case, answer is of the opposite sign if and only if there was overflow.\\ \hline zero & Z & 2 & The result of the last instruction was zero\index{zero flag}\index{flag!zero} \\ \hline negative & N & 3 & The result of the last operation was negative\index{negative flag}\index{flag!negative} \\ \hline extend & X & 4 & Used in arithmetic with data bigger than 32 bits to take carries from one long word to the next.\index{extend flag}\index{flag!extend} \\ \bottomrule \end{tabularx} \caption{The five flags in the status register in the \CPU.} \label{tab:statusFlags} \end{table} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \paragraph{Overflow: } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:overflow} \index{overflow flag|(} \index{flag!overflow|(} Overflow indicates that a \emph{signed arithmetic operation} produced the wrong answer. A wrong answer results when the destination operand is too small to hold the signed result. \index{addition!overflow} Overflow from addition may \emph{only} happen when two numbers of the same sign are added together. It will \emph{never} happen when two numbers of opposite sign are added together. You can tell if there is overflow yourself, even without looking at the V flag in the 68000. If you add two positive numbers and the result is negative, then you have overflow. If you add two negative numbers and the result is positive, then you have overflow. \begin{example} Is there overflow from the 8-bit addition 79\hex + 81\hex? \begin{equation*} % { for brace matching \left.\begin{array}{rrl} & 79\hex & > 0 \\ +& 81\hex & < 0 \\ \cline{2-2} & \F\A\hex & < 0 \end{array}\right\} \therefore \text{there is no overflow.} \end{equation*} \end{example} \begin{example} Is there overflow from the 8-bit addition 83\hex + 81\hex? \begin{equation*} % { for brace matching \left.\begin{array}{rrl} & 83\hex & < 0 \\ +& 81\hex & < 0 \\ \cline{2-2} & 04\hex & > 0 \end{array}\right\} \therefore \text{here we have overflow.} \end{equation*} The result is of opposite sign (it is positive). This answer is obviously wrong, because when we add two negative numbers together, we should always get a negative result. \end{example} Overflow is only of interest when you consider the numbers you are doing arithmetic with to be \emph{signed} numbers. It indicates that the destination for the instruction was too small to hold the result of a signed operation. Note that for \emph{signed arithmetic operations}, carry is totally irrelevant. \index{flag!overflow|)} \index{overflow flag|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \paragraph{Carry: } %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:carry} \index{carry flag|(}% \index{flag!carry|(}% Carry is more interesting when you are doing \emph{unsigned} arithmetic---you can completely ignore it when you are doing signed arithmetic. For \emph{unsigned arithmetic}, the carry flag indicates that the destination was too small for the result of an unsigned operation. In other words, the carry flag is set when an unsigned arithmetic operation gives the wrong answer. With subtraction, we have \meta{dest}~=~\meta{dest}~$-$~\meta{src}. The carry flag will be set if the source is greater than the destination, considering both the source and destination as unsigned numbers. Note that carry and overflow are independent; you can have one without the other, or both at the same time, or you can have no carry and no overflow. See section~\vref{sec:bccSimple} for more about how the status register flags are used by the \up for making decisions. \index{carry flag|)}% \index{flag!carry|)}% \index{status register|)}% \index{registers!status|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Memory} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:memory} \index{sixteen-bit microprocessor@16-bit microprocessor}% The 68000 is a 16-bit \up with a 16-bit arithmetic logic unit (\ALU) and a 16-bit address bus. It can transfer 16 bits at a time to or from memory. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The memory map of the 68000 system in the laboratory} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Memory is organised in \index{chunks!byte-sized}byte-sized chunks;\footnote{However I have rarely found these chunks% \index{chunks!juicy|nn}% \index{chunks!meaty|nn}% \index{chunks!as preferred by Rover|nn} to be either juicy or meaty, and it seems Rover still prefers his byte-sized chunks straight from the tin.} each chunk is called a \emph{memory location}. Each \ix{memory location} has its own special number, called its \emph{address}. Addresses are usually specified in hexadecimal. In the 68000 system\index{address!range in 68000 system}, \ix{address}es start from 0 and finish at $2^{24} - 1 = \F\F\F\F\F\F\hex$\e. This gives a maximum of more than 16 million possible memory locations. This range of possible addresses is called the \emph{address space}\index{address space}. Not all of these addresses are used;\index{addresses!used in \FLIGHT} in fact, only about $48 \times 2^{10}$ bytes = 48\,K~bytes are actually used. The way that the systems in the laboratory use the \ix{address space} is shown in figure~\vref{fig:memmap}. \begin{figure}[tbh] \centering \input{memmap.pic} \caption{A \ix{memory map} of the \FLIGHT \up board. M means \ix{megabyte}s; K means \ix{kilobyte}s. $1\M = 2^{20}$; $1\Kb = 2^{10}$.} \label{fig:memmap} \end{figure} Figure~\vref{fig:memmap} shows that there are 16\,K of \RAM (read-write memory), and 32\,K of \ROM containing \ix{monitor firmware}.\index{firmware!monitor} It also shows the addresses in which \IO ports in the 68230 \PIT and in the 68681 \DUART\index{DUART@\DUART} can be found. The \IO ports in the 68230 \PIT are discussed in section~\vref{sec:ioports}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The way data is stored in memory} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:bigendian} \begin{itemize} \item Each memory location has a unique number called an \emph{address}\index{address}. \item Each \ix{memory location} holds an 8-bit (1 byte) value \item Some data items are bigger than one byte, so they must occupy more than one memory location \item The \LSB (least significant byte) is the right-most 2 hexadecimal digits. Informally, we call this the `\ix{little end}' of the data.\index{LSB@\LSB}\index{least significant byte} \item The \MSB (most significant byte) is the left-most 2 hexadecimal digits. Informally, we call this the `\ix{big end}' of the data.\index{MSB@\MSB}\index{most significant byte} Examples: the word (\ie 16-bit) value 379\hex has \MSB 03\hex, and the \LSB is 79\hex. The long word (\ie 32-bit) value 379\hex consists of the four bytes 00, 00, 03 and 79\hex. The \LSB is 79\hex, the \MSB is 00. \item The \emph{address of a data item}\index{address!of data item} is always the lowest address of all the memory locations occupied by the data item. For instance, if a long word (4-byte) value is stored at address 8000$_{16}$, then the four bytes will occupy the four memory locations 8000$_{16}$, 8001$_{16}$, 8002$_{16}$ and 8003$_{16}$. \end{itemize} A 68000 \up always stores data in memory with the most significant byte of the data (the ``big end'') at the address of the data. We say that the 68000 is \emph{big-endian}.\index{big-endian} If we have a 32-bit hexadecimal number 6789abcd$_{16}$, and this number is stored at the address 8000$_{16}$, then the arrangement of the data in memory is as shown in table~\vref{tab:bigEndian}. \begin{table}[tbh] \centering % \begin{tabular}{|c||c||c|} % \hhline{-||-||-} % address & big-endian & little-endian \\ \hhline{:=::=::=:} % 8000 & 67 & cd \\ \hhline{|-||-||-|} % 8001 & 89 & ab \\ \hhline{|-||-||-|} % 8002 & ab & 89 \\ \hhline{|-||-||-|} % 8003 & cd & 67 \\ \hhline{|-||-||-|} % \end{tabular} % \begin{tabular}{@{}ccc@{}} \toprule address & big-endian & little-endian \\ \midrule 8000 & 67 & cd \\ 8001 & 89 & ab \\ 8002 & ab & 89 \\ 8003 & cd & 67 \\ \bottomrule \end{tabular} % \caption{A comparison of storing the number 6789abcd$_{16}$ at the memory address 8000 in a big-endian and a \ix{little-endian} architecture. All numbers are in hexadecimal.} \label{tab:bigEndian} \end{table} Let's see another example using a Motorola \up, which is \ix{big-endian}. Suppose we put the word (\ie a 2-byte value) with value C269\hex at the address 400404\hex. Then at what address is the byte C2\hex? Where is 69\hex?\footnote{The byte C2\hex is at the address 400404\hex. The byte 69\hex is at the address 400405\hex.} Here is a third example using an Intel \up, which is little-endian. Again we put the word (\ie two bytes) with value C269\hex at the absolute address 400404\hex. Then at what addresses are the bytes C2\hex and 69\hex?\footnote{The byte C2\hex is at the address 400405\hex. The byte 69\hex is at the address 400404\hex.} % \clearemptydoublepage %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{68000 Assembly language} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Here we look at some topics of assembly language relevant to the work you will do in the laboratory. This chapter does \emph{not} attempt to replace a text book on 68000 assembly language; it can provide some help with using conditional and branch instructions, as well as some help with addressing modes. I suggest consulting the text book by Horvath \cite{horvath}. A good book that specifically helps with assembly language (but not much else) is the book by Leventhal \cite{leventhal}. There are many books in the library that you may consult. The \FLIGHT manual \cite{flight} provides information about each instruction. There is also some on-line help in the laboratory: type \texttt{asmhelp}~\meta{Enter}% \index{asmhelp@\texttt{asmhelp}}% \index{assembler!on line help} at a \acro{DOS} prompt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The framework of an assembly language program} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Labels, opcodes, operands and comments} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:spacing} \newcommand*{\whiteSp}{\makebox[2em]{\parbox{7em}{\centering\emph{At least} one space or tab}}} \index{assembly language instruction!format|(} A line in a 68000 assembly language program may be either entirely a comment\index{comments} (see next section) or is divided into four parts: \index{label}labels, \ix{opcode}s, \ix{operands} and comments. One or more of these may not be present. The format is:\\[2ex] \begin{tabularx}{\linewidth}{@{}YCYCYCY@{}} \meta{label} & & \meta{opcode} && \makebox[3em][c]{\meta{operands}} && \makebox[3em][c]{\meta{comments}} \\ $\uparrow$ & $\uparrow$ & & $\uparrow$ & & $\uparrow$ & \\ No space or tab & \whiteSp & & \whiteSp & & \whiteSp & \\ Optional & & Required & & \makebox[3em][c]{\parbox{8em}{\centering May be required; depends on opcode}} & & Optional \end{tabularx} The \ix{assembler} divides a line into these fields by considering spaces to separate the fields. For that reason, there must be no space between two operands, otherwise the assembler will consider the second operand to be a comment, and will give a syntax error message complaining that one operand is missing. I suggest you use the \meta{Tab} key to separate each column in your assembly language programs. \index{assembly language instruction!format|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Size of operands and data} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{operand!size|(}% \index{opcode!suffix|(}% \index{W@\texttt{.W}|(}% \index{B@\texttt{.B}|(}% \index{L@\texttt{.L}|(}% Many opcodes (and operands too) can have a \emph{suffix},\label{sec:suffix} which may be one of \texttt{.B}, \texttt{.W} or \texttt{.L}\e. These are shown in the following table. \begin{center} \begin{tabular}{@{}llcc@{}} \toprule \bfseries Suffix & \bfseries Means: & \bfseries number of bytes & \bfseries number of bits\\ \midrule \ttfamily .B & byte & 1 & 8 \\ \ttfamily .W & word & 2 & 16 \\ \ttfamily .L & long word & 4 & 32 \\ \bottomrule \end{tabular} \end{center} If an instruction does not have a suffix, then it is the same as using the suffix ``\inst{.W}\,'', e.g., the instructions \inst{move d0,d1} and \inst{move.w d0,d1} both produce exactly the same machine language. \index{operand!size|)}% \index{opcode!suffix|)}% \index{W@\texttt{.W}|)}% \index{B@\texttt{.B}|)}% \index{L@\texttt{.L}|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Comments in programs} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:comments} \index{comments|(}% \index{assembly language!comments|(}% In the 68000 assembly language, comments can be written in two ways. A whole line comment begins at the left side of the screen with a symbol `\texttt{*}'. Comments can also begin in the fourth field as mentioned in the previous section. A comment is written for the human reader of a program. A comment has absolutely no effect on what the assembler sees; comments are ignored by the assembler. \eemph{You do not need to type the comments from these lab sheets into the computer.} They are there to help you only. When you write bigger programs of your own, then you \emph{should} write comments, especially at the beginning of each subroutine explaining what the subroutine does. This saves a lot of time when you or someone else wants to change the program a year later. \index{comments|)}% \index{assembly language!comments|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Integer numbers in programs} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:constants} \index{numbers!binary|(}% \index{numbers!octal|(}% \index{numbers!hexadecimal|(}% \index{octal numbers|(}% \index{hexadecimal numbers|(}% \index{binary numbers|(}% Integer numbers can be expressed in decimal (the default), hexadecimal, octal or binary. There is a choice of suffixes and prefixes to show which representation is being used. \vspace{1ex} \newcommand{\num}{\meta{number}} \begin{center} \begin{tabular}{@{}lc>{\ttfamily}l@{}} \toprule \textbf{Type} & \textbf{Format} & \textrm{\textbf{Examples}} \\ \midrule decimal & \num, \texttt{\&}\num & 297, \&297 \\ hexadecimal & \texttt{\$}\num, \num\texttt{H} & \$FF, 0FFH \\ octal & \texttt{@}\num, \num\texttt{O}, \num\texttt{Q} & @457, 457O, 457Q \\ binary & \texttt{\%}\num, \num\texttt{B} & \%11011011, 11011011B \\ \bottomrule \end{tabular} \end{center} \vspace{1ex} Notice that if you use \texttt{H}, the first digit must be 0--9, otherwise the assembler will think the value is a label. \index{numbers!binary|)}% \index{numbers!octal|)}% \index{numbers!hexadecimal|)}% \index{octal numbers|)}% \index{hexadecimal numbers|)}% \index{binary numbers|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Assembler directives: \inst{org}, \inst{dc}, \inst{ds} and \inst{end}} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{assembler!directive|(}% \index{pseudo-operand|(}% An \emph{assembler directive} is sometimes called a \emph{pseudo-opcode}, since it appears in the opcode (or mnemonic) column of an assembly language program, but it is not a real assembly language instruction---it does not generate any machine code. An assembler directive tells the assembler what to do. Each assembler has its own set of assembler directives; here we look at the most important: \inst{org}, \inst{dc}, \inst{ds} and \inst{end}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{The \inst{org} directive} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:org} The \inst{org} assembler directive is used with the syntax: \begin{prog} org \meta{address} \end{prog} The \inst{org} assembler directive tells the assembler: ``whatever comes next, put it starting at address \meta{address}''. For example, the \inst{org} statement in this part of an assembly language program causes the \inst{move} instruction to be assembled at address 400400\hex; in other words, the machine language instruction for \inst{move d0,d1} will be located in memory at the absolute address 400400\hex. \begin{prog} org $400400 move.w d0,d1 \end{prog} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{The \inst{dc}, \inst{ds} directives} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:dcDs} \index{variables!defining|(}% \index{dc@\inst{dc}|(}% \index{ds@\inst{ds}|(}% The \inst{dc} (``define constant data'') and \inst{ds} (``define storage'') directives both reserve space in the computer's memory, usually to store data for variables. The difference is that \inst{dc} provides an initial value, while \inst{ds} does not. The syntax is: \begin{prog} dc.\meta{size} \meta{initial value} ds.\meta{size} \meta{number of data items} \end{prog} The ``\inst{.}\meta{size}'' can be any of \inst{.B}, \inst{.W} or \inst{.L}\, or can be omitted altogether, which is the same as using ``\inst{.W\/}''. \inst{dc.w}. Here is an example of each: \begin{prog} dc.L 1234 Reserve one long word; initialise with 1234\dec ds.b 10 Reserve 10 bytes of memory. \end{prog} See section~\vref{sec:table} for more examples of the use of \inst{dc} and \inst{ds}. \index{variables!defining|)} \index{dc@\inst{dc}|)}% \index{ds@\inst{ds}|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{The \inst{end} directive} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:end} Every assembly language program needs to be terminated with the \inst{end} directive. Like all assembler directives, this does not tell the microprocessor what to do, only the assembler. We use the \inst{trap \#11} instruction followed by \inst{dc.w 0} to tell the microprocessor to terminate our assembly language programs. See section~\vref{sec:trap} for more about the \inst{trap} instruction. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Equates} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:equates} \emph{Equates} give a particular constant value to a label. The values of \inst{leds}, \inst{sevSeg} and \inst{switches} are set using \emph{equates}. If you are familiar with Pascal, an equate is like a Pascal constant defined with \texttt{const}. For those familiar with the C programming language, an equate is similar to a constant defined with \textsf{\textbf{\#define}}. Here is an example of some equates, showing the values given to these names: \begin{prog} keys equ $10 Port A data register switches equ $18 Port C data register sevSeg equ $12 Port B data register leds equ $12 Port B data register \end{prog} So \texttt{keys} has the value \$10, \texttt{leds} and \texttt{sevSeg} both have the value \$12, and the name \texttt{switches} has the value \$10. These values are the \emph{offsets} from the \emph{base address} of the 68230 parallel chip. See section~\vref{sec:68230} for more about the 68230 parallel interface/\allowbreak{}timer chip. Also, see page 11-2 of the \FLIGHT manual for a list of all the registers in the \PIT and their addresses on the \FLIGHT computer. \index{assembler!directive|)}% \index{pseudo-operand|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Addressing modes} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The MC68000 has 14 \emph{addressing modes}. In this section we look at these addressing modes and see some examples of their use. An \emph{addressing mode}\index{addressing mode} is a way of specifying the \emph{effective address}\index{effective address} of an operand, either in a register or in memory. The effective address tells us where the operand is. There are many addressing modes because there are many different ways of storing data, and addressing modes are designed for accessing data that is stored using these different methods. Chapter 7 of the \FLIGHT manual, \cite{flight}, discusses addressing modes. Also see chapter~5, section~5.4 of Horvath the text book \cite{horvath}, or pages 39--48 of Antonakos \cite{antonakos}. Particularly useful as an assembly language reference for the 68000 is \cite{motoProgRef}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Register direct addressing modes} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{register direct addressing mode|(}% \index{addressing mode!register direct|(}% This mode is the fastest and simplest addressing mode: the data is in a data register or in an address register. If the effective address is a data register, Motorola call this ``Data register direct mode'' in \cite{motoProgRef}. If the data is in an address register, Motorola call this ``address register direct mode.'' Here is an example of both data register direct and address register direct addressing modes: \begin{prog} move.w a0,d0 Each operand uses a register direct mode \end{prog} \begin{exercise} If this addressing mode is the fastest and simplest, why do we not use it all the time? \end{exercise} \index{register direct addressing mode|)}% \index{addressing mode!register direct|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Absolute addressing mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{absolute addressing mode|(}% \index{addressing mode!absolute|(}% The first lab used the absolute addressing mode often. It is also one of the simplest addressing modes: the assembly language instruction gives the address of the operand. However, it is quite slow, as the effective address is contained in the machine language instruction, and so it takes longer for the \CPU to fetch the instruction. Two examples are shown here: \begin{prog} org $400500 value dc.w org prog move value,d0 value is accessed using absolute addressing move $400500,d0 value is accessed using absolute addressing \end{prog} Note that the two \inst{move} instructions produce \emph{exactly} the same machine code. The label \inst{value} is equivalent to its address, 400500\hex. See pages 118 and 119, section~5.4.1 of the text book, \cite{horvath}, or see page 44 of \cite{antonakos}, under ``absolute short address'' and ``absolute long address.'' See also the \FLIGHT manual on page 7--9 under the same headings. Note that the assembler decides itself whether to use absolute long or absolute short direct addressing; there is no need to specify a ``\texttt{.L}'' or ``\texttt{.W}\,'' as shown in the manual, \cite{flight}. \index{absolute addressing mode!disadvantage}% \index{addressing mode!absolute!disadvantage}% Another disadvantage of the absolute addressing modes is that the effective address is \emph{fixed} when the program is assembled or compiled; the program cannot change the effective address after the program starts running. This is a disadvantage when working with data in \emph{tables}. Section~\vref{sec:table} discusses tables. \index{absolute addressing mode|)}% \index{addressing mode!absolute|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Immediate addressing mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:immediate} \index{immediate addressing mode|(}% \index{addressing mode!immediate|(}% The first lab also used the \emph{immediate} addressing mode. The value to be loaded is included with the instruction. The mode is indicated with a \# in front of the number: \begin{prog} move #1,d0 Put the actual value 1 into D0. \end{prog} See page 7--12 of the \FLIGHT manual \cite{flight}, or page 119 of the text book \cite{horvath} for some more information. \index{immediate addressing mode|)}% \index{addressing mode!immediate|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Address register indirect mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:ari} \index{address register indirect addressing mode|(}% \index{addressing mode!address register indirect (\ARI)|(}% The Address register indirect addressing mode overcomes two of the problems with the absolute addressing mode: the instruction executes faster and the effective address can be changed after the program starts to run. An address register holds the effective address. We say that the address register \emph{points} to the data. The address register must be loaded with the effective address first, of course. This is particularly useful for processing data in a loop: the loop can use one instruction, but the effective address contained in the address register can be changed at each step, so a whole table of data can be processed with one \inst{add} instruction! This example adds a table of numbers and puts the result into the D0 register. It loads the address of the start of the table into the A0 address register. Then we have a loop, in which we add the longwords together into D0, then increment the effective address to point to the next longword in the table, and then check whether we are at the end of the table yet. If we are not at the end, we continue the loop. At the end, D0 contains the sum of all the numbers in the table. The label \inst{tableEnd} is made equal to the address of whatever comes immediately after the end of the table. \begin{prog} table dc.L 1234,5678,91011,1213,1415,161718,22 tableEnd * ... move.L #table,a0 load the effective address into A0 loop add.L (a0),d0 The source uses address register indirect addq #4,a0 Increment effective address cmpa.L #tableEnd,a0 Are we at the end of the table? bne loop If not, keep adding more numbers. \end{prog} \index{address register indirect addressing mode|)}% \index{addressing mode!address register indirect (\ARI)|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Address register indirect with postincrement mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:postinc} \index{address register indirect addressing mode with postincrement|(}% \index{addressing mode!address register indirect with postincrement|(}% Notice that in the example above, the address register is incremented by 4 with an \inst{addq} instruction. It would be nice if the \CPU could do this incrementing for us automatically. Well, it can. This is the same example using the address register with postincrement mode. \begin{prog} table dc.L 1234,5678,91011,1213,1415,161718,22 tableEnd * ... move.L #table,a0 load the effective address into A0 loop add.L (a0)+,d0 The source uses postincrementing mode cmpa.L #tableEnd,a0 Are we at the end of the table? bne loop If not, keep adding more numbers. \end{prog} This addressing mode uses the effective address from the address register \emph{first}, \emph{then} increments the address register by the size of the data. If the \inst{add} instruction above were: \begin{prog} loop add.w (a0)+,d0 The source uses postincrementing mode \end{prog} then the A0 register would be incremented by \emph{two}, the size of a \emph{word} (specified by the ``\inst{.W}\,''). The address register indirect with postincrement is very useful for the \emph{pop} stack% \index{stack}% \index{stack!pop} operation. Section~\vref{sec:pop} describes the pop operation and the stack. \index{address register indirect addressing mode with postincrement|)}% \index{addressing mode!address register indirect with postincrement|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Address register indirect with predecrement mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:predec} \index{address register indirect addressing mode with predecrement|(}% \index{addressing mode!address register indirect with predecrement|(}% The predecrement mode does a subtraction from the effective address \emph{before} the effective address is used. The subtraction is the size of the data, as for the postincrement mode. This addressing mode is very useful for the \emph{push} operation. Examples are given of its use in section~\vref{sec:push}. \index{address register indirect addressing mode with predecrement|)}% \index{addressing mode!address register indirect with predecrement|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Address register indirect with displacement addressing mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:ariWDisp} \index{address register indirect with displacement addressing mode|(}% \index{addressing mode!address register indirect with displacement|(}% The \emph{address register indirect with displacement} mode may be new to you. We use this mode to access the registers of the parallel chip. First, the address register A6 is loaded with the base address of the parallel chip. The offset of the register from the base address is then added to the base address to give the address of the actual register. Here is an example: \begin{prog} move $18(a6),d0 Load data from address given by * adding 18\(\sb{16}\) to the contents of A6. \end{prog} Note that in your lab work, you will use the constants \inst{leds}, \inst{switches} and \inst{sevSeg} with this addressing mode. These constants have been defined in the file \texttt{flight.i}. See section~\vref{sec:macros} for more about the file \texttt{flight.i}. Here is another example, using the name \texttt{leds} defined in the file \texttt{flight.i}: \begin{prog} move d0,leds(a6) Copy data from D0 to address given by * adding leds to the contents of A6. \end{prog} The value of \texttt{leds} is \$12 --- see section~\vref{sec:equates} and section~\vref{sec:68230}. The \texttt{Init} macro loaded the base address of the parallel chip (\$800\,001) into A6\e. The effective address of ``\texttt{leds(a6)}'' is then $\$800\,001 + \$12 = \$800\,013$, which is the address of the port~B data register in the 68230 parallel chip. See table~\vref{tab:portAddr} and also table~\vref{tab:registerAddr} for lists of the registers and their addresses in the \PIT. See pages 7--7 and 7--8 of the \FLIGHT manual \cite{flight}, pages 112 and 113 of the text book \cite{horvath} or pages 42--43 of \cite{antonakos} for some more information about this addressing mode. \index{address register indirect with displacement addressing mode|)}% \index{addressing mode!address register indirect with displacement|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Address register indirect with index addressing mode} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:ariWithIndex} \index{address register indirect with index addressing mode|(}% \index{addressing mode!address register indirect with index|(}% The \emph{register indirect with index} addressing mode may also be new to you. In lab~3 we use it for looking up a pattern in a table to copy to the seven segment display. See page 7--8, section 7.6.2.5 of the \FLIGHT manual for more information. Here is an example of its use: \begin{prog} move $18(a0,d1),d0 \end{prog} Here, the hexadecimal value \$18 and the contents of D1 are added to the contents of the register A0 to give the memory address from which the data is \texttt{move}d. How would we use such an addressing mode? An example is looking up to the contents of the register A0 to give the memory address from which the data is \texttt{move}d. How would we use such an addressing mode? An example is looking up a number in a table. Suppose we have a table of bytes, which we could declare as: \verb|table dc.b 1,2,4,8,$10,$20,$40,$80| Now we could load the address of this table, which is given by the label \texttt{table}, into the address register A0: \verb| movea.L #table,a0 Immediate addressing mode| Note that that suffix is an \texttt{.L}, not a \texttt{1}! See section~\vref{sec:suffix}. Now suppose we want the fourth entry in this table. We could put the value 3 into D1 and then use the instruction: \verb| move.b 0(a0,d1),d0 Load value 8 from table into D0| Why did we put \textbf{3} into D0 to access the \emph{fourth} entry in the table? Suppose \verb|#table| has the value \$400500. This means that the first element of the table has the address \$400500, the second has address \$400501, the third \$400502 and the \emph{fourth} has address \$400503. Now the way the addressing mode works is that the number 0 is added to what is in A0, \$400500, and also to what is in D1, 3, to get the address \$400503, which is the correct address. So to access the first item in the table, we would put 0 into D1, and to access the last of the eight items in the table, we would load 7 into D1\e. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % In lab~4, we will use this addressing mode for the last program in % section~\ref{enl4s:sec:lastProg}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% See page 7--8 of the \FLIGHT manual, pages 122 and 123 of the text book \cite{horvath}, or page 43 of \cite{antonakos} for some more information. \index{address register indirect with index addressing mode|)}% \index{addressing mode!address register indirect with index|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Tables in MC68000 assembly language} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:table} \index{tables|(}% \index{lookup tables|(}% \index{arrays|(}% As we use the word here, a \emph{table} is a collection of numbers that are put one after the other in the computer's memory. We use an \emph{index} to access individual numbers in the table. Another word for such a table is \emph{array}. If you have used Pascal or C, you may have seen an array before. In MC68000 assembly language, an array is declared with the \texttt{dc}, declare constant data command, or with the \texttt{ds}, declare storage command. These assembler directives were introduced in section~\vref{sec:dcDs}. Here are examples of each: \begin{prog} org $400500 data starts at this address pattn dc.b $ff,$7f,$3f,$1f,$f,7,3,1 An array of 8 patterns values ds.w 5 An array of 5 words, not initialised. \end{prog} If there are initial values the entries in the table should have, then we use \texttt{dc}. If there are no special values the entries should be initialised to, or if the table is very big, we can use \texttt{ds}. We can access the entries in the table using the \emph{register indirect with index} addressing mode. See section~\vref{sec:ariWithIndex}. \index{tables|)}% \index{lookup tables|)}% \index{arrays|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Summary of 68000 addressing modes} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{addressing modes|(} See table~\ref{tab:addrReg} for examples of the use of each addressing mode. \begin{table}[tbh] \centering \begin{tabular}{@{}l>{\small\ttfamily}l@{}} \toprule Addressing mode & \normalfont Example \\ \midrule implied & rts \\ data register direct & move d0,d1 \\ address register direct & move a0,d1 \\ immediate & move \#\$1234,d1 \\ absolute short & move \$1234,d1 \\ absolute long & move \$400500,d1 \\ address register indirect (\acro{ARI}) & move (a0),d1 \\ \ARI with postincrement & move (a0)+,d1 \\ \ARI with predecrement & move -(a0),d1 \\ \ARI with index and displacement & move \$12(a0,d0),d1 \\ \ARI with displacement & move \$1234(a0),d1 \\ PC with displacement & move \$1234(pc),d1 \\ PC with index and displacement & move \$12(pc,d0),d1 \\ short relative & bra.s \ alpha \\ \bottomrule \end{tabular} \caption{Examples of each addressing mode.} \label{tab:addrReg} \end{table} \index{addressing modes|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Some assembly language instructions} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Loops and decisions: the \texttt{B$_\mathtt{CC}$} instructions} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:loops} Computers are good at doing repetitive jobs: the loop construction is ideal for implementing such repetitive work. We use two basic kinds of looping operations: conditional and unconditional. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Unconditional loops: the \inst{bra} instruction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:bra} An \ix{unconditional loop} just keeps going forever---or at least, till we press the reset button or turn the computer off. We may implement such loops using the \texttt{bra} instruction (branch always). Every time we execute the \texttt{bra} instruction, the \ix{program counter}% \index{registers!program counter} register is loaded with the address given by the label. The program will then execute the instruction with that label. Your lab sheets have many examples of the \texttt{bra} instruction. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Conditional loops: the \inst{bra} instruction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:conditional loops} A \ix{conditional loop} can be more clever. It can stop looping when a condition is met. We often use a \texttt{cmp} (compare) instruction to set the flags in the status register, then we use a \Bcc instruction such as \texttt{beq} to check those flags. The \Bcc instruction will choose whether to jump (or `branch') depending on the value of the status flags. These flags are the \textsc{\small Zero}, \textsc{\small Negative}, \textsc{\small Carry}, \textsc{\small oVerflow} and \textsc{\small eXtend} flags. See section \vref{sec:statusReg} for more about the status flags. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{The \texttt{cmp} instruction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:cmp} The \inst{cmp} instruction works the same as the \inst{sub} instruction, except that the result is thrown away. The syntax of the \inst{cmp} instruction is \hspace*{1cm} \texttt{cmp}\textsf{.s} \hspace{1cm} \meta{ea},\texttt{D}\Vf{n} \textsf{.s} is one of \texttt{.B}, \texttt{.W} or \texttt{.L},\\ \meta{ea} is an effective address given by any addressing mode, specifying the source \meta{src}, and \\ \texttt{D}\Vf{n} is any data register, specifying the destination \meta{dest}. The \CPU does the subtraction \meta{dest}~$-$~\meta{src}, throws the result away, but sets the flags in the status register. See section~\vref{sec:statusReg} for more about the flags. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Simple tests of the flags} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:bccSimple} \index{Bcc@\Bcc|(} \index{instruction!\Bcc|(} \index{instruction!conditional branch|(} \index{conditional branch|(} The \Bcc instructions in table~\vref{tab:simpleTests} are simple tests for a single flag in the status register being set (1) or clear (0). \begin{table} \centering \begin{tabular}{@{}l>{\tt}c>{\tt}c>{\tt}c>{\tt}c@{}} \toprule \bfseries flag & N & Z & V & C \\ \midrule branch if flag set & BMI & BEQ & BVS & BCS \\ branch if flag clear & BPL & BNE & BVC & BCC \\ \bottomrule \end{tabular} \caption{The simple tests of status register flags.} \label{tab:simpleTests} \end{table} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Testing the result of comparisons} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:bccCompare} The \Bcc instructions in table~\vref{tab:comparisonTests} are often applied after a subtraction. This subtraction is usually done with a \inst{CMP} instruction. A subtract or compare instruction subtracts the source $s$ from the destination $d$. Here $d$ means `destination' while $s$ means `source', so the subtraction is $d - s$. The \inst{TST} instruction is a special comparison that subtracts zero from $d$. Notice that some of the instructions in table~\ref{tab:comparisonTests} are repeated from table~\ref{tab:simpleTests}, because it is only necessary to test one status bit for some comparisons. The branch is taken if the result of the comparison is true. \begin{table} \centering \begin{tabular}{@{}>{\bf}l>{\tt}c>{\tt}c>{\tt}c>{\tt}c>{\tt}c>{\tt}c@{}} \toprule comparison & $d < s$ & $d \le s$ & $d = s$ & $d \neq s$ & $d \ge s$ & $d > s$ \\ \midrule signed & BLT & BLE & BEQ & BNE & BGE & BGT \\ unsigned & BCS & BLS & BEQ & BNE & BCC & BHI \\ \bottomrule \end{tabular} \caption{The number comparison tests.} \label{tab:comparisonTests} \end{table} \vspace{1ex} Table~\vref{tab:allTests} gives the full name for each mnemonic. \begin{table} \centering \setlength{\extrarowheight}{2pt} \mathversion{mathtt} \newcommand{\n}[1]{\overline{#1}} % \n for `not'. \begin{tabular}{@{}>{\tt}cl>{$}c<{$}@{}} \toprule \bf Mnemonic & \multicolumn{1}{c}{\bfseries Full name} & \multicolumn{1}{c}{\bfseries flags} \\ \midrule BCC & branch if carry clear & C \\ BCS & branch if carry set & \n{C} \\ BEQ & branch if equal & Z \\ BNE & branch if not equal & \n{Z} \\ BMI & branch if minus & N \\ BPL & branch if plus & \n{Z} \\ BVS & branch if overflow set & V \\ BVC & branch if overflow clear & \n{V} \\ \hline BGT & branch if greater than (signed) & N \cdot V \cdot Z + \n{N} \cdot \n{V} \cdot \n{Z} \\ BHI & branch if higher than (unsigned) & \n{C} \cdot \n{Z} \\ BGE & branch if greater than or equal to (signed) & N \cdot V + \n{N} \cdot \n{V} \\ BLE & branch if less than or equal to (signed) & Z + N \cdot \n{V} + \n{N} \cdot V \\ BLS & branch if less than or same as (unsigned) & C + V \\ BLT & branch if less than (signed) & N \cdot \n{V} + \n{N} \cdot V \\ \bottomrule \end{tabular} \caption{All the \protect\Bcc instructions (all tests).} \label{tab:allTests} \end{table} \index{Bcc@\Bcc|)} \index{instruction!\Bcc|)} \index{instruction!conditional branch|)} \index{conditional branch|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Loops: the \texttt{dbf} instruction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:dbf} \index{instruction!\inst{dbf}|(}% Another useful kind of looping instruction is the \DBcc family of instructions. A very useful member of this family is the \texttt{dbf} or \textsc{\small Decrement and Branch while False} instruction. The \texttt{dbf} instruction takes two operands: a data register, and a label. This instruction does the following: \begin{itemize} \item The value in the data register is decremented by 1. \item If the data register is not $-1$, then the program will branch to the label. \item If the data register $= -1$, then the program will execute the instruction following the \texttt{dbf} instruction. \end{itemize} The \texttt{dbf} instruction can be used like a \Kf{for} loop in Pascal or C, with a fixed number of times through a loop. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %There is an example of the use of \texttt{dbf} in %section~\vref{enl4s:sec:dbfProg} in lab~4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{instruction!\inst{dbf}|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{What if you find an instruction you do not know?} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% There are some other instructions introduced in your lab work that you may not have seen before. There are several places to look. The back of the text book (Horvath, \cite{horvath}), and also Antonakos, \cite{antonakos}, has details of each 68000 instruction. The \FLIGHT manual \cite{flight} explains each instruction in chapter 8. There is some online help with the assembler: to use this, at a \acro{DOS} prompt, type \texttt{asmhelp}% \index{asmhelp@\texttt{asmhelp}}% \index{assembler!on line help} \meta{Enter}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Subroutines} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:subroutines} \index{subroutine|(} A \emph{subroutine} is a specialised program module that may be called upon from anywhere else in the program outside of the normal sequence of execution. Most programs contain subroutines. Subroutines are often called \emph{functions} or \emph{procedures}, especially in higher level languages. We will talk more about functions later. \begin{figure}[hbt] \centering \input{coathang.pic} \caption{Executing a subroutine} \label{fig:coathanger} \end{figure} Figure~\vref{fig:coathanger} shows how a subroutine fits into the normal flow of execution of a program. At point $a$ there is a \emph{call} to the subroutine. This causes the next instruction in the execution of the program to be at point $x$. At $y$---the end of the subroutine---there is a \emph{return} instruction, which causes execution to continue at the next instruction after point $a$. Later, at point $b$, there is another call to the same subroutine, so again the processor executes instructions between $x$ and $y$. But this time, execution returns to the instruction immediately after point $b$. \begin{figure}[hbt] \centering \input{nested.pic} \caption{Executing nested subroutines} \label{fig:nested} \end{figure} Subroutines can also call other subroutines---see figure~\ref{fig:nested}. Here, the first subroutine is executed as in figure~\vref{fig:coathanger}, but when execution reaches instruction $c$, a call is made to a nested subroutine. Execution then starts at instruction $u$, the first instruction in the nested subroutine. At the end of the nested subroutine, a return instruction is executed at $v$, which causes execution to continue at the instruction immediately after instruction $c$. When the first subroutine finishes, the return instruction at $y$ will cause execution to continue at the correct location---the instruction immediately after $a$ or $b$, depending on where the call was made from. You will see that in this particular example, the nested subroutine is executed twice, as is the first subroutine. Notice that when the subroutine finishes, it must return to just after where it was called from. This is different from the way a normal branch instruction works. A special \emph{data structure} is required to hold the address of the instruction immediately after the calling instruction. A \ix{data structure} is a software method for storing data. A data structure has storage areas and software to perform operations on the data.% \index{subroutine|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The stack} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:stack} \index{stack|(} We could hold the return address of the subroutine in a single fixed location, but this would not allow one subroutine to call another, since each call would overwrite the previous value. A \emph{stack} data structure% \index{stack!data structure}% \index{data structure!stack} is generally chosen to hold these return addresses. The stack is usually in \RAM. As mentioned in lectures, the stack% \index{stack!definition} is a \textbf{l}ast \textbf{i}n, \textbf{f}irst \textbf{o}ut (\LIFO) structure. A stack has two operations% \index{stack!operations}% : \begin{emDescrip} \item[push] The push operation adds a data item to the stack. \item[pop] The pop operation removes the most recent value pushed (that has not already been popped). Some texts call this a \emph{pull} operation. \end{emDescrip} \textscl{Lifo} means that the value that is removed with a \emph{pop} operation is the value that was most recently pushed onto the stack. The memory address that holds the last value that was pushed is called the \emph{top of the stack}% \index{stack!top of}% \index{top of the stack}% . Each memory location, as we have discussed earlier, holds one byte and has a unique number called an \emph{address}. A special register called the stack pointer% \index{stack pointer}% \index{registers!stack pointer}% ---A7 in the 68000---holds the address of the top of the stack% \index{stack!top of}% \index{top of the stack}% . On some other microprocessors the stack pointer% \index{stack pointer}% \index{registers!stack pointer} points to the location where the \emph{next} data item \emph{will} be pushed, but on both the 68000 and 8086 \up families, the stack pointer points to the address of the \emph{last} data item \emph{already} pushed onto the stack. \index{stack|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The push operation} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:push} \index{stack!push|(}% \index{push|(}% The push operation has two steps. When a data item is pushed onto the stack, \begin{enumerate} \item the stack pointer% \index{stack pointer}% \index{registers!stack pointer} is decremented by the size of the data item (in bytes) \item the data value is copied into the location whose address is now in the stack pointer% \index{stack pointer}% \index{registers!stack pointer} register. \end{enumerate} Note that you can only push and pop data values with an \emph{even} number of bytes. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{An example of a push operation} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% In the 68000, there are a few instructions to push data onto the stack. When using the \texttt{move} instruction, the push operation uses the \emph{address register indirect with predecrement} addressing mode, described briefly in section~\vref{sec:predec}. As an example, the assembly language instruction to push a 32-bit longword value from the D0 register onto the stack is: \begin{prog} move.L d0,-(a7) ; Remember A7 is the stack pointer. \end{prog} Suppose before this instruction was executed, D0 contained 12345678\hex and A7 contained 4000\hex. Then the before and after pictures of this are shown in figure~\vref{fig:push}. Note that D0 is not changed by the operation, but four memory locations (in the computer's \RAM) and the stack pointer% \index{stack pointer}% \index{registers!stack pointer}% , A7 in the \CPU, do change. \begin{figure} \centering \input{stackpus.pic} \caption{Before and after pushing 12345678\hex onto the stack.} \label{fig:push} \end{figure} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Using the \inst{movem} instruction to push many registers onto the stack} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{instruction!\inst{movem}|(}% \index{movem@\inst{movem}|(}% \index{stack!push!many registers|(} Many registers can be pushed at once with the \texttt{movem} instruction. This example shows the 32-bit long word values in the registers D1, D2, D3, D4 and A0 all being pushed onto the stack: \begin{prog} movem.L d1-d4/a0,-(a7) Push all five registers onto the stack. \end{prog} Note here that the dash `\texttt{-}' between two registers means a range of registers: \texttt{d1-d4} means registers D1, D2, D3 and D4\e. To specify registers that are not part of a range, separate them with slashes, as shown above. There is an example program in section~\vref{sec:simTut} using a \inst{movem} instruction. Some of the programs you use in the lab sessions use the \texttt{movem} instruction at the beginning and at the end. This is to make them \emph{transparent}. See section~\vref{sec:transparency} for more about transparency.% \index{instruction!\inst{movem}|)}% \index{movem@\inst{movem}|)}% \index{stack!push!many registers|)} \index{stack!push|)}% \index{push|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The pop operation} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:pop} \index{stack!pop|(}% \index{pop|(}% The pop operation is the reverse of the push operation. When a value is popped from the stack: \begin{enumerate} \item the data value is copied from the top of the stack into another location, either a register or a memory location. \item the stack pointer% \index{stack pointer}% \index{registers!stack pointer} is incremented---by the size of the data item that was removed, in bytes---to point to the new top of stack. \end{enumerate} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{An example of a pop operation} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Several instructions in the 68000 pop data from the stack. When using the \texttt{move} instruction, the pop operation uses the \emph{address register indirect with postincrement} addressing mode, described in section~\vref{sec:postinc}. As an example, the assembly language instruction to pop a 32-bit long word from the stack into the D0 register is: \begin{prog} move.L (a7)+,d0 ; Pop operation. \end{prog} Suppose that before this instruction executes that A7 contains the value 3FFC\hex\e. In this example, the ``\inst{.L}'' means ``long word'', (4 bytes) so the operation first copies 4 bytes from memory locations 3FFC\hex, 3FFD\hex, 3FFE\hex and 3FFF\hex into the 32-bit D0 register. Then the A7 register is incremented by 4 so that it finally has the value 4000\hex. See figure~\vref{fig:pop}. In this way, a push operation followed by a pop operation will leave the stack as it was before the push. \begin{figure} \centering \input{stackpop.pic} \caption{Before and after popping the stack into the D0 register.} \label{fig:pop} \end{figure} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Using the \inst{movem} instruction to pop the stack into many registers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{instruction!\inst{movem}|(}% \index{movem@\inst{movem}|(}% \index{stack!pop!into many registers|(}% Many registers can be popped at once with the \texttt{movem} instruction. This example shows 5 32-bit long word values being popped from the stack into registers D1, D2, D3, D4 and A0: \begin{prog} movem.L (a7)+,d1-d4/a0 Pop all five registers from the stack. \end{prog}% \index{instruction!\inst{movem}|)}% \index{movem@\inst{movem}|)}% \index{stack!pop!into many registers|)}% \index{stack!pop|)}% \index{pop|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Preserving registers: transparency} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:transparency} \index{transparency|(}% \index{subroutine!and transparency|(}% \index{macro!and transparency|(}% Very often we use registers to hold variables in programs. There are many advantages to this: registers are quickly accessible---they are already inside the \CPU. A program that keeps its variables all in registers will run faster than if it keeps many variables in memory. \index{registers!limited number in \CPU} There is one problem here---there is a limited number of registers. The 68000 has more registers than many \up, but we can still run out. We may need to program other \up{}s with fewer registers. We may want to use a register for more than one variable. Another problem is \emph{local variables} in subroutines. The problem is that one subroutine may need five registers, and may call other subroutines that also need a few registers. Suppose the second subroutine changes a register that the first subroutine was using? Another problem is with \emph{interrupts}. A hardware \ix{interrupt} can take place at unpredictable times. The program may be interrupted between any two instructions. When the \CPU handles the interrupt, it executes a special type of subroutine called an \emph{interrupt service routine}, or \ISR. The \ISR must not change any registers used by the program, or the program will stop working properly. A solution to these problems is to make the subroutines ``transparent''. We make a subroutine \emph{transparent} by saving all the registers that it uses. Where do we save them? The system stack is a very convenient place. The subroutine can push all the registers it uses onto the stack as the very first action it does, then, at the very end, just before the \texttt{rts} instruction, it pops the the same registers in exactly the opposite order. A convenient instruction to do this is the \texttt{movem} instruction, which will push or pop as many registers as you want, always taking care of doing these operations in the correct order. Please see the sections \ref{sec:push} and \vref{sec:pop} for more information about these operations. Every \ISR \emph{must} be transparent to the program that it interrupts, or the \CPU may ``hang,'' or the program may give the wrong results. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % All the subroutines used in lab~4 are as `transparent' as % possible. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Most of the macros used in the lab also begin with push and end with pop operations. \index{transparency|)}% \index{subroutine!and transparency|)}% \index{macro!and transparency|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Subroutine calls, parameters and return values} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:parameters} Sometimes a subroutine does not need any information to do its job; if \texttt{Init} were a subroutine, it would need no extra information---it would just initialise the parallel chip. (The \inst{Init} macro is described in section~\vref{sec:initMacro}.) However, some other subroutines \emph{do} need to pass some information in or out to be useful. For example, the \texttt{ReadKey} subroutine %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % in section~\ref{enl4s:sec:readKeySub} of lab~4 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% needs to let us know what the key was that it read. It does this by putting the key index into the D0 register. We say that it \emph{returns} the key index in D0, or that the key index is its \emph{return value}. Similarly, the \texttt{GetKey} subroutine %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % , also in section~\vref{enl4s:sec:readKeySub} of lab~4, %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% returns a key index in register D0\e. \index{subroutine!parameters!register}% Information may be passed to a macro or subroutine through registers. An example is the \texttt{Transl}\index{Transl macro@\texttt{Transl} macro}\index{macro!\texttt{Transl}} macro, described in section~\vref{sec:translMacro}. \texttt{Transl} takes a key index in register D0 and returns a number, also in D0, that corresponds to the value on the top of the key. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Subroutine calls and the stack} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:subStack} \index{subroutine!call}% When a subroutine call is executed, the processor saves the return address on the top of the stack. When the most recent subroutine call finishes, the last stored return address is retrieved from the top of the stack. In this way, subroutines that call other subroutines (nested subroutines, as in figure~\vref{fig:nested}) will also return to the correct location, since the first subroutine call will be the last one to return. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{bsr} and \texttt{jsr} instructions} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% In the 68000, the call operation is implemented by either the \texttt{bsr}% \index{instruction!\inst{bsr}}% \index{bsr@\inst{bsr} instruction} instruction or the \texttt{jsr}% \index{instruction!\inst{jsr}} \index{jsr@\inst{jsr} instruction} instruction Both do the same thing, except that \texttt{bsr} can only use relative addressing modes, whereas \texttt{jsr} can use many addressing modes. To see the difference, refer to the \FLIGHT manual, chapter 8, or the text book. Here we are interested in what these instructions do with the stack. Suppose a subroutine is called \texttt{sub1}. Then a call to this subroutine with either \texttt{jsr sub1} or \texttt{bsr sub1} will: \begin{enumerate} \item push the address of the instruction immediately following the call instruction onto the stack. \item load the address of the first instruction in the subroutine into PC. \end{enumerate} The first step saves the return address of the subroutine on the stack. Because the return address is a long word (4 bytes), the push operation will decrement the stack pointer% \index{stack pointer}% \index{registers!stack pointer} by 4 before copying the return address to the stack. The second step causes execution of the instructions in the subroutine to begin. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{rts} instruction} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{rts@\inst{rts}|(}% \index{instruction!\inst{rts}|(}% Every subroutine must end with the \texttt{rts} (\textbf{r}e\textbf{t}urn from \textbf{s}ubroutine) instruction. When the \texttt{rts} instruction executes, it simply pops the long word (32-bit) value from the top of the stack into PC (the program counter register). The effect is to cause program execution to continue from the instruction immediately after the instruction that called the subroutine. That is, \begin{enumerate} \item the four bytes at the top of the stack are copied into the \ix{program counter}% \index{registers!program counter} \item the stack pointer% \index{stack pointer}% \index{registers!stack pointer} is incremented by 4 \end{enumerate} Hence, the \texttt{rts} instruction is simply a pop operation. \index{rts@\inst{rts}|)}% \index{instruction!\inst{rts}|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The subroutine must leave the stack as it found it} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{subroutine!and the stack}% \index{stack!and subroutines}% An important requirement is that when execution reaches the \texttt{rts} instruction, the stack \emph{must} be exactly the same as it was immediately after the subroutine was called. If it is not, then something else will be popped into the PC, and the computer is likely to `hang'. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{An example showing a subroutine call} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:subCall} \index{subroutine!call|(} \begin{figure}[htb] \centering \input{stackcal.pic} \caption{The stack before and after the call to subroutine \texttt{sub1}.} \label{fig:stackCall} \end{figure} A subroutine \inst{sub1} at address 400450\hex is called. The instruction \begin{prog} jsr sub1 \end{prog} is at location 400410\hex. Figure~\vref{fig:stackCall} shows the arrangement of the stack before and after the call. \begin{itemize} \item Before the call, the stack pointer% \index{stack pointer}% \index{registers!stack pointer} A7 is pointing to the top of the stack, which just happens to be at address 4003F0\hex. \end{itemize} Here are the three steps performed by the \CPU when it executes the call instruction: \begin{itemize} \item The stack pointer% \index{stack pointer}% \index{registers!stack pointer} is decremented by four, since an address occupies 4 bytes \item The return address, 400416\hex is copied from the program counter onto the top of the stack \item The address of the subroutine, 400450\hex is copied into the program counter \end{itemize} \begin{itemize} \item After the call, the value 400416\hex has been pushed onto the stack, since the \inst{jsr sub1} instruction at address 400410\hex is 6 bytes long. $400416\hex = 400410\hex + 6$. 400416\hex is the address of the instruction immediately after the \inst{jsr} instruction. When the \CPU fetched the \inst{jsr sub1} instruction, it automatically incremented the \ix{program counter}% \index{registers!program counter} to point to the next instruction. The \CPU pushed this incremented value of the program counter. The address of the top of the stack is always held in the stack pointer% \index{stack pointer}% \index{registers!stack pointer}% , A7\e. \item Because the \ix{program counter}% \index{registers!program counter} holds the address of the first instruction of the subroutine, the \CPU executes instructions in the subroutine. \end{itemize} \index{subroutine!call|)} \begin{comment} A subroutine \texttt{sub1} at address 400450\hex is called. The instruction \begin{prog} jsr sub1 \end{prog} is at location 400410\hex. \begin{itemize} \item Before the call, the stack pointer% \index{stack pointer}% \index{registers!stack pointer} A7 is pointing to the top of the stack, which just happens to be at address 4003F0\hex. \item After the call, the value 400416\hex is pushed onto the stack, since the \texttt{jsr sub1} instruction at address 400410\hex and is 6 bytes long. $400416\hex = 400410\hex + 6$. 400416\hex is the address of the instruction immediately after the \texttt{jsr} instruction. \item The stack pointer% \index{stack pointer}% \index{registers!stack pointer} is decremented by four, since an address occupies 4 bytes, then the return address, 400416\hex is pushed onto the stack. \item The new value at the top of the stack is the return address, 400416\hex. The address of the top of the stack is always held in the stack pointer% \index{stack pointer}% \index{registers!stack pointer}% , A7\e. \end{itemize} \end{comment} \begin{comment} Before the call, the stack pointer% \index{stack pointer}% \index{registers!stack pointer} A7 is pointing to the top of the stack, at address 4003F0\hex. After the call, the value 400416\hex is pushed onto the stack, since the \texttt{jsr sub1} instruction is 6 bytes long. 400416\hex is the address of the instruction immediately after the \texttt{jsr} instruction. The stack pointer% \index{stack pointer}% \index{registers!stack pointer} is decremented by four, since an address occupies 4 bytes, then the return address, 400416\hex is pushed onto the stack. The new value at the top of the stack is the return address, 400416\hex. The address of the top of the stack is always held in the stack pointer% \index{stack pointer}% \index{registers!stack pointer}% , A7\e. \end{comment} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The include file \texttt{flight.i}: using macros} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:macros} \index{macros|(}% You will notice that the programs in labs~3 and~4 all have the line \verb| include flight.i| Also, the first instruction in each program is \verb| Init Initialise the parallel chip| The first instruction opens a file, \texttt{flight.i}, which contains the definition of the names \inst{leds}, \inst{sevSeg} and \inst{switches}. The file also contains the definition of several \emph{macros} called \texttt{Init}, \texttt{Delay}, \texttt{WriteSs}, \texttt{Transl}, \texttt{ShowNum}, \texttt{WNKeyPr} and \texttt{WKeyPr}. You may look at the file \texttt{flight.i} if you like. A \emph{macro} is a sequence of instructions that has a name and that has a single idea or algorithm. A macro can be `called' from anywhere in the program. A macro is rather like a subroutine. The difference from a subroutine is that each time you `call' a macro, the instructions are inserted directly into that location by the assembler. If you call a macro ten times, the code for the macro will be inserted ten times in your program. On the other hand, if you call a subroutine ten times, the code for that subroutine will still only appear once in the program. A subroutine must finish with a \texttt{rts} instruction, while a macro does not. A macro may take one or more \emph{parameters}, such as the \texttt{Delay} macro, described in section~\vref{sec:delayMacro}. The other macros here take no parameters, but some may use information provided in register D0, and some provide information in register D0\e. These macros have been written so that they disturb the rest of your program as little as possible. They do this by pushing all registers that they use (except D0 in most cases, and A6) onto the stack, and then popping the registers back from the stack afterwards. \eemph{I have included these macros to make the lab work quicker for you to do.} To call a macro, simply type the name of the macro in the opcode field of your program, as shown in the programs in your lab sheet. \eemph{It is necessary to type the same case as shown, as this assembler is case sensitive with the names of macros.} Hence it is necessary to type \texttt{Init} and not \texttt{init}, \texttt{WriteSs} and not \texttt{writess}, and so on.% \index{macros|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{Init} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:initMacro} \index{parallel chip!initialising|(}% \index{sixty eight two thirty@68230!initialising|(}% \index{Init@\texttt{Init} macro|(}% \index{macro!\texttt{Init}|(}% The macro \texttt{Init} initialises the 68230 parallel chip, and also loads its base address into the A6 register for use with the address register indirect with displacement addressing mode---see pages 122 to 124 of the text book, Horvath \cite{horvath}, or p.~42--43 of Antonakos \cite{antonakos}, or p. 7-7 and 7-8 of the \emph{\FLIGHTsl user manual} for more about this addressing mode. \texttt{Init} sets the port connected to the \LED{}s as outputs, the port connected to the \ix{dip-switches} as inputs, and also sets the \ix{keypad} port up correctly, with port bits 0, 1 and 2 as outputs, and the other five bits as inputs. This is discussed in some detail in section~\vref{sec:ioports}, and particularly in section~\vref{sec:initPitForBitIO}. The \texttt{Init} macro contains six instructions. If you single-step through your program, you need to step over these first six instructions. \index{Init@\texttt{Init} macro|)}% \index{macro!\texttt{Init}|)}% \index{parallel chip!initialising|)}% \index{sixty eight two thirty@68230!initialising|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{Delay} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:delayMacro} \index{Delay macro@\texttt{Delay} macro|(}% \index{macro!\texttt{Delay}|(}% The \texttt{Delay} macro takes one parameter, as mentioned above. The parameter is a counting number, and is provided after the name \texttt{Delay}, like this: \begin{prog} Delay 20 Delay 20 milliseconds. \end{prog} \texttt{Delay} preserves all the registers it uses. In other words, all registers will have the same value before and after calling \texttt{Delay}. \texttt{Delay} will simply keep the \CPU busy counting until the number of milliseconds specified by the parameter has passed. \index{Delay macro@\texttt{Delay} macro|)}% \index{macro!\texttt{Delay}|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{WriteSs} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:writeSsMacro} \index{WriteSs macro@\texttt{WriteSs} macro|(}% \index{macro!\texttt{WriteSs}|(}% The \texttt{WriteSs} macro (write to seven segment display) takes a number in register D0 and uses a lookup table to display the value on the seven segment display, as in the last lab session. Only the four right-most bits are used; other bits in D0 are cleared. All other registers are not affected. \index{WriteSs macro@\texttt{WriteSs} macro|)}% \index{macro!\texttt{WriteSs}|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{Transl} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:translMacro} \index{Transl macro@\texttt{Transl} macro|(}% \index{macro!\texttt{Transl}|(}% The \texttt{Transl} macro (translate from a key index to key value) is used to interpret the result of \texttt{ReadKey}. ReadKey gives a number in the range 0 to 11, with the following values: \begin{center} \begin{tabular}{@{}lcccccccccccc@{}} \toprule Key index: & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 \\ Value printed on the key: & 1 & 4 & 7 & $*$ & 2 & 5 & 8 & 0 & 3 & 6 & 9 & \# \\ \bottomrule \end{tabular} \end{center} Here, the ``Key index'' is the index returned by \texttt{ReadKey}. The ``Value printed on the key'' is what you see when you look at the telephone keypad yourself. \texttt{Transl} uses a lookup table to take the key index as input (in D0) and provides the actual key value as output (also in D0). For `$*$' it returns \texttt{\$A}, and for `\#' it returns \texttt{\$B}, since the idea is to display the result on the seven segment display, which cannot display a `$*$' or a `\#'. \index{Transl macro@\texttt{Transl} macro|)}% \index{macro!\texttt{Transl}|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{ShowNum} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:showNumMacro} \index{ShowNum@\texttt{ShowNum} macro|(}% \index{macro!\texttt{ShowNum}|(}% The \texttt{ShowNum} macro takes a value in register D0 and displays the value as a four-digit decimal number on the terminal screen. It does this by serial transfer, using monitor firmware routines, in the ROM on the \FLIGHT board. All other registers are preserved besides D0\e. \index{ShowNum@\texttt{ShowNum} macro|)}% \index{macro!\texttt{ShowNum}|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{WNKeyPr} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:wNKeyPrMacro} \index{WNKeyPr@\texttt{WNKeyPr} macro|(}% \index{macro!\texttt{WNKeyPr}|(}% The \texttt{WNKeyPr} macro simply waits till a key is \emph{not} pressed on the keypad. All registers are preserved. It is very similar to the macro \texttt{WKeyPr}. \index{WNKeyPr@\texttt{WNKeyPr} macro|)}% \index{macro!\texttt{WNKeyPr}|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The \texttt{WKeyPr} macro} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:wKeyPrMacro} \index{WKeyPr macro@\texttt{WKeyPr} macro|(}% \index{macro!\texttt{WKeyPr}|(}% The \texttt{WKeyPr} macro simply waits till a key \emph{is} pressed on the keypad. All registers are preserved. It uses the algorithm described in section~\vref{sec:keyPressed}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % You will work %with a program to use this algorithm in section~\vref{enl4s:sec:readKey} %in lab~4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{WKeyPr macro@\texttt{WKeyPr} macro|)}% \index{macro!\texttt{WKeyPr}|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The \texttt{trap} instruction and monitor routines} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:trap} \index{software interrupts|(}% \index{interrupts!software|(}% \index{trap instruction@\texttt{trap} instruction|(}% \index{instruction!\texttt{trap}|(}% Almost every program here ends with a pair of lines that look like this: \begin{prog} trap #11 dc.w 0 \end{prog} The \texttt{trap} instruction is used here to call a \emph{monitor} routine.\index{monitor routine}\index{firmware!monitor routine} The monitor is a collection of programs that are in the EPROM on the \FLIGHT boards. These are mentioned briefly on page~\pageref{pag:monitor}. The number on the line with the \texttt{dc.w} directive indicates which of these monitor programs you want to call. There are 28 of these routines, and they are listed on page 5--3 of the \FLIGHT manual. Three of them are used for the \texttt{ShowNum} macro---see section~\vref{sec:showNumMacro} for more about this macro. This method of calling a monitor firmware routine is called a \emph{software interrupt}. Each program here terminates with a call to the \textsf{renter} routine, which terminates the program and enters the monitor routine that listens for your keystroke commands, such as when you type \meta{l}\meta{t} to begin a download, or \meta{g}\meta{o} to start a program running. \index{trap instruction@\texttt{trap} instruction|)}% \index{instruction!\texttt{trap}|)} \index{software interrupts|)}% \index{interrupts!software|)}% % \clearemptydoublepage %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Input and output} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:i/o} \index{port!\IO|(} \index{IO ports@\IO ports|(} \emph{Input}\index{input} is the transfer of data from outside the computer to inside the computer. \emph{Output}\index{output} is the transfer of data from the computer to the outside world. \begin{itemize} \item \textbf{All input and output takes place through \emph{I/O ports}}. \item An \IO port is a special ``memory'' location \item When the \up reads from an input port, data is transferred from the outside world to the \up \item When the \up writes to an output port, data is transferred from the \up to the outside world. \item An example of an output port is the output port connected to the \LED{}s and the seven segment display. This is port~B data register on the \PIT; we called this \inst{leds} and \inst{sevSeg} in our programs. \item An example of an input port is the input port connected to the dip-switches. This is the port~A data register on the \PIT. We called it \inst{switches} in our programs. \item In our assembly language programs, to do \emph{output}, we move the data \emph{to} the port, i.e., the port is the \emph{destination}, e.g., \begin{prog} move.b #$c8,leds(a6) \end{prog} \item To do \emph{input} in our programs, we put the input port as the \emph{source} of an instruction, e.g.,\label{sec:switches(a6)} \begin{prog} add.b switches(a6),d0 \end{prog} \end{itemize} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{I/O ports and the 68230 parallel interface/\allowbreak{}timer chip} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:ioports} An \IOsl \emph{port} is a special memory location in an input or output hardware interface. An \IO port is a connection between a computer and the outside world. For instance, the keyboard, printer and mouse on a desktop computer all are connected to the \PC through \IO ports. When we read from an input port, we cause input from something outside the computer. Data is transferred from outside to inside the computer through the input port. An input is a special sort of read operation. When we write to an output port, we cause data to be transferred from the computer to hardware that is outside the computer. An output is a special sort of write operation. In a memory-mapped \IO computer, such as the \FLIGHT in our labs, we use ordinary data movement instructions to do input and output, such as \texttt{move} or \texttt{clr}. An input or output port has an ordinary address, like memory. The only difference is that an address decoder selects an \IO chip such as the 68230 \PIT instead of a memory chip. The \IO chip is connected to external hardware so that it may perform input and output. The special memory locations in the 68230 are called registers---they are all listed in table~\vref{tab:registerAddr}. You may read some more about them in the \FLIGHT manual, in chapter 11. You may wish to look at the memory map of the \FLIGHT board, in figure~\vref{fig:memmap}. Here are some examples of input and output operations, as in our lab work: \begin{prog} move.b #$f,leds(a6) OUTPUT: Turn on the four \LED{}s on the right. move.b switches(a6),d0 INPUT from the dip switches into D0. clr.b keys(a6) OUTPUT zero to the keypad (port A) \end{prog} The first operation outputs the bit pattern 0000\,1111 to port~B, which is connected to the \LED{}s. This output operation is performed by writing to the port~B data register, which is at address \$800013\hex---see tables~\vref{tab:portAddr} and \vref{tab:registerAddr}. The second operation inputs from port~C\index{port!C}, which is connected to the dip switches. This input operation is performed by reading the port~C data register, which is at address \$800019\hex---see tables~\vref{tab:portAddr} and \vref{tab:registerAddr}. The third operation sends the data value 0 out of the port~A\index{port!A} data register, which is at the address 800011\hex. The names \texttt{leds}, \texttt{switches} and \texttt{keys} are named constants, defined in the file \texttt{flight.i}. See section~\vref{sec:equates} for more about these names.. \index{port!\IO|)} \index{IO ports@\IO ports|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The interface card} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% On the 68000 microprocessor \IO interface card\footnote{I will call the 68000 microprocessor \IO \ix{interface card} the \emph{interface card} from here on.} there are four devices, two for \emph{input} and two for \emph{output}. The input devices are a bank of eight \emph{dip switches}\index{dip-switches} and a telephone \emph{keypad}. The output devices are a set of eight \emph{\LED{}s} (Light Emitting Diodes) and a \emph{seven segment display}.\index{LEDs@\LED{}s}\index{seven-segment display} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The 68230 parallel interface/\allowbreak{}timer chip} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:68230} All these devices are connected to the 68230 parallel interface and timer (\PIT) chip via a 40 wire \emph{ribbon cable}. The \PIT has three \emph{ports}. On this chip, a port is a set of eight pins. Each of these pins can be individually set up as an input or as an output. This is more flexible than the Intel 8255A parallel chip, in which the pins in one port cannot be assigned as inputs and outputs in any order. The \ix{ports} are called port~A, port~B and port~C\@. Some of the pins in port~C\index{port!C} can be used also as inputs and outputs for the timer, and some others as control signals for transfers to and from ports A and B\@. We use port~C as an input port since it is connected to the dip switches. The 68230 \PIT on the \FLIGHT has been given a \emph{base address}\index{base address}\index{parallel interface/\allowbreak{}timer!base address} of \$800\,001. The base address is determined by the design of the \ix{address decoder} for the \PIT. The port~A data register has offset \$10 from the base address of the \PIT, the port~B data register has offset \$12 from the base address, while port~C\index{port!C} has offset \$18 from the base address. This means that port~A is at address $\$800\,001 + \$10 = \$800\,011$, while the port~B data register is at address $\$800\,001 + \$12 = \$800\,013$, while the port~C\index{port!C} data register is at address $\$800\,001 + \$18 = \$800\,019$. See section~\vref{sec:equates} for how these constants are defined. See table~\vref{tab:portAddr} for a list of the port addresses. \begin{table}[tbh] \centering \begin{tabular}{@{}llll@{}} \toprule \multicolumn{1}{c}{\textbf{register}} & \textbf{address} & \textbf{offset} & \textbf{name} \\ \midrule Port A data register & 800011\hex & 10\hex & \texttt{keys} \\ Port C data register & 800019\hex & 18\hex & \texttt{switches} \\ Port B data register & 800013\hex & 12\hex & \texttt{sevSeg} or \texttt{leds} \\ \bottomrule \end{tabular} \caption{The port addresses in the 68230 \PIT and their names} \label{tab:portAddr} \end{table} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Register addresses in the 68230 \PIT on the \FLIGHT} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{parallel interface/\allowbreak{}timer!registers|(}% \index{registers!PIT@registers!\PIT|(}% A 68230 \PIT has 23 separate registers. These occupy 64 memory locations. Individual bits in these registers can be set to control the behaviour of the IC\e. Three of them are the data registers used to communicate with the keyboard, dip-switches and \LED{}s in our lab work. Table~\vref{tab:registerAddr} gives the addresses of the registers in the 68230 on the \FLIGHT. \begin{table} \centering \begin{tabularx}{\linewidth}{@{}l>{\ttfamily}llcC@{}} \toprule \multicolumn{1}{c}{register} & \multicolumn{1}{c}{% \makebox[4em][c]{\normalfont\makebox[0.8\width][r]{abbreviation}}% } & address & offset & name \\ \midrule Port general control register & PGCR & 800001 & 0 & \\ Port service request register & PSRR & 800003 & 2 & \\ Port A data direction register & PADDR & 800005 & 4 & \\ Port B data direction register & PBDDR & 800007 & 6 & \\ Port C data direction register & PCDDR & 800009 & 8 & \\ Port interrupt vector register & PIVR & 80000B & A & \\ Port A control register & PACR & 80000D & C & \\ Port B control register & PBCR & 80000F & E & \\ Port A data register & PADR & 800011 & 10 & \texttt{keys} \\ Port B data register & PBDR & 800013 & 12 & \texttt{sevSeg} or \texttt{leds} \\ Port A alternate register & PAAR & 800015 & 14 & \\ Port B alternate register & PBAR & 800017 & 16 & \\ Port C data register & PCDR & 800019 & 18 & \texttt{switches} \\ Port status register & PSR & 80001B & 1A & \\ Timer control register & TCR & 800021 & 20 & \\ Timer interrupt vector register & TIVR & 800023 & 22 & \\ Counter preload register, high & CPRH & 800027 & 26 & \\ Counter preload register, middle & CPRM & 800029 & 28 & \\ Counter preload register, low & CPRL & 80002B & 2A & \\ Count register, high & CNTRH & 80002F & 2E & \\ Count register, middle & CNTRM & 800031 & 30 & \\ Count register, low & CNTRL & 800033 & 32 & \\ Timer status register & TSR & 800035 & 34 & \\ \bottomrule \end{tabularx} \caption{The addresses of registers in the 68230 \PIT on the \FLIGHT. All addresses and offsets are in hexadecimal.} \label{tab:registerAddr} \end{table} \index{parallel interface/\allowbreak{}timer!registers|)}% \index{registers!PIT@registers!\PIT|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \paragraph{The \PIT has many modes:} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The \PIT is a general purpose parallel and timer chip, and can be set up to work in many different ways. Each of these methods of operation is called a \emph{mode}. There are diagrams showing these modes on pages 1-4 and 1-5 of the data book, \cite{moto68230}. Some ways that the parallel ports can be set up include support for \emph{handshaking} and \emph{interrupts}. We will study these topics later in the course. There are many options within each mode. See pages 3-7 and 3-8 of the data book for a list of all the options. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \paragraph{Submode 1X} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:submode1x} \index{parallel interface/\allowbreak{}timer!submode 1x|(}% \index{submode 1x|(}% \index{parallel interface/\allowbreak{}timer!initialising for bit \IO|(}% We have used the \PIT in its simplest mode, where each pin can work as an input or as an output. The Motorola data sheets call this \emph{submode 1X}\e.\footnote{The name `submode 1X' comes from the fact that there are two bits used to determine the submode. The `1X' means that the first bit is a ``don't care'' while the second is a `1'.} Submode~1X is described on page~3-2 of the data book as ``Bit \IO (Pin-Definable Single-Buffered Output or Non-Latched Input).'' On page~3-6 Motorola says: \begin{quote} This submode is intended for applications in which several independent devices must be controlled or monitored. [\ldots] Data read from the data register is the instantaneous value of the pin or what was written to the data register, depending on the contents of the data direction register. \end{quote} In section~\vref{sec:ddr} we will examine the function of the data direction registers. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Initialising the \PIT for bit \IO} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:initPitForBitIO} \index{parallel chip!initialising|(}% \index{sixty eight two thirty@68230!initialising|(}% Before we use the \PIT, it needs to be set up in the right mode. We do this by writing particular values to some of the registers in the \PIT. How do we know what values to write there? We look in the data book, \cite{moto68230}. Examine pages 3-7 and 3-8 of the data book. I will not explain all the options: we will learn about them later. I will just give you a table (see table~\vref{tab:submode1x}) showing what we want to use in our lab work. \begin{table}[tbh] \begin{tabularx}{\linewidth}{@{}>{\ttfamily}ccccY@{}} \toprule \normalfont\textbf{register} & \multicolumn{3}{c}{\bfseries bits} & \multicolumn{1}{c}{\bfseries option} \\ \midrule PACR & & 7 & 6 & \textbf{Port A submode} \\ & & 1 & X & Submode 1X \\ \hline PACR & 5 & 4 & 3 & \textbf{H2 control}\\ & 0 & X & X & Input pin---edge sensitive status input, H2S is set on an asserted edge \\ \hline PACR & & & 2 & \textbf{H2 SVCRQ enable} \\ & & & 0 & The H2 interrupt is disabled \\ \hline PACR & & & 1 & \textbf{H1 SVCRQ enable} \\ & & & 0 & The H1 interrupt is disabled \\ \hline PACR & & & 0 & \textbf{H1 Status control} \\ & & & X & H1 is an edge-sensitive status input, H1S is set by an asserted edge of H1\\ \bottomrule \end{tabularx} \caption{The values to be written into individual bits of the port~A control register (\inst{PACR}) and the port~B control register (\inst{PBDR}) to put these ports into submode~1X.} \label{tab:submode1x} \end{table} \vspace{2ex} The arrangement for port~B is the same as for port~A, except that instead of \inst{PACR} we are writing to the register \inst{PBCR}, and instead of the handshaking lines H1 and H2, port~B works with the handshaking lines H3 and H4\e. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \paragraph{What does it all mean?} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Table~\vref{tab:submode1x} above gives values required to be written to various bits of the \inst{PACR} register to make the \PIT behave the way we want. The `X's are ``don't cares''. If the constant \inst{submode\_1X} contains the correct value, we can initialise port~A with the instruction \begin{prog} move.b #submode_1X,PACR(a6) \end{prog} \begin{exercise} What value should we write to the port~A control register to put port~A into submode~1X\e? Give your value in binary and in hexadecimal. \end{exercise} We also want to put port~B into submode~1X. \begin{exercise} What value do we need to write to port~B data register to put port~B into submode~1X? \end{exercise} What about port~C\e? Port~C\index{port!C} is also multi-purpose, but it shares its function with the timer (the `T' in \PIT stands for \textbf{t}imer), and also with interrupt functions. When port~C is used just as an ordinary parallel port, it behaves like port~A and port~B do when they are in submode~1X\e. There is no need to do anything special to put port~C\index{port!C} into submode~1X. \index{parallel interface/\allowbreak{}timer!submode 1x|)}% \index{submode 1x|)}% \index{parallel interface/\allowbreak{}timer!initialising for bit \IO|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Choosing inputs and outputs: the data direction registers} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:ddr} \index{registers!data direction in \PIT|(}% \index{data direction register|(}% The 68230 \PIT is flexible: you can choose whether individual pins are inputs or outputs. How can you select which pins are inputs and which pins are outputs? The answer is that you write values to the \emph{data direction registers} in the \PIT. There are three data direction registers, one for port~A (\inst{PADDR}), one for port~B (\inst{PBDDR}) and one for port~C (\inst{PCDDR}). If you write a `zero' to bit~0 of the port~A data direction register (\inst{PADDR}), then bit~0 of port~A will be an input. If you put a `one' there instead, it will be an output. Individual pins can be assigned as inputs or outputs in any order you like. \begin{exercise} If you want bits~0, 1, 2, and 3 to be inputs and bits 4, 5, 6 and 7 to be outputs of port~B, what values should you write to what register? \end{exercise} \index{registers!data direction in \PIT|)}% \index{data direction register|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{Initialising the \PIT for bit \IO: a summary} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The steps in initialising the \PIT are as follows: \begin{enumerate} \item Put port~A and port~B into submode~1X by writing the values shown in table~\vref{tab:submode1x} to the registers \inst{PACR} and \inst{PBCR}. \item Select which pins you want to be inputs and which pins you want to be outputs \item For each pin that you want to be an output, put a one into the corresponding bit-position in the \ix{data direction register}. Similarly, put a zero for each pin you want to be an input. \item That's it! You can now read from the input pins and write to the outputs. \end{enumerate} \index{parallel chip!initialising|)}% \index{sixty eight two thirty@68230!initialising|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{How the interface card is connected to the parallel chip} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{jumper!selecting \LED{}s or seven-segment}% \index{selecting \LED{}s or seven-segment}% \begin{figure}[tbh] \centering \input{jump.pic} \caption{The two settings for the jumper on the interface board} \label{fig:jump} \end{figure} The \ix{keypad} is connected to port~A\index{port!A} while the \LED{}s and seven segment display are both connected to port~B\index{port!B}\e. The \LED{}s and \ix{seven-segment display} are selected by \ix{jumper}; please see figure~\ref{fig:jump}. When the jumper is in the upper position across pins 1 and 2, the \LED{}s are selected. When the jumper is across pins 2 and 3, the seven segment display is selected. The dip switches are connected to port~C\index{port!C}\e. When you read from the port~C data register, you read an 8-bit number with one bit for each of the 8 switches in the dip switch. When you write to the port~B data register, the bottom eight bits cause the corresponding \LED to turn on and off, or cause the corresponding segment on the seven-segment display to turn on and off. In both cases, a bit value of `1' means ``on'', a `0' means ``off''. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The keypad} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{keypad|(}% On the interface board, the keypad is connected to port~A of the 68230 parallel interface/\allowbreak{}timer IC\e. \begin{figure}[tbh] \centering \input{keypad.pic} \caption{The connection of the keypad to the 68230 parallel interface/\allowbreak{}timer chip} \label{fig:keypad} \end{figure} The keypad is connected rather like figure~\vref{fig:keypad}. There is a key in the twelve positions where the vertical wires are shown crossing the horizontal ones. Each key connects the crossing wires when pressed. The columns are set up as outputs while the rows are set up as inputs. When no key is pressed, the row inputs are high. \index{keypad|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Scanning the keypad} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{keypad!scanning|(}% There are twelve keys on the keypad but we only have eight pins in one port. If we connected the key switches directly to twelve port pins, we could read the keys directly. But this will not work with a larger keypad or keyboard. Also port pins are too precious to waste---if possible, we would prefer to save them for another purpose. If we have fewer port pins available, it is still possible to read the keypad by \emph{scanning} it. To scan a $3 \times 4$ keypad, we use $3 + 4$ port pins. We can use $n + m$ port pins to scan a $n \times m$ keypad\footnote{The method shown here is not the \emph{only} way to scan a keypad---it is possible to scan $\frac{1}{2} n(n -1)$ keys with $n$ port pins. For instance, it is possible to read 28 keys with 8 port pins---if you are interested, ask the author.}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsubsection{The method} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:readKeyExplain} The idea is that we output a `1' to two of the column outputs to the keypad while outputting a `0' to each column in turn. After the left-most column is set low, we can read each row in turn. Say the key `4' on the keypad has been pressed (see figure~\vref{fig:keypad}). If we read the rows (\ie port inputs PA$_3$ to PA$_6$), then bits PA$_3$, PA$_5$ and PA$_6$ are all `1's, but PA$_4$ is a zero, because the key has connected the line PA$_4$ to PA$_0$ on which we have output a `0'. The resistance between PA$_0$ and ground is much lower than the value of the resistor between PA$_4$ and $\text{V}_{\text{CC}}$, so PA$_4$ will have a voltage close to zero volts. Let's look at the steps in scanning the keypad, from a high level point of view. \begin{itemize} \item For each column: \begin{itemize} \item drive that column low and the others high. \item Read the rows. \item For each row \begin{itemize} \item If that row bit is `0', then have found the key---exit. \item increment the key index \end{itemize} \end{itemize} \item If no key was found, then set key index to \texttt{noKey} \item The key index now has a number from 0 to 11 if a key was pressed, or a value of \texttt{noKey} if no key was pressed. \end{itemize} Let's now look at the steps in greater detail. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %It may help to read %the program in section~\vref{enl4s:sec:readKey} in lab~4, %\emph{Scanning a keypad in MC68000 assembly language}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{nameList}{step 1xx} \item[step 1] Start by initialising the key index number that indicates the current key, the column counter and the column pattern that we will output to the keypad columns \item[step 2] for each column, starting from PA$_0$, drive that column low while driving the other columns high \item[step 3] Read the pattern of bits from the rows, \ie PA$_3$ to PA$_6$. To do this, we read the 8 bits from the keypad port into a register, then \emph{rotate} the 8 bits three times, so that the bit from the top row is in bit position 0, \ie in the LSB (least significant bit) position. \item[step 4] for each row: \begin{nameList}{step 1xxx} \item[step 4a] Rotate the LSB into the carry flag in the \CPU status register. Look up how the \texttt{ror} and \texttt{rol} instructions work in the \FLIGHT manual, your text book or any 68000 reference book. \item[step 4b] check whether the carry flag is low. If it is, then the current key has been pressed. \item[step 4c] if the current key has not been pressed, then increment the key index number and try the next row---go to step 4a. \end{nameList} \item[step 5] Roll the bit pattern for the columns one position left so that we are ready to drive the next column low and the other columns high. \item[step 6] If we have read all the columns, then no key was pressed, so put a special value into the key index register to show that no key has been pressed. \item[step 7] We now have the key number. Use this as an index to look up the value of the key in a lookup table. \end{nameList} This list of steps is called a \emph{task list}. We can also express the algorithm in \emph{pseudocode}\index{pseudocode}. Pseudocode is a mixture of English (or you can use Cantonese if you like!) and some programming language. Here I have combined English with Pascal, since you may have studied Pascal: \label{pag:readkey}% \begin{prog} Algorithm ReadKey: key = 0, col = 2, columnPattern = $FE while col <> -1 do row = 3 keypad = columnPattern rowPattern = keypad roll port A bit 3 into rightmost bit position of rowPattern while row <> -1 do roll LSB of rowPattern into CPU carry flag if carry flag is clear goto found key := key + 1 row := row - 1 roll columnPattern left one position col := col - 1 key := noKey found: \end{prog} I hope that these two ways of showing the algorithm will help you to understand the subroutine \texttt{ReadKey}.% \index{keypad!scanning|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %, first introduced in your %lab work in section~\vref{enl4s:sec:readKey} in lab~4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Debouncing the keys} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:debouncing} \index{keypad!debouncing|(}% \index{debouncing a keypad|(}% However, there is more to it than this. We need to deal with a problem called \emph{key bounce}. When any switch opens or closes, it does not do so at only one moment. A closing switch will bounce open again for a very short time, close again, bounce apart again, and so on. This lasts about 10 milliseconds, depending on the type of switch. We need to be able to tell when a key has been pressed once and is bouncing or if it has been pressed more than once. Small amounts of noise can also be interpreted as keystrokes when there are none. There are many solutions to this problem, some using hardware such as a flip-flop or Schmidt trigger, others using software delay loops. We will take the software approach. If a key has been pressed, we can wait a while and see if it is still pressed some ten or twenty milliseconds later. If it is still seen as pressed, we shall say that this is a genuine keystroke. We can use the \texttt{Delay} macro to create a delay of 20 milliseconds: \begin{prog} Delay 20 Delay 20 milliseconds. \end{prog} Please read section~\vref{sec:delayMacro} for more about the \texttt{Delay} macro. \index{keypad!debouncing|)}% \index{debouncing a keypad|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{How to detect if any key has been pressed} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:keyPressed} \index{keypad!detecting key press|(}% To see how to scan a keypad we look here at the first part of the problem: detecting a keystroke. How can we do this? One simple way is to output a low to all the columns (\ie to bits 0, 1 and 2 of the keypad port, port~A). How do we write the pattern which has bits 0, 1 and 2 low? This is 0. To achieve this step, we can write: \begin{prog} clr.b keys(a6) output 0 to keypad columns \end{prog} Remember, to \emph{write} to a port register, we put it as the destination operand, \ie on the \emph{right} side of the \texttt{move} instruction. We can then read the keypad port. If any of the row bits (\ie bits 3 to 6) are low, then a key has probably been pressed. How can we do this? We can first input from the keypad into a register: \begin{prog} move.b keys(a6),d1 Register D1 contains the column bits \end{prog} Now how can we find out if any of the rows are low? Well, first we need to clear our copy of the bits that are not in the rows, \ie that are not bit 3, 4, 5 or 6. We use the \texttt{and} instruction to clear these bits. We can write: \begin{prog} andi.b #$78,d1 Clear all the bits that are not row bits \end{prog} Finally, we can compare this value read from the rows with \texttt{\$78}. We can tell if a key has been pressed by seeing if these values are the same or different. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % You will use this algorithm in your experimental work in % section~\vref{enl4s:sec:anyKeyPressedProg}, in lab~4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{exercise} What we can say about the number of keys pressed if the value read from the rows is equal to \texttt{\$78}?\label{qes:rowtest} \end{exercise} \index{keypad!detecting key press|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \appendix % \clearemptydoublepage %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Editing, assembling, downloading and running} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:genProc} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The program design} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The starting point is your \ix{program design}. I recommend writing the design in \emph{pseudocode}\index{pseudocode}. There is an example of the use of pseudocode on page~\pageref{pag:readkey}. Flow charts\index{flow charts} are also sometimes useful in working out complicated loops and decisions. Once you have the design, the rest is easy. This appendix is about how to assemble, download and run a program after you have designed it. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The general procedure for editing, assembling, downloading and running an assembly language program} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Remember: for each program, you will always use the same five steps: \begin{multicols}{2} \raggedcolumns \begin{thinItemise} \item edit \item assemble \item download \item run \item observe \end{thinItemise} \end{multicols} You have a number of choices for what programs to use for running an assembly language program on the \FLIGHT \up board. The method here uses \MSDOS. An alternative is to use \ix{Microsoft Windows}, and use \textsf{Terminal}\index{Terminal program@\textsf{Terminal} program} for the data transfer instead of the \texttt{comm.exe} program. \begin{enumerate} \item Before the \PC is turned on, be sure that you have an \ix{interface card} plugged into a \FLIGHT \up board, and that power is supplied to the \up board and that the \ix{serial connection} is made to the \PC. \item If you switch on the \PC, from the \MSDOS Startup Menu, select option 3, ``\texttt{Standalone Workstation}'' \item If you want to output to the \ix{seven segment display}, set the \ix{jumper} on the interface board to the seven-segment display position, as in figure~\vref{fig:jump}. If you are using the LED{}s\index{LEDs@\LED{}s}, set the jumper to the other position. \item Change to the \texttt{c:\bs 68x} directory by typing: \verb|cd \68x| \meta{Enter} \end{enumerate} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Editing the program} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:editing} \index{editing|(}% If your program uses any macros, please read about them in section~\vref{sec:macros}. Note that you \emph{must} type the macro names in the same case as shown in that section, or you will get an ``illegal opcode'' assembler error message. Here I assume that the program you are working with is called \meta{program}\texttt{.asm}. Just put the name of your own program instead of \meta{program}. I describe here how to use the same editor---Q-edit---as we did in previous labs, but you can also use \textsf{Emacs} or \texttt{edit}. \begin{enumerate} \item Edit a new program using Q-edit by typing \texttt{q }\meta{program}\texttt{.asm} \meta{Enter} \item Type in your program. \item Save this program and exit Q-edit by typing \meta{Alt-x} \end{enumerate} \index{editing|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Assembling the program} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:assembling} \index{assembling|(}% If the name of your assembly language program is \meta{program}\texttt{.asm} then you can assemble it by: \begin{enumerate} \item At the \verb|C:\68X>| prompt, type \texttt{xasm }\meta{program} \meta{Enter} \item In response to the \texttt{Object file: [}\meta{program}\texttt{.BIN] \_} prompt, press the \meta{Enter} key. \item In response to the prompt for a list file, you can either \begin{itemize} \item press \meta{Enter}, in which case the listing file will be displayed on the screen, or \item type \meta{program} \meta{Enter} in which case a file \meta{program}\texttt{.lst} will be created containing the assembler \emph{listing} output. You can examine this file with an editor if you like. \end{itemize} \item If the assembler reports \texttt{0 errors} and \texttt{0 warnings} then assembly was successful. Otherwise, write down the error messages. These show both the line number and the position in the line where the error was found. Edit the program again. The first error or warning is usually the best place to start. Examine that line and correct the error. Then assemble your program again until there are no errors or warnings. \end{enumerate} \index{assembling|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Downloading your program} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:downloading} \index{downloading|(}% Downloading a program is sending the assembler output from the \PC to the \FLIGHT board by a serial transfer. I assume here that the output from the assembler was the file \meta{program}\texttt{.bin}. If you are working with a different filename, use that name instead of \meta{program}. \begin{enumerate} \item Start the serial communications program at 9600 bits per second. We run the program at a baud rate of 9600 bits per second by typing: \texttt{comm /b9600} \begin{explanation} This program is also called a \emph{terminal} program, since the \PC behaves as a terminal for the \FLIGHT board. \end{explanation} \item Press the \meta{Enter} key three times until an \verb|F>| prompt appears. \begin{explanation} \index{monitor firmware!prompt}% \index{F prompt@\texttt{F\Gt} prompt!how to get}% If no \verb|F>| prompt appears, press the RESET button on the \FLIGHT board (not the one on the \PC!) then press \meta{Enter} three times. Call the lecturer or technician if you still get no \verb|F>| prompt. \end{explanation} \item Type \meta{l}\meta{t} \begin{explanation} There are some programs in the \FLIGHT \EPROM\e.\label{pag:monitor} These programs are called the \emph{monitor} programs. They are also called \emph{firmware} because they remain after the power is turned off. The command \meta{l}\meta{t} is a command to the monitor firmware to be ready to receive a serial stream of characters from the \PC. See also section~\vref{sec:trap} for more information about the firmware. \end{explanation} \item When prompted with: \verb|ENTER OFFSET ( IF NONE) : | press the \meta{Enter} key. \begin{explanation} \index{downloading!doesn't work}% Many people forget this little step! If you do, the transfer will not work, but all sorts of strange characters may appear on your screen, and perhaps the terminal will beep. \end{explanation} \item Now press the \meta{F2} key, and when prompted, enter the name of the program: \meta{program} \meta{Enter} \begin{explanation} This is a command to the terminal program to start sending characters from the file \meta{program}\texttt{.bin} down the serial cable. \end{explanation} \item The firmware will give a message \texttt{LOAD COMPLETED} indicating that the transfer was successful. \end{enumerate} \index{downloading|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Running the program} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{program!running|(}% \index{running a program|(}% \begin{enumerate} \item Type \meta{g}\meta{o} \item In response to the request for a starting address, type \eemph{the number that is the address of the first instruction in your program}. This is given by the \texttt{org} statement in your program that is put immediately before the first instruction. In labs~3 and~4, we will stay with 400400\hex. \item Observe what happens when the program runs. \item To exit from the \texttt{comm} terminal program press the \meta{Esc} key. \end{enumerate}% \index{program!running|)}% \index{running a program|)} % \clearemptydoublepage %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \chapter{Using the \simk simulator and \asmk assembler at home} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{chp:sim} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{What is a simulator?} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{simulator!what it is|(}% A simulator is software that can run on a computer like a \PC and which can run programs designed for another computer. The \simk simulator can run on any \PC and will behave as if you have a 68000 microprocessor board attached to the \PC! There is also an assembler that you can use, \asmk. This software is freely\index{simulator!\simk!freely distributable} distributable---you can take a copy home or to work, and edit, assemble and run 68000 assembly language programs without requiring a 68000 microprocessor system! This means that you can---and should---practice writing and running assembly language programs outside of your laboratory class. \index{simulator!what it is|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{The \simk simulator} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Here are the commands that the \simk simulator can use. For a full description of the simulator and assembler, see the book \cite{livadas}, \emph{Computer Organisation and the MC68000}, by Panos E. Livadas and Christopher Ward, Prentice-Hall, 1993\e. The authors of the book wrote the simulator and assembler and are making them freely available. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The Simulator} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The simulator is able to simulate the Motorola ``\ix{Educational Computer Board}'', or \index{ECB@\ECB}\ECB. The \ECB has a rather different \index{memory map}\ix{address map} from the \FLIGHT. Also the \ECB has similar \ix{firmware commands}, but they have different letters. These are listed below. The name of the simulator is \simk. The firmware in the \ECB is called \emph{Tutor}\index{Tutor}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{The Memory Map of the \ECB (and \simk)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{center} \begin{tabular}{@{}r@{--}lcl@{}} \toprule \multicolumn{2}{c}{Address range} & Device & Use \\ \midrule 000000 & 000007 & \ROM & Reset vector \\ 000008 & 0003FF & \RAM & Vector table \\ 000400 & 0008FF & \RAM & Tutor scratch pad \\ 000900 & 007FFF & \RAM & \textbf{USER MEMORY}\makebox[0pt][l]{\large{} $\pmb{\Longleftarrow}$} \\ 008000 & 00BFFF & \ROM & Tutor firmware \\ 00C000 & 00FFFF & & Unused \\ \bottomrule \end{tabular} \end{center} A 68230 \PIT and two 6850 \acro{UART}s are mapped above 00FFFF\hex, but the address space there is otherwise empty. \eemph{Note} that your program \eemph{must} be loaded into addresses \emph{lower} than \$7FFF, but higher than \$8FF. The stack is usually at the top of \RAM. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{\simk firmware commands} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% The \simk simulator is just software that runs on the \PC and of course contains no real firmware, but these are \ix{Tutor} firmware\index{firmware commands!Tutor} commands on the \ECB. ALL COMMANDS MUST BE ENTERED IN UPPER CASE LIKE THIS! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Getting started} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{tabularx}{\linewidth}{>{\bfseries}lY>{\ttfamily}l@{}} 1. & Assemble your program with the assembler, \asmk. & asm68k \meta{filename}.\meta{ext} \\ 2. & Load your program into the simulator: & LO \meta{filename}.s \\ 3. & Set the status register to \$0700: & .SR 0700 \\ 4. & Set the stack pointer% \index{stack pointer}% \index{registers!stack pointer} to \$7800: & .US 7800 \\ 5. & Single step (``Trace'') your program: & TR [\meta{address}] \\ & \quad OR & \\ & Execute your program: & GO [\meta{address}] \end{tabularx} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{List of firmware commands} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% A summary of the commands recognised by \simk is shown in table~\vref{tab:commands}. In that table, \texttt{[...]} means that \texttt{...} is optional. \begin{table} {\small \begin{tabularx}{\linewidth}{@{}>{\ttfamily}lY@{}} \toprule \normalfont Command & \multicolumn{1}{c}{Action} \\ \midrule DF & ``Display Formatted''---display the registers, like the \FLIGHT command \meta{r}\meta{d}.\\ MD \meta{address} & Memory dump \\ MD \meta{address} ;DI & Disassemble \\ EX & Exit the simulator \\ .\meta{register} & Display \meta{register}, e.g., \begin{tabular}[t]{@{}>{\ttfamily}l<{\space}@{}l@{}} .D0 & will display D0 \\ .A6 & will display A6 \\ .A7 & will display A7 \\ .SR & will display SR \\ .PC & will display PC. \end{tabular} \\ .\meta{register} \meta{data} & Modify \meta{register}, e.g., \begin{tabular}[t]{@{}>{\ttfamily}l<{\space}@{}l@{}} .D0 \meta{data} & will modify D0 \\ .A6 \meta{data} & will modify A6 \\ .A7 \meta{data} & will modify A7 \\ .SR \meta{data} & will modify SR \\ .PC \meta{data} & will modify PC. \end{tabular} \\ GO [\meta{address}] & Run program non-stop \\ TR [\meta{address}] & Single step \\ BR [\meta{address}] & Set/display breakpoints---up to a maximum of eight. \\ NOBR [\meta{address}] & Clear breakpoints \\ LO \meta{filename} & Load \meta{filename} into \simk. File contains Motorola S records. \\ ST \meta{filename} \meta{address} \meta{count} & load \meta{count} bytes starting at \meta{address} and store into \meta{filename}. \\ BF \meta{address1} \meta{address2} \meta{data} & Copy the hexadecimal word in \meta{data} to all addresses from \meta{address1} to \meta{address2}. \\ MM \meta{address} & Modify one byte of memory. Type . to terminate. \\ MM \meta{address};W & Modify one word of memory at a time. \\ \bottomrule \end{tabularx}% }% \caption{A list of commands recognised by \simk. These are compatible with the Motorola \ix{Tutor firmware commands}.} \label{tab:commands} \end{table} The firmware remembers the last command you entered. If you just type a carriage return at the prompt, the last command will be processed again. Disassembly works on one instruction the first time. If you press \meta{Enter} straight after, you get 16 instructions disassembled. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Monitor software interrupts} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{software interrupts|(}% \index{interrupts!software|(}% Like the \FLIGHT, the \ECB supports \ix{software interrupts}% \index{interrupts!software}. There is a difference in the way they are used: load a function number as a byte into the D7 register then execute the instruction \inst{trap \#14}. \begin{prog} move.b #\meta{function number},d7 trap #14 \end{prog} Here is a list of these \IO functions. The function numbers are given in decimal. \begin{tabularx}{\linewidth}{@{}llY@{}} \toprule \multicolumn{2}{c}{Function}& \\ \cmidrule(r){1-2} number & name & purpose \\ \midrule 241 & \inst{PORTIN1} & Read string from keyboard into A6, terminate with \CR \LF.\\ 247 & \inst{INCHE} & As for \inst{PORTIN1} but terminated with CR with no LF \\ 243 & \inst{OUTPUT} & Output string A6 points to to the screen \\ 227 & \inst{OUT1CR} & As \inst{OUTPUT} but also send \CR \LF. \\ 248 & \inst{OUTCH} & Byte in D0 is output to screen \\ 228 & \inst{EXIT} & Give termination message if Supervisor bit clear.\\ \bottomrule \end{tabularx} For more information see Appendix C of Livadas. \index{software interrupts|)}% \index{interrupts!software|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \subsection{Use the correct simulator and assembler} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{simulator!use right one|(}% Note that there are \emph{three} simulators in the \texttt{f:\bs sim68k} directory. Use \texttt{sim68k.exe}. There is one, \texttt{xsim68k}, that will run only under \UNIX. The other, \texttt{sim68.exe}, is a simulator for a simplified 68000. Similarly there are two assemblers: use \texttt{asm68k}. The other, \texttt{asm68}, is an assembler for the simplified simulator. \index{simulator!use right one|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{Using \asmk} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \index{asm68k@\asmk|(} \index{assembler!free|(} Syntax: \begin{alltt} asm68k \meta{filename} [-l[x]] [-r] \end{alltt} \begin{tabularx}{\linewidth}{@{}>{\ttfamily}lY@{}} -l & causes the assembler to produce a listing file.\\ -lx & produces a listing file containing a cross-reference table\\ -r & produces a relocatable object file that can be linked; output file will have the extension ``\texttt{.o}''. \end{tabularx} \index{asm68k@\asmk|)} \index{assembler!free|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{A Tutorial example using \asmk and \simk} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:simTut} \index{assembler!\asmk!tutorial|(}% \index{simulator!\simk!tutorial|(}% Follow the procedure: \begin{enumerate} \item At the \DOS prompt, type \texttt{edit ex1.asm} \meta{Enter} \item Type the following program: (note that it is the same as \texttt{ex1.asm} in Workshop~2 for part-time classes but the addresses \inst{data} and \inst{prog} were changed to match \simk). \begin{prog} data equ $1000 (note the addresses are different from the prog equ $1100 \FLIGHT{} board using in the laboratory) org data value1 dc.L $12345678 first value value2 dc.L $87654321 second value result ds.L 1 reserve long word storage org prog movem.L value1,d0/d1 load values to be compared cmp.L d0,d1 compare 32 bit values bhi store if value2 > value1 then goto store move.L d0,d1 else d1 = value1 store move.L d1,result store larger value nop (note: no trap #11 instruction) end \end{prog} \item Select File$\rightarrow$Exit from the editor menu and press \meta{Enter}. \item At the \DOS prompt, type \texttt{asm68k ex1.asm} \meta{Enter} \item type \texttt{sim68k} \meta{Enter} \item Press \meta{CapsLock} to switch to capital letters since the simulator \emph{only} accepts CAPITAL LETTERS! \item Type \texttt{LO ex1.s} \meta{Enter}\\ \texttt{MD 1100 13;DI} \meta{Enter} The screen will display the instructions of your program. \item \label{itm:seeMem}Type \texttt{MD 1000 10} \meta{Enter} Record the contents of the memory. \item Type \texttt{BR 1112} \meta{Enter} \\ \texttt{GO 1100} \meta{Enter} The program will execute from address 1100\hex and stop at 1112\hex. \item Type \texttt{MD 1000 10} \meta{Enter} Record the contents of the memory again and compare the result with (\ref{itm:seeMem}). \item Type \texttt{EX} \meta{Enter} The program will exit and return to \DOS. \end{enumerate}% \index{assembler!\asmk!tutorial|)}% \index{simulator!\simk!tutorial|)} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{How to obtain the simulator \simk and assembler \asmk} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \label{sec:simkObtain} \index{assembler!\asmk!obtaining|(}% \index{simulator!\simk!obtaining|(}% To obtain the simulator, go to the \MPA, room D226, and: \begin{itemize} \item Turn on one of the computers \item Select the ``Novell network'' option from the boot menu \item type: \texttt{f:} \meta{Enter} \item Type: \texttt{cd \bs sim68k} \meta{Enter} \item Put a formatted floppy disk into the 3\fracSl14'' drive \item Run the batch file \texttt{copyit.bat} by typing: \texttt{copyit} \meta{Enter} \item When disk activity stops, remove your floppy disk \item Take the floppy disk home \item Make a directory on your computer by typing \texttt{md \bs sim68k} \meta{Enter} \item Change to that directory with \texttt{cd \bs sim68k} \meta{Enter} \item Copy the programs from your floppy disk to your computer's hard disk by typing: \texttt{xcopy a:\bs sim68k\bs *.* .} \meta{Enter} \end{itemize} \index{assembler!\asmk!obtaining|)}% \index{simulator!\simk!obtaining|)}% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % \clearemptydoublepage \bibliographystyle{is-alpha} \clearpage \addcontentsline{toc}{chapter}{References} \bibliography{mc68k} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \clearpage% \addcontentsline{toc}{chapter}{Index} \printindex % INDEXING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \vspace*{\fill} \thispagestyle{plain} \begin{center} Reference: \referenceNum\\[1ex] \tiny \begin{tabularx}{\linewidth}{|l|c|c|X|} \hline \multicolumn{1}{|c|}{Compiled by} & Issue & Date & \multicolumn{1}{c|}{Remark} \\ \hline Nick Urbanik & 1 & \RCSDate & This is a major reworking of workshop 0 for 2540 from 1994/95. I have turned it into a book format and have improved the organisation, and added more useful information, such as the appendix on the simulator and an index and list of references. This is now for 2540, 2541, 2543, but is also useful to full-time students. \\ \hline \end{tabularx} \end{center} \end{document} TO DO: Needs info about the firmwaare commands for the FLIGHT-68K. More pictures. More info about the seven segment display and the interface board. Need explain more about the LEDs, dip switches. More assembly language instructions.