使用 TikZ 绘制寄存器图

使用 TikZ 绘制寄存器图

我想知道是否有办法在 LaTeX 中(可能在 TikZ 中)轻松绘制寄存器图(如下所示)?

在此处输入图片描述

我最接近的只是使用表格环境,但这还远远没有达到我想要的效果,我的结果如下所示。

\begin{table}[h!]
\begin{tabular}{p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt} p{20pt}  >{\centering\arraybackslash}m{20pt}}
\centering 15 & \centering 14 & \centering 13 & \centering 12 & \centering 11 & \centering 10 & \centering 9 & \centering 8 & \centering 7 & \centering 6 & \centering 5 & \centering 4 & \centering 3 & \centering 2 & \centering 1 & 0\\\hline
\multicolumn{1}{|c|}{SEL} & 
\multicolumn{1}{c|}{SEL} & 
\multicolumn{1}{c|}{SEL} & 
\multicolumn{5}{|c|}{\cellcolor{gray!25}} & 
\multicolumn{1}{c|}{DIR} & 
\multicolumn{7}{c|}{PWM} \\\hline
\end{tabular}
\end{table}

在此处输入图片描述

答案1

在此处输入图片描述

这里的问题有点晚了,但这应该是可行的。我不确定我是否对不同的字段有正确的语义,但这应该相对容易适应:

\documentclass{standalone}
\usepackage{tikz}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand{\bitrect}[2]{
  \begin{pgfonlayer}{foreground}
    \draw [thick] (0,0) rectangle (#1,1);
    \pgfmathsetmacro\result{#1-1}
    \foreach \x in {1,...,\result}
      \draw [thick] (\x,1) -- (\x, 0.8);
  \end{pgfonlayer}
%  \node [below left, align=right] at (0,0) {Type \\ Reset};
  \bitlabels{#1}{#2}
}
\newcommand{\rwbits}[3]{
  \draw [thick] (#1,0) rectangle ++(#2,1) node[pos=0.5]{#3};
  \pgfmathsetmacro\start{#1+0.5}
  \pgfmathsetmacro\finish{#1+#2-0.5}
%  \foreach \x in {\start,...,\finish}
%    \node [below, align=center] at (\x, 0) {R/W \\ 0};
}
\newcommand{\robits}[3]{
  \begin{pgfonlayer}{background}
    \draw [thick, fill=lightgray] (#1,0) rectangle ++(#2,1) node[pos=0.5]{#3};
  \end{pgfonlayer}
  \pgfmathsetmacro\start{#1+0.5}
  \pgfmathsetmacro\finish{#1+#2-0.5}
%  \foreach \x in {\start,...,\finish}
%    \node [below, align=center] at (\x, 0) {RO \\ 0};
}
\newcommand{\bitlabels}[2]{
  \foreach \bit in {1,...,#1}{
     \pgfmathsetmacro\result{#2}
     \node [above] at (\bit-0.5, 1) {\pgfmathprintnumber{\result}};
   }
}

\begin{document}
\begin{tikzpicture}
\bitrect{16}{16-\bit}
\rwbits{0}{1}{SEL}
\rwbits{1}{1}{SEL}
\rwbits{2}{1}{SEL}
\robits{3}{5}{}
\rwbits{8}{1}{DIR}
\rwbits{9}{7}{PWM}
\end{tikzpicture}
\end{document}

使用此前导码的其他变体包括将 SEL 位分组为类似于 PWM 位:

在此处输入图片描述

\begin{tikzpicture}
\bitrect{16}{16-\bit}
\rwbits{0}{3}{SEL}
\robits{3}{5}{}
\rwbits{8}{1}{DIR}
\rwbits{9}{7}{PWM}
\end{tikzpicture}

或者你给出的第一个例子:

在此处输入图片描述

\begin{tikzpicture}
% make sure to uncomment \foreach loops in \robits and \rwbits, and last node in \bitrect
% 16 bits, indices decreasing from 31...16
\bitrect{16}{32-\bit} % {length}{index label expression}
\rwbits{0}{3}{INTD} % {start bit}{length}{label}
\robits{3}{5}{reserved} % {start bit}{length}{label}
\rwbits{8}{3}{INTC}
\robits{11}{5}{reserved}
\end{tikzpicture}

相关内容