
\def\SignBoxCornerRadius{.75mm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\BeginTikzPicture{
    \begin{tikzpicture}[x=.4cm,y=.3cm]
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\EndTikzPicture{
    \end{tikzpicture}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the characters within a string evenly spaced at integral node positions
%
% #1 The number of characters in the string
% #2 The string to print
\newcommand\DrawBitstring[2]{
\foreach \x in {1,...,#1}%
	\draw(\x,0) node{\substring{#2}{\x}{\x}};%
%	\draw(\x,.5) node[text width = 10, text height = 1]{\substring{#2}{\x}{\x}};%	Improve vertical text alignment
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 The total size
% #2 The string to print
% #3 The value to use when extending to left
\newcommand\DrawLeftExtendedBitstring[3]{
	\StrLen{#2}[\numchars]

	\pgfmathsetmacro\leftpadd{int(#1-\numchars)}
	\foreach \x in {1,...,\leftpadd}
    	\draw(\x,0) node{#3};

	\pgfmathsetmacro\leftpadd{int(\leftpadd+1)}
	\foreach \x in {\leftpadd,...,#1}
		\pgfmathsetmacro\ix{int(\x-\leftpadd+1)}
		\draw(\x,0) node{\substring{#2}{\ix}{\ix}};
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If the string is shorter than expected, extend with #5 to the right.
%
% #1 The total size
% #2 Num chars to extend on the right
% #3 The string to print
% #4 The value to use when extending to left
% #5 The value to use when extending to right
\newcommand\DrawDoubleExtendedBitstring[5]{
	\StrLen{#3}[\numchars]

	\pgfmathsetmacro\leftpadd{int(#1-#2-\numchars)}
	\ifthenelse{1 > \leftpadd}
	{}
	{
		\foreach \x in {1,...,\leftpadd}
    		\draw(\x,0) node{#4};
	}

	\pgfmathsetmacro\leftpadd{int(\leftpadd+1)}
	\pgfmathsetmacro\rightpadd{int(\leftpadd+\numchars)}
	\foreach \x in {\leftpadd,...,\rightpadd}
		\pgfmathsetmacro\ix{int(\x-\leftpadd+1)}
		\draw(\x,0) node{\substring{#3}{\ix}{\ix}};


	%\pgfmathsetmacro\rightpadd{int(\rightpadd+1)}
	\ifthenelse{\rightpadd > #1}	
	{}
	{
		\foreach \x in {\rightpadd,...,#1}
			\draw(\x,0) node{#5};
	}
}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw a box suitable to show the given number of bits in a 
% labeled box suitable for showing expanded binary numbers.
%
% #1 The number of characters to display
\newcommand\DrawBitBox[1]{
    \draw (.5,-.75) -- (#1+.5,-.75);		% box bottom
    \draw (.5,.75) -- (#1+.5,.75);			% box top
    \draw (.5,-.75) -- (.5, 1.5);			% left end
    \draw (#1+.5,-.75) -- (#1+.5, 1.5);		% right end
    \pgfmathsetmacro\result{int(#1-1)}		% calc high bit 
    \node at (1,1.2) {\tiny\result};		% high bit label
    \draw(#1,1.2) node{\tiny0};				% low bit label

    \pgfmathsetmacro\result{#1/2}
    \node at (\result,-1.2) {\tiny#1};		% size below the box

    \pgfmathsetmacro\result{#1/2}
    \draw[->] (\result+.6,-1.2) -- (#1+.5,-1.2);
    \draw[->] (\result-.6,-1.2) -- (.5,-1.2);
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxUnsigned[1]{
	\StrLen{#1}[\numchars]
	\DrawBitBox{\numchars}
	\DrawBitstring{\numchars}{#1}		% show the bits
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxUnsignedPicture[1]{
	\BeginTikzPicture
	\DrawBitBoxUnsigned{#1}
	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxSignedPicture[1]{
    \BeginTikzPicture
    \DrawBitBoxUnsigned{#1}
	% draw a box around the sign bit
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};
    \EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 The total (extended) size
% #2 The value to use for left-side padding
% #3 The string to extend
\newcommand\DrawBitBoxLeftExtended[3]{
	\StrLen{#3}[\numchars]
	\pgfmathsetmacro\fill{int(#1-\numchars)}
	\begin{scope}[shift={(\fill,3.5)}]
    \DrawBitBoxUnsigned{#3}

   	% XXX IFF not zero-extending then draw a box around the sign bit
   	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};
	\end{scope}

	\DrawBitBox{#1}
	\DrawDoubleExtendedBitstring{#1}{0}{#3}{#2}{x}
	
   	% XXX IFF not zero-extending then draw a box around the sign bit
   	\draw {[rounded corners=\SignBoxCornerRadius] (\fill+1.35, -.6) -- (\fill+1.35, .6) -- (\fill+.65, .6) -- (\fill+.65, -.6) -- cycle};
    % draw a box around the extended sign bits
    \draw (.65, -.6) -- (.65, .6) -- (\fill+.35, .6) -- (\fill+.35, -.6) -- cycle;


}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxSignExtendedPicture[2]{
	\BeginTikzPicture
	\DrawBitBoxLeftExtended{#1}{\substring{#2}{1}{1}}{#2}
    \EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxZeroExtendedPicture[2]{
	\BeginTikzPicture
	\DrawBitBoxLeftExtended{#1}{0}{#2}
    \EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 Total bit length
% #2 The string to print
% #3 Right-side padding length
\newcommand\DrawBitBoxSignLeftZeroRightExtendedPicture[3]{
	\BeginTikzPicture

	\StrLen{#2}[\numchars]
	\pgfmathsetmacro\fill{int(#1-\numchars-#3)}
	\begin{scope}[shift={(\fill,3.5)}]
    \DrawBitBoxUnsigned{#2}
    % draw a box around the sign bit
    %\draw (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle;
    \draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};
	\end{scope}

	\DrawBitBox{#1}
	\DrawDoubleExtendedBitstring{#1}{#3}{#2}{\substring{#2}{1}{1}}{0}

	% Box the sign bit
    \draw {[rounded corners=\SignBoxCornerRadius] (\fill+1.35, -.6) -- (\fill+1.35, .6) -- (\fill+.65, .6) -- (\fill+.65, -.6) -- cycle};

	\ifthenelse{\fill > 0}
	{
    	% Box the left-extended sign bits
    	\draw (.65, -.6) -- (.65, .6) -- (\fill+.35, .6) -- (\fill+.35, -.6) -- cycle;
		% \fill[blue!40!white] (.65, -.6) rectangle (\fill-.25, 1.2);
	}
	{}
	\ifthenelse{#3 > 0}
	{
    	% Box the right-extended sign bits
		\pgfmathsetmacro\posn{int(\numchars+\fill)}
    	\draw (\posn+.65, -.6) -- (\posn+.65, .6) -- (\posn+#3+.35, .6) -- (\posn+#3+.35, -.6) -- cycle;
	}
	{}
	

    \EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the characters within a string evenly spaced at integral node positions
%
% #1 The number of characters in the string
% #2 The string of characters to plot
% #3 Right-side label
\newcommand\DrawInsnBitstring[3]{
	\pgfmathsetmacro\num{int(#1-1)}
	\foreach \x in {1,2,...,#1}
    	\draw(\x+.25,-.3) node[text width = 10, text height = 1]{\substring{#2}{\x}{\x}};
	\draw(#1+1,0) node[right]{#3};
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 total characters/width
% #2 MSB position
% #3 LSB position
% #4 the segment label
\newcommand\DrawInsnBoxSeg[4]{
	\pgfmathsetmacro\leftpos{int(#1-#2)}
	\pgfmathsetmacro\rightpos{int(#1-#3)}

	\draw (\leftpos-.5,-.75) -- (\rightpos+.5,-.75);	% box bottom
	\draw (\leftpos-.5,1.75) -- (\rightpos+.5,1.75);	% box top
	\draw (\leftpos-.5,-.75) -- (\leftpos-.5, 2.5);		% left end
	\draw (\rightpos+.5,-.75) -- (\rightpos+.5, 2.5);	% right end
	\node at (\leftpos,2.2) {\tiny#2};
	\draw(\rightpos,2.2) node{\tiny#3};

	\pgfmathsetmacro\posn{#1-#2+(#2-#3)/2}
	\node at (\posn,1.2) {\small#4};			% the field label

	\begin{scope}[shift={(0,-.7)}]\InsnBoxFieldWidthArrow{#1}{#2}{#3}\end{scope}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\InsnStatement[1]{
%	\textbf{\large #1}\\
%	\textbf{#1}\\
	{\large #1}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\InsnBoxFieldWidthArrowVskip{.5}
\newcommand\InsnBoxFieldWidthArrowHskip{.05}

% #1 number of bits-wide
% #2 MSB position
% #3 LSB position
\newcommand\InsnBoxFieldWidthArrow[3]{
	\pgfmathsetmacro\leftpos{int(#1-#2-1)}		% Calculate the left end position
	\pgfmathsetmacro\wid{int(#2-#3+1)}			% calculate the width
	\begin{scope}[shift={(\leftpos,-\InsnBoxFieldWidthArrowVskip)}]	% Move to left end of arrow & below origin
    	\pgfmathsetmacro\result{\wid*.5+.5}		% the center position
    	\node at (\result,0) {\tiny\wid};		% draw the size number below the box

		\ifthenelse{\wid > 9}					% make 1-9 narrower than 10-99
		{ \pgfmathsetmacro\Inset{0.4} }
		{ 
			\ifthenelse{\wid > 1} 				% make 1 narrower than 2-9
			{ \pgfmathsetmacro\Inset{0.25} }
			{ \pgfmathsetmacro\Inset{0.15} }
		}

		% arrowsInsnBoxFieldWidthArrowHskip
    	\draw[->] (\result+\Inset,0) -- (\wid+.5-\InsnBoxFieldWidthArrowHskip,0);	% arrow to the right
    	\draw[->] (\result-\Inset,0) -- (.5+\InsnBoxFieldWidthArrowHskip,0);		% arrow to the left

		\pgfmathsetmacro\x{.5}
		\pgfmathsetmacro\y{\InsnBoxFieldWidthArrowVskip}
		% vertical bars at the ends of the arrows
    	\draw[-] (\x,\y) -- (\x,-\y*.5);	
		\pgfmathsetmacro\x{(\wid+.5}
    	\draw[-] (\x,\y) -- (\x,-\y*.5);	

	\end{scope}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\BitBoxArrowTailInset{-.9}
\newcommand\BitBoxArrowHeadInset{-16.7-\BitBoxArrowTailInset}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw hex markers with a baseline at zero
% #1 The number of bits in the box
\newcommand\TheHexMark[1]{
	\draw [line width=.5mm] (#1+.5,0) -- (#1+.5, .4);
}

\newcommand\DrawHexMarkersRel[1]{
	\pgfmathsetmacro\num{int(#1)}
	\foreach \x in {0,4,...,\num}
		\draw [line width=.5mm] (\x+.5,0) -- (\x+.5, .4);
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw an instruction box with a baseline at zero
% #1 MSB position
% #2 LSB position
% #3 the segment label
\newcommand\DrawInsnBoxRelTop[3]{
	\pgfmathsetmacro\leftpos{int(32-#1)}
	\pgfmathsetmacro\rightpos{int(32-#2)}
	\draw (\leftpos-.5,1.5) -- (\rightpos+.5,1.5);	% box top
	\draw (\leftpos-.5,0) -- (\leftpos-.5, 1.5);	% left end
	\draw (\rightpos+.5,0) -- (\rightpos+.5, 1.5);	% right end
	\pgfmathsetmacro\posn{32-#1+(#1-#2)/2}
	\node at (\posn,.75) {\small#3};				% the field label
}

% Draw only the bottom line of an instruction box with a baseline at zero
% #1 MSB position
% #2 LSB position
\newcommand\DrawInsnBoxRelBottom[2]{
	\pgfmathsetmacro\leftpos{int(32-#1)}
	\pgfmathsetmacro\rightpos{int(32-#2)}
	\draw (\leftpos-.5,0) -- (\rightpos+.5,0);		% box bottom
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw an instruction box with a baseline at zero
% #1 MSB position
% #2 LSB position
% #3 the segment label
\newcommand\DrawInsnBoxRel[3]{
	\DrawInsnBoxRelTop{#1}{#2}{#3}
	\DrawInsnBoxRelBottom{#1}{#2}
}

% #1 MSB position
% #2 LSB position
\newcommand\DrawInsnBoxCastle[2]{
	\pgfmathsetmacro\leftpos{int(32-#1)}
	\pgfmathsetmacro\rightpos{int(32-#2)}
	\draw (\leftpos-.5,0) -- (\leftpos-.5, .75);		% left end
	\draw (\rightpos+.5,0) -- (\rightpos+.5, .75);	% right end
	\node at (\leftpos,.5) {\tiny#1};
	\ifthenelse{\equal{#1}{#2}}
	{}
	{ \draw(\rightpos,.5) node{\tiny#2}; }
}
