




\documentstyle[11pt,psfig]{article}

\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\topmargin}{-0.3in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.0in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}

\begin{document}

\title{Appendix A:  The Mac-1}

\author{Norman Matloff \\
University of California at Davis}                                

\date{November 20, 1996}

\maketitle            

\section{Mac-1 Architecture}

Mac-1 has 16-bit words and 12-bit addresses.  Its registers are the
program counter (PC), accumulator (AC) and the stack pointer 
(SP).\footnote{A register named tmp is alluded to in the list below.
However, this is a {\bf private register}, so called because the
programmer cannot access it; it is for Mac-1 internal use only.
Thus it is not considered part of the Mac-1 architecture.}

Following is the complete Mac-1 instruction set.  The notation M[z]
refers to the contents of memory location z.

\begin{verbatim}
machine language   mnemonic   long name         action
----------------   --------   ---------         ------
0000xxxxxxxxxxxx   lodd       load direct       AC:=M[x]
0001xxxxxxxxxxxx   stod       store direct      M[x]:=AC
0010xxxxxxxxxxxx   addd       add direct        AC:=AC+M[x]
0011xxxxxxxxxxxx   subd       subtract direct   AC:=AC-M[x]
0100xxxxxxxxxxxx   jpos       jump positive     if N = 0 and Z = 0 then PC:=x
0101xxxxxxxxxxxx   jzer       jump zero         if Z = 1 then PC:=x
0110xxxxxxxxxxxx   jump       jump              PC:=x
0111xxxxxxxxxxxx   loco       load constant     AC:=x, x nonnegative
1000xxxxxxxxxxxx   lodl       load local        AC:=M[SP+x]
1001xxxxxxxxxxxx   stol       store local       M[SP+x]:=AC
1010xxxxxxxxxxxx   addl       add local         AC:=AC+M[SP+x]
1011xxxxxxxxxxxx   subl       subtract local    AC:=AC-M[SP+x]
1100xxxxxxxxxxxx   jneg       jump negative     if N = 1 then PC:=x
1101xxxxxxxxxxxx   jnzr       jump nonzero      if Z = 0 then PC:=x
1110xxxxxxxxxxxx   call       call procedure    SP:=SP-1; M[SP]:=PC; PC:=x
1111000000000000   pushi      push indirect     SP:=SP-1; M[SP]:=M[AC]
1111001000000000   popi       pop indirect      M[AC]:=M[SP]; SP:=SP+1
1111010000000000   push       push              SP:=SP-1; M[SP]:=AC
1111011000000000   pop        pop               AC:=M[SP]; SP:=SP+1
1111100000000000   retn       return            PC:=M[SP]; SP:=SP+1
1111101000000000   swap       swap AC and SP    tmp:=AC; AC:=SP; SP:=tmp
11111100xxxxxxxx   insp       increment SP      SP:=SP+x, x nonnegative
11111110xxxxxxxx   desp       decrement SP      SP:=SP-x, x nonnegative
\end{verbatim}

The notation ``x unsigned'' means that x will be treated as a nonnegative
number, and thus 0s, not 1s, will be prepended before the indicated addition
or subtraction is performed.

\section{Simulator}

Mac-1 was originally an example in Andrew Tanenbaum's computer architecture
text, {\it Structured Computer Organization} (Prentice-Hall).  Tanenbaum
paired Mac-1 with a microengine Mic-1 (on which the microcode implementing
Mac-1 runs).  Due to the wide popularity of that text, numerous Mic-1/Mac-1 
simulators have been developed.  The one described here was written by
Professor Rosalee Nerheim-Wolfe at DePaul University.

Compile the simulator according to the instructions, and then change to
the ist directory.  In the following it will be assumed that you have
set a Unix variable \$ist naming that directory, with a statement in your
shell startup file, and that you have added the directories
\$ist/sim and \$ist/assem to your Unix search path.

The package can simulate many different architectures, not just Mac-1.
In our case, though, we are specifically interested in Mac-1, so we must
use the Mac-1 microcode, which is the file \$ist/frend/one/one.mic, and
the Mac-1 architecture definition file, \$ist/frend/one/one.def.

\subsection{Assembler Operation}

(Those who are just using the simulator as a ``bare bones'' machine,
without assembly language, may skip to the next section.)

Mac-1 assembly language files must have the suffix .asm.  Usage is

\begin{verbatim}
assem $ist/frend/one/one.def yourfilename
\end{verbatim}

where ``yourfilename'' does not include the .asm suffix.  This produces
an output file yourfilename.mac, which contains the Mac-1 machine code.

\subsection{Simulator Operation}

Usage for the simulator is

\begin{verbatim}
sim yourfilename.mac $ist/frend/one/one.mic
\end{verbatim}

The machine code from yourfilename.mac will be loaded into memory
starting at location 0.

Unfortunately, {\bf sim} does insists on having the first argument,
even if you wish to enter machine code into Mac-1 by hand rather than
through the assembly process.  If you do need to enter code by hand,
create a dummy file a.asm, say by executing

\begin{verbatim}
echo " pop" > a.asm
assem $ist/frend/one/one.def a
\end{verbatim}

(note the blank space within the quoted string),
and assemble it to produce a.mac.

Here are a few of the commands which are available in {\bf 
sim}:\footnote{We use their full names here, but the program will
accept their first two letters as abbreviations, as you will see
in the example below.}

{\bf examine:}  Check the contents of a register, a memory location, 
or a set of contiguous memory locations.  In the latter two cases,
addresses are given in decimal form.

{\bf set:}  Set the contents of a register or memory location to a
specified value.  The value can be specified either in hex form,
using the 0x prefix, or decimal form (no prefix).  However, the
address (if the value is being placed in a memory location) must
again be given in decimal form.

{\bf step:}  Execute a single instruction (the one pointed to by the
PC register).

{\bf go:}  Execute the code, starting with the one pointed to by the
PC register, continuing until a breakpoint is reached.

{\bf breakpoint:}  Specify a stopping point, to be used in conjunction
with the {\bf go} command.  Note that breakpoints are set on a
one-time basis; to re-use a breakpoint, you must set it again.

{\bf quit:}  Exit the simulator. 

As an example, let us use the code in Chapter 3, which was

\begin{verbatim}
0x100:  0x021c
0x101:  0x1258
0x102:  0x3228
0x103:  0x4106
0x104:  0x0228
0x105:  0x1258
0x106:  0x7001
0x107:  0x2258
0x108:  0x1258
\end{verbatim}

Here is a record of entering, running and verifying this code under
the simulator:

\begin{verbatim} 
ROOT# sim a.mac $ist/frend/one/one.mic
   ****** Welcome to the Instruction Set Testbed (IST) ******
/root/MicMac/ist/frend/one/one.mic read successfully. program length: 80
a.mac read successfully. program length: 2
asm:se 256 0x021c
asm:asm:se 257 0x1258
asm:asm:se 258 0x3228
asm:asm:se 259 0x4106
asm:asm:se 260 0x0228
asm:asm:se 261 0x1258
asm:asm:se 262 0x7001
asm:asm:se 263 0x2258
asm:asm:se 264 0x1258
asm:asm:ex 256-264
mem[256]: 0000 0010 0001 1100      lodd
mem[257]: 0001 0010 0101 1000      stod
mem[258]: 0011 0010 0010 1000      subd
mem[259]: 0100 0001 0000 0110      jpos
mem[260]: 0000 0010 0010 1000      lodd
mem[261]: 0001 0010 0101 1000      stod
mem[262]: 0111 0000 0000 0001      loco
mem[263]: 0010 0010 0101 1000      addd
mem[264]: 0001 0010 0101 1000      stod
asm:asm:se 540 5
asm:asm:se 552 2
asm:asm:se pc 256
asm:asm:br 265
asm:asm:go
breakpoint reached
asm:asm:ex 600
mem[600]: 0000 0000 0000 0110      lodd
asm:se 552 24
asm:asm:se pc 256
asm:asm:br 265
asm:asm:go
breakpoint reached
asm:asm:ex 600
mem[600]: 0000 0000 0001 1001      lodd
asm:qu
\end{verbatim}

In the first few lines, we are entering the code into memory, using
the {\bf set} command.  After doing so, we use {\bf examine} to check
that entry.

Next, we use {\bf set} to initialize the memory locations 540 and 552,
and set the PC to point to the start of the code.

We then set a breakpoint immediately following the end of the code,
and then run using {\bf go}.  When the breakpoint is reached, we use
{\bf examine} to confirm that the code placed the correct value in
memory location 600.\footnote{Note that the simulator also attempts
to tell us what ``instruction,'' {\bf lodd}, is stored at memory
location.  The contents of that location are of course data and not
an instruction, but as was stressed in Chapter 1, the machine (in
this case the simulator) cannot tell whether data or instructions
are stored in a given location, so the simulator gives us the
instruction just in case we do intend the contents to be an instruction.}

Finally, to complete our test of the code, we place a different test
value in memory location 552, and run the code again.

\end{document} 

