% ---------------------------------------------------------------------------
\section{The 6510 instructions}\label{6510}

The emulator understands all 6510 instructions; documented as well as
undocumented. I have put a good deal of work into reverse engineering
the undocumented ones. This section documents them once and for all.
Some instructions are not emulated perfectly. The differences are
discussed below.

I may have made mistakes here --- {\em please\/} let me know if you find
any. It may also be that not all 6510 processors perform the same thing
when encountering the same undocumented instructions.

\subsection{Known differences}

Whenever a two-byte address is loaded it is loaded with a single
instruction. This is not normally a problem but it means that the emulator
may access a word quantity at offset \hex{\$FFFF}. This may crash the
Pc since it generates an exception 13. I don't think I'll fix it but if I do
I'll do it by catching the exception. The only way to get the exception is
to execute a three byte instruction at \hex{\$FFFE}. Such instructions are
not useful. Exception from the two-byte rule: the indirect jump instruction,
\hex{6C}, has been fixed so it does not cause an exception on
\inst{Jmp (\$FFFF)}.

The addressing mode ``Indexed Indirect'', for example \inst{Lda (\$nn,X)},
also suffers from the two-byte rule: if \inst{\$nn+X=\$FF} then the effective
address will be taken from \inst{(\$FF,\$100)}. I don't think I'll fix this
one. 

The two-byte rule does not apply to stack access. Great care has been taken
to push and pop on a byte basis.

The timings of all the instructions are all wrong. You didn't expect them
to follow the 6510, did you? This means that some programs give unexpected
results: ultra short or extended delays. You'll have to live with it.
In general instructions that leave memory unchanged are very fast. Memory
changes takes a little longer if they don't affect the screen and somewhat
longer if they do. Changes of the control registers of the different chips
of the Commodore~64 may also take some time if they affect the screen.

\subsection{Previously undocumented instructions}

Here they are! Every single one of them. The \hex{xB} codes are really scary.
See \hex{6B} if you want an example. The main purpose of using these
instructions is to make the code obscure. Very few are useful in themselves ---
\hex{AF}, \hex{CB}, and the multi-byte \inst{Nop}s are possible exceptions.

I have tried to invent mnemonics for all but the most obscure instructions.
It is my hope that this manual standardizes them. Some instructions were
too wierd to get a real mnemonic. These are called \inst{Inn} where \hex{nn}
are their hex-code.

{\parindent=0in\parskip=1em plus 0.2em minus 0.1em
\hex{1A}, \hex{3A}, \hex{5A}, \hex{7A}, \hex{DA}, \hex{FA}: \inst{Nop}. These
instructions don't seem to do anything.

\hex{04}, \hex{14}, \hex{34}, \hex{44}, \hex{54}, \hex{64}, \hex{74}, \hex{80},
\hex{82}, \hex{89}, \hex{C2}, \hex{D4}, \hex{E2}, \hex{F4}: \inst{Nop 2} ---
No operation. These two-byte instructions don't seem to do anything. It is
not known whether they access memory.

\hex{0C}, \hex{1C}, \hex{3C}, \hex{5C}, \hex{7C}, \hex{DC}, \hex{FC}:
\inst{Nop 3} --- No operation. These three-byte instructions don't seem to do
anything. It is not known whether they access memory.

\hex{02}, \hex{12}, \hex{22}, \hex{32}, \hex{42}, \hex{52}, \hex{62}, \hex{72},
\hex{92}, \hex{B2}, \hex{D2}, \hex{F2}: \inst{Hlt} --- Halt processor. These
instruction put the processor in a state where it does not respond to
even Nmi. I call this state ``Shutdown mode'' and the reaction of the
emulator is to reset the system. See reset for further details.

\hex{03}: \inst{Aor (\$nn,X)} --- Asl, then Ora. This is equivalent to
\fragment{Asl (\$nn,X)\\Ora (\$nn,X)} except for the fact that the value is
not reloaded (if the effective address is \hex{\$A000} where the basic rom
is then the store will go to the ram at the same address. A reload would get
the rom byte.)

\hex{07}: \inst{Aor \$nn}; see \hex{03}.

\hex{0B}: \inst{I0b \#\$nn}; equivalent to
\fragment{And \#\$nn\\Clv\\Clc\\Bpl Over\\Sev\\Sec\\Over:}
where \inst{Sev} is an instruction that sets the overflow flag. After the
logical and the sign flag is copied to the overflow and carry flags.

\hex{0F}: \inst{Aor \$nnnn}; see \hex{03}.

\hex{13}: \inst{Aor (\$nn),Y}; see \hex{03}.

\hex{17}: \inst{Aor \$nn,X}; see \hex{03}.

\hex{1B}: \inst{Aor \$nnnn,Y}; see \hex{03}.

\hex{1F}: \inst{Aor \$nnnn,X}; see \hex{03}.

\hex{23}: \inst{Ran (\$nn,X)} --- Rol, then And. This is equivalent to
\fragment{Rol (\$nn,X)\\And (\$nn,X)} except for the fact that the value is
not reloaded.

\hex{27}: \inst{Ran \$nn}; see \hex{23}.

\hex{2B}: \inst{And \#\$nn} --- Logical And

\hex{2F}: \inst{Ran \$nnnn}; see \hex{23}.

\hex{33}: \inst{Ran (\$nn),Y}; see \hex{23}.

\hex{37}: \inst{Ran \$nn,X}; see \hex{23}.

\hex{3B}: \inst{Ran \$nnnn,Y}; see \hex{23}.

\hex{3F}: \inst{Ran \$nnnn,X}; see \hex{23}.

\hex{43}: \inst{Leo (\$nn,X)} --- Lsr, then Eor. This is equivalent to
\fragment{Lsr (\$nn,X)\\Eor (\$nn,X)} except for the fact that the value is
not reloaded.

\hex{47}: \inst{Leo \$nn}; see \hex{43}.

\hex{4B}: \inst{I4B \#\$nn}; equivalent to
\fragment{And \#\$nn\\Lsr A}

\hex{4F}: \inst{Leo \$nnnn}; see \hex{43}.

\hex{53}: \inst{Leo (\$nn),Y}; see \hex{43}.

\hex{57}: \inst{Leo \$nn,X}; see \hex{43}.

\hex{5B}: \inst{Leo \$nnnn,Y}; see \hex{43}.

\hex{5F}: \inst{Leo \$nnnn,X}; see \hex{43}.

\hex{63}: \inst{Rad (\$nn,X)} --- Ror, then Adc. This is equivalent to
\fragment{Ror (\$nn,X)\\Adc (\$nn,X)} except for the fact that the value is
not reloaded. This is the only one of the double-function instruction that
uses the carry flag in both components.

\hex{67}: \inst{Rad \$nn}; see \hex{63}.

\hex{6B}: \inst{I6B \#\$nn}; equivalent to
\fragment{And \#\$nn\\Ror A\\Clc\\Bvc Over\\Sec\\Over:}
That was an \inst{And}, a \inst{Ror} and a copy of the overflow flag to
the carry flag. Don't ask me what is going on here!

\hex{6F}: \inst{Rad \$nnnn}; see \hex{63}.

\hex{73}: \inst{Rad (\$nn),Y}; see \hex{63}.

\hex{77}: \inst{Rad \$nn,X}; see \hex{63}.

\hex{7B}: \inst{Rad \$nnnn,Y}; see \hex{63}.

\hex{7F}: \inst{Rad \$nnnn,X}; see \hex{63}.

\hex{83}: \inst{Sax (\$nn,X)} --- Store (X and A). This instruction stores
the result of logically anding \inst{A} and \inst{X}. No flags are modified.

\hex{87}: \inst{Sax \$nn}; see \hex{83}.

\hex{8B}: \inst{Clr A} --- Clear. Zeroes the \inst{A}-register.
This is a two-byte instruction. I'm not very sure of this one.

\hex{8F}: \inst{Sax \$nnnn}; see \hex{83}.

\hex{93}: \inst{Clr (\$nn),Y}; see \hex{8B}.

\hex{97}: \inst{Sax \$nn,Y}; see \hex{83}.

\hex{9B}: \inst{I9B \$nnnn,Y}. Perform \inst{Sax \$nnnn,Y}, then transfer the
same value to the stack pointer. See also \hex{83}.

\hex{9C}: \inst{Sy1 \$nnnn,X} --- Store (Y and 1). This instruction stores
the result of logically anding \inst{Y} and \inst{1}. No flags are modified.

\hex{9E}: \inst{Sy1 \$nnnn,Y}; see \hex{9C}.

\hex{9F}: \inst{Sx1 \$nnnn,Y} --- Store (X and 1). This instruction stores
the result of logically anding \inst{X} and \inst{1}. No flags are modified.

\hex{A3}: \inst{Lax (\$nn,X)} --- Load A and X. This instruction load a
value into \inst{A} and duplicates it into \inst{X}.

\hex{A7}: \inst{Lax \$nn}; see \hex{A3}.

\hex{AB}: \inst{IAB \#\$nn}; equivalent to
\fragment{Lda \#\$nn\\And \#\$EE\\Tax}
I don't know how \hex{\$EE} got into this.

\hex{AF}: \inst{Lax (\$nnnn)}; (indirection intentional), see \hex{A3}.

\hex{B3}: \inst{Lax \$nn,Y}; see \hex{A3}.

\hex{BB}: \inst{IBB \#\$nn}; equivalent to
\fragment{Lda \$nnnn,Y\\Tax\\Txs}

\hex{BF}: \inst{Lax \$nnnn,Y}; see \hex{A3}.

\hex{C3}: \inst{Dcp (\$nn,X)} --- Dec, then Cmp. This is equivalent to
\fragment{Dec (\$nn,X)\\Cmp (\$nn,X)} except for the fact that the value is
not reloaded.

\hex{C7}: \inst{Dcp \$nn}; see \hex{C3}.

\hex{CB}: \inst{Sbx \#\$nn} --- Subtract from X.

\hex{CF}: \inst{Dcp \$nnnn}; see \hex{C3}.

\hex{D3}: \inst{Dcp (\$nn),Y}; see \hex{C3}.

\hex{D7}: \inst{Dcp \$nn,X}; see \hex{C3}.

\hex{DB}: \inst{Dcp \$nnnn,Y}; see \hex{C3}.

\hex{DF}: \inst{Dcp \$nnnn,X}; see \hex{C3}.

\hex{E3}: \inst{Isb (\$nn,X)} --- Inc, then Sbc. This is equivalent to
\fragment{Inc (\$nn,X)\\Sbc (\$nn,X)} except for the fact that the value is
not reloaded.

\hex{E7}: \inst{Isb \$nn}; see \hex{E3}.

\hex{EB}: \inst{Sbc \#\$nn} --- Subtract.

\hex{EF}: \inst{Isb \$nnnn}; see \hex{E3}.

\hex{F3}: \inst{Isb (\$nn),Y}; see \hex{E3}.

\hex{F7}: \inst{Isb \$nn,X}; see \hex{E3}.

\hex{FB}: \inst{Isb \$nnnn,Y}; see \hex{E3}.

\hex{FF}: \inst{Isb \$nnnn,X}; see \hex{E3}.
}
