---
title: 湖科大教书匠计算机组成原理微课堂
author: 萑澈
pubDatetime: 2026-11-09T08:29:46Z
featured: false
draft: false
printable: true
copyable: true
tags:
  - 计算机组成原理
  - cs408
  - 考研
  - 湖科大教书匠
description: 湖科大教书匠《计算机组成原理微课堂》全课程总结。视频：https://www.bilibili.com/video/BV1qG41197E4/。覆盖数据表示、运算方法、存储系统、指令系统、CPU、总线与 I/O，含 101 张结构/时序 SVG
---



## 第1章 计算机系统概述

### 1.1 电子计算机的四代发展与摩尔定律

计算机的理论基础与第一台电子计算机的雏形在 1936—1946 年间相继出现：

- <strong>1936 图灵机</strong>：图灵提出由<strong>一条带子、一个读写头、一个控制装置</strong>构成的理想计算模型，能执行任何可计算函数。它是“存储程序”思想的最早来源，冯·诺依曼本人强调其存储程序思想主要来自图灵。
- <strong>1938 香农</strong>：硕士论文《继电器和开关电路的符号分析》揭示布尔代数与开关电路的对应关系，为数字电路设计与分析奠定数学基础。
- <strong>1942 ABC：阿塔纳索夫与贝瑞研制，使用 300 个电子管，不可编程</strong>，仅用于求解线性方程组，只是样机。
- <strong>1946 ENIAC：莫克利与艾克特研制，18000 个电子管、170 m²、30 吨、150 kW，采用十进制</strong>，每秒 5000 次加法、400 次乘法，用于弹道计算（20 分钟缩短到 30 秒）。ENIAC 无内存储器，靠旋钮和接插线设置程序，设置一次需数天。
- 1973 年美国明尼苏达地区法院判决：第一台电子数字计算机的真正发明人是<strong>阿塔纳索夫</strong>。

<figure class="cpd-figure not-content" id="hco-w1-fig-1">
  <svg viewBox="0 0 720 316" role="img" aria-label="电子计算机发展里程碑与四代逻辑元件演进图">
    <defs>
      <marker id="hco-w1-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="142" rx="6"/>
    <line class="fig-line-accent" x1="28" y1="88" x2="692" y2="88" marker-end="url(#hco-w1-arr-1)"/>
    <line class="fig-line" x1="98" y1="68" x2="98" y2="88"/>
    <line class="fig-line" x1="208" y1="88" x2="208" y2="106"/>
    <line class="fig-line" x1="332" y1="68" x2="332" y2="88"/>
    <line class="fig-line" x1="458" y1="88" x2="458" y2="106"/>
    <line class="fig-line" x1="590" y1="68" x2="590" y2="88"/>
    <circle class="fig-dot" cx="98" cy="88" r="4"/>
    <circle class="fig-dot" cx="208" cy="88" r="4"/>
    <circle class="fig-dot" cx="332" cy="88" r="4"/>
    <circle class="fig-dot" cx="458" cy="88" r="4"/>
    <circle class="fig-dot" cx="590" cy="88" r="4"/>
    <rect class="fig-box-blue" x="26" y="24" width="144" height="44" rx="5"/>
    <rect class="fig-box" x="136" y="106" width="144" height="44" rx="5"/>
    <rect class="fig-box" x="256" y="24" width="152" height="44" rx="5"/>
    <rect class="fig-box-orange" x="362" y="106" width="192" height="44" rx="5"/>
    <rect class="fig-box-purple" x="492" y="24" width="196" height="44" rx="5"/>
    <g text-anchor="middle" font-size="12">
      <text class="fig-blue" x="98" y="42">1936 图灵机</text>
      <text class="fig-muted" x="98" y="59" font-size="11">理想模型·存储程序源头</text>
      <text class="fig-label" x="208" y="124">1938 香农论文</text>
      <text class="fig-muted" x="208" y="141" font-size="11">布尔代数 ↔ 开关电路</text>
      <text class="fig-label" x="332" y="42">1942 ABC 样机</text>
      <text class="fig-muted" x="332" y="59" font-size="11">300 电子管·不可编程</text>
      <text class="fig-orange" x="458" y="124">1945 EDVAC / 1946 ENIAC</text>
      <text class="fig-muted" x="458" y="141" font-size="11">存储程序草案 vs 十进制手插线</text>
      <text class="fig-purple" x="590" y="42" font-size="11">1965 摩尔定律 → 2013</text>
      <text class="fig-muted" x="590" y="59" font-size="9.2">18～24 月翻番 → 放缓至约 3 年</text>
    </g>
    <rect class="fig-box-subtle" x="16" y="168" width="688" height="132" rx="6"/>
    <line class="fig-line-accent" x1="178" y1="244" x2="194" y2="244" marker-end="url(#hco-w1-arr-1)"/>
    <line class="fig-line-accent" x1="346" y1="244" x2="362" y2="244" marker-end="url(#hco-w1-arr-1)"/>
    <line class="fig-line-accent" x1="514" y1="244" x2="530" y2="244" marker-end="url(#hco-w1-arr-1)"/>
    <rect class="fig-box-orange" x="26" y="198" width="152" height="90" rx="5"/>
    <rect class="fig-box-blue" x="194" y="198" width="152" height="90" rx="5"/>
    <rect class="fig-box-purple" x="362" y="198" width="152" height="90" rx="5"/>
    <rect class="fig-box-green" x="530" y="198" width="164" height="90" rx="5"/>
    <text class="fig-title" x="360" y="188" text-anchor="middle" font-size="12">四代电子计算机演进（划分唯一依据：逻辑元件）</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-orange" x="102" y="218" font-size="12">① 第一代：电子管</text>
      <text class="fig-label" x="102" y="236">速度：几千～几万次/秒</text>
      <text class="fig-muted" x="102" y="254">延迟线/磁鼓 · 穿孔纸带</text>
      <text class="fig-muted" x="102" y="272">机器语言 · 无操作系统</text>
      <text class="fig-blue" x="270" y="218" font-size="12">② 第二代：晶体管</text>
      <text class="fig-label" x="270" y="236">速度：十万～百万次/秒</text>
      <text class="fig-muted" x="270" y="254">磁芯主存 · 磁带外存</text>
      <text class="fig-muted" x="270" y="272">汇编语言 · 单道批处理</text>
      <text class="fig-purple" x="438" y="218" font-size="12">③ 第三代：集成电路</text>
      <text class="fig-label" x="438" y="236">速度：百万～千万次/秒</text>
      <text class="fig-muted" x="438" y="254">半导体主存 · 磁盘为主</text>
      <text class="fig-muted" x="438" y="272">高级语言 · 多道/分时 OS</text>
      <text class="fig-green" x="612" y="218" font-size="10.8">④ 第四代：超大规模 IC</text>
      <text class="fig-label" x="612" y="236" font-size="9.5">速度：十亿～亿亿次/秒</text>
      <text class="fig-muted" x="612" y="254" font-size="9.5">大容量半导体主存与辅存</text>
      <text class="fig-muted" x="612" y="272" font-size="9.5">现代 OS · 微型化与巨型化</text>
    </g>
  </svg>
  <figcaption>图 1.1：计算机早期理论与样机里程碑，以及按逻辑元件划分的四代演进对比</figcaption>
</figure>

四代计算机的划分依据是<strong>逻辑元件</strong>，性能与软件同步演进：

| 代 | 逻辑元件 | 运算速度 | 存储与 I/O | 软件 |
|---|---|---|---|---|
| 一 | 电子管 | 几千～几万次/秒 | 水银延迟线、磁鼓、磁芯；穿孔卡片/纸带 | 机器语言，无操作系统 |
| 二 | 晶体管 | 数十万～数百万次/秒 | 磁芯主存，磁鼓、磁带；卡片、打印机 | 汇编语言，出现 FORTRAN、COBOL；单道批处理 |
| 三 | 集成电路 | 数百万～数千万次/秒 | 半导体主存，磁盘为主；键盘、鼠标、显示器 | 高级语言发展（B 语言即 C 前身）；多道批处理、分时系统；出现巨型机/小型机 |
| 四 | 超大规模集成电路 | 数十亿～数千亿次/秒 | 半导体主存与辅存容量大增 | C、C++、Java、Python；现代操作系统 |

发展规律：体积更小、功耗更低、可靠性更高、速度更快，全部依赖电子器件进步。

<strong>摩尔定律</strong>：1965 年戈登·摩尔预测，集成电路上可容纳的晶体管数量约每 <strong>18～24 个月</strong>翻一番，处理器性能约每两年翻一番而价格降为一半。50 余年发展基本验证了该预测；随着工艺接近极限，2013 年起放缓至约 3 年翻一番。

当前计算机朝两极化发展：一是<strong>微型化</strong>（低功耗、多用途），二是<strong>巨型化</strong>（超高速、并行处理、智能化）。我国“神威·太湖之光”每秒可进行 <strong>9.3 亿亿次</strong>浮点运算（2021 年 408 选择题考点）。

### 1.2 冯·诺依曼计算机的结构与五大部件

ENIAC 的通用性被“手工设置程序”严重削弱。1944 年提出研制可存储程序的新机器 EDVAC，1945 年冯·诺依曼写出《EDVAC 报告书》草案，确立现代计算机的设计道路。

<strong>冯·诺依曼计算机的五个特点</strong>：

1. 指令和数据均用<strong>二进制</strong>表示；
2. 指令和数据都存放在<strong>存储器</strong>中，按地址访问；
3. 指令在存储器中<strong>按顺序存放</strong>，一般顺序执行；
4. 指令由<strong>操作码 + 地址码</strong>组成：操作码指明做什么，地址码指明操作数在存储器中的位置；
5. 机器以<strong>运算器为中心</strong>，输入输出设备与存储器之间的数据传送通过运算器完成。

对应的五大部件：<strong>运算器、控制器、存储器、输入设备、输出设备</strong>。

| 部件 | 核心 | 功能 |
|---|---|---|
| 输入设备 | — | 把人们熟悉的信息形式转换为计算机可识别的形式（键盘、鼠标、扫描仪、摄像头） |
| 输出设备 | — | 把运算结果转换为人们熟悉的形式（显示器、打印机） |
| 存储器 | — | 主存存放程序和数据，可直接与 CPU 交换信息；辅存（外存）辅助主存，<strong>其信息必须调入主存后才能被 CPU 访问</strong> |
| 运算器 | ALU | 算术运算与逻辑运算，另含若干寄存器 |
| 控制器 | CU | 解释存储器中的指令并发出操作命令，I/O 也受其控制，另含若干寄存器 |

运算器与控制器在逻辑与电路结构上紧密关联，大规模集成电路出现后被集成在同一芯片，即 <strong>CPU</strong>；CPU 与主存合称<strong>主机</strong>；I/O 设备与外存合称<strong>外部设备</strong>；五大部件通过<strong>总线</strong>互联（数据线、控制线、反馈线均属总线）。

<figure class="cpd-figure not-content" id="hco-w1-fig-2">
  <svg viewBox="0 0 720 344" role="img" aria-label="早期冯·诺依曼结构与现代以存储器为中心计算机结构对比">
    <defs>
      <marker id="hco-w1-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="338" height="312" rx="6"/>
    <rect class="fig-box-subtle" x="366" y="16" width="338" height="312" rx="6"/>
    <line class="fig-line-accent" x1="108" y1="78" x2="132" y2="78" marker-end="url(#hco-w1-arr-2)"/>
    <line class="fig-line-accent" x1="238" y1="78" x2="262" y2="78" marker-end="url(#hco-w1-arr-2)"/>
    <path class="fig-line-accent" d="M 94 156 L 94 124 L 160 124 L 160 106" marker-start="url(#hco-w1-arr-2)" marker-end="url(#hco-w1-arr-2)"/>
    <line class="fig-line" x1="160" y1="186" x2="206" y2="186" marker-end="url(#hco-w1-arr-2)"/>
    <path class="fig-line-dashed" d="M 272 156 L 272 124 L 210 124 L 210 106" marker-end="url(#hco-w1-arr-2)"/>
    <path class="fig-line-dashed" d="M 240 218 L 240 236 L 94 236 L 94 218" marker-end="url(#hco-w1-arr-2)"/>
    <rect class="fig-box" x="26" y="58" width="82" height="40" rx="4"/>
    <rect class="fig-box-orange" x="132" y="50" width="106" height="56" rx="5"/>
    <rect class="fig-box" x="262" y="58" width="82" height="40" rx="4"/>
    <rect class="fig-box-blue" x="28" y="156" width="132" height="62" rx="5"/>
    <rect class="fig-box-purple" x="206" y="156" width="136" height="62" rx="5"/>
    <rect class="fig-box-red" x="28" y="250" width="314" height="64" rx="5"/>
    <rect class="fig-box-dashed" x="380" y="48" width="310" height="132" rx="6"/>
    <line class="fig-line-accent" x1="516" y1="114" x2="550" y2="114" marker-start="url(#hco-w1-arr-2)" marker-end="url(#hco-w1-arr-2)"/>
    <line class="fig-line-accent" x1="455" y1="166" x2="455" y2="202" marker-start="url(#hco-w1-arr-2)" marker-end="url(#hco-w1-arr-2)"/>
    <line class="fig-line-dashed" x1="616" y1="166" x2="616" y2="202" marker-end="url(#hco-w1-arr-2)"/>
    <rect class="fig-box-green" x="394" y="68" width="122" height="98" rx="5"/>
    <rect class="fig-box-accent" x="550" y="68" width="126" height="98" rx="5"/>
    <rect class="fig-box-blue" x="380" y="202" width="310" height="42" rx="5"/>
    <rect class="fig-box-green" x="380" y="254" width="310" height="60" rx="5"/>
    <rect class="fig-box" x="404" y="176" width="102" height="18" rx="3"/>
    <rect class="fig-box" x="580" y="176" width="72" height="18" rx="3"/>
    <text class="fig-title" x="185" y="36" text-anchor="middle" font-size="12">（a）冯·诺依曼结构：以运算器为中心</text>
    <text class="fig-title" x="535" y="36" text-anchor="middle" font-size="12">（b）现代计算机结构：以存储器为中心</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-label" x="67" y="82" font-size="12">输入设备</text>
      <text class="fig-orange" x="185" y="73" font-size="12">运算器 ALU</text>
      <text class="fig-muted" x="185" y="92">一切传送中转站</text>
      <text class="fig-label" x="303" y="82" font-size="12">输出设备</text>
      <text class="fig-blue" x="94" y="180" font-size="12">主存储器</text>
      <text class="fig-muted" x="94" y="198">指令+数据按址存放</text>
      <text class="fig-purple" x="274" y="180" font-size="12">控制器 CU</text>
      <text class="fig-muted" x="274" y="198">解释指令·发控制信号</text>
      <text class="fig-muted" x="183" y="179" font-size="10">取指</text>
      <text class="fig-muted" x="242" y="118" font-size="10">控制</text>
      <text class="fig-red" x="185" y="273" font-size="12">瓶颈：I/O ↔ 主存传送必经运算器</text>
      <text class="fig-muted" x="185" y="293">实线：数据/指令流 · 虚线：控制流</text>
      <text class="fig-muted" x="185" y="308">运算器频繁被 I/O 占用，浪费运算时间</text>
      <text class="fig-accent" x="678" y="61" text-anchor="end" font-size="11">主机 = CPU + 主存</text>
      <text class="fig-green" x="455" y="90" font-size="12">主存储器 MM</text>
      <text class="fig-muted" x="455" y="109">MAR + MDR 寻址</text>
      <text class="fig-label" x="455" y="129">[操作码 | 地址码]</text>
      <text class="fig-muted" x="455" y="149">二进制顺序存放</text>
      <text class="fig-accent" x="613" y="90" font-size="12">CPU 芯片</text>
      <text class="fig-label" x="613" y="111" font-size="9.5">运算器（ALU+GPR）</text>
      <text class="fig-label" x="613" y="131" font-size="9.5">控制器（CU+PC/IR）</text>
      <text class="fig-muted" x="613" y="151">超大规模集成合一</text>
      <text class="fig-green" x="455" y="189" font-size="10">直接交换数据</text>
      <text class="fig-muted" x="616" y="189" font-size="10">I/O 控制</text>
      <text class="fig-blue" x="535" y="220" font-size="10.2">外部设备 = 输入设备 + 输出设备 + 辅存（外存）</text>
      <text class="fig-muted" x="535" y="237" font-size="9">通过总线互联（辅存信息须先调入主存才能被 CPU 访问）</text>
      <text class="fig-green" x="535" y="276" font-size="10.2">★本质特征：存储程序 + 指令与数据同等按址存放</text>
      <text class="fig-muted" x="535" y="297" font-size="9.2">I/O 可经总线/DMA 直访主存，解放 CPU 专注运算</text>
    </g>
  </svg>
  <figcaption>图 1.2：冯·诺依曼机（以运算器为中心）与现代计算机（以存储器为中心、主机与外设分离）结构对比</figcaption>
</figure>

> <strong>注意</strong>：以运算器为中心时，每次 I/O 都要运算器参与，浪费大量可用于运算的时间；现代计算机改为以存储器为中心，I/O 设备可直接与存储器交换数据，效率更高。

> <strong>★考点</strong>：冯·诺依曼机的核心特征是“<strong>存储程序</strong>、指令与数据同等存放、按地址顺序访问”，而不是“以运算器为中心”——后者恰是其缺点。

### 1.3 计算机软件与语言翻译程序

软件 = 程序 + 数据。分两类：

- <strong>系统软件</strong>：为高效、安全地使用和管理硬件资源，以及为开发和运行应用软件而编制，位于硬件与应用软件之间，与具体应用关系不大。包括<strong>操作系统、数据库管理系统、语言处理程序（汇编程序、编译程序、解释程序）、服务性程序</strong>（诊断、排错）。
- <strong>应用软件</strong>：为解决特定应用问题编制，如科学计算、工程设计、数据处理、企业管理、自动控制，以及浏览器、办公软件、游戏、播放器。

语言的三层与翻译程序：

| 语言 | 程序名称 | 翻译程序 | 特点 |
|---|---|---|---|
| 机器语言 | 目标程序 | 无需翻译 | 二进制编码，可直接执行，速度最快，可读性差、易错 |
| 汇编语言 | 汇编语言程序 | 汇编程序（汇编器） | 助记符表示指令，仍面向机器，需熟悉指令集 |
| 高级语言 | 源程序 | 编译程序（编译器） | 与机器无关、面向使用者；编译可先到汇编再汇编，也可直接生成机器码 |
| 高级语言 | 源程序 | 解释程序（解释器） | 按执行顺序逐条翻译并执行，<strong>不产生完整目标程序</strong> |

编译程序、解释程序、汇编程序统称<strong>翻译程序。使用 GCC 编译 C 程序的完整过程是：预处理 → 编译 → 汇编 → 链接</strong>。C 源程序经编译得到汇编程序、再汇编得到机器语言程序，开发环境通常以<strong>十六进制</strong>显示机器码与地址，便于阅读。

<figure class="cpd-figure not-content" id="hco-w1-fig-3">
  <svg viewBox="0 0 720 326" role="img" aria-label="GCC四阶段编译链接流水线与解释程序执行方式对比图">
    <defs>
      <marker id="hco-w1-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="192" rx="6"/>
    <rect class="fig-box-subtle" x="16" y="218" width="688" height="92" rx="6"/>
    <line class="fig-line-accent" x1="146" y1="74" x2="184" y2="74" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line-accent" x1="316" y1="74" x2="354" y2="74" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line-accent" x1="494" y1="74" x2="532" y2="74" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line-accent" x1="612" y1="100" x2="612" y2="136" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line-accent" x1="532" y1="163" x2="486" y2="163" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line-accent" x1="296" y1="163" x2="246" y2="163" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line" x1="238" y1="272" x2="278" y2="272" marker-end="url(#hco-w1-arr-3)"/>
    <line class="fig-line" x1="478" y1="272" x2="518" y2="272" marker-end="url(#hco-w1-arr-3)"/>
    <rect class="fig-box-blue" x="28" y="48" width="118" height="52" rx="5"/>
    <rect class="fig-box" x="184" y="48" width="132" height="52" rx="5"/>
    <rect class="fig-box-purple" x="354" y="48" width="140" height="52" rx="5"/>
    <rect class="fig-box-orange" x="532" y="48" width="160" height="52" rx="5"/>
    <rect class="fig-box-accent" x="532" y="136" width="160" height="54" rx="5"/>
    <rect class="fig-box-green" x="296" y="136" width="190" height="54" rx="5"/>
    <rect class="fig-box-green" x="28" y="136" width="218" height="54" rx="5"/>
    <rect class="fig-box-blue" x="28" y="246" width="210" height="52" rx="5"/>
    <rect class="fig-box-orange" x="278" y="246" width="200" height="52" rx="5"/>
    <rect class="fig-box-red" x="518" y="246" width="174" height="52" rx="5"/>
    <text class="fig-title" x="360" y="36" text-anchor="middle" font-size="12">（a）编译方式（以 GCC 为例：预处理 → 编译 → 汇编 → 链接，生成完整机器码程序）</text>
    <text class="fig-title" x="360" y="236" text-anchor="middle" font-size="12">（b）解释方式（逐条翻译并立即执行，不产生完整目标程序）</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-blue" x="87" y="70" font-size="12">C 源程序 (.c)</text>
      <text class="fig-muted" x="87" y="88">高级语言·机器无关</text>
      <text class="fig-accent" x="165" y="66" font-size="8.2">①预处理</text>
      <text class="fig-label" x="250" y="70" font-size="12">预处理文件 (.i)</text>
      <text class="fig-muted" x="250" y="88">展开宏与头文件</text>
      <text class="fig-accent" x="335" y="66" font-size="8.6">②编译</text>
      <text class="fig-purple" x="424" y="70" font-size="11.2">汇编语言程序 (.s)</text>
      <text class="fig-muted" x="424" y="88">助记符·面向 ISA</text>
      <text class="fig-accent" x="513" y="66" font-size="8.8">③汇编</text>
      <text class="fig-orange" x="612" y="70" font-size="12">目标文件 (.o)</text>
      <text class="fig-muted" x="612" y="88">可重定位机器码</text>
      <text class="fig-accent" x="642" y="122" font-size="10">④链接</text>
      <text class="fig-accent" x="612" y="158" font-size="12">链接器 (ld) + 库</text>
      <text class="fig-muted" x="612" y="177">合并库函数·重定位</text>
      <text class="fig-green" x="391" y="158" font-size="12">可执行文件 (a.out)</text>
      <text class="fig-muted" x="391" y="177">机器码（常以 16 进制显示）</text>
      <text class="fig-green" x="137" y="158" font-size="12">CPU 直接高速执行</text>
      <text class="fig-muted" x="137" y="177">一次编译，可重复脱离源码运行</text>
      <text class="fig-blue" x="133" y="268" font-size="12">高级语言源程序</text>
      <text class="fig-muted" x="133" y="286">按执行顺序逐句送入</text>
      <text class="fig-orange" x="378" y="268" font-size="12">解释程序（解释器）</text>
      <text class="fig-muted" x="378" y="286">翻译一条语句 → 立即执行一条</text>
      <text class="fig-red" x="605" y="268" font-size="12">直接产生运行结果</text>
      <text class="fig-muted" x="605" y="286" font-size="9.8">★不生成完整目标程序</text>
    </g>
  </svg>
  <figcaption>图 1.3：C 语言四阶段编译链接流水线（生成完整机器码目标程序）与解释执行方式的核心区别</figcaption>
</figure>

> <strong>注意</strong>：高级语言程序并不一定逐层翻译，有的编译程序可直接生成机器语言程序；“编译”与“解释”的区别在于是否产生完整的目标程序。

### 1.4 计算机系统的层次结构与软硬件逻辑功能等价性

分层思想：每一层向上层提供<strong>简洁抽象的接口</strong>，其实现细节对上层<strong>透明</strong>；解决问题就是各层逐层转换的过程。计算机系统可划分为六个抽象层次（一种常见分法，并非唯一标准）：

| 层 | 名称 | 设计者 | 说明 |
|---|---|---|---|
| 6 | 高级语言层 | 高级语言程序员 | 使用与机器无关的高级语言，只需懂语法、算法与数据结构 |
| 5 | 汇编语言层 | 汇编语言程序员 | 语句与机器指令一一对应，仍需熟悉机器内部组成与指令系统 |
| 4 | 操作系统层 | 操作系统设计者 | 统一管理和调度软硬件资源；需懂设计理论、机器细节、汇编与 C |
| 3 | 指令集体系结构 ISA | 硬件系统架构师 | 定义机器可执行的全部机器指令：做什么操作、操作数在哪、何种类型 |
| 2 | 微程序层 | 硬件系统架构师 | 一条机器指令 → 一段微程序 → 若干微指令 → 一条或多条微操作；控制存储器存放微程序 |
| 1 | 逻辑电路层 | 硬件工程师 | 逻辑门、寄存器等，数字电路课程内容 |

第 1—3 层属<strong>硬件层</strong>，第 4—6 层属<strong>软件层</strong>；本课程主要讨论第 1—3 层。

<figure class="cpd-figure not-content" id="hco-w1-fig-4">
  <svg viewBox="0 0 720 336" role="img" aria-label="计算机系统六层抽象层次结构与ISA软硬件分界面图">
    <defs>
      <marker id="hco-w1-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="414" height="304" rx="6"/>
    <rect class="fig-box-subtle" x="442" y="16" width="262" height="304" rx="6"/>
    <line class="fig-line" x1="104" y1="30" x2="104" y2="148"/>
    <line class="fig-line" x1="104" y1="170" x2="104" y2="300"/>
    <line class="fig-line-accent" x1="28" y1="159" x2="418" y2="159" stroke-dasharray="6 3"/>
    <path class="fig-line-dashed" d="M 414 234 L 434 234 L 434 116 L 456 116" marker-end="url(#hco-w1-arr-4)"/>
    <rect class="fig-box-purple" x="118" y="28" width="296" height="36" rx="4"/>
    <rect class="fig-box-purple" x="118" y="70" width="296" height="36" rx="4"/>
    <rect class="fig-box-blue" x="118" y="112" width="296" height="36" rx="4"/>
    <rect class="fig-box-accent" x="118" y="170" width="296" height="36" rx="4"/>
    <rect class="fig-box-orange" x="118" y="216" width="296" height="36" rx="4"/>
    <rect class="fig-box-green" x="118" y="262" width="296" height="36" rx="4"/>
    <rect class="fig-box" x="28" y="149" width="84" height="20" rx="3"/>
    <rect class="fig-box" x="456" y="42" width="234" height="148" rx="5"/>
    <line class="fig-line" x1="573" y1="80" x2="573" y2="96" marker-end="url(#hco-w1-arr-4)"/>
    <line class="fig-line" x1="573" y1="124" x2="573" y2="140" marker-end="url(#hco-w1-arr-4)"/>
    <rect class="fig-box-accent" x="470" y="54" width="206" height="26" rx="4"/>
    <rect class="fig-box-orange" x="470" y="96" width="206" height="28" rx="4"/>
    <rect class="fig-box-green" x="470" y="140" width="206" height="38" rx="4"/>
    <rect class="fig-box-blue" x="456" y="200" width="234" height="106" rx="5"/>
    <text class="fig-title" x="573" y="34" text-anchor="middle" font-size="11.5">第 2 层微程序展开与软硬件等价性</text>
    <g font-size="11">
      <text class="fig-purple" x="62" y="84" text-anchor="middle" font-size="12">软件层</text>
      <text class="fig-muted" x="62" y="102" text-anchor="middle">（第 4—6 层）</text>
      <text class="fig-accent" x="70" y="163" text-anchor="middle" font-size="10">ISA 分界面</text>
      <text class="fig-green" x="62" y="226" text-anchor="middle" font-size="12">硬件层</text>
      <text class="fig-muted" x="62" y="244" text-anchor="middle">（第 1—3 层）</text>
      <text class="fig-orange" x="62" y="262" text-anchor="middle" font-size="10">本课核心范围</text>
      <text class="fig-purple" x="266" y="50" text-anchor="middle" font-size="12">6 高级语言层（算法/语法，机器无关）</text>
      <text class="fig-purple" x="266" y="92" text-anchor="middle" font-size="12">5 汇编语言层（助记符 ↔ 机器指令）</text>
      <text class="fig-blue" x="266" y="134" text-anchor="middle" font-size="12">4 操作系统层（软硬件资源管理调度）</text>
      <text class="fig-accent" x="266" y="192" text-anchor="middle" font-size="12">3 指令集体系结构 ISA（操作/寻址/类型）</text>
      <text class="fig-orange" x="266" y="238" text-anchor="middle" font-size="12">2 微程序层（控存 CM 解释机器指令）</text>
      <text class="fig-green" x="266" y="284" text-anchor="middle" font-size="12">1 数字逻辑电路层（门电路/寄存器）</text>
      <text class="fig-accent" x="573" y="71" text-anchor="middle">1 条机器指令（取入 IR）</text>
      <text class="fig-orange" x="573" y="114" text-anchor="middle">1 段微程序（含若干条微指令）</text>
      <text class="fig-green" x="573" y="156" text-anchor="middle">每条微指令 → 1 或多条微操作</text>
      <text class="fig-muted" x="573" y="172" text-anchor="middle" font-size="10">（微程序存放在控制存储器 CM 中）</text>
      <text class="fig-blue" x="573" y="220" text-anchor="middle" font-size="12">软硬件逻辑功能等价性</text>
      <text class="fig-label" x="468" y="241">• 硬件：并行快，成本高、难修改</text>
      <text class="fig-label" x="468" y="261">• 软件：成本低、易更新，速度较慢</text>
      <text class="fig-muted" x="468" y="281" font-size="10">例：386 硬解码卡 → 奔腾 MMX 软解码</text>
      <text class="fig-muted" x="468" y="297" font-size="10">下层实现细节对上层透明</text>
    </g>
  </svg>
  <figcaption>图 1.4：计算机系统六层抽象模型、ISA 软硬件分界面及微程序层映射关系</figcaption>
</figure>

<strong>软件与硬件的逻辑功能等价性</strong>：在特定条件下，用软件实现的逻辑功能也可用硬件电路实现，反之亦然，两者在功能上等效。选择依据是设计目标、技术水平与性价比：

- 硬件：性能高、响应快（电路可并行），适合高频交易、专用加速；但开发制造成本高、修改需重新设计制造。
- 软件：成本低、易修改更新，适合操作系统功能、逻辑不复杂且性能要求不高的场合。
- 实例：早期 33 MHz 的 386 上播放 VCD 需专用解码卡；486 上可用软件解码；1996 年奔腾 MMX 后软件解码普及，解码卡退出市场。

> <strong>★考点</strong>：ISA 是软硬件的分界面——它由硬件实现，又对软件（编译器、操作系统）可见；ISA 规定指令格式、操作类型、操作数存放位置与类型。

### 1.5 计算机系统的性能指标

性能指标衡量系统的工作效率、执行速度与处理能力，受硬件与软件共同影响（如 CPU 主频决定每秒可执行指令数，内存带宽影响读写速率，操作系统调度与算法优化影响响应与吞吐）。

<strong>（1）机器字长</strong>：CPU 一次能处理的二进制数据位数，等于内部整数运算 ALU 的位数、通用寄存器的宽度。字长越长，表示范围越大、精度越高，也影响运算速度。字长已从 8 位发展到 16/32/64 位乃至更高。

<strong>（2）存储容量</strong>：主存能存储的最大信息量。设存储单元数为 M、每单元 N 位：

$$
\text{Capacity} = N \times M \quad (\text{bit})
$$

由主存结构可得更实用的形式：

$$
\text{Capacity} = 2^{\text{MAR bits}} \times \text{MDR bits} \quad (\text{bit})
$$

其中<strong>存储字长</strong> = 每个存储单元存储的位数 = <strong>MDR 的位数</strong>；<strong>MAR 的位数</strong>决定存储单元个数 $2^n$。容量增大可减少程序运行期间对外存的访问，从而提高执行速度。

<figure class="cpd-figure not-content" id="hco-w1-fig-5">
  <svg viewBox="0 0 720 276" role="img" aria-label="机器字长与主存MAR、MDR及存储容量关系图">
    <defs>
      <marker id="hco-w1-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="214" height="202" rx="6"/>
    <rect class="fig-box-subtle" x="244" y="16" width="460" height="202" rx="6"/>
    <line class="fig-line-accent" x1="230" y1="82" x2="258" y2="82" marker-end="url(#hco-w1-arr-5)"/>
    <line class="fig-line-accent" x1="230" y1="168" x2="258" y2="168" marker-start="url(#hco-w1-arr-5)" marker-end="url(#hco-w1-arr-5)"/>
    <line class="fig-line" x1="390" y1="82" x2="418" y2="82" marker-end="url(#hco-w1-arr-5)"/>
    <line class="fig-line" x1="482" y1="82" x2="508" y2="82" marker-end="url(#hco-w1-arr-5)"/>
    <path class="fig-line-accent" d="M 600 156 L 600 163 L 390 163" marker-start="url(#hco-w1-arr-5)" marker-end="url(#hco-w1-arr-5)"/>
    <rect class="fig-box-accent" x="30" y="52" width="186" height="58" rx="5"/>
    <rect class="fig-box-blue" x="30" y="126" width="186" height="58" rx="5"/>
    <rect class="fig-box-orange" x="258" y="56" width="132" height="52" rx="5"/>
    <rect class="fig-box" x="418" y="52" width="64" height="60" rx="4"/>
    <rect class="fig-box-green" x="508" y="46" width="182" height="26" rx="3"/>
    <rect class="fig-box-green" x="508" y="76" width="182" height="26" rx="3"/>
    <rect class="fig-box" x="508" y="106" width="182" height="22" rx="3"/>
    <rect class="fig-box-green" x="508" y="132" width="182" height="26" rx="3"/>
    <rect class="fig-box-purple" x="258" y="138" width="132" height="46" rx="5"/>
    <rect class="fig-box-red" x="16" y="226" width="688" height="36" rx="5"/>
    <text class="fig-title" x="123" y="38" text-anchor="middle" font-size="12">CPU（决定机器字长）</text>
    <text class="fig-title" x="474" y="38" text-anchor="middle" font-size="12">主存储器（决定存储容量与存储字长）</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-accent" x="123" y="76" font-size="12">整数运算器 ALU</text>
      <text class="fig-muted" x="123" y="96">宽度 = 机器字长（如 32 位）</text>
      <text class="fig-blue" x="123" y="150" font-size="12">通用寄存器组 GPR</text>
      <text class="fig-muted" x="123" y="170">宽度 = 机器字长（如 32 位）</text>
      <text class="fig-orange" x="324" y="78" font-size="12">MAR（n 位）</text>
      <text class="fig-muted" x="324" y="96">决定单元数 M = 2^n</text>
      <text class="fig-label" x="450" y="78">地址</text>
      <text class="fig-label" x="450" y="96">译码</text>
      <text class="fig-green" x="599" y="63" font-size="9.8">单元 0（存储字长 N 位）</text>
      <text class="fig-green" x="599" y="93" font-size="9.8">单元 1（存储字长 N 位）</text>
      <text class="fig-muted" x="599" y="121" font-size="9.8">… 共 2^n 个存储单元 …</text>
      <text class="fig-green" x="599" y="149" font-size="9.8">单元 2^n - 1（N 位）</text>
      <text class="fig-purple" x="324" y="158" font-size="12">MDR（N 位）</text>
      <text class="fig-muted" x="324" y="175">位数 = 存储字长 N</text>
      <text class="fig-muted" x="123" y="206" font-size="9.5">★机器字长未必等于存储字长</text>
      <text class="fig-muted" x="474" y="206" font-size="9.2">总容量 = 存储单元数 × 存储字长 = 2^(MAR位数) × MDR位数 (bit)</text>
      <text class="fig-red" x="360" y="249" font-size="9.8">例：MAR = 16 位 → 2^16 = 64K 个单元；MDR = 32 位（4B）→ 主存总容量 = 64K × 32 bit = 256 KB</text>
    </g>
  </svg>
  <figcaption>图 1.5：CPU 机器字长与主存 MAR、MDR 及存储容量的硬件对应关系</figcaption>
</figure>

<strong>（3）吞吐量</strong>：单位时间内系统能处理的信息量，主要受 CPU 处理能力、内存访问速度、外存访问速度影响。

<strong>（4）响应时间</strong>：从提交作业到系统完成作业的时间。

响应时间 = CPU 时间 + 其他时间；CPU 时间 = <strong>CPU 执行时间</strong>（执行用户程序本身）+ <strong>系统 CPU 时间（为执行程序花费在操作系统上的时间）。其他时间包括访问内存、外存及外部设备的时间。由于二者难以精确区分，未特别说明时以 CPU 执行时间评价性能</strong>。

<strong>（5）与运算速度相关的指标</strong>：时钟频率 $f$ 与时钟周期 $T=1/f$、CPI（每条指令平均时钟周期数）、CPU 执行时间、MIPS（每秒百万条指令）、MFLOPS（每秒百万次浮点运算）。

$$
T_{CPU} = IC \times CPI \times T_{clk} = \frac{IC \times CPI}{f}
$$

$$
MIPS = \frac{IC}{T_{CPU} \times 10^6} = \frac{f}{CPI \times 10^6}
$$

<figure class="cpd-figure not-content" id="hco-w1-fig-6">
  <svg viewBox="0 0 720 318" role="img" aria-label="计算机系统响应时间拆解与CPU执行时间、CPI、MIPS指标关系图">
    <defs>
      <marker id="hco-w1-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="96" rx="6"/>
    <rect class="fig-box-accent" x="28" y="46" width="256" height="52" rx="4"/>
    <rect class="fig-box-purple" x="288" y="46" width="186" height="52" rx="4"/>
    <rect class="fig-box" x="478" y="46" width="214" height="52" rx="4"/>
    <line class="fig-line-accent" x1="234" y1="184" x2="254" y2="184" marker-end="url(#hco-w1-arr-6)"/>
    <line class="fig-line-accent" x1="466" y1="184" x2="486" y2="184" marker-end="url(#hco-w1-arr-6)"/>
    <rect class="fig-box-blue" x="16" y="124" width="218" height="122" rx="6"/>
    <rect class="fig-box-accent" x="254" y="124" width="212" height="122" rx="6"/>
    <rect class="fig-box-orange" x="486" y="124" width="218" height="122" rx="6"/>
    <rect class="fig-box-red" x="16" y="258" width="688" height="44" rx="5"/>
    <text class="fig-title" x="360" y="36" text-anchor="middle" font-size="12">作业响应时间 = CPU 时间（用户 CPU 执行时间 + 系统 CPU 时间）+ 其他等待时间</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-accent" x="156" y="68" font-size="12">用户 CPU 执行时间 T_CPU</text>
      <text class="fig-muted" x="156" y="86">执行用户程序本身（★默认评价指标）</text>
      <text class="fig-purple" x="381" y="68" font-size="12">系统 CPU 时间</text>
      <text class="fig-muted" x="381" y="86">OS 调度与系统服务耗时</text>
      <text class="fig-label" x="585" y="68" font-size="12">其他时间</text>
      <text class="fig-muted" x="585" y="86" font-size="9.8">访存等待 · 外存与 I/O 交互</text>
      <text class="fig-blue" x="125" y="146" font-size="12">① 时钟与加权 CPI</text>
      <text class="fig-label" x="125" y="168">时钟周期 T_clk = 1 / f</text>
      <text class="fig-label" x="125" y="190">CPI = Σ(IC_i × CPI_i) / IC</text>
      <text class="fig-muted" x="125" y="212">总时钟周期数 = IC × CPI</text>
      <text class="fig-muted" x="125" y="232" font-size="10">（IC：动态执行的指令总条数）</text>
      <text class="fig-accent" x="360" y="146" font-size="12">② CPU 执行时间（黄金标准）</text>
      <text class="fig-label" x="360" y="170">T_CPU = IC × CPI × T_clk</text>
      <text class="fig-label" x="360" y="192">      = (IC × CPI) / f</text>
      <text class="fig-muted" x="360" y="214">IC 由 ISA/编译器决定</text>
      <text class="fig-muted" x="360" y="232" font-size="10">CPI 由微架构决定 · f 由工艺决定</text>
      <text class="fig-orange" x="595" y="146" font-size="12">③ 运算速率指标</text>
      <text class="fig-label" x="595" y="168" font-size="9.8">MIPS = IC / (T_CPU × 10^6)</text>
      <text class="fig-label" x="595" y="190" font-size="9.8">     = f / (CPI × 10^6)</text>
      <text class="fig-label" x="595" y="212" font-size="9.8">MFLOPS = 浮点次数 / (T × 10^6)</text>
      <text class="fig-muted" x="595" y="232" font-size="9.2">GFLOPS / TFLOPS / PFLOPS</text>
      <text class="fig-red" x="360" y="276" font-size="9.8">★408 必考陷阱：主频 f 高 ≠ 速度快；不同机器 ISA 的指令数 IC 不同时，MIPS 高不一定 T_CPU 更短！</text>
      <text class="fig-muted" x="360" y="294" font-size="9.8">未特别说明时，一律以“CPU 执行时间 T_CPU 最短”作为衡量计算机性能优劣的最终依据</text>
    </g>
  </svg>
  <figcaption>图 1.6：响应时间构成拆解，以及主频、CPI、CPU 执行时间与 MIPS/MFLOPS 的定量关系</figcaption>
</figure>

> <strong>注意</strong>：<strong>存储字长 ≠ 机器字长</strong>。机器字长 32 位的计算机，存储字长可为 16、32 或 64 位。
>
> <strong>★考点</strong>：MAR 位数决定<strong>存储单元个数</strong>（$2^n$），MDR 位数等于<strong>存储字长</strong>；二者乘积才是总容量。不要把 MAR 位数误当作单元个数。

## 第2章 数据的表示

### 2.1 数值数据的分类与机器码

计算机内部用二进制表示一切数据：数值、字符、图像、音频都转为二进制存入寄存器或存储器。本章只讨论<strong>数值数据</strong>。

```
数值数据
├── 无符号数：全部位表示数值，如地址、门牌号
└── 有符号数（最高位为符号位）
    ├── 定点数：小数点位置固定（隐含，不存储）
    │   ├── 定点整数：小数点固定在最低位右侧
    │   └── 定点小数（纯小数）：小数点固定在符号位与数值位之间
    └── 浮点数：小数点位置浮动
        ├── 单精度（32 位：1 + 8 + 23）
        └── 双精度（64 位：1 + 11 + 52）
```

<strong>真值：日常生活中带正负号的实际数值。机器数 / 机器码</strong>：数值在机器中的编码，含符号位。同一个二进制串可解释为无符号数、有符号整数、纯小数或带小数，取决于编码格式。

<figure class="cpd-figure not-content" id="hco-w1-fig-7">
  <svg viewBox="0 0 720 332" role="img" aria-label="同一32位二进制机器数在不同编码格式下的四种真值解释与位段结构">
    <defs>
      <marker id="hco-w1-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="200" rx="6"/>
    <path class="fig-line" d="M 180 78 L 180 96 L 188 96 L 188 106" marker-end="url(#hco-w1-arr-7)"/>
    <path class="fig-line" d="M 540 78 L 540 96 L 532 96 L 532 106" marker-end="url(#hco-w1-arr-7)"/>
    <rect class="fig-box-accent" x="48" y="42" width="624" height="36" rx="5"/>
    <rect class="fig-box-blue" x="28" y="106" width="320" height="44" rx="5"/>
    <rect class="fig-box-orange" x="372" y="106" width="320" height="44" rx="5"/>
    <rect class="fig-box-purple" x="28" y="158" width="320" height="46" rx="5"/>
    <rect class="fig-box-green" x="372" y="158" width="320" height="46" rx="5"/>
    <rect class="fig-box-subtle" x="16" y="226" width="688" height="90" rx="6"/>
    <rect class="fig-box" x="28" y="252" width="206" height="50" rx="4"/>
    <rect class="fig-box" x="250" y="252" width="206" height="50" rx="4"/>
    <rect class="fig-box-accent" x="472" y="252" width="220" height="50" rx="4"/>
    <text class="fig-title" x="360" y="34" text-anchor="middle" font-size="12">同一 32 位寄存器内容（0xC1480000）在不同数据类型下的语义解释</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-accent" x="360" y="65" font-size="10.5">1 | 1000 0010 | 1001 0000 0000 0000 0000 000 （共 32 bit）</text>
      <text class="fig-blue" x="188" y="124" font-size="12">① 无符号整数 (uint32)</text>
      <text class="fig-muted" x="188" y="142">全部 32 位均为数值位 → 真值 3 242 721 280</text>
      <text class="fig-orange" x="532" y="124" font-size="12">② 有符号定点整数（补码 int32）</text>
      <text class="fig-muted" x="532" y="142" font-size="9.5">S=1 + 31 位数值，小数点在最右 → -1 052 246 016</text>
      <text class="fig-purple" x="188" y="176" font-size="12">③ 有符号定点小数（原码纯小数）</text>
      <text class="fig-muted" x="188" y="194" font-size="9.6">S=1 + 小数点 + 31 位小数 → -(2^-1 + 2^-7 + 2^-9 + …)</text>
      <text class="fig-green" x="532" y="176" font-size="10.5">④ IEEE 754 单精度浮点数 (float)</text>
      <text class="fig-muted" x="532" y="194" font-size="9.5">S=1, E=130, M=1001… → -1.1001_2 × 2^3 = -12.5</text>
      <text class="fig-title" x="360" y="244" font-size="10.5">定点数（小数点隐含不占存储位）vs 浮点数（阶码显式指明小数点浮动位置）</text>
      <text class="fig-label" x="131" y="272">定点小数：[ S ] . [ n 位数值 ]</text>
      <text class="fig-muted" x="131" y="292" font-size="10">小数点隐含在符号位 S 右侧</text>
      <text class="fig-label" x="353" y="272">定点整数：[ S ][ n 位数值 ] .</text>
      <text class="fig-muted" x="353" y="292" font-size="10">小数点隐含在最低数值位右侧</text>
      <text class="fig-accent" x="582" y="272" font-size="9.8">浮点数：[阶符|阶码][数符|尾数]</text>
      <text class="fig-muted" x="582" y="292" font-size="9.2">显式存储阶码，基数 R 隐含约定</text>
    </g>
  </svg>
  <figcaption>图 2.1：同一 32 位二进制串的四重数据解释，以及定点数与浮点数的位段隐含约定</figcaption>
</figure>

定点数因小数点位置固定，机器中<strong>无需表示和存储小数点</strong>；浮点数的小数点位置浮动，需用阶码指明。

### 2.2 进位计数制与按权展开

常用进位制：十进制（日常）、二进制（机器内部）、八进制与十六进制（书写阅读方便，因逻辑电路只有通/断两态，长比特串不便阅读）。

任意 R 进制数的要素：<strong>基数 R（可用数码个数）、位权 $R^i$</strong>、<strong>数码</strong>（0 \~ R−1；十六进制为 0\~9、A\~F）。

| 进制 | 基数 | 数码 | 进位规则 |
|---|---|---|---|
| 十进制 | 10 | 0–9 | 逢十进一 |
| 二进制 | 2 | 0、1 | 逢二进一 |
| 八进制 | 8 | 0–7 | 逢八进一 |
| 十六进制 | 16 | 0–9、A–F | 逢十六进一 |

书写表示：圆括号加下标 （1011）₂、后缀字母 $1011B$（不区分大小写），C 语言用前缀：二进制 `0b`/`0B`、八进制 `0`、十六进制 `0x`/`0X`，十进制无修饰。

任意 R 进制数按权展开：

$$
D = \sum_{i=-m}^{n-1} d_i R^i
$$

例：$(1011.101)_2 = 8+2+1+0.5+0.125 = 11.625$；（702.6）₈ = 7×8² + 2 + 6×8⁻¹ = 450.75；（A9D1.CF）₁₆ 需先把 A\~F 换成 10\~15 再展开。

> <strong>注意</strong>：读法上 （702.6）₈ 应读“八进制七零二点六”，不要误读为十进制的七百零二点六。
>
> <strong>技巧</strong>：八/十六进制转十进制时，把 $8^i$、$16^i$ 先化成 $2^{3i}$、$2^{4i}$，只需记住 2 的幂即可。

### 2.3 进制转换：二/八/十六互转与十进制转 R 进制

<strong>（1）二 ↔ 八：$2^3 = 8^1$，三位对应一位</strong>。整数部分从低位向高位每 3 位一组，最高组不足补 0；小数部分从高位向低位每 3 位一组，最低组不足补 0。用位权 4-2-1 快速求和。

<strong>（2）二 ↔ 十六：$2^4 = 16^1$，四位对应一位</strong>，分组方向同上，用位权 8-4-2-1。

<strong>（3）八 ↔ 十六</strong>：以二进制为桥梁，先展开成二进制再分组。

<strong>（4）十进制 → R 进制</strong>：整数部分<strong>除基取余，低位先得</strong>（余数自下而上排列，直到商为 0）；小数部分<strong>乘基取整，高位先得</strong>（整数部分自上而下排列，直到小数部分为 0 或达到所需精度）。

| 十进制 | 二进制 | 八进制 | 十六进制 |
|---|---|---|---|
| 0–9 | 0000–1001 | 0–11 | 0–9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |

常用 2 的幂：$2^7=128$、$2^8=256$、$2^{10}=1024$、$2^{16}=65536$，至少熟记整数部分第 8 位到第 0 位的位权 <strong>128 64 32 16 8 4 2 1</strong>。

例：123 = （1111011）₂（除基取余）；0.6875 = （0.1011）₂（乘基取整）。<strong>凑值法</strong>：把不超过 65536 的十进制整数拆成若干 2 的幂之和，位权所在位取 1，其余取 0，比除基取余快得多（如 IPv4 点分十进制转 8 位二进制）。

<figure class="cpd-figure not-content" id="hco-w1-fig-8">
  <svg viewBox="0 0 720 342" role="img" aria-label="二进制与八进制、十六进制分组转换及十进制整数除基取余与小数乘基取整步骤图">
    <defs>
      <marker id="hco-w1-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="148" rx="6"/>
    <line class="fig-line-accent" x1="396" y1="54" x2="396" y2="154" stroke-dasharray="4 3"/>
    <line class="fig-line" x1="378" y1="68" x2="140" y2="68" marker-end="url(#hco-w1-arr-8)"/>
    <line class="fig-line" x1="414" y1="68" x2="610" y2="68" marker-end="url(#hco-w1-arr-8)"/>
    <rect class="fig-box-blue" x="48" y="80" width="624" height="34" rx="4"/>
    <rect class="fig-box-green" x="48" y="120" width="624" height="34" rx="4"/>
    <rect class="fig-box-subtle" x="16" y="176" width="336" height="150" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="176" width="336" height="150" rx="6"/>
    <line class="fig-line-accent" x1="318" y1="286" x2="318" y2="214" marker-end="url(#hco-w1-arr-8)"/>
    <line class="fig-line-accent" x1="670" y1="214" x2="670" y2="278" marker-end="url(#hco-w1-arr-8)"/>
    <rect class="fig-box-orange" x="28" y="292" width="312" height="24" rx="4"/>
    <rect class="fig-box-red" x="380" y="286" width="312" height="30" rx="4"/>
    <text class="fig-title" x="360" y="34" text-anchor="middle" font-size="12">（a）二 ↔ 八 / 十六进制：以小数点为中心向两侧分组（例：1011011.101_2）</text>
    <g font-size="11">
      <text class="fig-muted" x="260" y="60" text-anchor="middle" font-size="10">整数：自右向左分组（高位不足补 0）</text>
      <text class="fig-muted" x="515" y="60" text-anchor="middle" font-size="10">小数：自左向右分组（低位不足补 0）</text>
      <text class="fig-blue" x="62" y="101">3 位一组（4-2-1 权）→ 八进制：</text>
      <text class="fig-label" x="388" y="101" text-anchor="end">[001]  [011]  [011]</text>
      <text class="fig-accent" x="396" y="101" text-anchor="middle">.</text>
      <text class="fig-label" x="404" y="101">[101]  =  ( 133.5 )_8</text>
      <text class="fig-green" x="62" y="141">4 位一组（8-4-2-1 权）→ 十六进制：</text>
      <text class="fig-label" x="388" y="141" text-anchor="end">[0101]  [1011]</text>
      <text class="fig-accent" x="396" y="141" text-anchor="middle">.</text>
      <text class="fig-label" x="404" y="141">[1010]  =  ( 5B.A )_16</text>
      <text class="fig-title" x="184" y="196" text-anchor="middle" font-size="12">（b）十进制整数 29 → 二进制（除 2 取余）</text>
      <text class="fig-label" x="40" y="220">29 ÷ 2 = 14 … 余 1（最低位 b_0，先得）</text>
      <text class="fig-label" x="40" y="238">14 ÷ 2 =  7 … 余 0（b_1）</text>
      <text class="fig-label" x="40" y="256"> 7 ÷ 2 =  3 … 余 1（b_2）</text>
      <text class="fig-label" x="40" y="274"> 3 ÷ 2 =  1 … 余 1（b_3）；1÷2=0 余 1(b_4)</text>
      <text class="fig-accent" x="302" y="254" text-anchor="end" font-size="10">逆序</text>
      <text class="fig-orange" x="184" y="308" text-anchor="middle">自下而上排列：(11101)_2 · 凑值法：16+8+4+1 = 29</text>
      <text class="fig-title" x="536" y="196" text-anchor="middle" font-size="12">（c）十进制小数 0.6875 → 二进制（乘 2 取整）</text>
      <text class="fig-label" x="384" y="220">0.6875 × 2 = 1.375 → 取整 1（最高位 b_-1）</text>
      <text class="fig-label" x="384" y="238">0.3750 × 2 = 0.750 → 取整 0（b_-2）</text>
      <text class="fig-label" x="384" y="256">0.7500 × 2 = 1.500 → 取整 1（b_-3）</text>
      <text class="fig-label" x="384" y="274">0.5000 × 2 = 1.000 → 取整 1（小数归 0 结束）</text>
      <text class="fig-accent" x="656" y="252" text-anchor="end" font-size="10">正序</text>
      <text class="fig-red" x="536" y="305" text-anchor="middle">自上而下：(0.1011)_2 · ★0.3 无限循环致精度缺失</text>
    </g>
  </svg>
  <figcaption>图 2.2：二/八/十六进制按小数点双向分组法则，以及十进制整数与小数转换的方向对比</figcaption>
</figure>

> <strong>注意</strong>：十进制小数转二进制可能出现<strong>无限循环</strong>（如 $0.3$），此时只能取到指定精度，产生<strong>精度缺失</strong>；八进制、十六进制同样存在该问题。

### 2.4 定点数的格式与表示范围

设机器字长 $n+1$ 位（1 位符号 + $n$ 位数值）。

- <strong>原码</strong>：最高位为符号位（0 正 1 负），数值位为真值绝对值。表示直观，但<strong>符号位不能直接参与运算</strong>，且 0 有两种表示（$+0$、$-0$）。
- <strong>反码</strong>：正数同原码；负数符号位不变、数值位按位取反。符号位可参与运算，但最高位产生的进位需“循环进位”加回最低位，0 仍有两种表示。
- <strong>补码</strong>：正数同原码；负数 = 反码末位加 1。减法可转加法，<strong>0 的表示唯一</strong>，比原码/反码多表示一个最小负数，符号位进位按模自动舍弃。

<figure class="cpd-figure not-content" id="hco-w1-fig-9">
  <svg viewBox="0 0 720 316" role="img" aria-label="8位定点整数原码反码补码转换链路与模256补码编码圆盘图">
    <defs>
      <marker id="hco-w1-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="352" height="284" rx="6"/>
    <rect class="fig-box-subtle" x="380" y="16" width="324" height="284" rx="6"/>
    <line class="fig-line-accent" x1="80" y1="126" x2="80" y2="146" marker-end="url(#hco-w1-arr-9)"/>
    <line class="fig-line-accent" x1="80" y1="180" x2="80" y2="200" marker-end="url(#hco-w1-arr-9)"/>
    <rect class="fig-box-green" x="28" y="46" width="328" height="36" rx="4"/>
    <rect class="fig-box-blue" x="28" y="92" width="328" height="34" rx="4"/>
    <rect class="fig-box-purple" x="28" y="146" width="328" height="34" rx="4"/>
    <rect class="fig-box-accent" x="28" y="200" width="328" height="34" rx="4"/>
    <rect class="fig-box-orange" x="28" y="244" width="328" height="44" rx="4"/>
    <circle class="fig-line-accent" cx="542" cy="154" r="58"/>
    <line class="fig-line-dashed" x1="542" y1="90" x2="542" y2="226"/>
    <circle class="fig-dot" cx="542" cy="96" r="4"/>
    <circle class="fig-dot-muted" cx="583" cy="113" r="3.5"/>
    <circle class="fig-dot-muted" cx="600" cy="154" r="3.5"/>
    <circle class="fig-dot-muted" cx="583" cy="195" r="3.5"/>
    <circle class="fig-dot" cx="542" cy="212" r="4"/>
    <circle class="fig-dot-muted" cx="501" cy="195" r="3.5"/>
    <circle class="fig-dot-muted" cx="484" cy="154" r="3.5"/>
    <circle class="fig-dot-muted" cx="501" cy="113" r="3.5"/>
    <rect class="fig-box-green" x="476" y="56" width="132" height="28" rx="4"/>
    <rect class="fig-box-red" x="396" y="240" width="292" height="48" rx="4"/>
    <rect class="fig-box" x="504" y="136" width="76" height="34" rx="4"/>
    <text class="fig-title" x="192" y="36" text-anchor="middle" font-size="12">（a）原码 / 反码 / 补码互转（8 位定点整数）</text>
    <text class="fig-title" x="542" y="36" text-anchor="middle" font-size="12">（b）8 位补码模 2^8 = 256 编码圆盘</text>
    <g font-size="11">
      <text class="fig-green" x="192" y="68" text-anchor="middle">正数（X = +18）：原 = 反 = 补 = 0001 0010</text>
      <text class="fig-blue" x="192" y="113" text-anchor="middle">负数 X = -18：[X]_原 = 1 0010010（S=1 + |X|）</text>
      <text class="fig-muted" x="92" y="140" font-size="10">符号位不变，数值位按位取反</text>
      <text class="fig-purple" x="192" y="167" text-anchor="middle">[X]_反 = 1 1101101（0 仍有 +0 / -0 两种）</text>
      <text class="fig-muted" x="92" y="194" font-size="9.8">末位 + 1（速算：右起首个 1 左边数值位取反）</text>
      <text class="fig-accent" x="192" y="221" text-anchor="middle">[X]_补 = 1 1101110（减法变加法，0 唯一）</text>
      <text class="fig-orange" x="192" y="262" text-anchor="middle">原码 [+0]=00000000，[-0]=10000000</text>
      <text class="fig-muted" x="192" y="280" text-anchor="middle" font-size="10">补码仅 00000000 表示 0；10000000 规定表示 -128</text>
      <text class="fig-green" x="542" y="74" text-anchor="middle" font-size="10">00000000 (0 唯一)</text>
      <text class="fig-label" x="591" y="116" font-size="10">00000001 (+1)</text>
      <text class="fig-label" x="608" y="158" font-size="10">01000000 (+64)</text>
      <text class="fig-blue" x="592" y="205" font-size="10">01111111 (+127)</text>
      <text class="fig-label" x="493" y="116" text-anchor="end" font-size="10">11111111 (-1)</text>
      <text class="fig-label" x="476" y="158" text-anchor="end" font-size="10">11000000 (-64)</text>
      <text class="fig-orange" x="496" y="204" text-anchor="end" font-size="10">10000001 (-127)</text>
      <text class="fig-muted" x="542" y="149" text-anchor="middle" font-size="10">顺时针 +1</text>
      <text class="fig-muted" x="542" y="165" text-anchor="middle" font-size="10">模 256 循环</text>
      <text class="fig-red" x="542" y="259" text-anchor="middle">底部 10000000 → 最小负数 -128（-2^7）</text>
      <text class="fig-muted" x="542" y="277" text-anchor="middle" font-size="10">★+127+1 越界为 -128；-128 无原码/反码</text>
    </g>
  </svg>
  <figcaption>图 2.3：8 位定点整数原码、反码、补码转换关系及模 256 编码圆盘（解释补码区间不对称与 -128 的来源）</figcaption>
</figure>

补码定义（定点小数，模 2）：

$$
[X]_{\mathrm{comp}} = X \quad (0 \le X < 1)
$$

$$
[X]_{\mathrm{comp}} = 2 + X \quad (-1 \le X < 0)
$$

定点整数（$n+1$ 位，模 $2^{n+1}$）：[X]补 = X 当 $0 \le X < 2^n$；[X]补 = $2^{n+1} + X$ 当 $-2^n \le X < 0$。

| 机器码 | 定点小数（$n+1$ 位） | 定点整数（$n+1$ 位） |
|---|---|---|
| 原码 | $-(1-2^{-n}) \sim (1-2^{-n})$ | $-(2^n-1) \sim (2^n-1)$ |
| 反码 | 同原码 | 同原码 |
| 补码 | $-1 \sim (1-2^{-n})$ | $-2^n \sim (2^n-1)$ |
| 移码 | 不用于小数 | $-2^n \sim (2^n-1)$ |

其中最大正数 $1-2^{-n}$ 由等比级数 $\sum_{i=1}^{n} 2^{-i}$ 求和得到，最小正数为 $2^{-n}$；补码的最小负数 $-1$（定点小数）或 $-2^n$（定点整数）的编码恰是“0 的原码”形式。

<figure class="cpd-figure not-content" id="hco-w1-fig-10">
  <svg viewBox="0 0 720 304" role="img" aria-label="定点小数与定点整数位段结构及原码反码补码表示范围数轴对比">
    <defs>
      <marker id="hco-w1-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="336" height="114" rx="6"/>
    <rect class="fig-box-subtle" x="368" y="16" width="336" height="114" rx="6"/>
    <rect class="fig-box-red" x="32" y="50" width="44" height="34" rx="4"/>
    <rect class="fig-box-blue" x="76" y="50" width="256" height="34" rx="4"/>
    <circle class="fig-dot" cx="76" cy="84" r="4"/>
    <rect class="fig-box-red" x="384" y="50" width="44" height="34" rx="4"/>
    <rect class="fig-box-green" x="428" y="50" width="252" height="34" rx="4"/>
    <circle class="fig-dot" cx="680" cy="84" r="4"/>
    <rect class="fig-box-subtle" x="16" y="140" width="688" height="148" rx="6"/>
    <line class="fig-line" x1="48" y1="204" x2="676" y2="204" marker-end="url(#hco-w1-arr-10)"/>
    <line class="fig-line" x1="370" y1="192" x2="370" y2="216"/>
    <line class="fig-line" x1="154" y1="196" x2="154" y2="212"/>
    <line class="fig-line" x1="586" y1="196" x2="586" y2="212"/>
    <line class="fig-line" x1="88" y1="196" x2="88" y2="254"/>
    <rect class="fig-box-blue" x="154" y="168" width="432" height="22" rx="3"/>
    <rect class="fig-box-green" x="88" y="242" width="498" height="24" rx="3"/>
    <text class="fig-title" x="184" y="36" text-anchor="middle" font-size="12">定点小数（1 位符号 + n 位尾数）</text>
    <text class="fig-title" x="536" y="36" text-anchor="middle" font-size="12">定点整数（1 位符号 + n 位数值）</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-red" x="54" y="71" font-size="12">S</text>
      <text class="fig-blue" x="204" y="71">n 位小数位（2^-1, 2^-2, …, 2^-n）</text>
      <text class="fig-accent" x="184" y="110">● 隐含小数点紧邻符号位 S 右侧</text>
      <text class="fig-red" x="406" y="71" font-size="12">S</text>
      <text class="fig-green" x="554" y="71" font-size="9.8">n 位整数位（2^(n-1), …, 2^1, 2^0）</text>
      <text class="fig-accent" x="536" y="110" font-size="9.8">● 隐含小数点位于最低数值位右侧</text>
      <text class="fig-title" x="360" y="158" font-size="10.2">n+1 位定点数表示范围对比（括号内为定点整数，括号外为定点小数）</text>
      <text class="fig-blue" x="370" y="183">原码 / 反码：关于 0 对称，存在 +0 与 -0 两种零</text>
      <text class="fig-label" x="370" y="232">0</text>
      <text class="fig-muted" x="154" y="230" font-size="10">-(1-2^-n) [-(2^n-1)]</text>
      <text class="fig-muted" x="586" y="230" font-size="10">1-2^-n [2^n-1]</text>
      <text class="fig-green" x="337" y="258">补码（移码仅整数）：0 表示唯一，向左多扩出一个最小负数</text>
      <text class="fig-accent" x="88" y="280">-1 [-2^n]</text>
    </g>
  </svg>
  <figcaption>图 2.4：定点小数与定点整数的隐含小数点位置，以及原码/反码与补码在真值数轴上的表示区间差异</figcaption>
</figure>

> <strong>注意</strong>：现代计算机的浮点数按 IEEE 754 表示，其中定点小数（尾数）用<strong>原码</strong>，因此通常不涉及定点小数补码；补码主要用于定点整数。

### 2.5 移码

移码只用于表示<strong>定点整数</strong>（尤其是浮点数的阶码）。定义：

$$
[X]_{\mathrm{bias}} = 2^n + X \quad (-2^n \le X < 2^n)
$$

其中 $n$ 为移码中数值位的位数，$2^n$ 为偏移常数。几何意义是把真值数轴整体向正方向平移 $2^n$ 个单元。

- 移码与补码<strong>仅符号位相反</strong>，数值位相同；
- 0 的表示唯一；
- <strong>保持了真值原有的大小顺序</strong>，可把移码当作无符号数直接比较大小；
- 最小真值的移码为全 0，最大真值的移码为全 1，符合人们习惯。

例（$n=7$）：$X=+1010110$，[X]移 = $10000000 + 1010110 = 1101\,0110$；$X=-1010110$，[X]移 = $10000000 - 1010110 = 0010\,1010$。

| 真值（4 位） | 补码 | 移码 |
|---|---|---|
| 0 | 0000 | 1000 |
| +1 | 0001 | 1001 |
| +6 | 0110 | 1110 |
| +7 | 0111 | 1111 |
| −1 | 1111 | 0111 |
| −6 | 1010 | 0010 |
| −7 | 1001 | 0001 |
| −8 | 1000 | 0000 |

<figure class="cpd-figure not-content" id="hco-w1-fig-11">
  <svg viewBox="0 0 720 310" role="img" aria-label="4位补码数轴与移码数轴整体右移映射及保序性对比图">
    <defs>
      <marker id="hco-w1-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="278" rx="6"/>
    <line class="fig-line" x1="56" y1="68" x2="664" y2="68" marker-end="url(#hco-w1-arr-11)"/>
    <line class="fig-line-accent" x1="56" y1="204" x2="664" y2="204" marker-end="url(#hco-w1-arr-11)"/>
    <line class="fig-line" x1="88" y1="60" x2="88" y2="76"/>
    <line class="fig-line" x1="204" y1="60" x2="204" y2="76"/>
    <line class="fig-line" x1="316" y1="60" x2="316" y2="76"/>
    <line class="fig-line" x1="396" y1="60" x2="396" y2="76"/>
    <line class="fig-line" x1="524" y1="60" x2="524" y2="76"/>
    <line class="fig-line" x1="628" y1="60" x2="628" y2="76"/>
    <line class="fig-line-accent" x1="88" y1="196" x2="88" y2="212"/>
    <line class="fig-line-accent" x1="204" y1="196" x2="204" y2="212"/>
    <line class="fig-line-accent" x1="316" y1="196" x2="316" y2="212"/>
    <line class="fig-line-accent" x1="396" y1="196" x2="396" y2="212"/>
    <line class="fig-line-accent" x1="524" y1="196" x2="524" y2="212"/>
    <line class="fig-line-accent" x1="628" y1="196" x2="628" y2="212"/>
    <line class="fig-line-dashed" x1="88" y1="112" x2="88" y2="190" marker-end="url(#hco-w1-arr-11)"/>
    <line class="fig-line-dashed" x1="316" y1="112" x2="316" y2="190" marker-end="url(#hco-w1-arr-11)"/>
    <line class="fig-line-dashed" x1="396" y1="112" x2="396" y2="190" marker-end="url(#hco-w1-arr-11)"/>
    <line class="fig-line-dashed" x1="628" y1="112" x2="628" y2="190" marker-end="url(#hco-w1-arr-11)"/>
    <rect class="fig-box-purple" x="196" y="122" width="328" height="28" rx="4"/>
    <rect class="fig-box-green" x="48" y="250" width="624" height="32" rx="4"/>
    <rect class="fig-box" x="102" y="156" width="388" height="20" rx="4"/>
    <text class="fig-title" x="48" y="38" font-size="12">4 位补码数轴（负数编码 1xxx 大于正数 0xxx，在真值 0 处发生无符号跳变）</text>
    <text class="fig-title" x="112" y="170" font-size="11">4 位移码数轴：[X]_移 = 2^3 + X（数轴整体右移 8 个单位）</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-label" x="88" y="92">-8</text><text class="fig-orange" x="88" y="108">1000</text>
      <text class="fig-label" x="204" y="92">-6</text><text class="fig-orange" x="204" y="108">1010</text>
      <text class="fig-label" x="316" y="92">-1</text><text class="fig-orange" x="316" y="108">1111</text>
      <text class="fig-label" x="396" y="92">0</text><text class="fig-blue" x="396" y="108">0000</text>
      <text class="fig-label" x="524" y="92">+6</text><text class="fig-blue" x="524" y="108">0110</text>
      <text class="fig-label" x="628" y="92">+7</text><text class="fig-blue" x="628" y="108">0111</text>
      <text class="fig-purple" x="360" y="140">符号位取反（数值位完全不变）：补码 ↔ 移码 一键互推</text>
      <text class="fig-label" x="88" y="226">-8</text><text class="fig-accent" x="88" y="242">0000(最小)</text>
      <text class="fig-label" x="204" y="226">-6</text><text class="fig-accent" x="204" y="242">0010</text>
      <text class="fig-label" x="316" y="226">-1</text><text class="fig-accent" x="316" y="242">0111</text>
      <text class="fig-label" x="396" y="226">0</text><text class="fig-accent" x="396" y="242">1000</text>
      <text class="fig-label" x="524" y="226">+6</text><text class="fig-accent" x="524" y="242">1110</text>
      <text class="fig-label" x="628" y="226">+7</text><text class="fig-accent" x="628" y="242">1111(最大)</text>
      <text class="fig-green" x="360" y="270" font-size="9.8">★移码严格保持真值大小顺序（0000 → 1111 单调递增），硬件可直接按无符号数比较阶码！</text>
    </g>
  </svg>
  <figcaption>图 2.5：4 位补码与移码的数轴平移几何意义、“符号位取反”互推关系及保序性</figcaption>
</figure>

> <strong>★考点</strong>：移码最小真值编码全 0、最大真值编码全 1；“移码 = 补码符号位取反”是快速互推的方法。

### 2.6 浮点数的表示形式与表示范围

定点数无法直接表示 $3.1415926$ 这类既有整数位又有小数位、且小数点位置随数量级变化的数。用“定点数 × 比例因子”的方法，比例因子因数据而异，无法统一。

仿照十进制科学计数法，二进制浮点数可写为：

$$
N = M \times R^{E}
$$

- <strong>$M$ 尾数</strong>：定点小数，其符号位即整个浮点数的符号（数符），决定<strong>精度</strong>；
- <strong>$E$ 阶码</strong>：定点整数，其符号位为阶符，决定<strong>小数点位置</strong>，即<strong>表示范围</strong>；
- <strong>$R$ 基数</strong>：隐含约定（2、4、8、16 等），不存储。

阶码可用原码/补码/反码/移码，尾数可用原码/补码/反码；采用不同机器码时表示范围略有差异。

设阶码数值位 $k$ 位、尾数数值位 $n$ 位，均用原码：

| 项目 | 表达式 |
|---|---|
| 最大正数 | $(1-2^{-n}) \times R^{2^k-1}$ |
| 最小正数 | $2^{-n} \times R^{-(2^k-1)}$ |
| 最大负数 | $-2^{-n} \times R^{-(2^k-1)}$ |
| 最小负数 | $-(1-2^{-n}) \times R^{2^k-1}$ |

- <strong>上溢</strong>：阶码大于最大阶码。机器停止运算，显示溢出标志。
- <strong>下溢：阶码小于最小阶码。绝对值极小，通常将尾数各位强制为 0，按机器零处理</strong>，机器可继续运行。
- 落在表示区间内但不在刻度上的数只能近似表示，产生<strong>精度溢出</strong>。

<figure class="cpd-figure not-content" id="hco-w1-fig-12">
  <svg viewBox="0 0 720 302" role="img" aria-label="通用浮点数位段格式与实数轴正负上溢区、可表示区及机器零下溢区分布图">
    <defs>
      <marker id="hco-w1-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="106" rx="6"/>
    <rect class="fig-box-blue" x="42" y="32" width="64" height="36" rx="4"/>
    <rect class="fig-box-blue" x="106" y="32" width="150" height="36" rx="4"/>
    <rect class="fig-box-orange" x="264" y="32" width="64" height="36" rx="4"/>
    <rect class="fig-box-green" x="328" y="32" width="212" height="36" rx="4"/>
    <rect class="fig-box-dashed" x="552" y="32" width="128" height="36" rx="4"/>
    <line class="fig-line" x1="42" y1="78" x2="256" y2="78"/>
    <line class="fig-line" x1="264" y1="78" x2="540" y2="78"/>
    <rect class="fig-box-subtle" x="16" y="132" width="688" height="154" rx="6"/>
    <line class="fig-line" x1="28" y1="202" x2="692" y2="202" marker-end="url(#hco-w1-arr-12)"/>
    <rect class="fig-box-red" x="36" y="170" width="96" height="28" rx="3"/>
    <rect class="fig-box-blue" x="136" y="170" width="144" height="28" rx="3"/>
    <rect class="fig-box-orange" x="284" y="170" width="152" height="28" rx="3"/>
    <rect class="fig-box-blue" x="440" y="170" width="144" height="28" rx="3"/>
    <rect class="fig-box-red" x="588" y="170" width="96" height="28" rx="3"/>
    <line class="fig-line" x1="134" y1="196" x2="134" y2="214"/>
    <line class="fig-line" x1="282" y1="196" x2="282" y2="214"/>
    <line class="fig-line" x1="360" y1="194" x2="360" y2="216"/>
    <line class="fig-line" x1="438" y1="196" x2="438" y2="214"/>
    <line class="fig-line" x1="586" y1="196" x2="586" y2="214"/>
    <g text-anchor="middle" font-size="11">
      <text class="fig-blue" x="74" y="54">阶符</text>
      <text class="fig-blue" x="181" y="54">阶码数值（k 位）</text>
      <text class="fig-orange" x="296" y="54">数符 S</text>
      <text class="fig-green" x="434" y="54">尾数数值（n 位定点小数）</text>
      <text class="fig-muted" x="616" y="54">基数 R 隐含不存</text>
      <text class="fig-blue" x="149" y="96">阶码 E（定点整数）→ 决定表示范围</text>
      <text class="fig-green" x="402" y="96">尾数 M（含数符）→ 决定数值精度与正负</text>
      <text class="fig-title" x="360" y="152" font-size="12">浮点数实数轴五区段分布（N = M × R^E）</text>
      <text class="fig-red" x="84" y="188">负上溢（中断）</text>
      <text class="fig-blue" x="208" y="188">负可表示区</text>
      <text class="fig-orange" x="360" y="188">下溢区（当机器零）</text>
      <text class="fig-blue" x="512" y="188">正可表示区</text>
      <text class="fig-red" x="636" y="188">正上溢（中断）</text>
      <text class="fig-muted" x="128" y="230" font-size="10">最小负数</text>
      <text class="fig-muted" x="128" y="245" font-size="10">-(1-2^-n)R^(2^k-1)</text>
      <text class="fig-muted" x="276" y="230" font-size="10">最大负数</text>
      <text class="fig-muted" x="276" y="245" font-size="10">-2^-n·R^-(2^k-1)</text>
      <text class="fig-label" x="360" y="232">0</text>
      <text class="fig-muted" x="444" y="230" font-size="10">最小正数</text>
      <text class="fig-muted" x="444" y="245" font-size="10">2^-n·R^-(2^k-1)</text>
      <text class="fig-muted" x="592" y="230" font-size="10">最大正数</text>
      <text class="fig-muted" x="592" y="245" font-size="10">(1-2^-n)R^(2^k-1)</text>
      <text class="fig-muted" x="360" y="272" font-size="9.5">阶码 &gt; 最大阶码 → 上溢停机报错；阶码 &lt; 最小阶码 → 下溢按机器零继续运行；刻度间隙 → 精度溢出</text>
    </g>
  </svg>
  <figcaption>图 2.6：通用浮点数编码位段分工（阶码定范围、尾数定精度）与实数轴五区段溢出特性</figcaption>
</figure>

> <strong>注意</strong>：计算浮点数值时要把尾数化成<strong>二进制</strong>再移位——$0.75 \times 2^3 = 0.11_2 \times 2^3 = 110_2 = 6$，不是 $750$。

### 2.7 浮点数的规格化

同一浮点数有多种阶码/尾数组合（如 $0.1100101 \times 2^{011}$ 与 $0.01100101 \times 2^{100}$），表示不唯一。规格化要求<strong>尾数最高数值位为有效值</strong>，好处是表示形式唯一、精度最高（去掉最高位的 0，相当于数值部分多补低位）。

| 基数 R | 规格化标准 | 左归（左移尾数） | 右归（右移尾数） |
|---|---|---|---|
| 2 | 尾数最高 1 位为 1 | 每左移 1 位，阶码减 1 | 每右移 1 位，阶码加 1 |
| 4 | 最高 2 位不全为 0 | 每左移 2 位，阶码减 1 | 每右移 2 位，阶码加 1 |
| 8 | 最高 3 位不全为 0 | 每左移 3 位，阶码减 1 | 每右移 3 位，阶码加 1 |
| 16 | 最高 4 位不全为 0 | 每左移 4 位，阶码减 1 | 每右移 4 位，阶码加 1 |

基数 R 的影响：<strong>R 越大，可表示范围越大、可表示数的个数越多，但精度越低</strong>（R=16 时尾数最高 4 位中可有 3 位为 0，比 R=2 少 3 位精度）。

规格化后：最大正数、最小负数不变（原尾数最高位本已为 1）；最小正数与最大负数变为 $2^{-n}\times R^{-(2^k-1)}$ 与 $-2^{-n}\times R^{-(2^k-1)}$。

例：$-56 = -111000_2 = -0.111000 \times 2^{110}$（阶码 5 位含阶符，尾数 11 位含数符）。若写成 $-0.0111000 \times 2^{111}$，尾数最高数值位为 0，需<strong>左归一位、阶码减一</strong>，得到 $-0.111000 \times 2^{110}$，即规格化形式。

<figure class="cpd-figure not-content" id="hco-w1-fig-13">
  <svg viewBox="0 0 720 288" role="img" aria-label="浮点数左归与右归规格化过程及原码补码规格化判据对比图">
    <defs>
      <marker id="hco-w1-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="352" height="256" rx="6"/>
    <rect class="fig-box-subtle" x="380" y="16" width="324" height="256" rx="6"/>
    <line class="fig-line-accent" x1="158" y1="92" x2="158" y2="120" marker-end="url(#hco-w1-arr-13)"/>
    <rect class="fig-box-orange" x="28" y="48" width="328" height="44" rx="4"/>
    <rect class="fig-box-green" x="28" y="120" width="328" height="48" rx="4"/>
    <rect class="fig-box-red" x="28" y="182" width="328" height="76" rx="4"/>
    <rect class="fig-box-blue" x="394" y="48" width="296" height="62" rx="4"/>
    <rect class="fig-box-purple" x="394" y="120" width="296" height="62" rx="4"/>
    <rect class="fig-box" x="394" y="192" width="296" height="66" rx="4"/>
    <text class="fig-title" x="192" y="36" text-anchor="middle" font-size="12">（a）左归与右归操作（R = 2，例：真值 -56）</text>
    <text class="fig-title" x="542" y="36" text-anchor="middle" font-size="12">（b）规格化形式判据与基数 R 的影响</text>
    <g text-anchor="middle" font-size="11">
      <text class="fig-orange" x="192" y="66">非规格化：-0.0111000 × 2^7（最高数值位为 0）</text>
      <text class="fig-muted" x="192" y="83" font-size="10">浪费 1 位有效位精度，表示不唯一</text>
      <text class="fig-accent" x="168" y="110" text-anchor="start" font-size="10">左归：尾数左移 1 位，阶码 - 1</text>
      <text class="fig-green" x="192" y="140">规格化：-0.1110000 × 2^6（最高数值位为 1）</text>
      <text class="fig-muted" x="192" y="158" font-size="10">末尾多保留 1 位有效数字，表示唯一且精度最高</text>
      <text class="fig-red" x="192" y="202" font-size="12">右归场景（加减运算尾数溢出时）</text>
      <text class="fig-label" x="192" y="222">双符号 01.x / 10.x → 尾数右移 1 位，阶码 + 1</text>
      <text class="fig-muted" x="192" y="242" font-size="10">★左归阶码减小可能下溢；右归阶码增大可能上溢！</text>
      <text class="fig-blue" x="542" y="68" font-size="12">原码尾数规格化（R = 2）</text>
      <text class="fig-label" x="542" y="88" font-size="9.8">正数 0.1xxxxxx…  |  负数 1.1xxxxxx…</text>
      <text class="fig-muted" x="542" y="103" font-size="10">不论正负，小数点后第 1 位恒为 1</text>
      <text class="fig-purple" x="542" y="140" font-size="12">补码尾数规格化（R = 2）</text>
      <text class="fig-label" x="542" y="160" font-size="9.8">正数 0.1xxxxxx…  |  负数 1.0xxxxxx…</text>
      <text class="fig-muted" x="542" y="175" font-size="9.2">符号位与最高数值位相反（特例 -1/2 补码为 1.100…0）</text>
      <text class="fig-label" x="542" y="212" font-size="10.5">基数 R 增大（如 R = 16 vs R = 2）</text>
      <text class="fig-muted" x="542" y="231" font-size="9.2">R=16 要求高 4 位不全为 0（至多前导 3 个 0）</text>
      <text class="fig-muted" x="542" y="248" font-size="9.2">R 越大 → 范围越大、移位越快，但精度越低</text>
    </g>
  </svg>
  <figcaption>图 2.7：浮点数左归/右归规格化调整、原码/补码尾数判据及基数 R 对精度与范围的权衡</figcaption>
</figure>

> <strong>★考点</strong>：规格化的两个收益——表示唯一、精度最高；左归可能下溢，右归可能上溢，因此规格化后必须检查阶码。
>
> <strong>注意</strong>：判断“是否已规格化”时，若尾数用补码给出，先转成原码再看最高数值位，正数补码与原码相同，负数需转换。

### 2.8 IEEE 754 浮点数标准

1985 年 IEEE 发布 754 标准，统一浮点格式，解决不同体系结构间数据传送与程序移植的格式转换问题。主要有 32 位单精度（C 的 `float`）与 64 位双精度（`double`）。

| 格式 | 总位数 | 符号 S | 阶码 E | 尾数 M | 偏置 |
|---|---|---|---|---|---|
| 单精度 | 32 | 1 | 8 | 23 | 127 |
| 双精度 | 64 | 1 | 11 | 52 | 1023 |

- 符号位：0 正、1 负；
- 阶码：定点整数，用<strong>移码</strong>（偏置 $2^{k-1}-1$，单精度 127、双精度 1023）；
- 尾数：定点小数，用<strong>原码</strong>，小数点隐含在尾数最前面，且<strong>小数点左侧隐藏一个 1</strong>，即实际尾数为 $1.M$（24 位有效位，只存 23 位），节省一位以提高精度。

<figure class="cpd-figure not-content" id="hco-w1-fig-14">
  <svg viewBox="0 0 720 316" role="img" aria-label="IEEE 754单精度与双精度浮点数位段格式、隐藏位1.M及偏置127设计原因">
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="190" rx="6"/>
    <rect class="fig-box-red" x="108" y="44" width="44" height="32" rx="4"/>
    <rect class="fig-box-blue" x="152" y="44" width="144" height="32" rx="4"/>
    <rect class="fig-box-dashed" x="304" y="44" width="48" height="32" rx="4"/>
    <rect class="fig-box-green" x="352" y="44" width="336" height="32" rx="4"/>
    <rect class="fig-box-red" x="108" y="122" width="44" height="32" rx="4"/>
    <rect class="fig-box-blue" x="152" y="122" width="176" height="32" rx="4"/>
    <rect class="fig-box-dashed" x="336" y="122" width="48" height="32" rx="4"/>
    <rect class="fig-box-green" x="384" y="122" width="304" height="32" rx="4"/>
    <rect class="fig-box-subtle" x="16" y="216" width="688" height="84" rx="6"/>
    <rect class="fig-box-orange" x="28" y="242" width="324" height="46" rx="4"/>
    <rect class="fig-box-accent" x="368" y="242" width="324" height="46" rx="4"/>
    <text class="fig-title" x="360" y="34" text-anchor="middle" font-size="12">IEEE 754 单精度（float, 32 位）与双精度（double, 64 位）位段布局</text>
    <g font-size="11">
      <text class="fig-label" x="30" y="64" font-size="12">单精度</text>
      <text class="fig-red" x="130" y="64" text-anchor="middle">S(1)</text>
      <text class="fig-blue" x="224" y="64" text-anchor="middle">E（8 位，偏 127）</text>
      <text class="fig-accent" x="328" y="64" text-anchor="middle">隐 1.</text>
      <text class="fig-green" x="520" y="64" text-anchor="middle">尾数 M（23 位原码，共 24 位有效精度）</text>
      <text class="fig-muted" x="114" y="91" font-size="10">bit 31</text>
      <text class="fig-muted" x="156" y="91" font-size="10">30</text>
      <text class="fig-muted" x="282" y="91" font-size="10">23</text>
      <text class="fig-muted" x="356" y="91" font-size="10">22（隐含 1. 不占存储位）</text>
      <text class="fig-muted" x="684" y="91" text-anchor="end" font-size="10">0</text>
      <text class="fig-label" x="30" y="142" font-size="12">双精度</text>
      <text class="fig-red" x="130" y="142" text-anchor="middle">S(1)</text>
      <text class="fig-blue" x="240" y="142" text-anchor="middle">阶码 E（11 位，偏置 1023）</text>
      <text class="fig-accent" x="360" y="142" text-anchor="middle">隐 1.</text>
      <text class="fig-green" x="536" y="142" text-anchor="middle">尾数 M（52 位原码，共 53 位有效精度）</text>
      <text class="fig-muted" x="114" y="169" font-size="10">bit 63</text>
      <text class="fig-muted" x="156" y="169" font-size="10">62</text>
      <text class="fig-muted" x="314" y="169" font-size="10">52</text>
      <text class="fig-muted" x="388" y="169" font-size="10">51</text>
      <text class="fig-muted" x="684" y="169" text-anchor="end" font-size="10">0</text>
      <text class="fig-label" x="360" y="194" text-anchor="middle">规格化数真值公式：(-1)^S × 1.M × 2^(E - Bias)，其中单精度 E ∈ [1, 254]，双精度 E ∈ [1, 2046]</text>
      <text class="fig-title" x="360" y="234" text-anchor="middle" font-size="12">为什么 8 位阶码偏置取 2^7 - 1 = 127，而不取标准移码的 2^7 = 128？</text>
      <text class="fig-orange" x="190" y="260" text-anchor="middle">若偏置 = 128（E=1～254 → 指数 [-127, +126]）</text>
      <text class="fig-muted" x="190" y="278" text-anchor="middle" font-size="10">最小规格化数 2^-127 求倒数得 2^+127，超出 +126 溢出！</text>
      <text class="fig-accent" x="530" y="260" text-anchor="middle">取偏置 = 127（E=1～254 → 指数 [-126, +127]）</text>
      <text class="fig-muted" x="530" y="278" text-anchor="middle" font-size="10">2^-126 倒数 2^+126 不溢出；全 0/全 1 作特殊值</text>
    </g>
  </svg>
  <figcaption>图 2.8：IEEE 754 单精度与双精度位段结构、隐藏位“1.”机制及偏置常数 127/1023 的防倒数溢出设计</figcaption>
</figure>

<strong>为什么偏置取 127 而非 128</strong>：若取 128，最小规格化数阶码真值为 $-128$，其倒数 $2^{+128}$ 超出 8 位标准移码最大真值 $+127$，会溢出；取 127 后，全 0 与全 1 两个阶码留作特殊用途，最小规格化阶码真值为 $-126$，倒数为 $2^{126}$，仍在 $[-126, 127]$ 内，不会溢出。

<strong>各类数的表示规则</strong>：

| 阶码 E | 尾数 M | 含义 | 真值 |
|---|---|---|---|
| 全 0 | 0 | ±0 | $(-1)^S \times 0$ |
| 全 0 | ≠0 | 非规格化数 | $(-1)^S \times 0.M \times 2^{-126}$ |
| 1 \~ 254 | 任意 | 规格化数 | $(-1)^S \times 1.M \times 2^{E-127}$ |
| 全 1 | 0 | ±∞ | 非零数除零的结果，程序可继续执行 |
| 全 1 | ≠0 | NaN（非数） | $0/0$、$\infty/\infty$、$0\times\infty$、负数开平方等 |

规格化数的表示范围（单精度）：

$$
V_{max} = (2 - 2^{-23}) \times 2^{127} \approx 3.4 \times 10^{38}
$$

$$
V_{min} = 1.0 \times 2^{-126} \approx 1.18 \times 10^{-38}
$$

非规格化数的最小值为 $2^{-149}$；双精度仅把阶码扩为 11 位、尾数扩为 52 位，规则完全相同（偏置 1023，非规格化数指数固定 $-1022$）。

<strong>十进制 → IEEE 754 单精度</strong>（例：$408.6875$）：

1. 转二进制：$408.6875 = 110011000.1011_2$；
2. 规格化为 $1.M \times 2^{E}$：$1.100110001011 \times 2^{8}$；
3. $S=0$；$E = 8 + 127 = 135 = 10000111_2$；$M = 100110001011$ 后补 11 个 0；
4. 拼接：`0 10000111 10011000101100000000000` → 十六进制 <strong>0x43CC5800</strong>。

<strong>IEEE 754 → 真值</strong>：按位段取出 S、E、M；若 E 非全 0/全 1，真值 $=(-1)^S \times 1.M \times 2^{E-127}$，再转十进制。

<figure class="cpd-figure not-content" id="hco-w1-fig-15">
  <svg viewBox="0 0 720 338" role="img" aria-label="IEEE 754五类数值实数轴分布与408.6875转0x43CC5800手算四步拆解图">
    <defs>
      <marker id="hco-w1-arr-15" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect class="fig-box-subtle" x="16" y="16" width="688" height="148" rx="6"/>
    <line class="fig-line" x1="26" y1="92" x2="606" y2="92" marker-end="url(#hco-w1-arr-15)"/>
    <rect class="fig-box-red" x="30" y="56" width="64" height="30" rx="3"/>
    <rect class="fig-box-blue" x="98" y="56" width="118" height="30" rx="3"/>
    <rect class="fig-box-orange" x="220" y="56" width="76" height="30" rx="3"/>
    <rect class="fig-box-green" x="300" y="56" width="38" height="30" rx="3"/>
    <rect class="fig-box-orange" x="342" y="56" width="76" height="30" rx="3"/>
    <rect class="fig-box-blue" x="422" y="56" width="118" height="30" rx="3"/>
    <rect class="fig-box-red" x="544" y="56" width="56" height="30" rx="3"/>
    <rect class="fig-box-purple" x="614" y="50" width="78" height="48" rx="4"/>
    <rect class="fig-box-subtle" x="16" y="174" width="688" height="148" rx="6"/>
    <line class="fig-line-accent" x1="178" y1="224" x2="196" y2="224" marker-end="url(#hco-w1-arr-15)"/>
    <line class="fig-line-accent" x1="382" y1="224" x2="400" y2="224" marker-end="url(#hco-w1-arr-15)"/>
    <rect class="fig-box-blue" x="28" y="200" width="150" height="48" rx="4"/>
    <rect class="fig-box-purple" x="196" y="200" width="186" height="48" rx="4"/>
    <rect class="fig-box-orange" x="400" y="200" width="292" height="48" rx="4"/>
    <rect class="fig-box-green" x="28" y="260" width="664" height="48" rx="4"/>
    <text class="fig-title" x="360" y="36" text-anchor="middle" font-size="12">（a）IEEE 754 单精度五类编码映射与渐进式下溢（注意非规格化指数固定为 -126！）</text>
    <g text-anchor="middle" font-size="10">
      <text class="fig-red" x="62" y="75" font-size="11">-∞</text>
      <text class="fig-blue" x="157" y="75" font-size="11">负规格化数</text>
      <text class="fig-orange" x="258" y="75">负非规格化</text>
      <text class="fig-green" x="319" y="75" font-size="11">±0</text>
      <text class="fig-orange" x="380" y="75">正非规格化</text>
      <text class="fig-blue" x="481" y="75" font-size="11">正规格化数</text>
      <text class="fig-red" x="572" y="75" font-size="11">+∞</text>
      <text class="fig-purple" x="653" y="70" font-size="11">NaN 非数</text>
      <text class="fig-muted" x="653" y="88" font-size="9">E=255, M≠0</text>
      <text class="fig-muted" x="62" y="108">E=255,M=0</text>
      <text class="fig-muted" x="157" y="108">E=1～254, -1.M</text>
      <text class="fig-muted" x="252" y="123" font-size="9.2">E=0, -0.M×2^-126</text>
      <text class="fig-muted" x="319" y="136" font-size="9">E=0,M=0</text>
      <text class="fig-muted" x="386" y="123" font-size="9.2">E=0, +0.M×2^-126</text>
      <text class="fig-muted" x="481" y="108">E=1～254, +1.M</text>
      <text class="fig-muted" x="572" y="108">E=255,M=0</text>
      <text class="fig-label" x="360" y="152" font-size="9.2">最小正非规格化 = 2^-23 × 2^-126 = 2^-149；最小正规格化 = 1.0 × 2^-126；最大正规格化 = (2 - 2^-23) × 2^127</text>
      <text class="fig-title" x="360" y="192" font-size="10.5">（b）408 必考手算实例：十进制 +408.6875 → IEEE 754 单精度十六进制</text>
      <text class="fig-blue" x="103" y="219" font-size="11">① 转二进制</text>
      <text class="fig-muted" x="103" y="237">110011000.1011_2</text>
      <text class="fig-purple" x="289" y="219" font-size="11">② 规格化（去隐藏位 1.）</text>
      <text class="fig-muted" x="289" y="237">1.100110001011 × 2^8</text>
      <text class="fig-orange" x="546" y="219" font-size="11">③ 求三字段（S / E / M）</text>
      <text class="fig-muted" x="546" y="237" font-size="8.8">S=0；E=8+127=135=10000111_2；M 右补 11 个 0</text>
      <text class="fig-green" x="360" y="279" font-size="9.8">④ 按 4 位一组拼接（勿漏 S 位！）：[0 100][0011] [1 100][1100] [0101][1000] [0000][0000]</text>
      <text class="fig-accent" x="360" y="298" font-size="10.2">对应十六进制机器码 = 0x43CC5800（小端主存低→高字节依次存放：00 58 CC 43）</text>
    </g>
  </svg>
  <figcaption>图 2.9：IEEE 754 单精度五类数值编码区间与 408.6875 转换手算四步分组示范</figcaption>
</figure>

> <strong>★考点</strong>：隐藏位只在<strong>规格化数</strong>中存在（$1.M$），非规格化数为 $0.M$；因此 E 全 0 时不能补 1。
>
> <strong>注意：IEEE 754 无法精确表示无限循环的二进制小数（如 $1.2$），运算误差会累积，不要直接用 `==` 比较浮点数</strong>；对误差极敏感的场合应用十进制浮点。

## 第3章 运算方法

### 3.1 补码加减运算公式与溢出检测

定点数在机器中采用补码，原因是符号位可与数值位一起参加运算，且减法可转成加法，运算规则简单、易于实现。

$$
[A+B]_{\mathrm{comp}} = [A]_{\mathrm{comp}} + [B]_{\mathrm{comp}}
$$

$$
[A-B]_{\mathrm{comp}} = [A]_{\mathrm{comp}} + [-B]_{\mathrm{comp}}
$$

其中 [-B]补 由 [B]补 <strong>全部位（含符号位）取反、末位加一</strong>得到。前提是运算在模 $2^{n+1}$（定点整数）或模 2（定点小数）意义下进行，符号位产生的进位按模自动舍弃。

证明按符号组合分四种情况（同正、一正一负、一负一正、同负），其中一正一负两种对称，只需证一种：

| 情况 | 证明要点 |
|---|---|
| $A\ge0,B\ge0$ | [A]补 = A、[B]补 = B、[A+B]补 = A+B，直接成立 |
| $A\ge0,B<0$ | [B]补 = $2+B$，和为 $2+(A+B)$；若 $A+B\ge0$ 舍模 2 得 $A+B$；若 $A+B<0$ 恰为补码定义 $2+(A+B)$ |
| $A<0,B<0$ | 和为 $4+A+B$，舍模 2 得 $2+A+B$；因 $A+B<0$，这正是 [A+B]补 |

减法公式由 [A-B]补 = [A+(-B)]补 及 [-B]补 = -[B]补 推出，后者可由 [B]补 + [-B]补 = 0 验证。

例：定点小数 $X=+0.1011$、$Y=-0.0110$，则 [X]补 = $0.1011$、[Y]补 = $1.1010$；[X+Y]补 = $0.1011+1.1010 = 0.0101$（舍去符号位进位），真值 $+0.0101$。

<strong>溢出检测三法</strong>：

| 方法 | 判据 | 特点 |
|---|---|---|
| 一：符号位 | 结果符号位与原操作数符号位不同（仅同号相加可能溢出） | 直观，需保存原符号 |
| 二：进位异或 | $OF = C_n \oplus C_{n-1}$，最高数值位进位与符号位进位不同则溢出 | 硬件最省，最常用 |
| 三：变形补码 | 双符号位：00 正、11 负、<strong>01 正溢</strong>、<strong>10 负溢</strong> | 手工最直观，硬件成本高 |

$$
OF = C_n \oplus C_{n-1}
$$

> <strong>注意</strong>：补码加法中符号位产生的进位是“模”，必须舍弃，它<strong>不是</strong>溢出；判断溢出只看最高数值位与符号位两个进位是否一致。

<figure class="cpd-figure not-content" id="hco-w2-fig-1">
  <svg viewBox="0 0 740 312" role="img" aria-label="补码模2加法实例与三种溢出检测硬件判据对比图">
    <defs>
      <marker id="hco-w2-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="14" y="16" width="346" height="280" rx="8" class="fig-box-subtle"/>
    <rect x="372" y="16" width="354" height="280" rx="8" class="fig-box-subtle"/>
    <rect x="24" y="48" width="326" height="114" rx="6" class="fig-box-green"/>
    <line x1="34" y1="116" x2="340" y2="116" class="fig-divider"/>
    <rect x="24" y="172" width="326" height="112" rx="6" class="fig-box-red"/>
    <line x1="34" y1="240" x2="340" y2="240" class="fig-divider"/>
    <rect x="384" y="48" width="330" height="66" rx="6" class="fig-box"/>
    <rect x="384" y="124" width="330" height="82" rx="6" class="fig-box-accent"/>
    <line x1="488" y1="168" x2="540" y2="168" class="fig-line" marker-end="url(#hco-w2-arr-1)"/>
    <line x1="488" y1="190" x2="540" y2="190" class="fig-line" marker-end="url(#hco-w2-arr-1)"/>
    <line x1="604" y1="179" x2="636" y2="179" class="fig-line-accent" marker-end="url(#hco-w2-arr-1)"/>
    <rect x="396" y="158" width="92" height="19" rx="4" class="fig-box"/>
    <rect x="396" y="180" width="92" height="19" rx="4" class="fig-box"/>
    <rect x="542" y="162" width="62" height="34" rx="5" class="fig-box-red"/>
    <rect x="638" y="162" width="64" height="34" rx="5" class="fig-box-red"/>
    <rect x="384" y="216" width="330" height="68" rx="6" class="fig-box-blue"/>
    <rect x="394" y="250" width="68" height="24" rx="4" class="fig-box-green"/>
    <rect x="470" y="250" width="68" height="24" rx="4" class="fig-box"/>
    <rect x="546" y="250" width="76" height="24" rx="4" class="fig-box-red"/>
    <rect x="630" y="250" width="74" height="24" rx="4" class="fig-box-red"/>
    <text x="187" y="37" text-anchor="middle" font-size="13" class="fig-title">补码模运算与进位实例</text>
    <text x="34" y="67" font-size="11" class="fig-green">例1（正常舍模）：(+0.1011) + (-0.0110)</text>
    <text x="34" y="84" font-size="10" class="fig-muted">进位链：C4=1（符号位进位）, C3=1（最高数值位进位）</text>
    <text x="34" y="101" font-size="10.5" class="fig-label">[X]补 0.1011 + [Y]补 1.1010 = (1) 0.0101</text>
    <text x="34" y="135" font-size="10.8" class="fig-green">舍去模进位 C4=1 → 结果 0.0101（+5/16）</text>
    <text x="34" y="152" font-size="10" class="fig-label">判溢出：OF = C4 ⊕ C3 = 1 ⊕ 1 = 0（有进位无溢出）</text>
    <text x="34" y="191" font-size="11" class="fig-red">例2（正上溢）：(+0.1011) + (+0.0111)</text>
    <text x="34" y="208" font-size="10" class="fig-muted">进位链：C4=0（符号位无进位）, C3=1（数值向符号进1）</text>
    <text x="34" y="225" font-size="9.3" class="fig-label">[X]补 0.1011 + [Z]补 0.0111 = (0) 1.0010（正+正变负）</text>
    <text x="34" y="259" font-size="10.2" class="fig-red">判溢出：OF = C4 ⊕ C3 = 0 ⊕ 1 = 1（发生正上溢）</text>
    <text x="34" y="276" font-size="10" class="fig-label">双符号验证：00.1011 + 00.0111 = 01.0010（01正溢）</text>
    <text x="549" y="37" text-anchor="middle" font-size="13" class="fig-title">溢出检测三种硬件判据对比</text>
    <text x="394" y="68" font-size="11.5" class="fig-label">① 单符号位法：V = As·Bs·/Ss + /As·/Bs·Ss</text>
    <text x="394" y="87" font-size="10.5" class="fig-muted">仅同号相加结果变号才溢出；需额外保存原操作数符号</text>
    <text x="394" y="104" font-size="10.5" class="fig-muted">减法时需先把减数符号取反后再参与判据</text>
    <text x="394" y="145" font-size="11.5" class="fig-accent">② 双进位异或法：OF = Cn ⊕ Cn-1（ALU 最常用）</text>
    <text x="442" y="172" text-anchor="middle" font-size="11" class="fig-label">符号进位 Cn</text>
    <text x="442" y="194" text-anchor="middle" font-size="11" class="fig-label">数值进位 Cn-1</text>
    <text x="573" y="183" text-anchor="middle" font-size="12" class="fig-red">XOR</text>
    <text x="670" y="183" text-anchor="middle" font-size="12" class="fig-red">OF</text>
    <text x="394" y="237" font-size="10.5" class="fig-blue">③ 变形补码双符号法：OF = Sf1 ⊕ Sf2（Sf1 为真符号）</text>
    <text x="428" y="266" text-anchor="middle" font-size="10.5" class="fig-green">00 正数</text>
    <text x="504" y="266" text-anchor="middle" font-size="10.5" class="fig-label">11 负数</text>
    <text x="584" y="266" text-anchor="middle" font-size="10.5" class="fig-red">01 正溢</text>
    <text x="667" y="266" text-anchor="middle" font-size="10.5" class="fig-red">10 负溢</text>
  </svg>
  <figcaption>图 3.1：补码模运算中“符号位进位舍弃 ≠ 溢出”实例与三种溢出检测硬件判据对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w2-fig-2">
  <svg viewBox="0 0 720 330" role="img" aria-label="逻辑移位与补码算术移位及零扩展与符号扩展位流对比图">
    <defs>
      <marker id="hco-w2-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="162" rx="8" class="fig-box-subtle"/>
    <rect x="16" y="190" width="688" height="126" rx="8" class="fig-box-subtle"/>
    <rect x="28" y="46" width="324" height="120" rx="6" class="fig-box"/>
    <rect x="368" y="46" width="324" height="120" rx="6" class="fig-box-blue"/>
    <line x1="108" y1="88" x2="78" y2="88" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="274" y1="88" x2="244" y2="88" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="78" y1="134" x2="108" y2="134" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="244" y1="134" x2="274" y2="134" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="448" y1="88" x2="418" y2="88" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="618" y1="88" x2="588" y2="88" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <path d="M 468 122 L 468 108 L 434 108 L 434 134 L 448 134" class="fig-line-accent" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="588" y1="134" x2="618" y2="134" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <rect x="42" y="76" width="34" height="24" rx="4" class="fig-box-orange"/>
    <rect x="110" y="76" width="132" height="24" rx="4" class="fig-box-subtle"/>
    <rect x="276" y="76" width="32" height="24" rx="4" class="fig-box-green"/>
    <rect x="42" y="122" width="34" height="24" rx="4" class="fig-box-green"/>
    <rect x="110" y="122" width="132" height="24" rx="4" class="fig-box-subtle"/>
    <rect x="276" y="122" width="34" height="24" rx="4" class="fig-box-orange"/>
    <rect x="382" y="76" width="34" height="24" rx="4" class="fig-box-orange"/>
    <rect x="450" y="76" width="36" height="24" rx="4" class="fig-box-purple"/>
    <rect x="486" y="76" width="100" height="24" rx="4" class="fig-box"/>
    <rect x="620" y="76" width="32" height="24" rx="4" class="fig-box-green"/>
    <rect x="450" y="122" width="36" height="24" rx="4" class="fig-box-purple"/>
    <rect x="486" y="122" width="100" height="24" rx="4" class="fig-box"/>
    <rect x="620" y="122" width="34" height="24" rx="4" class="fig-box-orange"/>
    <line x1="158" y1="268" x2="196" y2="268" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <path d="M 456 256 L 456 243 L 552 243 L 552 254" class="fig-line-accent" marker-end="url(#hco-w2-arr-2)"/>
    <line x1="508" y1="268" x2="532" y2="268" class="fig-line" marker-end="url(#hco-w2-arr-2)"/>
    <rect x="36" y="256" width="120" height="26" rx="4" class="fig-box"/>
    <rect x="198" y="256" width="76" height="26" rx="4" class="fig-box-green"/>
    <rect x="274" y="256" width="76" height="26" rx="4" class="fig-box"/>
    <rect x="438" y="256" width="34" height="26" rx="4" class="fig-box-purple"/>
    <rect x="472" y="256" width="34" height="26" rx="4" class="fig-box"/>
    <rect x="534" y="256" width="88" height="26" rx="4" class="fig-box-purple"/>
    <rect x="622" y="256" width="70" height="26" rx="4" class="fig-box"/>
    <text x="360" y="36" text-anchor="middle" font-size="13" class="fig-title">移位运算位流规则：逻辑移位（无符号）vs 补码算术移位（有符号）</text>
    <text x="190" y="64" text-anchor="middle" font-size="11.5" class="fig-label">逻辑移位：左移/右移空位一律补 0</text>
    <text x="59" y="92" text-anchor="middle" font-size="11" class="fig-orange">CF</text>
    <text x="176" y="92" text-anchor="middle" font-size="11" class="fig-label">SHL: b7 … b0（×2）</text>
    <text x="292" y="92" text-anchor="middle" font-size="11" class="fig-green">补0</text>
    <text x="59" y="138" text-anchor="middle" font-size="11" class="fig-green">补0</text>
    <text x="176" y="138" text-anchor="middle" font-size="11" class="fig-label">SHR: b7 … b0（÷2）</text>
    <text x="293" y="138" text-anchor="middle" font-size="11" class="fig-orange">CF</text>
    <text x="530" y="64" text-anchor="middle" font-size="11.5" class="fig-blue">补码算术移位：左移补 0 判溢，右移复制符号位 Xs</text>
    <text x="399" y="92" text-anchor="middle" font-size="11" class="fig-orange">CF</text>
    <text x="468" y="92" text-anchor="middle" font-size="11" class="fig-purple">Xs</text>
    <text x="536" y="92" text-anchor="middle" font-size="11" class="fig-label">SAL 数值位</text>
    <text x="636" y="92" text-anchor="middle" font-size="11" class="fig-green">补0</text>
    <text x="468" y="138" text-anchor="middle" font-size="11" class="fig-purple">Xs</text>
    <text x="536" y="138" text-anchor="middle" font-size="11" class="fig-label">SAR 数值位</text>
    <text x="637" y="138" text-anchor="middle" font-size="11" class="fig-orange">CF</text>
    <text x="530" y="159" text-anchor="middle" font-size="10.5" class="fig-muted">SAL 若移出位 ≠ 新 Xs 则溢出；SAR 丢 1 向 -∞ 舍入</text>
    <text x="190" y="159" text-anchor="middle" font-size="10.5" class="fig-muted">SHL 若移出 CF=1 则无符号乘 2 溢出</text>
    <text x="360" y="210" text-anchor="middle" font-size="13" class="fig-title">位宽扩展对比：零扩展（Zero-Extend）vs 符号扩展（Sign-Extend）</text>
    <text x="192" y="236" text-anchor="middle" font-size="10.8" class="fig-muted">无符号 4→8 位：高位硬连线补 0（真值 11 不变）</text>
    <text x="96" y="273" text-anchor="middle" font-size="11.5" class="fig-label">4位 1011 (11)</text>
    <text x="236" y="273" text-anchor="middle" font-size="11.5" class="fig-green">0000</text>
    <text x="312" y="273" text-anchor="middle" font-size="11.5" class="fig-label">1011</text>
    <text x="564" y="232" text-anchor="middle" font-size="10.2" class="fig-muted">补码 4→8 位：复制符号位 Xs（真值 -5 不变）</text>
    <text x="455" y="273" text-anchor="middle" font-size="11.5" class="fig-purple">1</text>
    <text x="489" y="273" text-anchor="middle" font-size="11.5" class="fig-label">011</text>
    <text x="578" y="273" text-anchor="middle" font-size="11.5" class="fig-purple">1111 1</text>
    <text x="657" y="273" text-anchor="middle" font-size="11.5" class="fig-label">011</text>
    <text x="360" y="304" text-anchor="middle" font-size="11" class="fig-muted">口诀：无符号扩展高补 0，补码扩展高补符（正补 0、负补 1）</text>
  </svg>
  <figcaption>图 3.2：逻辑移位与补码算术移位位流机制及零扩展、符号扩展硬件连线对比</figcaption>
</figure>

### 3.2 一位全加器与串行进位加法器

一位全加器以本位加数 $X_i$、$Y_i$ 和来自低位的进位 $C_i$ 为输入，输出本位和 $S_i$ 与向高位的进位 $C_{i+1}$：

$$
S_i = X_i \oplus Y_i \oplus C_i
$$

$$
C_{i+1} = X_i Y_i + (X_i \oplus Y_i)\,C_i
$$

<figure class="cpd-figure not-content" id="hco-w2-fig-3">
  <svg viewBox="0 0 720 264" role="img" aria-label="一位全加器门级逻辑电路与进位求和关键路径延迟分解图">
    <defs>
      <marker id="hco-w2-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="452" height="232" rx="8" class="fig-box-subtle"/>
    <rect x="480" y="16" width="224" height="232" rx="8" class="fig-box-subtle"/>
    <line x1="68" y1="74" x2="118" y2="74" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <line x1="68" y1="96" x2="118" y2="96" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <path d="M 88 74 L 88 144 L 118 144" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <path d="M 102 96 L 102 164 L 118 164" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <line x1="196" y1="85" x2="246" y2="85" class="fig-line-accent" marker-end="url(#hco-w2-arr-3)"/>
    <path d="M 218 85 L 218 182 L 246 182" class="fig-line-accent" marker-end="url(#hco-w2-arr-3)"/>
    <path d="M 68 204 L 232 204 L 232 103 L 246 103" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <line x1="232" y1="204" x2="246" y2="204" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <line x1="324" y1="94" x2="376" y2="94" class="fig-line-accent" marker-end="url(#hco-w2-arr-3)"/>
    <path d="M 196 154 L 330 154 L 330 168 L 344 168" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <line x1="324" y1="192" x2="344" y2="192" class="fig-line" marker-end="url(#hco-w2-arr-3)"/>
    <line x1="408" y1="180" x2="424" y2="180" class="fig-line-accent" marker-end="url(#hco-w2-arr-3)"/>
    <circle cx="88" cy="74" r="2.8" class="fig-dot"/>
    <circle cx="102" cy="96" r="2.8" class="fig-dot"/>
    <circle cx="218" cy="85" r="2.8" class="fig-dot"/>
    <circle cx="232" cy="204" r="2.8" class="fig-dot"/>
    <rect x="28" y="62" width="40" height="22" rx="4" class="fig-box"/>
    <rect x="28" y="86" width="40" height="22" rx="4" class="fig-box"/>
    <rect x="28" y="193" width="40" height="22" rx="4" class="fig-box-orange"/>
    <rect x="120" y="66" width="76" height="38" rx="6" class="fig-box-blue"/>
    <rect x="120" y="135" width="76" height="38" rx="6" class="fig-box"/>
    <rect x="248" y="75" width="76" height="38" rx="6" class="fig-box-blue"/>
    <rect x="248" y="173" width="76" height="38" rx="6" class="fig-box-orange"/>
    <rect x="346" y="161" width="62" height="38" rx="6" class="fig-box-orange"/>
    <rect x="378" y="78" width="78" height="32" rx="5" class="fig-box-green"/>
    <rect x="426" y="164" width="34" height="32" rx="5" class="fig-box-orange"/>
    <rect x="492" y="48" width="200" height="58" rx="6" class="fig-box-blue"/>
    <rect x="492" y="114" width="200" height="58" rx="6" class="fig-box-orange"/>
    <rect x="492" y="180" width="200" height="56" rx="6" class="fig-box-accent"/>
    <text x="242" y="37" text-anchor="middle" font-size="13" class="fig-title">一位全加器（FAi）门级逻辑与门延迟路径</text>
    <text x="48" y="77" text-anchor="middle" font-size="11.5" class="fig-label">Xi</text>
    <text x="48" y="101" text-anchor="middle" font-size="11.5" class="fig-label">Yi</text>
    <text x="48" y="208" text-anchor="middle" font-size="11.5" class="fig-orange">Ci</text>
    <text x="158" y="83" text-anchor="middle" font-size="11.5" class="fig-blue">XOR1</text>
    <text x="158" y="98" text-anchor="middle" font-size="10" class="fig-muted">延迟 3T</text>
    <text x="158" y="152" text-anchor="middle" font-size="11.5" class="fig-label">AND1</text>
    <text x="158" y="167" text-anchor="middle" font-size="10" class="fig-muted">Gi (1T)</text>
    <text x="220" y="76" text-anchor="middle" font-size="10.5" class="fig-blue">Pi (3T)</text>
    <text x="286" y="92" text-anchor="middle" font-size="11.5" class="fig-blue">XOR2</text>
    <text x="286" y="107" text-anchor="middle" font-size="10" class="fig-muted">延迟 3T</text>
    <text x="286" y="190" text-anchor="middle" font-size="11.5" class="fig-orange">AND2</text>
    <text x="286" y="205" text-anchor="middle" font-size="10" class="fig-muted">Pi·Ci (1T)</text>
    <text x="377" y="178" text-anchor="middle" font-size="11.5" class="fig-orange">OR</text>
    <text x="377" y="193" text-anchor="middle" font-size="10" class="fig-muted">延迟 1T</text>
    <text x="417" y="98" text-anchor="middle" font-size="11.5" class="fig-green">Si (6T)</text>
    <text x="443" y="184" text-anchor="middle" font-size="9.5" class="fig-orange">Ci+1</text>
    <text x="150" y="236" font-size="10.5" class="fig-muted">约定：简单与/或/非门 = 1T，异或门 XOR = 3T</text>
    <text x="592" y="37" text-anchor="middle" font-size="13" class="fig-title">门延迟关键路径拆解</text>
    <text x="502" y="68" font-size="11.5" class="fig-blue">① 首级 FA0（C0 在 0T 到达）</text>
    <text x="502" y="85" font-size="11" class="fig-label">进位 C1 = 3T + 1T + 1T = 5T</text>
    <text x="502" y="100" font-size="11" class="fig-label">本位和 S0 = 3T + 3T = 6T</text>
    <text x="502" y="134" font-size="10.5" class="fig-orange">② 第 i 级级联（Pi、Gi 已就绪）</text>
    <text x="502" y="151" font-size="11" class="fig-label">Ci 仅经 AND2(1T) + OR(1T)</text>
    <text x="502" y="166" font-size="11" class="fig-label">每多串一级进位增加 +2T</text>
    <text x="502" y="200" font-size="11.5" class="fig-accent">③ N 位行波进位总延迟</text>
    <text x="502" y="217" font-size="11" class="fig-label">最高进位 Cn = (2N + 3)T</text>
    <text x="502" y="232" font-size="11" class="fig-label">全部求和 Sn-1 = (2N + 4)T</text>
  </svg>
  <figcaption>图 3.3：一位全加器门级电路与关键路径延迟分解（XOR=3T，与/或门=1T）</figcaption>
</figure>

把 $N$ 个全加器按进位串联（$FA_i$ 的 $C_{i+1}$ 接 $FA_{i+1}$ 的 $C_i$），即 <strong>$N$ 位串行进位加法器</strong>（行波进位加法器）。用于无符号数时，最高位进位即溢出信号；用于补码时，最高数值位进位与符号位进位异或得到 $OF$。

<figure class="cpd-figure not-content" id="hco-w2-fig-4">
  <svg viewBox="0 0 720 272" role="img" aria-label="4位串行进位加法器行波进位链与双进位异或溢出检测电路图">
    <defs>
      <marker id="hco-w2-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="240" rx="8" class="fig-box-subtle"/>
    <line x1="108" y1="54" x2="108" y2="74" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="252" y1="54" x2="252" y2="74" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="396" y1="54" x2="396" y2="74" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="540" y1="54" x2="540" y2="74" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="28" y1="106" x2="56" y2="106" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="158" y1="106" x2="200" y2="106" class="fig-line-accent" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="302" y1="106" x2="344" y2="106" class="fig-line-accent" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="446" y1="106" x2="488" y2="106" class="fig-line-accent" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="590" y1="106" x2="646" y2="106" class="fig-line-accent" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="108" y1="138" x2="108" y2="158" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="252" y1="138" x2="252" y2="158" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="396" y1="138" x2="396" y2="158" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="540" y1="138" x2="540" y2="158" class="fig-line" marker-end="url(#hco-w2-arr-4)"/>
    <path d="M 468 106 L 468 210 L 580 210" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w2-arr-4)"/>
    <path d="M 616 106 L 616 184" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w2-arr-4)"/>
    <line x1="652" y1="205" x2="668" y2="205" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w2-arr-4)"/>
    <circle cx="468" cy="106" r="3" class="fig-dot"/>
    <circle cx="616" cy="106" r="3" class="fig-dot"/>
    <rect x="58" y="76" width="100" height="62" rx="6" class="fig-box"/>
    <rect x="202" y="76" width="100" height="62" rx="6" class="fig-box"/>
    <rect x="346" y="76" width="100" height="62" rx="6" class="fig-box"/>
    <rect x="490" y="76" width="100" height="62" rx="6" class="fig-box-blue"/>
    <rect x="70" y="160" width="76" height="24" rx="4" class="fig-box-green"/>
    <rect x="214" y="160" width="76" height="24" rx="4" class="fig-box-green"/>
    <rect x="358" y="160" width="76" height="24" rx="4" class="fig-box-green"/>
    <rect x="502" y="160" width="76" height="24" rx="4" class="fig-box-blue"/>
    <rect x="582" y="186" width="70" height="38" rx="6" class="fig-box-red"/>
    <text x="108" y="46" text-anchor="middle" font-size="11.5" class="fig-label">X0 , Y0</text>
    <text x="252" y="46" text-anchor="middle" font-size="11.5" class="fig-label">X1 , Y1</text>
    <text x="396" y="46" text-anchor="middle" font-size="11.5" class="fig-label">X2 , Y2</text>
    <text x="540" y="46" text-anchor="middle" font-size="11.5" class="fig-blue">X3 , Y3（符号）</text>
    <text x="38" y="98" text-anchor="middle" font-size="11" class="fig-muted">C0</text>
    <text x="180" y="97" text-anchor="middle" font-size="10.5" class="fig-accent">C1(5T)</text>
    <text x="324" y="97" text-anchor="middle" font-size="10.5" class="fig-accent">C2(7T)</text>
    <text x="468" y="97" text-anchor="middle" font-size="10.5" class="fig-red">C3(9T)</text>
    <text x="634" y="97" text-anchor="middle" font-size="10.5" class="fig-red">C4(11T)</text>
    <text x="108" y="102" text-anchor="middle" font-size="12.5" class="fig-title">FA0</text>
    <text x="108" y="122" text-anchor="middle" font-size="10.5" class="fig-muted">最低位</text>
    <text x="252" y="102" text-anchor="middle" font-size="12.5" class="fig-title">FA1</text>
    <text x="252" y="122" text-anchor="middle" font-size="10.5" class="fig-muted">数值位</text>
    <text x="396" y="102" text-anchor="middle" font-size="12.5" class="fig-title">FA2</text>
    <text x="396" y="122" text-anchor="middle" font-size="10.5" class="fig-muted">最高数值位</text>
    <text x="540" y="102" text-anchor="middle" font-size="12.5" class="fig-blue">FA3</text>
    <text x="540" y="122" text-anchor="middle" font-size="10.5" class="fig-muted">符号位</text>
    <text x="108" y="176" text-anchor="middle" font-size="11" class="fig-green">S0 (6T)</text>
    <text x="252" y="176" text-anchor="middle" font-size="11" class="fig-green">S1 (8T)</text>
    <text x="396" y="176" text-anchor="middle" font-size="11" class="fig-green">S2 (10T)</text>
    <text x="540" y="176" text-anchor="middle" font-size="11" class="fig-blue">S3 (12T)</text>
    <text x="524" y="204" text-anchor="middle" font-size="10.5" class="fig-red">C3 引出</text>
    <text x="617" y="203" text-anchor="middle" font-size="11.5" class="fig-red">XOR</text>
    <text x="617" y="218" text-anchor="middle" font-size="10" class="fig-muted">溢出检测</text>
    <text x="690" y="209" text-anchor="end" font-size="11.5" class="fig-red">OF</text>
    <text x="290" y="236" text-anchor="middle" font-size="11" class="fig-muted">无符号溢出看 C4；补码溢出看 OF = C4 ⊕ C3，4 位求和总延迟 (2×4+4)T = 12T</text>
  </svg>
  <figcaption>图 3.4：4 位串行进位加法器（行波进位链）与双进位异或溢出检测电路</figcaption>
</figure>

<strong>性能分析</strong>：设简单门延迟 1T、异或门 3T。$C_0$ 在 $T_0$ 到达，$FA_0$ 的进位在 5T 输出，本位和在 6T 输出；此后每个全加器的进位再延迟 2T。故 $N$ 位加法器输出全部本位和的时间为：

$$
t_{sum} = (2N+4)\,T
$$

与位数 $N$ 成线性关系，$N$ 大时性能差——这正是后续采用先行进位（并行进位）的原因。

<figure class="cpd-figure not-content" id="hco-w2-fig-5">
  <svg viewBox="0 0 720 316" role="img" aria-label="4位先行进位加法器CLA并行进位网络与16位两级成组先行进位BCLA层次结构图">
    <defs>
      <marker id="hco-w2-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="372" height="284" rx="8" class="fig-box-subtle"/>
    <rect x="400" y="16" width="304" height="284" rx="8" class="fig-box-subtle"/>
    <line x1="60" y1="108" x2="60" y2="140" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="84" y1="142" x2="84" y2="110" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="144" y1="108" x2="144" y2="140" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="168" y1="142" x2="168" y2="110" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="228" y1="108" x2="228" y2="140" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="252" y1="142" x2="252" y2="110" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="312" y1="108" x2="312" y2="140" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="378" y1="170" x2="356" y2="170" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="50" y1="170" x2="22" y2="170" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <rect x="36" y="54" width="72" height="54" rx="5" class="fig-box-blue"/>
    <rect x="120" y="54" width="72" height="54" rx="5" class="fig-box"/>
    <rect x="204" y="54" width="72" height="54" rx="5" class="fig-box"/>
    <rect x="288" y="54" width="66" height="54" rx="5" class="fig-box"/>
    <rect x="52" y="142" width="302" height="56" rx="6" class="fig-box-accent"/>
    <rect x="28" y="210" width="348" height="78" rx="6" class="fig-box"/>
    <line x1="434" y1="118" x2="434" y2="156" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="454" y1="158" x2="454" y2="120" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="504" y1="118" x2="504" y2="156" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="524" y1="158" x2="524" y2="120" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="574" y1="118" x2="574" y2="156" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="594" y1="158" x2="594" y2="120" class="fig-line-accent" marker-end="url(#hco-w2-arr-5)"/>
    <line x1="644" y1="118" x2="644" y2="156" class="fig-line" marker-end="url(#hco-w2-arr-5)"/>
    <rect x="412" y="64" width="62" height="54" rx="5" class="fig-box-blue"/>
    <rect x="482" y="64" width="62" height="54" rx="5" class="fig-box"/>
    <rect x="552" y="64" width="62" height="54" rx="5" class="fig-box"/>
    <rect x="622" y="64" width="66" height="54" rx="5" class="fig-box"/>
    <rect x="412" y="158" width="276" height="52" rx="6" class="fig-box-purple"/>
    <rect x="412" y="222" width="276" height="66" rx="6" class="fig-box-blue"/>
    <text x="202" y="37" text-anchor="middle" font-size="13" class="fig-title">4 位先行进位加法器（CLA）并行结构</text>
    <text x="72" y="76" text-anchor="middle" font-size="11.5" class="fig-blue">位3 求和</text>
    <text x="72" y="96" text-anchor="middle" font-size="10" class="fig-muted">S3 (8T)</text>
    <text x="156" y="76" text-anchor="middle" font-size="11.5" class="fig-label">位2 求和</text>
    <text x="156" y="96" text-anchor="middle" font-size="10" class="fig-muted">S2 (8T)</text>
    <text x="240" y="76" text-anchor="middle" font-size="11.5" class="fig-label">位1 求和</text>
    <text x="240" y="96" text-anchor="middle" font-size="10" class="fig-muted">S1 (8T)</text>
    <text x="321" y="76" text-anchor="middle" font-size="11.5" class="fig-label">位0 求和</text>
    <text x="321" y="96" text-anchor="middle" font-size="10" class="fig-muted">S0 (6T)</text>
    <text x="55" y="128" text-anchor="end" font-size="9.5" class="fig-muted">G3,P3</text>
    <text x="89" y="128" text-anchor="start" font-size="9.5" class="fig-accent">C3</text>
    <text x="139" y="128" text-anchor="end" font-size="9.5" class="fig-muted">G2,P2</text>
    <text x="173" y="128" text-anchor="start" font-size="9.5" class="fig-accent">C2</text>
    <text x="223" y="128" text-anchor="end" font-size="9.5" class="fig-muted">G1,P1</text>
    <text x="257" y="128" text-anchor="start" font-size="9.5" class="fig-accent">C1</text>
    <text x="332" y="128" text-anchor="middle" font-size="9.5" class="fig-muted">G0,P0</text>
    <text x="201" y="166" text-anchor="middle" font-size="12" class="fig-accent">4 位 CLA 先行进位部件（两级与或门 2T）</text>
    <text x="201" y="186" text-anchor="middle" font-size="10.2" class="fig-muted">C1～C4 在 3T+2T=5T 同时并发产出，无级联等待</text>
    <text x="372" y="162" text-anchor="middle" font-size="10" class="fig-muted">C0</text>
    <text x="33" y="161" text-anchor="middle" font-size="10" class="fig-accent">C4</text>
    <text x="36" y="230" font-size="10.2" class="fig-label">进位生成 Gi = Xi·Yi (1T)；进位传递 Pi = Xi ⊕ Yi (3T)</text>
    <text x="36" y="250" font-size="9.1" class="fig-accent">Ci+1 = Gi + Pi·Gi-1 + … + Pi…P0·C0（仅依赖 Gi,Pi 与 C0）</text>
    <text x="38" y="270" font-size="11" class="fig-muted">4位 CLA 求和延迟由 12T 降至 8T；对外输出成组 G*、P*</text>
    <text x="552" y="37" text-anchor="middle" font-size="13" class="fig-title">16 位成组先行进位（BCLA）</text>
    <text x="443" y="86" text-anchor="middle" font-size="11" class="fig-blue">4位CLA</text>
    <text x="443" y="104" text-anchor="middle" font-size="10" class="fig-muted">[15:12]</text>
    <text x="513" y="86" text-anchor="middle" font-size="11" class="fig-label">4位CLA</text>
    <text x="513" y="104" text-anchor="middle" font-size="10" class="fig-muted">[11:8]</text>
    <text x="583" y="86" text-anchor="middle" font-size="11" class="fig-label">4位CLA</text>
    <text x="583" y="104" text-anchor="middle" font-size="10" class="fig-muted">[7:4]</text>
    <text x="655" y="86" text-anchor="middle" font-size="11" class="fig-label">4位CLA</text>
    <text x="655" y="104" text-anchor="middle" font-size="10" class="fig-muted">[3:0]</text>
    <text x="468" y="142" text-anchor="middle" font-size="9.5" class="fig-accent">C12</text>
    <text x="538" y="142" text-anchor="middle" font-size="9.5" class="fig-accent">C8</text>
    <text x="608" y="142" text-anchor="middle" font-size="9.5" class="fig-accent">C4</text>
    <text x="650" y="142" text-anchor="start" font-size="9.5" class="fig-muted">G0*,P0*</text>
    <text x="550" y="181" text-anchor="middle" font-size="11.5" class="fig-purple">二级 BCLA 成组先行进位部件</text>
    <text x="550" y="199" text-anchor="middle" font-size="10" class="fig-muted">汇总四组 Gk*,Pk* 一步算出 C4,C8,C12,C16</text>
    <text x="422" y="242" font-size="11" class="fig-blue">• 组内并行 + 组间并行（全先行进位）</text>
    <text x="422" y="260" font-size="11" class="fig-label">• 解决单级 CLA 位数过大时门扇入受限问题</text>
    <text x="422" y="278" font-size="11" class="fig-muted">• 16 位加法仅需 12T（串行进位需 36T）</text>
  </svg>
  <figcaption>图 3.5：4 位先行进位加法器（CLA）并行进位网络与 16 位两级成组先行进位（BCLA）层次结构</figcaption>
</figure>

### 3.3 可控加减法电路与求补电路

补码减法只需“减数取反、末位加一”，因此对 $N$ 位串行进位加法器做两处改造即可同时支持加/减：

1. 在操作数 $Y$ 的每一位与对应全加器输入之间插入<strong>异或门</strong>，异或门的另一输入统一接控制端 `sub`；
2. 把 `sub` 同时接到<strong>最低位全加器的进位输入端</strong> $C_0$。

- `sub = 0`：$Y_i \oplus 0 = Y_i$，$C_0=0$，执行 $X+Y$；
- `sub = 1`：$Y_i \oplus 1 = \overline{Y_i}$，$C_0=1$，执行 $X+\overline{Y}+1 = X-Y$。

<figure class="cpd-figure not-content" id="hco-w2-fig-6">
  <svg viewBox="0 0 720 304" role="img" aria-label="4位补码可控加减法器与OF、CF、SF、ZF四大标志位硬件生成电路图">
    <defs>
      <marker id="hco-w2-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="272" rx="8" class="fig-box-subtle"/>
    <line x1="556" y1="48" x2="122" y2="48" class="fig-line" stroke="var(--sl-color-orange)"/>
    <path d="M 546 48 L 546 154 L 504 154" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="122" y1="48" x2="122" y2="76" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="238" y1="48" x2="238" y2="76" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="354" y1="48" x2="354" y2="76" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="470" y1="48" x2="470" y2="76" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="82" y1="70" x2="82" y2="126" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="198" y1="70" x2="198" y2="126" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="314" y1="70" x2="314" y2="126" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="430" y1="70" x2="430" y2="126" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="138" y1="64" x2="138" y2="76" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="254" y1="64" x2="254" y2="76" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="370" y1="64" x2="370" y2="76" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="486" y1="64" x2="486" y2="76" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="130" y1="104" x2="130" y2="126" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="246" y1="104" x2="246" y2="126" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="362" y1="104" x2="362" y2="126" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="478" y1="104" x2="478" y2="126" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="414" y1="154" x2="388" y2="154" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="298" y1="154" x2="272" y2="154" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="182" y1="154" x2="156" y2="154" class="fig-line-accent" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="66" y1="154" x2="34" y2="154" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="110" y1="180" x2="110" y2="206" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="226" y1="180" x2="226" y2="206" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="342" y1="180" x2="342" y2="206" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <line x1="458" y1="180" x2="458" y2="206" class="fig-line" marker-end="url(#hco-w2-arr-6)"/>
    <circle cx="546" cy="48" r="2.8" class="fig-dot"/>
    <circle cx="238" cy="48" r="2.8" class="fig-dot"/>
    <circle cx="354" cy="48" r="2.8" class="fig-dot"/>
    <circle cx="470" cy="48" r="2.8" class="fig-dot"/>
    <rect x="108" y="78" width="44" height="26" rx="4" class="fig-box-blue"/>
    <rect x="224" y="78" width="44" height="26" rx="4" class="fig-box-blue"/>
    <rect x="340" y="78" width="44" height="26" rx="4" class="fig-box-blue"/>
    <rect x="456" y="78" width="44" height="26" rx="4" class="fig-box-blue"/>
    <rect x="66" y="128" width="88" height="52" rx="6" class="fig-box-blue"/>
    <rect x="182" y="128" width="88" height="52" rx="6" class="fig-box"/>
    <rect x="298" y="128" width="88" height="52" rx="6" class="fig-box"/>
    <rect x="414" y="128" width="88" height="52" rx="6" class="fig-box"/>
    <rect x="82" y="208" width="56" height="24" rx="4" class="fig-box-purple"/>
    <rect x="198" y="208" width="56" height="24" rx="4" class="fig-box-green"/>
    <rect x="314" y="208" width="56" height="24" rx="4" class="fig-box-green"/>
    <rect x="430" y="208" width="56" height="24" rx="4" class="fig-box-green"/>
    <rect x="556" y="34" width="138" height="28" rx="5" class="fig-box-orange"/>
    <rect x="556" y="76" width="138" height="44" rx="5" class="fig-box-red"/>
    <rect x="556" y="128" width="138" height="44" rx="5" class="fig-box-orange"/>
    <rect x="556" y="180" width="138" height="40" rx="5" class="fig-box-purple"/>
    <rect x="556" y="228" width="138" height="42" rx="5" class="fig-box-green"/>
    <text x="275" y="34" text-anchor="middle" font-size="13" class="fig-title">4 位补码可控加减法器（ALU 核心通路与标志生成）</text>
    <text x="625" y="52" text-anchor="middle" font-size="11" class="fig-orange">控制端 Sub (0加/1减)</text>
    <text x="82" y="65" text-anchor="middle" font-size="11" class="fig-label">X3</text>
    <text x="148" y="70" text-anchor="start" font-size="10" class="fig-blue">Y3</text>
    <text x="198" y="65" text-anchor="middle" font-size="11" class="fig-label">X2</text>
    <text x="264" y="70" text-anchor="start" font-size="10" class="fig-blue">Y2</text>
    <text x="314" y="65" text-anchor="middle" font-size="11" class="fig-label">X1</text>
    <text x="380" y="70" text-anchor="start" font-size="10" class="fig-blue">Y1</text>
    <text x="430" y="65" text-anchor="middle" font-size="11" class="fig-label">X0</text>
    <text x="496" y="70" text-anchor="start" font-size="10" class="fig-blue">Y0</text>
    <text x="130" y="95" text-anchor="middle" font-size="10.5" class="fig-blue">XOR</text>
    <text x="246" y="95" text-anchor="middle" font-size="10.5" class="fig-blue">XOR</text>
    <text x="362" y="95" text-anchor="middle" font-size="10.5" class="fig-blue">XOR</text>
    <text x="478" y="95" text-anchor="middle" font-size="10.5" class="fig-blue">XOR</text>
    <text x="110" y="151" text-anchor="middle" font-size="12" class="fig-blue">FA3</text>
    <text x="110" y="168" text-anchor="middle" font-size="10" class="fig-muted">符号位</text>
    <text x="226" y="151" text-anchor="middle" font-size="12" class="fig-title">FA2</text>
    <text x="226" y="168" text-anchor="middle" font-size="10" class="fig-muted">数值位</text>
    <text x="342" y="151" text-anchor="middle" font-size="12" class="fig-title">FA1</text>
    <text x="342" y="168" text-anchor="middle" font-size="10" class="fig-muted">数值位</text>
    <text x="458" y="151" text-anchor="middle" font-size="12" class="fig-title">FA0</text>
    <text x="458" y="168" text-anchor="middle" font-size="10" class="fig-muted">最低位</text>
    <text x="524" y="168" text-anchor="middle" font-size="9" class="fig-orange">C0=Sub</text>
    <text x="401" y="145" text-anchor="middle" font-size="10" class="fig-muted">C1</text>
    <text x="285" y="145" text-anchor="middle" font-size="10" class="fig-muted">C2</text>
    <text x="169" y="145" text-anchor="middle" font-size="10" class="fig-red">C3</text>
    <text x="44" y="145" text-anchor="middle" font-size="10.5" class="fig-red">C4</text>
    <text x="110" y="224" text-anchor="middle" font-size="11" class="fig-purple">S3(SF)</text>
    <text x="226" y="224" text-anchor="middle" font-size="11" class="fig-green">S2</text>
    <text x="342" y="224" text-anchor="middle" font-size="11" class="fig-green">S1</text>
    <text x="458" y="224" text-anchor="middle" font-size="11" class="fig-green">S0</text>
    <text x="625" y="95" text-anchor="middle" font-size="11.5" class="fig-red">OF = C4 ⊕ C3</text>
    <text x="625" y="112" text-anchor="middle" font-size="10" class="fig-muted">有符号补码溢出标志</text>
    <text x="625" y="147" text-anchor="middle" font-size="11.5" class="fig-orange">CF = C4 ⊕ Sub</text>
    <text x="625" y="164" text-anchor="middle" font-size="10" class="fig-muted">无符号进位/借位标志</text>
    <text x="625" y="197" text-anchor="middle" font-size="11.5" class="fig-purple">SF = S3</text>
    <text x="625" y="213" text-anchor="middle" font-size="10" class="fig-muted">有符号结果正负标志</text>
    <text x="625" y="245" text-anchor="middle" font-size="10.5" class="fig-green">ZF = /(S3+S2+S1+S0)</text>
    <text x="620" y="262" text-anchor="middle" font-size="10" class="fig-muted">全零或非门判零标志</text>
    <text x="275" y="264" text-anchor="middle" font-size="11" class="fig-muted">Sub=1 时 Y 逐位取反且 C0=1（末位加一），实现 [X-Y]补 = [X]补 + [/Y] + 1</text>
  </svg>
  <figcaption>图 3.6：4 位补码可控加减法器及 ALU 四大状态标志（OF、CF、SF、ZF）硬件生成逻辑</figcaption>
</figure>

<strong>求补电路</strong>（把补码数值位转成原码数值位，供无符号阵列乘法器使用）：

- 符号位为 0（正数）：数值位不变，直接输出；
- 符号位为 1（负数）：数值位按位取反、末位加一。

用 $N$ 个全加器实现时，符号位同时作为各位异或门的选择信号和 $C_0$；更省的做法是模仿“扫描法”——从最低位起，保留第一个 1 及其右侧的位，对其左侧各位取反，可用或门链检测“是否已遇到 1”，硬件开销远小于全加器方案。

<figure class="cpd-figure not-content" id="hco-w2-fig-7">
  <svg viewBox="0 0 720 284" role="img" aria-label="补码数值位求补电路全加器方案与或门链扫描方案硬件结构对比图">
    <defs>
      <marker id="hco-w2-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="292" height="252" rx="8" class="fig-box-subtle"/>
    <rect x="322" y="16" width="382" height="252" rx="8" class="fig-box-subtle"/>
    <line x1="184" y1="68" x2="184" y2="96" class="fig-line" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="88" y1="112" x2="112" y2="112" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-7)"/>
    <path d="M 58 126 L 58 168 L 112 168" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="184" y1="126" x2="184" y2="144" class="fig-line" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="184" y1="190" x2="184" y2="210" class="fig-line-accent" marker-end="url(#hco-w2-arr-7)"/>
    <rect x="28" y="98" width="60" height="28" rx="4" class="fig-box-purple"/>
    <rect x="114" y="98" width="140" height="28" rx="4" class="fig-box-blue"/>
    <rect x="114" y="146" width="140" height="44" rx="6" class="fig-box"/>
    <rect x="102" y="212" width="164" height="28" rx="5" class="fig-box-green"/>
    <line x1="407" y1="76" x2="407" y2="140" class="fig-line" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="407" y1="168" x2="407" y2="186" class="fig-line-accent" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="489" y1="76" x2="489" y2="140" class="fig-line" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="489" y1="168" x2="489" y2="186" class="fig-line-accent" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="571" y1="76" x2="571" y2="186" class="fig-line" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="655" y1="76" x2="655" y2="186" class="fig-line" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="655" y1="101" x2="554" y2="101" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="571" y1="111" x2="554" y2="111" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="514" y1="101" x2="474" y2="101" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-7)"/>
    <line x1="489" y1="111" x2="474" y2="111" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-7)"/>
    <path d="M 533 118 L 533 155 L 514 155" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-7)"/>
    <path d="M 453 118 L 453 155 L 432 155" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-7)"/>
    <path d="M 350 76 L 350 155 L 382 155" class="fig-line-dashed" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-7)"/>
    <path d="M 350 132 L 480 132 L 480 140" class="fig-line-dashed" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-7)"/>
    <circle cx="655" cy="101" r="2.5" class="fig-dot"/>
    <circle cx="571" cy="111" r="2.5" class="fig-dot"/>
    <circle cx="489" cy="111" r="2.5" class="fig-dot"/>
    <circle cx="350" cy="132" r="2.5" class="fig-dot"/>
    <rect x="330" y="52" width="40" height="24" rx="4" class="fig-box-purple"/>
    <rect x="384" y="52" width="46" height="24" rx="4" class="fig-box"/>
    <rect x="466" y="52" width="46" height="24" rx="4" class="fig-box"/>
    <rect x="548" y="52" width="46" height="24" rx="4" class="fig-box-orange"/>
    <rect x="632" y="52" width="46" height="24" rx="4" class="fig-box"/>
    <rect x="514" y="94" width="38" height="24" rx="4" class="fig-box-orange"/>
    <rect x="434" y="94" width="38" height="24" rx="4" class="fig-box-orange"/>
    <rect x="384" y="142" width="46" height="26" rx="4" class="fig-box-blue"/>
    <rect x="466" y="142" width="46" height="26" rx="4" class="fig-box-blue"/>
    <rect x="382" y="188" width="50" height="26" rx="4" class="fig-box-green"/>
    <rect x="464" y="188" width="50" height="26" rx="4" class="fig-box-green"/>
    <rect x="544" y="188" width="54" height="26" rx="4" class="fig-box-orange"/>
    <rect x="630" y="188" width="50" height="26" rx="4" class="fig-box"/>
    <text x="162" y="37" text-anchor="middle" font-size="13" class="fig-title">方案一：全加器求补电路</text>
    <text x="184" y="61" text-anchor="middle" font-size="10.5" class="fig-label">补码数值位输入</text>
    <text x="58" y="116" text-anchor="middle" font-size="11" class="fig-purple">符号 Xf</text>
    <text x="86" y="162" text-anchor="middle" font-size="9.5" class="fig-purple">C0=Xf</text>
    <text x="184" y="116" text-anchor="middle" font-size="10.5" class="fig-blue">N 位 XOR 阵（⊕ Xf）</text>
    <text x="184" y="166" text-anchor="middle" font-size="11.5" class="fig-label">N 位全加器链（C0=Xf）</text>
    <text x="184" y="182" text-anchor="middle" font-size="10" class="fig-muted">另一输入全接 0</text>
    <text x="184" y="230" text-anchor="middle" font-size="11" class="fig-green">原码数值位 |X|</text>
    <text x="162" y="256" text-anchor="middle" font-size="10.5" class="fig-muted">Xf=1 时取反加一；硬件开销较大</text>
    <text x="513" y="37" text-anchor="middle" font-size="13" class="fig-title">方案二：或门链扫描求补（例：1 1010 → 0110）</text>
    <text x="350" y="68" text-anchor="middle" font-size="10.5" class="fig-purple">Xf=1</text>
    <text x="407" y="68" text-anchor="middle" font-size="10.5" class="fig-label">x3=1</text>
    <text x="489" y="68" text-anchor="middle" font-size="10.5" class="fig-label">x2=0</text>
    <text x="571" y="68" text-anchor="middle" font-size="10.5" class="fig-orange">x1=1</text>
    <text x="655" y="68" text-anchor="middle" font-size="10.5" class="fig-label">x0=0</text>
    <text x="533" y="110" text-anchor="middle" font-size="9.5" class="fig-orange">OR=1</text>
    <text x="453" y="110" text-anchor="middle" font-size="9.5" class="fig-orange">OR=1</text>
    <text x="407" y="159" text-anchor="middle" font-size="10" class="fig-blue">取反</text>
    <text x="489" y="159" text-anchor="middle" font-size="10" class="fig-blue">取反</text>
    <text x="407" y="205" text-anchor="middle" font-size="11" class="fig-green">0</text>
    <text x="489" y="205" text-anchor="middle" font-size="11" class="fig-green">1</text>
    <text x="571" y="205" text-anchor="middle" font-size="10.5" class="fig-orange">1(首1)</text>
    <text x="655" y="205" text-anchor="middle" font-size="10.5" class="fig-label">0(保)</text>
    <text x="513" y="236" text-anchor="middle" font-size="11" class="fig-accent">扫描规则：从最低位起，首个 1 及右侧保留，左侧遇 Xf=1 取反</text>
    <text x="513" y="256" text-anchor="middle" font-size="10.5" class="fig-muted">仅需或门链 + 异或门（与门受 Xf 使能），省去全加器进位链</text>
  </svg>
  <figcaption>图 3.7：补码数值位转原码绝对值的两种求补电路对比（全加器方案 vs 最低位扫描或门链方案）</figcaption>
</figure>

### 3.4 无符号数乘法的硬件实现

二进制笔算乘法：乘数每一位只有 0/1，位积要么是被乘数本身、要么是 0，逐个错位相加即可。机器实现的两个障碍：多个位积需多个寄存器暂存；乘积位宽翻倍（$n$ 位 × $n$ 位 = $2n$ 位）。

改进思路：从乘数最低位开始，每轮“<strong>若当前乘数最低位为 1 则加被乘数，然后整体右移一位</strong>”，共 $n$ 轮，只需 $n$ 位加法器和三个 $n$ 位寄存器。

<strong>硬件组成</strong>：$X$ 存被乘数，$MQ$ 存乘数（兼存乘积低位），$ACC$ 存部分积（兼存乘积高位），$C_{out}$ 保存加法器进位，计数器 $C_n$ 计轮次，控制逻辑根据 $MQ_0$ 决定是否加。

每轮操作：

1. 若 $MQ_0=1$：$ACC \leftarrow ACC + X$，进位存入 $C_{out}$；若 $MQ_0=0$ 则跳过加法；
2. $(C_{out}, ACC, MQ)$ <strong>逻辑右移一位</strong>：$C_{out}$ 移入 $ACC$ 最高位，$ACC$ 最低位移入 $MQ$ 最高位，$MQ_0$ 移出丢弃；
3. 计数器减一，为 0 时结束。乘积高位在 $ACC$、低位在 $MQ$。

例：$X=1101$（13），$Y=1011$（11）：

| 轮次 | $MQ_0$ | 加法后 $C_{out},ACC$ | 右移后 $C_{out},ACC,MQ$ |
|---|---|---|---|
| 初始 | — | 0, 0000 | 0, 0000, 1011 |
| 1 | 1 | 0, 1101 | 0, 0110, 1101 |
| 2 | 1 | 1, 0011 | 0, 1001, 1110 |
| 3 | 0 | 不加法 | 0, 0100, 1111 |
| 4 | 1 | 1, 0001 | 0, 1000, 1111 |

结果 $ACC\,MQ = 1000\,1111_2 = 143 = 13\times 11$。

<figure class="cpd-figure not-content" id="hco-w2-fig-8">
  <svg viewBox="0 0 720 310" role="img" aria-label="无符号数一位乘法硬件数据通路与Cout、ACC、MQ联合逻辑右移示意图">
    <defs>
      <marker id="hco-w2-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="278" rx="8" class="fig-box-subtle"/>
    <rect x="44" y="176" width="472" height="84" rx="6" class="fig-box-dashed"/>
    <line x1="138" y1="88" x2="188" y2="114" class="fig-line" marker-end="url(#hco-w2-arr-8)"/>
    <path d="M 296 190 L 296 170 L 326 170 L 326 98 L 266 98 L 266 114" class="fig-line" marker-end="url(#hco-w2-arr-8)"/>
    <path d="M 176 140 L 86 140 L 86 188" class="fig-line-accent" marker-end="url(#hco-w2-arr-8)"/>
    <line x1="244" y1="162" x2="244" y2="188" class="fig-line-accent" marker-end="url(#hco-w2-arr-8)"/>
    <line x1="34" y1="212" x2="54" y2="212" class="fig-line-accent" marker-end="url(#hco-w2-arr-8)"/>
    <line x1="118" y1="212" x2="148" y2="212" class="fig-line-accent" marker-end="url(#hco-w2-arr-8)"/>
    <line x1="316" y1="212" x2="346" y2="212" class="fig-line-accent" marker-end="url(#hco-w2-arr-8)"/>
    <path d="M 510 218 L 530 218 L 530 244" class="fig-line" marker-end="url(#hco-w2-arr-8)"/>
    <path d="M 494 190 L 494 144 L 548 144" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-8)"/>
    <line x1="548" y1="128" x2="314" y2="128" class="fig-line-dashed" marker-end="url(#hco-w2-arr-8)"/>
    <line x1="620" y1="176" x2="620" y2="198" class="fig-line" marker-end="url(#hco-w2-arr-8)"/>
    <rect x="58" y="44" width="160" height="44" rx="6" class="fig-box"/>
    <rect x="176" y="116" width="136" height="46" rx="6" class="fig-box-blue"/>
    <rect x="56" y="190" width="62" height="44" rx="5" class="fig-box-orange"/>
    <rect x="150" y="190" width="166" height="44" rx="5" class="fig-box-accent"/>
    <rect x="348" y="190" width="162" height="44" rx="5" class="fig-box-green"/>
    <rect x="550" y="106" width="140" height="70" rx="6" class="fig-box-purple"/>
    <rect x="550" y="200" width="140" height="48" rx="6" class="fig-box"/>
    <text x="360" y="36" text-anchor="middle" font-size="13" class="fig-title">无符号数一位乘法硬件数据通路（移位-相加结构）</text>
    <text x="138" y="65" text-anchor="middle" font-size="12" class="fig-label">寄存器 X（被乘数）</text>
    <text x="138" y="81" text-anchor="middle" font-size="10" class="fig-muted">n 位保持不变</text>
    <text x="244" y="137" text-anchor="middle" font-size="12" class="fig-blue">n 位加法器 ALU</text>
    <text x="244" y="153" text-anchor="middle" font-size="10" class="fig-muted">ACC + X → Cout, ACC</text>
    <text x="20" y="204" text-anchor="start" font-size="9.5" class="fig-accent">补0</text>
    <text x="87" y="210" text-anchor="middle" font-size="11.5" class="fig-orange">Cout</text>
    <text x="87" y="226" text-anchor="middle" font-size="10" class="fig-muted">1位进位</text>
    <text x="233" y="210" text-anchor="middle" font-size="11.5" class="fig-accent">ACC（部分积 / 积高 n 位）</text>
    <text x="233" y="226" text-anchor="middle" font-size="10" class="fig-muted">初值全 0</text>
    <text x="431" y="210" text-anchor="middle" font-size="11.5" class="fig-green">MQ（乘数 / 积低 n 位）</text>
    <text x="431" y="226" text-anchor="middle" font-size="10" class="fig-muted">最低位 MQ0 判加</text>
    <text x="530" y="211" text-anchor="middle" font-size="9.5" class="fig-muted">丢弃</text>
    <text x="280" y="251" text-anchor="middle" font-size="10.5" class="fig-accent">联合逻辑右移 1 位：(Cout , ACC , MQ) 整体右移，每轮必移</text>
    <text x="518" y="138" text-anchor="middle" font-size="10" class="fig-purple">MQ0</text>
    <text x="430" y="122" text-anchor="middle" font-size="10" class="fig-muted">加控制信号</text>
    <text x="620" y="128" text-anchor="middle" font-size="12" class="fig-purple">乘除控制逻辑</text>
    <text x="620" y="146" text-anchor="middle" font-size="10.5" class="fig-label">MQ0=1：加 X 后右移</text>
    <text x="620" y="163" text-anchor="middle" font-size="10.5" class="fig-muted">MQ0=0：跳过加仅右移</text>
    <text x="620" y="220" text-anchor="middle" font-size="11.5" class="fig-label">循环计数器 Cn</text>
    <text x="620" y="237" text-anchor="middle" font-size="10" class="fig-muted">初值 n，每轮减 1 至 0</text>
    <text x="360" y="282" text-anchor="middle" font-size="11" class="fig-muted">仅需 1 个 n 位 ALU + 2 个 n 位移位寄存器即可完成 2n 位乘积运算</text>
  </svg>
  <figcaption>图 3.8：无符号数一位乘法器硬件组成与 (Cout, ACC, MQ) 联合逻辑右移数据通路</figcaption>
</figure>

> <strong>注意</strong>：每轮固定执行一次右移（无论是否加法），因为位积要逐位对齐；不做加法时也必须移位。

<figure class="cpd-figure not-content" id="hco-w2-fig-9">
  <svg viewBox="0 0 720 324" role="img" aria-label="无符号乘法四轮寄存器状态追踪与Booth补码一位乘硬件规则对比图">
    <defs>
      <marker id="hco-w2-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="322" height="292" rx="8" class="fig-box-subtle"/>
    <rect x="350" y="16" width="354" height="292" rx="8" class="fig-box-subtle"/>
    <rect x="28" y="48" width="298" height="36" rx="5" class="fig-box"/>
    <rect x="28" y="92" width="298" height="42" rx="5" class="fig-box-blue"/>
    <rect x="28" y="142" width="298" height="42" rx="5" class="fig-box-blue"/>
    <rect x="28" y="192" width="298" height="42" rx="5" class="fig-box"/>
    <rect x="28" y="242" width="298" height="52" rx="5" class="fig-box-green"/>
    <rect x="362" y="48" width="330" height="56" rx="6" class="fig-box"/>
    <rect x="362" y="114" width="330" height="182" rx="6" class="fig-box-accent"/>
    <line x1="374" y1="176" x2="680" y2="176" class="fig-divider"/>
    <rect x="374" y="144" width="112" height="24" rx="4" class="fig-box-blue"/>
    <rect x="492" y="144" width="108" height="24" rx="4" class="fig-box-green"/>
    <rect x="606" y="144" width="70" height="24" rx="4" class="fig-box-purple"/>
    <rect x="374" y="186" width="146" height="24" rx="4" class="fig-box-green"/>
    <rect x="530" y="186" width="146" height="24" rx="4" class="fig-box-red"/>
    <rect x="374" y="216" width="302" height="24" rx="4" class="fig-box"/>
    <text x="177" y="37" text-anchor="middle" font-size="13" class="fig-title">13(1101) × 11(1011) 四轮移位追踪</text>
    <text x="38" y="70" font-size="11" class="fig-label">初态：Cout=0 , ACC=0000 , MQ=1011</text>
    <text x="38" y="109" font-size="11" class="fig-blue">第1轮 (MQ0=1)：+1101 → 0,1101</text>
    <text x="38" y="126" font-size="10.5" class="fig-muted">逻辑右移 1 位 → 0 , 0110 , 110[1]</text>
    <text x="38" y="159" font-size="11" class="fig-blue">第2轮 (MQ0=1)：+1101 → 1,0011（进位1）</text>
    <text x="38" y="176" font-size="10.5" class="fig-muted">Cout=1 移入 ACC → 0 , 1001 , 111[0]</text>
    <text x="38" y="209" font-size="11" class="fig-label">第3轮 (MQ0=0)：跳过加法（+0000）</text>
    <text x="38" y="226" font-size="10.5" class="fig-muted">仍须逻辑右移 → 0 , 0100 , 111[1]</text>
    <text x="38" y="260" font-size="11" class="fig-green">第4轮 (MQ0=1)：+1101 → 1,0001</text>
    <text x="38" y="277" font-size="11" class="fig-green">右移得终积：ACC,MQ = 1000 1111₂ = 143</text>
    <text x="527" y="37" text-anchor="middle" font-size="13" class="fig-title">有符号乘法扩展：原码一位乘 vs Booth 算法</text>
    <text x="374" y="68" font-size="11.5" class="fig-label">• 原码一位乘：符号 Pf = Xf ⊕ Yf 单独异或</text>
    <text x="374" y="88" font-size="10.5" class="fig-muted">数值位取绝对值 |X|·|Y|，完全复用无符号逻辑右移 n 轮</text>
    <text x="374" y="134" font-size="11.5" class="fig-accent">• Booth 补码一位乘：符号参算 + 附加位 y-1（初值0）</text>
    <text x="430" y="160" text-anchor="middle" font-size="10.5" class="fig-blue">ACC(双符号)</text>
    <text x="546" y="160" text-anchor="middle" font-size="10.5" class="fig-green">MQ(含符号)</text>
    <text x="641" y="160" text-anchor="middle" font-size="10.5" class="fig-purple">附加 y-1</text>
    <text x="447" y="202" text-anchor="middle" font-size="11" class="fig-green">yi yi-1 = 01：+[X]补</text>
    <text x="603" y="202" text-anchor="middle" font-size="11" class="fig-red">yi yi-1 = 10：+[-X]补</text>
    <text x="525" y="232" text-anchor="middle" font-size="11" class="fig-label">yi yi-1 = 00 或 11：不加减，仅算术右移</text>
    <text x="374" y="262" font-size="10.5" class="fig-label">移位：前 n 步执行“加减 + 补码算术右移”（高位补符号）</text>
    <text x="374" y="280" font-size="9.8" class="fig-orange">★ 末步（第 n+1 步）：仅按 ys yn-1 做最后加减校正，不移位！</text>
  </svg>
  <figcaption>图 3.9：无符号乘法逐轮寄存器状态追踪及原码一位乘、Booth 补码一位乘核心机制对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w2-fig-10">
  <svg viewBox="0 0 720 296" role="img" aria-label="定点原码一位除法恢复余数法与加减交替法数据通路及步进规则对比图">
    <defs>
      <marker id="hco-w2-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="306" height="264" rx="8" class="fig-box-subtle"/>
    <rect x="334" y="16" width="370" height="264" rx="8" class="fig-box-subtle"/>
    <line x1="169" y1="88" x2="169" y2="112" class="fig-line" marker-end="url(#hco-w2-arr-10)"/>
    <line x1="112" y1="162" x2="112" y2="188" class="fig-line-accent" marker-end="url(#hco-w2-arr-10)"/>
    <line x1="184" y1="210" x2="162" y2="210" class="fig-line-accent" marker-end="url(#hco-w2-arr-10)"/>
    <line x1="306" y1="210" x2="284" y2="210" class="fig-line-accent" marker-end="url(#hco-w2-arr-10)"/>
    <rect x="54" y="46" width="230" height="42" rx="6" class="fig-box"/>
    <rect x="64" y="114" width="210" height="48" rx="6" class="fig-box-blue"/>
    <rect x="32" y="190" width="130" height="42" rx="5" class="fig-box-accent"/>
    <rect x="186" y="190" width="98" height="42" rx="5" class="fig-box-green"/>
    <rect x="346" y="48" width="346" height="68" rx="6" class="fig-box"/>
    <rect x="346" y="126" width="346" height="142" rx="6" class="fig-box-accent"/>
    <rect x="358" y="156" width="322" height="34" rx="4" class="fig-box-green"/>
    <rect x="358" y="196" width="322" height="34" rx="4" class="fig-box-orange"/>
    <text x="169" y="37" text-anchor="middle" font-size="13" class="fig-title">原码除法通用左移数据通路</text>
    <text x="169" y="65" text-anchor="middle" font-size="11" class="fig-label">除数寄存器 Y：存 |Y| 与 [-|Y|]补</text>
    <text x="169" y="81" text-anchor="middle" font-size="10" class="fig-muted">商符 Qf = Xf ⊕ Yf 单独异或</text>
    <text x="169" y="135" text-anchor="middle" font-size="11.5" class="fig-blue">双符号/单符号补码 ALU</text>
    <text x="169" y="151" text-anchor="middle" font-size="10" class="fig-muted">执行 R + [-|Y|]补 或 R + [|Y|]补</text>
    <text x="97" y="209" text-anchor="middle" font-size="11.5" class="fig-accent">ACC（余数 R）</text>
    <text x="97" y="224" text-anchor="middle" font-size="9.5" class="fig-muted">初值 |X| 高位</text>
    <text x="235" y="209" text-anchor="middle" font-size="11.5" class="fig-green">MQ（商 Q）</text>
    <text x="235" y="224" text-anchor="middle" font-size="9.5" class="fig-muted">末位上商 qi</text>
    <text x="312" y="202" text-anchor="end" font-size="10" class="fig-green">上商</text>
    <text x="169" y="252" text-anchor="middle" font-size="10.5" class="fig-muted">(ACC, MQ) 联合逻辑左移；首步够减则商溢出</text>
    <text x="169" y="268" text-anchor="middle" font-size="10" class="fig-muted">最终真余数 = 恢复后正余数 × 2⁻ⁿ（与被除数同号）</text>
    <text x="519" y="37" text-anchor="middle" font-size="13" class="fig-title">恢复余数法 vs 加减交替法（不恢复余数法）</text>
    <text x="358" y="68" font-size="11.5" class="fig-label">① 恢复余数法：减 |Y| 后若余数 R &lt; 0，商上 0</text>
    <text x="358" y="86" font-size="10.5" class="fig-muted">必须先加 |Y| 恢复原正余数，再逻辑左移（每轮步数不均）</text>
    <text x="358" y="104" font-size="10.5" class="fig-muted">R ≥ 0 时商上 1，直接逻辑左移进入下一轮减 |Y|</text>
    <text x="358" y="146" font-size="11.5" class="fig-accent">② 加减交替法（408 高频）：合并恢复与左移，节拍规整</text>
    <text x="366" y="177" font-size="9.8" class="fig-green">余数 R ≥ 0（够减）：商上 1 → 下步“左移 1 位再减 |Y|”</text>
    <text x="366" y="217" font-size="9.8" class="fig-orange">余数 R &lt; 0（不够减）：商上 0 → 下步“左移 1 位再加 |Y|”</text>
    <text x="356" y="246" font-size="10" class="fig-label">次数：共加减 n+1 次、上商 n+1 位、左移 n 次</text>
    <text x="356" y="262" font-size="9.6" class="fig-red">★ 收尾：若第 n+1 步余数为负，需额外 +|Y| 恢复正余数（不移位）</text>
  </svg>
  <figcaption>图 3.10：定点原码一位除法通用左移通路与恢复余数法、加减交替法步进规则对比</figcaption>
</figure>

### 3.5 阵列乘法器与补码阵列乘法器

<strong>无符号阵列乘法器：$N\times N$ 位，输入两个 $N$ 位无符号数，输出 $2N$ 位乘积。由 $N^2$ 个与门产生全部位积 $p_{ij}=x_i y_j$，再由 $N(N-1)$ 个全加器组成的阵列按对角线逐级求和，组合逻辑、无时钟</strong>，一次通过即得乘积，速度远高于串行移位加法。

<figure class="cpd-figure not-content" id="hco-w2-fig-11">
  <svg viewBox="0 0 720 296" role="img" aria-label="4乘4无符号阵列乘法器与门位积矩阵及等权对角线全加器求和结构图">
    <defs>
      <marker id="hco-w2-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="366" height="264" rx="8" class="fig-box-subtle"/>
    <rect x="396" y="16" width="308" height="264" rx="8" class="fig-box-subtle"/>
    <line x1="112" y1="158" x2="176" y2="200" class="fig-line-dashed"/>
    <line x1="112" y1="116" x2="240" y2="200" class="fig-line-dashed"/>
    <line x1="112" y1="74" x2="304" y2="200" class="fig-line-accent"/>
    <line x1="176" y1="74" x2="304" y2="158" class="fig-line-dashed"/>
    <line x1="240" y1="74" x2="304" y2="116" class="fig-line-dashed"/>
    <line x1="356" y1="138" x2="394" y2="138" class="fig-line-accent" marker-end="url(#hco-w2-arr-11)"/>
    <line x1="550" y1="106" x2="550" y2="124" class="fig-line" marker-end="url(#hco-w2-arr-11)"/>
    <line x1="550" y1="188" x2="550" y2="206" class="fig-line-accent" marker-end="url(#hco-w2-arr-11)"/>
    <rect x="90" y="62" width="44" height="24" rx="4" class="fig-box-blue"/>
    <rect x="154" y="62" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="218" y="62" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="282" y="62" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="90" y="104" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="154" y="104" width="44" height="24" rx="4" class="fig-box-blue"/>
    <rect x="218" y="104" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="282" y="104" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="90" y="146" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="154" y="146" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="218" y="146" width="44" height="24" rx="4" class="fig-box-blue"/>
    <rect x="282" y="146" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="90" y="188" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="154" y="188" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="218" y="188" width="44" height="24" rx="4" class="fig-box"/>
    <rect x="282" y="188" width="44" height="24" rx="4" class="fig-box-blue"/>
    <rect x="412" y="50" width="276" height="56" rx="6" class="fig-box"/>
    <rect x="412" y="126" width="276" height="62" rx="6" class="fig-box-blue"/>
    <rect x="412" y="208" width="276" height="56" rx="6" class="fig-box-green"/>
    <text x="199" y="37" text-anchor="middle" font-size="13" class="fig-title">4×4 位积矩阵 pij = xi·yj 与等权斜线</text>
    <text x="72" y="78" text-anchor="end" font-size="11" class="fig-muted">y3</text>
    <text x="72" y="120" text-anchor="end" font-size="11" class="fig-muted">y2</text>
    <text x="72" y="162" text-anchor="end" font-size="11" class="fig-muted">y1</text>
    <text x="72" y="204" text-anchor="end" font-size="11" class="fig-muted">y0</text>
    <text x="112" y="232" text-anchor="middle" font-size="11" class="fig-muted">x0</text>
    <text x="176" y="232" text-anchor="middle" font-size="11" class="fig-muted">x1</text>
    <text x="240" y="232" text-anchor="middle" font-size="11" class="fig-muted">x2</text>
    <text x="304" y="232" text-anchor="middle" font-size="11" class="fig-muted">x3</text>
    <text x="112" y="78" text-anchor="middle" font-size="10.5" class="fig-blue">x0y3</text>
    <text x="176" y="78" text-anchor="middle" font-size="10.5" class="fig-label">x1y3</text>
    <text x="240" y="78" text-anchor="middle" font-size="10.5" class="fig-label">x2y3</text>
    <text x="304" y="78" text-anchor="middle" font-size="10.5" class="fig-label">x3y3</text>
    <text x="112" y="120" text-anchor="middle" font-size="10.5" class="fig-label">x0y2</text>
    <text x="176" y="120" text-anchor="middle" font-size="10.5" class="fig-blue">x1y2</text>
    <text x="240" y="120" text-anchor="middle" font-size="10.5" class="fig-label">x2y2</text>
    <text x="304" y="120" text-anchor="middle" font-size="10.5" class="fig-label">x3y2</text>
    <text x="112" y="162" text-anchor="middle" font-size="10.5" class="fig-label">x0y1</text>
    <text x="176" y="162" text-anchor="middle" font-size="10.5" class="fig-label">x1y1</text>
    <text x="240" y="162" text-anchor="middle" font-size="10.5" class="fig-blue">x2y1</text>
    <text x="304" y="162" text-anchor="middle" font-size="10.5" class="fig-label">x3y1</text>
    <text x="112" y="204" text-anchor="middle" font-size="10.5" class="fig-label">x0y0</text>
    <text x="176" y="204" text-anchor="middle" font-size="10.5" class="fig-label">x1y0</text>
    <text x="240" y="204" text-anchor="middle" font-size="10.5" class="fig-label">x2y0</text>
    <text x="304" y="204" text-anchor="middle" font-size="10.5" class="fig-blue">x3y0</text>
    <text x="199" y="254" text-anchor="middle" font-size="10.5" class="fig-blue">蓝色斜线 i+j=3 同权值 2³，送同一列全加器求和</text>
    <text x="199" y="270" text-anchor="middle" font-size="10" class="fig-muted">共 7 条斜线对应 P0(2⁰) ~ P6,P7(2⁷)</text>
    <text x="550" y="37" text-anchor="middle" font-size="13" class="fig-title">N×N 无符号阵列硬件构成</text>
    <text x="550" y="73" text-anchor="middle" font-size="11.5" class="fig-label">① 与门阵列：共 N² 个与门</text>
    <text x="550" y="92" text-anchor="middle" font-size="10.5" class="fig-muted">1T 并行产生全部 N² 个位积项</text>
    <text x="550" y="149" text-anchor="middle" font-size="11.5" class="fig-blue">② 斜向求和阵列：N(N-1) 个全加器</text>
    <text x="550" y="167" text-anchor="middle" font-size="10.5" class="fig-muted">4×4 需 16 个与门 + 12 个全加器</text>
    <text x="550" y="181" text-anchor="middle" font-size="10" class="fig-muted">按对角线权值逐行累加并向高位进位</text>
    <text x="550" y="231" text-anchor="middle" font-size="11.5" class="fig-green">③ 纯组合逻辑输出 2N 位乘积</text>
    <text x="550" y="250" text-anchor="middle" font-size="10.5" class="fig-muted">无时钟节拍，延迟 O(N)，硬件复杂度 O(N²)</text>
  </svg>
  <figcaption>图 3.11：4×4 无符号阵列乘法器位积等权对角线分布与 N×N 硬件组成</figcaption>
</figure>

<strong>补码阵列乘法器</strong>：补码不能直接送无符号阵列相乘（符号位会当作数值位）。做法是“<strong>符号位单独处理 + 数值位求补</strong>”：

1. 乘积符号位 $P_f = X_f \oplus Y_f$；
2. 用两个 $N$ 位求补电路，按 $X_f$、$Y_f$ 把补码数值位转成原码数值位（即绝对值）；
3. 送入 $N\times N$ 无符号阵列乘法器，得 $2N$ 位无符号乘积；
4. 用 $2N$ 位求补电路，按 $P_f$ 把乘积转成补码数值位；
5. 拼接 $P_f$ 与数值位，得补码乘积。

四种符号组合下：同号时两端求补电路均“直通”或均“取补两次”相互抵消，异号时恰有一次生效，结果均正确。

> <strong>注意</strong>：被乘数、乘数各自的求补由<strong>它们自己的符号位</strong>控制；乘积的求补由<strong>乘积符号位 $P_f$</strong> 控制，三者控制信号不同。

<figure class="cpd-figure not-content" id="hco-w2-fig-12">
  <svg viewBox="0 0 720 264" role="img" aria-label="补码阵列乘法器三级可控求补器与无符号阵列乘法器五步流水框图">
    <defs>
      <marker id="hco-w2-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="232" rx="8" class="fig-box-subtle"/>
    <path d="M 76 88 L 76 52 L 268 52" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-12)"/>
    <path d="M 42 190 L 26 190 L 26 66 L 268 66" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-12)"/>
    <path d="M 114 88 L 114 78 L 204 78 L 204 98" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-12)"/>
    <path d="M 114 212 L 114 216 L 204 216 L 204 202" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="134" y1="118" x2="152" y2="118" class="fig-line" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="134" y1="182" x2="152" y2="182" class="fig-line" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="256" y1="118" x2="286" y2="132" class="fig-line-accent" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="256" y1="182" x2="286" y2="168" class="fig-line-accent" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="436" y1="150" x2="466" y2="150" class="fig-line-accent" marker-end="url(#hco-w2-arr-12)"/>
    <path d="M 366 60 L 638 60 L 638 112" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="524" y1="60" x2="524" y2="112" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w2-arr-12)"/>
    <line x1="580" y1="150" x2="596" y2="150" class="fig-line-accent" marker-end="url(#hco-w2-arr-12)"/>
    <circle cx="524" cy="60" r="3" class="fig-dot"/>
    <rect x="42" y="88" width="92" height="44" rx="5" class="fig-box"/>
    <rect x="42" y="168" width="92" height="44" rx="5" class="fig-box"/>
    <rect x="270" y="42" width="96" height="36" rx="5" class="fig-box-purple"/>
    <rect x="154" y="100" width="102" height="40" rx="5" class="fig-box-orange"/>
    <rect x="154" y="162" width="102" height="40" rx="5" class="fig-box-orange"/>
    <rect x="288" y="112" width="148" height="76" rx="6" class="fig-box-blue"/>
    <rect x="468" y="114" width="112" height="72" rx="6" class="fig-box-red"/>
    <rect x="598" y="114" width="94" height="72" rx="6" class="fig-box-green"/>
    <text x="150" y="36" text-anchor="middle" font-size="13" class="fig-title">补码阵列乘法器五步流水</text>
    <text x="90" y="49" font-size="9.5" class="fig-purple">Xf</text>
    <text x="36" y="62" font-size="9.5" class="fig-purple">Yf</text>
    <text x="88" y="107" text-anchor="middle" font-size="11" class="fig-label">[X]补 输入</text>
    <text x="88" y="123" text-anchor="middle" font-size="9.5" class="fig-muted">Xf | N位数值</text>
    <text x="88" y="187" text-anchor="middle" font-size="11" class="fig-label">[Y]补 输入</text>
    <text x="88" y="203" text-anchor="middle" font-size="9.5" class="fig-muted">Yf | N位数值</text>
    <text x="318" y="58" text-anchor="middle" font-size="11" class="fig-purple">① 符号异或</text>
    <text x="318" y="72" text-anchor="middle" font-size="10" class="fig-muted">Pf = Xf ⊕ Yf</text>
    <text x="205" y="118" text-anchor="middle" font-size="11" class="fig-orange">② N位求补器1</text>
    <text x="205" y="133" text-anchor="middle" font-size="9.5" class="fig-muted">由 Xf 控制 → |X|</text>
    <text x="205" y="180" text-anchor="middle" font-size="11" class="fig-orange">② N位求补器2</text>
    <text x="205" y="195" text-anchor="middle" font-size="9.5" class="fig-muted">由 Yf 控制 → |Y|</text>
    <text x="362" y="142" text-anchor="middle" font-size="11.5" class="fig-blue">③ N×N 无符号</text>
    <text x="362" y="159" text-anchor="middle" font-size="11.5" class="fig-blue">阵列乘法器</text>
    <text x="362" y="177" text-anchor="middle" font-size="10" class="fig-muted">得 2N 位 |X|·|Y|</text>
    <text x="524" y="142" text-anchor="middle" font-size="11.5" class="fig-red">④ 2N位求补器3</text>
    <text x="524" y="159" text-anchor="middle" font-size="10.5" class="fig-red">由 Pf 控制！</text>
    <text x="524" y="176" text-anchor="middle" font-size="9.5" class="fig-muted">Pf=1 时取反加一</text>
    <text x="645" y="142" text-anchor="middle" font-size="11.5" class="fig-green">⑤ 拼接输出</text>
    <text x="645" y="159" text-anchor="middle" font-size="11" class="fig-green">[P]补</text>
    <text x="645" y="176" text-anchor="middle" font-size="9.5" class="fig-muted">Pf | 2N位数值</text>
    <text x="480" y="52" text-anchor="middle" font-size="10" class="fig-purple">积符号 Pf = Xf ⊕ Yf</text>
    <text x="360" y="234" text-anchor="middle" font-size="11" class="fig-muted">★ 考点区分：输入端两个 N 位求补器分别受 Xf、Yf 控制；输出端 2N 位求补器受 Pf 控制</text>
  </svg>
  <figcaption>图 3.12：补码阵列乘法器硬件框图（输入绝对值化 → 无符号阵列相乘 → 按积符号 Pf 求补还原）</figcaption>
</figure>

### 3.6 浮点加减法运算

浮点加减不能直接对阶码和尾数分别运算，必须五步：

1. <strong>对阶：求阶差 $\Delta E = E_x - E_y$，小阶向大阶看齐</strong>，小阶数的尾数右移 $|\Delta E|$ 位、阶码取大者。若反过来让大阶向小阶看齐，尾数需左移，高位被移出会造成严重数值错误。右移出的位<strong>保留为附加位</strong>参加中间运算，以提高精度。
2. <strong>尾数加减</strong>：同符号相加、异符号相减；用补码可统一为加法（减数取补）。
3. <strong>规格化</strong>：采用双符号位（变形补码）。$00.1\cdots$ 或 $11.0\cdots$ 已规格化；$00.0\cdots$ 或 $11.1\cdots$ 需<strong>左归</strong>（尾数每左移一位，阶码减一）；$01.\cdots$ 或 $10.\cdots$ 表示尾数溢出，需<strong>右归</strong>（尾数右移一位，阶码加一）。
4. <strong>舍入</strong>：对阶与右归都会移出低位，按选定方式处理。
5. <strong>溢出判断</strong>：看阶码双符号位——01 为上溢（结果溢出，置溢出标志或按 $\infty$ 处理）；10 为下溢（按机器零处理）。

| 舍入方法 | 做法 | 特点 |
|---|---|---|
| 截断法 | 直接丢弃超出字长的低位 | 简单，误差单向积累 |
| 末位恒置 1 法 | 把保留部分最低位恒置 1 | 损失一位精度，误差积累小 |
| 0 舍 1 入法 | 丢弃部分最高位为 1 时，保留部分末位加一 | 误差最小，但可能破坏规格化，需再次规格化 |

<figure class="cpd-figure not-content" id="hco-w2-fig-13">
  <svg viewBox="0 0 720 308" role="img" aria-label="浮点数加减法五步流水数据通路与尾数规格化、阶码双符号位溢出判别图">
    <defs>
      <marker id="hco-w2-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="16" width="688" height="276" rx="8" class="fig-box-subtle"/>
    <line x1="146" y1="84" x2="160" y2="84" class="fig-line" marker-end="url(#hco-w2-arr-13)"/>
    <line x1="280" y1="84" x2="294" y2="84" class="fig-line" marker-end="url(#hco-w2-arr-13)"/>
    <line x1="424" y1="84" x2="438" y2="84" class="fig-line-accent" marker-end="url(#hco-w2-arr-13)"/>
    <line x1="558" y1="84" x2="572" y2="84" class="fig-line-accent" marker-end="url(#hco-w2-arr-13)"/>
    <path d="M 499 116 L 499 136 L 388 136 L 388 118" class="fig-line-dashed" stroke="var(--sl-color-orange)" marker-end="url(#hco-w2-arr-13)"/>
    <line x1="324" y1="116" x2="324" y2="160" class="fig-line-accent" marker-end="url(#hco-w2-arr-13)"/>
    <line x1="633" y1="116" x2="633" y2="160" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w2-arr-13)"/>
    <rect x="28" y="52" width="118" height="64" rx="6" class="fig-box"/>
    <rect x="162" y="52" width="118" height="64" rx="6" class="fig-box"/>
    <rect x="296" y="52" width="128" height="64" rx="6" class="fig-box-blue"/>
    <rect x="440" y="52" width="118" height="64" rx="6" class="fig-box-orange"/>
    <rect x="574" y="52" width="118" height="64" rx="6" class="fig-box-red"/>
    <rect x="28" y="162" width="352" height="116" rx="6" class="fig-box-blue"/>
    <rect x="394" y="162" width="298" height="116" rx="6" class="fig-box-red"/>
    <text x="360" y="37" text-anchor="middle" font-size="13" class="fig-title">浮点加减法五步流水与双符号位规格化、阶码溢出判别</text>
    <text x="87" y="73" text-anchor="middle" font-size="12" class="fig-title">① 对阶</text>
    <text x="87" y="90" text-anchor="middle" font-size="10" class="fig-label">ΔE = Ex - Ey</text>
    <text x="87" y="106" text-anchor="middle" font-size="10" class="fig-muted">小阶尾数右移保留位</text>
    <text x="221" y="73" text-anchor="middle" font-size="12" class="fig-title">② 尾数加减</text>
    <text x="221" y="90" text-anchor="middle" font-size="10" class="fig-label">变形补码双符号</text>
    <text x="221" y="106" text-anchor="middle" font-size="10" class="fig-muted">减法转补码加法</text>
    <text x="360" y="73" text-anchor="middle" font-size="12" class="fig-blue">③ 规格化</text>
    <text x="360" y="90" text-anchor="middle" font-size="10" class="fig-label">判尾数双符号位</text>
    <text x="360" y="106" text-anchor="middle" font-size="10" class="fig-muted">左归阶减 / 右归阶加</text>
    <text x="499" y="73" text-anchor="middle" font-size="12" class="fig-orange">④ 舍入</text>
    <text x="499" y="90" text-anchor="middle" font-size="10" class="fig-label">截断/恒置1/0舍1入</text>
    <text x="499" y="106" text-anchor="middle" font-size="10" class="fig-muted">处理移出附加位</text>
    <text x="633" y="73" text-anchor="middle" font-size="12" class="fig-red">⑤ 溢出判断</text>
    <text x="633" y="90" text-anchor="middle" font-size="10" class="fig-label">仅看阶码双符号</text>
    <text x="633" y="106" text-anchor="middle" font-size="10" class="fig-muted">决定上溢/机器零</text>
    <text x="458" y="151" text-anchor="middle" font-size="10" class="fig-orange">0舍1入若进位破坏规格化 → 再次右归</text>
    <text x="40" y="182" font-size="11.5" class="fig-blue">阶段 ③ 尾数双符号位判别（尾数溢出可右归吸收）</text>
    <text x="40" y="203" font-size="11" class="fig-green">• 00.1… 或 11.0…：已规格化，无需移位</text>
    <text x="40" y="223" font-size="11" class="fig-label">• 00.0… 或 11.1…：左归（尾数左移 k 位，阶码 -k）</text>
    <text x="40" y="243" font-size="11" class="fig-orange">• 01.x… 或 10.x…：右归（尾数右移 1 位，阶码 +1）</text>
    <text x="40" y="264" font-size="10" class="fig-muted">注：00.00…0 全零尾数直接置机器零，不左归</text>
    <text x="406" y="182" font-size="11.5" class="fig-red">阶段 ⑤ 阶码双符号位判别（决定真溢出）</text>
    <text x="406" y="203" font-size="11" class="fig-green">• 阶码 00 / 11：未溢出，正常输出浮点结果</text>
    <text x="406" y="223" font-size="11" class="fig-red">• 阶码 01（阶码上溢）：结果真溢出（中断或置 ∞）</text>
    <text x="406" y="243" font-size="11" class="fig-orange">• 阶码 10（阶码下溢）：按机器零 0 处理（不中断）</text>
    <text x="406" y="264" font-size="10" class="fig-muted">★ 尾数出现 01/10 只触发右归，绝不直接判溢出！</text>
  </svg>
  <figcaption>图 3.13：浮点数加减法五步硬件流水、0 舍 1 入二次规格化回环与阶码双符号位溢出判据</figcaption>
</figure>

> <strong>★考点：浮点加减中“尾数运算出现 01/10”只说明尾数需要右归，不能立即判定结果溢出</strong>；真正的溢出要看<strong>规格化后阶码</strong>是否上溢/下溢。
>
> <strong>注意</strong>：0 舍 1 入可能使尾数再次变为非规格化数（如 $0.111\cdots1$ 进位成 $1.000\cdots0$），必须重新规格化。

## 第4章 存储系统

### 4.1 存储器的分类与存储系统层次结构

一台计算机包含多种存储器，它们共同构成<strong>存储系统</strong>。开发“大容量、高速度、低成本”的存储系统是计算机技术的关键目标之一。存储器可按五个角度分类：

| 角度 | 类别 | 典型例子 | 要点 |
|---|---|---|---|
| 存储介质 | 磁存储器 | 磁芯、磁带、磁盘、软盘 | 用磁化方向表示 0/1 |
| | 光存储器 | CD-ROM、DVD-ROM | 用不同反射率读出 |
| | 半导体存储器 | 内存条、固态硬盘 | 存取快、体积小、可靠，单位容量成本高 |
| 存取方式 | 顺序存取 | 磁带 | 只能按地址/物理位置顺序访问，速度与位置有关 |
| | 随机存取 | 磁芯、半导体存储器 | 可按任一地址存取，速度与位置无关 |
| | 直接存取 | 磁盘 | 先寻道再顺序读写，兼有两类特性 |
| 可改写性 | 读写存储器 | 磁芯、磁盘、磁带、内存、SSD | 可读可写 |
| | 只读存储器 | CD-ROM、ROM、BIOS 芯片、EEPROM | 只能读，需专用工具改写 |
| 可保存性 | 易失性 | 寄存器、Cache、主存 | 断电即丢失 |
| | 非易失性 | 磁盘、磁带、SSD、EEPROM | 断电不丢失（SSD 长期不通电仍可能丢数据） |
| 作用与速度 | 寄存器、Cache、主存、辅存 | — | 速度递减、容量递增、单位成本递减 |

> <strong>注意</strong>：机械硬盘属<strong>磁</strong>存储器，固态硬盘属<strong>半导体</strong>存储器；软盘 2009 年被淘汰，但“磁盘”不等于“机械硬盘”。

<strong>存储系统层次结构</strong>（金字塔）：寄存器 → Cache → 主存 → 辅存（机械硬盘、光盘、磁带）。越靠上层越接近 CPU，存取越快、单位容量成本越高、使用频率越高；越往下容量越大。上层为下层做缓冲，把最常使用的数据副本调度到上层，使 CPU 大部分时间只需访问快速小容量存储器，从而缓解 CPU 与主存、主存与辅存的速度差异；大容量辅存则弥补容量不足。

<figure class="cpd-figure not-content" id="hco-w3-fig-1">
  <svg viewBox="0 0 700 320" role="img" aria-label="计算机五级存储系统层次金字塔与局部性原理示意图">
    <defs>
      <marker id="hco-w3-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="466" y="20" width="216" height="58" rx="6" class="fig-box-subtle"/>
    <rect x="466" y="90" width="216" height="122" rx="6" class="fig-box-accent"/>
    <rect x="466" y="224" width="216" height="56" rx="6" class="fig-box-subtle"/>
    <path d="M 196 72 L 154 72 L 154 140 L 178 140" class="fig-line" stroke="var(--sl-color-blue)" marker-start="url(#hco-w3-arr-1)" marker-end="url(#hco-w3-arr-1)"/>
    <path d="M 166 154 L 146 154 L 146 206 L 154 206" class="fig-line" stroke="var(--sl-color-purple)" marker-start="url(#hco-w3-arr-1)" marker-end="url(#hco-w3-arr-1)"/>
    <line x1="452" y1="30" x2="452" y2="270" class="fig-line-accent" marker-start="url(#hco-w3-arr-1)" marker-end="url(#hco-w3-arr-1)"/>
    <polygon points="262,22 338,22 356,64 244,64" class="fig-box-red"/>
    <polygon points="244,68 356,68 378,114 222,114" class="fig-box-blue"/>
    <polygon points="222,118 378,118 402,168 198,168" class="fig-box-green"/>
    <polygon points="198,172 402,172 426,224 174,224" class="fig-box-orange"/>
    <polygon points="174,228 426,228 440,280 160,280" class="fig-box"/>
    <rect x="18" y="80" width="128" height="52" rx="5" class="fig-box-blue"/>
    <rect x="18" y="160" width="122" height="54" rx="5" class="fig-box-purple"/>
    <text x="300" y="48" text-anchor="middle" font-size="12" class="fig-red">CPU 寄存器</text>
    <text x="300" y="95" text-anchor="middle" font-size="12" class="fig-blue">Cache（SRAM）</text>
    <text x="300" y="148" text-anchor="middle" font-size="12" class="fig-green">主存（DRAM）</text>
    <text x="300" y="202" text-anchor="middle" font-size="12" class="fig-orange">辅存：SSD / 磁盘</text>
    <text x="300" y="258" text-anchor="middle" font-size="12" class="fig-label">磁带 / 光盘（离线归档）</text>
    <text x="82" y="101" text-anchor="middle" font-size="11" class="fig-blue">Cache—主存层</text>
    <text x="82" y="119" text-anchor="middle" font-size="10" class="fig-muted">硬件调度 · 提速</text>
    <text x="79" y="181" text-anchor="middle" font-size="11" class="fig-purple">主存—辅存层</text>
    <text x="79" y="199" text-anchor="middle" font-size="10" class="fig-muted">OS 虚存 · 扩容</text>
    <text x="572" y="43" text-anchor="middle" font-size="11" class="fig-title">上层：最快 (&lt;1 ns) · 最小 · 易失</text>
    <text x="572" y="62" text-anchor="middle" font-size="10" class="fig-muted">单位容量成本最高，CPU 高频直接访问</text>
    <text x="572" y="112" text-anchor="middle" font-size="11" class="fig-accent">程序访问局部性原理（理论基石）</text>
    <text x="474" y="134" font-size="10.5" class="fig-label">• 时间局部性：刚访问项近期再被访问</text>
    <text x="484" y="151" font-size="10" class="fig-muted">例：循环指令与累加变量 → 驻留上层</text>
    <text x="474" y="173" font-size="10.5" class="fig-label">• 空间局部性：相邻地址近期大概率访问</text>
    <text x="484" y="190" font-size="10" class="fig-muted">例：数组顺序遍历 → 按数据块整块调入</text>
    <text x="572" y="246" text-anchor="middle" font-size="11" class="fig-title">下层：大容量 (TB) · 慢速 · 非易失</text>
    <text x="572" y="265" text-anchor="middle" font-size="10" class="fig-muted">单位比特成本最低，断电长期保存</text>
    <text x="350" y="304" text-anchor="middle" font-size="10.5" class="fig-muted">透明性考点：Cache—主存对所有程序员透明；主存—辅存（虚存）仅对应用程序员透明，对系统程序员不透明</text>
  </svg>
  <figcaption>图 4.1：五级存储系统金字塔、“Cache—主存 / 主存—辅存”两级调度与局部性原理</figcaption>
</figure>

### 4.2 存储器的性能指标

<strong>（1）存储容量</strong>：存储器可存储的二进制信息总量。

$$
\text{Capacity} = \text{word length} \times \text{cell count}
$$

对主存即 $2^{\text{MAR bits}} \times \text{MDR bits}$。

<strong>（2）存取时间</strong>：启动一次存储器操作到该操作完成的时间。分为<strong>读出时间</strong>与<strong>写入时间</strong>，二者可能不同（如闪存读快写慢）。

<strong>（3）存取周期</strong>：连续两次访问存储器操作之间的最小时间间隔。

$$
\text{access cycle} = \text{access time} + \text{recovery time}
$$

因此<strong>存取周期略大于存取时间</strong>（DRAM 读出是破坏性的，恢复时间不可省）。

<strong>（4）存储器带宽</strong>：单位时间内存储器能传输的信息量，单位 bit/s 或 B/s，与一次传输的数据位宽和存取时间有关：位宽越大、存取时间越短，带宽越高。

<figure class="cpd-figure not-content" id="hco-w3-fig-2">
  <svg viewBox="0 0 700 248" role="img" aria-label="存储器存取时间Ta与存取周期Tc及时序恢复时间对比图">
    <defs>
      <marker id="hco-w3-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="664" height="118" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="144" width="664" height="88" rx="6" class="fig-box-subtle"/>
    <line x1="48" y1="84" x2="546" y2="84" class="fig-line" marker-end="url(#hco-w3-arr-2)"/>
    <line x1="76" y1="44" x2="76" y2="118" class="fig-line-dashed"/>
    <line x1="212" y1="44" x2="212" y2="96" class="fig-line-dashed"/>
    <line x1="306" y1="44" x2="306" y2="118" class="fig-line-dashed"/>
    <line x1="442" y1="44" x2="442" y2="96" class="fig-line-dashed"/>
    <line x1="536" y1="44" x2="536" y2="118" class="fig-line-dashed"/>
    <line x1="78" y1="110" x2="304" y2="110" class="fig-line-accent" marker-start="url(#hco-w3-arr-2)" marker-end="url(#hco-w3-arr-2)"/>
    <line x1="308" y1="110" x2="534" y2="110" class="fig-line-accent" marker-start="url(#hco-w3-arr-2)" marker-end="url(#hco-w3-arr-2)"/>
    <rect x="76" y="50" width="136" height="30" rx="4" class="fig-box-blue"/>
    <rect x="212" y="50" width="94" height="30" rx="4" class="fig-box-orange"/>
    <rect x="306" y="50" width="136" height="30" rx="4" class="fig-box-blue"/>
    <rect x="442" y="50" width="94" height="30" rx="4" class="fig-box-orange"/>
    <rect x="148" y="100" width="88" height="19" rx="3" class="fig-box"/>
    <rect x="378" y="100" width="88" height="19" rx="3" class="fig-box"/>
    <rect x="556" y="34" width="114" height="84" rx="5" class="fig-box-red"/>
    <rect x="32" y="156" width="308" height="64" rx="5" class="fig-box-green"/>
    <rect x="354" y="156" width="316" height="64" rx="5" class="fig-box-purple"/>
    <text x="34" y="36" font-size="12" class="fig-title">DRAM 连续访存时序：存取周期 Tc = 存取时间 Ta + 恢复时间 Trec</text>
    <text x="144" y="69" text-anchor="middle" font-size="11" class="fig-blue">存取时间 Ta（读出）</text>
    <text x="259" y="69" text-anchor="middle" font-size="11" class="fig-orange">恢复 Trec</text>
    <text x="374" y="69" text-anchor="middle" font-size="11" class="fig-blue">第2次存取时间 Ta</text>
    <text x="489" y="69" text-anchor="middle" font-size="11" class="fig-orange">恢复 Trec</text>
    <text x="192" y="113" text-anchor="middle" font-size="10.5" class="fig-accent">存取周期 Tc</text>
    <text x="422" y="113" text-anchor="middle" font-size="10.5" class="fig-accent">存取周期 Tc</text>
    <text x="546" y="98" text-anchor="end" font-size="10" class="fig-muted">t</text>
    <text x="613" y="55" text-anchor="middle" font-size="11" class="fig-red">破坏性读出恢复</text>
    <text x="613" y="74" text-anchor="middle" font-size="10" class="fig-label">DRAM 读后电容</text>
    <text x="613" y="90" text-anchor="middle" font-size="10" class="fig-label">电荷泄放，Trec 内</text>
    <text x="613" y="106" text-anchor="middle" font-size="10" class="fig-red">禁启下一次读写</text>
    <text x="186" y="179" text-anchor="middle" font-size="11" class="fig-green">SRAM 非破坏性读出：Trec 极短（Tc ≈ Ta）</text>
    <text x="186" y="202" text-anchor="middle" font-size="10.5" class="fig-label">单次操作耗时算 Ta；连续访问最小间隔算 Tc</text>
    <text x="512" y="179" text-anchor="middle" font-size="11" class="fig-purple">存储器最大带宽（极限传输率）计算</text>
    <text x="512" y="202" text-anchor="middle" font-size="10.5" class="fig-label">Bandwidth = 单次传输位宽 W / 存取周期 Tc</text>
  </svg>
  <figcaption>图 4.2：存取时间 Ta 与存取周期 Tc 时序关系（DRAM 破坏性读出重写恢复与带宽公式）</figcaption>
</figure>

> <strong>★考点</strong>：注意区分“存取时间”与“存取周期”，前者是单次操作耗时，后者是两次操作的最小间隔，做题时若问“连续读写”必须用存取周期。

### 4.3 主存的基本结构与读写过程

主存是机器指令直接操作的存储器，按地址随机访问。基本组成：

- <strong>存储体</strong>：由若干<strong>存储单元</strong>组成，每个单元由若干<strong>存储元件</strong>组成，每个存储元件存 1 位；一个单元存放一个<strong>存储字</strong>，其位数为<strong>存储字长</strong>；
- <strong>MDR</strong>：存放从某单元读出的信息或准备写入的信息，位数 = 存储字长；
- <strong>MAR</strong>：存放欲访问单元的地址，位数决定单元个数 $2^n$；
- <strong>地址译码器</strong>：把 MAR 的 $n$ 位地址译成 $2^n$ 条选通线，只有一条有效，从而选中唯一单元。

例：8 个单元 × 8 位，则 MAR 3 位、MDR 8 位，用 3-8 译码器，输出高电平有效者选中对应单元。

<strong>读过程</strong>：CPU 经地址总线送地址到 MAR → 译码选中单元 → 在该单元读出信息送入 MDR → 经数据总线送入 CPU。
<strong>写过程</strong>：CPU 经数据总线送数据到 MDR → 经地址总线送地址到 MAR → 译码选中单元 → 将 MDR 内容写入该单元。

<figure class="cpd-figure not-content" id="hco-w3-fig-3">
  <svg viewBox="0 0 700 330" role="img" aria-label="主存基本结构MAR与MDR地址译码器及存储体读写流程图">
    <defs>
      <marker id="hco-w3-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="14" width="160" height="182" rx="6" class="fig-box-subtle"/>
    <rect x="386" y="14" width="296" height="182" rx="6" class="fig-box-subtle"/>
    <line x1="164" y1="68" x2="214" y2="68" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-3)"/>
    <line x1="340" y1="53" x2="432" y2="53" class="fig-line"/>
    <line x1="340" y1="71" x2="432" y2="71" class="fig-line"/>
    <line x1="340" y1="89" x2="432" y2="89" class="fig-line-accent" marker-end="url(#hco-w3-arr-3)"/>
    <line x1="340" y1="107" x2="432" y2="107" class="fig-line"/>
    <line x1="340" y1="125" x2="432" y2="125" class="fig-line"/>
    <line x1="340" y1="143" x2="432" y2="143" class="fig-line"/>
    <line x1="340" y1="161" x2="432" y2="161" class="fig-line"/>
    <line x1="340" y1="179" x2="432" y2="179" class="fig-line"/>
    <path d="M 166 118 L 190 118 L 190 245 L 548 245 L 548 199" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#hco-w3-arr-3)" marker-end="url(#hco-w3-arr-3)"/>
    <path d="M 164 164 L 200 164 L 200 214 L 214 214" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w3-arr-3)"/>
    <line x1="340" y1="214" x2="442" y2="214" class="fig-line-dashed" stroke="var(--sl-color-purple)"/>
    <line x1="442" y1="214" x2="442" y2="199" class="fig-line-dashed" stroke="var(--sl-color-purple)" marker-end="url(#hco-w3-arr-3)"/>
    <rect x="34" y="52" width="130" height="32" rx="4" class="fig-box-blue"/>
    <rect x="34" y="102" width="130" height="32" rx="4" class="fig-box-green"/>
    <rect x="34" y="148" width="130" height="32" rx="4" class="fig-box-purple"/>
    <rect x="216" y="42" width="124" height="146" rx="5" class="fig-box-accent"/>
    <rect x="216" y="198" width="124" height="32" rx="5" class="fig-box-purple"/>
    <rect x="434" y="45" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="434" y="63" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="434" y="81" width="230" height="16" rx="2" class="fig-box-accent"/>
    <rect x="434" y="99" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="434" y="117" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="434" y="135" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="434" y="153" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="434" y="171" width="230" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="45" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="63" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="81" width="30" height="16" rx="2" class="fig-box"/>
    <rect x="398" y="99" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="117" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="135" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="153" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="398" y="171" width="30" height="15" rx="2" class="fig-box"/>
    <rect x="348" y="236" width="166" height="18" rx="3" class="fig-box"/>
    <rect x="18" y="262" width="324" height="56" rx="5" class="fig-box-blue"/>
    <rect x="356" y="262" width="326" height="56" rx="5" class="fig-box-green"/>
    <text x="98" y="34" text-anchor="middle" font-size="11" class="fig-title">CPU（现代集成 MAR/MDR）</text>
    <text x="99" y="72" text-anchor="middle" font-size="11" class="fig-blue">MAR（n=3 位地址）</text>
    <text x="99" y="122" text-anchor="middle" font-size="11" class="fig-green">MDR（m=8 位字长）</text>
    <text x="99" y="168" text-anchor="middle" font-size="11" class="fig-purple">读写控制 R / W</text>
    <text x="190" y="60" text-anchor="middle" font-size="9.5" class="fig-blue">① 送地址</text>
    <text x="278" y="110" text-anchor="middle" font-size="11" class="fig-accent">地址译码器</text>
    <text x="278" y="128" text-anchor="middle" font-size="9.5" class="fig-muted">3-8 译码 → 2³ 线</text>
    <text x="278" y="218" text-anchor="middle" font-size="10.5" class="fig-purple">控制电路 / 驱动器</text>
    <text x="534" y="34" text-anchor="middle" font-size="11.5" class="fig-title">存储体（2³=8 个存储单元 × 8 位存储字）</text>
    <text x="413" y="56" text-anchor="middle" font-size="9.5" class="fig-muted">000</text>
    <text x="413" y="74" text-anchor="middle" font-size="9.5" class="fig-muted">001</text>
    <text x="413" y="92" text-anchor="middle" font-size="9.5" class="fig-accent">010</text>
    <text x="413" y="110" text-anchor="middle" font-size="9.5" class="fig-muted">011</text>
    <text x="413" y="128" text-anchor="middle" font-size="9.5" class="fig-muted">100</text>
    <text x="413" y="146" text-anchor="middle" font-size="9.5" class="fig-muted">101</text>
    <text x="413" y="164" text-anchor="middle" font-size="9.5" class="fig-muted">110</text>
    <text x="413" y="182" text-anchor="middle" font-size="9.5" class="fig-muted">111</text>
    <text x="549" y="93" text-anchor="middle" font-size="9.5" class="fig-accent">② 唯一选通单元 [1 0 1 1 0 0 1 0]</text>
    <text x="431" y="249" text-anchor="middle" font-size="10" class="fig-green">③ 双向数据通路（8 位并行）</text>
    <text x="180" y="283" text-anchor="middle" font-size="11" class="fig-blue">读操作流程：地址 → MAR → 译码选中单元</text>
    <text x="180" y="303" text-anchor="middle" font-size="10.5" class="fig-label">→ 读出存储字送入 MDR → 经数据总线送 CPU</text>
    <text x="519" y="283" text-anchor="middle" font-size="11" class="fig-green">写操作流程：数据 → MDR 且 地址 → MAR</text>
    <text x="519" y="303" text-anchor="middle" font-size="10.5" class="fig-label">→ 译码选中单元 + 写命令有效 → MDR 写入单元</text>
  </svg>
  <figcaption>图 4.3：主存基本组成（MAR、MDR、地址译码器、存储体）与读写数据通路</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w3-fig-4">
  <svg viewBox="0 0 700 264" role="img" aria-label="主存一维单译码与二维双译码重合法选通线数量对比图">
    <defs>
      <marker id="hco-w3-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="316" height="232" rx="6" class="fig-box-subtle"/>
    <rect x="352" y="16" width="330" height="232" rx="6" class="fig-box-subtle"/>
    <line x1="82" y1="112" x2="112" y2="112" class="fig-line" marker-end="url(#hco-w3-arr-4)"/>
    <line x1="184" y1="74" x2="218" y2="74" class="fig-line"/>
    <line x1="184" y1="98" x2="218" y2="98" class="fig-line-accent" marker-end="url(#hco-w3-arr-4)"/>
    <line x1="184" y1="122" x2="218" y2="122" class="fig-line"/>
    <line x1="184" y1="152" x2="218" y2="152" class="fig-line"/>
    <line x1="422" y1="102" x2="450" y2="102" class="fig-line" marker-end="url(#hco-w3-arr-4)"/>
    <line x1="568" y1="158" x2="568" y2="144" class="fig-line" marker-end="url(#hco-w3-arr-4)"/>
    <line x1="514" y1="78" x2="646" y2="78" class="fig-line"/>
    <line x1="514" y1="102" x2="646" y2="102" class="fig-line-accent"/>
    <line x1="514" y1="126" x2="646" y2="126" class="fig-line"/>
    <line x1="554" y1="62" x2="554" y2="144" class="fig-line"/>
    <line x1="588" y1="62" x2="588" y2="144" class="fig-line-accent"/>
    <line x1="622" y1="62" x2="622" y2="144" class="fig-line"/>
    <rect x="30" y="92" width="52" height="40" rx="4" class="fig-box-blue"/>
    <rect x="114" y="60" width="70" height="104" rx="5" class="fig-box-orange"/>
    <rect x="220" y="60" width="98" height="104" rx="5" class="fig-box"/>
    <rect x="226" y="90" width="86" height="16" rx="2" class="fig-box-accent"/>
    <rect x="30" y="182" width="290" height="54" rx="5" class="fig-box-red"/>
    <rect x="366" y="82" width="56" height="40" rx="4" class="fig-box-blue"/>
    <rect x="452" y="62" width="62" height="82" rx="5" class="fig-box-green"/>
    <rect x="536" y="62" width="110" height="82" rx="5" class="fig-box-dashed"/>
    <circle cx="588" cy="102" r="7" class="fig-box-accent"/>
    <rect x="480" y="158" width="176" height="24" rx="4" class="fig-box-green"/>
    <rect x="366" y="192" width="302" height="44" rx="5" class="fig-box-green"/>
    <text x="176" y="38" text-anchor="middle" font-size="12" class="fig-title">（a）一维单译码结构（字选法）</text>
    <text x="56" y="109" text-anchor="middle" font-size="10" class="fig-blue">地址</text>
    <text x="56" y="123" text-anchor="middle" font-size="10" class="fig-blue">n=12位</text>
    <text x="149" y="106" text-anchor="middle" font-size="10.5" class="fig-orange">单译码器</text>
    <text x="149" y="124" text-anchor="middle" font-size="10" class="fig-muted">12→4096</text>
    <text x="269" y="78" text-anchor="middle" font-size="10" class="fig-muted">字线 W0</text>
    <text x="269" y="102" text-anchor="middle" font-size="10" class="fig-accent">选中 Wi</text>
    <text x="269" y="132" text-anchor="middle" font-size="10" class="fig-muted">……</text>
    <text x="269" y="154" text-anchor="middle" font-size="10" class="fig-muted">W4095</text>
    <text x="175" y="203" text-anchor="middle" font-size="11" class="fig-red">选通线总数 = 2ⁿ = 2¹² = 4096 条</text>
    <text x="175" y="223" text-anchor="middle" font-size="10" class="fig-label">容量较大时译码线爆炸，仅适用于小容量 ROM/RAM</text>
    <text x="517" y="38" text-anchor="middle" font-size="12" class="fig-title">（b）二维双译码结构（X-Y 重合法）</text>
    <text x="394" y="99" text-anchor="middle" font-size="10" class="fig-blue">行地址</text>
    <text x="394" y="113" text-anchor="middle" font-size="10" class="fig-blue">6 位</text>
    <text x="483" y="99" text-anchor="middle" font-size="10.5" class="fig-green">X 行译码</text>
    <text x="483" y="116" text-anchor="middle" font-size="10" class="fig-muted">2⁶ = 64</text>
    <text x="568" y="174" text-anchor="middle" font-size="9.5" class="fig-green">Y 列译码 (2⁶=64) ← 6位列址</text>
    <rect x="608" y="85" width="36" height="14" rx="2" class="fig-box"/>
    <text x="626" y="96" text-anchor="middle" font-size="9.5" class="fig-accent">X∩Y</text>
    <text x="517" y="210" text-anchor="middle" font-size="11" class="fig-green">选通线总数 = 2⁶ + 2⁶ = 64 + 64 = 128 条！</text>
    <text x="517" y="227" text-anchor="middle" font-size="10" class="fig-label">仅 X 行与 Y 列同时有效交叉点被选中（线数骤降 32 倍）</text>
  </svg>
  <figcaption>图 4.4：4K×1 位芯片一维单译码（4096 条线）与二维双译码重合法（128 条线）对比</figcaption>
</figure>

> <strong>注意</strong>：存储器本身只负责“按地址存取”，地址比较、命中判断、替换等由 Cache/MMU 等机制完成；MAR 位数决定的是<strong>单元数</strong>而非字节数（编址单位不同则容量换算不同）。

### 4.4 主存中数据的存放：编址、大小端与边界对齐

<strong>（1）编址单位与存储字长</strong>：主存一般<strong>按字节编址</strong>；存储字长是字节的 $2^k$ 倍（1、2、4、8 字节），分别称字节、半字、字、双字。存储字长为 4 字节时，一个存储单元含 4 个字节，字节地址的低 2 位用于选择单元内哪个字节；半字访问用低 1 位选择。

字节地址、半字地址、字地址的关系：字节地址<strong>逻辑右移 1 位得半字地址，右移 2 位</strong>得字地址。

<strong>（2）大端与小端</strong>：多字节数据的字节顺序。

| 方式 | 规则 | 典型架构 |
|---|---|---|
| 小端 | 低字节存低地址，高字节存高地址 | x86、RISC-V（常） |
| 大端 | 高字节存低地址，低字节存高地址 | PowerPC 等 |

两种方式无绝对优劣：小端使“地址高低”与“数据位权高低”一致，便于多精度运算；大端符合人的阅读习惯，便于网络传输与调试。ARM、MIPS 可同时支持两种。

<strong>（3）边界对齐</strong>：多字节变量若跨存储单元存放，访问需要多个存取周期；编译器按边界对齐分配，用空间换时间。

| 数据类型 | 字节数 | 起始地址要求 |
|---|---|---|
| char | 1 | 任意 |
| short | 2 | 最低 1 位为 0（2 的倍数） |
| int / float | 4 | 最低 2 位为 00（4 的倍数） |
| double / long long | 8 | 最低 3 位为 000（8 的倍数） |

例：`struct { int a; short b; double c; char d; short e; }`，未对齐时 a:0–3、b:4–5、c:6–13、d:14、e:15–16，c 跨 3 个单元、e 跨 2 个单元；对齐后 a:0–3、b:4–5、c:8–15、d:16、e:18–19，c 只跨 2 个单元、e 只占 1 个单元，但浪费了 6、7、17 三个字节。

<figure class="cpd-figure not-content" id="hco-w3-fig-5">
  <svg viewBox="0 0 700 318" role="img" aria-label="主存大小端字节序对比与结构体边界对齐存储布局图">
    <rect x="18" y="16" width="324" height="86" rx="6" class="fig-box-subtle"/>
    <rect x="356" y="16" width="326" height="86" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="112" width="664" height="92" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="212" width="664" height="92" rx="6" class="fig-box-subtle"/>
    <line x1="48" y1="136" x2="48" y2="180" class="fig-line-dashed"/>
    <line x1="152" y1="136" x2="152" y2="180" class="fig-line-dashed"/>
    <line x1="256" y1="136" x2="256" y2="180" class="fig-line-dashed"/>
    <line x1="360" y1="136" x2="360" y2="180" class="fig-line-dashed"/>
    <line x1="464" y1="136" x2="464" y2="180" class="fig-line-dashed"/>
    <line x1="568" y1="136" x2="568" y2="180" class="fig-line-dashed"/>
    <line x1="48" y1="236" x2="48" y2="280" class="fig-line-dashed"/>
    <line x1="152" y1="236" x2="152" y2="280" class="fig-line-dashed"/>
    <line x1="256" y1="236" x2="256" y2="280" class="fig-line-dashed"/>
    <line x1="360" y1="236" x2="360" y2="280" class="fig-line-dashed"/>
    <line x1="464" y1="236" x2="464" y2="280" class="fig-line-dashed"/>
    <line x1="568" y1="236" x2="568" y2="280" class="fig-line-dashed"/>
    <rect x="42" y="44" width="66" height="30" rx="3" class="fig-box-blue"/>
    <rect x="110" y="44" width="66" height="30" rx="3" class="fig-box"/>
    <rect x="178" y="44" width="66" height="30" rx="3" class="fig-box"/>
    <rect x="246" y="44" width="66" height="30" rx="3" class="fig-box-purple"/>
    <rect x="382" y="44" width="66" height="30" rx="3" class="fig-box-purple"/>
    <rect x="450" y="44" width="66" height="30" rx="3" class="fig-box"/>
    <rect x="518" y="44" width="66" height="30" rx="3" class="fig-box"/>
    <rect x="586" y="44" width="66" height="30" rx="3" class="fig-box-blue"/>
    <rect x="48" y="146" width="104" height="30" rx="3" class="fig-box-blue"/>
    <rect x="152" y="146" width="52" height="30" rx="3" class="fig-box-green"/>
    <rect x="204" y="146" width="208" height="30" rx="3" class="fig-box-red"/>
    <rect x="412" y="146" width="26" height="30" rx="3" class="fig-box-purple"/>
    <rect x="438" y="146" width="52" height="30" rx="3" class="fig-box-red"/>
    <rect x="576" y="138" width="94" height="52" rx="4" class="fig-box-red"/>
    <rect x="48" y="246" width="104" height="30" rx="3" class="fig-box-blue"/>
    <rect x="152" y="246" width="52" height="30" rx="3" class="fig-box-green"/>
    <rect x="204" y="246" width="52" height="30" rx="3" class="fig-box-dashed"/>
    <rect x="256" y="246" width="208" height="30" rx="3" class="fig-box-green"/>
    <rect x="464" y="246" width="26" height="30" rx="3" class="fig-box-purple"/>
    <rect x="490" y="246" width="26" height="30" rx="3" class="fig-box-dashed"/>
    <rect x="516" y="246" width="52" height="30" rx="3" class="fig-box-green"/>
    <rect x="576" y="238" width="94" height="52" rx="4" class="fig-box-green"/>
    <text x="180" y="35" text-anchor="middle" font-size="11.5" class="fig-title">小端（Little-Endian）：0x12345678</text>
    <text x="75" y="63" text-anchor="middle" font-size="11" class="fig-blue">78 (LSB)</text>
    <text x="143" y="63" text-anchor="middle" font-size="11" class="fig-label">56</text>
    <text x="211" y="63" text-anchor="middle" font-size="11" class="fig-label">34</text>
    <text x="279" y="63" text-anchor="middle" font-size="11" class="fig-purple">12 (MSB)</text>
    <text x="75" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1000</text>
    <text x="143" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1001</text>
    <text x="211" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1002</text>
    <text x="279" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1003</text>
    <text x="519" y="35" text-anchor="middle" font-size="11.5" class="fig-title">大端（Big-Endian）：0x12345678</text>
    <text x="415" y="63" text-anchor="middle" font-size="11" class="fig-purple">12 (MSB)</text>
    <text x="483" y="63" text-anchor="middle" font-size="11" class="fig-label">34</text>
    <text x="551" y="63" text-anchor="middle" font-size="11" class="fig-label">56</text>
    <text x="619" y="63" text-anchor="middle" font-size="11" class="fig-blue">78 (LSB)</text>
    <text x="415" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1000</text>
    <text x="483" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1001</text>
    <text x="551" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1002</text>
    <text x="619" y="90" text-anchor="middle" font-size="9.5" class="fig-muted">0x1003</text>
    <text x="34" y="131" font-size="11" class="fig-red">未对齐紧缩存放（0–16 共 17B，虚线为 32 位存储字边界）：c 跨 3 个存储字，e 跨 2 个存储字</text>
    <text x="100" y="165" text-anchor="middle" font-size="10.5" class="fig-blue">a (int 4B)</text>
    <text x="178" y="165" text-anchor="middle" font-size="10" class="fig-green">b(2B)</text>
    <text x="308" y="165" text-anchor="middle" font-size="10.5" class="fig-red">c (double 8B: 6–13 跨界)</text>
    <text x="425" y="165" text-anchor="middle" font-size="10" class="fig-purple">d</text>
    <text x="464" y="165" text-anchor="middle" font-size="9.5" class="fig-red">e(2B)</text>
    <text x="48" y="194" text-anchor="middle" font-size="9.5" class="fig-muted">0</text>
    <text x="152" y="194" text-anchor="middle" font-size="9.5" class="fig-muted">4</text>
    <text x="256" y="194" text-anchor="middle" font-size="9.5" class="fig-muted">8</text>
    <text x="360" y="194" text-anchor="middle" font-size="9.5" class="fig-muted">12</text>
    <text x="464" y="194" text-anchor="middle" font-size="9.5" class="fig-muted">16</text>
    <text x="568" y="194" text-anchor="middle" font-size="9.5" class="fig-muted">20</text>
    <text x="623" y="159" text-anchor="middle" font-size="10" class="fig-red">读 c 需 3 次</text>
    <text x="623" y="177" text-anchor="middle" font-size="10" class="fig-red">读 e 需 2 次</text>
    <text x="34" y="231" font-size="11" class="fig-green">按边界对齐存放（填充 6–7 与 17，起始地址为自身大小整数倍）：用 3B 空间换取最少访存周期</text>
    <text x="100" y="265" text-anchor="middle" font-size="10.5" class="fig-blue">a (0–3)</text>
    <text x="178" y="265" text-anchor="middle" font-size="10" class="fig-green">b(4-5)</text>
    <text x="230" y="265" text-anchor="middle" font-size="9.5" class="fig-muted">空2B</text>
    <text x="360" y="265" text-anchor="middle" font-size="10.5" class="fig-green">c (double 8B: 8–15 对齐)</text>
    <text x="477" y="265" text-anchor="middle" font-size="10" class="fig-purple">d</text>
    <text x="503" y="265" text-anchor="middle" font-size="9" class="fig-muted">空</text>
    <text x="542" y="265" text-anchor="middle" font-size="9.5" class="fig-green">e(2B)</text>
    <text x="48" y="294" text-anchor="middle" font-size="9.5" class="fig-muted">0</text>
    <text x="152" y="294" text-anchor="middle" font-size="9.5" class="fig-muted">4</text>
    <text x="256" y="294" text-anchor="middle" font-size="9.5" class="fig-muted">8</text>
    <text x="360" y="294" text-anchor="middle" font-size="9.5" class="fig-muted">12</text>
    <text x="464" y="294" text-anchor="middle" font-size="9.5" class="fig-muted">16</text>
    <text x="568" y="294" text-anchor="middle" font-size="9.5" class="fig-muted">20</text>
    <text x="623" y="259" text-anchor="middle" font-size="10" class="fig-green">读 c 仅 2 次</text>
    <text x="623" y="277" text-anchor="middle" font-size="10" class="fig-green">读 e 仅 1 次</text>
  </svg>
  <figcaption>图 4.5：大小端字节序对比与 struct 边界对齐前后跨字访存次数对比（64 位对齐下总大小还会补齐为 8 的倍数即 24B）</figcaption>
</figure>

> <strong>★考点</strong>：`0x12345678` 在小端机器上按字节从低地址到高地址依次为 `78 56 34 12`；做大题时先确定“数据低字节在低地址”还是“高字节在低地址”。

### 4.5 SRAM 与 DRAM 的存储元

<strong>SRAM（静态随机存取存储器）</strong>：存储元由 6 个 MOS 管构成。$T_1$、$T_2$ 为工作管（交叉耦合构成双稳态触发器），$T_3$、$T_4$ 为负载管（上拉电阻），$T_5$、$T_6$ 为门控（存取）管，由行选通线 $X$ 控制，把 $A$、$B$ 两点与位线接通；列选通线 $Y$ 在单元外选通位线对，与 6T 单元本身无关。

- 上电后 $A$、$B$ 两点电平随机（$A$ 高 $B$ 低表示 1，反之表示 0），只要不断电就保持；
- <strong>读操作不破坏</strong>原数据；
- 不需要刷新，速度快；
- 但 6 管结构<strong>功耗大、占用晶圆面积大、成本高、集成度低</strong>，适合做 Cache。

<strong>DRAM（动态随机存取存储器）</strong>：存储元由 1 个 MOS 管 + 1 个电容构成，用电容电荷表示 0/1。

- 读出是<strong>破坏性</strong>的：读出后必须立刻写回恢复；
- 电容会漏电，必须周期性<strong>刷新</strong>；
- 结构简单、集成度高、成本低、容量大，适合做主存；
- 存取速度低于 SRAM。

<figure class="cpd-figure not-content" id="hco-w3-fig-6">
  <svg viewBox="0 0 700 324" role="img" aria-label="6管SRAM双稳态触发器存储元与1T1C存储电容DRAM存储元电路对比图">
    <rect x="18" y="16" width="332" height="292" rx="6" class="fig-box-subtle"/>
    <rect x="364" y="16" width="318" height="292" rx="6" class="fig-box-subtle"/>
    <line x1="36" y1="54" x2="332" y2="54" class="fig-line-accent"/>
    <line x1="75" y1="54" x2="75" y2="116" class="fig-line-accent"/>
    <line x1="293" y1="54" x2="293" y2="116" class="fig-line-accent"/>
    <line x1="48" y1="64" x2="48" y2="200" class="fig-line"/>
    <line x1="320" y1="64" x2="320" y2="200" class="fig-line"/>
    <line x1="100" y1="68" x2="268" y2="68" class="fig-line"/>
    <line x1="131" y1="68" x2="131" y2="196" class="fig-line"/>
    <line x1="237" y1="68" x2="237" y2="196" class="fig-line"/>
    <line x1="100" y1="196" x2="268" y2="196" class="fig-line"/>
    <line x1="48" y1="128" x2="131" y2="128" class="fig-line"/>
    <line x1="237" y1="128" x2="320" y2="128" class="fig-line"/>
    <polyline points="131,114 196,114 196,88 220,88" class="fig-line" stroke="var(--sl-color-blue)"/>
    <polyline points="196,114 196,162 220,162" class="fig-line" stroke="var(--sl-color-blue)"/>
    <polyline points="237,140 172,140 172,88 148,88" class="fig-line" stroke="var(--sl-color-green)"/>
    <polyline points="172,140 172,162 148,162" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="386" y1="84" x2="650" y2="84" class="fig-line-accent"/>
    <line x1="422" y1="56" x2="422" y2="180" class="fig-line"/>
    <line x1="482" y1="84" x2="482" y2="106" class="fig-line-accent"/>
    <line x1="422" y1="118" x2="554" y2="118" class="fig-line"/>
    <line x1="554" y1="118" x2="554" y2="146" class="fig-line"/>
    <line x1="534" y1="146" x2="574" y2="146" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="534" y1="156" x2="574" y2="156" class="fig-line" stroke="var(--sl-color-green)"/>
    <line x1="554" y1="156" x2="554" y2="182" class="fig-line"/>
    <line x1="538" y1="182" x2="570" y2="182" class="fig-line"/>
    <line x1="544" y1="188" x2="564" y2="188" class="fig-line"/>
    <rect x="114" y="76" width="34" height="24" rx="3" class="fig-box"/>
    <rect x="220" y="76" width="34" height="24" rx="3" class="fig-box"/>
    <rect x="114" y="150" width="34" height="24" rx="3" class="fig-box-blue"/>
    <rect x="220" y="150" width="34" height="24" rx="3" class="fig-box-blue"/>
    <rect x="60" y="116" width="30" height="24" rx="3" class="fig-box-purple"/>
    <rect x="278" y="116" width="30" height="24" rx="3" class="fig-box-purple"/>
    <circle cx="131" cy="128" r="3.5" class="fig-dot"/>
    <circle cx="237" cy="128" r="3.5" class="fig-dot"/>
    <rect x="30" y="228" width="308" height="66" rx="5" class="fig-box-blue"/>
    <rect x="462" y="106" width="40" height="24" rx="3" class="fig-box-purple"/>
    <rect x="386" y="180" width="110" height="30" rx="4" class="fig-box-orange"/>
    <rect x="378" y="228" width="290" height="66" rx="5" class="fig-box-green"/>
    <text x="184" y="33" text-anchor="middle" font-size="12" class="fig-title">SRAM 存储元（6 个 MOS 管双稳态触发器）</text>
    <text x="184" y="50" text-anchor="middle" font-size="9.5" class="fig-accent">字线 WL（行选通 X）</text>
    <text x="48" y="214" text-anchor="middle" font-size="9.5" class="fig-muted">BL</text>
    <text x="320" y="214" text-anchor="middle" font-size="9.5" class="fig-muted">~BL</text>
    <text x="272" y="71" text-anchor="start" font-size="9" class="fig-muted">VCC</text>
    <text x="184" y="210" text-anchor="middle" font-size="9.5" class="fig-muted">GND</text>
    <text x="131" y="92" text-anchor="middle" font-size="10" class="fig-label">T3</text>
    <text x="237" y="92" text-anchor="middle" font-size="10" class="fig-label">T4</text>
    <text x="131" y="166" text-anchor="middle" font-size="10" class="fig-blue">T1</text>
    <text x="237" y="166" text-anchor="middle" font-size="10" class="fig-blue">T2</text>
    <text x="75" y="132" text-anchor="middle" font-size="9.5" class="fig-purple">T5</text>
    <text x="293" y="132" text-anchor="middle" font-size="9.5" class="fig-purple">T6</text>
    <text x="118" y="124" text-anchor="end" font-size="10.5" class="fig-blue">A</text>
    <text x="250" y="124" font-size="10.5" class="fig-green">B</text>
    <text x="184" y="250" text-anchor="middle" font-size="10.5" class="fig-blue">交叉耦合双稳态锁存 · 非破坏性读出 · 无需刷新</text>
    <text x="184" y="270" text-anchor="middle" font-size="10" class="fig-label">速度快但 6 管面积大、功耗高、成本高 → 作 Cache</text>
    <text x="184" y="286" text-anchor="middle" font-size="9.5" class="fig-muted">注：不断电即保持，但断电仍丢失（属易失性存储器）</text>
    <text x="523" y="38" text-anchor="middle" font-size="12" class="fig-title">DRAM 存储元（1T1C 存储电容）</text>
    <text x="580" y="77" text-anchor="middle" font-size="9.5" class="fig-accent">字线 WL（行选通）</text>
    <text x="422" y="51" text-anchor="middle" font-size="9.5" class="fig-muted">位线 BL</text>
    <text x="482" y="122" text-anchor="middle" font-size="10" class="fig-purple">T</text>
    <text x="582" y="155" font-size="10.5" class="fig-green">电容 Cs 存电荷</text>
    <text x="582" y="187" font-size="9.5" class="fig-muted">GND</text>
    <text x="441" y="199" text-anchor="middle" font-size="10" class="fig-orange">灵敏读出恢复放大器</text>
    <text x="523" y="250" text-anchor="middle" font-size="10.5" class="fig-green">电荷共享破坏性读出（读后立即重写恢复）</text>
    <text x="523" y="270" text-anchor="middle" font-size="10" class="fig-label">电容漏电需按行周期性刷新 · 集成度高 → 作主存</text>
    <text x="523" y="286" text-anchor="middle" font-size="9.5" class="fig-muted">整行打开时同行所有单元均需经灵敏放大器重写</text>
  </svg>
  <figcaption>图 4.6：SRAM（6T 双稳态触发器）与 DRAM（1T1C 存储电容 + 灵敏读放）存储元原理对比</figcaption>
</figure>

> <strong>★考点</strong>：SRAM 用触发器存信息，DRAM 用电容存信息；“静态”指<strong>不需刷新</strong>，不是“永不失电”——两者断电都丢数据，均属易失性存储器。

### 4.6 DRAM 的动态刷新

电容漏电使数据只能保持较短时间。从数据存入到即将丢失的时间为<strong>最大刷新周期</strong>（常见 2 ms、4 ms、8 ms）；实际两次完整刷新的间隔为<strong>刷新周期</strong>，必须 ≤ 最大刷新周期。

要点：

- 刷新<strong>按行进行</strong>（读操作会破坏整行，刷新同样以行为单位恢复），因此只需<strong>行地址</strong>、不需列地址；
- 刷新由内存控制器负责（现代集成在 CPU 内）；
- 缩短刷新周期可减少行数、增加列数（这也是 DRAM 行列不对称的原因）；
- 刷新期间不能响应 CPU 访问，存在<strong>访存争用</strong>。

例：128 行 × 128 列，存取周期 0.5 μs，刷新周期 2 ms。2 ms = 2000 μs，可划分为 4000 个 0.5 μs 时隙，需在其中安排 128 个时隙各刷新一行。

| 方式 | 安排 | 死区 | 特点 |
|---|---|---|---|
| 集中刷新 | 在 2 ms 末尾连续用 128 个时隙刷新全部行 | 128 × 0.5 = <strong>64 μs</strong> | 平时访问不受影响、速度快，但存在长死区 |
| 分散刷新 | 每个存取周期后都刷新一行（存取周期变为 1 μs） | 无 | 无死区，但 2 ms 内刷新 2000 次 ≈ 完整刷新 15 遍，系统速度严重下降 |
| 异步刷新 | 把 2 ms 分成 128 段，每段 15.625 μs，段末 0.5 μs 刷新一行 | 0.5 μs | 兼顾两者，效率最高、最常用 |

<figure class="cpd-figure not-content" id="hco-w3-fig-7">
  <svg viewBox="0 0 700 322" role="img" aria-label="DRAM三种动态刷新方式集中刷新分散刷新异步刷新时序对比图">
    <rect x="18" y="16" width="664" height="82" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="106" width="664" height="90" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="204" width="664" height="102" rx="6" class="fig-box-subtle"/>
    <rect x="34" y="46" width="442" height="32" rx="4" class="fig-box-blue"/>
    <rect x="480" y="46" width="188" height="32" rx="4" class="fig-box-red"/>
    <rect x="34" y="140" width="54" height="30" rx="3" class="fig-box-blue"/>
    <rect x="90" y="140" width="54" height="30" rx="3" class="fig-box-orange"/>
    <rect x="146" y="140" width="54" height="30" rx="3" class="fig-box-blue"/>
    <rect x="202" y="140" width="54" height="30" rx="3" class="fig-box-orange"/>
    <rect x="258" y="140" width="54" height="30" rx="3" class="fig-box-blue"/>
    <rect x="314" y="140" width="54" height="30" rx="3" class="fig-box-orange"/>
    <rect x="370" y="140" width="50" height="30" rx="3" class="fig-box-blue"/>
    <rect x="422" y="140" width="50" height="30" rx="3" class="fig-box-orange"/>
    <rect x="482" y="134" width="186" height="48" rx="4" class="fig-box-orange"/>
    <rect x="34" y="234" width="84" height="30" rx="3" class="fig-box-green"/>
    <rect x="120" y="234" width="22" height="30" rx="3" class="fig-box-red"/>
    <rect x="146" y="234" width="84" height="30" rx="3" class="fig-box-green"/>
    <rect x="232" y="234" width="22" height="30" rx="3" class="fig-box-red"/>
    <rect x="258" y="234" width="84" height="30" rx="3" class="fig-box-green"/>
    <rect x="344" y="234" width="22" height="30" rx="3" class="fig-box-red"/>
    <rect x="370" y="234" width="78" height="30" rx="3" class="fig-box-green"/>
    <rect x="450" y="234" width="22" height="30" rx="3" class="fig-box-red"/>
    <rect x="482" y="226" width="186" height="66" rx="4" class="fig-box-green"/>
    <text x="34" y="36" font-size="11.5" class="fig-title">① 集中刷新（2 ms = 4000 个 0.5 μs 时隙：前 3872 时隙读写，末尾 128 时隙集中刷完 128 行）</text>
    <text x="255" y="66" text-anchor="middle" font-size="11" class="fig-blue">正常读写或保持（1936 μs，高速无停顿）</text>
    <text x="574" y="66" text-anchor="middle" font-size="11" class="fig-red">死区 128×0.5 = 64 μs</text>
    <text x="34" y="126" font-size="11.5" class="fig-title">② 分散刷新（系统存取周期拉长为 tC = tM(0.5 μs) + tR(0.5 μs) = 1 μs，每读写一次刷新一行）</text>
    <text x="61" y="159" text-anchor="middle" font-size="10" class="fig-blue">读写tM</text>
    <text x="117" y="159" text-anchor="middle" font-size="10" class="fig-orange">刷新tR</text>
    <text x="173" y="159" text-anchor="middle" font-size="10" class="fig-blue">读写tM</text>
    <text x="229" y="159" text-anchor="middle" font-size="10" class="fig-orange">刷新tR</text>
    <text x="285" y="159" text-anchor="middle" font-size="10" class="fig-blue">读写tM</text>
    <text x="341" y="159" text-anchor="middle" font-size="10" class="fig-orange">刷新tR</text>
    <text x="395" y="159" text-anchor="middle" font-size="10" class="fig-blue">tM</text>
    <text x="447" y="159" text-anchor="middle" font-size="10" class="fig-orange">tR</text>
    <text x="575" y="153" text-anchor="middle" font-size="10.5" class="fig-orange">无死区，但 2ms 刷 2000 次</text>
    <text x="575" y="172" text-anchor="middle" font-size="10" class="fig-label">重复刷新 15.6 遍，整机变慢</text>
    <text x="34" y="222" font-size="11.5" class="fig-title">③ 异步刷新（2 ms / 128 行 = 每隔 15.625 μs 刷新 1 行，兼顾效率与响应）</text>
    <text x="76" y="253" text-anchor="middle" font-size="10" class="fig-green">15.125μs</text>
    <text x="131" y="253" text-anchor="middle" font-size="9.5" class="fig-red">R</text>
    <text x="188" y="253" text-anchor="middle" font-size="10" class="fig-green">15.125μs</text>
    <text x="243" y="253" text-anchor="middle" font-size="9.5" class="fig-red">R</text>
    <text x="300" y="253" text-anchor="middle" font-size="10" class="fig-green">15.125μs</text>
    <text x="355" y="253" text-anchor="middle" font-size="9.5" class="fig-red">R</text>
    <text x="409" y="253" text-anchor="middle" font-size="10" class="fig-green">……</text>
    <text x="461" y="253" text-anchor="middle" font-size="9.5" class="fig-red">R</text>
    <text x="575" y="246" text-anchor="middle" font-size="10.5" class="fig-green">死区缩至单次 0.5 μs</text>
    <text x="575" y="264" text-anchor="middle" font-size="10" class="fig-label">若藏于 CPU 译码阶段刷新</text>
    <text x="575" y="281" text-anchor="middle" font-size="10" class="fig-green">则等效消除死区（透明刷新）</text>
    <text x="252" y="288" text-anchor="middle" font-size="10" class="fig-muted">注：刷新只提供行地址（刷新计数器生成），对 CPU 程序完全透明</text>
  </svg>
  <figcaption>图 4.7：128×128 DRAM 三种动态刷新策略（集中式、分散式、异步式）时隙分配与死区对比</figcaption>
</figure>

### 4.7 DRAM 芯片、地址复用与 SDRAM/DDR

早期典型 DRAM 芯片（如 Intel 2116）：存储体 128 × 128，容量 <strong>16K × 1 位</strong>；行、列选通线各 128 条，行/列地址译码器各需 <strong>7 条</strong>地址输入。

- 每列带<strong>灵敏读出恢复放大器</strong>（读出微弱信号并立即恢复）；
- 采用<strong>地址复用</strong>：行地址线与列地址线分时共用一组引脚，7 位地址分两次锁存，引脚数由 14 降为 7；由 $\overline{RAS}$、$\overline{CAS}$ 先后锁存到行/列地址锁存器；
- 行地址线兼作刷新地址，配合行地址计数器逐行刷新；
- 数据输入/输出分开且可锁存；$\overline{RAS}$、$\overline{CAS}$、$\overline{WE}$ 均低电平有效，`EN` 高电平有效；
- 无独立片选 $\overline{CS}$，常由 $\overline{RAS}$ 与 $\overline{CAS}$ 代替。

<strong>SDRAM</strong>：读写与系统时钟同步，自带时钟；支持<strong>突发传输</strong>（第一个数据就绪后每周期输出一个），可用模式寄存器配置突发长度（1/2/4/8/整行）；相对异步 DRAM（FPM/EDO/BEDO）减少了等待周期。

<strong>DDR SDRAM</strong>：双倍数据率，在时钟<strong>上升沿和下降沿各传一次</strong>；内部采用多路预取（DDR/DDR2/DDR3/DDR4/DDR5 分别为 2n/4n/8n/8n（+Bank Group）/16n 预取）。

以 DDR4-3200 为例：等效传输频率 3200 MT/s，数据位宽 8 B（64 位）：

$$
\text{Bandwidth} = 3200 \times 10^6 \times 8\ B/s = 25.6\ GB/s
$$

数据总线时钟频率为等效速率的一半（双沿）即 1600 MHz；DDR4 采用 8n 预取，数据总线时钟是 DRAM 核心频率的 4 倍（等效速率为核心频率的 8 倍），故核心工作频率为 400 MHz。

<figure class="cpd-figure not-content" id="hco-w3-fig-8">
  <svg viewBox="0 0 700 312" role="img" aria-label="16Kx1位DRAM芯片行列地址复用结构与DDR4预取带宽换算图">
    <defs>
      <marker id="hco-w3-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="330" height="280" rx="6" class="fig-box-subtle"/>
    <rect x="362" y="16" width="320" height="280" rx="6" class="fig-box-subtle"/>
    <polyline points="100,76 108,76 108,62 124,62" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-8)"/>
    <polyline points="108,76 108,92 228,92" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-8)"/>
    <line x1="94" y1="120" x2="124" y2="68" class="fig-line-dashed"/>
    <polyline points="94,152 114,152 114,108 228,108 228,104" fill="none" class="fig-line-dashed"/>
    <line x1="94" y1="229" x2="124" y2="229" class="fig-line-dashed" marker-end="url(#hco-w3-arr-8)"/>
    <line x1="174" y1="82" x2="174" y2="112" class="fig-line" marker-end="url(#hco-w3-arr-8)"/>
    <line x1="280" y1="104" x2="280" y2="112" class="fig-line" marker-end="url(#hco-w3-arr-8)"/>
    <line x1="227" y1="150" x2="227" y2="166" class="fig-line" marker-end="url(#hco-w3-arr-8)"/>
    <line x1="227" y1="198" x2="227" y2="214" class="fig-line" marker-end="url(#hco-w3-arr-8)"/>
    <polyline points="424,58 462,58 468,74 656,74" class="fig-line" stroke="var(--sl-color-purple)"/>
    <polyline points="424,92 544,92 550,108 656,108" class="fig-line" stroke="var(--sl-color-orange)"/>
    <line x1="465" y1="54" x2="465" y2="122" class="fig-line-dashed"/>
    <line x1="547" y1="88" x2="547" y2="122" class="fig-line-dashed"/>
    <rect x="24" y="60" width="76" height="32" rx="4" class="fig-box-blue"/>
    <rect x="28" y="106" width="66" height="26" rx="4" class="fig-box-purple"/>
    <rect x="28" y="138" width="66" height="26" rx="4" class="fig-box-orange"/>
    <rect x="28" y="214" width="66" height="30" rx="4" class="fig-box"/>
    <rect x="126" y="48" width="96" height="34" rx="4" class="fig-box-purple"/>
    <rect x="230" y="70" width="102" height="34" rx="4" class="fig-box-orange"/>
    <rect x="126" y="114" width="206" height="36" rx="4" class="fig-box-green"/>
    <rect x="126" y="168" width="206" height="30" rx="4" class="fig-box-accent"/>
    <rect x="126" y="216" width="206" height="28" rx="4" class="fig-box"/>
    <rect x="28" y="254" width="306" height="30" rx="4" class="fig-box-blue"/>
    <rect x="454" y="122" width="76" height="22" rx="3" class="fig-box-purple"/>
    <rect x="538" y="122" width="82" height="22" rx="3" class="fig-box-orange"/>
    <rect x="376" y="160" width="292" height="124" rx="5" class="fig-box-green"/>
    <rect x="386" y="190" width="84" height="36" rx="4" class="fig-box"/>
    <rect x="480" y="190" width="86" height="36" rx="4" class="fig-box"/>
    <rect x="576" y="190" width="82" height="36" rx="4" class="fig-box-accent"/>
    <text x="183" y="36" text-anchor="middle" font-size="12" class="fig-title">16K × 1 位 DRAM 地址复用结构</text>
    <text x="62" y="80" text-anchor="middle" font-size="9.5" class="fig-blue">A0–A6(7b)</text>
    <text x="61" y="123" text-anchor="middle" font-size="10" class="fig-purple">~RAS 行选</text>
    <text x="61" y="155" text-anchor="middle" font-size="10" class="fig-orange">~CAS 列选</text>
    <text x="61" y="233" text-anchor="middle" font-size="10" class="fig-label">~WE 控制</text>
    <text x="174" y="69" text-anchor="middle" font-size="10" class="fig-purple">行地址锁存/译码</text>
    <text x="281" y="91" text-anchor="middle" font-size="10" class="fig-orange">列地址锁存/译码</text>
    <text x="229" y="136" text-anchor="middle" font-size="10.5" class="fig-green">128 行 × 128 列 存储矩阵</text>
    <text x="229" y="187" text-anchor="middle" font-size="10" class="fig-accent">128 路灵敏读出/重写恢复放大器</text>
    <text x="229" y="234" text-anchor="middle" font-size="10" class="fig-label">数据锁存器 / 三态门 (Din / Dout)</text>
    <text x="181" y="273" text-anchor="middle" font-size="10" class="fig-blue">14位地址分两次送 → 地址引脚 14→7（减半）</text>
    <text x="522" y="36" text-anchor="middle" font-size="12" class="fig-title">行列选通时序 &amp; DDR4 带宽换算</text>
    <text x="414" y="68" text-anchor="end" font-size="10" class="fig-purple">~RAS</text>
    <text x="414" y="102" text-anchor="end" font-size="10" class="fig-orange">~CAS</text>
    <text x="414" y="137" text-anchor="end" font-size="10" class="fig-blue">A0–A6</text>
    <text x="492" y="137" text-anchor="middle" font-size="9.5" class="fig-purple">锁存7位行址</text>
    <text x="579" y="137" text-anchor="middle" font-size="9.5" class="fig-orange">锁存7位列址</text>
    <text x="522" y="179" text-anchor="middle" font-size="11" class="fig-green">DDR4-3200（8n 预取 + 上下双沿传输）</text>
    <text x="428" y="205" text-anchor="middle" font-size="9.5" class="fig-label">核心频率</text>
    <text x="428" y="220" text-anchor="middle" font-size="10" class="fig-title">400 MHz</text>
    <text x="523" y="205" text-anchor="middle" font-size="9.5" class="fig-label">I/O 总线时钟</text>
    <text x="523" y="220" text-anchor="middle" font-size="10" class="fig-title">1600 MHz</text>
    <text x="617" y="205" text-anchor="middle" font-size="9.5" class="fig-accent">等效速率</text>
    <text x="617" y="220" text-anchor="middle" font-size="10" class="fig-accent">3200 MT/s</text>
    <text x="522" y="250" text-anchor="middle" font-size="10.5" class="fig-label">单通道带宽 = 3200M × 64 bit(8B) = 25.6 GB/s</text>
    <text x="522" y="270" text-anchor="middle" font-size="9.5" class="fig-muted">双通道并行位宽 128 bit → 峰值带宽翻倍至 51.2 GB/s</text>
  </svg>
  <figcaption>图 4.8：DRAM 芯片行列地址分时复用（~RAS/~CAS）时序与 DDR4-3200 多路预取带宽换算</figcaption>
</figure>

> <strong>注意</strong>：DRAM 地址复用减少的是<strong>引脚数</strong>，不是地址位数；芯片容量仍由 $2^{\text{rows}} \times 2^{\text{cols}}$ 决定。选择题常考“2K×1 芯片需 11 条地址线，复用后引脚为 6 条（6 行 + 5 列或 5 行 + 6 列）”，并倾向<strong>减少行数</strong>以降低刷新开销。

### 4.8 主存的位扩展与单体多字

<strong>位扩展</strong>：芯片数据位宽小于 CPU 数据总线位宽时使用。所有芯片的<strong>地址引脚并联</strong>接 CPU 地址总线，<strong>$\overline{WE}$ 并联</strong>接 CPU 的读写控制，<strong>$\overline{CS}$ 并联</strong>接 CPU 的 $\overline{MREQ}$，各芯片的<strong>数据引脚分别接数据总线的不同位</strong>。

例：用 32 片 256K×1 芯片构成 256K×32 的主存：每片 18 位地址（$2^{18}=256K$），32 片数据线各接数据总线一位；片选同时有效，一次访问同时读写 32 位。

<strong>字扩展：芯片容量不足、需要增加存储单元数（地址空间）时使用，用高位地址经译码器产生各芯片的片选；字位同时扩展</strong>则两者结合。位扩展又称数据总线扩展（字长扩展）。

<strong>单体多字存储器</strong>：多个存储模块<strong>共享地址总线</strong>，同一地址并行访问各模块的同一单元，一个存取周期读出多个存储字。$N$ 个模块可将带宽提高 $N$ 倍。PC 的<strong>多通道内存</strong>即此技术：双通道“联动模式”要求两根内存条容量、频率、时序完全一致（同一地址并行访问）；“非联动模式”中内存控制器有两套独立端口，两模块可并发但命令无需同步，只要求频率相同，灵活性更高、控制更复杂。

<figure class="cpd-figure not-content" id="hco-w3-fig-9">
  <svg viewBox="0 0 700 286" role="img" aria-label="主存位扩展32片256Kx1组成256Kx32连线图与单体多字双通道原理图">
    <defs>
      <marker id="hco-w3-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="664" height="202" rx="6" class="fig-box-subtle"/>
    <line x1="158" y1="68" x2="296" y2="68" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-9)"/>
    <line x1="158" y1="138" x2="296" y2="138" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w3-arr-9)"/>
    <line x1="484" y1="45" x2="560" y2="45" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#hco-w3-arr-9)" marker-end="url(#hco-w3-arr-9)"/>
    <line x1="484" y1="79" x2="560" y2="79" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#hco-w3-arr-9)" marker-end="url(#hco-w3-arr-9)"/>
    <line x1="484" y1="143" x2="560" y2="143" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#hco-w3-arr-9)" marker-end="url(#hco-w3-arr-9)"/>
    <line x1="484" y1="177" x2="560" y2="177" class="fig-line" stroke="var(--sl-color-green)" marker-start="url(#hco-w3-arr-9)" marker-end="url(#hco-w3-arr-9)"/>
    <line x1="560" y1="34" x2="560" y2="192" class="fig-line" stroke="var(--sl-color-green)"/>
    <rect x="34" y="42" width="124" height="132" rx="5" class="fig-box-blue"/>
    <rect x="298" y="32" width="186" height="26" rx="3" class="fig-box"/>
    <rect x="298" y="66" width="186" height="26" rx="3" class="fig-box"/>
    <rect x="298" y="130" width="186" height="26" rx="3" class="fig-box"/>
    <rect x="298" y="164" width="186" height="26" rx="3" class="fig-box"/>
    <rect x="574" y="68" width="94" height="86" rx="5" class="fig-box-green"/>
    <rect x="18" y="228" width="664" height="42" rx="5" class="fig-box-accent"/>
    <text x="96" y="68" text-anchor="middle" font-size="12" class="fig-blue">CPU 访存接口</text>
    <text x="96" y="94" text-anchor="middle" font-size="10" class="fig-label">地址 A0–A17 (18位)</text>
    <text x="96" y="114" text-anchor="middle" font-size="10" class="fig-muted">控制 ~MREQ + ~WE</text>
    <text x="96" y="136" text-anchor="middle" font-size="10" class="fig-green">数据 D0–D31 (32位)</text>
    <text x="96" y="156" text-anchor="middle" font-size="9.5" class="fig-muted">目标：256K × 32 位</text>
    <text x="227" y="59" text-anchor="middle" font-size="10" class="fig-blue">A0–A17 全部并联</text>
    <text x="227" y="129" text-anchor="middle" font-size="10" class="fig-purple">~CS、~WE 全部并联</text>
    <text x="227" y="155" text-anchor="middle" font-size="9.5" class="fig-muted">（一次同时选中 32 片）</text>
    <text x="391" y="49" text-anchor="middle" font-size="10.5" class="fig-label">芯片 #0（256K × 1 位）</text>
    <text x="391" y="83" text-anchor="middle" font-size="10.5" class="fig-label">芯片 #1（256K × 1 位）</text>
    <text x="391" y="114" text-anchor="middle" font-size="11" class="fig-muted">…… 共 32 片并联 ……</text>
    <text x="391" y="147" text-anchor="middle" font-size="10.5" class="fig-label">芯片 #30（256K × 1 位）</text>
    <text x="391" y="181" text-anchor="middle" font-size="10.5" class="fig-label">芯片 #31（256K × 1 位）</text>
    <text x="522" y="40" text-anchor="middle" font-size="9.5" class="fig-green">接 D0</text>
    <text x="522" y="74" text-anchor="middle" font-size="9.5" class="fig-green">接 D1</text>
    <text x="522" y="138" text-anchor="middle" font-size="9.5" class="fig-green">接 D30</text>
    <text x="522" y="172" text-anchor="middle" font-size="9.5" class="fig-green">接 D31</text>
    <text x="621" y="96" text-anchor="middle" font-size="11" class="fig-green">数据总线</text>
    <text x="621" y="115" text-anchor="middle" font-size="10.5" class="fig-label">D31..D0</text>
    <text x="621" y="134" text-anchor="middle" font-size="9.5" class="fig-muted">数据线各接</text>
    <text x="621" y="147" text-anchor="middle" font-size="9.5" class="fig-muted">不同位！</text>
    <text x="350" y="253" text-anchor="middle" font-size="10" class="fig-accent">单体多字（双通道原理）：N 个存储体共享同一地址并行读出 N 个字 → 峰值带宽提升 N 倍（适合顺序连续访问）</text>
  </svg>
  <figcaption>图 4.9：位扩展连接规则（地址线、片选线、读写线全并联，数据线分别接不同位）与单体多字并行访存</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w3-fig-10">
  <svg viewBox="0 0 700 310" role="img" aria-label="字位同时扩展8片16Kx4芯片构成64Kx8存储器译码片选与地址空间分配图">
    <defs>
      <marker id="hco-w3-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="664" height="278" rx="6" class="fig-box-subtle"/>
    <line x1="150" y1="68" x2="294" y2="68" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-10)"/>
    <line x1="150" y1="142" x2="178" y2="142" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w3-arr-10)"/>
    <line x1="150" y1="184" x2="178" y2="184" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w3-arr-10)"/>
    <line x1="268" y1="112" x2="294" y2="112" class="fig-line-accent" marker-end="url(#hco-w3-arr-10)"/>
    <line x1="268" y1="152" x2="294" y2="152" class="fig-line" marker-end="url(#hco-w3-arr-10)"/>
    <line x1="268" y1="192" x2="294" y2="192" class="fig-line" marker-end="url(#hco-w3-arr-10)"/>
    <line x1="268" y1="232" x2="294" y2="232" class="fig-line" marker-end="url(#hco-w3-arr-10)"/>
    <rect x="32" y="48" width="118" height="40" rx="4" class="fig-box-blue"/>
    <rect x="32" y="122" width="118" height="40" rx="4" class="fig-box-purple"/>
    <rect x="32" y="170" width="118" height="28" rx="4" class="fig-box-orange"/>
    <rect x="32" y="210" width="118" height="66" rx="4" class="fig-box-green"/>
    <rect x="180" y="102" width="88" height="144" rx="5" class="fig-box-purple"/>
    <rect x="296" y="48" width="368" height="36" rx="4" class="fig-box-blue"/>
    <rect x="296" y="96" width="110" height="32" rx="3" class="fig-box-accent"/>
    <rect x="412" y="96" width="110" height="32" rx="3" class="fig-box-accent"/>
    <rect x="530" y="96" width="134" height="32" rx="3" class="fig-box-green"/>
    <rect x="296" y="136" width="110" height="32" rx="3" class="fig-box"/>
    <rect x="412" y="136" width="110" height="32" rx="3" class="fig-box"/>
    <rect x="530" y="136" width="134" height="32" rx="3" class="fig-box"/>
    <rect x="296" y="176" width="110" height="32" rx="3" class="fig-box"/>
    <rect x="412" y="176" width="110" height="32" rx="3" class="fig-box"/>
    <rect x="530" y="176" width="134" height="32" rx="3" class="fig-box"/>
    <rect x="296" y="216" width="110" height="32" rx="3" class="fig-box"/>
    <rect x="412" y="216" width="110" height="32" rx="3" class="fig-box"/>
    <rect x="530" y="216" width="134" height="32" rx="3" class="fig-box"/>
    <text x="350" y="36" text-anchor="middle" font-size="12" class="fig-title">字位同时扩展实例：用 8 片 16K×4 位芯片构成 64K×8 位主存（共分 4 组 × 每组 2 片）</text>
    <text x="91" y="65" text-anchor="middle" font-size="10.5" class="fig-blue">低位 A0–A13</text>
    <text x="91" y="80" text-anchor="middle" font-size="9.5" class="fig-muted">14条片内地址</text>
    <text x="91" y="139" text-anchor="middle" font-size="10.5" class="fig-purple">高位 A15, A14</text>
    <text x="91" y="154" text-anchor="middle" font-size="9.5" class="fig-muted">2条字扩展选组</text>
    <text x="91" y="188" text-anchor="middle" font-size="10" class="fig-orange">~MREQ 访存有效</text>
    <text x="91" y="230" text-anchor="middle" font-size="10.5" class="fig-green">芯片数计算公式</text>
    <text x="91" y="248" text-anchor="middle" font-size="9.5" class="fig-label">4 组 × 2 片/组</text>
    <text x="91" y="264" text-anchor="middle" font-size="8.8" class="fig-label">总片数 = 4 × 2 = 8 片</text>
    <text x="224" y="156" text-anchor="middle" font-size="11" class="fig-purple">2-4 译码器</text>
    <text x="224" y="176" text-anchor="middle" font-size="9.5" class="fig-muted">(74LS139)</text>
    <text x="224" y="196" text-anchor="middle" font-size="9.5" class="fig-muted">输出 ~CS0..3</text>
    <text x="224" y="214" text-anchor="middle" font-size="9.5" class="fig-muted">仅 1 组有效</text>
    <text x="480" y="70" text-anchor="middle" font-size="10.5" class="fig-blue">A0–A13 与 ~WE 并联接全部 8 片芯片；同组内 2 片共用同一 ~CSi</text>
    <text x="351" y="116" text-anchor="middle" font-size="10" class="fig-accent">组0片A → D3..D0</text>
    <text x="467" y="116" text-anchor="middle" font-size="10" class="fig-accent">组0片B → D7..D4</text>
    <text x="597" y="116" text-anchor="middle" font-size="10" class="fig-green">00: 0000H–3FFFH</text>
    <text x="351" y="156" text-anchor="middle" font-size="10" class="fig-label">组1片A → D3..D0</text>
    <text x="467" y="156" text-anchor="middle" font-size="10" class="fig-label">组1片B → D7..D4</text>
    <text x="597" y="156" text-anchor="middle" font-size="10" class="fig-label">01: 4000H–7FFFH</text>
    <text x="351" y="196" text-anchor="middle" font-size="10" class="fig-label">组2片A → D3..D0</text>
    <text x="467" y="196" text-anchor="middle" font-size="10" class="fig-label">组2片B → D7..D4</text>
    <text x="597" y="196" text-anchor="middle" font-size="10" class="fig-label">10: 8000H–BFFFH</text>
    <text x="351" y="236" text-anchor="middle" font-size="10" class="fig-label">组3片A → D3..D0</text>
    <text x="467" y="236" text-anchor="middle" font-size="10" class="fig-label">组3片B → D7..D4</text>
    <text x="597" y="236" text-anchor="middle" font-size="10" class="fig-label">11: C000H–FFFFH</text>
    <text x="480" y="274" text-anchor="middle" font-size="9.2" class="fig-muted">408 必考：低位选片内单元，高位经译码器作 ~CS，~MREQ 接使能端防误选</text>
  </svg>
  <figcaption>图 4.10：字位同时扩展（8 片 16K×4 构成 64K×8）的高位译码片选（~CS）与十六进制地址空间映射</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w3-fig-11">
  <svg viewBox="0 0 700 304" role="img" aria-label="高位交叉与低位交叉四体并行存储器地址划分及流水线时序图">
    <rect x="18" y="16" width="322" height="84" rx="6" class="fig-box-subtle"/>
    <rect x="354" y="16" width="328" height="84" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="110" width="664" height="178" rx="6" class="fig-box-subtle"/>
    <line x1="88" y1="138" x2="88" y2="262" class="fig-line-dashed"/>
    <line x1="130" y1="138" x2="130" y2="262" class="fig-line-dashed"/>
    <line x1="172" y1="138" x2="172" y2="262" class="fig-line-dashed"/>
    <line x1="214" y1="138" x2="214" y2="262" class="fig-line-dashed"/>
    <line x1="256" y1="138" x2="256" y2="262" class="fig-line-dashed"/>
    <line x1="298" y1="138" x2="298" y2="262" class="fig-line-dashed"/>
    <line x1="340" y1="138" x2="340" y2="262" class="fig-line-dashed"/>
    <line x1="382" y1="138" x2="382" y2="262" class="fig-line-dashed"/>
    <line x1="424" y1="138" x2="424" y2="262" class="fig-line-dashed"/>
    <rect x="34" y="44" width="96" height="26" rx="3" class="fig-box-orange"/>
    <rect x="132" y="44" width="192" height="26" rx="3" class="fig-box"/>
    <rect x="370" y="44" width="192" height="26" rx="3" class="fig-box"/>
    <rect x="564" y="44" width="102" height="26" rx="3" class="fig-box-green"/>
    <rect x="88" y="144" width="42" height="22" rx="3" class="fig-box-blue"/>
    <rect x="130" y="144" width="126" height="22" rx="3" class="fig-box-green"/>
    <rect x="256" y="144" width="42" height="22" rx="3" class="fig-box-blue"/>
    <rect x="298" y="144" width="126" height="22" rx="3" class="fig-box-green"/>
    <rect x="130" y="172" width="42" height="22" rx="3" class="fig-box-blue"/>
    <rect x="172" y="172" width="126" height="22" rx="3" class="fig-box-green"/>
    <rect x="172" y="200" width="42" height="22" rx="3" class="fig-box-blue"/>
    <rect x="214" y="200" width="126" height="22" rx="3" class="fig-box-green"/>
    <rect x="214" y="228" width="42" height="22" rx="3" class="fig-box-blue"/>
    <rect x="256" y="228" width="126" height="22" rx="3" class="fig-box-green"/>
    <rect x="440" y="136" width="228" height="138" rx="5" class="fig-box-accent"/>
    <text x="179" y="36" text-anchor="middle" font-size="11.5" class="fig-title">高位交叉编址（顺序存储 = 单纯字扩展）</text>
    <text x="82" y="61" text-anchor="middle" font-size="10" class="fig-orange">体号 (高位)</text>
    <text x="228" y="61" text-anchor="middle" font-size="10" class="fig-label">体内地址 (低位)</text>
    <text x="179" y="88" text-anchor="middle" font-size="9.5" class="fig-muted">连续地址落同一体，串行耗时 nT，仅扩容不提速</text>
    <text x="518" y="36" text-anchor="middle" font-size="11.5" class="fig-title">低位交叉编址（交叉存储 = 流水线并行）</text>
    <text x="466" y="61" text-anchor="middle" font-size="10" class="fig-label">体内地址 (高位 ⌊A/m⌋)</text>
    <text x="615" y="61" text-anchor="middle" font-size="10" class="fig-green">体号 (A mod m)</text>
    <text x="518" y="88" text-anchor="middle" font-size="9.5" class="fig-green">连续地址轮流落 M0→M1→M2→M3，支持重叠流水</text>
    <text x="230" y="131" text-anchor="middle" font-size="11.5" class="fig-title">四体低位交叉流水线（存取周期 T = 4τ，每隔 τ 启动下一体）</text>
    <text x="78" y="159" text-anchor="end" font-size="10.5" class="fig-label">M0 体</text>
    <text x="78" y="187" text-anchor="end" font-size="10.5" class="fig-label">M1 体</text>
    <text x="78" y="215" text-anchor="end" font-size="10.5" class="fig-label">M2 体</text>
    <text x="78" y="243" text-anchor="end" font-size="10.5" class="fig-label">M3 体</text>
    <text x="109" y="159" text-anchor="middle" font-size="9.5" class="fig-blue">字0</text>
    <text x="193" y="159" text-anchor="middle" font-size="9.5" class="fig-green">M0 读写恢复 (3τ)</text>
    <text x="277" y="159" text-anchor="middle" font-size="9.5" class="fig-blue">字4</text>
    <text x="361" y="159" text-anchor="middle" font-size="9.5" class="fig-green">M0 恢复</text>
    <text x="151" y="187" text-anchor="middle" font-size="9.5" class="fig-blue">字1</text>
    <text x="235" y="187" text-anchor="middle" font-size="9.5" class="fig-green">M1 读写恢复 (3τ)</text>
    <text x="193" y="215" text-anchor="middle" font-size="9.5" class="fig-blue">字2</text>
    <text x="277" y="215" text-anchor="middle" font-size="9.5" class="fig-green">M2 读写恢复 (3τ)</text>
    <text x="235" y="243" text-anchor="middle" font-size="9.5" class="fig-blue">字3</text>
    <text x="319" y="243" text-anchor="middle" font-size="9.5" class="fig-green">M3 读写恢复 (3τ)</text>
    <text x="88" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">0</text>
    <text x="130" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">τ</text>
    <text x="172" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">2τ</text>
    <text x="214" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">3τ</text>
    <text x="256" y="276" text-anchor="middle" font-size="9.5" class="fig-accent">4τ=T</text>
    <text x="298" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">5τ</text>
    <text x="340" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">6τ</text>
    <text x="382" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">7τ</text>
    <text x="424" y="276" text-anchor="middle" font-size="9.5" class="fig-muted">8τ</text>
    <text x="554" y="158" text-anchor="middle" font-size="11" class="fig-accent">流水无冲突条件：m ≥ T / τ</text>
    <text x="554" y="182" text-anchor="middle" font-size="10.5" class="fig-label">连续存取 n 个字总时间：</text>
    <text x="554" y="203" text-anchor="middle" font-size="11.5" class="fig-title">t = T + (n - 1)τ</text>
    <text x="554" y="226" text-anchor="middle" font-size="10" class="fig-muted">例：连续读 5 个字耗时 T + 4τ = 2T</text>
    <text x="554" y="246" text-anchor="middle" font-size="10" class="fig-red">转移指令跳回同体则触发访存冲突！</text>
  </svg>
  <figcaption>图 4.11：多体并行存储器高位交叉与低位交叉编址对比及四体交叉流水线时序（t = T + (n-1)τ）</figcaption>
</figure>

### 4.9 Cache 的读/写流程与写策略

Cache 位于 CPU 与主存之间，容量小、速度快，存放主存中<strong>经常或即将使用</strong>的数据块副本。CPU 访问时先查 Cache：命中则直接读写；缺失则从主存调入数据块（利用空间局部性）。

<strong>读流程</strong>：给出主存地址 → 用地址中的索引/标记快速查找 → 命中则读出；缺失则分配 Cache 行（必要时替换），从主存调入整个数据块，更新查找表（标记、有效位），再读出。

<strong>写流程</strong>（按“写命中”与“写缺失”、以及写策略组合）：

| 情形 | 策略 | 动作 | 特点 |
|---|---|---|---|
| 写命中 | 写回（write back） | 只写 Cache，置<strong>脏位</strong>；该行被淘汰时才写回主存 | 响应最快；Cache 满时淘汰需先写回脏数据，可能比不用 Cache 还慢 |
| 写命中 | 写穿（write through） | 同时写 Cache 与主存 | 无脏数据、一致性好，但每次写都访存，速度慢 |
| 写缺失 | 非写分配 | 直接写主存，不分配 Cache 行 | 实现简单，写操作不污染 Cache |
| 写缺失 | 写分配 | 先把数据块调入 Cache，再按写命中处理 | 利用空间局部性，常与写回配合 |

<figure class="cpd-figure not-content" id="hco-w3-fig-12">
  <svg viewBox="0 0 700 226" role="img" aria-label="Cache同时访问与先后分级访问两种组织方式的平均访问时间AMAT对比图">
    <defs>
      <marker id="hco-w3-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="326" height="194" rx="6" class="fig-box-subtle"/>
    <rect x="358" y="16" width="324" height="194" rx="6" class="fig-box-subtle"/>
    <polyline points="100,82 122,82 122,63 142,63" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-12)"/>
    <polyline points="122,82 122,107 142,107" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w3-arr-12)"/>
    <line x1="438" y1="84" x2="466" y2="84" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-12)"/>
    <line x1="554" y1="84" x2="582" y2="84" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-12)"/>
    <rect x="34" y="62" width="66" height="40" rx="4" class="fig-box-blue"/>
    <rect x="144" y="46" width="178" height="34" rx="4" class="fig-box-green"/>
    <rect x="144" y="90" width="178" height="34" rx="4" class="fig-box-orange"/>
    <rect x="34" y="138" width="294" height="58" rx="5" class="fig-box-green"/>
    <rect x="374" y="64" width="64" height="40" rx="4" class="fig-box-blue"/>
    <rect x="468" y="64" width="86" height="40" rx="4" class="fig-box-green"/>
    <rect x="584" y="64" width="84" height="40" rx="4" class="fig-box-red"/>
    <rect x="374" y="138" width="294" height="58" rx="5" class="fig-box-purple"/>
    <text x="181" y="36" text-anchor="middle" font-size="11.5" class="fig-title">（a）同时访问（并联组织：同时启动访存）</text>
    <text x="67" y="86" text-anchor="middle" font-size="11" class="fig-blue">CPU</text>
    <text x="233" y="67" text-anchor="middle" font-size="10.5" class="fig-green">Cache 耗时 tc（命中率 H）</text>
    <text x="233" y="111" text-anchor="middle" font-size="10.5" class="fig-orange">主存耗时 tm（命中即停）</text>
    <text x="181" y="160" text-anchor="middle" font-size="11" class="fig-green">ta = H · tc + (1 - H) · tm</text>
    <text x="181" y="182" text-anchor="middle" font-size="10" class="fig-label">访问效率 e = tc / ta</text>
    <text x="520" y="36" text-anchor="middle" font-size="11.5" class="fig-title">（b）先后访问（串联组织：缺失再启动主存）</text>
    <text x="406" y="88" text-anchor="middle" font-size="11" class="fig-blue">CPU</text>
    <text x="511" y="82" text-anchor="middle" font-size="10" class="fig-green">先查 Cache</text>
    <text x="511" y="96" text-anchor="middle" font-size="9.5" class="fig-muted">必耗 tc</text>
    <text x="568" y="76" text-anchor="middle" font-size="9" class="fig-red">1-H</text>
    <text x="626" y="82" text-anchor="middle" font-size="10" class="fig-red">再访主存</text>
    <text x="626" y="96" text-anchor="middle" font-size="9.5" class="fig-muted">再耗 tm</text>
    <text x="521" y="160" text-anchor="middle" font-size="11" class="fig-purple">ta = H·tc + (1-H)(tc+tm) = tc + (1-H)·tm</text>
    <text x="521" y="182" text-anchor="middle" font-size="10" class="fig-label">注：若未声明“同时启动”，默认按先后访问计算</text>
  </svg>
  <figcaption>图 4.12：Cache—主存系统的“同时访问”与“先后访问”平均访问时间（AMAT）模型对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w3-fig-13">
  <svg viewBox="0 0 700 320" role="img" aria-label="Cache读写流程与写回脏位及全写法写缓冲队列策略对比图">
    <defs>
      <marker id="hco-w3-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="318" height="288" rx="6" class="fig-box-subtle"/>
    <rect x="350" y="16" width="332" height="138" rx="6" class="fig-box-subtle"/>
    <rect x="350" y="164" width="332" height="140" rx="6" class="fig-box-subtle"/>
    <line x1="176" y1="74" x2="176" y2="96" class="fig-line" marker-end="url(#hco-w3-arr-13)"/>
    <line x1="176" y1="130" x2="176" y2="152" class="fig-line" marker-end="url(#hco-w3-arr-13)"/>
    <polyline points="116,170 68,170 68,214" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-13)"/>
    <polyline points="236,170 262,170 262,214" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-13)"/>
    <polyline points="186,250 116,250" class="fig-line-dashed" marker-end="url(#hco-w3-arr-13)"/>
    <line x1="454" y1="84" x2="484" y2="84" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-13)"/>
    <line x1="572" y1="84" x2="598" y2="84" class="fig-line-dashed" marker-end="url(#hco-w3-arr-13)"/>
    <line x1="454" y1="226" x2="484" y2="226" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-13)"/>
    <polyline points="454,238 468,238 468,266 484,266" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w3-arr-13)"/>
    <line x1="572" y1="266" x2="598" y2="266" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w3-arr-13)"/>
    <rect x="80" y="42" width="192" height="32" rx="4" class="fig-box-blue"/>
    <rect x="80" y="98" width="192" height="32" rx="4" class="fig-box"/>
    <polygon points="176,152 236,170 176,188 116,170" class="fig-box-accent"/>
    <rect x="30" y="216" width="84" height="68" rx="4" class="fig-box-green"/>
    <rect x="188" y="216" width="136" height="68" rx="4" class="fig-box-red"/>
    <rect x="364" y="64" width="90" height="40" rx="4" class="fig-box-blue"/>
    <rect x="486" y="64" width="86" height="40" rx="4" class="fig-box-purple"/>
    <rect x="600" y="64" width="68" height="40" rx="4" class="fig-box"/>
    <rect x="364" y="210" width="90" height="40" rx="4" class="fig-box-green"/>
    <rect x="486" y="208" width="86" height="32" rx="4" class="fig-box-green"/>
    <rect x="486" y="250" width="86" height="32" rx="4" class="fig-box-orange"/>
    <rect x="600" y="250" width="68" height="32" rx="4" class="fig-box"/>
    <text x="177" y="34" text-anchor="middle" font-size="11.5" class="fig-title">Cache 访存通用判定流程</text>
    <text x="176" y="62" text-anchor="middle" font-size="10.5" class="fig-blue">CPU 发出主存物理地址</text>
    <text x="176" y="118" text-anchor="middle" font-size="10.5" class="fig-label">索引选行/组 + 比较 Tag 且 V=1</text>
    <text x="176" y="174" text-anchor="middle" font-size="10.5" class="fig-accent">命中？</text>
    <text x="88" y="163" text-anchor="middle" font-size="9.5" class="fig-green">是</text>
    <text x="256" y="163" text-anchor="middle" font-size="9.5" class="fig-red">否(缺失)</text>
    <text x="72" y="236" text-anchor="middle" font-size="10" class="fig-green">命中处理</text>
    <text x="72" y="253" text-anchor="middle" font-size="9.5" class="fig-label">读：直返CPU</text>
    <text x="72" y="270" text-anchor="middle" font-size="9.5" class="fig-label">写：见右侧</text>
    <text x="256" y="235" text-anchor="middle" font-size="10" class="fig-red">读缺失 / 写分配：</text>
    <text x="256" y="252" text-anchor="middle" font-size="9.5" class="fig-label">淘汰旧行(脏则回写)</text>
    <text x="256" y="269" text-anchor="middle" font-size="9.5" class="fig-label">调入整块并置 V=1</text>
    <text x="516" y="36" text-anchor="middle" font-size="11.5" class="fig-title">组合一：写回法（Write-Back）+ 写分配法</text>
    <text x="409" y="81" text-anchor="middle" font-size="10" class="fig-blue">写命中只改</text>
    <text x="409" y="95" text-anchor="middle" font-size="10" class="fig-blue">Cache 行</text>
    <text x="529" y="81" text-anchor="middle" font-size="10" class="fig-purple">置脏位 D=1</text>
    <text x="529" y="95" text-anchor="middle" font-size="9.5" class="fig-muted">暂不写主存</text>
    <text x="634" y="81" text-anchor="middle" font-size="10" class="fig-label">被替换时</text>
    <text x="634" y="95" text-anchor="middle" font-size="9.5" class="fig-muted">写回主存</text>
    <text x="516" y="124" text-anchor="middle" font-size="9.5" class="fig-muted">特点：大幅减少访存带宽占用；每行需额外增加 1 bit 脏位（修改位）</text>
    <text x="516" y="141" text-anchor="middle" font-size="9.5" class="fig-blue">写缺失调块入 Cache 再改（利用空间局部性）</text>
    <text x="516" y="184" text-anchor="middle" font-size="11.5" class="fig-title">组合二：全写法/写穿（Write-Through）+ 非写分配</text>
    <text x="409" y="227" text-anchor="middle" font-size="10" class="fig-green">CPU 写命中</text>
    <text x="409" y="241" text-anchor="middle" font-size="9.5" class="fig-muted">双路同写</text>
    <text x="529" y="228" text-anchor="middle" font-size="10" class="fig-green">改 Cache 行</text>
    <text x="529" y="270" text-anchor="middle" font-size="10" class="fig-orange">FIFO 写缓冲</text>
    <text x="634" y="270" text-anchor="middle" font-size="10" class="fig-label">后台写主存</text>
    <text x="516" y="295" text-anchor="middle" font-size="8.8" class="fig-muted">无需脏位（D=0 bits）；加 SRAM 写缓冲队列防 CPU 停等（满时仍阻塞）</text>
  </svg>
  <figcaption>图 4.13：Cache 访存判定流程与“写回 + 写分配（需脏位）”vs“全写 + 写缓冲 + 非写分配”机制对比</figcaption>
</figure>

> <strong>★考点</strong>：写回 + 写分配、写穿 + 非写分配是常见组合；判断“是否产生脏数据”看是否只写了 Cache。

### 4.10 Cache 的地址映射：直接映射

主存被划分为若干<strong>数据块</strong>（块大小 $2^W$ 字节），Cache 被划分为若干<strong>行/槽</strong>，每行可存一个主存块的副本，并附<strong>有效位</strong>（1 位）与<strong>标记 tag</strong>。映射规则决定主存块能放到哪一行。

<strong>直接映射</strong>：每个主存块只能映射到 Cache 中一个<strong>固定行</strong>：

$$
\text{Cache line} = \text{main block} \bmod \text{Cache lines}
$$

等价于把主存按 Cache 大小分区：每区含与 Cache 行数相同的主存块，区内块号与行号一一对应。主存地址划分为：

| 字段 | 位数 | 作用 |
|---|---|---|
| 标记 tag（主存分区号） | $S-R$ | 与 Cache 行中 tag 比较，判断是否同一分区 |
| 行号 index | $R$ | 索引 Cache 行（行数 $2^R$） |
| 块内偏移 offset | $W$ | 块内定位（块大小 $2^W$ 字节） |

查找：用 index 选行 → 比较该行 tag 与地址 tag 且有效位为 1 → 命中则用 offset 取字。

Cache 总容量（每行含 1 位有效位 + $S-R$ 位 tag + 数据块）：

$$
C = 2^R \times \left(1 + (S-R) + 2^W \times 8\right)\ \text{bit}
$$

（若还有脏位、替换位，需另加。）

<strong>特点</strong>：硬件最简单（一个译码器 + 一个比较器），成本低，适合大容量 Cache；但<strong>冲突率高、命中率低</strong>——Cache 未满时也可能因同一行被多个分区共用而替换；命中则无需替换算法，直接替换即可。

<figure class="cpd-figure not-content" id="hco-w3-fig-14">
  <svg viewBox="0 0 700 326" role="img" aria-label="Cache直接映射地址字段划分硬件比较器电路与总容量位宽构成图">
    <defs>
      <marker id="hco-w3-arr-14" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="664" height="294" rx="6" class="fig-box-subtle"/>
    <line x1="260" y1="68" x2="260" y2="86" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-14)"/>
    <line x1="260" y1="116" x2="260" y2="144" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-14)"/>
    <polyline points="110,68 110,80 526,80 526,132" fill="none" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-14)"/>
    <polyline points="154,196 154,146 494,146" fill="none" class="fig-line-dashed" marker-end="url(#hco-w3-arr-14)"/>
    <polyline points="92,196 92,122 588,122" fill="none" class="fig-line-dashed" marker-end="url(#hco-w3-arr-14)"/>
    <line x1="556" y1="150" x2="588" y2="150" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-14)"/>
    <polyline points="395,68 395,92 458,92 458,196 474,196" fill="none" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-14)"/>
    <line x1="444" y1="204" x2="474" y2="204" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-14)"/>
    <rect x="38" y="38" width="144" height="30" rx="3" class="fig-box-red"/>
    <rect x="184" y="38" width="152" height="30" rx="3" class="fig-box-blue"/>
    <rect x="338" y="38" width="114" height="30" rx="3" class="fig-box-green"/>
    <rect x="196" y="88" width="128" height="28" rx="4" class="fig-box-blue"/>
    <rect x="496" y="134" width="60" height="34" rx="4" class="fig-box-red"/>
    <rect x="590" y="114" width="76" height="54" rx="4" class="fig-box-green"/>
    <rect x="476" y="184" width="96" height="38" rx="4" class="fig-box-green"/>
    <rect x="74" y="146" width="370" height="106" rx="4" class="fig-box"/>
    <rect x="74" y="188" width="370" height="22" rx="2" class="fig-box-accent"/>
    <line x1="74" y1="168" x2="444" y2="168" class="fig-divider"/>
    <line x1="74" y1="210" x2="444" y2="210" class="fig-divider"/>
    <line x1="74" y1="231" x2="444" y2="231" class="fig-divider"/>
    <line x1="110" y1="146" x2="110" y2="252" class="fig-divider"/>
    <line x1="196" y1="146" x2="196" y2="252" class="fig-divider"/>
    <rect x="44" y="264" width="622" height="34" rx="4" class="fig-box-purple"/>
    <text x="244" y="31" text-anchor="middle" font-size="11" class="fig-title">主存物理地址（共 S + W 位：Cache 行号 i = 主存块号 j mod 2^R）</text>
    <text x="110" y="57" text-anchor="middle" font-size="10" class="fig-red">标记 Tag (S-R 位)</text>
    <text x="260" y="57" text-anchor="middle" font-size="10" class="fig-blue">Cache 行号 Index (R位)</text>
    <text x="395" y="57" text-anchor="middle" font-size="10" class="fig-green">块内地址 (W 位)</text>
    <text x="260" y="106" text-anchor="middle" font-size="10" class="fig-blue">R → 2^R 行译码器</text>
    <text x="526" y="155" text-anchor="middle" font-size="10" class="fig-red">比较器 =</text>
    <text x="628" y="136" text-anchor="middle" font-size="10" class="fig-green">与门(V=1)</text>
    <text x="628" y="154" text-anchor="middle" font-size="10" class="fig-green">→ Hit 命中</text>
    <text x="524" y="201" text-anchor="middle" font-size="10" class="fig-green">MUX 选字</text>
    <text x="524" y="215" text-anchor="middle" font-size="9.5" class="fig-muted">→ 送 CPU</text>
    <text x="92" y="161" text-anchor="middle" font-size="9.5" class="fig-muted">V</text>
    <text x="153" y="161" text-anchor="middle" font-size="9.5" class="fig-muted">Tag (S-R)</text>
    <text x="320" y="161" text-anchor="middle" font-size="9.5" class="fig-muted">数据块 Data（2^W 字节 = 2^W × 8 位）</text>
    <text x="66" y="183" text-anchor="end" font-size="9.5" class="fig-muted">行 0</text>
    <text x="66" y="203" text-anchor="end" font-size="9.5" class="fig-accent">行 i</text>
    <text x="92" y="203" text-anchor="middle" font-size="9.5" class="fig-accent">1</text>
    <text x="153" y="203" text-anchor="middle" font-size="9.5" class="fig-accent">Tag_i</text>
    <text x="320" y="203" text-anchor="middle" font-size="9.5" class="fig-accent">选定行数据块副本</text>
    <text x="66" y="224" text-anchor="end" font-size="9.5" class="fig-muted">……</text>
    <text x="66" y="245" text-anchor="end" font-size="9.5" class="fig-muted">2^R-1</text>
    <text x="355" y="285" text-anchor="middle" font-size="10.5" class="fig-purple">Cache 总存储容量 = 2^R 行 × [ 1(有效位V) + 0或1(写回脏位D) + (S-R)(Tag位) + 2^W × 8(数据位) ] bit</text>
  </svg>
  <figcaption>图 4.14：直接映射 Cache 地址划分、单比较器命中逻辑与硬件总容量（控制位 + Tag + 数据块）计算公式</figcaption>
</figure>

> <strong>注意</strong>：直接映射的 tag 是“主存分区号”，不是完整块号；比较 tag 相等且有效位为 1 才算命中。

### 4.11 Cache 的组相联映射与三种映射对比

<strong>组相联</strong>：把 Cache 所有行分成固定大小的<strong>组</strong>，每组 $K$ 行（$K$ 路组相联）。主存块先按<strong>直接映射</strong>定位到唯一的组，再在该组内<strong>全相联</strong>地放到任意一行：

$$
\text{Cache set} = \text{main block} \bmod \text{Cache sets}
$$

地址划分为 tag（$S-G$）| 组索引 index（$G$）| 块内偏移 offset（$W$），组数 $2^G$。查找时用 index 选组，将该组 $K$ 行的 tag 与地址 tag <strong>并发比较</strong>（$K$ 个比较器），有效位为 1 且 tag 匹配则命中，再由 offset 选择组内被命中行的数据字。

参数影响：$K$ 越大，冲突概率越低，但并发比较器越多、硬件越复杂；$K$ 过大则接近全相联。早期 $K=2$ 或 4，现代处理器常用 8 路或 16 路。

<strong>三种映射对比</strong>：

| 映射 | 规则 | 地址划分 | 比较器数 | 命中率 | 成本 |
|---|---|---|---|---|---|
| 直接 | 块 → 唯一行 | tag / 行号 / 偏移 | 1 | 低（冲突多） | 最低 |
| 全相联 | 块 → 任意行 | tag / 偏移 | 全部行数 | 最高 | 最高（相联存储器） |
| 组相联 | 块 → 唯一组，组内任意行 | tag / 组号 / 偏移 | $K$（每组路数） | 接近全相联 | 折中 |

<strong>特例</strong>：整个 Cache 只分一组 → 全相联；每组只有一行（$K=1$）→ 直接映射。

<figure class="cpd-figure not-content" id="hco-w3-fig-15">
  <svg viewBox="0 0 700 326" role="img" aria-label="K路组相联Cache并发比较硬件通路与三种映射地址划分对比图">
    <defs>
      <marker id="hco-w3-arr-15" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="420" height="244" rx="6" class="fig-box-subtle"/>
    <rect x="450" y="16" width="232" height="244" rx="6" class="fig-box-subtle"/>
    <line x1="214" y1="66" x2="214" y2="86" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-15)"/>
    <polyline points="214,112 214,120 126,120 126,128" fill="none" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-15)"/>
    <polyline points="214,120 328,120 328,128" fill="none" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-15)"/>
    <polyline points="96,66 96,78 26,78 26,189 308,189 308,196" fill="none" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-15)"/>
    <line x1="134" y1="189" x2="134" y2="196" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-15)"/>
    <line x1="110" y1="180" x2="110" y2="196" class="fig-line-dashed" marker-end="url(#hco-w3-arr-15)"/>
    <line x1="284" y1="180" x2="284" y2="196" class="fig-line-dashed" marker-end="url(#hco-w3-arr-15)"/>
    <polyline points="122,224 122,238 192,238" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-15)"/>
    <polyline points="296,224 296,238 268,238" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-15)"/>
    <rect x="34" y="40" width="124" height="26" rx="3" class="fig-box-red"/>
    <rect x="160" y="40" width="108" height="26" rx="3" class="fig-box-blue"/>
    <rect x="270" y="40" width="96" height="26" rx="3" class="fig-box-green"/>
    <rect x="140" y="88" width="148" height="24" rx="4" class="fig-box-blue"/>
    <rect x="34" y="130" width="184" height="50" rx="4" class="fig-box"/>
    <rect x="38" y="154" width="176" height="20" rx="2" class="fig-box-accent"/>
    <rect x="234" y="130" width="188" height="50" rx="4" class="fig-box"/>
    <rect x="238" y="154" width="180" height="20" rx="2" class="fig-box-accent"/>
    <rect x="86" y="198" width="76" height="26" rx="4" class="fig-box-red"/>
    <rect x="260" y="198" width="76" height="26" rx="4" class="fig-box-red"/>
    <rect x="194" y="226" width="72" height="24" rx="4" class="fig-box-green"/>
    <rect x="464" y="46" width="204" height="54" rx="4" class="fig-box"/>
    <rect x="464" y="110" width="204" height="54" rx="4" class="fig-box"/>
    <rect x="464" y="174" width="204" height="72" rx="4" class="fig-box-blue"/>
    <rect x="18" y="270" width="664" height="40" rx="5" class="fig-box-purple"/>
    <text x="228" y="32" text-anchor="middle" font-size="11.5" class="fig-title">2 路组相联（K=2，共 2^G 组）并发比较数据通路</text>
    <text x="96" y="57" text-anchor="middle" font-size="10" class="fig-red">Tag (S-G 位)</text>
    <text x="214" y="57" text-anchor="middle" font-size="10" class="fig-blue">组号 Set (G 位)</text>
    <text x="318" y="57" text-anchor="middle" font-size="10" class="fig-green">Offset (W 位)</text>
    <text x="214" y="104" text-anchor="middle" font-size="9.5" class="fig-blue">组译码器 (q = j mod 2^G)</text>
    <text x="126" y="145" text-anchor="middle" font-size="8.8" class="fig-muted">第 0 路：V | LRU | Tag0 | Data0</text>
    <text x="126" y="168" text-anchor="middle" font-size="9.5" class="fig-accent">选中组 q 第 0 路行</text>
    <text x="328" y="145" text-anchor="middle" font-size="8.8" class="fig-muted">第 1 路：V | LRU | Tag1 | Data1</text>
    <text x="328" y="168" text-anchor="middle" font-size="9.5" class="fig-accent">选中组 q 第 1 路行</text>
    <text x="124" y="215" text-anchor="middle" font-size="9.5" class="fig-red">比较器 #0</text>
    <text x="298" y="215" text-anchor="middle" font-size="9.5" class="fig-red">比较器 #1</text>
    <text x="230" y="242" text-anchor="middle" font-size="9.5" class="fig-green">或门 → Hit</text>
    <text x="566" y="35" text-anchor="middle" font-size="11.5" class="fig-title">三种映射地址与比较器对比</text>
    <text x="566" y="64" text-anchor="middle" font-size="10" class="fig-label">① 直接映射（K=1 特例）</text>
    <text x="566" y="81" text-anchor="middle" font-size="9.5" class="fig-muted">Tag(S-R) | 行(R) | W · 1个比较器</text>
    <text x="566" y="94" text-anchor="middle" font-size="9" class="fig-muted">无需替换位（LRU=0 bit）</text>
    <text x="566" y="128" text-anchor="middle" font-size="10" class="fig-label">② 全相联映射（仅1组特例）</text>
    <text x="566" y="145" text-anchor="middle" font-size="9.5" class="fig-muted">Tag(S) | W · 全部行数个比较器</text>
    <text x="566" y="158" text-anchor="middle" font-size="9" class="fig-muted">冲突最低，相联存储器成本最高</text>
    <text x="566" y="193" text-anchor="middle" font-size="10" class="fig-blue">③ K 路组相联（现代主流）</text>
    <text x="566" y="211" text-anchor="middle" font-size="9.5" class="fig-label">Tag(S-G) | 组(G) | W · K个比较器</text>
    <text x="566" y="228" text-anchor="middle" font-size="9.5" class="fig-blue">组间直接取模，组内 K 路全相联</text>
    <text x="350" y="294" text-anchor="middle" font-size="9.5" class="fig-purple">组相联总容量 = 总行数(K×2^G) × [ 1(V) + D(脏位) + log₂K(LRU位) + (S-G)(Tag) + 2^W×8(数据) ] bit</text>
  </svg>
  <figcaption>图 4.15：K 路组相联 Cache 并发比较器通路与三种映射方式（直接、全相联、组相联）容量通式对比</figcaption>
</figure>

> <strong>注意</strong>：组相联中“组号”由块号对<strong>组数</strong>取模，不是对行数取模；同一组内不同行可存不同分区的块。

### 4.12 Cache 替换算法

直接映射每块只有一个固定行，冲突时<strong>直接替换</strong>，无需算法；全相联和组相联需在候选行中选择牺牲者。

| 算法 | 规则 | 实现 | 特点 |
|---|---|---|---|
| 随机 | 随机选一行替换 | 最简单、最快 | 可能替换即将使用的数据，命中率略低 |
| FIFO | 替换<strong>最先载入</strong>的行 | 记录载入时刻/时间戳 | 实现方便、开销小；未考虑局部性，可能淘汰常用块 |
| LFU | 替换<strong>被访问次数最少</strong>的行 | 每行一个计数器，命中加一，替换时选最小 | 成本较高；历史计数不能反映<strong>近期</strong>访问，易受早期高频数据影响 |
| LRU | 替换<strong>近期最久未被访问</strong>的行 | 每行一个计数器：命中行清零，其他非空行加一，替换时选最大 | 较准确反映局部性，命中率最高；硬件成本高 |

LRU 的计数器（LRU 位）位数与路数有关：

$$
\text{LRU bits} = \log_2 K
$$

2 路组相联只需 <strong>1 位</strong>标志位（相当于“哪一行更近被访问”）；4 路需 2 位。

实现细节：为避免计数器位宽不够，实际规则改为“命中行清零，<strong>计数值比它小的</strong>其他行加一”。

若某段时间集中访问的存储区超过 Cache 组大小，会反复替换同一行，命中率极低，称为<strong>抖动/颠簸</strong>。LFU 尤其易抖动：早期高频数据长期占位，新数据刚载入又被换出。

<figure class="cpd-figure not-content" id="hco-w3-fig-16">
  <svg viewBox="0 0 700 302" role="img" aria-label="Cache替换算法FIFO与LRU计数器在3行全相联下的8步手工模拟对比轨迹表">
    <rect x="18" y="16" width="664" height="224" rx="6" class="fig-box-subtle"/>
    <rect x="18" y="248" width="664" height="38" rx="5" class="fig-box-orange"/>
    <rect x="30" y="44" width="112" height="28" rx="3" class="fig-box"/>
    <rect x="146" y="44" width="62" height="28" rx="3" class="fig-box-blue"/>
    <rect x="212" y="44" width="62" height="28" rx="3" class="fig-box-blue"/>
    <rect x="278" y="44" width="62" height="28" rx="3" class="fig-box-blue"/>
    <rect x="344" y="44" width="62" height="28" rx="3" class="fig-box-green"/>
    <rect x="410" y="44" width="62" height="28" rx="3" class="fig-box-red"/>
    <rect x="476" y="44" width="62" height="28" rx="3" class="fig-box-accent"/>
    <rect x="542" y="44" width="62" height="28" rx="3" class="fig-box-green"/>
    <rect x="608" y="44" width="62" height="28" rx="3" class="fig-box-green"/>
    <rect x="30" y="78" width="112" height="68" rx="3" class="fig-box"/>
    <rect x="146" y="78" width="62" height="68" rx="3" class="fig-box"/>
    <rect x="212" y="78" width="62" height="68" rx="3" class="fig-box"/>
    <rect x="278" y="78" width="62" height="68" rx="3" class="fig-box"/>
    <rect x="344" y="78" width="62" height="68" rx="3" class="fig-box-green"/>
    <rect x="410" y="78" width="62" height="68" rx="3" class="fig-box-red"/>
    <rect x="476" y="78" width="62" height="68" rx="3" class="fig-box-red"/>
    <rect x="542" y="78" width="62" height="68" rx="3" class="fig-box-green"/>
    <rect x="608" y="78" width="62" height="68" rx="3" class="fig-box-green"/>
    <rect x="30" y="154" width="112" height="74" rx="3" class="fig-box"/>
    <rect x="146" y="154" width="62" height="74" rx="3" class="fig-box"/>
    <rect x="212" y="154" width="62" height="74" rx="3" class="fig-box"/>
    <rect x="278" y="154" width="62" height="74" rx="3" class="fig-box"/>
    <rect x="344" y="154" width="62" height="74" rx="3" class="fig-box-green"/>
    <rect x="410" y="154" width="62" height="74" rx="3" class="fig-box-red"/>
    <rect x="476" y="154" width="62" height="74" rx="3" class="fig-box-green"/>
    <rect x="542" y="154" width="62" height="74" rx="3" class="fig-box-green"/>
    <rect x="608" y="154" width="62" height="74" rx="3" class="fig-box-green"/>
    <text x="350" y="34" text-anchor="middle" font-size="11.5" class="fig-title">3 行全相联组替换模拟对比（访问块序列：1, 2, 3, 1, 4, 1, 3, 4；括号内为 LRU 计数值）</text>
    <text x="86" y="62" text-anchor="middle" font-size="10" class="fig-label">访问主存块</text>
    <text x="177" y="62" text-anchor="middle" font-size="10.5" class="fig-blue">① 块1</text>
    <text x="243" y="62" text-anchor="middle" font-size="10.5" class="fig-blue">② 块2</text>
    <text x="309" y="62" text-anchor="middle" font-size="10.5" class="fig-blue">③ 块3</text>
    <text x="375" y="62" text-anchor="middle" font-size="10.5" class="fig-green">④ 块1</text>
    <text x="441" y="62" text-anchor="middle" font-size="10.5" class="fig-red">⑤ 块4</text>
    <text x="507" y="62" text-anchor="middle" font-size="10.5" class="fig-accent">⑥ 块1★</text>
    <text x="573" y="62" text-anchor="middle" font-size="10.5" class="fig-green">⑦ 块3</text>
    <text x="639" y="62" text-anchor="middle" font-size="10.5" class="fig-green">⑧ 块4</text>
    <text x="86" y="102" text-anchor="middle" font-size="10.5" class="fig-title">FIFO 先进先出</text>
    <text x="86" y="120" text-anchor="middle" font-size="9.5" class="fig-muted">命中不改入队序</text>
    <text x="86" y="136" text-anchor="middle" font-size="9.5" class="fig-red">命中率 = 3 / 8</text>
    <text x="177" y="98" text-anchor="middle" font-size="10" class="fig-label">1, -, -</text>
    <text x="177" y="134" text-anchor="middle" font-size="9" class="fig-muted">冷缺失</text>
    <text x="243" y="98" text-anchor="middle" font-size="10" class="fig-label">1, 2, -</text>
    <text x="243" y="134" text-anchor="middle" font-size="9" class="fig-muted">冷缺失</text>
    <text x="309" y="98" text-anchor="middle" font-size="10" class="fig-label">1, 2, 3</text>
    <text x="309" y="134" text-anchor="middle" font-size="9" class="fig-muted">冷缺失</text>
    <text x="375" y="98" text-anchor="middle" font-size="10" class="fig-green">1, 2, 3</text>
    <text x="375" y="118" text-anchor="middle" font-size="9" class="fig-muted">1仍最老</text>
    <text x="375" y="134" text-anchor="middle" font-size="9.5" class="fig-green">命中!</text>
    <text x="441" y="98" text-anchor="middle" font-size="10" class="fig-red">4, 2, 3</text>
    <text x="441" y="118" text-anchor="middle" font-size="9" class="fig-red">淘汰 1</text>
    <text x="441" y="134" text-anchor="middle" font-size="9" class="fig-red">缺失</text>
    <text x="507" y="98" text-anchor="middle" font-size="10" class="fig-red">4, 1, 3</text>
    <text x="507" y="118" text-anchor="middle" font-size="9" class="fig-red">淘汰 2</text>
    <text x="507" y="134" text-anchor="middle" font-size="9.5" class="fig-red">缺失!</text>
    <text x="573" y="98" text-anchor="middle" font-size="10" class="fig-green">4, 1, 3</text>
    <text x="573" y="134" text-anchor="middle" font-size="9.5" class="fig-green">命中!</text>
    <text x="639" y="98" text-anchor="middle" font-size="10" class="fig-green">4, 1, 3</text>
    <text x="639" y="134" text-anchor="middle" font-size="9.5" class="fig-green">命中!</text>
    <text x="86" y="178" text-anchor="middle" font-size="10.5" class="fig-title">LRU 近期最少用</text>
    <text x="86" y="196" text-anchor="middle" font-size="9.5" class="fig-muted">命中清0,小者+1</text>
    <text x="86" y="214" text-anchor="middle" font-size="9.5" class="fig-green">命中率 = 4 / 8</text>
    <text x="177" y="173" text-anchor="middle" font-size="9.5" class="fig-label">1(0)</text>
    <text x="177" y="216" text-anchor="middle" font-size="9" class="fig-muted">冷缺失</text>
    <text x="243" y="173" text-anchor="middle" font-size="9.5" class="fig-label">1(1)</text>
    <text x="243" y="189" text-anchor="middle" font-size="9.5" class="fig-label">2(0)</text>
    <text x="243" y="216" text-anchor="middle" font-size="9" class="fig-muted">冷缺失</text>
    <text x="309" y="171" text-anchor="middle" font-size="8.8" class="fig-label">1(2) 2(1)</text>
    <text x="309" y="187" text-anchor="middle" font-size="9.5" class="fig-label">3(0)</text>
    <text x="309" y="216" text-anchor="middle" font-size="9" class="fig-muted">冷缺失</text>
    <text x="375" y="171" text-anchor="middle" font-size="8.8" class="fig-green">1(0) 2(2)</text>
    <text x="375" y="187" text-anchor="middle" font-size="9.5" class="fig-green">3(1)</text>
    <text x="375" y="216" text-anchor="middle" font-size="9.5" class="fig-green">命中清0</text>
    <text x="441" y="171" text-anchor="middle" font-size="8.8" class="fig-red">1(1) 4(0)</text>
    <text x="441" y="187" text-anchor="middle" font-size="9.5" class="fig-red">3(2)</text>
    <text x="441" y="203" text-anchor="middle" font-size="9" class="fig-red">汰2(max)</text>
    <text x="441" y="218" text-anchor="middle" font-size="9" class="fig-red">缺失</text>
    <text x="507" y="171" text-anchor="middle" font-size="8.8" class="fig-green">1(0) 4(1)</text>
    <text x="507" y="187" text-anchor="middle" font-size="8.8" class="fig-green">3(2)不变</text>
    <text x="507" y="216" text-anchor="middle" font-size="9.5" class="fig-green">命中!★</text>
    <text x="573" y="171" text-anchor="middle" font-size="8.8" class="fig-green">1(1) 4(2)</text>
    <text x="573" y="187" text-anchor="middle" font-size="9.5" class="fig-green">3(0)</text>
    <text x="573" y="216" text-anchor="middle" font-size="9.5" class="fig-green">命中!</text>
    <text x="639" y="171" text-anchor="middle" font-size="8.8" class="fig-green">1(2) 4(0)</text>
    <text x="639" y="187" text-anchor="middle" font-size="9.5" class="fig-green">3(1)</text>
    <text x="639" y="216" text-anchor="middle" font-size="9.5" class="fig-green">命中!</text>
    <text x="350" y="271" text-anchor="middle" font-size="10" class="fig-orange">抖动（颠簸）考点：若循环顺序访问 {1,2,3,4} 共 4 个块装入 3 行组，FIFO 与 LRU 每次均缺失（命中率 0%）！</text>
  </svg>
  <figcaption>图 4.16：FIFO 与 LRU 计数器法（命中行清 0、仅小于原计数值行 +1、淘汰最大计数行）模拟对比及抖动现象</figcaption>
</figure>

> <strong>★考点</strong>：LRU 手工模拟时，“命中行清零、其余非空行加一”；替换时选<strong>计数值最大</strong>者。LFU 则“命中加一、替换时选最小”，并列最小可用 FIFO 或随机打破。

### 4.13 页式虚拟存储器与地址转换

虚拟存储器为程序员屏蔽主存容量限制，把主存与辅存统一编址。页式虚拟存储器把虚拟地址空间与主存空间都划分为<strong>大小相同的页</strong>（如 4 KB）。

- <strong>虚拟地址</strong> = 虚拟页号（VPN）+ 页内偏移；
- <strong>物理地址</strong> = 物理页号（PPN，又称页框号）+ 页内偏移；
- 页内偏移在转换中<strong>保持不变</strong>；
- <strong>页表</strong>：每进程一张，常驻主存，由<strong>页表基址寄存器</strong>定位；页表项含<strong>有效位（页面是否在主存）、物理页号</strong>、<strong>脏位</strong>（是否被修改）等；
- 虚拟页到物理页采用<strong>全相联映射</strong>：任一虚拟页可装入任一物理页框；缺页时由操作系统按替换算法选择牺牲页。

<strong>页面命中</strong>：CPU 给出虚拟地址 → MMU 用页表基址 + VPN 生成页表项地址 → 读主存页表项 → 有效位为 1 → 用 PPN 拼偏移得物理地址 → 访问主存取数据。

<strong>缺页：有效位为 0 → MMU 触发缺页异常 → 操作系统缺页处理程序：若主存页框已满，按替换算法选页，脏位为 1 则写回磁盘，否则直接丢弃；从磁盘调入新页并更新页表项；返回原进程，重新执行引起缺页的指令</strong>（此时不再缺页）。

<figure class="cpd-figure not-content" id="hco-w3-fig-17">
  <svg viewBox="0 0 700 332" role="img" aria-label="页式虚拟存储器PTBR页表地址转换数据通路与缺页异常处理流程图">
    <defs>
      <marker id="hco-w3-arr-17" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="664" height="300" rx="6" class="fig-box-subtle"/>
    <line x1="128" y1="100" x2="178" y2="100" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w3-arr-17)"/>
    <polyline points="160,64 160,90 178,90" fill="none" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-17)"/>
    <line x1="226" y1="100" x2="246" y2="100" class="fig-line-accent" marker-end="url(#hco-w3-arr-17)"/>
    <line x1="301" y1="114" x2="301" y2="138" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-17)"/>
    <polyline points="354,184 370,184 370,96 446,96 446,66" fill="none" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-17)"/>
    <polyline points="304,64 304,74 578,74 578,66" fill="none" class="fig-line-dashed" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-17)"/>
    <line x1="354" y1="218" x2="382" y2="218" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-17)"/>
    <rect x="86" y="36" width="148" height="28" rx="3" class="fig-box-blue"/>
    <rect x="236" y="36" width="136" height="28" rx="3" class="fig-box-green"/>
    <rect x="34" y="86" width="94" height="28" rx="4" class="fig-box-purple"/>
    <rect x="180" y="84" width="46" height="32" rx="4" class="fig-box-accent"/>
    <rect x="248" y="86" width="106" height="28" rx="4" class="fig-box"/>
    <rect x="386" y="36" width="122" height="28" rx="3" class="fig-box-blue"/>
    <rect x="510" y="36" width="136" height="28" rx="3" class="fig-box-green"/>
    <rect x="74" y="140" width="280" height="104" rx="4" class="fig-box"/>
    <rect x="74" y="172" width="280" height="24" rx="2" class="fig-box-accent"/>
    <rect x="74" y="206" width="280" height="24" rx="2" class="fig-box-red"/>
    <line x1="74" y1="164" x2="354" y2="164" class="fig-divider"/>
    <line x1="112" y1="140" x2="112" y2="244" class="fig-divider"/>
    <line x1="150" y1="140" x2="150" y2="244" class="fig-divider"/>
    <line x1="204" y1="140" x2="204" y2="244" class="fig-divider"/>
    <rect x="384" y="126" width="284" height="122" rx="5" class="fig-box-red"/>
    <rect x="34" y="262" width="630" height="38" rx="4" class="fig-box-purple"/>
    <text x="229" y="30" text-anchor="middle" font-size="11" class="fig-title">虚拟地址 VA（逻辑地址）</text>
    <text x="160" y="54" text-anchor="middle" font-size="10.5" class="fig-blue">虚拟页号 VPN</text>
    <text x="304" y="54" text-anchor="middle" font-size="10.5" class="fig-green">页内偏移 Offset</text>
    <text x="81" y="104" text-anchor="middle" font-size="10" class="fig-purple">页表基址 PTBR</text>
    <text x="203" y="104" text-anchor="middle" font-size="11" class="fig-accent">+</text>
    <text x="301" y="104" text-anchor="middle" font-size="9.5" class="fig-label">页表项物理地址</text>
    <text x="516" y="30" text-anchor="middle" font-size="11" class="fig-title">主存物理地址 PA</text>
    <text x="447" y="54" text-anchor="middle" font-size="10.5" class="fig-blue">物理页框号 PPN</text>
    <text x="578" y="54" text-anchor="middle" font-size="10.5" class="fig-green">页内偏移（原样直连）</text>
    <text x="93" y="156" text-anchor="middle" font-size="9.5" class="fig-muted">V</text>
    <text x="131" y="156" text-anchor="middle" font-size="9.5" class="fig-muted">脏D</text>
    <text x="177" y="156" text-anchor="middle" font-size="9.5" class="fig-muted">权限</text>
    <text x="279" y="156" text-anchor="middle" font-size="9.5" class="fig-muted">物理页框号 PPN / 外存地址</text>
    <text x="93" y="188" text-anchor="middle" font-size="10" class="fig-accent">1</text>
    <text x="131" y="188" text-anchor="middle" font-size="10" class="fig-accent">0</text>
    <text x="177" y="188" text-anchor="middle" font-size="9.5" class="fig-accent">R/W</text>
    <text x="279" y="188" text-anchor="middle" font-size="10" class="fig-accent">PPN = 0x1A3（命中）</text>
    <text x="93" y="222" text-anchor="middle" font-size="10" class="fig-red">0</text>
    <text x="131" y="222" text-anchor="middle" font-size="10" class="fig-red">—</text>
    <text x="177" y="222" text-anchor="middle" font-size="9.5" class="fig-red">R</text>
    <text x="279" y="222" text-anchor="middle" font-size="10" class="fig-red">磁盘块号（未装入主存）</text>
    <text x="526" y="146" text-anchor="middle" font-size="10.5" class="fig-red">V = 0 → 触发“缺页异常”（内中断/故障）</text>
    <text x="526" y="167" text-anchor="middle" font-size="9.5" class="fig-label">① OS 缺页程序接管，页框满则按 LRU 选牺牲页</text>
    <text x="526" y="186" text-anchor="middle" font-size="9.5" class="fig-label">② 牺牲页脏位 D=1 则写回磁盘，否则直接覆盖</text>
    <text x="526" y="205" text-anchor="middle" font-size="9.5" class="fig-label">③ 从磁盘调入目标页至主存，更新页表置 V=1</text>
    <text x="526" y="228" text-anchor="middle" font-size="10" class="fig-red">④ 异常返回后“重新执行引起缺页的当前指令”！</text>
    <text x="349" y="285" text-anchor="middle" font-size="10" class="fig-purple">PTE 地址 = PTBR + VPN × 页表项字节数；无快表时单级页表命中需 2 次访存（1 次读页表 + 1 次读写数据）</text>
  </svg>
  <figcaption>图 4.17：页式虚存硬件地址转换（PTBR + VPN 索引页表项）与缺页异常 OS 调页恢复流程</figcaption>
</figure>

> <strong>注意</strong>：缺页处理后不是“继续执行下一条指令”，而是<strong>重新执行当前指令</strong>——这是 408 选择题的高频陷阱。
>
> <strong>★考点</strong>：虚拟地址位数决定虚拟空间大小；页内偏移位数 $=\log_2(\text{page size})$；物理地址位数 = 物理页号位数 + 偏移位数，与虚拟地址位数<strong>不一定相同</strong>。

### 4.14 快表 TLB 与完整访存流程

页表在主存中，一次页面命中要两次访存（读页表项 + 取数据），太慢。TLB（转换后备缓冲器 / 快表）是 CPU 内专门缓存<strong>页表项</strong>的小容量相联存储器，可视为“页表项专用 Cache”。

- 一般采用<strong>全相联或组相联</strong>，缺失时常用随机替换；
- 表项含 tag（虚拟页号或其中一部分）、物理页号、有效位等；
- <strong>全相联</strong>：虚拟页号与所有表项 tag 并发比较；
- <strong>组相联</strong>：虚拟地址的虚拟页号再分为 <strong>TLB 标记 + TLB 组索引</strong>，用组索引选组，组内并发比较标记。

<strong>TLB 命中</strong>：MMU 用 VPN 查 TLB → 命中得物理页号 → 与页内偏移拼成物理地址 → 访问 Cache/主存。

<strong>TLB 缺失</strong>：查主存页表项 → 若有效位为 1，则把该页表项<strong>载入 TLB</strong>（可能替换），同时用其物理页号转换地址；若有效位为 0，则缺页，交给操作系统。

<strong>完整访存流程</strong>（TLB + 页表 + Cache + 主存 + 磁盘）：

1. CPU 给出虚拟地址；
2. 查 TLB：命中 → 得 PPN；缺失 → 查页表（可能再缺失 → 缺页）；
3. 拼出物理地址，查 Cache：命中 → 返回数据；缺失 → 访问主存并调入 Cache 块；
4. 若缺页 → 从磁盘调入页面，更新页表与 TLB，重新执行指令。

缺失代价与处理者：

| 缺失类型 | 代价 | 处理者 |
|---|---|---|
| Cache 缺失 | 访问主存（数十～数百周期） | 硬件 |
| TLB 缺失 | 访问主存页表（可能多次） | 硬件（页表遍历） |
| 缺页 | 访问磁盘（数百万周期） | 操作系统 |

<figure class="cpd-figure not-content" id="hco-w3-fig-18">
  <svg viewBox="0 0 700 310" role="img" aria-label="包含TLB快表慢表页表Cache主存与磁盘的CPU完整访存控制流程图">
    <defs>
      <marker id="hco-w3-arr-18" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="18" y="16" width="664" height="278" rx="6" class="fig-box-subtle"/>
    <line x1="126" y1="62" x2="162" y2="62" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-18)"/>
    <line x1="292" y1="62" x2="352" y2="62" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-18)"/>
    <line x1="227" y1="98" x2="227" y2="132" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w3-arr-18)"/>
    <polyline points="292,158 322,158 322,74 352,74" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-18)"/>
    <line x1="227" y1="192" x2="227" y2="224" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-18)"/>
    <polyline points="162,250 78,250 78,88" class="fig-line-dashed" stroke="var(--sl-color-red)" marker-end="url(#hco-w3-arr-18)"/>
    <line x1="474" y1="62" x2="514" y2="62" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w3-arr-18)"/>
    <line x1="590" y1="96" x2="590" y2="132" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-18)"/>
    <polyline points="552,96 552,118 453,118 453,164" fill="none" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w3-arr-18)"/>
    <polyline points="454,218 454,250 590,250 590,174" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w3-arr-18)"/>
    <rect x="34" y="38" width="92" height="48" rx="4" class="fig-box-blue"/>
    <rect x="164" y="30" width="128" height="68" rx="5" class="fig-box-purple"/>
    <rect x="164" y="134" width="128" height="58" rx="5" class="fig-box-orange"/>
    <rect x="164" y="226" width="194" height="50" rx="5" class="fig-box-red"/>
    <rect x="354" y="38" width="120" height="48" rx="4" class="fig-box-blue"/>
    <rect x="516" y="30" width="148" height="66" rx="5" class="fig-box-accent"/>
    <rect x="522" y="134" width="142" height="38" rx="4" class="fig-box-green"/>
    <rect x="392" y="166" width="122" height="52" rx="4" class="fig-box"/>
    <text x="80" y="58" text-anchor="middle" font-size="10.5" class="fig-blue">CPU 发出</text>
    <text x="80" y="74" text-anchor="middle" font-size="10" class="fig-blue">虚拟地址 VA</text>
    <text x="228" y="50" text-anchor="middle" font-size="11" class="fig-purple">查快表 TLB (SRAM)</text>
    <text x="228" y="68" text-anchor="middle" font-size="9.5" class="fig-label">全相联 / 组相联比较</text>
    <text x="228" y="84" text-anchor="middle" font-size="9.5" class="fig-muted">缓存近期页表项 PTE</text>
    <text x="322" y="53" text-anchor="middle" font-size="9.5" class="fig-green">TLB命中</text>
    <text x="256" y="118" font-size="9.5" class="fig-orange">TLB缺失</text>
    <text x="228" y="154" text-anchor="middle" font-size="10.5" class="fig-orange">查主存慢表（页表）</text>
    <text x="228" y="172" text-anchor="middle" font-size="9.5" class="fig-label">V=1 → 填入 TLB</text>
    <text x="262" y="212" font-size="9.5" class="fig-red">V=0 缺页</text>
    <text x="261" y="247" text-anchor="middle" font-size="10" class="fig-red">OS 缺页异常处理：读磁盘调页</text>
    <text x="261" y="264" text-anchor="middle" font-size="9.5" class="fig-label">更新页表 → 重新执行原指令</text>
    <text x="414" y="58" text-anchor="middle" font-size="10.5" class="fig-blue">拼接物理地址 PA</text>
    <text x="414" y="74" text-anchor="middle" font-size="9.5" class="fig-muted">PPN || 页内偏移</text>
    <text x="590" y="50" text-anchor="middle" font-size="11" class="fig-accent">查 Cache（按 PA）</text>
    <text x="590" y="68" text-anchor="middle" font-size="9.5" class="fig-label">比较物理 Tag 且 V=1</text>
    <text x="590" y="84" text-anchor="middle" font-size="9.5" class="fig-muted">硬件自动控制</text>
    <text x="618" y="118" font-size="9.5" class="fig-green">命中</text>
    <text x="502" y="112" text-anchor="middle" font-size="9.5" class="fig-orange">Cache缺失</text>
    <text x="593" y="157" text-anchor="middle" font-size="10" class="fig-green">将数据送回 CPU 寄存器</text>
    <text x="453" y="188" text-anchor="middle" font-size="10.5" class="fig-label">访问主存 DRAM</text>
    <text x="453" y="206" text-anchor="middle" font-size="9.5" class="fig-muted">调入整块至 Cache</text>
  </svg>
  <figcaption>图 4.18：VA → TLB → 主存页表 → 磁盘调页 → PA → Cache → 主存完整协同访存数据通路</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w3-fig-19">
  <svg viewBox="0 0 700 328" role="img" aria-label="TLB快表与Page页表及Cache八种命中缺失组合真值表与访存次数统计图">
    <rect x="18" y="16" width="664" height="296" rx="6" class="fig-box-subtle"/>
    <rect x="30" y="42" width="640" height="26" rx="3" class="fig-box"/>
    <rect x="30" y="72" width="640" height="25" rx="3" class="fig-box-green"/>
    <rect x="30" y="100" width="640" height="25" rx="3" class="fig-box-blue"/>
    <rect x="30" y="128" width="640" height="25" rx="3" class="fig-box-blue"/>
    <rect x="30" y="156" width="640" height="25" rx="3" class="fig-box-blue"/>
    <rect x="30" y="184" width="640" height="25" rx="3" class="fig-box-orange"/>
    <rect x="30" y="216" width="640" height="25" rx="3" class="fig-box-red"/>
    <rect x="30" y="244" width="640" height="25" rx="3" class="fig-box-red"/>
    <rect x="30" y="272" width="640" height="25" rx="3" class="fig-box-red"/>
    <line x1="76" y1="42" x2="76" y2="297" class="fig-divider"/>
    <line x1="152" y1="42" x2="152" y2="297" class="fig-divider"/>
    <line x1="234" y1="42" x2="234" y2="297" class="fig-divider"/>
    <line x1="312" y1="42" x2="312" y2="297" class="fig-divider"/>
    <text x="350" y="34" text-anchor="middle" font-size="11.5" class="fig-title">408 高频真题模型：TLB × 页表（Page）× Cache 八种组合可能性与访存次数全解</text>
    <text x="53" y="59" text-anchor="middle" font-size="10" class="fig-title">组合</text>
    <text x="114" y="59" text-anchor="middle" font-size="10" class="fig-title">TLB 快表</text>
    <text x="193" y="59" text-anchor="middle" font-size="10" class="fig-title">Page 页表</text>
    <text x="273" y="59" text-anchor="middle" font-size="10" class="fig-title">Cache</text>
    <text x="491" y="59" text-anchor="middle" font-size="10" class="fig-title">可行性判定 &amp; 访存/访盘代价（按单级页表计）</text>
    <text x="53" y="88" text-anchor="middle" font-size="10" class="fig-green">①</text>
    <text x="114" y="88" text-anchor="middle" font-size="10" class="fig-green">命中</text>
    <text x="193" y="88" text-anchor="middle" font-size="10" class="fig-green">命中</text>
    <text x="273" y="88" text-anchor="middle" font-size="10" class="fig-green">命中</text>
    <text x="491" y="88" text-anchor="middle" font-size="10" class="fig-green">可能（最快）：访主存 0 次，访盘 0 次（全在片上 SRAM 完成）</text>
    <text x="53" y="116" text-anchor="middle" font-size="10" class="fig-blue">②</text>
    <text x="114" y="116" text-anchor="middle" font-size="10" class="fig-blue">命中</text>
    <text x="193" y="116" text-anchor="middle" font-size="10" class="fig-blue">命中</text>
    <text x="273" y="116" text-anchor="middle" font-size="10" class="fig-label">缺失</text>
    <text x="491" y="116" text-anchor="middle" font-size="10" class="fig-label">可能：访主存 1 次（读主存数据块调入 Cache），访盘 0 次</text>
    <text x="53" y="144" text-anchor="middle" font-size="10" class="fig-blue">③</text>
    <text x="114" y="144" text-anchor="middle" font-size="10" class="fig-label">缺失</text>
    <text x="193" y="144" text-anchor="middle" font-size="10" class="fig-blue">命中</text>
    <text x="273" y="144" text-anchor="middle" font-size="10" class="fig-blue">命中</text>
    <text x="491" y="144" text-anchor="middle" font-size="10" class="fig-label">可能：访主存 1 次（查慢表得 PPN 并填 TLB，数据已在 Cache）</text>
    <text x="53" y="172" text-anchor="middle" font-size="10" class="fig-blue">④</text>
    <text x="114" y="172" text-anchor="middle" font-size="10" class="fig-label">缺失</text>
    <text x="193" y="172" text-anchor="middle" font-size="10" class="fig-blue">命中</text>
    <text x="273" y="172" text-anchor="middle" font-size="10" class="fig-label">缺失</text>
    <text x="491" y="172" text-anchor="middle" font-size="10" class="fig-label">可能：访主存 2 次（1 次查慢表 + 1 次读数据块），访盘 0 次</text>
    <text x="53" y="200" text-anchor="middle" font-size="10" class="fig-orange">⑤</text>
    <text x="114" y="200" text-anchor="middle" font-size="10" class="fig-orange">缺失</text>
    <text x="193" y="200" text-anchor="middle" font-size="10" class="fig-orange">缺失</text>
    <text x="273" y="200" text-anchor="middle" font-size="10" class="fig-orange">缺失</text>
    <text x="491" y="200" text-anchor="middle" font-size="10" class="fig-orange">可能（最慢）：查页表 V=0 触发缺页 → 至少 1 次读磁盘调页 + 重执</text>
    <text x="53" y="232" text-anchor="middle" font-size="10" class="fig-red">⑥</text>
    <text x="114" y="232" text-anchor="middle" font-size="10" class="fig-red">命中</text>
    <text x="193" y="232" text-anchor="middle" font-size="10" class="fig-red">缺失</text>
    <text x="273" y="232" text-anchor="middle" font-size="10" class="fig-red">命中</text>
    <text x="491" y="232" text-anchor="middle" font-size="10" class="fig-red">不可能！TLB 仅缓存主存有效页表项（Page 缺失则 TLB 必缺失）</text>
    <text x="53" y="260" text-anchor="middle" font-size="10" class="fig-red">⑦</text>
    <text x="114" y="260" text-anchor="middle" font-size="10" class="fig-red">命中</text>
    <text x="193" y="260" text-anchor="middle" font-size="10" class="fig-red">缺失</text>
    <text x="273" y="260" text-anchor="middle" font-size="10" class="fig-red">缺失</text>
    <text x="491" y="260" text-anchor="middle" font-size="10" class="fig-red">不可能！页面换出主存时 OS 已同步清除对应 TLB 表项</text>
    <text x="53" y="288" text-anchor="middle" font-size="10" class="fig-red">⑧</text>
    <text x="114" y="288" text-anchor="middle" font-size="10" class="fig-red">缺失</text>
    <text x="193" y="288" text-anchor="middle" font-size="10" class="fig-red">缺失</text>
    <text x="273" y="288" text-anchor="middle" font-size="10" class="fig-red">命中</text>
    <text x="491" y="288" text-anchor="middle" font-size="10" class="fig-red">不可能！目标页不在主存（缺页），其数据块不可能驻留 Cache</text>
  </svg>
  <figcaption>图 4.19：TLB、主存页表与 Cache 八种命中/缺失组合真值矩阵（5 种可能 vs 3 种包含关系矛盾不可能）</figcaption>
</figure>

> <strong>★考点</strong>：TLB 命中只说明“页表项在快表中”，不代表“数据在 Cache 中”；TLB 与 Cache 相互独立，四种命中/缺失组合都可能出现，最坏情况是 TLB 缺失 + 缺页 + Cache 缺失。
>
> <strong>注意</strong>：TLB 采用组相联时，虚拟页号被划分为 TLB 标记与 TLB 组索引；页内偏移不参与 TLB 比较。若 TLB 有 8 组，则组索引 3 位。

## 第5章 指令系统

### 5.1 指令格式与指令字长

指令一般由<strong>操作码字段</strong>与<strong>地址码字段</strong>构成：

- <strong>操作码 OP</strong>：指明进行何种操作（加、减、移位、传送等）；
- <strong>地址码 A</strong>：指明操作数或操作数地址，可有多个。如何取得操作数由<strong>寻址方式</strong>决定——寻址方式可<strong>显式</strong>包含在地址码字段中（如 x86），也可<strong>隐式</strong>包含在操作码中（如 MIPS、RISC-V）。

按地址码个数分为：

| 格式 | 结构 | 说明 |
|---|---|---|
| 三地址 | OP A1 A2 A3 | `A1 op A2 → A3`，需 4 次访存（取指 + 取两操作数 + 存结果） |
| 二地址 | OP A1 A2 | `A1 op A2 → A1`，常用；结果覆盖 A1 |
| 一地址 | OP A1 | 单操作数运算（自增、取反），或另一操作数隐含在累加器 ACC 中 |
| 零地址 | OP | 无操作数（空操作、停机）或操作数隐含在堆栈中 |

<strong>指令字长</strong>即构成指令的二进制位数。按各指令长度是否固定分为：

| 类型 | 优点 | 缺点 | 典型 |
|---|---|---|---|
| 定长指令系统 | 结构简单，便于顺序取指与译码，硬件易实现 | 平均指令长度较长、冗余状态多（简单操作用不满），受长度限制难扩展功能 | RISC，如 MIPS |
| 变长指令系统 | 结构灵活、平均长度短、冗余少、便于扩展 | 取指译码不便，取指可能多次访存，硬件实现难 | CISC，如 x86 |

两者共同点：<strong>指令字长都是字节的整数倍</strong>（存储器按字节编址）。按与机器字长的关系可分为<strong>半字长指令（一次访存可取两条）、单字长指令（一次访存取一条）、多字长指令</strong>（需多次访存）。变长指令系统中通常把<strong>最常用的指令设计得较短</strong>，以提高速度。

<figure class="cpd-figure not-content" id="hco-w4-fig-1">
  <svg viewBox="0 0 720 350" role="img" aria-label="四、三、二、一、零地址指令格式结构与主存访存次数对比图">
    <rect x="16" y="14" width="688" height="322" rx="8" class="fig-box-subtle"/>
    <line x1="28" y1="66" x2="692" y2="66" class="fig-divider"/>
    <rect x="114" y="76" width="52" height="32" rx="4" class="fig-box-red"/>
    <rect x="170" y="76" width="54" height="32" rx="4" class="fig-box-blue"/>
    <rect x="228" y="76" width="54" height="32" rx="4" class="fig-box-blue"/>
    <rect x="286" y="76" width="54" height="32" rx="4" class="fig-box-green"/>
    <rect x="344" y="76" width="58" height="32" rx="4" class="fig-box-purple"/>
    <rect x="586" y="76" width="102" height="32" rx="4" class="fig-box"/>
    <rect x="114" y="120" width="52" height="32" rx="4" class="fig-box-red"/>
    <rect x="170" y="120" width="74" height="32" rx="4" class="fig-box-blue"/>
    <rect x="248" y="120" width="74" height="32" rx="4" class="fig-box-blue"/>
    <rect x="326" y="120" width="76" height="32" rx="4" class="fig-box-green"/>
    <rect x="586" y="120" width="102" height="32" rx="4" class="fig-box"/>
    <rect x="114" y="164" width="52" height="32" rx="4" class="fig-box-red"/>
    <rect x="170" y="164" width="114" height="32" rx="4" class="fig-box-orange"/>
    <rect x="288" y="164" width="114" height="32" rx="4" class="fig-box-blue"/>
    <rect x="586" y="164" width="102" height="32" rx="4" class="fig-box-orange"/>
    <rect x="114" y="208" width="52" height="32" rx="4" class="fig-box-red"/>
    <rect x="170" y="208" width="140" height="32" rx="4" class="fig-box-blue"/>
    <rect x="316" y="208" width="86" height="32" rx="4" class="fig-box-dashed"/>
    <rect x="586" y="208" width="102" height="32" rx="4" class="fig-box-blue"/>
    <rect x="114" y="252" width="52" height="32" rx="4" class="fig-box-red"/>
    <rect x="170" y="252" width="232" height="32" rx="4" class="fig-box-dashed"/>
    <rect x="586" y="252" width="102" height="32" rx="4" class="fig-box-purple"/>
    <rect x="28" y="294" width="664" height="32" rx="5" class="fig-box"/>
    <text x="32" y="38" class="fig-title" font-size="13">四 / 三 / 二 / 一 / 零地址指令格式与主存访存次数对比（均含取指 1 次）</text>
    <text x="69" y="58" text-anchor="middle" class="fig-muted" font-size="11">指令类型</text>
    <text x="258" y="58" text-anchor="middle" class="fig-muted" font-size="11">指令字结构（OP + 显式/隐式地址码）</text>
    <text x="494" y="58" text-anchor="middle" class="fig-muted" font-size="11">数学语义与硬件约定</text>
    <text x="637" y="58" text-anchor="middle" class="fig-muted" font-size="11">总访存次数</text>
    <text x="69" y="97" text-anchor="middle" class="fig-label" font-size="12">四地址</text>
    <text x="140" y="97" text-anchor="middle" class="fig-red" font-size="11">OP</text>
    <text x="197" y="97" text-anchor="middle" class="fig-blue" font-size="11">A1</text>
    <text x="255" y="97" text-anchor="middle" class="fig-blue" font-size="11">A2</text>
    <text x="313" y="97" text-anchor="middle" class="fig-green" font-size="11">A3</text>
    <text x="373" y="97" text-anchor="middle" class="fig-purple" font-size="10.5">A4(下址)</text>
    <text x="494" y="97" text-anchor="middle" class="fig-muted" font-size="9.8">(A1) op (A2) → A3，下条取 A4</text>
    <text x="637" y="97" text-anchor="middle" class="fig-label" font-size="10.5">4 次 (1+2+1)</text>
    <text x="69" y="141" text-anchor="middle" class="fig-label" font-size="12">三地址</text>
    <text x="140" y="141" text-anchor="middle" class="fig-red" font-size="11">OP</text>
    <text x="207" y="141" text-anchor="middle" class="fig-blue" font-size="11">A1(源1)</text>
    <text x="285" y="141" text-anchor="middle" class="fig-blue" font-size="11">A2(源2)</text>
    <text x="364" y="141" text-anchor="middle" class="fig-green" font-size="11">A3(目的)</text>
    <text x="494" y="141" text-anchor="middle" class="fig-muted" font-size="9.8">(A1) op (A2) → A3，PC 代 A4</text>
    <text x="637" y="141" text-anchor="middle" class="fig-label" font-size="10.5">4 次 (1+2+1)</text>
    <text x="69" y="185" text-anchor="middle" class="fig-label" font-size="12">二地址</text>
    <text x="140" y="185" text-anchor="middle" class="fig-red" font-size="11">OP</text>
    <text x="227" y="185" text-anchor="middle" class="fig-orange" font-size="11">A1(源1 / 目的)</text>
    <text x="345" y="185" text-anchor="middle" class="fig-blue" font-size="11">A2(源2)</text>
    <text x="494" y="185" text-anchor="middle" class="fig-muted" font-size="9.8">(A1) op (A2) → A1（覆盖 A1）</text>
    <text x="637" y="185" text-anchor="middle" class="fig-orange" font-size="10.5">4 / 2 / 1 次*</text>
    <text x="69" y="229" text-anchor="middle" class="fig-label" font-size="12">一地址</text>
    <text x="140" y="229" text-anchor="middle" class="fig-red" font-size="11">OP</text>
    <text x="240" y="229" text-anchor="middle" class="fig-blue" font-size="11">A1(主存地址)</text>
    <text x="359" y="229" text-anchor="middle" class="fig-muted" font-size="10.5">隐含 ACC</text>
    <text x="494" y="229" text-anchor="middle" class="fig-muted" font-size="9.5">(ACC)op(A1)→ACC / (A1)+1→A1</text>
    <text x="637" y="229" text-anchor="middle" class="fig-blue" font-size="10.5">双目2 / 单目3*</text>
    <text x="69" y="273" text-anchor="middle" class="fig-label" font-size="12">零地址</text>
    <text x="140" y="273" text-anchor="middle" class="fig-red" font-size="11">OP</text>
    <text x="286" y="273" text-anchor="middle" class="fig-muted" font-size="10.5">无显式地址（NOP/HLT 或隐含栈顶 TOS）</text>
    <text x="494" y="273" text-anchor="middle" class="fig-muted" font-size="9.8">空操作 / 弹出栈顶两数运算压栈</text>
    <text x="637" y="273" text-anchor="middle" class="fig-purple" font-size="10.5">1 次 / 软栈4次</text>
    <text x="360" y="314" text-anchor="middle" class="fig-label" font-size="9.6">*易错：二地址 SS(存-存) 4 次、RS(寄-存) 2 次、RR(寄-寄) 1 次；一地址主存自增 INC M[A1] 需取指1+读1+写1 = 3 次访存！</text>
  </svg>
  <figcaption>图 5.1：按地址码个数划分的五类指令格式与主存访存次数对比（注意一地址双目与单目自增的访存差异）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w4-fig-2">
  <svg viewBox="0 0 720 244" role="img" aria-label="指令字长与存储字长对齐关系及定长变长指令对比图">
    <defs>
      <marker id="hco-w4-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="434" height="216" rx="8" class="fig-box-subtle"/>
    <rect x="462" y="14" width="242" height="216" rx="8" class="fig-box-subtle"/>
    <line x1="278" y1="76" x2="306" y2="76" class="fig-line" marker-end="url(#hco-w4-arr-2)"/>
    <line x1="278" y1="124" x2="306" y2="124" class="fig-line" marker-end="url(#hco-w4-arr-2)"/>
    <line x1="278" y1="174" x2="306" y2="174" class="fig-line" marker-end="url(#hco-w4-arr-2)"/>
    <rect x="96" y="60" width="88" height="32" rx="4" class="fig-box-green"/>
    <rect x="188" y="60" width="88" height="32" rx="4" class="fig-box-green"/>
    <rect x="308" y="60" width="128" height="32" rx="4" class="fig-box"/>
    <rect x="96" y="108" width="180" height="32" rx="4" class="fig-box-blue"/>
    <rect x="308" y="108" width="128" height="32" rx="4" class="fig-box"/>
    <rect x="96" y="156" width="88" height="36" rx="4" class="fig-box-orange"/>
    <rect x="188" y="156" width="88" height="36" rx="4" class="fig-box-orange"/>
    <rect x="308" y="158" width="128" height="32" rx="4" class="fig-box"/>
    <rect x="476" y="52" width="214" height="74" rx="6" class="fig-box-accent"/>
    <rect x="476" y="138" width="214" height="78" rx="6" class="fig-box"/>
    <text x="30" y="38" class="fig-title" font-size="12.5">指令字长 vs 存储字长（设主存按字节编址、存储字长 32 位 = 4B）</text>
    <text x="60" y="80" text-anchor="middle" class="fig-label" font-size="11">半字长</text>
    <text x="140" y="80" text-anchor="middle" class="fig-green" font-size="10.5">指令 i (2B)</text>
    <text x="232" y="80" text-anchor="middle" class="fig-green" font-size="10.5">指令 i+1 (2B)</text>
    <text x="372" y="80" text-anchor="middle" class="fig-green" font-size="10.5">1 次访存取 2 条</text>
    <text x="60" y="128" text-anchor="middle" class="fig-label" font-size="11">单字长</text>
    <text x="186" y="128" text-anchor="middle" class="fig-blue" font-size="10.5">指令 k (4B，如 MIPS/RISC-V)</text>
    <text x="372" y="128" text-anchor="middle" class="fig-blue" font-size="10.5">1 次访存取 1 条</text>
    <text x="60" y="178" text-anchor="middle" class="fig-label" font-size="11">多字长</text>
    <text x="140" y="178" text-anchor="middle" class="fig-orange" font-size="9.5">指令 m 字1(4B)</text>
    <text x="232" y="178" text-anchor="middle" class="fig-orange" font-size="9.5">指令 m 字2(4B)</text>
    <text x="372" y="178" text-anchor="middle" class="fig-orange" font-size="10.5">需 ≥2 次访存取指</text>
    <text x="233" y="216" text-anchor="middle" class="fig-muted" font-size="10.5">铁律：指令字长必为字节（8 bit）整数倍；高频指令应分配更短字长</text>
    <text x="583" y="38" text-anchor="middle" class="fig-title" font-size="12.5">408 核心概念辨析</text>
    <text x="583" y="73" text-anchor="middle" class="fig-accent" font-size="11.5">定长指令字长 ≠ 定长操作码</text>
    <text x="583" y="93" text-anchor="middle" class="fig-label" font-size="10.5">例：16 位定长指令（字长固定）</text>
    <text x="583" y="111" text-anchor="middle" class="fig-muted" font-size="10.5">仍可用 4/8/12/16 位扩展操作码</text>
    <text x="583" y="159" text-anchor="middle" class="fig-label" font-size="11">定长指令集 (RISC) vs 变长 (CISC)</text>
    <text x="583" y="179" text-anchor="middle" class="fig-muted" font-size="10.5">RISC：固定 32b，PC+4 译码极简</text>
    <text x="583" y="198" text-anchor="middle" class="fig-muted" font-size="10.5">CISC：1~15B 紧凑，跨字取指较慢</text>
  </svg>
  <figcaption>图 5.2：半字长、单字长、多字长指令与存储字长的取指访存关系，以及“定长指令系统”与“定长操作码”辨析</figcaption>
</figure>

> <strong>注意</strong>：“定长指令系统”与“定长操作码”是两个不同概念：前者指<strong>指令长度</strong>固定，后者指<strong>操作码字段长度</strong>固定。

### 5.2 操作码：定长操作码与扩展操作码

操作码指明操作种类，不同指令的编码值不同。按长度是否固定分为：

<strong>定长操作码</strong>：长度与位置都固定，译码简单、硬件简单。若指令系统有 $X$ 条指令、操作码 $n$ 位，则需：

$$
2^n \ge X
$$

例：26 条指令至少需 5 位（$2^5=32$），4 位只能表示 16 条，不够。

<strong>变长操作码</strong>：长度可变、位置不固定，可缩短操作码平均长度，用较短指令字长表示更多指令，并为地址码留出更多位数以扩大寻址空间。常见实现是<strong>扩展操作码技术</strong>：操作码长度随地址码个数减少而增加。

<strong>核心规则</strong>：<strong>短操作码不能是长操作码的前缀</strong>，否则译码时无法区分。

例：16 位定长指令，含 4 位操作码 + 3 个 4 位地址码，可设计为：

| 类型 | 操作码长度 | 可用状态数 | 分配 | 剩余 |
|---|---|---|---|---|
| 三地址 | 4 位 | $2^4=16$ | 15 条，留 `1111` | 1 |
| 二地址 | 8 位 | $1 \times 2^4 = 16$ | 15 条，留 `1111 1111` | 1 |
| 一地址 | 12 位 | $1 \times 2^4 = 16$ | 15 条，留全 1 | 1 |
| 零地址 | 16 位 | $1 \times 2^4 = 16$ | 16 条 | 0 |

若某类指令少留一个状态，下一类可扩展出的状态数就翻倍（如留 2 个三地址状态 → 二地址可用 $2 \times 2^4 = 32$ 个状态）。计算通法：<strong>每让出一条上位指令，就为下级多提供 $2^{k}$ 个状态</strong>（$k$ 为该级地址码位数）。

<figure class="cpd-figure not-content" id="hco-w4-fig-3">
  <svg viewBox="0 0 720 326" role="img" aria-label="16位定长指令扩展操作码前缀树与状态传递极值规律图">
    <defs>
      <marker id="hco-w4-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="444" height="298" rx="8" class="fig-box-subtle"/>
    <rect x="472" y="14" width="232" height="298" rx="8" class="fig-box-subtle"/>
    <line x1="80" y1="96" x2="80" y2="122" class="fig-line-accent" marker-end="url(#hco-w4-arr-3)"/>
    <line x1="184" y1="158" x2="184" y2="184" class="fig-line-accent" marker-end="url(#hco-w4-arr-3)"/>
    <line x1="288" y1="220" x2="288" y2="246" class="fig-line-accent" marker-end="url(#hco-w4-arr-3)"/>
    <rect x="30" y="62" width="100" height="34" rx="4" class="fig-box-red"/>
    <rect x="134" y="62" width="100" height="34" rx="4" class="fig-box-blue"/>
    <rect x="238" y="62" width="100" height="34" rx="4" class="fig-box-blue"/>
    <rect x="342" y="62" width="100" height="34" rx="4" class="fig-box-blue"/>
    <rect x="30" y="124" width="100" height="34" rx="4" class="fig-box-orange"/>
    <rect x="134" y="124" width="100" height="34" rx="4" class="fig-box-red"/>
    <rect x="238" y="124" width="100" height="34" rx="4" class="fig-box-blue"/>
    <rect x="342" y="124" width="100" height="34" rx="4" class="fig-box-blue"/>
    <rect x="30" y="186" width="100" height="34" rx="4" class="fig-box-orange"/>
    <rect x="134" y="186" width="100" height="34" rx="4" class="fig-box-orange"/>
    <rect x="238" y="186" width="100" height="34" rx="4" class="fig-box-red"/>
    <rect x="342" y="186" width="100" height="34" rx="4" class="fig-box-blue"/>
    <rect x="30" y="248" width="100" height="34" rx="4" class="fig-box-orange"/>
    <rect x="134" y="248" width="100" height="34" rx="4" class="fig-box-orange"/>
    <rect x="238" y="248" width="100" height="34" rx="4" class="fig-box-orange"/>
    <rect x="342" y="248" width="100" height="34" rx="4" class="fig-box-green"/>
    <rect x="484" y="50" width="208" height="78" rx="6" class="fig-box"/>
    <rect x="484" y="138" width="208" height="82" rx="6" class="fig-box-accent"/>
    <rect x="484" y="230" width="208" height="70" rx="6" class="fig-box-purple"/>
    <text x="30" y="36" class="fig-title" font-size="12.5">16 位扩展操作码前缀树（每级地址码 k = 4 位）</text>
    <text x="80" y="54" text-anchor="middle" class="fig-muted" font-size="10">15..12 (4b)</text>
    <text x="184" y="54" text-anchor="middle" class="fig-muted" font-size="10">11..8 (4b)</text>
    <text x="288" y="54" text-anchor="middle" class="fig-muted" font-size="10">7..4 (4b)</text>
    <text x="392" y="54" text-anchor="middle" class="fig-muted" font-size="10">3..0 (4b)</text>
    <text x="80" y="83" text-anchor="middle" class="fig-red" font-size="10.5">0000~1110(15)</text>
    <text x="184" y="83" text-anchor="middle" class="fig-blue" font-size="10.5">A1 (三地址)</text>
    <text x="288" y="83" text-anchor="middle" class="fig-blue" font-size="10.5">A2</text>
    <text x="392" y="83" text-anchor="middle" class="fig-blue" font-size="10.5">A3</text>
    <text x="90" y="114" class="fig-accent" font-size="10">留 1 个转义前缀 1111 → 扩展出 1×2⁴ = 16 种状态</text>
    <text x="80" y="145" text-anchor="middle" class="fig-orange" font-size="10.5">1111 (前缀)</text>
    <text x="184" y="145" text-anchor="middle" class="fig-red" font-size="10.5">0000~1110(15)</text>
    <text x="288" y="145" text-anchor="middle" class="fig-blue" font-size="10.5">A1 (二地址)</text>
    <text x="392" y="145" text-anchor="middle" class="fig-blue" font-size="10.5">A2</text>
    <text x="194" y="176" class="fig-accent" font-size="10">留 1111 1111 → 扩展出 1×2⁴ = 16 种状态</text>
    <text x="80" y="207" text-anchor="middle" class="fig-orange" font-size="10.5">1111</text>
    <text x="184" y="207" text-anchor="middle" class="fig-orange" font-size="10.5">1111</text>
    <text x="288" y="207" text-anchor="middle" class="fig-red" font-size="10.5">0000~1110(15)</text>
    <text x="392" y="207" text-anchor="middle" class="fig-blue" font-size="10.5">A1 (一地址)</text>
    <text x="298" y="238" class="fig-accent" font-size="10">留全 1 → 零地址 16 条</text>
    <text x="80" y="269" text-anchor="middle" class="fig-orange" font-size="10.5">1111</text>
    <text x="184" y="269" text-anchor="middle" class="fig-orange" font-size="10.5">1111</text>
    <text x="288" y="269" text-anchor="middle" class="fig-orange" font-size="10.5">1111</text>
    <text x="392" y="269" text-anchor="middle" class="fig-green" font-size="10.5">0000~1111(16)</text>
    <text x="238" y="301" text-anchor="middle" class="fig-muted" font-size="10">无前缀歧义铁律：短操作码绝不能是长操作码的前缀（1111 不可再表示三地址）</text>
    <text x="588" y="36" text-anchor="middle" class="fig-title" font-size="12.5">状态传递与极值计算</text>
    <text x="588" y="70" text-anchor="middle" class="fig-label" font-size="11">递推公式（地址位宽 k）</text>
    <text x="588" y="90" text-anchor="middle" class="fig-blue" font-size="10.5">本级可用 = 上级剩余 × 2^k</text>
    <text x="588" y="109" text-anchor="middle" class="fig-muted" font-size="10">本级剩余 = 本级可用 − 本级已排</text>
    <text x="588" y="158" text-anchor="middle" class="fig-accent" font-size="11">★ 最大指令条数规律</text>
    <text x="588" y="178" text-anchor="middle" class="fig-label" font-size="10.5">上级少排 1 条 → 下级多 2^k 条</text>
    <text x="588" y="198" text-anchor="middle" class="fig-orange" font-size="10">净增 (2^k − 1) 条，故上级尽量取 1</text>
    <text x="588" y="250" text-anchor="middle" class="fig-purple" font-size="11">编码优化与本例总数</text>
    <text x="588" y="270" text-anchor="middle" class="fig-muted" font-size="10">高频指令用短 OP（缩短平均译码）</text>
    <text x="588" y="288" text-anchor="middle" class="fig-green" font-size="10.5">本例共 15+15+15+16 = 61 条</text>
  </svg>
  <figcaption>图 5.3：16 位指令扩展操作码前缀树（1111 逐级转义扩展）与最大指令条数递推规律</figcaption>
</figure>

> <strong>★考点</strong>：扩展操作码计算题先定“每级地址码位数 $k$”，再用“上级剩余状态数 × $2^k$”求本级可用状态；若题目给定各级指令条数，用“可用 − 已用 = 剩余”逐级往下算。

### 5.3 指令寻址方式

<strong>顺序寻址</strong>：指令在存储器中顺序存放，多数情况下顺序执行。取下一条指令的地址由 PC 自动加上<strong>本条指令的字节数</strong>得到：

$$
PC \leftarrow PC + \text{instr length (bytes)}
$$

例：8 位机、指令字长 8 位（1 字节），PC 每次加 1；16 位机、指令字长 16 位（2 字节），PC 每次加 2。习惯上说“PC 自动加一”，其中的“一”实际是<strong>以字节为单位的指令字长</strong>。

<strong>跳跃寻址</strong>：执行分支/跳转指令时改变执行顺序。下一条指令地址不来自 PC 自增，而由本条指令（及测试条件）决定：

- 无条件跳转：直接把目标地址送入 PC；
- 条件转移：由状态标志（CF、ZF、SF、OF 等）决定是否转移。

<figure class="cpd-figure not-content" id="hco-w4-fig-4">
  <svg viewBox="0 0 720 272" role="img" aria-label="指令寻址方式：顺序寻址PC自增字节数与跳跃寻址分支控制流对比图">
    <defs>
      <marker id="hco-w4-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="336" height="244" rx="8" class="fig-box-subtle"/>
    <rect x="364" y="14" width="340" height="244" rx="8" class="fig-box-subtle"/>
    <path d="M 222 71 L 254 71 L 254 113 L 224 113" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w4-arr-4)"/>
    <path d="M 222 113 L 254 113 L 254 155 L 224 155" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w4-arr-4)"/>
    <path d="M 222 155 L 254 155 L 254 197 L 224 197" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w4-arr-4)"/>
    <line x1="491" y1="86" x2="491" y2="96" class="fig-line" marker-end="url(#hco-w4-arr-4)"/>
    <line x1="491" y1="128" x2="491" y2="140" class="fig-line-dashed" marker-end="url(#hco-w4-arr-4)"/>
    <path d="M 556 112 L 580 112 L 580 198 L 558 198" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w4-arr-4)"/>
    <line x1="594" y1="154" x2="580" y2="154" class="fig-line-dashed"/>
    <rect x="88" y="55" width="134" height="32" rx="4" class="fig-box-blue"/>
    <rect x="88" y="97" width="134" height="32" rx="4" class="fig-box-blue"/>
    <rect x="88" y="139" width="134" height="32" rx="4" class="fig-box-blue"/>
    <rect x="88" y="181" width="134" height="32" rx="4" class="fig-box"/>
    <rect x="426" y="54" width="130" height="32" rx="4" class="fig-box"/>
    <rect x="426" y="96" width="130" height="32" rx="4" class="fig-box-orange"/>
    <rect x="426" y="142" width="130" height="30" rx="4" class="fig-box-dashed"/>
    <rect x="426" y="182" width="130" height="32" rx="4" class="fig-box-green"/>
    <rect x="594" y="124" width="98" height="60" rx="5" class="fig-box-purple"/>
    <text x="30" y="38" class="fig-title" font-size="12.5">① 顺序寻址：PC ← (PC) + 指令字节数</text>
    <text x="80" y="75" text-anchor="end" class="fig-muted" font-size="10.5">0x1000</text>
    <text x="80" y="117" text-anchor="end" class="fig-muted" font-size="10.5">0x1004</text>
    <text x="80" y="159" text-anchor="end" class="fig-muted" font-size="10.5">0x1008</text>
    <text x="80" y="201" text-anchor="end" class="fig-muted" font-size="10.5">0x100C</text>
    <text x="155" y="75" text-anchor="middle" class="fig-blue" font-size="10.5">ADD R1, R2 (4B)</text>
    <text x="155" y="117" text-anchor="middle" class="fig-blue" font-size="10.5">SUB R3, R1 (4B)</text>
    <text x="155" y="159" text-anchor="middle" class="fig-blue" font-size="10.5">AND R4, R3 (4B)</text>
    <text x="155" y="201" text-anchor="middle" class="fig-label" font-size="10.5">OR R5, R4 (4B)</text>
    <text x="262" y="96" class="fig-blue" font-size="10.5">PC + 4</text>
    <text x="262" y="138" class="fig-blue" font-size="10.5">PC + 4</text>
    <text x="262" y="180" class="fig-blue" font-size="10.5">PC + 4</text>
    <text x="184" y="242" text-anchor="middle" class="fig-muted" font-size="10">★“PC+1”指加 1 条指令字长（按字节编址 32 位定长指令即 +4）</text>
    <text x="378" y="38" class="fig-title" font-size="12.5">② 跳跃寻址：由转移指令重写 PC</text>
    <text x="418" y="74" text-anchor="end" class="fig-muted" font-size="10.5">0x2000</text>
    <text x="418" y="116" text-anchor="end" class="fig-muted" font-size="10.5">0x2004</text>
    <text x="418" y="161" text-anchor="end" class="fig-muted" font-size="10.5">0x2008</text>
    <text x="418" y="202" text-anchor="end" class="fig-muted" font-size="10.5">0x2010</text>
    <text x="491" y="74" text-anchor="middle" class="fig-label" font-size="10.5">CMP R1, R2</text>
    <text x="491" y="116" text-anchor="middle" class="fig-orange" font-size="10.5">JE 0x2010 (分支)</text>
    <text x="491" y="161" text-anchor="middle" class="fig-muted" font-size="10.5">顺序下条 (ZF=0)</text>
    <text x="491" y="202" text-anchor="middle" class="fig-green" font-size="9.6">目标指令 (PC←0x2010)</text>
    <text x="643" y="106" text-anchor="middle" class="fig-orange" font-size="10">ZF=1 跳转成立</text>
    <text x="643" y="146" text-anchor="middle" class="fig-purple" font-size="10.5">PSW 状态标志</text>
    <text x="643" y="166" text-anchor="middle" class="fig-muted" font-size="10">CF/ZF/SF/OF</text>
    <text x="534" y="242" text-anchor="middle" class="fig-muted" font-size="10">无条件跳转直接写 PC；条件转移依标志位选顺序或目标地址</text>
  </svg>
  <figcaption>图 5.4：指令寻址方式对比——按字节编址的顺序寻址（PC 自增指令字节数）与标志位控制的跳跃寻址</figcaption>
</figure>

### 5.4 操作数寻址方式

含寻址方式的单地址指令格式把地址码字段分为<strong>寻址方式字段 M</strong>（特征码）与<strong>形式地址字段 D</strong>；寻址过程就是把 $(M, D)$ 转换为<strong>有效地址 EA</strong> 的过程。设形式地址为 $D$，寄存器内容为 $(R)$，主存单元内容为 $(D)$。

| 方式 | 特征码示例 | 有效地址 / 操作数 | 访存次数 | 特点与用途 |
|---|---|---|---|---|
| 立即寻址 | 000 | 操作数 = $D$ 本身 | 0（随指令取出） | 最快；$D$ 位宽限制数值范围；用于赋初值 |
| 直接寻址 | 001 | $EA = D$ | 1 | 地址直观、无需计算；范围受 $D$ 位宽限制，地址在指令中不便修改 |
| 间接寻址 | 011 | $EA = (D)$ | 2 | 短形式地址可访问大空间；改地址只需改存储单元；速度慢 |
| 寄存器寻址 | 010 | 操作数 = $(R)$ | 0 | 不访存、速度快；寄存器少，$D$ 位数少，利于缩短指令 |
| 寄存器间接寻址 | 100 | $EA = (R)$ | 1 | 兼具间接寻址扩大范围与少访存的优点；寄存器数量有限 |
| 相对寻址 | 101 | $EA = (PC) + D$ | 1 | $D$ 为补码偏移（可正可负）；便于代码浮动、程序跳转 |
| 变址寻址 | 110 | EA = R变 + D | 1 | $R$ 内容用户可改、$D$ 固定；面向用户，用于数组/循环 |
| 基址寻址 | 111 | EA = R基 + D | 1 | $R$ 由操作系统设定、$D$ 用户可改；面向系统，用于重定位 |
| 堆栈寻址 | — | 由 SP 指向栈顶 | 1 | 后进先出；入栈 SP 减、出栈 SP 加（以数据宽度为单位） |

<strong>要点辨析</strong>：

- <strong>立即寻址</strong>的操作数与指令一起存放在主存中，取指时一并送入 IR，执行时直接用；
- <strong>间接寻址</strong>的形式地址是“操作数地址的地址”，可用较短的形式地址访问较大空间（如 16 位形式地址指向 32 位有效地址，寻址范围由 64K 扩到 4G）；
- <strong>相对、变址、基址</strong>统称<strong>偏移寻址</strong>，都是“某寄存器内容 + 形式地址”：
  - 相对：寄存器是 <strong>PC</strong>，指令中不显式给出；取指后 PC 已指向<strong>下一条指令</strong>，故 EA = 下条指令地址 + D；
  - 变址：寄存器由指令中的寄存器编号给出，<strong>内容可变</strong>，形式地址固定；
  - 基址：寄存器同样由编号给出，<strong>内容固定</strong>，形式地址可变。
- <strong>堆栈</strong>：存储器堆栈用 SP 记录栈顶；栈向低地址增长时，入栈先 SP−1 再存数，出栈先取数再 SP+1。寄存器堆栈（硬堆栈）成本高、容量小；存储器堆栈（软堆栈）常用。

<strong>复合寻址</strong>（CISC 常见）：相对 + 间接（先相对得存放有效地址的单元地址，再间接取有效地址）、变址 + 间接、间接 + 变址（先间接取得偏移量，再变址）。

<figure class="cpd-figure not-content" id="hco-w4-fig-5">
  <svg viewBox="0 0 720 356" role="img" aria-label="六种基本操作数寻址方式硬件数据通路与取数访存次数对比图">
    <defs>
      <marker id="hco-w4-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="336" height="102" rx="8" class="fig-box-subtle"/>
    <rect x="368" y="14" width="336" height="102" rx="8" class="fig-box-subtle"/>
    <rect x="16" y="128" width="336" height="102" rx="8" class="fig-box-subtle"/>
    <rect x="368" y="128" width="336" height="102" rx="8" class="fig-box-subtle"/>
    <rect x="16" y="242" width="336" height="100" rx="8" class="fig-box-subtle"/>
    <rect x="368" y="242" width="336" height="100" rx="8" class="fig-box-subtle"/>
    <line x1="192" y1="63" x2="222" y2="63" class="fig-line-accent" marker-end="url(#hco-w4-arr-5)"/>
    <line x1="492" y1="63" x2="532" y2="63" class="fig-line" marker-end="url(#hco-w4-arr-5)"/>
    <line x1="106" y1="177" x2="128" y2="177" class="fig-line" marker-end="url(#hco-w4-arr-5)"/>
    <line x1="220" y1="177" x2="242" y2="177" class="fig-line" marker-end="url(#hco-w4-arr-5)"/>
    <line x1="486" y1="177" x2="526" y2="177" class="fig-line-accent" marker-end="url(#hco-w4-arr-5)"/>
    <line x1="110" y1="291" x2="130" y2="291" class="fig-line" marker-end="url(#hco-w4-arr-5)"/>
    <line x1="220" y1="291" x2="242" y2="291" class="fig-line" marker-end="url(#hco-w4-arr-5)"/>
    <rect x="28" y="48" width="54" height="30" rx="4" class="fig-box"/>
    <rect x="84" y="48" width="108" height="30" rx="4" class="fig-box-green"/>
    <rect x="224" y="48" width="114" height="30" rx="4" class="fig-box-accent"/>
    <rect x="380" y="48" width="112" height="30" rx="4" class="fig-box-blue"/>
    <rect x="534" y="48" width="156" height="30" rx="4" class="fig-box-green"/>
    <rect x="28" y="162" width="78" height="30" rx="4" class="fig-box-blue"/>
    <rect x="130" y="162" width="90" height="30" rx="4" class="fig-box-orange"/>
    <rect x="244" y="162" width="96" height="30" rx="4" class="fig-box-green"/>
    <rect x="380" y="162" width="106" height="30" rx="4" class="fig-box-purple"/>
    <rect x="528" y="162" width="162" height="30" rx="4" class="fig-box-green"/>
    <rect x="28" y="276" width="82" height="30" rx="4" class="fig-box-purple"/>
    <rect x="132" y="276" width="88" height="30" rx="4" class="fig-box-orange"/>
    <rect x="244" y="276" width="96" height="30" rx="4" class="fig-box-green"/>
    <rect x="380" y="274" width="150" height="32" rx="4" class="fig-box-purple"/>
    <rect x="540" y="274" width="150" height="32" rx="4" class="fig-box-blue"/>
    <text x="28" y="36" class="fig-title" font-size="11.5">① 立即寻址（操作数 = D）· 0 次访存</text>
    <text x="55" y="67" text-anchor="middle" class="fig-label" font-size="10.5">OP|#</text>
    <text x="138" y="67" text-anchor="middle" class="fig-green" font-size="10.5">D (立即数本身)</text>
    <text x="281" y="67" text-anchor="middle" class="fig-accent" font-size="10.5">直达 ALU 赋初值</text>
    <text x="184" y="102" text-anchor="middle" class="fig-muted" font-size="10">随取指一并入 IR，最快；数值范围受 D 位数限制</text>
    <text x="380" y="36" class="fig-title" font-size="11.5">② 直接寻址（EA = D）· 1 次访存</text>
    <text x="436" y="67" text-anchor="middle" class="fig-blue" font-size="10.5">IR: OP | D</text>
    <text x="612" y="67" text-anchor="middle" class="fig-green" font-size="10.5">主存 M[D] = 操作数</text>
    <text x="536" y="102" text-anchor="middle" class="fig-muted" font-size="10">无需计算有效地址；寻址空间受限于 D 字段长度</text>
    <text x="28" y="150" class="fig-title" font-size="11.5">③ 间接寻址（EA = (D)）· 2 次访存</text>
    <text x="67" y="181" text-anchor="middle" class="fig-blue" font-size="10.5">OP | @D</text>
    <text x="175" y="181" text-anchor="middle" class="fig-orange" font-size="10.5">M[D] = EA</text>
    <text x="292" y="181" text-anchor="middle" class="fig-green" font-size="10.5">M[EA] = 数</text>
    <text x="184" y="216" text-anchor="middle" class="fig-muted" font-size="10">短形式地址可指 32/64 位大指针；取数需 2 次访存</text>
    <text x="380" y="150" class="fig-title" font-size="11.5">④ 寄存器寻址（操作数 = (Ri)）· 0 次访存</text>
    <text x="433" y="181" text-anchor="middle" class="fig-purple" font-size="10.5">IR: OP | Ri</text>
    <text x="609" y="181" text-anchor="middle" class="fig-green" font-size="10.5">寄存器组 R[i] = 操作数</text>
    <text x="536" y="216" text-anchor="middle" class="fig-muted" font-size="10">执行期不访存极快；寄存器号短，有效压缩指令字长</text>
    <text x="28" y="264" class="fig-title" font-size="11.5">⑤ 寄存器间接（EA = (Ri)）· 1 次访存</text>
    <text x="69" y="295" text-anchor="middle" class="fig-purple" font-size="10.5">OP | (Ri)</text>
    <text x="176" y="295" text-anchor="middle" class="fig-orange" font-size="10.5">R[i] = EA</text>
    <text x="292" y="295" text-anchor="middle" class="fig-green" font-size="10.5">M[EA] = 数</text>
    <text x="184" y="328" text-anchor="middle" class="fig-muted" font-size="10">兼具大寻址范围与仅 1 次访存优点（C 语言指针 *p）</text>
    <text x="380" y="264" class="fig-title" font-size="11.5">⑥ 堆栈寻址（隐含 SP，低地址生长）· 软栈 1 次</text>
    <text x="455" y="294" text-anchor="middle" class="fig-purple" font-size="10">PUSH: SP−4 → 写 M[SP]</text>
    <text x="615" y="294" text-anchor="middle" class="fig-blue" font-size="10">POP: 读 M[SP] → SP+4</text>
    <text x="536" y="328" text-anchor="middle" class="fig-muted" font-size="10">硬堆栈不访存；主存软堆栈每次入栈/出栈访存 1 次</text>
  </svg>
  <figcaption>图 5.5：六类基础操作数寻址方式的硬件寻址路径与执行阶段访存次数（均不含取指访存）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w4-fig-6">
  <svg viewBox="0 0 720 304" role="img" aria-label="相对寻址PC自增偏移计算实例与基址变址寻址对比图">
    <defs>
      <marker id="hco-w4-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="350" height="276" rx="8" class="fig-box-subtle"/>
    <rect x="378" y="14" width="326" height="276" rx="8" class="fig-box-subtle"/>
    <path d="M 212 111 L 236 111 L 236 150 L 214 150" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#hco-w4-arr-6)"/>
    <path d="M 212 156 L 348 156 L 348 68 L 214 68" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#hco-w4-arr-6)"/>
    <rect x="80" y="54" width="132" height="28" rx="4" class="fig-box-green"/>
    <rect x="80" y="96" width="132" height="30" rx="4" class="fig-box-orange"/>
    <rect x="80" y="138" width="132" height="28" rx="4" class="fig-box-blue"/>
    <rect x="28" y="182" width="326" height="96" rx="6" class="fig-box"/>
    <rect x="392" y="50" width="298" height="108" rx="6" class="fig-box-purple"/>
    <rect x="392" y="170" width="298" height="108" rx="6" class="fig-box-blue"/>
    <text x="28" y="38" class="fig-title" font-size="12">★ 相对寻址：EA = (PC) + D（PC 已自增！）</text>
    <text x="72" y="72" text-anchor="end" class="fig-muted" font-size="10">0x1FF8</text>
    <text x="72" y="115" text-anchor="end" class="fig-muted" font-size="10">0x2000</text>
    <text x="72" y="156" text-anchor="end" class="fig-muted" font-size="10">0x2004</text>
    <text x="146" y="72" text-anchor="middle" class="fig-green" font-size="10">LOOP: 目标指令</text>
    <text x="146" y="115" text-anchor="middle" class="fig-orange" font-size="10">JMP D=−12 (长4B)</text>
    <text x="146" y="156" text-anchor="middle" class="fig-blue" font-size="10">下条指令 (新 PC)</text>
    <text x="242" y="136" class="fig-blue" font-size="10">① 取指后 PC+4</text>
    <text x="282" y="61" text-anchor="middle" class="fig-orange" font-size="10">② 0x2004 + (−12)</text>
    <text x="191" y="202" text-anchor="middle" class="fig-label" font-size="10.5">EA = (当前指令首址 + 本条字节数 L) + 补码偏移 D</text>
    <text x="191" y="222" text-anchor="middle" class="fig-orange" font-size="10.5">实例：EA = (0x2000 + 4) + (−12) = 0x1FF8</text>
    <text x="191" y="242" text-anchor="middle" class="fig-muted" font-size="10">反求偏移：D = 目标地址 − (当前地址 + L)，支持代码浮动</text>
    <text x="191" y="262" text-anchor="middle" class="fig-red" font-size="10">易错点：切勿直接用当前指令地址 0x2000 加 D！</text>
    <text x="392" y="38" class="fig-title" font-size="12">基址寻址 vs 变址寻址（谁可变 · 面向谁）</text>
    <text x="541" y="71" text-anchor="middle" class="fig-purple" font-size="11">基址寻址：EA = (BR) + D 【面向系统】</text>
    <text x="404" y="93" class="fig-label" font-size="10">• 基址寄存器 BR：内容不变（OS 特权装入程序基址）</text>
    <text x="404" y="113" class="fig-label" font-size="10">• 形式地址 D：可变（不同变量在段内偏移量不同）</text>
    <text x="404" y="133" class="fig-muted" font-size="10">• 典型场景：多道程序整段动态重定位、扩充寻址</text>
    <text x="541" y="191" text-anchor="middle" class="fig-blue" font-size="11">变址寻址：EA = (IX) + D 【面向用户】</text>
    <text x="404" y="213" class="fig-label" font-size="10">• 变址寄存器 IX：内容可变（随循环下标 i×4 自增）</text>
    <text x="404" y="233" class="fig-label" font-size="10">• 形式地址 D：固定不变（指令内写死数组首址 &amp;A[0]）</text>
    <text x="404" y="253" class="fig-muted" font-size="10">• 典型场景：数组元素循环遍历、字符串批处理</text>
  </svg>
  <figcaption>图 5.6：三大偏移寻址机制剖析——相对寻址“先自增 PC 再加补码偏移”实例与基址/变址对比</figcaption>
</figure>

> <strong>★考点</strong>：变址与基址的唯一区别是“谁可变”——变址<strong>寄存器内容可变、形式地址固定</strong>（面向用户，数组循环）；基址<strong>寄存器内容固定、形式地址可变</strong>（面向系统，程序重定位）。
>
> <strong>注意</strong>：间接寻址的“两次访存”指取操作数需两次（先取 EA 再取数），不含取指令本身。

### 5.5 指令的操作类型与 CISC/RISC

不同架构的指令系统差异很大，但基本操作类型相似：

| 类型 | 内容 | 说明 |
|---|---|---|
| 数据传送 | MOVE / LOAD / STORE | 寄存器↔寄存器、寄存器↔存储器；可按字节/字/双字，有的支持成组传送 |
| 算术与逻辑 | 加、减、乘、除；与、或、非、异或 | 有的仅支持定点加减、比较、求补；有的支持浮点、十进制、乘方开方；大型机还支持向量/矩阵运算 |
| 移位 | 逻辑移位、算术移位、循环移位 | 逻辑移位对象为无符号数；算术移位对象为有符号数；循环移位分不带 CF 的“小循环”与带 CF 的“大循环” |
| 程序控制 | 转移、循环控制、子程序调用与返回 | 见下 |
| 输入输出 | I/O 指令 | 主机向外设发控制命令、读外设状态；统一编址时可用访存指令代替 |
| 其他 | 停机、等待、空操作、开/关中断、特权指令、多处理机指令 | 特权指令面向操作系统，不直接提供给用户 |

<strong>程序控制类</strong>：

- <strong>转移</strong>：无条件转移与条件转移；条件转移依据状态标志寄存器（CF 进位/借位、ZF 零、SF 符号、OF 溢出），这些标志由前一条指令设置。
- <strong>循环控制</strong>：修改循环变量、条件测试、地址转移，功能更强的转移指令。
- <strong>子程序调用与返回</strong>：调用指令给出入口地址，并把<strong>断点（调用指令的下一条指令地址）压栈保护；返回指令从栈中弹出断点送入 PC。与转移指令的区别：条件转移需满足条件、同一程序内转移、不返回；调用/返回一般无条件、跨程序转移、必须保护断点并返回</strong>。

<figure class="cpd-figure not-content" id="hco-w4-fig-7">
  <svg viewBox="0 0 720 318" role="img" aria-label="移位指令三大家族规则与CMP条件转移标志位判定机制图">
    <defs>
      <marker id="hco-w4-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="688" height="150" rx="8" class="fig-box-subtle"/>
    <rect x="16" y="176" width="688" height="128" rx="8" class="fig-box-subtle"/>
    <line x1="170" y1="252" x2="194" y2="252" class="fig-line" marker-end="url(#hco-w4-arr-7)"/>
    <path d="M 390 232 L 414 232" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w4-arr-7)"/>
    <path d="M 390 274 L 414 274" class="fig-line-accent" marker-end="url(#hco-w4-arr-7)"/>
    <rect x="28" y="46" width="214" height="106" rx="6" class="fig-box"/>
    <rect x="253" y="46" width="214" height="106" rx="6" class="fig-box-orange"/>
    <rect x="478" y="46" width="214" height="106" rx="6" class="fig-box-purple"/>
    <rect x="28" y="214" width="142" height="76" rx="6" class="fig-box-blue"/>
    <rect x="196" y="214" width="194" height="76" rx="6" class="fig-box"/>
    <rect x="416" y="210" width="276" height="38" rx="5" class="fig-box-green"/>
    <rect x="416" y="256" width="276" height="38" rx="5" class="fig-box-accent"/>
    <text x="28" y="36" class="fig-title" font-size="12.5">移位操作三大家族（无符号逻辑移位 vs 有符号算术移位 vs 大小循环移位）</text>
    <text x="135" y="66" text-anchor="middle" class="fig-label" font-size="11">逻辑移位 SHL / SHR</text>
    <text x="38" y="88" class="fig-muted" font-size="10">• 适用无符号数：空位一律补 0</text>
    <text x="38" y="108" class="fig-muted" font-size="10">• 左移 ×2（最高位进 CF）</text>
    <text x="38" y="128" class="fig-muted" font-size="10">• 右移 ÷2（最低位进 CF）</text>
    <text x="360" y="66" text-anchor="middle" class="fig-orange" font-size="11">算术移位 SAL / SAR</text>
    <text x="263" y="88" class="fig-label" font-size="10">• 有符号补码：右移高位补符号位 S</text>
    <text x="263" y="108" class="fig-label" font-size="10">• 左移低位补 0；前后符号不同则溢</text>
    <text x="263" y="128" class="fig-orange" font-size="10">• 硬件置 OF = 1（有符号溢出！）</text>
    <text x="585" y="66" text-anchor="middle" class="fig-purple" font-size="11">循环移位 ROL/ROR · RCL/RCR</text>
    <text x="488" y="88" class="fig-label" font-size="10">• 小循环(不带CF)：n 位首尾闭环</text>
    <text x="488" y="108" class="fig-label" font-size="10">• 大循环(带CF)：连同 CF 共 n+1 位</text>
    <text x="488" y="128" class="fig-muted" font-size="10">• 用途：多字节级联移位与位拼接</text>
    <text x="28" y="198" class="fig-title" font-size="12.5">CMP 比较指令与条件转移标志位分流（硬件同一加法器同时生成 4 标志）</text>
    <text x="99" y="236" text-anchor="middle" class="fig-blue" font-size="11">CMP A, B 本质</text>
    <text x="99" y="256" text-anchor="middle" class="fig-label" font-size="10">执行减法 A − B</text>
    <text x="99" y="276" text-anchor="middle" class="fig-muted" font-size="9.5">不存差值，仅置标志</text>
    <text x="293" y="236" text-anchor="middle" class="fig-label" font-size="10.5">PSW 状态标志寄存器</text>
    <text x="293" y="256" text-anchor="middle" class="fig-muted" font-size="10">ZF(零) · SF(负) · OF(溢出)</text>
    <text x="293" y="276" text-anchor="middle" class="fig-orange" font-size="10">减法借位 CF = Cout ⊕ 1</text>
    <text x="554" y="233" text-anchor="middle" class="fig-green" font-size="9.2">无符号 JA/JB：查 CF, ZF (A&gt;B: CF=0, ZF=0)</text>
    <text x="554" y="279" text-anchor="middle" class="fig-accent" font-size="9.2">有符号 JG/JL：查 SF⊕OF, ZF (A&gt;B: SF=OF, ZF=0)</text>
  </svg>
  <figcaption>图 5.7：逻辑/算术/循环移位规则对比，以及 CMP 减法置标志后无符号（JA/JB）与有符号（JG/JL）条件转移判据</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w4-fig-8">
  <svg viewBox="0 0 720 330" role="img" aria-label="x86 Intel与AT&amp;T汇编语法对比及CALL与RET过程调用栈帧布局图">
    <defs>
      <marker id="hco-w4-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="14" width="326" height="302" rx="8" class="fig-box-subtle"/>
    <rect x="354" y="14" width="350" height="302" rx="8" class="fig-box-subtle"/>
    <line x1="402" y1="134" x2="426" y2="134" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w4-arr-8)"/>
    <line x1="402" y1="202" x2="426" y2="202" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w4-arr-8)"/>
    <line x1="654" y1="78" x2="654" y2="190" class="fig-line-dashed" marker-end="url(#hco-w4-arr-8)"/>
    <rect x="28" y="48" width="302" height="82" rx="6" class="fig-box-blue"/>
    <rect x="28" y="140" width="302" height="82" rx="6" class="fig-box-purple"/>
    <rect x="28" y="232" width="302" height="72" rx="6" class="fig-box"/>
    <rect x="428" y="52" width="182" height="30" rx="4" class="fig-box"/>
    <rect x="428" y="84" width="182" height="32" rx="4" class="fig-box-red"/>
    <rect x="428" y="118" width="182" height="32" rx="4" class="fig-box-purple"/>
    <rect x="428" y="152" width="182" height="32" rx="4" class="fig-box-blue"/>
    <rect x="428" y="186" width="182" height="32" rx="4" class="fig-box-green"/>
    <rect x="366" y="230" width="326" height="74" rx="6" class="fig-box-orange"/>
    <text x="28" y="38" class="fig-title" font-size="12">x86 汇编双语法对照（Intel vs AT&amp;T）</text>
    <text x="179" y="69" text-anchor="middle" class="fig-blue" font-size="11">Intel 格式：OP 目的 dst, 源 src</text>
    <text x="179" y="91" text-anchor="middle" class="fig-label" font-size="10">mov eax, dword ptr [ebx + ecx*4 + 8]</text>
    <text x="179" y="112" text-anchor="middle" class="fig-muted" font-size="9.5">无寄存器/立即数前缀，以 ptr 标明访存宽度</text>
    <text x="179" y="161" text-anchor="middle" class="fig-purple" font-size="11">AT&amp;T 格式：OP后缀 源 src, 目的 dst</text>
    <text x="179" y="183" text-anchor="middle" class="fig-label" font-size="10">movl 8(%ebx, %ecx, 4), %eax</text>
    <text x="179" y="204" text-anchor="middle" class="fig-muted" font-size="9.5">%寄存器 · $立即数 · 后缀 b(1B)/w(2B)/l(4B)/q(8B)</text>
    <text x="179" y="252" text-anchor="middle" class="fig-label" font-size="10.5">★ 408 真题速判：见 % / $ 即 AT&amp;T（左源 → 右目的）</text>
    <text x="179" y="272" text-anchor="middle" class="fig-muted" font-size="10">复合寻址 EA = 基址 + 变址 × 比例因子(1/2/4/8) + 位移</text>
    <text x="179" y="291" text-anchor="middle" class="fig-muted" font-size="9.5">subl %eax, %edx 表示 (EDX) − (EAX) → EDX！</text>
    <text x="366" y="38" class="fig-title" font-size="12">子程序 CALL / RET 栈帧布局（EBP / ESP）</text>
    <text x="396" y="138" text-anchor="end" class="fig-purple" font-size="10.5">EBP</text>
    <text x="396" y="206" text-anchor="end" class="fig-green" font-size="10.5">ESP</text>
    <text x="519" y="71" text-anchor="middle" class="fig-muted" font-size="10">[ebp + 8] 调用者实参 arg1</text>
    <text x="519" y="104" text-anchor="middle" class="fig-red" font-size="10">[ebp + 4] 返回地址 (断点 PC)</text>
    <text x="519" y="138" text-anchor="middle" class="fig-purple" font-size="10">[ebp] 旧 EBP (调用者帧基址)</text>
    <text x="519" y="172" text-anchor="middle" class="fig-blue" font-size="10">[ebp − 4] 被调函数局部变量</text>
    <text x="519" y="206" text-anchor="middle" class="fig-green" font-size="10">[esp] 当前栈顶 / 现场保存</text>
    <text x="654" y="68" text-anchor="middle" class="fig-muted" font-size="9.5">高地址</text>
    <text x="654" y="208" text-anchor="middle" class="fig-muted" font-size="9.5">低地址生长</text>
    <text x="529" y="250" text-anchor="middle" class="fig-orange" font-size="10">CALL P：ESP−4 压入下条指令断点 PC → 跳转至 P</text>
    <text x="529" y="270" text-anchor="middle" class="fig-label" font-size="10">建帧：push ebp; mov ebp, esp | 拆帧：leave (mov+pop)</text>
    <text x="529" y="290" text-anchor="middle" class="fig-orange" font-size="10">RET：从栈顶弹出断点送回 PC，ESP+4 恢复调用流</text>
  </svg>
  <figcaption>图 5.8：x86 汇编 Intel 与 AT&amp;T 语法对照，以及子程序调用（CALL 压入断点 PC、EBP/ESP 维护栈帧、RET 弹出返回）机制</figcaption>
</figure>

<strong>CISC 与 RISC</strong>：

| 维度 | CISC（复杂指令集） | RISC（精简指令集） |
|---|---|---|
| 指令数量 | 多，功能复杂 | 少而精，常用指令 |
| 指令字长 | <strong>变长</strong> | <strong>定长</strong> |
| 寻址方式 | 多，支持<strong>复合寻址</strong> | 少，简单 |
| 访存指令 | 多数指令可直接访存 | 一般只有 LOAD/STORE 访存 |
| 通用寄存器 | 较少 | 较多 |
| 控制器 | 多为微程序控制 | 多为硬布线控制 |
| 流水线 | 难实现 | 易于流水 |
| 典型 | x86 | MIPS、RISC-V |

<figure class="cpd-figure not-content" id="hco-w4-fig-9">
  <svg viewBox="0 0 720 302" role="img" aria-label="CISC复杂指令集与RISC精简指令集体系结构及流水线特性对比图">
    <rect x="16" y="14" width="336" height="274" rx="8" class="fig-box-subtle"/>
    <rect x="368" y="14" width="336" height="274" rx="8" class="fig-box-subtle"/>
    <rect x="28" y="48" width="312" height="62" rx="5" class="fig-box-orange"/>
    <rect x="28" y="120" width="312" height="66" rx="5" class="fig-box"/>
    <rect x="28" y="196" width="312" height="78" rx="5" class="fig-box-purple"/>
    <rect x="380" y="48" width="312" height="62" rx="5" class="fig-box-blue"/>
    <rect x="380" y="120" width="312" height="66" rx="5" class="fig-box-green"/>
    <rect x="380" y="196" width="312" height="78" rx="5" class="fig-box-accent"/>
    <text x="184" y="37" text-anchor="middle" class="fig-title" font-size="12.5">CISC 复杂指令集（典型：x86）</text>
    <text x="184" y="69" text-anchor="middle" class="fig-orange" font-size="11">① 变长指令字长（1~15B）+ 复合寻址</text>
    <text x="184" y="92" text-anchor="middle" class="fig-muted" font-size="9.3">[1B: push] [3B: add] [7B: mov [ebx+ecx*4+8], imm]</text>
    <text x="184" y="142" text-anchor="middle" class="fig-label" font-size="11">② 运算指令可直接访存（通用寄存器少）</text>
    <text x="184" y="165" text-anchor="middle" class="fig-muted" font-size="10">支持 ADD M[A], R1 等主存直算，单条指令耗时不均</text>
    <text x="184" y="218" text-anchor="middle" class="fig-purple" font-size="11">③ 微程序控制器 · 不易直接硬件流水</text>
    <text x="184" y="240" text-anchor="middle" class="fig-label" font-size="10">控存 ROM 查微指令；指令 CPI 差异大，译码边界不固定</text>
    <text x="184" y="259" text-anchor="middle" class="fig-muted" font-size="9.5">优势：代码密度高、占用主存小（20% 指令占 80% 运行时间）</text>
    <text x="536" y="37" text-anchor="middle" class="fig-title" font-size="12.5">RISC 精简指令集（典型：MIPS / RISC-V）</text>
    <text x="536" y="69" text-anchor="middle" class="fig-blue" font-size="11">① 定长 32 位指令（4B 对齐）+ 简单寻址</text>
    <text x="536" y="92" text-anchor="middle" class="fig-muted" font-size="10">OP · rs1 · rs2 · rd 字段位置规整，硬件并行译码</text>
    <text x="536" y="142" text-anchor="middle" class="fig-green" font-size="11">② Load / Store 架构（32 个通用寄存器）</text>
    <text x="536" y="165" text-anchor="middle" class="fig-muted" font-size="10">仅 LW / SW 访问主存；算术逻辑运算全在寄存器间完成</text>
    <text x="536" y="218" text-anchor="middle" class="fig-accent" font-size="11">③ 硬布线控制器 · 天然契合五级流水线</text>
    <text x="536" y="240" text-anchor="middle" class="fig-label" font-size="10">IF取指 → ID译码 → EX执行 → MEM访存 → WB写回</text>
    <text x="536" y="259" text-anchor="middle" class="fig-muted" font-size="9.5">优势：组合逻辑极速控制，理想 CPI 趋近 1，编译优化友好</text>
  </svg>
  <figcaption>图 5.9：CISC（变长指令、直接访存、微程序控制）与 RISC（定长指令、Load/Store 隔离、硬布线五级流水）体系结构全维度对比</figcaption>
</figure>

> <strong>注意</strong>：CF/OF 的“语义”是<strong>软件视角</strong>：CF 表示无符号数运算的进位/借位，OF 表示有符号数（补码）运算的溢出。硬件加法器并不区分操作数是否有符号，同一套电路同时产生两个标志，由编译器/程序员决定使用哪一个。减法时 CF = 最高位进位的<strong>取反</strong>（有借位则 CF=1）。

> <strong>★考点</strong>：给定汇编片段判断条件转移是否成立时，先确定操作数按有符号还是无符号解释，再选对应标志位；`cmp` 实质是做减法并置标志。

## 第6章 CPU

### 6.1 CPU 的组成与寄存器可见性

CPU 由<strong>运算器</strong>与<strong>控制器两大部件构成，外加寄存器组。运算器完成算术与逻辑运算；控制器按程序对应的机器指令序列，逐条完成取指、译码、执行，形成“取指—译码—执行”的永久循环。数据通路</strong>是数据在各功能部件之间传送的路径：CPU 内的功能部件包括专用寄存器、通用寄存器、状态寄存器、L1 Cache、MMU、浮点运算逻辑、异常与中断处理逻辑；CPU 外的功能部件是存储器与 I/O 设备。

判断一个寄存器是否“程序员可见”，标准是它是否属于指令集结构（ISA）的一部分：

| 寄存器 | 作用 | 汇编程序员可见 |
|---|---|---|
| 通用寄存器 R0–R3 | 存放操作数、地址、运算结果 | <strong>可见</strong> |
| PC | 存放当前/下一条指令地址 | <strong>可见</strong> |
| PSW（状态寄存器） | 存放 ZF、CF 等标志 | <strong>可见</strong> |
| MAR | 存放主存地址 | 不可见 |
| MDR | 存放与主存交换的数据 | 不可见 |
| IR | 存放当前指令 | 不可见 |
| 暂存器 A/T/AC、SR | 暂存 ALU 操作数与结果 | 不可见 |
| 微地址寄存器 μAR、微指令寄存器 μIR | 微程序控制器的内部部件 | 不可见 |

可见寄存器是<strong>汇编语言的操作接口</strong>，程序员通过它们直接控制数据、执行流程和存储访问；不可见寄存器属于<strong>硬件实现的内部细节</strong>。

部件的位宽由它所承载的信息决定：16 位机器字长、20 位地址线、8 位数据线、16 位定长指令字的机器中，<strong>ALU 位宽 = 机器字长 = 16 位</strong>，<strong>IR 位宽 = 指令字长 = 16 位</strong>，<strong>MAR 位宽 = 地址线位数 = 20 位</strong>，<strong>MDR 位宽 = 数据线位数 = 8 位</strong>；按字节编址时可寻址空间为 $2^{20}$ B，即 1 MB。

<figure class="cpd-figure not-content" id="hco-w5-fig-1">
  <svg viewBox="0 0 700 252" role="img" aria-label="CPU寄存器程序员可见性边界与部件位宽推导关系图">
    <defs>
      <marker id="hco-w5-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="38" width="328" height="180" rx="6" class="fig-box-subtle"/>
    <rect x="356" y="38" width="328" height="180" rx="6" class="fig-box-subtle"/>
    <line x1="26" y1="133" x2="334" y2="133" class="fig-line-dashed"/>
    <line x1="472" y1="80" x2="500" y2="80" class="fig-line" marker-end="url(#hco-w5-arr-1)"/>
    <line x1="472" y1="116" x2="500" y2="116" class="fig-line" marker-end="url(#hco-w5-arr-1)"/>
    <line x1="472" y1="152" x2="500" y2="152" class="fig-line" marker-end="url(#hco-w5-arr-1)"/>
    <line x1="472" y1="188" x2="500" y2="188" class="fig-line" marker-end="url(#hco-w5-arr-1)"/>
    <rect x="28" y="64" width="304" height="56" rx="4" class="fig-box-blue"/>
    <rect x="112" y="123" width="136" height="20" rx="4" class="fig-box-accent"/>
    <rect x="28" y="150" width="304" height="58" rx="4" class="fig-box"/>
    <rect x="368" y="66" width="104" height="28" rx="4" class="fig-box"/>
    <rect x="502" y="66" width="170" height="28" rx="4" class="fig-box-green"/>
    <rect x="368" y="102" width="104" height="28" rx="4" class="fig-box"/>
    <rect x="502" y="102" width="170" height="28" rx="4" class="fig-box-purple"/>
    <rect x="368" y="138" width="104" height="28" rx="4" class="fig-box"/>
    <rect x="502" y="138" width="170" height="28" rx="4" class="fig-box-orange"/>
    <rect x="368" y="174" width="104" height="28" rx="4" class="fig-box"/>
    <rect x="502" y="174" width="170" height="28" rx="4" class="fig-box-blue"/>
    <text x="16" y="24" font-size="13" class="fig-title">ISA 可见性边界与各硬件部件位宽推导模型</text>
    <text x="28" y="55" font-size="11" class="fig-muted">（a）汇编程序员视角 vs 微体系结构透明层</text>
    <text x="180" y="86" font-size="12" text-anchor="middle" class="fig-blue">程序员可见（属于 ISA）</text>
    <text x="180" y="106" font-size="11" text-anchor="middle" class="fig-label">通用寄存器 R0–Rn · PC · PSW（ZF/CF/OF）</text>
    <text x="180" y="137" font-size="10" text-anchor="middle" class="fig-accent">ISA 指令集架构分界线</text>
    <text x="180" y="172" font-size="12" text-anchor="middle" class="fig-label">程序员透明 / 不可见（硬件内部细节）</text>
    <text x="180" y="192" font-size="8.6" text-anchor="middle" class="fig-muted">MAR · MDR · IR · 暂存 A/T · AC(结果)/SR(移位) · μAR/μIR</text>
    <text x="368" y="55" font-size="11" class="fig-muted">（b）位宽对应实例（字长16b / 地址20b / 数据8b）</text>
    <text x="420" y="84" font-size="11" text-anchor="middle" class="fig-label">机器字长 16b</text>
    <text x="587" y="84" font-size="11" text-anchor="middle" class="fig-green">ALU = 16 位 · GPR = 16 位</text>
    <text x="420" y="120" font-size="11" text-anchor="middle" class="fig-label">指令字长 16b</text>
    <text x="587" y="120" font-size="11" text-anchor="middle" class="fig-purple">IR 指令寄存器 = 16 位</text>
    <text x="420" y="156" font-size="11" text-anchor="middle" class="fig-label">地址总线 20b</text>
    <text x="587" y="156" font-size="11" text-anchor="middle" class="fig-orange">MAR = 20 位（寻址 1 MB）</text>
    <text x="420" y="192" font-size="11" text-anchor="middle" class="fig-label">数据总线 8b</text>
    <text x="587" y="192" font-size="11" text-anchor="middle" class="fig-blue">MDR = 8 位（取1字需2次）</text>
    <text x="16" y="238" font-size="11" class="fig-muted">核心判据：凡汇编指令能直接指定或状态分支可感知的寄存器均可见；MAR/MDR/IR 仅参与微操作，对程序员完全透明。</text>
  </svg>
  <figcaption>图 6.1：CPU 寄存器 ISA 可见性分层与部件位宽推导对照</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w5-fig-2">
  <svg viewBox="0 0 700 442" role="img" aria-label="单总线CPU的内部部件、可见与透明寄存器及运算器数据通路">
    <defs>
      <marker id="hco-w5-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="38" width="534" height="236" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="286" width="668" height="120" rx="6" class="fig-box-subtle"/>
    <line x1="336" y1="50" x2="336" y2="266" class="fig-line-accent" stroke-width="3"/>
    <line x1="186" y1="74" x2="334" y2="74" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="186" y1="112" x2="334" y2="112" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="186" y1="150" x2="334" y2="150" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="186" y1="188" x2="334" y2="188" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="186" y1="226" x2="334" y2="226" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="336" y1="84" x2="418" y2="84" class="fig-line" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="340" y1="136" x2="418" y2="136" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="528" y1="84" x2="582" y2="84" class="fig-line" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="530" y1="136" x2="582" y2="136" class="fig-line" marker-start="url(#hco-w5-arr-2)" marker-end="url(#hco-w5-arr-2)"/>
    <polyline points="336,266 336,304 82,304 82,326" class="fig-line" marker-end="url(#hco-w5-arr-2)"/>
    <polyline points="336,304 336,320 586,320 586,334" fill="none" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#hco-w5-arr-2)"/>
    <circle cx="336" cy="304" r="3" class="fig-dot"/>
    <line x1="134" y1="344" x2="182" y2="344" class="fig-line" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="504" y1="380" x2="294" y2="380" class="fig-line" marker-end="url(#hco-w5-arr-2)"/>
    <line x1="292" y1="352" x2="358" y2="352" class="fig-line" marker-end="url(#hco-w5-arr-2)"/>
    <polyline points="410,334 410,304 338,304" fill="none" class="fig-line-accent" marker-end="url(#hco-w5-arr-2)"/>
    <rect x="34" y="59" width="150" height="30" rx="4" class="fig-box-blue"/>
    <rect x="34" y="97" width="150" height="30" rx="4" class="fig-box"/>
    <rect x="34" y="135" width="150" height="30" rx="4" class="fig-box-blue"/>
    <rect x="34" y="173" width="150" height="30" rx="4" class="fig-box-blue"/>
    <rect x="34" y="211" width="150" height="30" rx="4" class="fig-box"/>
    <rect x="228" y="54" width="60" height="16" rx="3" class="fig-box"/>
    <rect x="420" y="68" width="108" height="32" rx="4" class="fig-box"/>
    <rect x="420" y="120" width="108" height="32" rx="4" class="fig-box"/>
    <rect x="584" y="60" width="100" height="104" rx="5" class="fig-box-orange"/>
    <rect x="34" y="328" width="100" height="34" rx="4" class="fig-box"/>
    <rect x="184" y="324" width="108" height="64" rx="5" class="fig-box-accent"/>
    <rect x="360" y="334" width="104" height="36" rx="4" class="fig-box"/>
    <rect x="504" y="336" width="164" height="54" rx="4" class="fig-box-purple"/>
    <rect x="172" y="295" width="90" height="18" rx="3" class="fig-box"/>
    <rect x="364" y="295" width="90" height="18" rx="3" class="fig-box"/>
    <text x="16" y="24" font-size="13" class="fig-title">单总线 CPU 的内部部件、寄存器与运算器连接结构</text>
    <text x="30" y="52" font-size="11" class="fig-muted">CPU 内部（单总线结构）</text>
    <text x="109" y="79" font-size="12" text-anchor="middle" class="fig-blue">PC（可见）</text>
    <text x="109" y="117" font-size="12" text-anchor="middle" class="fig-label">IR（透明）</text>
    <text x="109" y="155" font-size="12" text-anchor="middle" class="fig-blue">通用寄存器 R0–R3</text>
    <text x="109" y="193" font-size="12" text-anchor="middle" class="fig-blue">PSW 状态字（可见）</text>
    <text x="109" y="231" font-size="12" text-anchor="middle" class="fig-label">暂存器 A / T（透明）</text>
    <text x="258" y="66" font-size="10" text-anchor="middle" class="fig-muted">三态门</text>
    <text x="346" y="258" font-size="11" class="fig-accent">CPU 内部单总线</text>
    <text x="474" y="89" font-size="12" text-anchor="middle" class="fig-label">MAR（透明）</text>
    <text x="474" y="141" font-size="12" text-anchor="middle" class="fig-label">MDR（透明）</text>
    <text x="556" y="76" font-size="10" text-anchor="middle" class="fig-muted">地址</text>
    <text x="556" y="128" font-size="10" text-anchor="middle" class="fig-muted">数据</text>
    <text x="634" y="106" font-size="12" text-anchor="middle" class="fig-orange">主存 M</text>
    <text x="634" y="126" font-size="10" text-anchor="middle" class="fig-muted">（CPU 外部）</text>
    <text x="30" y="392" font-size="11" class="fig-muted">运算器组局部放大</text>
    <text x="84" y="350" font-size="12" text-anchor="middle" class="fig-label">暂存器 A</text>
    <text x="156" y="338" font-size="10" text-anchor="middle" class="fig-muted">端口 A</text>
    <text x="238" y="360" font-size="13" text-anchor="middle" class="fig-accent">ALU</text>
    <text x="325" y="346" font-size="10" text-anchor="middle" class="fig-muted">结果</text>
    <text x="412" y="357" font-size="12" text-anchor="middle" class="fig-label">暂存 SR / AC</text>
    <text x="325" y="394" font-size="10" text-anchor="middle" class="fig-muted">端口 B</text>
    <text x="586" y="358" font-size="11" text-anchor="middle" class="fig-purple">MUX 多路选择器</text>
    <text x="586" y="376" font-size="10" text-anchor="middle" class="fig-muted">总线数据 / 常数 → ALU B</text>
    <text x="217" y="308" font-size="10" text-anchor="middle" class="fig-muted">总线锁存 → A</text>
    <text x="409" y="308" font-size="10" text-anchor="middle" class="fig-accent">经三态门回总线</text>
    <text x="16" y="428" font-size="11" class="fig-muted">注：蓝色框为汇编程序员可见寄存器，中性框为透明寄存器；ALU 两输入端无法同时取自单总线，故需暂存器 A 与结果寄存器 AC/SR。</text>
  </svg>
  <figcaption>图 6.2：单总线 CPU 的寄存器连接与运算器暂存结构（蓝色为程序员可见，其余为硬件透明）</figcaption>
</figure>

> <strong>★考点</strong>：程序员可见寄存器（PC、PSW、通用寄存器）与不可见寄存器（MAR、MDR、IR、暂存器）的区分；由机器字长/地址线/数据线/指令字长反推各部件位宽。

### 6.2 指令执行的一般流程

CPU 上电复位后进入“取指—译码—执行”的循环。在控制器控制下，一条指令的执行流程为：

1. <strong>取指</strong>：PC 的值送入 MAR，经地址总线送主存；发读命令，主存内容经数据总线送入 MDR。
2. <strong>送 IR</strong>：MDR 的内容送入指令寄存器 IR。
3. <strong>更新 PC</strong>：PC 加上当前指令的长度，指向下一条指令。变长指令系统中“加 1”的实际含义是<strong>加本条指令的字节数</strong>。
4. <strong>译码</strong>：IR 送指令译码器，译码结果决定进入哪条执行路径。
5. <strong>执行</strong>：取操作数、执行运算、存放结果。
6. <strong>中断判断</strong>：指令执行完毕后检查中断请求；有请求则进入中断响应，响应结束后回到取指；无请求则直接进入下一次取指。

若当前指令是<strong>分支指令且条件满足</strong>，只需把分支目标地址写入 PC，下一条取指就从目标地址开始；条件不满足则顺序执行。

<figure class="cpd-figure not-content" id="hco-w5-fig-3">
  <svg viewBox="0 0 680 444" role="img" aria-label="CPU指令执行的一般循环流程图：取指、译码、分支修改PC、执行与中断响应">
    <defs>
      <marker id="hco-w5-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="648" height="380" rx="6" class="fig-box-subtle"/>
    <line x1="280" y1="92" x2="280" y2="108" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <line x1="280" y1="150" x2="280" y2="166" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <line x1="376" y1="196" x2="452" y2="196" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <polyline points="548,220 548,317 282,317" class="fig-line-accent" marker-end="url(#hco-w5-arr-3)"/>
    <line x1="280" y1="224" x2="280" y2="262" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <line x1="280" y1="308" x2="280" y2="324" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <line x1="376" y1="354" x2="452" y2="354" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <line x1="280" y1="382" x2="280" y2="400" class="fig-line"/>
    <polyline points="548,378 548,400 48,400 48,69 156,69" class="fig-line" marker-end="url(#hco-w5-arr-3)"/>
    <rect x="158" y="46" width="244" height="46" rx="4" class="fig-box-blue"/>
    <rect x="158" y="110" width="244" height="40" rx="4" class="fig-box-purple"/>
    <polygon points="280,168 376,196 280,224 184,196" class="fig-box-orange"/>
    <rect x="454" y="172" width="188" height="48" rx="4" class="fig-box-blue"/>
    <rect x="158" y="264" width="244" height="44" rx="4" class="fig-box-green"/>
    <polygon points="280,326 376,354 280,382 184,354" class="fig-box-orange"/>
    <rect x="454" y="330" width="188" height="48" rx="4" class="fig-box-red"/>
    <rect x="396" y="186" width="32" height="18" rx="3" class="fig-box"/>
    <rect x="264" y="232" width="32" height="16" rx="3" class="fig-box"/>
    <rect x="396" y="344" width="32" height="18" rx="3" class="fig-box"/>
    <rect x="150" y="391" width="32" height="16" rx="3" class="fig-box"/>
    <text x="16" y="24" font-size="13" class="fig-title">CPU 指令执行与中断检测的永久循环流程</text>
    <text x="280" y="65" font-size="12" text-anchor="middle" class="fig-blue">① 取指：PC → MAR，M(MAR) → MDR</text>
    <text x="280" y="83" font-size="11" text-anchor="middle" class="fig-label">更新 PC ← (PC) + 本条指令字节长度</text>
    <text x="280" y="135" font-size="10.8" text-anchor="middle" class="fig-purple">② 装载与译码：MDR → IR → 指令译码器</text>
    <text x="280" y="200" font-size="11" text-anchor="middle" class="fig-orange">分支成立?</text>
    <text x="412" y="199" font-size="10" text-anchor="middle" class="fig-label">是</text>
    <text x="548" y="192" font-size="12" text-anchor="middle" class="fig-blue">PC ← 分支目标地址</text>
    <text x="548" y="210" font-size="10" text-anchor="middle" class="fig-muted">条件满足才改写 PC</text>
    <text x="280" y="244" font-size="10" text-anchor="middle" class="fig-label">否</text>
    <text x="280" y="283" font-size="10.8" text-anchor="middle" class="fig-green">③ 执行：取操作数 · ALU 运算 · 写回结果</text>
    <text x="280" y="300" font-size="10" text-anchor="middle" class="fig-muted">（NOP 空操作指令此阶段无动作，但取指已改 PC）</text>
    <text x="280" y="358" font-size="11" text-anchor="middle" class="fig-orange">中断请求?</text>
    <text x="412" y="357" font-size="10" text-anchor="middle" class="fig-label">是</text>
    <text x="548" y="350" font-size="12" text-anchor="middle" class="fig-red">④ 关中断 · 保存断点</text>
    <text x="548" y="368" font-size="11" text-anchor="middle" class="fig-label">服务程序入口地址 → PC</text>
    <text x="166" y="403" font-size="10" text-anchor="middle" class="fig-label">否</text>
    <text x="16" y="432" font-size="11" class="fig-muted">注：中断请求在每条指令（含分支指令）执行完毕的边界处检测；响应中断将服务入口送 PC 后，同样回到取指阶段。</text>
  </svg>
  <figcaption>图 6.3：指令执行的“取指—译码—执行—中断检测”基本控制流</figcaption>
</figure>

> <strong>注意</strong>：即使取到的是<strong>空操作指令（NOP）</strong>，取指阶段仍会自动更新 PC，使 PC 指向下一条指令。所以“不改变 PC”的说法是错的。

### 6.3 指令周期与机器周期

<strong>指令周期</strong>指 CPU 从主存取出并执行一条指令所需的时间。一个完整的指令周期通常包含四个阶段：

| 阶段 | 主要工作 |
|---|---|
| 取指周期 | 按 PC 取指令，<strong>并包含译码</strong> |
| 间址周期 | 间接寻址时先取操作数的有效地址 |
| 执行周期 | 取操作数、运算、存结果 |
| 中断周期 | 响应中断：关中断、保存断点、送服务程序入口 |

不同指令的指令周期长度不同：无条件转移指令只需要<strong>取指 + 执行</strong>；采用间接寻址的指令需要<strong>取指 + 间址 + 执行</strong>，且间接寻址（含间址加变址）除取指的一次访存外，还要<strong>额外两次访存</strong>才能最终取到操作数。

指令周期用<strong>CPU 时钟周期</strong>计量。为便于同步，可把指令周期划分为若干<strong>机器周期</strong>；机器周期一般指从存储器取出一个存储字所需的最短时间，每个机器周期由若干时钟周期构成。定长指令周期中，所有指令的机器周期数固定、每个机器周期的时钟数也固定；变长指令周期中各指令直接按 CPU 时钟周期同步，所需时钟数按需分配，机器周期概念逐渐消失，现代计算机普遍采用变长指令周期。

$$
T_{instr} = m \times T_{clk}
$$

式中 $m$ 为一条指令包含的时钟周期数。若每个机器周期含 $c$ 个时钟周期、指令含 $k$ 个机器周期，则 $m = k \times c$（仅定长指令周期成立）。

<figure class="cpd-figure not-content" id="hco-w5-fig-4">
  <svg viewBox="0 0 680 306" role="img" aria-label="定长指令周期与变长指令周期的机器周期及时钟节拍对比图">
    <rect x="16" y="36" width="648" height="124" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="172" width="648" height="102" rx="6" class="fig-box-subtle"/>
    <line x1="104" y1="54" x2="104" y2="70" class="fig-line"/><line x1="149" y1="54" x2="149" y2="70" class="fig-line"/>
    <line x1="194" y1="54" x2="194" y2="70" class="fig-line"/><line x1="239" y1="54" x2="239" y2="70" class="fig-line"/>
    <line x1="284" y1="54" x2="284" y2="70" class="fig-line"/><line x1="329" y1="54" x2="329" y2="70" class="fig-line"/>
    <line x1="374" y1="54" x2="374" y2="70" class="fig-line"/><line x1="419" y1="54" x2="419" y2="70" class="fig-line"/>
    <line x1="464" y1="54" x2="464" y2="70" class="fig-line"/><line x1="509" y1="54" x2="509" y2="70" class="fig-line"/>
    <line x1="554" y1="54" x2="554" y2="70" class="fig-line"/><line x1="599" y1="54" x2="599" y2="70" class="fig-line"/>
    <line x1="644" y1="54" x2="644" y2="70" class="fig-line"/>
    <line x1="104" y1="190" x2="104" y2="206" class="fig-line"/><line x1="149" y1="190" x2="149" y2="206" class="fig-line"/>
    <line x1="194" y1="190" x2="194" y2="206" class="fig-line"/><line x1="239" y1="190" x2="239" y2="206" class="fig-line"/>
    <line x1="284" y1="190" x2="284" y2="206" class="fig-line"/><line x1="329" y1="190" x2="329" y2="206" class="fig-line"/>
    <line x1="374" y1="190" x2="374" y2="206" class="fig-line"/><line x1="419" y1="190" x2="419" y2="206" class="fig-line"/>
    <line x1="464" y1="190" x2="464" y2="206" class="fig-line"/><line x1="509" y1="190" x2="509" y2="206" class="fig-line"/>
    <line x1="554" y1="190" x2="554" y2="206" class="fig-line"/><line x1="599" y1="190" x2="599" y2="206" class="fig-line"/>
    <line x1="644" y1="190" x2="644" y2="206" class="fig-line"/>
    <rect x="104" y="74" width="180" height="34" rx="3" class="fig-box-blue"/>
    <rect x="284" y="74" width="180" height="34" rx="3" class="fig-box-purple"/>
    <rect x="464" y="74" width="180" height="34" rx="3" class="fig-box-green"/>
    <rect x="104" y="114" width="180" height="32" rx="3" class="fig-box"/>
    <rect x="284" y="114" width="180" height="32" rx="3" class="fig-box"/>
    <rect x="464" y="114" width="180" height="32" rx="3" class="fig-box"/>
    <rect x="104" y="210" width="180" height="34" rx="3" class="fig-box-blue"/>
    <rect x="284" y="210" width="135" height="34" rx="3" class="fig-box-green"/>
    <rect x="419" y="210" width="225" height="34" rx="3" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">定长指令周期（机器周期同步）与变长指令周期（时钟节拍同步）对比</text>
    <text x="28" y="54" font-size="11" class="fig-muted">（a）定长</text>
    <text x="28" y="68" font-size="10" class="fig-muted">3×4=12拍</text>
    <text x="126" y="66" font-size="10" text-anchor="middle" class="fig-muted">1</text>
    <text x="171" y="66" font-size="10" text-anchor="middle" class="fig-muted">2</text>
    <text x="216" y="66" font-size="10" text-anchor="middle" class="fig-muted">3</text>
    <text x="261" y="66" font-size="10" text-anchor="middle" class="fig-muted">4</text>
    <text x="306" y="66" font-size="10" text-anchor="middle" class="fig-muted">5</text>
    <text x="351" y="66" font-size="10" text-anchor="middle" class="fig-muted">6</text>
    <text x="396" y="66" font-size="10" text-anchor="middle" class="fig-muted">7</text>
    <text x="441" y="66" font-size="10" text-anchor="middle" class="fig-muted">8</text>
    <text x="486" y="66" font-size="10" text-anchor="middle" class="fig-muted">9</text>
    <text x="531" y="66" font-size="10" text-anchor="middle" class="fig-muted">10</text>
    <text x="576" y="66" font-size="10" text-anchor="middle" class="fig-muted">11</text>
    <text x="621" y="66" font-size="10" text-anchor="middle" class="fig-muted">12</text>
    <text x="194" y="96" font-size="12" text-anchor="middle" class="fig-blue">机器周期 M1（4 拍）</text>
    <text x="374" y="96" font-size="12" text-anchor="middle" class="fig-purple">机器周期 M2（4 拍）</text>
    <text x="554" y="96" font-size="12" text-anchor="middle" class="fig-green">机器周期 M3（4 拍）</text>
    <text x="194" y="135" font-size="11" text-anchor="middle" class="fig-label">取指周期（含译码）</text>
    <text x="374" y="135" font-size="11" text-anchor="middle" class="fig-label">间址周期（可选）</text>
    <text x="554" y="135" font-size="11" text-anchor="middle" class="fig-label">执行周期</text>
    <text x="28" y="192" font-size="11" class="fig-muted">（b）变长</text>
    <text x="28" y="206" font-size="10" class="fig-muted">按需分配</text>
    <text x="126" y="202" font-size="10" text-anchor="middle" class="fig-muted">1</text>
    <text x="171" y="202" font-size="10" text-anchor="middle" class="fig-muted">2</text>
    <text x="216" y="202" font-size="10" text-anchor="middle" class="fig-muted">3</text>
    <text x="261" y="202" font-size="10" text-anchor="middle" class="fig-muted">4</text>
    <text x="306" y="202" font-size="10" text-anchor="middle" class="fig-muted">5</text>
    <text x="351" y="202" font-size="10" text-anchor="middle" class="fig-muted">6</text>
    <text x="396" y="202" font-size="10" text-anchor="middle" class="fig-muted">7</text>
    <text x="441" y="202" font-size="10" text-anchor="middle" class="fig-muted">8</text>
    <text x="486" y="202" font-size="10" text-anchor="middle" class="fig-muted">9</text>
    <text x="531" y="202" font-size="10" text-anchor="middle" class="fig-muted">10</text>
    <text x="576" y="202" font-size="10" text-anchor="middle" class="fig-muted">11</text>
    <text x="621" y="202" font-size="10" text-anchor="middle" class="fig-muted">12</text>
    <text x="194" y="232" font-size="11" text-anchor="middle" class="fig-blue">I1 寄存器运算（4 拍）</text>
    <text x="351" y="232" font-size="11" text-anchor="middle" class="fig-green">I2 转移（3 拍）</text>
    <text x="531" y="232" font-size="11" text-anchor="middle" class="fig-orange">I3 访存加载指令（5 拍）</text>
    <text x="28" y="264" font-size="11" class="fig-muted">现代 CPU 直接以时钟周期 Tclk 同步，短指令无需补齐空拍，大幅提升有效吞吐率。</text>
    <text x="16" y="294" font-size="11" class="fig-muted">注：机器周期基准通常取单次主存读写的最小耗时；变长指令周期下 m 随指令类型动态变化。</text>
  </svg>
  <figcaption>图 6.4：定长指令周期（固定机器周期数）与现代变长指令周期（按需分配节拍）对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w5-fig-5">
  <svg viewBox="0 0 700 286" role="img" aria-label="指令周期四阶段FE、IND、EX、INT状态转移与访存信息区分图">
    <defs>
      <marker id="hco-w5-arr-5" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="404" height="218" rx="6" class="fig-box-subtle"/>
    <rect x="432" y="36" width="252" height="218" rx="6" class="fig-box-subtle"/>
    <line x1="188" y1="93" x2="254" y2="93" class="fig-line" marker-end="url(#hco-w5-arr-5)"/>
    <polyline points="154,114 154,143 292,143 292,170" class="fig-line-accent" marker-end="url(#hco-w5-arr-5)"/>
    <line x1="348" y1="114" x2="348" y2="170" class="fig-line" marker-end="url(#hco-w5-arr-5)"/>
    <line x1="256" y1="193" x2="190" y2="193" class="fig-line" marker-end="url(#hco-w5-arr-5)"/>
    <line x1="120" y1="172" x2="120" y2="116" class="fig-line" marker-end="url(#hco-w5-arr-5)"/>
    <polyline points="328,214 328,240 30,240 30,93 50,93" class="fig-line" marker-end="url(#hco-w5-arr-5)"/>
    <rect x="52" y="72" width="136" height="42" rx="5" class="fig-box-blue"/>
    <rect x="256" y="72" width="144" height="42" rx="5" class="fig-box-purple"/>
    <rect x="256" y="172" width="144" height="42" rx="5" class="fig-box-green"/>
    <rect x="52" y="172" width="136" height="42" rx="5" class="fig-box-red"/>
    <rect x="192" y="75" width="56" height="16" rx="3" class="fig-box"/>
    <rect x="184" y="134" width="68" height="18" rx="3" class="fig-box"/>
    <rect x="196" y="184" width="48" height="18" rx="3" class="fig-box"/>
    <rect x="128" y="231" width="96" height="16" rx="3" class="fig-box"/>
    <rect x="444" y="66" width="228" height="38" rx="4" class="fig-box-blue"/>
    <rect x="444" y="110" width="228" height="38" rx="4" class="fig-box-purple"/>
    <rect x="444" y="154" width="228" height="38" rx="4" class="fig-box-green"/>
    <rect x="444" y="198" width="228" height="38" rx="4" class="fig-box-red"/>
    <text x="16" y="24" font-size="13" class="fig-title">指令周期四阶段（FE / IND / EX / INT）状态机与访存性质区分</text>
    <text x="28" y="55" font-size="11" class="fig-muted">（a）标志触发器状态流转</text>
    <text x="120" y="90" font-size="12" text-anchor="middle" class="fig-blue">取指周期（FE=1）</text>
    <text x="120" y="106" font-size="10" text-anchor="middle" class="fig-muted">取指令 + 译码</text>
    <text x="328" y="90" font-size="12" text-anchor="middle" class="fig-purple">间址周期（IND=1）</text>
    <text x="328" y="106" font-size="10" text-anchor="middle" class="fig-muted">1→IND 取有效地址</text>
    <text x="328" y="190" font-size="12" text-anchor="middle" class="fig-green">执行周期（EX=1）</text>
    <text x="328" y="206" font-size="10" text-anchor="middle" class="fig-muted">1→EX 运算/访存</text>
    <text x="120" y="190" font-size="12" text-anchor="middle" class="fig-red">中断周期（INT=1）</text>
    <text x="120" y="206" font-size="10" text-anchor="middle" class="fig-muted">1→INT 存断点/转入口</text>
    <text x="220" y="87" font-size="10" text-anchor="middle" class="fig-muted">间接寻址</text>
    <text x="218" y="147" font-size="10" text-anchor="middle" class="fig-accent">非间址指令</text>
    <text x="220" y="197" font-size="10" text-anchor="middle" class="fig-muted">有中断</text>
    <text x="92" y="148" font-size="10" text-anchor="middle" class="fig-muted">1→FE</text>
    <text x="176" y="243" font-size="10" text-anchor="middle" class="fig-muted">无中断（1→FE）</text>
    <text x="444" y="55" font-size="11" class="fig-muted">（b）各阶段访存数据流性质</text>
    <text x="558" y="89" font-size="11" text-anchor="middle" class="fig-blue">FE 访存：读出机器指令 → IR</text>
    <text x="558" y="133" font-size="11" text-anchor="middle" class="fig-purple">IND 访存：读出有效地址 EA → MDR</text>
    <text x="558" y="177" font-size="11" text-anchor="middle" class="fig-green">EX 访存：读/写操作数 ↔ MDR</text>
    <text x="558" y="221" font-size="11" text-anchor="middle" class="fig-red">INT 保存：程序断点 (PC) → 栈/EPC</text>
    <text x="16" y="274" font-size="11" class="fig-muted">冯·诺依曼机指令与数据同存于主存，CPU 严格依据当前处于哪个周期阶段（FE/IND/EX/INT）来区分访存字的性质。</text>
  </svg>
  <figcaption>图 6.5：指令周期四阶段触发器转移与各周期主存访问性质对照</figcaption>
</figure>

> <strong>★考点</strong>：CPU 区分<strong>指令与数据</strong>的依据是“指令周期的不同阶段”——取指阶段访问主存取的是指令，执行阶段访问主存取的是数据，而不是地址的高低位或存储器类型。

### 6.4 数据通路：单总线结构与三态门

CPU 内部各部件通过一条公共总线传送数据，就是<strong>单总线结构</strong>。每个寄存器的输出经一个<strong>三态门</strong>接到总线上，三态门由各自的控制信号（如 PCout、R1out、MDRout）控制通断；同一时刻只允许一个三态门打开，否则总线上的数据会冲突。因此单总线上<strong>一个时钟周期只能完成一次传送</strong>，一条指令的指令周期必须由多个时钟周期（节拍）组成。

单总线的两条直接后果：

- <strong>ALU 的两个操作数不能同时由总线送来</strong>，必须分时传送，先到的操作数暂存在<strong>暂存寄存器</strong>（A 或 T）中；运算结果也需要暂存（AC，或经具有直送/移位功能的 SR）再送回总线。
- <strong>单周期处理器若用单总线，则一个时钟周期内无法完成指令的全部操作</strong>，因为一次总线操作无法满足取指、取数、运算、写回的全部传送需求。

<figure class="cpd-figure not-content" id="hco-w5-fig-6">
  <svg viewBox="0 0 700 332" role="img" aria-label="单总线数据通路三态门分时控制与多总线及专用通路对比图">
    <defs>
      <marker id="hco-w5-arr-6" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="416" height="258" rx="6" class="fig-box-subtle"/>
    <rect x="444" y="36" width="240" height="258" rx="6" class="fig-box-subtle"/>
    <line x1="248" y1="64" x2="248" y2="278" class="fig-line-accent" stroke-width="3"/>
    <line x1="120" y1="84" x2="246" y2="84" class="fig-line-dashed"/>
    <line x1="120" y1="138" x2="244" y2="138" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w5-arr-6)"/>
    <line x1="120" y1="192" x2="246" y2="192" class="fig-line-dashed"/>
    <line x1="248" y1="84" x2="314" y2="84" class="fig-line-dashed" marker-end="url(#hco-w5-arr-6)"/>
    <line x1="248" y1="128" x2="314" y2="128" class="fig-line-dashed" marker-end="url(#hco-w5-arr-6)"/>
    <line x1="248" y1="172" x2="314" y2="172" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w5-arr-6)"/>
    <line x1="366" y1="188" x2="366" y2="218" class="fig-line" marker-end="url(#hco-w5-arr-6)"/>
    <line x1="248" y1="248" x2="314" y2="248" class="fig-line-dashed" marker-end="url(#hco-w5-arr-6)"/>
    <rect x="32" y="68" width="88" height="32" rx="4" class="fig-box"/>
    <rect x="32" y="122" width="88" height="32" rx="4" class="fig-box-green"/>
    <rect x="32" y="176" width="88" height="32" rx="4" class="fig-box"/>
    <polygon points="172,76 190,84 172,92" class="fig-box"/>
    <polygon points="172,130 190,138 172,146" class="fig-box-green"/>
    <polygon points="172,184 190,192 172,200" class="fig-box"/>
    <rect x="316" y="68" width="102" height="32" rx="4" class="fig-box"/>
    <rect x="316" y="112" width="102" height="32" rx="4" class="fig-box"/>
    <rect x="316" y="156" width="102" height="32" rx="4" class="fig-box-blue"/>
    <rect x="316" y="220" width="102" height="52" rx="5" class="fig-box-accent"/>
    <rect x="456" y="64" width="216" height="66" rx="4" class="fig-box"/>
    <rect x="456" y="138" width="216" height="68" rx="4" class="fig-box-purple"/>
    <rect x="456" y="214" width="216" height="68" rx="4" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">单总线三态门分时互斥机制与三类内部数据通路对比</text>
    <text x="28" y="52" font-size="10.5" class="fig-muted">（a）单总线：同拍仅允许 1 个 out 三态门导通</text>
    <text x="256" y="272" font-size="10" class="fig-accent">内部单总线</text>
    <text x="76" y="89" font-size="12" text-anchor="middle" class="fig-label">PC</text>
    <text x="76" y="143" font-size="12" text-anchor="middle" class="fig-green">R1（发送）</text>
    <text x="76" y="197" font-size="12" text-anchor="middle" class="fig-label">MDR</text>
    <text x="180" y="71" font-size="9" text-anchor="middle" class="fig-muted">PCout=0（高阻）</text>
    <text x="180" y="125" font-size="9" text-anchor="middle" class="fig-green">R1out=1（导通）</text>
    <text x="180" y="179" font-size="9" text-anchor="middle" class="fig-muted">MDRout=0（高阻）</text>
    <text x="282" y="78" font-size="9" text-anchor="middle" class="fig-muted">MARin=0</text>
    <text x="282" y="122" font-size="9" text-anchor="middle" class="fig-muted">IRin=0</text>
    <text x="282" y="166" font-size="9" text-anchor="middle" class="fig-green">Ain=1</text>
    <text x="282" y="242" font-size="9" text-anchor="middle" class="fig-muted">下一拍送B</text>
    <text x="367" y="89" font-size="12" text-anchor="middle" class="fig-label">MAR</text>
    <text x="367" y="133" font-size="12" text-anchor="middle" class="fig-label">IR</text>
    <text x="367" y="177" font-size="12" text-anchor="middle" class="fig-blue">暂存器 A</text>
    <text x="367" y="250" font-size="13" text-anchor="middle" class="fig-accent">ALU</text>
    <text x="32" y="242" font-size="10" class="fig-muted">当拍微操作：R1 → Bus → 暂存 A</text>
    <text x="32" y="258" font-size="10" class="fig-muted">时钟边沿到来时 Ain 锁存总线值</text>
    <text x="456" y="54" font-size="11" class="fig-muted">（b）数据通路架构演进</text>
    <text x="564" y="83" font-size="11" text-anchor="middle" class="fig-label">① 单总线结构</text>
    <text x="564" y="100" font-size="10" text-anchor="middle" class="fig-muted">结构最简，1 拍仅 1 次传送</text>
    <text x="564" y="116" font-size="10" text-anchor="middle" class="fig-muted">必配暂存器 A/AC，无法单周期</text>
    <text x="564" y="157" font-size="11" text-anchor="middle" class="fig-purple">② 多（三）总线结构</text>
    <text x="564" y="174" font-size="10" text-anchor="middle" class="fig-muted">A/B 双源总线同拍送 ALU</text>
    <text x="564" y="190" font-size="10" text-anchor="middle" class="fig-muted">C 总线同拍写回，CPI 显著降低</text>
    <text x="564" y="233" font-size="11" text-anchor="middle" class="fig-orange">③ 专用数据通路</text>
    <text x="564" y="250" font-size="10" text-anchor="middle" class="fig-muted">按并发数据流点对点连接</text>
    <text x="564" y="266" font-size="10" text-anchor="middle" class="fig-muted">用 MUX 选路，支撑单周期与流水线</text>
    <text x="16" y="318" font-size="11" class="fig-muted">注：单总线中同一节拍允许 1 个发送方打开三态门、多个接收方同时锁存（如一发多收），严禁两发送端同时导通。</text>
  </svg>
  <figcaption>图 6.6：单总线三态门互斥控制原理与三类 CPU 数据通路结构对比</figcaption>
</figure>

| 项目 | 单周期处理器 | 多周期处理器 |
|---|---|---|
| 每条指令时钟数（CPI） | 1 | 多条，随指令而异 |
| 时钟周期长度 | 由<strong>最慢</strong>指令决定，较长、主频较低 | 每拍只需满足该拍操作，较短 |
| 控制信号 | 一个周期内不变 | 每个时钟周期都可能变化 |

多路选择器（MUX）用于在总线数据与常数之间选择 ALU 的 B 端口输入。16 位定长指令字、按字节编址时，顺序执行的下一条指令地址为 <strong>PC+2</strong>，故 MUX 的一个输入端接常数 2，由 MUXop 选择后送入 ALU 的 B 口完成 PC 加 2。

> <strong>注意</strong>：CPU 与<strong>主存之间</strong>若是专用存储总线（其他部件不能使用），不会产生总线冲突，MDR 与存储总线之间可以不加三态门；而各寄存器向 CPU 内部单总线输出的通路必须加三态门。

### 6.5 数据通路实例：ADD 指令的节拍与控制信号

以“R0 内容 + 存储器(R1 所指单元) → 该单元”的 ADD 指令为例，取指阶段 4 拍固定，执行阶段 5 拍，每拍只能有一次总线传送：

| 节拍 | 功能 | 有效控制信号 |
|---|---|---|
| T0 | PC→MAR，送地址总线 | PCout, MARin |
| T1 | 读主存，M(MAR)→MDR；PC 加 1 | MemR, MDRinE, PC+1 |
| T2 | MDR→IR | MDRout, IRin |
| T3 | 译码 | 无 |
| T4 | R1→MAR（形成操作数地址） | R1out, MARin |
| T5 | 读主存→MDR；R0→A | MemR, MDRinE, R0out, Ain |
| T6 | MDR 与 A 相加→AC | MDRout, ADD, ACin |
| T7 | AC→MDR | ACout, MDRin |
| T8 | MDR→主存（写回同一单元） | MDRoutE, MemW |

T4 写入 MAR 的地址一直保持到 T8，因此 T8 的写操作自然落回原单元。执行阶段的划分<strong>答案不唯一</strong>，只要同一节拍内不出现两个部件同时向单总线输出即可。

数据通路类题目的通用解法：先看该拍要传送什么，找出<strong>发送方</strong>（置其 out 信号）与<strong>接收方</strong>（置其 in 信号），再检查是否与同拍的其他传送争用总线；需要两个操作数时，一个走总线进暂存器，另一个在下一拍由 MDR/寄存器送上总线。

<figure class="cpd-figure not-content" id="hco-w5-fig-7">
  <svg viewBox="0 0 700 326" role="img" aria-label="单总线结构下ADD指令9个节拍的微操作序列与有效控制信号分解图">
    <rect x="16" y="36" width="318" height="252" rx="6" class="fig-box-subtle"/>
    <rect x="346" y="36" width="338" height="252" rx="6" class="fig-box-subtle"/>
    <rect x="28" y="64" width="44" height="36" rx="4" class="fig-box-blue"/>
    <rect x="78" y="64" width="244" height="36" rx="4" class="fig-box"/>
    <rect x="28" y="108" width="44" height="36" rx="4" class="fig-box-blue"/>
    <rect x="78" y="108" width="244" height="36" rx="4" class="fig-box"/>
    <rect x="28" y="152" width="44" height="36" rx="4" class="fig-box-blue"/>
    <rect x="78" y="152" width="244" height="36" rx="4" class="fig-box"/>
    <rect x="28" y="196" width="44" height="36" rx="4" class="fig-box-purple"/>
    <rect x="78" y="196" width="244" height="36" rx="4" class="fig-box"/>
    <rect x="358" y="62" width="44" height="32" rx="4" class="fig-box-green"/>
    <rect x="408" y="62" width="264" height="32" rx="4" class="fig-box"/>
    <rect x="358" y="100" width="44" height="34" rx="4" class="fig-box-orange"/>
    <rect x="408" y="100" width="264" height="34" rx="4" class="fig-box-orange"/>
    <rect x="358" y="140" width="44" height="32" rx="4" class="fig-box-green"/>
    <rect x="408" y="140" width="264" height="32" rx="4" class="fig-box"/>
    <rect x="358" y="178" width="44" height="32" rx="4" class="fig-box-green"/>
    <rect x="408" y="178" width="264" height="32" rx="4" class="fig-box"/>
    <rect x="358" y="216" width="44" height="32" rx="4" class="fig-box-green"/>
    <rect x="408" y="216" width="264" height="32" rx="4" class="fig-box"/>
    <text x="16" y="24" font-size="13" class="fig-title">ADD (R1), R0 指令的 9 拍微操作与单总线控制信号分解</text>
    <text x="28" y="54" font-size="11" class="fig-muted">（a）取指阶段（T0–T3，4 拍公用）</text>
    <text x="50" y="87" font-size="12" text-anchor="middle" class="fig-blue">T0</text>
    <text x="90" y="80" font-size="11" class="fig-label">PC → MAR</text>
    <text x="90" y="94" font-size="10" class="fig-muted">信号：PCout, MARin</text>
    <text x="50" y="131" font-size="12" text-anchor="middle" class="fig-blue">T1</text>
    <text x="90" y="124" font-size="11" class="fig-label">M(MAR) → MDR；PC + 1 → PC</text>
    <text x="90" y="138" font-size="10" class="fig-muted">信号：MemR, MDRinE, PC+1</text>
    <text x="50" y="175" font-size="12" text-anchor="middle" class="fig-blue">T2</text>
    <text x="90" y="168" font-size="11" class="fig-label">MDR → IR</text>
    <text x="90" y="182" font-size="10" class="fig-muted">信号：MDRout, IRin</text>
    <text x="50" y="219" font-size="12" text-anchor="middle" class="fig-purple">T3</text>
    <text x="90" y="212" font-size="11" class="fig-label">指令译码器译码</text>
    <text x="90" y="226" font-size="10" class="fig-muted">信号：无内部单总线传送</text>
    <text x="28" y="256" font-size="10" class="fig-muted">注：MDRinE 表从外部数据总线锁存，</text>
    <text x="28" y="272" font-size="10" class="fig-muted">不占用 CPU 内部单总线。</text>
    <text x="358" y="54" font-size="11" class="fig-muted">（b）执行阶段（T4–T8，5 拍读-算-写）</text>
    <text x="380" y="83" font-size="12" text-anchor="middle" class="fig-green">T4</text>
    <text x="418" y="82" font-size="11" class="fig-label">R1 → MAR · 信号：R1out, MARin</text>
    <text x="380" y="122" font-size="12" text-anchor="middle" class="fig-orange">T5</text>
    <text x="418" y="115" font-size="11" class="fig-orange">M(MAR)→MDR；R0→A（内外并行）</text>
    <text x="418" y="129" font-size="10" class="fig-muted">信号：MemR, MDRinE, R0out, Ain</text>
    <text x="380" y="161" font-size="12" text-anchor="middle" class="fig-green">T6</text>
    <text x="416" y="160" font-size="9.6" class="fig-label">(MDR)+(A) → AC · 信号：MDRout, ADD, ACin</text>
    <text x="380" y="199" font-size="12" text-anchor="middle" class="fig-green">T7</text>
    <text x="416" y="198" font-size="10.2" class="fig-label">AC → MDR · 信号：ACout, MDRin</text>
    <text x="380" y="237" font-size="12" text-anchor="middle" class="fig-green">T8</text>
    <text x="416" y="236" font-size="10.2" class="fig-label">MDR → M(MAR) · 信号：MDRoutE, MemW</text>
    <text x="358" y="270" font-size="10" class="fig-muted">★T4 送入 MAR 的地址保持至 T8，写回自动命中原单元。</text>
    <text x="16" y="312" font-size="11" class="fig-muted">核心判据：T5 中主存读入 MDR 走片外存储总线，R0 送暂存器 A 走片内单总线，两路物理隔离故可合并在同一节拍执行。</text>
  </svg>
  <figcaption>图 6.7：单总线 ADD (R1), R0 指令的 9 拍微操作安排与内外总线并行细节</figcaption>
</figure>

> <strong>★考点</strong>：给出指令功能，补全每个节拍的“功能 + 有效控制信号”；判断某条指令的执行阶段至少需要几个时钟周期（数一数不能合并的传送次数）。

### 6.6 时序发生器与三级时序

早期计算机用<strong>三级时序</strong>对控制信号定时：

| 级别 | 含义 |
|---|---|
| 状态周期（机器周期） | 完成一个完整操作（如取指、访存）的时间，由若干节拍电位组成 |
| 节拍电位 | 一个状态周期内的相序，用 T1、T2…表示 |
| 节拍脉冲 | 最基本的定时脉冲，由晶振经整形、分频得到 |

<strong>时序发生器</strong>本质上是一个<strong>同步时序电路</strong>，可用摩尔型有限状态机（FSM）设计：输出（状态周期信号、节拍电位信号）<strong>只取决于当前状态</strong>；状态转换<strong>仅由节拍脉冲的下降沿触发</strong>，与其他输入无关。

- 定长指令周期：状态周期与节拍电位只由节拍脉冲分频得到。
- 变长指令周期：还要以<strong>指令译码信号</strong>和<strong>反馈信息信号</strong>为输入，在取指周期的最后一个状态按指令类型分支。
- 还需启动、停止、复位信号，保证节拍电位从第一个节拍到最后一个节拍完整走完。

以五条典型指令的变长三级时序为例，状态划分为：取指 4 拍（S0–S3）、计算 2 拍（S4–S5）、执行 3 拍（S6–S8）。S3 处按译码结果分支：LW/SW/BEQ 进入 S4（需要访存计算地址），ADD/ADDI 直接进入 S6（无计算周期）；S5 进入 S6，S8 返回 S0。状态编码用 4 位（$2^4=16$ 种组合中取 9 种）。

输出信号的逻辑表达式由状态编码相“或”得到，例如执行周期信号与节拍电位 T3：

$$
MEX = S6 + S7 + S8
$$

$$
T3 = S2 + S8
$$

设计流程共四步：<strong>画三级时序图 → 构造摩尔型 FSM（状态转换图）→ 写状态转换表 → 用状态寄存器 + 组合逻辑 + 节拍脉冲构建时序发生器</strong>。

<figure class="cpd-figure not-content" id="hco-w5-fig-8">
  <svg viewBox="0 0 700 356" role="img" aria-label="变长三级时序摩尔型有限状态机状态转换图与三级时序波形层级">
    <defs>
      <marker id="hco-w5-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="428" height="282" rx="6" class="fig-box-subtle"/>
    <rect x="456" y="36" width="228" height="282" rx="6" class="fig-box-subtle"/>
    <line x1="94" y1="92" x2="136" y2="92" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <line x1="184" y1="92" x2="226" y2="92" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <line x1="274" y1="92" x2="316" y2="92" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <path d="M 356 108 Q 392 122 392 142" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <path d="M 332 114 Q 308 184 294 248" class="fig-line-accent" marker-end="url(#hco-w5-arr-8)"/>
    <line x1="392" y1="188" x2="392" y2="216" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <path d="M 372 250 Q 342 268 314 272" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <line x1="268" y1="272" x2="214" y2="272" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <line x1="166" y1="272" x2="114" y2="272" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <path d="M 68 272 Q 30 182 52 108" class="fig-line" marker-end="url(#hco-w5-arr-8)"/>
    <line x1="504" y1="74" x2="504" y2="196" class="fig-line-dashed"/>
    <line x1="544" y1="74" x2="544" y2="196" class="fig-line-dashed"/>
    <line x1="584" y1="74" x2="584" y2="196" class="fig-line-dashed"/>
    <line x1="624" y1="74" x2="624" y2="196" class="fig-line-dashed"/>
    <line x1="664" y1="74" x2="664" y2="196" class="fig-line-dashed"/>
    <path d="M 494 98 L 504 98 L 504 80 L 664 80 L 664 98 L 672 98" class="fig-line" stroke="var(--sl-color-blue)"/>
    <path d="M 494 128 L 504 128 L 504 112 L 544 112 L 544 128 L 672 128" class="fig-line" stroke="var(--sl-color-purple)"/>
    <path d="M 494 156 L 544 156 L 544 140 L 584 140 L 584 156 L 672 156" class="fig-line" stroke="var(--sl-color-purple)"/>
    <path d="M 494 190 L 504 190 L 504 174 L 524 174 L 524 190 L 544 190 L 544 174 L 564 174 L 564 190 L 584 190 L 584 174 L 604 174 L 604 190 L 624 190 L 624 174 L 644 174 L 644 190 L 672 190" class="fig-line-accent"/>
    <circle cx="72" cy="92" r="22" class="fig-box-blue"/>
    <circle cx="160" cy="92" r="22" class="fig-box-blue"/>
    <circle cx="250" cy="92" r="22" class="fig-box-blue"/>
    <circle cx="340" cy="92" r="22" class="fig-box-blue"/>
    <circle cx="392" cy="166" r="22" class="fig-box-purple"/>
    <circle cx="392" cy="240" r="22" class="fig-box-purple"/>
    <circle cx="290" cy="272" r="22" class="fig-box-green"/>
    <circle cx="190" cy="272" r="22" class="fig-box-green"/>
    <circle cx="90" cy="272" r="22" class="fig-box-green"/>
    <rect x="272" y="172" width="76" height="18" rx="3" class="fig-box"/>
    <rect x="356" y="114" width="78" height="18" rx="3" class="fig-box"/>
    <rect x="468" y="210" width="204" height="96" rx="4" class="fig-box"/>
    <text x="16" y="24" font-size="13" class="fig-title">变长三级时序摩尔型 FSM 状态转换图与时序波形层级</text>
    <text x="28" y="54" font-size="11" class="fig-muted">（a）五条典型指令的状态转移（9 状态 → 4 位编码）</text>
    <text x="72" y="97" font-size="12" text-anchor="middle" class="fig-blue">S0</text>
    <text x="160" y="97" font-size="12" text-anchor="middle" class="fig-blue">S1</text>
    <text x="250" y="97" font-size="12" text-anchor="middle" class="fig-blue">S2</text>
    <text x="340" y="97" font-size="12" text-anchor="middle" class="fig-blue">S3</text>
    <text x="392" y="171" font-size="12" text-anchor="middle" class="fig-purple">S4</text>
    <text x="392" y="245" font-size="12" text-anchor="middle" class="fig-purple">S5</text>
    <text x="290" y="277" font-size="12" text-anchor="middle" class="fig-green">S6</text>
    <text x="190" y="277" font-size="12" text-anchor="middle" class="fig-green">S7</text>
    <text x="90" y="277" font-size="12" text-anchor="middle" class="fig-green">S8</text>
    <text x="206" y="128" font-size="11" text-anchor="middle" class="fig-blue">取指 MIF（4 拍）</text>
    <text x="342" y="208" font-size="10" text-anchor="middle" class="fig-purple">计算 MCL</text>
    <text x="190" y="308" font-size="11" text-anchor="middle" class="fig-green">执行 MEX（3 拍）</text>
    <text x="395" y="127" font-size="10" text-anchor="middle" class="fig-muted">LW/SW/BEQ</text>
    <text x="310" y="185" font-size="10" text-anchor="middle" class="fig-accent">ADD/ADDI</text>
    <text x="64" y="192" font-size="10" class="fig-muted">回取指</text>
    <text x="468" y="54" font-size="11" class="fig-muted">（b）三级时序波形与摩尔输出</text>
    <text x="468" y="92" font-size="10" class="fig-blue">MIF</text>
    <text x="468" y="124" font-size="10" class="fig-purple">T1</text>
    <text x="468" y="152" font-size="10" class="fig-purple">T2</text>
    <text x="468" y="186" font-size="10" class="fig-accent">CLK</text>
    <text x="570" y="229" font-size="11" text-anchor="middle" class="fig-label">摩尔机输出仅由现态相“或”决定</text>
    <text x="480" y="249" font-size="10" class="fig-blue">MIF = S0 + S1 + S2 + S3</text>
    <text x="480" y="266" font-size="10" class="fig-purple">MCL = S4 + S5</text>
    <text x="480" y="283" font-size="10" class="fig-green">MEX = S6 + S7 + S8</text>
    <text x="480" y="299" font-size="10" class="fig-muted">T3 = S2 + S8；CLK 下降沿切态</text>
    <text x="16" y="340" font-size="11" class="fig-muted">注：S3 末尾依据指令译码结果决定进入 S4（需计算访存/分支地址）或直达 S6；状态转换统一在节拍脉冲下降沿触发。</text>
  </svg>
  <figcaption>图 6.8：变长三级时序摩尔型 FSM 状态转换图与三级时序（状态周期—节拍电位—脉冲）层级</figcaption>
</figure>

> <strong>★考点</strong>：摩尔型 FSM 的两个特征（输出仅由当前状态决定、状态仅由时钟边沿触发）；状态数、编码位数（$\lceil \log_2 N \rceil$）；变长时序分支的条件来自指令译码与反馈信号。

### 6.7 硬布线控制器及设计流程

硬布线控制器由<strong>时序发生器 + 状态寄存器 + 组合逻辑单元</strong>构成。组合逻辑单元的输入是<strong>指令译码信号、状态周期信号、节拍电位信号、反馈信息信号（如 ZF）</strong>，输出是<strong>微操作控制信号序列</strong>。它用固定硬件电路直接产生控制信号，速度最快。

设计流程五步：

1. 分析每条指令执行时的数据通路，写出操作流程与所需控制信号。
2. 把每个微操作划分到相应的状态周期与节拍电位上（定时控制）。
3. 按定长/变长同步方式构造时序发生器。
4. 对每个控制信号，分析它在不同指令、不同状态周期、不同节拍下是否有效，写出逻辑表达式。
5. 用逻辑门、PLA 或 ROM 实现这些表达式。

例：控制信号 `xin`（暂存器写入）在取指周期 T1 对所有指令有效；计算周期 T1 只对 LW/SW/BEQ 有效；执行周期 T1 只对 BEQ/ADDI/ADD 有效：

$$
\text{xin} = MIF \cdot T1 + MCL \cdot T1 \cdot (LW + SW + BEQ) + MEX \cdot T1 \cdot (BEQ + ADDI + ADD)
$$

`PCin` 在 BEQ 指令执行周期 T3、且 ZF=1（分支成立）时有效，这里用到了反馈信号：

$$
\text{PCin} = MEX \cdot T3 \cdot BEQ \cdot ZF
$$

> <strong>注意</strong>：不是每个控制信号的表达式都含反馈信号。`xin` 与 FB 无关，`PCin` 才需要 ZF。写表达式时按“与项相加”的范式，每一项是“状态周期 × 节拍 × 指令”的与。

<figure class="cpd-figure not-content" id="hco-w5-fig-9">
  <svg viewBox="0 0 700 340" role="img" aria-label="硬布线控制器的硬件组成与组合逻辑布尔表达式矩阵结构">
    <defs>
      <marker id="hco-w5-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="444" height="266" rx="6" class="fig-box-subtle"/>
    <rect x="472" y="36" width="212" height="266" rx="6" class="fig-box-subtle"/>
    <line x1="124" y1="74" x2="156" y2="74" class="fig-line" marker-end="url(#hco-w5-arr-9)"/>
    <line x1="124" y1="110" x2="156" y2="110" class="fig-line" marker-end="url(#hco-w5-arr-9)"/>
    <line x1="124" y1="146" x2="156" y2="146" class="fig-line" marker-end="url(#hco-w5-arr-9)"/>
    <line x1="124" y1="182" x2="156" y2="182" class="fig-line" marker-end="url(#hco-w5-arr-9)"/>
    <line x1="312" y1="128" x2="346" y2="128" class="fig-line-accent" marker-end="url(#hco-w5-arr-9)"/>
    <polyline points="90,232 90,212 184,212 184,202" class="fig-line" marker-end="url(#hco-w5-arr-9)"/>
    <line x1="230" y1="232" x2="230" y2="202" class="fig-line" marker-end="url(#hco-w5-arr-9)"/>
    <line x1="282" y1="200" x2="282" y2="230" class="fig-line-dashed" marker-end="url(#hco-w5-arr-9)"/>
    <rect x="28" y="60" width="96" height="28" rx="4" class="fig-box-blue"/>
    <rect x="28" y="96" width="96" height="28" rx="4" class="fig-box-purple"/>
    <rect x="28" y="132" width="96" height="28" rx="4" class="fig-box-purple"/>
    <rect x="28" y="168" width="96" height="28" rx="4" class="fig-box-orange"/>
    <rect x="158" y="56" width="154" height="144" rx="5" class="fig-box-accent"/>
    <rect x="348" y="92" width="100" height="72" rx="4" class="fig-box-green"/>
    <rect x="28" y="232" width="128" height="54" rx="4" class="fig-box"/>
    <rect x="184" y="232" width="144" height="54" rx="4" class="fig-box-purple"/>
    <rect x="484" y="62" width="188" height="104" rx="4" class="fig-box"/>
    <rect x="484" y="176" width="188" height="110" rx="4" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">硬布线控制器（组合逻辑 CU）结构与控制方程生成原理</text>
    <text x="28" y="52" font-size="11" class="fig-muted">（a）硬件框图（纯门电路直接译码）</text>
    <text x="76" y="78" font-size="11" text-anchor="middle" class="fig-blue">指令译码 ID</text>
    <text x="76" y="114" font-size="11" text-anchor="middle" class="fig-purple">状态周期 M</text>
    <text x="76" y="150" font-size="11" text-anchor="middle" class="fig-purple">节拍电位 T</text>
    <text x="76" y="186" font-size="11" text-anchor="middle" class="fig-orange">反馈标志 FB</text>
    <text x="235" y="116" font-size="12" text-anchor="middle" class="fig-accent">组合逻辑阵列</text>
    <text x="235" y="136" font-size="10" text-anchor="middle" class="fig-label">与-或门阵列 / PLA</text>
    <text x="235" y="154" font-size="10" text-anchor="middle" class="fig-muted">Ci = Σ(Mj · Tk · Im)</text>
    <text x="398" y="118" font-size="12" text-anchor="middle" class="fig-green">各执行部件</text>
    <text x="398" y="136" font-size="10" text-anchor="middle" class="fig-muted">ALU / GPR</text>
    <text x="398" y="152" font-size="10" text-anchor="middle" class="fig-muted">MAR / 主存</text>
    <text x="92" y="255" font-size="11" text-anchor="middle" class="fig-label">时序发生器</text>
    <text x="92" y="273" font-size="10" text-anchor="middle" class="fig-muted">脉冲 + 启停复位</text>
    <text x="256" y="255" font-size="11" text-anchor="middle" class="fig-purple">状态寄存器</text>
    <text x="256" y="273" font-size="10" text-anchor="middle" class="fig-muted">下降沿：次态→现态</text>
    <text x="212" y="220" font-size="9" text-anchor="middle" class="fig-muted">现态</text>
    <text x="300" y="220" font-size="9" text-anchor="middle" class="fig-muted">次态</text>
    <text x="342" y="258" font-size="10" class="fig-muted">零控存访存</text>
    <text x="342" y="274" font-size="10" class="fig-muted">速度最快</text>
    <text x="484" y="52" font-size="11" class="fig-muted">（b）与-或范式实例</text>
    <text x="494" y="82" font-size="11" class="fig-label">① 无条件微命令 xin：</text>
    <text x="494" y="102" font-size="10" class="fig-blue">MIF · T1</text>
    <text x="494" y="120" font-size="10" class="fig-purple">+ MCL · T1 · (LW+SW+BEQ)</text>
    <text x="494" y="138" font-size="10" class="fig-green">+ MEX · T1 · (BEQ+ADD+...)</text>
    <text x="494" y="156" font-size="9" class="fig-muted">与状态标志位无关</text>
    <text x="494" y="196" font-size="11" class="fig-orange">② 条件微命令 PCin：</text>
    <text x="494" y="218" font-size="11" class="fig-label">MEX · T3 · BEQ · ZF</text>
    <text x="494" y="242" font-size="10" class="fig-muted">仅当 ZF=1 时改写 PC</text>
    <text x="494" y="260" font-size="10" class="fig-muted">扩充指令需重布硬件</text>
    <text x="494" y="276" font-size="10" class="fig-muted">典型代表：RISC 处理器</text>
    <text x="16" y="326" font-size="11" class="fig-muted">注：硬布线控制器把每个控制信号写成“状态周期 × 节拍 × 指令译码（× 反馈标志）”的积之和，由组合门电路直接驱动。</text>
  </svg>
  <figcaption>图 6.9：硬布线控制器组成框图与微操作控制信号布尔逻辑表达式示例</figcaption>
</figure>

<strong>特点</strong>：速度快（无需访问控制存储器），但控制逻辑由硬件固定，结构复杂、修改和扩展困难，增加一条指令就要重新设计电路；适合指令少、格式规整的 RISC 处理器。

### 6.8 微程序控制器：概念、组成与微指令格式

<strong>核心思想</strong>：模仿程序设计，把每条指令执行所需的控制信号按规则编码成“微指令”，为每条指令编写一段“微程序”，全部存放在 CPU 内的控制存储器（控存）中。控制器执行微程序，就按顺序产生该指令的全部控制信号——这是<strong>软件时序</strong>控制，替代了硬布线的硬件时序。

| 概念 | 含义 |
|---|---|
| 微命令 | 控制部件向执行部件发出的控制信号（如 PCout） |
| 微操作 | 执行部件收到微命令后完成的最基本操作 |
| 相容性微操作 | 可在同一机器周期（同一时钟周期）内并行执行 |
| 互斥性微操作 | 不能并行，如同时向单总线输出、同时读写存储 |
| 微指令 | 一个机器周期（现代时序为一个时钟周期）内的一组相容微命令 + 顺序控制信息 |
| 微程序 | 实现一条机器指令功能的微指令序列 |

只有<strong>相容</strong>的微命令才能出现在同一条微指令中。现代时序取消机器周期后，相容/互斥以一个<strong>时钟周期</strong>为单位判断。

微指令由两大部分组成：

| 字段 | 作用 |
|---|---|
| 操作控制字段 | 每个微命令占一位（或编码），1 表示发出该微命令 |
| 顺序控制字段 · 判别测试字段 | P0 为指令译码测试位；P1 检测外部状态（如 ZF）；PN 表示微程序最后一条 |
| 顺序控制字段 · 下址字段 | 给出下一条微指令的地址 |

<strong>微程序控制器组成</strong>：控制存储器、微地址寄存器（μAR）、地址转移逻辑，以及微指令寄存器（μIR，暂存从控存读出的微指令，程序员不可见）。

- 控存字长 = 微指令字长；容量取决于所有微指令的总数。
- 微地址 0 号单元存放<strong>取指微程序</strong>的第一条微指令，系统上电自动从此执行；取出指令后按译码结果跳转到对应微程序入口。
- 取指微程序的最后一条微指令 P0=1，表示不检查其他条件、直接按指令译码分支。
- μAR 在时钟脉冲触发下更新（三级时序在状态周期最后一个节拍结束时，现代时序在当前时钟周期结束时）；若控制信号上升沿有效，则 μAR 在下降沿更新。
- 控存通常用 ROM；现代 CPU 也可用 RAM/Flash 以支持微程序更新（如修复漏洞）。

<strong>微指令周期：从控存取出并执行一条微指令所需的时间。串行执行方式下，传统三级时序把它设计为一个机器周期，现代时序则为一个 CPU 时钟周期。指令的微程序有多少条微指令，就占多少个时钟周期，不要与指令周期混淆</strong>。

| 对比项 | 硬布线控制器 | 微程序控制器 |
|---|---|---|
| 控制信号产生 | 组合逻辑直接产生 | 从控存读出微指令产生 |
| 速度 | 快，无控存访问时延 | 慢，多一次控存访问 |
| 修改/扩展 | 改电路，难、成本高 | 改微程序，灵活 |
| 典型适用 | RISC（如 ARM） | CISC、复杂控制 |

<figure class="cpd-figure not-content" id="hco-w5-fig-10">
  <svg viewBox="0 0 700 356" role="img" aria-label="微程序控制器的硬件组成：微地址寄存器CMAR、控制存储器CM、微指令寄存器CMDR与地址转移逻辑">
    <defs>
      <marker id="hco-w5-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="668" height="282" rx="6" class="fig-box-subtle"/>
    <line x1="142" y1="108" x2="174" y2="108" class="fig-line" marker-end="url(#hco-w5-arr-10)"/>
    <line x1="340" y1="108" x2="374" y2="108" class="fig-line" marker-end="url(#hco-w5-arr-10)"/>
    <line x1="451" y1="156" x2="451" y2="180" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w5-arr-10)"/>
    <polyline points="590,156 590,252 422,252" class="fig-line" marker-end="url(#hco-w5-arr-10)"/>
    <line x1="476" y1="284" x2="422" y2="284" class="fig-line" marker-end="url(#hco-w5-arr-10)"/>
    <polyline points="196,268 89,268 89,142" class="fig-line-accent" marker-end="url(#hco-w5-arr-10)"/>
    <rect x="36" y="76" width="106" height="64" rx="4" class="fig-box-blue"/>
    <rect x="176" y="56" width="164" height="108" rx="5" class="fig-box-purple"/>
    <rect x="188" y="84" width="140" height="22" rx="3" class="fig-box"/>
    <rect x="188" y="110" width="140" height="22" rx="3" class="fig-box"/>
    <rect x="188" y="136" width="140" height="22" rx="3" class="fig-box"/>
    <rect x="376" y="64" width="292" height="92" rx="5" class="fig-box"/>
    <rect x="386" y="96" width="130" height="48" rx="3" class="fig-box-green"/>
    <rect x="520" y="96" width="64" height="48" rx="3" class="fig-box-orange"/>
    <rect x="588" y="96" width="70" height="48" rx="3" class="fig-box-blue"/>
    <rect x="386" y="182" width="130" height="40" rx="4" class="fig-box-green"/>
    <rect x="196" y="236" width="224" height="66" rx="5" class="fig-box-accent"/>
    <rect x="476" y="264" width="192" height="40" rx="4" class="fig-box-orange"/>
    <rect x="96" y="258" width="84" height="18" rx="3" class="fig-box"/>
    <rect x="484" y="242" width="90" height="18" rx="3" class="fig-box"/>
    <text x="16" y="24" font-size="13" class="fig-title">微程序控制器核心架构（CMAR → 控存 CM → CMDR → 地址转移逻辑）</text>
    <text x="89" y="102" font-size="12" text-anchor="middle" class="fig-blue">μAR (CMAR)</text>
    <text x="89" y="122" font-size="10" text-anchor="middle" class="fig-muted">微地址寄存器</text>
    <text x="158" y="98" font-size="10" text-anchor="middle" class="fig-muted">地址</text>
    <text x="258" y="75" font-size="12" text-anchor="middle" class="fig-purple">控制存储器 CM (ROM)</text>
    <text x="258" y="99" font-size="10" text-anchor="middle" class="fig-label">00H：公用取指微程序</text>
    <text x="258" y="125" font-size="10" text-anchor="middle" class="fig-muted">xxH：各机器指令微程序</text>
    <text x="258" y="151" font-size="10" text-anchor="middle" class="fig-muted">yyH：中断响应微程序</text>
    <text x="522" y="84" font-size="12" text-anchor="middle" class="fig-label">μIR (CMDR) 微指令寄存器（字长 = 控存字长）</text>
    <text x="451" y="116" font-size="11" text-anchor="middle" class="fig-green">操作控制字段</text>
    <text x="451" y="133" font-size="10" text-anchor="middle" class="fig-muted">相容微命令位</text>
    <text x="552" y="116" font-size="11" text-anchor="middle" class="fig-orange">判别 P</text>
    <text x="552" y="133" font-size="10" text-anchor="middle" class="fig-muted">P0/P1</text>
    <text x="623" y="116" font-size="11" text-anchor="middle" class="fig-blue">下址</text>
    <text x="623" y="133" font-size="10" text-anchor="middle" class="fig-muted">NextAddr</text>
    <text x="451" y="206" font-size="11" text-anchor="middle" class="fig-green">驱动各部件控制点</text>
    <text x="308" y="262" font-size="12" text-anchor="middle" class="fig-accent">微地址形成 / 地址转移逻辑</text>
    <text x="308" y="282" font-size="9.2" text-anchor="middle" class="fig-muted">P0=1 按操作码映射入口 · 否则按下址/条件分支</text>
    <text x="529" y="255" font-size="10" text-anchor="middle" class="fig-muted">顺序控制信息</text>
    <text x="572" y="282" font-size="11" text-anchor="middle" class="fig-orange">IR 操作码 · 状态 ZF · 中断</text>
    <text x="572" y="297" font-size="9" text-anchor="middle" class="fig-muted">外部测试与映射输入</text>
    <text x="138" y="271" font-size="10" text-anchor="middle" class="fig-accent">下一条微地址</text>
    <text x="16" y="340" font-size="11" class="fig-muted">注：上电复位 μAR←0 执行取指微程序；取指末条微指令置 P0=1，由地址转移逻辑按 IR 操作码跳入对应指令微程序段。</text>
  </svg>
  <figcaption>图 6.10：微程序控制器内部数据流与控存（CM）微程序分段结构</figcaption>
</figure>

> <strong>★考点</strong>：控存与主存的区别——控存在 CPU 内、按微地址访问、存微指令、通常用 ROM；主存在 CPU 外、按地址访问、存指令和数据。

### 6.9 微指令设计：下址字段法与计数器法

以单总线数据通路的 22 个微操作控制点为例，操作控制字段需要 22 位，每一位对应一个微命令。其中 IR 向总线输出的控制点要细分为两个：`IRout(I)` 输出立即数、`IRout(A)` 输出分支目标地址，否则无法区分两种来源。

| 顺序控制方式 | 格式 | 特点 |
|---|---|---|
| 下址字段法 | 判别测试字段（P0、P1）+ 下址字段 | 每条微指令直接给出下一条地址；5 位下址可寻址 $2^5 = 32$ 条微指令 |
| 计数器法 | 判别测试字段（P0、P1、PN），无下址字段 | 微地址顺序加 1；用 PN 标明最后一条，结束后返回取指微程序 |

操作控制字段有三种表示方法：

- <strong>直接表示法</strong>：每个微命令一位，并行性最强、微指令最长。
- <strong>编码表示法（字段译码法）</strong>：把互斥的微命令分组编码，经译码器产生，缩短字长但增加译码延迟。
- <strong>混合表示法</strong>：两者结合，现代处理器常用。

| 对比项 | 水平型微指令 | 垂直型微指令 |
|---|---|---|
| 设计思想 | 面向数据通路，直接控制硬件信号 | 面向操作算法，接近机器指令 |
| 格式 | 操作控制字段 + 顺序控制字段 | 微操作码 + 微地址码 |
| 并行性 | 一条可控制数十个微操作 | 一条只控制 1–2 个微操作 |
| 微程序长度 | 短，占用控存小 | 长，占用控存大 |
| 可读性/编程 | 类似二进制位操作，难读难写 | 类似汇编，易读易写 |
| 应用 | 追求性能的 CISC、浮点单元、早期 GPU/DSP | 资源受限的 MCU、RISC 异常处理、简单 I/O 控制器 |

垂直型微指令因性能问题已基本被淘汰；现代处理器采用<strong>以水平型为主、局部垂直型优化</strong>的混合方案。

<figure class="cpd-figure not-content" id="hco-w5-fig-11">
  <svg viewBox="0 0 700 262" role="img" aria-label="微指令顺序控制的下址字段法与计数器法字段结构对比图">
    <rect x="16" y="36" width="668" height="190" rx="6" class="fig-box-subtle"/>
    <rect x="34" y="68" width="320" height="44" rx="3" class="fig-box-green"/>
    <rect x="358" y="68" width="144" height="44" rx="3" class="fig-box-orange"/>
    <rect x="506" y="68" width="160" height="44" rx="3" class="fig-box-blue"/>
    <rect x="34" y="152" width="320" height="44" rx="3" class="fig-box-green"/>
    <rect x="358" y="152" width="196" height="44" rx="3" class="fig-box-orange"/>
    <rect x="558" y="152" width="108" height="44" rx="3" class="fig-box-dashed"/>
    <text x="16" y="24" font-size="13" class="fig-title">微指令顺序控制格式对比：断定法（下址字段法）vs 计数器法</text>
    <text x="34" y="58" font-size="11" class="fig-muted">（a）下址字段法（断定方式：22 + 2 + 5 = 29 位）</text>
    <text x="194" y="88" font-size="12" text-anchor="middle" class="fig-green">操作控制字段（22 位）</text>
    <text x="194" y="104" font-size="10" text-anchor="middle" class="fig-muted">含 IRout(I) 与 IRout(A) 细分位</text>
    <text x="430" y="88" font-size="12" text-anchor="middle" class="fig-orange">判别测试字段</text>
    <text x="430" y="104" font-size="10" text-anchor="middle" class="fig-muted">P0（译码）· P1（状态）</text>
    <text x="586" y="88" font-size="12" text-anchor="middle" class="fig-blue">下址字段（5 位）</text>
    <text x="586" y="104" font-size="10" text-anchor="middle" class="fig-muted">直接寻址 2^5 = 32 条</text>
    <text x="34" y="142" font-size="11" class="fig-muted">（b）计数器法（顺序 μAR + 1 方式：省去下址字段，增设 PN 结束位）</text>
    <text x="194" y="172" font-size="12" text-anchor="middle" class="fig-green">操作控制字段（22 位）</text>
    <text x="194" y="188" font-size="10" text-anchor="middle" class="fig-muted">直接控制各执行部件</text>
    <text x="456" y="172" font-size="12" text-anchor="middle" class="fig-orange">判别测试字段（含 PN）</text>
    <text x="456" y="188" font-size="10" text-anchor="middle" class="fig-muted">P0 · P1 · PN（末条标志）</text>
    <text x="612" y="172" font-size="11" text-anchor="middle" class="fig-muted">省去下址字段</text>
    <text x="612" y="188" font-size="10" text-anchor="middle" class="fig-muted">默认 (μAR)+1</text>
    <text x="34" y="216" font-size="10" class="fig-muted">P0=1：取指微程序末条按操作码跳转入口；PN=1：当前微程序执行完毕，转中断检测或回 00H 取指。</text>
    <text x="16" y="248" font-size="11" class="fig-muted">注：下址字段法跳转灵活、无顺序限制，但控存字长较宽；计数器法缩短微指令字长，非顺序跳转依赖地址转移逻辑。</text>
  </svg>
  <figcaption>图 6.11：微指令顺序控制的下址字段法（断定法）与计数器法格式对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w5-fig-12">
  <svg viewBox="0 0 700 292" role="img" aria-label="水平型微指令直接编码与字段直接编码互斥分组译码原理对比图">
    <defs>
      <marker id="hco-w5-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="320" height="218" rx="6" class="fig-box-subtle"/>
    <rect x="348" y="36" width="336" height="218" rx="6" class="fig-box-subtle"/>
    <line x1="60" y1="104" x2="60" y2="132" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="112" y1="104" x2="112" y2="132" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="164" y1="104" x2="164" y2="132" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="422" y1="104" x2="422" y2="128" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="534" y1="104" x2="534" y2="128" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="396" y1="164" x2="396" y2="188" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="422" y1="164" x2="422" y2="188" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="448" y1="164" x2="448" y2="188" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="516" y1="164" x2="516" y2="188" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <line x1="552" y1="164" x2="552" y2="188" class="fig-line" marker-end="url(#hco-w5-arr-12)"/>
    <rect x="34" y="68" width="52" height="36" rx="3" class="fig-box-green"/>
    <rect x="86" y="68" width="52" height="36" rx="3" class="fig-box"/>
    <rect x="138" y="68" width="52" height="36" rx="3" class="fig-box-green"/>
    <rect x="190" y="68" width="130" height="36" rx="3" class="fig-box"/>
    <rect x="34" y="176" width="286" height="64" rx="4" class="fig-box"/>
    <rect x="366" y="68" width="112" height="36" rx="3" class="fig-box-purple"/>
    <rect x="484" y="68" width="100" height="36" rx="3" class="fig-box-purple"/>
    <rect x="590" y="68" width="80" height="36" rx="3" class="fig-box-blue"/>
    <rect x="372" y="130" width="100" height="34" rx="4" class="fig-box-accent"/>
    <rect x="488" y="130" width="92" height="34" rx="4" class="fig-box-accent"/>
    <rect x="586" y="130" width="88" height="110" rx="4" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">操作控制字段编码方式：直接表示法 vs 字段直接编码（互斥分组译码）</text>
    <text x="28" y="54" font-size="11" class="fig-muted">（a）直接表示法（1 位 = 1 个微命令）</text>
    <text x="60" y="90" font-size="11" text-anchor="middle" class="fig-green">1</text>
    <text x="112" y="90" font-size="11" text-anchor="middle" class="fig-muted">0</text>
    <text x="164" y="90" font-size="11" text-anchor="middle" class="fig-green">1</text>
    <text x="255" y="90" font-size="11" text-anchor="middle" class="fig-muted">… 共 N 位</text>
    <text x="60" y="148" font-size="10" text-anchor="middle" class="fig-green">R0out</text>
    <text x="112" y="148" font-size="10" text-anchor="middle" class="fig-muted">PCout</text>
    <text x="164" y="148" font-size="10" text-anchor="middle" class="fig-green">MARin</text>
    <text x="177" y="196" font-size="11" text-anchor="middle" class="fig-label">无需译码器 · 速度最快 · 控存最宽</text>
    <text x="177" y="214" font-size="10" text-anchor="middle" class="fig-muted">垂直型微指令则类似机器指令（微操作码）</text>
    <text x="177" y="230" font-size="10" text-anchor="middle" class="fig-muted">每条仅发 1–2 个微命令，微程序长、效率低</text>
    <text x="360" y="54" font-size="11" class="fig-muted">（b）字段直接编码法（分段互斥 · 段间相容）</text>
    <text x="422" y="84" font-size="11" text-anchor="middle" class="fig-purple">字段 F1 (3b)</text>
    <text x="422" y="98" font-size="9" text-anchor="middle" class="fig-muted">7个互斥微命令</text>
    <text x="534" y="84" font-size="11" text-anchor="middle" class="fig-purple">字段 F2 (2b)</text>
    <text x="534" y="98" font-size="9" text-anchor="middle" class="fig-muted">3个互斥微命令</text>
    <text x="630" y="89" font-size="11" text-anchor="middle" class="fig-blue">下址/判别</text>
    <text x="422" y="151" font-size="11" text-anchor="middle" class="fig-accent">3-8 译码器</text>
    <text x="534" y="151" font-size="11" text-anchor="middle" class="fig-accent">2-4 译码器</text>
    <text x="422" y="204" font-size="10" text-anchor="middle" class="fig-label">互斥组1（如各Xout）</text>
    <text x="534" y="204" font-size="10" text-anchor="middle" class="fig-label">互斥组2（ALU）</text>
    <text x="460" y="232" font-size="10" text-anchor="middle" class="fig-muted">段内互斥，段间相容并行输出</text>
    <text x="630" y="152" font-size="11" text-anchor="middle" class="fig-orange">★编码铁律</text>
    <text x="630" y="172" font-size="10" text-anchor="middle" class="fig-label">每段必留 1 个</text>
    <text x="630" y="188" font-size="9" text-anchor="middle" class="fig-label">全0“不发命令”</text>
    <text x="630" y="208" font-size="10" text-anchor="middle" class="fig-orange">k位最多编码</text>
    <text x="630" y="224" font-size="11" text-anchor="middle" class="fig-orange">2^k - 1 个</text>
    <text x="16" y="278" font-size="11" class="fig-muted">408 高频公式：若某互斥分组含 n 个微命令，加上“空操作（NOP）”状态共 n+1 种，该字段至少需 ⌈log2(n + 1)⌉ 位。</text>
  </svg>
  <figcaption>图 6.12：水平型微指令的直接表示法与字段直接编码法（互斥分组留空状态规则）对比</figcaption>
</figure>

> <strong>注意</strong>：下址字段法需要为每条微指令保存一个后继地址；计数器法省去下址字段、微指令更短，但顺序执行以外的转移必须靠判别测试与地址转移逻辑完成，且必须增设 PN 位判断微程序结束。

### 6.10 异常与中断：响应流程与控制器改造

CPU 在<strong>每条指令执行结束时</strong>检查中断请求。若允许响应（开中断），进入中断响应：

1. <strong>关中断</strong>：中断使能位 IE 清零。IE 与中断请求信号经与门送入控制器，IE=0 相当于关闭与门，控制器不再看到请求。
2. <strong>保存断点</strong>：PC 的值（返回后应继续执行的地址）经内部总线送入<strong>异常指令地址寄存器 EPC</strong>。
3. <strong>转入口</strong>：中断服务程序的入口地址经内部总线送入 PC。
4. <strong>执行服务程序</strong>：从入口地址开始取指，进入正常的取指—译码—执行循环。
5. <strong>中断返回</strong>：服务程序最后安排一条中断返回指令（MIPS32 为 <strong>ERET</strong>），完成 `EPC→PC`（返回断点）、`IE←1`（开中断）、清除当前中断请求信号。这些操作可在一个节拍内完成。

<strong>硬布线控制器的改造</strong>：在状态转换图上增加中断响应周期两个状态——S9 完成关中断与保存断点，S10 完成入口地址送 PC；再把 ERET 作为一个分支加入状态图，安排在执行周期的最后一个节拍（S8），一拍完成返回断点、开中断与清请求。

<strong>微程序控制器的改造</strong>：操作控制字段增加 6 个中断相关微命令位；顺序控制字段增加 PN 位；新增 2 条中断响应微指令和 1 条中断返回微指令；地址转移逻辑中给多路选择器增加“中断响应微程序入口地址”输入端，选择条件为“当前微程序最后一条 + 有中断请求 + 无其他判别测试条件成立”。

> <strong>注意</strong>：传统三级时序划分固定，ERET 这种只需一拍的小指令也不得不占满三个节拍；现代时序按需分配，只需一个时钟周期，这是变长时序效率更高的直接体现。

<figure class="cpd-figure not-content" id="hco-w5-fig-13">
  <svg viewBox="0 0 700 366" role="img" aria-label="CPU中断响应与ERET中断返回流程及两类控制器中断改造要点">
    <defs>
      <marker id="hco-w5-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="424" height="292" rx="6" class="fig-box-subtle"/>
    <rect x="452" y="36" width="232" height="292" rx="6" class="fig-box-subtle"/>
    <line x1="136" y1="74" x2="154" y2="74" class="fig-line" marker-end="url(#hco-w5-arr-13)"/>
    <line x1="282" y1="74" x2="308" y2="74" class="fig-line" marker-end="url(#hco-w5-arr-13)"/>
    <line x1="218" y1="100" x2="218" y2="118" class="fig-line" marker-end="url(#hco-w5-arr-13)"/>
    <line x1="218" y1="164" x2="218" y2="180" class="fig-line" marker-end="url(#hco-w5-arr-13)"/>
    <line x1="218" y1="218" x2="218" y2="232" class="fig-line" marker-end="url(#hco-w5-arr-13)"/>
    <line x1="218" y1="268" x2="218" y2="282" class="fig-line" marker-end="url(#hco-w5-arr-13)"/>
    <polyline points="332,301 416,301 416,92" class="fig-line-accent" marker-end="url(#hco-w5-arr-13)"/>
    <rect x="28" y="56" width="108" height="36" rx="4" class="fig-box-blue"/>
    <polygon points="218,48 282,74 218,100 154,74" class="fig-box-orange"/>
    <rect x="310" y="56" width="118" height="36" rx="4" class="fig-box"/>
    <rect x="104" y="120" width="228" height="44" rx="4" class="fig-box-red"/>
    <rect x="104" y="182" width="228" height="36" rx="4" class="fig-box-purple"/>
    <rect x="104" y="234" width="228" height="34" rx="4" class="fig-box-blue"/>
    <rect x="104" y="284" width="228" height="34" rx="4" class="fig-box-green"/>
    <rect x="464" y="64" width="208" height="118" rx="4" class="fig-box"/>
    <rect x="464" y="194" width="208" height="122" rx="4" class="fig-box-purple"/>
    <text x="16" y="24" font-size="13" class="fig-title">中断响应（硬件隐指令）与中断返回（ERET）闭环及控制器改造</text>
    <text x="82" y="79" font-size="11" text-anchor="middle" class="fig-blue">指令执行完毕</text>
    <text x="218" y="78" font-size="10" text-anchor="middle" class="fig-orange">IRQ &amp; IE=1?</text>
    <text x="294" y="68" font-size="10" text-anchor="middle" class="fig-muted">否</text>
    <text x="369" y="79" font-size="11" text-anchor="middle" class="fig-label">取下一条指令</text>
    <text x="230" y="113" font-size="10" class="fig-muted">是</text>
    <text x="218" y="138" font-size="11" text-anchor="middle" class="fig-red">① 关中断 IE←0 · 保存断点 PC→EPC</text>
    <text x="218" y="155" font-size="10" text-anchor="middle" class="fig-muted">硬件自动完成（S9 拍 / 第1条响应微指令）</text>
    <text x="218" y="205" font-size="11" text-anchor="middle" class="fig-purple">② 中断服务入口地址 → PC（S10 拍）</text>
    <text x="218" y="255" font-size="11" text-anchor="middle" class="fig-blue">③ 取指并执行中断服务程序（ISR）</text>
    <text x="218" y="305" font-size="11" text-anchor="middle" class="fig-green">④ ERET：EPC→PC，IE←1，清请求</text>
    <text x="394" y="204" font-size="10" text-anchor="middle" class="fig-accent">返回断点</text>
    <text x="464" y="54" font-size="11" class="fig-muted">控制器硬件/微码改造对照</text>
    <text x="476" y="84" font-size="11" class="fig-label">① 硬布线控制器改造</text>
    <text x="476" y="104" font-size="10" class="fig-muted">· 新增响应状态 S9、S10（共 2 拍）</text>
    <text x="476" y="122" font-size="10" class="fig-muted">· ERET 挂入执行末拍 S8 分支</text>
    <text x="476" y="140" font-size="10" class="fig-muted">· 现代变长时序下 ERET 仅占 1 拍</text>
    <text x="476" y="158" font-size="10" class="fig-muted">· IE=0 关闭与门即可屏蔽外部请求</text>
    <text x="476" y="214" font-size="11" class="fig-purple">② 微程序控制器改造</text>
    <text x="476" y="234" font-size="10" class="fig-muted">· 操作控制字段增设 6 位中断微命令</text>
    <text x="476" y="252" font-size="10" class="fig-muted">· 顺序控制增设 PN 末条标识位</text>
    <text x="476" y="270" font-size="10" class="fig-muted">· 新增 2 条响应 + 1 条返回微指令</text>
    <text x="476" y="288" font-size="10" class="fig-muted">· 转移逻辑 MUX 增加中断微程序入口</text>
    <text x="16" y="350" font-size="11" class="fig-muted">注：关中断、保存断点与转服务入口由硬件中断隐指令原子完成；返回断点与重开中断由服务程序末尾的 ERET 指令完成。</text>
  </svg>
  <figcaption>图 6.13：异常与中断的响应—服务—返回闭环及两类控制器改造方案</figcaption>
</figure>

### 6.11 指令流水线：时空图与性能公式

指令流水线把一条指令的执行拆成若干<strong>功能段</strong>（MIPS 标准五段：IF 取指、ID 译码/读寄存器、EX 执行、MEM 访存、WB 写回），段间用<strong>流水寄存器</strong>锁存控制信号与数据，使多条指令重叠执行。理想情况下每个时钟周期流出一条指令，<strong>CPI = 1</strong>。

设流水线有 $k$ 段、连续输入 $n$ 条指令且无阻塞，时钟周期为 $\Delta t$：

$$
T = (k + n - 1)\Delta t
$$

$$
TP = \frac{n}{T} = \frac{n}{(k+n-1)\Delta t}
$$

$$
S = \frac{nk}{k+n-1}
$$

$$
E = \frac{n}{k+n-1}
$$

例如 5 段流水线执行 4 条指令，需要 $5+4-1=8$ 个时钟周期。

| 处理器类型 | 理想 CPI |
|---|---|
| 单周期 CPU | 1（每条指令固定一个时钟周期） |
| 多周期 CPU | > 1（一般 3–5 个时钟周期） |
| 基本流水线 CPU | 1（每周期流出一条） |
| 超标量（多发射）CPU | < 1（每周期流出多条） |

<strong>超流水线</strong>：把每个功能段再细分为两段（如 5 段 → 8 段），缩短每段延迟，从而提高主频。理想 CPI 仍为 1，但时钟周期减半，同样时间内流出指令数约翻倍。代价是深流水线一旦发生数据相关或分支预测失败，清空流水线的损失更大；流水寄存器更多、硬件开销更大。

<strong>多发射</strong>：复制译码逻辑、寄存器堆端口、运算器与旁路通路，使一个时钟周期能发射多条指令。例如 5 段流水线四发射，满载时同时有 $5 \times 4 = 20$ 条指令在流水线中，每周期流出 4 条，CPI ≈ 1/4。

- <strong>静态多发射</strong>：编译器在编译时解决冲突并打包指令（如超长指令字 VLIW），硬件简单，但跨平台通常需重新编译。
- <strong>动态多发射（超标量）</strong>：硬件在执行阶段动态处理冲突，兼容性强，编译器不优化也能正确运行。

<figure class="cpd-figure not-content" id="hco-w5-fig-14">
  <svg viewBox="0 0 700 340" role="img" aria-label="MIPS五段流水线段间锁存寄存器结构与5条指令9拍时空图">
    <rect x="16" y="36" width="668" height="54" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="100" width="668" height="202" rx="6" class="fig-box-subtle"/>
    <line x1="30" y1="63" x2="670" y2="63" class="fig-line"/>
    <rect x="28" y="46" width="82" height="34" rx="4" class="fig-box-blue"/>
    <rect x="118" y="44" width="40" height="38" rx="3" class="fig-box-purple"/>
    <rect x="166" y="46" width="82" height="34" rx="4" class="fig-box"/>
    <rect x="256" y="44" width="40" height="38" rx="3" class="fig-box-purple"/>
    <rect x="304" y="46" width="82" height="34" rx="4" class="fig-box-accent"/>
    <rect x="394" y="44" width="46" height="38" rx="3" class="fig-box-purple"/>
    <rect x="448" y="46" width="82" height="34" rx="4" class="fig-box-green"/>
    <rect x="538" y="44" width="48" height="38" rx="3" class="fig-box-purple"/>
    <rect x="594" y="46" width="76" height="34" rx="4" class="fig-box-orange"/>
    <rect x="92" y="132" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="154" y="132" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="216" y="132" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="278" y="132" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="340" y="132" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="154" y="164" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="216" y="164" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="278" y="164" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="340" y="164" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="402" y="164" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="216" y="196" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="278" y="196" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="340" y="196" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="402" y="196" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="464" y="196" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="278" y="228" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="340" y="228" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="402" y="228" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="464" y="228" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="526" y="228" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="340" y="260" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="402" y="260" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="464" y="260" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="526" y="260" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="588" y="260" width="56" height="26" rx="3" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">MIPS 五段流水线段间锁存器与时空图（k=5 段，n=5 条 → 9 拍完成）</text>
    <text x="69" y="67" font-size="11" text-anchor="middle" class="fig-blue">IF 取指</text>
    <text x="138" y="67" font-size="9" text-anchor="middle" class="fig-purple">IF/ID</text>
    <text x="207" y="67" font-size="11" text-anchor="middle" class="fig-label">ID 译码</text>
    <text x="276" y="67" font-size="9" text-anchor="middle" class="fig-purple">ID/EX</text>
    <text x="345" y="67" font-size="11" text-anchor="middle" class="fig-accent">EX 执行</text>
    <text x="417" y="67" font-size="9" text-anchor="middle" class="fig-purple">EX/MEM</text>
    <text x="489" y="67" font-size="11" text-anchor="middle" class="fig-green">MEM 访存</text>
    <text x="562" y="67" font-size="9" text-anchor="middle" class="fig-purple">MEM/WB</text>
    <text x="632" y="67" font-size="11" text-anchor="middle" class="fig-orange">WB 写回</text>
    <text x="34" y="122" font-size="10" class="fig-muted">功能段 \ 拍</text>
    <text x="120" y="122" font-size="10" text-anchor="middle" class="fig-muted">1</text>
    <text x="182" y="122" font-size="10" text-anchor="middle" class="fig-muted">2</text>
    <text x="244" y="122" font-size="10" text-anchor="middle" class="fig-muted">3</text>
    <text x="306" y="122" font-size="10" text-anchor="middle" class="fig-muted">4</text>
    <text x="368" y="122" font-size="10" text-anchor="middle" class="fig-accent">5(满载)</text>
    <text x="430" y="122" font-size="10" text-anchor="middle" class="fig-muted">6</text>
    <text x="492" y="122" font-size="10" text-anchor="middle" class="fig-muted">7</text>
    <text x="554" y="122" font-size="10" text-anchor="middle" class="fig-muted">8</text>
    <text x="616" y="122" font-size="10" text-anchor="middle" class="fig-muted">9</text>
    <text x="52" y="149" font-size="11" text-anchor="middle" class="fig-blue">IF</text>
    <text x="52" y="181" font-size="11" text-anchor="middle" class="fig-label">ID</text>
    <text x="52" y="213" font-size="11" text-anchor="middle" class="fig-accent">EX</text>
    <text x="52" y="245" font-size="11" text-anchor="middle" class="fig-green">MEM</text>
    <text x="52" y="277" font-size="11" text-anchor="middle" class="fig-orange">WB</text>
    <text x="120" y="149" font-size="11" text-anchor="middle" class="fig-blue">I1</text>
    <text x="182" y="149" font-size="11" text-anchor="middle" class="fig-blue">I2</text>
    <text x="244" y="149" font-size="11" text-anchor="middle" class="fig-blue">I3</text>
    <text x="306" y="149" font-size="11" text-anchor="middle" class="fig-blue">I4</text>
    <text x="368" y="149" font-size="11" text-anchor="middle" class="fig-blue">I5</text>
    <text x="182" y="181" font-size="11" text-anchor="middle" class="fig-label">I1</text>
    <text x="244" y="181" font-size="11" text-anchor="middle" class="fig-label">I2</text>
    <text x="306" y="181" font-size="11" text-anchor="middle" class="fig-label">I3</text>
    <text x="368" y="181" font-size="11" text-anchor="middle" class="fig-label">I4</text>
    <text x="430" y="181" font-size="11" text-anchor="middle" class="fig-label">I5</text>
    <text x="244" y="213" font-size="11" text-anchor="middle" class="fig-accent">I1</text>
    <text x="306" y="213" font-size="11" text-anchor="middle" class="fig-accent">I2</text>
    <text x="368" y="213" font-size="11" text-anchor="middle" class="fig-accent">I3</text>
    <text x="430" y="213" font-size="11" text-anchor="middle" class="fig-accent">I4</text>
    <text x="492" y="213" font-size="11" text-anchor="middle" class="fig-accent">I5</text>
    <text x="306" y="245" font-size="11" text-anchor="middle" class="fig-green">I1</text>
    <text x="368" y="245" font-size="11" text-anchor="middle" class="fig-green">I2</text>
    <text x="430" y="245" font-size="11" text-anchor="middle" class="fig-green">I3</text>
    <text x="492" y="245" font-size="11" text-anchor="middle" class="fig-green">I4</text>
    <text x="554" y="245" font-size="11" text-anchor="middle" class="fig-green">I5</text>
    <text x="368" y="277" font-size="11" text-anchor="middle" class="fig-orange">I1</text>
    <text x="430" y="277" font-size="11" text-anchor="middle" class="fig-orange">I2</text>
    <text x="492" y="277" font-size="11" text-anchor="middle" class="fig-orange">I3</text>
    <text x="554" y="277" font-size="11" text-anchor="middle" class="fig-orange">I4</text>
    <text x="616" y="277" font-size="11" text-anchor="middle" class="fig-orange">I5</text>
    <text x="16" y="324" font-size="11" class="fig-muted">总耗时 T = (k + n − 1)Δt = 9Δt；第 5 拍首次流水线满载（5 段同时工作），此后理想状态下每拍流出 1 条指令（CPI = 1）。</text>
  </svg>
  <figcaption>图 6.14：MIPS 五段流水线段间流水寄存器配置与 5 条无阻塞指令时空图</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w5-fig-15">
  <svg viewBox="0 0 700 326" role="img" aria-label="超流水线、超标量动态多发射与VLIW超长指令字三种指令级并行架构对比">
    <rect x="16" y="36" width="668" height="82" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="126" width="668" height="82" rx="6" class="fig-box-subtle"/>
    <rect x="16" y="216" width="668" height="76" rx="6" class="fig-box-subtle"/>
    <line x1="252" y1="46" x2="252" y2="110" class="fig-line-dashed"/>
    <line x1="348" y1="46" x2="348" y2="110" class="fig-line-dashed"/>
    <line x1="444" y1="46" x2="444" y2="110" class="fig-line-dashed"/>
    <line x1="540" y1="46" x2="540" y2="110" class="fig-line-dashed"/>
    <line x1="252" y1="136" x2="252" y2="200" class="fig-line-dashed"/>
    <line x1="348" y1="136" x2="348" y2="200" class="fig-line-dashed"/>
    <line x1="444" y1="136" x2="444" y2="200" class="fig-line-dashed"/>
    <line x1="540" y1="136" x2="540" y2="200" class="fig-line-dashed"/>
    <rect x="252" y="54" width="240" height="16" rx="2" class="fig-box-blue"/>
    <rect x="300" y="74" width="240" height="16" rx="2" class="fig-box-purple"/>
    <rect x="348" y="94" width="240" height="16" rx="2" class="fig-box-green"/>
    <rect x="252" y="142" width="320" height="16" rx="2" class="fig-box-blue"/>
    <rect x="252" y="160" width="320" height="16" rx="2" class="fig-box-blue"/>
    <rect x="348" y="180" width="320" height="16" rx="2" class="fig-box-green"/>
    <rect x="252" y="232" width="100" height="44" rx="3" class="fig-box-purple"/>
    <rect x="356" y="232" width="100" height="44" rx="3" class="fig-box-blue"/>
    <rect x="460" y="232" width="100" height="44" rx="3" class="fig-box-green"/>
    <rect x="564" y="232" width="104" height="44" rx="3" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">指令级并行（ILP）三大扩展方向：超流水线 · 超标量 · 超长指令字（VLIW）</text>
    <text x="28" y="56" font-size="12" class="fig-blue">① 超流水线（时分复用）</text>
    <text x="28" y="75" font-size="10" class="fig-muted">细分流水段，时钟周期缩为 Δt/2</text>
    <text x="28" y="91" font-size="10" class="fig-muted">单拍仍发 1 条（按新时钟 CPI=1）</text>
    <text x="28" y="107" font-size="10" class="fig-muted">代价：锁存器增多，分支惩罚加大</text>
    <text x="372" y="66" font-size="10" text-anchor="middle" class="fig-blue">I1：每半拍推进一子段（主频翻倍）</text>
    <text x="420" y="86" font-size="10" text-anchor="middle" class="fig-purple">I2：滞后 Δt/2 启动</text>
    <text x="468" y="106" font-size="10" text-anchor="middle" class="fig-green">I3：滞后 1Δt 启动</text>
    <text x="28" y="146" font-size="12" class="fig-green">② 超标量（动态多发射）</text>
    <text x="28" y="165" font-size="10" class="fig-muted">空分复用：多套译码器 + 多 ALU</text>
    <text x="28" y="181" font-size="10" class="fig-muted">理想 CPI &lt; 1（如双发射 CPI=0.5）</text>
    <text x="28" y="197" font-size="10" class="fig-muted">硬件运行期动态检测冲突与乱序调度</text>
    <text x="412" y="154" font-size="10" text-anchor="middle" class="fig-blue">I1：IF — ID — EX — MEM — WB（通道 A）</text>
    <text x="412" y="172" font-size="10" text-anchor="middle" class="fig-blue">I2：IF — ID — EX — MEM — WB（通道 B 同拍并发）</text>
    <text x="508" y="192" font-size="10" text-anchor="middle" class="fig-green">I3 &amp; I4：下一时钟周期双双并发流入</text>
    <text x="28" y="236" font-size="12" class="fig-purple">③ VLIW（静态多发射）</text>
    <text x="28" y="255" font-size="10" class="fig-muted">编译器静态消解冲突并打包长指令</text>
    <text x="28" y="271" font-size="10" class="fig-muted">硬件省去复杂调度逻辑，功耗低</text>
    <text x="28" y="285" font-size="10" class="fig-muted">空槽填 NOP，代码体积膨胀、难兼容</text>
    <text x="302" y="252" font-size="11" text-anchor="middle" class="fig-purple">操作槽 1：ALU0</text>
    <text x="302" y="268" font-size="9" text-anchor="middle" class="fig-muted">定点运算</text>
    <text x="406" y="252" font-size="11" text-anchor="middle" class="fig-blue">操作槽 2：ALU1</text>
    <text x="406" y="268" font-size="9" text-anchor="middle" class="fig-muted">浮点运算</text>
    <text x="510" y="252" font-size="11" text-anchor="middle" class="fig-green">操作槽 3：MEM</text>
    <text x="510" y="268" font-size="9" text-anchor="middle" class="fig-muted">访存读写</text>
    <text x="616" y="252" font-size="11" text-anchor="middle" class="fig-orange">操作槽 4：NOP</text>
    <text x="616" y="268" font-size="9" text-anchor="middle" class="fig-muted">无并行操作填空</text>
    <text x="16" y="312" font-size="11" class="fig-muted">408 核心区分：超流水线靠提高时钟频率（CPI 仍 ≥ 1）提速；超标量与 VLIW 靠单周期多条并发（CPI &lt; 1）提速。</text>
  </svg>
  <figcaption>图 6.15：超流水线、超标量（动态多发射）与超长指令字（静态多发射 VLIW）对比</figcaption>
</figure>

> <strong>★考点</strong>：给定时空图或指令条数，算所需时钟周期数 $k+n-1$；CPI 与吞吐率、加速比、效率的换算；单周期/多周期/基本流水线/超标量的 CPI 大小关系。

### 6.12 流水线冲突：结构、控制与数据

流水线重叠执行会带来三类冲突：

| 冲突类型 | 原因 | 解决 |
|---|---|---|
| 结构冲突（资源冲突） | 多条指令同一时刻争用同一部件（ALU、存储器端口） | 增加部件（如专用加法器）、指令存储器与数据存储器分开、停顿 |
| 控制冲突 | 分支指令改变 PC，已取入的后续指令可能作废 | 清空（flush）流水线中分支后的指令、插入气泡、分支预测 |
| 数据冲突 | 指令间存在数据相关，读写次序无法满足 | 插入气泡 / 空操作、数据旁路（转发）、编译器调度 |

数据相关有四种，是否在流水线中形成冲突取决于流水线类型：

| 类型 | 含义 | MIPS 标准五段（按序） | 乱序调度 |
|---|---|---|---|
| RAW（写后读） | 后一条指令读前一条指令尚未写回的目的寄存器 | <strong>会冲突</strong> | 会冲突 |
| WAR（读后写） | 后一条指令写前一条指令还要读的寄存器 | 不冲突 | 可能冲突 |
| WAW（写后写） | 两条指令写同一个目的寄存器 | 不冲突 | 可能冲突 |
| RAR（读后读） | 两条指令读同一寄存器 | 不算相关问题 | 不算 |

以 `I1: AND R2, R3, R4` 与 `I2: SUB R3, R2, R4` 为例：R2 是 I1 的目的、I2 的源，属于 <strong>RAW</strong>；R3 是 I1 的源、I2 的目的，属于 WAR（五段流水线不冲突）；R4 只读，RAR 不考虑。

<strong>RAW 冲突的成因</strong>（设 I1 的目的寄存器为 R3，I2–I5 都要读 R3）：寄存器写回在 WB 段，读寄存器在 ID 段。I1 进入 WB 时，紧随其后的 I2、I3 已分别位于 MEM、EX 段并读过了 R3，I4 正在 ID 段读 R3，读到的都是旧值。若相关指令与 I1 相隔足够远（如 I5 在 I1 处于 WB 时仍在 IF），到它进入 ID 时 I1 已写回，就不会冲突——<strong>相关不一定冲突</strong>。

<strong>硬件检测</strong>（ID 段指令与 EX/MEM 段指令比较）：

$$
Hazard = H_{EX,rs} + H_{EX,rt} + H_{MEM,rs} + H_{MEM,rt}
$$

其中每一项形如“ID 段要用该寄存器（编号 ≠ 0）× 对方 RegWrite 有效 × 编号相同”。0 号寄存器恒为零、写入无效，必须排除。检测到冲突后：冻结 PC 与 IF/ID 寄存器（使能置 0，忽略时钟），清空 ID/EX 寄存器（插入气泡）。

<strong>两类解决方法</strong>：

- <strong>软件方法</strong>：编译器在相关指令间插入空操作指令，简单但浪费指令空间。
- <strong>硬件方法</strong>：插入气泡（相当于空操作），并配合<strong>数据旁路/转发</strong>把执行结果抄近道送给需要它的指令，避免停顿。

本课程的五段流水线中，ID 与 EX、ID 与 MEM 的 RAW 冲突用插入气泡解决；ID 与 WB 的冲突靠<strong>时钟信号的功能划分</strong>解决：寄存器组在时钟<strong>下降沿写入</strong>、低电平期间读出、上升沿锁存流水寄存器。因此同一寄存器“先写回、后读出”，相邻指令间<strong>只需连续插入两个气泡</strong>。

> <strong>注意</strong>：若题目给定“未采用转发技术”且“同一寄存器读写不能在同一时钟周期进行”（如 2012 年题），ID 与 WB 的冲突也必须靠气泡解决，此时需要插入<strong>三个</strong>气泡。

<figure class="cpd-figure not-content" id="hco-w5-fig-16">
  <svg viewBox="0 0 700 326" role="img" aria-label="无转发机制下五段流水线RAW写后读冲突与插入2个气泡时空图">
    <rect x="16" y="36" width="668" height="248" rx="6" class="fig-box-subtle"/>
    <rect x="336" y="44" width="64" height="196" rx="4" class="fig-box-subtle"/>
    <rect x="92" y="70" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="154" y="70" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="216" y="70" width="56" height="26" rx="3" class="fig-box-blue"/>
    <rect x="278" y="70" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="340" y="70" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="154" y="102" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="216" y="102" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="278" y="102" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="340" y="102" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="402" y="102" width="56" height="26" rx="3" class="fig-box"/>
    <rect x="216" y="134" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="278" y="134" width="56" height="26" rx="3" class="fig-box-red"/>
    <rect x="340" y="134" width="56" height="26" rx="3" class="fig-box-red"/>
    <rect x="402" y="134" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="464" y="134" width="56" height="26" rx="3" class="fig-box-accent"/>
    <rect x="278" y="166" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="340" y="166" width="56" height="26" rx="3" class="fig-box-red"/>
    <rect x="402" y="166" width="56" height="26" rx="3" class="fig-box-red"/>
    <rect x="464" y="166" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="526" y="166" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="340" y="198" width="56" height="26" rx="3" class="fig-box-green"/>
    <rect x="402" y="198" width="56" height="26" rx="3" class="fig-box-dashed"/>
    <rect x="464" y="198" width="56" height="26" rx="3" class="fig-box-dashed"/>
    <rect x="526" y="198" width="56" height="26" rx="3" class="fig-box-orange"/>
    <rect x="588" y="198" width="56" height="26" rx="3" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">无转发五段流水线 RAW 冲突与插入 2 个气泡（I2 读 I1 目的寄存器）</text>
    <text x="34" y="60" font-size="10" class="fig-muted">段 \ 时钟</text>
    <text x="120" y="60" font-size="10" text-anchor="middle" class="fig-muted">1</text>
    <text x="182" y="60" font-size="10" text-anchor="middle" class="fig-muted">2</text>
    <text x="244" y="60" font-size="10" text-anchor="middle" class="fig-muted">3</text>
    <text x="306" y="60" font-size="10" text-anchor="middle" class="fig-muted">4</text>
    <text x="368" y="60" font-size="10" text-anchor="middle" class="fig-green">5(写后读)</text>
    <text x="430" y="60" font-size="10" text-anchor="middle" class="fig-muted">6</text>
    <text x="492" y="60" font-size="10" text-anchor="middle" class="fig-muted">7</text>
    <text x="554" y="60" font-size="10" text-anchor="middle" class="fig-muted">8</text>
    <text x="616" y="60" font-size="10" text-anchor="middle" class="fig-muted">9</text>
    <text x="52" y="87" font-size="11" text-anchor="middle" class="fig-blue">IF</text>
    <text x="52" y="119" font-size="11" text-anchor="middle" class="fig-label">ID</text>
    <text x="52" y="151" font-size="11" text-anchor="middle" class="fig-accent">EX</text>
    <text x="52" y="183" font-size="11" text-anchor="middle" class="fig-green">MEM</text>
    <text x="52" y="215" font-size="11" text-anchor="middle" class="fig-orange">WB</text>
    <text x="120" y="87" font-size="11" text-anchor="middle" class="fig-blue">I1</text>
    <text x="182" y="87" font-size="11" text-anchor="middle" class="fig-blue">I2</text>
    <text x="244" y="87" font-size="11" text-anchor="middle" class="fig-blue">I3</text>
    <text x="306" y="87" font-size="10" text-anchor="middle" class="fig-orange">I3(冻)</text>
    <text x="368" y="87" font-size="10" text-anchor="middle" class="fig-orange">I3(冻)</text>
    <text x="182" y="119" font-size="11" text-anchor="middle" class="fig-label">I1</text>
    <text x="244" y="119" font-size="11" text-anchor="middle" class="fig-label">I2</text>
    <text x="306" y="119" font-size="10" text-anchor="middle" class="fig-orange">I2(停)</text>
    <text x="368" y="119" font-size="10" text-anchor="middle" class="fig-green">I2(后读)</text>
    <text x="430" y="119" font-size="11" text-anchor="middle" class="fig-label">I3</text>
    <text x="244" y="151" font-size="11" text-anchor="middle" class="fig-accent">I1</text>
    <text x="306" y="151" font-size="10" text-anchor="middle" class="fig-red">气泡</text>
    <text x="368" y="151" font-size="10" text-anchor="middle" class="fig-red">气泡</text>
    <text x="430" y="151" font-size="11" text-anchor="middle" class="fig-accent">I2</text>
    <text x="492" y="151" font-size="11" text-anchor="middle" class="fig-accent">I3</text>
    <text x="306" y="183" font-size="11" text-anchor="middle" class="fig-green">I1</text>
    <text x="368" y="183" font-size="10" text-anchor="middle" class="fig-red">气泡</text>
    <text x="430" y="183" font-size="10" text-anchor="middle" class="fig-red">气泡</text>
    <text x="492" y="183" font-size="11" text-anchor="middle" class="fig-green">I2</text>
    <text x="554" y="183" font-size="11" text-anchor="middle" class="fig-green">I3</text>
    <text x="368" y="215" font-size="10" text-anchor="middle" class="fig-green">I1(先写)</text>
    <text x="430" y="215" font-size="10" text-anchor="middle" class="fig-muted">气泡</text>
    <text x="492" y="215" font-size="10" text-anchor="middle" class="fig-muted">气泡</text>
    <text x="554" y="215" font-size="11" text-anchor="middle" class="fig-orange">I2</text>
    <text x="616" y="215" font-size="11" text-anchor="middle" class="fig-orange">I3</text>
    <text x="34" y="252" font-size="10" class="fig-muted">· 冻结机制：第 3 拍 ID 段检出冲突，PCWrite=0、IF/IDWrite=0 冻结 I2 与 I3，同时清零 ID/EX 控制位注入气泡。</text>
    <text x="34" y="270" font-size="10" class="fig-muted">· 半拍读写：第 5 拍前半周（下降沿）I1 在 WB 写回寄存器堆，后半周（低电平）I2 在 ID 读出新值，仅需 2 个气泡。</text>
    <text x="16" y="310" font-size="11" class="fig-muted">★考题陷阱：若题目声明“寄存器堆不支持同周期先写后读”，则第 5 拍仍冲突，相邻 RAW 指令必须插入 3 个气泡！</text>
  </svg>
  <figcaption>图 6.16：无转发五段流水线 RAW 阻塞停顿与“前半拍写回、后半拍读出”化解 ID-WB 冲突</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w5-fig-17">
  <svg viewBox="0 0 700 326" role="img" aria-label="数据旁路转发解决ALU-ALU写后读冲突与Load-Use冒险必插1个气泡对比图">
    <defs>
      <marker id="hco-w5-arr-17" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="330" height="252" rx="6" class="fig-box-subtle"/>
    <rect x="358" y="36" width="326" height="252" rx="6" class="fig-box-subtle"/>
    <path d="M 224 110 L 246 142" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w5-arr-17)"/>
    <path d="M 582 110 L 602 142" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w5-arr-17)"/>
    <rect x="98" y="82" width="42" height="28" rx="3" class="fig-box-blue"/>
    <rect x="144" y="82" width="42" height="28" rx="3" class="fig-box"/>
    <rect x="190" y="82" width="42" height="28" rx="3" class="fig-box-green"/>
    <rect x="236" y="82" width="42" height="28" rx="3" class="fig-box"/>
    <rect x="282" y="82" width="42" height="28" rx="3" class="fig-box"/>
    <rect x="144" y="144" width="42" height="28" rx="3" class="fig-box-blue"/>
    <rect x="190" y="144" width="42" height="28" rx="3" class="fig-box"/>
    <rect x="236" y="144" width="42" height="28" rx="3" class="fig-box-green"/>
    <rect x="282" y="144" width="42" height="28" rx="3" class="fig-box"/>
    <rect x="136" y="117" width="84" height="18" rx="3" class="fig-box"/>
    <rect x="28" y="188" width="306" height="86" rx="4" class="fig-box"/>
    <rect x="424" y="82" width="38" height="28" rx="3" class="fig-box-blue"/>
    <rect x="466" y="82" width="38" height="28" rx="3" class="fig-box"/>
    <rect x="508" y="82" width="38" height="28" rx="3" class="fig-box"/>
    <rect x="550" y="82" width="38" height="28" rx="3" class="fig-box-orange"/>
    <rect x="592" y="82" width="38" height="28" rx="3" class="fig-box"/>
    <rect x="466" y="144" width="38" height="28" rx="3" class="fig-box-blue"/>
    <rect x="508" y="144" width="38" height="28" rx="3" class="fig-box"/>
    <rect x="550" y="144" width="38" height="28" rx="3" class="fig-box-red"/>
    <rect x="592" y="144" width="38" height="28" rx="3" class="fig-box-green"/>
    <rect x="634" y="144" width="38" height="28" rx="3" class="fig-box"/>
    <rect x="488" y="117" width="88" height="18" rx="3" class="fig-box"/>
    <rect x="370" y="188" width="302" height="86" rx="4" class="fig-box-orange"/>
    <text x="16" y="24" font-size="13" class="fig-title">数据旁路（Forwarding）效果对比：ALU-ALU 零停顿 vs Load-Use 必停 1 拍</text>
    <text x="28" y="54" font-size="11" class="fig-muted">（a）ALU 运算结果旁路转发（0 气泡）</text>
    <text x="119" y="74" font-size="9" text-anchor="middle" class="fig-muted">C1</text>
    <text x="165" y="74" font-size="9" text-anchor="middle" class="fig-muted">C2</text>
    <text x="211" y="74" font-size="9" text-anchor="middle" class="fig-muted">C3</text>
    <text x="257" y="74" font-size="9" text-anchor="middle" class="fig-muted">C4</text>
    <text x="303" y="74" font-size="9" text-anchor="middle" class="fig-muted">C5</text>
    <text x="60" y="100" font-size="10" text-anchor="middle" class="fig-label">ADD R1</text>
    <text x="119" y="100" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="165" y="100" font-size="10" text-anchor="middle" class="fig-label">ID</text>
    <text x="211" y="100" font-size="10" text-anchor="middle" class="fig-green">EX</text>
    <text x="257" y="100" font-size="10" text-anchor="middle" class="fig-label">MEM</text>
    <text x="303" y="100" font-size="10" text-anchor="middle" class="fig-label">WB</text>
    <text x="60" y="162" font-size="10" text-anchor="middle" class="fig-label">SUB R2, R1</text>
    <text x="165" y="162" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="211" y="162" font-size="10" text-anchor="middle" class="fig-label">ID</text>
    <text x="257" y="162" font-size="10" text-anchor="middle" class="fig-green">EX</text>
    <text x="303" y="162" font-size="10" text-anchor="middle" class="fig-label">MEM</text>
    <text x="178" y="130" font-size="9" text-anchor="middle" class="fig-green">EX/MEM→ALU</text>
    <text x="40" y="208" font-size="10" class="fig-green">· 相邻运算：EX/MEM 流水锁存器直通下拍 ALU（0 停顿）</text>
    <text x="40" y="226" font-size="10" class="fig-label">· 隔 1 条指令：MEM/WB 流水锁存器直通 ALU（0 停顿）</text>
    <text x="40" y="244" font-size="10" class="fig-muted">· 隔 2 条指令：WB 前半拍写回、ID 后半拍读出，天然无碍</text>
    <text x="40" y="262" font-size="10" class="fig-muted">结论：纯 ALU 型 RAW 冲突可由硬件转发 100% 消除！</text>
    <text x="370" y="54" font-size="11" class="fig-muted">（b）Load-Use 冒险（LW 后紧跟读该寄存器）</text>
    <text x="443" y="74" font-size="9" text-anchor="middle" class="fig-muted">C1</text>
    <text x="485" y="74" font-size="9" text-anchor="middle" class="fig-muted">C2</text>
    <text x="527" y="74" font-size="9" text-anchor="middle" class="fig-muted">C3</text>
    <text x="569" y="74" font-size="9" text-anchor="middle" class="fig-muted">C4</text>
    <text x="611" y="74" font-size="9" text-anchor="middle" class="fig-muted">C5</text>
    <text x="653" y="74" font-size="9" text-anchor="middle" class="fig-muted">C6</text>
    <text x="394" y="100" font-size="10" text-anchor="middle" class="fig-orange">LW R1</text>
    <text x="443" y="100" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="485" y="100" font-size="10" text-anchor="middle" class="fig-label">ID</text>
    <text x="527" y="100" font-size="10" text-anchor="middle" class="fig-label">EX</text>
    <text x="569" y="100" font-size="10" text-anchor="middle" class="fig-orange">MEM</text>
    <text x="611" y="100" font-size="10" text-anchor="middle" class="fig-label">WB</text>
    <text x="408" y="162" font-size="10" text-anchor="middle" class="fig-label">SUB R2, R1</text>
    <text x="485" y="162" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="527" y="162" font-size="10" text-anchor="middle" class="fig-label">ID</text>
    <text x="569" y="162" font-size="9" text-anchor="middle" class="fig-red">气泡</text>
    <text x="611" y="162" font-size="10" text-anchor="middle" class="fig-green">EX</text>
    <text x="653" y="162" font-size="10" text-anchor="middle" class="fig-label">MEM</text>
    <text x="532" y="130" font-size="9" text-anchor="middle" class="fig-green">MEM/WB→ALU</text>
    <text x="382" y="208" font-size="10" class="fig-orange">★唯一不能单靠转发消除的 RAW 冒险：</text>
    <text x="382" y="226" font-size="10" class="fig-label">LW 的主存数据在 C4 末尾才送入 MEM/WB，</text>
    <text x="382" y="244" font-size="10" class="fig-label">无法倒流给 C4 初的 EX，硬件必插 1 个气泡，</text>
    <text x="382" y="262" font-size="10" class="fig-muted">或在编译阶段调度一条无关指令插入其间消除。</text>
    <text x="16" y="310" font-size="11" class="fig-muted">408 必背结论：带转发通路的标准五段流水线中，仅相邻“Load + 立即使用该目的寄存器”会产生 1 个时钟周期阻塞！</text>
  </svg>
  <figcaption>图 6.17：数据旁路（Forwarding）化解 ALU-ALU 冲突与 Load-Use 必插 1 拍气泡原理</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w5-fig-18">
  <svg viewBox="0 0 700 284" role="img" aria-label="流水线结构冲突哈佛Cache分离与分支指令控制冲突Flush冲刷气泡对比图">
    <defs>
      <marker id="hco-w5-arr-18" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6.5" markerHeight="6.5" markerUnits="userSpaceOnUse" orient="auto-start-reverse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="16" y="36" width="330" height="210" rx="6" class="fig-box-subtle"/>
    <rect x="358" y="36" width="326" height="210" rx="6" class="fig-box-subtle"/>
    <line x1="158" y1="92" x2="196" y2="108" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w5-arr-18)"/>
    <line x1="158" y1="138" x2="196" y2="122" class="fig-line" stroke="var(--sl-color-red)" marker-end="url(#hco-w5-arr-18)"/>
    <line x1="181" y1="156" x2="181" y2="178" class="fig-line-accent" marker-end="url(#hco-w5-arr-18)"/>
    <rect x="32" y="74" width="126" height="34" rx="4" class="fig-box-green"/>
    <rect x="32" y="120" width="126" height="34" rx="4" class="fig-box-blue"/>
    <rect x="198" y="92" width="134" height="46" rx="4" class="fig-box-red"/>
    <rect x="32" y="180" width="298" height="52" rx="4" class="fig-box-accent"/>
    <rect x="426" y="68" width="44" height="24" rx="3" class="fig-box-blue"/>
    <rect x="474" y="68" width="44" height="24" rx="3" class="fig-box"/>
    <rect x="522" y="68" width="62" height="24" rx="3" class="fig-box-orange"/>
    <rect x="474" y="100" width="44" height="24" rx="3" class="fig-box-blue"/>
    <rect x="522" y="100" width="62" height="24" rx="3" class="fig-box"/>
    <rect x="588" y="100" width="84" height="24" rx="3" class="fig-box-red"/>
    <rect x="522" y="132" width="62" height="24" rx="3" class="fig-box-blue"/>
    <rect x="588" y="132" width="84" height="24" rx="3" class="fig-box-red"/>
    <rect x="588" y="164" width="84" height="24" rx="3" class="fig-box-green"/>
    <rect x="370" y="198" width="302" height="36" rx="4" class="fig-box"/>
    <text x="16" y="24" font-size="13" class="fig-title">结构冲突（访存端口争用）与控制冲突（分支跳转冲刷）机制</text>
    <text x="28" y="54" font-size="11" class="fig-muted">（a）结构冲突：同拍争用单端口主存</text>
    <text x="95" y="95" font-size="11" text-anchor="middle" class="fig-green">I1 处于 MEM 段</text>
    <text x="95" y="141" font-size="11" text-anchor="middle" class="fig-blue">I4 处于 IF 段</text>
    <text x="265" y="112" font-size="11" text-anchor="middle" class="fig-red">单端口存储器冲突！</text>
    <text x="265" y="128" font-size="10" text-anchor="middle" class="fig-muted">无法同时读指令与数据</text>
    <text x="181" y="201" font-size="11" text-anchor="middle" class="fig-accent">根治方案：哈佛结构分离 L1 Cache</text>
    <text x="181" y="220" font-size="9" text-anchor="middle" class="fig-muted">IF 访指令 Cache（I-Cache）· MEM 访数据 Cache（D-Cache）</text>
    <text x="370" y="54" font-size="11" class="fig-muted">（b）控制冲突：BEQ 跳转成功冲刷流水线</text>
    <text x="394" y="84" font-size="10" text-anchor="middle" class="fig-orange">BEQ</text>
    <text x="448" y="84" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="496" y="84" font-size="10" text-anchor="middle" class="fig-label">ID</text>
    <text x="553" y="84" font-size="9.5" text-anchor="middle" class="fig-orange">EX(改PC)</text>
    <text x="394" y="116" font-size="10" text-anchor="middle" class="fig-muted">顺序I+1</text>
    <text x="496" y="116" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="553" y="116" font-size="10" text-anchor="middle" class="fig-label">ID</text>
    <text x="630" y="116" font-size="9.8" text-anchor="middle" class="fig-red">Flush(作废)</text>
    <text x="394" y="148" font-size="10" text-anchor="middle" class="fig-muted">顺序I+2</text>
    <text x="553" y="148" font-size="10" text-anchor="middle" class="fig-blue">IF</text>
    <text x="630" y="148" font-size="9.8" text-anchor="middle" class="fig-red">Flush(作废)</text>
    <text x="394" y="180" font-size="10" text-anchor="middle" class="fig-green">目标指令</text>
    <text x="630" y="180" font-size="9.5" text-anchor="middle" class="fig-green">IF(新PC取指)</text>
    <text x="521" y="213" font-size="10" text-anchor="middle" class="fig-label">优化手段：分支判断前移至 ID 段 · 动态分支预测（BTB）</text>
    <text x="521" y="228" font-size="10" text-anchor="middle" class="fig-muted">冲刷（Flush）本质是清零 IF/ID、ID/EX 控制信号变气泡</text>
    <text x="16" y="268" font-size="11" class="fig-muted">注：结构冲突靠“增设独立硬件/端口”解决；控制冲突靠“Flush 清空错误预取指令 + 分支预测/延迟槽”降低分支惩罚。</text>
  </svg>
  <figcaption>图 6.18：结构冲突的哈佛 Cache 分离方案与分支控制冲突的流水线冲刷（Flush）过程</figcaption>
</figure>

> <strong>★考点</strong>：判断给定指令序列在五段流水线中的 RAW 冲突；无转发时计算需要插入几个气泡并画时空图；识别分支引起的控制冲突用清空流水线解决、结构冲突用增加部件解决；区分“数据相关”与“数据冲突”——相关是否成冲突取决于指令间距与流水线实现。

## 第7章 总线

### 7.1 总线传输过程的四个阶段

总线传输周期是完成一次总线操作所耗费的时间，由<strong>申请、寻址、传输、结束</strong>四个阶段构成。

<strong>申请阶段</strong>——决定谁获得总线使用权：

1. <strong>传输请求</strong>：主设备经控制总线向总线控制器发出总线请求信号。
2. <strong>总线仲裁</strong>：控制器收到一个或多个请求后，按预设仲裁规则，决定把下一个总线传输周期的使用权分配给哪一个主设备。
3. <strong>获得授权</strong>：被选中的主设备收到控制器发回的总线许可信号，获得总线控制权。

<strong>寻址阶段</strong>——找到目标从设备：

4. <strong>启动从设备</strong>：主设备经地址总线发出目标从设备地址，同时经控制总线发出相应控制命令。
5. <strong>从设备响应</strong>：总线上所有从设备都收到地址与命令，其中地址匹配的从设备自动响应，准备后续数据传输。

<strong>传输阶段</strong>：

6. <strong>数据传送</strong>：主从设备之间经数据总线传输。写操作数据由主设备传向从设备，读操作由从设备传向主设备。一般情况下（非 DDR、非突发）一个总线传输周期只传输一个总线宽度的数据。

<strong>结束阶段</strong>：

7. <strong>撤销请求</strong>：主设备撤销总线请求、释放总线控制权，停止驱动地址总线、数据总线和控制总线，使其回归<strong>高阻态</strong>。
8. <strong>控制权交还</strong>：总线控制器检测到主设备已释放总线后收回控制权，可对新的请求重新仲裁，开启下一个总线传输周期。

<figure class="cpd-figure not-content" id="hco-w6-fig-1">
  <svg viewBox="0 0 700 215" role="img" aria-label="总线传输周期的四个阶段与一般总线事务范围">
    <defs>
      <marker id="hco-w6-arr-1" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">总线传输周期 = 申请阶段 + 寻址阶段 + 传输阶段 + 结束阶段</text>
    <path d="M 24 42 V 32 H 676 V 42" class="fig-line"/>
    <line x1="166" y1="89" x2="190" y2="89" class="fig-line" marker-end="url(#hco-w6-arr-1)"/>
    <line x1="336" y1="89" x2="360" y2="89" class="fig-line-accent" marker-end="url(#hco-w6-arr-1)"/>
    <line x1="506" y1="89" x2="530" y2="89" class="fig-line" marker-end="url(#hco-w6-arr-1)"/>
    <rect x="24" y="48" width="142" height="82" rx="6" class="fig-box-subtle"/>
    <text x="95" y="72" text-anchor="middle" font-size="13" class="fig-label">① 申请阶段</text>
    <text x="95" y="94" text-anchor="middle" font-size="11" class="fig-muted">请求 → 仲裁</text>
    <text x="95" y="112" text-anchor="middle" font-size="11" class="fig-muted">→ 获得授权</text>
    <rect x="194" y="48" width="142" height="82" rx="6" class="fig-box-blue"/>
    <text x="265" y="72" text-anchor="middle" font-size="13" class="fig-blue">② 寻址阶段</text>
    <text x="265" y="94" text-anchor="middle" font-size="11" class="fig-muted">发地址与控制命令</text>
    <text x="265" y="112" text-anchor="middle" font-size="11" class="fig-muted">→ 从设备响应</text>
    <rect x="364" y="48" width="142" height="82" rx="6" class="fig-box-green"/>
    <text x="435" y="72" text-anchor="middle" font-size="13" class="fig-green">③ 传输阶段</text>
    <text x="435" y="94" text-anchor="middle" font-size="11" class="fig-muted">数据总线传送</text>
    <text x="435" y="112" text-anchor="middle" font-size="9.6" class="fig-muted">读：从 → 主；写：主 → 从</text>
    <rect x="534" y="48" width="142" height="82" rx="6" class="fig-box-subtle"/>
    <text x="605" y="72" text-anchor="middle" font-size="13" class="fig-label">④ 结束阶段</text>
    <text x="605" y="94" text-anchor="middle" font-size="11" class="fig-muted">撤销请求 → 高阻态</text>
    <text x="605" y="112" text-anchor="middle" font-size="11" class="fig-muted">→ 交还控制权</text>
    <path d="M 194 134 V 150 H 506 V 134" class="fig-line-accent"/>
    <text x="350" y="170" text-anchor="middle" font-size="12" class="fig-accent">一般总线事务 = 寻址阶段 + 传输阶段（数据阶段）</text>
    <text x="350" y="194" text-anchor="middle" font-size="11" class="fig-muted">申请与结束阶段属于总线传输周期，但不属于一般总线事务</text>
  </svg>
  <figcaption>图 7.1：总线传输周期的四个阶段与一般总线事务范围</figcaption>
</figure>

> <strong>注意</strong>：高阻态不是“电平为 0”，而是主设备不再驱动该线，电平由其他设备或上拉/下拉决定。

### 7.2 总线事务与突发传输

<strong>总线事务</strong>是总线上<strong>一对主从设备之间完成一次信息交换</strong>的过程。按操作性质，典型类型有存储器读、存储器写、I/O 读、I/O 写、中断响应、DMA 响应（后两者在第八章展开）。

一般总线事务只包含<strong>一个寻址阶段和一个传输阶段</strong>（传输阶段也称数据阶段），不含申请与结束阶段。

> <strong>★考点</strong>：区分“总线传输周期”（申请、寻址、传输、结束四阶段）与“总线事务”（寻址、传输两阶段）。2014 年真题问“一次总线事务完成的方式”，答案即突发传输。

<strong>突发传输</strong>：一次寻址 + 多个传输阶段。寻址阶段只给出<strong>一段连续数据单元的首地址</strong>，随后多个传输阶段连续传送该数据块，无需重复送地址。地址总线操作次数减少，连续数据块传输效率显著提高。例如普通模式传 4 个数据单元需 8 个总线时钟周期，相同 8 个周期内突发模式只需 1 次寻址，可连续传送 7 个数据单元。

| 模式 | 地址操作 | 传输阶段数 | 适用场景 |
|---|---|---|---|
| 普通模式 | 每个数据单元单独寻址 | 1 | 离散、单字传输 |
| 突发模式 | 一次给出连续块首地址 | 多个 | 连续数据块 |

<figure class="cpd-figure not-content" id="hco-w6-fig-2">
  <svg viewBox="0 0 700 280" role="img" aria-label="普通传输模式与突发传输模式时序对比">
    <defs>
      <marker id="hco-w6-arr-2" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="24" y="24" font-size="12" class="fig-muted">普通模式：每个数据单元都要单独送地址（4 个数据单元占 8 个总线时钟周期）</text>
    <g class="fig-line-dashed">
      <line x1="120" y1="38" x2="120" y2="96"/>
      <line x1="180" y1="38" x2="180" y2="96"/>
      <line x1="240" y1="38" x2="240" y2="96"/>
      <line x1="300" y1="38" x2="300" y2="96"/>
      <line x1="360" y1="38" x2="360" y2="96"/>
      <line x1="420" y1="38" x2="420" y2="96"/>
      <line x1="480" y1="38" x2="480" y2="96"/>
      <line x1="540" y1="38" x2="540" y2="96"/>
      <line x1="600" y1="38" x2="600" y2="96"/>
      <line x1="120" y1="142" x2="120" y2="196"/>
      <line x1="180" y1="142" x2="180" y2="196"/>
      <line x1="240" y1="142" x2="240" y2="196"/>
      <line x1="300" y1="142" x2="300" y2="196"/>
      <line x1="360" y1="142" x2="360" y2="196"/>
      <line x1="420" y1="142" x2="420" y2="196"/>
      <line x1="600" y1="142" x2="600" y2="196"/>
    </g>
    <text x="108" y="72" text-anchor="end" font-size="12" class="fig-label">普通模式</text>
    <rect x="120" y="50" width="60" height="34" rx="4" class="fig-box-blue"/>
    <text x="150" y="71" text-anchor="middle" font-size="11" class="fig-blue">地址 0</text>
    <rect x="180" y="50" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="210" y="71" text-anchor="middle" font-size="11" class="fig-green">数据 0</text>
    <rect x="240" y="50" width="60" height="34" rx="4" class="fig-box-blue"/>
    <text x="270" y="71" text-anchor="middle" font-size="11" class="fig-blue">地址 1</text>
    <rect x="300" y="50" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="330" y="71" text-anchor="middle" font-size="11" class="fig-green">数据 1</text>
    <rect x="360" y="50" width="60" height="34" rx="4" class="fig-box-blue"/>
    <text x="390" y="71" text-anchor="middle" font-size="11" class="fig-blue">地址 2</text>
    <rect x="420" y="50" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="450" y="71" text-anchor="middle" font-size="11" class="fig-green">数据 2</text>
    <rect x="480" y="50" width="60" height="34" rx="4" class="fig-box-blue"/>
    <text x="510" y="71" text-anchor="middle" font-size="11" class="fig-blue">地址 3</text>
    <rect x="540" y="50" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="570" y="71" text-anchor="middle" font-size="11" class="fig-green">数据 3</text>
    <text x="24" y="132" font-size="12" class="fig-muted">突发模式：一次寻址给出首地址，连续传送多个数据单元（4 个数据单元占 5 个总线时钟周期）</text>
    <text x="108" y="172" text-anchor="end" font-size="12" class="fig-label">突发模式</text>
    <rect x="120" y="150" width="60" height="34" rx="4" class="fig-box-blue"/>
    <text x="150" y="171" text-anchor="middle" font-size="11" class="fig-blue">首地址</text>
    <rect x="180" y="150" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="210" y="171" text-anchor="middle" font-size="11" class="fig-green">数据 0</text>
    <rect x="240" y="150" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="270" y="171" text-anchor="middle" font-size="11" class="fig-green">数据 1</text>
    <rect x="300" y="150" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="330" y="171" text-anchor="middle" font-size="11" class="fig-green">数据 2</text>
    <rect x="360" y="150" width="60" height="34" rx="4" class="fig-box-green"/>
    <text x="390" y="171" text-anchor="middle" font-size="11" class="fig-green">数据 3</text>
    <rect x="420" y="150" width="180" height="34" rx="4" class="fig-box-subtle"/>
    <text x="510" y="171" text-anchor="middle" font-size="11" class="fig-muted">可继续传后续数据单元</text>
    <path d="M 120 188 V 198 H 180 V 188" class="fig-line-accent"/>
    <text x="150" y="214" text-anchor="middle" font-size="11" class="fig-accent">仅 1 次地址操作</text>
    <line x1="120" y1="242" x2="616" y2="242" class="fig-line" marker-end="url(#hco-w6-arr-2)"/>
    <text x="624" y="246" font-size="11" class="fig-muted">时间 t</text>
    <text x="360" y="266" text-anchor="middle" font-size="11" class="fig-muted">减少地址总线开销：相同 8 个周期内，突发模式可连续传送 7 个数据单元</text>
  </svg>
  <figcaption>图 7.2：普通传输模式与突发（Burst）传输模式时序对比</figcaption>
</figure>

<strong>同步总线数据传输率的三个有效手段（2018 年真题）：增大总线宽度（单次传更多字节）、提高总线工作频率（单位时间更多传输周期）、采用突发传输（减少地址开销）。地址/数据线复用只是用同一组物理线在不同时刻传地址或数据，目的是减少信号线数量，属硬件设计优化，不能</strong>提高同步总线数据传输率。

例：总线时钟频率 100 MHz，一次存储器写事务含 1 个时钟周期的寻址阶段和 4 个时钟周期的数据阶段。

$$
T_{\text{clk}}=\frac{1}{100\ \text{MHz}}=10\ \text{ns},\qquad T_{\min}=(1+4)T_{\text{clk}}=5\times10\ \text{ns}=50\ \text{ns}
$$

> <strong>注意</strong>：50 ns 是“至少”时间，未计入等待周期和总线空闲。

### 7.3 总线仲裁

多个主设备同时竞争总线时，由仲裁逻辑决定使用权归属。按仲裁逻辑的位置分为<strong>集中式</strong>与<strong>分布式</strong>。

<strong>集中式仲裁</strong>：仲裁器集中在总线控制器（或 CPU）内，有三种方式：

- <strong>链式查询（菊花链）：所有设备共享 BR（请求）和 BS（忙）线，BG（授权）线串行穿过各设备。设备有请求时置 BR；仲裁器发出 BG，BG 经无请求设备继续下传，被第一个有请求的设备截获，该设备获得总线。离仲裁器越近，优先级越高</strong>。线数少、增删设备容易；但优先级固定、对链路故障敏感、授权速度慢。
- <strong>计数器定时查询</strong>：共享 BR、BS 线，仲裁器用计数器经一组设备地址线轮流查询，计数值命中请求设备时即授权。优先级可由计数器初值改变；线数少于独立请求，速度居中。
- <strong>独立请求</strong>：每个设备各有一对独立的 BR$_i$、BG$_i$ 线，仲裁器内部按优先级排队/编码。响应最快、优先级灵活；线数最多、成本最高。

| 方式 | 主要信号线 | 优先级 | 授权速度 | 特点 |
|---|---|---|---|---|
| 链式查询 | BR、BG（串接）、BS | 固定，由物理位置决定 | 慢 | 结构简单、易扩展，链路故障会阻塞后续设备 |
| 计数器定时查询 | BR、BS、设备地址线 | 可由计数初值改变 | 中 | 线数居中，灵活性居中 |
| 独立请求 | 每设备 BR$_i$、BG$_i$ | 灵活，可按优先级编码 | 快 | 线数最多、成本最高 |

<figure class="cpd-figure not-content" id="hco-w6-fig-3">
  <svg viewBox="0 0 700 245" role="img" aria-label="集中式仲裁：链式查询（菊花链）结构与信号流">
    <defs>
      <marker id="hco-w6-arr-3" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">链式查询（菊花链）：BG 逐级穿过设备，物理位置越近优先级越高</text>
    <line x1="130" y1="114" x2="186" y2="114" class="fig-line-accent" marker-end="url(#hco-w6-arr-3)"/>
    <line x1="288" y1="114" x2="336" y2="114" class="fig-line-accent" marker-end="url(#hco-w6-arr-3)"/>
    <line x1="438" y1="114" x2="486" y2="114" class="fig-line-accent" marker-end="url(#hco-w6-arr-3)"/>
    <line x1="588" y1="114" x2="632" y2="114" class="fig-line-accent" marker-end="url(#hco-w6-arr-3)"/>
    <text x="594" y="102" text-anchor="start" font-size="10" class="fig-accent">BG（串行授权）</text>
    <line x1="66" y1="132" x2="66" y2="170" class="fig-line"/>
    <line x1="66" y1="170" x2="604" y2="170" class="fig-line"/>
    <line x1="238" y1="132" x2="238" y2="170" class="fig-line"/>
    <circle cx="238" cy="170" r="3" class="fig-dot-muted"/>
    <line x1="388" y1="132" x2="388" y2="170" class="fig-line"/>
    <circle cx="388" cy="170" r="3" class="fig-dot-muted"/>
    <line x1="538" y1="132" x2="538" y2="170" class="fig-line"/>
    <circle cx="538" cy="170" r="3" class="fig-dot-muted"/>
    <text x="612" y="174" font-size="11" class="fig-blue">BR（共享）</text>
    <line x1="92" y1="132" x2="92" y2="202" class="fig-line"/>
    <line x1="92" y1="202" x2="604" y2="202" class="fig-line"/>
    <line x1="264" y1="132" x2="264" y2="202" class="fig-line"/>
    <circle cx="264" cy="202" r="3" class="fig-dot-muted"/>
    <line x1="414" y1="132" x2="414" y2="202" class="fig-line"/>
    <circle cx="414" cy="202" r="3" class="fig-dot-muted"/>
    <line x1="564" y1="132" x2="564" y2="202" class="fig-line"/>
    <circle cx="564" cy="202" r="3" class="fig-dot-muted"/>
    <text x="612" y="206" font-size="11" class="fig-green">BS（共享）</text>
    <rect x="28" y="56" width="102" height="76" rx="6" class="fig-box-blue"/>
    <text x="79" y="88" text-anchor="middle" font-size="13" class="fig-blue">仲裁器</text>
    <text x="79" y="108" text-anchor="middle" font-size="11" class="fig-muted">BR / BG / BS</text>
    <rect x="190" y="56" width="98" height="76" rx="6" class="fig-box-accent"/>
    <text x="239" y="88" text-anchor="middle" font-size="13" class="fig-label">设备 D0</text>
    <text x="239" y="108" text-anchor="middle" font-size="11" class="fig-accent">优先级最高</text>
    <rect x="340" y="56" width="98" height="76" rx="6" class="fig-box"/>
    <text x="389" y="88" text-anchor="middle" font-size="13" class="fig-label">设备 D1</text>
    <text x="389" y="108" text-anchor="middle" font-size="11" class="fig-muted">次高</text>
    <rect x="490" y="56" width="98" height="76" rx="6" class="fig-box"/>
    <text x="539" y="88" text-anchor="middle" font-size="13" class="fig-label">设备 D2</text>
    <text x="539" y="108" text-anchor="middle" font-size="11" class="fig-muted">最低</text>
    <text x="350" y="232" text-anchor="middle" font-size="11" class="fig-muted">BG 被第一个有请求的设备截获后不再下传；链路某处断线会阻塞后续设备</text>
  </svg>
  <figcaption>图 7.3：集中式仲裁——链式查询（菊花链）结构与信号流</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w6-fig-4">
  <svg viewBox="0 0 700 250" role="img" aria-label="集中式仲裁：计数器定时查询结构与轮询流程">
    <defs>
      <marker id="hco-w6-arr-4" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">计数器定时查询：仲裁器用计数器经设备地址线轮询，命中请求设备即授权</text>
    <polyline points="154,138 154,154 596,154" fill="none" stroke="var(--sl-color-accent)" stroke-width="1.5" marker-end="url(#hco-w6-arr-4)"/>
    <line x1="279" y1="128" x2="279" y2="154" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3"/>
    <circle cx="279" cy="154" r="3" class="fig-dot"/>
    <line x1="429" y1="128" x2="429" y2="154" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3"/>
    <circle cx="429" cy="154" r="3" class="fig-dot"/>
    <line x1="579" y1="128" x2="579" y2="154" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3"/>
    <circle cx="579" cy="154" r="3" class="fig-dot"/>
    <text x="604" y="158" font-size="11" class="fig-accent">设备地址线</text>
    <line x1="78" y1="138" x2="78" y2="184" class="fig-line"/>
    <line x1="78" y1="184" x2="596" y2="184" class="fig-line"/>
    <line x1="255" y1="128" x2="255" y2="184" class="fig-line"/>
    <circle cx="255" cy="184" r="3" class="fig-dot-muted"/>
    <line x1="405" y1="128" x2="405" y2="184" class="fig-line"/>
    <circle cx="405" cy="184" r="3" class="fig-dot-muted"/>
    <line x1="555" y1="128" x2="555" y2="184" class="fig-line"/>
    <circle cx="555" cy="184" r="3" class="fig-dot-muted"/>
    <text x="604" y="188" font-size="11" class="fig-blue">BR（共享）</text>
    <line x1="118" y1="138" x2="118" y2="212" class="fig-line"/>
    <line x1="118" y1="212" x2="596" y2="212" class="fig-line"/>
    <line x1="303" y1="128" x2="303" y2="212" class="fig-line"/>
    <circle cx="303" cy="212" r="3" class="fig-dot-muted"/>
    <line x1="453" y1="128" x2="453" y2="212" class="fig-line"/>
    <circle cx="453" cy="212" r="3" class="fig-dot-muted"/>
    <line x1="596" y1="128" x2="596" y2="212" class="fig-line"/>
    <circle cx="596" cy="212" r="3" class="fig-dot-muted"/>
    <text x="604" y="216" font-size="11" class="fig-green">BS（共享）</text>
    <rect x="28" y="48" width="150" height="90" rx="6" class="fig-box-blue"/>
    <text x="103" y="70" text-anchor="middle" font-size="13" class="fig-blue">仲裁器</text>
    <rect x="42" y="82" width="122" height="44" rx="4" class="fig-box"/>
    <text x="103" y="102" text-anchor="middle" font-size="11" class="fig-label">计数器（初值可设）</text>
    <text x="103" y="118" text-anchor="middle" font-size="10" class="fig-muted">当前计数值：01</text>
    <rect x="230" y="54" width="98" height="74" rx="6" class="fig-box"/>
    <text x="279" y="84" text-anchor="middle" font-size="13" class="fig-label">设备 00</text>
    <text x="279" y="106" text-anchor="middle" font-size="11" class="fig-muted">未请求</text>
    <rect x="380" y="54" width="98" height="74" rx="6" class="fig-box-green"/>
    <text x="429" y="84" text-anchor="middle" font-size="13" class="fig-green">设备 01</text>
    <text x="429" y="106" text-anchor="middle" font-size="9.5" class="fig-green">地址命中 → 置 BS</text>
    <rect x="530" y="54" width="98" height="74" rx="6" class="fig-box"/>
    <text x="579" y="84" text-anchor="middle" font-size="13" class="fig-label">设备 10</text>
    <text x="579" y="106" text-anchor="middle" font-size="9.5" class="fig-muted">有请求 / 等待</text>
    <text x="350" y="238" text-anchor="middle" font-size="11" class="fig-muted">初值从 0 开始为固定优先级，从上次终点继续为循环公平；总控制线约 log₂N + 2 条</text>
  </svg>
  <figcaption>图 7.4：集中式仲裁——计数器定时查询结构与轮询流程</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w6-fig-5">
  <svg viewBox="0 0 700 265" role="img" aria-label="集中式仲裁：独立请求结构">
    <defs>
      <marker id="hco-w6-arr-5" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">独立请求：每个设备独享一对请求（BRᵢ）与授权（BGᵢ）线</text>
    <line x1="168" y1="62" x2="436" y2="62" stroke="var(--sl-color-blue)" fill="none" stroke-width="1.4" marker-end="url(#hco-w6-arr-5)"/>
    <line x1="440" y1="84" x2="172" y2="84" stroke="var(--sl-color-green)" fill="none" stroke-width="1.4" marker-end="url(#hco-w6-arr-5)"/>
    <text x="304" y="55" text-anchor="middle" font-size="11" class="fig-blue">BR0</text>
    <text x="304" y="99" text-anchor="middle" font-size="11" class="fig-green">BG0</text>
    <line x1="168" y1="130" x2="436" y2="130" stroke="var(--sl-color-blue)" fill="none" stroke-width="1.4" marker-end="url(#hco-w6-arr-5)"/>
    <line x1="440" y1="152" x2="172" y2="152" stroke="var(--sl-color-green)" fill="none" stroke-width="1.4" marker-end="url(#hco-w6-arr-5)"/>
    <text x="304" y="123" text-anchor="middle" font-size="11" class="fig-blue">BR1</text>
    <text x="304" y="167" text-anchor="middle" font-size="11" class="fig-green">BG1</text>
    <line x1="168" y1="198" x2="436" y2="198" stroke="var(--sl-color-blue)" fill="none" stroke-width="1.4" marker-end="url(#hco-w6-arr-5)"/>
    <line x1="440" y1="220" x2="172" y2="220" stroke="var(--sl-color-green)" fill="none" stroke-width="1.4" marker-end="url(#hco-w6-arr-5)"/>
    <text x="304" y="191" text-anchor="middle" font-size="11" class="fig-blue">BR2</text>
    <text x="304" y="235" text-anchor="middle" font-size="11" class="fig-green">BG2</text>
    <rect x="48" y="46" width="120" height="52" rx="6" class="fig-box"/>
    <text x="108" y="77" text-anchor="middle" font-size="13" class="fig-label">设备 D0</text>
    <rect x="48" y="114" width="120" height="52" rx="6" class="fig-box"/>
    <text x="108" y="145" text-anchor="middle" font-size="13" class="fig-label">设备 D1</text>
    <rect x="48" y="182" width="120" height="52" rx="6" class="fig-box"/>
    <text x="108" y="213" text-anchor="middle" font-size="13" class="fig-label">设备 D2</text>
    <rect x="440" y="42" width="200" height="196" rx="6" class="fig-box-blue"/>
    <text x="540" y="76" text-anchor="middle" font-size="14" class="fig-blue">集中仲裁器</text>
    <text x="540" y="102" text-anchor="middle" font-size="11" class="fig-muted">优先级排队 / 编码 / 屏蔽</text>
    <rect x="462" y="124" width="156" height="54" rx="4" class="fig-box"/>
    <text x="540" y="148" text-anchor="middle" font-size="11" class="fig-label">支持固定、轮转</text>
    <text x="540" y="166" text-anchor="middle" font-size="11" class="fig-label">或动态优先级</text>
    <text x="350" y="255" text-anchor="middle" font-size="11" class="fig-muted">响应最快、优先级最灵活，但 N 个设备需要 2N 条请求/授权线（含 BS 共 2N + 1 条）</text>
  </svg>
  <figcaption>图 7.5：集中式仲裁——独立请求结构</figcaption>
</figure>

<strong>分布式仲裁</strong>：不设集中仲裁器，把仲裁逻辑分散到各主设备中。

1. <strong>自举分散方式</strong>：每个主设备有<strong>专用的总线请求线 BR$_i$</strong>，共享一条<strong>总线忙线 BS</strong>；每个设备只监听<strong>比自己优先级高</strong>的请求线。设 4 个主设备，M1 优先级最低、M4 最高。M2 与 M3 同时请求时，M2 检测到更高优先级的 M3 也在请求，退出竞争；M3 未检测到更高优先级请求，获得控制权并置 BS=1，阻止其他设备使用总线，随后依次进入寻址、传输、结束阶段。优点：仲裁逻辑简单；缺点：请求/仲裁线多，N 条线只能表示 N 个优先级、仲裁 N 个设备。实际可用数据总线兼作请求与仲裁线，如 SCSI 总线。
2. <strong>并行竞争方式</strong>：取消各设备专用请求线，各设备把<strong>仲裁号</strong>经驱动电路送到共享的<strong>仲裁位线</strong>上，线上结果为<strong>线或</strong>；仲裁号越大优先级越高。另设一条“正在仲裁”信号线。各设备把自己的仲裁号与线上结果<strong>从高位到低位逐位比较</strong>：某位小于线上值即退出竞争，相等则继续比较下一位；最终与线上值完全相等者获得总线。N 条仲裁线可表示 $2^N$ 个仲裁号、仲裁 $2^N$ 个设备。例：M1 仲裁号 1010、M2 仲裁号 1100，线或得 1110；M1 在 AN2 位 $0<1$，退出，线上变为 1100；M2 逐位相等，获得总线。FutureBus 采用此方式。优点：线数少、可扩展；缺点：比较与控制逻辑复杂。
3. <strong>冲突检测分散方式</strong>：各设备自由发送并监听冲突，冲突后停止发送、随机延迟后重发，原理类似以太网的 CSMA/CD。

<figure class="cpd-figure not-content" id="hco-w6-fig-6">
  <svg viewBox="0 0 700 305" role="img" aria-label="分布式仲裁：自举分散方式信号连接">
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">自举分散：各设备专用 BR 线，只监听比自己优先级更高的请求</text>
    <line x1="60" y1="46" x2="640" y2="46" stroke="var(--sl-color-green)" fill="none" stroke-width="1.5"/>
    <text x="64" y="38" font-size="11" class="fig-green">BS（共享，忙 = 1）</text>
    <g stroke="var(--sl-color-blue)" fill="none" stroke-width="1.4">
      <line x1="120" y1="64" x2="120" y2="274"/>
      <line x1="175" y1="64" x2="175" y2="274"/>
      <line x1="230" y1="64" x2="230" y2="274"/>
      <line x1="285" y1="64" x2="285" y2="274"/>
    </g>
    <g font-size="11" text-anchor="middle">
      <text x="120" y="58" class="fig-blue">BR1</text>
      <text x="175" y="58" class="fig-blue">BR2</text>
      <text x="230" y="58" class="fig-blue">BR3</text>
      <text x="285" y="58" class="fig-blue">BR4</text>
    </g>
    <line x1="490" y1="96" x2="285" y2="96" class="fig-line"/>
    <circle cx="285" cy="96" r="3" class="fig-dot-muted"/>
    <line x1="490" y1="150" x2="230" y2="150" class="fig-line"/>
    <circle cx="230" cy="150" r="3" class="fig-dot-muted"/>
    <line x1="490" y1="204" x2="175" y2="204" class="fig-line"/>
    <circle cx="175" cy="204" r="3" class="fig-dot-muted"/>
    <line x1="490" y1="258" x2="120" y2="258" class="fig-line"/>
    <circle cx="120" cy="258" r="3" class="fig-dot-muted"/>
    <g stroke="var(--sl-color-blue)" fill="none" stroke-width="1.15" stroke-dasharray="4 3">
      <line x1="490" y1="134" x2="285" y2="134"/>
      <line x1="490" y1="186" x2="230" y2="186"/>
      <line x1="490" y1="194" x2="285" y2="194"/>
      <line x1="490" y1="238" x2="175" y2="238"/>
      <line x1="490" y1="245" x2="230" y2="245"/>
      <line x1="490" y1="252" x2="285" y2="252"/>
    </g>
    <g fill="var(--sl-color-blue)" stroke="none">
      <circle cx="285" cy="134" r="2.5"/>
      <circle cx="230" cy="186" r="2.5"/>
      <circle cx="285" cy="194" r="2.5"/>
      <circle cx="175" cy="238" r="2.5"/>
      <circle cx="230" cy="245" r="2.5"/>
      <circle cx="285" cy="252" r="2.5"/>
    </g>
    <rect x="490" y="68" width="150" height="42" rx="6" class="fig-box-accent"/>
    <text x="565" y="94" text-anchor="middle" font-size="12" class="fig-accent">M4（最高优先级）</text>
    <rect x="490" y="122" width="150" height="42" rx="6" class="fig-box"/>
    <text x="565" y="148" text-anchor="middle" font-size="12" class="fig-label">M3</text>
    <rect x="490" y="176" width="150" height="42" rx="6" class="fig-box"/>
    <text x="565" y="202" text-anchor="middle" font-size="12" class="fig-label">M2</text>
    <rect x="490" y="230" width="150" height="42" rx="6" class="fig-box"/>
    <text x="565" y="256" text-anchor="middle" font-size="12" class="fig-label">M1（最低优先级）</text>
    <text x="350" y="294" text-anchor="middle" font-size="11" class="fig-muted">实线：驱动自己的 BRᵢ；虚线：监听更高优先级请求（N 条线仅支持 N 个设备）</text>
  </svg>
  <figcaption>图 7.6：分布式仲裁——自举分散方式信号连接</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w6-fig-7">
  <svg viewBox="0 0 700 280" role="img" aria-label="分布式仲裁：并行竞争（线或与逐位比较）示例">
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">并行竞争：各设备将仲裁号送上共享位线，按位“线或”后从高位到低位逐位比较</text>
    <line x1="144" y1="88" x2="254" y2="88" class="fig-line"/>
    <line x1="144" y1="178" x2="254" y2="178" class="fig-line"/>
    <line x1="254" y1="72" x2="254" y2="192" class="fig-line"/>
    <g class="fig-line">
      <line x1="254" y1="72" x2="510" y2="72"/>
      <line x1="254" y1="98" x2="510" y2="98"/>
      <line x1="254" y1="124" x2="510" y2="124"/>
      <line x1="254" y1="150" x2="510" y2="150"/>
      <line x1="254" y1="192" x2="510" y2="192"/>
    </g>
    <g font-size="11" text-anchor="end">
      <text x="242" y="76" class="fig-muted">AN3（MSB）</text>
      <text x="242" y="102" class="fig-muted">AN2</text>
      <text x="242" y="128" class="fig-muted">AN1</text>
      <text x="242" y="154" class="fig-muted">AN0（LSB）</text>
      <text x="242" y="196" class="fig-muted">AB（正在仲裁）</text>
    </g>
    <g font-size="11" text-anchor="middle">
      <text x="330" y="66" class="fig-blue">1 ∨ 1 = 1</text>
      <text x="330" y="92" class="fig-blue">0 ∨ 1 = 1</text>
      <text x="330" y="118" class="fig-blue">1 ∨ 0 = 1</text>
      <text x="330" y="144" class="fig-blue">0 ∨ 0 = 0</text>
    </g>
    <text x="448" y="92" text-anchor="middle" font-size="10" class="fig-red">M1：0 &lt; 1（退出）</text>
    <text x="382" y="185" text-anchor="middle" font-size="11" class="fig-muted">任一方请求则线上为 1</text>
    <rect x="28" y="54" width="116" height="68" rx="6" class="fig-box-blue"/>
    <text x="86" y="78" text-anchor="middle" font-size="12" class="fig-label">M1 仲裁号</text>
    <text x="86" y="106" text-anchor="middle" font-size="16" class="fig-blue">1010</text>
    <rect x="28" y="144" width="116" height="68" rx="6" class="fig-box-green"/>
    <text x="86" y="168" text-anchor="middle" font-size="12" class="fig-label">M2 仲裁号</text>
    <text x="86" y="196" text-anchor="middle" font-size="16" class="fig-green">1100</text>
    <rect x="528" y="56" width="144" height="118" rx="6" class="fig-box-accent"/>
    <text x="600" y="78" text-anchor="middle" font-size="11" class="fig-muted">线或结果</text>
    <text x="600" y="104" text-anchor="middle" font-size="17" class="fig-label">1110</text>
    <text x="600" y="130" text-anchor="middle" font-size="11" class="fig-muted">M1 退出后</text>
    <text x="600" y="156" text-anchor="middle" font-size="14" class="fig-green">1100（M2 胜出）</text>
    <text x="350" y="236" text-anchor="middle" font-size="12" class="fig-label">M1 在 AN2 位小于线上值，退出竞争；M2 逐位相等，获得总线控制权</text>
    <text x="350" y="260" text-anchor="middle" font-size="11" class="fig-muted">N 条仲裁线可表示 2<tspan baseline-shift="super" font-size="8.2">N</tspan> 个仲裁号，最多仲裁 2<tspan baseline-shift="super" font-size="8.2">N</tspan> 个设备（如 FutureBus）</text>
  </svg>
  <figcaption>图 7.7：分布式仲裁——并行竞争（线或与逐位比较）示例</figcaption>
</figure>

> <strong>★考点</strong>：N 条仲裁线的容量——自举分散为 N 个设备/N 个优先级；并行竞争为 $2^N$ 个设备/仲裁号。

### 7.4 总线定时：同步通信与异步通信

<strong>总线定时</strong>研究总线事务的每个步骤、总线上各类信号何时开始与结束。常见方式有同步通信、异步通信、半同步通信、分离事务通信；异步通信又分非互锁、半互锁、全互锁。

<strong>同步通信</strong>：传输双方在<strong>公共时钟</strong>控制下，按预定的时钟周期进行通信。

读数据时序（4 个时钟周期）：

- T1 上升沿：主设备把目标从设备地址送上地址总线。
- T2 上升沿：发出读命令（低电平有效）。
- T3 上升沿到来前：从设备把 CPU 所需数据送上数据总线；T3 上升沿 CPU 将数据打入内部寄存器。
- T4 上升沿：撤销读命令，从设备不再向数据总线送数据；T4 结束后整个总线传输过程结束。

写数据时序：

- T1 上升沿发地址，<strong>下降沿</strong>把待写数据送上数据总线。
- T2 上升沿发写命令；从设备用 <strong>T2、T3 两个时钟周期</strong>完成数据写入。
- T4 上升沿撤销写命令，主设备不再驱动数据总线，传输结束。

<figure class="cpd-figure not-content" id="hco-w6-fig-8">
  <svg viewBox="0 0 700 298" role="img" aria-label="同步通信读数据时序（T1 至 T4）">
    <text x="350" y="20" text-anchor="middle" font-size="12" class="fig-muted">同步读：T1 送地址，T2 发读命令，T3 上升沿前数据就绪并锁存，T4 撤销</text>
    <g class="fig-line-dashed">
      <line x1="90" y1="34" x2="90" y2="254"/>
      <line x1="210" y1="34" x2="210" y2="254"/>
      <line x1="330" y1="34" x2="330" y2="155"/>
      <line x1="330" y1="187" x2="330" y2="254"/>
      <line x1="450" y1="34" x2="450" y2="254"/>
      <line x1="570" y1="34" x2="570" y2="254"/>
    </g>
    <text x="76" y="72" text-anchor="end" font-size="11" class="fig-label">地址总线</text>
    <path d="M 82 67 H 90 M 210 67 H 620" class="fig-line"/>
    <rect x="90" y="52" width="120" height="30" rx="3" class="fig-box-blue"/>
    <text x="150" y="71" text-anchor="middle" font-size="11" class="fig-blue">从设备地址</text>
    <text x="76" y="124" text-anchor="end" font-size="11" class="fig-label">读命令</text>
    <path d="M 82 110 H 210 V 134 H 450 V 110 H 620" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.6"/>
    <text x="270" y="126" text-anchor="middle" font-size="10.5" class="fig-accent">低电平有效</text>
    <text x="76" y="176" text-anchor="end" font-size="11" class="fig-label">数据总线</text>
    <path d="M 82 171 H 310 M 450 171 H 620" class="fig-line"/>
    <rect x="310" y="156" width="140" height="30" rx="3" class="fig-box-green"/>
    <text x="380" y="175" text-anchor="middle" font-size="11" class="fig-green">有效数据（从 → 主）</text>
    <text x="76" y="228" text-anchor="end" font-size="11" class="fig-label">公共时钟</text>
    <path d="M 82 238 H 90 V 210 H 150 V 238 H 210 V 210 H 270 V 238 H 330 V 210 H 390 V 238 H 450 V 210 H 510 V 238 H 570" stroke="var(--sl-color-gray-3)" fill="none" stroke-width="1.6"/>
    <circle cx="90" cy="238" r="3" class="fig-dot"/>
    <circle cx="210" cy="238" r="3" class="fig-dot"/>
    <circle cx="330" cy="238" r="3" class="fig-dot"/>
    <circle cx="450" cy="238" r="3" class="fig-dot"/>
    <g text-anchor="middle" font-size="11">
      <text x="150" y="264" class="fig-muted">T1</text>
      <text x="270" y="264" class="fig-muted">T2</text>
      <text x="390" y="264" class="fig-muted">T3</text>
      <text x="510" y="264" class="fig-muted">T4</text>
    </g>
    <text x="350" y="286" text-anchor="middle" font-size="11" class="fig-muted">T3 上升沿 CPU 锁存数据，T4 上升沿撤销读命令</text>
  </svg>
  <figcaption>图 7.8：同步通信读数据时序（T1 ~ T4）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="hco-w6-fig-9">
  <svg viewBox="0 0 700 265" role="img" aria-label="同步通信写数据时序（T1 至 T4）">
    <text x="350" y="20" text-anchor="middle" font-size="12" class="fig-muted">同步写：T1 上升沿送地址、下降沿送数据，T2 发写命令，T4 撤销</text>
    <g class="fig-line-dashed">
      <line x1="90" y1="34" x2="90" y2="240"/>
      <line x1="210" y1="34" x2="210" y2="95"/>
      <line x1="210" y1="127" x2="210" y2="240"/>
      <line x1="330" y1="34" x2="330" y2="95"/>
      <line x1="330" y1="127" x2="330" y2="240"/>
      <line x1="450" y1="34" x2="450" y2="240"/>
      <line x1="570" y1="34" x2="570" y2="240"/>
    </g>
    <text x="76" y="68" text-anchor="end" font-size="11" class="fig-label">地址总线</text>
    <path d="M 82 63 H 90 M 210 63 H 620" class="fig-line"/>
    <rect x="90" y="48" width="120" height="30" rx="3" class="fig-box-blue"/>
    <text x="150" y="67" text-anchor="middle" font-size="11" class="fig-blue">从设备地址</text>
    <text x="76" y="116" text-anchor="end" font-size="11" class="fig-label">数据总线</text>
    <path d="M 82 111 H 150 M 450 111 H 620" class="fig-line"/>
    <rect x="150" y="96" width="300" height="30" rx="3" class="fig-box-green"/>
    <text x="300" y="115" text-anchor="middle" font-size="11" class="fig-green">待写入数据（从设备用 T2、T3 两个周期完成写入）</text>
    <text x="76" y="164" text-anchor="end" font-size="11" class="fig-label">写命令</text>
    <path d="M 82 150 H 210 V 174 H 450 V 150 H 620" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.6"/>
    <text x="270" y="166" text-anchor="middle" font-size="10.5" class="fig-accent">低电平有效</text>
    <text x="76" y="216" text-anchor="end" font-size="11" class="fig-label">公共时钟</text>
    <path d="M 82 226 H 90 V 198 H 150 V 226 H 210 V 198 H 270 V 226 H 330 V 198 H 390 V 226 H 450 V 198 H 510 V 226 H 570" stroke="var(--sl-color-gray-3)" fill="none" stroke-width="1.6"/>
    <circle cx="90" cy="226" r="3" class="fig-dot"/>
    <circle cx="210" cy="226" r="3" class="fig-dot"/>
    <circle cx="330" cy="226" r="3" class="fig-dot"/>
    <circle cx="450" cy="226" r="3" class="fig-dot"/>
    <g text-anchor="middle" font-size="11">
      <text x="150" y="252" class="fig-muted">T1</text>
      <text x="270" y="252" class="fig-muted">T2</text>
      <text x="390" y="252" class="fig-muted">T3</text>
      <text x="510" y="252" class="fig-muted">T4</text>
    </g>
  </svg>
  <figcaption>图 7.9：同步通信写数据时序（T1 ~ T4）</figcaption>
</figure>

同步通信的优缺点：<strong>优点是时间规定明确，设备间配合简单一致；缺点</strong>是主从设备必须严格同步，操作须在限定时间内完成，因此<strong>公共时钟频率取决于总线上最慢的设备</strong>，各种速度的从设备都要按最慢者设计，严重影响总线工作效率。同步通信一般适用于总线长度短、设备速度相近、传输可靠性高的场景，例如直接与 CPU 相连的系统总线。

> <strong>注意</strong>：同步总线使用<strong>统一的公共时钟</strong>，不是各设备分别提供时钟（真题常见错误项）。

<strong>异步通信</strong>：不使用公共时钟，靠收发双方的<strong>请求 REQ / 确认 ACK</strong> 握手信号协调。按请求与确认的撤销是否相互依赖，分三种：

| 方式 | 握手次数 | 请求撤销 | 确认撤销 | 可靠性 | 速度 |
|---|---|---|---|---|---|
| 非互锁 | 1 | 自动 | 自动 | 最低：请求或确认可能丢失 | 最快 |
| 半互锁 | 2 | 依赖确认建立 | 自动 | 较高：未收到确认则一直保持请求 | 中 |
| 全互锁 | 3 | 依赖确认建立 | 依赖请求撤销 | 最高 | 最慢 |

- <strong>非互锁</strong>：REQ 建立 → ACK 建立，双方各维持一段时间后<strong>自动撤销</strong>，两个撤销互不依赖。主设备不关心从设备是否收到请求，从设备也不关心主设备是否收到确认。
- <strong>半互锁</strong>：主设备<strong>收到 ACK 后才撤销 REQ</strong>；ACK 维持一段时间后自动撤销。解决了“主设备不知确认是否到达”的问题，但从设备仍不关心主设备是否收到确认。
- <strong>全互锁</strong>：REQ 撤销依赖 ACK 建立，ACK 撤销又依赖 REQ 撤销——<strong>三次握手</strong>，可靠性最高、速度最慢，类似 TCP 建立连接的三次握手。

<figure class="cpd-figure not-content" id="hco-w6-fig-10">
  <svg viewBox="0 0 700 320" role="img" aria-label="异步通信三种握手机制对比：非互锁、半互锁、全互锁">
    <defs>
      <marker id="hco-w6-arr-10" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6.5" markerHeight="6.5" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">异步握手对比：三种方式的差异在于“REQ 与 ACK 的撤销”是否依赖对方</text>
    <rect x="24" y="38" width="652" height="76" rx="6" class="fig-box-subtle"/>
    <text x="36" y="68" font-size="13" class="fig-label">非互锁</text>
    <text x="36" y="88" font-size="9.5" class="fig-muted">1 次依赖 / 最快</text>
    <text x="152" y="62" text-anchor="end" font-size="10" class="fig-blue">REQ</text>
    <path d="M 160 66 H 190 V 48 H 300 V 66 H 400" stroke="var(--sl-color-blue)" fill="none" stroke-width="1.5"/>
    <text x="152" y="96" text-anchor="end" font-size="10" class="fig-green">ACK</text>
    <path d="M 160 100 H 240 V 82 H 370 V 100 H 400" stroke="var(--sl-color-green)" fill="none" stroke-width="1.5"/>
    <path d="M 194 52 C 214 52 224 82 238 86" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3" marker-end="url(#hco-w6-arr-10)"/>
    <text x="420" y="72" font-size="11" class="fig-label">① 请求建立 → 确认建立</text>
    <text x="420" y="92" font-size="11" class="fig-muted">REQ、ACK 均延时自动撤销（互不依赖）</text>
    <rect x="24" y="126" width="652" height="76" rx="6" class="fig-box-subtle"/>
    <text x="36" y="156" font-size="13" class="fig-label">半互锁</text>
    <text x="36" y="176" font-size="9.5" class="fig-muted">2 次依赖 / 居中</text>
    <text x="152" y="150" text-anchor="end" font-size="10" class="fig-blue">REQ</text>
    <path d="M 160 154 H 190 V 136 H 310 V 154 H 400" stroke="var(--sl-color-blue)" fill="none" stroke-width="1.5"/>
    <text x="152" y="184" text-anchor="end" font-size="10" class="fig-green">ACK</text>
    <path d="M 160 188 H 240 V 170 H 380 V 188 H 400" stroke="var(--sl-color-green)" fill="none" stroke-width="1.5"/>
    <path d="M 194 140 C 214 140 224 170 238 174" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3" marker-end="url(#hco-w6-arr-10)"/>
    <path d="M 244 172 C 272 172 288 152 308 150" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3" marker-end="url(#hco-w6-arr-10)"/>
    <text x="420" y="160" font-size="11" class="fig-label">① 请求 → 确认，② 确认 → 撤销请求</text>
    <text x="420" y="180" font-size="11" class="fig-muted">REQ 等 ACK 到达后撤销；ACK 自动撤销</text>
    <rect x="24" y="214" width="652" height="76" rx="6" class="fig-box-accent"/>
    <text x="36" y="244" font-size="13" class="fig-accent">全互锁</text>
    <text x="36" y="264" font-size="9.5" class="fig-muted">3 次握手 / 最可靠</text>
    <text x="152" y="238" text-anchor="end" font-size="10" class="fig-blue">REQ</text>
    <path d="M 160 242 H 190 V 224 H 310 V 242 H 400" stroke="var(--sl-color-blue)" fill="none" stroke-width="1.5"/>
    <text x="152" y="272" text-anchor="end" font-size="10" class="fig-green">ACK</text>
    <path d="M 160 276 H 240 V 258 H 370 V 276 H 400" stroke="var(--sl-color-green)" fill="none" stroke-width="1.5"/>
    <path d="M 194 228 C 214 228 224 258 238 262" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3" marker-end="url(#hco-w6-arr-10)"/>
    <path d="M 244 260 C 272 260 288 240 308 238" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3" marker-end="url(#hco-w6-arr-10)"/>
    <path d="M 314 238 C 338 238 350 268 368 272" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.3" marker-end="url(#hco-w6-arr-10)"/>
    <text x="420" y="248" font-size="11" class="fig-accent">① 请求 → 确认，② 确认 → 撤请求</text>
    <text x="420" y="268" font-size="11" class="fig-accent">③ 撤请求 → 撤确认（闭环三次握手）</text>
    <text x="350" y="310" text-anchor="middle" font-size="11" class="fig-muted">箭头表示因果依赖；全互锁可靠性最高、速度最慢，类似 TCP 建立连接的三次握手</text>
  </svg>
  <figcaption>图 7.10：异步通信三种握手机制对比（非互锁、半互锁、全互锁）</figcaption>
</figure>

### 7.5 半同步通信与分离事务

<strong>半同步通信</strong> = 同步通信 + 一条<strong>等待响应线</strong> + 插入等待时钟周期。它保留公共时钟，用握手信号 WAIT 让慢速从设备插入等待，兼取同步与异步之长。

读数据示例（6 个时钟周期）：

- T1 送地址；T2 发读命令。
- T3 从设备未就绪，置 WAIT 有效；T4 继续等待（插入等待周期）。
- T5 从设备把数据送上数据总线并撤销 WAIT，CPU 在 T5 内读取数据。
- T6 结束阶段：主设备撤销读命令，从设备撤销对数据总线的驱动。

<strong>优点：继承同步通信控制简单的优点，仅增加一条等待响应线，即可允许不同速度的设备协调工作。缺点</strong>：受握手机制限制，时钟频率不能设计得太高，影响系统整体速度。

> <strong>注意</strong>：半同步通信中握手信号的采样仍由<strong>同步时钟</strong>控制（真题正确项）。

<figure class="cpd-figure not-content" id="hco-w6-fig-11">
  <svg viewBox="0 0 700 296" role="img" aria-label="半同步通信读数据时序（WAIT 插入等待周期）">
    <text x="350" y="20" text-anchor="middle" font-size="12" class="fig-muted">半同步读：T3、T4 从设备未就绪，WAIT 有效，插入 2 个等待周期后于 T5 传数据</text>
    <g class="fig-line-dashed">
      <line x1="90" y1="34" x2="90" y2="272"/>
      <line x1="180" y1="34" x2="180" y2="272"/>
      <line x1="270" y1="34" x2="270" y2="272"/>
      <line x1="360" y1="34" x2="360" y2="120"/>
      <line x1="360" y1="144" x2="360" y2="272"/>
      <line x1="450" y1="34" x2="450" y2="272"/>
      <line x1="540" y1="34" x2="540" y2="209"/>
      <line x1="540" y1="226" x2="540" y2="272"/>
      <line x1="630" y1="34" x2="630" y2="209"/>
      <line x1="630" y1="226" x2="630" y2="272"/>
    </g>
    <text x="76" y="66" text-anchor="end" font-size="11" class="fig-label">地址总线</text>
    <path d="M 82 61 H 90 M 180 61 H 645" class="fig-line"/>
    <rect x="90" y="48" width="90" height="26" rx="3" class="fig-box-blue"/>
    <text x="135" y="65" text-anchor="middle" font-size="11" class="fig-blue">地址</text>
    <text x="76" y="106" text-anchor="end" font-size="11" class="fig-label">读命令</text>
    <path d="M 82 94 H 180 V 116 H 540 V 94 H 645" stroke="var(--sl-color-accent)" fill="none" stroke-width="1.6"/>
    <text x="315" y="110" text-anchor="middle" font-size="10.5" class="fig-accent">读命令保持有效</text>
    <text x="76" y="158" text-anchor="end" font-size="11" class="fig-label">WAIT</text>
    <path d="M 82 166 H 270 V 144 H 450 V 166 H 645" stroke="var(--sl-color-red)" fill="none" stroke-width="1.6"/>
    <text x="360" y="135" text-anchor="middle" font-size="10" class="fig-red">从设备未就绪，插入等待周期</text>
    <text x="76" y="200" text-anchor="end" font-size="11" class="fig-label">数据总线</text>
    <path d="M 82 197 H 450 M 540 197 H 645" class="fig-line"/>
    <rect x="450" y="184" width="90" height="26" rx="3" class="fig-box-green"/>
    <text x="495" y="201" text-anchor="middle" font-size="11" class="fig-green">有效数据</text>
    <text x="585" y="222" text-anchor="middle" font-size="9.2" class="fig-muted">T5 读取，T6 撤销</text>
    <text x="76" y="246" text-anchor="end" font-size="11" class="fig-label">公共时钟</text>
    <path d="M 82 258 H 90 V 232 H 135 V 258 H 180 V 232 H 225 V 258 H 270 V 232 H 315 V 258 H 360 V 232 H 405 V 258 H 450 V 232 H 495 V 258 H 540 V 232 H 585 V 258 H 630" stroke="var(--sl-color-gray-3)" fill="none" stroke-width="1.6"/>
    <circle cx="90" cy="258" r="2.8" class="fig-dot"/>
    <circle cx="180" cy="258" r="2.8" class="fig-dot"/>
    <circle cx="270" cy="258" r="2.8" class="fig-dot"/>
    <circle cx="360" cy="258" r="2.8" class="fig-dot"/>
    <circle cx="450" cy="258" r="2.8" class="fig-dot"/>
    <circle cx="540" cy="258" r="2.8" class="fig-dot"/>
    <g text-anchor="middle" font-size="11">
      <text x="135" y="282" class="fig-muted">T1</text>
      <text x="225" y="282" class="fig-muted">T2</text>
      <text x="315" y="282" class="fig-muted">T3</text>
      <text x="405" y="282" class="fig-muted">T4</text>
      <text x="495" y="282" class="fig-muted">T5</text>
      <text x="585" y="282" class="fig-muted">T6</text>
    </g>
  </svg>
  <figcaption>图 7.11：半同步通信读数据时序（WAIT 插入等待周期）</figcaption>
</figure>

<strong>分离事务通信</strong>（split transaction）：把一次总线事务拆成“请求”与“数据传送”两段，中间释放总线。

1. 主设备向从设备发出请求，给出地址和请求命令。
2. 从设备应答后，主设备<strong>立即释放总线控制权</strong>。
3. 从设备准备数据；此期间总线可用于处理其他总线事务。
4. 从设备准备好数据后<strong>作为主设备重新申请总线</strong>，获得控制权后把数据送上数据总线。
5. 原主设备<strong>作为从设备</strong>通过总线接收数据。

<strong>优点：从设备准备数据期间总线不再空等，大幅提高总线利用率。缺点</strong>：控制方式更复杂。目前流行的 PCIe 总线支持这种通信方式。

<figure class="cpd-figure not-content" id="hco-w6-fig-12">
  <svg viewBox="0 0 700 258" role="img" aria-label="传统占用式总线事务与分离事务通信对比">
    <defs>
      <marker id="hco-w6-arr-12" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="350" y="22" text-anchor="middle" font-size="12" class="fig-muted">分离事务通信：拆分为“请求”与“数据回传”两段，中间释放总线供其他事务使用</text>
    <text x="86" y="74" text-anchor="end" font-size="12" class="fig-label">传统占用式</text>
    <rect x="98" y="50" width="96" height="38" rx="4" class="fig-box-blue"/>
    <text x="146" y="73" text-anchor="middle" font-size="11" class="fig-blue">主发请求</text>
    <rect x="194" y="50" width="266" height="38" rx="4" class="fig-box-red"/>
    <text x="327" y="73" text-anchor="middle" font-size="11" class="fig-red">从设备准备数据（总线空等占用）</text>
    <rect x="460" y="50" width="110" height="38" rx="4" class="fig-box-green"/>
    <text x="515" y="73" text-anchor="middle" font-size="11" class="fig-green">从回传数据</text>
    <text x="86" y="158" text-anchor="end" font-size="12" class="fig-accent">分离事务</text>
    <rect x="98" y="134" width="96" height="38" rx="4" class="fig-box-blue"/>
    <text x="146" y="157" text-anchor="middle" font-size="11" class="fig-blue">① 主发请求</text>
    <rect x="194" y="134" width="54" height="38" rx="4" class="fig-box-subtle"/>
    <text x="221" y="157" text-anchor="middle" font-size="11" class="fig-muted">② 释放</text>
    <rect x="248" y="134" width="212" height="38" rx="4" class="fig-box-orange"/>
    <text x="354" y="157" text-anchor="middle" font-size="11" class="fig-orange">③ 总线处理其他设备事务</text>
    <rect x="460" y="134" width="96" height="38" rx="4" class="fig-box-purple"/>
    <text x="508" y="157" text-anchor="middle" font-size="11" class="fig-purple">④ 从重申请</text>
    <rect x="556" y="134" width="110" height="38" rx="4" class="fig-box-green"/>
    <text x="611" y="157" text-anchor="middle" font-size="11" class="fig-green">⑤ 互换传数据</text>
    <path d="M 194 178 V 190 H 460 V 178" class="fig-line-dashed"/>
    <text x="327" y="206" text-anchor="middle" font-size="11" class="fig-muted">原从设备在后台准备数据，不占用总线</text>
    <line x1="98" y1="226" x2="660" y2="226" class="fig-line" marker-end="url(#hco-w6-arr-12)"/>
    <text x="380" y="246" text-anchor="middle" font-size="11" class="fig-muted">时间 t → （大幅提高总线利用率，常见于 PCIe 等高速总线）</text>
  </svg>
  <figcaption>图 7.12：传统占用式总线事务与分离事务通信（Split Transaction）对比</figcaption>
</figure>

> <strong>★考点</strong>：四种定时方式的取舍——同步最简单但受最慢设备限制；异步最灵活但三次握手最慢；半同步折中；分离事务提高利用率、控制最复杂。

## 第8章 输入输出系统

### 8.1 I/O 系统的功能与构成

输入输出系统（I/O 系统）承担 <strong>CPU 与外部设备之间、外部设备与主存之间</strong> 的信息交换，外部设备简称 I/O 设备。它由硬件与软件两部分构成：

- <strong>I/O 硬件</strong>：I/O 设备、I/O 接口、I/O 管理部件（DMA 控制器、通道、I/O 处理机等）、总线；
- <strong>I/O 软件</strong>：设备驱动程序、软件访问接口（系统调用库、I/O 库函数）、用户程序。

在计算机发展早期，I/O 设备都通过 I/O 接口直接连在系统总线上，与 CPU、主存交互；现代计算机普遍采用<strong>分离的层次总线结构</strong>，把不同速度的 I/O 设备接在不同层次的 I/O 总线上，再经扩展总线控制器、桥接芯片或通道处理器与 CPU 交换信息。

<figure class="cpd-figure not-content" id="hco-w7-fig-1">
  <svg viewBox="0 0 700 330" role="img" aria-label="分离的层次总线结构与 I/O 系统组成">
    <line x1="115" y1="62" x2="115" y2="86" class="fig-line"/>
    <line x1="285" y1="62" x2="285" y2="86" class="fig-line"/>
    <line x1="485" y1="62" x2="485" y2="86" class="fig-line"/>
    <line x1="350" y1="110" x2="350" y2="136" class="fig-line"/>
    <line x1="350" y1="176" x2="350" y2="202" class="fig-line"/>
    <line x1="130" y1="224" x2="130" y2="240" class="fig-line"/>
    <line x1="350" y1="224" x2="350" y2="240" class="fig-line"/>
    <line x1="570" y1="224" x2="570" y2="240" class="fig-line"/>
    <line x1="130" y1="274" x2="130" y2="286" class="fig-line"/>
    <line x1="350" y1="274" x2="350" y2="286" class="fig-line"/>
    <line x1="570" y1="274" x2="570" y2="286" class="fig-line"/>
    <rect x="45" y="16" width="140" height="46" rx="4" class="fig-box-blue"/>
    <text x="115" y="44" text-anchor="middle" class="fig-title fig-blue">CPU</text>
    <rect x="215" y="16" width="140" height="46" rx="4" class="fig-box"/>
    <text x="285" y="44" text-anchor="middle" class="fig-title">主存</text>
    <rect x="390" y="16" width="190" height="46" rx="4" class="fig-box-purple"/>
    <text x="485" y="36" text-anchor="middle" class="fig-label fig-purple">I/O 管理部件</text>
    <text x="485" y="53" text-anchor="middle" font-size="10" class="fig-muted">DMA 控制器 / 通道 / IOP</text>
    <rect x="30" y="86" width="640" height="24" rx="3" class="fig-box-subtle"/>
    <text x="350" y="102" text-anchor="middle" font-size="12" class="fig-label">系统总线（地址 / 数据 / 控制）</text>
    <rect x="275" y="136" width="150" height="40" rx="4" class="fig-box-accent"/>
    <text x="350" y="153" text-anchor="middle" class="fig-label fig-accent">扩展总线控制器</text>
    <text x="350" y="169" text-anchor="middle" font-size="10" class="fig-muted">桥接芯片 / 通道</text>
    <rect x="30" y="202" width="640" height="22" rx="3" class="fig-box-subtle"/>
    <text x="350" y="217" text-anchor="middle" font-size="11" class="fig-muted">I/O 总线（低层次，挂接低速设备）</text>
    <rect x="65" y="240" width="130" height="34" rx="4" class="fig-box"/>
    <text x="130" y="261" text-anchor="middle" class="fig-label">I/O 接口</text>
    <rect x="285" y="240" width="130" height="34" rx="4" class="fig-box"/>
    <text x="350" y="261" text-anchor="middle" class="fig-label">I/O 接口</text>
    <rect x="505" y="240" width="130" height="34" rx="4" class="fig-box"/>
    <text x="570" y="261" text-anchor="middle" class="fig-label">I/O 接口</text>
    <rect x="65" y="286" width="130" height="30" rx="4" class="fig-box-subtle"/>
    <text x="130" y="305" text-anchor="middle" font-size="11" class="fig-muted">键盘 / 打印机</text>
    <rect x="285" y="286" width="130" height="30" rx="4" class="fig-box-subtle"/>
    <text x="350" y="305" text-anchor="middle" font-size="11" class="fig-muted">串口 / 终端</text>
    <rect x="505" y="286" width="130" height="30" rx="4" class="fig-box-subtle"/>
    <text x="570" y="305" text-anchor="middle" font-size="11" class="fig-muted">USB / 音频</text>
  </svg>
  <figcaption>图 8.1：分离的层次总线结构（高速设备接高层总线，低速设备接低层 I/O 总线）</figcaption>
</figure>

### 8.2 I/O 接口的功能

I/O 设备<strong>不能直接挂到总线上</strong>，必须通过 I/O 接口（也称 I/O 控制器、设备控制器）转接。接口是 CPU 与设备之间的“翻译 + 缓冲 + 握手”部件，主要功能有七项：

| 功能 | 具体内容 |
|---|---|
| 设备寻址 | 接收并译码地址信息，选中 I/O 设备中的特定寄存器或存储器 |
| 设备控制 | 存储并识别 CPU 送来的命令，实现设备启停、复位、读写等控制 |
| 状态检测 | 采集并保存设备状态（就绪、忙、故障、中断请求），供 CPU 查询 |
| 数据交互 | I/O 设备与 CPU、I/O 设备与主存之间的数据交换，是<strong>最基本</strong>的功能 |
| 数据缓冲 | 用缓冲寄存器暂存数据，协调 CPU 与设备的速度差异 |
| 格式转换 | 位宽不同时做并行/串行转换；电平与总线规范不同时做电平转换 |
| 中断与检错 | 中断时序控制、数据检测与纠错等 |

> <strong>注意</strong>：CPU 发往设备的<strong>控制命令经数据总线</strong>传输；控制总线传送的是协调总线操作本身的<strong>全局信号</strong>（总线请求/应答、中断响应等）。两者不要混淆。

### 8.3 I/O 接口的结构

接口内部由<strong>总线接口</strong>与<strong>内部接口</strong>两部分组成：总线接口必须按总线规范标准设计，内部接口随设备而异、是非标准的。

<figure class="cpd-figure not-content" id="hco-w7-fig-2">
  <svg viewBox="0 0 700 330" role="img" aria-label="I/O 接口内部结构与信号连接">
    <defs>
      <marker id="hco-w7-arr-2" markerWidth="8" markerHeight="8" refX="7" refY="4" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1 L 7 4 L 1 7 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="140" y="18" width="380" height="278" rx="6" class="fig-box"/>
    <text x="154" y="36" font-size="12" class="fig-title">I/O 接口</text>
    <line x1="20" y1="68" x2="163" y2="68" class="fig-line" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="22" y1="104" x2="262" y2="104" class="fig-line-accent" marker-start="url(#hco-w7-arr-2)"/>
    <polyline points="22,166 170,166 170,194" fill="none" class="fig-line" marker-start="url(#hco-w7-arr-2)" marker-end="url(#hco-w7-arr-2)"/>
    <polyline points="255,60 274,60 274,137 283,137" fill="none" class="fig-line" style="stroke:var(--sl-color-orange)" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="274" y1="60" x2="283" y2="60" class="fig-line" style="stroke:var(--sl-color-orange)" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="274" y1="98" x2="283" y2="98" class="fig-line" style="stroke:var(--sl-color-orange)" marker-end="url(#hco-w7-arr-2)"/>
    <polyline points="262,74 262,151 285,151" fill="none" class="fig-line-accent"/>
    <line x1="264" y1="74" x2="283" y2="74" class="fig-line-accent" marker-start="url(#hco-w7-arr-2)" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="262" y1="112" x2="283" y2="112" class="fig-line-accent" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="283" y1="151" x2="264" y2="151" class="fig-line-accent" marker-end="url(#hco-w7-arr-2)"/>
    <polyline points="244,196 244,178 335,178 335,162" fill="none" class="fig-line" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="387" y1="68" x2="450" y2="68" class="fig-line" marker-start="url(#hco-w7-arr-2)" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="385" y1="107" x2="450" y2="107" class="fig-line" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="450" y1="145" x2="387" y2="145" class="fig-line" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="297" y1="228" x2="450" y2="228" class="fig-line" marker-start="url(#hco-w7-arr-2)" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="508" y1="122" x2="555" y2="122" class="fig-line" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="510" y1="156" x2="555" y2="156" class="fig-line-green" marker-start="url(#hco-w7-arr-2)" marker-end="url(#hco-w7-arr-2)"/>
    <line x1="555" y1="190" x2="510" y2="190" class="fig-line" marker-end="url(#hco-w7-arr-2)"/>
    <text x="24" y="62" font-size="11" class="fig-muted">地址总线</text>
    <text x="24" y="96" font-size="11" class="fig-blue">数据总线</text>
    <text x="24" y="158" font-size="11" class="fig-muted">控制总线</text>
    <rect x="165" y="48" width="90" height="40" rx="4" class="fig-box"/>
    <text x="210" y="65" text-anchor="middle" font-size="11" class="fig-label">地址</text>
    <text x="210" y="81" text-anchor="middle" font-size="11" class="fig-label">译码器</text>
    <rect x="165" y="116" width="90" height="40" rx="4" class="fig-box-subtle"/>
    <text x="210" y="133" text-anchor="middle" font-size="11" class="fig-label">设备存储器</text>
    <text x="210" y="147" text-anchor="middle" font-size="10" class="fig-muted">（非必需）</text>
    <rect x="285" y="52" width="100" height="32" rx="4" class="fig-box-blue"/>
    <text x="335" y="72" text-anchor="middle" font-size="12" class="fig-label fig-blue">DBR</text>
    <rect x="285" y="92" width="100" height="30" rx="4" class="fig-box"/>
    <text x="335" y="111" text-anchor="middle" font-size="12" class="fig-label">DCR</text>
    <rect x="285" y="130" width="100" height="30" rx="4" class="fig-box"/>
    <text x="335" y="149" text-anchor="middle" font-size="12" class="fig-label">DSR</text>
    <rect x="168" y="196" width="127" height="62" rx="4" class="fig-box-purple"/>
    <text x="231" y="222" text-anchor="middle" font-size="12" class="fig-label fig-purple">控制逻辑</text>
    <text x="231" y="240" text-anchor="middle" font-size="10" class="fig-muted">指挥中心 / 握手</text>
    <text x="260" y="284" text-anchor="middle" font-size="10" class="fig-muted">总线接口（按总线规范设计）</text>
    <rect x="452" y="52" width="56" height="206" rx="4" class="fig-box-dashed"/>
    <text x="480" y="155" text-anchor="middle" font-size="11" class="fig-muted" transform="rotate(-90 480 155)">内部接口（随设备而异）</text>
    <rect x="557" y="100" width="123" height="114" rx="5" class="fig-box-green"/>
    <text x="618" y="148" text-anchor="middle" font-size="12.5" class="fig-title fig-green">I/O 设备</text>
    <text x="618" y="168" text-anchor="middle" font-size="10" class="fig-muted">键盘 / 磁盘 / 网卡</text>
    <text x="350" y="318" text-anchor="middle" font-size="11" class="fig-muted">读：设备 → DBR → 数据总线 → CPU；写：CPU → 数据总线 → DBR → 设备</text>
  </svg>
  <figcaption>图 8.2：I/O 接口内部结构与总线信号连接</figcaption>
</figure>

各部件作用：

- <strong>数据缓冲寄存器 DBR</strong>：协调 CPU 与设备的速度差异。CPU 读时暂存设备送来的数据，再经总线送入 CPU 寄存器；CPU 写时暂存 CPU 送来的数据，最终输出到设备。
- <strong>设备命令寄存器 DCR</strong>：接收 CPU 经数据总线送来的命令（复位、识别、读写等），据此控制设备。简单设备（键盘输入、字符终端输出）可能没有 DCR。
- <strong>设备状态寄存器 DSR</strong>：向 CPU 反馈设备状态（就绪、忙、错误等），程序查询方式下 CPU 就是靠读 DSR 判断设备状态。简单设备可能没有 DSR。
- 为简化硬件、节省芯片内寄存器资源、降低成本，DCR 与 DSR <strong>可合并为一个寄存器</strong>，例如高位作命令、低位作状态。
- <strong>设备存储器</strong>：供设备自身运算和处理使用（固态硬盘中的 DRAM、显卡上的显存）。它<strong>不是必需的</strong>，取决于具体设备。
- <strong>地址译码器</strong>：对地址总线上的地址译码，选中对应接口及其内部寄存器，从而建立 CPU 与特定设备的通信。
- <strong>控制逻辑</strong>：接口的指挥中心，根据 CPU 经数据总线送来的命令协调各寄存器工作，并管理与设备之间的握手通信。

> <strong>注意</strong>：采用总线主控技术后，I/O 接口也能作为<strong>主设备</strong>，通过 DMA 请求接管总线，独立完成发送地址、命令和读写主存的全套操作。因此接口所连的各类总线都必须支持<strong>双向</strong>信号传输。

### 8.4 I/O 端口的编址

所有 I/O 接口中的寄存器和存储器都由 CPU 统一分配地址并访问，但地址空间的组织方式分两种。

<figure class="cpd-figure not-content" id="hco-w7-fig-3">
  <svg viewBox="0 0 700 296" role="img" aria-label="I/O 端口的统一编址与独立编址对比">
    <line x1="340" y1="24" x2="340" y2="266" class="fig-line-dashed"/>
    <text x="40" y="34" class="fig-title fig-blue">统一编址（内存映射）</text>
    <rect x="90" y="50" width="170" height="204" rx="4" class="fig-box"/>
    <line x1="90" y1="100" x2="260" y2="100" class="fig-divider"/>
    <line x1="90" y1="176" x2="260" y2="176" class="fig-divider"/>
    <rect x="91" y="177" width="168" height="76" class="fig-box-red" stroke="none"/>
    <text x="175" y="79" text-anchor="middle" class="fig-label">ROM（64 KB）</text>
    <text x="175" y="142" text-anchor="middle" class="fig-label">主存 RAM</text>
    <text x="175" y="210" text-anchor="middle" class="fig-label fig-red">I/O 设备端口</text>
    <text x="175" y="228" text-anchor="middle" font-size="10" class="fig-muted">占用最高 64 KB</text>
    <text x="80" y="60" text-anchor="end" font-size="10" class="fig-muted">0x00000</text>
    <text x="80" y="250" text-anchor="end" font-size="10" class="fig-muted">0xFFFFF</text>
    <text x="175" y="278" text-anchor="middle" font-size="11" class="fig-muted">load / store 即可访问，无需专用 I/O 指令</text>
    <text x="380" y="34" class="fig-title fig-green">独立编址（端口映射）</text>
    <rect x="385" y="50" width="130" height="204" rx="4" class="fig-box"/>
    <text x="450" y="154" text-anchor="middle" class="fig-label">主存空间</text>
    <rect x="550" y="114" width="110" height="78" rx="4" class="fig-box-green"/>
    <text x="605" y="148" text-anchor="middle" class="fig-label fig-green">I/O 端口</text>
    <text x="605" y="166" text-anchor="middle" font-size="10" class="fig-muted">独立 64 KB</text>
    <text x="550" y="106" font-size="10" class="fig-muted">0x0000</text>
    <text x="550" y="206" font-size="10" class="fig-muted">0xFFFF</text>
    <text x="520" y="278" text-anchor="middle" font-size="11" class="fig-muted">IN / OUT 访问端口，MOV 访问主存</text>
  </svg>
  <figcaption>图 8.3：I/O 端口的统一编址与独立编址对比</figcaption>
</figure>

<strong>（1）统一编址（内存映射编址）</strong>：I/O 地址与主存地址统一编排，按不同地址区域区分。不需要专用 I/O 指令，用 `load/store` 访存指令即可访问设备。

- 优点：无需专用 I/O 指令，编程灵活；
- 缺点：主存地址空间被设备占去一部分；I/O 地址与主存地址等长，接口译码逻辑复杂。

MIPS、ARM 等精简指令集处理器采用统一编址。大部分接口地址在设计时已确定，扩展卡、热插拔设备则在系统启动时动态分配。

> <strong>注意：I/O 地址虽映射到存储空间，但接口中的数据是动态变化的（如设备状态实时更新），不能使用 Cache 缓存</strong>，否则 CPU 读不到最新状态。C 语言中访问 I/O 的变量应声明为 `volatile`，防止编译器做不恰当的优化，保证每次都从该地址重新读取。

<strong>（2）独立编址（端口映射编址）</strong>：I/O 地址空间与主存空间相互独立，必须使用<strong>专用 I/O 指令</strong>访问（x86 的 `IN`、`OUT`；主存仍用 `MOV`）。I/O 地址又称 I/O 端口。

- 64KB 的 I/O 地址空间较小，地址译码逻辑简单；
- 两空间相互独立，但地址区间可能重叠，靠<strong>指令</strong>而非地址区分。

随着设备内置存储器容量增大（如显存），I/O 地址空间日益紧张，PCI 总线出现后，无论统一编址还是独立编址，接口中的大容量存储器都必须通过<strong>内存映射</strong>映射到存储空间。

### 8.5 I/O 控制方式概述

I/O 控制方式指 CPU 与 I/O 设备交换信息时所采用的控制方式，其发展遵循“由简单到复杂、由低效到高效、由 CPU 集中控制到分散控制”的路线。

| 方式 | 数据传送过程 | CPU 参与程度 | 主要实现 | 适用设备 |
|---|---|---|---|---|
| 程序直接控制（程序查询） | CPU 读 DSR 查询状态，数据经 CPU 寄存器中转 | 全程占用或轮询 | 软件 | 低速 |
| 中断驱动 | 设备就绪后主动发中断，CPU 响应后传送 | 每传送一个字都需 CPU | 软件为主 | 低速 |
| 直接存储器存取（DMA） | DMA 控制器接管总线，设备与主存成批传送 | 仅开始与结束 | 硬件 | 高速 |
| 通道控制 | 通道执行通道程序，独立管理整个 I/O 过程 | 只需发一条 I/O 指令 | 硬件 | 高速 |
| I/O 处理机 | 拥有独立指令系统与存储器，可处理复杂 I/O 与差错恢复 | 进一步解放 CPU | 硬件 | 高速复杂 |

程序查询与中断驱动主要由<strong>软件</strong>实现，适合数据传输率较低的设备；DMA、通道、I/O 处理机主要由<strong>硬件</strong>实现，适合高速设备。本课程重点是程序查询、中断驱动和 DMA 三种。

### 8.6 程序查询方式

<strong>基本思想</strong>：CPU 按独占式查询或定时轮询策略读取 I/O 接口的状态寄存器 DSR，检查设备是否就绪；就绪后数据经 <strong>CPU 通用寄存器</strong>中转——读操作时设备 → DBR → CPU 寄存器 → 主存，写操作时主存 → CPU 寄存器 → DBR → 设备。

<figure class="cpd-figure not-content" id="hco-w7-fig-4">
  <svg viewBox="0 0 700 350" role="img" aria-label="程序查询方式流程与两种查询策略时间轴">
    <defs>
      <marker id="hco-w7-arr-4" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="150" y1="54" x2="150" y2="82" class="fig-line" marker-end="url(#hco-w7-arr-4)"/>
    <line x1="150" y1="120" x2="150" y2="146" class="fig-line" marker-end="url(#hco-w7-arr-4)"/>
    <polyline points="235,172 258,172 258,101 232,101" fill="none" stroke="var(--sl-color-red)" stroke-width="1.4" marker-end="url(#hco-w7-arr-4)"/>
    <line x1="150" y1="198" x2="150" y2="226" class="fig-line" marker-end="url(#hco-w7-arr-4)"/>
    <line x1="150" y1="264" x2="150" y2="290" class="fig-line" marker-end="url(#hco-w7-arr-4)"/>
    <polyline points="75,314 30,314 30,101 66,101" fill="none" stroke="var(--sl-color-red)" stroke-width="1.4" marker-end="url(#hco-w7-arr-4)"/>
    <line x1="330" y1="208" x2="670" y2="208" class="fig-line" marker-end="url(#hco-w7-arr-4)"/>
    <rect x="70" y="18" width="160" height="36" rx="4" class="fig-box"/>
    <text x="150" y="40" text-anchor="middle" class="fig-label">启动设备，送出命令</text>
    <rect x="70" y="84" width="160" height="36" rx="4" class="fig-box-blue"/>
    <text x="150" y="106" text-anchor="middle" class="fig-label fig-blue">读 DSR 查状态</text>
    <polygon points="150,148 235,172 150,196 65,172" class="fig-box"/>
    <text x="150" y="176" text-anchor="middle" class="fig-label">就绪？</text>
    <text x="242" y="166" font-size="10" class="fig-red">否</text>
    <text x="262" y="94" font-size="10" class="fig-red">循环等待</text>
    <text x="160" y="216" font-size="11" class="fig-green">是</text>
    <rect x="70" y="228" width="160" height="36" rx="4" class="fig-box-green"/>
    <text x="150" y="250" text-anchor="middle" class="fig-label fig-green">经 CPU 寄存器传送</text>
    <polygon points="150,292 225,314 150,336 75,314" class="fig-box"/>
    <text x="150" y="318" text-anchor="middle" class="fig-label">全部完成？</text>
    <text x="42" y="334" font-size="11" class="fig-red">否</text>
    <line x1="225" y1="314" x2="258" y2="314" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#hco-w7-arr-4)"/>
    <text x="240" y="308" text-anchor="middle" font-size="10.5" class="fig-green">是</text>
    <rect x="260" y="302" width="46" height="24" rx="4" class="fig-box-green"/>
    <text x="283" y="318" text-anchor="middle" font-size="10.5" class="fig-green">结束</text>
    <line x1="320" y1="24" x2="320" y2="332" class="fig-line-dashed"/>
    <text x="340" y="32" class="fig-title fig-muted">两种查询策略（时间轴对比）</text>
    <text x="340" y="58" class="fig-label">1. 独占式查询</text>
    <rect x="350" y="68" width="280" height="22" rx="3" class="fig-box-red"/>
    <text x="490" y="83" text-anchor="middle" font-size="11" class="fig-red">CPU 连续轮询 DSR，完全被占用</text>
    <rect x="350" y="96" width="280" height="20" rx="3" class="fig-box-dashed"/>
    <text x="490" y="110" text-anchor="middle" font-size="10" class="fig-muted">设备准备数据（CPU 空转，串行等待）</text>
    <text x="340" y="142" class="fig-label">2. 定时轮询</text>
    <rect x="350" y="150" width="76" height="22" rx="3" class="fig-box-blue"/>
    <text x="388" y="165" text-anchor="middle" font-size="10" class="fig-blue">其他任务</text>
    <rect x="432" y="150" width="26" height="22" rx="3" class="fig-box-red"/>
    <text x="445" y="165" text-anchor="middle" font-size="10" class="fig-red">查</text>
    <rect x="464" y="150" width="76" height="22" rx="3" class="fig-box-blue"/>
    <text x="502" y="165" text-anchor="middle" font-size="10" class="fig-blue">其他任务</text>
    <rect x="546" y="150" width="26" height="22" rx="3" class="fig-box-red"/>
    <text x="559" y="165" text-anchor="middle" font-size="10" class="fig-red">查</text>
    <rect x="578" y="150" width="52" height="22" rx="3" class="fig-box-blue"/>
    <text x="604" y="165" text-anchor="middle" font-size="10" class="fig-blue">任务</text>
    <rect x="350" y="178" width="280" height="20" rx="3" class="fig-box-dashed"/>
    <text x="490" y="192" text-anchor="middle" font-size="10" class="fig-muted">设备准备数据（CPU 并行执行其他任务）</text>
    <text x="666" y="224" text-anchor="end" font-size="10" class="fig-muted">时间</text>
    <text x="340" y="250" font-size="11" class="fig-muted">• 独占式：单任务 OS，轮询等待浪费大量 CPU 周期</text>
    <text x="340" y="272" font-size="11" class="fig-muted">• 定时轮询：多任务 OS，定时器中断仍有固定开销</text>
    <text x="340" y="294" font-size="11" class="fig-muted">• 适用于设备本身无中断请求功能的多任务系统</text>
    <text x="340" y="316" font-size="11" class="fig-muted">• 高速同步 I/O（如 NVMe）准备极短，查询反快于中断</text>
  </svg>
  <figcaption>图 8.4：程序查询方式流程图与独占式 / 定时轮询时间轴对比</figcaption>
</figure>

| 策略 | 设备准备期间 CPU 状态 | 代价 | 适用 |
|---|---|---|---|
| 独占式查询 | 完全被查询程序占用，循环等待 | 浪费大量 CPU 时间 | 单任务 OS；NVMe 固态硬盘的同步 I/O 也采用 |
| 定时轮询 | 可执行其他任务，定时器中断时查询 | 定时器中断服务占用一定 CPU 时间 | 设备无中断请求功能的多任务 OS |

> <strong>★考点</strong>：程序查询方式下 CPU 与设备<strong>串行</strong>工作，CPU 效率低；但因无需中断开销，在设备准备时间极短的高速场合，独占式查询反而可能比中断方式更快——不能简单断言“查询只适合慢速设备”。

### 8.7 中断与中断驱动方式

<strong>中断</strong>：CPU 在执行程序过程中，遇到急需处理的异常或外部事件时，暂停现行程序，转去执行相应的处理程序，处理完再返回断点继续执行。

按不同角度分类：<strong>向量中断 / 非向量中断</strong>；<strong>单重（单级）中断 / 多重中断</strong>。按中断源分为：

- <strong>外部中断</strong>：可屏蔽中断（经中断控制器送 CPU 的 INTR，可由中断允许标志 IF 与中断屏蔽寄存器屏蔽）；不可屏蔽中断（NMI，请求信号直接接到 CPU 内部中断逻辑，优先级最高、不受屏蔽）；
- <strong>内部异常</strong>：除零、溢出、断点、缺页等，由 CPU 内部中断逻辑处理。其中硬件终止异常优先级最高，故障、自陷等软件异常次之。

<strong>中断处理的一般过程</strong>：关中断 → 保存断点（PC、PSW）→ 中断识别（取得中断号/向量地址）→ 执行中断服务程序 → 恢复现场、开中断、中断返回。

<figure class="cpd-figure not-content" id="hco-w7-fig-5">
  <svg viewBox="0 0 722 220" role="img" aria-label="中断处理一般过程：硬件中断隐指令周期与软件 ISR 执行流程">
    <defs>
      <marker id="hco-w7-arr-5" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <rect x="20" y="20" width="275" height="146" rx="6" class="fig-box-dashed"/>
    <text x="157" y="40" text-anchor="middle" class="fig-label fig-red">硬件：中断隐指令周期（自动完成）</text>
    <rect x="315" y="20" width="387" height="146" rx="6" class="fig-box-dashed"/>
    <text x="508" y="40" text-anchor="middle" class="fig-label fig-blue">软件：中断服务程序 ISR（编程控制）</text>
    <line x1="106" y1="86" x2="118" y2="86" class="fig-line" marker-end="url(#hco-w7-arr-5)"/>
    <line x1="196" y1="86" x2="208" y2="86" class="fig-line" marker-end="url(#hco-w7-arr-5)"/>
    <line x1="286" y1="86" x2="328" y2="86" class="fig-line-accent" marker-end="url(#hco-w7-arr-5)"/>
    <line x1="406" y1="86" x2="418" y2="86" class="fig-line" marker-end="url(#hco-w7-arr-5)"/>
    <line x1="498" y1="86" x2="510" y2="86" class="fig-line" marker-end="url(#hco-w7-arr-5)"/>
    <line x1="588" y1="86" x2="600" y2="86" class="fig-line" marker-end="url(#hco-w7-arr-5)"/>
    <rect x="30" y="58" width="76" height="56" rx="4" class="fig-box-red"/>
    <text x="68" y="82" text-anchor="middle" font-size="11" class="fig-red">1. 关中断</text>
    <text x="68" y="100" text-anchor="middle" font-size="9" class="fig-muted">IF ← 0</text>
    <rect x="120" y="58" width="76" height="56" rx="4" class="fig-box-red"/>
    <text x="158" y="82" text-anchor="middle" font-size="11" class="fig-red">2. 存断点</text>
    <text x="158" y="100" text-anchor="middle" font-size="9" class="fig-muted">PC、PSW 入栈</text>
    <rect x="210" y="58" width="76" height="56" rx="4" class="fig-box-red"/>
    <text x="248" y="82" text-anchor="middle" font-size="11" class="fig-red">3. 引出入口</text>
    <text x="248" y="100" text-anchor="middle" font-size="9" class="fig-muted">向量 / 查询</text>
    <rect x="330" y="58" width="76" height="56" rx="4" class="fig-box-blue"/>
    <text x="368" y="82" text-anchor="middle" font-size="11" class="fig-blue">4. 保护现场</text>
    <text x="368" y="100" text-anchor="middle" font-size="9" class="fig-muted">通用寄存器</text>
    <rect x="420" y="58" width="78" height="56" rx="4" class="fig-box-purple"/>
    <text x="459" y="82" text-anchor="middle" font-size="9.8" class="fig-purple">5. 设屏蔽/开</text>
    <text x="459" y="100" text-anchor="middle" font-size="9" class="fig-muted">支持多重中断</text>
    <rect x="512" y="58" width="76" height="56" rx="4" class="fig-box-green"/>
    <text x="550" y="82" text-anchor="middle" font-size="11" class="fig-green">6. 执行服务</text>
    <text x="550" y="100" text-anchor="middle" font-size="9" class="fig-muted">I/O 传送处理</text>
    <rect x="602" y="58" width="88" height="56" rx="4" class="fig-box-blue"/>
    <text x="646" y="82" text-anchor="middle" font-size="11" class="fig-blue">7. 恢复/返</text>
    <text x="646" y="100" text-anchor="middle" font-size="8.4" class="fig-muted">关→复→开→IRET</text>
    <text x="157" y="146" text-anchor="middle" font-size="10" class="fig-muted">指令周期末尾采样，响应后硬件自动执行</text>
    <text x="508" y="146" text-anchor="middle" font-size="10" class="fig-muted">恢复现场期间需再次关中断，防止断点与寄存器被破坏</text>
    <text x="361" y="196" text-anchor="middle" font-size="11" class="fig-muted">区分关键：保存断点（PC/PSW）由硬件隐指令完成；保护通用寄存器现场由软件 ISR 完成</text>
  </svg>
  <figcaption>图 8.5：中断处理一般过程（硬件隐指令周期与软件中断服务程序边界）</figcaption>
</figure>

<strong>中断驱动方式的工作过程</strong>：CPU 执行用户进程 P1，P1 通过系统调用 `read` 请求读数据；设备驱动程序经 I/O 接口把读命令与参数（设备地址、数据量、目标缓冲区）发给设备。P1 随即调用 `wait`，由操作系统放入 I/O 等待队列，进程调度切换到 P2 运行——<strong>设备准备数据与 P2 运行并行</strong>。设备准备好后主动发中断请求，CPU 暂停 P2、转去执行该设备的中断服务程序（此时处于内核态）；服务程序用 `signal` 把 P1 唤醒、放入就绪队列，最后执行中断返回指令回到 P2 的断点。P1 获得时间片后完成数据传送。

<figure class="cpd-figure not-content" id="hco-w7-fig-6">
  <svg viewBox="0 0 700 320" role="img" aria-label="中断驱动方式的工作过程与进程调度时序">
    <defs>
      <marker id="hco-w7-arr-6" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="160" y1="52" x2="160" y2="282" class="fig-line-dashed"/>
    <line x1="530" y1="52" x2="530" y2="282" class="fig-line-dashed"/>
    <line x1="160" y1="76" x2="526" y2="76" class="fig-line-accent" marker-end="url(#hco-w7-arr-6)"/>
    <line x1="530" y1="150" x2="164" y2="150" stroke="var(--sl-color-red)" fill="none" stroke-width="1.5" marker-end="url(#hco-w7-arr-6)"/>
    <rect x="65" y="16" width="190" height="36" rx="4" class="fig-box-blue"/>
    <text x="160" y="39" text-anchor="middle" class="fig-title fig-blue">CPU / 操作系统</text>
    <rect x="435" y="16" width="190" height="36" rx="4" class="fig-box-subtle"/>
    <text x="530" y="39" text-anchor="middle" class="fig-title">I/O 设备</text>
    <text x="345" y="68" text-anchor="middle" font-size="11" class="fig-blue">read 系统调用 → 驱动发命令、启动设备</text>
    <rect x="45" y="92" width="230" height="34" rx="4" class="fig-box"/>
    <text x="160" y="113" text-anchor="middle" font-size="11" class="fig-label">P1 wait 入等待队列，调度 P2 运行</text>
    <rect x="425" y="92" width="210" height="34" rx="4" class="fig-box-green"/>
    <text x="530" y="113" text-anchor="middle" font-size="11" class="fig-green">准备数据（与 P2 并行）</text>
    <text x="345" y="142" text-anchor="middle" font-size="11" class="fig-red">中断请求（数据就绪）</text>
    <rect x="45" y="166" width="250" height="32" rx="4" class="fig-box-red"/>
    <text x="170" y="186" text-anchor="middle" font-size="11" class="fig-red">暂停 P2，转设备 ISR（内核态）</text>
    <rect x="45" y="206" width="250" height="32" rx="4" class="fig-box-red"/>
    <text x="170" y="226" text-anchor="middle" font-size="11" class="fig-red">signal：唤醒 P1 进就绪队列</text>
    <rect x="45" y="246" width="230" height="32" rx="4" class="fig-box-blue"/>
    <text x="160" y="266" text-anchor="middle" font-size="11" class="fig-blue">中断返回，继续执行 P2</text>
    <text x="45" y="302" font-size="11" class="fig-muted">P1 随后获得时间片完成数据传送（含两次上下文切换与 ISR 开销）</text>
  </svg>
  <figcaption>图 8.6：中断驱动方式的工作过程与进程调度时序</figcaption>
</figure>

与程序查询相比：中断驱动<strong>不需要轮询设备状态</strong>（省去查询开销），且设备主动请求比定时轮询更及时，不必选择定时器时间间隔；但仍有<strong>两次上下文切换</strong>（P1 让出、P1 恢复）和<strong>中断服务程序自身的开销。因此它提升效率的前提是“额外开销小于设备准备数据的时间”，适合慢速设备</strong>；对准备时间极短的高速设备，中断开销可能超过收益，反而不如独占式查询（如 NVMe 固态硬盘的同步 I/O）。

> <strong>★考点</strong>：中断驱动方式的 CPU 时间占比与“中断请求间隔/每次响应处理时间”直接相关；若每次请求都要两次上下文切换，间隔越短，CPU 被中断吞噬的比例越高。这正是高速设备必须改用 DMA 的原因。

<strong>（1）中断优先级的两层含义</strong>

- <strong>响应优先级</strong>：多个中断源<strong>同时</strong>请求时，CPU 响应它们的先后次序。硬件设计时已固定，难以更改，按中断事件的重要性与迫切性划分。
- <strong>处理优先级</strong>：多重中断中<strong>实际的处理次序</strong>。单重中断不存在处理优先级的概念；不使用屏蔽技术时，处理优先级与响应优先级相同。

响应优先级从高到低的一般规律：

1. 不可屏蔽中断（外部）最高；
2. 内部异常居中（硬件终止 > 故障/自陷等软件异常）；
3. 可屏蔽中断（外部）最低；
4. DMA 请求优先于 I/O 设备传送类中断；同类中高速设备优先于低速设备，输入设备优先于输出设备，实时控制设备优先于普通设备。

<strong>（2）中断屏蔽</strong>

中断控制器中设有<strong>中断屏蔽寄存器 IMR</strong>，每一位对应一个中断源：该位为 1 表示屏蔽对应设备的中断请求，为 0 表示允许。IMR 的值称为<strong>中断屏蔽字</strong>。通过给各设备配置不同的屏蔽字，可以<strong>动态调整多重中断的处理优先级</strong>，使低优先级中断也能打断高优先级的中断服务程序。

> <strong>注意</strong>：中断屏蔽只在 CPU <strong>运行中断服务程序</strong>时对处理优先级起作用，不影响运行主程序时的响应优先级；若在运行中断服务程序时同时到达多个未屏蔽请求，仍按响应优先级处理；屏蔽字仅在执行中断服务程序时才能被更新。

<strong>（3）例：A、B、C 同时请求，响应优先级 A > B > C</strong>

| 设备 | 例一屏蔽字 | 例一处理次序 | 例二屏蔽字 | 例二处理次序 |
|---|---|---|---|---|
| A | 111 | 1 | 100 | 3 |
| B | 011 | 2 | 110 | 2 |
| C | 001 | 3 | 111 | 1 |

例一：A 的屏蔽字 111 屏蔽自身及 B、C，A 的服务程序一次执行完；随后按响应优先级依次处理 B、C，处理次序与响应次序一致。

例二：A 的屏蔽字 100 只屏蔽自身，B、C 的请求仍有效，于是 A 的服务程序被 B 打断；B 的屏蔽字 110 屏蔽自身与 A，C 仍可打断 B；C 的屏蔽字 111 屏蔽全部。最终按 C → B → A 的顺序返回，处理次序与响应次序相反。

<figure class="cpd-figure not-content" id="hco-w7-fig-7">
  <svg viewBox="0 0 700 300" role="img" aria-label="多重中断屏蔽字 IMR 嵌套执行时间轴">
    <defs>
      <marker id="hco-w7-arr-7" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="175" y1="56" x2="175" y2="82" class="fig-line-dashed"/>
    <line x1="315" y1="104" x2="315" y2="130" class="fig-line-dashed"/>
    <line x1="445" y1="152" x2="445" y2="178" class="fig-line-dashed"/>
    <line x1="560" y1="178" x2="560" y2="152" class="fig-line-dashed"/>
    <line x1="600" y1="130" x2="600" y2="104" class="fig-line-dashed"/>
    <line x1="635" y1="82" x2="635" y2="56" class="fig-line-dashed"/>
    <line x1="85" y1="224" x2="675" y2="224" class="fig-line" marker-end="url(#hco-w7-arr-7)"/>
    <text x="26" y="50" class="fig-label fig-muted">主程序</text>
    <text x="26" y="98" class="fig-label fig-red">A 服务</text>
    <text x="26" y="146" class="fig-label fig-orange">B 服务</text>
    <text x="26" y="194" class="fig-label fig-purple">C 服务</text>
    <rect x="85" y="34" width="90" height="22" rx="3" class="fig-box-blue"/>
    <text x="130" y="49" text-anchor="middle" font-size="11" class="fig-blue">主程序</text>
    <rect x="175" y="82" width="140" height="22" rx="3" class="fig-box-red"/>
    <text x="245" y="97" text-anchor="middle" font-size="11" class="fig-red">A（IMR=100）</text>
    <rect x="315" y="130" width="130" height="22" rx="3" class="fig-box-orange"/>
    <text x="380" y="145" text-anchor="middle" font-size="11" class="fig-orange">B（IMR=110）</text>
    <rect x="445" y="178" width="115" height="22" rx="3" class="fig-box-purple"/>
    <text x="502" y="193" text-anchor="middle" font-size="11" class="fig-purple">C（IMR=111）</text>
    <rect x="560" y="130" width="40" height="22" rx="3" class="fig-box-orange"/>
    <text x="580" y="145" text-anchor="middle" font-size="10" class="fig-orange">B 续</text>
    <rect x="600" y="82" width="35" height="22" rx="3" class="fig-box-red"/>
    <text x="617" y="97" text-anchor="middle" font-size="10" class="fig-red">A 续</text>
    <rect x="635" y="34" width="38" height="22" rx="3" class="fig-box-blue"/>
    <text x="654" y="49" text-anchor="middle" font-size="10" class="fig-blue">主续</text>
    <text x="670" y="242" text-anchor="end" font-size="10" class="fig-muted">时间</text>
    <text x="85" y="264" font-size="11" class="fig-muted">• 例二处理次序 C → B → A：屏蔽字中 1 的个数越多，处理优先级越高（自身对应位必为 1）</text>
    <text x="85" y="284" font-size="11" class="fig-muted">• 例一（A=111，B=011，C=001）不嵌套，处理次序与响应次序一致：A → B → C</text>
  </svg>
  <figcaption>图 8.7：中断屏蔽字（IMR）动态改变多重中断处理次序示例（例二嵌套轨迹）</figcaption>
</figure>

> <strong>★考点</strong>：判断某设备的中断服务程序应设置的屏蔽字——<strong>优先级高于自己的设备对应位取 0（允许打断），自身及优先级低于自己的设备对应位取 1（屏蔽）</strong>。例如 L0 > L2 > L1 > L3 时，L1 服务程序的屏蔽字中 L0、L2 位为 0，L1、L3 位为 1。漏掉“屏蔽自身”这一位是常见错因。

### 8.8 中断请求方式与中断控制器

如何把大量外部中断源的请求信号有效连接到 CPU，是中断技术的关键问题，常见四种方式。

<strong>（1）独立请求方式</strong>：每个中断源都有独立的中断请求线和中断应答线。源经请求线发请求，CPU 经应答线回应答，收到应答的源清除自己的请求。

- 优点：CPU 可灵活处理优先级；中断识别简单（用优先级编码电路即可）；便于实现向量中断，快速找到服务程序入口；
- 缺点：每个源都占用独立的请求、应答线，大量占用 CPU 引脚，系统难以扩展。

<strong>（2）链式请求方式</strong>：所有中断源共用一条中断请求线，中断源识别有两种办法。

- <strong>硬件查询</strong>：中断应答信号沿菊花链串行传递，位于传递路径上离 CPU 越近的源优先级越高；
- <strong>软件查询</strong>：公共中断服务程序轮询各源接口中的寄存器（DSR），有请求则转入对应服务程序，否则查下一个；优先级由轮询顺序决定，先查者优先。

优点：扩展容易，增加中断源只需接入公共请求线，无需额外布线，硬件实现简单；缺点：中断源识别过程复杂，硬件查询的优先级由物理位置固定、无法用软件灵活调整，软件查询则需 CPU 逐个发起总线事务轮询 DSR。

<figure class="cpd-figure not-content" id="hco-w7-fig-8">
  <svg viewBox="0 0 700 280" role="img" aria-label="独立请求方式与链式请求方式结构对比">
    <defs>
      <marker id="hco-w7-arr-8" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="340" y1="22" x2="340" y2="262" class="fig-line-dashed"/>
    <text x="30" y="24" class="fig-title fig-blue">（a）独立请求方式</text>
    <line x1="226" y1="54" x2="112" y2="54" class="fig-line" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="110" y1="68" x2="224" y2="68" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="226" y1="104" x2="112" y2="104" class="fig-line" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="110" y1="118" x2="224" y2="118" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="226" y1="154" x2="112" y2="154" class="fig-line" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="110" y1="168" x2="224" y2="168" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <rect x="30" y="48" width="80" height="132" rx="4" class="fig-box"/>
    <text x="70" y="118" text-anchor="middle" class="fig-title">CPU</text>
    <rect x="228" y="46" width="86" height="30" rx="4" class="fig-box"/>
    <text x="271" y="65" text-anchor="middle" font-size="11" class="fig-label">设备 1</text>
    <rect x="228" y="96" width="86" height="30" rx="4" class="fig-box"/>
    <text x="271" y="115" text-anchor="middle" font-size="11" class="fig-label">设备 2</text>
    <rect x="228" y="146" width="86" height="30" rx="4" class="fig-box"/>
    <text x="271" y="165" text-anchor="middle" font-size="11" class="fig-label">设备 3</text>
    <text x="168" y="48" text-anchor="middle" font-size="10" class="fig-muted">REQ₁</text>
    <text x="168" y="82" text-anchor="middle" font-size="10" class="fig-blue">ACK₁</text>
    <text x="168" y="198" text-anchor="middle" font-size="11" class="fig-muted">每源独立 REQ / ACK：引脚多、识别快</text>
    <text x="366" y="24" class="fig-title fig-green">（b）链式请求方式</text>
    <line x1="480" y1="112" x2="448" y2="112" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="480" y1="68" x2="480" y2="174" class="fig-line-accent"/>
    <line x1="530" y1="68" x2="484" y2="68" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="530" y1="120" x2="484" y2="120" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="530" y1="174" x2="484" y2="174" class="fig-line-accent" marker-end="url(#hco-w7-arr-8)"/>
    <polyline points="446,136 502,136 502,40 595,40 595,50" fill="none" stroke="var(--sl-color-red)" stroke-width="1.4" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="595" y1="84" x2="595" y2="102" stroke="var(--sl-color-red)" fill="none" stroke-width="1.4" marker-end="url(#hco-w7-arr-8)"/>
    <line x1="595" y1="136" x2="595" y2="156" stroke="var(--sl-color-red)" fill="none" stroke-width="1.4" marker-end="url(#hco-w7-arr-8)"/>
    <rect x="366" y="60" width="80" height="122" rx="4" class="fig-box"/>
    <text x="406" y="125" text-anchor="middle" class="fig-title">CPU</text>
    <text x="466" y="58" text-anchor="middle" font-size="10" class="fig-blue">REQ</text>
    <text x="462" y="150" text-anchor="middle" font-size="10" class="fig-red">INTA</text>
    <rect x="532" y="52" width="126" height="32" rx="4" class="fig-box"/>
    <text x="595" y="72" text-anchor="middle" font-size="11" class="fig-label">设备 1（优先高）</text>
    <rect x="532" y="104" width="126" height="32" rx="4" class="fig-box"/>
    <text x="595" y="124" text-anchor="middle" font-size="11" class="fig-label">设备 2</text>
    <rect x="532" y="158" width="126" height="32" rx="4" class="fig-box"/>
    <text x="595" y="178" text-anchor="middle" font-size="11" class="fig-label">设备 3（优先低）</text>
    <text x="595" y="210" text-anchor="middle" font-size="10" class="fig-red">INTA 菊花链串行传递</text>
    <text x="515" y="236" text-anchor="middle" font-size="11" class="fig-muted">共用一条请求线；离 CPU 越近优先级越高</text>
    <text x="515" y="256" text-anchor="middle" font-size="11" class="fig-muted">也可由公共服务程序轮询 DSR（软件查询）</text>
  </svg>
  <figcaption>图 8.8：独立请求方式与链式请求方式（硬件菊花链）结构对比</figcaption>
</figure>

<strong>（3）分组链式请求方式：各中断源分组，组间采用独立请求，组内</strong>采用链式请求，相当于二维分层结构；对要求快速响应的中断源仍用独立请求方式（“插队”）。它在 CPU 引脚资源消耗与响应速度之间取得平衡。

<figure class="cpd-figure not-content" id="hco-w7-fig-9">
  <svg viewBox="0 0 700 250" role="img" aria-label="分组链式请求方式结构">
    <defs>
      <marker id="hco-w7-arr-9" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="172" y1="59" x2="114" y2="59" class="fig-line-accent" marker-end="url(#hco-w7-arr-9)"/>
    <line x1="172" y1="95" x2="114" y2="95" class="fig-line-accent" marker-end="url(#hco-w7-arr-9)"/>
    <line x1="157" y1="184" x2="114" y2="184" class="fig-line-green" marker-end="url(#hco-w7-arr-9)"/>
    <line x1="112" y1="204" x2="365" y2="204" stroke="var(--sl-color-red)" fill="none" stroke-width="1.4" marker-end="url(#hco-w7-arr-9)"/>
    <rect x="30" y="46" width="82" height="166" rx="4" class="fig-box"/>
    <text x="71" y="132" text-anchor="middle" class="fig-title">CPU</text>
    <rect x="157" y="34" width="218" height="84" rx="5" class="fig-box-dashed"/>
    <text x="266" y="26" text-anchor="middle" class="fig-label fig-blue">组 1：高实时独立请求（直连插队）</text>
    <rect x="172" y="46" width="188" height="26" rx="4" class="fig-box-blue"/>
    <text x="266" y="63" text-anchor="middle" font-size="11" class="fig-blue">高实时中断源 1（独立 REQ/ACK）</text>
    <rect x="172" y="82" width="188" height="26" rx="4" class="fig-box-blue"/>
    <text x="266" y="99" text-anchor="middle" font-size="11" class="fig-blue">高实时中断源 2（独立 REQ/ACK）</text>
    <rect x="157" y="148" width="218" height="76" rx="5" class="fig-box-dashed"/>
    <text x="266" y="140" text-anchor="middle" class="fig-label fig-green">组 2：链式请求（常规设备）</text>
    <rect x="169" y="160" width="44" height="26" rx="4" class="fig-box-green"/>
    <text x="191" y="177" text-anchor="middle" font-size="10" class="fig-green">P1</text>
    <rect x="219" y="160" width="44" height="26" rx="4" class="fig-box-green"/>
    <text x="241" y="177" text-anchor="middle" font-size="10" class="fig-green">P2</text>
    <rect x="269" y="160" width="44" height="26" rx="4" class="fig-box-subtle"/>
    <text x="291" y="177" text-anchor="middle" font-size="10" class="fig-muted">…</text>
    <rect x="319" y="160" width="44" height="26" rx="4" class="fig-box-green"/>
    <text x="341" y="177" text-anchor="middle" font-size="10" class="fig-green">PN</text>
    <text x="266" y="218" text-anchor="middle" font-size="10" class="fig-red">组内 INTA 链式传递</text>
    <rect x="415" y="64" width="255" height="122" rx="5" class="fig-box-subtle"/>
    <text x="432" y="92" font-size="11" class="fig-label">二维分层折中方案：</text>
    <text x="432" y="116" font-size="11" class="fig-muted">• 组间采用独立请求（省引脚）</text>
    <text x="432" y="140" font-size="11" class="fig-muted">• 组内采用链式排队（易扩展）</text>
    <text x="432" y="164" font-size="11" class="fig-muted">• 高实时源可独立连线“插队”</text>
  </svg>
  <figcaption>图 8.9：分组链式请求方式（组间独立请求、组内链式请求）</figcaption>
</figure>

<strong>（4）中断控制器方式</strong>：各中断源用独立请求方式接到中断控制器，控制器完成优先级仲裁与中断识别后，向 CPU 发中断请求；CPU 回中断响应，控制器把中断源编号经<strong>数据总线</strong>送给 CPU。中断控制器本身也是一个 I/O 设备，CPU 可通过总线访问其接口来编程配置，故称<strong>可编程中断控制器</strong>。

<figure class="cpd-figure not-content" id="hco-w7-fig-10">
  <svg viewBox="0 0 700 246" role="img" aria-label="可编程中断控制器方式连接示意图">
    <defs>
      <marker id="hco-w7-arr-10" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="138" y1="45" x2="230" y2="45" class="fig-line" marker-end="url(#hco-w7-arr-10)"/>
    <line x1="138" y1="84" x2="230" y2="84" class="fig-line" marker-end="url(#hco-w7-arr-10)"/>
    <line x1="138" y1="123" x2="230" y2="123" class="fig-line" marker-end="url(#hco-w7-arr-10)"/>
    <line x1="138" y1="176" x2="230" y2="176" class="fig-line" marker-end="url(#hco-w7-arr-10)"/>
    <line x1="392" y1="84" x2="526" y2="84" stroke="var(--sl-color-red)" fill="none" stroke-width="1.5" marker-end="url(#hco-w7-arr-10)"/>
    <line x1="528" y1="116" x2="394" y2="116" class="fig-line-green" marker-end="url(#hco-w7-arr-10)"/>
    <line x1="392" y1="148" x2="526" y2="148" class="fig-line-accent" marker-end="url(#hco-w7-arr-10)"/>
    <rect x="34" y="31" width="104" height="28" rx="4" class="fig-box"/>
    <text x="86" y="49" text-anchor="middle" font-size="11" class="fig-label">设备 0</text>
    <rect x="34" y="70" width="104" height="28" rx="4" class="fig-box"/>
    <text x="86" y="88" text-anchor="middle" font-size="11" class="fig-label">设备 1</text>
    <rect x="34" y="109" width="104" height="28" rx="4" class="fig-box"/>
    <text x="86" y="127" text-anchor="middle" font-size="11" class="fig-label">设备 2</text>
    <text x="86" y="153" text-anchor="middle" font-size="12" class="fig-muted">…</text>
    <rect x="34" y="162" width="104" height="28" rx="4" class="fig-box"/>
    <text x="86" y="180" text-anchor="middle" font-size="11" class="fig-label">设备 7</text>
    <rect x="232" y="28" width="160" height="164" rx="5" class="fig-box-blue"/>
    <text x="312" y="64" text-anchor="middle" class="fig-title fig-blue">中断控制器</text>
    <text x="312" y="92" text-anchor="middle" font-size="10" class="fig-muted">IRR / ISR / IMR / PR</text>
    <text x="312" y="118" text-anchor="middle" font-size="10" class="fig-muted">优先级仲裁 + 中断识别</text>
    <text x="312" y="144" text-anchor="middle" font-size="10" class="fig-muted">8259A（可级联）</text>
    <rect x="528" y="60" width="136" height="108" rx="5" class="fig-box"/>
    <text x="596" y="119" text-anchor="middle" class="fig-title">CPU</text>
    <text x="460" y="76" text-anchor="middle" font-size="11" class="fig-red">INTR</text>
    <text x="460" y="108" text-anchor="middle" font-size="11" class="fig-green">INTA</text>
    <text x="460" y="140" text-anchor="middle" font-size="10" class="fig-blue">中断号（数据总线）</text>
    <text x="350" y="224" text-anchor="middle" font-size="11" class="fig-muted">控制器承担优先级仲裁与中断识别，CPU 只负责响应与处理</text>
  </svg>
  <figcaption>图 8.10：中断控制器方式（独立请求汇聚与向量号经数据总线送 CPU）</figcaption>
</figure>

<strong>8259A 的内部结构</strong>：IRR（中断请求寄存器）锁存外部请求，某条请求线有效则对应位置 1；ISR（在服务寄存器）记录 CPU 当前正在响应的中断，对应位置 1，用于防止被同级或低级中断打断，并由 EOI 命令清除；PR（优先级仲裁器）同时比较 IRR 中未被屏蔽的请求与 ISR 中正在服务的请求，裁决出最高优先级者；IMR（中断屏蔽寄存器）可按位单独屏蔽或开放请求线，被屏蔽的请求不参与优先级比较；控制逻辑依据 CPU 发来的初始化命令字 ICW 和操作命令字 OCW 协调全片工作；数据总线缓冲器是芯片与系统数据总线的双向接口，用于写入命令字、读出状态（IRR/ISR/IMR）；读写逻辑接收 CPU 的读/写控制信号。芯片通过 CS 片选和地址线 A0 精确寻址。

<figure class="cpd-figure not-content" id="hco-w7-fig-11">
  <svg viewBox="0 0 700 340" role="img" aria-label="8259A 可编程中断控制器内部结构">
    <defs>
      <marker id="hco-w7-arr-11" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="56" y1="40" x2="156" y2="40" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="56" y1="62" x2="156" y2="62" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="56" y1="84" x2="156" y2="84" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="56" y1="146" x2="156" y2="146" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="258" y1="90" x2="294" y2="90" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <polyline points="258,203 336,203 336,134" fill="none" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="414" y1="62" x2="378" y2="88" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="376" y1="108" x2="412" y2="148" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="464" y1="126" x2="464" y2="94" class="fig-line" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="440" y1="206" x2="440" y2="250" class="fig-line"/>
    <line x1="258" y1="272" x2="288" y2="272" class="fig-line"/>
    <polyline points="454,272 618,272 618,224" fill="none" stroke="var(--sl-color-blue)" stroke-width="1.4" marker-start="url(#hco-w7-arr-11)" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="514" y1="154" x2="564" y2="154" stroke="var(--sl-color-red)" fill="none" stroke-width="1.5" marker-end="url(#hco-w7-arr-11)"/>
    <line x1="566" y1="184" x2="516" y2="184" class="fig-line-green" marker-end="url(#hco-w7-arr-11)"/>
    <text x="26" y="44" font-size="10" class="fig-muted">IR0</text>
    <text x="26" y="66" font-size="10" class="fig-muted">IR1</text>
    <text x="26" y="88" font-size="10" class="fig-muted">IR2</text>
    <text x="72" y="122" font-size="11" class="fig-muted">…</text>
    <text x="26" y="150" font-size="10" class="fig-muted">IR7</text>
    <rect x="158" y="28" width="100" height="132" rx="4" class="fig-box-blue"/>
    <text x="208" y="82" text-anchor="middle" class="fig-title fig-blue">IRR</text>
    <text x="208" y="102" text-anchor="middle" font-size="10" class="fig-muted">请求锁存</text>
    <rect x="158" y="184" width="100" height="38" rx="4" class="fig-box"/>
    <text x="208" y="207" text-anchor="middle" class="fig-label">IMR</text>
    <text x="24" y="207" font-size="10" class="fig-muted">IMR 按位屏蔽请求</text>
    <rect x="296" y="70" width="80" height="62" rx="4" class="fig-box-red"/>
    <text x="336" y="96" text-anchor="middle" class="fig-title fig-red">PR</text>
    <text x="336" y="114" text-anchor="middle" font-size="10" class="fig-muted">优先级仲裁</text>
    <rect x="414" y="28" width="100" height="64" rx="4" class="fig-box-purple"/>
    <text x="464" y="54" text-anchor="middle" class="fig-title fig-purple">ISR</text>
    <text x="464" y="74" text-anchor="middle" font-size="10" class="fig-muted">正在服务</text>
    <rect x="414" y="126" width="100" height="80" rx="4" class="fig-box"/>
    <text x="464" y="160" text-anchor="middle" class="fig-label">控制逻辑</text>
    <text x="464" y="180" text-anchor="middle" font-size="10" class="fig-muted">ICW / OCW</text>
    <rect x="158" y="250" width="100" height="44" rx="4" class="fig-box-subtle"/>
    <text x="208" y="269" text-anchor="middle" font-size="11" class="fig-label">读写逻辑</text>
    <text x="208" y="286" text-anchor="middle" font-size="10" class="fig-muted">RD / WR / A0</text>
    <rect x="290" y="250" width="162" height="44" rx="4" class="fig-box-green"/>
    <text x="371" y="269" text-anchor="middle" font-size="11" class="fig-green">数据总线缓冲器</text>
    <text x="371" y="286" text-anchor="middle" font-size="10" class="fig-muted">命令字 / 中断号 / 状态</text>
    <rect x="566" y="76" width="104" height="146" rx="5" class="fig-box"/>
    <text x="618" y="153" text-anchor="middle" class="fig-title">CPU</text>
    <text x="540" y="146" text-anchor="middle" font-size="10" class="fig-red">INTR</text>
    <text x="540" y="176" text-anchor="middle" font-size="10" class="fig-green">INTA</text>
    <text x="530" y="264" text-anchor="middle" font-size="10" class="fig-blue">数据总线</text>
    <text x="350" y="324" text-anchor="middle" font-size="11" class="fig-muted">8259A 只辅助可屏蔽中断；NMI 直接接 CPU，内部异常由 CPU 内部逻辑处理</text>
  </svg>
  <figcaption>图 8.11：8259A 可编程中断控制器内部结构与 CPU 信号交互</figcaption>
</figure>

<strong>中断控制器方式的工作流程</strong>（前段为中断响应阶段，后段为中断服务阶段）：

1. CPU 取出一条指令并执行完，判断控制器是否送来 INTR、且中断允许标志 IF 是否为 1；不成立则继续执行下一条指令。
2. 某设备经中断请求线发出请求，控制器将其锁存，<strong>IRR 对应位置 1</strong>。
3. 若 IMR 对应位为 0（未被屏蔽），且优先级高于 ISR 中正在服务的中断，则 PR 裁决胜出，控制逻辑向 CPU 发 <strong>INTR</strong>。
4. CPU 受理：<strong>关中断、保存断点</strong>。
5. 第一个中断应答脉冲 <strong>INTA</strong>：控制器清除该中断请求、清该源触发器的使能位（暂不再接收同类请求），并把 <strong>ISR 对应位置 1</strong>。
6. 第二个 INTA：控制器将<strong>中断号经数据总线</strong>送 CPU，完成中断识别；CPU 查中断向量表得到服务程序入口。
7. 执行中断服务程序；结束后向控制器发<strong>中断结束命令 EOI</strong>，ISR 对应位清零、恢复该源使能。
8. 中断返回，回到断点继续执行。

> <strong>注意</strong>：8259A 在现代 PC 与服务器中已基本不用；主流架构是芯片组中的 <strong>IOAPIC</strong>（接收外部设备中断）加集成在每个 CPU 核心内的 <strong>LAPIC</strong>（处理本核中断）。

### 8.9 DMA 方式

程序查询与中断驱动都要求 CPU 参与<strong>每一次</strong>数据传输：数据先从设备读到 CPU 寄存器，再从寄存器写入主存（或反向）。数据量大时（磁盘读写、网络收包），频繁中断与寄存器中转大量占用 CPU 时间。

<strong>DMA 的核心思想是绕过 CPU 直接交换数据：引入独立的 DMA 控制器（DMAC），传输时 DMAC 临时向 CPU 借用总线控制权，由 DMAC 直接控制数据在 I/O 设备与主存之间流动，不再经过 CPU 寄存器</strong>。

<strong>DMA 传输的五个步骤</strong>：

1. CPU 访问 DMAC 接口，设置传输参数（主存地址、数据块长度、传输方向等）；
2. CPU 向 I/O 设备的接口发送命令和参数，启动设备；
3. 传输过程中 CPU 继续执行其他程序；
4. 设备需要 DMA 操作时，由 DMAC 向 CPU 申请总线控制权，用于数据传输；
5. 一个数据块传输结束后，DMAC 通过<strong>中断</strong>请求 CPU 对数据缓冲区和 DMAC 做后续处理。

<strong>DMAC 的基本结构</strong>：

- <strong>地址寄存器</strong>：存放待交换数据的主存地址，预处理阶段由 CPU 设置，每传送一次自动递增，指向下一单元；
- <strong>字计数器</strong>：记录待交换数据块长度，预处理阶段由 CPU 设置，每传送一个字/字节自动减 1，减到 0 表示块结束，DMAC 发出 DMA 中断请求；
- <strong>数据缓冲寄存器</strong>：DMAC 作从设备（受 CPU 指令控制）时接收 CPU 送来的数据；作主设备（自主发起传输）时暂存传输数据，为设备与主存之间的流动提供缓冲，平衡双方速率差；
- <strong>命令寄存器 / 状态寄存器</strong>：接收 CPU 控制命令、向 CPU 反馈 DMAC 状态；
- <strong>DMA 控制逻辑</strong>：含控制电路、时序电路与状态标志，负责修改地址寄存器与字计数器、指定传输方向、协调并同步总线请求与总线响应、支持块结束时的中断。

> <strong>注意：DMAC 作为 I/O 设备接在系统总线上，既可作主设备也可作从设备</strong>。一个 DMAC 可支持多个通路（每路有独立的地址寄存器、字计数器），同时为多个设备服务；系统总线上也可挂多个 DMAC。设备与主存之间的数据都经“第三方”DMAC 中转时称为<strong>第三方 DMA</strong>，设备过多时它成为瓶颈；把 DMA 功能集成进 I/O 接口、为专属设备服务则称<strong>第一方 DMA</strong>，是现代总线主控技术的产物。

<figure class="cpd-figure not-content" id="hco-w7-fig-12">
  <svg viewBox="0 0 700 326" role="img" aria-label="DMA 控制器基本结构与信号连接">
    <defs>
      <marker id="hco-w7-arr-12" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto-start-reverse" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="176" y1="68" x2="122" y2="68" stroke="var(--sl-color-red)" fill="none" stroke-width="1.5" marker-end="url(#hco-w7-arr-12)"/>
    <line x1="120" y1="92" x2="174" y2="92" class="fig-line-green" marker-end="url(#hco-w7-arr-12)"/>
    <line x1="556" y1="110" x2="500" y2="110" class="fig-line" marker-end="url(#hco-w7-arr-12)"/>
    <line x1="498" y1="148" x2="554" y2="148" class="fig-line" marker-end="url(#hco-w7-arr-12)"/>
    <polyline points="500,210 612,210 612,178" fill="none" stroke="var(--sl-color-blue)" stroke-width="1.5" marker-start="url(#hco-w7-arr-12)" marker-end="url(#hco-w7-arr-12)"/>
    <rect x="24" y="42" width="96" height="76" rx="5" class="fig-box-blue"/>
    <text x="72" y="85" text-anchor="middle" class="fig-title fig-blue">CPU</text>
    <text x="148" y="60" text-anchor="middle" font-size="10" class="fig-red">HOLD</text>
    <text x="148" y="108" text-anchor="middle" font-size="10" class="fig-green">HLDA</text>
    <rect x="176" y="26" width="322" height="232" rx="6" class="fig-box"/>
    <text x="337" y="48" text-anchor="middle" class="fig-title">DMA 控制器（DMAC）</text>
    <rect x="194" y="62" width="136" height="36" rx="4" class="fig-box"/>
    <text x="262" y="84" text-anchor="middle" font-size="11" class="fig-label">地址寄存器</text>
    <rect x="194" y="108" width="136" height="36" rx="4" class="fig-box"/>
    <text x="262" y="130" text-anchor="middle" font-size="11" class="fig-label">字计数器</text>
    <rect x="194" y="154" width="136" height="36" rx="4" class="fig-box"/>
    <text x="262" y="176" text-anchor="middle" font-size="11" class="fig-label">数据缓冲寄存器</text>
    <rect x="348" y="62" width="134" height="36" rx="4" class="fig-box"/>
    <text x="415" y="84" text-anchor="middle" font-size="11" class="fig-label">命令寄存器</text>
    <rect x="348" y="108" width="134" height="36" rx="4" class="fig-box"/>
    <text x="415" y="130" text-anchor="middle" font-size="11" class="fig-label">状态寄存器</text>
    <rect x="348" y="156" width="134" height="86" rx="4" class="fig-box-green"/>
    <text x="415" y="186" text-anchor="middle" font-size="11" class="fig-green">DMA</text>
    <text x="415" y="204" text-anchor="middle" font-size="11" class="fig-green">控制逻辑</text>
    <text x="415" y="226" text-anchor="middle" font-size="9" class="fig-muted">地址/计数修改、方向、时序</text>
    <rect x="556" y="88" width="118" height="88" rx="5" class="fig-box"/>
    <text x="615" y="126" text-anchor="middle" class="fig-title">I/O 设备</text>
    <text x="615" y="148" text-anchor="middle" font-size="10" class="fig-muted">磁盘 / 网卡</text>
    <text x="528" y="102" text-anchor="middle" font-size="10" class="fig-muted">DREQ</text>
    <text x="528" y="140" text-anchor="middle" font-size="10" class="fig-muted">DACK</text>
    <text x="555" y="226" text-anchor="middle" font-size="10" class="fig-blue">数据总线</text>
    <text x="350" y="288" text-anchor="middle" font-size="11" class="fig-muted">作从设备：CPU 经总线配置参数；作主设备：接管总线直接控制传送</text>
    <text x="350" y="308" text-anchor="middle" font-size="11" class="fig-muted">地址寄存器自动递增、字计数器自动递减，减到 0 向 CPU 发 DMA 中断</text>
  </svg>
  <figcaption>图 8.12：DMA 控制器（DMAC）基本结构与总线/设备握手信号</figcaption>
</figure>

<strong>DMA 读操作流程</strong>（读取设备数据写入主存）分三个阶段：

- <strong>预处理阶段</strong>（CPU 执行程序完成）：CPU 初始化 DMAC，把主存地址、块长度、传输方向经系统总线送入 DMAC 的接口（此时 CPU 是主设备、DMAC 是从设备）；CPU 再向 I/O 设备接口发送 DMA 读命令与参数（设备地址、块长、方向），启动设备；随后挂起当前进程，调度其他进程运行。
- <strong>传输阶段</strong>（无需 CPU 参与）：设备备好数据后经 <strong>DREQ</strong> 向 DMAC 发请求；DMAC 经 <strong>HOLD</strong> 向 CPU 申请总线控制权；CPU 的总线控制器仲裁，在<strong>当前机器周期结束后</strong>经 <strong>HLDA</strong> 响应；DMAC 获得总线后，把主存地址送上地址总线、把主存写控制信号送上控制总线，经 <strong>DACK</strong> 通知设备；设备把一个机器字送上数据总线，在写信号控制下写入主存。此后设备准备下一个数据、再次发 DREQ，重复上述过程，DMAC 同步维护地址寄存器递增与字计数器递减。
- <strong>结束阶段</strong>：整个数据块传完（计数为 0），DMAC 经 <strong>INTR</strong> 向 CPU 发 DMA 中断请求；CPU 执行 DMA 中断服务程序，查询 DMAC 接口状态判断传输是否正常：正常则唤醒之前挂起的进程，出错则执行错误诊断与处理。

<figure class="cpd-figure not-content" id="hco-w7-fig-13">
  <svg viewBox="0 0 700 345" role="img" aria-label="DMA 读操作单字传送信号时序图">
    <defs>
      <marker id="hco-w7-arr-13" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <line x1="180" y1="22" x2="180" y2="298" class="fig-line-dashed"/>
    <line x1="255" y1="22" x2="255" y2="298" class="fig-line-dashed"/>
    <line x1="372" y1="22" x2="372" y2="298" class="fig-line-dashed"/>
    <line x1="82" y1="298" x2="670" y2="298" class="fig-line" marker-end="url(#hco-w7-arr-13)"/>
    <text x="24" y="47" font-size="11" class="fig-label">DREQ</text>
    <text x="24" y="87" font-size="11" class="fig-label">HOLD</text>
    <text x="24" y="127" font-size="11" class="fig-label">HLDA</text>
    <text x="24" y="167" font-size="11" class="fig-label">地址</text>
    <text x="24" y="207" font-size="11" class="fig-label">MEMW</text>
    <text x="24" y="247" font-size="11" class="fig-label">DACK</text>
    <text x="24" y="283" font-size="11" class="fig-label">数据</text>
    <polyline points="90,54 180,54 180,38 270,38 270,54 372,54" fill="none" stroke="var(--sl-color-blue)" stroke-width="1.6"/>
    <polyline points="90,94 210,94 210,78 352,78 352,94 372,94" fill="none" stroke="var(--sl-color-red)" stroke-width="1.6"/>
    <polyline points="90,134 242,134 242,118 364,118 364,134 372,134" fill="none" stroke="var(--sl-color-green)" stroke-width="1.6"/>
    <rect x="262" y="148" width="76" height="22" rx="3" class="fig-box"/>
    <text x="300" y="163" text-anchor="middle" font-size="10" class="fig-label">主存地址</text>
    <polyline points="90,214 262,214 262,198 338,198 338,214 372,214" class="fig-line"/>
    <polyline points="90,254 255,254 255,238 342,238 342,254 372,254" fill="none" stroke="var(--sl-color-red)" stroke-width="1.6"/>
    <rect x="270" y="266" width="62" height="22" rx="3" class="fig-box-blue"/>
    <text x="301" y="281" text-anchor="middle" font-size="10" class="fig-blue">机器字</text>
    <rect x="405" y="144" width="210" height="44" rx="5" class="fig-box-dashed"/>
    <text x="510" y="170" text-anchor="middle" font-size="11" class="fig-muted">重复上述过程直至字计数器为 0</text>
    <text x="135" y="316" text-anchor="middle" font-size="10" class="fig-muted">设备准备</text>
    <text x="218" y="316" text-anchor="middle" font-size="10" class="fig-muted">总线仲裁</text>
    <text x="310" y="316" text-anchor="middle" font-size="10" class="fig-muted">传送一个字</text>
    <text x="510" y="316" text-anchor="middle" font-size="10" class="fig-muted">块传输循环</text>
    <text x="668" y="316" text-anchor="end" font-size="10" class="fig-muted">时间</text>
    <text x="350" y="336" text-anchor="middle" font-size="11" class="fig-muted">每次仅占用一个存储周期：周期挪用方式</text>
  </svg>
  <figcaption>图 8.13：DMA 读操作（设备 → 主存）单字传送信号时序图</figcaption>
</figure>

<strong>DMAC 与 CPU 争用存储器的三种解决方式</strong>：

| 方式 | 做法 | 优点 | 缺点 / 适用 |
|---|---|---|---|
| DMA 独占访问 | DMAC 请求总线并要 CPU 暂停访存，整批数据传完后才归还总线 | 控制简单 | 设备与主存速度差异大时存储效率无法发挥，CPU 可能长时间不能访存；适用于两者速度相近、或 CPU 在 DMA 周期不访存的应用 |
| 交替访问 | 把存储周期划分为两个时隙，一个给 CPU、一个给 DMAC，各在其时隙访存 | 无访存冲突、确定性高，DMAC 无需复杂的申请/仲裁/释放逻辑 | 某方无访存需求时该时隙被闲置；适用于实时性、可预测性要求高且负载固定的嵌入式或专用硬件 |
| 周期挪用 | 只在需要传送的那个时刻挪用<strong>一个存储周期</strong>传送一个数据，随后交还总线 | 动态按需，不长时间独占；不需保存/恢复现场，切换效率高 | 若 CPU 同时访存则 DMA 优先，CPU 当前访存延迟一个周期；是实际应用中最主流的方式 |

> <strong>★考点</strong>：周期挪用中发生访存冲突时 <strong>DMA 控制器优先</strong>，CPU 的访存被延迟一个周期，而不是 DMA 让路。此外 DMA 传送不修改 CPU 寄存器与程序状态，故没有传统中断保存/恢复现场的开销。

<figure class="cpd-figure not-content" id="hco-w7-fig-14">
  <svg viewBox="0 0 700 250" role="img" aria-label="DMAC 与 CPU 争用存储器的三种方式对比">
    <text x="26" y="36" class="fig-label fig-muted">1. 独占访问</text>
    <rect x="136" y="20" width="90" height="24" rx="3" class="fig-box-blue"/>
    <text x="181" y="36" text-anchor="middle" font-size="10" class="fig-blue">CPU 访存</text>
    <rect x="226" y="20" width="260" height="24" rx="3" class="fig-box-red"/>
    <text x="356" y="36" text-anchor="middle" font-size="10" class="fig-red">DMAC 独占总线，CPU 暂停访存</text>
    <rect x="486" y="20" width="74" height="24" rx="3" class="fig-box-blue"/>
    <text x="523" y="36" text-anchor="middle" font-size="10" class="fig-blue">CPU</text>
    <text x="26" y="96" class="fig-label fig-muted">2. 交替访问</text>
    <rect x="136" y="80" width="26" height="22" rx="2" class="fig-box-blue"/>
    <rect x="162" y="80" width="26" height="22" rx="2" class="fig-box-red"/>
    <rect x="188" y="80" width="26" height="22" rx="2" class="fig-box-blue"/>
    <rect x="214" y="80" width="26" height="22" rx="2" class="fig-box-red"/>
    <rect x="240" y="80" width="26" height="22" rx="2" class="fig-box-blue"/>
    <rect x="266" y="80" width="26" height="22" rx="2" class="fig-box-red"/>
    <rect x="292" y="80" width="26" height="22" rx="2" class="fig-box-blue"/>
    <rect x="318" y="80" width="26" height="22" rx="2" class="fig-box-red"/>
    <rect x="344" y="80" width="26" height="22" rx="2" class="fig-box-blue"/>
    <rect x="370" y="80" width="26" height="22" rx="2" class="fig-box-red"/>
    <text x="416" y="95" font-size="10" class="fig-muted">蓝=CPU 时隙，红=DMA 时隙（无冲突但可能闲置）</text>
    <text x="26" y="156" class="fig-label fig-muted">3. 周期挪用</text>
    <rect x="136" y="140" width="52" height="22" rx="3" class="fig-box-blue"/>
    <text x="162" y="155" text-anchor="middle" font-size="10" class="fig-blue">CPU</text>
    <rect x="188" y="140" width="32" height="22" rx="3" class="fig-box-red"/>
    <text x="204" y="155" text-anchor="middle" font-size="10" class="fig-red">DMA</text>
    <rect x="220" y="140" width="64" height="22" rx="3" class="fig-box-blue"/>
    <text x="252" y="155" text-anchor="middle" font-size="10" class="fig-blue">CPU</text>
    <rect x="284" y="140" width="32" height="22" rx="3" class="fig-box-red"/>
    <text x="300" y="155" text-anchor="middle" font-size="10" class="fig-red">DMA</text>
    <rect x="316" y="140" width="96" height="22" rx="3" class="fig-box-blue"/>
    <text x="364" y="155" text-anchor="middle" font-size="10" class="fig-blue">CPU 继续执行</text>
    <text x="426" y="155" font-size="10" class="fig-muted">按需挪用单周期，冲突时 DMA 优先</text>
    <text x="136" y="204" font-size="11" class="fig-muted">• 周期挪用是实际系统中最主流的 DMA 传送方式</text>
    <text x="136" y="226" font-size="11" class="fig-muted">• DMA 不修改 CPU 寄存器与程序状态，无需保存 / 恢复现场</text>
  </svg>
  <figcaption>图 8.14：DMAC 与 CPU 争用存储器的三种方式（独占访问 / 交替访问 / 周期挪用）</figcaption>
</figure>

<strong>中断驱动与 DMA 的对比</strong>：

| 项目 | 中断驱动 | DMA |
|---|---|---|
| 传送单位 | 字 / 字节 | 数据块（成批） |
| 传送控制者 | CPU（中断服务程序） | DMA 控制器 |
| CPU 参与 | 每传送一个数据都要响应中断 | 仅预处理与结束阶段 |
| 数据通路 | 设备 → 接口 DBR → CPU 寄存器 → 主存 | 设备 ↔ 主存（经总线，不过 CPU 寄存器） |
| 适用 | 低速、少量数据 | 高速、大批量数据 |

<figure class="cpd-figure not-content" id="hco-w7-fig-15">
  <svg viewBox="0 0 700 260" role="img" aria-label="程序查询、中断驱动与 DMA 三种 I/O 方式的数据通路对比">
    <defs>
      <marker id="hco-w7-arr-15" markerWidth="10" markerHeight="10" refX="8" refY="5" orient="auto" markerUnits="userSpaceOnUse">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="16" y="34" class="fig-title fig-muted">1. 程序查询</text>
    <text x="16" y="52" font-size="10" class="fig-muted">（全程轮询）</text>
    <line x1="226" y1="42" x2="298" y2="42" class="fig-line" marker-end="url(#hco-w7-arr-15)"/>
    <line x1="412" y1="42" x2="484" y2="42" class="fig-line" marker-end="url(#hco-w7-arr-15)"/>
    <rect x="130" y="22" width="96" height="40" rx="4" class="fig-box"/>
    <text x="178" y="46" text-anchor="middle" font-size="11" class="fig-label">I/O 接口 DBR</text>
    <rect x="300" y="22" width="112" height="40" rx="4" class="fig-box-red"/>
    <text x="356" y="46" text-anchor="middle" font-size="11" class="fig-red">CPU 通用寄存器</text>
    <rect x="486" y="22" width="96" height="40" rx="4" class="fig-box"/>
    <text x="534" y="46" text-anchor="middle" font-size="11" class="fig-label">主存</text>
    <text x="636" y="46" text-anchor="middle" font-size="10" class="fig-red">串行、效率低</text>
    <text x="16" y="114" class="fig-title fig-blue">2. 中断驱动</text>
    <text x="16" y="132" font-size="10" class="fig-muted">（每字中断）</text>
    <line x1="226" y1="122" x2="298" y2="122" class="fig-line-accent" marker-end="url(#hco-w7-arr-15)"/>
    <line x1="412" y1="122" x2="484" y2="122" class="fig-line-accent" marker-end="url(#hco-w7-arr-15)"/>
    <rect x="130" y="102" width="96" height="40" rx="4" class="fig-box"/>
    <text x="178" y="126" text-anchor="middle" font-size="11" class="fig-label">I/O 接口 DBR</text>
    <rect x="300" y="102" width="112" height="40" rx="4" class="fig-box-blue"/>
    <text x="356" y="126" text-anchor="middle" font-size="11" class="fig-blue">CPU 通用寄存器</text>
    <rect x="486" y="102" width="96" height="40" rx="4" class="fig-box"/>
    <text x="534" y="126" text-anchor="middle" font-size="11" class="fig-label">主存</text>
    <text x="636" y="126" text-anchor="middle" font-size="10" class="fig-blue">适合低速设备</text>
    <text x="16" y="194" class="fig-title fig-green">3. DMA 方式</text>
    <text x="16" y="212" font-size="10" class="fig-muted">（成块直传）</text>
    <line x1="226" y1="202" x2="484" y2="202" class="fig-line-green" marker-end="url(#hco-w7-arr-15)"/>
    <rect x="130" y="182" width="96" height="40" rx="4" class="fig-box"/>
    <text x="178" y="206" text-anchor="middle" font-size="11" class="fig-label">I/O 设备</text>
    <rect x="304" y="170" width="104" height="26" rx="4" class="fig-box-green"/>
    <text x="356" y="187" text-anchor="middle" font-size="10" class="fig-green">DMAC 控总线</text>
    <rect x="486" y="182" width="96" height="40" rx="4" class="fig-box"/>
    <text x="534" y="206" text-anchor="middle" font-size="11" class="fig-label">主存</text>
    <text x="636" y="206" text-anchor="middle" font-size="10" class="fig-green">高速大批量</text>
    <text x="350" y="246" text-anchor="middle" font-size="11" class="fig-muted">本质区别：程序查询与中断均须经过 CPU 寄存器中转；DMA 绕过 CPU 直接在设备与主存间成块交换</text>
  </svg>
  <figcaption>图 8.15：三种基本 I/O 控制方式的数据通路与 CPU 参与度对比</figcaption>
</figure>

> <strong>★考点</strong>：中断方式并非“适用所有外部设备”。对硬盘、网卡等高速设备，频繁中断带来的两次上下文切换与中断服务开销使效率极低，应采用 DMA。

> <strong>★考点</strong>：中断方式下 CPU 时间占比可估算。设缓冲寄存器为 $n$ 位、设备传输率为 $R$（B/s）、每次中断开销为 $c$ 个时钟周期、CPU 主频为 $f$，每次中断传送 $B$ 字节、每秒中断 $k$ 次、CPU 时间占比为 $\eta$：

$$
B = \frac{n}{8}
$$

$$
k = \frac{R}{B}
$$

$$
\eta = \frac{c \times k}{f}
$$

例如 $n=32$、$R=50\text{KB/s}$、$c=1000$、$f=1\text{GHz}$：每次传 4 字节，每秒中断 12.5K 次，占比 $\eta = 1000 \times 12500 / 10^9 = 1.25\%$。若给定中断请求周期 $T$ 与每次响应处理时间 $t$，则：

$$
\eta = \frac{t}{T}
$$
