我想动态循环遍历指令字段,以便绘制包含它们的寄存器。我想出的代码如下:
\documentclass{standalone}
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{tikz}
\usetikzlibrary{calc, shapes.multipart}
% Instruction fields
\def\fields{cond/31/28, op/27/26, I/25/25, cmd/24/21, S/20/20, Rn/19/16, Rd/15/12, rot/11/8, immeight/7/0}
\def\fieldlen{9}
% Instruction register
\tikzset{instr_reg/.style={
draw,
thick,
inner sep = 4pt,
rectangle split,
rectangle split parts = \fieldlen,
rectangle split horizontal,
anchor = center,
minimum height = \cellheight,
}
}
% Sizes
\pgfmathsetmacro{\bitnum}{32}
\def\scalefactor{100}
\def\cellheight{25pt}
\begin{document}
% Field widths
\foreach \field/\high/\low in \fields {
\pgfmathsetmacro{\width}{((\high-\low + 1) / \bitnum) * \scalefactor}
\expandafter\xdef\csname \field w\endcsname{\width mm}
}
\begin{tikzpicture}
% Draw node
\node[instr_reg] (adds_i) at (0,0) {
\foreach \field/\high/\low [count=\i from 2] in \fields {
\parbox[c][\cellheight][c]{\csname\field w\endcsname}{\centering \field}
}
};
\end{tikzpicture}
\end{document}
但每当我将 tikz 放置\nodepart{<number>}
在下方时,都会输出有关或被遗忘的\parbox
错误。\endgroup
}
以下是想要的输出:
答案1
这里有一个 vie 包bytefield
,它可以帮助您解决您描述的 Tikz 问题:
- 它不是基于 Tikz
- 当然,你也可以添加一些 Tikz
此代码已根据手册第 10 页的示例进行了调整。除了使用,\bitbox
您还可以将其用于\wordbox
更高的字段。
为了处理任意寄存器,您可以创建一个或多个(\newcommand
带或不带循环)。
\documentclass[10pt,a4paper]{article}
\usepackage{bytefield}
\usepackage{tikz}
\begin{document}
\begin{bytefield}[ % not based on Tikz
bitformatting={\small\bfseries},
bitwidth=auto,
endianness=big]{32}
\bitheader{0-31} \\
\bitbox{4}{cond} &
% ~~~ but include some Tikz if you like ~~~~~~~~~~
\bitbox{2}{\tikz{\node{op} node[anchor=north,font=\tiny,blue]{00};}} &
\bitbox{1}{I} &
\bitbox{4}{cmd} &
\bitbox{1}{S} &
\bitbox{4}{$R_n$} &
\bitbox{4}{$R_d$} &
\bitbox{4}{rot} &
\bitbox{8}{imm8} \\
\end{bytefield}
\end{document}