\chapter{Instruction Set Architecture}

\newcommand{\OpcodeTable}[5] {
	\noindent
	\begin{minipage}{\textwidth}
		\begin{tabularx}{\textwidth}{ | X | X | X | X | }
			\hline
			Opcode Hex & Instruction & Operand 1 & Operand 2 \\
			\hline
			#1 & #2 & #3 & #4 \\
			\hline
		\end{tabularx}
		\paragraph{Description} #5.
	\end{minipage}
}

\def\minY{0} \def\maxY{0.5}
\def\minX{-7} \def\maxX{7}

\usetikzlibrary{decorations.pathreplacing}

\section{Encoding Scheme}
There are two basic encoding schemes that the object code will take. The most basic layout is shown in Figure \ref{fig:OpShortEncodingFigure} that is used by instructions that take one or zero arguments, where the 16 most significant bits are used to encode the instruction itself. Followed by the 16 least significant bits being used to encode a numeric constant that may be interpreted as a relative address. For instructions that require two arguments the encoding scheme shown in Figure \ref{fig:OpArgEncodingFigure} is used.
%Placement info here: https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Figures
\begin{figure}[h]
	\begin{tikzpicture}
		\draw (\minX, \minY) -- (\maxX, \minY);
		\draw (\minX, \minY) -- (\minX, \maxY) node[above] {31}; %{$y$}; Note the $'s, those make something math stylized.
		\draw (\maxX, \minY) -- (\maxX, \maxY) node[above] {0};
		\draw (0, \minY) -- (0, \maxY) node[above] {15};
		\draw [decorate, decoration={brace, amplitude=1em, mirror}] (\minX, -\maxY) -- (0, -\maxY) node[midway, below=1em] {Op Code (16-bits)};
		\draw [decorate, decoration={brace, amplitude=1em, mirror}] (0, -\maxY) -- (\maxX, -\maxY) node[midway, below=1em] {Near Address (16-bits)};
	\end{tikzpicture}
	\caption{Short Valued Opcodes}
	\label{fig:OpShortEncodingFigure} % https://www.overleaf.com/learn/latex/Referencing_Figures
\end{figure}
\begin{figure}
	\begin{tikzpicture}
		\draw (\minX, \minY) -- (\maxX, \minY);
		\draw (\minX, \minY) -- (\minX, \maxY) node[above] {31}; %{$y$}; Note the $'s, those make something math stylized.
		\draw (\maxX, \minY) -- (\maxX, \maxY) node[above] {0};
		\draw (0, \minY) -- (0, \maxY) node[above] {15};
		\draw [dotted] ({\maxX / 2}, \minY) -- ({\maxX / 2}, \maxY) node[above] {7};
		\draw [decorate, decoration={brace, amplitude=1em, mirror}] (\minX, -\maxY) -- (0, -\maxY) node[midway, below=1em] {Op Code (16-bits)};
		\draw [decorate, decoration={brace, amplitude=1em, mirror}] (0, -\maxY) -- ({\maxX / 2}, -\maxY) node[midway, below=1em] {Destination (8-bits)};
		\draw [decorate, decoration={brace, amplitude=1em, mirror}] ({\maxX / 2}, -\maxY) -- (\maxX, -\maxY) node[midway, below=1em] {Source (8-bits)};
	\end{tikzpicture}
	\caption{Register Based Opcodes}
	\label{fig:OpArgEncodingFigure} % https://www.overleaf.com/learn/latex/Referencing_Figures
	\paragraph{Note} How the \textit{Destination} and \textit{Source} are interpreted is dependent on the opcode and operand mode bits as shown in Figure \ref{fig:OperandModeBitPattern}.
\end{figure}
\begin{figure}
	\begin{tabularx}{\textwidth}{ | X | X | X | }
		\hline
		Bit Pattern & Destination & Source \\
		\hline
		000 & Register & Register \\
		001 & Register & 8 Bit Immediate \\
		010 & Register & 32 Bit Immediate \\
		011 & 8 Bit Immediate & Register \\
		100 & 32 Bit Immediate & Register \\
		\hline
	\end{tabularx}
	\caption{Operand Mode Bit Pattern}
	\label{fig:OperandModeBitPattern}
	\paragraph{Note} The \textit{Immediate} values could represent an address (if 8-bit, relative otherwise 32-bit values will represent absolute addresses) or a numeric constant. 
\end{figure}

\clearpage
\section{Instructions}
The /r represents a byte that specifies a register. /iX is for the immediate data type, where \textit{X} defines the width, which are 8, 16, or 32 bits.
\subsection{Add}
\OpcodeTable{01 /r /r}{add /r, /r}{Register}{Register}{Sums \textit{Operand 1} and \textit{Operand 2} placing the result into \textit{Operand 1}.}