% ---------------------------------------------------------------------------
\section{The emulation}

This section descibes the emulation of the Commodore 64. Various aspects
of the of 6510-{\em instructions} are descibed in section~\ref{6510}.

\subsection{Pc memory and register layout}

At all times the 64Kb selected by the value of \inst{Ds} contains what a
{\em read\/} by {\em the processor\/} would read. The byte value at the same
offset but using a \inst{Gs} segment override contains information about
where the byte read originates (and at the same time where a store to the
location would go and if it would affect the screen).

This table describes the generel use of the pc-registers:
\begin{center}
  \begin{tabular}{|c|p{4.7in}|}\hline
    Reg        & Use \\\hline\hline
    \inst{Ds}  & Points to what the cpu can read. \\
    \inst{Es}  & Free. \\
    \inst{Fs}  & A segment value so that the input/output chips can be
                 accessed at their usual offsets. The border colour is
                 located at \inst{Fs:0D020h} for example. \\
    \inst{Gs}  & Describes the layout of the memory as the cpu sees it.\\\hline
    \inst{Al}  & Normally free. On entry to store handlers, this register
                 holds the value to be stored. \\
    \inst{Ah}  & Zero if and if only the zero flag of the 6510 is set. \\
    \inst{Bl}  & Free. \\
    \inst{Bh}  & Always zero. \\
    \inst{Cl}  & A negative value if and if only the sign flag
                 of the 6510 is set. \\
    \inst{Ch}  & The value of the \inst{A} register of the 6510. \\
    \inst{Dl}  & The value of the \inst{X} register of the 6510. \\
    \inst{Dh}  & The value of the \inst{Y} register of the 6510. \\
    \inst{Si}  & The value of the \inst{Pc} register of the 6510. \\
    \inst{Di}  & Normally free. On entry to store handlers, this register
                 holds the offset at which to store. \\
    \inst{Bp}  & \hex{-1} if the carry flag of the 6510 is set, zero if
                 it is not. \\\hline
  \end{tabular}
\end{center}
By ``Free'' I mean that all routines can use these registers without saving
their contents.

The registers \inst{Ah}, \inst{Cl}, \inst{Ch}, \inst{Dl}, \inst{Dh},
\inst{Si}, and \inst{Bp} should always be accessed using the equates and macros
found in the definition module of the source. I want my hands free to swap
register uses.

The stack pointer of the 6510 is kept in a byte variable, \inst{SPtr}, which
in memory is followed by a byte containing the value one. This allows for
easy access to the stack.

The byte variable \inst{Flags} holds the contents of bits 2--6 of the 6510
flags register. Bits 0, 1, and 7 are kept as described in the above table.

\subsection{Main loop}

The emulation of the micro computer is done on the instruction level as
opposed to the hardware or basic level. This means that emulation takes
a cpu view of the computer and it corresponds to taking a software view of
the computer. It also means that 100\% perfect emulation of the video
system is not possibly; the master of the bus in a Commodore 64 is the
video chip. Don't worry alot about this.

The main emulation loop (label \inst{Next} in the source) reads one byte
from memory at the current program counter (\inst{Pc}) location, advances
\inst{Pc} by one, and jumps to one out of 256~handlers controlled by the
byte read. These handlers are responsible for emulating one instruction each.

The handlers perform their functions, advances \inst{Pc} adequately, but if
the instruction requires that a value be stored in memory they don't just do
it. Instead they determine where a store to the given location would end
up (using \inst{Gs} as descibed above) and jumps to a corresponding handler.
This is {\em not\/} so for writes to the stack, they are just done.

When the handlers perform their functions they may need to read some bytes
in memory. These bytes they simply read from the \inst{Ds} segment.
This is mostly correct, but there are situations where it is not: reading
certain registers in the input/output chips should clear some bits.
See also section~\ref{cias}.
