%%
%% This is file `register.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% register.dtx  (with options: `package')
%% Hardware register diagrams with field descriptions.
%% 
%% This package may be distributed and/or modified under the conditions
%% of the LaTeX Project Public License, either version 1.3 of this
%% license or (at your option) any later version.  The latest version of
%% this license is in:
%% 
%%                 http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3c or later is part of all distributions of LaTeX version
%% 2006/05/20 or later.
%% 
%% Author: Matthew Lovell (lovells@gmail.com)
%% 



\ProvidesPackage{register} [2019/01/01 v1.9 Register macros with
hyperref/LyX support]

\RequirePackage{ifthen}[1997/11/02]
\RequirePackage{graphicx}[1997/06/09]
\RequirePackage{float}[2001/07/25]
\RequirePackage{calc}[1998/06/07]
\DeclareOption{LyX}{\setboolean{RegisterLyX}{true}}
\DeclareOption{hyperref}{\setboolean{RegisterHyperref}{true}}
\DeclareOption{TRflags}{\setboolean{RegisterTRFlags}{true}}
\DeclareOption{TRboxed}{\setboolean{RegisterTRBoxed}{true}}
\DeclareOption{color}{\setboolean{RegisterColors}{true}}

\DeclareOption*{%  Emit a warning for other options
  \PackageWarning{register}{Unknown option '\CurrentOption'}%
}

\newboolean{RegisterLyX}
\newboolean{RegisterHyperref}
\newboolean{RegisterTRFlags}
\newboolean{RegisterTRBoxed}
\newboolean{RegisterColors}

\setboolean{RegisterLyX}{false}
\setboolean{RegisterHyperref}{false}
\setboolean{RegisterTRFlags}{false}
\setboolean{RegisterTRBoxed}{false}
\setboolean{RegisterColors}{false}

\ProcessOptions\relax  % Process package options
\ifthenelse{\boolean{RegisterColors}}{\RequirePackage{xcolor}[2007/01/21]}{}
\newboolean{RegisterContext}
\setboolean{RegisterContext}{false}
\newcommand{\regFloatName}{Register}
\newcommand{\regListName}{List of Registers}
\newcommand{\regResetName}{Reset}

\floatstyle{plaintop} \@ifundefined{chapter}
{\newfloat{Regfloat}{tbp}{rdf}[section]}
{\newfloat{Regfloat}{tbp}{rdf}[chapter]}
\floatname{Regfloat}{\regFloatName}
\newlength{\regWidth}
\newlength{\regFieldLen}
\newlength{\regLabelAdjust}
\newlength{\regResetHeight}
\newlength{\regResetDepth}
\newlength{\regResetDrop}
\newlength{\regDescSkip}
\newlength{\regRsvdHeight}
\newlength{\regRsvdDrop}
\newlength{\regFboxSep}
\setlength{\regWidth}{0.95\textwidth}
\newcommand{\regBitWidth}{32}
\newcommand{\regBitSize}{\tiny}
\newcommand{\regBitFamily}{\sffamily}
\newcommand{\regResetSize}{\scriptsize}
\newcommand{\regLabelSize}{\footnotesize}
\newcommand{\regLabelFamily}{\rmfamily}
\newcommand{\regDescFamily}{\bf}
\newlength{\regdescsep}
\newlength{\oldregdescsep}
\setlength{\regdescsep}{-\medskipamount}

\newsavebox{\Label}
\newsavebox{\RotatedLabel}
\newcounter{upperbit}
\newcounter{lowerbit}
\newenvironment{reglist}[1][M]
  {\begin{list}{}
    {\settowidth{\labelwidth}{\regDescFamily #1}
     \addtolength{\labelwidth}{\labelsep}
     \setlength{\leftmargin}{\labelwidth}
     \addtolength{\leftmargin}{\labelsep}
     \addtolength{\leftmargin}{0.5\regDescSkip}
     \addtolength{\rightmargin}{0.5\regDescSkip}
     \setlength{\topsep}{0pt}
     \setlength{\itemsep}{0pt}
     \setlength{\parsep}{0.5\baselineskip}
     \renewcommand{\makelabel}[1]{\regDescFamily ##1 \hfill}}}
  {\end{list}}
\newenvironment{regdesc}[1][0.90\textwidth]%
{%
 \setlength{\regDescSkip}{\textwidth - #1}%
 \vspace{\regdescsep}%
\ifthenelse{\boolean{RegisterLyX}}{%
  \renewenvironment{lyxlist}[1]
    {\begin{list}{}
      {\settowidth{\labelwidth}{\regDescFamily ##1}
       \addtolength{\labelwidth}{\labelsep}
       \setlength{\leftmargin}{\labelwidth}
       \addtolength{\leftmargin}{\labelsep}
       \addtolength{\leftmargin}{0.5\regDescSkip}
       \addtolength{\rightmargin}{0.5\regDescSkip}
       \setlength{\topsep}{0pt}
       %\setlength{\partopsep}{0pt}
       \renewcommand{\makelabel}[1]{\regDescFamily ####1 \hfill}}}
    {\end{list}}}{}%
 % endif
 % set spacing appropriately
 \leftskip 0.5\regDescSkip%
 \rightskip 0.5\regDescSkip%
 \parfillskip=\z@ plus 1fil%
 \parskip=0.5\baselineskip \advance\parskip by 0pt plus 2pt%
}% end begin{regdesc}
{\vskip\baselineskip}
\newcommand{\regnewline}{\\*}
\newenvironment{register}[3]
{\begin{Regfloat}[#1]%
  \setlength{\leftskip}{0pt}%
  \setlength{\oldregdescsep}{\regdescsep}%
  \setlength{\regdescsep}{0.5\baselineskip}%
  \setlength{\partopsep}{0pt}%
  \setlength{\topsep}{0pt}%
  \setboolean{RegisterContext}{true}%
  \ifthenelse{\equal{#3}{}}%
  {\caption[#2]{\textsc{#2}}}% else
  {\caption[#2]{\textsc{#2} ({#3})}}%
  \centering}
{% restore lengths
  \leftskip\z@%
  \rightskip\z@%
  \parfillskip=\z@ plus 1fil%
  \setlength{\regdescsep}{\oldregdescsep}%
  \setboolean{RegisterContext}{false}%
  \end{Regfloat}}
\newenvironment{register*}[3]
{\begin{Regfloat}[#1]%
   \setlength{\leftskip}{0pt}%
   \setlength{\oldregdescsep}{\regdescsep}%
   \setlength{\regdescsep}{0.5\baselineskip}%
   \setlength{\partopsep}{0pt}%
   \setlength{\topsep}{0pt}%
   \setboolean{RegisterContext}{true}%
   \ifthenelse{\equal{#3}{}}%
   {\centering\textsc{#2}\\}% else
   {\centering\textsc{#2} ({#3})\\}%
   \centering}
{% restore lengths
   \leftskip\z@%
   \rightskip\z@%
   \parfillskip=\z@ plus 1fil%
   \setlength{\regdescsep}{\oldregdescsep}%
   \setboolean{RegisterContext}{false}%
   \end{Regfloat}}
\def\regUnderScore{_}%
\def\regFiller#1{\def\regInner{#1}%
\ifx\regInner\regUnderScore%
\else%
\ifnum\count0>0%
\hfill#1%
\else#1\fi%
\fi%
\advance\count0 by 1%
}
\def\regSpread#1{\count0=0{}\regSpreadaux#1\empty}
\def\regSpreadaux#1#2\empty{\def\aux{#1}%\show#1%
\ifx\aux\empty%
\else%
\def\aux{#2}%
\regFiller{#1}%
\ifx\aux\empty%
\else%
\regSpreadaux#2\empty%
\fi\fi}
\newcommand{\setRegLengths}{%
  % Compute basic width of a single bit
  \settowidth{\regFieldLen}{\regLabelSize \regResetName}%
  \setlength{\regFieldLen}{\regWidth - \regFieldLen}%
  \setlength{\regFieldLen}{\regFieldLen / \regBitWidth}%
  % Figure out height and depth of reset field in current font
  % Is there a more ``official'' method to do this?
  \settodepth{\regResetDepth}{\regResetSize ()jgpq}%
  \settoheight{\regResetHeight}{\regResetSize ()ABCjkl}%
  \addtolength{\regResetHeight}{\regResetDepth}%
  % Compute how far to drop the reset fields down.  The value at
  % the end is effectively the separation between the bit position
  % box and the reset value box.
  \setlength{\regResetDrop}{\regResetHeight + 2\fboxsep - 2\fboxrule + 3pt}%
  % New lengths to support colorbox use, when fboxsep gets set to 0
  \setlength{\regRsvdDrop}{\regResetDrop + \fboxsep}%
  \setlength{\regRsvdHeight}{\regResetHeight + 2\fboxsep}%
  \setlength{\regFboxSep}{\fboxsep}%
}
\newcommand{\regMakeFieldName}[1]{%
  % Create box to hold label
  \savebox{\Label}{\regLabelSize\regLabelFamily #1}%
}
\newcommand{\regRotateFieldName}{%
  \savebox{\RotatedLabel}{\rotatebox[origin=lB]{45}{\usebox{\Label}}}%
  \makebox[0pt][l]{\raisebox{\regResetHeight + \fboxsep + \depth + 1pt}%
        {\hspace{\regLabelAdjust}\usebox{\RotatedLabel}}}%
}
\newcommand{\typesetRegBits}[1]{%
  \ifthenelse{#1 > 1}%
    {\framebox[\regFieldLen][c]%
        {\regBitSize \rule[-1\regResetDepth]{0pt}{\regResetHeight}%
           \regBitFamily\arabic{upperbit} \hfill \arabic{lowerbit}}}%
    {\framebox[\regFieldLen][c]%
        {\regBitSize \rule[-1\regResetDepth]{0pt}{\regResetHeight}%
           \regBitFamily\arabic{lowerbit}}}%
}
\newcommand{\typesetRegReset}[1]{%
 % Typeset reset value in a framebox
  \makebox[0pt][l]{\raisebox{-1\regResetDrop}{\framebox[\regFieldLen][c]%
        % Place an invisible rule to control the box
        % surrounding the reset field
        {\regResetSize \rule[-1\regResetDepth]{0pt}{\regResetHeight}\regSpread{#1}}}}%
}
\newcommand{\regfieldNoColor}[4]{%
  % Compute overall field length
  \setRegLengths%
  \setlength{\regFieldLen}{#2\regFieldLen + \fboxrule}%
  % Figure out bit positions
  \setcounter{lowerbit}{#3}%
  \setcounter{upperbit}{#3 + #2 - 1}%
  \regMakeFieldName{#1}%
  % Figure out how far over to place label, accounting for height
  \setlength{\regLabelAdjust}{0.5\regFieldLen - 0.707107\ht\Label}%
  % Now, rotate and type the label
  \regRotateFieldName%
  \typesetRegReset{#4}%
  % Typeset bit positions in a framebox
  \typesetRegBits{#2}%
  \hspace{-1\fboxrule}%
}
\newcommand{\regfieldbNoColor}[3]{%
  % Compute overall field length
  \setRegLengths%
  \setlength{\regFieldLen}{#2\regFieldLen + \fboxrule}%
  % Figure out bit positions
  \setcounter{lowerbit}{#3}%
  \setcounter{upperbit}{#3 + #2 - 1}%
  % Create box to hold label
  \regMakeFieldName{#1}%
  % Figure out how far over to place label, accounting for height
  \setlength{\regLabelAdjust}{0.5\regFieldLen - 0.707107\ht\Label}%
  % Now, rotate and typeset the label
  \regRotateFieldName%
  % Typeset bit positions
  \typesetRegBits{#2}%
  \hspace{-1\fboxrule}%
}
\ifthenelse{\boolean{RegisterColors}}{%
\newcommand{\typesetRegColorBits}[2]{%
  \ifthenelse{#2 > 1}%
    {\setlength\fboxsep{0pt}%
      \colorbox{#1}{%
        \framebox[\regFieldLen][c]%
        {\regBitSize \rule[-1\regResetDepth - \regFboxSep]{0pt}%
          {\regResetHeight + 2\regFboxSep}%
           \regBitFamily\hspace{\regFboxSep}%
\arabic{upperbit} \hfill \arabic{lowerbit}%
\hspace{\regFboxSep}}}}%
    {\setlength\fboxsep{0pt}%
      \colorbox{#1}{%
        \framebox[\regFieldLen][c]%
        {\regBitSize \rule[-1\regResetDepth - \regFboxSep]{0pt}%
           {\regResetHeight + 2\regFboxSep}%
           \regBitFamily\arabic{lowerbit}}}}%
  \setlength\fboxsep{\regFboxSep}%
}
\newcommand{\typesetRegColorReset}[2]{%
 % Typeset reset value in a framebox
  \makebox[0pt][l]{\raisebox{-1\regRsvdDrop}{%
      \setlength\fboxsep{0pt}%
        \colorbox{#1}{\framebox[\regFieldLen][c]%
        % Place an invisible rule to control the box surrounding the reset field
        {\regResetSize \rule[-1\regResetDepth]{0pt}{\regRsvdHeight}%
        \raisebox{\regFboxSep}{\makebox[\regFieldLen]%
         {\hspace{\regFboxSep}\regSpread{#2}\hspace{\regFboxSep}}}}}%
}}}
\newcommand{\regfieldColor}[5]{%
  % Compute overall field length
  \setRegLengths%
  \setlength{\regFieldLen}{#3\regFieldLen + \fboxrule}%
  % Figure out bit positions
  \setcounter{lowerbit}{#4}%
  \setcounter{upperbit}{#4 + #3 - 1}%
  \regMakeFieldName{#2}%
  % Figure out how far over to place label, accounting for height
  \setlength{\regLabelAdjust}{0.5\regFieldLen - 0.707107\ht\Label}%
  % Now, rotate and type the label
  \regRotateFieldName%
  \typesetRegColorReset{#1}{#5}%
  % Typeset bit positions in a framebox
  \typesetRegColorBits{#1}{#3}%
  \hspace{-1\fboxrule}%
}
\newcommand{\regfieldbColor}[4]{%
  % Compute overall field length
  \setRegLengths%
  \setlength{\regFieldLen}{#3\regFieldLen + \fboxrule}%
  % Figure out bit positions
  \setcounter{lowerbit}{#4}%
  \setcounter{upperbit}{#4 + #3 - 1}%
  % Create box to hold label
  \regMakeFieldName{#2}%
  % Figure out how far over to place label, accounting for height
  \setlength{\regLabelAdjust}{0.5\regFieldLen - 0.707107\ht\Label}%
  % Now, rotate and typeset the label
  \regRotateFieldName%
  % Typeset bit positions
  \typesetRegColorBits{#1}{#3}%
  \hspace{-1\fboxrule}%
}
}{}
\ifthenelse{\boolean{RegisterColors}}{
  \newcommand{\regfield}[5][]{%
    \ifthenelse{\equal{#1}{}}{%
      \regfieldNoColor{#2}{#3}{#4}{#5}}%
    {\regfieldColor{#1}{#2}{#3}{#4}{#5}}}
  \newcommand{\regfieldb}[4][]{%
    \ifthenelse{\equal{#1}{}}{%
      \regfieldbNoColor{#2}{#3}{#4}}%
    {\regfieldbColor{#1}{#2}{#3}{#4}}}
}{
  \newcommand{\regfield}[5][]{%
    \regfieldNoColor{#2}{#3}{#4}{#5}}%
  \newcommand{\regfieldb}[4][]{%
    \regfieldbNoColor{#2}{#3}{#4}}%
}
\newcommand{\regbits}[3]{%
  % Compute overall field length
  \setRegLengths%
  \setlength{\regFieldLen}{#2\regFieldLen + \fboxrule}%
  % Figure out bit positions
  \setcounter{lowerbit}{#3}%
  \setcounter{upperbit}{#3 + #2 - 1}%
  % Create box to hold label
  \regMakeFieldName{#1}%
  % Figure out how far over to place label, accounting for height
  \setlength{\regLabelAdjust}{0.5\regFieldLen - 0.707107\ht\Label}%
  % Now, rotate and typeset the label
  \regRotateFieldName%
  % Typeset field value
  \framebox[\regFieldLen][c]%
        {\tiny\regSpread{#3}}%
  \hspace{-1\fboxrule}%
}
\newcommand{\regspace}[1]{%
  % Compute overall field length
  \setRegLengths%
  \setlength{\regFieldLen}{#1\regFieldLen + \fboxrule}%
  \makebox[\regFieldLen]{}%
}

\newcommand{\reglabel}[1]{%
  \settowidth{\regFieldLen}{\regLabelSize \regResetName}%
  \setlength{\regResetDrop}{\regResetDrop + 0.5\fboxsep}%
  $\,$\raisebox{-1\regResetDrop}{\makebox[\regFieldLen][l]%
    {\regLabelSize\regBitFamily #1}}%
}

\newcommand{\reglabelb}[1]{%
  \settowidth{\regFieldLen}{\regLabelSize \regResetName}%
  $\,$\raisebox{-0.5\fboxsep}{\makebox[\regFieldLen][l]%
    {\regLabelSize\regBitFamily #1}}%
}
\ifthenelse{\boolean{RegisterHyperref}}{%
  % Define a counter for the hyperref package.  Otherwise,
  % the hyperlinks to registers don't work correctly
  \@namedef{theHRegfloat}{\theRegfloat}

  % Define a bookmark level for Regfloats (for hyperref package)
  \def\toclevel@Regfloat{0}
}{}
\newcommand{\listofregisters}{%
  \@ifundefined{ext@Regfloat}{\float@error{Regfloat}}{%
    \@ifundefined{chapter}{\def\@tempa{\section*}}%
      {\def\@tempa{\chapter*}}%
    \@tempa{\regListName\@mkboth{\uppercase{\regListName}}%
       {\uppercase{\regListName}}}%
    \addcontentsline{toc}{chapter}{\regListName}%
    \@starttoc{\@nameuse{ext@Regfloat}}}}
\newcommand\l@Regfloat{\@dottedtocline{1}{1.5em}{2.3em}}
\@ifundefined{@mainmatter}{\newif\if@mainmatter \@mainmattertrue}{}
\@ifundefined{chapter}{}
{% Adjust chapter definition slightly for Regfloats
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
  \if@mainmatter
    \refstepcounter{chapter}%
    \typeout{\@chapapp\space\thechapter.}%
    \addcontentsline{toc}{chapter}%
    {\protect\numberline{\thechapter}#1}%
  \else
    \addcontentsline{toc}{chapter}{#1}%
  \fi
  \else
    \addcontentsline{toc}{chapter}{#1}%
  \fi
  \chaptermark{#1}%
    \addtocontents{lof}{\protect\addvspace{10\p@}}%
    \addtocontents{lot}{\protect\addvspace{10\p@}}%
    \addtocontents{rdf}{\protect\addvspace{10\p@}}% --- Add space ---
  \if@twocolumn
    \@topnewpage[\@makechapterhead{#2}]%
  \else
    \@makechapterhead{#2}%
    \@afterheading
  \fi}
}
\newcommand\@GetTRSecondParam{}
\long\def\@GetTRSecondParam#1#2#3\@nil{#2}
\newcommand*{\GetTRPageRef}[1]{%
  \expandafter\expandafter\expandafter\@GetTRSecondParam
    \csname r@#1\endcsname
    0% dummy, if the reference is undefined
    \@nil
}

\newcommand{\TRfamily}{\sffamily}
\newcounter{T@peReleaseTag}
\newlength{\TRwidth}
\setlength{\TRwidth}{\marginparwidth}
\newlength{\T@peReleaseDepth}

\newcommand{\TRwriteout}[1]{%
  \makebox[\TRwidth][c]{%
    \raisebox{\T@peReleaseDepth}{%
     \ifthenelse{\boolean{RegisterTRBoxed}}%
       {\fbox{\TRfamily #1}}%
       {\TRfamily #1}}}%
}

\newcommand{\TRrightlabel}[1]{%
  % Place a strut in order to set line depth
  \mbox{}\strut\settodepth{\T@peReleaseDepth}{\strut}%
   \vadjust{\hspace{\textwidth}\hspace{\marginparsep}%
   \smash{\rlap{\TRwriteout{#1}}}}}

\ifthenelse{\boolean{@twoside}}{
  % Two-sided document
  \newcommand{\TRleftlabel}[1]{%
    % Place a strut in order to set line depth
    \mbox{}\strut\settodepth{\T@peReleaseDepth}{\strut}%
    \vadjust{\smash{\llap{\TRwriteout{#1}}\kern\marginparsep}}}
}{
  % Otherwise, the command is the same as rightlabel
  \newcommand{\TRleftlabel}[1]{%
    \TRrightlabel{#1}}
}
\ifthenelse{\boolean{RegisterTRFlags}}{
\newcommand{\TR}[1]{%
  \stepcounter{T@peReleaseTag}%
  \label{TapeReleaseTag-\theT@peReleaseTag}%
  \ifthenelse{\isodd{\GetTRPageRef{TapeReleaseTag-\theT@peReleaseTag}}}%
   {\TRrightlabel{TR\hspace{1pt}#1}}%
   {\TRleftlabel{TR\hspace{1pt}#1}}%
}}
{\newcommand{\TR}[1]{}}
\endinput
%%
%% End of file `register.sty'.
