---
title: 【计算机组成原理】哈尔滨工业大学 | 刘宏伟
author: 萑澈
pubDatetime: 2026-11-09T08:29:46Z
featured: false
draft: false
printable: true
copyable: true
tags:
  - 计算机组成原理
  - cs408
  - 考研
  - 刘宏伟
description: 哈尔滨工业大学刘宏伟《计算机组成原理》全课程总结。视频：https://www.bilibili.com/video/BV1tun4z9E5z/。覆盖系统概论、总线、存储器、I/O、运算方法、指令、CPU、控制单元与微程序，含 117 张结构/时序 SVG
---



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

### 1.1 计算机系统简介

现代计算机系统由<strong>硬件</strong>和<strong>软件</strong>两大部分组成。

- <strong>硬件</strong>：计算机的实体，看得见摸得着——主机、外设、主板、主存、显卡、硬盘等。本课程研究硬件系统的<strong>逻辑实现</strong>：用数字逻辑、门电路、触发器等数字电子器件实现计算机硬件。
- <strong>软件</strong>：由具有各类特殊功能的信息组成，保存在计算机中，硬件通过<strong>一条条指令的有序执行</strong>来发挥作用。只有硬件没有软件，系统没有用途。

| 软件类别 | 包含内容 | 作用 |
|---|---|---|
| 系统软件 | 语言处理程序（编译、汇编）、操作系统、服务性程序（数学库、MPI）、数据库管理系统、网络软件 | 管理软硬件资源；提供人机界面；使多用户、多程序协调运行 |
| 应用软件 | 游戏、办公、各类专用业务程序 | 面向特定任务、特定目标 |

计算机系统具有明显的<strong>多态性</strong>，规模从一枚纽扣大小的传感器节点（温度、湿度、地磁），到 PDA、笔记本、PC、多路服务器，直至 HPC：天河二号 312 万个处理器核心，Linpack 实测 33.86 PFLOPS、理论峰值约 54.9 PFLOPS；泰坦 18688 个计算节点、200 个机柜，CPU＋GPU 异构，20 PFLOPS，最高可扩展到 50 PFLOPS 以上。规模差别再大，顶层抽象都是“硬件＋软件”。

课程名称的三个关键词：

| 关键词 | 含义 |
|---|---|
| 计算机 | 历史上出现过机械式、模拟电子、数字电子计算机，今天几乎全是<strong>数字电子计算机</strong> |
| 组成（organization） | 计算机硬件系统的<strong>逻辑实现</strong>，不是物理实现 |
| 原理 | 不依托具体机型，讲授多种数字电子计算机实现的<strong>公共原理</strong> |

课程内容三部分：基本部件的结构和组织方式、基本运算的操作原理、基本部件与基本单元的设计思想；再加上“把各部件连接成能协调运行的整体”。

> <strong>考点</strong>：<strong>计算机组成回答“如何实现”（用数字电路实现体系结构规定的属性）；计算机系统结构（体系结构）</strong>回答“程序员看到什么”。

| | 计算机系统结构（体系结构） | 计算机组成 |
|---|---|---|
| 定义 | 程序员所看到的计算机属性、概念性结构与功能特性 | 硬件系统的逻辑实现 |
| 关注 | 指令集、寄存器组织、编址单位、数据表示（类型与长度）、有无乘法指令 | 乘法指令由什么电路实现、控制信号如何产生、控制器用硬布线还是微程序 |
| 类比 | 软硬件的交界面：哪些功能给软件、哪些给硬件 | 在给定分工下用数字电路做出来 |

<figure class="cpd-figure not-content" id="lhw-w1-fig-1">
  <svg viewBox="0 0 700 284" role="img" aria-label="计算机系统软硬件组成与计算机系统结构对比计算机组成的交界面示意图">
    <defs>
      <marker id="lhw-w1-arr1" 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="18" y="16" width="232" height="252" rx="8"/>
    <rect class="fig-box-subtle" x="266" y="16" width="416" height="252" rx="8"/>
    <line class="fig-line" x1="134" y1="90" x2="134" y2="112" marker-end="url(#lhw-w1-arr1)"/>
    <line class="fig-line-accent" x1="134" y1="158" x2="134" y2="188" marker-end="url(#lhw-w1-arr1)"/>
    <line class="fig-line-dashed" x1="28" y1="173" x2="672" y2="173"/>
    <line class="fig-line-accent" x1="474" y1="138" x2="474" y2="196" marker-end="url(#lhw-w1-arr1)"/>
    <rect class="fig-box-blue" x="34" y="48" width="200" height="42" rx="5"/>
    <rect class="fig-box-purple" x="34" y="112" width="200" height="46" rx="5"/>
    <rect class="fig-box-green" x="34" y="188" width="200" height="64" rx="5"/>
    <rect class="fig-box-accent" x="282" y="48" width="384" height="90" rx="6"/>
    <rect class="fig-box-orange" x="282" y="196" width="384" height="58" rx="6"/>
    <rect class="fig-box" x="394" y="162" width="160" height="22" rx="4"/>
    <text class="fig-title" x="34" y="36" font-size="13">计算机系统全景</text>
    <text class="fig-title" x="282" y="36" font-size="13">核心辨析：系统结构 vs 计算机组成（以乘法为例）</text>
    <text class="fig-blue" x="134" y="67" font-size="12" text-anchor="middle">应用软件（面向特定任务）</text>
    <text class="fig-muted" x="134" y="83" font-size="10.5" text-anchor="middle">科学计算 / 办公 / 业务程序</text>
    <text class="fig-purple" x="134" y="132" font-size="12" text-anchor="middle">系统软件（资源管理与翻译）</text>
    <text class="fig-muted" x="134" y="149" font-size="10.5" text-anchor="middle">OS · 编译/汇编 · DBMS · 网络</text>
    <text class="fig-green" x="134" y="209" font-size="12" text-anchor="middle">硬件实体（逻辑实现层）</text>
    <text class="fig-muted" x="134" y="226" font-size="10.5" text-anchor="middle">CPU · 主存 · 总线 · I/O 外设</text>
    <text class="fig-muted" x="134" y="242" font-size="10.5" text-anchor="middle">门电路 · 触发器 · 寄存器通路</text>
    <text class="fig-accent" x="296" y="68" font-size="12">计算机系统结构（Architecture）：程序员看到什么（What）</text>
    <text class="fig-label" x="296" y="88" font-size="11">· 指令集与寻址：机器到底有没有一条专用的 MUL 乘法指令？</text>
    <text class="fig-label" x="296" y="106" font-size="11">· 数据表示与编址：支持定点数/浮点数？按字节还是按字编址？</text>
    <text class="fig-muted" x="296" y="124" font-size="10.5">· 程序员可见寄存器组（通用寄存器 / PC / 状态字）</text>
    <text class="fig-accent" x="474" y="177" font-size="11" text-anchor="middle">软硬件交界面（ISA 契约）</text>
    <text class="fig-orange" x="296" y="216" font-size="12">计算机组成（Organization）：硬件逻辑如何实现（How）</text>
    <text class="fig-label" x="292" y="234" font-size="10.2">· MUL 指令是用高速阵列乘法器，还是用 ALU“加法 + 移位”复用实现？</text>
    <text class="fig-muted" x="296" y="248" font-size="10.5">· 控制信号由硬布线产生还是微程序产生？（对上层程序员透明）</text>
  </svg>
  <figcaption>图 1-1　计算机系统的软硬件层次与“系统结构（有无该属性）vs 计算机组成（如何用逻辑电路实现）”辨析</figcaption>
</figure>

### 1.2 层次结构与抽象

<strong>抽象</strong>：有目的地隐藏一个过程或制品的某些细节，以便把其他方面或结构表达得更清楚。对同一系统可以从不同角度抽象。

按<strong>物理构成</strong>，计算机可抽象为九层，每一层以上一层的典型构建模块为基础：

| 层次 | 构建模块 |
|---|---|
| 物理层 | 电子设备，靠电子移动工作，用量子力学、麦克斯韦方程描述 |
| 器件层 | 利用电子在不同材料中的移动特征构成电子元件，用端点电流、电压描述 |
| 模拟电路层 | 放大器、滤波器等模拟部件 |
| 数字电路层 | 与门、非门等门电路 |
| 逻辑层 | 数据通路与控制 |
| 微体系结构层 | 数据通路与控制的具体组织 |

再往上是指令集体系结构、操作系统（虚拟机）、应用软件等更高抽象层。

本课程主要研究<strong>逻辑层</strong>和<strong>微体系结构层</strong>。

从<strong>程序员视角</strong>看，计算机是逐层“加壳”的虚拟机，共五层：

<figure class="cpd-figure not-content" id="lhw-w1-fig-2">
  <svg viewBox="0 0 700 326" role="img" aria-label="程序员视角五层虚拟机M4至M0与物理抽象层次及软硬件交界面示意图">
    <defs>
      <marker id="lhw-w1-arr2" 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="18" y="16" width="440" height="294" rx="8"/>
    <rect class="fig-box-subtle" x="470" y="16" width="212" height="294" rx="8"/>
    <line class="fig-line" x1="330" y1="80" x2="330" y2="98" marker-end="url(#lhw-w1-arr2)"/>
    <line class="fig-line" x1="330" y1="134" x2="330" y2="152" marker-end="url(#lhw-w1-arr2)"/>
    <line class="fig-line-accent" x1="330" y1="188" x2="330" y2="212" marker-end="url(#lhw-w1-arr2)"/>
    <line class="fig-line" x1="330" y1="248" x2="330" y2="266" marker-end="url(#lhw-w1-arr2)"/>
    <line class="fig-line-dashed" x1="26" y1="200" x2="450" y2="200"/>
    <rect class="fig-box-blue" x="92" y="44" width="222" height="36" rx="5"/>
    <rect class="fig-box-blue" x="92" y="98" width="222" height="36" rx="5"/>
    <rect class="fig-box-purple" x="92" y="152" width="222" height="36" rx="5"/>
    <rect class="fig-box-green" x="92" y="212" width="222" height="36" rx="5"/>
    <rect class="fig-box-orange" x="92" y="266" width="222" height="36" rx="5"/>
    <rect class="fig-box" x="28" y="190" width="60" height="20" rx="3"/>
    <rect class="fig-box" x="336" y="189" width="114" height="22" rx="3"/>
    <rect class="fig-box" x="482" y="46" width="188" height="68" rx="5"/>
    <rect class="fig-box-accent" x="482" y="124" width="188" height="68" rx="5"/>
    <rect class="fig-box" x="482" y="202" width="188" height="96" rx="5"/>
    <text class="fig-title" x="32" y="34" font-size="12.5">程序员视角：五层“加壳”机器（M4～M0）</text>
    <text class="fig-title" x="482" y="34" font-size="12.5">物理抽象栈与软硬件等价</text>
    <text class="fig-muted" x="56" y="66" font-size="11" text-anchor="middle">软件</text>
    <text class="fig-muted" x="56" y="120" font-size="11" text-anchor="middle">虚拟机</text>
    <text class="fig-accent" x="58" y="204" font-size="10.5" text-anchor="middle">ISA 界面</text>
    <text class="fig-muted" x="56" y="234" font-size="11" text-anchor="middle">实际</text>
    <text class="fig-muted" x="56" y="286" font-size="11" text-anchor="middle">硬件机</text>
    <text class="fig-blue" x="104" y="60" font-size="11.5">M4 高级语言机器</text>
    <text class="fig-muted" x="104" y="74" font-size="10">例：y = a * x + b</text>
    <text class="fig-blue" x="104" y="114" font-size="11.5">M3 汇编语言机器</text>
    <text class="fig-muted" x="104" y="128" font-size="10">例：LDA x / MUL a / ADD b</text>
    <text class="fig-purple" x="104" y="168" font-size="11.5">M2 操作系统机器</text>
    <text class="fig-muted" x="104" y="182" font-size="10">系统调用 / 存储与进程管理</text>
    <text class="fig-green" x="104" y="228" font-size="11.5">M1 实际机器（机器语言）</text>
    <text class="fig-muted" x="104" y="242" font-size="10">二进制码：000001 0000001000</text>
    <text class="fig-orange" x="104" y="282" font-size="11.5">M0 微程序机器（微指令）</text>
    <text class="fig-muted" x="104" y="296" font-size="10">硬件直接执行：PC→MAR, 1→R</text>
    <text class="fig-label" x="342" y="93" font-size="9.8">① 编译程序（翻译）</text>
    <text class="fig-label" x="342" y="147" font-size="9.8">② 汇编程序（翻译）</text>
    <text class="fig-accent" x="342" y="203" font-size="9.8">③ 系统调用 / 原语</text>
    <text class="fig-label" x="342" y="261" font-size="9.8">④ 微程序解释执行</text>
    <text class="fig-label" x="490" y="64" font-size="10.5">上层抽象：ISA / OS / 应用</text>
    <text class="fig-muted" x="490" y="82" font-size="9.5">底层物理：数字门电路 ← 模拟</text>
    <text class="fig-muted" x="490" y="98" font-size="9.5">← 器件层（晶体管）← 物理层</text>
    <text class="fig-accent" x="490" y="143" font-size="11">本课核心：逻辑层 + 微体系结构</text>
    <text class="fig-label" x="490" y="161" font-size="10">数据通路组织 + 控制信号产生</text>
    <text class="fig-muted" x="490" y="178" font-size="9.5">自顶向下细化 · 自底向上组装</text>
    <text class="fig-label" x="490" y="220" font-size="10.5">软硬件逻辑等价与权衡：</text>
    <text class="fig-green" x="490" y="239" font-size="10">· 硬件实现：速度快（单指令），</text>
    <text class="fig-muted" x="490" y="254" font-size="9.5">电路复杂、造价高、灵活性差</text>
    <text class="fig-blue" x="490" y="273" font-size="10">· 软件实现：造价低、易修改，</text>
    <text class="fig-muted" x="490" y="288" font-size="9.5">需多条指令组合，运行速度慢</text>
  </svg>
  <figcaption>图 1-2　五层多级计算机系统（M4～M2 软件虚拟机与 M1～M0 实际硬件机）及本课程在抽象栈中的定位</figcaption>
</figure>

- <strong>M0 微程序机器</strong>：硬件直接执行<strong>微指令</strong>；一条机器指令的微操作按先后关系拆到多条微指令中，一个<strong>微程序</strong>对应一条机器指令。
- <strong>M1 实际机器</strong>：机器语言（二进制代码）机器，用微程序解释机器指令。
- <strong>M2 操作系统机器</strong>：在机器语言层之上提供系统调用，管理软硬件资源。
- <strong>M3 汇编语言机器</strong>：由汇编程序把符号化的汇编语言翻译成机器语言（基本一一对应）。
- <strong>M4 高级语言机器</strong>：由编译程序（或解释程序）翻译成汇编语言/机器语言。

虚线是<strong>软硬件界面</strong>：虚线以下由硬件实现，以上由软件实现；它同时完成软硬件功能的划分，并提供上层软件与硬件交互的接口。同一功能用硬件或用软件实现<strong>逻辑上等价</strong>：

| 实现方式 | 代价 |
|---|---|
| 硬件实现 | 底层硬件复杂、造价高、设计周期长，但一条指令即可完成 |
| 软件实现 | 需多条指令组合，程序庞大、运行速度慢 |

<strong>应对系统复杂性的三种方法</strong>（贯穿全课程）：

1. <strong>层次化</strong>：把系统划分为模块/子模块，分别设计并可递归细化，使大问题变成小问题。本课程即<strong>自顶向下</strong>层层细化。
2. <strong>模块化</strong>：每个模块有明确定义的<strong>功能与接口</strong>，否则子模块无法组装成更大的模块。
3. <strong>规则性</strong>：接口与功能遵循工业标准/国际标准，使模块可复用。典型例子是内存条——只要接口标准化，任何厂家的内存都能插到机器上，并细化了产业分工。

设计过程是“自顶向下细化 + 自底向上组装”：细化到用简单逻辑电路就能实现，再逐级组装成整机。

### 1.3 冯·诺依曼结构与存储程序

现代计算机都是<strong>存储程序</strong>结构的计算机：程序和数据都以二进制形式存放在计算机中。这一结构由美籍匈牙利科学家<strong>冯·诺依曼</strong>提出，故称冯·诺依曼计算机，他本人被称为“现代计算机之父”。

来历：1944 年夏，冯·诺依曼在火车站偶遇弹道实验室负责人<strong>戈尔斯坦</strong>，得知 ENIAC 的研制情况后加入研制小组；1945 年小组发表<strong>存储程序通用电子计算机方案 EDVAC</strong>（Electronic Discrete Variable Automatic Computer），该报告成为计算机发展史上划时代的文献，明确了计算机由<strong>五大部分</strong>组成并描述了其职能与相互关系。1949 年研制出世界上第一台冯·诺依曼结构的机器 EDVAC。

> <strong>注意</strong>：按“第一台投入运行的存储程序计算机”这一说法，通常指 1949 年的 <strong>EDSAC</strong>；EDVAC 方案 1945 年发表，机器 1952 年才交付。ENIAC 靠接线与开关设置程序，本身没有存储程序。

<strong>五大部件</strong>：

| 部件 | 功能 |
|---|---|
| 运算器 | 完成算术运算和逻辑运算 |
| 控制器 | 指挥程序运行，控制指令的执行及指令间的先后顺序 |
| 存储器 | 存放程序和数据 |
| 输入设备 | 把人能识别的信息转换成机器能识别的形式 |
| 输出设备 | 把处理结果转换成人们熟悉的形式 |

<strong>冯·诺依曼机的六个特点</strong>：

1. 计算机由<strong>五大部件</strong>组成。
2. <strong>指令和数据以同等地位</strong>存放在存储器中，可按地址访问。
3. 指令和数据都用<strong>二进制</strong>表示。
4. <strong>指令由操作码和地址码两部分构成</strong>：操作码指明做什么操作，地址码指明操作数所在地址。
5. <strong>存储程序</strong>：程序存放在存储器中，机器自动按序执行。
6. <strong>以运算器为中心</strong>。

其中<strong>存储程序</strong>是核心特征，凡具此特征的机器都称冯·诺依曼计算机。

<figure class="cpd-figure not-content" id="lhw-w1-fig-3">
  <svg viewBox="0 0 700 298" role="img" aria-label="冯诺依曼计算机以运算器为中心的五大部件数据流与控制流框图">
    <defs>
      <marker id="lhw-w1-arr3" 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="18" y="16" width="464" height="244" rx="8"/>
    <rect class="fig-box-subtle" x="496" y="16" width="186" height="244" rx="8"/>
    <path class="fig-line-accent" d="M 134 106 L 160 106 L 160 146 L 184 146" marker-end="url(#lhw-w1-arr3)"/>
    <path class="fig-line-accent" d="M 184 166 L 160 166 L 160 206 L 134 206" marker-end="url(#lhw-w1-arr3)"/>
    <line class="fig-line-accent" x1="244" y1="88" x2="244" y2="128" marker-end="url(#lhw-w1-arr3)"/>
    <line class="fig-line-accent" x1="270" y1="128" x2="270" y2="88" marker-end="url(#lhw-w1-arr3)"/>
    <path class="fig-line-accent" d="M 338 62 L 418 62 L 418 128" marker-end="url(#lhw-w1-arr3)"/>
    <line class="fig-line-dashed" x1="364" y1="146" x2="328" y2="146" marker-end="url(#lhw-w1-arr3)"/>
    <line class="fig-line-dashed" x1="328" y1="168" x2="364" y2="168" marker-end="url(#lhw-w1-arr3)"/>
    <path class="fig-line-dashed" d="M 418 184 L 418 238 L 84 238 L 84 226" marker-end="url(#lhw-w1-arr3)"/>
    <rect class="fig-box-blue" x="176" y="40" width="162" height="48" rx="5"/>
    <rect class="fig-box-red" x="186" y="128" width="140" height="56" rx="6"/>
    <rect class="fig-box-purple" x="364" y="128" width="104" height="56" rx="5"/>
    <rect class="fig-box" x="34" y="84" width="100" height="42" rx="5"/>
    <rect class="fig-box" x="34" y="184" width="100" height="42" rx="5"/>
    <rect class="fig-box" x="348" y="48" width="60" height="20" rx="3"/>
    <rect class="fig-box" x="508" y="44" width="162" height="202" rx="5"/>
    <text class="fig-title" x="34" y="32" font-size="12.5">冯·诺依曼原始结构（IAS 机）：以运算器为中心</text>
    <text class="fig-title" x="508" y="34" font-size="12.5">冯·诺依曼六大特征</text>
    <text class="fig-blue" x="257" y="61" font-size="12.5" text-anchor="middle">存储器（主存）</text>
    <text class="fig-muted" x="257" y="78" font-size="10" text-anchor="middle">指令与数据同等地位存放</text>
    <text class="fig-red" x="256" y="152" font-size="12.5" text-anchor="middle">运算器（中心）</text>
    <text class="fig-muted" x="256" y="170" font-size="9.2" text-anchor="middle">中转全部 I/O → 系统瓶颈</text>
    <text class="fig-purple" x="416" y="152" font-size="12.5" text-anchor="middle">控制器</text>
    <text class="fig-muted" x="416" y="170" font-size="10" text-anchor="middle">指令译码与发命令</text>
    <text class="fig-label" x="84" y="109" font-size="12" text-anchor="middle">输入设备</text>
    <text class="fig-label" x="84" y="209" font-size="12" text-anchor="middle">输出设备</text>
    <text class="fig-muted" x="218" y="112" font-size="10">取数</text>
    <text class="fig-muted" x="278" y="112" font-size="10">存数</text>
    <text class="fig-purple" x="378" y="62" font-size="10" text-anchor="middle">取指令</text>
    <text class="fig-muted" x="341" y="140" font-size="9.5" text-anchor="middle">控制</text>
    <text class="fig-muted" x="341" y="182" font-size="9.5" text-anchor="middle">反馈</text>
    <text class="fig-accent" x="518" y="64" font-size="11">★ 核心：存储程序自动执行</text>
    <text class="fig-label" x="518" y="85" font-size="10.5">1. 五大部件组成硬件</text>
    <text class="fig-label" x="518" y="105" font-size="10.5">2. 指令与数据同存主存</text>
    <text class="fig-label" x="518" y="125" font-size="10.5">3. 均采用二进制编码</text>
    <text class="fig-label" x="518" y="145" font-size="10.5">4. 指令 = 操作码 + 地址码</text>
    <text class="fig-label" x="518" y="165" font-size="10.5">5. 按地址访问、顺序执行</text>
    <text class="fig-red" x="518" y="185" font-size="10.5">6. 以运算器为中心（缺陷）</text>
    <text class="fig-muted" x="518" y="212" font-size="10">里程碑：1945 EDVAC 方案</text>
    <text class="fig-muted" x="518" y="228" font-size="10">1949 EDSAC 首台投入运行</text>
    <text class="fig-muted" x="24" y="280" font-size="11">实线箭头：数据/指令传输通路　｜　虚线箭头：控制信号与状态反馈　｜　痛点：I/O 进出主存均须经运算器中转</text>
  </svg>
  <figcaption>图 1-3　冯·诺依曼计算机“以运算器为中心”的五大部件结构与六大核心特征</figcaption>
</figure>

> <strong>注意</strong>：六个特点都可以再讨论——指令与数据可否分存（今日指令 cache 与数据 cache 分离，甚至按内容查找）、可否用三进制/四进制、操作码可否兼指寻址方式与数据类型（IBM 360 的加法按基值和长度分为 8 种）、中心部件可否换成别的部件。

### 1.4 硬件框图：从以运算器为中心到以存储器为中心

冯·诺依曼原框图有两大问题：<strong>以运算器为中心，输入输出数据都必须经运算器中转，运算器成为瓶颈；图本身杂乱、不具层次化特征</strong>。改进分两步。

<strong>第一步：改为以存储器为中心。</strong> 输入设备、输出设备与存储器直接交换信息，运算器只负责运算，数据仍可与主存双向传送。

<figure class="cpd-figure not-content" id="lhw-w1-fig-4">
  <svg viewBox="0 0 700 282" role="img" aria-label="改进后以存储器为中心的五大部件数据流与控制流框图">
    <defs>
      <marker id="lhw-w1-arr4" 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="18" y="16" width="664" height="232" rx="8"/>
    <line class="fig-line-accent" x1="154" y1="84" x2="254" y2="84" marker-end="url(#lhw-w1-arr4)"/>
    <line class="fig-line-accent" x1="436" y1="84" x2="536" y2="84" marker-end="url(#lhw-w1-arr4)"/>
    <line class="fig-line-accent" x1="316" y1="116" x2="316" y2="174" marker-end="url(#lhw-w1-arr4)"/>
    <line class="fig-line-accent" x1="354" y1="174" x2="354" y2="116" marker-end="url(#lhw-w1-arr4)"/>
    <path class="fig-line" stroke="var(--sl-color-purple)" d="M 418 116 L 418 188 L 490 188" marker-end="url(#lhw-w1-arr4)"/>
    <line class="fig-line-dashed" x1="488" y1="210" x2="402" y2="210" marker-start="url(#lhw-w1-arr4)" marker-end="url(#lhw-w1-arr4)"/>
    <path class="fig-line-dashed" d="M 564 174 L 564 140 L 386 140 L 386 116" marker-end="url(#lhw-w1-arr4)"/>
    <path class="fig-line-dashed" d="M 94 156 L 94 110" marker-end="url(#lhw-w1-arr4)"/>
    <path class="fig-line-dashed" d="M 596 156 L 596 110" marker-end="url(#lhw-w1-arr4)"/>
    <line class="fig-line-dashed" x1="94" y1="156" x2="596" y2="156"/>
    <line class="fig-line-dashed" x1="536" y1="174" x2="536" y2="156"/>
    <rect class="fig-box" x="34" y="60" width="120" height="48" rx="5"/>
    <rect class="fig-box-accent" x="254" y="52" width="182" height="64" rx="6"/>
    <rect class="fig-box" x="536" y="60" width="120" height="48" rx="5"/>
    <rect class="fig-box-green" x="254" y="174" width="146" height="54" rx="5"/>
    <rect class="fig-box-purple" x="490" y="174" width="166" height="54" rx="5"/>
    <rect class="fig-box" x="172" y="73" width="66" height="20" rx="3"/>
    <rect class="fig-box" x="452" y="73" width="66" height="20" rx="3"/>
    <rect class="fig-box" x="426" y="177" width="54" height="20" rx="3"/>
    <text class="fig-title" x="34" y="36" font-size="12.5">第一步改进：改为“以存储器为中心”的数据交换结构</text>
    <text class="fig-label" x="94" y="88" font-size="12" text-anchor="middle">输入设备</text>
    <text class="fig-accent" x="345" y="79" font-size="13" text-anchor="middle">存储器（中心枢纽）</text>
    <text class="fig-muted" x="345" y="98" font-size="10.5" text-anchor="middle">直接与 I/O 及运算器交换数据</text>
    <text class="fig-label" x="596" y="88" font-size="12" text-anchor="middle">输出设备</text>
    <text class="fig-green" x="327" y="198" font-size="11" text-anchor="middle">运算器（专注算逻运算）</text>
    <text class="fig-muted" x="327" y="216" font-size="9.2" text-anchor="middle">释放中转负担，消除系统瓶颈</text>
    <text class="fig-purple" x="573" y="198" font-size="12.5" text-anchor="middle">控制器（全局指挥）</text>
    <text class="fig-muted" x="573" y="216" font-size="10" text-anchor="middle">取指译码 · 发出控制信号</text>
    <text class="fig-accent" x="205" y="87" font-size="10" text-anchor="middle">直写主存</text>
    <text class="fig-accent" x="485" y="87" font-size="10" text-anchor="middle">直读输出</text>
    <text class="fig-muted" x="292" y="148" font-size="10">取数</text>
    <text class="fig-muted" x="364" y="148" font-size="10">结果</text>
    <text class="fig-purple" x="453" y="191" font-size="10" text-anchor="middle">指令流</text>
    <text class="fig-muted" x="445" y="224" font-size="9.5" text-anchor="middle">控制 / 反馈</text>
    <text class="fig-muted" x="24" y="268" font-size="11">关键演进：输入/输出数据绕开运算器直通主存（后续演进为 DMA 与通道），运算器仅专注于算术逻辑运算</text>
  </svg>
  <figcaption>图 1-4　现代计算机第一步改进：以存储器为中心的五大部件数据流与控制流</figcaption>
</figure>

<strong>第二步：层次化。</strong> ALU 与 CU 合起来是 <strong>CPU</strong>；主存与 CPU 合起来是<strong>主机</strong>；输入设备、输出设备与辅存合起来是 <strong>I/O 设备</strong>；主机与 I/O 构成计算机硬件。

<figure class="cpd-figure not-content" id="lhw-w1-fig-5">
  <svg viewBox="0 0 700 296" role="img" aria-label="现代计算机硬件系统层次化框图：主机包含CPU与主存，IO包含输入输出与辅存">
    <defs>
      <marker id="lhw-w1-arr5" 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="18" y="16" width="664" height="248" rx="8"/>
    <rect class="fig-box" x="34" y="48" width="398" height="202" rx="6"/>
    <rect class="fig-box" x="472" y="48" width="194" height="202" rx="6"/>
    <rect class="fig-box-subtle" x="48" y="80" width="224" height="156" rx="5"/>
    <line class="fig-line-accent" x1="272" y1="138" x2="296" y2="138" marker-end="url(#lhw-w1-arr5)"/>
    <line class="fig-line-accent" x1="296" y1="168" x2="272" y2="168" marker-end="url(#lhw-w1-arr5)"/>
    <line class="fig-line-accent" x1="432" y1="138" x2="470" y2="138" marker-end="url(#lhw-w1-arr5)"/>
    <line class="fig-line-accent" x1="472" y1="176" x2="434" y2="176" marker-end="url(#lhw-w1-arr5)"/>
    <rect class="fig-box-blue" x="62" y="110" width="94" height="112" rx="5"/>
    <rect class="fig-box-purple" x="166" y="110" width="94" height="112" rx="5"/>
    <rect class="fig-box-green" x="298" y="80" width="124" height="156" rx="5"/>
    <rect class="fig-box" x="484" y="82" width="166" height="40" rx="4"/>
    <rect class="fig-box" x="484" y="134" width="166" height="40" rx="4"/>
    <rect class="fig-box-orange" x="484" y="186" width="166" height="50" rx="4"/>
    <text class="fig-title" x="34" y="36" font-size="13">计算机硬件系统（主机 + I/O 设备，通过系统总线互连）</text>
    <text class="fig-accent" x="48" y="68" font-size="12">主机（CPU + 主存）</text>
    <text class="fig-accent" x="484" y="68" font-size="12">I/O 设备（外部设备）</text>
    <text class="fig-label" x="62" y="99" font-size="11.5">CPU（中央处理器）</text>
    <text class="fig-blue" x="109" y="134" font-size="12" text-anchor="middle">ALU</text>
    <text class="fig-muted" x="109" y="152" font-size="10" text-anchor="middle">运算器</text>
    <text class="fig-muted" x="109" y="174" font-size="9.5" text-anchor="middle">ACC / MQ / X</text>
    <text class="fig-muted" x="109" y="192" font-size="9.5" text-anchor="middle">算术逻辑单元</text>
    <text class="fig-purple" x="213" y="134" font-size="12" text-anchor="middle">CU</text>
    <text class="fig-muted" x="213" y="152" font-size="10" text-anchor="middle">控制器</text>
    <text class="fig-muted" x="213" y="174" font-size="9.5" text-anchor="middle">PC / IR / 译码</text>
    <text class="fig-muted" x="213" y="192" font-size="9.5" text-anchor="middle">时序控制电路</text>
    <text class="fig-green" x="360" y="108" font-size="12.5" text-anchor="middle">主存（内存）</text>
    <text class="fig-muted" x="360" y="132" font-size="10" text-anchor="middle">存储体（按址访问）</text>
    <text class="fig-muted" x="360" y="152" font-size="10" text-anchor="middle">MAR（地址寄存器）</text>
    <text class="fig-muted" x="360" y="172" font-size="10" text-anchor="middle">MDR（数据寄存器）</text>
    <text class="fig-green" x="360" y="206" font-size="9.5" text-anchor="middle">CPU 可直接随机访问</text>
    <text class="fig-muted" x="452" y="160" font-size="9" text-anchor="middle">总线</text>
    <text class="fig-label" x="567" y="106" font-size="10" text-anchor="middle">输入设备（键盘/鼠标/传感器）</text>
    <text class="fig-label" x="567" y="158" font-size="10" text-anchor="middle">输出设备（显示器/打印机）</text>
    <text class="fig-orange" x="567" y="206" font-size="11.5" text-anchor="middle">辅存（磁盘 / SSD 外存）</text>
    <text class="fig-muted" x="567" y="224" font-size="9.5" text-anchor="middle">须先调入主存，CPU 才能执行</text>
    <text class="fig-muted" x="24" y="282" font-size="11">★ 408 必考归属：辅存属于 I/O（外设），不属于主机！现代芯片常将 MAR、MDR 物理集成在 CPU 内部（逻辑仍属主存）</text>
  </svg>
  <figcaption>图 1-5　现代计算机硬件系统的层次化模块化结构（主机 vs I/O 设备及辅存归属）</figcaption>
</figure>

| 层次 | 组成 |
|---|---|
| 计算机硬件 | 主机 + I/O 设备 |
| 主机 | CPU + 主存 |
| CPU | ALU（运算器）+ CU（控制器） |
| I/O | 输入设备 + 输出设备 + 辅存 |

> <strong>注意</strong>：ALU 为 Arithmetic Logic Unit，ACC 为 Accumulator，CU 为 Control Unit，PC 为 Program Counter，IR 为 Instruction Register，MAR/MDR 为 Memory Address/Data Register——缩写务必记准。

### 1.5 从问题到程序：指令格式与程序设计

用计算机解题的完整链条：<strong>可计算性判断 → 建立数学模型 → 确定计算方法 → 编写程序 → 指令序列</strong>。

- <strong>可计算性</strong>：函数定义域 $D$、值域 $R$，若存在算法对任意 $x \in D$ 都能算出 $f(x)$，则 $f$ 可计算。著名的不可计算问题是<strong>图灵机停机问题</strong>。
- <strong>数学模型</strong>：如正弦交流电实时电压，先写出电压随时间的函数关系；但指令集里并没有一条“求 $\sin$”的指令。
- <strong>计算方法</strong>：用级数展开算 $\sin x$、用迭代公式算 $\sqrt{x}$，直到相邻两次结果之差满足精度要求为止。
- <strong>程序</strong>：运算所需要的全部步骤；程序由指令构成，一条指令对应一个步骤。

<strong>指令格式（模型机）</strong>：指令字长 16 位 = 6 位操作码 + 10 位地址码。

<figure class="cpd-figure not-content" id="lhw-w1-fig-6">
  <svg viewBox="0 0 700 304" role="img" aria-label="模型机16位单地址指令字格式与数据字在主存中共存及区分机制示意图">
    <defs>
      <marker id="lhw-w1-arr6" 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="18" y="16" width="664" height="114" rx="8"/>
    <rect class="fig-box-subtle" x="18" y="142" width="664" height="146" rx="8"/>
    <rect class="fig-box-purple" x="78" y="54" width="204" height="44" rx="4"/>
    <rect class="fig-box-blue" x="282" y="54" width="340" height="44" rx="4"/>
    <line class="fig-divider" x1="112" y1="54" x2="112" y2="62"/>
    <line class="fig-divider" x1="146" y1="54" x2="146" y2="62"/>
    <line class="fig-divider" x1="180" y1="54" x2="180" y2="62"/>
    <line class="fig-divider" x1="214" y1="54" x2="214" y2="62"/>
    <line class="fig-divider" x1="248" y1="54" x2="248" y2="62"/>
    <line class="fig-divider" x1="316" y1="54" x2="316" y2="62"/>
    <line class="fig-divider" x1="350" y1="54" x2="350" y2="62"/>
    <line class="fig-divider" x1="384" y1="54" x2="384" y2="62"/>
    <line class="fig-divider" x1="418" y1="54" x2="418" y2="62"/>
    <line class="fig-divider" x1="452" y1="54" x2="452" y2="62"/>
    <line class="fig-divider" x1="486" y1="54" x2="486" y2="62"/>
    <line class="fig-divider" x1="520" y1="54" x2="520" y2="62"/>
    <line class="fig-divider" x1="554" y1="54" x2="554" y2="62"/>
    <line class="fig-divider" x1="588" y1="54" x2="588" y2="62"/>
    <line class="fig-line-accent" x1="330" y1="192" x2="404" y2="192" marker-end="url(#lhw-w1-arr6)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="330" y1="246" x2="404" y2="246" marker-end="url(#lhw-w1-arr6)"/>
    <rect class="fig-box-purple" x="34" y="172" width="296" height="42" rx="4"/>
    <rect class="fig-box-green" x="34" y="226" width="296" height="42" rx="4"/>
    <rect class="fig-box" x="404" y="170" width="262" height="46" rx="5"/>
    <rect class="fig-box" x="404" y="224" width="262" height="46" rx="5"/>
    <text class="fig-title" x="34" y="34" font-size="12.5">模型机 16 位单地址指令格式（隐含另一操作数与结果均在 ACC）</text>
    <text class="fig-muted" x="95" y="49" font-size="9.5" text-anchor="middle">15</text>
    <text class="fig-muted" x="265" y="49" font-size="9.5" text-anchor="middle">10</text>
    <text class="fig-muted" x="299" y="49" font-size="9.5" text-anchor="middle">9</text>
    <text class="fig-muted" x="605" y="49" font-size="9.5" text-anchor="middle">0</text>
    <text class="fig-purple" x="180" y="75" font-size="10.2" text-anchor="middle">操作码 OP（6 位）→ 2⁶ = 64 种操作</text>
    <text class="fig-muted" x="180" y="91" font-size="10" text-anchor="middle">例：000001（取数 LDA）</text>
    <text class="fig-blue" x="452" y="75" font-size="11" text-anchor="middle">地址码 A（10 位）→ 直接寻址 2¹⁰ = 1K 单元</text>
    <text class="fig-muted" x="452" y="91" font-size="10.5" text-anchor="middle">例：0000001000（主存地址 M = 8）</text>
    <text class="fig-muted" x="350" y="118" font-size="10.5" text-anchor="middle">机器码 0000010000001000B = 指令“将主存 8 号单元内容取至 ACC”</text>
    <text class="fig-title" x="34" y="160" font-size="12.5">主存中指令字与数据字同等地位共存（408 必考：CPU 如何区分二者？）</text>
    <text class="fig-purple" x="46" y="190" font-size="11">主存 000H（指令字）：000001 | 0000001000</text>
    <text class="fig-muted" x="46" y="206" font-size="10">二进制外观无标记，存放程序指令 LDA 8</text>
    <text class="fig-green" x="46" y="244" font-size="11">主存 008H（数据字）：0000010000001000</text>
    <text class="fig-muted" x="46" y="260" font-size="10">完全相同的 0/1 串，若作为补码整数则为 +1032</text>
    <text class="fig-accent" x="416" y="189" font-size="11">① 取指周期（时间） / PC 供址（空间）</text>
    <text class="fig-muted" x="416" y="206" font-size="10">读出流向 MDR → IR → CU 译码，视为【指令】</text>
    <text class="fig-green" x="416" y="243" font-size="11">② 执行周期（时间） / 地址码供址（空间）</text>
    <text class="fig-muted" x="416" y="260" font-size="10">读出流向 MDR → ALU/ACC 运算，视为【数据】</text>
  </svg>
  <figcaption>图 1-6　模型机 16 位单地址指令格式，以及主存中指令字与数据字的共存与时间/空间区分机制</figcaption>
</figure>

模型机指令集共六条，均为<strong>单地址</strong>指令：另一个操作数隐含在 <strong>ACC</strong> 中，结果也默认送 ACC。

| 指令 | 功能 | 地址码含义 |
|---|---|---|
| 取数 LDA | $M \to ACC$ | 源数据地址 |
| 存数 STA | $ACC \to M$ | 目的单元地址 |
| 加法 ADD | $ACC + M \to ACC$ | 加数地址 |
| 乘法 MUL | $ACC \times M \to ACC,MQ$ | 乘数地址 |
| 打印 PRT | $M \to$ 打印机 | 数据地址 |
| 停机 HLT | 停机 | 无 |

<strong>例：计算 $ax^2+bx+c$</strong>（$x,a,b,c$ 已存入主存，结果存入指定单元）。设机器为<strong>累加器型</strong>：参与运算的两个操作数一个在 ACC、一个在主存。

直接计算需 8 条指令：

| 步 | 指令 | 结果 |
|---|---|---|
| 1 | 取数 x | ACC = x |
| 2 | 乘法 x | ACC = x² |
| 3 | 乘法 a | ACC = ax² |
| 4 | 存数 T | M[T] = ax² |
| 5 | 取数 b | ACC = b |
| 6 | 乘法 x | ACC = bx |
| 7 | 加法 T | ACC = ax²+bx |
| 8 | 加法 c | ACC = ax²+bx+c |

改写为 $(ax+b)x+c$（秦九韶/Horner 形式）只需 5 条：

| 步 | 指令 | 结果 |
|---|---|---|
| 1 | 取数 x | ACC = x |
| 2 | 乘法 a | ACC = ax |
| 3 | 加法 b | ACC = ax+b |
| 4 | 乘法 x | ACC = (ax+b)x |
| 5 | 加法 c | ACC = (ax+b)x+c |

<figure class="cpd-figure not-content" id="lhw-w1-fig-7">
  <svg viewBox="0 0 700 286" role="img" aria-label="多项式求值直接法8条指令与秦九韶算法5条指令累加器执行轨迹对比图">
    <rect class="fig-box-subtle" x="18" y="16" width="322" height="254" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="16" width="326" height="254" rx="8"/>
    <rect class="fig-box" x="30" y="44" width="298" height="24" rx="3"/>
    <rect class="fig-box" x="30" y="72" width="298" height="24" rx="3"/>
    <rect class="fig-box" x="30" y="100" width="298" height="24" rx="3"/>
    <rect class="fig-box-red" x="30" y="128" width="298" height="26" rx="3"/>
    <rect class="fig-box" x="30" y="158" width="298" height="24" rx="3"/>
    <rect class="fig-box" x="30" y="186" width="298" height="24" rx="3"/>
    <rect class="fig-box-orange" x="30" y="214" width="298" height="24" rx="3"/>
    <rect class="fig-box" x="30" y="242" width="298" height="22" rx="3"/>
    <rect class="fig-box-green" x="370" y="48" width="298" height="28" rx="4"/>
    <rect class="fig-box-green" x="370" y="84" width="298" height="28" rx="4"/>
    <rect class="fig-box-green" x="370" y="120" width="298" height="28" rx="4"/>
    <rect class="fig-box-green" x="370" y="156" width="298" height="28" rx="4"/>
    <rect class="fig-box-accent" x="370" y="192" width="298" height="28" rx="4"/>
    <rect class="fig-box" x="370" y="228" width="298" height="34" rx="4"/>
    <text class="fig-title" x="30" y="35" font-size="12">直接求值 ax²+bx+c（8 条指令 + 临时存数）</text>
    <text class="fig-title" x="370" y="35" font-size="12">秦九韶改写 (ax+b)x+c（5 条指令，零写存）</text>
    <text class="fig-label" x="40" y="60" font-size="10.5">① LDA x   → ACC = x</text>
    <text class="fig-label" x="40" y="88" font-size="10.5">② MUL x   → ACC = x²（共第 1 次乘）</text>
    <text class="fig-label" x="40" y="116" font-size="10.5">③ MUL a   → ACC = ax²（共第 2 次乘）</text>
    <text class="fig-red" x="40" y="145" font-size="10.5">④ STA T   → M[T] = ax²（被迫写回主存暂存）</text>
    <text class="fig-label" x="40" y="174" font-size="10.5">⑤ LDA b   → ACC = b（覆盖累加器）</text>
    <text class="fig-label" x="40" y="202" font-size="10.5">⑥ MUL x   → ACC = bx（共第 3 次乘）</text>
    <text class="fig-orange" x="40" y="230" font-size="10.5">⑦ ADD T   → ACC = ax² + bx（再读 M[T]）</text>
    <text class="fig-label" x="40" y="257" font-size="10.5">⑧ ADD c   → ACC = ax² + bx + c</text>
    <text class="fig-green" x="382" y="66" font-size="11">① LDA x   → ACC = x</text>
    <text class="fig-green" x="382" y="102" font-size="11">② MUL a   → ACC = ax（第 1 次乘）</text>
    <text class="fig-green" x="382" y="138" font-size="11">③ ADD b   → ACC = ax + b（直接累加）</text>
    <text class="fig-green" x="382" y="174" font-size="11">④ MUL x   → ACC = (ax + b)x（第 2 次乘）</text>
    <text class="fig-accent" x="382" y="210" font-size="11">⑤ ADD c   → ACC = (ax + b)x + c（完成）</text>
    <text class="fig-accent" x="382" y="243" font-size="10.5">优化收益：减少 3 条指令（少 1 次耗时乘法</text>
    <text class="fig-muted" x="382" y="257" font-size="10">+ 免去临时单元 M[T] 的 1 写 1 读），Ic 与访存双降</text>
  </svg>
  <figcaption>图 1-7　累加器型模型机求解二次多项式：直接算法（8 步）与秦九韶算法（5 步）的指令数与访存开销对比</figcaption>
</figure>

> <strong>考点</strong>：同一问题，算法不同 → <strong>指令条数</strong>不同 → 执行时间不同、占用的存储空间不同。5 条指令的版本在同一台机器上更快、程序更短。

> <strong>注意</strong>：若寄存器与存储单元都是 8 位，两个 8 位数相乘得 16 位，8 位寄存器存不下乘积的低/高半部——这正是运算器要配 MQ、乘积用两个寄存器保存的原因，第六章讲运算器时解决。

### 1.6 存储器与运算器的基本结构

<strong>存储器</strong>的核心是<strong>存储体</strong>：由若干<strong>存储单元</strong>构成，每个存储单元由若干<strong>存储元件</strong>构成（每个元件存 1 位）。类比：存储体＝大楼，存储单元＝房间，存储元件＝床位。

| 术语 | 含义 |
|---|---|
| 存储字 | 一个存储单元中存放的二进制代码组合 |
| 存储字长 | 存储字的位数，即存储单元中二进制代码的位数 |
| 存储单元地址 | 每个存储单元的编号，<strong>按地址访问</strong> |
| MAR | 存储器地址寄存器，存放要访问单元的地址 |
| MDR | 存储器数据寄存器，存放读出或写入的数据 |

<figure class="cpd-figure not-content" id="lhw-w1-fig-8">
  <svg viewBox="0 0 700 296" role="img" aria-label="主存储器逻辑结构图：MAR地址译码选择存储体单元并与MDR读写交换数据">
    <defs>
      <marker id="lhw-w1-arr8" 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="18" y="16" width="664" height="248" rx="8"/>
    <line class="fig-line-accent" x1="138" y1="118" x2="174" y2="118" marker-end="url(#lhw-w1-arr8)"/>
    <line class="fig-line-accent" x1="252" y1="78" x2="286" y2="78" marker-end="url(#lhw-w1-arr8)"/>
    <line class="fig-line" x1="252" y1="108" x2="286" y2="108" marker-end="url(#lhw-w1-arr8)"/>
    <line class="fig-line" x1="252" y1="168" x2="286" y2="168" marker-end="url(#lhw-w1-arr8)"/>
    <line class="fig-line-accent" x1="486" y1="104" x2="536" y2="104" marker-end="url(#lhw-w1-arr8)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="536" y1="134" x2="486" y2="134" marker-end="url(#lhw-w1-arr8)"/>
    <line class="fig-line-dashed" x1="598" y1="196" x2="598" y2="156" marker-end="url(#lhw-w1-arr8)"/>
    <path class="fig-line-dashed" d="M 536 218 L 468 218 L 468 190" marker-end="url(#lhw-w1-arr8)"/>
    <rect class="fig-box-purple" x="34" y="88" width="104" height="60" rx="5"/>
    <rect class="fig-box" x="174" y="56" width="78" height="134" rx="5"/>
    <rect class="fig-box" x="286" y="56" width="200" height="134" rx="5"/>
    <rect class="fig-box-accent" x="286" y="64" width="200" height="28" rx="0"/>
    <line class="fig-divider" x1="286" y1="92" x2="486" y2="92"/>
    <line class="fig-divider" x1="286" y1="122" x2="486" y2="122"/>
    <line class="fig-divider" x1="286" y1="152" x2="486" y2="152"/>
    <line class="fig-divider" x1="336" y1="56" x2="336" y2="190"/>
    <line class="fig-divider" x1="386" y1="56" x2="386" y2="190"/>
    <line class="fig-divider" x1="436" y1="56" x2="436" y2="190"/>
    <rect class="fig-box-blue" x="536" y="88" width="126" height="68" rx="5"/>
    <rect class="fig-box-orange" x="536" y="196" width="126" height="44" rx="5"/>
    <rect class="fig-box" x="28" y="196" width="256" height="48" rx="5"/>
    <text class="fig-title" x="34" y="36" font-size="12.5">主存储器逻辑组成：MAR（n 位地址）+ 译码驱动 + 存储体（2ⁿ × m）+ MDR（m 位字长）</text>
    <text class="fig-purple" x="86" y="112" font-size="12.5" text-anchor="middle">MAR</text>
    <text class="fig-muted" x="86" y="130" font-size="10" text-anchor="middle">n 位地址寄存器</text>
    <text class="fig-muted" x="156" y="110" font-size="9.5" text-anchor="middle">n 位</text>
    <text class="fig-label" x="213" y="106" font-size="11" text-anchor="middle">地址</text>
    <text class="fig-label" x="213" y="124" font-size="11" text-anchor="middle">译码器</text>
    <text class="fig-muted" x="213" y="144" font-size="9.5" text-anchor="middle">2ⁿ 选 1</text>
    <text class="fig-accent" x="311" y="82" font-size="10" text-anchor="middle">1</text>
    <text class="fig-accent" x="361" y="82" font-size="10" text-anchor="middle">0</text>
    <text class="fig-accent" x="411" y="82" font-size="10" text-anchor="middle">…</text>
    <text class="fig-accent" x="461" y="82" font-size="10" text-anchor="middle">1</text>
    <text class="fig-muted" x="386" y="51" font-size="10" text-anchor="middle">← 存储字长 m 位（每格 1 个存储元件）→</text>
    <text class="fig-muted" x="278" y="73" font-size="9.5" text-anchor="end">0</text>
    <text class="fig-muted" x="278" y="103" font-size="9.5" text-anchor="end">1</text>
    <text class="fig-muted" x="278" y="138" font-size="9.5" text-anchor="end">…</text>
    <text class="fig-muted" x="280" y="163" font-size="8.5" text-anchor="end">2ⁿ−1</text>
    <text class="fig-label" x="386" y="206" font-size="11" text-anchor="middle">存储体（共 2ⁿ 个存储单元）</text>
    <text class="fig-accent" x="511" y="98" font-size="9.5" text-anchor="middle">读出</text>
    <text class="fig-green" x="511" y="148" font-size="9.5" text-anchor="middle">写入</text>
    <text class="fig-blue" x="599" y="114" font-size="12.5" text-anchor="middle">MDR</text>
    <text class="fig-muted" x="599" y="132" font-size="10" text-anchor="middle">m 位数据寄存器</text>
    <text class="fig-muted" x="599" y="147" font-size="9.5" text-anchor="middle">等于 1 个存储字长</text>
    <text class="fig-orange" x="599" y="215" font-size="11" text-anchor="middle">读写控制电路</text>
    <text class="fig-muted" x="599" y="231" font-size="9.5" text-anchor="middle">接收 CU 读/写命令</text>
    <text class="fig-accent" x="36" y="214" font-size="9.5">容量公式：C = 2ⁿ × m bit = (2ⁿ × m / 8) B</text>
    <text class="fig-muted" x="36" y="232" font-size="9.2">例：MAR=16位, MDR=32位 → 64K×32b = 256KB</text>
    <text class="fig-muted" x="24" y="282" font-size="11">类比记忆：存储体 ＝ 整栋大楼　｜　存储单元 ＝ 编号房间（共 2ⁿ 间）　｜　存储元件 ＝ 房间床位（每间 m 张床，存 0 或 1）</text>
  </svg>
  <figcaption>图 1-8　主存储器内部逻辑结构与容量计算（MAR 位数决定单元数 $2^n$，MDR 位数等于存储字长 $m$）</figcaption>
</figure>

<strong>MAR 的位数决定存储单元个数</strong>（$n$ 位地址 → $2^n$ 个单元）；<strong>MDR 的位数等于存储字长</strong>。

$$
C = 2^{n} \times m
$$

其中 $n$ 为 MAR 位数、$m$ 为 MDR 位数（存储字长），容量单位是“位”。

- 例：MAR 4 位、MDR 8 位 → 16 个单元、字长 8 → $16 \times 8$ 位。
- 例：MAR 10 位、MDR 8 位 → $2^{10}=1K$ 个单元 → 容量写作 <strong>1K×8</strong>，即 1KB。
- 例：MAR 16 位、MDR 32 位 → 64K×32 = <strong>256KB</strong>。

> <strong>注意</strong>：`1K×8` 的写法同时说明“有多少单元”和“每单元多少位”，比直接写字节数更清晰；而 `2^13 b = 1KB`、`2^21 b = 256KB` 是字节表示法。

<strong>运算器</strong>的核心是 <strong>ALU（算逻单元）</strong>，通常是<strong>组合电路</strong>——输入撤销则输出随之撤销，因此必须在输入端加寄存器锁存操作数，并保存结果。模型机设三个寄存器：<strong>ACC</strong>（累加器）、<strong>X</strong>（数据寄存器）、<strong>MQ</strong>（乘商寄存器）。

<figure class="cpd-figure not-content" id="lhw-w1-fig-9">
  <svg viewBox="0 0 700 298" role="img" aria-label="累加器型运算器内部结构图：ACC与X送组合电路ALU运算并配合MQ实现乘除法">
    <defs>
      <marker id="lhw-w1-arr9" 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="18" y="16" width="388" height="248" rx="8"/>
    <rect class="fig-box-subtle" x="420" y="16" width="262" height="248" rx="8"/>
    <line class="fig-line-accent" x1="194" y1="98" x2="194" y2="146" marker-end="url(#lhw-w1-arr9)"/>
    <line class="fig-line-accent" x1="226" y1="146" x2="226" y2="98" marker-end="url(#lhw-w1-arr9)"/>
    <line class="fig-line" x1="122" y1="182" x2="146" y2="182" marker-end="url(#lhw-w1-arr9)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="274" y1="170" x2="306" y2="170" marker-end="url(#lhw-w1-arr9)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="306" y1="194" x2="274" y2="194" marker-end="url(#lhw-w1-arr9)"/>
    <path class="fig-line-dashed" d="M 78 210 L 78 238 L 348 238 L 348 210" marker-end="url(#lhw-w1-arr9)"/>
    <rect class="fig-box-blue" x="146" y="48" width="130" height="50" rx="5"/>
    <rect class="fig-box-accent" x="148" y="146" width="126" height="68" rx="6"/>
    <rect class="fig-box" x="34" y="156" width="88" height="54" rx="5"/>
    <rect class="fig-box-purple" x="306" y="156" width="86" height="54" rx="5"/>
    <rect class="fig-box" x="136" y="226" width="150" height="22" rx="4"/>
    <rect class="fig-box" x="434" y="46" width="234" height="54" rx="5"/>
    <rect class="fig-box-green" x="434" y="112" width="234" height="64" rx="5"/>
    <rect class="fig-box-orange" x="434" y="188" width="234" height="62" rx="5"/>
    <text class="fig-title" x="34" y="36" font-size="12.5">累加器型运算器基本结构（锁存 + 组合逻辑）</text>
    <text class="fig-title" x="434" y="36" font-size="12.5">四则运算寄存器分工速查</text>
    <text class="fig-blue" x="211" y="70" font-size="12" text-anchor="middle">ACC（累加器）</text>
    <text class="fig-muted" x="211" y="88" font-size="9" text-anchor="middle">源操作数 1 / 运算结果</text>
    <text class="fig-accent" x="211" y="175" font-size="12.5" text-anchor="middle">ALU</text>
    <text class="fig-muted" x="211" y="194" font-size="9" text-anchor="middle">组合逻辑电路（无记忆）</text>
    <text class="fig-label" x="78" y="178" font-size="12.5" text-anchor="middle">X</text>
    <text class="fig-muted" x="78" y="196" font-size="9.5" text-anchor="middle">通用操作数缓冲</text>
    <text class="fig-purple" x="349" y="178" font-size="12.5" text-anchor="middle">MQ</text>
    <text class="fig-muted" x="349" y="196" font-size="9.5" text-anchor="middle">乘商寄存器</text>
    <text class="fig-muted" x="174" y="126" font-size="9.5">输入</text>
    <text class="fig-muted" x="234" y="126" font-size="9.5">写回</text>
    <text class="fig-muted" x="211" y="241" font-size="9" text-anchor="middle">主存 MDR ⇄ X / ACC / MQ</text>
    <text class="fig-label" x="444" y="64" font-size="11">加减法：(ACC) ± (X) → ACC</text>
    <text class="fig-muted" x="444" y="83" font-size="10">主存操作数先经 MDR 送入 X，MQ 闲置不用</text>
    <text class="fig-green" x="444" y="131" font-size="10.2">乘法：被乘数 X × 乘数 MQ（加 + 右移）</text>
    <text class="fig-label" x="444" y="149" font-size="10">初态 ACC 清 0 存部分积；终态双倍字长乘积</text>
    <text class="fig-muted" x="444" y="165" font-size="10">高位保存在 ACC，低位保存在 MQ</text>
    <text class="fig-orange" x="444" y="207" font-size="10.2">除法：被除数 ACC ÷ 除数 X（减 + 左移）</text>
    <text class="fig-label" x="444" y="225" font-size="10">每步上商移入 MQ 低位；终态：</text>
    <text class="fig-muted" x="444" y="241" font-size="10">商保存在 MQ，余数保存在 ACC</text>
    <text class="fig-muted" x="24" y="282" font-size="11">★ 为什么必须配寄存器？ALU 是纯组合电路，输入撤销则输出消失，故输入端须锁存操作数、输出端须锁存结果</text>
  </svg>
  <figcaption>图 1-9　累加器型运算器内部数据通路（ALU + ACC / X / MQ）及加减乘除四则运算寄存器角色分工</figcaption>
</figure>

| 运算 | ACC | X | MQ | 结果去向 |
|---|---|---|---|---|
| 加法 | 被加数 | 加数 | 不用 | 和 → ACC |
| 减法 | 被减数 | 减数 | 不用 | 差 → ACC |
| 乘法 | 部分积 / 积高位 | 被乘数 | 乘数 / 积低位 | 积 → ACC（高）+ MQ（低） |
| 除法 | 被除数 / 余数 | 除数 | 商 | 商 → MQ，余数 → ACC |

> <strong>注意</strong>：上述结构是<strong>累加器型模型机</strong>的约定；若设计通用寄存器型机器（多寄存器），寄存器的种类和数量都要相应改变。另外，模型机假设寄存器长度、存储字长彼此相等，这只是简化，并非所有机器如此。

### 1.7 控制器与指令的执行过程

控制器负责<strong>解释</strong>一条指令（取指 → 分析 → 取操作数 → 执行 → 保存结果），并保证指令<strong>按序执行</strong>。它由三部分组成：

| 部件 | 作用 |
|---|---|
| CU（控制单元） | 核心，发出各种控制信号 |
| IR（指令寄存器） | 存放当前正在执行的指令，OP 送 CU 译码 |
| PC（程序计数器） | 存放当前/下一条要执行指令的地址，具有计数功能（PC+1 → PC） |

> <strong>注意</strong>：PC 加“1”究竟加几取决于机器结构——按指令字长、按字节编址等因素决定，后续章节再定。

<figure class="cpd-figure not-content" id="lhw-w1-fig-10">
  <svg viewBox="0 0 700 326" role="img" aria-label="模型机整机数据通路与取数指令LDA完整取指分析执行七步微操作轨迹图">
    <defs>
      <marker id="lhw-w1-arr10" 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="18" y="16" width="664" height="252" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="126" y1="74" x2="176" y2="74" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="264" y1="74" x2="310" y2="74" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="434" y1="74" x2="486" y2="74" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="538" y1="100" x2="538" y2="134" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="498" y1="178" x2="498" y2="212" marker-end="url(#lhw-w1-arr10)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 594 178 L 594 204 L 220 204 L 220 100" marker-end="url(#lhw-w1-arr10)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 618 74 L 656 74 L 656 124 L 296 124 L 296 154" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line" x1="286" y1="198" x2="286" y2="216" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line" x1="156" y1="234" x2="242" y2="234" marker-end="url(#lhw-w1-arr10)"/>
    <line class="fig-line-dashed" x1="454" y1="236" x2="340" y2="236" marker-end="url(#lhw-w1-arr10)"/>
    <rect class="fig-box-blue" x="34" y="48" width="92" height="52" rx="5"/>
    <rect class="fig-box" x="176" y="48" width="88" height="52" rx="5"/>
    <rect class="fig-box" x="310" y="42" width="124" height="64" rx="5"/>
    <rect class="fig-box" x="486" y="48" width="132" height="52" rx="5"/>
    <rect class="fig-box-purple" x="454" y="134" width="88" height="44" rx="4"/>
    <rect class="fig-box-green" x="542" y="134" width="96" height="44" rx="4"/>
    <rect class="fig-box-purple" x="454" y="214" width="184" height="44" rx="5"/>
    <rect class="fig-box-green" x="242" y="154" width="98" height="44" rx="5"/>
    <rect class="fig-box" x="242" y="216" width="98" height="40" rx="5"/>
    <rect class="fig-box" x="72" y="214" width="84" height="42" rx="4"/>
    <rect class="fig-box" x="34" y="146" width="122" height="42" rx="4"/>
    <rect class="fig-box" x="344" y="195" width="98" height="18" rx="3"/>
    <rect class="fig-box" x="358" y="114" width="92" height="19" rx="3"/>
    <rect class="fig-box" x="516" y="106" width="44" height="16" rx="3"/>
    <rect class="fig-box" x="506" y="182" width="46" height="16" rx="3"/>
    <text class="fig-title" x="34" y="34" font-size="12.5">模型机整机通路：取数指令 LDA M 的取指（蓝）→ 分析（紫）→ 执行（绿）完整轨迹</text>
    <text class="fig-blue" x="80" y="70" font-size="12.5" text-anchor="middle">PC</text>
    <text class="fig-muted" x="80" y="88" font-size="9.5" text-anchor="middle">取指完 +1 → PC</text>
    <text class="fig-label" x="220" y="70" font-size="12.5" text-anchor="middle">MAR</text>
    <text class="fig-muted" x="220" y="88" font-size="9.5" text-anchor="middle">地址锁存</text>
    <text class="fig-label" x="372" y="68" font-size="12.5" text-anchor="middle">存储体 M</text>
    <text class="fig-muted" x="372" y="88" font-size="9.5" text-anchor="middle">②读指令 / ⑥读数据</text>
    <text class="fig-label" x="552" y="70" font-size="12.5" text-anchor="middle">MDR</text>
    <text class="fig-muted" x="552" y="88" font-size="9.5" text-anchor="middle">③锁存指令/数据</text>
    <text class="fig-purple" x="498" y="153" font-size="11.5" text-anchor="middle">IR(OP)</text>
    <text class="fig-muted" x="498" y="169" font-size="9.5" text-anchor="middle">操作码 6b</text>
    <text class="fig-green" x="590" y="153" font-size="11.5" text-anchor="middle">IR(AD)</text>
    <text class="fig-muted" x="590" y="169" font-size="9.5" text-anchor="middle">地址码 10b</text>
    <text class="fig-purple" x="546" y="233" font-size="12" text-anchor="middle">CU（控制单元）</text>
    <text class="fig-muted" x="546" y="249" font-size="9.5" text-anchor="middle">译码并发出各拍控制信号</text>
    <text class="fig-green" x="291" y="174" font-size="12.5" text-anchor="middle">ACC</text>
    <text class="fig-muted" x="291" y="190" font-size="9.5" text-anchor="middle">累加器得操作数</text>
    <text class="fig-label" x="291" y="240" font-size="12" text-anchor="middle">ALU</text>
    <text class="fig-label" x="114" y="232" font-size="11.5" text-anchor="middle">X</text>
    <text class="fig-muted" x="114" y="247" font-size="9.5" text-anchor="middle">数据寄存器</text>
    <text class="fig-label" x="95" y="165" font-size="11.5" text-anchor="middle">MQ（乘商寄存器）</text>
    <text class="fig-muted" x="95" y="180" font-size="9.5" text-anchor="middle">乘除法时投入工作</text>
    <text class="fig-blue" x="151" y="67" font-size="10.5" text-anchor="middle">①</text>
    <text class="fig-blue" x="287" y="67" font-size="10.5" text-anchor="middle">②</text>
    <text class="fig-blue" x="460" y="67" font-size="10.5" text-anchor="middle">③</text>
    <text class="fig-blue" x="538" y="118" font-size="10" text-anchor="middle">④送IR</text>
    <text class="fig-purple" x="529" y="194" font-size="9.5" text-anchor="middle">⑤译码</text>
    <text class="fig-green" x="393" y="208" font-size="10" text-anchor="middle">⑥ AD → MAR</text>
    <text class="fig-green" x="404" y="127" font-size="10" text-anchor="middle">⑦ MDR → ACC</text>
    <text class="fig-muted" x="397" y="249" font-size="9.5" text-anchor="middle">控制信号</text>
    <text class="fig-muted" x="24" y="288" font-size="10">取指阶段：①(PC)→MAR → ②读主存 → ③M(MAR)→MDR 且 (PC)+1→PC → ④(MDR)→IR　｜　分析阶段：⑤IR(OP)→CU 译码</text>
    <text class="fig-muted" x="24" y="308" font-size="10.5">执行阶段（LDA）：⑥IR(AD)→MAR → 读主存操作数送 MDR → ⑦(MDR)→ACC（注意 IR(AD)→MAR 回送取数通路）</text>
  </svg>
  <figcaption>图 1-10　模型机整机数据通路与取数指令 <code>LDA M</code> 的“取指—分析—执行”微操作全流程</figcaption>
</figure>

<strong>取数指令（LDA）的执行</strong>：

| 阶段 | 操作 |
|---|---|
| 取指 | ① PC → MAR；② MAR → 存储体，控制器发“读”；③ 指定单元内容 → MDR；④ MDR → IR；同时 PC+1 → PC |
| 分析 | IR 中操作码 OP → CU，译码后发出相应控制信号 |
| 执行 | IR 中地址码 AD → MAR → 存储体，读出的数据 → MDR → ACC |

<strong>存数指令（STA）</strong>：取指、分析阶段与取数完全相同；执行阶段为 IR 的地址码 → MAR，<strong>ACC → MDR</strong>，再在控制器控制下写入存储体指定单元（全过程约九步）。

<strong>程序的执行</strong>：

1. 通过输入设备把程序和数据装入主存；
2. 程序首地址送入 PC；
3. 启动机器，按“取指—分析—执行”逐条执行指令；
4. <strong>取指结束即可调整 PC</strong>（PC+1 → PC），因为分析与执行阶段不再使用 PC；
5. 遇到停机指令，程序结束。

> <strong>考点</strong>：任何指令的取指、分析阶段操作都相同，只有执行阶段因指令而异——这是“指令周期”划分与后续微程序控制器设计的基础。

<figure class="cpd-figure not-content" id="lhw-w1-fig-11">
  <svg viewBox="0 0 700 288" role="img" aria-label="典型指令ADD加法MUL乘法STA存数的公共取指分析与差异化执行微操作对比图">
    <defs>
      <marker id="lhw-w1-arr11" 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="18" y="16" width="664" height="256" rx="8"/>
    <line class="fig-line-accent" x1="350" y1="86" x2="350" y2="108" marker-end="url(#lhw-w1-arr11)"/>
    <line class="fig-line" x1="126" y1="96" x2="574" y2="96"/>
    <line class="fig-line" x1="126" y1="96" x2="126" y2="108" marker-end="url(#lhw-w1-arr11)"/>
    <line class="fig-line" x1="574" y1="96" x2="574" y2="108" marker-end="url(#lhw-w1-arr11)"/>
    <rect class="fig-box-purple" x="34" y="42" width="632" height="44" rx="5"/>
    <rect class="fig-box-blue" x="34" y="108" width="202" height="148" rx="5"/>
    <rect class="fig-box-green" x="249" y="108" width="202" height="148" rx="5"/>
    <rect class="fig-box-orange" x="464" y="108" width="202" height="148" rx="5"/>
    <text class="fig-title" x="34" y="34" font-size="12.5">指令周期共性与个性：公共“取指 + 分析”阶段 vs 不同指令的“执行”阶段微操作序列</text>
    <text class="fig-purple" x="350" y="61" font-size="10.2" text-anchor="middle">所有指令共享前 5 步：①(PC)→MAR  ②读主存  ③M(MAR)→MDR 且 (PC)+1→PC  ④(MDR)→IR  ⑤IR(OP)→CU 译码</text>
    <text class="fig-muted" x="350" y="78" font-size="10" text-anchor="middle">取指结束即可自增 PC（因后续阶段不再使用当前指令地址）；进入执行阶段后按 OP 分流：</text>
    <text class="fig-blue" x="135" y="128" font-size="12" text-anchor="middle">加法指令 ADD M</text>
    <text class="fig-label" x="46" y="150" font-size="10.5">⑥ IR(AD) → MAR</text>
    <text class="fig-label" x="46" y="169" font-size="10.5">⑦ M(MAR) → MDR（读加数）</text>
    <text class="fig-label" x="46" y="188" font-size="10.5">⑧ (MDR) → X（锁存操作数）</text>
    <text class="fig-blue" x="46" y="207" font-size="10.5">⑨ (ACC) + (X) → ALU → ACC</text>
    <text class="fig-muted" x="46" y="236" font-size="9.5">特点：访存读 1 次，和写回 ACC</text>
    <text class="fig-green" x="350" y="128" font-size="12" text-anchor="middle">乘法指令 MUL M</text>
    <text class="fig-label" x="259" y="150" font-size="10.5">⑥ IR(AD) → MAR</text>
    <text class="fig-label" x="259" y="169" font-size="10.5">⑦ M(MAR) → MDR（读因数）</text>
    <text class="fig-label" x="259" y="188" font-size="10">⑧ (MDR)→X, (ACC)→MQ, 0→ACC</text>
    <text class="fig-green" x="259" y="207" font-size="10">⑨ ALU 迭代“加+移位”→ ACC,MQ</text>
    <text class="fig-muted" x="259" y="236" font-size="9.5">特点：双倍字长积存 ACC||MQ</text>
    <text class="fig-orange" x="565" y="128" font-size="12" text-anchor="middle">存数指令 STA M（共 9 步）</text>
    <text class="fig-label" x="476" y="150" font-size="10.5">⑥ IR(AD) → MAR（送目的地址）</text>
    <text class="fig-label" x="476" y="169" font-size="10.5">⑦ (ACC) → MDR（准备写数据）</text>
    <text class="fig-label" x="476" y="188" font-size="10.5">⑧ CU 向主存发出“写”控制命令</text>
    <text class="fig-orange" x="476" y="207" font-size="10.5">⑨ (MDR) → M(MAR)（写入单元）</text>
    <text class="fig-muted" x="476" y="236" font-size="9.5">特点：执行阶段唯一写主存指令</text>
  </svg>
  <figcaption>图 1-11　模型机 <code>ADD</code>、<code>MUL</code>、<code>STA</code> 三类典型指令的公共取指分析与差异化执行阶段微操作对比</figcaption>
</figure>

### 1.8 计算机硬件的主要技术指标

评价一台机器，先看用途（文档处理、游戏需要强图形能力、科学计算需要高浮点性能），再看预算——买最合适的，不买最贵的。核心指标是<strong>速度快、容量大、价格低</strong>。

<strong>（1）机器字长</strong>

机器字长 = CPU <strong>一次能处理的二进制数据位数</strong>，通常等于 CPU 内寄存器的位数（模型机中与 ACC、X、MQ 等长）。

- 两个 8 位二进制数相加、结果仍是 8 位，则字长为 8 位，不是 24 位。
- 字长越长性能越好：64 位加法在字长 64 位的机器上一次完成；在字长 8 位的机器上要拆成 8 次 8 位加法并处理进位。

> <strong>注意</strong>：模型机假设存储字长 = 寄存器长度，仅为简化；真实机器中两者可以不同。

<strong>（2）运算速度</strong>

| 指标 | 含义 | 局限 |
|---|---|---|
| 主频 $f$ | 时钟频率，$T_c = 1/f$ 为时钟周期 | 主频高不等于速度快，还要看一条指令需多少时钟周期 |
| 核数 / 每核线程数 | 并行处理能力 | 不是速度的直接指标，受程序并行度制约 |
| 吉布森法 | 按指令出现频率对执行时间加权平均 | 需先统计频率 |
| CPI | 执行一条指令所需时钟周期数，取加权平均 | 越少越好，理想为 1 |
| IPC | 一个时钟周期完成的指令条数，$IPC = 1/CPI$ | 超标量机一个周期可完成多条指令 |
| MIPS | 每秒执行多少<strong>百万条</strong>指令 | 不区分指令复杂度，跨机比较不可靠 |
| MFLOPS 等 | 每秒百万次浮点运算 | 更接近“完成了多少运算”的本质 |
| 基准程序 | 用常用程序实测（如 Linpack、游戏） | 最直接、最贴合实际需求 |

<strong>吉布森法</strong>：把指令集中每条指令的执行时间按其在程序中的出现频率加权平均。

$$
T = \sum_{i} f_i \, CPI_i \, T_c
$$

$f_i$ 为第 $i$ 类指令的使用频率，可有两种统计方式：直接数程序清单得的<strong>静态使用频率</strong>，或把程序跑起来统计得的<strong>动态使用频率</strong>（更准确）。

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

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

<figure class="cpd-figure not-content" id="lhw-w1-fig-12">
  <svg viewBox="0 0 700 318" role="img" aria-label="CPU性能铁三角公式推导与软硬件影响因子及MIPS和存储容量换算辨析图">
    <defs>
      <marker id="lhw-w1-arr12" 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="18" y="16" width="664" height="278" rx="8"/>
    <line class="fig-line-accent" x1="226" y1="68" x2="250" y2="112" marker-end="url(#lhw-w1-arr12)"/>
    <line class="fig-line-accent" x1="226" y1="132" x2="250" y2="132" marker-end="url(#lhw-w1-arr12)"/>
    <line class="fig-line-accent" x1="226" y1="196" x2="250" y2="152" marker-end="url(#lhw-w1-arr12)"/>
    <line class="fig-line" x1="434" y1="112" x2="466" y2="68" marker-end="url(#lhw-w1-arr12)"/>
    <line class="fig-line" x1="434" y1="132" x2="466" y2="132" marker-end="url(#lhw-w1-arr12)"/>
    <line class="fig-line" x1="434" y1="152" x2="466" y2="196" marker-end="url(#lhw-w1-arr12)"/>
    <rect class="fig-box-blue" x="28" y="42" width="198" height="52" rx="5"/>
    <rect class="fig-box-purple" x="28" y="106" width="198" height="52" rx="5"/>
    <rect class="fig-box-green" x="28" y="170" width="198" height="52" rx="5"/>
    <rect class="fig-box-accent" x="252" y="92" width="182" height="80" rx="6"/>
    <rect class="fig-box-orange" x="468" y="42" width="204" height="52" rx="5"/>
    <rect class="fig-box" x="468" y="106" width="204" height="52" rx="5"/>
    <rect class="fig-box" x="468" y="170" width="204" height="52" rx="5"/>
    <rect class="fig-box-red" x="28" y="234" width="644" height="46" rx="5"/>
    <text class="fig-title" x="28" y="34" font-size="12.5">CPU 性能铁三角方程：T_CPU = IC × CPI × Tc 与衍生速率指标</text>
    <text class="fig-blue" x="36" y="62" font-size="10.8">指令条数 IC（动态执行数）</text>
    <text class="fig-muted" x="36" y="80" font-size="9">取决于：算法 · 编译器 · ISA 指令集</text>
    <text class="fig-purple" x="36" y="126" font-size="10.2">平均周期数 CPI = ∑(fi·CPIi)</text>
    <text class="fig-muted" x="36" y="144" font-size="9">取决于：ISA · 微体系结构 · Cache</text>
    <text class="fig-green" x="36" y="190" font-size="10.8">时钟周期 Tc = 1 / 主频 f</text>
    <text class="fig-muted" x="36" y="208" font-size="9">取决于：微体系结构（流水级）· 工艺</text>
    <text class="fig-accent" x="343" y="116" font-size="12" text-anchor="middle">CPU 执行时间（金标准）</text>
    <text class="fig-label" x="343" y="138" font-size="11.5" text-anchor="middle">T = IC × CPI × Tc</text>
    <text class="fig-muted" x="343" y="158" font-size="10.5" text-anchor="middle">= (IC × CPI) / f</text>
    <text class="fig-orange" x="570" y="62" font-size="11" text-anchor="middle">MIPS = f / (CPI × 10⁶)</text>
    <text class="fig-muted" x="570" y="80" font-size="9" text-anchor="middle">忽略指令复杂度，跨 ISA 比较易误导</text>
    <text class="fig-label" x="570" y="126" font-size="11" text-anchor="middle">IPC = 1 / CPI</text>
    <text class="fig-muted" x="570" y="144" font-size="9" text-anchor="middle">超标量多发射 CPU 可使 IPC &gt; 1</text>
    <text class="fig-label" x="570" y="190" font-size="10.2" text-anchor="middle">MFLOPS = 浮点次数 / (T × 10⁶)</text>
    <text class="fig-muted" x="570" y="208" font-size="9" text-anchor="middle">科学计算与基准程序实测更客观</text>
    <text class="fig-red" x="36" y="254" font-size="9.2">★ 408 量纲必考陷阱：存储容量 K/M/G 用 2 的幂（2¹⁰/2²⁰/2³⁰），主频 Hz 与速率 MIPS/MFLOPS 用 10 的幂（10⁶/10⁹）！</text>
    <text class="fig-muted" x="36" y="271" font-size="9.2">为什么 MIPS 高不一定快？例：RISC 机 IC 大、CPI 小 → MIPS 更高，但若 IC×CPI 乘积更大，总耗时 T_CPU 反而更慢！</text>
  </svg>
  <figcaption>图 1-12　CPU 执行时间核心公式 $T_{\text{CPU}} = IC \times \text{CPI} \times T_c$ 的软硬件决定因素与 408 量纲/MIPS 易错辨析</figcaption>
</figure>

<strong>（3）存储容量</strong>

容量 = 存放二进制信息的总位数，有两种表示：

- <strong>单元数 × 存储字长</strong>：如 MAR 10 位、MDR 8 位 → `1K×8`（1KB）；MAR 16 位、MDR 32 位 → `64K×32`（256KB）。
- <strong>字节数</strong>：`1B = 8b`，`1KB = 2^10 B`，`1MB = 2^20 B`，`1GB = 2^30 B`，如“80GB”。

$$
1\text{KB} = 2^{10}\text{B} = 2^{13}\text{b}
$$

$$
1\text{MB} = 2^{20}\text{B} = 2^{23}\text{b}, \quad 1\text{GB} = 2^{30}\text{B} = 2^{33}\text{b}
$$

> <strong>★考点</strong>：给出 MAR、MDR 位数，既要求出单元数 $2^n$ 与存储字长 $m$，也要能换算成字节数：$2^n \times m \div 8$ 字节。存储器分为主存与辅存，指标写法相同。

> <strong>注意</strong>：主频、核数只是“可能更快”的必要条件；真正决定速度的是 <strong>IC × CPI × 时钟周期</strong> 三者之积，以及程序本身的指令构成。

## 第2章 计算机的发展与应用

### 2.1 第一台电子计算机 ENIAC

ENIAC 由宾夕法尼亚大学摩尔学院（Moore School）的摩尔小组研制，核心成员为格尔斯坦、艾克特（Eckert，当时 24 岁）、莫克利（Mauchly）等四位科学家与工程师，1946 年完成。它是教材所称的“第一台电子计算机”，其研制过程直接催生了冯·诺依曼结构思想（但该思想并未用于 ENIAC 本身）。

| 项目 | 指标 |
|---|---|
| 运算方式 | 十进制 |
| 电子管 | 约 18000 只（文献记 17000～18800 不等） |
| 继电器 | 1500 只 |
| 功耗 | 150 kW（相当于 1500 个 100 W 灯泡） |
| 重量 / 占地 | 30 吨 / 1500 平方英尺 |
| 运算速度 | 5000 次加法/秒 |
| 相对速度 | 比当时最快计算机快 1000 倍，是手工计算的 20 万倍 |
| 经费 | 军方先投 15 万美元，总花费 48 万美元 |
| 用途 | 为美军弹道研究实验室计算弹道表 |
| 编程方式 | 手工扳动开关、插拔电缆 |
| 退役 | 1955 年 |

ENIAC 的两个根本缺点：<strong>没有存储器</strong>；<strong>用布线接板控制</strong>，运行一个小程序也要搭接几天，编程工作量抵消了运算速度优势。

<figure class="cpd-figure not-content" id="lhw-w1-fig-13">
  <svg viewBox="0 0 700 248" role="img" aria-label="第一台电子计算机ENIAC手工布线编程与冯诺依曼存储程序计算机的历史跨越对比图">
    <defs>
      <marker id="lhw-w1-arr13" 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="18" y="16" width="308" height="216" rx="8"/>
    <rect class="fig-box-subtle" x="374" y="16" width="308" height="216" rx="8"/>
    <line class="fig-line-accent" x1="326" y1="124" x2="374" y2="124" marker-end="url(#lhw-w1-arr13)"/>
    <rect class="fig-box-orange" x="32" y="44" width="280" height="82" rx="5"/>
    <rect class="fig-box-red" x="32" y="138" width="280" height="80" rx="5"/>
    <rect class="fig-box-green" x="388" y="44" width="280" height="82" rx="5"/>
    <rect class="fig-box-accent" x="388" y="138" width="280" height="80" rx="5"/>
    <text class="fig-title" x="32" y="34" font-size="12.5">ENIAC（1946，宾大摩尔学院）</text>
    <text class="fig-title" x="388" y="34" font-size="11.5">存储程序计算机（EDVAC 方案 → EDSAC / IAS）</text>
    <text class="fig-orange" x="44" y="64" font-size="11.5">核心指标：十进制 · 1.8 万只电子管</text>
    <text class="fig-label" x="44" y="83" font-size="10.5">· 重 30 吨，占地 1500 ft²，功耗 150 kW</text>
    <text class="fig-label" x="44" y="101" font-size="10.5">· 运算速度：5000 次加法/秒（算弹道火力表）</text>
    <text class="fig-muted" x="44" y="117" font-size="9.5">· 意义：验证电子高速计算可行，催生冯氏思想</text>
    <text class="fig-red" x="44" y="158" font-size="11.5">两大根本缺陷（非存储程序机）：</text>
    <text class="fig-label" x="44" y="177" font-size="10.5">① 无存储器保存程序与大量数据</text>
    <text class="fig-label" x="44" y="195" font-size="10.5">② 靠手工扳开关、插拔电缆硬连线编程</text>
    <text class="fig-muted" x="44" y="211" font-size="9.5">（搭接电路耗费数天，运算仅几秒，效率倒挂）</text>
    <text class="fig-green" x="400" y="64" font-size="11.5">核心变革：二进制 · 存储程序自动执行</text>
    <text class="fig-label" x="400" y="83" font-size="10.5">· 1945 发表 EDVAC 方案（五部件职责明确）</text>
    <text class="fig-label" x="400" y="101" font-size="10.5">· 1949 EDSAC 首台投入运行；1952 IAS/EDVAC</text>
    <text class="fig-muted" x="400" y="117" font-size="9.5">· 二进制天然契合电子开关稳态（0 与 1）</text>
    <text class="fig-accent" x="400" y="158" font-size="11.5">彻底解决编程瓶颈：</text>
    <text class="fig-label" x="400" y="177" font-size="10.5">① 指令与数据预先装入主存，按地址访问</text>
    <text class="fig-label" x="400" y="195" font-size="10.5">② 控制器按 PC 自动取指执行，无需重连硬件</text>
    <text class="fig-muted" x="400" y="211" font-size="9.5">（换题目只需更换主存中的程序，实现通用计算）</text>
  </svg>
  <figcaption>图 2-1　ENIAC（手工布线、十进制、无存储程序）向冯·诺依曼存储程序计算机的历史跨越</figcaption>
</figure>

> <strong>注意</strong>：“谁是第一台电子计算机”本身有争议，当时有多台机器在研制，不同专家与教材表述不同；ENIAC 的意义在于对后续计算机发展的深远影响，而非“第一”的名号。
>
> <strong>★考点</strong>：ENIAC 的器件（电子管）、数制（十进制）、速度（5000 次加法/秒）、编程方式（开关+插线板）与两大缺点。

### 2.2 发展驱动力、分代与代表机型

<strong>驱动力是需求</strong>：弹道研究实验室每天要向陆军提供 6 张火力表，每条弹道是一组复杂非线性方程，只能数值近似求解；200 名计算员加班两个月才算完一张表。战争胜负取决于计算速度，高速运算设备成为刚需。

<strong>技术动力：电子管 → 晶体管 → 集成电路，电子技术的发展是直接动力；当电子技术处于某一平台期时，体系结构技术</strong>接棒提升速度。整个发展史中，电子技术与体系结构技术的贡献<strong>约各占一半</strong>。硬件技术起决定作用，因此<strong>计算机分代以硬件器件为标志</strong>。

<figure class="cpd-figure not-content" id="lhw-w1-fig-14">
  <svg viewBox="0 0 700 268" role="img" aria-label="电子计算机按硬件逻辑器件四代演进与需求电子技术体系结构驱动力示意图">
    <defs>
      <marker id="lhw-w1-arr14" 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="18" y="16" width="664" height="236" rx="8"/>
    <line class="fig-line-accent" x1="176" y1="108" x2="192" y2="108" marker-end="url(#lhw-w1-arr14)"/>
    <line class="fig-line-accent" x1="336" y1="108" x2="352" y2="108" marker-end="url(#lhw-w1-arr14)"/>
    <line class="fig-line-accent" x1="496" y1="108" x2="512" y2="108" marker-end="url(#lhw-w1-arr14)"/>
    <rect class="fig-box-orange" x="32" y="48" width="144" height="120" rx="5"/>
    <rect class="fig-box-blue" x="192" y="48" width="144" height="120" rx="5"/>
    <rect class="fig-box-purple" x="352" y="48" width="144" height="120" rx="5"/>
    <rect class="fig-box-green" x="512" y="48" width="156" height="120" rx="5"/>
    <rect class="fig-box" x="32" y="184" width="636" height="54" rx="5"/>
    <text class="fig-title" x="32" y="36" font-size="12.5">电子计算机四代演进（分代唯一核心标志：硬件逻辑器件）</text>
    <text class="fig-orange" x="104" y="68" font-size="12" text-anchor="middle">第一代（1946—1957）</text>
    <text class="fig-label" x="104" y="88" font-size="11.5" text-anchor="middle">电子管</text>
    <text class="fig-muted" x="104" y="108" font-size="9.5" text-anchor="middle">ENIAC / EDVAC / IAS</text>
    <text class="fig-muted" x="104" y="126" font-size="9.5" text-anchor="middle">后期约 4 万次/秒</text>
    <text class="fig-muted" x="104" y="144" font-size="9.5" text-anchor="middle">机器语言 / 汇编起步</text>
    <text class="fig-blue" x="264" y="68" font-size="12" text-anchor="middle">第二代</text>
    <text class="fig-label" x="264" y="88" font-size="11.5" text-anchor="middle">晶体管</text>
    <text class="fig-muted" x="264" y="108" font-size="9.5" text-anchor="middle">体积、功耗大幅下降</text>
    <text class="fig-muted" x="264" y="126" font-size="9.5" text-anchor="middle">可靠性显著提升</text>
    <text class="fig-muted" x="264" y="144" font-size="9.5" text-anchor="middle">高级语言与监控程序</text>
    <text class="fig-purple" x="424" y="68" font-size="12" text-anchor="middle">第三代</text>
    <text class="fig-label" x="424" y="88" font-size="11.5" text-anchor="middle">中小规模集成电路</text>
    <text class="fig-muted" x="424" y="108" font-size="9.5" text-anchor="middle">SSI / MSI 替代分立元件</text>
    <text class="fig-muted" x="424" y="126" font-size="9.5" text-anchor="middle">代表：IBM System/360</text>
    <text class="fig-muted" x="424" y="144" font-size="9.5" text-anchor="middle">操作系统与系列机普及</text>
    <text class="fig-green" x="590" y="68" font-size="12" text-anchor="middle">第四代（1978—至今）</text>
    <text class="fig-label" x="590" y="88" font-size="11.5" text-anchor="middle">大规模 → 超大规模 IC</text>
    <text class="fig-muted" x="590" y="108" font-size="9.5" text-anchor="middle">LSI / VLSI（微处理器）</text>
    <text class="fig-muted" x="590" y="126" font-size="9.5" text-anchor="middle">微型机、多核与超算并行</text>
    <text class="fig-muted" x="590" y="144" font-size="9.5" text-anchor="middle">网络化与智能化时代</text>
    <text class="fig-accent" x="46" y="204" font-size="11">三大发展驱动力：应用刚需牵引（弹道计算 → 科学/商业/AI） ＋ 双技术引擎交替接棒：</text>
    <text class="fig-muted" x="46" y="224" font-size="10">① 微电子器件工艺升级（直接动力，约占 50% 性能贡献）　＋　② 计算机体系结构创新（平台期接力，约占 50% 贡献）</text>
  </svg>
  <figcaption>图 2-2　按硬件逻辑器件划分的四代电子计算机演进及“电子工艺 + 体系结构”双轮驱动规律</figcaption>
</figure>

> <strong>注意</strong>：字幕仅给出第一代（1946—1957）与超大规模 IC（1978 起）的年份，其余代际年份未讲，上图只按器件给出定性区间。第一代“约 4 万次/秒”指该代后期机型，ENIAC 本身为 5000 次加法/秒。

<strong>IAS 机</strong>：1946 年开始设计、1952 年投入运行，由普林斯顿大学<strong>先进技术研究所</strong>（Institute for Advanced Study）研制。它是典型的冯·诺依曼结构：五大部件，I/O 与运算器相连，是<strong>以运算器为中心</strong>的结构。

<strong>IBM System/360</strong>：1961 年开始研制，1964 年研制成功，研发投入约 50 亿美元，被《幸福》杂志称为“IBM 50 亿美元的大赌博”，最终赌赢。其革命性在于<strong>系列产品而非单台机器：大中小型 360 具有相同的指令系统、相同的数据表示，可运行相同软件、配置相同外设，机器之间可互联。系列机是同厂家生产的同体系结构机器，兼容机</strong>是不同厂家生产的同体系结构机器。

360 的两大贡献：

1. 阿姆达尔（Amdahl）、布鲁克斯（Brooks）提出<strong>计算机系统结构</strong>概念，明确软硬件的交界面。硬件与软件人员可依据同一界面并行设计，缩短周期；界面不变时内部总线可为 8/16/32/64 位，指令集与接口相同，软件可跨型号运行。
2. 由此产生<strong>系列机与兼容机</strong>，使计算机从低速发展跨入高速发展阶段。沃森称它是公司历史上最重要的产品公告——“不只是一个产品，而是一系列产品”。

> <strong>★考点</strong>：IBM 360 = 系列机 + 系统结构（软硬件界面）概念；系列机与兼容机的定义。

<figure class="cpd-figure not-content" id="lhw-w1-fig-15">
  <svg viewBox="0 0 700 282" role="img" aria-label="IBM System 360统一计算机系统结构与系列机对比兼容机示意图">
    <defs>
      <marker id="lhw-w1-arr15" 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="18" y="16" width="664" height="250" rx="8"/>
    <rect class="fig-box-subtle" x="34" y="162" width="442" height="92" rx="6"/>
    <line class="fig-line-accent" x1="350" y1="84" x2="350" y2="104" marker-end="url(#lhw-w1-arr15)"/>
    <line class="fig-line-accent" x1="112" y1="148" x2="112" y2="174" marker-end="url(#lhw-w1-arr15)"/>
    <line class="fig-line-accent" x1="256" y1="148" x2="256" y2="174" marker-end="url(#lhw-w1-arr15)"/>
    <line class="fig-line-accent" x1="400" y1="148" x2="400" y2="174" marker-end="url(#lhw-w1-arr15)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="578" y1="148" x2="578" y2="174" marker-end="url(#lhw-w1-arr15)"/>
    <rect class="fig-box-blue" x="96" y="42" width="508" height="42" rx="5"/>
    <rect class="fig-box-accent" x="34" y="104" width="632" height="44" rx="6"/>
    <rect class="fig-box-green" x="48" y="174" width="128" height="54" rx="4"/>
    <rect class="fig-box-green" x="192" y="174" width="128" height="54" rx="4"/>
    <rect class="fig-box-green" x="336" y="174" width="128" height="54" rx="4"/>
    <rect class="fig-box-purple" x="490" y="174" width="176" height="54" rx="5"/>
    <text class="fig-title" x="34" y="32" font-size="12.5">IBM System/360（1964）里程碑：确立“计算机系统结构”概念与软硬件解耦</text>
    <text class="fig-blue" x="350" y="61" font-size="11.5" text-anchor="middle">同一套操作系统与应用软件（二进制兼容，无需重写即可跨型号运行）</text>
    <text class="fig-muted" x="350" y="77" font-size="9.5" text-anchor="middle">软硬件工程师依据同一接口契约并行开发，大幅缩短研制周期</text>
    <text class="fig-accent" x="350" y="123" font-size="12" text-anchor="middle">统一的计算机系统结构（相同指令系统 · 相同数据表示 · 统一 I/O 标准接口）</text>
    <text class="fig-muted" x="350" y="140" font-size="9.5" text-anchor="middle">↑ 上看是软硬件交界面（体系结构相同）　｜　↓ 下看允许完全不同的内部逻辑实现（计算机组成不同）</text>
    <text class="fig-green" x="112" y="194" font-size="11" text-anchor="middle">低端小型机</text>
    <text class="fig-muted" x="112" y="212" font-size="9.5" text-anchor="middle">8 位内部数据通路</text>
    <text class="fig-green" x="256" y="194" font-size="11" text-anchor="middle">中端中型机</text>
    <text class="fig-muted" x="256" y="212" font-size="9.5" text-anchor="middle">16/32 位数据通路</text>
    <text class="fig-green" x="400" y="194" font-size="11" text-anchor="middle">高端大型机</text>
    <text class="fig-muted" x="400" y="212" font-size="9.5" text-anchor="middle">64 位高速并行通路</text>
    <text class="fig-green" x="255" y="244" font-size="10.5" text-anchor="middle">★ 系列机：同一厂家生产，系统结构相同、组成实现不同</text>
    <text class="fig-purple" x="578" y="194" font-size="11" text-anchor="middle">其他厂家机型</text>
    <text class="fig-muted" x="578" y="212" font-size="9.5" text-anchor="middle">遵循同一 360 指令集接口</text>
    <text class="fig-purple" x="578" y="244" font-size="10.5" text-anchor="middle">★ 兼容机：不同厂家、同系统结构</text>
  </svg>
  <figcaption>图 2-3　IBM System/360 统一系统结构（ISA 交界面）与“系列机（同厂）vs 兼容机（异厂）”辨析</figcaption>
</figure>

### 2.3 微处理器与摩尔定律

<strong>微处理器是用一片大规模集成电路构成的处理器，片上集成运算器与控制器；微型计算机</strong>是以微处理器为核心、面向个人用户，体积小、重量轻、价格低的计算机。

- 1971 年 Intel 推出 <strong>4004</strong>，4 位；字长沿 4 位 → 8 位 → 16 位 → 32 位 → 64 位演进。
- 1970 年出现存储芯片，容量仅 256 位；微处理器芯片与存储芯片共同奠定微型计算机的基础。
- Intel 是微处理器发展史上最重要的公司。

| 型号 | 晶体管数 | 说明 |
|---|---|---|
| 8086 | 2.9 万 | 起点 |
| 80286 | 13.4 万 | 约 3 年后，约为前者 4 倍 |
| 80386 | 27.5 万 | 1985 年 |
| 80486 | 120 万 | 约 4 年后，约为前者 4 倍 |
| 现今芯片 | 约 30 亿 | 集成度继续指数增长 |

<strong>摩尔定律</strong>：1965 年，时任仙童半导体研究开发实验室主任的戈登·摩尔（Intel 创始人之一）应《Electronics》35 周年邀稿，撰写《让集成电路填满更多元件》。他统计发现，芯片上晶体管数量约为上一代的两倍，而新一代芯片约在前一代之后 18～24 个月出现。

$$
T_{\mathrm{double}} \approx 18\ \text{months},\qquad N(t)=N_0\cdot 2^{t/18}
$$

即：价格不变时，芯片晶体管数目每 18 个月翻一番，性能相应提高一倍；每 3 年变为原来的 4 倍；每一美元买到的计算性能每 18 个月提高一倍。

<figure class="cpd-figure not-content" id="lhw-w1-fig-16">
  <svg viewBox="0 0 700 314" role="img" aria-label="微处理器字长演进与摩尔定律晶体管数量对数增长曲线图">
    <defs>
      <marker id="lhw-w1-arr16" 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="18" y="16" width="664" height="282" rx="8"/>
    <line class="fig-divider" x1="88" y1="84" x2="646" y2="84"/>
    <line class="fig-divider" x1="88" y1="106" x2="646" y2="106"/>
    <line class="fig-divider" x1="88" y1="128" x2="646" y2="128"/>
    <line class="fig-divider" x1="88" y1="150" x2="646" y2="150"/>
    <line class="fig-divider" x1="88" y1="172" x2="646" y2="172"/>
    <line class="fig-divider" x1="88" y1="194" x2="646" y2="194"/>
    <line class="fig-divider" x1="88" y1="216" x2="646" y2="216"/>
    <line class="fig-line" x1="88" y1="238" x2="656" y2="238" marker-end="url(#lhw-w1-arr16)"/>
    <line class="fig-line" x1="88" y1="238" x2="88" y2="74" marker-end="url(#lhw-w1-arr16)"/>
    <polyline class="fig-line-accent" points="140,230 242,206 328,191 414,184 504,170 612,95"/>
    <rect class="fig-box" x="92" y="186" width="102" height="20" rx="3"/>
    <rect class="fig-box" x="236" y="216" width="102" height="20" rx="3"/>
    <rect class="fig-box" x="244" y="158" width="114" height="20" rx="3"/>
    <rect class="fig-box" x="388" y="198" width="114" height="20" rx="3"/>
    <rect class="fig-box" x="396" y="136" width="110" height="20" rx="3"/>
    <rect class="fig-box-green" x="456" y="78" width="128" height="22" rx="4"/>
    <rect class="fig-box-blue" x="34" y="42" width="632" height="26" rx="4"/>
    <circle class="fig-dot" cx="140" cy="230" r="3.5"/>
    <circle class="fig-dot" cx="242" cy="206" r="3.5"/>
    <circle class="fig-dot" cx="328" cy="191" r="3.5"/>
    <circle class="fig-dot" cx="414" cy="184" r="3.5"/>
    <circle class="fig-dot" cx="504" cy="170" r="3.5"/>
    <circle class="fig-dot" cx="612" cy="95" r="4"/>
    <text class="fig-title" x="34" y="34" font-size="12.5">摩尔定律对数坐标趋势：N(t) = N₀ · 2^(t/18)（纵轴每格 10 倍，直线代表指数增长）</text>
    <text class="fig-blue" x="350" y="59" font-size="9.8" text-anchor="middle">微处理器字长演进：Intel 4004（4 位, 1971）→ 8 位 → 8086/286（16 位）→ 386/486（32 位）→ 现代多核（64 位）</text>
    <text class="fig-muted" x="80" y="88" font-size="9.5" text-anchor="end">10¹⁰</text>
    <text class="fig-muted" x="80" y="110" font-size="9.5" text-anchor="end">10⁹</text>
    <text class="fig-muted" x="80" y="132" font-size="9.5" text-anchor="end">10⁸</text>
    <text class="fig-muted" x="80" y="154" font-size="9.5" text-anchor="end">10⁷</text>
    <text class="fig-muted" x="80" y="176" font-size="9.5" text-anchor="end">10⁶</text>
    <text class="fig-muted" x="80" y="198" font-size="9.5" text-anchor="end">10⁵</text>
    <text class="fig-muted" x="80" y="220" font-size="9.5" text-anchor="end">10⁴</text>
    <text class="fig-muted" x="80" y="241" font-size="9.5" text-anchor="end">10³</text>
    <text class="fig-label" x="143" y="200" font-size="9.5" text-anchor="middle">4004（2300 只）</text>
    <text class="fig-label" x="287" y="230" font-size="9.5" text-anchor="middle">8086（2.9 万）</text>
    <text class="fig-label" x="301" y="172" font-size="9.5" text-anchor="middle">80286（13.4 万）</text>
    <text class="fig-label" x="445" y="212" font-size="9.5" text-anchor="middle">80386（27.5 万）</text>
    <text class="fig-label" x="451" y="150" font-size="9.5" text-anchor="middle">80486（120 万）</text>
    <text class="fig-green" x="520" y="93" font-size="10" text-anchor="middle">现今芯片（≈30 亿）</text>
    <text class="fig-muted" x="140" y="255" font-size="10" text-anchor="middle">1971</text>
    <text class="fig-muted" x="280" y="255" font-size="10" text-anchor="middle">1980</text>
    <text class="fig-muted" x="414" y="255" font-size="10" text-anchor="middle">1985</text>
    <text class="fig-muted" x="504" y="255" font-size="10" text-anchor="middle">1990</text>
    <text class="fig-muted" x="612" y="255" font-size="10" text-anchor="middle">现今</text>
    <text class="fig-accent" x="350" y="282" font-size="11" text-anchor="middle">★ 戈登·摩尔（1965）：价格不变时，每 18～24 个月晶体管数与性能翻一番（每 3 年约增长为 4 倍）</text>
  </svg>
  <figcaption>图 2-4　微处理器字长演进与摩尔定律对数坐标增长趋势（每 18 个月翻番，每 3 年 4 倍）</figcaption>
</figure>

> <strong>注意</strong>：上图横轴年份按字幕所给“3 年后 / 4 年后 / 85 年”近似放置，仅表示趋势，不是精确发布日。
>
> <strong>★考点</strong>：摩尔定律的提出者（戈登·摩尔）、时间（1965 年）、原始结论（18～24 个月翻倍）与常用表述（每 18 个月翻番、每 3 年 4 倍）。

### 2.4 软件技术的发展

硬件之外，软件是计算机广泛应用的前提。编程语言沿“贴近机器 → 面向问题”演进：

<figure class="cpd-figure not-content" id="lhw-w1-fig-17">
  <svg viewBox="0 0 700 292" role="img" aria-label="编程语言三阶段演进可移植性对比与系统软件组成结构图">
    <defs>
      <marker id="lhw-w1-arr17" 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="18" y="16" width="664" height="260" rx="8"/>
    <line class="fig-line-accent" x1="224" y1="98" x2="256" y2="98" marker-end="url(#lhw-w1-arr17)"/>
    <line class="fig-line-accent" x1="444" y1="98" x2="476" y2="98" marker-end="url(#lhw-w1-arr17)"/>
    <path class="fig-line-dashed" d="M 572 152 L 572 168 L 350 168 L 350 152" marker-end="url(#lhw-w1-arr17)"/>
    <path class="fig-line-dashed" d="M 350 168 L 128 168 L 128 152" marker-end="url(#lhw-w1-arr17)"/>
    <rect class="fig-box-orange" x="34" y="44" width="190" height="108" rx="5"/>
    <rect class="fig-box-blue" x="256" y="44" width="188" height="108" rx="5"/>
    <rect class="fig-box-green" x="476" y="44" width="190" height="108" rx="5"/>
    <rect class="fig-box-purple" x="34" y="186" width="632" height="76" rx="5"/>
    <rect class="fig-box" x="192" y="158" width="128" height="18" rx="3"/>
    <rect class="fig-box" x="416" y="158" width="128" height="18" rx="3"/>
    <text class="fig-title" x="34" y="34" font-size="12.5">编程语言演进：从“面向具体机器硬件”走向“面向人类求解问题”（抽象度与可移植性逐级提升）</text>
    <text class="fig-orange" x="129" y="64" font-size="12" text-anchor="middle">机器语言（直接执行）</text>
    <text class="fig-label" x="129" y="84" font-size="10.5" text-anchor="middle">000001 0000001000</text>
    <text class="fig-muted" x="129" y="103" font-size="9.5" text-anchor="middle">纯二进制代码，难读难排错</text>
    <text class="fig-muted" x="129" y="121" font-size="9.5" text-anchor="middle">强绑定具体硬件，不可移植</text>
    <text class="fig-muted" x="129" y="139" font-size="9.5" text-anchor="middle">唯一能被 CPU 直接识别</text>
    <text class="fig-blue" x="350" y="64" font-size="12" text-anchor="middle">汇编语言（符号助记）</text>
    <text class="fig-label" x="350" y="84" font-size="10.5" text-anchor="middle">LDA 8  /  ADD 9</text>
    <text class="fig-muted" x="350" y="103" font-size="9.5" text-anchor="middle">助记符替代二进制操作码</text>
    <text class="fig-muted" x="350" y="121" font-size="9.5" text-anchor="middle">仍面向机器指令集，难移植</text>
    <text class="fig-muted" x="350" y="139" font-size="9.5" text-anchor="middle">与机器指令近似一一对应</text>
    <text class="fig-green" x="571" y="64" font-size="12" text-anchor="middle">高级语言（面向问题）</text>
    <text class="fig-label" x="571" y="84" font-size="10.5" text-anchor="middle">y = a * x + b;</text>
    <text class="fig-muted" x="571" y="103" font-size="9.5" text-anchor="middle">屏蔽底层指令集，可移植性强</text>
    <text class="fig-muted" x="571" y="121" font-size="8.6" text-anchor="middle">Fortran（科学）/ C++ / Java（跨平台）</text>
    <text class="fig-muted" x="571" y="139" font-size="9.5" text-anchor="middle">1 条语句对应多条机器指令</text>
    <text class="fig-muted" x="256" y="171" font-size="9.5" text-anchor="middle">← 汇编程序翻译</text>
    <text class="fig-muted" x="480" y="171" font-size="9.5" text-anchor="middle">← 编译 / 解释程序</text>
    <text class="fig-purple" x="48" y="206" font-size="11.5">系统软件五大核心成员（管理软硬件资源、支撑应用运行）：</text>
    <text class="fig-label" x="48" y="226" font-size="10.5">① 语言处理程序（汇编 / 编译 / 解释）　② 操作系统（UNIX / Linux / Windows / 麒麟）　③ 数据库管理系统（DBMS）</text>
    <text class="fig-muted" x="48" y="246" font-size="10">④ 网络通信软件　⑤ 服务性程序（标准数学库 / 装配链接 / 调试诊断）　｜　软件工程痛点：周期长、人力贵、路径爆炸难测</text>
  </svg>
  <figcaption>图 2-5　机器语言、汇编语言、高级语言的演进转化关系及系统软件构成体系</figcaption>
</figure>

<strong>软件发展的四个特点</strong>：

1. <strong>开发周期长</strong>：大型软件数千万行。设 4000 万行、每人年 1 万行，则需 4000 万 ÷ 1 万 = 4000 人年。
2. <strong>成本昂贵</strong>：主要靠人编写，人力成本高。
3. <strong>质量难检测</strong>：输入域穷举不现实；代码覆盖、分支覆盖、路径覆盖都难以做到；循环嵌套导致路径组合爆炸，无法保证无错。
4. <strong>开发效率低</strong>：硬件设计有大量辅助工具，软件仍主要靠人写代码；应用越复杂，人的思维局限使缺陷几乎不可避免（历史上曾导致火箭爆炸、银行网络与电商网站瘫痪）。

> <strong>★考点</strong>：机器语言、汇编语言、高级语言三者的面向对象与可移植性差异；系统软件的组成。

### 2.5 计算机应用与展望

<strong>科学计算</strong>是电子计算机产生的动因：天体建模、核爆炸模拟、全球气候变化、天气预报、流体/海洋/大气建模，主要使用高性能计算机（HPC）。

<strong>数据处理</strong>：银行、电信、税务等数据密集型任务，单个事务运算量不大但并发事务多，主要采用大型机（mainframe）结构，如 IBM 360、IBM z10。

<strong>工业控制与实时控制</strong>：自动化通过计算机控制设备实现；无人驾驶依赖实时控制。

<strong>网络技术</strong>：电子商务（京东、淘宝等）、网络教育（MOOC）、敏捷制造（在先进制造技术、有技术有知识的管理人员等资源支撑下，对迅速变化的市场需求快速响应）、虚拟现实（VR，又称灵境技术/人工环境；以计算机生成虚拟世界，通过视觉、听觉、触觉感受，如飞行训练模拟器）。

<strong>其他应用</strong>：办公自动化与信息管理系统；CAD（计算机辅助设计）、CAM（计算机辅助制造）、CIMS（计算机集成制造系统，将产品设计中分散孤立的自动化子系统有机联合，适合多品种小批量生产）；多媒体技术（声音、视频）；人工智能。

<figure class="cpd-figure not-content" id="lhw-w1-fig-18">
  <svg viewBox="0 0 700 326" role="img" aria-label="计算机八大应用领域机型匹配与后摩尔时代硅芯片三重限制及光DNA量子计算展望图">
    <rect class="fig-box-subtle" x="18" y="16" width="664" height="294" rx="8"/>
    <rect class="fig-box-blue" x="32" y="44" width="152" height="84" rx="5"/>
    <rect class="fig-box-blue" x="194" y="44" width="152" height="84" rx="5"/>
    <rect class="fig-box-blue" x="356" y="44" width="152" height="84" rx="5"/>
    <rect class="fig-box-blue" x="518" y="44" width="150" height="84" rx="5"/>
    <rect class="fig-box-green" x="32" y="138" width="152" height="72" rx="5"/>
    <rect class="fig-box-green" x="194" y="138" width="152" height="72" rx="5"/>
    <rect class="fig-box-green" x="356" y="138" width="152" height="72" rx="5"/>
    <rect class="fig-box-accent" x="518" y="138" width="150" height="72" rx="5"/>
    <rect class="fig-box-red" x="32" y="222" width="302" height="74" rx="5"/>
    <rect class="fig-box-purple" x="348" y="222" width="320" height="74" rx="5"/>
    <text class="fig-title" x="32" y="34" font-size="12.5">计算机应用领域全景（机型匹配）与后摩尔时代新型计算路线</text>
    <text class="fig-blue" x="108" y="63" font-size="11.5" text-anchor="middle">科学计算（起源动因）</text>
    <text class="fig-label" x="108" y="82" font-size="10" text-anchor="middle">天体/核模拟/天气/流体</text>
    <text class="fig-muted" x="108" y="100" font-size="9.5" text-anchor="middle">主力：HPC 高性能超算</text>
    <text class="fig-muted" x="108" y="116" font-size="9.5" text-anchor="middle">重浮点吞吐（FLOPS）</text>
    <text class="fig-blue" x="270" y="63" font-size="11.5" text-anchor="middle">数据处理（事务密集）</text>
    <text class="fig-label" x="270" y="82" font-size="10" text-anchor="middle">银行/电信/税务高并发</text>
    <text class="fig-muted" x="270" y="100" font-size="9.5" text-anchor="middle">主力：大型机（IBM z10）</text>
    <text class="fig-muted" x="270" y="116" font-size="9.5" text-anchor="middle">单笔运算小、高可靠 I/O</text>
    <text class="fig-blue" x="432" y="63" font-size="11.5" text-anchor="middle">工业与实时控制</text>
    <text class="fig-label" x="432" y="82" font-size="10" text-anchor="middle">自动化产线 / 自动驾驶</text>
    <text class="fig-muted" x="432" y="100" font-size="9.5" text-anchor="middle">主力：工控 / 嵌入式机</text>
    <text class="fig-muted" x="432" y="116" font-size="9.5" text-anchor="middle">硬实时响应延迟约束</text>
    <text class="fig-blue" x="593" y="63" font-size="11.5" text-anchor="middle">网络与虚拟现实</text>
    <text class="fig-label" x="593" y="82" font-size="10" text-anchor="middle">电商 / MOOC / 敏捷制造</text>
    <text class="fig-muted" x="593" y="100" font-size="9.5" text-anchor="middle">VR（灵境/人工环境）</text>
    <text class="fig-muted" x="593" y="116" font-size="9.5" text-anchor="middle">视听触多通道沉浸模拟</text>
    <text class="fig-green" x="108" y="158" font-size="11.5" text-anchor="middle">办公自动化 / MIS</text>
    <text class="fig-muted" x="108" y="177" font-size="9.5" text-anchor="middle">信息管理系统</text>
    <text class="fig-muted" x="108" y="195" font-size="9.5" text-anchor="middle">日常业务流程数字化</text>
    <text class="fig-green" x="270" y="158" font-size="11.5" text-anchor="middle">CAD / CAM / CIMS</text>
    <text class="fig-muted" x="270" y="177" font-size="9.5" text-anchor="middle">辅助设计制造与集成制造</text>
    <text class="fig-muted" x="270" y="195" font-size="9.5" text-anchor="middle">适配多品种小批量生产</text>
    <text class="fig-green" x="432" y="158" font-size="11.5" text-anchor="middle">多媒体技术</text>
    <text class="fig-muted" x="432" y="177" font-size="9.5" text-anchor="middle">音频 / 视频编解码渲染</text>
    <text class="fig-muted" x="432" y="195" font-size="9.5" text-anchor="middle">人机自然交互基础</text>
    <text class="fig-accent" x="593" y="158" font-size="11.5" text-anchor="middle">人工智能（AI）</text>
    <text class="fig-muted" x="593" y="177" font-size="9.5" text-anchor="middle">Watson / 类脑大模型</text>
    <text class="fig-muted" x="593" y="195" font-size="9.5" text-anchor="middle">赖于大容量+高算力匹配</text>
    <text class="fig-red" x="44" y="241" font-size="11">硅芯片集成度的三重瓶颈：</text>
    <text class="fig-label" x="44" y="259" font-size="10">① 物理极限：晶体管尺寸逼近原子尺度</text>
    <text class="fig-muted" x="44" y="275" font-size="9.5">② 制造成本几何级攀升　③ 功耗散热墙与线延迟剧增</text>
    <text class="fig-purple" x="360" y="241" font-size="11">后硅时代三条探索路线：</text>
    <text class="fig-label" x="360" y="259" font-size="10">· 光计算：光束空中反射交叉无冲突　· DNA 计算：生化反应并行</text>
    <text class="fig-muted" x="360" y="275" font-size="9.5">· 量子计算：原子量子态叠加运算（量子密码通信已成热点）</text>
  </svg>
  <figcaption>图 2-6　计算机应用领域分类（科学计算→HPC、数据处理→大型机）与硅基瓶颈及新型计算展望</figcaption>
</figure>

<strong>展望</strong>：

1. <strong>人工智能</strong>：计算机具备类似人脑的超级智能，如自动驾驶、IBM Watson（以创始人命名）、谷歌大脑、百度大脑。前提是<strong>大存储容量、高运算速度与高速模式匹配</strong>。
2. <strong>芯片集成度受三重限制</strong>：一是物理极限，晶体管再小也要比原子大；二是制造设备越来越昂贵，成本呈几何级数上升；三是功耗与散热增加、线延迟增大、制造缺陷增多。集成度还能提高多少存在疑问。
3. <strong>替代硅芯片的可能</strong>：光计算机（数据存储与传输已可用光实现；光在机内可用反射镜/折射镜在空中交叉，而电路板总线不能直接交叉，光在传输上最方便）、DNA 计算机（控制 DNA 分子间生化反应完成计算）、量子计算机（利用原子的量子特性，当前量子密码是热门方向）。哪条路线能替代硅芯片，尚无定论。

> <strong>★考点</strong>：三类基本应用（科学计算 → HPC；数据处理 → 大型机；工业/实时控制）；摩尔定律面临的物理极限与成本、功耗约束；光、DNA、量子计算的基本原理。

## 第3章 总线

### 3.1 总线的基本概念

<strong>为什么需要总线</strong>：冯·诺依曼机的五大部件（运算器、控制器、存储器、输入设备、输出设备）必须互连才能协调工作。若采用分散连接，让 $n$ 个部件两两直连，需要 $n(n-1)/2$ 条通信线路；每条线束若按 100 根计，线路数量、印刷电路板成本与接口占用空间都无法承受，而且新增一个设备还要增加 $n$ 条连线，系统几乎无法扩展。

<strong>总线的定义</strong>：总线是一组信息的公共传输线（bus）。所有部件都挂接在同一组公共线上，需要通信的一对设备分时使用这组线。

<strong>总线结构的两面性</strong>：

- 任一时刻只允许一对设备/部件使用总线，其余设备只能等待前者释放，因此总线是系统的<strong>瓶颈</strong>；
- 结构统一、连线少、接口标准化，<strong>可扩展性</strong>好，增加设备只需挂到总线上。

> <strong>★考点</strong>：总线的关键词是“公共”与“分时”。后面总线判优解决“谁用”，总线通信控制解决“怎么用”。

<strong>串行传输与并行传输</strong>：

| 方式 | 数据传送 | 线路 | 距离 | 说明 |
|---|---|---|---|---|
| 串行 | 一位一位传送 | 少 | 长，可跨机器 | 现代高速链路多采用串行 |
| 并行 | 多位同时传送 | 多条数据线 | 短，多在机箱内 | 线间干扰、信号变形；远距离需特殊措施 |

<figure class="cpd-figure not-content" id="lhw-w2-fig-1">
  <svg viewBox="0 0 700 284" role="img" aria-label="分散连接与公共总线三态门互连对比及串并行传输示意图">
    <defs>
      <marker id="lhw-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="16" y="14" width="330" height="256" rx="8" class="fig-box-subtle"/>
    <rect x="358" y="14" width="326" height="256" rx="8" class="fig-box-subtle"/>
    <line x1="30" y1="142" x2="332" y2="142" class="fig-divider"/>
    <line x1="372" y1="146" x2="670" y2="146" class="fig-divider"/>
    <line x1="78" y1="62" x2="148" y2="54" class="fig-line"/>
    <line x1="78" y1="62" x2="218" y2="62" class="fig-line"/>
    <line x1="78" y1="62" x2="104" y2="108" class="fig-line"/>
    <line x1="78" y1="62" x2="192" y2="108" class="fig-line"/>
    <line x1="148" y1="54" x2="218" y2="62" class="fig-line"/>
    <line x1="148" y1="54" x2="104" y2="108" class="fig-line"/>
    <line x1="148" y1="54" x2="192" y2="108" class="fig-line"/>
    <line x1="218" y1="62" x2="104" y2="108" class="fig-line"/>
    <line x1="218" y1="62" x2="192" y2="108" class="fig-line"/>
    <line x1="104" y1="108" x2="192" y2="108" class="fig-line"/>
    <circle cx="78" cy="62" r="16" class="fig-box-red"/>
    <circle cx="148" cy="54" r="16" class="fig-box-red"/>
    <circle cx="218" cy="62" r="16" class="fig-box-red"/>
    <circle cx="104" cy="108" r="16" class="fig-box-red"/>
    <circle cx="192" cy="108" r="16" class="fig-box-red"/>
    <line x1="32" y1="226" x2="330" y2="226" class="fig-line-accent" stroke-width="3.5"/>
    <line x1="74" y1="194" x2="74" y2="224" class="fig-line" stroke="var(--sl-color-green)" marker-end="url(#lhw-w2-arr-1)"/>
    <line x1="176" y1="224" x2="176" y2="194" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w2-arr-1)"/>
    <line x1="278" y1="194" x2="278" y2="224" class="fig-line-dashed"/>
    <rect x="34" y="164" width="80" height="30" rx="5" class="fig-box-green"/>
    <rect x="136" y="164" width="80" height="30" rx="5" class="fig-box-blue"/>
    <rect x="238" y="164" width="82" height="30" rx="5" class="fig-box"/>
    <line x1="448" y1="68" x2="582" y2="68" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w2-arr-1)"/>
    <line x1="448" y1="84" x2="582" y2="84" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w2-arr-1)"/>
    <line x1="448" y1="100" x2="582" y2="100" class="fig-line-dashed"/>
    <line x1="448" y1="116" x2="582" y2="116" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w2-arr-1)"/>
    <rect x="374" y="60" width="74" height="64" rx="5" class="fig-box-blue"/>
    <rect x="584" y="60" width="84" height="64" rx="5" class="fig-box-blue"/>
    <line x1="464" y1="198" x2="574" y2="198" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-1)"/>
    <rect x="374" y="174" width="90" height="48" rx="5" class="fig-box-green"/>
    <rect x="576" y="174" width="92" height="48" rx="5" class="fig-box-green"/>
    <rect x="472" y="180" width="96" height="22" rx="4" class="fig-box"/>
    <text x="30" y="34" class="fig-title" font-size="12.5">（a）互连演进：分散直连 vs 公共总线（分时）</text>
    <text x="78" y="66" text-anchor="middle" class="fig-label" font-size="10.5">运算</text>
    <text x="148" y="58" text-anchor="middle" class="fig-label" font-size="10.5">控制</text>
    <text x="218" y="66" text-anchor="middle" class="fig-label" font-size="10.5">主存</text>
    <text x="104" y="112" text-anchor="middle" class="fig-label" font-size="10.5">输入</text>
    <text x="192" y="112" text-anchor="middle" class="fig-label" font-size="10.5">输出</text>
    <text x="240" y="76" class="fig-red" font-size="10">分散全互连：</text>
    <text x="240" y="94" class="fig-muted" font-size="10">n(n-1)/2 = 10 组</text>
    <text x="240" y="112" class="fig-muted" font-size="10">增1设备需+n组线</text>
    <text x="74" y="183" text-anchor="middle" class="fig-green" font-size="11">发送方 EN=1</text>
    <text x="176" y="183" text-anchor="middle" class="fig-blue" font-size="11">接收方 采样</text>
    <text x="279" y="183" text-anchor="middle" class="fig-muted" font-size="11">其余 Hi-Z</text>
    <text x="181" y="246" text-anchor="middle" class="fig-accent" font-size="11.5">公共传输总线（三态门隔离，任一时刻仅一对通信）</text>
    <text x="181" y="262" text-anchor="middle" class="fig-muted" font-size="10.5">核心特征：“公共”挂接（易扩展）+ “分时”独占（易成瓶颈）</text>
    <text x="372" y="34" class="fig-title" font-size="12.5">（b）数据传送方式：并行传输 vs 串行传输</text>
    <text x="411" y="88" text-anchor="middle" class="fig-label" font-size="11.5">发送端</text>
    <text x="411" y="106" text-anchor="middle" class="fig-muted" font-size="10.5">并行接口</text>
    <text x="626" y="88" text-anchor="middle" class="fig-label" font-size="11.5">接收端</text>
    <text x="626" y="106" text-anchor="middle" class="fig-muted" font-size="10.5">并行接口</text>
    <text x="515" y="63" text-anchor="middle" class="fig-blue" font-size="10.5">D7 (MSB)</text>
    <text x="515" y="80" text-anchor="middle" class="fig-blue" font-size="10.5">D6</text>
    <text x="515" y="112" text-anchor="middle" class="fig-blue" font-size="10.5">D0 (LSB)</text>
    <text x="515" y="135" text-anchor="middle" class="fig-orange" font-size="10.5">并行：多位齐发，短距机箱内；长距有串扰与偏移</text>
    <text x="419" y="195" text-anchor="middle" class="fig-label" font-size="11">并→串移位</text>
    <text x="419" y="212" text-anchor="middle" class="fig-muted" font-size="10">10110010</text>
    <text x="622" y="195" text-anchor="middle" class="fig-label" font-size="11">串→并移位</text>
    <text x="622" y="212" text-anchor="middle" class="fig-muted" font-size="10">10110010</text>
    <text x="520" y="195" text-anchor="middle" class="fig-green" font-size="10.5">b0→b1→…→b7</text>
    <text x="520" y="242" text-anchor="middle" class="fig-green" font-size="10.5">串行：1对差分线逐位传，无多线偏移，支持高频远距</text>
    <text x="520" y="259" text-anchor="middle" class="fig-muted" font-size="10.5">典型代表：USB、RS-232、PCIe、SATA</text>
  </svg>
  <figcaption>图 3-1　总线互连原理与传输方式对比：（a）三态缓冲器实现“公共挂接、分时共享”；（b）并行与串行传输物理特性</figcaption>
</figure>

<strong>单总线示例的问题</strong>：CPU、主存、I/O 全部挂在一条系统总线上。I/O 与主存之间传数据时，CPU 无法访存，程序执行停顿；总线较长时远端设备延迟大；多设备还会争用总线。改进方向是引入多条总线，让不同传输并行。

### 3.2 总线的分类

按<strong>总线位置</strong>划分（教材标准）：

<figure class="cpd-figure not-content" id="lhw-w2-fig-2">
  <svg viewBox="0 0 700 306" role="img" aria-label="总线按位置分类层次树与系统总线三类信号方向示意图">
    <defs>
      <marker id="lhw-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="14" width="352" height="278" rx="8" class="fig-box-subtle"/>
    <rect x="380" y="14" width="304" height="278" rx="8" class="fig-box-subtle"/>
    <line x1="192" y1="74" x2="192" y2="94" class="fig-line"/>
    <line x1="76" y1="94" x2="308" y2="94" class="fig-line"/>
    <line x1="76" y1="94" x2="76" y2="110" class="fig-line"/>
    <line x1="192" y1="94" x2="192" y2="110" class="fig-line-accent"/>
    <line x1="308" y1="94" x2="308" y2="110" class="fig-line"/>
    <line x1="192" y1="146" x2="192" y2="188" class="fig-line-accent"/>
    <line x1="80" y1="188" x2="304" y2="188" class="fig-line-accent"/>
    <line x1="80" y1="188" x2="80" y2="204" class="fig-line-accent"/>
    <line x1="192" y1="188" x2="192" y2="204" class="fig-line-accent"/>
    <line x1="304" y1="188" x2="304" y2="204" class="fig-line-accent"/>
    <rect x="142" y="42" width="100" height="32" rx="5" class="fig-box-accent"/>
    <rect x="28" y="110" width="96" height="36" rx="5" class="fig-box"/>
    <rect x="138" y="110" width="108" height="36" rx="5" class="fig-box-blue"/>
    <rect x="260" y="110" width="96" height="36" rx="5" class="fig-box"/>
    <rect x="32" y="204" width="96" height="34" rx="5" class="fig-box-green"/>
    <rect x="144" y="204" width="96" height="34" rx="5" class="fig-box-blue"/>
    <rect x="256" y="204" width="96" height="34" rx="5" class="fig-box-purple"/>
    <line x1="478" y1="88" x2="584" y2="88" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2" marker-end="url(#lhw-w2-arr-2)"/>
    <line x1="486" y1="150" x2="584" y2="150" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-start="url(#lhw-w2-arr-2)" marker-end="url(#lhw-w2-arr-2)"/>
    <line x1="478" y1="206" x2="584" y2="206" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#lhw-w2-arr-2)"/>
    <line x1="592" y1="226" x2="486" y2="226" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#lhw-w2-arr-2)"/>
    <rect x="394" y="50" width="84" height="202" rx="6" class="fig-box"/>
    <rect x="404" y="72" width="64" height="32" rx="4" class="fig-box-blue"/>
    <rect x="404" y="134" width="64" height="32" rx="4" class="fig-box-green"/>
    <rect x="404" y="196" width="64" height="42" rx="4" class="fig-box-purple"/>
    <rect x="592" y="50" width="78" height="202" rx="6" class="fig-box"/>
    <text x="30" y="32" class="fig-title" font-size="12.5">（a）按连接位置与传输信息的标准分类</text>
    <text x="192" y="63" text-anchor="middle" class="fig-accent" font-size="13">总 线</text>
    <text x="76" y="133" text-anchor="middle" class="fig-label" font-size="12">片内总线</text>
    <text x="192" y="133" text-anchor="middle" class="fig-blue" font-size="12">系统总线</text>
    <text x="308" y="133" text-anchor="middle" class="fig-label" font-size="12">通信总线</text>
    <text x="76" y="162" text-anchor="middle" class="fig-muted" font-size="10.5">CPU 芯片内部</text>
    <text x="76" y="176" text-anchor="middle" class="fig-muted" font-size="10.5">寄存器↔ALU</text>
    <text x="308" y="162" text-anchor="middle" class="fig-muted" font-size="10.5">机间 / 跨系统</text>
    <text x="308" y="176" text-anchor="middle" class="fig-muted" font-size="10.5">串行或并行</text>
    <text x="80" y="226" text-anchor="middle" class="fig-green" font-size="12">数据总线</text>
    <text x="192" y="226" text-anchor="middle" class="fig-blue" font-size="12">地址总线</text>
    <text x="304" y="226" text-anchor="middle" class="fig-purple" font-size="12">控制总线</text>
    <text x="80" y="256" text-anchor="middle" class="fig-muted" font-size="10.5">双向传输</text>
    <text x="80" y="272" text-anchor="middle" class="fig-muted" font-size="10.5">宽度 ≤ 机器字长</text>
    <text x="192" y="256" text-anchor="middle" class="fig-muted" font-size="10.5">单向（主模块发）</text>
    <text x="192" y="272" text-anchor="middle" class="fig-muted" font-size="10.5">m 根寻址 2^m</text>
    <text x="304" y="256" text-anchor="middle" class="fig-muted" font-size="10.5">整体“有出有入”</text>
    <text x="304" y="272" text-anchor="middle" class="fig-muted" font-size="10.5">单根信号线单向</text>
    <text x="394" y="32" class="fig-title" font-size="12.5">（b）系统总线三类信号硬件流向</text>
    <text x="436" y="65" text-anchor="middle" class="fig-muted" font-size="10">CPU / 主设备</text>
    <text x="436" y="92" text-anchor="middle" class="fig-blue" font-size="11.5">MAR</text>
    <text x="436" y="154" text-anchor="middle" class="fig-green" font-size="11.5">MDR</text>
    <text x="436" y="215" text-anchor="middle" class="fig-purple" font-size="11">CU 控制</text>
    <text x="436" y="230" text-anchor="middle" class="fig-muted" font-size="9.5">命令/状态</text>
    <text x="631" y="128" text-anchor="middle" class="fig-label" font-size="12">主存</text>
    <text x="631" y="148" text-anchor="middle" class="fig-muted" font-size="11">或</text>
    <text x="631" y="168" text-anchor="middle" class="fig-label" font-size="12">I/O 接口</text>
    <text x="631" y="190" text-anchor="middle" class="fig-muted" font-size="10">（从设备）</text>
    <text x="535" y="80" text-anchor="middle" class="fig-blue" font-size="10.2">AB 地址（单向）</text>
    <text x="535" y="142" text-anchor="middle" class="fig-green" font-size="10.2">DB 数据（双向）</text>
    <text x="535" y="199" text-anchor="middle" class="fig-purple" font-size="10">R/W、M/IO 命令</text>
    <text x="535" y="242" text-anchor="middle" class="fig-orange" font-size="10">INTR、READY 反馈</text>
    <text x="532" y="274" text-anchor="middle" class="fig-muted" font-size="10.5">注：64位机配8位数据总线时，1个机器字需分8次传送</text>
  </svg>
  <figcaption>图 3-2　总线分类体系与系统总线三类信号线方向（数据双向、地址单向、控制整体有出有入）</figcaption>
</figure>

<strong>系统总线三类信号</strong>：

| 类别 | 方向 | 宽度与内容 |
|---|---|---|
| 数据总线 | 双向 | 根数即总线宽度；通常小于或等于机器字长/存储字长，不一定相等。64 位机器若数据总线 8 位，传一个 64 位数据要 8 次 |
| 地址总线 | 单向，由 CPU 或主设备发出 | 决定可寻址的存储单元与 I/O 端口数；模型机中宽度与 MAR 相同，$m$ 根可寻址 $2^{m}$ 个单元 |
| 控制总线 | 有出有入 | 存储器读/写、总线使用权许可、中断请求与响应、总线请求等 |

- <strong>片内总线</strong>：芯片内部各部件之间的连接。
- <strong>系统总线</strong>：计算机各部件之间的信息传输，是本章主体。
- <strong>通信总线</strong>：计算机与计算机、计算机与其他系统/设备之间，构成更大规模系统；有串行、并行两种传输方式。

> <strong>注意</strong>：数据总线宽度与机器字长不是同一概念。字长是 CPU 一次处理的位数，总线宽度是一次能传的位数，前者可以大于后者。

### 3.3 总线的特性与性能指标

<strong>总线在机器中的实现</strong>：总线印刷在主板上，主板上留出插槽，CPU 板、存储板、I/O 板插入插槽，通过总线互连。为保证插板与总线可靠连接，总线具有四种特性：

- <strong>机械特性</strong>：尺寸、形状、引脚数量与排列次序，两块板必须一致才能插接。
- <strong>电气特性</strong>：信号传输方向（数据线双向、地址线单向）、有效电平范围（高/低电平对应的电压区间）。
- <strong>功能特性</strong>：每根线传输什么信号（地址、数据、控制、状态反馈等）。
- <strong>时间特性</strong>：各信号之间的时序关系，即有效电平持续的先后次序。

<strong>主要性能指标</strong>：

- <strong>总线宽度</strong>：数据线的根数，即一次能并行传送的位数。
- <strong>标准传输率（带宽）</strong>：总线满负荷时每秒传送的最大字节数。
- <strong>时钟同步方式</strong>：同步或不同步（不同步还包含多种形式，所以不直接称“异步”）。
- <strong>总线复用</strong>：同一组线分时传不同信息，典型是地址线与数据线复用。
- <strong>信号线总数</strong>：地址线、数据线、控制线数量之和。
- <strong>总线控制方式</strong>：判优与通信控制方式。
- <strong>负载能力</strong>：总线上可挂接的设备数量。

带宽计算：设总线宽度为 $W$ 字节、工作频率为 $f$，则

$$
B = W \times f
$$

数据线 $n$ 根时，宽度换算为

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

若一次数据传输占 $k$ 个时钟周期，则传输周期为

$$
T = \frac{k}{f}
$$

实际带宽为

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

例：16 位数据线、8 MHz 时钟、每周期传一次，则 $2\text{ B} \times 8\text{ MHz} = 16\text{ MB/s}$。

<strong>总线复用</strong>：8086 有 20 根地址线，其中 16 根与数据线复用。传数据时 CPU 先送地址并锁存，之后这 16 根线改传数据。复用目的是<strong>减少芯片引脚数</strong>，引脚少则封装体积小；芯片核心裸片很小，引脚数量对封装尺寸影响很大。

<figure class="cpd-figure not-content" id="lhw-w2-fig-3">
  <svg viewBox="0 0 700 286" role="img" aria-label="总线带宽计算公式时序模型与8086地址数据分时复用锁存电路示意图">
    <defs>
      <marker id="lhw-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="14" width="336" height="258" rx="8" class="fig-box-subtle"/>
    <rect x="362" y="14" width="322" height="258" rx="8" class="fig-box-subtle"/>
    <line x1="88" y1="54" x2="88" y2="148" class="fig-line-dashed"/>
    <line x1="158" y1="54" x2="158" y2="148" class="fig-line-dashed"/>
    <line x1="228" y1="54" x2="228" y2="148" class="fig-line-dashed"/>
    <line x1="298" y1="54" x2="298" y2="148" class="fig-line-dashed"/>
    <path d="M 88 86 L 88 66 L 123 66 L 123 86 L 158 86 L 158 66 L 193 66 L 193 86 L 228 86 L 228 66 L 263 66 L 263 86 L 298 86" class="fig-line"/>
    <line x1="88" y1="142" x2="298" y2="142" class="fig-line-accent" marker-start="url(#lhw-w2-arr-3)" marker-end="url(#lhw-w2-arr-3)"/>
    <rect x="92" y="98" width="202" height="28" rx="4" class="fig-box-blue"/>
    <rect x="132" y="132" width="122" height="20" rx="4" class="fig-box"/>
    <rect x="24" y="164" width="320" height="94" rx="6" class="fig-box"/>
    <line x1="438" y1="68" x2="670" y2="68" class="fig-line" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w2-arr-3)"/>
    <polyline points="438,102 508,102 508,116" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#lhw-w2-arr-3)"/>
    <line x1="438" y1="138" x2="468" y2="138" class="fig-line" stroke="var(--sl-color-accent)" stroke-width="2" marker-end="url(#lhw-w2-arr-3)"/>
    <line x1="546" y1="138" x2="670" y2="138" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2" marker-end="url(#lhw-w2-arr-3)"/>
    <polyline points="454,144 454,185 670,185" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-start="url(#lhw-w2-arr-3)" marker-end="url(#lhw-w2-arr-3)"/>
    <circle cx="454" cy="138" r="3" class="fig-dot"/>
    <rect x="368" y="54" width="70" height="106" rx="5" class="fig-box-accent"/>
    <rect x="470" y="118" width="76" height="40" rx="5" class="fig-box-orange"/>
    <rect x="372" y="204" width="298" height="54" rx="5" class="fig-box"/>
    <path d="M 454 228 L 468 228 L 468 214 L 502 214 L 502 228 L 652 228" class="fig-line" stroke="var(--sl-color-orange)"/>
    <rect x="462" y="234" width="76" height="18" rx="3" class="fig-box-blue"/>
    <rect x="546" y="234" width="106" height="18" rx="3" class="fig-box-green"/>
    <text x="28" y="34" class="fig-title" font-size="12">（a）带宽公式：B = W·f / k = n·f / (8k)</text>
    <text x="34" y="80" class="fig-muted" font-size="11">CLK(f)</text>
    <text x="34" y="116" class="fig-blue" font-size="11">DB(n根)</text>
    <text x="123" y="58" text-anchor="middle" class="fig-muted" font-size="10.5">T_clk=1/f</text>
    <text x="193" y="116" text-anchor="middle" class="fig-blue" font-size="11">1 次并行传送 W = n / 8 字节</text>
    <text x="193" y="146" text-anchor="middle" class="fig-accent" font-size="10.5">总线周期 T = k / f</text>
    <text x="32" y="184" class="fig-label" font-size="9.6">• ISA 标准：n=16b (2B), f=8MHz, k=1 → 16 MB/s</text>
    <text x="32" y="204" class="fig-green" font-size="9.6">• EISA 标准：n=32b (4B), f=8MHz, k=1 → 32 MB/s</text>
    <text x="32" y="224" class="fig-orange" font-size="9.6">• 多周期实例：n=32b, f=33MHz, k=3 → 44 MB/s</text>
    <text x="32" y="245" class="fig-muted" font-size="9.2">注：若 k 个时钟周期传 1 次数据，带宽必除以 k</text>
    <text x="374" y="34" class="fig-title" font-size="12">（b）8086 地址/数据分时复用（省引脚减体积）</text>
    <text x="403" y="76" text-anchor="middle" class="fig-accent" font-size="11.5">8086</text>
    <text x="403" y="96" text-anchor="middle" class="fig-label" font-size="11">CPU</text>
    <text x="403" y="118" text-anchor="middle" class="fig-muted" font-size="9.5">20位寻址</text>
    <text x="403" y="134" text-anchor="middle" class="fig-muted" font-size="9.5">16位数据</text>
    <text x="556" y="61" text-anchor="middle" class="fig-blue" font-size="10">高4位地址 A19～A16</text>
    <text x="476" y="96" text-anchor="middle" class="fig-orange" font-size="10">ALE 锁存</text>
    <text x="508" y="136" text-anchor="middle" class="fig-orange" font-size="10.2">地址锁存器</text>
    <text x="508" y="150" text-anchor="middle" class="fig-muted" font-size="9.2">8282/373</text>
    <text x="614" y="131" text-anchor="middle" class="fig-blue" font-size="9.5">A15～A0 稳定地址</text>
    <text x="568" y="177" text-anchor="middle" class="fig-green" font-size="9.5">AD15～AD0 后半段作双向 D15～D0</text>
    <text x="384" y="224" class="fig-orange" font-size="10.5">ALE</text>
    <text x="384" y="247" class="fig-label" font-size="10.5">AD 线</text>
    <text x="500" y="247" text-anchor="middle" class="fig-blue" font-size="10">① 先送地址</text>
    <text x="599" y="247" text-anchor="middle" class="fig-green" font-size="10">② 再传读写数据</text>
  </svg>
  <figcaption>图 3-3　总线带宽周期计算模型与 8086 地址/数据线分时复用（ALE 锁存减少 16 根芯片引脚）</figcaption>
</figure>

### 3.4 总线标准

不同厂家分别生产 CPU、内存、I/O 设备，只要共同遵守总线的约定（标准界面），部件就能集成为一个协调运行的计算机系统。总线标准经历了从低速、简单向高速、高可扩展性的发展，部分老标准已被淘汰。

| 标准 | 含义 | 数据宽度 | 时钟 | 带宽/特点 |
|---|---|---|---|---|
| ISA | Industry Standard Architecture，工业标准体系结构 | 16 位 | 8 MHz | 16 MB/s |
| EISA | Extended ISA，对 ISA 的扩展，兼容 ISA | 32 位 | 8 MHz | 32 MB/s，带宽翻倍 |
| VESA（VL-Bus） | 视频电子标准协会（1989 年成立，140 多家成员，非盈利，总部加州） | — | — | 面向 PC 高速活动图像；信号直接取自 CPU，驱动能力差 |
| PCI | Peripheral Component Interconnect，外设部件互联 | — | — | 独立于处理器，中间缓冲器式设计，CPU 子系统与外设分开；现微机主流插槽 |
| AGP | Intel 1996 年 7 月推出，点对点局部总线 | — | — | 连接控制芯片与显卡，由 PCI 发展而来，最高 533 MB/s |
| RS-232 | 推荐标准串行通信总线 | 串行 | — | 常用串行通信标准 |
| USB | 通用串行总线 | 串行 | — | 外设连接 |

> <strong>★考点</strong>：ISA 16 位/8 MHz → 16 MB/s，EISA 32 位/8 MHz → 32 MB/s，是带宽公式的直接应用。PCI 的关键词是“独立于处理器、中间缓冲”。

### 3.5 总线结构

<strong>单总线结构</strong>：CPU、主存、I/O 接口全部挂在一条系统总线上。优点是简单、易扩展；缺点是同一时刻只能一对部件通信，总线成为瓶颈。I/O 与主存交换数据时 CPU 只能等待；总线越长，远端访问延迟越大。

<figure class="cpd-figure not-content" id="lhw-w2-fig-4">
  <svg viewBox="0 0 700 268" role="img" aria-label="单总线结构部件挂接方式与三大性能瓶颈示意图">
    <defs>
      <marker id="lhw-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="14" width="668" height="240" rx="8" class="fig-box-subtle"/>
    <line x1="36" y1="128" x2="664" y2="128" class="fig-line-accent" stroke-width="4"/>
    <line x1="96" y1="84" x2="96" y2="124" class="fig-line" marker-start="url(#lhw-w2-arr-4)" marker-end="url(#lhw-w2-arr-4)"/>
    <line x1="252" y1="84" x2="252" y2="124" class="fig-line" marker-start="url(#lhw-w2-arr-4)" marker-end="url(#lhw-w2-arr-4)"/>
    <line x1="428" y1="84" x2="428" y2="124" class="fig-line" marker-start="url(#lhw-w2-arr-4)" marker-end="url(#lhw-w2-arr-4)"/>
    <line x1="592" y1="84" x2="592" y2="124" class="fig-line" marker-start="url(#lhw-w2-arr-4)" marker-end="url(#lhw-w2-arr-4)"/>
    <path d="M 264 122 Q 340 96 416 122" class="fig-line" stroke="var(--sl-color-red)" stroke-dasharray="5 3" marker-start="url(#lhw-w2-arr-4)" marker-end="url(#lhw-w2-arr-4)"/>
    <rect x="46" y="42" width="100" height="42" rx="5" class="fig-box-orange"/>
    <rect x="200" y="42" width="104" height="42" rx="5" class="fig-box-blue"/>
    <rect x="372" y="42" width="112" height="42" rx="5" class="fig-box"/>
    <rect x="536" y="42" width="112" height="42" rx="5" class="fig-box"/>
    <rect x="304" y="96" width="76" height="18" rx="3" class="fig-box-red"/>
    <rect x="36" y="160" width="390" height="80" rx="6" class="fig-box-red"/>
    <rect x="440" y="160" width="224" height="80" rx="6" class="fig-box-green"/>
    <text x="96" y="62" text-anchor="middle" class="fig-orange" font-size="12.5">CPU</text>
    <text x="96" y="77" text-anchor="middle" class="fig-muted" font-size="10">被迫停顿等待</text>
    <text x="252" y="67" text-anchor="middle" class="fig-blue" font-size="12.5">主存储器</text>
    <text x="428" y="61" text-anchor="middle" class="fig-label" font-size="11.5">I/O 接口 1</text>
    <text x="428" y="77" text-anchor="middle" class="fig-muted" font-size="10">接高速磁盘</text>
    <text x="592" y="61" text-anchor="middle" class="fig-label" font-size="11.5">I/O 接口 n</text>
    <text x="592" y="77" text-anchor="middle" class="fig-muted" font-size="10">远端慢速外设</text>
    <text x="342" y="109" text-anchor="middle" class="fig-red" font-size="10">独占传数中</text>
    <text x="350" y="146" text-anchor="middle" class="fig-accent" font-size="12">单一系统总线（地址 AB + 数据 DB + 控制 CB）</text>
    <text x="50" y="181" class="fig-red" font-size="11.5">三大瓶颈（推动多总线演进）：</text>
    <text x="50" y="200" class="fig-label" font-size="11">① 任一时刻仅一对通信：I/O↔主存交换时 CPU 无法取指访存</text>
    <text x="50" y="218" class="fig-label" font-size="11">② 物理总线过长 → 远端信号传播延迟大；③ 高低速外设争用带宽</text>
    <text x="454" y="181" class="fig-green" font-size="11.5">突出优点：</text>
    <text x="454" y="202" class="fig-label" font-size="11">• 结构极简，控制逻辑统一</text>
    <text x="454" y="221" class="fig-label" font-size="11">• 接口标准化，增删外设容易</text>
  </svg>
  <figcaption>图 3-4　单总线结构：所有部件共挂同一系统总线，接口易扩展但访存与 I/O 严重争用成为系统瓶颈</figcaption>
</figure>

<strong>双总线结构（以 CPU 为中心）</strong>：CPU 引出两条总线，M 总线连主存、I/O 总线连外设。CPU 与主存之间信息交换最频繁，专用 M 总线改善了这一点。但主存与外设之间没有直接通路，仍需 CPU 作媒介，传输时 CPU 程序执行被打断。

<strong>双总线结构（以存储器为中心）</strong>：主存引出存储总线与系统总线，CPU 挂在存储总线上，外设经 I/O 接口挂在系统总线上，CPU 同时也挂在系统总线上。CPU–主存走专用存储总线，CPU–外设与主存–外设走系统总线。局限是两条总线通常仍不能真正同时工作：双端口 RAM 给两个端口送数据目前仍是分时进行。

<figure class="cpd-figure not-content" id="lhw-w2-fig-5">
  <svg viewBox="0 0 700 280" role="img" aria-label="双总线结构的三种典型方案对比图">
    <defs>
      <marker id="lhw-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="14" width="214" height="252" rx="8" class="fig-box-subtle"/>
    <rect x="242" y="14" width="216" height="252" rx="8" class="fig-box-subtle"/>
    <rect x="470" y="14" width="214" height="252" rx="8" class="fig-box-subtle"/>
    <polyline points="96,86 96,118 68,118 68,144" class="fig-line-accent" stroke-width="2" marker-end="url(#lhw-w2-arr-5)"/>
    <polyline points="150,86 150,118 178,118 178,144" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2" marker-end="url(#lhw-w2-arr-5)"/>
    <path d="M 68 186 Q 123 212 178 186" class="fig-line-dashed"/>
    <rect x="82" y="48" width="82" height="38" rx="5" class="fig-box-orange"/>
    <rect x="28" y="146" width="80" height="38" rx="5" class="fig-box-blue"/>
    <rect x="138" y="146" width="80" height="38" rx="5" class="fig-box"/>
    <polyline points="320,86 320,118 288,118 288,144" class="fig-line-accent" stroke-width="2" marker-end="url(#lhw-w2-arr-5)"/>
    <polyline points="380,86 380,118 412,118 412,144" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-5)"/>
    <line x1="324" y1="168" x2="376" y2="168" class="fig-line-dashed"/>
    <rect x="304" y="48" width="92" height="38" rx="5" class="fig-box-blue"/>
    <rect x="250" y="146" width="74" height="38" rx="5" class="fig-box"/>
    <rect x="376" y="146" width="74" height="38" rx="5" class="fig-box"/>
    <line x1="486" y1="88" x2="668" y2="88" class="fig-line-accent" stroke-width="3"/>
    <line x1="522" y1="70" x2="522" y2="88" class="fig-line"/>
    <line x1="632" y1="70" x2="632" y2="88" class="fig-line"/>
    <line x1="577" y1="88" x2="577" y2="112" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-5)"/>
    <line x1="577" y1="150" x2="577" y2="176" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-5)"/>
    <line x1="486" y1="176" x2="668" y2="176" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="3"/>
    <line x1="526" y1="176" x2="526" y2="192" class="fig-line"/>
    <line x1="628" y1="176" x2="628" y2="192" class="fig-line"/>
    <rect x="490" y="42" width="64" height="28" rx="4" class="fig-box"/>
    <rect x="598" y="42" width="68" height="28" rx="4" class="fig-box-blue"/>
    <rect x="530" y="114" width="94" height="36" rx="5" class="fig-box-green"/>
    <rect x="488" y="192" width="76" height="28" rx="4" class="fig-box"/>
    <rect x="590" y="192" width="76" height="28" rx="4" class="fig-box"/>
    <text x="28" y="32" class="fig-title" font-size="12">（a）以 CPU 为中心</text>
    <text x="123" y="71" text-anchor="middle" class="fig-orange" font-size="12">CPU</text>
    <text x="58" y="112" text-anchor="middle" class="fig-accent" font-size="10.5">M 总线</text>
    <text x="188" y="112" text-anchor="middle" class="fig-blue" font-size="10.5">I/O 总线</text>
    <text x="68" y="169" text-anchor="middle" class="fig-blue" font-size="11.5">主存</text>
    <text x="178" y="169" text-anchor="middle" class="fig-label" font-size="11.5">外设</text>
    <text x="123" y="222" text-anchor="middle" class="fig-red" font-size="10.5">无直接通路：I/O↔主存</text>
    <text x="123" y="239" text-anchor="middle" class="fig-muted" font-size="10.5">须由 CPU 中转，打断程序</text>
    <text x="254" y="32" class="fig-title" font-size="12">（b）以存储器为中心</text>
    <text x="350" y="71" text-anchor="middle" class="fig-blue" font-size="12">主存</text>
    <text x="284" y="112" text-anchor="middle" class="fig-accent" font-size="10.5">存储总线</text>
    <text x="414" y="112" text-anchor="middle" class="fig-green" font-size="10.5">系统总线</text>
    <text x="287" y="169" text-anchor="middle" class="fig-label" font-size="11.5">CPU</text>
    <text x="413" y="169" text-anchor="middle" class="fig-label" font-size="10.8">I/O 接口</text>
    <text x="350" y="159" text-anchor="middle" class="fig-muted" font-size="8.6">亦接系统</text>
    <text x="350" y="222" text-anchor="middle" class="fig-orange" font-size="10.5">CPU-主存专用存储总线</text>
    <text x="350" y="239" text-anchor="middle" class="fig-muted" font-size="10.5">双端口 RAM 仍分时工作</text>
    <text x="482" y="32" class="fig-title" font-size="12">（c）存储/I/O 经通道分离</text>
    <text x="522" y="60" text-anchor="middle" class="fig-label" font-size="11">CPU</text>
    <text x="632" y="60" text-anchor="middle" class="fig-blue" font-size="11">主存</text>
    <text x="577" y="82" text-anchor="middle" class="fig-accent" font-size="10">存储总线</text>
    <text x="577" y="131" text-anchor="middle" class="fig-green" font-size="11">通道 Channel</text>
    <text x="577" y="144" text-anchor="middle" class="fig-muted" font-size="9.5">专用 I/O 处理器</text>
    <text x="530" y="170" text-anchor="middle" class="fig-blue" font-size="10">I/O 总线</text>
    <text x="526" y="210" text-anchor="middle" class="fig-label" font-size="10.5">外设 1</text>
    <text x="628" y="210" text-anchor="middle" class="fig-label" font-size="10.5">外设 n</text>
    <text x="577" y="242" text-anchor="middle" class="fig-green" font-size="10.5">执行 OS 通道程序管理 I/O</text>
  </svg>
  <figcaption>图 3-5　三种双总线结构演进：（a）以 CPU 为中心；（b）以存储器为中心；（c）经通道连接存储总线与 I/O 总线</figcaption>
</figure>

<strong>双总线（存储总线与 I/O 总线分离，经通道连接）</strong>：存储总线连 CPU 与主存，I/O 总线连各类 I/O 设备，中间用<strong>通道</strong>连接，实现外设与主存、外设与 CPU 的通信。通道是专门用于输入输出的简易处理器，有自己的控制器和指令系统，能执行通道程序；通道程序通常由操作系统编写，不是人工编写。

<strong>三总线结构（增加 DMA 总线）</strong>：主存总线、I/O 总线之外再加 DMA 总线。高速外设与主存之间经 DMA 总线直接传送，不必经过 I/O 总线由 CPU 中转；低速设备与主存交换仍走 I/O 总线。

<strong>三总线结构（局部总线 + 系统总线 + 扩展总线）</strong>：CPU 与 Cache 之间用局部总线连接，局部 I/O 控制器可接高速局部设备；系统总线连接主存等；系统总线经扩展总线接口接扩展总线，各类设备挂在扩展总线上。这解决了 I/O 扩展问题，但不同速度的设备混在扩展总线上会互相拖慢。

Cache 的引入背景：1985—2002 年 CPU 性能约每年提高 52%，而存储器延迟约每 10 年才减半，内存成为瓶颈。于是在 CPU 与内存间加小容量高速 Cache，CPU 运行所需指令和数据主要取自 Cache，由局部总线连接 CPU 与 Cache。

<figure class="cpd-figure not-content" id="lhw-w2-fig-6">
  <svg viewBox="0 0 700 264" role="img" aria-label="三总线结构的两种典型组织方式对比图">
    <defs>
      <marker id="lhw-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="14" width="328" height="236" rx="8" class="fig-box-subtle"/>
    <rect x="356" y="14" width="328" height="236" rx="8" class="fig-box-subtle"/>
    <line x1="108" y1="68" x2="218" y2="68" class="fig-line-accent" stroke-width="3"/>
    <line x1="72" y1="86" x2="72" y2="162" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.5"/>
    <line x1="34" y1="162" x2="176" y2="162" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="3"/>
    <line x1="258" y1="86" x2="258" y2="162" class="fig-line" stroke="var(--sl-color-green)" stroke-width="3" marker-start="url(#lhw-w2-arr-6)" marker-end="url(#lhw-w2-arr-6)"/>
    <line x1="88" y1="162" x2="88" y2="180" class="fig-line"/>
    <rect x="36" y="48" width="72" height="38" rx="5" class="fig-box"/>
    <rect x="218" y="48" width="88" height="38" rx="5" class="fig-box-blue"/>
    <rect x="44" y="180" width="96" height="34" rx="5" class="fig-box"/>
    <rect x="206" y="164" width="108" height="48" rx="5" class="fig-box-green"/>
    <line x1="372" y1="84" x2="668" y2="84" class="fig-line-accent" stroke-width="3"/>
    <line x1="408" y1="70" x2="408" y2="84" class="fig-line"/>
    <line x1="504" y1="70" x2="504" y2="84" class="fig-line"/>
    <line x1="504" y1="84" x2="504" y2="132" class="fig-line"/>
    <line x1="372" y1="132" x2="668" y2="132" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="3"/>
    <line x1="422" y1="132" x2="422" y2="150" class="fig-line"/>
    <line x1="576" y1="132" x2="576" y2="146" class="fig-line"/>
    <line x1="576" y1="176" x2="576" y2="196" class="fig-line"/>
    <line x1="478" y1="196" x2="668" y2="196" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="3"/>
    <line x1="522" y1="196" x2="522" y2="208" class="fig-line"/>
    <line x1="624" y1="196" x2="624" y2="208" class="fig-line"/>
    <rect x="376" y="42" width="64" height="28" rx="4" class="fig-box"/>
    <rect x="468" y="42" width="72" height="28" rx="4" class="fig-box-accent"/>
    <rect x="380" y="150" width="84" height="32" rx="4" class="fig-box-blue"/>
    <rect x="524" y="146" width="104" height="30" rx="4" class="fig-box-purple"/>
    <rect x="480" y="208" width="84" height="28" rx="4" class="fig-box-orange"/>
    <rect x="582" y="208" width="84" height="28" rx="4" class="fig-box"/>
    <text x="30" y="32" class="fig-title" font-size="12">（a）主存 + I/O + DMA 三总线</text>
    <text x="72" y="72" text-anchor="middle" class="fig-label" font-size="12">CPU</text>
    <text x="262" y="72" text-anchor="middle" class="fig-blue" font-size="12">主存</text>
    <text x="163" y="60" text-anchor="middle" class="fig-accent" font-size="10.5">主存总线</text>
    <text x="118" y="154" text-anchor="middle" class="fig-blue" font-size="10.5">I/O 总线</text>
    <text x="216" y="128" text-anchor="middle" class="fig-green" font-size="10.5">DMA 总线</text>
    <text x="92" y="201" text-anchor="middle" class="fig-label" font-size="11">低速外设</text>
    <text x="260" y="185" text-anchor="middle" class="fig-green" font-size="11">高速外设</text>
    <text x="260" y="201" text-anchor="middle" class="fig-muted" font-size="9.5">直传主存免中转</text>
    <text x="180" y="236" text-anchor="middle" class="fig-muted" font-size="10.5">高速块设备走 DMA 总线，释放 CPU 周期</text>
    <text x="370" y="32" class="fig-title" font-size="12">（b）局部 + 系统 + 扩展三总线</text>
    <text x="408" y="60" text-anchor="middle" class="fig-label" font-size="11">CPU</text>
    <text x="504" y="60" text-anchor="middle" class="fig-accent" font-size="11">Cache</text>
    <text x="614" y="77" text-anchor="middle" class="fig-accent" font-size="10">局部总线</text>
    <text x="422" y="125" text-anchor="middle" class="fig-blue" font-size="10">系统总线</text>
    <text x="422" y="170" text-anchor="middle" class="fig-blue" font-size="11">主存</text>
    <text x="576" y="165" text-anchor="middle" class="fig-purple" font-size="10.5">扩展总线接口</text>
    <text x="502" y="190" text-anchor="middle" class="fig-orange" font-size="10">扩展总线</text>
    <text x="522" y="226" text-anchor="middle" class="fig-orange" font-size="10">高速图形/网</text>
    <text x="624" y="226" text-anchor="middle" class="fig-muted" font-size="10">低速键鼠/串</text>
    <text x="380" y="206" class="fig-red" font-size="10">局限：快慢外设</text>
    <text x="380" y="222" class="fig-red" font-size="10">混挂互相拖慢！</text>
  </svg>
  <figcaption>图 3-6　三总线结构的两种形态：（a）引入专用 DMA 总线直连主存；（b）引入局部总线与 Cache，但扩展总线快慢设备混杂</figcaption>
</figure>

<strong>四总线结构</strong>：局部总线连接 CPU、Cache 与桥；系统总线连接主存、Cache 与桥；桥再引出高速总线接高速外设，低速设备经扩展总线接口挂到扩展总线上。高速设备与低速设备分类组织，各自使用合适的总线。

<figure class="cpd-figure not-content" id="lhw-w2-fig-7">
  <svg viewBox="0 0 700 296" role="img" aria-label="四总线分层结构与高低速外设分流示意图">
    <defs>
      <marker id="lhw-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="14" width="668" height="268" rx="8" class="fig-box-subtle"/>
    <line x1="36" y1="74" x2="646" y2="74" class="fig-line-accent" stroke-width="3.5"/>
    <line x1="88" y1="58" x2="88" y2="74" class="fig-line"/>
    <line x1="214" y1="58" x2="214" y2="74" class="fig-line"/>
    <line x1="214" y1="74" x2="214" y2="128" class="fig-line"/>
    <line x1="484" y1="58" x2="484" y2="74" class="fig-line"/>
    <line x1="484" y1="74" x2="484" y2="128" class="fig-line"/>
    <line x1="36" y1="128" x2="540" y2="128" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="3.5"/>
    <line x1="98" y1="128" x2="98" y2="146" class="fig-line"/>
    <line x1="256" y1="128" x2="256" y2="146" class="fig-line"/>
    <line x1="256" y1="180" x2="256" y2="218" class="fig-line"/>
    <line x1="484" y1="128" x2="484" y2="178" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-7)"/>
    <circle cx="214" cy="74" r="3" class="fig-dot"/>
    <circle cx="214" cy="128" r="3" class="fig-dot"/>
    <circle cx="484" cy="74" r="3" class="fig-dot"/>
    <circle cx="484" cy="128" r="3" class="fig-dot"/>
    <line x1="366" y1="180" x2="664" y2="180" class="fig-line" stroke="var(--sl-color-green)" stroke-width="3.5"/>
    <line x1="432" y1="180" x2="432" y2="198" class="fig-line"/>
    <line x1="586" y1="180" x2="586" y2="198" class="fig-line"/>
    <line x1="36" y1="218" x2="338" y2="218" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="3.5"/>
    <line x1="102" y1="218" x2="102" y2="234" class="fig-line"/>
    <line x1="256" y1="218" x2="256" y2="234" class="fig-line"/>
    <rect x="48" y="26" width="80" height="32" rx="5" class="fig-box-orange"/>
    <rect x="170" y="26" width="88" height="32" rx="5" class="fig-box-accent"/>
    <rect x="432" y="26" width="104" height="32" rx="5" class="fig-box-green"/>
    <rect x="48" y="146" width="100" height="34" rx="5" class="fig-box-blue"/>
    <rect x="196" y="146" width="120" height="34" rx="5" class="fig-box-purple"/>
    <rect x="374" y="198" width="116" height="38" rx="5" class="fig-box-green"/>
    <rect x="516" y="198" width="140" height="38" rx="5" class="fig-box-green"/>
    <rect x="44" y="234" width="116" height="34" rx="5" class="fig-box"/>
    <rect x="196" y="234" width="120" height="34" rx="5" class="fig-box"/>
    <text x="88" y="47" text-anchor="middle" class="fig-orange" font-size="12">CPU</text>
    <text x="214" y="47" text-anchor="middle" class="fig-accent" font-size="12">Cache</text>
    <text x="484" y="47" text-anchor="middle" class="fig-green" font-size="11.5">桥（PCI 桥）</text>
    <text x="345" y="68" text-anchor="middle" class="fig-accent" font-size="9.2">① 局部总线（CPU-Cache 高频直连）</text>
    <text x="372" y="120" text-anchor="middle" class="fig-blue" font-size="11">② 系统总线（主存通路）</text>
    <text x="98" y="168" text-anchor="middle" class="fig-blue" font-size="12">主存储器</text>
    <text x="256" y="168" text-anchor="middle" class="fig-purple" font-size="11.5">扩展总线接口</text>
    <text x="580" y="172" text-anchor="middle" class="fig-green" font-size="11">③ 高速总线（PCI/VL）</text>
    <text x="432" y="221" text-anchor="middle" class="fig-green" font-size="11">图形 / 显示卡</text>
    <text x="586" y="221" text-anchor="middle" class="fig-green" font-size="11">高速网卡 / 多媒体</text>
    <text x="142" y="210" text-anchor="middle" class="fig-orange" font-size="11">④ 扩展总线（ISA/EISA）</text>
    <text x="102" y="255" text-anchor="middle" class="fig-label" font-size="11">低速串口/键鼠</text>
    <text x="256" y="255" text-anchor="middle" class="fig-label" font-size="11">传真/调制解调器</text>
    <text x="516" y="260" text-anchor="middle" class="fig-muted" font-size="10.5">核心思想：快慢设备按速率分级挂接，桥负责缓冲与级联扩展</text>
  </svg>
  <figcaption>图 3-7　四总线分层结构：局部总线、系统总线、高速总线与扩展总线四级分离，彻底解决快慢外设混挂拖慢问题</figcaption>
</figure>

<strong>应用实例</strong>：传统微机用一条系统总线连 CPU 与主存，再经标准总线控制器扩展 I/O 总线；VESA 结构在系统总线之外扩展高速 VL 总线接图形设备，再经标准总线控制器接低速总线；PCI 结构经 PCI 桥扩展高速 PCI 总线接多媒体、高速网络、图形设备，再经标准总线控制器扩展 ISA/EISA 低速总线。PCI 设备较多、驱动能力不足时，可用 PCI 桥级联扩展，提高驱动能力并延长总线。

| 结构 | 主要通路 | 主要问题 |
|---|---|---|
| 单总线 | 所有部件共用一条 | 瓶颈；CPU 停顿；延迟与争用 |
| 双总线（CPU 为中心） | CPU–主存 M 总线；CPU–I/O I/O 总线 | 主存–外设须经 CPU |
| 双总线（存储器为中心） | 主存–CPU 存储总线；系统总线 | 两总线仍分时 |
| 双总线（存储/I/O 分离 + 通道） | 通道连接两侧 | 通道是专用简易处理器 |
| 三总线（DMA） | 主存总线、I/O 总线、DMA 总线 | 高速设备走 DMA，低速仍走 I/O |
| 三总线（局部/系统/扩展） | 局部总线连 CPU–Cache | 扩展总线上设备速度混杂 |
| 四总线 | 局部、系统、高速、扩展 | 结构复杂、成本高 |

### 3.6 总线判优控制

总线上有多个设备，必须解决两个问题：判优（仲裁）决定哪一对设备使用总线；<strong>通信控制</strong>保证这一对设备可靠地完成传送。

<strong>主设备与从设备</strong>：

- <strong>主设备（主模块）</strong>：对总线有控制权，可提出总线占用请求，占用后控制通信过程。
- <strong>从设备（从模块）</strong>：不能控制总线、不能提出请求，只能响应主设备发来的总线命令。
- 有些设备既可作主设备也可作从设备；有的总线允许多个主设备，有的只允许一个。

<strong>集中式与分布式</strong>：集中式把判优逻辑集中在一个部件（如 CPU）中；分布式把判优逻辑分散到各设备或各设备端口。

<strong>（1）链式查询</strong>：控制部件集中，另有数据总线、地址总线，以及三根仲裁线——BR（总线请求，所有设备共用）、BG（总线授权，逐级下传）、BS（总线忙）。设备经 BR 提出请求，控制部件发 BG 逐个下查，BG 到达第一个提出请求的接口即被截获，该设备占用总线并置 BS。

<figure class="cpd-figure not-content" id="lhw-w2-fig-8">
  <svg viewBox="0 0 700 286" role="img" aria-label="集中式总线仲裁之链式查询电路与BG断链故障示意图">
    <defs>
      <marker id="lhw-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="14" width="668" height="258" rx="8" class="fig-box-subtle"/>
    <line x1="168" y1="84" x2="656" y2="84" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-start="url(#lhw-w2-arr-8)"/>
    <line x1="160" y1="154" x2="210" y2="154" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" marker-end="url(#lhw-w2-arr-8)"/>
    <line x1="312" y1="154" x2="362" y2="154" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2.2" marker-end="url(#lhw-w2-arr-8)"/>
    <line x1="460" y1="154" x2="554" y2="154" class="fig-line-dashed"/>
    <line x1="168" y1="222" x2="656" y2="222" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-start="url(#lhw-w2-arr-8)"/>
    <line x1="262" y1="126" x2="262" y2="84" class="fig-line-dashed"/>
    <line x1="412" y1="126" x2="412" y2="86" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#lhw-w2-arr-8)"/>
    <line x1="604" y1="126" x2="604" y2="86" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#lhw-w2-arr-8)"/>
    <line x1="262" y1="182" x2="262" y2="222" class="fig-line-dashed"/>
    <line x1="412" y1="182" x2="412" y2="220" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-8)"/>
    <line x1="604" y1="182" x2="604" y2="222" class="fig-line-dashed"/>
    <rect x="32" y="64" width="128" height="174" rx="6" class="fig-box-accent"/>
    <rect x="212" y="126" width="100" height="56" rx="5" class="fig-box"/>
    <rect x="364" y="126" width="96" height="56" rx="5" class="fig-box-green"/>
    <rect x="556" y="126" width="98" height="56" rx="5" class="fig-box"/>
    <rect x="472" y="143" width="72" height="22" rx="4" class="fig-box-red"/>
    <text x="96" y="124" text-anchor="middle" class="fig-accent" font-size="12.5">总线控制部件</text>
    <text x="96" y="144" text-anchor="middle" class="fig-muted" font-size="10.5">（集中仲裁器）</text>
    <text x="96" y="172" text-anchor="middle" class="fig-label" font-size="10.5">仅需 3 根仲裁线：</text>
    <text x="96" y="190" text-anchor="middle" class="fig-muted" font-size="10.5">BR + BG + BS</text>
    <text x="292" y="75" text-anchor="middle" class="fig-purple" font-size="11">① BR 总线请求线（所有主设备共用）</text>
    <text x="186" y="144" text-anchor="middle" class="fig-blue" font-size="10.5">② BG</text>
    <text x="338" y="144" text-anchor="middle" class="fig-blue" font-size="10.5">透传</text>
    <text x="508" y="158" text-anchor="middle" class="fig-red" font-size="9.5">截断 / 断链</text>
    <text x="262" y="149" text-anchor="middle" class="fig-label" font-size="11.5">I/O 接口 0</text>
    <text x="262" y="168" text-anchor="middle" class="fig-muted" font-size="9.8">无请求（最高优）</text>
    <text x="412" y="149" text-anchor="middle" class="fig-green" font-size="11.5">I/O 接口 1</text>
    <text x="412" y="168" text-anchor="middle" class="fig-green" font-size="9.5">截获 BG 并置 BS</text>
    <text x="605" y="149" text-anchor="middle" class="fig-label" font-size="11">I/O 接口 n−1</text>
    <text x="605" y="168" text-anchor="middle" class="fig-orange" font-size="9.8">最低优 / 易饥饿</text>
    <text x="310" y="240" text-anchor="middle" class="fig-green" font-size="11">③ BS 总线忙线（获权设备置忙，锁定总线使用权）</text>
    <text x="350" y="261" text-anchor="middle" class="fig-red" font-size="9.8">★考点：优先级按物理连接位置固定；对电路故障极敏感——某处 BG 断链则其后设备永久无法获权</text>
  </svg>
  <figcaption>图 3-8　链式查询方式：3 根仲裁线（BR、BG、BS），BG 菊花链逐级串行下传，结构最简但存在断链全瘫风险</figcaption>
</figure>

- <strong>优先级</strong>：由 BG 查询顺序即设备连接位置决定，靠近控制部件者优先；优先级低的设备可能长期得不到响应（饥饿）。
- <strong>优点</strong>：结构简单，仲裁线仅 3 根；增删设备容易；可靠性设计容易（BG、BS 可各用两根线冗余）。
- <strong>缺点</strong>：对电路故障敏感，BG 线在某个接口断开，后面设备全部失效；逐级查询速度慢。多用于微型机或简单嵌入式系统。

<strong>（2）计数器定时查询</strong>：仍用 BR、BS，但取消 BG，增加一组<strong>设备地址线</strong>。控制部件内有计数器，收到请求后启动计数，计数值经设备地址线送出，各设备用地址比较器比对；命中且提出请求的设备响应并置 BS。

<figure class="cpd-figure not-content" id="lhw-w2-fig-9">
  <svg viewBox="0 0 700 296" role="img" aria-label="集中式总线仲裁之计数器定时查询电路与三种优先级策略示意图">
    <defs>
      <marker id="lhw-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="14" width="466" height="268" rx="8" class="fig-box-subtle"/>
    <rect x="492" y="14" width="192" height="268" rx="8" class="fig-box-subtle"/>
    <line x1="56" y1="102" x2="466" y2="102" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2"/>
    <line x1="108" y1="102" x2="108" y2="86" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="74" y1="142" x2="466" y2="142" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2"/>
    <line x1="136" y1="142" x2="136" y2="86" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="166" y1="84" x2="166" y2="184" class="fig-line-accent" stroke-width="2.5"/>
    <line x1="32" y1="184" x2="466" y2="184" class="fig-line-accent" stroke-width="3"/>
    <line x1="64" y1="220" x2="64" y2="102" class="fig-line-dashed"/>
    <line x1="176" y1="220" x2="176" y2="104" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="1.8" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="428" y1="220" x2="428" y2="104" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="1.8" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="82" y1="220" x2="82" y2="142" class="fig-line-dashed"/>
    <line x1="192" y1="220" x2="192" y2="144" class="fig-line" stroke="var(--sl-color-green)" stroke-width="1.8" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="446" y1="220" x2="446" y2="142" class="fig-line-dashed"/>
    <line x1="110" y1="184" x2="110" y2="193" class="fig-line-accent"/>
    <line x1="110" y1="211" x2="110" y2="218" class="fig-line-accent" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="224" y1="184" x2="224" y2="193" class="fig-line-accent"/>
    <line x1="224" y1="211" x2="224" y2="218" class="fig-line-accent" marker-end="url(#lhw-w2-arr-9)"/>
    <line x1="392" y1="184" x2="392" y2="193" class="fig-line-accent"/>
    <line x1="392" y1="211" x2="392" y2="218" class="fig-line-accent" marker-end="url(#lhw-w2-arr-9)"/>
    <rect x="28" y="26" width="188" height="58" rx="6" class="fig-box"/>
    <rect x="120" y="34" width="88" height="42" rx="4" class="fig-box-accent"/>
    <circle cx="110" cy="202" r="9" class="fig-box"/>
    <circle cx="224" cy="202" r="9" class="fig-box-green"/>
    <circle cx="392" cy="202" r="9" class="fig-box"/>
    <rect x="36" y="220" width="94" height="46" rx="5" class="fig-box"/>
    <rect x="156" y="220" width="94" height="46" rx="5" class="fig-box-green"/>
    <rect x="368" y="220" width="94" height="46" rx="5" class="fig-box"/>
    <rect x="504" y="46" width="168" height="62" rx="5" class="fig-box-blue"/>
    <rect x="504" y="118" width="168" height="96" rx="5" class="fig-box"/>
    <rect x="504" y="224" width="168" height="44" rx="5" class="fig-box-green"/>
    <rect x="240" y="80" width="176" height="18" rx="3" class="fig-box"/>
    <rect x="214" y="114" width="204" height="20" rx="4" class="fig-box"/>
    <rect x="204" y="152" width="218" height="22" rx="4" class="fig-box"/>
    <text x="74" y="52" text-anchor="middle" class="fig-label" font-size="11.5">控制部件</text>
    <text x="74" y="69" text-anchor="middle" class="fig-muted" font-size="9.8">收到 BR 计数</text>
    <text x="164" y="52" text-anchor="middle" class="fig-accent" font-size="11.5">计数器</text>
    <text x="164" y="68" text-anchor="middle" class="fig-muted" font-size="9.2">初值软件可设</text>
    <text x="328" y="93" text-anchor="middle" class="fig-purple" font-size="9.8">① BR 总线请求（共用）</text>
    <text x="316" y="128" text-anchor="middle" class="fig-green" font-size="9.5">③ BS 总线忙（命中置 1 停计数）</text>
    <text x="313" y="167" text-anchor="middle" class="fig-accent" font-size="9.5">② 设备地址线（⌈log₂ n⌉ 根，代 BG）</text>
    <text x="110" y="206" text-anchor="middle" class="fig-muted" font-size="10.5">=</text>
    <text x="224" y="206" text-anchor="middle" class="fig-green" font-size="10.5">=</text>
    <text x="392" y="206" text-anchor="middle" class="fig-muted" font-size="10.5">=</text>
    <text x="83" y="241" text-anchor="middle" class="fig-label" font-size="11.5">I/O 接口 0</text>
    <text x="83" y="257" text-anchor="middle" class="fig-muted" font-size="9.2">ID=00 (未请求)</text>
    <text x="203" y="241" text-anchor="middle" class="fig-green" font-size="11.5">I/O 接口 1</text>
    <text x="203" y="257" text-anchor="middle" class="fig-green" font-size="9.2">ID=01 匹配获权</text>
    <text x="415" y="241" text-anchor="middle" class="fig-label" font-size="11">I/O 接口 n−1</text>
    <text x="415" y="257" text-anchor="middle" class="fig-muted" font-size="8.6">ID=n−1（继续等待）</text>
    <text x="588" y="34" text-anchor="middle" class="fig-title" font-size="12">线数与优先级控制</text>
    <text x="588" y="68" text-anchor="middle" class="fig-blue" font-size="11">仲裁线总数：</text>
    <text x="588" y="88" text-anchor="middle" class="fig-label" font-size="11.5">⌈log₂ n⌉ + 2 根</text>
    <text x="588" y="102" text-anchor="middle" class="fig-muted" font-size="9.5">（地址线 + BR + BS）</text>
    <text x="514" y="138" class="fig-label" font-size="10.5">• 每次从 0 计：固定优先级</text>
    <text x="514" y="162" class="fig-accent" font-size="10.5">• 从中止点继续：循环公平</text>
    <text x="514" y="186" class="fig-orange" font-size="10.5">• 置初值 k：k 号最高优先</text>
    <text x="514" y="205" class="fig-muted" font-size="9.5">每次轮询仍需时钟计数开销</text>
    <text x="588" y="243" text-anchor="middle" class="fig-green" font-size="10.5">优点：无 BG 串联断链扩散</text>
    <text x="588" y="260" text-anchor="middle" class="fig-muted" font-size="10">单接口故障不影响其余设备</text>
  </svg>
  <figcaption>图 3-9　计数器定时查询方式：用 $\lceil \log_2 n \rceil$ 根设备地址线替代 BG 菊花链，通过计数器初值灵活实现固定、循环或动态优先级</figcaption>
</figure>

- <strong>线数</strong>：设备地址线至少 $\lceil \log_2 n \rceil$ 根，加上 BR、BS 共 $\lceil \log_2 n \rceil + 2$ 根。
- <strong>优先级灵活</strong>：计数器每次从 0 开始 → 固定优先级；从上次停止值继续 → 循环优先级；软件设定初值 $k$ → $k$ 号设备最高优先级。
- 相比链式，不存在 BG 链断导致下游全失效的问题，但线数随设备数对数增长。

<strong>（3）独立请求</strong>：每个设备都有一对独立的 BR、BG 线，共 $2n$ 根，全部送入控制部件的排队器。排队器内部同时收到多个请求，按预定规则裁决后只对其中一个发 BG。

<figure class="cpd-figure not-content" id="lhw-w2-fig-10">
  <svg viewBox="0 0 700 286" role="img" aria-label="集中式总线仲裁之独立请求方式与三种方式综合对比示意图">
    <defs>
      <marker id="lhw-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="14" width="442" height="258" rx="8" class="fig-box-subtle"/>
    <rect x="470" y="14" width="214" height="258" rx="8" class="fig-box-subtle"/>
    <line x1="138" y1="66" x2="282" y2="66" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#lhw-w2-arr-10)"/>
    <line x1="284" y1="88" x2="140" y2="88" class="fig-line-dashed"/>
    <line x1="138" y1="142" x2="282" y2="142" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#lhw-w2-arr-10)"/>
    <line x1="284" y1="164" x2="140" y2="164" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2.2" marker-end="url(#lhw-w2-arr-10)"/>
    <line x1="138" y1="218" x2="282" y2="218" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2" marker-end="url(#lhw-w2-arr-10)"/>
    <line x1="284" y1="240" x2="140" y2="240" class="fig-line-dashed"/>
    <rect x="34" y="52" width="104" height="48" rx="5" class="fig-box"/>
    <rect x="34" y="128" width="104" height="48" rx="5" class="fig-box-green"/>
    <rect x="34" y="204" width="104" height="48" rx="5" class="fig-box"/>
    <rect x="284" y="48" width="158" height="206" rx="6" class="fig-box-accent"/>
    <rect x="298" y="106" width="130" height="90" rx="5" class="fig-box"/>
    <rect x="484" y="48" width="186" height="64" rx="5" class="fig-box"/>
    <rect x="484" y="122" width="186" height="64" rx="5" class="fig-box"/>
    <rect x="484" y="196" width="186" height="64" rx="5" class="fig-box-accent"/>
    <text x="30" y="34" class="fig-title" font-size="12.5">（a）独立请求：每设备独占一对 BR_i / BG_i（共 2n 根）</text>
    <text x="86" y="74" text-anchor="middle" class="fig-label" font-size="11.5">主设备 0</text>
    <text x="86" y="90" text-anchor="middle" class="fig-muted" font-size="9.5">请求挂起</text>
    <text x="86" y="150" text-anchor="middle" class="fig-green" font-size="11.5">主设备 1</text>
    <text x="86" y="166" text-anchor="middle" class="fig-green" font-size="9.5">最高优获权</text>
    <text x="86" y="226" text-anchor="middle" class="fig-label" font-size="11">主设备 n−1</text>
    <text x="86" y="242" text-anchor="middle" class="fig-muted" font-size="9.5">请求挂起</text>
    <text x="210" y="60" text-anchor="middle" class="fig-purple" font-size="10.5">BR0 = 1</text>
    <text x="210" y="101" text-anchor="middle" class="fig-muted" font-size="10">BG0 = 0</text>
    <text x="210" y="136" text-anchor="middle" class="fig-purple" font-size="10.5">BR1 = 1</text>
    <text x="210" y="178" text-anchor="middle" class="fig-green" font-size="10.5">BG1 = 1（授权）</text>
    <text x="210" y="212" text-anchor="middle" class="fig-purple" font-size="10.2">BR(n−1) = 1</text>
    <text x="210" y="253" text-anchor="middle" class="fig-muted" font-size="10">BG(n−1) = 0</text>
    <text x="363" y="72" text-anchor="middle" class="fig-accent" font-size="12.5">集中排队器</text>
    <text x="363" y="90" text-anchor="middle" class="fig-muted" font-size="10">（并行仲裁逻辑）</text>
    <text x="306" y="128" class="fig-label" font-size="10.5">支持任意策略：</text>
    <text x="306" y="148" class="fig-muted" font-size="9.6">• 固定 / 循环优先级</text>
    <text x="306" y="166" class="fig-muted" font-size="9.6">• 自适应 / 软件编程</text>
    <text x="306" y="184" class="fig-green" font-size="9.6">• 屏蔽故障设备请求</text>
    <text x="363" y="222" text-anchor="middle" class="fig-orange" font-size="10.5">无需逐级传或计数</text>
    <text x="363" y="240" text-anchor="middle" class="fig-accent" font-size="10.5">并行裁决响应最快！</text>
    <text x="577" y="34" text-anchor="middle" class="fig-title" font-size="12">三种集中仲裁横向对比</text>
    <text x="496" y="69" class="fig-label" font-size="11">1. 链式查询：3 根线</text>
    <text x="496" y="88" class="fig-muted" font-size="10">最省线，速度最慢，怕断链</text>
    <text x="496" y="103" class="fig-muted" font-size="10">优先级由连接位置死锁</text>
    <text x="496" y="143" class="fig-label" font-size="11">2. 计数器查询：⌈log₂ n⌉+2</text>
    <text x="496" y="162" class="fig-muted" font-size="10">线数对数级，初值调优先级</text>
    <text x="496" y="177" class="fig-muted" font-size="10">轮询计数仍有时间开销</text>
    <text x="496" y="217" class="fig-accent" font-size="11">3. 独立请求：2n 根 (+BS)</text>
    <text x="496" y="236" class="fig-green" font-size="10">响应最快，优先级最灵活</text>
    <text x="496" y="251" class="fig-orange" font-size="10">代价：控制线多、逻辑复杂</text>
  </svg>
  <figcaption>图 3-10　独立请求方式与三种集中式仲裁对比：用 2n 根独立请求/授权线换取并行裁决的最高响应速度与故障隔离能力</figcaption>
</figure>

| 方式 | 仲裁线 | 优先级 | 特点 |
|---|---|---|---|
| 链式查询 | BR、BG、BS，3 根 | 由连接顺序固定 | 最简单、最省线；BG 断链致命；速度慢 |
| 计数器定时查询 | BR、BS + $\lceil \log_2 n \rceil$ 根地址线 | 初值决定，可固定/循环/软件设定 | 无 BG 链故障扩散；线数居中 |
| 独立请求 | 每设备一对 BR/BG，$2n$ 根 | 排队器任意设定 | 速度最快、最灵活；线数最多 |

### 3.7 总线通信控制

<strong>总线传输周期</strong>：主设备与从设备完成一次完整、可靠通信所需的时间，分四个阶段：

1. <strong>申请分配</strong>：主设备申请并获得总线使用权，由判优逻辑完成。
2. <strong>寻址</strong>：主设备发出地址与命令，找到从设备并指定操作。
3. <strong>数据传输</strong>：从设备发送数据，或接收主设备发来的数据。
4. <strong>结束</strong>：主设备与从设备分别撤销地址、命令、数据等信息。

<figure class="cpd-figure not-content" id="lhw-w2-fig-11">
  <svg viewBox="0 0 700 198" role="img" aria-label="总线传输周期四个阶段状态流转示意图">
    <defs>
      <marker id="lhw-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="14" width="668" height="170" rx="8" class="fig-box-subtle"/>
    <line x1="174" y1="90" x2="194" y2="90" class="fig-line-accent" stroke-width="2" marker-end="url(#lhw-w2-arr-11)"/>
    <line x1="338" y1="90" x2="358" y2="90" class="fig-line-accent" stroke-width="2" marker-end="url(#lhw-w2-arr-11)"/>
    <line x1="502" y1="90" x2="522" y2="90" class="fig-line-accent" stroke-width="2" marker-end="url(#lhw-w2-arr-11)"/>
    <polyline points="596,136 596,164 103,164 103,138" class="fig-line-dashed" marker-end="url(#lhw-w2-arr-11)"/>
    <rect x="32" y="44" width="142" height="92" rx="6" class="fig-box-purple"/>
    <rect x="196" y="44" width="142" height="92" rx="6" class="fig-box-blue"/>
    <rect x="360" y="44" width="142" height="92" rx="6" class="fig-box-green"/>
    <rect x="524" y="44" width="144" height="92" rx="6" class="fig-box"/>
    <rect x="242" y="153" width="216" height="20" rx="4" class="fig-box"/>
    <text x="30" y="32" class="fig-title" font-size="12.5">一次完整总线传输周期（Bus Cycle）的四阶段模型</text>
    <text x="103" y="66" text-anchor="middle" class="fig-purple" font-size="12">① 申请分配阶段</text>
    <text x="103" y="88" text-anchor="middle" class="fig-label" font-size="10.5">主模块提请求 BR</text>
    <text x="103" y="105" text-anchor="middle" class="fig-muted" font-size="10">判优裁决获 BG 置 BS</text>
    <text x="103" y="123" text-anchor="middle" class="fig-purple" font-size="10">［总线判优控制］</text>
    <text x="267" y="66" text-anchor="middle" class="fig-blue" font-size="12">② 寻址阶段</text>
    <text x="267" y="88" text-anchor="middle" class="fig-label" font-size="10.5">主模块发地址与命令</text>
    <text x="267" y="105" text-anchor="middle" class="fig-muted" font-size="10">译码选中目标从模块</text>
    <text x="267" y="123" text-anchor="middle" class="fig-blue" font-size="10">［占用 AB + CB］</text>
    <text x="431" y="66" text-anchor="middle" class="fig-green" font-size="12">③ 传数阶段</text>
    <text x="431" y="88" text-anchor="middle" class="fig-label" font-size="10.5">主↔从模块交换数据</text>
    <text x="431" y="105" text-anchor="middle" class="fig-muted" font-size="10">单字传送或突发成块</text>
    <text x="431" y="123" text-anchor="middle" class="fig-green" font-size="10">［占用 DB + 联络线］</text>
    <text x="596" y="66" text-anchor="middle" class="fig-label" font-size="12">④ 结束阶段</text>
    <text x="596" y="88" text-anchor="middle" class="fig-label" font-size="10.5">撤销地址/命令/数据</text>
    <text x="596" y="105" text-anchor="middle" class="fig-muted" font-size="10">清除 BS 让出使用权</text>
    <text x="596" y="123" text-anchor="middle" class="fig-orange" font-size="10">［三态门回高阻］</text>
    <text x="350" y="167" text-anchor="middle" class="fig-muted" font-size="10">总线恢复空闲，进入下一轮主模块竞争</text>
  </svg>
  <figcaption>图 3-11　总线传输周期的四阶段流程：阶段 ① 解决“谁用总线”（仲裁），阶段 ②～④ 解决“怎么通信”（时序联络）</figcaption>
</figure>

<strong>四种通信方式</strong>：

| 方式 | 联络方式 | 适用场合 |
|---|---|---|
| 同步通信 | 统一、定宽定距的时钟 | 总线短、各模块存取时间接近 |
| 异步通信 | 请求/应答握手 | 速度差异大、距离较远 |
| 半同步通信 | 时钟 + WAIT 信号 | 兼有同步与异步特征 |
| 分离式通信 | 一个传输周期拆成两个子周期 | 充分挖掘总线每一瞬间的潜力 |

<strong>（1）同步通信</strong>：用统一时标控制整个传送过程，每个操作都在固定时刻发生。以 CPU 从外设读入数据为例，一个总线周期由 4 个时钟周期组成：

- T1 上升沿：主设备给出地址；
- T2 上升沿：给出读命令；
- T3 上升沿之前：从设备必须把数据放到数据总线上；
- T4 上升沿：撤销数据与读命令；T4 结束：撤销地址。

<figure class="cpd-figure not-content" id="lhw-w2-fig-12">
  <svg viewBox="0 0 700 296" role="img" aria-label="同步通信四个时钟周期读写完整时序对比图">
    <rect x="16" y="14" width="330" height="268" rx="8" class="fig-box-subtle"/>
    <rect x="356" y="14" width="328" height="268" rx="8" class="fig-box-subtle"/>
    <line x1="82" y1="54" x2="82" y2="196" class="fig-line-dashed"/>
    <line x1="144" y1="54" x2="144" y2="196" class="fig-line-dashed"/>
    <line x1="206" y1="54" x2="206" y2="196" class="fig-line-dashed"/>
    <line x1="268" y1="54" x2="268" y2="196" class="fig-line-dashed"/>
    <line x1="330" y1="54" x2="330" y2="196" class="fig-line-dashed"/>
    <path d="M 82 80 L 82 60 L 113 60 L 113 80 L 144 80 L 144 60 L 175 60 L 175 80 L 206 80 L 206 60 L 237 60 L 237 80 L 268 80 L 268 60 L 299 60 L 299 80 L 330 80" class="fig-line"/>
    <path d="M 82 146 L 148 146 L 148 128 L 268 128 L 268 146 L 330 146" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2"/>
    <line x1="82" y1="174" x2="196" y2="174" class="fig-line-dashed"/>
    <line x1="268" y1="174" x2="330" y2="174" class="fig-line-dashed"/>
    <rect x="86" y="94" width="242" height="22" rx="3" class="fig-box-blue"/>
    <rect x="196" y="162" width="72" height="24" rx="3" class="fig-box-green"/>
    <line x1="422" y1="54" x2="422" y2="196" class="fig-line-dashed"/>
    <line x1="484" y1="54" x2="484" y2="196" class="fig-line-dashed"/>
    <line x1="546" y1="54" x2="546" y2="196" class="fig-line-dashed"/>
    <line x1="608" y1="54" x2="608" y2="196" class="fig-line-dashed"/>
    <line x1="670" y1="54" x2="670" y2="196" class="fig-line-dashed"/>
    <path d="M 422 80 L 422 60 L 453 60 L 453 80 L 484 80 L 484 60 L 515 60 L 515 80 L 546 80 L 546 60 L 577 60 L 577 80 L 608 80 L 608 60 L 639 60 L 639 80 L 670 80" class="fig-line"/>
    <line x1="422" y1="138" x2="453" y2="138" class="fig-line-dashed"/>
    <line x1="608" y1="138" x2="670" y2="138" class="fig-line-dashed"/>
    <path d="M 422 182 L 488 182 L 488 164 L 608 164 L 608 182 L 670 182" class="fig-line" stroke="var(--sl-color-orange)" stroke-width="2"/>
    <rect x="426" y="94" width="242" height="22" rx="3" class="fig-box-blue"/>
    <rect x="453" y="126" width="155" height="24" rx="3" class="fig-box-green"/>
    <rect x="184" y="131" width="48" height="14" rx="3" class="fig-box"/>
    <rect x="508" y="167" width="80" height="14" rx="3" class="fig-box"/>
    <text x="28" y="31" class="fig-title" font-size="12">（a）同步读时序（主设备 ← 从设备）</text>
    <text x="113" y="48" text-anchor="middle" class="fig-accent" font-size="11">T1</text>
    <text x="175" y="48" text-anchor="middle" class="fig-accent" font-size="11">T2</text>
    <text x="237" y="48" text-anchor="middle" class="fig-accent" font-size="11">T3</text>
    <text x="299" y="48" text-anchor="middle" class="fig-accent" font-size="11">T4</text>
    <text x="28" y="74" class="fig-muted" font-size="10.5">CLK</text>
    <text x="28" y="109" class="fig-blue" font-size="10.5">地址</text>
    <text x="28" y="142" class="fig-purple" font-size="10.5">读命令</text>
    <text x="28" y="178" class="fig-green" font-size="10.5">数据</text>
    <text x="207" y="109" text-anchor="middle" class="fig-blue" font-size="10">有效地址 A</text>
    <text x="208" y="142" text-anchor="middle" class="fig-purple" font-size="9.5">RD 有效</text>
    <text x="232" y="178" text-anchor="middle" class="fig-green" font-size="10">读出数据</text>
    <text x="28" y="216" class="fig-label" font-size="10.5">• T1↑：主设备发地址；  T2↑：主设备发读命令</text>
    <text x="28" y="236" class="fig-green" font-size="10.5">• T3↑前：从设备将数据送上数据总线供主设备采样</text>
    <text x="28" y="256" class="fig-muted" font-size="10.5">• T4↑：撤销数据与读命令；  T4末：撤销地址</text>
    <text x="28" y="273" class="fig-red" font-size="10">局限：时钟按最慢模块设定，快模块被迫降速等待</text>
    <text x="368" y="31" class="fig-title" font-size="12">（b）同步写时序（主设备 → 从设备）</text>
    <text x="453" y="48" text-anchor="middle" class="fig-accent" font-size="11">T1</text>
    <text x="515" y="48" text-anchor="middle" class="fig-accent" font-size="11">T2</text>
    <text x="577" y="48" text-anchor="middle" class="fig-accent" font-size="11">T3</text>
    <text x="639" y="48" text-anchor="middle" class="fig-accent" font-size="11">T4</text>
    <text x="368" y="74" class="fig-muted" font-size="10.5">CLK</text>
    <text x="368" y="109" class="fig-blue" font-size="10.5">地址</text>
    <text x="368" y="142" class="fig-green" font-size="10.5">数据</text>
    <text x="368" y="178" class="fig-orange" font-size="10.5">写命令</text>
    <text x="547" y="109" text-anchor="middle" class="fig-blue" font-size="10">有效地址 A</text>
    <text x="530" y="142" text-anchor="middle" class="fig-green" font-size="10">T1↓ 主设备给写入数据</text>
    <text x="548" y="178" text-anchor="middle" class="fig-orange" font-size="9.5">WR 有效(T3写)</text>
    <text x="368" y="216" class="fig-label" font-size="10.5">• T1↑：发地址；  T1↓：主设备提前送出写入数据</text>
    <text x="368" y="236" class="fig-orange" font-size="10.5">• T2↑：发写命令；  T3↑：从设备将数据写入内部</text>
    <text x="368" y="256" class="fig-muted" font-size="10.5">• T4↑：撤销写命令与数据；  T4末：撤销地址</text>
    <text x="368" y="273" class="fig-muted" font-size="10">适用：总线长度短、各部件存取速度相近的系统</text>
  </svg>
  <figcaption>图 3-12　同步通信读/写时序对比：统一由定宽定距时钟 T1～T4 驱动，每个边沿执行固定动作</figcaption>
</figure>

<strong>同步写</strong>过程类似：T1 上升沿发地址，T1 下降沿发数据，T2 上升沿发写命令，T3 上升沿执行写入，T4 上升沿撤销数据与写命令，T4 结束撤销地址。

<strong>同步方式的特点</strong>：所有从模块受同一时标控制，主从模块强制同步；设计时必须按<strong>最慢模块</strong>确定时钟周期，快设备也被拖慢。因此适用于总线较短、各模块存取时间较一致的场合。总线越长，信号传播延迟越大，时钟周期必须放宽。

> <strong>★考点</strong>：同步通信一个总线周期固定为 4 个时钟，读/写在每个时钟沿各做什么是常考时序。

<strong>（2）异步通信</strong>：不设统一时钟，增加<strong>请求</strong>与<strong>应答</strong>两根联络线，靠握手保证正确性。按撤销方式分三种：

- <strong>不互锁</strong>：主设备发请求，从设备应答后，双方各自经过一定延迟就撤销信号，不确认对方是否收到。类似扔飞盘，喊一声就扔，可靠性差。
- <strong>半互锁</strong>：主设备收到应答后才撤销请求；从设备发出应答后经一定延迟自行撤销，不再等待。问题是主设备若迟迟收不到应答，请求可能长期保持高电平。
- <strong>全互锁</strong>：主设备收到应答才撤请求，从设备等到请求撤销才撤应答。最可靠，出错时可请求重发，但握手时间最长。

<figure class="cpd-figure not-content" id="lhw-w2-fig-13">
  <svg viewBox="0 0 700 264" role="img" aria-label="异步通信不互锁半互锁与全互锁三种握手因果时序对比图">
    <defs>
      <marker id="lhw-w2-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" 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="214" height="236" rx="8" class="fig-box-subtle"/>
    <rect x="242" y="14" width="216" height="236" rx="8" class="fig-box-subtle"/>
    <rect x="470" y="14" width="214" height="236" rx="8" class="fig-box-subtle"/>
    <path d="M 64 86 L 78 86 L 78 58 L 154 58 L 154 86 L 216 86" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2"/>
    <path d="M 64 150 L 108 150 L 108 122 L 184 122 L 184 150 L 216 150" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2"/>
    <path d="M 80 72 Q 94 102 105 126" class="fig-line-accent" marker-end="url(#lhw-w2-arr-13)"/>
    <path d="M 290 86 L 304 86 L 304 58 L 386 58 L 386 86 L 444 86" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2"/>
    <path d="M 290 150 L 334 150 L 334 122 L 416 122 L 416 150 L 444 150" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2"/>
    <path d="M 306 72 Q 320 102 331 126" class="fig-line-accent" marker-end="url(#lhw-w2-arr-13)"/>
    <path d="M 337 122 Q 362 94 383 72" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#lhw-w2-arr-13)"/>
    <path d="M 518 86 L 532 86 L 532 58 L 604 58 L 604 86 L 670 86" class="fig-line" stroke="var(--sl-color-blue)" stroke-width="2"/>
    <path d="M 518 150 L 560 150 L 560 122 L 642 122 L 642 150 L 670 150" class="fig-line" stroke="var(--sl-color-green)" stroke-width="2"/>
    <path d="M 534 72 Q 547 102 557 126" class="fig-line-accent" marker-end="url(#lhw-w2-arr-13)"/>
    <path d="M 563 122 Q 584 94 601 72" class="fig-line" stroke="var(--sl-color-orange)" marker-end="url(#lhw-w2-arr-13)"/>
    <path d="M 606 74 Q 624 102 639 126" class="fig-line" stroke="var(--sl-color-purple)" marker-end="url(#lhw-w2-arr-13)"/>
    <text x="28" y="33" class="fig-title" font-size="12">（a）不互锁方式</text>
    <text x="28" y="78" class="fig-blue" font-size="10.5">请求</text>
    <text x="28" y="142" class="fig-green" font-size="10.5">应答</text>
    <text x="84" y="106" class="fig-accent" font-size="10">①</text>
    <text x="154" y="51" text-anchor="middle" class="fig-muted" font-size="9.5">延时自撤</text>
    <text x="184" y="115" text-anchor="middle" class="fig-muted" font-size="9.5">延时自撤</text>
    <text x="28" y="182" class="fig-label" font-size="10.5">• 主、从均定时自动撤销</text>
    <text x="28" y="201" class="fig-muted" font-size="10.5">• 双方均不确认撤销结果</text>
    <text x="28" y="220" class="fig-red" font-size="10.5">• 速度最快，但可靠性最差</text>
    <text x="254" y="33" class="fig-title" font-size="12">（b）半互锁方式</text>
    <text x="254" y="78" class="fig-blue" font-size="10.5">请求</text>
    <text x="254" y="142" class="fig-green" font-size="10.5">应答</text>
    <text x="310" y="106" class="fig-accent" font-size="10">①</text>
    <text x="360" y="104" class="fig-orange" font-size="10">②</text>
    <text x="416" y="115" text-anchor="middle" class="fig-muted" font-size="9.5">延时自撤</text>
    <text x="254" y="182" class="fig-label" font-size="10.5">• 主收到应答②后才撤请求</text>
    <text x="254" y="201" class="fig-muted" font-size="10.5">• 从发应答后延时自行撤销</text>
    <text x="254" y="220" class="fig-orange" font-size="10.5">• 若应答丢失，请求长期挂死</text>
    <text x="482" y="33" class="fig-title" font-size="12">（c）全互锁（四次握手）</text>
    <text x="482" y="78" class="fig-blue" font-size="10.5">请求</text>
    <text x="482" y="142" class="fig-green" font-size="10.5">应答</text>
    <text x="538" y="106" class="fig-accent" font-size="10">①</text>
    <text x="582" y="104" class="fig-orange" font-size="10">②</text>
    <text x="626" y="102" class="fig-purple" font-size="10">③</text>
    <text x="482" y="182" class="fig-label" font-size="10.5">• 请求↑→①应答↑→②请求↓</text>
    <text x="482" y="201" class="fig-purple" font-size="10.5">• →③检测请求撤除后撤应答</text>
    <text x="482" y="220" class="fig-green" font-size="10.5">• 闭环最可靠，但握手耗时最长</text>
    <text x="482" y="238" class="fig-muted" font-size="9.5">最适合远距或速率悬殊模块通信</text>
  </svg>
  <figcaption>图 3-13　异步通信三种握手联络时序：箭头表示严格因果触发关系，全互锁构成完整闭环确认</figcaption>
</figure>

<strong>（3）半同步通信</strong>：同步与异步的结合。保留定宽定距时钟（同步特征），另加由从设备发出的 <strong>WAIT</strong> 信号（低电平有效）协调速度差异。以读入数据为例：

- T1 上升沿发地址，T2 上升沿发读命令；
- T3 到来前从设备若未准备好，就在 T3 前把 WAIT 拉低；
- 主设备在每个时钟周期到来前检测 WAIT，为低则插入一个等待周期 $T_w$；
- 某个周期前检测到 WAIT 变高，进入 T3，从设备给出数据；
- T4 撤销读命令与数据，T4 结束撤销地址。

<figure class="cpd-figure not-content" id="lhw-w2-fig-14">
  <svg viewBox="0 0 700 282" role="img" aria-label="半同步通信通过WAIT低电平有效信号插入等待周期Tw的读时序图">
    <rect x="16" y="14" width="668" height="254" rx="8" class="fig-box-subtle"/>
    <rect x="284" y="52" width="180" height="166" class="fig-box-dashed"/>
    <line x1="104" y1="52" x2="104" y2="218" class="fig-line-dashed"/>
    <line x1="194" y1="52" x2="194" y2="218" class="fig-line-dashed"/>
    <line x1="284" y1="52" x2="284" y2="218" class="fig-line-dashed"/>
    <line x1="374" y1="52" x2="374" y2="218" class="fig-line-dashed"/>
    <line x1="464" y1="52" x2="464" y2="218" class="fig-line-dashed"/>
    <line x1="554" y1="52" x2="554" y2="218" class="fig-line-dashed"/>
    <line x1="644" y1="52" x2="644" y2="218" class="fig-line-dashed"/>
    <path d="M 104 78 L 104 58 L 149 58 L 149 78 L 194 78 L 194 58 L 239 58 L 239 78 L 284 78 L 284 58 L 329 58 L 329 78 L 374 78 L 374 58 L 419 58 L 419 78 L 464 78 L 464 58 L 509 58 L 509 78 L 554 78 L 554 58 L 599 58 L 599 78 L 644 78" class="fig-line"/>
    <path d="M 104 140 L 198 140 L 198 122 L 554 122 L 554 140 L 644 140" class="fig-line" stroke="var(--sl-color-purple)" stroke-width="2"/>
    <path d="M 104 156 L 268 156 L 268 176 L 448 176 L 448 156 L 644 156" class="fig-line" stroke="var(--sl-color-red)" stroke-width="2.2"/>
    <line x1="104" y1="200" x2="456" y2="200" class="fig-line-dashed"/>
    <line x1="554" y1="200" x2="644" y2="200" class="fig-line-dashed"/>
    <rect x="108" y="90" width="534" height="22" rx="3" class="fig-box-blue"/>
    <rect x="456" y="188" width="98" height="24" rx="3" class="fig-box-green"/>
    <circle cx="284" cy="176" r="4" class="fig-box-red"/>
    <circle cx="374" cy="176" r="4" class="fig-box-red"/>
    <circle cx="464" cy="156" r="4" class="fig-box-green"/>
    <rect x="316" y="125" width="120" height="14" rx="3" class="fig-box"/>
    <rect x="296" y="158" width="124" height="14" rx="3" class="fig-box"/>
    <text x="149" y="42" text-anchor="middle" class="fig-accent" font-size="11.5">T1</text>
    <text x="239" y="42" text-anchor="middle" class="fig-accent" font-size="11.5">T2</text>
    <text x="329" y="42" text-anchor="middle" class="fig-red" font-size="11.5">Tw1（等待）</text>
    <text x="419" y="42" text-anchor="middle" class="fig-red" font-size="11.5">Tw2（等待）</text>
    <text x="509" y="42" text-anchor="middle" class="fig-green" font-size="11.5">T3</text>
    <text x="599" y="42" text-anchor="middle" class="fig-accent" font-size="11.5">T4</text>
    <text x="30" y="72" class="fig-muted" font-size="11">CLK</text>
    <text x="30" y="105" class="fig-blue" font-size="11">地址</text>
    <text x="30" y="136" class="fig-purple" font-size="11">读命令</text>
    <text x="30" y="170" class="fig-red" font-size="11">WAIT(低)</text>
    <text x="30" y="204" class="fig-green" font-size="11">数据</text>
    <text x="375" y="105" text-anchor="middle" class="fig-blue" font-size="10.5">地址信号在整个含等待的总线周期内保持稳定</text>
    <text x="376" y="136" text-anchor="middle" class="fig-purple" font-size="10">读命令延长保持至 T4↑</text>
    <text x="358" y="169" text-anchor="middle" class="fig-red" font-size="10">从模块未就绪拉低 WAIT</text>
    <text x="505" y="204" text-anchor="middle" class="fig-green" font-size="10">就绪送出数据</text>
    <text x="30" y="240" class="fig-red" font-size="11">● 采样点 1、2（T3 前）：检测到 WAIT = 0，自动插入整数个时钟等待周期 Tw，主设备暂停采样</text>
    <text x="30" y="258" class="fig-green" font-size="11">● 采样点 3：检测到 WAIT 恢复为 1，进入真正的 T3 接收数据，T4 撤销命令与数据并释放总线</text>
  </svg>
  <figcaption>图 3-14　半同步通信读时序：保留统一时钟（同步），慢速从模块通过拉低 WAIT 动态插入等待周期 Tw（异步握手）</figcaption>
</figure>

<strong>（4）分离式通信</strong>：把一个总线传输周期拆成两个子周期：

1. 主设备申请总线，发送地址与命令，随后<strong>立即放弃总线</strong>；
2. 从设备在获得地址命令后准备数据，这段准备时间<strong>不占用总线</strong>；准备好后，从设备作为新的主设备申请总线，完成数据传输。

<figure class="cpd-figure not-content" id="lhw-w2-fig-15">
  <svg viewBox="0 0 700 288" role="img" aria-label="分离式通信双子周期时间轴复用与磁盘读操作总线占用对比示意图">
    <defs>
      <marker id="lhw-w2-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="16" y="14" width="668" height="260" rx="8" class="fig-box-subtle"/>
    <line x1="30" y1="168" x2="670" y2="168" class="fig-divider"/>
    <path d="M 224 128 Q 350 156 476 128" class="fig-line-dashed" marker-end="url(#lhw-w2-arr-15)"/>
    <rect x="114" y="46" width="110" height="32" rx="4" class="fig-box-blue"/>
    <rect x="228" y="46" width="244" height="32" rx="4" class="fig-box-red"/>
    <rect x="476" y="46" width="188" height="32" rx="4" class="fig-box-green"/>
    <rect x="114" y="94" width="110" height="34" rx="4" class="fig-box-blue"/>
    <rect x="228" y="94" width="244" height="34" rx="4" class="fig-box-accent"/>
    <rect x="476" y="94" width="188" height="34" rx="4" class="fig-box-green"/>
    <rect x="240" y="135" width="220" height="20" rx="4" class="fig-box"/>
    <rect x="34" y="198" width="256" height="38" rx="5" class="fig-box-dashed"/>
    <rect x="298" y="198" width="178" height="38" rx="5" class="fig-box-dashed"/>
    <rect x="484" y="198" width="180" height="38" rx="5" class="fig-box-green"/>
    <line x1="290" y1="217" x2="296" y2="217" class="fig-line" marker-end="url(#lhw-w2-arr-15)"/>
    <line x1="476" y1="217" x2="482" y2="217" class="fig-line-accent" marker-end="url(#lhw-w2-arr-15)"/>
    <text x="30" y="33" class="fig-title" font-size="12.5">（a）传统独占等待 vs 分离式通信（双子周期解耦提高总线利用率）</text>
    <text x="30" y="66" class="fig-muted" font-size="11">传统通信</text>
    <text x="169" y="66" text-anchor="middle" class="fig-blue" font-size="10.5">主A 发地址命令</text>
    <text x="350" y="66" text-anchor="middle" class="fig-red" font-size="10.5">从B 准备数据期间死占总线（空等浪费）</text>
    <text x="570" y="66" text-anchor="middle" class="fig-green" font-size="10.5">从B 送数据至主A</text>
    <text x="30" y="115" class="fig-accent" font-size="11">分离通信</text>
    <text x="169" y="109" text-anchor="middle" class="fig-blue" font-size="10.5">子周期1 (主A)</text>
    <text x="169" y="123" text-anchor="middle" class="fig-muted" font-size="9.5">发命令后即释放</text>
    <text x="350" y="109" text-anchor="middle" class="fig-accent" font-size="10.5">总线让给其他模块 C↔D 传输！</text>
    <text x="350" y="123" text-anchor="middle" class="fig-muted" font-size="9.5">（每次占线均在有效传信息）</text>
    <text x="570" y="109" text-anchor="middle" class="fig-green" font-size="10.5">子周期2 (从B→新主模块)</text>
    <text x="570" y="123" text-anchor="middle" class="fig-muted" font-size="9.5">重新申权，同步发送地址+数据</text>
    <text x="350" y="149" text-anchor="middle" class="fig-orange" font-size="10">从模块 B 后台内部准备数据（完全不占总线）</text>
    <text x="30" y="188" class="fig-title" font-size="12">（b）典型实例：5000 r/min 磁盘读数据的三阶段耗时对比</text>
    <text x="162" y="215" text-anchor="middle" class="fig-label" font-size="11">① 磁头寻道：数十 ms</text>
    <text x="162" y="230" text-anchor="middle" class="fig-muted" font-size="10">机械移动，不占系统总线</text>
    <text x="387" y="215" text-anchor="middle" class="fig-label" font-size="11">② 旋转等待：≈ 6 ms</text>
    <text x="387" y="230" text-anchor="middle" class="fig-muted" font-size="10">半圈等待，不占系统总线</text>
    <text x="574" y="215" text-anchor="middle" class="fig-green" font-size="11">③ 传数据：仅 μs 级</text>
    <text x="574" y="230" text-anchor="middle" class="fig-green" font-size="10">申权突发占用总线！</text>
    <text x="350" y="259" text-anchor="middle" class="fig-muted" font-size="10.5">结论：剥离数十 ms 的机械等待后，总线每一瞬间均用于有效控制或数据传输，充分挖掘最大带宽潜力</text>
  </svg>
  <figcaption>图 3-15　分离式通信：将传输周期拆为“发命令即释放”与“备妥后反向主控回传”两个同步子周期，彻底消除慢速准备空等</figcaption>
</figure>

<strong>分离式通信的四个特点</strong>：

- 各模块都有权申请占用总线，从模块准备好数据后可转变为主模块；
- 第二个子周期采用<strong>同步方式</strong>，用定宽定距时钟发送地址、数据和写命令；
- 各模块准备数据期间不占用总线；
- 总线被占用时一定在进行数据通信或传输控制命令，总线带宽得到充分利用。

> <strong>★考点</strong>：分离式通信的核心是把“设备准备数据”的空闲时间从总线周期中剥离出去，提高总线利用率。硬盘寻道、旋转等待是典型例子：机械动作耗时数十毫秒，真正传数据只需几微秒。

## 第4章 存储器

### 4.1 存储器的层次结构

计算机存储系统按“容量—速度—价格”构成层次：<strong>CPU 寄存器 → cache → 主存 → 辅存</strong>。越靠近 CPU，速度越快、容量越小、每位的价格越贵。

<figure class="cpd-figure not-content" id="lhw-w3-fig-1">
  <svg viewBox="0 0 700 266" role="img" aria-label="存储器的多维分类体系：按介质、存取方式、断电保持性与系统作用分类图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="238" rx="8"/>
    <rect class="fig-box-blue" x="28" y="48" width="154" height="162" rx="6"/>
    <rect class="fig-box-green" x="192" y="48" width="162" height="162" rx="6"/>
    <rect class="fig-box-orange" x="364" y="48" width="150" height="162" rx="6"/>
    <rect class="fig-box-purple" x="524" y="48" width="148" height="162" rx="6"/>
    <line class="fig-divider" x1="36" y1="74" x2="174" y2="74"/>
    <line class="fig-divider" x1="200" y1="74" x2="346" y2="74"/>
    <line class="fig-divider" x1="372" y1="74" x2="506" y2="74"/>
    <line class="fig-divider" x1="532" y1="74" x2="664" y2="74"/>
    <text class="fig-title" x="30" y="35" font-size="13">存储器的四维分类体系与核心物理属性</text>
    <text class="fig-blue" x="105" y="67" font-size="11.5" text-anchor="middle">① 按存储介质</text>
    <text class="fig-label" x="38" y="94" font-size="10.5">• 半导体：TTL / MOS</text>
    <text class="fig-muted" x="48" y="111" font-size="9.5">易集成（SRAM / DRAM）</text>
    <text class="fig-label" x="38" y="132" font-size="10.5">• 磁表面：磁盘 / 磁带</text>
    <text class="fig-muted" x="48" y="149" font-size="9.5">磁层剩磁极性记 0 / 1</text>
    <text class="fig-label" x="38" y="170" font-size="10.5">• 光盘介质：激光读写</text>
    <text class="fig-muted" x="48" y="187" font-size="9.5">凹坑反光或磁光材料</text>
    <text class="fig-green" x="273" y="67" font-size="11.5" text-anchor="middle">② 按存取方式</text>
    <text class="fig-label" x="202" y="94" font-size="10.5">• 随机存取（RAM/ROM）</text>
    <text class="fig-muted" x="212" y="111" font-size="9.5">耗时与物理位置无关</text>
    <text class="fig-label" x="202" y="132" font-size="10.5">• 顺序存取（磁带）</text>
    <text class="fig-muted" x="212" y="149" font-size="9.5">必须按线性次序扫描</text>
    <text class="fig-label" x="202" y="170" font-size="10.5">• 直接存取（硬磁盘）</text>
    <text class="fig-muted" x="212" y="187" font-size="9.5">先定位磁道，道内顺序</text>
    <text class="fig-orange" x="439" y="67" font-size="11.5" text-anchor="middle">③ 断电与读出特性</text>
    <text class="fig-label" x="374" y="94" font-size="10.5">• 易失性存储器</text>
    <text class="fig-muted" x="384" y="111" font-size="9.5">SRAM、DRAM 断电即失</text>
    <text class="fig-label" x="374" y="132" font-size="10.5">• 非易失性存储器</text>
    <text class="fig-muted" x="384" y="149" font-size="9.5">ROM、Flash、磁/光盘</text>
    <text class="fig-label" x="374" y="170" font-size="10.5">• 破坏性读出</text>
    <text class="fig-muted" x="384" y="187" font-size="9.5">单管 DRAM 读后须再生</text>
    <text class="fig-purple" x="598" y="67" font-size="11.5" text-anchor="middle">④ 按系统作用</text>
    <text class="fig-label" x="534" y="94" font-size="10.5">• 高速缓冲（Cache）</text>
    <text class="fig-muted" x="544" y="111" font-size="9.5">位于 CPU 与主存之间</text>
    <text class="fig-label" x="534" y="132" font-size="10.5">• 主存储器（内存）</text>
    <text class="fig-muted" x="544" y="149" font-size="9.5">运行中程序数据，直连CPU</text>
    <text class="fig-label" x="534" y="170" font-size="10.5">• 辅助存储器（外存）</text>
    <text class="fig-muted" x="544" y="187" font-size="9.5">属 I/O 外设，须调入主存</text>
    <rect class="fig-box" x="28" y="218" width="644" height="24" rx="4"/>
    <text class="fig-accent" x="350" y="234" font-size="10.5" text-anchor="middle">★ 408 概念陷阱：RAM 指“随机存取”而非“可读可写”——ROM 同样属于随机存取存储器（读取任意单元耗时相同）！</text>
  </svg>
  <figcaption>图 4-1　存储器的多维分类体系与易失性、随机/直接/顺序存取及破坏性读出辨析</figcaption>
</figure>

| 层次 | 器件 | 特点 | 解决的问题 |
|---|---|---|---|
| cache | SRAM | 速度快、集成度低、功耗大、价格贵，容量小 | 速度（cache–主存层次，硬件自动，对程序员透明） |
| 主存 | DRAM | 容量大、速度低 | 存放正在运行的程序和数据 |
| 辅存 | 磁盘、光盘、磁带 | 容量最大、速度最低、可脱机保存 | 容量（主存–辅存层次） |

<strong>辅存不能直接与 CPU 交换信息</strong>：程序要运行、数据要被使用，必须先调入主存，CPU 通过主存才能读写。

CPU 与主存的速度差称为<strong>存储墙</strong>：1985—2002 年，RISC 技术使 CPU 性能约以<strong>每年 52%</strong> 指数式提升；而内存约<strong>每 10 年访问延迟才降为原来的一半</strong>。CPU 取不到指令、取不到数据，只能空等，因此存储墙是影响整机性能的主要因素之一。

<figure class="cpd-figure not-content" id="lhw-w3-fig-2">
  <svg viewBox="0 0 700 310" role="img" aria-label="计算机存储层次金字塔与三级存储体系双层分工及存储墙示意图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="356" height="282" rx="8"/>
    <rect class="fig-box-subtle" x="384" y="14" width="300" height="282" rx="8"/>
    <line class="fig-line-accent" x1="40" y1="210" x2="40" y2="70" marker-end="url(#lhw-w3-arr-2)"/>
    <line class="fig-line" x1="308" y1="60" x2="308" y2="210" marker-end="url(#lhw-w3-arr-2)"/>
    <polygon class="fig-box-purple" points="118,58 234,58 244,92 108,92"/>
    <polygon class="fig-box-blue" points="104,96 248,96 260,130 92,130"/>
    <polygon class="fig-box-green" points="88,134 264,134 278,168 74,168"/>
    <polygon class="fig-box-orange" points="70,172 282,172 296,208 56,208"/>
    <rect class="fig-box" x="28" y="222" width="332" height="62" rx="5"/>
    <rect class="fig-box-blue" x="398" y="48" width="272" height="72" rx="6"/>
    <rect class="fig-box-green" x="398" y="130" width="272" height="72" rx="6"/>
    <rect class="fig-box-red" x="398" y="212" width="272" height="72" rx="6"/>
    <text class="fig-title" x="30" y="33" font-size="12">存储金字塔（越往上越靠近 CPU）</text>
    <text class="fig-purple" x="176" y="79" font-size="10.5" text-anchor="middle">CPU 寄存器（~ns）</text>
    <text class="fig-blue" x="176" y="117" font-size="11" text-anchor="middle">Cache（SRAM）</text>
    <text class="fig-green" x="176" y="155" font-size="11" text-anchor="middle">主存（DRAM）</text>
    <text class="fig-orange" x="176" y="194" font-size="11" text-anchor="middle">辅存（磁盘 / SSD / 光盘）</text>
    <text class="fig-accent" x="40" y="60" font-size="10" text-anchor="middle">快·贵</text>
    <text class="fig-muted" x="316" y="110" font-size="10.5">容量递增</text>
    <text class="fig-muted" x="316" y="130" font-size="10.5">速度递减</text>
    <text class="fig-muted" x="316" y="150" font-size="10.5">位价递减</text>
    <text class="fig-label" x="40" y="241" font-size="10.5">• CPU 只能直接读写：寄存器、Cache、主存</text>
    <text class="fig-muted" x="40" y="259" font-size="10">• 辅存信息必须先调入主存，CPU 才能访问</text>
    <text class="fig-muted" x="40" y="275" font-size="10">• 目标：接近 Cache 的速度 + 接近辅存的容量与位价</text>
    <text class="fig-title" x="398" y="36" font-size="12.5">两级存储层次与“存储墙”瓶颈</text>
    <text class="fig-blue" x="410" y="68" font-size="11.5">① Cache–主存层次（解决速度矛盾）</text>
    <text class="fig-label" x="410" y="87" font-size="10.5">数据块调入与替换完全由【纯硬件】自动完成</text>
    <text class="fig-muted" x="410" y="105" font-size="10">透明性：对系统程序员与应用程序员均透明</text>
    <text class="fig-green" x="410" y="150" font-size="11.5">② 主存–辅存层次（解决容量矛盾）</text>
    <text class="fig-label" x="410" y="169" font-size="10.5">由【硬件 MMU + 操作系统】协同构成虚拟存储器</text>
    <text class="fig-muted" x="410" y="187" font-size="10">透明性：对应用程序员透明，对系统程序员不透明</text>
    <text class="fig-red" x="410" y="232" font-size="11.5">③ 存储墙（Memory Wall）</text>
    <text class="fig-label" x="410" y="251" font-size="10.5">CPU 性能约 +52%/年 vs DRAM 延迟 10 年才减半</text>
    <text class="fig-muted" x="410" y="269" font-size="10">剪刀差拉大导致 CPU 频繁空等指令与操作数</text>
  </svg>
  <figcaption>图 4-2　计算机四级存储金字塔、两级存储层次（Cache–主存 vs 主存–辅存）透明性对比与存储墙成因</figcaption>
</figure>

### 4.2 半导体存储芯片的结构、容量与译码驱动

<strong>基本结构由四部分组成：存储矩阵（存放 0/1 代码）、译码驱动电路（把地址译成存储单元的选择信号）、读写电路（放大与驱动）、接口</strong>（与 CPU 及外设控制器相连的引线）。

| 引线 | 方向 | 作用 |
|---|---|---|
| 地址线（n 根） | 单向输入 | CPU 或外设控制器给出地址，选中存储矩阵中的一个（组）单元 |
| 数据线（m 根） | 双向 | 读时向外送出，写时向内送入 |
| 片选线 CS 或 CE | 输入，低电平有效 | 本次地址是否落在本芯片内 |
| 读写控制线 | 输入 | 一根 WE（低有效为写）；或 OE（低有效为读）+ WE（低有效为写） |

<strong>芯片容量</strong>由地址线条数与数据线条数共同确定：

$$
\text{Capacity} = 2^{n}\times m
$$

| 地址线 | 数据线 | 容量 |
|---|---|---|
| 10 | 4 | $2^{10}\times 4$ = 1K×4 |
| 14 | 1 | $2^{14}\times 1$ = 16K×1 |
| 13 | 8 | $2^{13}\times 8$ = 8K×8 |

<strong>片选的作用</strong>：机器中一条内存条上有许多芯片，本次访问落在哪个（哪些）芯片由片选决定。以 32 片 16K×1 芯片构成 64K×8 为例：每 8 片一组同时读写（各出一位，凑成 8 位），共 4 组；同组 8 片的片选连在一起，由地址高位译码产生。

| 组 | 地址范围 | 有效片选 |
|---|---|---|
| 第 1 组 | 0 ～ 16K−1 | CS0 |
| 第 2 组 | 16K ～ 32K−1 | CS1 |
| 第 3 组 | 32K ～ 48K−1 | CS2 |
| 第 4 组 | 48K ～ 64K−1 | CS3 |

地址 65535（=64K−1）落在第 4 组，只有 CS3 为低电平，其余三组片选为高电平（无效）。

<figure class="cpd-figure not-content" id="lhw-w3-fig-3">
  <svg viewBox="0 0 700 288" role="img" aria-label="半导体存储芯片内部四部件逻辑结构与引脚容量计算示意图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="464" height="260" rx="8"/>
    <rect class="fig-box-subtle" x="492" y="14" width="192" height="260" rx="8"/>
    <rect class="fig-box-dashed" x="128" y="44" width="276" height="160" rx="6"/>
    <line class="fig-line-accent" x1="32" y1="114" x2="140" y2="114" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line" x1="216" y1="96" x2="234" y2="96" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line" x1="216" y1="132" x2="234" y2="132" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line" x1="322" y1="114" x2="338" y2="114" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line-accent" x1="394" y1="106" x2="466" y2="106" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line-accent" x1="466" y1="122" x2="396" y2="122" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="179" y1="236" x2="179" y2="154" marker-end="url(#lhw-w3-arr-3)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="367" y1="236" x2="367" y2="154" marker-end="url(#lhw-w3-arr-3)"/>
    <rect class="fig-box-blue" x="142" y="76" width="74" height="76" rx="5"/>
    <rect class="fig-box" x="236" y="56" width="86" height="116" rx="4"/>
    <line class="fig-divider" x1="236" y1="85" x2="322" y2="85"/>
    <line class="fig-divider" x1="236" y1="114" x2="322" y2="114"/>
    <line class="fig-divider" x1="236" y1="143" x2="322" y2="143"/>
    <line class="fig-divider" x1="265" y1="56" x2="265" y2="172"/>
    <line class="fig-divider" x1="294" y1="56" x2="294" y2="172"/>
    <rect class="fig-box-green" x="265" y="85" width="29" height="29" rx="2"/>
    <rect class="fig-box-green" x="340" y="76" width="54" height="76" rx="5"/>
    <rect class="fig-box-accent" x="504" y="44" width="168" height="60" rx="5"/>
    <rect class="fig-box" x="504" y="114" width="168" height="148" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="12.5">半导体存储芯片内部四部件与对外引线</text>
    <text class="fig-accent" x="82" y="96" font-size="10.5" text-anchor="middle">地址线 A</text>
    <text class="fig-muted" x="82" y="132" font-size="9.5" text-anchor="middle">n 根（单向输入）</text>
    <text class="fig-blue" x="179" y="109" font-size="11.5" text-anchor="middle">译码</text>
    <text class="fig-blue" x="179" y="127" font-size="11.5" text-anchor="middle">驱动</text>
    <text class="fig-green" x="279" y="104" font-size="10" text-anchor="middle">0/1</text>
    <text class="fig-label" x="279" y="191" font-size="10.5" text-anchor="middle">存储矩阵</text>
    <text class="fig-green" x="367" y="109" font-size="11.5" text-anchor="middle">读写</text>
    <text class="fig-green" x="367" y="127" font-size="11.5" text-anchor="middle">电路</text>
    <text class="fig-accent" x="440" y="94" font-size="10.5" text-anchor="middle">数据线 D</text>
    <text class="fig-muted" x="440" y="140" font-size="9" text-anchor="middle">m 根（双向）</text>
    <text class="fig-purple" x="179" y="252" font-size="10.5" text-anchor="middle">片选 CS / CE（低有效）</text>
    <text class="fig-orange" x="367" y="252" font-size="10.5" text-anchor="middle">读写控制 WE / OE（低有效）</text>
    <text class="fig-muted" x="266" y="269" font-size="9.5" text-anchor="middle">虚线框：芯片封装边界（含对外接口引线）</text>
    <text class="fig-title" x="504" y="34" font-size="12.5">芯片容量与引脚核算</text>
    <text class="fig-accent" x="588" y="66" font-size="11.5" text-anchor="middle">芯片容量 = 2ⁿ × m</text>
    <text class="fig-muted" x="588" y="88" font-size="10" text-anchor="middle">单元数 2ⁿ × 位数 m</text>
    <text class="fig-label" x="514" y="136" font-size="10.5">最小数据/地址引脚和：</text>
    <text class="fig-blue" x="514" y="155" font-size="10">• 1K×4：10 + 4 = 14 根</text>
    <text class="fig-blue" x="514" y="173" font-size="10">• 8K×8：13 + 8 = 21 根</text>
    <text class="fig-orange" x="514" y="191" font-size="10">• 16K×1 DRAM（复用）：</text>
    <text class="fig-muted" x="522" y="207" font-size="9.5">地址减半 7 根 + 数据 1 根</text>
    <text class="fig-label" x="514" y="229" font-size="10">另加控制线：片选 1 根</text>
    <text class="fig-muted" x="510" y="247" font-size="8.8">+ 读写 1 根(WE)或 2 根(OE/WE)</text>
  </svg>
  <figcaption>图 4-3　半导体存储芯片内部四大部分逻辑框图、控制信号流向与引脚容量核算关系</figcaption>
</figure>

<strong>译码驱动方式</strong>有两种：

- <strong>线选法（单译码）</strong>：n 位地址经译码产生 $2^{n}$ 根字线，每根字线选中一整行单元。地址位数多时选择线数量急剧膨胀，只适合小容量。
- <strong>重合法（双译码）</strong>：地址分成行、列两组分别译码，行选择线与列选择线交叉处的单元被选中，选择线数约为 $2^{n_1}+2^{n_2}$，远少于 $2^{n_1+n_2}$。

<figure class="cpd-figure not-content" id="lhw-w3-fig-4">
  <svg viewBox="0 0 700 284" role="img" aria-label="线选法单译码与重合法双译码在1K乘1存储矩阵中的选择线数量对比示意图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="332" height="256" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="14" width="326" height="256" rx="8"/>
    <line class="fig-line" x1="24" y1="122" x2="60" y2="122" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="152" y1="76" x2="196" y2="76" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" x1="152" y1="104" x2="196" y2="104" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" x1="152" y1="168" x2="196" y2="168" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" x1="366" y1="112" x2="402" y2="112" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="480" y1="96" x2="504" y2="96" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" x1="480" y1="70" x2="504" y2="70"/>
    <line class="fig-line" x1="480" y1="146" x2="504" y2="146"/>
    <line class="fig-line" x1="566" y1="184" x2="566" y2="166" marker-end="url(#lhw-w3-arr-4)"/>
    <line class="fig-line" x1="468" y1="198" x2="496" y2="198" marker-end="url(#lhw-w3-arr-4)"/>
    <rect class="fig-box-red" x="62" y="66" width="90" height="114" rx="5"/>
    <rect class="fig-box" x="198" y="56" width="118" height="134" rx="4"/>
    <rect class="fig-box-red" x="206" y="66" width="102" height="20" rx="2"/>
    <rect class="fig-box" x="206" y="94" width="102" height="20" rx="2"/>
    <rect class="fig-box" x="206" y="158" width="102" height="20" rx="2"/>
    <rect class="fig-box-red" x="24" y="206" width="316" height="52" rx="5"/>
    <rect class="fig-box-blue" x="404" y="72" width="76" height="80" rx="5"/>
    <rect class="fig-box" x="504" y="54" width="124" height="110" rx="4"/>
    <line class="fig-line-dashed" x1="504" y1="96" x2="628" y2="96"/>
    <line class="fig-line-dashed" x1="566" y1="54" x2="566" y2="140"/>
    <rect class="fig-box-accent" x="551" y="84" width="30" height="24" rx="3"/>
    <rect class="fig-box-green" x="498" y="184" width="136" height="28" rx="4"/>
    <rect class="fig-box-green" x="366" y="218" width="310" height="42" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">（a）线选法（1D 单译码）：1K×1 实例</text>
    <text class="fig-muted" x="38" y="108" font-size="9" text-anchor="middle">A9..A0</text>
    <text class="fig-muted" x="38" y="140" font-size="9" text-anchor="middle">10 位</text>
    <text class="fig-red" x="107" y="114" font-size="11.5" text-anchor="middle">行译码器</text>
    <text class="fig-label" x="107" y="134" font-size="10.5" text-anchor="middle">10 → 1024</text>
    <text class="fig-red" x="174" y="71" font-size="9.5" text-anchor="middle">W0</text>
    <text class="fig-muted" x="174" y="99" font-size="9.5" text-anchor="middle">W1</text>
    <text class="fig-muted" x="174" y="138" font-size="11" text-anchor="middle">⋮</text>
    <text class="fig-muted" x="174" y="164" font-size="9" text-anchor="middle">W1023</text>
    <text class="fig-red" x="257" y="80" font-size="10" text-anchor="middle">字单元 0（选中）</text>
    <text class="fig-muted" x="257" y="108" font-size="10" text-anchor="middle">字单元 1</text>
    <text class="fig-muted" x="257" y="140" font-size="11" text-anchor="middle">⋮</text>
    <text class="fig-muted" x="257" y="172" font-size="10" text-anchor="middle">字单元 1023</text>
    <text class="fig-red" x="182" y="225" font-size="9.5" text-anchor="middle">选择线数 = 2¹⁰ = 1024 根（1M 容量高达 10⁶ 根）</text>
    <text class="fig-muted" x="182" y="244" font-size="9.2" text-anchor="middle">译码线随地址位数指数暴涨，仅适用于极小容量存储阵列</text>
    <text class="fig-title" x="370" y="36" font-size="12.5">（b）重合法（2D 双译码）：1K×1 实例</text>
    <text class="fig-muted" x="382" y="98" font-size="8.8" text-anchor="middle">行A4..0</text>
    <text class="fig-blue" x="442" y="106" font-size="11" text-anchor="middle">X 行译码</text>
    <text class="fig-label" x="442" y="124" font-size="10" text-anchor="middle">5 → 32 根</text>
    <text class="fig-muted" x="462" y="202" font-size="9.2" text-anchor="end">列A9..5</text>
    <text class="fig-green" x="566" y="202" font-size="9.8" text-anchor="middle">Y 列译码（5 → 32 根）</text>
    <text class="fig-accent" x="566" y="100" font-size="9" text-anchor="middle">交叉</text>
    <text class="fig-muted" x="566" y="154" font-size="9.5" text-anchor="middle">32 行 × 32 列矩阵</text>
    <text class="fig-blue" x="655" y="100" font-size="9.5" text-anchor="middle">Xi=1</text>
    <text class="fig-green" x="655" y="120" font-size="9.5" text-anchor="middle">Yj=1</text>
    <text class="fig-muted" x="655" y="138" font-size="9" text-anchor="middle">共选中</text>
    <text class="fig-green" x="521" y="236" font-size="9.5" text-anchor="middle">选择线总数 = 2⁵ + 2⁵ = 64 根（仅为线选法的 1/16）</text>
    <text class="fig-muted" x="521" y="252" font-size="9.2" text-anchor="middle">公式：2ⁿ¹ + 2ⁿ² ≪ 2ⁿ¹⁺ⁿ²，被现代芯片普遍采用</text>
  </svg>
  <figcaption>图 4-4　两种译码驱动方式对比：线选法（单译码 $2^n$ 根线）vs 重合法（双译码 $2^{n_1}+2^{n_2}$ 根线，大幅压缩芯片面积）</figcaption>
</figure>

<strong>2114 芯片（1K×4）实例</strong>：存储矩阵为 <strong>64 行 × 64 列</strong>，64 列分成 4 组、每组 16 列。行地址 6 位（译出 64 根行选择线），列地址 4 位（在每组 16 列中选一列）。一次读写同时选中 4 组中相同列号的 4 个单元，正好输出 4 位。

写操作过程：行地址有效 → 第 0 行上所有单元电路被选中；列地址有效 → 各组对应列的列开关打开；4 个交叉点单元被选中；WE 低电平、CS 低电平；数据由 I/O1～I/O4 经读写电路写入各单元的 a 端与 a′ 端，4 个单元对应 4 位二进制数。

<figure class="cpd-figure not-content" id="lhw-w3-fig-5">
  <svg viewBox="0 0 700 302" role="img" aria-label="2114芯片1K乘4重合法存储矩阵64行乘4组乘16列与4位并行读写电路图">
    <defs>
      <marker id="lhw-w3-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="14" width="668" height="274" rx="8"/>
    <line class="fig-line" x1="30" y1="112" x2="54" y2="112" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="152" y1="96" x2="180" y2="96" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" x1="152" y1="138" x2="180" y2="138" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="230" y1="220" x2="230" y2="196" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="335" y1="220" x2="335" y2="196" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="440" y1="220" x2="440" y2="196" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="545" y1="220" x2="545" y2="196" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line" x1="230" y1="220" x2="545" y2="220"/>
    <line class="fig-line" x1="388" y1="234" x2="388" y2="220"/>
    <line class="fig-line-accent" x1="212" y1="198" x2="212" y2="260" marker-start="url(#lhw-w3-arr-5)" marker-end="url(#lhw-w3-arr-5)"/>
    <line class="fig-line-accent" x1="569" y1="198" x2="569" y2="260" marker-start="url(#lhw-w3-arr-5)" marker-end="url(#lhw-w3-arr-5)"/>
    <rect class="fig-box-blue" x="56" y="76" width="96" height="76" rx="5"/>
    <rect class="fig-box" x="182" y="50" width="420" height="144" rx="5"/>
    <line class="fig-divider" x1="287" y1="50" x2="287" y2="162"/>
    <line class="fig-divider" x1="392" y1="50" x2="392" y2="162"/>
    <line class="fig-divider" x1="497" y1="50" x2="497" y2="162"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="182" y1="96" x2="602" y2="96"/>
    <line class="fig-line-dashed" x1="230" y1="74" x2="230" y2="162"/>
    <line class="fig-line-dashed" x1="335" y1="74" x2="335" y2="162"/>
    <line class="fig-line-dashed" x1="440" y1="74" x2="440" y2="162"/>
    <line class="fig-line-dashed" x1="545" y1="74" x2="545" y2="162"/>
    <rect class="fig-box-red" x="218" y="86" width="24" height="20" rx="3"/>
    <rect class="fig-box-red" x="323" y="86" width="24" height="20" rx="3"/>
    <rect class="fig-box-red" x="428" y="86" width="24" height="20" rx="3"/>
    <rect class="fig-box-red" x="533" y="86" width="24" height="20" rx="3"/>
    <rect class="fig-box-green" x="300" y="234" width="176" height="38" rx="5"/>
    <rect class="fig-box" x="24" y="218" width="176" height="54" rx="5"/>
    <text class="fig-title" x="30" y="36" font-size="12.5">Intel 2114（1K×4 SRAM）：64 行 ×（4 组 × 16 列）重合法矩阵</text>
    <text class="fig-muted" x="42" y="102" font-size="9.5" text-anchor="middle">行A</text>
    <text class="fig-muted" x="42" y="128" font-size="9.5" text-anchor="middle">6位</text>
    <text class="fig-blue" x="104" y="108" font-size="11.5" text-anchor="middle">行地址译码</text>
    <text class="fig-label" x="104" y="128" font-size="10.5" text-anchor="middle">6 → 64 行</text>
    <text class="fig-muted" x="234" y="68" font-size="10" text-anchor="middle">组 0（16 列）</text>
    <text class="fig-muted" x="339" y="68" font-size="10" text-anchor="middle">组 1（16 列）</text>
    <text class="fig-muted" x="444" y="68" font-size="10" text-anchor="middle">组 2（16 列）</text>
    <text class="fig-muted" x="549" y="68" font-size="10" text-anchor="middle">组 3（16 列）</text>
    <text class="fig-red" x="230" y="100" font-size="9" text-anchor="middle">D0</text>
    <text class="fig-red" x="335" y="100" font-size="9" text-anchor="middle">D1</text>
    <text class="fig-red" x="440" y="100" font-size="9" text-anchor="middle">D2</text>
    <text class="fig-red" x="545" y="100" font-size="9" text-anchor="middle">D3</text>
    <text class="fig-red" x="640" y="94" font-size="10" text-anchor="middle">第 0 行有效</text>
    <text class="fig-muted" x="640" y="112" font-size="9.5" text-anchor="middle">（共 64 行）</text>
    <text class="fig-muted" x="392" y="178" font-size="10" text-anchor="middle">每次选中 4 组中相同列号的 4 个交叉单元，并行完成 4 位读写</text>
    <text class="fig-green" x="388" y="250" font-size="11" text-anchor="middle">列地址译码（4 → 16 列）</text>
    <text class="fig-muted" x="388" y="265" font-size="9.2" text-anchor="middle">4 位列地址 + WE + CS 控制列开关</text>
    <text class="fig-accent" x="212" y="276" font-size="10" text-anchor="middle">I/O1</text>
    <text class="fig-accent" x="569" y="276" font-size="10" text-anchor="middle">I/O4</text>
    <text class="fig-label" x="30" y="238" font-size="8.6">地址总数 = 6 + 4 = 10 根（1K）</text>
    <text class="fig-muted" x="30" y="254" font-size="8.6">选择线数 = 64 + 16 = 80 根</text>
    <text class="fig-muted" x="30" y="268" font-size="8.6">数据引线：I/O1～I/O4 共 4 根</text>
  </svg>
  <figcaption>图 4-5　Intel 2114（$1\text{K}\times 4$）内部存储矩阵分组结构：6 位行译码与 4 位列译码同时选中 4 组同列单元</figcaption>
</figure>

### 4.3 SRAM 与 DRAM 的存储元

| 项目 | SRAM（静态） | DRAM（动态） |
|---|---|---|
| 存储原理 | 双稳态触发器（六管：两管交叉耦合 + 两个负载管 + 两个门控管） | 电容存储电荷：有电为 1，无电为 0 |
| 读出 | 非破坏性读出 | 读出破坏信息，需再生（刷新） |
| 速度 | 快 | 慢 |
| 集成度 | 低 | 高 |
| 功耗 | 大 | 小 |
| 价格 | 贵 | 便宜 |
| 用途 | cache | 主存 |

<strong>SRAM 单元</strong>：T1、T2 交叉耦合构成双稳态，T5、T6 为门控管，由字线控制把单元与位线 BL、BL′ 接通。读出时位线电压只有微小变化，由读出放大器放大。

<strong>DRAM 三管单元</strong>：T1 为存储管（栅极接电容 Cg），T2 为读选择管，T3 为写选择管，T4 为预充电管。

- <strong>读：先由预充电信号使 T4 导通，VDD 把读数据线充为高电平；读选择线有效使 T2 导通。若 Cg 有电荷，T1 导通，读数据线经 T2、T1 放电为低；若 Cg 无电荷，T1 截止，读数据线保持高电平。读出值与存储值相反，输出端必须加非门。</strong>
- <strong>写：写选择线有效使 T3 导通，写数据线经 T3 对 Cg 充电（写 1）或放电（写 0），写入值与输入相同。</strong>

<strong>DRAM 单管单元</strong>：只有一个电容和一个门控管，字线控制管子的通断。读时若电容有电荷，位线上有电流；无电荷则无电流，由此判别 1/0；写时对电容充电或放电。单管单元集成度最高，但读出信号弱，需要灵敏的读出放大器。

<figure class="cpd-figure not-content" id="lhw-w3-fig-6">
  <svg viewBox="0 0 700 292" role="img" aria-label="SRAM六管双稳态触发器存储元电路结构与字线位线差分读写原理图">
    <defs>
      <marker id="lhw-w3-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="14" width="444" height="264" rx="8"/>
    <rect class="fig-box-subtle" x="472" y="14" width="212" height="264" rx="8"/>
    <line class="fig-line-accent" x1="108" y1="60" x2="108" y2="236"/>
    <line class="fig-line-accent" x1="376" y1="60" x2="376" y2="236"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="36" y1="92" x2="438" y2="92"/>
    <line class="fig-line" x1="110" y1="152" x2="164" y2="152" marker-start="url(#lhw-w3-arr-6)" marker-end="url(#lhw-w3-arr-6)"/>
    <line class="fig-line" x1="374" y1="152" x2="322" y2="152" marker-start="url(#lhw-w3-arr-6)" marker-end="url(#lhw-w3-arr-6)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="236" y1="142" x2="248" y2="142" marker-end="url(#lhw-w3-arr-6)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 285 174 L 285 202 L 201 202 L 201 176" marker-end="url(#lhw-w3-arr-6)"/>
    <line class="fig-line-dashed" x1="108" y1="224" x2="206" y2="224"/>
    <line class="fig-line-dashed" x1="376" y1="224" x2="278" y2="224"/>
    <rect class="fig-box-purple" x="92" y="78" width="32" height="28" rx="4"/>
    <rect class="fig-box-purple" x="360" y="78" width="32" height="28" rx="4"/>
    <rect class="fig-box-blue" x="166" y="128" width="70" height="46" rx="5"/>
    <rect class="fig-box-blue" x="250" y="128" width="70" height="46" rx="5"/>
    <rect class="fig-box-green" x="206" y="210" width="72" height="28" rx="4"/>
    <rect class="fig-box-blue" x="484" y="48" width="188" height="102" rx="5"/>
    <rect class="fig-box-orange" x="484" y="160" width="188" height="104" rx="5"/>
    <text class="fig-title" x="30" y="32" font-size="12.5">SRAM 六管存储元（6T 双稳态触发器电路）</text>
    <text class="fig-accent" x="108" y="54" font-size="10.5" text-anchor="middle">位线 BL</text>
    <text class="fig-accent" x="376" y="54" font-size="10.5" text-anchor="middle">位线 BL′</text>
    <text class="fig-purple" x="62" y="85" font-size="10" text-anchor="middle">字线 WL</text>
    <text class="fig-purple" x="108" y="96" font-size="10.5" text-anchor="middle">T5</text>
    <text class="fig-purple" x="376" y="96" font-size="10.5" text-anchor="middle">T6</text>
    <text class="fig-label" x="136" y="146" font-size="10" text-anchor="middle">a 端(1)</text>
    <text class="fig-label" x="348" y="146" font-size="10" text-anchor="middle">a′ 端(0)</text>
    <text class="fig-blue" x="201" y="147" font-size="10.2" text-anchor="middle">工作管 T1</text>
    <text class="fig-muted" x="201" y="164" font-size="8.8" text-anchor="middle">+ 负载管 T3</text>
    <text class="fig-blue" x="285" y="147" font-size="10.2" text-anchor="middle">工作管 T2</text>
    <text class="fig-muted" x="285" y="164" font-size="8.8" text-anchor="middle">+ 负载管 T4</text>
    <text class="fig-green" x="242" y="196" font-size="9.5" text-anchor="middle">正反馈锁存</text>
    <text class="fig-green" x="242" y="228" font-size="10" text-anchor="middle">读出放大器</text>
    <text class="fig-muted" x="242" y="260" font-size="10" text-anchor="middle">WL 高电平时 T5、T6 导通，将双稳态 a / a′ 接入差分位线 BL / BL′</text>
    <text class="fig-title" x="486" y="36" font-size="12.5">6 管分工与 408 考点</text>
    <text class="fig-blue" x="494" y="68" font-size="10.5">• 触发器核心（4 管）：</text>
    <text class="fig-label" x="502" y="86" font-size="10">T1、T2 交叉耦合存 0/1</text>
    <text class="fig-label" x="502" y="103" font-size="10">T3、T4 作有源负载供电</text>
    <text class="fig-blue" x="494" y="122" font-size="10.5">• 门控开关（2 管）：</text>
    <text class="fig-muted" x="502" y="139" font-size="9.5">T5、T6 受字线 WL 控制</text>
    <text class="fig-orange" x="494" y="180" font-size="10.5">• 为什么用作 Cache？</text>
    <text class="fig-label" x="502" y="198" font-size="10">1. 读出非破坏，无需刷新</text>
    <text class="fig-label" x="502" y="216" font-size="10">2. 双稳态翻转极快</text>
    <text class="fig-muted" x="502" y="234" font-size="9.5">代价：6 管占地大、集成度低、</text>
    <text class="fig-muted" x="502" y="250" font-size="9.5">静态导通功耗高、价格昂贵</text>
  </svg>
  <figcaption>图 4-6　SRAM 六管（6T）双稳态触发器存储元电路：非破坏性读出、无需刷新，专用于高速 Cache</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w3-fig-7">
  <svg viewBox="0 0 700 306" role="img" aria-label="DRAM三管存储元与单管1T1C存储元电路原理及读写特性对比图">
    <defs>
      <marker id="lhw-w3-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="14" width="336" height="278" rx="8"/>
    <rect class="fig-box-subtle" x="364" y="14" width="320" height="278" rx="8"/>
    <line class="fig-line-accent" x1="48" y1="74" x2="282" y2="74"/>
    <line class="fig-line-accent" x1="326" y1="74" x2="344" y2="74" marker-end="url(#lhw-w3-arr-7)"/>
    <line class="fig-line" x1="74" y1="44" x2="74" y2="52"/>
    <line class="fig-line" x1="74" y1="68" x2="74" y2="74"/>
    <line class="fig-line" x1="168" y1="74" x2="168" y2="92"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="36" y1="104" x2="152" y2="104"/>
    <line class="fig-line" x1="168" y1="116" x2="168" y2="132"/>
    <line class="fig-line" x1="152" y1="144" x2="128" y2="144"/>
    <line class="fig-line" x1="128" y1="144" x2="128" y2="166"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="112" y1="166" x2="144" y2="166"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="112" y1="173" x2="144" y2="173"/>
    <line class="fig-line" x1="128" y1="173" x2="128" y2="190"/>
    <line class="fig-line" x1="118" y1="190" x2="138" y2="190"/>
    <line class="fig-line" x1="168" y1="156" x2="168" y2="190"/>
    <line class="fig-line" x1="158" y1="190" x2="178" y2="190"/>
    <line class="fig-line" x1="78" y1="184" x2="78" y2="144"/>
    <line class="fig-line" x1="78" y1="144" x2="128" y2="144"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="36" y1="196" x2="62" y2="196"/>
    <line class="fig-line" x1="78" y1="208" x2="78" y2="222"/>
    <line class="fig-line-accent" x1="48" y1="222" x2="328" y2="222"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="390" y1="88" x2="660" y2="88"/>
    <line class="fig-line-accent" x1="496" y1="60" x2="496" y2="196"/>
    <line class="fig-line-accent" x1="496" y1="120" x2="530" y2="120"/>
    <circle class="fig-dot" cx="496" cy="120" r="2.5"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="546" y1="88" x2="546" y2="108"/>
    <circle class="fig-dot" cx="546" cy="88" r="2.5"/>
    <line class="fig-line" x1="546" y1="132" x2="546" y2="154"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="528" y1="154" x2="564" y2="154"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="528" y1="162" x2="564" y2="162"/>
    <line class="fig-line" x1="546" y1="162" x2="546" y2="184"/>
    <line class="fig-line" x1="534" y1="184" x2="558" y2="184"/>
    <rect class="fig-box" x="60" y="52" width="28" height="16" rx="3"/>
    <rect class="fig-box-purple" x="152" y="92" width="32" height="24" rx="3"/>
    <rect class="fig-box-blue" x="152" y="132" width="32" height="24" rx="3"/>
    <rect class="fig-box-orange" x="62" y="184" width="32" height="24" rx="3"/>
    <rect class="fig-box-red" x="282" y="62" width="44" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="236" width="316" height="44" rx="4"/>
    <rect class="fig-box-purple" x="530" y="108" width="32" height="24" rx="3"/>
    <rect class="fig-box-green" x="448" y="196" width="96" height="28" rx="4"/>
    <rect class="fig-box" x="374" y="236" width="302" height="44" rx="4"/>
    <text class="fig-title" x="30" y="32" font-size="12.5">（a）DRAM 三管单元（1103 芯片采用）</text>
    <text class="fig-muted" x="42" y="50" font-size="9">VDD</text>
    <text class="fig-label" x="74" y="63" font-size="9" text-anchor="middle">T4</text>
    <text class="fig-muted" x="112" y="66" font-size="9">预充电</text>
    <text class="fig-accent" x="234" y="66" font-size="10" text-anchor="middle">读数据线</text>
    <text class="fig-red" x="304" y="78" font-size="9.5" text-anchor="middle">非门</text>
    <text class="fig-purple" x="88" y="98" font-size="9.5" text-anchor="middle">读选择线</text>
    <text class="fig-purple" x="168" y="108" font-size="10" text-anchor="middle">T2</text>
    <text class="fig-blue" x="168" y="148" font-size="10" text-anchor="middle">T1</text>
    <text class="fig-muted" x="224" y="148" font-size="9.5">存储管(栅极电容)</text>
    <text class="fig-green" x="104" y="173" font-size="10" text-anchor="end">Cg</text>
    <text class="fig-orange" x="56" y="189" font-size="9.5" text-anchor="end">写选择</text>
    <text class="fig-orange" x="78" y="200" font-size="10" text-anchor="middle">T3</text>
    <text class="fig-accent" x="234" y="215" font-size="10" text-anchor="middle">写数据线</text>
    <text class="fig-label" x="40" y="253" font-size="10">• 读出与原存值【反相】（Cg 有电→T1 导通放电→输出需非门）</text>
    <text class="fig-muted" x="40" y="270" font-size="9.5">• 读/写选择线与数据线分离；读出不泄放 Cg，但漏电仍须定期刷新</text>
    <text class="fig-title" x="380" y="32" font-size="12.5">（b）DRAM 单管 1T1C 单元（4116 及现代主存）</text>
    <text class="fig-accent" x="496" y="54" font-size="10" text-anchor="middle">位线（数据线）</text>
    <text class="fig-purple" x="618" y="80" font-size="10" text-anchor="middle">字线 WL</text>
    <text class="fig-purple" x="546" y="124" font-size="10" text-anchor="middle">T</text>
    <text class="fig-muted" x="606" y="124" font-size="9.5" text-anchor="middle">门控管</text>
    <text class="fig-green" x="582" y="162" font-size="10.5">存储电容 Cs</text>
    <text class="fig-muted" x="614" y="178" font-size="9.5" text-anchor="middle">有电荷=1 / 无=0</text>
    <text class="fig-green" x="496" y="214" font-size="10" text-anchor="middle">读出/再生放大器</text>
    <text class="fig-label" x="382" y="253" font-size="9.2">• 破坏性读出：WL 导通时 Cs 向位线放电，读后必须立即再生回写</text>
    <text class="fig-muted" x="382" y="270" font-size="9.2">• 仅 1 管 1 电容，集成度极高、功耗极低，是大容量主存首选</text>
  </svg>
  <figcaption>图 4-7　DRAM 三管单元（读写分离、读出反相）与单管 1T1C 单元（破坏性读出、集成度最高）电路对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w3-fig-8">
  <svg viewBox="0 0 700 236" role="img" aria-label="只读存储器ROM五代技术演进MROM至Flash快闪存储器对比图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="208" rx="8"/>
    <line class="fig-line-accent" x1="142" y1="108" x2="154" y2="108" marker-end="url(#lhw-w3-arr-8)"/>
    <line class="fig-line-accent" x1="272" y1="108" x2="284" y2="108" marker-end="url(#lhw-w3-arr-8)"/>
    <line class="fig-line-accent" x1="402" y1="108" x2="414" y2="108" marker-end="url(#lhw-w3-arr-8)"/>
    <line class="fig-line-accent" x1="538" y1="108" x2="550" y2="108" marker-end="url(#lhw-w3-arr-8)"/>
    <rect class="fig-box" x="26" y="50" width="116" height="116" rx="5"/>
    <rect class="fig-box-blue" x="156" y="50" width="116" height="116" rx="5"/>
    <rect class="fig-box-purple" x="286" y="50" width="116" height="116" rx="5"/>
    <rect class="fig-box-orange" x="416" y="50" width="122" height="116" rx="5"/>
    <rect class="fig-box-green" x="552" y="50" width="120" height="116" rx="5"/>
    <rect class="fig-box" x="26" y="176" width="646" height="34" rx="4"/>
    <text class="fig-title" x="30" y="36" font-size="12.5">补充知识链：只读存储器（ROM）家族从“固定掩模”向“快闪在线可擦写”的五代演进</text>
    <text class="fig-label" x="84" y="70" font-size="11" text-anchor="middle">① MROM</text>
    <text class="fig-muted" x="84" y="88" font-size="9.5" text-anchor="middle">掩模 ROM</text>
    <text class="fig-muted" x="84" y="110" font-size="9.5" text-anchor="middle">厂家光刻写入</text>
    <text class="fig-muted" x="84" y="126" font-size="9.5" text-anchor="middle">用户不可更改</text>
    <text class="fig-muted" x="84" y="148" font-size="9" text-anchor="middle">可靠性高·大批量便宜</text>
    <text class="fig-blue" x="214" y="70" font-size="11" text-anchor="middle">② PROM</text>
    <text class="fig-muted" x="214" y="88" font-size="9.5" text-anchor="middle">一次性可编程</text>
    <text class="fig-label" x="214" y="110" font-size="9.5" text-anchor="middle">熔丝烧断写 1 次</text>
    <text class="fig-muted" x="214" y="126" font-size="9.5" text-anchor="middle">写入后不可擦除</text>
    <text class="fig-muted" x="214" y="148" font-size="9" text-anchor="middle">满足用户定制需求</text>
    <text class="fig-purple" x="344" y="70" font-size="11" text-anchor="middle">③ EPROM</text>
    <text class="fig-muted" x="344" y="88" font-size="9.5" text-anchor="middle">光可擦可编程</text>
    <text class="fig-label" x="344" y="110" font-size="9.5" text-anchor="middle">浮栅雪崩注入</text>
    <text class="fig-purple" x="344" y="126" font-size="9.5" text-anchor="middle">紫外线整片擦除</text>
    <text class="fig-muted" x="344" y="148" font-size="9" text-anchor="middle">须拆下芯片照射擦除</text>
    <text class="fig-orange" x="477" y="70" font-size="11" text-anchor="middle">④ EEPROM</text>
    <text class="fig-muted" x="477" y="88" font-size="9.5" text-anchor="middle">电可擦可编程</text>
    <text class="fig-label" x="477" y="110" font-size="9.5" text-anchor="middle">联机脉冲电擦除</text>
    <text class="fig-orange" x="477" y="126" font-size="9.5" text-anchor="middle">可按字节选择擦写</text>
    <text class="fig-muted" x="477" y="148" font-size="9" text-anchor="middle">工艺较复杂·密度偏低</text>
    <text class="fig-green" x="612" y="70" font-size="11" text-anchor="middle">⑤ Flash</text>
    <text class="fig-muted" x="612" y="88" font-size="9.5" text-anchor="middle">快闪存储器</text>
    <text class="fig-label" x="612" y="110" font-size="9.5" text-anchor="middle">电整块快速擦除</text>
    <text class="fig-green" x="612" y="126" font-size="9.5" text-anchor="middle">U 盘 / SSD 基石</text>
    <text class="fig-muted" x="612" y="148" font-size="9" text-anchor="middle">写前须先擦·读快写慢</text>
    <text class="fig-accent" x="349" y="197" font-size="10.5" text-anchor="middle">★ 演进主线：全部保持【非易失性 + 随机存取】，逐步具备在线电擦写能力，模糊了传统 ROM 与辅存（SSD）的界限</text>
  </svg>
  <figcaption>图 4-8　ROM 家族五代演进对比（MROM → PROM → EPROM → EEPROM → Flash 快闪存储器）</figcaption>
</figure>

### 4.4 DRAM 的刷新与典型芯片

<strong>为什么刷新</strong>：DRAM 用电容上的电荷表示信息，电容会漏电，一段时间后信息消失。芯片内设有<strong>刷新放大器</strong>（再生放大器），每隔固定时间（刷新周期，常见 2 ms）把每个单元的信息读出、放大、再写回。

刷新按<strong>行</strong>进行：只需行地址，由刷新计数器逐行给出；刷新不经过列译码，不向 CPU 送数据，也不改变存储内容——<strong>刷新不是重写（改写）</strong>。

| 方式 | 做法 | 特点 |
|---|---|---|
| 集中刷新 | 在刷新周期内用一段时间集中刷新全部行 | 存在<strong>死区</strong>（刷新期间不能访存），存取周期不变 |
| 分散刷新 | 每个存取周期后刷新一行 | 无死区，但系统存取周期变为“存取 + 刷新”，速度降低 |
| 异步刷新 | 把全部行的刷新均匀分散在刷新周期内 | 死区最短，兼顾速度与刷新 |

<figure class="cpd-figure not-content" id="lhw-w3-fig-9">
  <svg viewBox="0 0 700 316" role="img" aria-label="DRAM集中刷新分散刷新与异步刷新三种方式在2ms刷新周期内的时序与死区对比图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="288" rx="8"/>
    <line class="fig-line" x1="112" y1="84" x2="664" y2="84" marker-end="url(#lhw-w3-arr-9)"/>
    <line class="fig-line" x1="112" y1="162" x2="664" y2="162" marker-end="url(#lhw-w3-arr-9)"/>
    <line class="fig-line" x1="112" y1="240" x2="664" y2="240" marker-end="url(#lhw-w3-arr-9)"/>
    <rect class="fig-box-blue" x="112" y="56" width="380" height="24" rx="3"/>
    <rect class="fig-box-red" x="492" y="56" width="154" height="24" rx="3"/>
    <rect class="fig-box-blue" x="112" y="134" width="46" height="24" rx="3"/>
    <rect class="fig-box-orange" x="158" y="134" width="46" height="24" rx="3"/>
    <rect class="fig-box-blue" x="204" y="134" width="46" height="24" rx="3"/>
    <rect class="fig-box-orange" x="250" y="134" width="46" height="24" rx="3"/>
    <rect class="fig-box-blue" x="296" y="134" width="46" height="24" rx="3"/>
    <rect class="fig-box-orange" x="342" y="134" width="46" height="24" rx="3"/>
    <rect class="fig-box" x="396" y="134" width="250" height="24" rx="3"/>
    <rect class="fig-box-blue" x="112" y="212" width="106" height="24" rx="3"/>
    <rect class="fig-box-green" x="218" y="212" width="24" height="24" rx="3"/>
    <rect class="fig-box-blue" x="242" y="212" width="106" height="24" rx="3"/>
    <rect class="fig-box-green" x="348" y="212" width="24" height="24" rx="3"/>
    <rect class="fig-box-blue" x="372" y="212" width="106" height="24" rx="3"/>
    <rect class="fig-box-green" x="478" y="212" width="24" height="24" rx="3"/>
    <rect class="fig-box" x="512" y="212" width="134" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="266" width="644" height="26" rx="4"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">DRAM 三种刷新方式时序对比（例：128×128 矩阵，存取周期 0.5 μs，刷新周期 2 ms = 4000 周期）</text>
    <text class="fig-label" x="28" y="72" font-size="11.5">① 集中刷新</text>
    <text class="fig-blue" x="302" y="72" font-size="10.5" text-anchor="middle">正常读写 3872 周期（1936 μs）</text>
    <text class="fig-red" x="569" y="72" font-size="10" text-anchor="middle">集中刷 128 行（64 μs）</text>
    <text class="fig-muted" x="302" y="100" font-size="9.5" text-anchor="middle">读写期间全速运行，不受刷新干扰</text>
    <text class="fig-red" x="569" y="100" font-size="9.5" text-anchor="middle">★ 死区 64 μs（CPU/DMA 禁止访存）</text>
    <text class="fig-label" x="28" y="150" font-size="11.5">② 分散刷新</text>
    <text class="fig-blue" x="135" y="150" font-size="9" text-anchor="middle">tM 0.5</text>
    <text class="fig-orange" x="181" y="150" font-size="9" text-anchor="middle">tR 0.5</text>
    <text class="fig-blue" x="227" y="150" font-size="9" text-anchor="middle">tM 0.5</text>
    <text class="fig-orange" x="273" y="150" font-size="9" text-anchor="middle">tR 0.5</text>
    <text class="fig-blue" x="319" y="150" font-size="9" text-anchor="middle">tM 0.5</text>
    <text class="fig-orange" x="365" y="150" font-size="9" text-anchor="middle">tR 0.5</text>
    <text class="fig-muted" x="521" y="150" font-size="9.5" text-anchor="middle">每 128 μs 即刷完一遍（2 ms 刷 15.6 遍）</text>
    <text class="fig-orange" x="378" y="178" font-size="9.5" text-anchor="middle">系统存取周期拉长一倍 tC = tM + tR = 1 μs：彻底消除死区，但过度刷新拖慢整机速度</text>
    <text class="fig-label" x="28" y="228" font-size="11.5">③ 异步刷新</text>
    <text class="fig-blue" x="165" y="228" font-size="9.5" text-anchor="middle">读写 15.1 μs</text>
    <text class="fig-green" x="230" y="228" font-size="8.5" text-anchor="middle">刷</text>
    <text class="fig-blue" x="295" y="228" font-size="9.5" text-anchor="middle">读写 15.1 μs</text>
    <text class="fig-green" x="360" y="228" font-size="8.5" text-anchor="middle">刷</text>
    <text class="fig-blue" x="425" y="228" font-size="9.5" text-anchor="middle">读写 15.1 μs</text>
    <text class="fig-green" x="490" y="228" font-size="8.5" text-anchor="middle">刷</text>
    <text class="fig-muted" x="579" y="228" font-size="9.5" text-anchor="middle">共均匀刷完 128 行</text>
    <text class="fig-green" x="378" y="256" font-size="9.5" text-anchor="middle">间隔 Δt = 2 ms ÷ 128 ≈ 15.6 μs 刷 1 行：死区缩至 0.5 μs；若安排在 CPU 指令译码期则变为“透明刷新”（无死区）</text>
    <text class="fig-accent" x="350" y="283" font-size="9.5" text-anchor="middle">★ 408 必考：刷新按【行】进行（刷新计数器产行地址），不走列译码、不占数据总线，对程序员透明且无须 CPU 参与读出！</text>
  </svg>
  <figcaption>图 4-9　DRAM 集中刷新、分散刷新与异步刷新在 $2\,\text{ms}$ 周期内的时序分布与死区定量对比</figcaption>
</figure>

<strong>1103（三管，1K×1）</strong>：10 根地址线（行 5 位、列 5 位，各译出 32 行/列）；行译码输出<strong>读选择线</strong>与<strong>写选择线</strong>两组，读写控制信号参与行译码；每行单元接有刷新放大器。

<strong>4116（单管，16K×1）</strong>：需 14 位地址，但芯片只有 7 根地址引脚，故<strong>行地址、列地址分两次传送</strong>：RAS（行选通）到来时送入 7 位行地址，存入行地址缓冲器；CAS（列选通）到来时送入 7 位列地址，存入列地址缓冲器，再分别经行译码、列译码选中单元。片内由行选通、列选通、读写控制信号产生行时钟、列时钟、写时钟，控制内部的读出与写入；I/O 缓冲器含数据输入寄存器与输出驱动。

存储阵列为 <strong>128 行 × 128 列</strong>，每列有一个读出放大器（<strong>跷跷板电路</strong>：一端被强制为 1，另一端必为 0），位于第 63 行与第 64 行之间。读时选中行使整行门控管导通，电容信息送到放大器一侧，另一侧取其反；写时数据经列开关送到放大器一侧，再反相写入单元。由于读出、写入各反相一次，最终读写数据一致。

<figure class="cpd-figure not-content" id="lhw-w3-fig-10">
  <svg viewBox="0 0 700 296" role="img" aria-label="4116芯片16K乘1单管DRAM地址线分时复用RAS和CAS选通及跷跷板读出放大器结构图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="464" height="268" rx="8"/>
    <rect class="fig-box-subtle" x="492" y="14" width="192" height="268" rx="8"/>
    <line class="fig-line-accent" x1="30" y1="112" x2="72" y2="112"/>
    <line class="fig-line-accent" x1="72" y1="74" x2="72" y2="152"/>
    <line class="fig-line-accent" x1="72" y1="74" x2="92" y2="74" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line-accent" x1="72" y1="152" x2="92" y2="152" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line" x1="186" y1="74" x2="206" y2="74" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line" x1="186" y1="152" x2="206" y2="152" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="278" y1="74" x2="288" y2="74"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="288" y1="70" x2="288" y2="162"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="288" y1="70" x2="298" y2="70" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="288" y1="162" x2="298" y2="162" marker-end="url(#lhw-w3-arr-10)"/>
    <polyline class="fig-line" stroke="var(--sl-color-green)" fill="none" points="243,134 243,114 304,114" marker-end="url(#lhw-w3-arr-10)"/>
    <polyline class="fig-line" stroke="var(--sl-color-purple)" fill="none" points="82,208 82,116 140,116 140,94" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="164" y1="208" x2="164" y2="170" marker-end="url(#lhw-w3-arr-10)"/>
    <line class="fig-line-accent" x1="406" y1="112" x2="422" y2="112" marker-end="url(#lhw-w3-arr-10)"/>
    <rect class="fig-box-blue" x="94" y="56" width="92" height="36" rx="4"/>
    <rect class="fig-box-green" x="94" y="134" width="92" height="36" rx="4"/>
    <rect class="fig-box-blue" x="208" y="56" width="70" height="36" rx="4"/>
    <rect class="fig-box-green" x="208" y="134" width="70" height="36" rx="4"/>
    <rect class="fig-box" x="300" y="48" width="106" height="134" rx="5"/>
    <rect class="fig-box-red" x="306" y="102" width="94" height="24" rx="3"/>
    <rect class="fig-box-accent" x="424" y="84" width="46" height="56" rx="4"/>
    <rect class="fig-box" x="28" y="236" width="440" height="34" rx="4"/>
    <line class="fig-line-dashed" x1="556" y1="68" x2="556" y2="152"/>
    <line class="fig-line-dashed" x1="618" y1="112" x2="618" y2="152"/>
    <polyline class="fig-line" stroke="var(--sl-color-purple)" points="510,68 556,68 556,88 666,88"/>
    <polyline class="fig-line" stroke="var(--sl-color-orange)" points="510,112 618,112 618,132 666,132"/>
    <rect class="fig-box-blue" x="534" y="152" width="52" height="24" rx="3"/>
    <rect class="fig-box-green" x="594" y="152" width="58" height="24" rx="3"/>
    <rect class="fig-box" x="504" y="192" width="168" height="78" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">Intel 4116（16K×1 DRAM）：7 根地址线分两次传 14 位地址</text>
    <text class="fig-accent" x="50" y="96" font-size="10" text-anchor="middle">A6..A0</text>
    <text class="fig-muted" x="50" y="128" font-size="9.5" text-anchor="middle">仅 7 根</text>
    <text class="fig-blue" x="140" y="78" font-size="10" text-anchor="middle">行地址缓冲器</text>
    <text class="fig-green" x="140" y="156" font-size="10" text-anchor="middle">列地址缓冲器</text>
    <text class="fig-blue" x="243" y="78" font-size="10" text-anchor="middle">行译码</text>
    <text class="fig-green" x="243" y="156" font-size="10" text-anchor="middle">列译码</text>
    <text class="fig-muted" x="353" y="72" font-size="9.5" text-anchor="middle">上半 0～63 行</text>
    <text class="fig-red" x="353" y="118" font-size="9.5" text-anchor="middle">128 列跷跷板放大</text>
    <text class="fig-muted" x="353" y="162" font-size="9.5" text-anchor="middle">下半 64～127 行</text>
    <text class="fig-accent" x="447" y="108" font-size="10" text-anchor="middle">I/O</text>
    <text class="fig-muted" x="447" y="124" font-size="9.5" text-anchor="middle">1 位</text>
    <text class="fig-purple" x="82" y="222" font-size="10" text-anchor="middle">① RAS′(低)</text>
    <text class="fig-orange" x="164" y="222" font-size="10" text-anchor="middle">② CAS′(低)</text>
    <text class="fig-label" x="248" y="257" font-size="10" text-anchor="middle">读出放大器居中（跷跷板电路：一侧为 1 另一侧必为 0；读/写各反相一次故首尾同相）</text>
    <text class="fig-title" x="504" y="36" font-size="12">地址复用时序与引脚</text>
    <text class="fig-purple" x="504" y="62" font-size="9.2">RAS′（行）</text>
    <text class="fig-orange" x="504" y="106" font-size="9.2">CAS′（列）</text>
    <text class="fig-blue" x="560" y="168" font-size="9.5" text-anchor="middle">行A 7位</text>
    <text class="fig-green" x="623" y="168" font-size="9.5" text-anchor="middle">列A 7位</text>
    <text class="fig-label" x="510" y="212" font-size="9.2">• 16K = 2¹⁴（7 行 + 7 列）</text>
    <text class="fig-label" x="514" y="230" font-size="10">• 地址引脚减半：14 → 7 根</text>
    <text class="fig-muted" x="514" y="248" font-size="9.5">• 代价：多加 RAS、CAS 两根</text>
    <text class="fig-muted" x="514" y="262" font-size="9.5">　选通线（净省 5 根引脚！）</text>
  </svg>
  <figcaption>图 4-10　4116（$16\text{K}\times 1$ DRAM）地址线分时复用原理：$\overline{\text{RAS}}$ 锁存 7 位行地址，$\overline{\text{CAS}}$ 锁存 7 位列地址</figcaption>
</figure>

### 4.5 存储器容量的扩展与地址格式

| 扩展方式 | 做法 | 例 |
|---|---|---|
| 位扩展 | 地址线并联、片选同时有效，各片数据线分别接 D0～D7 | 8 片 16K×1 → 16K×8 |
| 字扩展 | 数据线并联，片选由高位地址译码产生 | 4 组 16K×8 → 64K×8 |
| 字位同时扩展 | 先位扩展成组，再字扩展成体 | 32 片 16K×1 → 64K×8 |

地址格式由容量决定：<strong>片内地址位数 = $\log_2(\text{chip cells})$；片选（组号）位数</strong> = $\log_2(\text{chips or groups})$。

<strong>习题 4.14（模块板结构）</strong>：8 位机，地址码 18 位，用 4K×4 的 RAM 芯片构成模块板存储器。

| 问 | 计算 | 结果 |
|---|---|---|
| 最大存储空间 | $2^{18}\times 8$ = 256K×8 | 256KB |
| 模块板数（32K×8） | 256K×8 ÷ 32K×8 | 8 块 |
| 每板芯片数 | 32K×8 ÷ 4K×4 | 16 片 |
| 总芯片数 | 8×16 或 256K×8 ÷ 4K×4 | 128 片 |

地址格式：<strong>模块号 3 位 | 组号 3 位 | 片内地址 12 位</strong>，共 18 位。模块板 32K×8 由 8 组 4K×8 构成（每组 2 片 4K×4 位扩展成 8 位），故组号 3 位；4K 单元需 12 位片内地址。

> <strong>注意</strong>：这里的“芯片号”实际是<strong>组号</strong>——一次要读出 8 位，必须同时选中同一组的 2 片 4K×4，不能按单片编址。

<figure class="cpd-figure not-content" id="lhw-w3-fig-11">
  <svg viewBox="0 0 700 286" role="img" aria-label="位扩展与字扩展连线规律对比及习题4.14模块板18位地址三级划分图">
    <defs>
      <marker id="lhw-w3-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="14" width="330" height="176" rx="8"/>
    <rect class="fig-box-subtle" x="358" y="14" width="326" height="176" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="198" width="668" height="74" rx="8"/>
    <line class="fig-line-accent" x1="36" y1="68" x2="290" y2="68"/>
    <line class="fig-line-accent" x1="94" y1="68" x2="94" y2="86" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line-accent" x1="184" y1="68" x2="184" y2="86" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line-accent" x1="274" y1="68" x2="274" y2="86" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="94" y1="130" x2="94" y2="156" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="184" y1="130" x2="184" y2="156" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="274" y1="130" x2="274" y2="156" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="364" y1="112" x2="406" y2="112" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="466" y1="98" x2="492" y2="98" marker-end="url(#lhw-w3-arr-11)"/>
    <polyline class="fig-line" stroke="var(--sl-color-purple)" fill="none" points="466,126 480,126 480,142 582,142 582,109 594,109" marker-end="url(#lhw-w3-arr-11)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="444" y1="162" x2="672" y2="162"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="532" y1="130" x2="532" y2="162"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="634" y1="130" x2="634" y2="162"/>
    <rect class="fig-box-blue" x="58" y="88" width="72" height="42" rx="4"/>
    <rect class="fig-box-blue" x="148" y="88" width="72" height="42" rx="4"/>
    <rect class="fig-box-blue" x="238" y="88" width="72" height="42" rx="4"/>
    <rect class="fig-box-purple" x="408" y="86" width="58" height="52" rx="4"/>
    <rect class="fig-box-green" x="494" y="88" width="76" height="42" rx="4"/>
    <rect class="fig-box" x="596" y="88" width="76" height="42" rx="4"/>
    <rect class="fig-box-purple" x="190" y="232" width="110" height="28" rx="4"/>
    <rect class="fig-box-orange" x="300" y="232" width="110" height="28" rx="4"/>
    <rect class="fig-box-blue" x="410" y="232" width="256" height="28" rx="4"/>
    <text class="fig-title" x="28" y="36" font-size="12">（a）位扩展：地址/CS/WE 并联，数据线各接各</text>
    <text class="fig-accent" x="164" y="60" font-size="9.5" text-anchor="middle">低位片内地址 A 与片选 CS 全部并联</text>
    <text class="fig-blue" x="94" y="106" font-size="10" text-anchor="middle">16K×1 #0</text>
    <text class="fig-muted" x="94" y="121" font-size="9" text-anchor="middle">CS 同时有效</text>
    <text class="fig-blue" x="184" y="106" font-size="10" text-anchor="middle">16K×1 #1</text>
    <text class="fig-muted" x="184" y="121" font-size="9" text-anchor="middle">CS 同时有效</text>
    <text class="fig-blue" x="274" y="106" font-size="10" text-anchor="middle">… #7</text>
    <text class="fig-muted" x="274" y="121" font-size="9" text-anchor="middle">共 8 片</text>
    <text class="fig-green" x="94" y="172" font-size="10" text-anchor="middle">接 D0</text>
    <text class="fig-green" x="184" y="172" font-size="10" text-anchor="middle">接 D1</text>
    <text class="fig-green" x="274" y="172" font-size="10" text-anchor="middle">接 D7</text>
    <text class="fig-title" x="370" y="36" font-size="12">（b）字扩展：地址/数据并联，高位译码选片</text>
    <text class="fig-purple" x="382" y="102" font-size="9.2" text-anchor="middle">高位A</text>
    <text class="fig-purple" x="437" y="108" font-size="10" text-anchor="middle">译码器</text>
    <text class="fig-muted" x="437" y="124" font-size="9" text-anchor="middle">74138</text>
    <text class="fig-green" x="532" y="106" font-size="10" text-anchor="middle">组0 (CS0=0)</text>
    <text class="fig-muted" x="532" y="121" font-size="9" text-anchor="middle">16K×8 选中</text>
    <text class="fig-label" x="634" y="106" font-size="10" text-anchor="middle">组3 (CS3=1)</text>
    <text class="fig-muted" x="634" y="121" font-size="9" text-anchor="middle">高阻态隔离</text>
    <text class="fig-green" x="526" y="178" font-size="9" text-anchor="middle">D7～D0 公共数据总线并联（同一时刻仅 1 组导通）</text>
    <text class="fig-title" x="28" y="218" font-size="12">（c）习题 4.14 模块板 18 位地址格式（128 片 4K×4 → 8 块板 × 8 组/板 × 2 片/组 = 256K×8）：</text>
    <text class="fig-label" x="104" y="250" font-size="10.5" text-anchor="middle">A17 ～ A0（18 位）：</text>
    <text class="fig-purple" x="245" y="250" font-size="10.5" text-anchor="middle">模块板号（3位）</text>
    <text class="fig-orange" x="355" y="250" font-size="10.5" text-anchor="middle">板内组号（3位）</text>
    <text class="fig-blue" x="538" y="250" font-size="10.5" text-anchor="middle">片内地址（12 位，寻址 4K）</text>
  </svg>
  <figcaption>图 4-11　位扩展与字扩展连线准则对比，以及习题 4.14 模块板存储器的 18 位三级地址划分（组号而非单片号）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w3-fig-12">
  <svg viewBox="0 0 700 296" role="img" aria-label="32片16K乘1芯片字位同时扩展构成64K乘8存储器硬件连线图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="268" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="32" y1="138" x2="64" y2="138" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="140" y1="74" x2="174" y2="74" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" x1="140" y1="116" x2="174" y2="116" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" x1="140" y1="158" x2="174" y2="158" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" x1="140" y1="200" x2="174" y2="200" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="195" y1="218" x2="195" y2="246" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="235" y1="218" x2="235" y2="246" marker-end="url(#lhw-w3-arr-12)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="475" y1="218" x2="475" y2="246" marker-end="url(#lhw-w3-arr-12)"/>
    <rect class="fig-box-purple" x="66" y="64" width="74" height="148" rx="5"/>
    <rect class="fig-box-blue" x="176" y="56" width="320" height="36" rx="4"/>
    <rect class="fig-box" x="176" y="98" width="320" height="36" rx="4"/>
    <rect class="fig-box" x="176" y="140" width="320" height="36" rx="4"/>
    <rect class="fig-box" x="176" y="182" width="320" height="36" rx="4"/>
    <line class="fig-divider" x1="216" y1="56" x2="216" y2="218"/>
    <line class="fig-divider" x1="256" y1="56" x2="256" y2="218"/>
    <line class="fig-divider" x1="296" y1="56" x2="296" y2="218"/>
    <line class="fig-divider" x1="336" y1="56" x2="336" y2="218"/>
    <line class="fig-divider" x1="376" y1="56" x2="376" y2="218"/>
    <line class="fig-divider" x1="416" y1="56" x2="416" y2="218"/>
    <line class="fig-divider" x1="456" y1="56" x2="456" y2="218"/>
    <rect class="fig-box" x="506" y="56" width="170" height="162" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">字位同时扩展：32 片 16K×1 芯片 → 64K×8 存储体（4 组 × 每组 8 片）</text>
    <text class="fig-purple" x="48" y="124" font-size="9.5" text-anchor="middle">A15</text>
    <text class="fig-purple" x="48" y="156" font-size="9.5" text-anchor="middle">A14</text>
    <text class="fig-purple" x="103" y="128" font-size="10.5" text-anchor="middle">2-4 译码</text>
    <text class="fig-muted" x="103" y="146" font-size="9.5" text-anchor="middle">高位选组</text>
    <text class="fig-purple" x="157" y="69" font-size="9" text-anchor="middle">CS0</text>
    <text class="fig-muted" x="157" y="111" font-size="9" text-anchor="middle">CS1</text>
    <text class="fig-muted" x="157" y="153" font-size="9" text-anchor="middle">CS2</text>
    <text class="fig-muted" x="157" y="195" font-size="9" text-anchor="middle">CS3</text>
    <text class="fig-blue" x="336" y="78" font-size="10" text-anchor="middle">第 0 组：8 片 16K×1 并排（0000H ～ 3FFFH）</text>
    <text class="fig-label" x="336" y="120" font-size="10" text-anchor="middle">第 1 组：8 片 16K×1 并排（4000H ～ 7FFFH）</text>
    <text class="fig-label" x="336" y="162" font-size="10" text-anchor="middle">第 2 组：8 片 16K×1 并排（8000H ～ BFFFH）</text>
    <text class="fig-label" x="336" y="204" font-size="10" text-anchor="middle">第 3 组：8 片 16K×1 并排（C000H ～ FFFFH）</text>
    <text class="fig-green" x="195" y="262" font-size="10" text-anchor="middle">D0</text>
    <text class="fig-green" x="235" y="262" font-size="10" text-anchor="middle">D1</text>
    <text class="fig-muted" x="355" y="258" font-size="10" text-anchor="middle">…… 同列芯片数据线并联接同一位 Di ……</text>
    <text class="fig-green" x="475" y="262" font-size="10" text-anchor="middle">D7</text>
    <text class="fig-blue" x="514" y="78" font-size="10.5">连线三要素：</text>
    <text class="fig-label" x="514" y="98" font-size="9.5">1. 低位 A13～A0 并联</text>
    <text class="fig-muted" x="522" y="114" font-size="9">接全部 32 片地址引脚</text>
    <text class="fig-label" x="514" y="134" font-size="9.5">2. 同行 8 片 CS 共接</text>
    <text class="fig-muted" x="522" y="150" font-size="9">凑足 8 位位宽同时读写</text>
    <text class="fig-label" x="514" y="170" font-size="9.5">3. 同列 4 片 I/O 共接</text>
    <text class="fig-muted" x="522" y="186" font-size="9">分别引出到 D0～D7</text>
    <text class="fig-accent" x="514" y="206" font-size="8.6">总片数 = (64K×8)/(16K×1) = 32</text>
  </svg>
  <figcaption>图 4-12　字位同时扩展硬件连线图：8 片横向位扩展成组（$16\text{K}\times 8$），4 组纵向字扩展成体（$64\text{K}\times 8$）</figcaption>
</figure>

### 4.6 提高访存速度：单体多字与多体并行

解决 CPU 与主存速度不匹配（存储墙）有三条途径：<strong>采用更高速的器件</strong>；<strong>采用 cache–主存层次结构（见 4.8 起）；调整存储结构</strong>，即改变存储字长或把存储器分成多个可并行的体。

<strong>单体多字</strong>：把存储字长加宽，例如 CPU 字长 16 位、存储字长 64 位，一次访存取出 4 个机器字存入数据寄存器，后续直接取用，从而提高带宽。两个问题：

- 写入一个 16 位字时，若按 64 位整体写，其余 48 位会被误改，存储器内必须另加硬件完成单字写入，结构复杂；
- 若取出的 4 条指令中第一条就是转移指令且转移范围超出这 4 个字，则预取的其余 3 条无效。

<strong>多体并行</strong>：把存储器分成 m 个体，每个体有自己的 MAR、MDR、地址译码器和读写电路，可独立并行工作。按编址方式分为两种：

| 方式 | 编址 | 地址格式 | 特点 |
|---|---|---|---|
| 高位交叉 | 顺序编址：先编完 m0 再编 m1 | 体号（高位）+ 体内地址（低位） | 适合<strong>容量扩展</strong>；程序顺序执行时只反复访问一个体，其余空闲，不能提高带宽 |
| 低位交叉 | 轮流编址：相邻地址分给相邻的体 | 体内地址（高位）+ 体号（低位） | 连续地址分布在不同体，可<strong>并行</strong>工作，提高带宽 |

低位交叉连续读取 m 个字所需时间为

$$
T = T_m + (m-1)\,r
$$

其中 $T_m$ 为存取周期，$r$ 为总线传送周期。块长与多体交叉直接相关：Cray-1 采用 16 体交叉，块长为 16 个存储字；IBM 370 采用 4 体交叉，存储字 64 位，块长 256 位。

<strong>习题 4.23（八体低位交叉）</strong>：CPU 有 16 根地址线 A0～A15、8 根数据线 D0～D7；M/IO 高电平访存，RD、WE 分别低电平有效；要求 64KB、八体低位交叉。

- 每体容量：64KB ÷ 8 = 8KB，即 <strong>8K×8</strong> 的 RAM 8 片。
- 地址格式：<strong>体号 3 位（A2～A0）| 体内地址 13 位（A15～A3）</strong>，共 16 位。
- 连接：A15～A3 接各片的 A12～A0（注意不是 A0 对 A0）；A2～A0 接 74138 的 C、B、A，Y0′～Y7′ 分别作 RAM0～RAM7 的片选；G1 接高电平（可由 M/IO 提供），G2A′、G2B′ 接地。
- 地址范围（不连续，地址对 8 取余等于体号）：

| 体 | 地址范围 |
|---|---|
| RAM0 | 0000H ～ FFF8H |
| RAM1 | 0001H ～ FFF9H |
| RAM2 | 0002H ～ FFFAH |
| … | … |
| RAM7 | 0007H ～ FFFFH |

> <strong>★考点</strong>：低位交叉的体号在<strong>低位</strong>，因此片选由低位地址译码，而芯片地址线接的是<strong>高位</strong>地址——这与字扩展中“高位译码产生片选”正好相反，是最易出错处。

<figure class="cpd-figure not-content" id="lhw-w3-fig-13">
  <svg viewBox="0 0 700 318" role="img" aria-label="高位交叉顺序编址与低位交叉轮流编址对比及四体低位交叉流水线读时序图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="322" height="172" rx="8"/>
    <rect class="fig-box-subtle" x="350" y="14" width="334" height="172" rx="8"/>
    <rect class="fig-box-subtle" x="16" y="194" width="668" height="110" rx="8"/>
    <line class="fig-line" x1="112" y1="286" x2="664" y2="286" marker-end="url(#lhw-w3-arr-13)"/>
    <line class="fig-line-dashed" x1="184" y1="220" x2="184" y2="286"/>
    <line class="fig-line-dashed" x1="256" y1="220" x2="256" y2="286"/>
    <line class="fig-line-dashed" x1="328" y1="220" x2="328" y2="286"/>
    <line class="fig-line-dashed" x1="400" y1="220" x2="400" y2="286"/>
    <line class="fig-line-dashed" x1="616" y1="220" x2="616" y2="286"/>
    <rect class="fig-box-blue" x="28" y="48" width="142" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="76" width="142" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="104" width="142" height="24" rx="3"/>
    <rect class="fig-box" x="28" y="132" width="142" height="24" rx="3"/>
    <rect class="fig-box-purple" x="182" y="54" width="64" height="26" rx="3"/>
    <rect class="fig-box-blue" x="246" y="54" width="80" height="26" rx="3"/>
    <rect class="fig-box-green" x="362" y="48" width="152" height="24" rx="3"/>
    <rect class="fig-box-green" x="362" y="76" width="152" height="24" rx="3"/>
    <rect class="fig-box-green" x="362" y="104" width="152" height="24" rx="3"/>
    <rect class="fig-box-green" x="362" y="132" width="152" height="24" rx="3"/>
    <rect class="fig-box-blue" x="526" y="54" width="82" height="26" rx="3"/>
    <rect class="fig-box-orange" x="608" y="54" width="64" height="26" rx="3"/>
    <rect class="fig-box-blue" x="112" y="220" width="288" height="14" rx="2"/>
    <rect class="fig-box-green" x="184" y="236" width="288" height="14" rx="2"/>
    <rect class="fig-box-orange" x="256" y="252" width="288" height="14" rx="2"/>
    <rect class="fig-box-purple" x="328" y="268" width="288" height="14" rx="2"/>
    <text class="fig-title" x="28" y="36" font-size="12">（a）高位交叉（顺序编址，4 体×16 单元）</text>
    <text class="fig-blue" x="99" y="64" font-size="9.5" text-anchor="middle">M0：0 ～ 15（忙）</text>
    <text class="fig-muted" x="99" y="92" font-size="9.5" text-anchor="middle">M1：16 ～ 31（闲）</text>
    <text class="fig-muted" x="99" y="120" font-size="9.5" text-anchor="middle">M2：32 ～ 47（闲）</text>
    <text class="fig-muted" x="99" y="148" font-size="9.5" text-anchor="middle">M3：48 ～ 63（闲）</text>
    <text class="fig-purple" x="214" y="71" font-size="9.5" text-anchor="middle">体号(2位)</text>
    <text class="fig-blue" x="286" y="71" font-size="9.5" text-anchor="middle">体内地址(4位)</text>
    <text class="fig-label" x="182" y="104" font-size="9.5">• 顺序取指连续落入 M0</text>
    <text class="fig-red" x="182" y="122" font-size="9.5">• 一体忙、三体空，串行无提速</text>
    <text class="fig-muted" x="182" y="140" font-size="9.5">• 优势：故障隔离，易扩容</text>
    <text class="fig-title" x="362" y="36" font-size="12">（b）低位交叉（轮流编址：体号 = 地址 mod 4）</text>
    <text class="fig-green" x="438" y="64" font-size="9.5" text-anchor="middle">M0：0, 4, 8, 12 …</text>
    <text class="fig-green" x="438" y="92" font-size="9.5" text-anchor="middle">M1：1, 5, 9, 13 …</text>
    <text class="fig-green" x="438" y="120" font-size="9.5" text-anchor="middle">M2：2, 6, 10, 14 …</text>
    <text class="fig-green" x="438" y="148" font-size="9.5" text-anchor="middle">M3：3, 7, 11, 15 …</text>
    <text class="fig-blue" x="567" y="71" font-size="9.5" text-anchor="middle">体内地址(4位)</text>
    <text class="fig-orange" x="640" y="71" font-size="9.5" text-anchor="middle">体号(2位)</text>
    <text class="fig-label" x="526" y="104" font-size="9.5">• 连续地址分摊至各体</text>
    <text class="fig-green" x="526" y="122" font-size="9.5">• 四体流水并行提高带宽</text>
    <text class="fig-muted" x="526" y="140" font-size="9.5">• 满足条件：m ≥ Tm / r</text>
    <text class="fig-title" x="28" y="212" font-size="11.5">（c）四体低位交叉流水线连续读 4 个字时序（存取周期 Tm = 4r，每隔总线周期 r 启动下一体）：</text>
    <text class="fig-blue" x="68" y="231" font-size="9.5" text-anchor="middle">M0 存取 Tm</text>
    <text class="fig-green" x="68" y="247" font-size="9.5" text-anchor="middle">M1 存取 Tm</text>
    <text class="fig-orange" x="68" y="263" font-size="9.5" text-anchor="middle">M2 存取 Tm</text>
    <text class="fig-purple" x="68" y="279" font-size="9.5" text-anchor="middle">M3 存取 Tm</text>
    <text class="fig-muted" x="148" y="298" font-size="9" text-anchor="middle">r</text>
    <text class="fig-muted" x="220" y="298" font-size="9" text-anchor="middle">r</text>
    <text class="fig-muted" x="292" y="298" font-size="9" text-anchor="middle">r</text>
    <text class="fig-accent" x="456" y="299" font-size="10" text-anchor="middle">★ 连续读 m 字总耗时：T = Tm + (m − 1)r（串行需 m·Tm！）</text>
  </svg>
  <figcaption>图 4-13　多体并行存储器：高位交叉顺序编址 vs 低位交叉轮流编址，及连续读 $m$ 字的流水线耗时 $T = T_m + (m-1)r$</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w3-fig-14">
  <svg viewBox="0 0 700 288" role="img" aria-label="习题4.23八体低位交叉存储器CPU与74138译码器及8片8K乘8芯片连线图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="260" rx="8"/>
    <line class="fig-line-accent" x1="120" y1="64" x2="626" y2="64"/>
    <line class="fig-line-accent" x1="290" y1="64" x2="290" y2="86" marker-end="url(#lhw-w3-arr-14)"/>
    <line class="fig-line-accent" x1="418" y1="64" x2="418" y2="86" marker-end="url(#lhw-w3-arr-14)"/>
    <line class="fig-line-accent" x1="612" y1="64" x2="612" y2="86" marker-end="url(#lhw-w3-arr-14)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="120" y1="128" x2="144" y2="128" marker-end="url(#lhw-w3-arr-14)"/>
    <line class="fig-line" stroke="var(--sl-color-purple)" x1="120" y1="168" x2="144" y2="168" marker-end="url(#lhw-w3-arr-14)"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="234" y1="118" x2="258" y2="118" marker-end="url(#lhw-w3-arr-14)"/>
    <polyline class="fig-line" stroke="var(--sl-color-orange)" fill="none" points="234,144 252,144 252,170 374,170 374,122 384,122" marker-end="url(#lhw-w3-arr-14)"/>
    <polyline class="fig-line" stroke="var(--sl-color-orange)" fill="none" points="234,188 558,188 558,122 570,122" marker-end="url(#lhw-w3-arr-14)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="120" y1="212" x2="650" y2="212"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="308" y1="158" x2="308" y2="212"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="436" y1="158" x2="436" y2="212"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="630" y1="158" x2="630" y2="212"/>
    <rect class="fig-box-purple" x="28" y="50" width="92" height="178" rx="6"/>
    <rect class="fig-box-orange" x="146" y="102" width="88" height="92" rx="5"/>
    <rect class="fig-box-blue" x="260" y="88" width="100" height="70" rx="5"/>
    <rect class="fig-box-blue" x="386" y="88" width="100" height="70" rx="5"/>
    <rect class="fig-box-blue" x="572" y="88" width="100" height="70" rx="5"/>
    <rect class="fig-box" x="28" y="236" width="638" height="28" rx="4"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">习题 4.23 八体低位交叉连线图：低位 A2～A0 经 74138 选体，高位 A15～A3 接片内地址</text>
    <text class="fig-purple" x="74" y="84" font-size="11.5" text-anchor="middle">CPU</text>
    <text class="fig-accent" x="74" y="106" font-size="9.5" text-anchor="middle">高位 A15～A3</text>
    <text class="fig-orange" x="74" y="132" font-size="9.5" text-anchor="middle">低位 A2～A0</text>
    <text class="fig-label" x="74" y="172" font-size="9.5" text-anchor="middle">M/IO=1 访存</text>
    <text class="fig-green" x="74" y="214" font-size="9.5" text-anchor="middle">D7～D0, RD/WE</text>
    <text class="fig-accent" x="430" y="57" font-size="9.5" text-anchor="middle">高位 A15～A3（13 根）并联接全部芯片的片内地址引脚 A12～A0（错位对接！）</text>
    <text class="fig-orange" x="190" y="122" font-size="10.5" text-anchor="middle">74138</text>
    <text class="fig-label" x="190" y="140" font-size="8.8" text-anchor="middle">C B A ← A2..0</text>
    <text class="fig-muted" x="190" y="156" font-size="9" text-anchor="middle">G1 ← M/IO</text>
    <text class="fig-muted" x="190" y="172" font-size="8.8" text-anchor="middle">G2A′,G2B′ 接地</text>
    <text class="fig-orange" x="247" y="113" font-size="8.5" text-anchor="middle">Y0′</text>
    <text class="fig-orange" x="247" y="138" font-size="8.5" text-anchor="middle">Y1′</text>
    <text class="fig-orange" x="247" y="183" font-size="8.5" text-anchor="middle">Y7′</text>
    <text class="fig-blue" x="310" y="108" font-size="9.5" text-anchor="middle">RAM0 (8K×8)</text>
    <text class="fig-muted" x="310" y="125" font-size="8.8" text-anchor="middle">0000H, 0008H…</text>
    <text class="fig-muted" x="310" y="142" font-size="9" text-anchor="middle">mod 8 = 0</text>
    <text class="fig-blue" x="436" y="108" font-size="9.5" text-anchor="middle">RAM1 (8K×8)</text>
    <text class="fig-muted" x="436" y="125" font-size="8.8" text-anchor="middle">0001H, 0009H…</text>
    <text class="fig-muted" x="436" y="142" font-size="9" text-anchor="middle">mod 8 = 1</text>
    <text class="fig-muted" x="528" y="125" font-size="12" text-anchor="middle">……</text>
    <text class="fig-blue" x="622" y="108" font-size="9.5" text-anchor="middle">RAM7 (8K×8)</text>
    <text class="fig-muted" x="622" y="125" font-size="8.8" text-anchor="middle">0007H, 000FH…</text>
    <text class="fig-muted" x="622" y="142" font-size="9" text-anchor="middle">mod 8 = 7</text>
    <text class="fig-green" x="390" y="226" font-size="9.5" text-anchor="middle">D7～D0 双向数据总线与 RD′、WE′ 读写控制线全部并联</text>
    <text class="fig-accent" x="347" y="254" font-size="10" text-anchor="middle">★ 易错必记：字扩展是【高位译码选片、低位接片内】；低位交叉恰好相反——【低位译码选体、高位接片内】！</text>
  </svg>
  <figcaption>图 4-14　八体低位交叉存储器与 CPU、74138 译码器的完整连线图（习题 4.23：$A_2\text{–}A_0$ 选体，$A_{15}\text{–}A_3$ 接片内）</figcaption>
</figure>

### 4.7 汉明码校验

<strong>编码的最小距离</strong> L：合法代码集中任意两个码字之间二进制位差异的最小值，即把一个合法码字变成另一个合法码字所需改变的最少位数。L 越大，检错、纠错能力越强：

$$
L - 1 = D + C
$$

其中 D 为可检测的错误位数，C 为可纠正的错误位数，通常 $D \ge C$。汉明码是<strong>具有一位纠错能力</strong>的编码，其 $L=3$。

<strong>奇偶校验</strong>只能发现“奇数个位出错”，不能定位。若把数据分组、每组各配一个校验位，可把出错范围缩小到某一组。

<strong>汉明码采用非划分式分组</strong>：组与组之间有重叠，某些位属于多个组，校验位则独占一组、放在 $2^{i}$ 位置上（第 1、2、4、8… 位），其余为数据位。以 7 位编码为例：G1 覆盖 1,3,5,7；G2 覆盖 2,3,6,7；G4 覆盖 4,5,6,7。各组按偶校验实现（异或门）。

接收端对每组分别校验，得到 3 位结果（G4G2G1），其二进制值<strong>直接就是出错位号</strong>：

| 校验结果 | 含义 |
|---|---|
| 000 | 无错 |
| 001 | 第 1 位错（仅 G1 错） |
| 011 | 第 3 位错（G1、G2 错） |
| 101 | 第 5 位错（G1、G4 错） |
| 110 | 第 6 位错（G2、G4 错） |
| 111 | 第 7 位错（三组公共位） |

> <strong>★考点</strong>：一位出错时，凡是含有该位的组校验结果均为 1，不含的为 0，故结果值即错误位置；这也是校验位必须放在 $2^i$ 位的原因——它只属于一个组，出错时结果中只出现单个 1。

<figure class="cpd-figure not-content" id="lhw-w3-fig-15">
  <svg viewBox="0 0 700 326" role="img" aria-label="7位汉明码2的幂次位置分布与非划分式偶校验韦恩图及指错子纠错机制图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="328" height="298" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="14" width="328" height="298" rx="8"/>
    <circle cx="134" cy="142" r="74" fill="none" stroke="var(--sl-color-blue)" stroke-width="1.8"/>
    <circle cx="226" cy="142" r="74" fill="none" stroke="var(--sl-color-green)" stroke-width="1.8"/>
    <circle cx="180" cy="212" r="74" fill="none" stroke="var(--sl-color-red)" stroke-width="1.8"/>
    <rect class="fig-box-blue" x="78" y="112" width="32" height="22" rx="4"/>
    <rect class="fig-box-green" x="250" y="112" width="32" height="22" rx="4"/>
    <rect class="fig-box-red" x="164" y="250" width="32" height="22" rx="4"/>
    <rect class="fig-box" x="164" y="102" width="32" height="22" rx="4"/>
    <rect class="fig-box" x="116" y="188" width="32" height="22" rx="4"/>
    <rect class="fig-box" x="212" y="188" width="32" height="22" rx="4"/>
    <rect class="fig-box-accent" x="164" y="156" width="32" height="22" rx="4"/>
    <rect class="fig-box-blue" x="368" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box-green" x="410" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box" x="452" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box-red" x="494" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box" x="536" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box" x="578" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box" x="620" y="54" width="42" height="36" rx="3"/>
    <rect class="fig-box-purple" x="368" y="104" width="304" height="58" rx="5"/>
    <rect class="fig-box-orange" x="368" y="172" width="304" height="62" rx="5"/>
    <rect class="fig-box" x="368" y="242" width="304" height="58" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12">（a）7 位汉明码非划分式交叉分组（偶校验）</text>
    <text class="fig-blue" x="74" y="64" font-size="11">G1 组(1,3,5,7)</text>
    <text class="fig-green" x="222" y="64" font-size="11">G2 组(2,3,6,7)</text>
    <text class="fig-red" x="180" y="302" font-size="11" text-anchor="middle">G4 组(4,5,6,7)</text>
    <text class="fig-blue" x="94" y="126" font-size="9" text-anchor="middle">1:P1</text>
    <text class="fig-green" x="266" y="126" font-size="9" text-anchor="middle">2:P2</text>
    <text class="fig-red" x="180" y="264" font-size="9" text-anchor="middle">4:P4</text>
    <text class="fig-label" x="180" y="116" font-size="9" text-anchor="middle">3:D1</text>
    <text class="fig-label" x="132" y="202" font-size="9" text-anchor="middle">5:D2</text>
    <text class="fig-label" x="228" y="202" font-size="9" text-anchor="middle">6:D3</text>
    <text class="fig-accent" x="180" y="170" font-size="9" text-anchor="middle">7:D4</text>
    <text class="fig-title" x="368" y="36" font-size="12">（b）2ⁱ 位置排布、位数不等式与纠错</text>
    <text class="fig-blue" x="389" y="70" font-size="9.5" text-anchor="middle">位1</text>
    <text class="fig-blue" x="389" y="84" font-size="9.5" text-anchor="middle">P1</text>
    <text class="fig-green" x="431" y="70" font-size="9.5" text-anchor="middle">位2</text>
    <text class="fig-green" x="431" y="84" font-size="9.5" text-anchor="middle">P2</text>
    <text class="fig-label" x="473" y="70" font-size="9.5" text-anchor="middle">位3</text>
    <text class="fig-muted" x="473" y="84" font-size="9.5" text-anchor="middle">D1</text>
    <text class="fig-red" x="515" y="70" font-size="9.5" text-anchor="middle">位4</text>
    <text class="fig-red" x="515" y="84" font-size="9.5" text-anchor="middle">P4</text>
    <text class="fig-label" x="557" y="70" font-size="9.5" text-anchor="middle">位5</text>
    <text class="fig-muted" x="557" y="84" font-size="9.5" text-anchor="middle">D2</text>
    <text class="fig-label" x="599" y="70" font-size="9.5" text-anchor="middle">位6</text>
    <text class="fig-muted" x="599" y="84" font-size="9.5" text-anchor="middle">D3</text>
    <text class="fig-label" x="641" y="70" font-size="9.5" text-anchor="middle">位7</text>
    <text class="fig-muted" x="641" y="84" font-size="9.5" text-anchor="middle">D4</text>
    <text class="fig-purple" x="378" y="123" font-size="10">★ 校验位位数约束（n 位数据 + k 位校验）：</text>
    <text class="fig-label" x="378" y="142" font-size="9.5">纠 1 位错(SEC)：2ᵏ ≥ n + k + 1（+1 表无错态）</text>
    <text class="fig-muted" x="378" y="156" font-size="9.2">纠 1 检 2(SEC-DED)：需再增设 1 位总偶校验位</text>
    <text class="fig-orange" x="378" y="191" font-size="10">★ 异或生成偶校验指错子 S = G4 G2 G1：</text>
    <text class="fig-label" x="378" y="209" font-size="9.5">G1=1⊕3⊕5⊕7，G2=2⊕3⊕6⊕7，G4=4⊕5⊕6⊕7</text>
    <text class="fig-muted" x="378" y="226" font-size="9">例：S = 101B → 十进制 5 → 直接把第 5 位取反纠错！</text>
    <text class="fig-label" x="378" y="261" font-size="9.5">• 校验位独占 2ⁱ（1,2,4,8…），二进制仅含单个 1</text>
    <text class="fig-muted" x="378" y="279" font-size="9">• 码距 L = 3：满足 L − 1 = D + C（纠1检1 或 检2）</text>
    <text class="fig-muted" x="378" y="294" font-size="9">• 若按奇校验，指错子需对每组异或结果再取反</text>
  </svg>
  <figcaption>图 4-15　(7,4) 汉明码交叉偶校验分组、$2^k \ge n+k+1$ 位数不等式与指错子 $G_4G_2G_1$ 自动定位纠错原理</figcaption>
</figure>

### 4.8 cache 的工作原理与命中率

<strong>程序的局部性原理</strong>是 cache 能生效的前提：

- <strong>时间局部性</strong>：当前正在使用的指令和数据，不久的将来还会被使用；
- <strong>空间局部性</strong>：当前使用的指令和数据，其相邻的指令和数据不久也将被使用。

因此 cache 与主存之间<strong>以“块”为单位交换</strong>：把当前使用的信息连同相邻信息一起调入 cache。块的大小由实验确定。

<strong>分块模型</strong>：主存分为 M 块，cache 分为 C 块，$M \gg C$。主存地址 = <strong>主存块号 + 块内地址</strong>；cache 地址 = <strong>缓存块号 + 块内地址</strong>。由于按块整体传送、块内顺序不变，块内地址的位数和值在两侧完全相同；cache 地址在实际机器中并不真正形成。

cache 每块设一个<strong>标记（tag）</strong>，记录该块当前保存的是哪个主存块。CPU 给出地址后，用主存块号与各标记比较：相等且该块有效 → 命中，直接从 cache 取数；否则不命中，必须访问主存。

<strong>命中率</strong>：设 N1 次在 cache 中找到、N2 次未找到，

$$
h = \frac{N_1}{N_1 + N_2}
$$

命中率与 <strong>cache 容量</strong>（越大越高）和<strong>块长</strong>有关：块太小，取来没执行几条指令就用完，不能充分利用局部性；块太大，cache 能容纳的块数变少，且块内无用信息挤占空间，同样降低命中率。一般块长取 <strong>4～8 个字</strong>，并与多体交叉的体数相关。

<figure class="cpd-figure not-content" id="lhw-w3-fig-16">
  <svg viewBox="0 0 700 296" role="img" aria-label="主存与Cache按块交换标记有效位对应关系及块长对命中率影响曲线图">
    <defs>
      <marker id="lhw-w3-arr-16" 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="14" width="462" height="268" rx="8"/>
    <rect class="fig-box-subtle" x="490" y="14" width="194" height="268" rx="8"/>
    <line class="fig-line-accent" x1="150" y1="146" x2="218" y2="122" marker-end="url(#lhw-w3-arr-16)"/>
    <line class="fig-line" x1="516" y1="210" x2="664" y2="210" marker-end="url(#lhw-w3-arr-16)"/>
    <line class="fig-line" x1="516" y1="210" x2="516" y2="96" marker-end="url(#lhw-w3-arr-16)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 524 196 Q 584 102 654 176"/>
    <line class="fig-line-dashed" x1="594" y1="143" x2="594" y2="210"/>
    <rect class="fig-box" x="30" y="58" width="120" height="30" rx="3"/>
    <rect class="fig-box" x="30" y="92" width="120" height="30" rx="3"/>
    <rect class="fig-box-blue" x="30" y="130" width="120" height="32" rx="3"/>
    <rect class="fig-box" x="30" y="166" width="120" height="30" rx="3"/>
    <rect class="fig-box-purple" x="220" y="64" width="34" height="32" rx="3"/>
    <rect class="fig-box-orange" x="254" y="64" width="58" height="32" rx="3"/>
    <rect class="fig-box" x="312" y="64" width="150" height="32" rx="3"/>
    <rect class="fig-box-green" x="220" y="106" width="34" height="34" rx="3"/>
    <rect class="fig-box-blue" x="254" y="106" width="58" height="34" rx="3"/>
    <rect class="fig-box-blue" x="312" y="106" width="150" height="34" rx="3"/>
    <rect class="fig-box-purple" x="220" y="150" width="34" height="32" rx="3"/>
    <rect class="fig-box-orange" x="254" y="150" width="58" height="32" rx="3"/>
    <rect class="fig-box" x="312" y="150" width="150" height="32" rx="3"/>
    <rect class="fig-box" x="30" y="230" width="432" height="38" rx="4"/>
    <circle class="fig-dot" cx="594" cy="143" r="3.5"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">主存（M 块）与 Cache（C 块，M ≫ C）的分块调入与标记映射</text>
    <text class="fig-muted" x="90" y="52" font-size="10" text-anchor="middle">主存空间</text>
    <text class="fig-label" x="90" y="77" font-size="10" text-anchor="middle">主存块 0</text>
    <text class="fig-label" x="90" y="111" font-size="10" text-anchor="middle">主存块 1</text>
    <text class="fig-blue" x="90" y="150" font-size="10" text-anchor="middle">主存块 3（正在用）</text>
    <text class="fig-label" x="90" y="185" font-size="10" text-anchor="middle">主存块 4</text>
    <text class="fig-muted" x="90" y="214" font-size="10" text-anchor="middle">…… 块 M−1</text>
    <text class="fig-accent" x="184" y="122" font-size="9.5" text-anchor="middle">整块复制</text>
    <text class="fig-purple" x="237" y="56" font-size="9.5" text-anchor="middle">有效</text>
    <text class="fig-orange" x="283" y="56" font-size="9.5" text-anchor="middle">标记 Tag</text>
    <text class="fig-muted" x="387" y="56" font-size="9.5" text-anchor="middle">Cache 行数据副本（如 4～8 字）</text>
    <text class="fig-muted" x="237" y="84" font-size="9.5" text-anchor="middle">0</text>
    <text class="fig-muted" x="283" y="84" font-size="9.5" text-anchor="middle">—</text>
    <text class="fig-muted" x="387" y="84" font-size="9.5" text-anchor="middle">Cache 行 0（空闲无效）</text>
    <text class="fig-green" x="237" y="127" font-size="10" text-anchor="middle">1</text>
    <text class="fig-blue" x="283" y="127" font-size="10" text-anchor="middle">Tag=3</text>
    <text class="fig-blue" x="387" y="127" font-size="9" text-anchor="middle">Cache 行 1（主存块 3 副本）</text>
    <text class="fig-purple" x="237" y="170" font-size="9.5" text-anchor="middle">1</text>
    <text class="fig-orange" x="283" y="170" font-size="9.5" text-anchor="middle">Tag=k</text>
    <text class="fig-label" x="387" y="170" font-size="9.5" text-anchor="middle">Cache 行 C−1</text>
    <text class="fig-label" x="246" y="247" font-size="10" text-anchor="middle">主存地址 ＝【主存块号】＋【块内地址】（两侧块长与块内顺序完全一致，块内地址直通）</text>
    <text class="fig-muted" x="246" y="261" font-size="9.5" text-anchor="middle">命中条件：有效位 V = 1 且 Cache 标记 Tag == CPU 访存块号特征位</text>
    <text class="fig-title" x="502" y="36" font-size="12">块长与命中率 h 关系</text>
    <text class="fig-label" x="504" y="62" font-size="9.5">命中率 h = N1 / (N1 + N2)</text>
    <text class="fig-muted" x="504" y="78" font-size="9">容量固定时块长影响：</text>
    <text class="fig-muted" x="528" y="104" font-size="9">h</text>
    <text class="fig-green" x="594" y="132" font-size="9.5" text-anchor="middle">最佳 4～8 字</text>
    <text class="fig-muted" x="646" y="224" font-size="9" text-anchor="middle">块长 →</text>
    <text class="fig-label" x="502" y="244" font-size="9.5">• 太小：未用足空间局部性</text>
    <text class="fig-red" x="502" y="262" font-size="9.5">• 太大：块数骤减反致冲突</text>
  </svg>
  <figcaption>图 4-16　主存与 Cache 按块调入、有效位 + 标记（Tag）命中判别及容量固定时块长对命中率的倒 U 型影响</figcaption>
</figure>

### 4.9 cache 的访问效率与基本结构

设 cache 访问时间 $t_c$、主存访问时间 $t_m$、命中率 h。若 cache 与主存<strong>并行访问</strong>：

$$
t_a = h\,t_c + (1-h)\,t_m
$$

访问效率

$$
e = \frac{t_c}{t_a}\times 100\% = \frac{t_c}{h\,t_c + (1-h)\,t_m}\times 100\%
$$

$h=0$ 时 $e=t_c/t_m$（最小），$h=1$ 时 $e=1$（最大），故 $t_c/t_m \le e \le 1$。

若采用<strong>先访问 cache、不命中再访问主存</strong>的串行方式，则平均访问时间变为

$$
t_a = t_c + (1-h)\,t_m
$$

<strong>基本结构包含：cache 存储体（按块保存标记与数据）、地址映像与变换机构</strong>、<strong>替换机构</strong>、主存与 cache 之间的直接通路。

- <strong>映像（map）</strong>：规定主存块可以装入 cache 的哪些块——是规则；
- <strong>变换</strong>：把主存块号（地址）转换成 cache 的块号（地址），并完成查找与命中判断。

CPU 给出地址后，块内地址直接送 cache；块号经映像变换机构判断是否命中并给出缓存块号。不命中时，若 cache 尚有可装入的位置则调入；若允许的位置已满，则启动替换机构，按替换算法淘汰一块（写回或作废），把新块调入。有些机器在不命中的同时，经数据线把所需数据直接送给 CPU，以减少等待。

<figure class="cpd-figure not-content" id="lhw-w3-fig-17">
  <svg viewBox="0 0 700 298" role="img" aria-label="Cache硬件四大组成结构数据通路与并行串行平均访问时间公式对比图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="454" height="270" rx="8"/>
    <rect class="fig-box-subtle" x="482" y="14" width="202" height="270" rx="8"/>
    <line class="fig-line-accent" x1="92" y1="90" x2="132" y2="90" marker-end="url(#lhw-w3-arr-17)"/>
    <path class="fig-line" d="M 108 90 L 108 56 L 284 56" marker-end="url(#lhw-w3-arr-17)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="252" y1="90" x2="284" y2="90" marker-end="url(#lhw-w3-arr-17)"/>
    <path class="fig-line" stroke="var(--sl-color-green)" d="M 370 116 L 370 142 L 94 142" marker-end="url(#lhw-w3-arr-17)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="192" y1="118" x2="192" y2="178" marker-end="url(#lhw-w3-arr-17)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="252" y1="208" x2="284" y2="208" marker-end="url(#lhw-w3-arr-17)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="396" y1="180" x2="396" y2="118" marker-end="url(#lhw-w3-arr-17)"/>
    <path class="fig-line-dashed" d="M 342 238 L 342 260 L 60 260 L 60 156" marker-end="url(#lhw-w3-arr-17)"/>
    <rect class="fig-box-purple" x="28" y="72" width="64" height="82" rx="5"/>
    <rect class="fig-box-blue" x="134" y="68" width="118" height="48" rx="5"/>
    <rect class="fig-box-green" x="286" y="48" width="170" height="68" rx="5"/>
    <rect class="fig-box-red" x="134" y="180" width="118" height="58" rx="5"/>
    <rect class="fig-box" x="286" y="180" width="170" height="58" rx="5"/>
    <rect class="fig-box-blue" x="494" y="48" width="178" height="104" rx="5"/>
    <rect class="fig-box-orange" x="494" y="162" width="178" height="108" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">Cache 基本结构与四大硬件通路</text>
    <text class="fig-purple" x="60" y="110" font-size="11.5" text-anchor="middle">CPU</text>
    <text class="fig-muted" x="60" y="128" font-size="9" text-anchor="middle">取指/读写</text>
    <text class="fig-muted" x="196" y="50" font-size="9.5" text-anchor="middle">块内地址（直通，不变换）</text>
    <text class="fig-blue" x="193" y="89" font-size="10.5" text-anchor="middle">地址映像变换机构</text>
    <text class="fig-muted" x="193" y="106" font-size="9" text-anchor="middle">主存块号 → 判命中</text>
    <text class="fig-green" x="268" y="83" font-size="9" text-anchor="middle">命中</text>
    <text class="fig-green" x="371" y="70" font-size="11" text-anchor="middle">Cache 存储体</text>
    <text class="fig-label" x="371" y="88" font-size="9.5" text-anchor="middle">有效位 + Tag + 数据块</text>
    <text class="fig-muted" x="371" y="104" font-size="9" text-anchor="middle">按块组织 SRAM</text>
    <text class="fig-green" x="280" y="132" font-size="9.2" text-anchor="middle">命中：Cache 直接送数给 CPU</text>
    <text class="fig-red" x="164" y="158" font-size="9.5" text-anchor="middle">未命中</text>
    <text class="fig-red" x="193" y="203" font-size="10.5" text-anchor="middle">替换控制机构</text>
    <text class="fig-muted" x="193" y="221" font-size="9" text-anchor="middle">LRU / FIFO / 随机</text>
    <text class="fig-label" x="371" y="203" font-size="11" text-anchor="middle">主存储器（DRAM）</text>
    <text class="fig-muted" x="371" y="221" font-size="9.5" text-anchor="middle">调出目标块 / 接收脏块</text>
    <text class="fig-green" x="430" y="152" font-size="9" text-anchor="middle">直接块通路</text>
    <text class="fig-muted" x="200" y="274" font-size="9.5" text-anchor="middle">虚线旁路：未命中读主存时，目标字可同时直传 CPU 减少等待</text>
    <text class="fig-title" x="494" y="36" font-size="12">平均时间 ta 与效率 e</text>
    <text class="fig-blue" x="504" y="68" font-size="10.5">① 并行访问（同时启动）：</text>
    <text class="fig-label" x="504" y="88" font-size="10">ta = h·tc + (1 − h)·tm</text>
    <text class="fig-label" x="504" y="108" font-size="10">效率 e = tc / ta × 100%</text>
    <text class="fig-muted" x="504" y="128" font-size="9.5">范围：tc/tm ≤ e ≤ 1</text>
    <text class="fig-muted" x="504" y="142" font-size="9">h 越接近 1、tm/tc 越小，e 越高</text>
    <text class="fig-orange" x="504" y="182" font-size="10.5">② 串行访问（先查后访）：</text>
    <text class="fig-label" x="504" y="202" font-size="10">ta = tc + (1 − h)·tm</text>
    <text class="fig-muted" x="504" y="222" font-size="9.5">缺失时多花一次查标记 tc</text>
    <text class="fig-accent" x="504" y="244" font-size="9.5">★ 审题先看是否“同时访问”！</text>
    <text class="fig-muted" x="504" y="260" font-size="9">默认未说明时按题给时序套用</text>
  </svg>
  <figcaption>图 4-17　Cache 硬件四部件组成框图，以及“并行启动”与“先查后访”两种模式的平均访问时间 $t_a$ 及效率 $e$ 公式</figcaption>
</figure>

### 4.10 cache 的读/写操作与改进

<strong>读操作</strong>：CPU 给出地址 → 映像变换机构判断是否命中 → 命中则从 cache 直接取数送 CPU；不命中则同时做两件事：访问主存取所需信息送 CPU；检查该主存块允许装入的 cache 块是否已满，满则用替换算法淘汰一块并把新块调入，不满则直接调入。

<strong>写操作</strong>会造成 cache 与主存内容不一致，必须用写策略解决：

| 策略 | 做法 | 优点 | 缺点 |
|---|---|---|---|
| 写直达（写通过，write through） | 同时写 cache 和主存 | 时刻保持一致；块换出时无需回写 | 反复写同一单元会造成 cache 与主存频繁交换，写时间等于访存时间 |
| 写回（write back） | 只写 cache，块被替换出时才写回主存 | 减少访存次数 | 非实时一致；多处理器各自 cache 有副本时产生<strong>副本一致性</strong>问题 |

<strong>改进措施</strong>：

- <strong>分级</strong>：现代 CPU 的 cache 是多级的，离 CPU 近的（L1、L2）做在片内，主板上再做容量更大的；多核处理器每个核有私有 cache，各核还共享 cache，通常至少三级。
- <strong>统一与分离</strong>：冯·诺依曼结构把指令和数据同等保存，对应<strong>统一 cache</strong>；流水线机器为避免取指与取数冲突，采用<strong>指令 cache 与数据 cache 分离</strong>。例如 Pentium：指令 8K + 数据 8K；PowerPC 620：指令 32K + 数据 32K。

> <strong>注意</strong>：写直达的“写时间”就是访问主存的时间，写回则把多次写合并成一次块回写；前者简单、一致性好，后者速度快但一致性维护复杂。

<figure class="cpd-figure not-content" id="lhw-w3-fig-18">
  <svg viewBox="0 0 700 306" role="img" aria-label="Cache读操作命中缺失双分支流程图与写直达写回一致性策略对比图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="428" height="278" rx="8"/>
    <rect class="fig-box-subtle" x="456" y="14" width="228" height="278" rx="8"/>
    <line class="fig-line-accent" x1="108" y1="82" x2="132" y2="82" marker-end="url(#lhw-w3-arr-18)"/>
    <line class="fig-line" stroke="var(--sl-color-green)" x1="242" y1="72" x2="278" y2="62" marker-end="url(#lhw-w3-arr-18)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="242" y1="94" x2="278" y2="122" marker-end="url(#lhw-w3-arr-18)"/>
    <line class="fig-line" x1="354" y1="154" x2="354" y2="180" marker-end="url(#lhw-w3-arr-18)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="278" y1="202" x2="244" y2="202" marker-end="url(#lhw-w3-arr-18)"/>
    <rect class="fig-box-purple" x="28" y="62" width="80" height="40" rx="5"/>
    <rect class="fig-box-blue" x="134" y="62" width="108" height="40" rx="5"/>
    <rect class="fig-box-green" x="280" y="44" width="150" height="36" rx="5"/>
    <rect class="fig-box" x="280" y="114" width="150" height="40" rx="5"/>
    <rect class="fig-box-orange" x="280" y="182" width="150" height="42" rx="5"/>
    <rect class="fig-box-red" x="134" y="182" width="108" height="42" rx="5"/>
    <rect class="fig-box" x="28" y="238" width="402" height="42" rx="4"/>
    <rect class="fig-box-blue" x="468" y="48" width="204" height="74" rx="5"/>
    <rect class="fig-box-green" x="468" y="130" width="204" height="82" rx="5"/>
    <rect class="fig-box-purple" x="468" y="220" width="204" height="60" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">（a）Cache 读操作双分支硬件流程</text>
    <text class="fig-purple" x="68" y="86" font-size="10.5" text-anchor="middle">CPU 发地址</text>
    <text class="fig-blue" x="188" y="80" font-size="10" text-anchor="middle">查 Tag 判命中</text>
    <text class="fig-muted" x="188" y="95" font-size="9" text-anchor="middle">比较器 + 有效位</text>
    <text class="fig-green" x="256" y="56" font-size="9">命中</text>
    <text class="fig-green" x="355" y="66" font-size="10.5" text-anchor="middle">直接读 Cache 送 CPU</text>
    <text class="fig-red" x="248" y="122" font-size="9">缺失</text>
    <text class="fig-label" x="355" y="131" font-size="10" text-anchor="middle">访主存取字送 CPU</text>
    <text class="fig-muted" x="355" y="147" font-size="9" text-anchor="middle">同时把所在整块调出</text>
    <text class="fig-orange" x="355" y="200" font-size="10" text-anchor="middle">检查目标位置满否？</text>
    <text class="fig-muted" x="355" y="216" font-size="9" text-anchor="middle">未满直装 / 已满淘汰</text>
    <text class="fig-red" x="188" y="200" font-size="10" text-anchor="middle">替换并装入新块</text>
    <text class="fig-muted" x="188" y="216" font-size="9" text-anchor="middle">置 V=1，更新 Tag</text>
    <text class="fig-label" x="229" y="255" font-size="10" text-anchor="middle">流水线机器改进：分离【指令 I-Cache】与【数据 D-Cache】</text>
    <text class="fig-muted" x="229" y="271" font-size="9.5" text-anchor="middle">彻底消除流水线“取指阶段”与“访存阶段”争用同一 Cache 的结构冲突</text>
    <text class="fig-title" x="468" y="36" font-size="12">（b）写一致性两大策略</text>
    <text class="fig-blue" x="478" y="66" font-size="10.5">① 全写法（写直达）</text>
    <text class="fig-label" x="478" y="84" font-size="9.5">同时写 Cache 与主存（无脏位）</text>
    <text class="fig-muted" x="478" y="100" font-size="9">• 实时一致，换出无需回写</text>
    <text class="fig-muted" x="478" y="114" font-size="9">• 缺点：访存频繁（常配写缓冲）</text>
    <text class="fig-green" x="478" y="148" font-size="10.5">② 写回法（Write-Back）</text>
    <text class="fig-label" x="478" y="166" font-size="9.5">只改 Cache 并置【脏位 D=1】</text>
    <text class="fig-muted" x="478" y="182" font-size="9">• 被替换且 D=1 时才整块写回</text>
    <text class="fig-muted" x="478" y="198" font-size="9">• 合并多次写，大幅省总线带宽</text>
    <text class="fig-purple" x="478" y="238" font-size="10">现代多级架构：L1(分)→L2→L3(共)</text>
    <text class="fig-muted" x="478" y="255" font-size="9">L1 极速小容量，L3 多核共享</text>
    <text class="fig-muted" x="478" y="270" font-size="9">多核写回需 MESI 一致性协议</text>
  </svg>
  <figcaption>图 4-18　Cache 读操作命中/缺失流程、写直达 vs 写回（脏位 D）一致性策略及分离式 I/D-Cache 改进</figcaption>
</figure>

### 4.11 直接映射与替换算法

映像方式决定主存块能放到 cache 的哪些块。<strong>直接映射</strong>是最简单的一种：

$$
\text{cache line} = \text{main block} \bmod C
$$

其中 C 为 cache 的行（块）数。主存地址被划分为三段：<strong>标记 | 行号 | 块内地址</strong>。行号位数 = $\log_2 C$，块内地址位数 = $\log_2(\text{block size})$，其余高位为标记。

例：主存 64 KB、块长 16 B、cache 8 行（共 128 B）。主存共 $64\text{KB}/16\text{B}=4096=2^{12}$ 块，地址 16 位，划分为 <strong>标记 9 位 | 行号 3 位 | 块内地址 4 位</strong>；主存块 j 只能放入行 $j \bmod 8$，故同一行被 $4096/8=512$ 个主存块争用。

- <strong>优点</strong>：硬件简单，命中判断只需比较一个标记（一个比较器）。
- <strong>缺点</strong>：冲突率高——映射到同一行的不同块会反复替换，即使 cache 有空行也不能用。
- <strong>替换：位置唯一，无需替换算法</strong>。

作为对比：<strong>全相联允许任一主存块放入任一 cache 行，冲突最少但需比较全部标记，成本高；组相联</strong>把 cache 分成若干组、组内全相联，是两者的折中。

需要替换时常用的算法有：<strong>LRU（最近最少使用）</strong>，替换最久未被访问的块，符合时间局部性，效果最好但硬件较复杂；<strong>FIFO（先进先出），替换最早调入的块；随机法</strong>，实现最简单但命中率不稳定。

> <strong>★考点</strong>：直接映射只需“标记 + 行号 + 块内地址”三段，标记位数 = 主存地址位数 − 行号位数 − 块内地址位数；不要与“组相联”的三段（标记 + 组号 + 块内地址）混淆。

<figure class="cpd-figure not-content" id="lhw-w3-fig-19">
  <svg viewBox="0 0 700 318" role="img" aria-label="直接映射全相联映射与2路组相联映射的16位地址划分及比较器硬件对比图">
    <defs>
      <marker id="lhw-w3-arr-19" 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="14" width="668" height="290" rx="8"/>
    <rect class="fig-box-blue" x="132" y="54" width="186" height="32" rx="3"/>
    <rect class="fig-box-green" x="318" y="54" width="76" height="32" rx="3"/>
    <rect class="fig-box-orange" x="394" y="54" width="84" height="32" rx="3"/>
    <rect class="fig-box" x="492" y="50" width="178" height="40" rx="4"/>
    <rect class="fig-box-blue" x="132" y="130" width="262" height="32" rx="3"/>
    <rect class="fig-box-orange" x="394" y="130" width="84" height="32" rx="3"/>
    <rect class="fig-box" x="492" y="126" width="178" height="40" rx="4"/>
    <rect class="fig-box-blue" x="132" y="206" width="196" height="32" rx="3"/>
    <rect class="fig-box-purple" x="328" y="206" width="66" height="32" rx="3"/>
    <rect class="fig-box-orange" x="394" y="206" width="84" height="32" rx="3"/>
    <rect class="fig-box" x="492" y="202" width="178" height="40" rx="4"/>
    <rect class="fig-box" x="28" y="260" width="642" height="32" rx="4"/>
    <text class="fig-title" x="28" y="36" font-size="12.5">三种映射方式地址划分对比（统一例：主存 64KB = 16 位地址，块长 16B = 4 位，Cache 共 8 行）</text>
    <text class="fig-label" x="30" y="75" font-size="11.5">① 直接映射</text>
    <text class="fig-blue" x="225" y="75" font-size="10.5" text-anchor="middle">标记 Tag（9 位）</text>
    <text class="fig-green" x="356" y="75" font-size="10.5" text-anchor="middle">行号（3位）</text>
    <text class="fig-orange" x="436" y="75" font-size="10" text-anchor="middle">块内（4位）</text>
    <text class="fig-muted" x="304" y="103" font-size="9.5" text-anchor="middle">i = j mod 8：行号直达唯一行，仅需比对 1 次 Tag，无需替换算法，但冲突最严重</text>
    <text class="fig-label" x="502" y="67" font-size="9.5">比较器：1 个（9 位）</text>
    <text class="fig-muted" x="502" y="83" font-size="9">硬件最简 · 空间利用率低</text>
    <text class="fig-label" x="30" y="151" font-size="11.5">② 全相联映射</text>
    <text class="fig-blue" x="263" y="151" font-size="10.5" text-anchor="middle">标记 Tag ＝ 完整主存块号（12 位）</text>
    <text class="fig-orange" x="436" y="151" font-size="10" text-anchor="middle">块内（4位）</text>
    <text class="fig-muted" x="304" y="179" font-size="9.5" text-anchor="middle">任意主存块可放任意空行：全部 8 行并行相联比较，冲突最低，满时才启动替换算法</text>
    <text class="fig-red" x="502" y="143" font-size="9.5">比较器：8 个（12 位并行）</text>
    <text class="fig-muted" x="502" y="159" font-size="9">命中率最高 · 相联电路昂贵</text>
    <text class="fig-label" x="30" y="227" font-size="11.5">③ 2路组相联</text>
    <text class="fig-blue" x="230" y="227" font-size="10.5" text-anchor="middle">标记 Tag（10 位）</text>
    <text class="fig-purple" x="361" y="227" font-size="9.5" text-anchor="middle">组号（2位）</text>
    <text class="fig-orange" x="436" y="227" font-size="10" text-anchor="middle">块内（4位）</text>
    <text class="fig-muted" x="304" y="253" font-size="9.5" text-anchor="middle">分 Q = 8/2 = 4 组（组间直接映射 q = j mod 4，组内 2 行全相联，兼顾速度与命中率）</text>
    <text class="fig-green" x="502" y="219" font-size="9.5">比较器：2 个（路数 r=2）</text>
    <text class="fig-muted" x="502" y="235" font-size="9">每升 1 倍路数，Tag 多 1 位</text>
    <text class="fig-accent" x="349" y="280" font-size="10" text-anchor="middle">★ 通杀公式：块内位数 b = log2(块长)；组数 Q = C / r；Tag 位数 = 主存地址总位数 − log2(Q) − b</text>
  </svg>
  <figcaption>图 4-19　直接映射、全相联映射与 2 路组相联映射的地址字段切分及比较器开销对比（64KB 主存、8 行 Cache）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w3-fig-20">
  <svg viewBox="0 0 700 296" role="img" aria-label="直接映射模8冲突抖动机制与LRU对比FIFO替换算法轨迹图">
    <defs>
      <marker id="lhw-w3-arr-20" 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="14" width="350" height="268" rx="8"/>
    <rect class="fig-box-subtle" x="378" y="14" width="306" height="268" rx="8"/>
    <line class="fig-line" stroke="var(--sl-color-blue)" x1="134" y1="68" x2="210" y2="68" marker-end="url(#lhw-w3-arr-20)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="134" y1="138" x2="210" y2="74" marker-end="url(#lhw-w3-arr-20)"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="134" y1="176" x2="210" y2="80" marker-end="url(#lhw-w3-arr-20)"/>
    <line class="fig-line" x1="134" y1="102" x2="210" y2="108" marker-end="url(#lhw-w3-arr-20)"/>
    <rect class="fig-box-blue" x="26" y="54" width="108" height="28" rx="3"/>
    <rect class="fig-box" x="26" y="88" width="108" height="28" rx="3"/>
    <rect class="fig-box-red" x="26" y="124" width="108" height="28" rx="3"/>
    <rect class="fig-box-red" x="26" y="160" width="108" height="28" rx="3"/>
    <rect class="fig-box-red" x="212" y="54" width="140" height="32" rx="3"/>
    <rect class="fig-box" x="212" y="94" width="140" height="28" rx="3"/>
    <rect class="fig-box" x="212" y="160" width="140" height="28" rx="3"/>
    <rect class="fig-box" x="28" y="212" width="324" height="56" rx="4"/>
    <rect class="fig-box-green" x="390" y="50" width="282" height="104" rx="5"/>
    <rect class="fig-box-orange" x="390" y="164" width="282" height="104" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12">（a）直接映射模 C 争用与“颠簸（抖动）”</text>
    <text class="fig-blue" x="80" y="72" font-size="9" text-anchor="middle">主存块 0 (Tag=0)</text>
    <text class="fig-label" x="80" y="106" font-size="9" text-anchor="middle">主存块 1 (Tag=0)</text>
    <text class="fig-red" x="80" y="142" font-size="9" text-anchor="middle">主存块 8 (Tag=1)</text>
    <text class="fig-red" x="80" y="178" font-size="9" text-anchor="middle">主存块 16 (Tag=2)</text>
    <text class="fig-red" x="282" y="74" font-size="10" text-anchor="middle">Cache 行 0（激烈冲突！）</text>
    <text class="fig-muted" x="282" y="112" font-size="9.5" text-anchor="middle">Cache 行 1（空闲闲置）</text>
    <text class="fig-muted" x="282" y="142" font-size="11" text-anchor="middle">⋮</text>
    <text class="fig-muted" x="282" y="178" font-size="9.5" text-anchor="middle">Cache 行 7（空闲闲置）</text>
    <text class="fig-label" x="190" y="231" font-size="9.5" text-anchor="middle">规则：行号 = j mod 8，Tag = ⌊j / 8⌋（共 512 块抢 1 行）</text>
    <text class="fig-red" x="190" y="248" font-size="9.5" text-anchor="middle">痛点：交替访问块 0 与块 8 时命中率跌至 0%，其余空行却无法借用</text>
    <text class="fig-muted" x="190" y="262" font-size="9" text-anchor="middle">解决途径：改用 2 路 / 4 路组相联，同一组可同时容纳多个同模块</text>
    <text class="fig-title" x="390" y="36" font-size="12">（b）全相联/组相联替换算法对比</text>
    <text class="fig-green" x="400" y="69" font-size="10.5">① LRU（最近最少使用，命中率最高）</text>
    <text class="fig-label" x="400" y="87" font-size="9.5">淘汰【最久未被访问】的块（遵循时间局部性）</text>
    <text class="fig-muted" x="400" y="104" font-size="9">硬件实现（年龄计数器）：命中行清 0，同组其余</text>
    <text class="fig-muted" x="400" y="119" font-size="9">小于它的行 +1；缺失满时替换计数值最大的行</text>
    <text class="fig-green" x="400" y="138" font-size="9">例(2路访 A,B,A,C)：A 刚被访保留，淘汰最久未访 B</text>
    <text class="fig-orange" x="400" y="183" font-size="10.5">② FIFO（先进先出）与 RAND（随机）</text>
    <text class="fig-label" x="400" y="201" font-size="9.5">FIFO 淘汰【最早调入】的块，不看近期是否热访问</text>
    <text class="fig-red" x="400" y="218" font-size="9">例(2路访 A,B,A,C)：A 最早进，虽刚命中仍被误杀！</text>
    <text class="fig-muted" x="400" y="235" font-size="9">• FIFO 存在 Belady 异常（扩容后缺失率偶见反升）</text>
    <text class="fig-muted" x="400" y="252" font-size="9">• 随机法硬件最简，无历史开销，但命中率波动较大</text>
  </svg>
  <figcaption>图 4-20　直接映射模 $C$ 冲突抖动成因，以及组相联/全相联下 LRU 年龄计数器与 FIFO 替换轨迹对比</figcaption>
</figure>

### 4.12 辅助存储器

辅助存储器用于保存程序、数据、文档和音像资料，<strong>不能直接与 CPU 交换信息</strong>，必须先调入主存。最常用的是磁表面存储器。

| 指标 | 含义 |
|---|---|
| 道密度 | 沿盘片半径方向单位长度的磁道数 |
| 位密度 | 单位长度磁道上记录的二进制位数；磁盘同心圆磁道越靠外，位密度越低 |
| 存储容量 | 盘面数 × 每面磁道数 × 每道二进制位数 |
| 平均寻址时间 | 平均<strong>寻道时间</strong>（磁头移动到目标磁道）<br>+ 平均<strong>等待时间</strong>（等待目标扇区转到磁头下） |
| 数据传输率 | 位密度 × 转速（目标磁道从磁头下经过的这段时间内完成读写） |
| 误码率 | 出错信息位数与读出信息总数之比 |

$$
\text{Capacity} = \text{surfaces}\times\text{tracks/surface}\times\text{bits/track}
$$

<strong>磁记录原理</strong>：写时给磁头线圈通以不同方向的电流，铁芯产生不同方向的磁通，把盘片磁层磁化，用磁化方向区分 0 与 1；读时磁层相对磁头运动，切割磁力线，线圈中产生感应电动势，由电动势变化判别 0/1。

<strong>硬磁盘类型</strong>：

- 按磁头是否固定：<strong>固定磁头（每个磁道一个磁头，盘片只转不平移，速度快但磁头数量庞大、结构复杂）；移动磁头</strong>（每面一个磁头，沿半径移动，常用）。
- 按盘片是否可更换：<strong>可换盘</strong>与<strong>固定盘</strong>。现代硬盘多为固定盘，盘片一旦取出沾尘即报废。

<strong>结构</strong>：主机 → <strong>磁盘控制器</strong>（主机与驱动器之间的接口，完成命令转换、数据格式转换、读写控制）→ <strong>磁盘驱动器</strong>（主轴与传动机构带动盘片组旋转、磁头悬浮于盘面、小车带动磁头沿半径移动、音圈电机闭环控制位置与速度）。

<strong>软盘</strong>：速度低、磁头直接接触盘片、塑料盘片可更换、价格低、环境要求低；现已退出市场。

<strong>光盘</strong>：采用光存储技术，用激光读写。只读型或一次性写入型用激光热作用烧灼盘面，写后不可改；可擦写型采用磁光材料，可多次写入。

<figure class="cpd-figure not-content" id="lhw-w3-fig-21">
  <svg viewBox="0 0 700 308" role="img" aria-label="硬磁盘盘面磁道扇区柱面几何结构与平均存取时间三段计算公式图">
    <defs>
      <marker id="lhw-w3-arr-21" 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="14" width="356" height="280" rx="8"/>
    <rect class="fig-box-subtle" x="384" y="14" width="300" height="280" rx="8"/>
    <circle cx="132" cy="158" r="90" fill="none" stroke="var(--sl-color-blue)" stroke-width="1.6"/>
    <circle cx="132" cy="158" r="64" fill="none" stroke="var(--sl-color-gray-4)" stroke-width="1.1"/>
    <circle cx="132" cy="158" r="42" fill="none" stroke="var(--sl-color-green)" stroke-width="1.6"/>
    <circle cx="132" cy="158" r="16" fill="none" stroke="var(--sl-color-gray-4)" stroke-width="1.2"/>
    <line class="fig-divider" x1="132" y1="68" x2="132" y2="248"/>
    <line class="fig-divider" x1="42" y1="158" x2="222" y2="158"/>
    <line class="fig-divider" x1="68" y1="94" x2="196" y2="222"/>
    <line class="fig-divider" x1="196" y1="94" x2="68" y2="222"/>
    <line class="fig-line" stroke="var(--sl-color-red)" x1="306" y1="118" x2="206" y2="118"/>
    <line class="fig-line" stroke="var(--sl-color-orange)" x1="294" y1="140" x2="358" y2="140" marker-start="url(#lhw-w3-arr-21)" marker-end="url(#lhw-w3-arr-21)"/>
    <line class="fig-line-dashed" x1="292" y1="188" x2="292" y2="244"/>
    <ellipse cx="292" cy="196" rx="42" ry="8" fill="none" stroke="var(--sl-color-purple)" stroke-width="1.3"/>
    <ellipse cx="292" cy="218" rx="42" ry="8" fill="none" stroke="var(--sl-color-purple)" stroke-width="1.3"/>
    <ellipse cx="292" cy="240" rx="42" ry="8" fill="none" stroke="var(--sl-color-purple)" stroke-width="1.3"/>
    <circle class="fig-dot" cx="132" cy="158" r="4"/>
    <rect class="fig-box-red" x="192" y="110" width="22" height="16" rx="3"/>
    <rect class="fig-box-orange" x="292" y="106" width="68" height="26" rx="4"/>
    <rect class="fig-box-blue" x="394" y="48" width="280" height="72" rx="5"/>
    <rect class="fig-box-green" x="394" y="128" width="280" height="86" rx="5"/>
    <rect class="fig-box-purple" x="394" y="222" width="280" height="60" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12">（a）硬磁盘盘面（磁道/扇区）与柱面结构</text>
    <polyline points="64,238 82,238 102,188" fill="none" stroke="var(--sl-color-green)" stroke-width="1"/>
    <text class="fig-blue" x="132" y="57" font-size="9.5" text-anchor="middle">外圈磁道（周长最大，位密度最低）</text>
    <text class="fig-green" x="30" y="242" font-size="9" text-anchor="start">最内道</text>
    <text class="fig-label" x="186" y="182" font-size="9.5">扇区</text>
    <text class="fig-muted" x="132" y="268" font-size="9.5" text-anchor="middle">主轴旋转（每道总位数相等！）</text>
    <text class="fig-red" x="232" y="106" font-size="9.5" text-anchor="start">磁头</text>
    <text class="fig-orange" x="326" y="123" font-size="9.5" text-anchor="middle">音圈小车</text>
    <text class="fig-muted" x="326" y="155" font-size="8.8" text-anchor="middle">沿半径寻道</text>
    <text class="fig-purple" x="292" y="180" font-size="9.2" text-anchor="middle">同半径构成【柱面】</text>
    <text class="fig-muted" x="292" y="262" font-size="9" text-anchor="middle">柱面数 = 单面磁道数</text>
    <text class="fig-title" x="394" y="36" font-size="12">（b）容量与平均存取时间 408 公式</text>
    <text class="fig-blue" x="404" y="66" font-size="10">① 非格式化容量（按最内道最大位密度算）：</text>
    <text class="fig-label" x="404" y="84" font-size="8.8">容量 = 记录面数 × 每面道数 × 最内道周长 × 位密度</text>
    <text class="fig-muted" x="404" y="102" font-size="8.8">注意：n 张双面盘片有效记录面常为 2n 或 2n − 2</text>
    <text class="fig-green" x="404" y="146" font-size="10">② 一次读写平均存取时间 Ta（三段求和）：</text>
    <text class="fig-label" x="404" y="165" font-size="9">Ta = Tseek(寻道) + 1/(2r)(旋转) + b/(rN)(传输)</text>
    <text class="fig-muted" x="404" y="183" font-size="8.8">• r：每秒转数（如 7200 rpm → r = 120 转/秒）</text>
    <text class="fig-muted" x="404" y="199" font-size="8.8">• 平均旋转延迟 = 半圈时间 = 1 / (2r)</text>
    <text class="fig-purple" x="404" y="240" font-size="9.2">③ 磁盘地址三元组：[柱面号 | 盘面号 | 扇区号]</text>
    <text class="fig-muted" x="404" y="258" font-size="8.4">柱面号排最高位：连续数据填同柱面各盘面，</text>
    <text class="fig-muted" x="404" y="272" font-size="8.4">电子切磁头无需移动小车，大幅减少寻道时间！</text>
  </svg>
  <figcaption>图 4-21　硬磁盘盘面（磁道、扇区、柱面）几何结构与平均存取时间三段公式（寻道 + 平均半周旋转等待 + 传输）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w3-fig-22">
  <svg viewBox="0 0 700 282" role="img" aria-label="廉价冗余磁盘阵列RAID0条带化RAID1镜像与RAID5分布式奇偶校验对比图">
    <rect class="fig-box-subtle" x="16" y="14" width="212" height="254" rx="8"/>
    <rect class="fig-box-subtle" x="240" y="14" width="212" height="254" rx="8"/>
    <rect class="fig-box-subtle" x="464" y="14" width="220" height="254" rx="8"/>
    <rect class="fig-box" x="34" y="54" width="80" height="122" rx="5"/>
    <rect class="fig-box" x="130" y="54" width="80" height="122" rx="5"/>
    <rect class="fig-box-blue" x="42" y="76" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="138" y="76" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="42" y="106" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="138" y="106" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="42" y="136" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="138" y="136" width="64" height="24" rx="3"/>
    <rect class="fig-box-red" x="28" y="186" width="188" height="70" rx="5"/>
    <rect class="fig-box" x="258" y="54" width="80" height="122" rx="5"/>
    <rect class="fig-box" x="354" y="54" width="80" height="122" rx="5"/>
    <rect class="fig-box-blue" x="266" y="76" width="64" height="24" rx="3"/>
    <rect class="fig-box-green" x="362" y="76" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="266" y="106" width="64" height="24" rx="3"/>
    <rect class="fig-box-green" x="362" y="106" width="64" height="24" rx="3"/>
    <rect class="fig-box-blue" x="266" y="136" width="64" height="24" rx="3"/>
    <rect class="fig-box-green" x="362" y="136" width="64" height="24" rx="3"/>
    <rect class="fig-box-green" x="252" y="186" width="188" height="70" rx="5"/>
    <rect class="fig-box" x="478" y="54" width="60" height="122" rx="5"/>
    <rect class="fig-box" x="544" y="54" width="60" height="122" rx="5"/>
    <rect class="fig-box" x="610" y="54" width="60" height="122" rx="5"/>
    <rect class="fig-box-blue" x="484" y="76" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="550" y="76" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="616" y="76" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="484" y="106" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="550" y="106" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="616" y="106" width="48" height="24" rx="3"/>
    <rect class="fig-box-purple" x="484" y="136" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="550" y="136" width="48" height="24" rx="3"/>
    <rect class="fig-box-blue" x="616" y="136" width="48" height="24" rx="3"/>
    <rect class="fig-box-orange" x="476" y="186" width="196" height="70" rx="5"/>
    <text class="fig-title" x="28" y="36" font-size="12">① RAID 0（条带化分块）</text>
    <text class="fig-muted" x="74" y="69" font-size="9.5" text-anchor="middle">盘 0</text>
    <text class="fig-muted" x="170" y="69" font-size="9.5" text-anchor="middle">盘 1</text>
    <text class="fig-blue" x="74" y="92" font-size="10" text-anchor="middle">块 D0</text>
    <text class="fig-blue" x="170" y="92" font-size="10" text-anchor="middle">块 D1</text>
    <text class="fig-blue" x="74" y="122" font-size="10" text-anchor="middle">块 D2</text>
    <text class="fig-blue" x="170" y="122" font-size="10" text-anchor="middle">块 D3</text>
    <text class="fig-blue" x="74" y="152" font-size="10" text-anchor="middle">块 D4</text>
    <text class="fig-blue" x="170" y="152" font-size="10" text-anchor="middle">块 D5</text>
    <text class="fig-red" x="38" y="204" font-size="9.5">• 空间利用率：100%（无校验）</text>
    <text class="fig-label" x="38" y="222" font-size="9.5">• 并发读写带宽最高（类低位交叉）</text>
    <text class="fig-red" x="38" y="240" font-size="9.5">• 无冗余容错：坏 1 盘全盘丢数据</text>
    <text class="fig-title" x="252" y="36" font-size="12">② RAID 1（全盘镜像备份）</text>
    <text class="fig-muted" x="298" y="69" font-size="9.5" text-anchor="middle">数据盘</text>
    <text class="fig-muted" x="394" y="69" font-size="9.5" text-anchor="middle">镜像盘</text>
    <text class="fig-blue" x="298" y="92" font-size="10" text-anchor="middle">块 D0</text>
    <text class="fig-green" x="394" y="92" font-size="10" text-anchor="middle">副本 D0′</text>
    <text class="fig-blue" x="298" y="122" font-size="10" text-anchor="middle">块 D1</text>
    <text class="fig-green" x="394" y="122" font-size="10" text-anchor="middle">副本 D1′</text>
    <text class="fig-blue" x="298" y="152" font-size="10" text-anchor="middle">块 D2</text>
    <text class="fig-green" x="394" y="152" font-size="10" text-anchor="middle">副本 D2′</text>
    <text class="fig-green" x="262" y="204" font-size="9.5">• 空间利用率：50%（成本最高）</text>
    <text class="fig-label" x="262" y="222" font-size="9.5">• 双盘完全同步拷贝，读可分流加速</text>
    <text class="fig-green" x="262" y="240" font-size="9.5">• 高可靠：单盘损坏零延迟无缝接管</text>
    <text class="fig-title" x="476" y="36" font-size="12">③ RAID 5（分布式奇偶校验）</text>
    <text class="fig-muted" x="508" y="69" font-size="9.5" text-anchor="middle">盘 0</text>
    <text class="fig-muted" x="574" y="69" font-size="9.5" text-anchor="middle">盘 1</text>
    <text class="fig-muted" x="640" y="69" font-size="9.5" text-anchor="middle">盘 2</text>
    <text class="fig-blue" x="508" y="92" font-size="9.5" text-anchor="middle">D0</text>
    <text class="fig-blue" x="574" y="92" font-size="9.5" text-anchor="middle">D1</text>
    <text class="fig-purple" x="640" y="92" font-size="9.5" text-anchor="middle">P0</text>
    <text class="fig-blue" x="508" y="122" font-size="9.5" text-anchor="middle">D2</text>
    <text class="fig-purple" x="574" y="122" font-size="9.5" text-anchor="middle">P1</text>
    <text class="fig-blue" x="640" y="122" font-size="9.5" text-anchor="middle">D3</text>
    <text class="fig-purple" x="508" y="152" font-size="9.5" text-anchor="middle">P2</text>
    <text class="fig-blue" x="574" y="152" font-size="9.5" text-anchor="middle">D4</text>
    <text class="fig-blue" x="640" y="152" font-size="9.5" text-anchor="middle">D5</text>
    <text class="fig-orange" x="486" y="204" font-size="9.5">• 利用率：(N−1)/N（至少 N≥3 盘）</text>
    <text class="fig-label" x="486" y="222" font-size="9.5">• 奇偶块 Pi 轮流分散免除单盘写瓶颈</text>
    <text class="fig-orange" x="486" y="240" font-size="9.5">• 容忍任意 1 块盘损坏（异或重建）</text>
  </svg>
  <figcaption>图 4-22　廉价冗余磁盘阵列（RAID）典型级别对比：RAID 0 条带化提速、RAID 1 双盘镜像、RAID 5 分布式奇偶校验</figcaption>
</figure>

## 第5章 输入输出系统

### 5.1 输入输出系统概述

I/O 系统是计算机中<strong>种类最多、功能最多、结构最复杂、构成最多样</strong>的部分，现代机器外设成本可占整机总成本的 <strong>80% 以上。它的发展只有一条主线：数据输入输出操作不断从 CPU 中分离出来，I/O 的自治能力不断增强</strong>。

| 阶段 | 连接方式 | 传送控制方式 | CPU 与 I/O 的关系 |
|---|---|---|---|
| 早期 | <strong>分散连接</strong>：每台设备一套专门控制线路，甚至与 CPU 控制电路设计在一起（集中耦合），增删设备困难 | 程序查询 | 串行，CPU 查询等待 |
| 接口/DMA 时期 | 出现 I/O 接口、DMA 控制器；设备经接口挂到<strong>总线</strong>上，一条总线可接多台设备 | 程序中断、DMA | 部分并行 |
| 通道 | 设备→设备控制器→子通道→通道 | 通道程序 | 设备与主存直接传送，CPU 只负责启动 |
| I/O 处理机 | 专用处理器，或直接用通用微处理器（大型机可用与主机同型的处理器） | I/O 处理机程序 | I/O 处理机空闲时甚至可承担主机计算任务 |

分散连接以“一设备一套线路”换来了控制的简单，代价是增删设备困难；设备一多，这种方式立刻成为瓶颈，于是改为总线+接口。五种控制方式中，<strong>程序查询、程序中断、DMA</strong> 是本章重点，通道与 I/O 处理机只作了解。

<figure class="cpd-figure not-content" id="lhw-w4-fig-1">
  <svg viewBox="0 0 700 248" role="img" aria-label="输入输出系统四阶段演进与自治能力提升对比图">
    <defs>
      <marker id="lhw-w4-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="14" width="154" height="176" rx="7"/>
    <rect class="fig-box-subtle" x="186" y="14" width="154" height="176" rx="7"/>
    <rect class="fig-box-subtle" x="356" y="14" width="154" height="176" rx="7"/>
    <rect class="fig-box-subtle" x="526" y="14" width="158" height="176" rx="7"/>
    <rect class="fig-box-accent" x="16" y="198" width="668" height="36" rx="6"/>
    <line class="fig-line-accent" x1="170" y1="102" x2="186" y2="102" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line-accent" x1="340" y1="102" x2="356" y2="102" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line-accent" x1="510" y1="102" x2="526" y2="102" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line" x1="68" y1="74" x2="48" y2="106" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line" x1="93" y1="74" x2="93" y2="106" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line" x1="118" y1="74" x2="138" y2="106" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line" x1="200" y1="90" x2="326" y2="90"/>
    <line class="fig-line" x1="232" y1="74" x2="232" y2="90"/>
    <line class="fig-line" x1="294" y1="74" x2="294" y2="90"/>
    <line class="fig-line" x1="232" y1="90" x2="232" y2="106"/>
    <line class="fig-line" x1="294" y1="90" x2="294" y2="106"/>
    <line class="fig-line" x1="433" y1="72" x2="433" y2="86" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line" x1="433" y1="110" x2="433" y2="124" marker-end="url(#lhw-w4-arr-1)"/>
    <line class="fig-line" x1="605" y1="74" x2="605" y2="106" marker-end="url(#lhw-w4-arr-1)"/>
    <rect class="fig-box-red" x="38" y="46" width="110" height="28" rx="4"/>
    <rect class="fig-box" x="26" y="106" width="42" height="26" rx="3"/>
    <rect class="fig-box" x="72" y="106" width="42" height="26" rx="3"/>
    <rect class="fig-box" x="118" y="106" width="42" height="26" rx="3"/>
    <rect class="fig-box-blue" x="198" y="46" width="68" height="28" rx="4"/>
    <rect class="fig-box-blue" x="272" y="46" width="56" height="28" rx="4"/>
    <rect class="fig-box-orange" x="198" y="106" width="68" height="26" rx="3"/>
    <rect class="fig-box-purple" x="272" y="106" width="56" height="26" rx="3"/>
    <rect class="fig-box-blue" x="374" y="46" width="118" height="26" rx="4"/>
    <rect class="fig-box-purple" x="374" y="86" width="118" height="24" rx="4"/>
    <rect class="fig-box" x="374" y="124" width="118" height="24" rx="4"/>
    <rect class="fig-box-blue" x="542" y="46" width="126" height="28" rx="4"/>
    <rect class="fig-box-green" x="542" y="106" width="126" height="32" rx="4"/>
    <text class="fig-title" x="26" y="34" font-size="11.5">① 早期：分散连接</text>
    <text class="fig-red" x="93" y="64" font-size="10" text-anchor="middle">CPU（内含专控线）</text>
    <text class="fig-label" x="47" y="123" font-size="9.5" text-anchor="middle">外设1</text>
    <text class="fig-label" x="93" y="123" font-size="9.5" text-anchor="middle">外设2</text>
    <text class="fig-label" x="139" y="123" font-size="9.5" text-anchor="middle">外设3</text>
    <text class="fig-muted" x="26" y="154" font-size="9.5">• 控制：程序查询等待</text>
    <text class="fig-red" x="26" y="172" font-size="9.5">• 关系：完全串行，增删难</text>
    <text class="fig-title" x="196" y="34" font-size="11.5">② 接口与 DMA 期</text>
    <text class="fig-blue" x="232" y="64" font-size="10" text-anchor="middle">CPU</text>
    <text class="fig-blue" x="300" y="64" font-size="10" text-anchor="middle">主存</text>
    <text class="fig-muted" x="263" y="86" font-size="8.5" text-anchor="middle">系统总线</text>
    <text class="fig-orange" x="232" y="123" font-size="9.5" text-anchor="middle">中断接口</text>
    <text class="fig-purple" x="300" y="123" font-size="9.5" text-anchor="middle">DMA</text>
    <text class="fig-muted" x="196" y="154" font-size="9.5">• 控制：程序中断 / DMA</text>
    <text class="fig-orange" x="196" y="172" font-size="9.5">• 关系：主存直通，部分并行</text>
    <text class="fig-title" x="366" y="34" font-size="11.5">③ 通道管理期</text>
    <text class="fig-blue" x="433" y="63" font-size="8.6" text-anchor="middle">CPU + 主存 (通道程序)</text>
    <text class="fig-purple" x="433" y="102" font-size="9.5" text-anchor="middle">通道 → 多子通道</text>
    <text class="fig-label" x="433" y="140" font-size="9.5" text-anchor="middle">设备控制器 → 外设群</text>
    <text class="fig-muted" x="366" y="164" font-size="9.5">• CPU 仅发启动命令</text>
    <text class="fig-purple" x="366" y="180" font-size="9.5">• 通道执行通道指令自治</text>
    <text class="fig-title" x="536" y="34" font-size="11.5">④ I/O 处理机期</text>
    <text class="fig-blue" x="605" y="64" font-size="10" text-anchor="middle">主机 CPU + 主存</text>
    <text class="fig-green" x="605" y="126" font-size="8.6" text-anchor="middle">专用 I/O 处理机 (局部存)</text>
    <text class="fig-muted" x="536" y="156" font-size="9.5">• 独立指令集与处理器</text>
    <text class="fig-green" x="536" y="174" font-size="9.5">• 高度并行，空闲可助算</text>
    <text class="fig-accent" x="350" y="214" font-size="10.5" text-anchor="middle">演进核心主线：I/O 操作持续从 CPU 剥离 → CPU 介入粒度由「每字空转 → 每字中断 → 每数据块 → 整套通道程序」递减</text>
    <text class="fig-muted" x="350" y="228" font-size="9.5" text-anchor="middle">408 高频考点：现代外设成本可占整机 80% 以上；程序查询、程序中断、DMA 为考查核心，通道与 I/O 处理机重在概念辨析</text>
  </svg>
  <figcaption>图 5-1　I/O 系统四阶段连接与控制演进对比（CPU 与外设耦合度递减、I/O 自治能力与并行度递增）</figcaption>
</figure>

<strong>I/O 系统的软件</strong>

<strong>I/O 指令</strong>：CPU 指令集中的指令，编在应用程序里，由 CPU 执行，格式为：

| 操作码 | 命令码 | 设备码 |
|---|---|---|
| 标识这是一条 I/O 指令 | 查询／输入／输出／其他控制 | 设备编码，即设备地址或 I/O 端口地址 |

- <strong>通道指令</strong>：通道自身的指令，组成通道程序，一般较长（IBM 370 为 <strong>64 位</strong>）。需给出成块数据的<strong>主存首址（单字节传送时给出该字节地址）、传送字数</strong>、<strong>传送方向</strong>、<strong>设备地址</strong>。
- <strong>广义 I/O 指令</strong>：应用程序中只给出参加传送的设备、主存首址、传送长度与方向；操作系统据此编写通道程序，放到内存或通道存储器的指定位置，再启动通道。

<strong>I/O 系统的硬件</strong>：I/O 接口、设备控制器、子通道、通道、DMA 控制器。通道下可带若干<strong>子通道</strong>，子通道可并行工作；每个子通道下可接多个设备控制器，每个控制器又可接多台设备。

<figure class="cpd-figure not-content" id="lhw-w4-fig-2">
  <svg viewBox="0 0 700 294" role="img" aria-label="IO指令与通道指令格式对比及通道四级层次结构与三类通道对比图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="328" height="266" rx="7"/>
    <rect class="fig-box-subtle" x="356" y="14" width="328" height="266" rx="7"/>
    <line class="fig-line" x1="520" y1="68" x2="520" y2="82" marker-end="url(#lhw-w4-arr-2)"/>
    <line class="fig-line" x1="480" y1="106" x2="430" y2="122" marker-end="url(#lhw-w4-arr-2)"/>
    <line class="fig-line" x1="560" y1="106" x2="610" y2="122" marker-end="url(#lhw-w4-arr-2)"/>
    <line class="fig-line" x1="430" y1="146" x2="430" y2="160" marker-end="url(#lhw-w4-arr-2)"/>
    <line class="fig-line" x1="610" y1="146" x2="610" y2="160" marker-end="url(#lhw-w4-arr-2)"/>
    <rect class="fig-box-blue" x="28" y="60" width="90" height="28" rx="3"/>
    <rect class="fig-box-orange" x="120" y="60" width="102" height="28" rx="3"/>
    <rect class="fig-box-green" x="224" y="60" width="106" height="28" rx="3"/>
    <rect class="fig-box-purple" x="28" y="140" width="66" height="28" rx="3"/>
    <rect class="fig-box-blue" x="96" y="140" width="76" height="28" rx="3"/>
    <rect class="fig-box-orange" x="174" y="140" width="74" height="28" rx="3"/>
    <rect class="fig-box-green" x="250" y="140" width="80" height="28" rx="3"/>
    <rect class="fig-box" x="26" y="206" width="308" height="62" rx="5"/>
    <rect class="fig-box-blue" x="446" y="44" width="148" height="24" rx="4"/>
    <rect class="fig-box-purple" x="454" y="82" width="132" height="24" rx="4"/>
    <rect class="fig-box" x="372" y="122" width="116" height="24" rx="4"/>
    <rect class="fig-box" x="552" y="122" width="116" height="24" rx="4"/>
    <rect class="fig-box-subtle" x="372" y="160" width="116" height="22" rx="3"/>
    <rect class="fig-box-subtle" x="552" y="160" width="116" height="22" rx="3"/>
    <rect class="fig-box-green" x="362" y="192" width="102" height="76" rx="4"/>
    <rect class="fig-box-blue" x="469" y="192" width="102" height="76" rx="4"/>
    <rect class="fig-box-orange" x="576" y="192" width="102" height="76" rx="4"/>
    <text class="fig-title" x="28" y="34" font-size="12">① I/O 指令 vs 通道指令辨析</text>
    <text class="fig-muted" x="26" y="53" font-size="8.8">CPU I/O 指令（通用指令系统，CPU 执行，用户/OS 可编）：</text>
    <text class="fig-blue" x="73" y="78" font-size="10" text-anchor="middle">操作码 OP</text>
    <text class="fig-orange" x="171" y="78" font-size="10" text-anchor="middle">命令码（读/写/查）</text>
    <text class="fig-green" x="277" y="78" font-size="10" text-anchor="middle">设备码（端口址）</text>
    <text class="fig-muted" x="28" y="114" font-size="9.5">通道指令 CCW（属通道专用指令，通道硬件执行，存于主存如 64 位）：</text>
    <text class="fig-muted" x="28" y="131" font-size="9.5">构成通道程序，由操作系统响应「广义 I/O 指令（SVC）」后组织启动</text>
    <text class="fig-purple" x="61" y="158" font-size="9.5" text-anchor="middle">操作码</text>
    <text class="fig-blue" x="134" y="158" font-size="9.5" text-anchor="middle">主存首址</text>
    <text class="fig-orange" x="211" y="158" font-size="9.5" text-anchor="middle">传送字数</text>
    <text class="fig-green" x="290" y="158" font-size="9.5" text-anchor="middle">标志/设备址</text>
    <text class="fig-label" x="38" y="224" font-size="10">★ 易混点速记：</text>
    <text class="fig-muted" x="34" y="241" font-size="8.8">• I/O 指令不直接给主存成块首址，只发启动或单字读写</text>
    <text class="fig-muted" x="34" y="257" font-size="8.8">• 通道指令含「主存首址 + 计数 + 方向」，与主存直传块</text>
    <text class="fig-title" x="368" y="34" font-size="12">② 通道四级拓扑与三类通道</text>
    <text class="fig-blue" x="520" y="60" font-size="10" text-anchor="middle">CPU + 主存（1 对多通道）</text>
    <text class="fig-purple" x="520" y="98" font-size="10" text-anchor="middle">通道（含多个并行子通道）</text>
    <text class="fig-label" x="430" y="138" font-size="9.5" text-anchor="middle">子通道1→设备控制器</text>
    <text class="fig-label" x="610" y="138" font-size="9.5" text-anchor="middle">子通道k→设备控制器</text>
    <text class="fig-muted" x="430" y="175" font-size="9" text-anchor="middle">外设 1 … 外设 m</text>
    <text class="fig-muted" x="610" y="175" font-size="9" text-anchor="middle">外设 1 … 外设 n</text>
    <text class="fig-green" x="413" y="209" font-size="10" text-anchor="middle">字节多路通道</text>
    <text class="fig-muted" x="413" y="226" font-size="8.6" text-anchor="middle">多子通道分时轮转</text>
    <text class="fig-muted" x="413" y="242" font-size="8.6" text-anchor="middle">每次仅传 1 字节</text>
    <text class="fig-label" x="413" y="258" font-size="8.4" text-anchor="middle">连大量慢速字符设备</text>
    <text class="fig-blue" x="520" y="209" font-size="10" text-anchor="middle">数组多路通道</text>
    <text class="fig-muted" x="520" y="226" font-size="8.6" text-anchor="middle">按数据块分时切换</text>
    <text class="fig-muted" x="520" y="242" font-size="8.6" text-anchor="middle">寻道并行/传输串行</text>
    <text class="fig-label" x="520" y="258" font-size="8.4" text-anchor="middle">连多台高速磁盘机</text>
    <text class="fig-orange" x="627" y="209" font-size="10" text-anchor="middle">选择通道</text>
    <text class="fig-muted" x="627" y="226" font-size="8.6" text-anchor="middle">单子通道独占传输</text>
    <text class="fig-muted" x="627" y="242" font-size="8.6" text-anchor="middle">整批传完才换设备</text>
    <text class="fig-label" x="627" y="258" font-size="8.4" text-anchor="middle">连高速外设但利用率低</text>
  </svg>
  <figcaption>图 5-2　I/O 软件指令（CPU I/O 指令 vs 通道指令）格式对比与通道四级硬件拓扑及三类通道特征</figcaption>
</figure>

> <strong>★考点</strong>：外设成本占比（80%）、四种连接/控制阶段的演进顺序、I/O 指令与通道指令的区别（谁执行、放在哪、参数有哪些）。

### 5.2 I/O 设备

外设种类极多，只能按标准分类：

| 大类 | 作用 | 典型设备 |
|---|---|---|
| 人机交互设备 | 人的信息 ↔ 机器二进制信息 | 输入：键盘、鼠标、触摸屏；输出：显示器、打印机 |
| 信息存储设备 | 存储系统的一部分，同时是外设 | 磁盘、光盘、磁带 |
| 机机通信设备 | 计算机与计算机／其他设备交换信息 | 调制解调器、网卡 |

<strong>输入设备</strong>

- <strong>编码键盘</strong>：按键排成矩阵，计数器经行译码、列译码扫描；扫到被按下的键时计数器停止计数，计数值作为 ROM 的输入地址，ROM 中存有该键的 ASCII 码；CPU 以<strong>中断方式</strong>读入，读入后经一段延迟送中断控制装置清除中断标记，重新启动计数器，为下一个字符的输入做准备。
- <strong>鼠标</strong>：机械式、光电式。
- <strong>触摸屏</strong>。

<strong>输出设备</strong>

- <strong>显示器</strong>三类：字符显示器（内含<strong>字符发生器</strong>，把 ASCII 码变成字符点阵）、图形显示器（显示<strong>主观图像</strong>，即人工或程序用点、线、面画出的图形）、图像显示器（显示照片、视频）。
- <strong>打印机</strong>：按是否打击分——<strong>打击式</strong>（针式，字符点阵经色带与纸打印，有逐字／逐行、窄行／宽行之分，票据打印机仍用它）与<strong>非打击式</strong>（激光、喷墨）。


<figure class="cpd-figure not-content" id="lhw-w4-fig-3">
  <svg viewBox="0 0 700 256" role="img" aria-label="编码键盘矩阵扫描硬件通路与三类显示器对比图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="388" height="228" rx="7"/>
    <rect class="fig-box-subtle" x="416" y="14" width="268" height="228" rx="7"/>
    <line class="fig-line" x1="146" y1="75" x2="130" y2="75" marker-end="url(#lhw-w4-arr-3)"/>
    <line class="fig-line-accent" x1="262" y1="75" x2="278" y2="75" marker-end="url(#lhw-w4-arr-3)"/>
    <line class="fig-line-accent" x1="335" y1="98" x2="335" y2="118" marker-end="url(#lhw-w4-arr-3)"/>
    <line class="fig-line-accent" x1="278" y1="137" x2="262" y2="137" marker-end="url(#lhw-w4-arr-3)"/>
    <line class="fig-line" x1="146" y1="137" x2="132" y2="137" marker-end="url(#lhw-w4-arr-3)"/>
    <path class="fig-line-dashed" d="M 80 118 L 80 106 L 204 106 L 204 98" marker-end="url(#lhw-w4-arr-3)"/>
    <rect class="fig-box" x="28" y="52" width="102" height="46" rx="4"/>
    <rect class="fig-box-blue" x="146" y="52" width="116" height="46" rx="4"/>
    <rect class="fig-box-purple" x="278" y="52" width="114" height="46" rx="4"/>
    <rect class="fig-box-green" x="278" y="118" width="114" height="38" rx="4"/>
    <rect class="fig-box-orange" x="146" y="118" width="116" height="38" rx="4"/>
    <rect class="fig-box" x="28" y="118" width="104" height="38" rx="4"/>
    <rect class="fig-box" x="24" y="170" width="372" height="60" rx="5"/>
    <rect class="fig-box-blue" x="428" y="48" width="244" height="52" rx="4"/>
    <rect class="fig-box-purple" x="428" y="110" width="244" height="52" rx="4"/>
    <rect class="fig-box-green" x="428" y="172" width="244" height="58" rx="4"/>
    <text class="fig-title" x="28" y="35" font-size="12">① 编码键盘硬件扫描—查表—中断读入闭环</text>
    <text class="fig-label" x="79" y="72" font-size="10" text-anchor="middle">按键矩阵 R×C</text>
    <text class="fig-muted" x="79" y="88" font-size="9" text-anchor="middle">检测到某键闭合</text>
    <text class="fig-blue" x="204" y="72" font-size="10" text-anchor="middle">计数器 + 行列译码</text>
    <text class="fig-muted" x="204" y="88" font-size="9" text-anchor="middle">扫中即停计锁存</text>
    <text class="fig-purple" x="335" y="72" font-size="10" text-anchor="middle">编码 ROM 查表</text>
    <text class="fig-muted" x="335" y="88" font-size="9" text-anchor="middle">计数值作地址输入</text>
    <text class="fig-green" x="335" y="134" font-size="10" text-anchor="middle">生成键 ASCII 码</text>
    <text class="fig-muted" x="335" y="148" font-size="9" text-anchor="middle">送接口并发中断</text>
    <text class="fig-orange" x="204" y="134" font-size="10" text-anchor="middle">CPU 中断方式读入</text>
    <text class="fig-muted" x="204" y="148" font-size="9" text-anchor="middle">取走字符存入缓冲</text>
    <text class="fig-label" x="80" y="134" font-size="9.5" text-anchor="middle">延迟清中断标记</text>
    <text class="fig-muted" x="80" y="148" font-size="9" text-anchor="middle">重启计数器扫下键</text>
    <text class="fig-label" x="32" y="189" font-size="9.2">• 编码键盘：硬件自动扫矩阵并经 ROM 转 ASCII 码（速度快、略繁）</text>
    <text class="fig-muted" x="32" y="206" font-size="8.8">• 汉字三码：输入码（拼音/五笔）→ 机内码（存储交换）→ 字形码（点阵）</text>
    <text class="fig-muted" x="32" y="222" font-size="8.8">• 远距通信转换：机内数字信号 → D/A 变模拟传输 → 接收端 A/D 还原数字</text>
    <text class="fig-title" x="428" y="35" font-size="12">② 三类显示器与终端核心考点</text>
    <text class="fig-blue" x="438" y="67" font-size="10.5">字符显示器（含字符发生器 ROM）</text>
    <text class="fig-muted" x="438" y="86" font-size="9.2">显存存 ASCII 码 → 字符发生器译成点阵</text>
    <text class="fig-purple" x="438" y="129" font-size="10.5">图形显示器（显示「主观图像」）</text>
    <text class="fig-muted" x="438" y="148" font-size="9.2">人工或程序用点、线、面几何图元绘制</text>
    <text class="fig-green" x="438" y="190" font-size="10.5">图像显示器 &amp; 远程终端</text>
    <text class="fig-muted" x="438" y="207" font-size="9.2">• 图像：客观照片/视频像素阵列</text>
    <text class="fig-muted" x="438" y="222" font-size="9.2">• 终端：键+显+控+缓，不能脱离主机独用</text>
  </svg>
  <figcaption>图 5-3　编码键盘矩阵扫描硬件闭环（计数器停计 → ROM 查 ASCII → 中断读入）与三类显示器原理辨析</figcaption>
</figure>

<strong>其他</strong>

- <strong>A/D、D/A</strong>：机内是数字信号，控制设备或远距离传输要先 D/A 变成模拟信号，接收端再 A/D 变回数字信号。
- <strong>终端：显示器＋键盘＋控制机构＋缓存，类似瘦客户机，能完成显示控制、存储、键盘管理与通信控制，不能单独使用</strong>，需经线路连远程主机。
- <strong>汉字处理</strong>：输入码、内码、字形码。
- <strong>多媒体</strong>：关键技术包括数据压缩解压缩与编码、专用芯片、语音识别、图像识别等。

> <strong>注意</strong>：磁盘既属于存储系统（第 4 章），又属于外设（本章），考试中按题目语境判断。

### 5.3 I/O 接口

接口是设备与系统总线之间的缓冲、转换环节，<strong>结构由功能决定</strong>。基本功能可归为四项：

| 功能 | 实现部件 | 要点 |
|---|---|---|
| 选址 | 设备选择电路 | 地址线上的设备地址与接口地址比较，相符则 <strong>SEL 有效</strong>；只有 SEL 有效接口才开始工作 |
| 数据缓冲 | 数据缓冲寄存器 DBR | 暂存一个字节／字，匹配 CPU 与设备的速度差 |
| 状态标记 | 设备状态触发器 D | D=1 表示数据已就绪（设备空闲） |
| 命令缓冲与译码 | 命令寄存器 + 命令译码器 | 接收并译码 CPU 的控制命令；命令与 SEL 同时有效才置位工作触发器 B |

采用<strong>程序中断方式时还要增加：中断请求触发器 INTR</strong>、<strong>中断屏蔽触发器 MASK</strong>、<strong>中断排队器</strong>、<strong>中断向量地址形成部件</strong>。

<figure class="cpd-figure not-content" id="lhw-w4-fig-4">
  <svg viewBox="0 0 700 264" role="img" aria-label="IO端口统一编址与独立编址对比示意图">
    <rect class="fig-box-subtle" x="16" y="14" width="328" height="236" rx="7"/>
    <rect class="fig-box-subtle" x="356" y="14" width="328" height="236" rx="7"/>
    <rect class="fig-box-blue" x="28" y="50" width="112" height="98" rx="4"/>
    <rect class="fig-box-orange" x="28" y="150" width="112" height="46" rx="4"/>
    <rect class="fig-box" x="150" y="50" width="182" height="146" rx="4"/>
    <rect class="fig-box-accent" x="28" y="206" width="304" height="32" rx="4"/>
    <rect class="fig-box-blue" x="368" y="50" width="76" height="146" rx="4"/>
    <rect class="fig-box-purple" x="452" y="84" width="70" height="112" rx="4"/>
    <rect class="fig-box" x="530" y="50" width="142" height="146" rx="4"/>
    <rect class="fig-box-accent" x="368" y="206" width="304" height="32" rx="4"/>
    <text class="fig-title" x="28" y="35" font-size="12">① 统一编址（存储器映射 I/O）</text>
    <text class="fig-blue" x="84" y="88" font-size="10" text-anchor="middle">主存单元空间</text>
    <text class="fig-muted" x="84" y="106" font-size="9" text-anchor="middle">0000H ~ EFFFH</text>
    <text class="fig-muted" x="84" y="124" font-size="8.5" text-anchor="middle">（占大部分地址）</text>
    <text class="fig-orange" x="84" y="170" font-size="10" text-anchor="middle">I/O 端口空间</text>
    <text class="fig-muted" x="84" y="186" font-size="9" text-anchor="middle">F000H ~ FFFFH</text>
    <text class="fig-label" x="160" y="70" font-size="9.8">• 区分手段：地址码范围</text>
    <text class="fig-muted" x="160" y="88" font-size="9.2">无专用 I/O 指令，直接复用</text>
    <text class="fig-muted" x="160" y="104" font-size="9.2">全部访存指令（LOAD/STORE）</text>
    <text class="fig-green" x="160" y="128" font-size="9.5">✓ 优点：寻址方式丰富灵活</text>
    <text class="fig-red" x="160" y="148" font-size="9.5">✗ 缺点：占用主存地址容量，</text>
    <text class="fig-muted" x="160" y="164" font-size="9.2">地址译码器较复杂（全地址译码）</text>
    <text class="fig-muted" x="160" y="184" font-size="9.2">代表架构：MIPS / RISC-V / ARM</text>
    <text class="fig-accent" x="180" y="226" font-size="10" text-anchor="middle">把接口中的 DBR/状态/命令寄存器视作特殊主存单元</text>
    <text class="fig-title" x="368" y="35" font-size="12">② 独立编址（I/O 端口专用映射）</text>
    <text class="fig-blue" x="406" y="104" font-size="9.8" text-anchor="middle">主存全空间</text>
    <text class="fig-muted" x="406" y="122" font-size="8.8" text-anchor="middle">0000~FFFFH</text>
    <text class="fig-muted" x="406" y="140" font-size="8.5" text-anchor="middle">（容量无损）</text>
    <text class="fig-purple" x="487" y="124" font-size="9.8" text-anchor="middle">独立端口</text>
    <text class="fig-muted" x="487" y="142" font-size="8.8" text-anchor="middle">00H~FFH</text>
    <text class="fig-muted" x="487" y="160" font-size="8.5" text-anchor="middle">（地址短）</text>
    <text class="fig-label" x="538" y="70" font-size="9.6">• 区分：专用指令+控制线</text>
    <text class="fig-muted" x="538" y="88" font-size="9">必须设 IN / OUT 指令及</text>
    <text class="fig-muted" x="538" y="104" font-size="9">M/IO 控制引脚区分空间</text>
    <text class="fig-green" x="538" y="128" font-size="9.2">✓ 不占主存空间，译码快</text>
    <text class="fig-red" x="538" y="148" font-size="9.2">✗ I/O 指令少、寻址单一</text>
    <text class="fig-muted" x="538" y="184" font-size="9">代表架构：Intel x86</text>
    <text class="fig-accent" x="520" y="226" font-size="10" text-anchor="middle">主存地址与端口地址可重叠，靠控制总线信号严格区分</text>
  </svg>
  <figcaption>图 5-4　I/O 端口两种编址方式对比：统一编址（靠地址区分，复用访存指令）vs 独立编址（靠专用指令与控制线区分）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w4b-fig-1">
  <svg viewBox="0 0 700 388" role="img" aria-label="程序中断方式 I/O 接口内部结构与外部连接框图">
    <defs>
      <marker id="lhw-w4b-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="120" y="38" width="370" height="296" rx="7"/>
    <line class="fig-line" x1="34" y1="42" x2="34" y2="330"/>
    <line class="fig-line" x1="66" y1="42" x2="66" y2="330"/>
    <line class="fig-line" x1="98" y1="42" x2="98" y2="330"/>
    <line class="fig-line" x1="66" y1="80" x2="136" y2="80" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line" x1="98" y1="128" x2="136" y2="128" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line-accent" x1="34" y1="176" x2="136" y2="176" marker-start="url(#lhw-w4b-arr-1)" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line" x1="258" y1="80" x2="546" y2="80" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line" x1="258" y1="128" x2="546" y2="128" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line-accent" x1="258" y1="176" x2="546" y2="176" marker-start="url(#lhw-w4b-arr-1)" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line" x1="546" y1="204" x2="258" y2="204" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line" x1="258" y1="218" x2="272" y2="218"/>
    <line class="fig-line" x1="272" y1="218" x2="272" y2="236"/>
    <line class="fig-line" x1="258" y1="256" x2="272" y2="256"/>
    <line class="fig-line" x1="272" y1="256" x2="272" y2="252"/>
    <line class="fig-line" x1="304" y1="244" x2="310" y2="244"/>
    <line class="fig-line" x1="327" y1="244" x2="334" y2="244"/>
    <line class="fig-line" x1="406" y1="260" x2="406" y2="282" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line-accent" x1="478" y1="299" x2="520" y2="299" marker-end="url(#lhw-w4b-arr-1)"/>
    <line class="fig-line" x1="596" y1="140" x2="596" y2="170" marker-end="url(#lhw-w4b-arr-1)"/>
    <rect class="fig-box" x="136" y="64" width="122" height="32" rx="4"/>
    <rect class="fig-box" x="136" y="112" width="122" height="32" rx="4"/>
    <rect class="fig-box-blue" x="136" y="160" width="122" height="32" rx="4"/>
    <rect class="fig-box-orange" x="136" y="196" width="122" height="32" rx="4"/>
    <rect class="fig-box-purple" x="136" y="240" width="122" height="32" rx="4"/>
    <rect x="272" y="230" width="24" height="28" rx="2" fill="var(--sl-color-bg)" stroke="var(--sl-color-text)" stroke-width="1.2"/>
    <circle cx="300" cy="244" r="3.5" fill="var(--sl-color-bg)" stroke="var(--sl-color-text)" stroke-width="1.2"/>
    <polygon points="310,237 310,251 320,244" fill="var(--sl-color-bg)" stroke="var(--sl-color-text)" stroke-width="1.2"/>
    <circle cx="323.5" cy="244" r="3" fill="var(--sl-color-bg)" stroke="var(--sl-color-text)" stroke-width="1.2"/>
    <rect class="fig-box-orange" x="334" y="228" width="144" height="32" rx="4"/>
    <rect class="fig-box-blue" x="334" y="282" width="144" height="36" rx="4"/>
    <rect class="fig-box" x="546" y="60" width="104" height="80" rx="4"/>
    <rect class="fig-box" x="546" y="170" width="104" height="130" rx="4"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">I/O 接口框图（程序中断方式）</text>
    <text class="fig-muted" x="34" y="36" font-size="10.5" text-anchor="middle">数据</text>
    <text class="fig-muted" x="66" y="36" font-size="10.5" text-anchor="middle">地址</text>
    <text class="fig-muted" x="98" y="36" font-size="10.5" text-anchor="middle">控制</text>
    <text class="fig-muted" x="130" y="54" font-size="10.5">I/O 接口</text>
    <text class="fig-label" x="197" y="84" font-size="11" text-anchor="middle">设备选择电路</text>
    <text class="fig-label" x="197" y="132" font-size="11" text-anchor="middle">命令寄存器/译码</text>
    <text class="fig-blue" x="197" y="180" font-size="11" text-anchor="middle">数据缓冲 DBR</text>
    <text class="fig-orange" x="197" y="216" font-size="11" text-anchor="middle">状态触发器 D</text>
    <text class="fig-purple" x="197" y="260" font-size="11" text-anchor="middle">屏蔽触发器 MASK</text>
    <text class="fig-label" x="284" y="248" font-size="11" text-anchor="middle">&amp;</text>
    <text class="fig-orange" x="406" y="248" font-size="11" text-anchor="middle">中断请求 INTR</text>
    <text class="fig-blue" x="406" y="297" font-size="10.5" text-anchor="middle">中断排队器</text>
    <text class="fig-blue" x="406" y="311" font-size="10.5" text-anchor="middle">向量地址形成</text>
    <text class="fig-accent" x="514" y="288" font-size="10" text-anchor="middle">→ CPU</text>
    <text class="fig-label" x="598" y="104" font-size="11" text-anchor="middle">设备控制器</text>
    <text class="fig-label" x="598" y="228" font-size="11.5" text-anchor="middle">外设</text>
    <text class="fig-muted" x="598" y="248" font-size="10" text-anchor="middle">机械/电磁/光</text>
    <text class="fig-muted" x="524" y="74" font-size="10" text-anchor="middle">SEL</text>
    <text class="fig-muted" x="524" y="122" font-size="10" text-anchor="middle">命令</text>
    <text class="fig-blue" x="524" y="170" font-size="10" text-anchor="middle">数据</text>
    <text class="fig-muted" x="524" y="198" font-size="10" text-anchor="middle">状态</text>
    <text class="fig-label" x="120" y="356" font-size="11">• D=1 且 MASK 开放（Q′=1）→ 与非门、非门 → INTR 具备置位条件</text>
    <text class="fig-muted" x="120" y="374" font-size="10.5">• 指令执行阶段结束时，CPU 发中断查询信号将其打入，并启动排队器</text>
  </svg>
  <figcaption>图 5-5　程序中断方式 I/O 接口框图（选址、命令、数据缓冲 DBR、状态 D、屏蔽 MASK 与中断排队逻辑）</figcaption>
</figure>

> <strong>注意</strong>：命令与 SEL <strong>同时有效</strong>才置位工作触发器 B（B=1 设备忙、D=0 数据未就绪）；设备工作结束把数据送入 DBR，同时 D=1、B=0。这一“启动—忙—就绪”的翻转是中断方式接口的工作核心。

### 5.4 程序查询方式

最早的方式，CPU 与 I/O <strong>完全串行</strong>。以输入为例，流程是：

1. CPU 执行 I/O 指令，向指定设备发<strong>读命令</strong>（同时给出设备地址）。
2. 读接口<strong>状态标志</strong>（状态触发器 D），判断数据是否准备好。
3. 未就绪 → 用跳转指令<strong>反复读状态</strong>，CPU 原地踏步；状态出错 → 转出错处理。
4. 就绪 → 从 DBR 读一个字（字节）到 CPU 的某个寄存器。
5. CPU 用存数指令把该寄存器内容写入主存指定单元。
6. 判断整批数据是否传完：未完转 1，继续发读命令；完成则结束。

<figure class="cpd-figure not-content" id="lhw-w4b-fig-2">
  <svg viewBox="0 0 700 412" role="img" aria-label="程序查询方式以输入为例的流程图">
    <defs>
      <marker id="lhw-w4b-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>
    <line class="fig-line" x1="160" y1="72" x2="160" y2="88" marker-end="url(#lhw-w4b-arr-2)"/>
    <line class="fig-line" x1="160" y1="120" x2="160" y2="134" marker-end="url(#lhw-w4b-arr-2)"/>
    <path class="fig-line-dashed" d="M 60 164 L 28 164 L 28 104 L 58 104" stroke="var(--sl-color-red)" fill="none" marker-end="url(#lhw-w4b-arr-2)"/>
    <line class="fig-line" x1="160" y1="194" x2="160" y2="210" marker-end="url(#lhw-w4b-arr-2)"/>
    <line class="fig-line" x1="160" y1="242" x2="160" y2="256" marker-end="url(#lhw-w4b-arr-2)"/>
    <line class="fig-line" x1="160" y1="288" x2="160" y2="302" marker-end="url(#lhw-w4b-arr-2)"/>
    <path class="fig-line-dashed" d="M 260 332 L 320 332 L 320 56 L 262 56" stroke="var(--sl-color-red)" fill="none" marker-end="url(#lhw-w4b-arr-2)"/>
    <line class="fig-line" x1="160" y1="362" x2="160" y2="376" marker-end="url(#lhw-w4b-arr-2)"/>
    <rect class="fig-box-blue" x="60" y="40" width="200" height="32" rx="4"/>
    <rect class="fig-box" x="60" y="88" width="200" height="32" rx="4"/>
    <polygon class="fig-box-orange" points="60,164 160,134 260,164 160,194"/>
    <rect class="fig-box" x="60" y="210" width="200" height="32" rx="4"/>
    <rect class="fig-box" x="60" y="256" width="200" height="32" rx="4"/>
    <polygon class="fig-box-orange" points="60,332 160,302 260,332 160,362"/>
    <rect class="fig-box-green" x="90" y="376" width="140" height="28" rx="14"/>
    <rect class="fig-box-subtle" x="356" y="140" width="320" height="156" rx="6"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">程序查询方式（以输入为例）</text>
    <text class="fig-blue" x="160" y="60" font-size="11.5" text-anchor="middle">CPU 发读命令</text>
    <text class="fig-label" x="160" y="108" font-size="11.5" text-anchor="middle">读接口状态标志 D</text>
    <text class="fig-orange" x="160" y="168" font-size="11.5" text-anchor="middle">D = 1？</text>
    <text class="fig-red" x="42" y="156" font-size="11" text-anchor="middle">否</text>
    <text class="fig-green" x="172" y="205" font-size="10.5">是</text>
    <text class="fig-label" x="160" y="230" font-size="11.5" text-anchor="middle">读一字到 CPU 寄存器</text>
    <text class="fig-label" x="160" y="276" font-size="11.5" text-anchor="middle">写入主存单元</text>
    <text class="fig-orange" x="160" y="336" font-size="11.5" text-anchor="middle">整批传完？</text>
    <text class="fig-red" x="276" y="324" font-size="11" text-anchor="middle">否</text>
    <text class="fig-green" x="172" y="371" font-size="10.5">是</text>
    <text class="fig-green" x="160" y="394" font-size="11.5" text-anchor="middle">结束</text>
    <text class="fig-title" x="374" y="166" font-size="11.5">核心特征与瓶颈</text>
    <text class="fig-red" x="374" y="192" font-size="11">• 虚线 = 反复查询，CPU 原地踏步</text>
    <text class="fig-muted" x="374" y="214" font-size="10.5">• 设备准备慢、CPU 快，等待时间白白消耗</text>
    <text class="fig-muted" x="374" y="236" font-size="10.5">• 数据通路：设备 → DBR → CPU 寄存器 → 主存</text>
    <text class="fig-muted" x="374" y="258" font-size="10.5">• CPU 与 I/O 完全串行，硬件最简但效率最低</text>
    <text class="fig-muted" x="374" y="280" font-size="10.5">• 状态出错时需跳转至专门的出错处理分支</text>
  </svg>
  <figcaption>图 5-6　程序查询方式工作流程（CPU 踏步等待外设就绪，数据经 CPU 寄存器中转）</figcaption>
</figure>

<strong>特点</strong>

- 数据通路：设备 → DBR → <strong>CPU 寄存器 → 主存（输出时反向），即“CPU 一手托两家”，一边接口一边主存，任何数据都要经 CPU 中转</strong>。
- 设备准备期 CPU 一直查询等待，CPU 与 I/O 串行，<strong>CPU 效率最低</strong>，但硬件最简单、控制最直接。

| 优点 | 缺点 |
|---|---|
| 接口硬件简单，无需中断机构与 DMA 控制器；程序流程直观 | CPU 与 I/O 完全串行，外设准备期间 CPU 空转，效率极低 |

> <strong>★考点</strong>：查询方式下 CPU 与 I/O 串行；数据必须经 CPU 中转；查询过程由程序（跳转指令）实现。

### 5.5 程序中断方式（一）：中断概念与响应

<strong>中断</strong>：CPU 执行程序过程中出现意外事件或特殊事件，CPU 中断当前程序的执行，转去执行<strong>中断服务程序</strong>处理该事件，处理完再返回<strong>程序断点</strong>继续执行原程序。能引发中断的因素（CPU 内外、主机内外）称为<strong>中断源</strong>。

以打印机输出为例：CPU 用打印命令启动打印机后继续执行原程序；打印机完成打印准备后向 CPU 发<strong>中断请求；CPU 响应，执行中断服务程序把数据传送给打印机；传送结束后中断返回，CPU 继续执行原程序，打印机同时打印；缓冲区空了再发下一次请求。宏观上两者并行</strong>：设备准备期 CPU 一直在执行主程序。

<strong>响应条件</strong>

1. CPU 内<strong>中断允许触发器 EINT=1</strong>（开中断指令置 1、关中断指令或硬件自动复位清 0）。
2. 接口未被屏蔽：<strong>MASK 的 Q=0、Q̅=1</strong>。
3. 该接口有中断请求（INTR=1）。

<strong>响应时间</strong>：外设提出请求的时刻是随机的，CPU 只在<strong>每条指令执行阶段结束时</strong>发中断查询信号，此时才响应。

<strong>I/O 中断处理过程（以输入为例）</strong>

1. CPU 执行输入指令，地址线上给出设备地址。
2. 接口的<strong>设备选择电路</strong>比较地址，相符则 <strong>SEL 有效</strong>，接口开始工作。
3. CPU 送来的启动/控制命令经译码，与 SEL 同时有效 → 置 <strong>B=1（设备忙）、D=0（数据未就绪）</strong>，启动设备。
4. 设备工作结束，把数据送入 <strong>DBR</strong>，同时 <strong>D=1、B=0</strong>。
5. 若未被屏蔽（MASK 的 Q̅=1），D 与 Q̅ 均为 1 → 经与非门、非门使 INTR 具备置位条件。
6. 指令执行阶段结束，CPU 发<strong>中断查询信号</strong>，把 INTR 置 1，并启动<strong>排队器</strong>。
7. 排队器输出中只有优先级最高的那一位为 1；CPU 发<strong>中断响应</strong>信号，由<strong>向量地址形成部件</strong>产生向量地址，经数据线送 CPU。
8. CPU 据此取得中断服务程序入口地址（或一条跳转指令），转去执行服务程序，把数据取走。

<figure class="cpd-figure not-content" id="lhw-w4b-fig-3">
  <svg viewBox="0 0 700 380" role="img" aria-label="中断处理全流程：主程序到中断周期再到中断服务程序">
    <defs>
      <marker id="lhw-w4b-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>
    <line class="fig-line" x1="310" y1="72" x2="310" y2="88" marker-end="url(#lhw-w4b-arr-3)"/>
    <path class="fig-line-dashed" d="M 190 120 L 160 120 L 160 74" marker-end="url(#lhw-w4b-arr-3)"/>
    <line class="fig-line" x1="310" y1="150" x2="310" y2="166" marker-end="url(#lhw-w4b-arr-3)"/>
    <rect class="fig-box-dashed" x="60" y="166" width="520" height="68" rx="6"/>
    <line class="fig-line" x1="210" y1="206" x2="224" y2="206" marker-end="url(#lhw-w4b-arr-3)"/>
    <line class="fig-line" x1="364" y1="206" x2="378" y2="206" marker-end="url(#lhw-w4b-arr-3)"/>
    <line class="fig-line" x1="310" y1="234" x2="310" y2="250" marker-end="url(#lhw-w4b-arr-3)"/>
    <rect class="fig-box-dashed" x="60" y="250" width="520" height="86" rx="6"/>
    <line class="fig-line" x1="154" y1="290" x2="166" y2="290" marker-end="url(#lhw-w4b-arr-3)"/>
    <line class="fig-line" x1="250" y1="290" x2="262" y2="290" marker-end="url(#lhw-w4b-arr-3)"/>
    <line class="fig-line" x1="346" y1="290" x2="358" y2="290" marker-end="url(#lhw-w4b-arr-3)"/>
    <line class="fig-line" x1="442" y1="290" x2="454" y2="290" marker-end="url(#lhw-w4b-arr-3)"/>
    <path class="fig-line-accent" d="M 538 290 L 636 290 L 636 54 L 602 54" fill="none" marker-end="url(#lhw-w4b-arr-3)"/>
    <rect class="fig-box-blue" x="20" y="36" width="580" height="36" rx="4"/>
    <polygon class="fig-box-orange" points="190,120 310,90 430,120 310,150"/>
    <rect class="fig-box" x="74" y="188" width="136" height="36" rx="4"/>
    <rect class="fig-box" x="224" y="188" width="140" height="36" rx="4"/>
    <rect class="fig-box" x="378" y="188" width="188" height="36" rx="4"/>
    <rect class="fig-box" x="70" y="274" width="84" height="32" rx="4"/>
    <rect class="fig-box" x="166" y="274" width="84" height="32" rx="4"/>
    <rect class="fig-box" x="262" y="274" width="84" height="32" rx="4"/>
    <rect class="fig-box-blue" x="358" y="274" width="84" height="32" rx="4"/>
    <rect class="fig-box-green" x="454" y="274" width="84" height="32" rx="4"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">中断处理流程：主程序 → 中断周期 → 中断服务程序</text>
    <text class="fig-blue" x="310" y="58" font-size="11.5" text-anchor="middle">主程序：设备准备期间 CPU 继续执行（宏观并行）</text>
    <text class="fig-orange" x="310" y="124" font-size="11" text-anchor="middle">有请求且 EINT=1？</text>
    <text class="fig-muted" x="174" y="112" font-size="10.5" text-anchor="middle">否</text>
    <text class="fig-green" x="322" y="161" font-size="10.5">是</text>
    <text class="fig-muted" x="70" y="181" font-size="10.5">中断隐指令（硬件自动完成，非一条指令）</text>
    <text class="fig-label" x="142" y="210" font-size="11" text-anchor="middle">关中断</text>
    <text class="fig-label" x="294" y="210" font-size="11" text-anchor="middle">保护断点 PC/PSR</text>
    <text class="fig-label" x="472" y="210" font-size="11" text-anchor="middle">形成服务程序入口地址</text>
    <text class="fig-muted" x="70" y="266" font-size="10.5">中断服务程序（软件）</text>
    <text class="fig-label" x="112" y="294" font-size="11" text-anchor="middle">保护现场</text>
    <text class="fig-label" x="208" y="294" font-size="11" text-anchor="middle">设备服务</text>
    <text class="fig-label" x="304" y="294" font-size="11" text-anchor="middle">恢复现场</text>
    <text class="fig-blue" x="400" y="294" font-size="11" text-anchor="middle">开中断</text>
    <text class="fig-green" x="496" y="294" font-size="11" text-anchor="middle">中断返回</text>
    <text class="fig-blue" x="70" y="326" font-size="10.5">多重中断：开中断提前到“保护现场”之后</text>
    <text class="fig-accent" x="590" y="356" font-size="10.5" text-anchor="middle">中断返回，继续原程序</text>
  </svg>
  <figcaption>图 5-7　程序中断处理全流程（硬件中断隐指令三件事与软件中断服务程序四阶段）</figcaption>
</figure>

> <strong>★考点</strong>：响应条件（EINT、MASK、请求）与响应时间（<strong>一条指令执行阶段结束时</strong>）；中断隐指令的三件事——<strong>关中断、保护断点、形成入口地址</strong>；中断服务程序四段——保护现场、设备服务、恢复现场、中断返回。

### 5.5 程序中断方式（二）：排队、向量与多重中断

<strong>中断排队</strong>：同一时刻可能有多个设备请求，需由排队器选出优先级最高者。硬件排队有两种实现——集中放在 CPU 内，或分散在各接口中串接成<strong>链式（菊花链）排队器</strong>；软件排队则是按优先级表从高到低逐个查询。

链式排队器每级由一个<strong>非门 + 与非门</strong>构成，各级 $\overline{\text{INTP}}$ 串接：左侧设备优先级最高。若某级有请求（$\overline{\text{INTR}}=0$），则<strong>其后各级的 $\overline{\text{INTP}}$ 全部变为 0</strong>，其前各级保持 1，于是只有该级的输出为 1，被编码后送出向量地址。

<strong>中断向量</strong>：CPU 响应后，排队器唯一有效的那一级经<strong>设备编码器</strong>产生<strong>向量地址</strong>，经数据线送 CPU，CPU 用它取出中断服务程序入口地址（或一条跳转指令），从而转入服务程序。

<figure class="cpd-figure not-content" id="lhw-w4b-fig-8">
  <svg viewBox="0 0 700 220" role="img" aria-label="链式（菊花链）中断排队器电路与向量地址编码原理图">
    <defs>
      <marker id="lhw-w4b-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="34" width="668" height="172" rx="7"/>
    <line class="fig-line" x1="24" y1="86" x2="64" y2="86" marker-end="url(#lhw-w4b-arr-8)"/>
    <line class="fig-line-accent" x1="180" y1="86" x2="222" y2="86" marker-end="url(#lhw-w4b-arr-8)"/>
    <line class="fig-line-accent" x1="338" y1="86" x2="380" y2="86" marker-end="url(#lhw-w4b-arr-8)"/>
    <line class="fig-line" x1="123" y1="110" x2="123" y2="146" marker-end="url(#lhw-w4b-arr-8)"/>
    <line class="fig-line-accent" x1="281" y1="110" x2="281" y2="146" marker-end="url(#lhw-w4b-arr-8)"/>
    <line class="fig-line" x1="439" y1="110" x2="439" y2="146" marker-end="url(#lhw-w4b-arr-8)"/>
    <line class="fig-line-accent" x1="496" y1="165" x2="522" y2="165" marker-end="url(#lhw-w4b-arr-8)"/>
    <rect class="fig-box" x="66" y="62" width="114" height="48" rx="4"/>
    <rect class="fig-box-orange" x="224" y="62" width="114" height="48" rx="4"/>
    <rect class="fig-box" x="382" y="62" width="114" height="48" rx="4"/>
    <rect class="fig-box-blue" x="66" y="146" width="430" height="38" rx="4"/>
    <rect class="fig-box-green" x="524" y="146" width="150" height="38" rx="4"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">链式（菊花链）中断排队器与向量地址编码</text>
    <text class="fig-muted" x="44" y="78" font-size="9.5" text-anchor="middle">INTP′=1</text>
    <text class="fig-label" x="123" y="83" font-size="10.2" text-anchor="middle">第 1 级（最高）</text>
    <text class="fig-muted" x="123" y="99" font-size="8.8" text-anchor="middle">INTR₁′ = 1（无请求）</text>
    <text class="fig-muted" x="202" y="78" font-size="9.5" text-anchor="middle">INTP′=1</text>
    <text class="fig-orange" x="281" y="83" font-size="10.2" text-anchor="middle">第 2 级（有请求）</text>
    <text class="fig-red" x="281" y="99" font-size="8.8" text-anchor="middle">INTR₂′ = 0 → 输出 1</text>
    <text class="fig-red" x="360" y="78" font-size="9.5" text-anchor="middle">INTP′=0</text>
    <text class="fig-label" x="439" y="83" font-size="10.2" text-anchor="middle">第 3 级（被封锁）</text>
    <text class="fig-muted" x="439" y="99" font-size="8.8" text-anchor="middle">即使有请求也输出 0</text>
    <text class="fig-muted" x="148" y="132" font-size="9.5">0</text>
    <text class="fig-orange" x="306" y="132" font-size="9.5">1（唯一有效）</text>
    <text class="fig-muted" x="464" y="132" font-size="9.5">0</text>
    <text class="fig-blue" x="281" y="169" font-size="10.8" text-anchor="middle">设备编码器（将唯一为 1 的排队输出编为向量地址）</text>
    <text class="fig-green" x="599" y="169" font-size="9.8" text-anchor="middle">向量地址 → 数据线 → CPU</text>
  </svg>
  <figcaption>图 5-8　链式（菊花链）中断排队器：左侧优先级最高，有请求时封锁后续各级 $\overline{\text{INTP}}=0$，仅唯一有效级经编码器送出向量地址</figcaption>
</figure>

从指令周期看，一条指令执行结束后若响应中断，就进入<strong>中断周期</strong>；该周期完成三件事——<strong>关中断、保护断点、形成中断服务程序入口地址</strong>，全部由硬件按时序自动完成，故称<strong>中断隐指令</strong>（它并不是一条指令）。

<strong>中断服务程序的流程</strong>

| 阶段 | 内容 | 实现 |
|---|---|---|
| 保护现场 | 保存<strong>程序断点</strong>（PC 值 + 程序状态）与通用/体系结构寄存器 | 断点由<strong>中断隐指令</strong>（硬件）保存；寄存器一般用压栈指令保存，也可存到指定内存单元或转存到其他寄存器 |
| 设备服务 | 与设备相关的具体输入输出操作 | 各设备的中断服务程序不同 |
| 恢复现场 | 恢复寄存器内容 | 与保护方式对应（出栈或取数指令） |
| 中断返回 | 返回断点 | 中断返回指令 |

> <strong>注意</strong>：保护现场与恢复现场要执行若干条指令。若一次只传一个字节，保护+恢复可能要十几到几十条指令，这笔开销常常超过数据传送本身——这是中断方式“解放 CPU 不彻底”的根源。

<strong>单重中断与多重中断</strong>

- <strong>单重中断</strong>：执行中断服务程序期间，任何新请求（不论优先级高低）都不能打断，EINT 全程为 0，直到最后才开中断。
- <strong>多重中断（中断嵌套）</strong>：允许级别更高的中断源打断当前的中断服务程序。做法是把<strong>开中断提前到保护现场之后</strong>，使设备服务期间可以响应更高级请求。

<figure class="cpd-figure not-content" id="lhw-w4b-fig-4">
  <svg viewBox="0 0 700 382" role="img" aria-label="单重中断与多重中断的服务程序流程对比图">
    <defs>
      <marker id="lhw-w4b-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="32" width="316" height="334" rx="7"/>
    <rect class="fig-box-subtle" x="352" y="32" width="332" height="334" rx="7"/>
    <line class="fig-line" x1="180" y1="98" x2="180" y2="112" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="180" y1="142" x2="180" y2="156" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="180" y1="186" x2="180" y2="200" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="180" y1="230" x2="180" y2="244" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="520" y1="98" x2="520" y2="112" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="520" y1="142" x2="520" y2="156" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="520" y1="186" x2="520" y2="200" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="520" y1="230" x2="520" y2="244" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="520" y1="274" x2="520" y2="288" marker-end="url(#lhw-w4b-arr-4)"/>
    <line class="fig-line" x1="520" y1="318" x2="520" y2="332" marker-end="url(#lhw-w4b-arr-4)"/>
    <rect class="fig-box-red" x="44" y="68" width="10" height="118" rx="2"/>
    <rect class="fig-box-green" x="44" y="200" width="10" height="74" rx="2"/>
    <rect class="fig-box" x="70" y="68" width="220" height="30" rx="4"/>
    <rect class="fig-box" x="70" y="112" width="220" height="30" rx="4"/>
    <rect class="fig-box" x="70" y="156" width="220" height="30" rx="4"/>
    <rect class="fig-box-blue" x="70" y="200" width="220" height="30" rx="4"/>
    <rect class="fig-box-green" x="70" y="244" width="220" height="30" rx="4"/>
    <rect class="fig-box-red" x="382" y="68" width="10" height="44" rx="2"/>
    <rect class="fig-box-green" x="382" y="112" width="10" height="88" rx="2"/>
    <rect class="fig-box-red" x="382" y="200" width="10" height="88" rx="2"/>
    <rect class="fig-box-green" x="382" y="288" width="10" height="74" rx="2"/>
    <rect class="fig-box" x="410" y="68" width="220" height="30" rx="4"/>
    <rect class="fig-box-blue" x="410" y="112" width="220" height="30" rx="4"/>
    <rect class="fig-box" x="410" y="156" width="220" height="30" rx="4"/>
    <rect class="fig-box-red" x="410" y="200" width="220" height="30" rx="4"/>
    <rect class="fig-box" x="410" y="244" width="220" height="30" rx="4"/>
    <rect class="fig-box-blue" x="410" y="288" width="220" height="30" rx="4"/>
    <rect class="fig-box-green" x="410" y="332" width="220" height="30" rx="4"/>
    <rect class="fig-box-red" x="42" y="334" width="10" height="10" rx="2"/>
    <rect class="fig-box-green" x="176" y="334" width="10" height="10" rx="2"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">单重中断与多重中断的服务程序流程</text>
    <text class="fig-title" x="32" y="54" font-size="11.5">单重中断（不可嵌套）</text>
    <text class="fig-label" x="180" y="87" font-size="11" text-anchor="middle">保护现场</text>
    <text class="fig-label" x="180" y="131" font-size="11" text-anchor="middle">设备服务</text>
    <text class="fig-label" x="180" y="175" font-size="11" text-anchor="middle">恢复现场</text>
    <text class="fig-blue" x="180" y="219" font-size="11" text-anchor="middle">开中断</text>
    <text class="fig-green" x="180" y="263" font-size="11" text-anchor="middle">中断返回</text>
    <text class="fig-red" x="174" y="302" font-size="10.2" text-anchor="middle">设备服务期间 EINT=0，不响应新请求</text>
    <text class="fig-muted" x="58" y="343" font-size="9.5">EINT=0（关中断）</text>
    <text class="fig-muted" x="192" y="343" font-size="9.5">EINT=1（开中断）</text>
    <text class="fig-title" x="368" y="54" font-size="11.5">多重中断（可嵌套）</text>
    <text class="fig-label" x="520" y="87" font-size="11" text-anchor="middle">保护现场</text>
    <text class="fig-blue" x="520" y="131" font-size="11" text-anchor="middle">开中断（允许高级嵌套）</text>
    <text class="fig-label" x="520" y="175" font-size="11" text-anchor="middle">设备服务</text>
    <text class="fig-red" x="520" y="219" font-size="11" text-anchor="middle">关中断</text>
    <text class="fig-label" x="520" y="263" font-size="11" text-anchor="middle">恢复现场</text>
    <text class="fig-blue" x="520" y="307" font-size="11" text-anchor="middle">开中断</text>
    <text class="fig-green" x="520" y="351" font-size="11" text-anchor="middle">中断返回</text>
  </svg>
  <figcaption>图 5-9　单重中断（全程关中断）与多重中断（保护现场后提前开中断）服务程序流程对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w4b-fig-9">
  <svg viewBox="0 0 700 262" role="img" aria-label="多重中断嵌套时空执行轨迹与堆栈变化示意图">
    <defs>
      <marker id="lhw-w4b-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="34" width="668" height="214" rx="7"/>
    <line class="fig-line-dashed" x1="170" y1="140" x2="170" y2="196"/>
    <line class="fig-line-dashed" x1="310" y1="190" x2="310" y2="196"/>
    <line class="fig-line-dashed" x1="430" y1="190" x2="430" y2="196"/>
    <line class="fig-line-dashed" x1="550" y1="140" x2="550" y2="196"/>
    <line class="fig-line-accent" x1="170" y1="82" x2="170" y2="110" marker-end="url(#lhw-w4b-arr-9)"/>
    <line class="fig-line-accent" x1="310" y1="140" x2="310" y2="160" marker-end="url(#lhw-w4b-arr-9)"/>
    <line class="fig-line-accent" x1="430" y1="162" x2="430" y2="142" marker-end="url(#lhw-w4b-arr-9)"/>
    <line class="fig-line-accent" x1="550" y1="112" x2="550" y2="84" marker-end="url(#lhw-w4b-arr-9)"/>
    <rect class="fig-box" x="34" y="54" width="136" height="28" rx="4"/>
    <rect class="fig-box" x="550" y="54" width="118" height="28" rx="4"/>
    <rect class="fig-box-blue" x="170" y="112" width="140" height="28" rx="4"/>
    <rect class="fig-box-blue" x="430" y="112" width="120" height="28" rx="4"/>
    <rect class="fig-box-orange" x="310" y="162" width="120" height="28" rx="4"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">多重中断（中断嵌套）时空轨迹与堆栈变化</text>
    <text class="fig-label" x="102" y="72" font-size="11" text-anchor="middle">主程序</text>
    <text class="fig-label" x="609" y="72" font-size="11" text-anchor="middle">继续主程序</text>
    <text class="fig-blue" x="240" y="130" font-size="10.5" text-anchor="middle">ISR1（低级，已开中断）</text>
    <text class="fig-blue" x="490" y="130" font-size="10.5" text-anchor="middle">继续 ISR1</text>
    <text class="fig-orange" x="370" y="180" font-size="10.5" text-anchor="middle">ISR2（高级嵌套）</text>
    <text class="fig-muted" x="170" y="216" font-size="9.8" text-anchor="middle">压入 PC₁</text>
    <text class="fig-muted" x="310" y="216" font-size="9.8" text-anchor="middle">压入 PC₂（栈深=2）</text>
    <text class="fig-muted" x="430" y="216" font-size="9.8" text-anchor="middle">弹出 PC₂</text>
    <text class="fig-muted" x="550" y="216" font-size="9.8" text-anchor="middle">弹出 PC₁（栈空）</text>
    <text class="fig-accent" x="350" y="236" font-size="10.5" text-anchor="middle">栈遵循后进先出（LIFO）：高级中断先返回到低级 ISR1，低级 ISR1 再返回到主程序</text>
  </svg>
  <figcaption>图 5-10　多重中断嵌套时空执行轨迹与堆栈 LIFO 保护/恢复断点过程</figcaption>
</figure>

<strong>与查询方式的本质差别</strong>：设备准备阶段 CPU 被解放出来，只有真正传送数据时才打断主程序。但数据传送仍要 CPU 执行程序完成，且保护/恢复现场有额外开销，因此只是<strong>部分并行</strong>。

### 5.6 DMA 方式（一）：特点与访存方式

DMA（直接存储器访问）在主存与设备之间建立<strong>直接通路</strong>，由 DMA 接口控制成块数据传送，传送过程不需要 CPU 参与。

| 项目 | 程序查询／程序中断 | DMA |
|---|---|---|
| 数据通路 | 设备 ↔ 接口 ↔ <strong>CPU 寄存器</strong> ↔ 主存 | 设备 ↔ <strong>DMA 接口</strong> ↔ 主存 |
| 传送控制 | CPU 执行程序（软件） | DMA 控制逻辑（硬件） |
| CPU 参与程度 | 查询：全程；中断：每字一次 | 仅预处理与后处理 |

<figure class="cpd-figure not-content" id="lhw-w4b-fig-5">
  <svg viewBox="0 0 700 282" role="img" aria-label="程序查询或中断方式与 DMA 方式的数据通路对比图">
    <defs>
      <marker id="lhw-w4b-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="32" width="668" height="82" rx="7"/>
    <rect class="fig-box-subtle" x="16" y="124" width="668" height="146" rx="7"/>
    <line class="fig-line" x1="146" y1="80" x2="202" y2="80" marker-start="url(#lhw-w4b-arr-5)" marker-end="url(#lhw-w4b-arr-5)"/>
    <line class="fig-line" x1="330" y1="80" x2="386" y2="80" marker-start="url(#lhw-w4b-arr-5)" marker-end="url(#lhw-w4b-arr-5)"/>
    <line class="fig-line" x1="500" y1="80" x2="556" y2="80" marker-start="url(#lhw-w4b-arr-5)" marker-end="url(#lhw-w4b-arr-5)"/>
    <line class="fig-line-accent" x1="146" y1="178" x2="286" y2="178" marker-start="url(#lhw-w4b-arr-5)" marker-end="url(#lhw-w4b-arr-5)"/>
    <line class="fig-line-accent" x1="422" y1="178" x2="556" y2="178" marker-start="url(#lhw-w4b-arr-5)" marker-end="url(#lhw-w4b-arr-5)"/>
    <line class="fig-line-dashed" x1="354" y1="224" x2="354" y2="200" marker-end="url(#lhw-w4b-arr-5)"/>
    <rect class="fig-box" x="42" y="60" width="104" height="40" rx="4"/>
    <rect class="fig-box-orange" x="202" y="60" width="128" height="40" rx="4"/>
    <rect class="fig-box" x="386" y="60" width="114" height="40" rx="4"/>
    <rect class="fig-box" x="556" y="60" width="104" height="40" rx="4"/>
    <rect class="fig-box" x="42" y="158" width="104" height="40" rx="4"/>
    <rect class="fig-box-blue" x="286" y="158" width="136" height="40" rx="4"/>
    <rect class="fig-box" x="556" y="158" width="104" height="40" rx="4"/>
    <rect class="fig-box" x="294" y="224" width="120" height="34" rx="4"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">两种数据通路对比</text>
    <text class="fig-muted" x="28" y="50" font-size="10.5">(a) 程序查询 / 程序中断：数据必经 CPU 寄存器中转</text>
    <text class="fig-label" x="94" y="84" font-size="11.5" text-anchor="middle">主存</text>
    <text class="fig-orange" x="266" y="77" font-size="11" text-anchor="middle">CPU</text>
    <text class="fig-muted" x="266" y="92" font-size="10" text-anchor="middle">寄存器 ACC</text>
    <text class="fig-label" x="443" y="84" font-size="11.5" text-anchor="middle">I/O 接口</text>
    <text class="fig-label" x="608" y="84" font-size="11.5" text-anchor="middle">外设</text>
    <text class="fig-muted" x="28" y="144" font-size="10.5">(b) DMA：主存与外设直接通路，CPU 只做预处理与后处理</text>
    <text class="fig-label" x="94" y="182" font-size="11.5" text-anchor="middle">主存</text>
    <text class="fig-blue" x="354" y="175" font-size="11" text-anchor="middle">DMA 接口</text>
    <text class="fig-muted" x="354" y="190" font-size="10" text-anchor="middle">AR / WC / BR / DAR</text>
    <text class="fig-label" x="608" y="182" font-size="11.5" text-anchor="middle">外设</text>
    <text class="fig-label" x="354" y="245" font-size="11.5" text-anchor="middle">CPU</text>
    <text class="fig-muted" x="424" y="216" font-size="10">仅启动（预处理）与善后（后处理）</text>
  </svg>
  <figcaption>图 5-11　数据通路对比：程序查询/中断（经 CPU 寄存器中转）vs DMA（主存与外设直通）</figcaption>
</figure>

<strong>DMA 与主存交换数据的三种方式</strong>

| 方式 | 做法 | 特点 |
|---|---|---|
| 停止 CPU 访存 | 整块数据传送期间 CPU 放弃总线与访存权，<br>全交给 DMA 接口 | 控制最简单，适合大批量传送；<br>字间有间隔、DMA 仍占访存权，<br>主存利用率低 |
| <strong>周期挪用（周期窃取）</strong> | 每传一个字，DMA 申请占用<br><strong>一个或几个存取周期</strong>，传完立即归还 | 主存与总线利用率高，<br>是现代机器常用方式 |
| 交替访存 | 把 CPU 工作周期分为 <strong>C1、C2</strong>，<br>C1 专供 DMA、C2 专供 CPU | 时序固定，无需申请/归还总线，<br>速度最快；实用性不强 |

周期挪用时的三种情况：CPU <strong>不访存</strong> → 访存权直接给 DMA；CPU <strong>正处在存取周期中间</strong> → DMA 只能等待，不能抢占；CPU 与 DMA <strong>同时请求</strong> → <strong>DMA 优先</strong>，因为 DMA 接的是高速设备，不及时响应可能丢数据。

<figure class="cpd-figure not-content" id="lhw-w4b-fig-6">
  <svg viewBox="0 0 700 336" role="img" aria-label="DMA 三种访存方式（停止 CPU 访存、周期挪用、交替访存）时序对比图">
    <rect class="fig-box-subtle" x="16" y="32" width="668" height="264" rx="7"/>
    <line class="fig-line" x1="160" y1="74" x2="660" y2="74" stroke-opacity="0.25"/>
    <line x1="160" y1="74" x2="270" y2="74" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="270" y1="74" x2="490" y2="74" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="490" y1="74" x2="660" y2="74" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line class="fig-line" x1="160" y1="164" x2="660" y2="164" stroke-opacity="0.25"/>
    <line x1="160" y1="164" x2="250" y2="164" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="290" y1="164" x2="375" y2="164" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="415" y1="164" x2="500" y2="164" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="540" y1="164" x2="660" y2="164" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="250" y1="164" x2="290" y2="164" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="375" y1="164" x2="415" y2="164" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="500" y1="164" x2="540" y2="164" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line class="fig-line" x1="160" y1="250" x2="660" y2="250" stroke-opacity="0.25"/>
    <line x1="160" y1="250" x2="202" y2="250" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="202" y1="250" x2="244" y2="250" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="244" y1="250" x2="286" y2="250" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="286" y1="250" x2="328" y2="250" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="328" y1="250" x2="370" y2="250" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="370" y1="250" x2="412" y2="250" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="412" y1="250" x2="454" y2="250" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="454" y1="250" x2="496" y2="250" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="496" y1="250" x2="538" y2="250" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="538" y1="250" x2="580" y2="250" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="580" y1="250" x2="622" y2="250" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <line x1="622" y1="250" x2="660" y2="250" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="160" y1="316" x2="196" y2="316" stroke="var(--sl-color-text-accent)" stroke-opacity="0.45" stroke-width="10"/>
    <line x1="290" y1="316" x2="326" y2="316" stroke="var(--sl-color-blue)" stroke-width="10"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">DMA 三种访存方式的时序对比</text>
    <text class="fig-label" x="28" y="78" font-size="11.5">(a) 停止 CPU 访存</text>
    <text class="fig-blue" x="380" y="58" font-size="11" text-anchor="middle">DMA 独占总线与主存（整块传送）</text>
    <text class="fig-muted" x="160" y="102" font-size="10">CPU 保持等待（除非指令缓冲中还有不需访存的指令）</text>
    <text class="fig-red" x="160" y="118" font-size="10">字间间隔内仍占访存权 → 主存利用率低</text>
    <text class="fig-label" x="28" y="168" font-size="11.5">(b) 周期挪用</text>
    <text class="fig-blue" x="395" y="148" font-size="11" text-anchor="middle">每传一字窃取 1 个存取周期（同时请求时 DMA 优先）</text>
    <text class="fig-muted" x="160" y="190" font-size="10.5">CPU 在字间间隔照常访存，主存与总线利用率最高</text>
    <text class="fig-label" x="28" y="254" font-size="11.5">(c) 交替访存</text>
    <text class="fig-muted" x="160" y="276" font-size="10.5">C1 归 DMA、C2 归 CPU，时序固定，无需申请/归还总线</text>
    <text class="fig-label" x="204" y="320" font-size="11">CPU 访存</text>
    <text class="fig-blue" x="334" y="320" font-size="11">DMA 访存</text>
    <text class="fig-muted" x="436" y="320" font-size="10.5">主存是全机速度瓶颈（存储墙）</text>
  </svg>
  <figcaption>图 5-12　DMA 三种访存方式时序对比（停止 CPU 访存 vs 周期挪用/窃取 vs 交替访存）</figcaption>
</figure>

> <strong>注意</strong>：DMA 并不“打断程序的执行”——它只在存取周期一级抢占总线与主存。CPU 若已预取到指令缓冲，且该指令不需要访存，仍可在被窃取的周期内继续执行。

### 5.6 DMA 方式（二）：接口组成与传送过程

<strong>DMA 接口的五项功能</strong>（结构由功能决定）

1. 向 CPU 提出<strong>总线与主存的占用请求</strong>；
2. CPU 允许后，处理<strong>控制权转交</strong>；
3. 接管并<strong>管理总线与主存</strong>，控制数据传送；
4. 确定<strong>传送方向</strong>，给出主存首址、传送数据量、设备地址；
5. 每传一个字<strong>修改主存地址与长度</strong>，全部结束后给出<strong>操作完成信号</strong>。

<strong>DMA 接口的组成</strong>

| 部件 | 作用 |
|---|---|
| <strong>AR</strong> 主存地址寄存器 | 经地址线给出要访问的主存单元地址，每传一字修改 |
| <strong>WC</strong> 字计数器 | 以补码存 <strong>-N</strong>，每传一字加一，计到 0 溢出即表示结束 |
| <strong>BR</strong> 数据缓冲寄存器 | 暂存设备与主存之间传送的一个字 |
| <strong>DAR</strong> 设备地址寄存器 | 供设备选择电路比较；访问硬盘时还可存柱面号、磁道号、扇区号 |
| <strong>DMA 控制逻辑</strong> | 收发 DREQ（设备请求）与设备应答 DACK、总线请求/应答、读写命令 |
| <strong>中断机构</strong> | WC 溢出后置中断请求触发器，一条指令结束时向 CPU 发中断请求，供后处理 |

字计数器与传送字数的关系为：

$$
WC_{0}=-N,\quad WC_{k}=WC_{0}+k,\quad WC_{N}=0\ \text{(overflow, transfer ends)}
$$

<strong>传送过程分三段</strong>

- <strong>预处理</strong>（CPU 执行 I/O 指令完成）：设置传送方向、主存起始地址、传送数据量、设备地址，然后<strong>启动设备</strong>；此后 CPU 继续执行主程序。
- <strong>数据传送</strong>（DMA 接口控制）：判断能否占用总线与主存 → 由 AR 送主存地址 → 传一个字（BR↔主存） → 修改 AR 与 WC → 未结束则继续下一字，结束则 WC 溢出。
- <strong>后处理</strong>（中断服务程序）：WC 溢出 → DMA 接口向 CPU 发中断请求 → CPU 执行中断服务程序做<strong>数据校验</strong>、判断<strong>是否继续使用 DMA</strong>、用诊断程序检查传送是否正确，然后中断返回继续主程序。

<figure class="cpd-figure not-content" id="lhw-w4b-fig-7">
  <svg viewBox="0 0 700 306" role="img" aria-label="DMA 传送三阶段流程：预处理、数据传送与后处理">
    <defs>
      <marker id="lhw-w4b-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>
    <line class="fig-line" x1="72" y1="86" x2="72" y2="122" marker-end="url(#lhw-w4b-arr-7)"/>
    <line class="fig-line" x1="132" y1="145" x2="146" y2="145" marker-end="url(#lhw-w4b-arr-7)"/>
    <line class="fig-line" x1="258" y1="145" x2="272" y2="145" marker-end="url(#lhw-w4b-arr-7)"/>
    <line class="fig-line" x1="384" y1="145" x2="398" y2="145" marker-end="url(#lhw-w4b-arr-7)"/>
    <line class="fig-line" x1="510" y1="145" x2="524" y2="145" marker-end="url(#lhw-w4b-arr-7)"/>
    <path class="fig-line-dashed" d="M 578 166 L 578 186 L 76 186 L 76 168" stroke="var(--sl-color-blue)" fill="none" marker-end="url(#lhw-w4b-arr-7)"/>
    <path class="fig-line" d="M 632 145 L 664 145 L 664 208 L 350 208 L 350 224" fill="none" marker-end="url(#lhw-w4b-arr-7)"/>
    <rect class="fig-box" x="20" y="36" width="644" height="50" rx="5"/>
    <rect class="fig-box" x="20" y="124" width="112" height="42" rx="4"/>
    <rect class="fig-box" x="146" y="124" width="112" height="42" rx="4"/>
    <rect class="fig-box" x="272" y="124" width="112" height="42" rx="4"/>
    <rect class="fig-box" x="398" y="124" width="112" height="42" rx="4"/>
    <rect class="fig-box-blue" x="524" y="124" width="108" height="42" rx="4"/>
    <rect class="fig-box-green" x="20" y="226" width="644" height="62" rx="5"/>
    <text class="fig-title" x="16" y="22" font-size="12.5">DMA 传送过程：预处理 → 数据传送 → 后处理</text>
    <text class="fig-label" x="34" y="56" font-size="11">① 预处理（CPU 执行 I/O 指令）：传送方向 → 主存起始地址 → 数据量（-N）→ 设备地址</text>
    <text class="fig-muted" x="34" y="74" font-size="10.5">随后启动设备，CPU 继续执行主程序</text>
    <text class="fig-muted" x="84" y="106" font-size="10">设备准备好后</text>
    <text class="fig-blue" x="168" y="114" font-size="11">② 数据传送（DMA 控制逻辑硬件完成）</text>
    <text class="fig-label" x="76" y="142" font-size="10.5" text-anchor="middle">允许使用</text>
    <text class="fig-label" x="76" y="157" font-size="10.5" text-anchor="middle">总线/主存？</text>
    <text class="fig-label" x="202" y="149" font-size="10.5" text-anchor="middle">送主存地址 AR</text>
    <text class="fig-label" x="328" y="149" font-size="10.5" text-anchor="middle">传一个字 BR</text>
    <text class="fig-label" x="454" y="142" font-size="10.5" text-anchor="middle">修改 AR、</text>
    <text class="fig-label" x="454" y="157" font-size="10.5" text-anchor="middle">WC（加一）</text>
    <text class="fig-blue" x="578" y="149" font-size="10.5" text-anchor="middle">WC 溢出？</text>
    <text class="fig-blue" x="330" y="200" font-size="10.5" text-anchor="middle">否（继续传送下一个字）</text>
    <text class="fig-label" x="590" y="202" font-size="10.5">是（WC=0）</text>
    <text class="fig-green" x="34" y="248" font-size="11">③ 后处理（中断服务程序）：WC 溢出 → DMA 接口发中断请求 → CPU 响应</text>
    <text class="fig-muted" x="34" y="268" font-size="10.5">数据校验 → 判断是否继续使用 DMA → 诊断传送是否正确 → 中断返回，继续主程序</text>
  </svg>
  <figcaption>图 5-13　DMA 传送三阶段流程（CPU 预处理 → DMA 硬件成块传送 → 中断后处理）</figcaption>
</figure>

<strong>输入与输出的差别</strong>：输入时设备先把数据送入 BR，再由 DMA 接口写到主存指定地址；输出时 DMA 接口先把主存数据读入 BR，再送设备，并向设备发应答信号开始下一次传送。两者都在每传一字后修改 AR 与 WC，靠 WC 溢出判断结束。

> <strong>★考点</strong>：DMA 接口四寄存器（AR、WC、BR、DAR）；WC 用补码存 -N 并靠溢出判结束；预处理/传送/后处理三段各由谁完成；周期挪用时 DMA 与 CPU 同时请求访存则 DMA 优先。

> <strong>注意</strong>：DMA 方式并没有“保存现场、恢复现场、执行中断服务程序”来完成数据传送，传送本身全由硬件完成——这是它比中断方式更彻底地解放 CPU 的关键。CPU 只在预处理（几条 I/O 指令）和后处理（一次中断）时介入。

## 第6章 计算机的运算方法

### 6.1 数据表示、真值与机器数

<strong>数据表示</strong>是计算机中能够被硬件直接识别和处理的数据类型。判据有两条：该类型出现在指令系统中，且相应运算由硬件实现。本章沿“先有表示、再有运算方法、最后有运算部件”的顺序展开：无符号数与有符号数 → 定点/浮点表示 → 定点/浮点运算 → 运算器（ALU）。

- <strong>真值</strong>：带正负号的实际数值，如 $-1101$、$+0.1101$。
- <strong>机器数</strong>：符号与数值一起用二进制编码后的数，符号位用 0 表示正、1 表示负，符号位与数值位在<strong>书写</strong>时用逗号（整数）或小数点（小数）隔开，机器内部并无此分隔符。
- 一台机器能表示的数受<strong>字长</strong>限制，超出字长的位必须丢弃，因此“定义”之外还要回答“范围”与“转换”两个问题。

同一串二进制代码，用不同机器数规则解读，真值完全不同；四种规则各有用途。

| 机器数 | 负数编码方式 | 零的个数 | 主要用途 |
|---|---|---|---|
| 原码 | 符号位 1 + 数值位绝对值 | 2 个（±0） | 表示直观，乘除时符号单独处理 |
| 反码 | 符号位 1 + 数值位逐位取反 | 2 个（±0） | 原码到补码的过渡形式 |
| 补码 | 符号位 1 + 数值位取反末位加一 | 1 个 | 加减运算的主流形式 |
| 移码 | 补码符号位取反 | 1 个 | 表示阶码，便于比较大小 |

<figure class="cpd-figure not-content" id="lhw-w5-fig-1">
  <svg viewBox="0 0 700 264" role="img" aria-label="真值与四种机器数（原码、反码、补码、移码）编码规则及零点个数对比示意图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="236" rx="8"/>
    <line class="fig-line" x1="142" y1="138" x2="188" y2="138" marker-end="url(#lhw-w5-arr-1)"/>
    <path class="fig-line" d="M 326 138 H 362 V 62 H 398" marker-end="url(#lhw-w5-arr-1)"/>
    <path class="fig-line" d="M 326 138 H 362 V 112 H 398" marker-end="url(#lhw-w5-arr-1)"/>
    <path class="fig-line-accent" d="M 326 138 H 362 V 162 H 398" marker-end="url(#lhw-w5-arr-1)"/>
    <path class="fig-line" d="M 326 138 H 362 V 212 H 398" marker-end="url(#lhw-w5-arr-1)"/>
    <rect class="fig-box" x="30" y="106" width="112" height="64" rx="6"/>
    <rect class="fig-box-accent" x="190" y="102" width="136" height="72" rx="6"/>
    <rect class="fig-box-blue" x="400" y="42" width="268" height="40" rx="5"/>
    <rect class="fig-box" x="400" y="92" width="268" height="40" rx="5"/>
    <rect class="fig-box-green" x="400" y="142" width="268" height="40" rx="5"/>
    <rect class="fig-box-purple" x="400" y="192" width="268" height="40" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="13">同一二进制串的四种机器数解读</text>
    <text class="fig-label" x="86" y="132" text-anchor="middle" font-size="12.5">真值 x</text>
    <text class="fig-muted" x="86" y="151" text-anchor="middle" font-size="10.5">带 ± 号的实际数值</text>
    <text class="fig-accent" x="258" y="130" text-anchor="middle" font-size="12.5">机器数编码</text>
    <text class="fig-muted" x="258" y="149" text-anchor="middle" font-size="10.5">符号位 + 数值位</text>
    <text class="fig-muted" x="258" y="164" text-anchor="middle" font-size="10">（受字长 n+1 限制）</text>
    <text class="fig-blue" x="414" y="60" font-size="12">原码：符号位 + |x|</text>
    <text class="fig-muted" x="414" y="75" font-size="10.5">2 个零（±0），乘除时符号单独处理</text>
    <text class="fig-label" x="414" y="110" font-size="12">反码：负数数值位逐位取反</text>
    <text class="fig-muted" x="414" y="125" font-size="10.5">2 个零（±0），原码到补码的过渡</text>
    <text class="fig-green" x="414" y="160" font-size="12">补码：反码末位加 1（模 2^(n+1)）</text>
    <text class="fig-muted" x="414" y="175" font-size="10.5">1 个零，加减统一且符号位参与运算</text>
    <text class="fig-purple" x="414" y="210" font-size="12">移码：补码符号位取反（真值 + 2^n）</text>
    <text class="fig-muted" x="414" y="225" font-size="10.5">1 个零，单调递增，专用于浮点阶码</text>
  </svg>
  <figcaption>图 6-1　真值与四种机器数（原码、反码、补码、移码）的编码规则与用途对比</figcaption>
</figure>

> <strong>注意</strong>：符号位是编码位，不是数值位。原码、反码中符号位不参加数值运算；补码中符号位与数值位一起参加运算，符号位产生的进位自然丢弃。

### 6.2 无符号数与有符号数

<strong>无符号数</strong>：全部 $n$ 位都是数值位，没有符号位，只能表示非负数。

$$
0 \le x \le 2^{n}-1
$$

<strong>有符号数</strong>：最高位为符号位（0 正、1 负），其余位为数值位。同一个 8 位码串按不同规则解读的结果如下表。

| 8 位代码 | 无符号数 | 原码 | 补码 | 反码 |
|---|---|---|---|---|
| 0000 0000 | 0 | +0 | 0 | +0 |
| 0111 1111 | 127 | +127 | +127 | +127 |
| 1111 1111 | 255 | -127 | -1 | -0 |
| 1000 0000 | 128 | -0 | -128 | -127 |

可见：字长固定时，有符号数的绝对值范围大约是无符号数的一半；补码把原码中“-0”这个码点利用起来，向负方向多表示一个数。

<figure class="cpd-figure not-content" id="lhw-w5-fig-2">
  <svg viewBox="0 0 700 286" role="img" aria-label="四个典型 8 位二进制代码按无符号数、原码、补码、反码解读的真值对照图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="258" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">同一 8 位码串在不同机器数规则下的真值差异</text>
    <text class="fig-muted" x="30" y="54" font-size="10">b7（符号位）</text>
    <text class="fig-muted" x="222" y="54" text-anchor="end" font-size="10">b0（最低位）</text>
    <rect class="fig-box-blue" x="30" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="54" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="78" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="102" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="126" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="150" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="174" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="198" y="64" width="24" height="38" rx="2"/>
    <rect class="fig-box-blue" x="30" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="54" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="78" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="102" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="126" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="150" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="174" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="198" y="114" width="24" height="38" rx="2"/>
    <rect class="fig-box-red" x="30" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="54" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="78" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="102" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="126" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="150" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="174" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="198" y="164" width="24" height="38" rx="2"/>
    <rect class="fig-box-green" x="30" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="54" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="78" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="102" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="126" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="150" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="174" y="214" width="24" height="38" rx="2"/>
    <rect class="fig-box" x="198" y="214" width="24" height="38" rx="2"/>
    <g text-anchor="middle" font-size="12">
      <text class="fig-blue" x="42" y="88">0</text><text class="fig-label" x="66" y="88">0</text><text class="fig-label" x="90" y="88">0</text><text class="fig-label" x="114" y="88">0</text><text class="fig-label" x="138" y="88">0</text><text class="fig-label" x="162" y="88">0</text><text class="fig-label" x="186" y="88">0</text><text class="fig-label" x="210" y="88">0</text>
      <text class="fig-blue" x="42" y="138">0</text><text class="fig-label" x="66" y="138">1</text><text class="fig-label" x="90" y="138">1</text><text class="fig-label" x="114" y="138">1</text><text class="fig-label" x="138" y="138">1</text><text class="fig-label" x="162" y="138">1</text><text class="fig-label" x="186" y="138">1</text><text class="fig-label" x="210" y="138">1</text>
      <text class="fig-red" x="42" y="188">1</text><text class="fig-label" x="66" y="188">1</text><text class="fig-label" x="90" y="188">1</text><text class="fig-label" x="114" y="188">1</text><text class="fig-label" x="138" y="188">1</text><text class="fig-label" x="162" y="188">1</text><text class="fig-label" x="186" y="188">1</text><text class="fig-label" x="210" y="188">1</text>
      <text class="fig-green" x="42" y="238">1</text><text class="fig-label" x="66" y="238">0</text><text class="fig-label" x="90" y="238">0</text><text class="fig-label" x="114" y="238">0</text><text class="fig-label" x="138" y="238">0</text><text class="fig-label" x="162" y="238">0</text><text class="fig-label" x="186" y="238">0</text><text class="fig-label" x="210" y="238">0</text>
    </g>
    <rect class="fig-box" x="242" y="64" width="428" height="38" rx="5"/>
    <rect class="fig-box" x="242" y="114" width="428" height="38" rx="5"/>
    <rect class="fig-box-red" x="242" y="164" width="428" height="38" rx="5"/>
    <rect class="fig-box-green" x="242" y="214" width="428" height="38" rx="5"/>
    <text class="fig-label" x="252" y="88" font-size="11">无符号：0　｜　原码：+0　｜　反码：+0　｜　补码：0</text>
    <text class="fig-label" x="252" y="138" font-size="11">无符号：127　｜　原码：+127　｜　反码：+127　｜　补码：+127</text>
    <text class="fig-red" x="252" y="188" font-size="11">无符号：255　｜　原码：-127　｜　反码：-0　｜　补码：-1</text>
    <text class="fig-green" x="252" y="238" font-size="10.8">无符号：128　｜　原码：-0　｜　反码：-127　｜　补码：-128（多出码点）</text>
  </svg>
  <figcaption>图 6-2　8 位二进制代码在无符号数、原码、反码与补码下的真值对照</figcaption>
</figure>

> <strong>★考点</strong>：给定一串代码，先看题目按哪种机器数解释；补码的负数范围比原码多一个码点（小数多 $-1$，整数多 $-2^{n}$）。

### 6.3 原码与反码表示法

<strong>原码</strong>：符号位表示正负，数值位取真值的绝对值，定义按真值分段：

| 真值范围（整数，$n$ 位数值） | [x]原 | 真值范围（小数） | [x]原 |
|---|---|---|---|
| $0 \le x < 2^{n}$ | $0,x$ | $0 \le x < 1$ | $0.x_1x_2\cdots x_n$（即 $x$） |
| $-2^{n} < x \le 0$ | 符号位 1，后接绝对值 | $-1 < x \le 0$ | 符号位 1，后接绝对值（即 $1-x$） |

<strong>反码</strong>：正数与原码相同；负数为“符号位 1 + 数值位逐位取反”。其模为 $2^{n+1}-1$（整数）或 $2-2^{-n}$（小数）。

| 真值范围（整数） | [x]反 | 真值范围（小数） | [x]反 |
|---|---|---|---|
| $0 \le x < 2^{n}$ | $0,x$ | $0 \le x < 1$ | $x$ |
| $-2^{n} < x \le 0$ | $(2^{n+1}-1)+x$ | $-1 < x \le 0$ | $(2-2^{-n})+x$ |

<strong>反码与补码只差末位那个 1</strong>：反码是补码“末位不加一”的结果，反码的模也因此比补码的模小 1。

例（字长 5 位，1 符号 + 4 数值）：

- $x=-1101$：[x]原 = $1{,}1101$，[x]反 = $1{,}0010$，[x]补 = $1{,}0011$。
- $x=-0.1010$（$n=4$）：[x]原 = $1.1010$，[x]反 = $1.0101$，[x]补 = $1.0110$。

<strong>原码的范围关于原点对称</strong>：小数定点机为 $-(1-2^{-n})\sim +(1-2^{-n})$，整数定点机为 $-(2^{n}-1)\sim +(2^{n}-1)$，且<strong>正零与负零是两个不同的码</strong>（$0.00\cdots0$ 与 $1.00\cdots0$）。反码范围与原码完全相同，同样有两个零。

> <strong>注意</strong>：小数原码不能表示 $-1$（$1.000\cdots0$ 是负零）；只有补码能用 $1.000\cdots0$ 表示 $-1$。

### 6.4 补码表示法

<strong>补码</strong>以 $2^{n+1}$（整数）或 $2$（小数）为模，把负数映射到模内：

| 真值范围（整数） | [x]补 | 真值范围（小数） | [x]补 |
|---|---|---|---|
| $0 \le x < 2^{n}$ | $0,x$ | $0 \le x < 1$ | $x$ |
| $-2^{n} \le x < 0$ | $2^{n+1}+x \pmod{2^{n+1}}$ | $-1 \le x < 0$ | $2+x \pmod{2}$ |

快速求法：负数取“符号位 1，数值位按位取反，末位加一”。由此可推出两条常用结论：

- <strong>零唯一</strong>：[+0]补 = [-0]补 = $0.00\cdots0$，省下的一个码点用于表示 $-1$（小数）或 $-2^{n}$（整数）。
- <strong>求相反数的补码</strong>：不论 $y$ 正负，[-y]补 都等于 [y]补 <strong>连同符号位在内</strong>每位取反、末位加一。

$$
[-y]_{\mathrm{comp}} = \overline{[y]_{\mathrm{comp}}} + 1
$$

这是补码减法能转成加法的依据。一个 8 位补码的取值范围是 $-128 \sim +127$，比原码多出一个 $-128$（码 `1000 0000`）。

例：[x]补 = $0{,}1110 \Rightarrow x=+1110$；[x]补 = $1{,}1111 \Rightarrow$ 取反加一得 $0{,}0001$，故 $x=-1$。

<figure class="cpd-figure not-content" id="lhw-w5-fig-3">
  <svg viewBox="0 0 700 218" role="img" aria-label="负数从真值依次转换为原码、反码、补码和移码的流水线关系图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="190" rx="8"/>
    <line class="fig-line" x1="118" y1="100" x2="156" y2="100" marker-end="url(#lhw-w5-arr-3)"/>
    <line class="fig-line" x1="252" y1="100" x2="290" y2="100" marker-end="url(#lhw-w5-arr-3)"/>
    <line class="fig-line-accent" x1="386" y1="100" x2="424" y2="100" marker-end="url(#lhw-w5-arr-3)"/>
    <line class="fig-line" x1="520" y1="100" x2="558" y2="100" marker-end="url(#lhw-w5-arr-3)"/>
    <rect class="fig-box" x="30" y="76" width="88" height="48" rx="6"/>
    <rect class="fig-box-blue" x="160" y="76" width="92" height="48" rx="6"/>
    <rect class="fig-box" x="294" y="76" width="92" height="48" rx="6"/>
    <rect class="fig-box-green" x="428" y="76" width="92" height="48" rx="6"/>
    <rect class="fig-box-purple" x="562" y="76" width="106" height="48" rx="6"/>
    <text class="fig-title" x="30" y="36" font-size="13">负数编码转换链（以 5 位字长 x = -1101 为例）</text>
    <text class="fig-label" x="74" y="96" text-anchor="middle" font-size="12">真值 x</text>
    <text class="fig-muted" x="74" y="114" text-anchor="middle" font-size="10.5">-1101</text>
    <text class="fig-blue" x="206" y="96" text-anchor="middle" font-size="12">原码</text>
    <text class="fig-muted" x="206" y="114" text-anchor="middle" font-size="10.5">1,1101</text>
    <text class="fig-label" x="340" y="96" text-anchor="middle" font-size="12">反码</text>
    <text class="fig-muted" x="340" y="114" text-anchor="middle" font-size="10.5">1,0010</text>
    <text class="fig-green" x="474" y="96" text-anchor="middle" font-size="12">补码</text>
    <text class="fig-muted" x="474" y="114" text-anchor="middle" font-size="10.5">1,0011</text>
    <text class="fig-purple" x="615" y="96" text-anchor="middle" font-size="12">移码</text>
    <text class="fig-muted" x="615" y="114" text-anchor="middle" font-size="10.5">0,0011</text>
    <text class="fig-muted" x="138" y="64" text-anchor="middle" font-size="10">符号 1 + |x|</text>
    <text class="fig-muted" x="272" y="64" text-anchor="middle" font-size="10">数值位取反</text>
    <text class="fig-accent" x="406" y="64" text-anchor="middle" font-size="10">末位加 1</text>
    <text class="fig-muted" x="540" y="64" text-anchor="middle" font-size="10">符号位取反</text>
    <text class="fig-muted" x="30" y="160" font-size="11">· 正数：原码、反码、补码完全相同（符号位 0 后接数值位），移码仅符号位取反为 1</text>
    <text class="fig-muted" x="30" y="182" font-size="11">· 模对比：反码模 2^(n+1)-1；补码模 2^(n+1)；移码等价于真值加偏移量 2^n</text>
  </svg>
  <figcaption>图 6-3　负数从真值到原码、反码、补码与移码的逐级转换链</figcaption>
</figure>

### 6.5 移码表示法

<strong>移码</strong>在补码基础上把符号位取反，等价于给真值加一个偏移量 $2^{n}$（$n$ 为数值位数）：

$$
[x]_{\mathrm{bias}} = 2^{n} + x, \quad -2^{n} \le x \le 2^{n}-1
$$

移码只用于表示<strong>整数</strong>，典型用途是浮点数的阶码。它的优点是<strong>码值随真值单调递增</strong>：把移码当作无符号数直接比较，结果就是真值的大小关系，不必先判符号。

| 真值 | 移码（8 位） | 补码（8 位） |
|---|---|---|
| $-128$ | 0000 0000 | 1000 0000 |
| $-1$ | 0111 1111 | 1111 1111 |
| $0$ | 1000 0000 | 0000 0000 |
| $+127$ | 1111 1111 | 0111 1111 |

> <strong>注意</strong>：移码零唯一，且与补码只差符号位的一次取反；比较阶码大小时按无符号数直接比即可。

### 6.6 补码加减运算与溢出判断

补码把减法变成加法，符号位直接参加运算，最高位产生的进位丢弃：

$$
[x+y]_{\mathrm{comp}} = [x]_{\mathrm{comp}} + [y]_{\mathrm{comp}} \pmod{2^{n+1}}
$$

整数按模 $2^{n+1}$ 相加，小数按模 $2$ 相加，最高位产生的进位一律丢弃。把减法转成加法：

$$
[x-y]_{\mathrm{comp}} = [x]_{\mathrm{comp}} + [-y]_{\mathrm{comp}}
$$

<strong>溢出判断</strong>有两种常用方法：

- <strong>单符号位</strong>：同号相加而结果符号相反，即溢出；异号相加不会溢出。
- <strong>双符号位（变形补码）</strong>：运算时符号位取两位。`00` 为正、`11` 为负，均正常；`01` 为正溢出，`10` 为负溢出。溢出标志为两个符号位的异或。

| 双符号位结果 | 含义 |
|---|---|
| 00.xxx | 正数，正常 |
| 11.xxx | 负数，正常 |
| 01.xxx | 正溢出（结果超出最大正数） |
| 10.xxx | 负溢出（结果小于最小负数） |

例（小数，4 位数值，双符号位）：[x]补 = $00.1101$，[y]补 = $00.1011$，相加得 $01.1000$。真值 $0.8125+0.6875=1.5$，已超过 $1-2^{-4}$，符号位 `01` 判为正溢出。

<figure class="cpd-figure not-content" id="lhw-w5-fig-4">
  <svg viewBox="0 0 700 288" role="img" aria-label="4位补码模16码环与双符号位01和10溢出判定机理示意图">
    <defs>
      <marker id="lhw-w5-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="14" width="322" height="260" rx="8"/>
    <rect class="fig-box-subtle" x="352" y="14" width="332" height="260" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">4 位补码模 16 码环（+7 与 -8 相邻）</text>
    <text class="fig-title" x="366" y="34" font-size="13">双符号位（变形补码）溢出判定</text>
    <circle cx="176" cy="154" r="78" fill="none" stroke="var(--sl-color-gray-4)" stroke-width="1.2"/>
    <line class="fig-line-dashed" x1="176" y1="66" x2="176" y2="244"/>
    <circle class="fig-dot" cx="176" cy="76" r="4"/>
    <circle class="fig-dot-muted" cx="254" cy="154" r="3.5"/>
    <circle class="fig-dot" cx="206" cy="226" r="4"/>
    <circle class="fig-dot" cx="146" cy="226" r="4"/>
    <circle class="fig-dot-muted" cx="98" cy="154" r="3.5"/>
    <rect class="fig-box" x="112" y="134" width="128" height="56" rx="4"/>
    <text class="fig-label" x="176" y="60" text-anchor="middle" font-size="11">0000 (0)</text>
    <text class="fig-blue" x="264" y="150" font-size="11">0100 (+4)</text>
    <text class="fig-green" x="214" y="246" font-size="11">0111 (+7)</text>
    <text class="fig-red" x="138" y="246" text-anchor="end" font-size="11">1000 (-8)</text>
    <text class="fig-orange" x="88" y="150" text-anchor="end" font-size="11">1100 (-4)</text>
    <text class="fig-red" x="176" y="148" text-anchor="middle" font-size="11">溢出断层</text>
    <text class="fig-muted" x="176" y="166" text-anchor="middle" font-size="9.2">顺时针 +1 跨越 +7→-8</text>
    <text class="fig-muted" x="176" y="182" text-anchor="middle" font-size="9.2">即正溢出；逆时针为负溢出</text>
    <rect class="fig-box-green" x="366" y="52" width="144" height="44" rx="5"/>
    <rect class="fig-box-blue" x="524" y="52" width="146" height="44" rx="5"/>
    <rect class="fig-box-red" x="366" y="108" width="144" height="58" rx="5"/>
    <rect class="fig-box-orange" x="524" y="108" width="146" height="58" rx="5"/>
    <rect class="fig-box" x="366" y="178" width="304" height="82" rx="5"/>
    <text class="fig-green" x="438" y="71" text-anchor="middle" font-size="12">00.xxx：正常正数</text>
    <text class="fig-muted" x="438" y="88" text-anchor="middle" font-size="10.5">V = 0 ⊕ 0 = 0</text>
    <text class="fig-blue" x="597" y="71" text-anchor="middle" font-size="12">11.xxx：正常负数</text>
    <text class="fig-muted" x="597" y="88" text-anchor="middle" font-size="10.5">V = 1 ⊕ 1 = 0</text>
    <text class="fig-red" x="438" y="128" text-anchor="middle" font-size="12">01.xxx：正溢出</text>
    <text class="fig-muted" x="438" y="144" text-anchor="middle" font-size="10">例：00.1101 + 00.1011</text>
    <text class="fig-red" x="438" y="158" text-anchor="middle" font-size="10.5">= 01.1000（V = 1）</text>
    <text class="fig-orange" x="597" y="128" text-anchor="middle" font-size="12">10.xxx：负溢出</text>
    <text class="fig-muted" x="597" y="144" text-anchor="middle" font-size="10">例：11.0011 + 11.0101</text>
    <text class="fig-orange" x="597" y="158" text-anchor="middle" font-size="10.5">= 10.1000（V = 1）</text>
    <text class="fig-accent" x="380" y="198" font-size="11.5">单符号位等价判别：V = Cn ⊕ Cn-1</text>
    <text class="fig-label" x="380" y="218" font-size="11">· Cn：最高符号位向更前方的进位</text>
    <text class="fig-label" x="380" y="236" font-size="11">· Cn-1：最高数值位向符号位的进位</text>
    <text class="fig-muted" x="380" y="252" font-size="10.5">两者相异（01 或 10）即发生溢出</text>
  </svg>
  <figcaption>图 6-4　4 位补码模 16 码环与双符号位（01 正溢出 / 10 负溢出）判定机理</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w5-fig-5">
  <svg viewBox="0 0 700 264" role="img" aria-label="补码加减运算硬件框图：求补电路、并行加法器与溢出检测逻辑">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="236" rx="8"/>
    <path class="fig-line" d="M 118 76 H 316 V 108 H 340" marker-end="url(#lhw-w5-arr-5)"/>
    <line class="fig-line" x1="118" y1="176" x2="152" y2="176" marker-end="url(#lhw-w5-arr-5)"/>
    <path class="fig-line" d="M 284 176 H 316 V 146 H 340" marker-end="url(#lhw-w5-arr-5)"/>
    <line class="fig-line-accent" x1="474" y1="127" x2="520" y2="127" marker-end="url(#lhw-w5-arr-5)"/>
    <path class="fig-line" d="M 407 78 V 48 H 520" marker-end="url(#lhw-w5-arr-5)"/>
    <line class="fig-line-dashed" x1="218" y1="218" x2="218" y2="204" marker-end="url(#lhw-w5-arr-5)"/>
    <rect class="fig-box" x="30" y="54" width="88" height="44" rx="5"/>
    <rect class="fig-box" x="30" y="154" width="88" height="44" rx="5"/>
    <rect class="fig-box-blue" x="154" y="146" width="130" height="60" rx="5"/>
    <rect class="fig-box-accent" x="342" y="78" width="132" height="98" rx="6"/>
    <rect class="fig-box-green" x="522" y="104" width="146" height="46" rx="5"/>
    <rect class="fig-box-red" x="522" y="26" width="146" height="46" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="13">补码加减运算硬件框图</text>
    <text class="fig-label" x="74" y="80" text-anchor="middle" font-size="12.5">[x]补</text>
    <text class="fig-label" x="74" y="180" text-anchor="middle" font-size="12.5">[y]补</text>
    <text class="fig-blue" x="219" y="170" text-anchor="middle" font-size="12">求补控制电路</text>
    <text class="fig-muted" x="219" y="189" text-anchor="middle" font-size="10.5">M=1：连同符号位取反+1</text>
    <text class="fig-accent" x="408" y="114" text-anchor="middle" font-size="12.5">并行加法器</text>
    <text class="fig-label" x="408" y="140" text-anchor="middle" font-size="18">+</text>
    <text class="fig-muted" x="408" y="160" text-anchor="middle" font-size="10">符号位直接参与运算</text>
    <text class="fig-green" x="595" y="131" text-anchor="middle" font-size="12.5">[x ± y]补</text>
    <text class="fig-red" x="595" y="46" text-anchor="middle" font-size="12">溢出标志 V</text>
    <text class="fig-muted" x="595" y="63" text-anchor="middle" font-size="10.5">V = Cn ⊕ Cn-1</text>
    <text class="fig-muted" x="30" y="232" font-size="11">控制信号 M=0 做加法（[y]补直通）；M=1 做减法（求 [-y]补 再相加），最高位进位自然丢弃</text>
  </svg>
  <figcaption>图 6-5　补码加减运算硬件框图与溢出检测通路</figcaption>
</figure>

### 6.7 定点表示与表示范围

<strong>定点表示</strong>指小数点的位置由体系结构约定，硬件中没有专门的小数点部件；软件必须遵守同一约定调整数值。按约定位置分两类：

- <strong>小数定点机</strong>：小数点在数符之后、数值位之前，机器中存放的都是纯小数。
- <strong>整数定点机</strong>：小数点在数值部分之后，机器中存放的都是整数。

设数值位 $n$ 位，各机器数的范围如下。

| 机器数 | 小数定点机 | 整数定点机 |
|---|---|---|
| 原码 | $-(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)$ |

<strong>离散性</strong>：$n+1$ 位共 $2^{n+1}$ 个码点，只能表示同样多个数；区间内其余实数只能用最近码点近似。所以定点数的“范围”与“精度”都由字长决定。

16 位（1 符号 + 15 数值）的具体范围：

| 形式 | 范围 |
|---|---|
| 无符号数 | $0 \sim 65535$ |
| 原码定点小数 | $\pm(1-2^{-15})$，含 $\pm 0$ |
| 补码定点小数 | $-1 \sim +(1-2^{-15})$ |
| 原码定点整数 | $-32767 \sim +32767$ |
| 补码定点整数 | $-32768 \sim +32767$ |

<figure class="cpd-figure not-content" id="lhw-w5-fig-6">
  <svg viewBox="0 0 700 250" role="img" aria-label="定点小数原码与补码表示范围数轴对比图：补码零点唯一且负方向多出-1">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="222" rx="8"/>
    <line class="fig-line" x1="54" y1="132" x2="646" y2="132" marker-end="url(#lhw-w5-arr-6)"/>
    <line class="fig-line" x1="90" y1="124" x2="90" y2="140"/>
    <line class="fig-line" x1="350" y1="124" x2="350" y2="140"/>
    <line class="fig-line" x1="610" y1="124" x2="610" y2="140"/>
    <rect class="fig-box-blue" x="98" y="68" width="504" height="18" rx="4"/>
    <rect class="fig-box-green" x="90" y="182" width="512" height="18" rx="4"/>
    <line stroke="var(--sl-color-red)" stroke-width="2" x1="90" y1="176" x2="90" y2="206"/>
    <circle class="fig-dot" cx="90" cy="191" r="4"/>
    <circle class="fig-dot-muted" cx="350" cy="77" r="4"/>
    <circle class="fig-dot" cx="350" cy="191" r="4"/>
    <text class="fig-title" x="30" y="36" font-size="13">定点小数表示范围对比（数轴）</text>
    <text class="fig-blue" x="98" y="58" font-size="12">原码：-(1 - 2^-n) ~ +(1 - 2^-n)，关于原点对称，含 ±0 两个零</text>
    <text class="fig-label" x="90" y="156" text-anchor="middle" font-size="12">-1</text>
    <text class="fig-label" x="350" y="156" text-anchor="middle" font-size="12">0</text>
    <text class="fig-label" x="610" y="156" text-anchor="middle" font-size="12">+1</text>
    <text class="fig-red" x="90" y="220" text-anchor="middle" font-size="11">多出的 -1（1.00…0）</text>
    <text class="fig-green" x="380" y="220" text-anchor="middle" font-size="12">补码：-1 ~ +(1 - 2^-n)，零点唯一，负向多 1 个码点</text>
  </svg>
  <figcaption>图 6-6　定点小数原码与补码表示范围对比（补码零点唯一且多出 -1）</figcaption>
</figure>

> <strong>★考点</strong>：定点数超出表示范围就是<strong>溢出错误</strong>，没有“下溢当零”的说法；那是浮点数阶码的规则。

### 6.8 浮点表示格式与表示范围

定点表示范围小，绝对值相差悬殊的数无法共处：太阳质量与电子质量相差约 $10^{61}$ 量级，若用二进制定点表示需约 203 位，且高位大量 0 白白浪费存储、存储单元利用率极低。由此引入浮点表示。

$$
N = S \times r^{j}
$$

$S$ 为尾数（纯小数，$|S|<1$），$j$ 为阶码（整数，可正可负），$r$ 为基数（一般取 2，也可取 4、8、16）。同一数有多种写法，如 $11.0101=0.110101\times2^{2}=0.0110101\times2^{3}$；只有尾数最高数值位为 1 的那种是<strong>规格化</strong>形式，可以存入机器。

存储格式：阶符 1 位 + 阶码 $m$ 位 + 数符 1 位 + 尾数 $n$ 位，总长 $m+n+2$。

<figure class="cpd-figure not-content" id="lhw-w5-fig-7">
  <svg viewBox="0 0 700 206" role="img" aria-label="浮点数通用存储格式示意图：阶符1位、阶码m位、数符1位、尾数n位">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="178" rx="8"/>
    <rect class="fig-box-purple" x="36" y="74" width="64" height="46" rx="4"/>
    <rect class="fig-box-blue" x="100" y="74" width="210" height="46" rx="4"/>
    <rect class="fig-box-orange" x="310" y="74" width="64" height="46" rx="4"/>
    <rect class="fig-box-green" x="374" y="74" width="290" height="46" rx="4"/>
    <text class="fig-title" x="36" y="34" font-size="13">浮点数通用存储格式（N = S × r^j，总长 1 + m + 1 + n 位）</text>
    <text class="fig-muted" x="68" y="64" text-anchor="middle" font-size="11">1 位</text>
    <text class="fig-muted" x="205" y="64" text-anchor="middle" font-size="11">小数点位于末尾</text>
    <text class="fig-muted" x="342" y="64" text-anchor="middle" font-size="11">1 位</text>
    <text class="fig-muted" x="519" y="64" text-anchor="middle" font-size="11">小数点紧接数符之后</text>
    <text class="fig-purple" x="68" y="102" text-anchor="middle" font-size="12">阶符</text>
    <text class="fig-blue" x="205" y="102" text-anchor="middle" font-size="12.5">阶码 j（m 位整数）</text>
    <text class="fig-orange" x="342" y="102" text-anchor="middle" font-size="12">数符</text>
    <text class="fig-green" x="519" y="102" text-anchor="middle" font-size="12.5">尾数 S（n 位纯小数）</text>
    <text class="fig-muted" x="36" y="148" font-size="11">· 数符决定整个浮点数的正负；阶符与阶码共同决定小数点实际位置（表示范围）</text>
    <text class="fig-muted" x="36" y="168" font-size="11">· 尾数位数 n 决定有效数字精度；m、n 及基数 r 均由体系结构事先约定</text>
  </svg>
  <figcaption>图 6-7　浮点数通用存储格式（阶符、阶码、数符与尾数划分）</figcaption>
</figure>

设阶码 $m$ 位、尾数 $n$ 位，均取原码且不规格化，则四个边界值为：

| 量 | 表达式 |
|---|---|
| 最小负数（绝对值最大） | $-(1-2^{-n})\times 2^{\,2^{m}-1}$ |
| 最大负数（最接近 0 的负数） | $-2^{-n}\times 2^{-(2^{m}-1)}$ |
| 最小正数 | $2^{-n}\times 2^{-(2^{m}-1)}$ |
| 最大正数 | $(1-2^{-n})\times 2^{\,2^{m}-1}$ |

$m=4$、$n=10$ 时共 16 位，能表示 $2^{16}$ 个离散值，用它们近似区间内无限多的实数。

<strong>上溢与下溢</strong>：阶码大于最大阶码为上溢，按计算出错处理；阶码小于最小阶码为下溢，按<strong>机器零</strong>处理。尾数部分的下溢不算浮点溢出。

<strong>基数的影响：$r$ 越大，同样位数下表示的浮点数范围越大、可表示个数越多，但尾数每一位的“权重”变大，精度反而下降</strong>。

浮点格式设计题（24 位、要求覆盖 $\pm30000$、精度最大）：$2^{14}=16384<30000<2^{15}=32768$，故阶码需 4 位数值；为留出最多尾数位，取 1 阶符 + 4 阶码 + 1 数符 + 18 位尾数。用 6 位阶码 + 10 位尾数（1+5、1+9）时，原码阶码范围 $-31\sim+31$，非规格化原码尾数范围 $2^{-9}\sim(1-2^{-9})$，故正数范围是 $2^{-9}\times2^{-31}\sim(1-2^{-9})\times2^{31}$，负数与之对称。

<figure class="cpd-figure not-content" id="lhw-w5-fig-8">
  <svg viewBox="0 0 700 282" role="img" aria-label="IEEE 754单精度32位与双精度64位浮点格式字段划分及特殊值编码表">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="254" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">IEEE 754 标准浮点格式（单精度 32 位 / 双精度 64 位）</text>
    <rect class="fig-box-orange" x="30" y="54" width="52" height="38" rx="4"/>
    <rect class="fig-box-blue" x="82" y="54" width="176" height="38" rx="4"/>
    <rect class="fig-box-green" x="258" y="54" width="412" height="38" rx="4"/>
    <text class="fig-orange" x="56" y="77" text-anchor="middle" font-size="11">S (1)</text>
    <text class="fig-blue" x="170" y="77" text-anchor="middle" font-size="10.8">阶码 E（8 位，偏置 127）</text>
    <text class="fig-green" x="464" y="77" text-anchor="middle" font-size="10">尾数 M（23 位，规格化隐含前导 1：(-1)^S × 1.M × 2^(E-127)）</text>
    <rect class="fig-box-orange" x="30" y="102" width="52" height="38" rx="4"/>
    <rect class="fig-box-blue" x="82" y="102" width="214" height="38" rx="4"/>
    <rect class="fig-box-green" x="296" y="102" width="374" height="38" rx="4"/>
    <text class="fig-orange" x="56" y="125" text-anchor="middle" font-size="11">S (1)</text>
    <text class="fig-blue" x="189" y="125" text-anchor="middle" font-size="10.8">阶码 E（11 位，偏置 1023）</text>
    <text class="fig-green" x="483" y="125" text-anchor="middle" font-size="9.5">尾数 M（52 位，规格化隐含 1：(-1)^S × 1.M × 2^(E-1023)）</text>
    <rect class="fig-box" x="30" y="152" width="640" height="104" rx="5"/>
    <line class="fig-divider" x1="30" y1="178" x2="670" y2="178"/>
    <line class="fig-divider" x1="158" y1="152" x2="158" y2="256"/>
    <line class="fig-divider" x1="286" y1="152" x2="286" y2="256"/>
    <text class="fig-accent" x="94" y="169" text-anchor="middle" font-size="10.5">阶码全 0 / 全 1</text>
    <text class="fig-accent" x="222" y="169" text-anchor="middle" font-size="10.5">尾数 M</text>
    <text class="fig-accent" x="478" y="169" text-anchor="middle" font-size="10.5">真值含义（以单精度偏置 127 为例）</text>
    <text class="fig-label" x="94" y="196" text-anchor="middle" font-size="10.5">E = 0（全 0）</text>
    <text class="fig-label" x="222" y="196" text-anchor="middle" font-size="10.5">M = 0 / M ≠ 0</text>
    <text class="fig-muted" x="296" y="196" font-size="9.6">M = 0 表示 ±0；M ≠ 0 表示非规格化数 (-1)^S × 0.M × 2^(-126)</text>
    <text class="fig-label" x="94" y="220" text-anchor="middle" font-size="10.5">1 ≤ E ≤ 254</text>
    <text class="fig-label" x="222" y="220" text-anchor="middle" font-size="10.5">任意 M</text>
    <text class="fig-green" x="296" y="220" font-size="10">规格化数：(-1)^S × 1.M × 2^(E-127)</text>
    <text class="fig-label" x="94" y="244" text-anchor="middle" font-size="10.5">E = 255（全 1）</text>
    <text class="fig-label" x="222" y="244" text-anchor="middle" font-size="10.5">M = 0 / M ≠ 0</text>
    <text class="fig-red" x="296" y="244" font-size="10">M = 0 表示 ±∞（溢出）；M ≠ 0 表示 NaN（非数）</text>
  </svg>
  <figcaption>图 6-8　IEEE 754 单精度与双精度浮点格式及特殊值编码规则</figcaption>
</figure>

### 6.9 浮点数的规格化

规格化的目的只有一个：<strong>让尾数的有效位数尽可能多</strong>，把有限字长的精度用满。不规格化时尾数小数点后可能出现多个前导 0，超出字长的低位被丢弃，精度严重受损。

$r=2$ 时规格化定义为尾数绝对值不小于 $1/2$：

$$
\frac{1}{2} \le |S| < 1
$$

即尾数最高数值位为 1。基数不同，条件随之变化：$r=4$ 时最高两位不能全为 0，$r=8$ 时最高三位不能全为 0，$r=16$ 时最高四位不能全为 0。

<strong>判断方法</strong>：原码与反码看最高数值位是否为 1（正数 `0.1xxx`、负数 `1.1xxx`）；补码看<strong>符号位与最高数值位是否相异</strong>（正数 `0.1xxx`、负数 `1.0xxx`），一个异或门即可判定，结果为 1 表示已规格化。

| 尾数符号 | 补码形式 | 判定 | 处理 |
|---|---|---|---|
| 正 | 0.1xxx | 规格化 | 无 |
| 负 | 1.0xxx | 规格化 | 无 |
| 正 | 0.0xxx | 非规格化 | 左规 |
| 负 | 1.1xxx | 非规格化 | 左规 |
| 溢出 | 01.xxx / 10.xxx | 尾数溢出 | 右规一次 |

<strong>两个特例</strong>：$S=-1/2$ 的补码为 `1.1000…0`，符号位与最高数值位同为 1，按上述规则被判为<strong>非规格化</strong>，实际上它是真值意义上的边界值，遇到时需按非规格化处理；$S=-1$ 的补码为 `1.000…0`，符号位与最高数值位相异，判为<strong>规格化</strong>，这也是 $-1$ 只能由补码表示的原因。

| 操作 | 尾数 | 阶码 | 适用 |
|---|---|---|---|
| 左规 | 左移 1 位（$r=2$） | 减 1 | 尾数前导位与符号位相同 |
| 右规 | 右移 1 位（$r=2$） | 加 1 | 尾数求和后出现 01/10 溢出 |

$r=4$ 时每次移 2 位、阶码仍只加减 1；$r=8$ 每次移 3 位；$r=16$ 每次移 4 位。左规可能连续多次，右规最多一次。

<figure class="cpd-figure not-content" id="lhw-w5-fig-9">
  <svg viewBox="0 0 700 264" role="img" aria-label="补码浮点尾数规格化判定与左规右规操作示意图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="236" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">补码尾数规格化判定（符号位与最高数值位异或为 1 即规格化）</text>
    <rect class="fig-box-green" x="30" y="50" width="150" height="36" rx="5"/>
    <rect class="fig-box-green" x="30" y="96" width="150" height="36" rx="5"/>
    <rect class="fig-box-orange" x="30" y="142" width="150" height="36" rx="5"/>
    <rect class="fig-box-red" x="30" y="188" width="150" height="36" rx="5"/>
    <text class="fig-green" x="105" y="73" text-anchor="middle" font-size="12.5">0.1 × × ×</text>
    <text class="fig-green" x="105" y="119" text-anchor="middle" font-size="12.5">1.0 × × ×</text>
    <text class="fig-orange" x="105" y="165" text-anchor="middle" font-size="11.2">0.0xxx / 1.1xxx</text>
    <text class="fig-red" x="105" y="211" text-anchor="middle" font-size="12.5">01.x / 10.x</text>
    <text class="fig-green" x="190" y="73" font-size="10.5">正数补码已规格化（无需移位）</text>
    <text class="fig-green" x="190" y="119" font-size="10.5">负数补码已规格化（含 -1 特例 1.000…0）</text>
    <text class="fig-orange" x="190" y="165" font-size="10.5">非规格化（含 -1/2 特例 1.100…0）→ 需左规</text>
    <text class="fig-red" x="190" y="211" font-size="10.5">双符号位尾数溢出 → 必须右规 1 次</text>
    <rect class="fig-box-blue" x="466" y="50" width="206" height="82" rx="6"/>
    <rect class="fig-box-purple" x="466" y="142" width="206" height="82" rx="6"/>
    <text class="fig-blue" x="569" y="72" text-anchor="middle" font-size="12">左规（可连续多次）</text>
    <text class="fig-label" x="476" y="94" font-size="10.5">· 尾数算术左移 1 位，低位补 0</text>
    <text class="fig-label" x="476" y="114" font-size="10.5">· 阶码减 1（r=4 时移 2 位阶-1）</text>
    <text class="fig-purple" x="569" y="164" text-anchor="middle" font-size="12">右规（最多只需 1 次）</text>
    <text class="fig-label" x="476" y="186" font-size="10.2">· 尾数右移 1 位，高位补第 1 符号位</text>
    <text class="fig-label" x="476" y="206" font-size="10.5">· 阶码加 1（超界则浮点上溢）</text>
  </svg>
  <figcaption>图 6-9　补码浮点尾数规格化判定规则与左规、右规操作对比</figcaption>
</figure>

> <strong>注意</strong>：右规意味着尾数绝对值已经大于等于 1，必须同时把阶码加 1，否则整个浮点数的值被缩小一半；若阶码加 1 后超界，就是浮点上溢。

### 6.10 定点乘法：原码一位乘法

笔算乘法要保存 $n$ 个部分积再统一相加，硬件无法承受。改进思路是<strong>边乘边加、只保留一个部分积</strong>：每得到一个部分积就右移一位，把已用过的乘数位移出。

原码乘法规则：<strong>符号位单独处理</strong>（$x_0\oplus y_0$），数值部分按绝对值相乘，因此是绝对值运算。递推式为

$$
z_{i+1} = 2^{-1}\left(z_i + y_{n-i}\,|x|\right), \quad z_0 = 0
$$

即根据乘数当前最低位决定“加 $|x|$”还是“加 0”，加完统一右移一位；共 $n$ 次加法、$n$ 次右移。

例：$x=-0.1101$，$y=+0.1011$，$|x|=0.1101$，乘数数值位 `1011`。

| 步骤 | 乘数最低位 | 操作 | 加后部分积 | 右移后部分积 | 乘数/乘积低位 |
|---|---|---|---|---|---|
| 初值 | — | — | — | 0.0000 | 1011 |
| 1 | 1 | $+0.1101$ | 0.1101 | 0.0110 | 1101 |
| 2 | 1 | $+0.1101$ | 1.0011 | 0.1001 | 1110 |
| 3 | 0 | $+0$ | 0.1001 | 0.0100 | 1111 |
| 4 | 1 | $+0.1101$ | 1.0001 | 0.1000 | 1111 |

结果数值部分为 $0.1000\,1111$，符号 $1\oplus0=1$，故 [x·y]原 = $1.10001111$。

<strong>三个特点</strong>：① 只对绝对值运算，符号位另行异或；② 右移是<strong>逻辑右移</strong>，左端补 0，看似符号位的那个 1 其实是数值位；③ 用<strong>移位次数</strong>判断结束（$n$ 次），不能用加法次数，因为乘数位为 0 时可跳过加法但移位不能省。

<figure class="cpd-figure not-content" id="lhw-w5-fig-10">
  <svg viewBox="0 0 700 286" role="img" aria-label="原码一位乘法硬件结构框图：被乘数X、加法器、ACC部分积、MQ乘数与计数器">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="258" rx="8"/>
    <path class="fig-line" d="M 154 78 H 188 V 114 H 214" marker-end="url(#lhw-w5-arr-10)"/>
    <path class="fig-line-accent" d="M 340 98 H 366 V 66 H 404" marker-end="url(#lhw-w5-arr-10)"/>
    <path class="fig-line" d="M 406 86 H 382 V 134 H 342" marker-end="url(#lhw-w5-arr-10)"/>
    <line class="fig-line-accent" x1="474" y1="92" x2="474" y2="146" marker-end="url(#lhw-w5-arr-10)"/>
    <path class="fig-line" d="M 474 198 V 226 H 342" marker-end="url(#lhw-w5-arr-10)"/>
    <line class="fig-line-dashed" x1="340" y1="226" x2="544" y2="226" marker-end="url(#lhw-w5-arr-10)"/>
    <rect class="fig-box-blue" x="30" y="54" width="124" height="48" rx="5"/>
    <rect class="fig-box-accent" x="216" y="76" width="124" height="84" rx="6"/>
    <rect class="fig-box-green" x="406" y="42" width="136" height="50" rx="5"/>
    <rect class="fig-box-green" x="406" y="148" width="136" height="50" rx="5"/>
    <rect class="fig-box" x="216" y="202" width="124" height="48" rx="5"/>
    <rect class="fig-box-purple" x="546" y="202" width="122" height="48" rx="24"/>
    <rect class="fig-box-orange" x="556" y="42" width="112" height="72" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="13">原码一位乘法硬件通路（边乘边加、只保留一个部分积）</text>
    <text class="fig-blue" x="92" y="74" text-anchor="middle" font-size="12">X（|x| 被乘数）</text>
    <text class="fig-muted" x="92" y="92" text-anchor="middle" font-size="10.5">计算中保持不变</text>
    <text class="fig-accent" x="278" y="108" text-anchor="middle" font-size="12.5">并行加法器</text>
    <text class="fig-label" x="278" y="132" text-anchor="middle" font-size="18">+</text>
    <text class="fig-muted" x="278" y="150" text-anchor="middle" font-size="10">加 |x| 或加 0</text>
    <text class="fig-green" x="474" y="63" text-anchor="middle" font-size="12">ACC 部分积</text>
    <text class="fig-muted" x="474" y="81" text-anchor="middle" font-size="10.5">最高位存进位（非符号）</text>
    <text class="fig-green" x="474" y="169" text-anchor="middle" font-size="12">MQ 乘数</text>
    <text class="fig-muted" x="474" y="187" text-anchor="middle" font-size="10.5">接收 ACC 移出的低位</text>
    <text class="fig-muted" x="484" y="124" font-size="10.5">逻辑右移 1 位</text>
    <text class="fig-label" x="278" y="222" text-anchor="middle" font-size="11.5">移位与加控制</text>
    <text class="fig-muted" x="278" y="239" text-anchor="middle" font-size="10.5">由 MQ 最低位控制</text>
    <text class="fig-purple" x="607" y="222" text-anchor="middle" font-size="12">移位计数器</text>
    <text class="fig-muted" x="607" y="239" text-anchor="middle" font-size="10.5">计满 n 次结束</text>
    <text class="fig-orange" x="612" y="66" text-anchor="middle" font-size="12">符号异或门</text>
    <text class="fig-label" x="612" y="86" text-anchor="middle" font-size="11">Pf = x0 ⊕ y0</text>
    <text class="fig-muted" x="612" y="104" text-anchor="middle" font-size="10">符号单独生成</text>
  </svg>
  <figcaption>图 6-10　原码一位乘法硬件结构与符号异或通路</figcaption>
</figure>

> <strong>注意：ACC 的最高位保存的是数值相加产生的进位，不是符号位</strong>；原码乘法全程没有符号参与，符号位由异或门单独产生。

<figure class="cpd-figure not-content" id="lhw-w5-fig-11">
  <svg viewBox="0 0 700 304" role="img" aria-label="补码一位乘法Booth算法递推规则表与x=-0.1101乘y=+0.1011步进跟踪图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="276" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">补码一位乘法（Booth 算法）规则与步进跟踪</text>
    <rect class="fig-box" x="28" y="48" width="242" height="128" rx="6"/>
    <line class="fig-divider" x1="28" y1="74" x2="270" y2="74"/>
    <line class="fig-divider" x1="108" y1="48" x2="108" y2="176"/>
    <text class="fig-accent" x="68" y="66" text-anchor="middle" font-size="11">yi  yi+1</text>
    <text class="fig-accent" x="189" y="66" text-anchor="middle" font-size="11">操作（双符号位）</text>
    <text class="fig-label" x="68" y="96" text-anchor="middle" font-size="11">0   0</text>
    <text class="fig-muted" x="189" y="96" text-anchor="middle" font-size="11">+0，算术右移 1 位</text>
    <text class="fig-blue" x="68" y="120" text-anchor="middle" font-size="11">0   1</text>
    <text class="fig-blue" x="189" y="120" text-anchor="middle" font-size="11">+[x]补，算术右移 1 位</text>
    <text class="fig-orange" x="68" y="144" text-anchor="middle" font-size="11">1   0</text>
    <text class="fig-orange" x="189" y="144" text-anchor="middle" font-size="11">+[-x]补，算术右移 1 位</text>
    <text class="fig-label" x="68" y="168" text-anchor="middle" font-size="11">1   1</text>
    <text class="fig-muted" x="189" y="168" text-anchor="middle" font-size="11">+0，算术右移 1 位</text>
    <rect class="fig-box-blue" x="28" y="188" width="242" height="84" rx="6"/>
    <text class="fig-blue" x="38" y="208" font-size="11.2">例：x = -0.1101，y = +0.1011</text>
    <text class="fig-label" x="38" y="226" font-size="10.5">[x]补 = 11.0011，[-x]补 = 00.1101</text>
    <text class="fig-label" x="38" y="244" font-size="10.5">[y]补 = 0.1011，附加位 y5 = 0</text>
    <text class="fig-muted" x="38" y="262" font-size="9.6">共 n 次右移、n+1 次累加（末步不移位）</text>
    <rect class="fig-box" x="282" y="48" width="390" height="224" rx="6"/>
    <line class="fig-divider" x1="282" y1="74" x2="672" y2="74"/>
    <text class="fig-accent" x="304" y="66" text-anchor="middle" font-size="11">步</text>
    <text class="fig-accent" x="354" y="66" text-anchor="middle" font-size="11">判断位</text>
    <text class="fig-accent" x="466" y="66" text-anchor="middle" font-size="11">部分积（ACC）</text>
    <text class="fig-accent" x="584" y="66" text-anchor="middle" font-size="11">乘数 MQ</text>
    <text class="fig-accent" x="646" y="66" text-anchor="middle" font-size="11">附加位</text>
    <text class="fig-muted" x="304" y="96" text-anchor="middle" font-size="11">初</text>
    <text class="fig-muted" x="354" y="96" text-anchor="middle" font-size="11">—</text>
    <text class="fig-label" x="466" y="96" text-anchor="middle" font-size="11">00.0000</text>
    <text class="fig-label" x="584" y="96" text-anchor="middle" font-size="11">0.1011</text>
    <text class="fig-label" x="646" y="96" text-anchor="middle" font-size="11">0</text>
    <text class="fig-label" x="304" y="124" text-anchor="middle" font-size="11">1</text>
    <text class="fig-orange" x="354" y="124" text-anchor="middle" font-size="11">10 → -x</text>
    <text class="fig-label" x="466" y="124" text-anchor="middle" font-size="11">00.1101 → 00.0110</text>
    <text class="fig-label" x="584" y="124" text-anchor="middle" font-size="11">10.101</text>
    <text class="fig-label" x="646" y="124" text-anchor="middle" font-size="11">1</text>
    <text class="fig-label" x="304" y="152" text-anchor="middle" font-size="11">2</text>
    <text class="fig-muted" x="354" y="152" text-anchor="middle" font-size="11">11 → +0</text>
    <text class="fig-label" x="466" y="152" text-anchor="middle" font-size="11">00.0110 → 00.0011</text>
    <text class="fig-label" x="584" y="152" text-anchor="middle" font-size="11">010.10</text>
    <text class="fig-label" x="646" y="152" text-anchor="middle" font-size="11">1</text>
    <text class="fig-label" x="304" y="180" text-anchor="middle" font-size="11">3</text>
    <text class="fig-blue" x="354" y="180" text-anchor="middle" font-size="11">01 → +x</text>
    <text class="fig-label" x="466" y="180" text-anchor="middle" font-size="11">11.0110 → 11.1011</text>
    <text class="fig-label" x="584" y="180" text-anchor="middle" font-size="11">0010.1</text>
    <text class="fig-label" x="646" y="180" text-anchor="middle" font-size="11">0</text>
    <text class="fig-label" x="304" y="208" text-anchor="middle" font-size="11">4</text>
    <text class="fig-orange" x="354" y="208" text-anchor="middle" font-size="11">10 → -x</text>
    <text class="fig-label" x="466" y="208" text-anchor="middle" font-size="11">00.1000 → 00.0100</text>
    <text class="fig-label" x="584" y="208" text-anchor="middle" font-size="11">00010.</text>
    <text class="fig-label" x="646" y="208" text-anchor="middle" font-size="11">1</text>
    <text class="fig-label" x="304" y="236" text-anchor="middle" font-size="11">5</text>
    <text class="fig-blue" x="354" y="236" text-anchor="middle" font-size="11">01 → +x</text>
    <text class="fig-green" x="466" y="236" text-anchor="middle" font-size="11">11.0111（不移位）</text>
    <text class="fig-green" x="584" y="236" text-anchor="middle" font-size="11">0001</text>
    <text class="fig-muted" x="646" y="236" text-anchor="middle" font-size="11">—</text>
    <text class="fig-green" x="477" y="256" text-anchor="middle" font-size="11">[x·y]补 = 1.0111 0001（真值 -0.10001111）</text>
  </svg>
  <figcaption>图 6-11　补码一位乘法（Booth 算法）递推规则与步进跟踪示例</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w5-fig-12">
  <svg viewBox="0 0 700 256" role="img" aria-label="定点原码除法恢复余数法与加减交替法（不恢复余数法）对比流程图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="324" height="228" rx="8"/>
    <rect class="fig-box-subtle" x="356" y="14" width="328" height="228" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">恢复余数法（余数负时多一步加回）</text>
    <text class="fig-title" x="370" y="34" font-size="13">加减交替法（不恢复余数，硬件更规则）</text>
    <line class="fig-line" x1="178" y1="88" x2="178" y2="108" marker-end="url(#lhw-w5-arr-12)"/>
    <path class="fig-line-accent" d="M 130 130 H 92 V 156" marker-end="url(#lhw-w5-arr-12)"/>
    <path class="fig-line" d="M 226 130 H 264 V 156" marker-end="url(#lhw-w5-arr-12)"/>
    <path class="fig-line-accent" d="M 468 102 H 430 V 136" marker-end="url(#lhw-w5-arr-12)"/>
    <path class="fig-line" d="M 564 102 H 602 V 136" marker-end="url(#lhw-w5-arr-12)"/>
    <rect class="fig-box" x="108" y="52" width="140" height="36" rx="5"/>
    <rect class="fig-box-blue" x="108" y="110" width="140" height="36" rx="5"/>
    <rect class="fig-box-green" x="30" y="158" width="124" height="44" rx="5"/>
    <rect class="fig-box-red" x="202" y="158" width="124" height="44" rx="5"/>
    <rect class="fig-box-blue" x="446" y="80" width="140" height="38" rx="5"/>
    <rect class="fig-box-green" x="370" y="138" width="136" height="52" rx="5"/>
    <rect class="fig-box-orange" x="534" y="138" width="136" height="52" rx="5"/>
    <text class="fig-label" x="178" y="75" text-anchor="middle" font-size="11.5">R ← R - |y|（试减）</text>
    <text class="fig-blue" x="178" y="132" text-anchor="middle" font-size="11.5">判断余数 R 符号</text>
    <text class="fig-green" x="92" y="177" text-anchor="middle" font-size="11">R ≥ 0：上商 1</text>
    <text class="fig-muted" x="92" y="194" text-anchor="middle" font-size="10">直接左移 1 位</text>
    <text class="fig-red" x="264" y="177" text-anchor="middle" font-size="11">R &lt; 0：上商 0</text>
    <text class="fig-muted" x="264" y="194" text-anchor="middle" font-size="10">先 +|y| 恢复再左移</text>
    <text class="fig-muted" x="178" y="228" text-anchor="middle" font-size="10.5">共左移 n 次；末步若 R&lt;0 仍需恢复</text>
    <text class="fig-blue" x="516" y="103" text-anchor="middle" font-size="11.5">判当前余数 R 符号</text>
    <text class="fig-green" x="438" y="158" text-anchor="middle" font-size="11">R ≥ 0：上商 1</text>
    <text class="fig-muted" x="438" y="177" text-anchor="middle" font-size="10.5">左移 1 位，再 -|y|</text>
    <text class="fig-orange" x="602" y="158" text-anchor="middle" font-size="11">R &lt; 0：上商 0</text>
    <text class="fig-muted" x="602" y="177" text-anchor="middle" font-size="10.5">左移 1 位，再 +|y|</text>
    <text class="fig-muted" x="520" y="212" text-anchor="middle" font-size="10.5">等价于 2R + |y| = 2(R+|y|)-|y|</text>
    <text class="fig-muted" x="520" y="228" text-anchor="middle" font-size="10.5">每步固定“移位+加/减”各 1 次</text>
  </svg>
  <figcaption>图 6-12　定点原码除法：恢复余数法与加减交替法对比流程</figcaption>
</figure>

### 6.11 浮点加减运算

浮点加减固定五步：

1. <strong>对阶</strong>：用补码减法求阶差 $\Delta j=j_x-j_y$，小阶向大阶看齐，小阶的尾数右移 $|\Delta j|$ 位、阶码加至与大阶相同。
2. <strong>尾数求和</strong>：阶码一致后，尾数按补码定点加减法相加（双符号位便于判溢出）。
3. <strong>规格化</strong>：结果非规格化则左规；尾数出现 01/10 溢出则右规一次。
4. <strong>舍入</strong>：右规与对阶都会移出低位，用截断法（直接丢弃）、0 舍 1 入法（移出最高位为 1 则末位加 1）或末位恒置 1 法处理。
5. <strong>溢出判断</strong>：看阶码是否超出范围，而不是看尾数。

<figure class="cpd-figure not-content" id="lhw-w5-fig-13">
  <svg viewBox="0 0 700 292" role="img" aria-label="浮点加减运算五步流程与0.1101乘2的2次方加0.1011乘2的1次方算例跟踪图">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="264" rx="8"/>
    <line class="fig-line" x1="148" y1="90" x2="164" y2="90" marker-end="url(#lhw-w5-arr-13)"/>
    <line class="fig-line" x1="278" y1="90" x2="294" y2="90" marker-end="url(#lhw-w5-arr-13)"/>
    <line class="fig-line" x1="408" y1="90" x2="424" y2="90" marker-end="url(#lhw-w5-arr-13)"/>
    <line class="fig-line" x1="538" y1="90" x2="554" y2="90" marker-end="url(#lhw-w5-arr-13)"/>
    <rect class="fig-box-blue" x="30" y="52" width="118" height="76" rx="6"/>
    <rect class="fig-box" x="166" y="52" width="112" height="76" rx="6"/>
    <rect class="fig-box-green" x="296" y="52" width="112" height="76" rx="6"/>
    <rect class="fig-box-purple" x="426" y="52" width="112" height="76" rx="6"/>
    <rect class="fig-box-red" x="556" y="52" width="114" height="76" rx="6"/>
    <text class="fig-title" x="30" y="34" font-size="13">浮点加减五步固定流程（附算例轨迹）</text>
    <text class="fig-blue" x="89" y="74" text-anchor="middle" font-size="12">① 对阶</text>
    <text class="fig-muted" x="89" y="94" text-anchor="middle" font-size="10.5">Δj = jx - jy</text>
    <text class="fig-muted" x="89" y="112" text-anchor="middle" font-size="10.5">小阶尾数右移</text>
    <text class="fig-label" x="222" y="74" text-anchor="middle" font-size="12">② 尾数求和</text>
    <text class="fig-muted" x="222" y="94" text-anchor="middle" font-size="10.5">补码定点加法</text>
    <text class="fig-muted" x="222" y="112" text-anchor="middle" font-size="10.5">双符号位判溢</text>
    <text class="fig-green" x="352" y="74" text-anchor="middle" font-size="12">③ 规格化</text>
    <text class="fig-muted" x="352" y="94" text-anchor="middle" font-size="10.5">左规 / 右规</text>
    <text class="fig-muted" x="352" y="112" text-anchor="middle" font-size="10.5">阶码同步增减</text>
    <text class="fig-purple" x="482" y="74" text-anchor="middle" font-size="12">④ 舍入</text>
    <text class="fig-muted" x="482" y="94" text-anchor="middle" font-size="10.5">截断 / 0舍1入</text>
    <text class="fig-muted" x="482" y="112" text-anchor="middle" font-size="10.5">末位恒置 1</text>
    <text class="fig-red" x="613" y="74" text-anchor="middle" font-size="12">⑤ 溢出判断</text>
    <text class="fig-muted" x="613" y="94" text-anchor="middle" font-size="10.5">看阶码双符号</text>
    <text class="fig-muted" x="613" y="112" text-anchor="middle" font-size="10.5">非看尾数</text>
    <rect class="fig-box" x="30" y="142" width="640" height="66" rx="5"/>
    <text class="fig-accent" x="42" y="162" font-size="11">算例跟踪：x = 0.1101×2^2，y = 0.1011×2^1（阶码 3 位、尾数 6 位，双符号位）</text>
    <text class="fig-label" x="42" y="181" font-size="10.2">① 对阶：y 右移 → 00.010110　→　② 求和得 01.001010（尾数正溢）</text>
    <text class="fig-label" x="42" y="198" font-size="10.2">③ 右规得 00.100101（阶 00,011）　→　④ 舍入不变　→　⑤ 阶码正常，无溢出</text>
    <text class="fig-red" x="30" y="232" font-size="11.5">· 阶码双符号位 01：浮点上溢（转出错中断处理）</text>
    <text class="fig-blue" x="30" y="254" font-size="11.5">· 阶码双符号位 10：浮点下溢（按机器零处理，不算错误）</text>
  </svg>
  <figcaption>图 6-13　浮点加减五步流程与算例跟踪（阶码溢出规则）</figcaption>
</figure>

例：$x=0.1101\times2^{10}$，$y=0.1011\times2^{01}$（阶码写作二进制，即 $x=0.1101\times2^{2}$、$y=0.1011\times2^{1}$），阶码 3 位、尾数 6 位，均用补码、双符号位。

| 步骤 | 计算 | 结果 |
|---|---|---|
| ① 对阶 | 阶差 $=00{,}010-00{,}001=00{,}001$ | $y$ 阶码加 1，尾数右移：$00.101100\to00.010110$ |
| ② 求和 | $00.110100+00.010110$ | $01.001010$，符号位 01，尾数正溢出 |
| ③ 右规 | 尾数右移 1 位，阶码加 1 | $00.100101$，阶码 $00{,}011$ |
| ④ 舍入 | 移出位为 0，截断即可 | 不变 |
| ⑤ 判溢出 | 阶码未越界 | $x+y=0.100101\times2^{3}$ |

校验：$3.25+1.375=4.625=0.100101_2\times2^{3}$。

减法例（$x-y$ 且 $x$ 阶小于 $y$ 阶）：对阶时改为 $x$ 的尾数右移、阶码加 1；尾数求和时把减法转成加 [-y]补，结果为 `10.x`（负向溢出），再右规一次、阶码加 1。

### 6.12 算术逻辑单元与快速进位链

ALU（Arithmetic Logic Unit）是定点与浮点运算部件的统称，与控制器一起构成 CPU。74181 的具体功能表见教材。

<strong>外特性</strong>：输入端 $A_i$、$B_i$，控制端 $K$ 选择运算种类，输出端 $F$。ALU 是<strong>组合逻辑电路，没有记忆功能</strong>——输入撤消，结果随即消失，因此输入、输出两端都必须接寄存器。

74181 是 4 位 ALU：$M$ 为低电平时做算术运算，高电平时做逻辑运算；$S_3\sim S_0$ 的不同组合选择 16 种运算。

<figure class="cpd-figure not-content" id="lhw-w5-fig-14">
  <svg viewBox="0 0 700 236" role="img" aria-label="ALU算术逻辑单元外特性框图：Ai、Bi输入、K控制端与F输出端">
    <defs>
      <marker id="lhw-w5-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 class="fig-box-subtle" x="16" y="14" width="668" height="208" rx="8"/>
    <line class="fig-line" x1="142" y1="84" x2="232" y2="96" marker-end="url(#lhw-w5-arr-14)"/>
    <line class="fig-line" x1="142" y1="152" x2="232" y2="140" marker-end="url(#lhw-w5-arr-14)"/>
    <line class="fig-line-accent" x1="350" y1="42" x2="350" y2="59" marker-end="url(#lhw-w5-arr-14)"/>
    <line class="fig-line-accent" x1="466" y1="114" x2="554" y2="114" marker-end="url(#lhw-w5-arr-14)"/>
    <rect class="fig-box-blue" x="48" y="64" width="94" height="38" rx="5"/>
    <rect class="fig-box-blue" x="48" y="132" width="94" height="38" rx="5"/>
    <polygon class="fig-box-accent" points="234,68 466,54 466,174 234,160"/>
    <rect class="fig-box-green" x="556" y="92" width="110" height="44" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="13">ALU 外特性（74181 组合逻辑框图）</text>
    <text class="fig-blue" x="95" y="88" text-anchor="middle" font-size="12">输入 Ai</text>
    <text class="fig-blue" x="95" y="156" text-anchor="middle" font-size="12">输入 Bi</text>
    <text class="fig-accent" x="350" y="34" text-anchor="middle" font-size="12">控制端 K（M、S3~S0）</text>
    <text class="fig-accent" x="350" y="110" text-anchor="middle" font-size="18">ALU</text>
    <text class="fig-muted" x="350" y="134" text-anchor="middle" font-size="11">纯组合逻辑电路，无记忆功能</text>
    <text class="fig-green" x="611" y="118" text-anchor="middle" font-size="12.5">输出 F</text>
    <text class="fig-muted" x="30" y="206" font-size="11">· 输入撤消则输出立即消失，因此输入/输出端必须接寄存器；74181 中 M=0 算术运算，M=1 逻辑运算</text>
  </svg>
  <figcaption>图 6-14　算术逻辑单元（ALU）外特性与控制端口示意图</figcaption>
</figure>

<strong>全加器与进位</strong>：

$$
S_i = A_i \oplus B_i \oplus C_{i-1}
$$

$$
C_i = A_iB_i + (A_i+B_i)C_{i-1} = D_i + T_iC_{i-1}
$$

其中 $D_i=A_iB_i$ 为<strong>本地进位</strong>（只由本位两个操作数决定），$T_i=A_i+B_i$ 为<strong>传送条件</strong>（为 1 时低位进位被传上来）。和 $S_i$ 必须等 $C_{i-1}$ 到达才能算出，因此<strong>进位链是加法器的速度瓶颈</strong>。

<strong>串行进位</strong>：按 $C_0=D_0+T_0C_{-1}$、$C_1=D_1+T_1C_0$ 逐级串联，每级两个与非门、延迟 $2t_y$：

$$
t_{\mathrm{serial}} = 2n\,t_y
$$

4 位全加器产生 $C_0\sim C_3$ 需 $8t_y$，16 位需 $32t_y$，32 位需 $64t_y$。

<figure class="cpd-figure not-content" id="lhw-w5-fig-15">
  <svg viewBox="0 0 700 264" role="img" aria-label="4位先行进位CLA逻辑结构与串行进位链延迟对比图">
    <defs>
      <marker id="lhw-w5-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="14" width="320" height="236" rx="8"/>
    <rect class="fig-box-subtle" x="348" y="14" width="336" height="236" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">串行进位链（4 位需 4×2ty = 8ty）</text>
    <text class="fig-title" x="362" y="34" font-size="13">4 位先行进位 CLA（仅需 2.5ty）</text>
    <line class="fig-line" x1="94" y1="86" x2="112" y2="86" marker-end="url(#lhw-w5-arr-15)"/>
    <line class="fig-line" x1="164" y1="86" x2="182" y2="86" marker-end="url(#lhw-w5-arr-15)"/>
    <line class="fig-line" x1="234" y1="86" x2="252" y2="86" marker-end="url(#lhw-w5-arr-15)"/>
    <line class="fig-line" x1="304" y1="86" x2="322" y2="86" marker-end="url(#lhw-w5-arr-15)"/>
    <rect class="fig-box" x="42" y="64" width="52" height="44" rx="4"/>
    <rect class="fig-box" x="114" y="64" width="50" height="44" rx="4"/>
    <rect class="fig-box" x="184" y="64" width="50" height="44" rx="4"/>
    <rect class="fig-box" x="254" y="64" width="50" height="44" rx="4"/>
    <text class="fig-label" x="68" y="90" text-anchor="middle" font-size="11">FA0</text>
    <text class="fig-label" x="139" y="90" text-anchor="middle" font-size="11">FA1</text>
    <text class="fig-label" x="209" y="90" text-anchor="middle" font-size="11">FA2</text>
    <text class="fig-label" x="279" y="90" text-anchor="middle" font-size="11">FA3</text>
    <text class="fig-muted" x="103" y="78" text-anchor="middle" font-size="9.5">C0</text>
    <text class="fig-muted" x="173" y="78" text-anchor="middle" font-size="9.5">C1</text>
    <text class="fig-muted" x="243" y="78" text-anchor="middle" font-size="9.5">C2</text>
    <text class="fig-muted" x="313" y="78" text-anchor="middle" font-size="9.5">C3</text>
    <rect class="fig-box-red" x="30" y="132" width="292" height="102" rx="6"/>
    <text class="fig-red" x="44" y="154" font-size="11.5">瓶颈：高位必须逐级等待低位进位</text>
    <text class="fig-label" x="44" y="176" font-size="11">· Ci = Di + Ti·Ci-1，每级 2 个与非门（2ty）</text>
    <text class="fig-label" x="44" y="196" font-size="11">· 16 位需 32ty，32 位需 64ty</text>
    <text class="fig-muted" x="44" y="216" font-size="10.5">· 其中 Di = Ai·Bi，Ti = Ai + Bi</text>
    <line class="fig-line-accent" x1="472" y1="88" x2="508" y2="88" marker-end="url(#lhw-w5-arr-15)"/>
    <line class="fig-line-accent" x1="472" y1="126" x2="508" y2="126" marker-end="url(#lhw-w5-arr-15)"/>
    <line class="fig-line-accent" x1="472" y1="164" x2="508" y2="164" marker-end="url(#lhw-w5-arr-15)"/>
    <line class="fig-line-accent" x1="472" y1="202" x2="508" y2="202" marker-end="url(#lhw-w5-arr-15)"/>
    <rect class="fig-box-blue" x="364" y="64" width="108" height="156" rx="6"/>
    <rect class="fig-box-green" x="510" y="64" width="160" height="156" rx="6"/>
    <text class="fig-blue" x="418" y="90" text-anchor="middle" font-size="12">并行展开</text>
    <text class="fig-muted" x="418" y="114" text-anchor="middle" font-size="10.5">全部 Di, Ti</text>
    <text class="fig-muted" x="418" y="132" text-anchor="middle" font-size="10.5">与 C-1 同时输入</text>
    <text class="fig-muted" x="418" y="156" text-anchor="middle" font-size="10.5">两级与或逻辑</text>
    <text class="fig-accent" x="418" y="180" text-anchor="middle" font-size="11">同时输出</text>
    <text class="fig-green" x="590" y="92" text-anchor="middle" font-size="9.6">C0 = D0 + T0·C-1</text>
    <text class="fig-green" x="590" y="130" text-anchor="middle" font-size="9.6">C1 = D1 + T1D0 + T1T0C-1</text>
    <text class="fig-green" x="590" y="168" text-anchor="middle" font-size="9.6">C2 = D2 + T2D1 + …</text>
    <text class="fig-green" x="590" y="206" text-anchor="middle" font-size="9.6">C3 = D3 + T3D2 + …</text>
  </svg>
  <figcaption>图 6-15　串行进位链与 4 位先行进位（CLA）并行展开对比</figcaption>
</figure>

<strong>分组跳跃进位</strong>：把 $n$ 位分成若干小组，每组先由本地输入并行算出“小组本地进位 $D$”与“小组传送条件 $T$”，再用一层组内 CLA 并行产生组内各进位，组间再按同样方式跳跃。双重分组即“小组 → 大组”两级 CLA。

<figure class="cpd-figure not-content" id="lhw-w5-fig-16">
  <svg viewBox="0 0 700 284" role="img" aria-label="双重分组跳跃进位链结构图：小组产生D和T送第二层CLA并行输出C3、C7、C11、C15">
    <defs>
      <marker id="lhw-w5-arr-16" 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="14" width="668" height="256" rx="8"/>
    <path class="fig-line" d="M 180 68 H 212 V 126 H 242" marker-end="url(#lhw-w5-arr-16)"/>
    <path class="fig-line" d="M 180 118 H 212 V 136 H 242" marker-end="url(#lhw-w5-arr-16)"/>
    <path class="fig-line" d="M 180 168 H 212 V 146 H 242" marker-end="url(#lhw-w5-arr-16)"/>
    <path class="fig-line" d="M 180 218 H 212 V 156 H 242" marker-end="url(#lhw-w5-arr-16)"/>
    <line class="fig-line-accent" x1="398" y1="142" x2="440" y2="142" marker-end="url(#lhw-w5-arr-16)"/>
    <rect class="fig-box" x="26" y="48" width="154" height="40" rx="5"/>
    <rect class="fig-box" x="26" y="98" width="154" height="40" rx="5"/>
    <rect class="fig-box" x="26" y="148" width="154" height="40" rx="5"/>
    <rect class="fig-box" x="26" y="198" width="154" height="40" rx="5"/>
    <rect class="fig-box-blue" x="244" y="96" width="154" height="92" rx="6"/>
    <rect class="fig-box-green" x="442" y="96" width="228" height="92" rx="6"/>
    <text class="fig-title" x="28" y="34" font-size="13">双重分组跳跃进位（小组 → 大组两级 CLA）</text>
    <text class="fig-label" x="103" y="65" text-anchor="middle" font-size="10">第 5 小组（bits 12~15）</text>
    <text class="fig-muted" x="103" y="81" text-anchor="middle" font-size="9.5">产出 D5*、T5*</text>
    <text class="fig-label" x="103" y="115" text-anchor="middle" font-size="10">第 6 小组（bits 8~11）</text>
    <text class="fig-muted" x="103" y="131" text-anchor="middle" font-size="9.5">产出 D6*、T6*</text>
    <text class="fig-label" x="103" y="165" text-anchor="middle" font-size="10">第 7 小组（bits 4~7）</text>
    <text class="fig-muted" x="103" y="181" text-anchor="middle" font-size="9.5">产出 D7*、T7*</text>
    <text class="fig-label" x="103" y="215" text-anchor="middle" font-size="10">第 8 小组（bits 0~3）</text>
    <text class="fig-muted" x="103" y="231" text-anchor="middle" font-size="9.5">产出 D8*、T8*</text>
    <text class="fig-blue" x="321" y="128" text-anchor="middle" font-size="12">第二层 CLA</text>
    <text class="fig-muted" x="321" y="148" text-anchor="middle" font-size="10">输入各组 D*、T*</text>
    <text class="fig-muted" x="321" y="164" text-anchor="middle" font-size="10">与 C-1 并行生成</text>
    <text class="fig-green" x="556" y="126" text-anchor="middle" font-size="12">C3、C7、C11、C15</text>
    <text class="fig-label" x="556" y="148" text-anchor="middle" font-size="10.8">各小组最高位进位同时产生</text>
    <text class="fig-muted" x="556" y="168" text-anchor="middle" font-size="9.5">不再逐级等待（16 位 7.5ty，32 位 10ty）</text>
    <text class="fig-muted" x="30" y="256" font-size="10">时间线：2.5ty 出各小组 D*/T*（与 C-1 无关）→ 5ty 出组间进位 C3/C7/C11/C15 → 7.5ty 出全部组内进位</text>
  </svg>
  <figcaption>图 6-16　双重分组跳跃进位链（两级 CLA 并行生成组间进位）</figcaption>
</figure>

<strong>时间对比</strong>：

| 进位结构 | 16 位 | 32 位 |
|---|---|---|
| 串行进位链 | $32t_y$ | $64t_y$ |
| 单重分组跳跃 | $10t_y$ | — |
| 双重分组跳跃 | $7.5t_y$ | $10t_y$ |

32 位双重分组的时间线：$2.5t_y$ 并行产生各小组的 $D^{\ast}/T^{\ast}$（与 $C_{-1}$ 无关）；$5t_y$ 经第二层 CLA 产生第二大组各小组最高位进位 $C_3,C_7,C_{11},C_{15}$；$7.5t_y$ 产生第二大组全部组内进位，并由 $C_{15}$ 向第一大组（$C_{16}$ 起）送入进位；$10t_y$ 全部 32 位进位产生。相比串行的 $64t_y$，提速约 6 倍。

> <strong>★考点</strong>：进位链延迟按“每级与非门 $2t_y$、CLA 每层 $2.5t$”累计；分组跳跃的实质是让 $D$、$T$ 只依赖本地操作数，从而把进位计算从“串行等待”变成“并行预计算”。

## 第7章 指令系统

### 7.1 机器指令与指令格式

<strong>机器指令</strong>是 CPU 能够直接识别并执行的操作命令；一个处理器能执行的全部机器指令构成<strong>指令集</strong>。指令系统是<strong>软件与硬件的交界面</strong>：软件通过指令告诉硬件做什么，硬件通过指令把运算结果和状态返回给软件。

指令格式抽象成两部分：<strong>操作码字段</strong>（做什么操作）+ <strong>地址码字段</strong>（对谁操作）。

<figure class="cpd-figure not-content" id="lhw-w5-fig-17">
  <svg viewBox="0 0 700 286" role="img" aria-label="指令格式抽象及三地址、二地址、一地址、零地址指令对比示意图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="258" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">指令格式抽象与按地址个数分类（三 / 二 / 一 / 零地址）</text>
    <rect class="fig-box" x="108" y="48" width="136" height="30" rx="4"/>
    <rect class="fig-box-blue" x="244" y="48" width="106" height="30" rx="4"/>
    <rect class="fig-box-blue" x="350" y="48" width="106" height="30" rx="4"/>
    <rect class="fig-box-blue" x="456" y="48" width="106" height="30" rx="4"/>
    <text class="fig-label" x="176" y="68" text-anchor="middle" font-size="12">操作码 OP</text>
    <text class="fig-blue" x="297" y="68" text-anchor="middle" font-size="12">地址码 A1</text>
    <text class="fig-blue" x="403" y="68" text-anchor="middle" font-size="12">地址码 A2</text>
    <text class="fig-blue" x="509" y="68" text-anchor="middle" font-size="12">地址码 A3</text>
    <text class="fig-label" x="30" y="114" font-size="12">三地址</text>
    <rect class="fig-box-accent" x="108" y="96" width="64" height="26" rx="3"/>
    <rect class="fig-box-blue" x="172" y="96" width="84" height="26" rx="3"/>
    <rect class="fig-box-blue" x="256" y="96" width="84" height="26" rx="3"/>
    <rect class="fig-box-blue" x="340" y="96" width="84" height="26" rx="3"/>
    <text class="fig-accent" x="140" y="113" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-label" x="214" y="113" text-anchor="middle" font-size="11.5">A1</text>
    <text class="fig-label" x="298" y="113" text-anchor="middle" font-size="11.5">A2</text>
    <text class="fig-label" x="382" y="113" text-anchor="middle" font-size="11.5">A3</text>
    <text class="fig-muted" x="440" y="113" font-size="11">(A1) op (A2) → A3（不破坏源操作数）</text>
    <text class="fig-label" x="30" y="150" font-size="12">二地址</text>
    <rect class="fig-box-accent" x="108" y="132" width="64" height="26" rx="3"/>
    <rect class="fig-box-blue" x="172" y="132" width="110" height="26" rx="3"/>
    <rect class="fig-box-blue" x="282" y="132" width="110" height="26" rx="3"/>
    <text class="fig-accent" x="140" y="149" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-label" x="227" y="149" text-anchor="middle" font-size="11.5">A1</text>
    <text class="fig-label" x="337" y="149" text-anchor="middle" font-size="11.5">A2</text>
    <text class="fig-muted" x="440" y="149" font-size="11">(A1) op (A2) → A1（结果覆盖 A1）</text>
    <text class="fig-label" x="30" y="186" font-size="12">一地址</text>
    <rect class="fig-box-accent" x="108" y="168" width="64" height="26" rx="3"/>
    <rect class="fig-box-blue" x="172" y="168" width="148" height="26" rx="3"/>
    <text class="fig-accent" x="140" y="185" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-label" x="246" y="185" text-anchor="middle" font-size="11.5">A1</text>
    <text class="fig-muted" x="440" y="185" font-size="11">(ACC) op (A1) → ACC（另一数隐含）</text>
    <text class="fig-label" x="30" y="222" font-size="12">零地址</text>
    <rect class="fig-box-accent" x="108" y="204" width="64" height="26" rx="3"/>
    <text class="fig-accent" x="140" y="221" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-muted" x="440" y="221" font-size="11">操作数隐含（栈顶 / ACC）或无操作数（NOP）</text>
    <text class="fig-muted" x="30" y="254" font-size="11">减少地址码靠隐含约定（ACC、SP 栈顶），目的是缩短指令字长、减少访存次数</text>
  </svg>
  <figcaption>图 7-1　指令格式抽象与三 / 二 / 一 / 零地址指令对比</figcaption>
</figure>

<strong>操作码的隐含语义</strong>。操作码不只是“做什么”，很多机器还用它指出“对什么类型的数据做”。IBM 360 中同样是加法，因参与运算的数据类型、尾数基值不同而有<strong>八条加法指令</strong>（定点加法、浮点加法，浮点尾数基值取 2 或 16 等）。部分机器的操作码还编码操作数的寻址方式。

<strong>操作码长度</strong>：

- <strong>固定长度</strong>：译码电路简单、译码快。RISC 指令较长、操作码固定；IBM 370 固定为 8 位。
- <strong>可变长度</strong>：x86 系列典型，需逐级扩展译码，硬件复杂。

> <strong>注意</strong>：图中操作码字段“集中放在一起”只是逻辑表示，实际机器中操作码可以拆开、分散在不同位置。

<figure class="cpd-figure not-content" id="lhw-w5-fig-18">
  <svg viewBox="0 0 700 264" role="img" aria-label="16位指令4-4-4-4等长扩展操作码编码树示意图：三地址15条、二地址15条、一地址15条、零地址16条">
    <defs>
      <marker id="lhw-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 class="fig-box-subtle" x="16" y="14" width="668" height="236" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">扩展操作码技术（以 16 位指令、每地址 4 位为例）</text>
    <line class="fig-line-accent" x1="176" y1="84" x2="176" y2="108" marker-end="url(#lhw-w5-arr-18)"/>
    <line class="fig-line-accent" x1="286" y1="138" x2="286" y2="162" marker-end="url(#lhw-w5-arr-18)"/>
    <line class="fig-line-accent" x1="396" y1="192" x2="396" y2="216" marker-end="url(#lhw-w5-arr-18)"/>
    <rect class="fig-box-blue" x="122" y="54" width="110" height="30" rx="4"/>
    <rect class="fig-box" x="232" y="54" width="110" height="30" rx="4"/>
    <rect class="fig-box" x="342" y="54" width="110" height="30" rx="4"/>
    <rect class="fig-box" x="452" y="54" width="110" height="30" rx="4"/>
    <text class="fig-blue" x="177" y="73" text-anchor="middle" font-size="11">OP (4 位)</text>
    <text class="fig-label" x="287" y="73" text-anchor="middle" font-size="11">A1 (4 位)</text>
    <text class="fig-label" x="397" y="73" text-anchor="middle" font-size="11">A2 (4 位)</text>
    <text class="fig-label" x="507" y="73" text-anchor="middle" font-size="11">A3 (4 位)</text>
    <text class="fig-label" x="30" y="73" font-size="11.5">三地址 (15条)</text>
    <text class="fig-muted" x="574" y="73" font-size="10.5">0000~1110</text>
    <rect class="fig-box-accent" x="122" y="108" width="110" height="30" rx="4"/>
    <rect class="fig-box-blue" x="232" y="108" width="110" height="30" rx="4"/>
    <rect class="fig-box" x="342" y="108" width="110" height="30" rx="4"/>
    <rect class="fig-box" x="452" y="108" width="110" height="30" rx="4"/>
    <text class="fig-accent" x="177" y="127" text-anchor="middle" font-size="11">1111（扩展）</text>
    <text class="fig-blue" x="287" y="127" text-anchor="middle" font-size="11">0000~1110</text>
    <text class="fig-label" x="397" y="127" text-anchor="middle" font-size="11">A2 (4 位)</text>
    <text class="fig-label" x="507" y="127" text-anchor="middle" font-size="11">A3 (4 位)</text>
    <text class="fig-label" x="30" y="127" font-size="11.5">二地址 (15条)</text>
    <text class="fig-muted" x="574" y="127" font-size="10.5">共 8 位 OP</text>
    <rect class="fig-box-accent" x="122" y="162" width="110" height="30" rx="4"/>
    <rect class="fig-box-accent" x="232" y="162" width="110" height="30" rx="4"/>
    <rect class="fig-box-blue" x="342" y="162" width="110" height="30" rx="4"/>
    <rect class="fig-box" x="452" y="162" width="110" height="30" rx="4"/>
    <text class="fig-accent" x="177" y="181" text-anchor="middle" font-size="11">1111</text>
    <text class="fig-accent" x="287" y="181" text-anchor="middle" font-size="11">1111（扩展）</text>
    <text class="fig-blue" x="397" y="181" text-anchor="middle" font-size="11">0000~1110</text>
    <text class="fig-label" x="507" y="181" text-anchor="middle" font-size="11">A3 (4 位)</text>
    <text class="fig-label" x="30" y="181" font-size="11.5">一地址 (15条)</text>
    <text class="fig-muted" x="574" y="181" font-size="10.5">共 12 位 OP</text>
    <rect class="fig-box-accent" x="122" y="216" width="110" height="30" rx="4"/>
    <rect class="fig-box-accent" x="232" y="216" width="110" height="30" rx="4"/>
    <rect class="fig-box-accent" x="342" y="216" width="110" height="30" rx="4"/>
    <rect class="fig-box-green" x="452" y="216" width="110" height="30" rx="4"/>
    <text class="fig-accent" x="177" y="235" text-anchor="middle" font-size="11">1111</text>
    <text class="fig-accent" x="287" y="235" text-anchor="middle" font-size="11">1111</text>
    <text class="fig-accent" x="397" y="235" text-anchor="middle" font-size="11">1111（扩展）</text>
    <text class="fig-green" x="507" y="235" text-anchor="middle" font-size="11">0000~1111</text>
    <text class="fig-label" x="30" y="235" font-size="11.5">零地址 (16条)</text>
    <text class="fig-muted" x="574" y="235" font-size="10.5">共 16 位 OP</text>
  </svg>
  <figcaption>图 7-2　定长指令字下的等长扩展操作码（4-4-4-4 编码树）</figcaption>
</figure>

<strong>指令字长</strong>指一条指令的长度（位数），有<strong>固定字长</strong>与<strong>可变字长</strong>两种确定方式。分析寻址方式时常做约定：指令字长 = 存储字长 = 机器字长，以简化例子。

### 7.2 操作数类型与操作类型

指令可处理的操作数类型：<strong>数值（定点、浮点）、地址（指针、无符号整数）、字符</strong>、<strong>逻辑数据</strong>。同一操作码可用附加字段区分数据类型，也可以为每种类型单设操作码（IBM 360 属于后者）。

操作类型按功能分五类：

| 类型 | 典型指令 | 说明 |
|---|---|---|
| 数据传送 | MOV、LOAD、STORE、PUSH、POP、MOVS | 寄存器↔寄存器、寄存器↔存储器、存储器↔存储器；另有寄存器清零/置一 |
| 算术逻辑 | ADD、SUB、MUL、DIV、AND、OR、XOR、NOT、CMP | 指令集必须支持的基本运算 |
| 移位 | 算术移位、逻辑移位、循环移位 | 左移/右移、带进位循环 |
| 转移 | JMP、JZ、JO、JC、CALL、RET、陷阱 | 改变程序控制流 |
| 输入输出 | IN、OUT | 仅独立编址的机器才需要 |

<strong>数据传送</strong>：源和目的可以是寄存器或存储器。`MOV`（寄存器↔存储器）、`LOAD`（存储器→寄存器）、`STORE`（寄存器→存储器）、`MOVS`（存储器→存储器），以及把指定寄存器清零/置一的特殊传送指令。

<strong>算术逻辑运算</strong>：常用的算术操作与逻辑操作都必须由指令集支持（8086 的 ADD/SUB/MUL/DIV、AND/OR/XOR/NOT 等）。

<strong>转移指令</strong>的四种形态：

1. <strong>无条件转移</strong>：直接转到目标地址。
2. <strong>条件转移（分支）</strong>：如结果为 0 转（JZ）、溢出转（JO）、有进位转（JC）；还有一条“<strong>跳过下一条指令</strong>”。程序查询方式做 I/O 时就用它：读取完成触发器 D，D=0 则跳过下面的输入/输出指令，D=1 才执行 I/O。
3. <strong>调用与返回：CALL 使控制流转入子程序，RET 返回到调用点的下一条指令，可嵌套</strong>（主程序→子程序 1→子程序 2→RET 回子程序 1→RET 回主程序）。
4. <strong>陷阱</strong>：指令执行过程中出现意外（非法操作码、操作数越界、除数为零）时由 <strong>CPU 硬件自动执行</strong>，不是用户可用的普通指令，用户看不见，故称<strong>隐指令</strong>；部分机器也提供用户可用的陷阱指令，用于程序调试或 I/O（如 8086 的 INT）。

<strong>输入输出指令</strong>：I/O 端口<strong>单独编址</strong>时必须有 IN/OUT；若端口统一编入内存地址空间，则直接用访存指令即可，无需 I/O 指令。IN 把端口内容送入 CPU 寄存器（`IN AL,n`、`IN AX,n`，数据宽度由端口决定）；OUT 把 CPU 寄存器内容送到端口（`OUT n,AL`、`OUT n,AX`）。

> <strong>★考点</strong>：给一条指令判断属于哪类操作、是否需要访问存储器、是否改变 PC。

### 7.3 指令寻址

寻址方式的定义：<strong>确定本条指令操作数的地址，或下一条要执行指令的地址</strong>。按对象分为<strong>指令寻址</strong>与<strong>数据寻址</strong>两类。

<strong>顺序寻址</strong>：取完一条指令后顺序取下一条，$PC \leftarrow PC + 1$ 中的“1”是<strong>一条指令的长度</strong>，不是常数 1。

- 字节编址、指令 32 位（4 字节）→ 每次加 4；
- 指令 64 字节 → 每次加 64；
- 变长指令时增量随指令长度变化，逻辑更复杂。

<strong>跳跃寻址</strong>：由转移指令给出下一条指令的地址。例：指令地址 0 起，PC 置 0，取出 LDA（取数）后 PC 自动加“1”；地址 3 处是<strong>无条件转移</strong>指令，目标地址 7，执行后跳到 7 取指令，7 之后又恢复顺序寻址。

> <strong>注意</strong>：<strong>相对寻址</strong>虽属数据寻址的讲法，但最常用于转移指令，实质应归入<strong>指令寻址</strong>。

### 7.4 数据寻址方式

数据寻址的指令格式在地址码前增加一个<strong>寻址特征字段</strong>，标识用哪种方式找操作数：

<figure class="cpd-figure not-content" id="lhw-w5-fig-19">
  <svg viewBox="0 0 700 286" role="img" aria-label="立即寻址、直接寻址与一次间接寻址的有效地址EA与访存次数对比图">
    <defs>
      <marker id="lhw-w5-arr-19" 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="14" width="212" height="258" rx="8"/>
    <rect class="fig-box-subtle" x="244" y="14" width="212" height="258" rx="8"/>
    <rect class="fig-box-subtle" x="472" y="14" width="212" height="258" rx="8"/>
    <line class="fig-line-accent" x1="122" y1="88" x2="122" y2="118" marker-end="url(#lhw-w5-arr-19)"/>
    <line class="fig-line-accent" x1="350" y1="88" x2="350" y2="112" marker-end="url(#lhw-w5-arr-19)"/>
    <line class="fig-line-accent" x1="578" y1="88" x2="578" y2="106" marker-end="url(#lhw-w5-arr-19)"/>
    <line class="fig-line-accent" x1="578" y1="162" x2="578" y2="180" marker-end="url(#lhw-w5-arr-19)"/>
    <text class="fig-blue" x="122" y="38" text-anchor="middle" font-size="13">立即寻址（0 次访存）</text>
    <rect class="fig-box" x="30" y="56" width="60" height="32" rx="4"/>
    <rect class="fig-box-blue" x="90" y="56" width="124" height="32" rx="4"/>
    <text class="fig-label" x="60" y="76" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-blue" x="152" y="76" text-anchor="middle" font-size="11.5">#A（操作数）</text>
    <rect class="fig-box-green" x="38" y="120" width="168" height="40" rx="5"/>
    <text class="fig-green" x="122" y="145" text-anchor="middle" font-size="12">ACC / 运算部件</text>
    <text class="fig-muted" x="30" y="192" font-size="11">· 取指时操作数已进入 CPU</text>
    <text class="fig-muted" x="30" y="214" font-size="11">· 执行阶段不再访存（0 次）</text>
    <text class="fig-muted" x="30" y="236" font-size="11">· A 为补码，位数限制范围</text>
    <text class="fig-blue" x="350" y="38" text-anchor="middle" font-size="13">直接寻址（1 次访存）</text>
    <rect class="fig-box" x="258" y="56" width="60" height="32" rx="4"/>
    <rect class="fig-box-blue" x="318" y="56" width="124" height="32" rx="4"/>
    <text class="fig-label" x="288" y="76" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-blue" x="380" y="76" text-anchor="middle" font-size="11.5">A = 2000H</text>
    <rect class="fig-box" x="258" y="114" width="184" height="82" rx="5"/>
    <line class="fig-divider" x1="258" y1="140" x2="442" y2="140"/>
    <text class="fig-muted" x="350" y="131" text-anchor="middle" font-size="11">主存储器</text>
    <text class="fig-label" x="350" y="162" text-anchor="middle" font-size="11.5">2000H：操作数</text>
    <text class="fig-green" x="350" y="184" text-anchor="middle" font-size="11">EA = A，1 次访存</text>
    <text class="fig-muted" x="258" y="222" font-size="11">· A 位数限制寻址范围</text>
    <text class="fig-muted" x="258" y="244" font-size="11">· 地址难改，循环写起来笨重</text>
    <text class="fig-blue" x="578" y="38" text-anchor="middle" font-size="13">间接寻址（2 次访存）</text>
    <rect class="fig-box" x="486" y="56" width="60" height="32" rx="4"/>
    <rect class="fig-box-blue" x="546" y="56" width="124" height="32" rx="4"/>
    <text class="fig-label" x="516" y="76" text-anchor="middle" font-size="11.5">OP</text>
    <text class="fig-blue" x="608" y="76" text-anchor="middle" font-size="11.5">A</text>
    <rect class="fig-box" x="486" y="108" width="184" height="54" rx="5"/>
    <text class="fig-label" x="578" y="130" text-anchor="middle" font-size="11.5">A 单元：EA = 3000H</text>
    <text class="fig-muted" x="578" y="150" text-anchor="middle" font-size="10.5">第 1 次访存取出 EA</text>
    <rect class="fig-box-green" x="486" y="182" width="184" height="54" rx="5"/>
    <text class="fig-green" x="578" y="204" text-anchor="middle" font-size="11.5">EA 单元：操作数</text>
    <text class="fig-muted" x="578" y="224" text-anchor="middle" font-size="10.5">第 2 次访存取出操作数</text>
    <text class="fig-muted" x="486" y="258" font-size="10.5">一次间接需 2 次访存（不含取指）</text>
  </svg>
  <figcaption>图 7-3　立即寻址、直接寻址与一次间接寻址的有效地址与访存次数对比</figcaption>
</figure>

<strong>形式地址 A 是指令中给出的地址，有效地址 EA</strong> 是操作数的真实地址，二者由寻址方式换算。

| 寻址方式 | 有效地址 EA | 取操作数访存次数 | 特点与用途 |
|---|---|---|---|
| 立即寻址 | 无，操作数就是 A | 0 | 最快，A 位数决定数值范围 |
| 直接寻址 | $EA = A$ | 1 | 简单；寻址范围受 A 位数限制，程序难修改 |
| 隐含寻址 | 隐含在操作码中 | 0 或 1 | 少一个地址字段，缩短指令；如 ACC、栈顶 |
| 间接寻址 | $EA = (A)$ | 2 | 扩大寻址范围，代价是多次访存 |
| 变址寻址 | $EA = A + (IX)$ | 1 | 数组/循环，IX 可自增 |
| 基址寻址 | $EA = A + (BR)$ | 1 | 程序浮动、扩大寻址空间 |
| 相对寻址 | $EA = A + (PC)$ | 1 | 转移指令、程序浮动 |
| 堆栈寻址 | $EA = (SP)$ | 1 | 先进后出，保护现场/子程序调用 |

<figure class="cpd-figure not-content" id="lhw-w5-fig-20">
  <svg viewBox="0 0 700 282" role="img" aria-label="变址、基址与相对寻址共用加法通路图：形式地址A与不同基准寄存器相加生成EA">
    <defs>
      <marker id="lhw-w5-arr-20" 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="14" width="668" height="254" rx="8"/>
    <path class="fig-line" d="M 176 70 H 212 V 140 H 254" marker-end="url(#lhw-w5-arr-20)"/>
    <line class="fig-line" x1="176" y1="116" x2="212" y2="116"/>
    <line class="fig-line" x1="176" y1="162" x2="212" y2="162"/>
    <path class="fig-line" d="M 176 208 H 212 V 140"/>
    <line class="fig-line-accent" x1="380" y1="140" x2="416" y2="140" marker-end="url(#lhw-w5-arr-20)"/>
    <line class="fig-line-accent" x1="490" y1="140" x2="526" y2="140" marker-end="url(#lhw-w5-arr-20)"/>
    <rect class="fig-box" x="30" y="52" width="146" height="34" rx="5"/>
    <rect class="fig-box-blue" x="30" y="98" width="146" height="34" rx="5"/>
    <rect class="fig-box-purple" x="30" y="144" width="146" height="34" rx="5"/>
    <rect class="fig-box-orange" x="30" y="190" width="146" height="34" rx="5"/>
    <rect class="fig-box-accent" x="256" y="108" width="124" height="64" rx="6"/>
    <rect class="fig-box-blue" x="418" y="114" width="72" height="52" rx="5"/>
    <rect class="fig-box-green" x="528" y="108" width="142" height="64" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="13">变址 / 基址 / 相对寻址：同一套加法通路，仅基准寄存器不同</text>
    <text class="fig-label" x="103" y="74" text-anchor="middle" font-size="12">形式地址 A（偏移）</text>
    <text class="fig-blue" x="103" y="120" text-anchor="middle" font-size="12">变址寄存器 IX</text>
    <text class="fig-purple" x="103" y="166" text-anchor="middle" font-size="12">基址寄存器 BR</text>
    <text class="fig-orange" x="103" y="212" text-anchor="middle" font-size="12">程序计数器 PC</text>
    <text class="fig-accent" x="318" y="135" text-anchor="middle" font-size="12.5">+ 加法器</text>
    <text class="fig-muted" x="318" y="155" text-anchor="middle" font-size="11">EA = A + (R)</text>
    <text class="fig-blue" x="454" y="145" text-anchor="middle" font-size="13">EA</text>
    <text class="fig-green" x="599" y="135" text-anchor="middle" font-size="12">主存储器</text>
    <text class="fig-muted" x="599" y="155" text-anchor="middle" font-size="11">按 EA 取操作数</text>
    <text class="fig-muted" x="30" y="248" font-size="11">· 变址：IX 面向用户（循环数组）；基址：BR 面向系统（程序浮动/扩址）；相对：PC 基准（转移）</text>
  </svg>
  <figcaption>图 7-4　变址、基址与相对寻址共用的有效地址加法通路</figcaption>
</figure>

<strong>直接寻址</strong>：$EA = A$，A 直接给出操作数所在内存单元的地址，按该地址访问一次内存取数。缺点是 A 的位数固定、地址码部分<strong>难以修改</strong>：若要对数组中每个元素加一，用循环就写不出来，除非另有一条能取出并修改该指令地址码的指令。

<strong>隐含寻址</strong>：参与运算的某个操作数位置由操作码直接规定，指令中只给出另一个操作数的地址，从而省掉一个地址字段、缩短指令。例：8086 的乘法指令，被乘数隐含在 AX/AL 中，结果隐含在 DX:AX。加法指令中，内存操作数由地址码给出，另一个操作数隐含在 ACC，运算结果再送回 ACC。

<strong>间接寻址：先按 A 访存取出 EA，再按 EA 访存取出操作数，一次间接需两次访存</strong>；EA 的宽度（存储字长）决定其寻址范围，因而比直接寻址范围大得多。

<strong>变址寻址</strong>：把变址寄存器 IX 的内容与形式地址 A 相加。数组求和时，A 取数组首地址 D，IX 从 0 递增到 n−1，一条 `ADD ACC, D(IX)` 就能遍历全部元素。与基址寻址的区别在于<strong>变址寄存器内容由用户改变（面向用户）</strong>，基址寄存器内容由系统设定（面向系统）；IBM 360 的存储器地址采用<strong>基址 + 变址</strong>方式生成。

<strong>相对寻址</strong>：以当前 PC 为基准，

$$
EA = (PC) + A
$$

A 是相对于当前指令的<strong>位移量</strong>，用补码表示，可正可负；A 的位数决定可跳转的相对距离。优点是<strong>程序浮动</strong>：程序在内存中整体搬家后，指令间的相对距离不变，程序照样正确执行。

> <strong>注意</strong>：不同教材对 $(PC)$ 取“当前指令地址”还是“取指后的 PC（当前指令地址 + 指令长度）”有两种约定，做题必须先看题目约定，见 7.5 的 2000H 例。

<strong>堆栈寻址</strong>：堆栈是<strong>先进后出</strong>的存储区，只有一个出入口。分<strong>硬堆栈</strong>（用 2\~3 个或多个寄存器作栈顶，堆栈型计算机常用）和<strong>软堆栈</strong>（在内存中划出一段区域）。栈顶由 <strong>SP 指定；栈底在高地址，栈顶向低地址方向增长</strong>。

<figure class="cpd-figure not-content" id="lhw-w5-fig-21">
  <svg viewBox="0 0 700 284" role="img" aria-label="堆栈寻址原理图：栈底在高地址、栈顶向低地址方向增长、PUSH先减再写与POP先读再加">
    <defs>
      <marker id="lhw-w5-arr-21" 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="14" width="668" height="256" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">堆栈寻址（栈底在高地址，栈顶向低地址生长）</text>
    <line class="fig-line-accent" x1="126" y1="80" x2="188" y2="80" marker-end="url(#lhw-w5-arr-21)"/>
    <line class="fig-line" x1="126" y1="114" x2="188" y2="114" marker-end="url(#lhw-w5-arr-21)"/>
    <rect class="fig-box-blue" x="190" y="64" width="210" height="32" rx="3"/>
    <rect class="fig-box" x="190" y="96" width="210" height="32" rx="3"/>
    <rect class="fig-box" x="190" y="128" width="210" height="32" rx="3"/>
    <rect class="fig-box" x="190" y="160" width="210" height="32" rx="3"/>
    <rect class="fig-box" x="190" y="192" width="210" height="32" rx="3"/>
    <text class="fig-blue" x="30" y="84" font-size="11.5">SP = 1FFFH</text>
    <text class="fig-muted" x="30" y="118" font-size="11.5">原栈顶 2000H</text>
    <text class="fig-blue" x="242" y="84" text-anchor="middle" font-size="10.8">1FFFH</text>
    <text class="fig-label" x="342" y="84" text-anchor="middle" font-size="10.8">15H（新入栈）</text>
    <text class="fig-muted" x="242" y="116" text-anchor="middle" font-size="10.8">2000H</text>
    <text class="fig-muted" x="242" y="148" text-anchor="middle" font-size="10.8">2001H</text>
    <text class="fig-muted" x="242" y="180" text-anchor="middle" font-size="10.8">2002H</text>
    <text class="fig-muted" x="242" y="212" text-anchor="middle" font-size="10.8">2003H（栈底）</text>
    <text class="fig-muted" x="295" y="248" text-anchor="middle" font-size="11">地址增大方向 ↓　栈顶向低地址生长 ↑</text>
    <rect class="fig-box" x="424" y="64" width="244" height="160" rx="6"/>
    <text class="fig-blue" x="546" y="86" text-anchor="middle" font-size="12.5">PUSH / POP 微操作（字节编址）</text>
    <text class="fig-label" x="438" y="112" font-size="11.5">入栈（PUSH）：</text>
    <text class="fig-muted" x="438" y="132" font-size="11">① SP ← SP - 1（2000H → 1FFFH）</text>
    <text class="fig-muted" x="438" y="150" font-size="11">② 数据写入 M[SP]（原 2000H 不动）</text>
    <text class="fig-label" x="438" y="178" font-size="11.5">出栈（POP）：</text>
    <text class="fig-muted" x="438" y="198" font-size="11">① 从 M[SP] 读出栈顶数据送 ACC</text>
    <text class="fig-muted" x="438" y="216" font-size="11">② SP ← SP + 1（1FFFH → 2000H）</text>
  </svg>
  <figcaption>图 7-5　堆栈寻址（软堆栈向低地址生长与 PUSH / POP 操作）</figcaption>
</figure>

SP 的修改量与<strong>编址单位</strong>有关：压入一个字节（字节编址）→ SP 减 1（出栈加 1）；存储字长 16 位、按字压入 → 减 2（加 2）；存储字长 32 位 → 减 4（加 4）。硬堆栈的栈顶寄存器在堆栈型机器中直接参与运算，两个操作数都取自栈顶。

<figure class="cpd-figure not-content" id="lhw-w5-fig-22">
  <svg viewBox="0 0 700 288" role="img" aria-label="寻址在指令周期中的位置示意图：取指、译码、计算EA、取操作数与执行五阶段及顺序与跳跃寻址">
    <defs>
      <marker id="lhw-w5-arr-22" 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="14" width="668" height="260" rx="8"/>
    <line class="fig-line-accent" x1="144" y1="88" x2="160" y2="88" marker-end="url(#lhw-w5-arr-22)"/>
    <line class="fig-line-accent" x1="274" y1="88" x2="290" y2="88" marker-end="url(#lhw-w5-arr-22)"/>
    <line class="fig-line-accent" x1="404" y1="88" x2="420" y2="88" marker-end="url(#lhw-w5-arr-22)"/>
    <line class="fig-line-accent" x1="534" y1="88" x2="550" y2="88" marker-end="url(#lhw-w5-arr-22)"/>
    <line class="fig-line" x1="87" y1="126" x2="87" y2="154" marker-end="url(#lhw-w5-arr-22)"/>
    <line class="fig-line" x1="610" y1="126" x2="610" y2="154" marker-end="url(#lhw-w5-arr-22)"/>
    <rect class="fig-box-blue" x="30" y="52" width="114" height="74" rx="5"/>
    <rect class="fig-box" x="162" y="52" width="112" height="74" rx="5"/>
    <rect class="fig-box-accent" x="292" y="52" width="112" height="74" rx="5"/>
    <rect class="fig-box-green" x="422" y="52" width="112" height="74" rx="5"/>
    <rect class="fig-box-purple" x="552" y="52" width="118" height="74" rx="5"/>
    <rect class="fig-box" x="30" y="156" width="290" height="54" rx="5"/>
    <rect class="fig-box" x="380" y="156" width="290" height="54" rx="5"/>
    <text class="fig-title" x="30" y="34" font-size="13">寻址在指令周期中的位置（指令寻址 vs 数据寻址）</text>
    <text class="fig-blue" x="87" y="74" text-anchor="middle" font-size="12">① 取指</text>
    <text class="fig-muted" x="87" y="94" text-anchor="middle" font-size="10.5">PC→MAR，M→MDR</text>
    <text class="fig-muted" x="87" y="112" text-anchor="middle" font-size="10.5">→IR；PC←PC+k</text>
    <text class="fig-label" x="218" y="74" text-anchor="middle" font-size="12">② 译码</text>
    <text class="fig-muted" x="218" y="94" text-anchor="middle" font-size="10.5">OP→控制信号</text>
    <text class="fig-muted" x="218" y="112" text-anchor="middle" font-size="10.5">识别寻址特征位</text>
    <text class="fig-accent" x="348" y="74" text-anchor="middle" font-size="12">③ 计算 EA</text>
    <text class="fig-muted" x="348" y="94" text-anchor="middle" font-size="10.5">直接：EA = A</text>
    <text class="fig-muted" x="348" y="112" text-anchor="middle" font-size="10.5">变址：A + (IX)</text>
    <text class="fig-green" x="478" y="74" text-anchor="middle" font-size="12">④ 取操作数</text>
    <text class="fig-muted" x="478" y="94" text-anchor="middle" font-size="10.5">M[EA] → MDR</text>
    <text class="fig-muted" x="478" y="112" text-anchor="middle" font-size="10.5">立即数无需访存</text>
    <text class="fig-purple" x="611" y="74" text-anchor="middle" font-size="12">⑤ 执行</text>
    <text class="fig-muted" x="611" y="94" text-anchor="middle" font-size="10.5">运算 / 传送</text>
    <text class="fig-muted" x="611" y="112" text-anchor="middle" font-size="10.5">写结果、改状态</text>
    <text class="fig-blue" x="44" y="178" font-size="11.5">顺序寻址：PC ← PC + k</text>
    <text class="fig-muted" x="44" y="198" font-size="10.5">k = 当前指令字节数（32 位定长指令 k = 4）</text>
    <text class="fig-purple" x="394" y="178" font-size="11.5">跳跃寻址：⑤ 阶段改写 PC</text>
    <text class="fig-muted" x="394" y="198" font-size="10.5">转移指令的目标地址或相对位移写入 PC</text>
    <text class="fig-muted" x="30" y="234" font-size="10.8">· 立即/寄存器寻址可跳过④访存；一次间接寻址在③后增一次访存（先读 EA），再由④取数</text>
    <text class="fig-muted" x="30" y="254" font-size="11">· ③ 的加法器同时服务于变址、基址与相对寻址的有效地址计算</text>
  </svg>
  <figcaption>图 7-6　寻址操作在指令周期五个阶段中的位置</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w5-fig-23">
  <svg viewBox="0 0 700 318" role="img" aria-label="10种常见数据寻址方式的有效地址EA计算公式与访存次数全景对照图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="290" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">10 种数据寻址方式有效地址（EA）与执行阶段访存次数全景</text>
    <rect class="fig-box-green" x="30" y="50" width="206" height="46" rx="5"/>
    <rect class="fig-box-green" x="247" y="50" width="206" height="46" rx="5"/>
    <rect class="fig-box-green" x="464" y="50" width="206" height="46" rx="5"/>
    <rect class="fig-box-blue" x="30" y="106" width="206" height="46" rx="5"/>
    <rect class="fig-box-blue" x="247" y="106" width="206" height="46" rx="5"/>
    <rect class="fig-box-blue" x="464" y="106" width="206" height="46" rx="5"/>
    <rect class="fig-box-purple" x="30" y="162" width="206" height="46" rx="5"/>
    <rect class="fig-box-purple" x="247" y="162" width="206" height="46" rx="5"/>
    <rect class="fig-box-purple" x="464" y="162" width="206" height="46" rx="5"/>
    <rect class="fig-box-red" x="30" y="218" width="640" height="46" rx="5"/>
    <text class="fig-green" x="42" y="70" font-size="11.5">1. 隐含寻址（0 次访存）</text>
    <text class="fig-muted" x="42" y="88" font-size="10.5">操作数位置隐含在 OP（如 ACC）</text>
    <text class="fig-green" x="259" y="70" font-size="11.5">2. 立即寻址（0 次访存）</text>
    <text class="fig-muted" x="259" y="88" font-size="10.5">操作数即形式地址 #A 本身</text>
    <text class="fig-green" x="476" y="70" font-size="11.5">3. 寄存器直接（0 次访存）</text>
    <text class="fig-muted" x="476" y="88" font-size="10.5">EA = Ri（操作数在通用寄存器）</text>
    <text class="fig-blue" x="42" y="126" font-size="11.5">4. 直接寻址（1 次访存）</text>
    <text class="fig-muted" x="42" y="144" font-size="10.5">EA = A，按 A 直接访问主存</text>
    <text class="fig-blue" x="259" y="126" font-size="11.5">5. 寄存器间接（1 次访存）</text>
    <text class="fig-muted" x="259" y="144" font-size="10.5">EA = (Ri)，指针放在寄存器 Ri</text>
    <text class="fig-blue" x="476" y="126" font-size="11.5">6. 堆栈寻址（1 次访存）</text>
    <text class="fig-muted" x="476" y="144" font-size="10.5">EA = (SP)，软堆栈访存 1 次</text>
    <text class="fig-purple" x="42" y="182" font-size="11.5">7. 相对寻址（1 次访存）</text>
    <text class="fig-muted" x="42" y="200" font-size="10.5">EA = (PC) + A（转移/程序浮动）</text>
    <text class="fig-purple" x="259" y="182" font-size="11.5">8. 基址寻址（1 次访存）</text>
    <text class="fig-muted" x="259" y="200" font-size="10.5">EA = (BR) + A（OS 设定，扩址）</text>
    <text class="fig-purple" x="476" y="182" font-size="11.5">9. 变址寻址（1 次访存）</text>
    <text class="fig-muted" x="476" y="200" font-size="10.5">EA = (IX) + A（用户修改，数组）</text>
    <text class="fig-red" x="42" y="238" font-size="11.5">10. 一次间接寻址（2 次访存，多级间址需 k+1 次）</text>
    <text class="fig-muted" x="42" y="256" font-size="10.5">EA = (A)：第 1 次访存从 M[A] 读出有效地址 EA（宽度 = 存储字长），第 2 次访存从 M[EA] 读出操作数</text>
    <text class="fig-muted" x="30" y="288" font-size="11">注：以上访存次数均指执行阶段取操作数所需访存次数，不含取指令本身的访存</text>
  </svg>
  <figcaption>图 7-7　10 种数据寻址方式的有效地址公式与执行阶段访存次数对照</figcaption>
</figure>

### 7.5 两个典型例子：变址与相对寻址

<strong>例 1（数组求和求平均值，数据块首地址 D，共 n 个数）</strong>

直接寻址版：先把 D 取入 ACC，再依次与 D+1、D+2、…、D+n−1 相加，然后立即数寻址做除法，最后存回。<strong>需要 n+2 条指令</strong>，程序占空间大。

变址寻址版（8 条指令）：

1. `CLR ACC`（立即数 0 清零）
2. `CLR IX`（IX 从 0 开始）
3. `M: ADD ACC, D(IX)`（变址寻址，累加第 IX 个元素）
4. `INC IX`
5. `CMP IX, #n`
6. `BNE M`（相对寻址，未到 n 则回 M）
7. `DIV #n`（立即数寻址求平均）
8. `STA R`（直接寻址，把平均值存到结果单元）

<strong>结论</strong>：变址寻址把“地址码随循环变化”变成“变址寄存器自增”，指令条数从 n+2 降到 8，程序占用内存大幅减少，这是变址寻址用于数组/循环的本质。

<strong>例 2（相对寻址的位移量计算）</strong>

该转移指令当前地址 2000H、指令字长 2 字节、目标指令地址 2008H。取指后 PC 已自动变为 2002H（<strong>PC 在取指阶段就被修改</strong>），因此指令中的位移量不能写 8，而应写

$$
A = 2008_H - 2002_H = 06_H
$$

即 `JMP *+06H`。若按“PC 为当前指令地址”的口径直接写 +8，跳转就会多跳一个指令长度。

循环回跳同一原理：把绝对地址的 `BNE m` 改写为 `DNE *−3`，按该例约定取指后 PC = m+3，则

$$
EA = (PC) + A = (m+3) - 3 = m
$$

位移量为补码 −3，程序无论装入何处都能正确回跳，这就是<strong>程序浮动</strong>。

<figure class="cpd-figure not-content" id="lhw-w5-fig-24">
  <svg viewBox="0 0 700 282" role="img" aria-label="相对寻址位移量计算示意图：指令在2000H、取指后PC为2002H、目标2008H、位移量A=06H">
    <defs>
      <marker id="lhw-w5-arr-24" 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="14" width="668" height="254" rx="8"/>
    <line class="fig-line" x1="170" y1="88" x2="244" y2="88" marker-end="url(#lhw-w5-arr-24)"/>
    <line class="fig-line-dashed" x1="386" y1="88" x2="460" y2="88" marker-end="url(#lhw-w5-arr-24)"/>
    <path class="fig-line-accent" d="M 315 114 V 154 H 530 V 116" marker-end="url(#lhw-w5-arr-24)"/>
    <line class="fig-line" x1="100" y1="114" x2="100" y2="174" marker-end="url(#lhw-w5-arr-24)"/>
    <rect class="fig-box-blue" x="30" y="62" width="140" height="52" rx="5"/>
    <rect class="fig-box" x="246" y="62" width="140" height="52" rx="5"/>
    <rect class="fig-box-green" x="462" y="62" width="140" height="52" rx="5"/>
    <rect class="fig-box" x="366" y="138" width="114" height="24" rx="4"/>
    <text class="fig-title" x="30" y="34" font-size="13">相对寻址位移量计算（PC 在取指阶段已前移）</text>
    <text class="fig-muted" x="100" y="82" text-anchor="middle" font-size="11">2000H（2 字节）</text>
    <text class="fig-blue" x="100" y="102" text-anchor="middle" font-size="12">JMP *+06H</text>
    <text class="fig-muted" x="316" y="82" text-anchor="middle" font-size="11">2002H（取指后 PC）</text>
    <text class="fig-label" x="316" y="102" text-anchor="middle" font-size="12">下一条指令</text>
    <text class="fig-muted" x="532" y="82" text-anchor="middle" font-size="11">2008H</text>
    <text class="fig-green" x="532" y="102" text-anchor="middle" font-size="12">目标指令</text>
    <text class="fig-accent" x="423" y="154" text-anchor="middle" font-size="11.5">位移量 A = +06H</text>
    <rect class="fig-box" x="30" y="176" width="300" height="76" rx="5"/>
    <rect class="fig-box" x="350" y="176" width="320" height="76" rx="5"/>
    <text class="fig-label" x="44" y="198" font-size="11.5">正确推导（取指后 PC 已加 2）：</text>
    <text class="fig-muted" x="44" y="218" font-size="11">· 取指后 (PC) = 2000H + 2 = 2002H</text>
    <text class="fig-muted" x="44" y="238" font-size="11">· A = 目标 - (PC) = 2008H - 2002H = 06H</text>
    <text class="fig-red" x="364" y="198" font-size="11.5">常见误区（直接写 +8）：</text>
    <text class="fig-muted" x="364" y="218" font-size="11">· 若 A = 08H，则 EA = 2002H + 08H = 200AH</text>
    <text class="fig-muted" x="364" y="238" font-size="11">· 相对位移随程序搬家不变 → 支持程序浮动</text>
  </svg>
  <figcaption>图 7-8　相对寻址位移量计算与“取指后 PC 前移”机制</figcaption>
</figure>

### 7.6 指令格式举例：IBM 360 与 8086

设计指令格式要考虑：<strong>指令系统兼容性（老程序不改就能在新机器上运行，否则用户重写、厂商难推广）、操作类型（指令条数、操作难度、使用频度）、数据类型（每种类型单设操作码还是在操作码后加字段）、指令格式</strong>（定长/变长、操作码扩展方式、地址码位数与个数、采用哪些寻址方式、是否任意操作码都能配任意寻址方式与数据类型）。

<strong>IBM 360：五种指令格式</strong>，操作码一律 8 位，通用寄存器 16 个（编号 4 位），存储器地址由<strong>基址 + 变址 + 偏移</strong>生成。

| 格式 | 字段构成 | 长度 | 用途 |
|---|---|---|---|
| RR（寄存器-寄存器） | OP(8) R1(4) R2(4) | 2 字节 | 两个寄存器参加运算，结果存其中一个 |
| RX（寄存器-存储器） | OP(8) R(4) X(4) B(4) D(12) | 4 字节 | X 为变址寄存器，B 为基址寄存器，D 为偏移 |
| RS（寄存器-存储器，成组） | OP(8) R1(4) R3(4) B(4) D(12) | 4 字节 | R1\~R3 编号区间内的寄存器内容<strong>成组</strong>传送到内存，内存起始地址用基址寻址 |
| RI（立即数-存储器） | OP(8) + 寄存器号 + 立即数 | 4 字节 | 立即数与内存操作数运算 |
| SS（存储器-存储器） | OP(8) L(8) B1(4) D1(12) B2(4) D2(12) | 6 字节 | 两块内存间传送，L 给出块长；典型如成块传送、翻译（TR）指令 |

<strong>8086：典型 CISC</strong>，指令字长 1\~6 字节；地址格式多样，有零地址（`NOP`）、一地址（`INC`；`CALL` 段内 3 字节、段间 5 字节）、二地址（`ADD reg,reg` 2 字节；`ADD AX,imm16` 3 字节；`ADD reg,mem` 4 字节）。

<figure class="cpd-figure not-content" id="lhw-w5-fig-25">
  <svg viewBox="0 0 700 264" role="img" aria-label="8086变长指令格式1至6字节对比图：NOP、ADD、CALL等不同寻址方式导致字长变化">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="236" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">8086 变长指令格式（1~6 字节，典型 CISC）</text>
    <text class="fig-muted" x="120" y="54" text-anchor="middle" font-size="10.5">字节 1</text>
    <text class="fig-muted" x="190" y="54" text-anchor="middle" font-size="10.5">字节 2</text>
    <text class="fig-muted" x="260" y="54" text-anchor="middle" font-size="10.5">字节 3</text>
    <text class="fig-muted" x="330" y="54" text-anchor="middle" font-size="10.5">字节 4</text>
    <text class="fig-muted" x="400" y="54" text-anchor="middle" font-size="10.5">字节 5</text>
    <text class="fig-muted" x="470" y="54" text-anchor="middle" font-size="10.5">字节 6</text>
    <rect class="fig-box-blue" x="85" y="64" width="70" height="24" rx="3"/>
    <rect class="fig-box-blue" x="85" y="94" width="140" height="24" rx="3"/>
    <rect class="fig-box-blue" x="85" y="124" width="210" height="24" rx="3"/>
    <rect class="fig-box-blue" x="85" y="154" width="280" height="24" rx="3"/>
    <rect class="fig-box-blue" x="85" y="184" width="350" height="24" rx="3"/>
    <rect class="fig-box-red" x="85" y="214" width="420" height="24" rx="3"/>
    <text class="fig-blue" x="120" y="80" text-anchor="middle" font-size="11">NOP</text>
    <text class="fig-muted" x="166" y="80" font-size="11">1 字节（零地址）</text>
    <text class="fig-blue" x="155" y="110" text-anchor="middle" font-size="11">ADD reg, reg</text>
    <text class="fig-muted" x="236" y="110" font-size="11">2 字节（二地址，寄存器寻址）</text>
    <text class="fig-blue" x="190" y="140" text-anchor="middle" font-size="11">ADD AX, imm16</text>
    <text class="fig-muted" x="306" y="140" font-size="11">3 字节（含 16 位立即数）</text>
    <text class="fig-blue" x="225" y="170" text-anchor="middle" font-size="11">ADD reg, mem</text>
    <text class="fig-muted" x="376" y="170" font-size="11">4 字节（含内存偏移地址字段）</text>
    <text class="fig-blue" x="260" y="200" text-anchor="middle" font-size="11">CALL 段间</text>
    <text class="fig-muted" x="446" y="200" font-size="11">5 字节（段内 CALL 为 3 字节）</text>
    <text class="fig-red" x="295" y="230" text-anchor="middle" font-size="11">最长 6 字节指令（如带前缀与双字位移/立即数）</text>
  </svg>
  <figcaption>图 7-9　8086 变长指令格式（1~6 字节长度分布）</figcaption>
</figure>

> <strong>★考点</strong>：给出指令字长、操作码位数、地址码位数，判断各格式能表示多少个寄存器、寻址范围多大。

### 7.7 指令格式设计（习题 7.19）

<strong>条件</strong>：32 个 32 位通用寄存器；64 种操作；指令字长 = 机器字长 = 32 位；采用<strong>寄存器-存储器（R-S）型</strong>，存储器支持<strong>直接或间接</strong>寻址。

<strong>位数分配推导</strong>：

1. 32 个通用寄存器 → 寄存器编号 $5$ 位（00000\~11111），且寄存器 32 位 ⇒ <strong>机器字长 32 位</strong>，又指令字长 = 机器字长 ⇒ 指令共 32 位。
2. 64 种操作 → 操作码 $6$ 位（64 = $2^6$）。
3. R-S 型至少两个地址：一个寄存器号 5 位 + 一个存储器形式地址 A。
4. 存储器寻址方式有直接、间接 2 种 → <strong>寻址特征 I 取 1 位</strong>（0 直接、1 间接），紧挨在 A 之前。
5. 形式地址 $A = 32 - 6 - 5 - 1 = 20$ 位。

| 字段 | OP | R_i | I | A |
|---|---|---|---|---|
| 位数 | 6 | 5 | 1 | 20 |

<strong>寻址空间</strong>：

- 直接寻址：A 的 20 位直接当地址 → $2^{20}$ 个字 = <strong>1M 字</strong>（注意单位是<strong>字</strong>，因为按字编址）。
- 一次间接寻址：A 指向的单元中存放 32 位有效地址，默认存储字长 = 机器字长 = 32 位 → $2^{32}$ 个字 = <strong>4G 字</strong>。

<strong>执行过程</strong>：先由 R_i 取操作数一（不访存）；直接寻址时按 A 访存取操作数二（1 次访存）；间接寻址时先按 A 访存取 EA（32 位），再按 EA 访存取操作数二（2 次访存）。

<strong>加基址寻址的两种改法</strong>：

- 基址寄存器<strong>专用</strong>（不占指令字段）：寻址方式变为 3 种（直接/间接/基址），特征位 2 位 → $A = 32-6-5-2 = 19$ 位。
- 基址寄存器由<strong>通用寄存器兼任</strong>（需在指令中指明是哪一个）：追加 5 位基址寄存器号 B，特征位仍 1 位 → $A = 32-6-5-1-5 = 15$ 位；$EA = (BR) + A$，32 位基址加 15 位偏移，结果按长者为 32 位，寻址空间由 $2^{20}$ 字跃升到 $2^{32}$ 字。

<figure class="cpd-figure not-content" id="lhw-w5-fig-26">
  <svg viewBox="0 0 700 250" role="img" aria-label="习题7.19三种32位R-S型指令格式位分配与寻址空间对比图">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="222" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">习题 7.19：32 位 R-S 型指令格式位分配与基址改法对比</text>
    <text class="fig-label" x="30" y="72" font-size="11.5">原格式</text>
    <rect class="fig-box-accent" x="116" y="52" width="84" height="32" rx="3"/>
    <rect class="fig-box-blue" x="200" y="52" width="70" height="32" rx="3"/>
    <rect class="fig-box-orange" x="270" y="52" width="32" height="32" rx="3"/>
    <rect class="fig-box-green" x="302" y="52" width="238" height="32" rx="3"/>
    <text class="fig-accent" x="158" y="72" text-anchor="middle" font-size="11">OP (6)</text>
    <text class="fig-blue" x="235" y="72" text-anchor="middle" font-size="11">Ri (5)</text>
    <text class="fig-orange" x="286" y="72" text-anchor="middle" font-size="9.2">I(1)</text>
    <text class="fig-green" x="421" y="72" text-anchor="middle" font-size="11">形式地址 A (20 位)</text>
    <text class="fig-muted" x="550" y="72" font-size="10.5">直 1M / 间 4G 字</text>
    <text class="fig-label" x="30" y="122" font-size="11.5">专用 BR</text>
    <rect class="fig-box-accent" x="116" y="102" width="84" height="32" rx="3"/>
    <rect class="fig-box-blue" x="200" y="102" width="70" height="32" rx="3"/>
    <rect class="fig-box-orange" x="270" y="102" width="56" height="32" rx="3"/>
    <rect class="fig-box-green" x="326" y="102" width="214" height="32" rx="3"/>
    <text class="fig-accent" x="158" y="122" text-anchor="middle" font-size="11">OP (6)</text>
    <text class="fig-blue" x="235" y="122" text-anchor="middle" font-size="11">Ri (5)</text>
    <text class="fig-orange" x="298" y="122" text-anchor="middle" font-size="9.2">寻址(2)</text>
    <text class="fig-green" x="433" y="122" text-anchor="middle" font-size="11">形式地址 A (19 位)</text>
    <text class="fig-muted" x="550" y="122" font-size="10.5">3 种寻址（不占 B）</text>
    <text class="fig-label" x="30" y="172" font-size="11.5">通用 BR</text>
    <rect class="fig-box-accent" x="116" y="152" width="84" height="32" rx="3"/>
    <rect class="fig-box-blue" x="200" y="152" width="70" height="32" rx="3"/>
    <rect class="fig-box-orange" x="270" y="152" width="32" height="32" rx="3"/>
    <rect class="fig-box-purple" x="302" y="152" width="66" height="32" rx="3"/>
    <rect class="fig-box-green" x="368" y="152" width="172" height="32" rx="3"/>
    <text class="fig-accent" x="158" y="172" text-anchor="middle" font-size="11">OP (6)</text>
    <text class="fig-blue" x="235" y="172" text-anchor="middle" font-size="11">Ri (5)</text>
    <text class="fig-orange" x="286" y="172" text-anchor="middle" font-size="9.2">I(1)</text>
    <text class="fig-purple" x="335" y="172" text-anchor="middle" font-size="10.5">B (5)</text>
    <text class="fig-green" x="454" y="172" text-anchor="middle" font-size="11">偏移 A (15 位)</text>
    <text class="fig-muted" x="550" y="172" font-size="10.5">EA=(BR)+A → 4G 字</text>
    <text class="fig-muted" x="30" y="216" font-size="10.5">规律：OP 位数 = ⌈log₂ 操作种数⌉；Ri 位数 = ⌈log₂ 寄存器数⌉；特征位 = ⌈log₂ 寻址种数⌉；余下全给 A</text>
  </svg>
  <figcaption>图 7-10　习题 7.19 三种 32 位 R-S 型指令格式位分配与寻址空间对比</figcaption>
</figure>

> <strong>注意</strong>：扩大寻址空间应优先采用<strong>基址/变址寻址</strong>（可增大 $2^{12}$ 倍量级），而不是把单字节指令简单加长为双字节指令——后者硬件代价太高。
>
> <strong>★考点</strong>：设计题的固定套路——操作种数定 OP 位数；寄存器个数定寄存器号位数；寻址方式种数定特征位位数（$\lceil \log_2 M \rceil$）；指令字长与机器字长是否相等定总位数；总位数减去各字段即形式地址位数。

### 7.8 RISC 技术

早期指令集本不复杂，设计者为“面向目标程序、面向高级语言优化”，不断把常用操作固化成指令，指令集膨胀到三四百条。70 末 80 初人们开始反思“指令是否越多越好”，分析得出<strong>二八规律</strong>：典型程序中 <strong>80% 的语句只用到 20% 的指令</strong>，程序执行时 80% 的执行时间花在 20% 的简单指令上。

复杂指令带来两个恶果：控制器只能用<strong>微程序实现，硬件复杂；而那 20% 最常用的简单指令反被强功能指令拖慢（如同借一元钱却要走银行全套手续）。RISC 的思路是：只保留高频简单指令，复杂功能用这些简单指令组合实现</strong>。

<figure class="cpd-figure not-content" id="lhw-w5-fig-27">
  <svg viewBox="0 0 700 286" role="img" aria-label="RISC精简指令集与CISC复杂指令集在指令条数、格式、访存、控制器与流水线上的结构性差异对比">
    <rect class="fig-box-subtle" x="16" y="14" width="668" height="258" rx="8"/>
    <text class="fig-title" x="30" y="34" font-size="13">RISC 与 CISC 的结构性差异（二八规律驱动控制器变革）</text>
    <rect class="fig-box-blue" x="30" y="48" width="310" height="210" rx="6"/>
    <rect class="fig-box-red" x="360" y="48" width="310" height="210" rx="6"/>
    <text class="fig-blue" x="185" y="70" text-anchor="middle" font-size="13">RISC（精简指令集）</text>
    <text class="fig-red" x="515" y="70" text-anchor="middle" font-size="13">CISC（复杂指令集）</text>
    <text class="fig-label" x="44" y="94" font-size="11.5">· 指令少，只保留 20% 高频简单指令</text>
    <text class="fig-label" x="44" y="116" font-size="11.5">· 定长指令字、格式种类少、译码快</text>
    <text class="fig-label" x="44" y="138" font-size="11.5">· 寻址方式少，仅 Load/Store 访存</text>
    <text class="fig-label" x="44" y="160" font-size="11.5">· 大量通用寄存器（减少访存）</text>
    <text class="fig-label" x="44" y="182" font-size="11.5">· 硬布线（组合逻辑）控制，单周期</text>
    <text class="fig-label" x="44" y="204" font-size="11.5">· 易流水线，复杂功能靠编译器组合</text>
    <text class="fig-green" x="44" y="232" font-size="11.5">→ 芯片面积小、主频高，但兼容性较差</text>
    <text class="fig-label" x="374" y="94" font-size="11.5">· 指令多（数百条），频度符合二八规律</text>
    <text class="fig-label" x="374" y="116" font-size="11.5">· 变长指令、格式多样、译码复杂</text>
    <text class="fig-label" x="374" y="138" font-size="11.5">· 寻址方式多，运算指令可直接访存</text>
    <text class="fig-label" x="374" y="160" font-size="11.5">· 较多专用寄存器（变址 / 基址等）</text>
    <text class="fig-label" x="374" y="182" font-size="11.5">· 微程序控制，多时钟周期访问控存</text>
    <text class="fig-label" x="374" y="204" font-size="11.5">· 指令功能强，向后兼容性好</text>
    <text class="fig-orange" x="374" y="232" font-size="11.5">→ 硬件复杂，20% 常用指令反被拖慢</text>
  </svg>
  <figcaption>图 7-11　RISC 与 CISC 的结构性差异对比</figcaption>
</figure>

| 维度 | RISC | CISC |
|---|---|---|
| 指令条数 | 少，仅选高频简单指令 | 多，可达数百条 |
| 长度与格式 | 固定长度、格式种类少 | 长度不固定、格式多样 |
| 寻址方式 | 少 | 多，译码过程复杂 |
| 访存限制 | 只有 LOAD/STORE 访存，其余操作均在寄存器间、结果存寄存器 | 访存指令不受限制，算术指令可直接读写内存 |
| 寄存器 | 较多通用寄存器 | 较多专用寄存器（变址、基址） |
| 控制器 | 组合逻辑（硬布线），单周期 | 微程序控制，一条指令拆成多条微指令、多周期访问控制存储器 |
| 优点 | 控制器占用芯片面积小，省下的资源可做存储与流水；速度快；便于设计、成本低；结构简单、可靠性高 | 指令功能强，与老程序兼容 |
| 缺点 | 指令系统兼容性差，格式紧凑、后续增指令困难 | 硬件复杂，20% 的常用指令被拖慢 |

RISC 提高速度的手段：<strong>指令数少、格式固定、寻址方式少</strong> ⇒ 译码简单，操作码与操作数位置固定，译码的同时即可读操作数；<strong>Load/Store 结构 + 大量通用寄存器</strong>；<strong>流水线（目标是一个时钟周期完成一条指令）；硬布线控制</strong>。

RISC 的代价是<strong>兼容性差</strong>：指令格式设计紧凑，后续扩充指令困难。现代处理器的趋势是 RISC 与 CISC <strong>互相结合</strong>（哪些处理器用哪种结构、如何结合，属于后续体系结构课程与课外查阅的内容）。

## 第8章 CPU

### 8.1 CPU 的功能与结构

指令系统是软硬件的交界面，而<strong>机器指令真正在硬件上被执行</strong>，靠的是 CPU 从内存取指令、分析、执行并写回结果。CPU 的功能可归纳为五类，再加上三类控制：

| 功能 | 含义 | 承担部件 |
|---|---|---|
| 指令控制 | 取出指令，保证指令按序执行 | PC、IR |
| 操作控制 | 产生完成指令所需的各操作命令 | CU |
| 时间控制 | 各操作命令必须按先后顺序、在给定时刻发出 | CU（时序） |
| 数据加工 | 算术运算与逻辑运算 | ALU、通用寄存器 |
| 中断处理 | 处理异常事件与特殊请求 | 中断系统 |

此外控制器还要控制<strong>程序的输入与结果的输出</strong>、进行<strong>总线管理</strong>（总线控制权与使用权），并在程序运行中出现异常或特殊请求时及时处理。

CPU 由<strong>运算器与控制器</strong>两大部分构成，所以其功能既是控制器的功能，也是运算器的功能。结构上对应为：

- <strong>PC</strong>：给出要取的指令的地址，是“指令控制”的硬件基础；
- <strong>IR</strong>：存放从内存取出的当前指令，操作码送 CU 译码；
- <strong>CU</strong>：对指令译码，并在给定时刻给出给定操作命令，完成操作控制与时间控制；
- <strong>ALU + 通用寄存器</strong>：数据加工，操作数与结果都在寄存器中周转；
- <strong>中断系统</strong>：响应异常与特殊请求；
- 以及辅助电路和连接各部分的<strong>CPU 内部互联机构</strong>。

CPU 通过三组系统总线与外界通信：<strong>地址总线单向（CPU 发出，送主存或 I/O 接口）、数据总线双向</strong>、<strong>控制总线双向</strong>（CPU 向外发命令，外部向内提请求、送状态）。

<figure class="cpd-figure not-content" id="lhw-w6-fig-1">
  <svg viewBox="0 0 700 320" role="img" aria-label="CPU 内部结构与系统总线" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-1" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" 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>
    <text x="16" y="20" class="fig-title" font-size="13">CPU 内部结构与系统总线</text>
    <rect x="20" y="32" width="435" height="264" rx="4" class="fig-box-dashed"/>
    <text x="32" y="50" class="fig-label">CPU</text>
    <line x1="35" y1="165" x2="440" y2="165" class="fig-line-accent" stroke-width="2"/>
    <rect x="218" y="172" width="66" height="18" rx="3" class="fig-box"/>
    <text x="251" y="185" text-anchor="middle" class="fig-blue" font-size="11">内部总线</text>
    <line x1="92" y1="85" x2="92" y2="165" class="fig-line"/>
    <line x1="197" y1="85" x2="197" y2="165" class="fig-line"/>
    <line x1="300" y1="86" x2="300" y2="165" class="fig-line"/>
    <line x1="403" y1="86" x2="403" y2="165" class="fig-line"/>
    <line x1="92" y1="215" x2="92" y2="165" class="fig-line"/>
    <line x1="197" y1="215" x2="197" y2="165" class="fig-line"/>
    <line x1="300" y1="210" x2="300" y2="165" class="fig-line"/>
    <line x1="403" y1="210" x2="403" y2="165" class="fig-line"/>
    <rect x="45" y="55" width="95" height="30" rx="3" class="fig-box"/>
    <text x="92" y="75" text-anchor="middle" class="fig-label">PC</text>
    <rect x="150" y="55" width="95" height="30" rx="3" class="fig-box"/>
    <text x="197" y="75" text-anchor="middle" class="fig-label">IR</text>
    <rect x="255" y="50" width="90" height="36" rx="3" class="fig-box"/>
    <text x="300" y="73" text-anchor="middle" class="fig-label">通用寄存器</text>
    <rect x="355" y="50" width="96" height="36" rx="3" class="fig-box"/>
    <text x="403" y="73" text-anchor="middle" class="fig-label">中断系统</text>
    <rect x="45" y="215" width="95" height="30" rx="3" class="fig-box"/>
    <text x="92" y="235" text-anchor="middle" class="fig-label">MAR</text>
    <rect x="150" y="215" width="95" height="30" rx="3" class="fig-box"/>
    <text x="197" y="235" text-anchor="middle" class="fig-label">MDR</text>
    <rect x="255" y="210" width="90" height="45" rx="3" class="fig-box"/>
    <text x="300" y="238" text-anchor="middle" class="fig-label">ALU</text>
    <rect x="355" y="210" width="96" height="45" rx="3" class="fig-box-blue"/>
    <text x="403" y="238" text-anchor="middle" class="fig-blue">控制单元 CU</text>
    <line x1="455" y1="112" x2="558" y2="112" class="fig-line-accent" marker-end="url(#lhw-w6-arr-1)"/>
    <text x="466" y="105" class="fig-blue">地址总线（单向）</text>
    <line x1="461" y1="165" x2="558" y2="165" class="fig-line-accent" marker-start="url(#lhw-w6-arr-1)" marker-end="url(#lhw-w6-arr-1)"/>
    <text x="466" y="158" class="fig-blue">数据总线（双向）</text>
    <line x1="461" y1="255" x2="558" y2="255" class="fig-line-accent" marker-start="url(#lhw-w6-arr-1)" marker-end="url(#lhw-w6-arr-1)"/>
    <text x="466" y="248" class="fig-blue">控制总线（双向）</text>
    <rect x="565" y="45" width="118" height="235" rx="4" class="fig-box-subtle"/>
    <text x="624" y="150" text-anchor="middle" class="fig-label">主存 /</text>
    <text x="624" y="172" text-anchor="middle" class="fig-label">I/O 接口</text>
  </svg>
  <figcaption>图 8-1　CPU 内部结构与三类系统总线</figcaption>
</figure>

> <strong>★考点</strong>：CPU 五大功能与对应部件；指令控制靠 PC/IR，操作控制与时间控制靠 CU；三类总线中只有<strong>地址总线单向</strong>。

### 8.2 指令周期与 CPU 工作周期标志

<strong>指令周期</strong>是从取出一条指令开始，到这条指令执行结束（结果写回）为止的全部时间，即解释一条指令所需的全部时间。

解释一条指令包含：取指令 → 分析（操作码送 CU 译码、分析寻址方式）→ 执行 → 写回。教材把<strong>取指与分析合并为取指周期</strong>，把取操作数、运算、写回合并为<strong>执行周期</strong>。这只是教材给出的一种划分示例：不同 CPU、不同指令集可以把一条指令分成不同阶段，必须具体分析。

不同指令的指令周期长度并不相同，即使同一 CPU 内也是如此：

| 情形 | 周期构成 | 原因 |
|---|---|---|
| NOP（空操作） | 只有取指周期 | 不需要执行任何操作 |
| ADD mem（内存操作数与 ACC 相加） | 取指 + 执行，两段<strong>等长</strong> | 取指访存一次，执行取操作数再访存一次，访存占主导 |
| MUL mem（乘法） | 取指 + 执行，执行<strong>更长</strong> | 乘法所需时钟周期多 |
| 间接寻址指令 | 取指 + <strong>间址</strong> + 执行 | 间址阶段需访存取出操作数的地址，执行阶段再访存取操作数 |
| 支持中断时的复杂指令 | 取指 + 间址 + 执行 + <strong>中断</strong> | 执行周期结束时确认中断请求，响应需保存断点、形成入口、关中断 |

对应的指令周期流程是<strong>有分支的</strong>：先取指；判断是否采用间址寻址，是则进入间址周期；间址结束（或不用间址）进入执行周期；执行结束判断有无中断请求，有则进入中断周期；无中断或中断周期结束后回到取指周期，开始下一条指令。注意这是<strong>一条指令的解释流程</strong>，不是程序流程图。

<figure class="cpd-figure not-content" id="lhw-w6-fig-2">
  <svg viewBox="0 0 660 380" role="img" aria-label="指令周期流程" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-2" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="20" class="fig-title" font-size="13">指令周期流程</text>
    <line x1="140" y1="62" x2="140" y2="80" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <line x1="220" y1="110" x2="346" y2="110" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <line x1="415" y1="127" x2="415" y2="191" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <polyline points="140,138 140,212 346,212" fill="none" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <line x1="415" y1="229" x2="415" y2="246" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <line x1="335" y1="278" x2="274" y2="278" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <polyline points="415,306 415,345 40,345 40,45 76,45" fill="none" class="fig-line" marker-end="url(#lhw-w6-arr-2)"/>
    <line x1="210" y1="295" x2="210" y2="345" class="fig-line"/>
    <rect x="80" y="28" width="120" height="34" rx="4" class="fig-box-blue"/>
    <text x="140" y="49" text-anchor="middle" class="fig-blue">取指周期</text>
    <polygon points="140,82 220,110 140,138 60,110" class="fig-box"/>
    <text x="140" y="114" text-anchor="middle" class="fig-label">间址寻址？</text>
    <rect x="350" y="93" width="130" height="34" rx="4" class="fig-box-subtle"/>
    <text x="415" y="114" text-anchor="middle" class="fig-label">间址周期</text>
    <rect x="350" y="195" width="130" height="34" rx="4" class="fig-box-green"/>
    <text x="415" y="216" text-anchor="middle" class="fig-green">执行周期</text>
    <polygon points="415,250 495,278 415,306 335,278" class="fig-box"/>
    <text x="415" y="282" text-anchor="middle" class="fig-label">有中断请求？</text>
    <rect x="150" y="261" width="120" height="34" rx="4" class="fig-box-red"/>
    <text x="210" y="282" text-anchor="middle" class="fig-red">中断周期</text>
    <text x="232" y="103" class="fig-muted">是</text>
    <text x="150" y="158" class="fig-muted">否</text>
    <text x="300" y="270" class="fig-muted">是</text>
    <text x="424" y="332" class="fig-muted">否</text>
  </svg>
  <figcaption>图 8-2　一条指令的解释流程（取指、间址、执行、中断）</figcaption>
</figure>

<strong>CPU 工作周期的标志</strong>：不同阶段对同一部件（尤其存储器）的操作目的不同，控制器必须知道自己处于哪个阶段才能发正确的命令，因此用 <strong>D 触发器</strong>分别标识取指、间址、执行、中断四个周期。

存储器在同一指令周期的不同阶段被访问时，读回的数据性质完全不同：

| 阶段 | 访存操作 | 数据性质 | 去向 |
|---|---|---|---|
| 取指周期 | 读 | 指令 | → MDR → IR |
| 间址周期 | 读 | 操作数的<strong>地址</strong> | → MDR（或 IR 的地址码字段） |
| 执行周期 | 读 | 操作数 | → CPU 内寄存器，供运算 |
| 中断周期 | 写 | 程序断点 | → 指定内存单元 |

> <strong>★考点</strong>：指令周期定义；NOP 只有取指周期；间址周期访存取的是“地址”而非操作数；四个周期用 D 触发器标识。

<figure class="cpd-figure not-content" id="lhw-w6-fig-3">
  <svg viewBox="0 0 660 330" role="img" aria-label="不同指令的指令周期长度对比" font-family="sans-serif" font-size="12">
    <text x="16" y="20" class="fig-title" font-size="13">不同指令的指令周期长度对比（每格 1 个机器周期）</text>
    <line x1="110" y1="40" x2="110" y2="278" class="fig-line-dashed"/>
    <line x1="165" y1="40" x2="165" y2="278" class="fig-line-dashed"/>
    <line x1="220" y1="40" x2="220" y2="278" class="fig-line-dashed"/>
    <line x1="275" y1="40" x2="275" y2="278" class="fig-line-dashed"/>
    <line x1="330" y1="40" x2="330" y2="278" class="fig-line-dashed"/>
    <line x1="385" y1="40" x2="385" y2="278" class="fig-line-dashed"/>
    <text x="100" y="73" text-anchor="end" class="fig-label">NOP</text>
    <rect x="110" y="55" width="55" height="28" rx="2" class="fig-box-blue"/>
    <text x="137" y="73" text-anchor="middle" class="fig-blue">取指</text>
    <line x1="170" y1="69" x2="394" y2="69" class="fig-line-dashed"/>
    <text x="400" y="73" class="fig-muted">无执行周期</text>
    <text x="100" y="118" text-anchor="end" class="fig-label">ADD mem</text>
    <rect x="110" y="100" width="55" height="28" rx="2" class="fig-box-blue"/>
    <text x="137" y="118" text-anchor="middle" class="fig-blue">取指</text>
    <rect x="165" y="100" width="55" height="28" rx="2" class="fig-box-green"/>
    <text x="192" y="118" text-anchor="middle" class="fig-green">执行</text>
    <line x1="224" y1="114" x2="394" y2="114" class="fig-line-dashed"/>
    <text x="400" y="118" class="fig-muted">两段等长（各访存一次）</text>
    <text x="100" y="163" text-anchor="end" class="fig-label">MUL mem</text>
    <rect x="110" y="145" width="55" height="28" rx="2" class="fig-box-blue"/>
    <text x="137" y="163" text-anchor="middle" class="fig-blue">取指</text>
    <rect x="165" y="145" width="110" height="28" rx="2" class="fig-box-green"/>
    <text x="220" y="163" text-anchor="middle" class="fig-green">执行</text>
    <text x="100" y="208" text-anchor="end" class="fig-label">间接寻址</text>
    <rect x="110" y="190" width="55" height="28" rx="2" class="fig-box-blue"/>
    <text x="137" y="208" text-anchor="middle" class="fig-blue">取指</text>
    <rect x="165" y="190" width="55" height="28" rx="2" class="fig-box-subtle"/>
    <text x="192" y="208" text-anchor="middle" class="fig-label">间址</text>
    <rect x="220" y="190" width="55" height="28" rx="2" class="fig-box-green"/>
    <text x="247" y="208" text-anchor="middle" class="fig-green">执行</text>
    <text x="100" y="253" text-anchor="end" class="fig-label">含中断</text>
    <rect x="110" y="235" width="55" height="28" rx="2" class="fig-box-blue"/>
    <text x="137" y="253" text-anchor="middle" class="fig-blue">取指</text>
    <rect x="165" y="235" width="55" height="28" rx="2" class="fig-box-green"/>
    <text x="192" y="253" text-anchor="middle" class="fig-green">执行</text>
    <rect x="220" y="235" width="55" height="28" rx="2" class="fig-box-red"/>
    <text x="247" y="253" text-anchor="middle" class="fig-red">中断</text>
    <line x1="110" y1="282" x2="385" y2="282" class="fig-line"/>
    <text x="137" y="302" text-anchor="middle" class="fig-label">1</text>
    <text x="192" y="302" text-anchor="middle" class="fig-label">2</text>
    <text x="247" y="302" text-anchor="middle" class="fig-label">3</text>
    <text x="302" y="302" text-anchor="middle" class="fig-label">4</text>
    <text x="357" y="302" text-anchor="middle" class="fig-label">5</text>
    <text x="400" y="302" class="fig-muted">机器周期</text>
  </svg>
  <figcaption>图 8-3　不同指令的指令周期长度对比</figcaption>
</figure>

### 8.2 指令周期的数据流

<strong>取指周期的数据流</strong>（PC → MAR → 地址总线 → 存储器；数据总线 → MDR → IR）：

1. PC 给出要取指令的地址，送 <strong>MAR</strong>，经<strong>地址总线</strong>送存储器；
2. CU 经<strong>控制总线</strong>发<strong>读</strong>命令；
3. 存储器把指令送<strong>数据总线</strong>，写入 <strong>MDR</strong>，即指令已进入 CPU；
4. MDR → <strong>IR</strong>（专用指令寄存器）；
5. CU 控制 PC 加一（无转移时），<strong>为取下一条指令做准备</strong>，写回 PC。

<strong>间址周期的数据流</strong>（MDR 的地址码 → MAR → 地址总线 → 存储器；数据总线 → MDR）：

1. 指令的地址码部分（在 IR/MDR 中）送 <strong>MAR</strong>，经地址总线送存储器；
2. CU 发<strong>读</strong>命令；
3. 存储器送回的是<strong>操作数的地址</strong>，送 <strong>MDR</strong>，替换原来的形式地址。

<strong>中断周期的数据流</strong>（保存断点、形成入口、关中断）：

1. CU 给出存放断点的内存单元地址 → <strong>MAR</strong> → 地址总线；同时经控制总线发<strong>写</strong>命令；
2. <strong>PC</strong>（下一条要执行的指令地址，即断点）→ <strong>MDR</strong> → 数据总线 → 写入该存储单元；
3. CU 把<strong>中断服务程序入口地址</strong>直接送入 <strong>PC</strong>，随后即从中断服务程序开始执行；
4. 硬件<strong>关中断</strong>（这三项操作合称中断隐指令）。

执行周期的数据流因指令而异（取操作数、运算、写回），在本章不展开。全过程中所有部件动作均由 CU 控制，取指末的 <strong>PC+1</strong> 一步不能遗漏，否则下一条指令取错。

> <strong>★考点</strong>：取指数据流的完整顺序（PC→MAR→M→MDR→IR，最后 PC+1）；间址周期读回的是地址；中断周期由硬件完成“关中断、保存断点（PC 值）、形成入口地址送 PC”。

### 8.3 并行性与指令流水原理

提高整机速度的途径有三：提高存储器速度（高速芯片、Cache、多体交叉）、提高 I/O 与主机间传送速度（程序查询 → 中断 → DMA → 通道 → I/O 处理机）、提高运算速度（高速芯片、改进算法、快速进位）。除此之外最有效的一条是<strong>改进体系结构、开发并行性</strong>。

<strong>并行性有两层含义</strong>：

| 概念 | 含义 | 例子 |
|---|---|---|
| 并发 | 两个或多个事件在<strong>同一时间段</strong>内发生，分时轮流占用资源 | 分时操作系统多道程序，各占一个时间片 |
| 同时 | 两个或多个事件在<strong>同一时刻</strong>发生，时间上重叠 | 流水线 |

<strong>并行性的等级</strong>：程序级、过程级（粗粒度，通常由<strong>软件实现，如操作系统调度）；指令级并行 ILP</strong>（两条或多条指令同时处于被解释/被执行状态，细粒度，由<strong>硬件</strong>实现）；指令内部各微操作之间并行。流水线属于<strong>同时性</strong>，是指令级并行的硬件实现。

<strong>串行执行的浪费</strong>：取指阶段只用取指部件，执行部件空闲；执行阶段只用执行部件，取指部件空闲——总有一个部件闲置，控制器利用率很低。

<strong>二级流水：把取指与执行重叠，执行第 1 条指令的同时取第 2 条，执行第 2 条时取第 3 条……取指部件与执行部件都被充分利用，理想情况下指令周期减半，速度提高一倍</strong>。

<figure class="cpd-figure not-content" id="lhw-w6-fig-4">
  <svg viewBox="0 0 660 240" role="img" aria-label="串行执行与二级流水" font-family="sans-serif" font-size="12">
    <text x="16" y="20" class="fig-title" font-size="13">串行执行与二级流水（每格 1 个机器周期）</text>
    <text x="62" y="78" text-anchor="end" class="fig-label">串行</text>
    <rect x="110" y="60" width="50" height="28" rx="2" class="fig-box-blue"/>
    <text x="135" y="78" text-anchor="middle" class="fig-blue">取</text>
    <rect x="160" y="60" width="50" height="28" rx="2" class="fig-box-green"/>
    <text x="185" y="78" text-anchor="middle" class="fig-green">执</text>
    <rect x="210" y="60" width="50" height="28" rx="2" class="fig-box-blue"/>
    <text x="235" y="78" text-anchor="middle" class="fig-blue">取</text>
    <rect x="260" y="60" width="50" height="28" rx="2" class="fig-box-green"/>
    <text x="285" y="78" text-anchor="middle" class="fig-green">执</text>
    <rect x="310" y="60" width="50" height="28" rx="2" class="fig-box-blue"/>
    <text x="335" y="78" text-anchor="middle" class="fig-blue">取</text>
    <rect x="360" y="60" width="50" height="28" rx="2" class="fig-box-green"/>
    <text x="385" y="78" text-anchor="middle" class="fig-green">执</text>
    <rect x="410" y="60" width="50" height="28" rx="2" class="fig-box-blue"/>
    <text x="435" y="78" text-anchor="middle" class="fig-blue">取</text>
    <rect x="460" y="60" width="50" height="28" rx="2" class="fig-box-green"/>
    <text x="485" y="78" text-anchor="middle" class="fig-green">执</text>
    <text x="520" y="78" class="fig-muted">4 条指令 8 个周期</text>
    <text x="62" y="140" text-anchor="end" class="fig-label">二级流水</text>
    <rect x="110" y="110" width="50" height="24" rx="2" class="fig-box-blue"/>
    <text x="135" y="126" text-anchor="middle" class="fig-blue">取1</text>
    <rect x="160" y="110" width="50" height="24" rx="2" class="fig-box-blue"/>
    <text x="185" y="126" text-anchor="middle" class="fig-blue">取2</text>
    <rect x="210" y="110" width="50" height="24" rx="2" class="fig-box-blue"/>
    <text x="235" y="126" text-anchor="middle" class="fig-blue">取3</text>
    <rect x="260" y="110" width="50" height="24" rx="2" class="fig-box-blue"/>
    <text x="285" y="126" text-anchor="middle" class="fig-blue">取4</text>
    <rect x="160" y="138" width="50" height="24" rx="2" class="fig-box-green"/>
    <text x="185" y="154" text-anchor="middle" class="fig-green">执1</text>
    <rect x="210" y="138" width="50" height="24" rx="2" class="fig-box-green"/>
    <text x="235" y="154" text-anchor="middle" class="fig-green">执2</text>
    <rect x="260" y="138" width="50" height="24" rx="2" class="fig-box-green"/>
    <text x="285" y="154" text-anchor="middle" class="fig-green">执3</text>
    <rect x="310" y="138" width="50" height="24" rx="2" class="fig-box-green"/>
    <text x="335" y="154" text-anchor="middle" class="fig-green">执4</text>
    <text x="380" y="140" class="fig-muted">5 个周期完成，每 Δt 输出一条</text>
  </svg>
  <figcaption>图 8-4　串行执行与二级指令流水对比</figcaption>
</figure>

<strong>影响流水效率的因素</strong>（以二级流水为例，现代处理器段数更多）：

- <strong>执行时间过长</strong>：取指格式统一、时间固定，而复杂指令的执行要长得多。解决：在取指与执行部件之间加<strong>指令缓冲区</strong>，取出的指令先缓存，执行部件空闲即从缓冲区取。
- <strong>条件转移指令：无条件转移影响小，译码阶段即可知道它是转移并得到转移地址；条件转移</strong>必须等执行阶段产生结果，才能判断条件是否成立、下一条地址是多少，时间损失大。程序中约 <strong>3～5 条指令就有一条转移指令。解决：分支预测</strong>（最简单是猜测法，其他方法可自行查阅）。

<strong>六级流水线示例</strong>：取指、译码、形成操作数地址、取操作数、执行、写结果。各段时间相同，9 条指令串行需 $9\times6=54$ 个时间单位；流水只需 <strong>14</strong> 个时间单位（$6+9-1=14$）。

### 8.3 流水线的性能指标

设流水线分 <strong>m</strong> 段，各段时间均为 <strong>Δt</strong>，连续处理 <strong>n</strong> 条指令。

<strong>吞吐率 TP</strong>：单位时间内流水线完成的指令条数（或输出的结果数）。满负荷、无资源冲突、无数据相关、无转移指令时的<strong>最大吞吐率</strong>为每 Δt 输出一个结果：

$$
TP_{\max}=\frac{1}{\Delta t}
$$

n 条指令的实际执行时间 = 第 1 条指令充满流水线的 $m\Delta t$ + 其余 $n-1$ 条每 Δt 流出一个：

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

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

<strong>加速比 S</strong>：不用流水线时每条指令都要 $m\Delta t$，n 条共 $nm\Delta t$：

$$
S=\frac{nm\Delta t}{(m+n-1)\Delta t}=\frac{nm}{m+n-1}
$$

<strong>效率 E</strong>：流水线硬件的利用率，即<strong>时空图中有阴影的工作面积与总面积之比</strong>（总面积 = m 个流水段 × T）：

$$
E=\frac{nm\Delta t}{m(m+n-1)\Delta t}=\frac{n}{m+n-1}
$$

由此可得三者关系：

$$
S=mE
$$

<strong>建立时间指第 1 条指令从流入到流出（此间后面的段逐个被填满）；排空时间</strong>指最后一条指令从进入到流出。这两段时间以及冲突、相关造成的停顿使各功能段无法始终工作，所以实际效率总低于 1。n 越大，建立与排空所占比例越小，S 越接近 m、E 越接近 1；n 有限时三者都受段数 m 制约。

<figure class="cpd-figure not-content" id="lhw-w6-fig-5">
  <svg viewBox="0 0 660 360" role="img" aria-label="六级流水线时空图" font-family="sans-serif" font-size="12">
    <text x="16" y="20" class="fig-title" font-size="13">六级流水线时空图（9 条指令，T = 14Δt）</text>
    <line x1="120" y1="45" x2="120" y2="292" class="fig-line-dashed"/>
    <line x1="624" y1="45" x2="624" y2="292" class="fig-line-dashed"/>
    <text x="105" y="76" text-anchor="end" class="fig-label">I1</text>
    <rect x="120" y="58" width="36" height="26" class="fig-box-blue"/><text x="138" y="75" text-anchor="middle" class="fig-blue">1</text>
    <rect x="156" y="58" width="36" height="26" class="fig-box-blue"/><text x="174" y="75" text-anchor="middle" class="fig-blue">2</text>
    <rect x="192" y="58" width="36" height="26" class="fig-box-blue"/><text x="210" y="75" text-anchor="middle" class="fig-blue">3</text>
    <rect x="228" y="58" width="36" height="26" class="fig-box-blue"/><text x="246" y="75" text-anchor="middle" class="fig-blue">4</text>
    <rect x="264" y="58" width="36" height="26" class="fig-box-blue"/><text x="282" y="75" text-anchor="middle" class="fig-blue">5</text>
    <rect x="300" y="58" width="36" height="26" class="fig-box-blue"/><text x="318" y="75" text-anchor="middle" class="fig-blue">6</text>
    <text x="105" y="102" text-anchor="end" class="fig-label">I2</text>
    <rect x="156" y="84" width="36" height="26" class="fig-box-subtle"/><text x="174" y="101" text-anchor="middle" class="fig-muted">1</text>
    <rect x="192" y="84" width="36" height="26" class="fig-box-subtle"/><text x="210" y="101" text-anchor="middle" class="fig-muted">2</text>
    <rect x="228" y="84" width="36" height="26" class="fig-box-subtle"/><text x="246" y="101" text-anchor="middle" class="fig-muted">3</text>
    <rect x="264" y="84" width="36" height="26" class="fig-box-subtle"/><text x="282" y="101" text-anchor="middle" class="fig-muted">4</text>
    <rect x="300" y="84" width="36" height="26" class="fig-box-subtle"/><text x="318" y="101" text-anchor="middle" class="fig-muted">5</text>
    <rect x="336" y="84" width="36" height="26" class="fig-box-subtle"/><text x="354" y="101" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="128" text-anchor="end" class="fig-label">I3</text>
    <rect x="192" y="110" width="36" height="26" class="fig-box-subtle"/><text x="210" y="127" text-anchor="middle" class="fig-muted">1</text>
    <rect x="228" y="110" width="36" height="26" class="fig-box-subtle"/><text x="246" y="127" text-anchor="middle" class="fig-muted">2</text>
    <rect x="264" y="110" width="36" height="26" class="fig-box-subtle"/><text x="282" y="127" text-anchor="middle" class="fig-muted">3</text>
    <rect x="300" y="110" width="36" height="26" class="fig-box-subtle"/><text x="318" y="127" text-anchor="middle" class="fig-muted">4</text>
    <rect x="336" y="110" width="36" height="26" class="fig-box-subtle"/><text x="354" y="127" text-anchor="middle" class="fig-muted">5</text>
    <rect x="372" y="110" width="36" height="26" class="fig-box-subtle"/><text x="390" y="127" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="154" text-anchor="end" class="fig-label">I4</text>
    <rect x="228" y="136" width="36" height="26" class="fig-box-subtle"/><text x="246" y="153" text-anchor="middle" class="fig-muted">1</text>
    <rect x="264" y="136" width="36" height="26" class="fig-box-subtle"/><text x="282" y="153" text-anchor="middle" class="fig-muted">2</text>
    <rect x="300" y="136" width="36" height="26" class="fig-box-subtle"/><text x="318" y="153" text-anchor="middle" class="fig-muted">3</text>
    <rect x="336" y="136" width="36" height="26" class="fig-box-subtle"/><text x="354" y="153" text-anchor="middle" class="fig-muted">4</text>
    <rect x="372" y="136" width="36" height="26" class="fig-box-subtle"/><text x="390" y="153" text-anchor="middle" class="fig-muted">5</text>
    <rect x="408" y="136" width="36" height="26" class="fig-box-subtle"/><text x="426" y="153" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="180" text-anchor="end" class="fig-label">I5</text>
    <rect x="264" y="162" width="36" height="26" class="fig-box-subtle"/><text x="282" y="179" text-anchor="middle" class="fig-muted">1</text>
    <rect x="300" y="162" width="36" height="26" class="fig-box-subtle"/><text x="318" y="179" text-anchor="middle" class="fig-muted">2</text>
    <rect x="336" y="162" width="36" height="26" class="fig-box-subtle"/><text x="354" y="179" text-anchor="middle" class="fig-muted">3</text>
    <rect x="372" y="162" width="36" height="26" class="fig-box-subtle"/><text x="390" y="179" text-anchor="middle" class="fig-muted">4</text>
    <rect x="408" y="162" width="36" height="26" class="fig-box-subtle"/><text x="426" y="179" text-anchor="middle" class="fig-muted">5</text>
    <rect x="444" y="162" width="36" height="26" class="fig-box-subtle"/><text x="462" y="179" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="206" text-anchor="end" class="fig-label">I6</text>
    <rect x="300" y="188" width="36" height="26" class="fig-box-subtle"/><text x="318" y="205" text-anchor="middle" class="fig-muted">1</text>
    <rect x="336" y="188" width="36" height="26" class="fig-box-subtle"/><text x="354" y="205" text-anchor="middle" class="fig-muted">2</text>
    <rect x="372" y="188" width="36" height="26" class="fig-box-subtle"/><text x="390" y="205" text-anchor="middle" class="fig-muted">3</text>
    <rect x="408" y="188" width="36" height="26" class="fig-box-subtle"/><text x="426" y="205" text-anchor="middle" class="fig-muted">4</text>
    <rect x="444" y="188" width="36" height="26" class="fig-box-subtle"/><text x="462" y="205" text-anchor="middle" class="fig-muted">5</text>
    <rect x="480" y="188" width="36" height="26" class="fig-box-subtle"/><text x="498" y="205" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="232" text-anchor="end" class="fig-label">I7</text>
    <rect x="336" y="214" width="36" height="26" class="fig-box-subtle"/><text x="354" y="231" text-anchor="middle" class="fig-muted">1</text>
    <rect x="372" y="214" width="36" height="26" class="fig-box-subtle"/><text x="390" y="231" text-anchor="middle" class="fig-muted">2</text>
    <rect x="408" y="214" width="36" height="26" class="fig-box-subtle"/><text x="426" y="231" text-anchor="middle" class="fig-muted">3</text>
    <rect x="444" y="214" width="36" height="26" class="fig-box-subtle"/><text x="462" y="231" text-anchor="middle" class="fig-muted">4</text>
    <rect x="480" y="214" width="36" height="26" class="fig-box-subtle"/><text x="498" y="231" text-anchor="middle" class="fig-muted">5</text>
    <rect x="516" y="214" width="36" height="26" class="fig-box-subtle"/><text x="534" y="231" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="258" text-anchor="end" class="fig-label">I8</text>
    <rect x="372" y="240" width="36" height="26" class="fig-box-subtle"/><text x="390" y="257" text-anchor="middle" class="fig-muted">1</text>
    <rect x="408" y="240" width="36" height="26" class="fig-box-subtle"/><text x="426" y="257" text-anchor="middle" class="fig-muted">2</text>
    <rect x="444" y="240" width="36" height="26" class="fig-box-subtle"/><text x="462" y="257" text-anchor="middle" class="fig-muted">3</text>
    <rect x="480" y="240" width="36" height="26" class="fig-box-subtle"/><text x="498" y="257" text-anchor="middle" class="fig-muted">4</text>
    <rect x="516" y="240" width="36" height="26" class="fig-box-subtle"/><text x="534" y="257" text-anchor="middle" class="fig-muted">5</text>
    <rect x="552" y="240" width="36" height="26" class="fig-box-subtle"/><text x="570" y="257" text-anchor="middle" class="fig-muted">6</text>
    <text x="105" y="284" text-anchor="end" class="fig-label">I9</text>
    <rect x="408" y="266" width="36" height="26" class="fig-box-green"/><text x="426" y="283" text-anchor="middle" class="fig-green">1</text>
    <rect x="444" y="266" width="36" height="26" class="fig-box-green"/><text x="462" y="283" text-anchor="middle" class="fig-green">2</text>
    <rect x="480" y="266" width="36" height="26" class="fig-box-green"/><text x="498" y="283" text-anchor="middle" class="fig-green">3</text>
    <rect x="516" y="266" width="36" height="26" class="fig-box-green"/><text x="534" y="283" text-anchor="middle" class="fig-green">4</text>
    <rect x="552" y="266" width="36" height="26" class="fig-box-green"/><text x="570" y="283" text-anchor="middle" class="fig-green">5</text>
    <rect x="588" y="266" width="36" height="26" class="fig-box-green"/><text x="606" y="283" text-anchor="middle" class="fig-green">6</text>
    <line x1="120" y1="292" x2="624" y2="292" class="fig-line"/>
    <text x="138" y="307" text-anchor="middle" class="fig-label">1</text>
    <text x="318" y="307" text-anchor="middle" class="fig-label">6</text>
    <text x="606" y="307" text-anchor="middle" class="fig-label">14</text>
    <text x="630" y="307" class="fig-muted">Δt</text>
    <text x="120" y="330" class="fig-muted">1 取指　2 译码　3 形成操作数地址　4 取操作数　5 执行　6 写结果</text>
    <text x="120" y="348" class="fig-muted">T = (6 + 9 − 1)Δt = 14Δt，串行需 9×6 = 54Δt</text>
  </svg>
  <figcaption>图 8-5　六级流水线时空图（9 条指令，T = 14Δt）</figcaption>
</figure>

> <strong>★考点</strong>：$TP=n/[(m+n-1)\Delta t]$、$S=nm/(m+n-1)$、$E=n/(m+n-1)$、$S=mE$ 四个公式；建立时间与排空时间使 E 达不到 1；时空图面积比即效率。

<figure class="cpd-figure not-content" id="lhw-w6-fig-8">
  <svg viewBox="0 0 700 266" role="img" aria-label="五级流水线数据相关与转发对比" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-4" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="20" class="fig-title" font-size="13">五级流水线 RAW 数据相关：停顿阻塞 vs 旁路转发</text>
    <text x="16" y="46" class="fig-muted">示例：I1: ADD R1, R2, R3　→　I2: SUB R4, R1, R5（I2 在 EX 段需要 I1 写入 R1 的结果）</text>
    <text x="16" y="78" class="fig-red">无转发（停顿 2 拍）</text>
    <text x="16" y="102" class="fig-label">I1 (ADD)</text>
    <rect x="130" y="86" width="52" height="24" rx="2" class="fig-box-blue"/><text x="156" y="102" text-anchor="middle" class="fig-blue">IF</text>
    <rect x="184" y="86" width="52" height="24" rx="2" class="fig-box-blue"/><text x="210" y="102" text-anchor="middle" class="fig-blue">ID</text>
    <rect x="238" y="86" width="52" height="24" rx="2" class="fig-box-green"/><text x="264" y="102" text-anchor="middle" class="fig-green">EX</text>
    <rect x="292" y="86" width="52" height="24" rx="2" class="fig-box-subtle"/><text x="318" y="102" text-anchor="middle" class="fig-muted">MEM</text>
    <rect x="346" y="86" width="52" height="24" rx="2" class="fig-box-orange"/><text x="372" y="102" text-anchor="middle" class="fig-orange">WB</text>
    <text x="16" y="132" class="fig-label">I2 (SUB)</text>
    <rect x="184" y="116" width="52" height="24" rx="2" class="fig-box-blue"/><text x="210" y="132" text-anchor="middle" class="fig-blue">IF</text>
    <rect x="238" y="116" width="52" height="24" rx="2" class="fig-box-red"/><text x="264" y="132" text-anchor="middle" class="fig-red">阻塞</text>
    <rect x="292" y="116" width="52" height="24" rx="2" class="fig-box-red"/><text x="318" y="132" text-anchor="middle" class="fig-red">阻塞</text>
    <rect x="346" y="116" width="52" height="24" rx="2" class="fig-box-blue"/><text x="372" y="132" text-anchor="middle" class="fig-blue">ID</text>
    <rect x="400" y="116" width="52" height="24" rx="2" class="fig-box-green"/><text x="426" y="132" text-anchor="middle" class="fig-green">EX</text>
    <rect x="454" y="116" width="52" height="24" rx="2" class="fig-box-subtle"/><text x="480" y="132" text-anchor="middle" class="fig-muted">MEM</text>
    <rect x="508" y="116" width="52" height="24" rx="2" class="fig-box-orange"/><text x="534" y="132" text-anchor="middle" class="fig-orange">WB</text>
    <text x="16" y="176" class="fig-green">旁路转发（0 停顿）</text>
    <text x="16" y="200" class="fig-label">I1 (ADD)</text>
    <rect x="130" y="184" width="52" height="24" rx="2" class="fig-box-blue"/><text x="156" y="200" text-anchor="middle" class="fig-blue">IF</text>
    <rect x="184" y="184" width="52" height="24" rx="2" class="fig-box-blue"/><text x="210" y="200" text-anchor="middle" class="fig-blue">ID</text>
    <rect x="238" y="184" width="52" height="24" rx="2" class="fig-box-green"/><text x="264" y="200" text-anchor="middle" class="fig-green">EX</text>
    <rect x="292" y="184" width="52" height="24" rx="2" class="fig-box-subtle"/><text x="318" y="200" text-anchor="middle" class="fig-muted">MEM</text>
    <rect x="346" y="184" width="52" height="24" rx="2" class="fig-box-orange"/><text x="372" y="200" text-anchor="middle" class="fig-orange">WB</text>
    <text x="16" y="236" class="fig-label">I2 (SUB)</text>
    <rect x="184" y="220" width="52" height="24" rx="2" class="fig-box-blue"/><text x="210" y="236" text-anchor="middle" class="fig-blue">IF</text>
    <rect x="238" y="220" width="52" height="24" rx="2" class="fig-box-blue"/><text x="264" y="236" text-anchor="middle" class="fig-blue">ID</text>
    <rect x="292" y="220" width="52" height="24" rx="2" class="fig-box-green"/><text x="318" y="236" text-anchor="middle" class="fig-green">EX</text>
    <rect x="346" y="220" width="52" height="24" rx="2" class="fig-box-subtle"/><text x="372" y="236" text-anchor="middle" class="fig-muted">MEM</text>
    <rect x="400" y="220" width="52" height="24" rx="2" class="fig-box-orange"/><text x="426" y="236" text-anchor="middle" class="fig-orange">WB</text>
    <path d="M 274 208 L 274 214 L 308 214 L 308 219" fill="none" class="fig-line-accent" marker-end="url(#lhw-w6-arr-4)"/>
    <text x="468" y="236" class="fig-blue">EX/MEM → EX 直接转发</text>
  </svg>
  <figcaption>图 8-6　五级流水线 RAW 数据相关与旁路转发</figcaption>
</figure>

> <strong>注意</strong>：最大吞吐率只在“满负荷、无资源冲突、无数据相关、无转移”的理想条件下成立；实际吞吐率必须用“完成的任务数 ÷ 实际耗时”计算，不能直接套 $1/\Delta t$。

### 8.4 中断系统与中断请求

中断不只用于 I/O，还用于程序调试（如设置断点）和计算机系统异常事件的处理。<strong>中断源</strong>有五类：

| 中断源 | 典型例子 |
|---|---|
| 人为设置的中断 | 程序中编入<strong>访管指令</strong>，访问操作系统的管理程序 |
| 程序性事故 | 运算<strong>溢出</strong>、操作码不能识别、非法除法（除零） |
| 硬件故障 | 存储器故障、硬盘坏道、电源掉电 |
| I/O 设备 | 设备完成输入输出后请求 CPU 处理 |
| 外部事件 | 键盘上的中断键中断程序执行 |

中断系统要解决的问题（也是它的功能清单）：

1. 各中断源<strong>如何提出</strong>中断请求；
2. 多个中断源<strong>同时请求</strong>时响应哪一个；
3. CPU 在<strong>什么条件、什么时间、以什么方式</strong>响应中断；
4. 断点如何保存、现场如何恢复；
5. 如何<strong>寻找/形成中断服务程序的入口地址</strong>并送入 PC；
6. 如何恢复现场与返回；
7. 处理中断过程中<strong>又出现新中断</strong>怎么办。

这些功能用软件还是硬件实现，取决于对<strong>速度、设计复杂性、灵活性</strong>三者的折中；不同计算机的软硬件划分不同。

<strong>中断请求的提出</strong>：给每个中断源设置一个<strong>中断请求标志触发器</strong>，标记该源是否提出了请求。请求触发器可分散放在各中断源的接口电路中，也可集中在 CPU 的中断系统内；它们在<strong>逻辑上组成中断请求标志寄存器</strong>，每一位对应一个中断源。

<figure class="cpd-figure not-content" id="lhw-w6-fig-6">
  <svg viewBox="0 0 640 175" role="img" aria-label="中断请求标志寄存器" font-family="sans-serif" font-size="12">
    <text x="16" y="20" class="fig-title" font-size="13">中断请求标志寄存器（逻辑示意）</text>
    <rect x="100" y="50" width="60" height="40" class="fig-box-blue"/>
    <text x="130" y="44" text-anchor="middle" class="fig-label">位7</text>
    <text x="130" y="75" text-anchor="middle" class="fig-blue">掉电</text>
    <rect x="160" y="50" width="60" height="40" class="fig-box-blue"/>
    <text x="190" y="44" text-anchor="middle" class="fig-label">位6</text>
    <text x="190" y="75" text-anchor="middle" class="fig-blue">过热</text>
    <rect x="220" y="50" width="60" height="40" class="fig-box-blue"/>
    <text x="250" y="44" text-anchor="middle" class="fig-label">位5</text>
    <text x="250" y="75" text-anchor="middle" class="fig-blue">校验错</text>
    <rect x="280" y="50" width="60" height="40" class="fig-box-blue"/>
    <text x="310" y="44" text-anchor="middle" class="fig-label">位4</text>
    <text x="310" y="75" text-anchor="middle" class="fig-blue">I/O</text>
    <rect x="340" y="50" width="60" height="40" class="fig-box-subtle"/>
    <text x="370" y="44" text-anchor="middle" class="fig-label">位3</text>
    <text x="370" y="75" text-anchor="middle" class="fig-muted">…</text>
    <rect x="400" y="50" width="60" height="40" class="fig-box-subtle"/>
    <text x="430" y="44" text-anchor="middle" class="fig-label">位2</text>
    <text x="430" y="75" text-anchor="middle" class="fig-muted">…</text>
    <rect x="460" y="50" width="60" height="40" class="fig-box-subtle"/>
    <text x="490" y="44" text-anchor="middle" class="fig-label">位1</text>
    <text x="490" y="75" text-anchor="middle" class="fig-muted">…</text>
    <rect x="520" y="50" width="60" height="40" class="fig-box-subtle"/>
    <text x="550" y="44" text-anchor="middle" class="fig-label">位0</text>
    <text x="550" y="75" text-anchor="middle" class="fig-muted">…</text>
    <text x="100" y="115" class="fig-muted">每个中断源接口中一个请求触发器，逻辑上拼成一个请求标志寄存器</text>
    <text x="100" y="135" class="fig-muted">1 = 该中断源提出了请求；CPU 在指令执行结束时采样</text>
  </svg>
  <figcaption>图 8-7　中断请求标志寄存器逻辑示意</figcaption>
</figure>

<strong>中断判优</strong>：多个请求同时到来，需要<strong>中断排队器</strong>（硬件，可集中在 CPU 内或分散在各接口）按<strong>响应优先级</strong>选出一个。响应优先级由硬件电路固定，不可由程序修改；能改的是<strong>处理优先级</strong>（见下节）。

> <strong>★考点</strong>：中断源五类；中断系统七大功能问题；请求标志触发器“每源一个、逻辑上组成寄存器”；响应优先级由硬件排队器固定。

### 8.4 中断判优、屏蔽与多重中断

<strong>响应优先级 vs 处理优先级</strong>：

| | 响应优先级 | 处理优先级 |
|---|---|---|
| 由谁决定 | 硬件排队器电路 | 中断屏蔽字（可程序设置） |
| 能否改变 | <strong>不能改变</strong> | <strong>可以改变</strong> |
| 作用 | 同时请求时先响应谁 | 服务程序执行中谁能打断谁 |

<strong>屏蔽字</strong>的每一位对应一个中断源，<strong>1 表示屏蔽</strong>（该源的请求不能进入排队器，更不能被响应），0 表示开放。设 A、B、C、D 四个中断源响应优先级依次降低；若 C 的屏蔽字设为 <strong>0110</strong>（位序 A B C D），则 C 执行时屏蔽 B 和 C 自己，开放 A 和 D——于是 D 可以打断 C，处理次序就不再等于响应次序。可见提高某中断的处理优先级，本质是让它屏蔽掉原本优先于它的那些源。

<strong>多重中断（中断嵌套）的执行轨迹：主程序执行中，A、B、C、D 同时请求，按硬件响应次序先响应 A。A 的屏蔽字全 1，B、C、D 都不能打断 A；A 返回主程序后屏蔽字恢复为主程序的全 0，B、C、D 再次排队，响应 B；B 的屏蔽字开放 C、D；C 的屏蔽字只屏蔽 B 和自己、开放 D，于是 D 打断 C。返回次序相反：D → C → B → A → 主程序。中断屏蔽技术也可用来人为屏蔽某个中断源的请求</strong>。

<strong>中断服务程序的完整过程</strong>：

1. <strong>保护现场</strong>：用 push 把服务程序中要用的寄存器内容入栈（或用存数指令存入指定单元）；
2. <strong>设置中断屏蔽字</strong>——必须在<strong>开中断之前</strong>完成；
3. <strong>开中断</strong>（提前开中断，才能实现多重中断/嵌套）；
4. 执行中断服务程序本体；
5. <strong>关中断</strong>，<strong>恢复现场</strong>并<strong>恢复原屏蔽字</strong>（这两步不能被打断）；
6. <strong>开中断</strong>，<strong>中断返回</strong>。

<strong>断点保护</strong>的两种办法：

- <strong>断点进栈</strong>：由中断隐指令（硬件）直接把 PC 压入堆栈，多重中断自然不冲突；
- <strong>存入特定单元</strong>：如存入某个约定的“零地址”（加引号，不一定是物理地址 0），同样由中断隐指令完成。多重中断时后一个断点会<strong>覆盖</strong>前一个断点（三个断点存同一单元，至少两个丢失），因此服务程序开头要做<strong>转存</strong>：把零地址的内容取出，存入专设的 return 单元（程序断点），返回时用<strong>间接跳转</strong>——跳转到 return 单元中保存的地址，而不是跳到 return 本身。这个过程实质是把“特定单元中的断点”转移保护起来。

<figure class="cpd-figure not-content" id="lhw-w6-fig-7">
  <svg viewBox="0 0 660 450" role="img" aria-label="中断响应与中断服务流程" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-3" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="20" class="fig-title" font-size="13">中断响应与中断服务流程</text>
    <line x1="320" y1="66" x2="320" y2="82" class="fig-line" marker-end="url(#lhw-w6-arr-3)"/>
    <line x1="320" y1="122" x2="320" y2="138" class="fig-line" marker-end="url(#lhw-w6-arr-3)"/>
    <line x1="320" y1="178" x2="320" y2="194" class="fig-line" marker-end="url(#lhw-w6-arr-3)"/>
    <line x1="320" y1="250" x2="320" y2="266" class="fig-line" marker-end="url(#lhw-w6-arr-3)"/>
    <line x1="320" y1="306" x2="320" y2="322" class="fig-line" marker-end="url(#lhw-w6-arr-3)"/>
    <line x1="320" y1="362" x2="320" y2="378" class="fig-line" marker-end="url(#lhw-w6-arr-3)"/>
    <rect x="170" y="30" width="300" height="36" rx="4" class="fig-box-blue"/>
    <text x="320" y="53" text-anchor="middle" class="fig-blue">中断请求：各中断源置请求标志</text>
    <rect x="170" y="86" width="300" height="36" rx="4" class="fig-box-blue"/>
    <text x="320" y="109" text-anchor="middle" class="fig-blue">中断判优：排队器按响应优先级选出</text>
    <rect x="170" y="142" width="300" height="36" rx="4" class="fig-box-blue"/>
    <text x="320" y="165" text-anchor="middle" class="fig-blue">响应时机：一条指令的执行周期结束</text>
    <rect x="170" y="198" width="300" height="52" rx="4" class="fig-box-red"/>
    <text x="320" y="219" text-anchor="middle" class="fig-red">中断隐指令：关中断、保存断点</text>
    <text x="320" y="239" text-anchor="middle" class="fig-red">形成服务程序入口地址 → PC</text>
    <rect x="170" y="270" width="300" height="36" rx="4" class="fig-box-green"/>
    <text x="320" y="293" text-anchor="middle" class="fig-green">保护现场、设置屏蔽字、开中断</text>
    <rect x="170" y="326" width="300" height="36" rx="4" class="fig-box-green"/>
    <text x="320" y="349" text-anchor="middle" class="fig-green">执行中断服务程序</text>
    <rect x="170" y="382" width="300" height="52" rx="4" class="fig-box-green"/>
    <text x="320" y="403" text-anchor="middle" class="fig-green">关中断、恢复现场与屏蔽字</text>
    <text x="320" y="423" text-anchor="middle" class="fig-green">开中断、中断返回</text>
    <text x="482" y="228" class="fig-muted">硬件</text>
    <text x="482" y="340" class="fig-muted">软件</text>
  </svg>
  <figcaption>图 8-8　中断响应与中断服务程序完整流程</figcaption>
</figure>

> <strong>★考点</strong>：响应优先级硬件固定、处理优先级靠屏蔽字改变；屏蔽字“1 屏蔽、0 开放”；设置屏蔽字必须在开中断之前；多重中断靠<strong>提前开中断</strong>；断点保护=进栈或存特定单元+转存；返回用间接跳转。

> <strong>注意</strong>：屏蔽字改变的是<strong>处理次序，不能</strong>改变硬件排队器决定的响应次序；恢复现场与恢复屏蔽字必须在关中断状态下进行。

## 第9章 控制单元的功能

### 9.1 操作命令的分析：微操作与四个工作周期

控制单元（控制器）是计算机系统的控制核心，其基本功能是<strong>完成指令解释</strong>：取指、分析（间址）、执行、中断响应，并在这个过程中发出各种控制命令。

<strong>微操作命令</strong>：指令解释过程中由控制单元发出的、完成动作远小于整条指令功能的命令。例如取指时的 `PC→MAR` 只完成一次地址传送。一条指令的功能由一串有先后顺序的微操作命令实现。

本章模型机有一个前提假设：<strong>指令定长，指令字长与存储字长相等</strong>，一次访存即可取回整条指令、一个操作数地址或一个操作数。因此取指周期和间址周期都只有固定的几个微操作，而执行周期因指令功能不同而差别很大。

四个工作周期：

- <strong>取指周期</strong>：按 PC 给出的地址从存储器取出指令送入 IR。
- <strong>间址周期</strong>：仅当指令采用间接寻址时才有，用于从存储器取出操作数的有效地址。
- <strong>执行周期</strong>：取出操作数并完成运算或传送。
- <strong>中断周期</strong>：仅有中断请求并响应时才有，完成保存断点、形成中断服务程序入口、关中断。

> <strong>★考点</strong>：取指周期一定访存一次；间址周期再访存一次；执行周期是否访存取决于指令类型。

### 9.2 取指周期与间址周期的操作命令

<strong>取指周期</strong>以 PC 为起点，控制单元依次发出：

1. `PC→MAR`：指令地址送入存储器地址寄存器。
2. `1→R`：向存储器发读命令。
3. `M(MAR)→MDR`：读出的指令经数据总线送入 MDR。
4. `MDR→IR`：指令送入指令寄存器。
5. `OP(IR)→CU`：操作码送控制单元译码，确定指令要做什么。
6. `PC+1→PC`：指令已取出，修改 PC 为取下一条指令作准备。

<strong>间址周期</strong>仅用于间接寻址。此时 IR 中指令的地址码是形式地址，需再访存一次取出有效地址：

1. `Ad(IR)→MAR`：形式地址送入 MAR。
2. `1→R`：发读命令。
3. `M(MAR)→MDR`：读出的操作数地址送入 MDR。
4. `MDR→Ad(IR)`：有效地址写回 IR 的地址码字段。

此后 IR 地址码部分保存的就是操作数的物理地址，执行周期可直接用它访存。

> <strong>★考点</strong>：取指与间址的微操作序列结构相同（送地址→读→入 MDR→写入寄存器），区别只在源和目的：取指是 `PC→MAR`、`MDR→IR`，间址是 `Ad(IR)→MAR`、`MDR→Ad(IR)`。

<figure class="cpd-figure not-content" id="lhw-w6-fig-23">
  <svg viewBox="0 0 640 296" role="img" aria-label="取指周期与间址周期的数据通路对比">
    <defs>
      <marker id="lhw-w6-arr-23" 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="30" y="50" width="64" height="36" rx="4" class="fig-box"/>
    <rect x="150" y="50" width="64" height="36" rx="4" class="fig-box"/>
    <rect x="270" y="32" width="110" height="72" rx="4" class="fig-box"/>
    <rect x="430" y="50" width="64" height="36" rx="4" class="fig-box"/>
    <rect x="430" y="148" width="64" height="44" rx="4" class="fig-box"/>
    <rect x="250" y="152" width="100" height="50" rx="4" class="fig-box-blue"/>
    <line x1="94" y1="68" x2="146" y2="68" class="fig-line" marker-end="url(#lhw-w6-arr-23)"/>
    <line x1="214" y1="68" x2="266" y2="68" class="fig-line" marker-end="url(#lhw-w6-arr-23)"/>
    <line x1="380" y1="68" x2="426" y2="68" class="fig-line" marker-end="url(#lhw-w6-arr-23)"/>
    <line x1="452" y1="86" x2="452" y2="144" class="fig-line" marker-end="url(#lhw-w6-arr-23)"/>
    <line x1="486" y1="86" x2="486" y2="144" class="fig-line-dashed" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w6-arr-23)"/>
    <line x1="430" y1="172" x2="354" y2="172" class="fig-line" marker-end="url(#lhw-w6-arr-23)"/>
    <path d="M 462 192 V 250 H 182 V 90" fill="none" class="fig-line-dashed" stroke="var(--sl-color-blue)" marker-end="url(#lhw-w6-arr-23)"/>
    <path d="M 62 50 V 30 H 82 V 46" fill="none" class="fig-line" marker-end="url(#lhw-w6-arr-23)"/>
    <line x1="30" y1="274" x2="54" y2="274" class="fig-line"/>
    <line x1="300" y1="274" x2="324" y2="274" class="fig-line-dashed" stroke="var(--sl-color-blue)"/>
    <text x="62" y="73" font-size="13" text-anchor="middle" class="fig-label">PC</text>
    <text x="182" y="73" font-size="13" text-anchor="middle" class="fig-label">MAR</text>
    <text x="325" y="64" font-size="13" text-anchor="middle" class="fig-label">存储器</text>
    <text x="325" y="88" font-size="12" text-anchor="middle" class="fig-muted">M</text>
    <text x="462" y="73" font-size="13" text-anchor="middle" class="fig-label">MDR</text>
    <text x="462" y="176" font-size="13" text-anchor="middle" class="fig-label">IR</text>
    <text x="300" y="174" font-size="13" text-anchor="middle" class="fig-blue">CU</text>
    <text x="300" y="192" font-size="12" text-anchor="middle" class="fig-muted">控制单元</text>
    <text x="96" y="48" font-size="11.5" class="fig-label">PC→MAR</text>
    <text x="240" y="60" font-size="11.5" text-anchor="middle" class="fig-muted">地址</text>
    <text x="403" y="60" font-size="11.5" text-anchor="middle" class="fig-muted">读数据</text>
    <text x="444" y="118" font-size="11.5" text-anchor="end" class="fig-label">MDR→IR</text>
    <text x="494" y="114" font-size="11.5" class="fig-blue">MDR→Ad(IR)</text>
    <text x="392" y="164" font-size="11.5" text-anchor="middle" class="fig-label">OP(IR)→CU</text>
    <text x="322" y="242" font-size="11.5" text-anchor="middle" class="fig-blue">Ad(IR)→MAR（间址）</text>
    <text x="72" y="22" font-size="11.5" class="fig-muted">PC+1→PC</text>
    <text x="60" y="278" font-size="12" class="fig-label">取指周期通路</text>
    <text x="330" y="278" font-size="12" class="fig-blue">间址周期通路</text>
  </svg>
  <figcaption>图 9-1　取指周期与间址周期的数据通路对比</figcaption>
</figure>

### 9.3 执行周期的操作命令：三类指令

执行周期的操作随指令功能变化，按功能分为三类。

<strong>一、非访存指令</strong>（执行周期不访问存储器）

| 指令 | 功能 | 执行周期的微操作 |
|---|---|---|
| CLA | ACC 清零 | `0→ACC` |
| COM | ACC 按位取反 | `~ACC→ACC` |
| SHR | 算术右移 | ACC 各位右移一位；符号位 ACC0 回填 ACC0 |
| CSL | 循环左移 | ACC 各位左移一位；ACC0（最高位）移入 ACCn（最低位） |
| STP | 停机 | `0→停机标志` |

SHR 的“右移”和“符号位回填”、CSL 的“左移”和“最高位送最低位”都可以在同一节拍并行完成，因此每条指令只需一到两个微操作命令。

<strong>二、访存指令</strong>（ACC 为一个操作数，X 为内存地址）

| 指令 | 功能 | 执行周期的微操作 |
|---|---|---|
| ADD X | (ACC)+(X)→ACC | `Ad(IR)→MAR`；`1→R`；`M(MAR)→MDR`；`(ACC)+(MDR)→ACC` |
| STA X | (ACC)→X | `Ad(IR)→MAR`；`1→W`；`ACC→MDR`；`MDR→M(MAR)` |
| LDA X | (X)→ACC | `Ad(IR)→MAR`；`1→R`；`M(MAR)→MDR`；`MDR→ACC` |

ADD 的前三步取加数，最后一步是 CPU 内部加法，不再访存；STA 是三类中唯一写存储器的指令。

<strong>三、转移指令</strong>

| 指令 | 功能 | 执行周期的微操作 |
|---|---|---|
| JMP X | 无条件转 X | `Ad(IR)→PC` |
| BN X | 上次结果为负则转 X | ACC0=1：`Ad(IR)→PC`；ACC0=0：不操作 |

> <strong>注意</strong>：取指阶段已执行 `PC+1→PC`，所以执行阶段不必再修改 PC；不转移时 PC 已指向下一条指令。
>
> <strong>★考点</strong>：条件转移的判断依据是上一条指令留在 ACC 中的结果符号位 ACC0（ACC0=1 为负）。

<figure class="cpd-figure not-content" id="lhw-w6-fig-24">
  <svg viewBox="0 0 640 304" role="img" aria-label="执行周期三类指令微操作命令汇总">
    <rect x="260" y="14" width="120" height="36" rx="4" class="fig-box-blue"/>
    <path d="M 320 50 V 72 M 100 72 H 540 M 100 72 V 92 M 320 72 V 92 M 540 72 V 92" fill="none" class="fig-line"/>
    <rect x="24" y="92" width="152" height="38" rx="4" class="fig-box"/>
    <rect x="220" y="92" width="200" height="38" rx="4" class="fig-box"/>
    <rect x="464" y="92" width="152" height="38" rx="4" class="fig-box"/>
    <rect x="24" y="136" width="152" height="152" rx="4" class="fig-box-subtle"/>
    <rect x="220" y="136" width="200" height="152" rx="4" class="fig-box-subtle"/>
    <rect x="464" y="136" width="152" height="152" rx="4" class="fig-box-subtle"/>
    <text x="320" y="37" font-size="13" text-anchor="middle" class="fig-blue">执行周期</text>
    <text x="100" y="116" font-size="13" text-anchor="middle" class="fig-title">非访存指令</text>
    <text x="320" y="116" font-size="13" text-anchor="middle" class="fig-title">访存指令</text>
    <text x="540" y="116" font-size="13" text-anchor="middle" class="fig-title">转移指令</text>
    <text x="32" y="158" font-size="11.5" class="fig-label">CLA：0→ACC</text>
    <text x="32" y="180" font-size="11.5" class="fig-label">COM：ACC 取反→ACC</text>
    <text x="32" y="202" font-size="11.5" class="fig-label">SHR：右移，符号位不变</text>
    <text x="32" y="224" font-size="11.5" class="fig-label">CSL：左移，ACC0→ACCn</text>
    <text x="32" y="246" font-size="11.5" class="fig-label">STP：0→停机标志</text>
    <text x="232" y="158" font-size="11" class="fig-label">ADD X：Ad(IR)→MAR，读</text>
    <text x="240" y="176" font-size="11" class="fig-muted">M(MAR)→MDR</text>
    <text x="240" y="194" font-size="11" class="fig-muted">(ACC)+(MDR)→ACC</text>
    <text x="232" y="216" font-size="11" class="fig-label">STA X：Ad(IR)→MAR，写</text>
    <text x="240" y="234" font-size="11" class="fig-muted">ACC→MDR，MDR→M(MAR)</text>
    <text x="232" y="256" font-size="11" class="fig-label">LDA X：Ad(IR)→MAR，读</text>
    <text x="240" y="274" font-size="11" class="fig-muted">M(MAR)→MDR，MDR→ACC</text>
    <text x="472" y="158" font-size="11.5" class="fig-label">JMP X：Ad(IR)→PC</text>
    <text x="472" y="182" font-size="11.5" class="fig-label">BN X：ACC0=1 时</text>
    <text x="480" y="202" font-size="11.5" class="fig-muted">Ad(IR)→PC</text>
    <text x="480" y="222" font-size="11.5" class="fig-muted">否则不操作</text>
  </svg>
  <figcaption>图 9-2　执行周期三类指令的微操作命令汇总</figcaption>
</figure>

### 9.4 指令周期的组合与中断周期

不同指令的指令周期由若干工作周期组合而成：

| 指令类型 | 指令周期 |
|---|---|
| 非访存指令 | 取指 + 执行 |
| 直接访存指令 | 取指 + 执行 |
| 间接访存指令 | 取指 + 间址 + 执行 |
| 直接转移指令 | 取指 + 执行 |
| 间接转移指令 | 取指 + 间址 + 执行 |

<strong>中断周期对所有指令都相同</strong>，固定完成三件事：

1. <strong>保存断点</strong>：断点是 PC 的内容（下一条指令的地址），因为取指后 PC 已加一。
2. <strong>形成中断服务程序入口地址</strong>。
3. <strong>关中断</strong>：禁止新的中断请求。

保存断点有两种方法：

- 存入 0 号单元：`0→MAR`；`1→W`；`PC→MDR`；`MDR→M(MAR)`。
- 断点进栈：`(SP)-1→SP`；`SP→MAR`；`1→W`；`PC→MDR`；`MDR→M(MAR)`。

形成入口地址有两种方法：

- <strong>硬件向量法</strong>：由硬件产生向量地址，`向量地址→PC`。
- <strong>软件查询法</strong>：`中断识别程序入口→PC`，由该程序查询中断源后再给出服务程序入口。

关中断：`0→中断允许触发器`。

> <strong>★考点</strong>：中断周期三件事——保存断点、形成中断服务程序入口、关中断；断点保存的是 PC（下一条指令地址），不是当前指令地址。

<figure class="cpd-figure not-content" id="lhw-w6-fig-25">
  <svg viewBox="0 0 680 334" role="img" aria-label="四个工作周期的微操作命令汇总表">
    <rect x="14" y="10" width="652" height="312" rx="4" class="fig-box"/>
    <rect x="14" y="10" width="652" height="28" rx="4" class="fig-box-subtle"/>
    <line x1="122" y1="10" x2="122" y2="322" class="fig-divider"/>
    <line x1="14" y1="38" x2="666" y2="38" class="fig-divider"/>
    <line x1="14" y1="86" x2="666" y2="86" class="fig-divider"/>
    <line x1="14" y1="128" x2="666" y2="128" class="fig-divider"/>
    <line x1="14" y1="178" x2="666" y2="178" class="fig-divider"/>
    <line x1="14" y1="228" x2="666" y2="228" class="fig-divider"/>
    <line x1="14" y1="258" x2="666" y2="258" class="fig-divider"/>
    <text x="68" y="29" font-size="12.5" text-anchor="middle" class="fig-title">工作周期</text>
    <text x="136" y="29" font-size="12.5" class="fig-title">控制单元发出的微操作命令</text>
    <text x="68" y="66" font-size="12" text-anchor="middle" class="fig-blue">取指周期</text>
    <text x="132" y="56" font-size="11.5" class="fig-label">PC→MAR，1→R，M(MAR)→MDR</text>
    <text x="132" y="74" font-size="11.5" class="fig-label">MDR→IR，OP(IR)→CU，PC+1→PC</text>
    <text x="68" y="112" font-size="12" text-anchor="middle" class="fig-blue">间址周期</text>
    <text x="132" y="112" font-size="11.5" class="fig-label">Ad(IR)→MAR，1→R，M(MAR)→MDR，MDR→Ad(IR)</text>
    <text x="68" y="150" font-size="12" text-anchor="middle" class="fig-blue">执行</text>
    <text x="68" y="168" font-size="11.5" text-anchor="middle" class="fig-blue">非访存</text>
    <text x="132" y="150" font-size="11.5" class="fig-label">0→ACC ｜ ~ACC→ACC ｜ ACC 算术右移</text>
    <text x="132" y="168" font-size="11.5" class="fig-label">ACC 循环左移 ｜ 0→停机标志（CLA/COM/SHR/CSL/STP）</text>
    <text x="68" y="199" font-size="12" text-anchor="middle" class="fig-blue">执行</text>
    <text x="68" y="216" font-size="11.5" text-anchor="middle" class="fig-blue">访存</text>
    <text x="132" y="198" font-size="11.5" class="fig-label">Ad(IR)→MAR，1→R（读）/ 1→W（写），M(MAR)→MDR</text>
    <text x="132" y="216" font-size="11.2" class="fig-label">ADD：(ACC)+(MDR)→ACC ｜ STA：ACC→MDR→M(MAR) ｜ LDA：MDR→ACC</text>
    <text x="68" y="248" font-size="11.5" text-anchor="middle" class="fig-blue">执行·转移</text>
    <text x="132" y="248" font-size="11.5" class="fig-label">Ad(IR)→PC（JMP）；ACC0=1 时 Ad(IR)→PC（BN）</text>
    <text x="68" y="294" font-size="12" text-anchor="middle" class="fig-blue">中断周期</text>
    <text x="132" y="277" font-size="11.5" class="fig-label">0→MAR（或 (SP)-1→SP，SP→MAR），1→W</text>
    <text x="132" y="295" font-size="11.5" class="fig-label">PC→MDR，MDR→M(MAR)</text>
    <text x="132" y="312" font-size="11.5" class="fig-label">向量地址→PC，0→中断允许触发器</text>
  </svg>
  <figcaption>图 9-3　四个工作周期的微操作命令总览</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w6-fig-26">
  <svg viewBox="0 0 640 292" role="img" aria-label="中断周期固定完成的三项操作">
    <defs>
      <marker id="lhw-w6-arr-26" 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="20" y="24" width="132" height="46" rx="4" class="fig-box-blue"/>
    <rect x="20" y="126" width="132" height="46" rx="4" class="fig-box-blue"/>
    <rect x="20" y="228" width="132" height="46" rx="4" class="fig-box-blue"/>
    <line x1="86" y1="70" x2="86" y2="122" class="fig-line" marker-end="url(#lhw-w6-arr-26)"/>
    <line x1="86" y1="172" x2="86" y2="224" class="fig-line" marker-end="url(#lhw-w6-arr-26)"/>
    <rect x="180" y="16" width="436" height="94" rx="4" class="fig-box"/>
    <rect x="180" y="118" width="436" height="68" rx="4" class="fig-box"/>
    <rect x="180" y="220" width="436" height="58" rx="4" class="fig-box"/>
    <text x="86" y="52" font-size="13" text-anchor="middle" class="fig-blue">① 保存断点</text>
    <text x="86" y="154" font-size="13" text-anchor="middle" class="fig-blue">② 形成入口</text>
    <text x="86" y="256" font-size="13" text-anchor="middle" class="fig-blue">③ 关中断</text>
    <text x="196" y="38" font-size="12" class="fig-title">① 断点存入 0 号单元</text>
    <text x="196" y="56" font-size="12" class="fig-label">0→MAR，1→W，PC→MDR，MDR→M(MAR)</text>
    <text x="196" y="78" font-size="12" class="fig-title">② 断点进栈</text>
    <text x="196" y="96" font-size="12" class="fig-label">(SP)-1→SP，SP→MAR，1→W，PC→MDR，MDR→M(MAR)</text>
    <text x="196" y="140" font-size="12" class="fig-label">① 硬件向量法：向量地址→PC</text>
    <text x="196" y="160" font-size="12" class="fig-label">② 软件查询法：中断识别程序入口→PC</text>
    <text x="196" y="178" font-size="11.5" class="fig-muted">前者由硬件直接给出入口，后者由程序查询中断源</text>
    <text x="196" y="244" font-size="12" class="fig-label">0→中断允许触发器</text>
    <text x="196" y="264" font-size="11.5" class="fig-muted">禁止新的中断请求，保证中断处理不被嵌套打断</text>
  </svg>
  <figcaption>图 9-4　中断周期的三项固定操作及其微命令实现</figcaption>
</figure>

### 9.5 控制单元的功能与外特性

控制单元的功能概括为：<strong>发出各种控制命令（微命令），控制 CPU 内部各部件以及系统总线，使整个计算机协调、稳定、正确、连续地运行</strong>。这些命令都受时钟控制，在指定时间点发出。

<strong>输入信号</strong>（四类）：

- <strong>IR 的操作码</strong>：送 CU 译码，决定“做什么操作”，控制信号与操作码直接相关。
- <strong>时钟信号</strong>：一个时钟脉冲发出一个或一组可并行执行的微操作命令。
- <strong>标志</strong>：如结果符号 ACC0、进位、零标志等，是指令正确执行的依据，条件转移指令据此判断是否转移。
- <strong>外来信号</strong>：中断请求、总线请求等，由 CU 统一管理硬件资源。

<strong>输出信号</strong>（两大类）：

- <strong>CPU 内部控制信号</strong>：寄存器之间的数据传送、修改 PC（`PC+1→PC`）、控制 ALU 完成算术/逻辑运算。
- <strong>系统总线控制信号</strong>：`MREQ`（存储器请求）、访存/访 I/O 选择（IOM，低电平访 I/O、高电平访存）、存储器读/写命令、中断响应、总线应答。

> <strong>★考点</strong>：控制单元的输入是“操作码、时钟、标志、外来信号”四类；输出是“CPU 内部、系统总线”两类。中断响应和总线应答都是由 CPU 输出的。

<figure class="cpd-figure not-content" id="lhw-w6-fig-13">
  <svg viewBox="0 0 620 300" role="img" aria-label="控制单元输入输出外特性" font-family="sans-serif">
    <defs>
      <marker id="lhw-w6-arr-7" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="20" y="16" font-size="13" font-weight="bold" class="fig-muted">输入信号</text>
    <text x="430" y="16" font-size="13" font-weight="bold" class="fig-muted">输出信号</text>
    <line x1="200" y1="42" x2="246" y2="42" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <line x1="200" y1="88" x2="246" y2="88" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <line x1="200" y1="134" x2="246" y2="134" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <line x1="200" y1="180" x2="246" y2="180" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <line x1="200" y1="226" x2="246" y2="226" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <line x1="380" y1="80" x2="426" y2="80" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <text x="386" y="74" font-size="12" class="fig-label">内部</text>
    <line x1="380" y1="220" x2="426" y2="220" class="fig-line" marker-end="url(#lhw-w6-arr-7)"/>
    <text x="386" y="214" font-size="12" class="fig-label">总线</text>
    <rect x="250" y="20" width="130" height="240" rx="6" class="fig-box-blue" stroke-width="1.4"/>
    <text x="315" y="122" font-size="14" font-weight="bold" text-anchor="middle" class="fig-blue">控制单元</text>
    <text x="315" y="142" font-size="12" text-anchor="middle" class="fig-label">CU</text>
    <rect x="20" y="26" width="180" height="32" rx="4" class="fig-box"/>
    <text x="30" y="46" font-size="12" class="fig-label">IR（操作码）</text>
    <rect x="20" y="72" width="180" height="32" rx="4" class="fig-box"/>
    <text x="30" y="92" font-size="12" class="fig-label">时钟信号</text>
    <rect x="20" y="118" width="180" height="32" rx="4" class="fig-box"/>
    <text x="30" y="138" font-size="12" class="fig-label">标志（ACC0、进位等）</text>
    <rect x="20" y="164" width="180" height="32" rx="4" class="fig-box"/>
    <text x="30" y="184" font-size="12" class="fig-label">中断请求</text>
    <rect x="20" y="210" width="180" height="32" rx="4" class="fig-box"/>
    <text x="30" y="230" font-size="12" class="fig-label">总线请求</text>
    <rect x="430" y="44" width="170" height="92" rx="4" class="fig-box"/>
    <text x="440" y="64" font-size="12" font-weight="bold" class="fig-label">CPU 内部控制信号</text>
    <text x="440" y="84" font-size="12" class="fig-label">寄存器间数据传送</text>
    <text x="440" y="102" font-size="12" class="fig-label">PC+1→PC</text>
    <text x="440" y="120" font-size="12" class="fig-label">ALU 运算控制</text>
    <rect x="430" y="164" width="170" height="126" rx="4" class="fig-box"/>
    <text x="440" y="186" font-size="12" font-weight="bold" class="fig-label">系统总线控制信号</text>
    <text x="440" y="206" font-size="12" class="fig-label">MREQ</text>
    <text x="440" y="224" font-size="12" class="fig-label">访存/访 I/O 选择</text>
    <text x="440" y="242" font-size="12" class="fig-label">读/写命令</text>
    <text x="440" y="260" font-size="12" class="fig-label">中断响应</text>
    <text x="440" y="278" font-size="12" class="fig-label">总线应答</text>
  </svg>
  <figcaption>图 9-5　控制单元（CU）的输入与输出信号</figcaption>
</figure>

### 9.6 控制信号的产生：分散连接与内部总线

CPU 内部各部件可以<strong>分散连接</strong>（点对点连线），也可以连到<strong>内部总线</strong>上。以间接寻址的 `ADD X` 为例，两种结构下控制单元发出的信号如下。

<strong>分散连接结构</strong>：控制信号编号 C0～C8，由 CU 在不同阶段发出。

| 阶段 | 控制信号 | 完成的微操作 |
|---|---|---|
| 取指 | C0 | `PC→MAR` |
| 取指 | C1 | `MAR→地址总线` |
| 取指 | 读命令 | 存储器读 |
| 取指 | C2 | `数据总线→MDR` |
| 取指 | C3 | `MDR→IR` |
| 取指 | C4 | `OP(IR)→CU` 译码 |
| 取指 | — | `PC+1→PC` |
| 间址 | C5 | `MDR→MAR` |
| 间址 | C1 | `MAR→地址总线` |
| 间址 | 读命令 | 存储器读 |
| 间址 | C2 | `数据总线→MDR` |
| 间址 | C3 | `MDR→Ad(IR)` |
| 执行 | C5 | `MDR→MAR` |
| 执行 | C1 | `MAR→地址总线` |
| 执行 | 读命令 | 存储器读 |
| 执行 | C2 | `数据总线→MDR` |
| 执行 | C6 | `ACC→ALU` |
| 执行 | C7 | `MDR→ALU` |
| 执行 | ALU 控制 | 加法运算 |
| 执行 | C8 | `ALU→ACC` |

> <strong>注意</strong>：间址周期的形式地址是从 MDR 取得的；若 IR 与 MAR 之间有通路，也可以直接从 IR 取，效果相同。

<strong>内部总线结构</strong>：各部件通过一组公共总线传送数据，用 `out`/`in` 控制信号决定谁能输出、谁接收。取指周期依次为 `PCout、MARin` → 读 → `MDRin` → `MDRout、IRin` → 译码、`PC+1`；间址周期为 `MDRout、MARin` → 读 → `MDRin` → `MDRout、IRin(Ad)`。

执行周期（ADD X，间接寻址）的节拍与信号：

| 节拍 | 有效控制信号 | 动作 |
|---|---|---|
| T0 | `MDRout`、`MARin` | 有效地址（还在 MDR 中）送入 MAR |
| T1 | `1→R` | 向存储器发读命令 |
| T2 | `MDRin` | 读出的操作数送入 MDR |
| T3 | `MDRout`、`Yin` | 操作数送入 ALU 输入寄存器 Y |
| T4 | `ACout`、`ALU 加`、`Zin` | ACC 与 Y 相加，结果存入 Z |
| T5 | `Zout`、`ACin` | 结果写回 ACC，执行结束 |

> <strong>★考点</strong>：控制信号之间有严格的先后顺序：先送地址、再发读命令，数据进入 MDR 后才能继续传送；读命令未给出前数据总线上不会有数据。
>
> <strong>注意</strong>：内部总线是分时使用的，同一节拍只能有一个部件输出（out 有效），否则会发生总线冲突。

<figure class="cpd-figure not-content" id="lhw-w6-fig-14">
  <svg viewBox="0 0 620 240" role="img" aria-label="间接寻址 ADD X 内部总线节拍" font-family="sans-serif">
    <text x="16" y="16" font-size="12" class="fig-muted">时钟</text>
    <path d="M24,42 V26 H70 V42 H116 V26 H162 V42 H208 V26 H254 V42 H300 V26 H346 V42 H392 V26 H438 V42 H484 V26 H530 V42 H576" fill="none" class="fig-line"/>
    <text x="70" y="16" font-size="12" text-anchor="middle" class="fig-label">T0</text>
    <text x="162" y="16" font-size="12" text-anchor="middle" class="fig-label">T1</text>
    <text x="254" y="16" font-size="12" text-anchor="middle" class="fig-label">T2</text>
    <text x="346" y="16" font-size="12" text-anchor="middle" class="fig-label">T3</text>
    <text x="438" y="16" font-size="12" text-anchor="middle" class="fig-label">T4</text>
    <text x="530" y="16" font-size="12" text-anchor="middle" class="fig-label">T5</text>
    <line x1="24" y1="26" x2="24" y2="150" class="fig-line-dashed"/>
    <line x1="116" y1="26" x2="116" y2="150" class="fig-line-dashed"/>
    <line x1="208" y1="26" x2="208" y2="150" class="fig-line-dashed"/>
    <line x1="300" y1="26" x2="300" y2="150" class="fig-line-dashed"/>
    <line x1="392" y1="26" x2="392" y2="150" class="fig-line-dashed"/>
    <line x1="484" y1="26" x2="484" y2="150" class="fig-line-dashed"/>
    <line x1="576" y1="26" x2="576" y2="150" class="fig-line-dashed"/>
    <rect x="29" y="78" width="82" height="56" rx="4" class="fig-box-blue"/>
    <text x="70" y="102" font-size="11" text-anchor="middle" class="fig-blue">MDRout</text>
    <text x="70" y="118" font-size="11" text-anchor="middle" class="fig-blue">MARin</text>
    <rect x="121" y="78" width="82" height="56" rx="4" class="fig-box-blue"/>
    <text x="162" y="102" font-size="11" text-anchor="middle" class="fig-blue">1→R</text>
    <text x="162" y="118" font-size="10.2" text-anchor="middle" class="fig-blue">（读存储器）</text>
    <rect x="213" y="78" width="82" height="56" rx="4" class="fig-box-blue"/>
    <text x="254" y="102" font-size="11" text-anchor="middle" class="fig-blue">MDRin</text>
    <text x="254" y="118" font-size="10.2" text-anchor="middle" class="fig-blue">（数据→MDR）</text>
    <rect x="305" y="78" width="82" height="56" rx="4" class="fig-box-blue"/>
    <text x="346" y="102" font-size="11" text-anchor="middle" class="fig-blue">MDRout</text>
    <text x="346" y="118" font-size="11" text-anchor="middle" class="fig-blue">Yin</text>
    <rect x="397" y="78" width="82" height="56" rx="4" class="fig-box-blue"/>
    <text x="438" y="102" font-size="11" text-anchor="middle" class="fig-blue">ACout</text>
    <text x="438" y="118" font-size="10.2" text-anchor="middle" class="fig-blue">ALU 加，Zin</text>
    <rect x="489" y="78" width="82" height="56" rx="4" class="fig-box-blue"/>
    <text x="530" y="102" font-size="11" text-anchor="middle" class="fig-blue">Zout</text>
    <text x="530" y="118" font-size="11" text-anchor="middle" class="fig-blue">ACin</text>
    <text x="24" y="164" font-size="12" class="fig-label">执行周期：地址→读出→入 MDR→送 Y→相加→写回 ACC</text>
    <text x="24" y="188" font-size="12" class="fig-red">每个节拍发一组可并行命令，节拍顺序不可颠倒</text>
    <text x="24" y="212" font-size="12" class="fig-muted">（间接寻址 ADD X，CPU 内部总线结构）</text>
  </svg>
  <figcaption>图 9-6　间接寻址 ADD X 在内部总线结构下的执行节拍</figcaption>
</figure>

### 9.7 多级时序系统与控制方式

<strong>三级时序</strong>：指令周期 ⊃ 机器周期 ⊃ 时钟周期（节拍）。

- <strong>机器周期</strong>：所有指令执行过程中的基准时间。划分时考虑每条指令要经过哪些步骤、每步需要多长时间，再把各步骤安排到不同机器周期中。基准通常取<strong>最复杂（最长）的微操作时间</strong>，而最长的微操作一般是访存，所以常以<strong>一次访存时间</strong>作为机器周期。若指令字长与存储字长相等，取指周期就是一个机器周期。
- <strong>时钟周期（节拍）</strong>：计算机中最基本的时间单位，使时间离散化。一个时钟周期可完成一个或若干个可并行的微操作；有的微操作（如访存）要占多个时钟周期。机器周期内用节拍控制微操作的先后顺序，节拍 T0、T1、T2、T3 在时钟上升沿触发控制命令。

各机器周期包含的节拍数可以相同，也可以不同。例如一条指令的指令周期含三个机器周期且节拍数相等；而执行阶段只做 CPU 内部操作的指令，取指用 4 个节拍、执行只用 3 个节拍。

若指令含 $n$ 个机器周期、每个机器周期含 $m$ 个节拍：

$$
T_{\mathrm{instr}}=n\cdot m\cdot T_{clk}
$$

<strong>主频与机器速度</strong>：主频高不等于速度快。只有在两台机器均不采用流水线、指令周期所含机器周期数相同、每个机器周期的节拍数也相同的条件下，平均指令执行速度之比才等于主频之比：

$$
\frac{v_A}{v_B}=\frac{f_A}{f_B}
$$

只要一方采用流水线，即使主频相同，速度也会不同。

<strong>三种控制方式</strong>：

| 控制方式 | 时序基准 | 特点 |
|---|---|---|
| 同步控制 | 统一时钟，微操作安排在固定节拍 | 设计简单、时序规则；节拍按最长微操作确定，短操作也要等待，时间利用率低 |
| 异步控制 | 无统一基准，采用请求—应答 | 前一操作完成并应答后才启动下一操作，时间按需分配、效率高；控制电路复杂 |
| 联合控制 | 同步为主、异步为辅 | 大多数操作按固定节拍同步控制，访存等慢速操作采用异步应答 |

> <strong>★考点</strong>：机器周期通常以访存时间（最复杂的微操作）为基准；同步控制的节拍数按最长微操作确定，因此高速部件会有等待。
>
> <strong>注意</strong>：异步/联合控制在这里只作对比，本章时序系统采用的是同步控制。

<figure class="cpd-figure not-content" id="lhw-w6-fig-15">
  <svg viewBox="0 0 620 306" role="img" aria-label="同步控制与异步控制对比" font-family="sans-serif">
    <defs>
      <marker id="lhw-w6-arr-8" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <path d="M 1 1.5 L 8.5 5 L 1 8.5 z" fill="context-stroke" stroke="none"/>
      </marker>
    </defs>
    <text x="20" y="22" font-size="13" font-weight="bold" class="fig-blue">同步控制</text>
    <path d="M40,62 V46 H100 V62 H160 V46 H220 V62 H280 V46 H340 V62 H400 V46 H460 V62 H520" fill="none" class="fig-line"/>
    <text x="70" y="38" font-size="12" text-anchor="middle" class="fig-label">T0</text>
    <text x="190" y="38" font-size="12" text-anchor="middle" class="fig-label">T1</text>
    <text x="310" y="38" font-size="12" text-anchor="middle" class="fig-label">T2</text>
    <text x="430" y="38" font-size="12" text-anchor="middle" class="fig-label">T3</text>
    <line x1="40" y1="46" x2="40" y2="132" class="fig-line-dashed"/>
    <line x1="160" y1="46" x2="160" y2="132" class="fig-line-dashed"/>
    <line x1="280" y1="46" x2="280" y2="132" class="fig-line-dashed"/>
    <line x1="400" y1="46" x2="400" y2="132" class="fig-line-dashed"/>
    <line x1="520" y1="46" x2="520" y2="132" class="fig-line-dashed"/>
    <rect x="44" y="76" width="112" height="40" rx="4" class="fig-box-blue"/>
    <text x="100" y="101" font-size="12" text-anchor="middle" class="fig-blue">PC→MAR</text>
    <rect x="164" y="76" width="112" height="40" rx="4" class="fig-box-blue"/>
    <text x="220" y="101" font-size="12" text-anchor="middle" class="fig-blue">1→R</text>
    <rect x="284" y="76" width="112" height="40" rx="4" class="fig-box-blue"/>
    <text x="340" y="101" font-size="12" text-anchor="middle" class="fig-blue">MDR→IR</text>
    <rect x="404" y="76" width="112" height="40" rx="4" class="fig-box-blue"/>
    <text x="460" y="101" font-size="12" text-anchor="middle" class="fig-blue">PC+1</text>
    <text x="20" y="146" font-size="12" class="fig-muted">节拍数固定：按最长微操作定时，短操作也占满一个节拍</text>
    <text x="20" y="180" font-size="13" font-weight="bold" class="fig-green">异步控制（请求—应答）</text>
    <line x1="150" y1="221" x2="206" y2="221" class="fig-line" marker-end="url(#lhw-w6-arr-8)"/>
    <text x="158" y="213" font-size="12" class="fig-label">请求</text>
    <line x1="400" y1="221" x2="456" y2="221" class="fig-line" marker-end="url(#lhw-w6-arr-8)"/>
    <text x="408" y="213" font-size="12" class="fig-label">启动</text>
    <path d="M305,242 V260 H95 V245" fill="none" class="fig-line-dashed" marker-end="url(#lhw-w6-arr-8)"/>
    <rect x="40" y="200" width="110" height="42" rx="4" class="fig-box-green"/>
    <text x="95" y="226" font-size="12" text-anchor="middle" class="fig-green">操作 A</text>
    <rect x="210" y="200" width="190" height="42" rx="4" class="fig-box-green"/>
    <text x="305" y="226" font-size="12" text-anchor="middle" class="fig-green">操作 B（访存，耗时长）</text>
    <rect x="460" y="200" width="100" height="42" rx="4" class="fig-box-green"/>
    <text x="510" y="226" font-size="12" text-anchor="middle" class="fig-green">操作 C</text>
    <text x="190" y="276" font-size="12" class="fig-muted">应答（完成握手）</text>
    <text x="20" y="296" font-size="12" class="fig-muted">无统一基准：前一操作完成并应答后，才启动下一操作</text>
  </svg>
  <figcaption>图 9-7　同步控制与异步控制时序对比</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w6-fig-16">
  <svg viewBox="0 0 700 185" role="img" aria-label="三级时序系统层次关系" font-family="sans-serif" font-size="12">
    <text x="16" y="20" class="fig-title" font-size="13">三级时序系统：指令周期 ⊃ 机器周期 ⊃ 时钟周期（节拍）</text>
    <rect x="20" y="34" width="660" height="32" rx="3" class="fig-box-blue"/>
    <text x="350" y="55" text-anchor="middle" class="fig-blue">指令周期（取指 + 间址 + 执行 + 中断）</text>
    <rect x="20" y="74" width="160" height="30" rx="3" class="fig-box-green"/>
    <text x="100" y="94" text-anchor="middle" class="fig-green">机器周期 1（取指）</text>
    <rect x="186" y="74" width="160" height="30" rx="3" class="fig-box-subtle"/>
    <text x="266" y="94" text-anchor="middle" class="fig-muted">机器周期 2（间址）</text>
    <rect x="352" y="74" width="160" height="30" rx="3" class="fig-box-green"/>
    <text x="432" y="94" text-anchor="middle" class="fig-green">机器周期 3（执行）</text>
    <rect x="518" y="74" width="162" height="30" rx="3" class="fig-box-red"/>
    <text x="599" y="94" text-anchor="middle" class="fig-red">机器周期 4（中断）</text>
    <rect x="20" y="112" width="38" height="26" rx="2" class="fig-box-orange"/><text x="39" y="129" text-anchor="middle" class="fig-orange">T0</text>
    <rect x="60" y="112" width="38" height="26" rx="2" class="fig-box-orange"/><text x="79" y="129" text-anchor="middle" class="fig-orange">T1</text>
    <rect x="100" y="112" width="38" height="26" rx="2" class="fig-box-orange"/><text x="119" y="129" text-anchor="middle" class="fig-orange">T2</text>
    <rect x="140" y="112" width="40" height="26" rx="2" class="fig-box-orange"/><text x="160" y="129" text-anchor="middle" class="fig-orange">T3</text>
    <text x="200" y="129" class="fig-muted">每个机器周期由若干时钟周期（节拍 T0…T3）组成</text>
    <text x="20" y="164" class="fig-muted">注：机器周期通常以一次访存时间为基准；不同指令所含机器周期数可以不同。</text>
  </svg>
  <figcaption>图 9-8　三级时序系统（指令周期、机器周期与时钟周期）</figcaption>
</figure>

## 第10章 微程序设计

### 10.1 微程序控制的基本概念

组合逻辑控制器用<strong>专门的逻辑电路</strong>为每个控制信号生成波形：思路直观，但电路庞杂，一个控制信号一套电路。指令集一旦扩展，新增指令涉及的微操作与控制命令都要改电路，修改量极大。

微程序控制换一种思路——<strong>存储逻辑</strong>：把每个节拍要发出的控制信号事先存进存储器，执行时按地址逐条读出，用读出的位（高/低电平）直接驱动各部件。这一思想由剑桥大学 Wilkes 于 1951 年提出。它把“控制器设计”变成“微程序编写”，规整、易改、天然支持指令扩展；代价是每条微指令都要先从控制存储器读出，控制器的速度受存储器访问时间限制。

| 概念 | 含义 |
|---|---|
| 微命令 | 一个控制信号，控制一个部件完成一个动作 |
| 微操作 | 微命令所完成的操作；<strong>相容</strong>（可并行）的微操作可放在同一节拍 |
| 微指令 | 一个节拍内并行发出的微命令集合，外加下一条微指令的地址 |
| 微程序 | 完成一条机器指令（或一个机器周期）的微指令序列 |
| 控制存储器 CM | 存放全部微程序的<strong>只读</strong>存储器 |
| CMAR | 控制存储器地址寄存器，存放要读的微指令地址 |
| CMDR | 控制存储器数据寄存器，存放读出的微指令 |

层次关系是<strong>逐级解释</strong>：一条机器指令对应一个微程序，一个微程序由若干条微指令组成，一条微指令包含一个或多个微命令。

- 取指、间址、中断三个周期的操作对所有指令都一样，因此<strong>各编一个公共微程序</strong>，所有指令共用；只有执行周期的微程序随指令不同而不同。
- 因此 CM 中存放的是“公共微程序 + 各指令的执行微程序”的集合，取指微程序入口地址固定（记为 M），执行微程序入口由指令操作码决定。

<figure class="cpd-figure not-content" id="lhw-w6-fig-17">
  <svg viewBox="0 0 720 360" role="img" aria-label="微程序控制器的组成与数据流" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-9" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="20" class="fig-title" font-size="13">微程序控制器的组成与数据流</text>
    <line x1="125" y1="117" x2="156" y2="117" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="280" y1="117" x2="311" y2="117" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="375" y1="49" x2="375" y2="78" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="375" y1="152" x2="375" y2="191" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="375" y1="227" x2="375" y2="248" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="375" y1="278" x2="375" y2="290" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="435" y1="317" x2="486" y2="317" class="fig-line" marker-end="url(#lhw-w6-arr-9)"/>
    <line x1="650" y1="310" x2="704" y2="310" class="fig-line-accent" marker-end="url(#lhw-w6-arr-9)"/>
    <path d="M570,294 L570,105 L439,105" fill="none" stroke="var(--sl-color-red)" marker-end="url(#lhw-w6-arr-9)"/>
    <rect x="20" y="92" width="105" height="50" rx="3" class="fig-box"/>
    <text x="72" y="113" text-anchor="middle" class="fig-label">IR</text>
    <text x="72" y="131" text-anchor="middle" font-size="11" class="fig-muted">操作码 OP</text>
    <rect x="160" y="92" width="120" height="50" rx="3" class="fig-box"/>
    <text x="220" y="112" text-anchor="middle" class="fig-label">微地址形成部件</text>
    <text x="220" y="130" text-anchor="middle" font-size="11" class="fig-muted">OP → 微程序入口</text>
    <rect x="315" y="82" width="120" height="70" rx="3" class="fig-box"/>
    <text x="375" y="105" text-anchor="middle" class="fig-label">顺序逻辑</text>
    <text x="375" y="122" text-anchor="middle" font-size="11" class="fig-muted">多路选择器</text>
    <text x="375" y="139" text-anchor="middle" font-size="11" class="fig-muted">受标志、时钟控制</text>
    <text x="375" y="42" text-anchor="middle" font-size="11" class="fig-label">标志、时钟</text>
    <text x="383" y="177" font-size="11" class="fig-muted">微地址</text>
    <rect x="315" y="195" width="120" height="32" rx="3" class="fig-box"/>
    <text x="375" y="216" text-anchor="middle" class="fig-label">CMAR</text>
    <rect x="315" y="252" width="120" height="26" rx="3" class="fig-box-subtle"/>
    <text x="375" y="269" text-anchor="middle" font-size="11" class="fig-label">地址译码</text>
    <rect x="315" y="294" width="120" height="46" rx="3" class="fig-box-blue"/>
    <text x="375" y="313" text-anchor="middle" class="fig-blue">CM（ROM）</text>
    <text x="375" y="330" text-anchor="middle" font-size="11" class="fig-muted">全部微程序</text>
    <text x="440" y="309" font-size="11" class="fig-muted">微指令</text>
    <rect x="490" y="294" width="160" height="46" rx="3" class="fig-box"/>
    <text x="570" y="312" text-anchor="middle" class="fig-label">CMDR</text>
    <text x="570" y="330" text-anchor="middle" font-size="11" class="fig-muted">操作控制 + 顺序控制</text>
    <text x="652" y="300" font-size="11" class="fig-blue">控制信号</text>
    <text x="472" y="97" font-size="11" class="fig-red">下地址</text>
  </svg>
  <figcaption>图 10-1　微程序控制器的组成与取址数据流</figcaption>
</figure>

控制器的取址流程：<strong>OP → 微地址形成部件 → 顺序逻辑 → CMAR → 地址译码 → CM → CMDR</strong>。CMDR 的操作控制字段直接送到各部件作为控制信号；顺序控制字段（下地址）回送顺序逻辑，与“标志”一起参与选择下一条微指令地址。

> <strong>★考点</strong>：微指令存放在<strong>控制存储器</strong>（ROM）中；CMAR 存地址、CMDR 存读出的微指令；执行周期的微程序入口由<strong>指令操作码</strong>经微地址形成部件产生。

> <strong>注意</strong>：微程序对<strong>机器指令程序员不可见</strong>（透明），程序员看到的是机器指令；微指令也不属于指令系统。二者是两层不同的“程序”。

### 10.2 微指令的编码方式

编码方式决定操作控制字段有多少位、要不要译码器，直接决定微指令字长和 CM 容量。

<strong>（1）直接编码（直接控制）</strong>

操作控制字段的<strong>每一位对应一个微命令</strong>，该位为 1 就发出这个控制信号，为 0 则不发出。全部微命令可以并行，无需译码，速度最快。缺点是字长等于微命令总数：本课程这台模型机共 <strong>20 种微操作</strong>，直接编码就需要 20 位。指令系统扩展时字长还要加长。

<strong>（2）字段直接编码</strong>

把微命令按<strong>互斥性</strong>分组：同一节拍内不可能同时出现的微命令编为一组，组内用二进制编码选中一个，组间仍可并行。每组 $n$ 个微命令需

$$
k=\lceil \log_2(n+1) \rceil
$$

位，多加的“1”是留给<strong>全 0 表示本组不操作</strong>的编码。读出后每组经一个译码器还原成一条控制线。

例：15 个微命令按互斥关系分成 4 组（4、3、6、2 个），各组位数分别为 3、2、3、2，共 <strong>10 位</strong>，而直接编码要 15 位。字段直接编码显著缩短字长，代价是多一级译码延迟，且分组必须正确——组内不互斥会丢失并行性。

<strong>（3）字段间接编码</strong>

一个字段的某些微命令不能独立译码，还需由<strong>另一个字段中的某些微命令</strong>来解释其含义（隐式/两级译码）。它比字段直接编码进一步缩短字长，但要多一级译码，控制信号产生更慢且并行性受限，一般只用于微命令很多的大机器。

| 编码方式 | 控制字段位数 | 译码 | 并行性 | 典型用途 |
|---|---|---|---|---|
| 直接编码 | = 微命令数（每位一条） | 无 | 最高 | 水平型微指令 |
| 字段直接编码 | 各组 $\lceil \log_2(n+1)\rceil$ 之和 | 每组一级 | 组内串行、组间并行 | 水平型/混合型 |
| 字段间接编码 | 更少 | 两级 | 组内串行 | 微命令极多时 |

<figure class="cpd-figure not-content" id="lhw-w6-fig-18">
  <svg viewBox="0 0 700 328" role="img" aria-label="三种编码方式对控制字段的组织" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-10" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="18" class="fig-title" font-size="13">三种编码方式对控制字段的组织</text>
    <text x="16" y="66" class="fig-label">直接编码</text>
    <rect x="110" y="46" width="20" height="28" class="fig-box"/><rect x="130" y="46" width="20" height="28" class="fig-box-blue"/><rect x="150" y="46" width="20" height="28" class="fig-box"/><rect x="170" y="46" width="20" height="28" class="fig-box"/><rect x="190" y="46" width="20" height="28" class="fig-box-blue"/><rect x="210" y="46" width="20" height="28" class="fig-box"/><rect x="230" y="46" width="20" height="28" class="fig-box"/><rect x="250" y="46" width="20" height="28" class="fig-box"/><rect x="270" y="46" width="20" height="28" class="fig-box"/><rect x="290" y="46" width="20" height="28" class="fig-box"/><rect x="310" y="46" width="20" height="28" class="fig-box-blue"/><rect x="330" y="46" width="20" height="28" class="fig-box"/><rect x="350" y="46" width="20" height="28" class="fig-box"/><rect x="370" y="46" width="20" height="28" class="fig-box"/><rect x="390" y="46" width="20" height="28" class="fig-box"/>
    <text x="120" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="140" y="65" text-anchor="middle" font-size="11" class="fig-blue">1</text>
    <text x="160" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="180" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="200" y="65" text-anchor="middle" font-size="11" class="fig-blue">1</text>
    <text x="220" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="240" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="260" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="280" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="300" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="320" y="65" text-anchor="middle" font-size="11" class="fig-blue">1</text>
    <text x="340" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="360" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="380" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="400" y="65" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="420" y="58" font-size="11" class="fig-label">15 个微命令 = 15 位</text>
    <text x="420" y="74" font-size="11" class="fig-muted">每位直连一条控制线，无译码</text>
    <text x="16" y="142" class="fig-label">字段直接</text>
    <rect x="110" y="122" width="72" height="28" class="fig-box-blue"/>
    <rect x="182" y="122" width="48" height="28" class="fig-box-green"/>
    <rect x="230" y="122" width="72" height="28" class="fig-box-blue"/>
    <rect x="302" y="122" width="48" height="28" class="fig-box-green"/>
    <line x1="134" y1="122" x2="134" y2="150" class="fig-divider"/>
    <line x1="158" y1="122" x2="158" y2="150" class="fig-divider"/>
    <line x1="206" y1="122" x2="206" y2="150" class="fig-divider"/>
    <line x1="254" y1="122" x2="254" y2="150" class="fig-divider"/>
    <line x1="278" y1="122" x2="278" y2="150" class="fig-divider"/>
    <line x1="326" y1="122" x2="326" y2="150" class="fig-divider"/>
    <text x="146" y="116" text-anchor="middle" font-size="10.5" class="fig-blue">组 A</text>
    <text x="206" y="116" text-anchor="middle" font-size="10.5" class="fig-green">组 B</text>
    <text x="266" y="116" text-anchor="middle" font-size="10.5" class="fig-blue">组 C</text>
    <text x="326" y="116" text-anchor="middle" font-size="10.5" class="fig-green">组 D</text>
    <text x="146" y="166" text-anchor="middle" font-size="10" class="fig-muted">3 位</text>
    <text x="206" y="166" text-anchor="middle" font-size="10" class="fig-muted">2 位</text>
    <text x="266" y="166" text-anchor="middle" font-size="10" class="fig-muted">3 位</text>
    <text x="326" y="166" text-anchor="middle" font-size="10" class="fig-muted">2 位</text>
    <text x="366" y="132" font-size="11" class="fig-label">15 个微命令 → 10 位</text>
    <text x="366" y="148" font-size="11" class="fig-muted">组内互斥、组间可并行</text>
    <text x="366" y="164" font-size="11" class="fig-muted">各组 4 / 3 / 6 / 2 个</text>
    <text x="16" y="230" class="fig-label">字段间接</text>
    <line x1="216" y1="210" x2="254" y2="210" class="fig-line" marker-end="url(#lhw-w6-arr-10)"/>
    <line x1="216" y1="244" x2="254" y2="244" class="fig-line-accent" marker-end="url(#lhw-w6-arr-10)"/>
    <line x1="418" y1="227" x2="454" y2="227" class="fig-line-accent" marker-end="url(#lhw-w6-arr-10)"/>
    <rect x="110" y="196" width="106" height="28" rx="3" class="fig-box-blue"/>
    <text x="163" y="214" text-anchor="middle" font-size="10" class="fig-blue">字段 E（编码位）</text>
    <rect x="110" y="230" width="106" height="28" rx="3" class="fig-box-orange"/>
    <text x="163" y="248" text-anchor="middle" font-size="10" class="fig-orange">字段 F（解释码）</text>
    <rect x="258" y="196" width="160" height="62" rx="4" class="fig-box-subtle"/>
    <text x="338" y="222" text-anchor="middle" font-size="11" class="fig-label">二级联合译码器</text>
    <text x="338" y="240" text-anchor="middle" font-size="10" class="fig-muted">由 F 的状态解释 E 的含义</text>
    <rect x="458" y="208" width="136" height="38" rx="4" class="fig-box-green"/>
    <text x="526" y="231" text-anchor="middle" font-size="11" class="fig-green">输出对应微命令</text>
    <text x="110" y="284" font-size="11" class="fig-muted">字段 E 的微命令含义需由字段 F 中的某些微命令来解释，故需两级译码</text>
    <text x="110" y="306" font-size="11" class="fig-muted">字长进一步缩短，但控制信号产生最慢、并行性受限</text>
  </svg>
  <figcaption>图 10-2　微指令操作控制字段的三种编码方式</figcaption>
</figure>

> <strong>★考点：字段直接编码位数必须按 $\lceil \log_2(n+1)\rceil$ 算，不能漏掉“不操作”</strong> 那一个编码；只有<strong>互斥</strong>的微命令才能编在同一字段。

> <strong>注意</strong>：字段直接编码的“组”不是按功能随意划分，而是按“是否可能在同一节拍同时有效”划分。若把可并行的两个微命令放进同一组，二者就只能择一，机器会丢并行性。

### 10.3 微指令的格式：水平型与垂直型

微指令由两段构成：<strong>操作控制字段</strong>（发哪些微命令）+ <strong>顺序控制字段</strong>（下一条微指令的地址，以及供分支用的判别测试位/标志）。两段的宽度之比，决定了微指令是“水平”还是“垂直”。

| 比较项 | 水平型微指令 | 垂直型微指令 |
|---|---|---|
| 一次能完成的微操作 | 多个（并行） | 一个（或极少） |
| 操作控制字段 | 很长，接近微命令总数 | 很短，类似操作码 |
| 是否需译码 | 直接编码时不需要 | 需要译码 |
| 微指令字长 | 长（几十至上百位） | 短（十几至几十位） |
| 微程序长度 | 短 | 长 |
| 执行速度 | 快（并行度高） | 慢（串行逐条） |
| 编写/调试 | 难 | 易 |
| 典型编码 | 直接编码、字段直接编码 | 字段编码，类似机器指令 |

水平型与垂直型是<strong>并行程度的两个极端</strong>，实际机器常取混合型：字段直接编码，一组字段内的微命令互斥、字段间并行。本课程模型机最终采用的 24 位操作控制字段就属水平型（直接编码 + 余量）。

<figure class="cpd-figure not-content" id="lhw-w6-fig-19">
  <svg viewBox="0 0 700 320" role="img" aria-label="水平型与垂直型微指令对比" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-11" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="18" class="fig-title" font-size="13">水平型与垂直型微指令对比</text>
    <rect x="20" y="30" width="320" height="272" rx="4" class="fig-box-dashed"/>
    <text x="180" y="50" text-anchor="middle" font-size="13" class="fig-title">水平型</text>
    <rect x="40" y="62" width="35" height="30" class="fig-box"/><rect x="75" y="62" width="35" height="30" class="fig-box"/><rect x="110" y="62" width="35" height="30" class="fig-box"/><rect x="145" y="62" width="35" height="30" class="fig-box-blue"/><rect x="180" y="62" width="35" height="30" class="fig-box"/><rect x="215" y="62" width="35" height="30" class="fig-box"/><rect x="250" y="62" width="35" height="30" class="fig-box"/><rect x="285" y="62" width="35" height="30" class="fig-box-blue"/>
    <text x="57" y="82" text-anchor="middle" font-size="11" class="fig-label">1</text>
    <text x="92" y="82" text-anchor="middle" font-size="11" class="fig-label">1</text>
    <text x="127" y="82" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="162" y="82" text-anchor="middle" font-size="11" class="fig-blue">1</text>
    <text x="197" y="82" text-anchor="middle" font-size="11" class="fig-label">1</text>
    <text x="232" y="82" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="267" y="82" text-anchor="middle" font-size="11" class="fig-label">0</text>
    <text x="302" y="82" text-anchor="middle" font-size="11" class="fig-blue">1</text>
    <text x="180" y="112" text-anchor="middle" font-size="11" class="fig-label">一条微指令可同时发出多个微命令</text>
    <line x1="102" y1="138" x2="115" y2="138" class="fig-line" marker-end="url(#lhw-w6-arr-11)"/>
    <line x1="180" y1="138" x2="193" y2="138" class="fig-line" marker-end="url(#lhw-w6-arr-11)"/>
    <rect x="40" y="126" width="62" height="24" class="fig-box"/><rect x="118" y="126" width="62" height="24" class="fig-box"/><rect x="196" y="126" width="62" height="24" class="fig-box"/>
    <text x="71" y="143" text-anchor="middle" font-size="11" class="fig-label">μ0</text>
    <text x="149" y="143" text-anchor="middle" font-size="11" class="fig-label">μ1</text>
    <text x="227" y="143" text-anchor="middle" font-size="11" class="fig-label">μ2</text>
    <text x="40" y="176" font-size="11" class="fig-muted">微程序条数少</text>
    <text x="40" y="200" font-size="11" class="fig-label">· 字长长、并行度高、速度快</text>
    <text x="40" y="220" font-size="11" class="fig-label">· CM 容量大，编写调试难</text>
    <text x="40" y="240" font-size="11" class="fig-label">· 直接编码 / 字段直接编码</text>
    <text x="40" y="260" font-size="11" class="fig-label">· 一条微指令 ≈ 一个节拍的全部控制</text>
    <text x="40" y="280" font-size="11" class="fig-blue">本模型机的 24 位控制字段属此类</text>
    <rect x="370" y="30" width="320" height="272" rx="4" class="fig-box-dashed"/>
    <text x="530" y="50" text-anchor="middle" font-size="13" class="fig-title">垂直型</text>
    <rect x="390" y="62" width="90" height="30" class="fig-box"/>
    <text x="435" y="82" text-anchor="middle" font-size="11" class="fig-label">操作码</text>
    <rect x="480" y="62" width="170" height="30" class="fig-box"/>
    <text x="565" y="82" text-anchor="middle" font-size="11" class="fig-label">下地址 / 操作数</text>
    <text x="530" y="112" text-anchor="middle" font-size="11" class="fig-label">一条微指令只完成一个微操作</text>
    <rect x="390" y="126" width="38" height="24" class="fig-box"/><rect x="434" y="126" width="38" height="24" class="fig-box"/><rect x="478" y="126" width="38" height="24" class="fig-box"/><rect x="522" y="126" width="38" height="24" class="fig-box"/><rect x="566" y="126" width="38" height="24" class="fig-box"/><rect x="610" y="126" width="38" height="24" class="fig-box"/>
    <text x="409" y="143" text-anchor="middle" font-size="9" class="fig-label">μ0</text>
    <text x="453" y="143" text-anchor="middle" font-size="9" class="fig-label">μ1</text>
    <text x="497" y="143" text-anchor="middle" font-size="9" class="fig-label">μ2</text>
    <text x="541" y="143" text-anchor="middle" font-size="9" class="fig-label">μ3</text>
    <text x="585" y="143" text-anchor="middle" font-size="9" class="fig-label">μ4</text>
    <text x="629" y="143" text-anchor="middle" font-size="9" class="fig-label">μ5</text>
    <text x="390" y="176" font-size="11" class="fig-muted">微程序条数多，串行逐条解释</text>
    <text x="390" y="200" font-size="11" class="fig-label">· 字长短、并行度低、速度慢</text>
    <text x="390" y="220" font-size="11" class="fig-label">· CM 容量小，编写容易</text>
    <text x="390" y="240" font-size="11" class="fig-label">· 类似机器指令格式，需译码</text>
    <text x="390" y="260" font-size="11" class="fig-label">· 一条微指令 ≈ 一个微命令</text>
    <text x="390" y="280" font-size="11" class="fig-blue">毫微程序控制器中的微指令用此类</text>
  </svg>
  <figcaption>图 10-3　水平型与垂直型微指令格式对比</figcaption>
</figure>

> <strong>注意</strong>：不能把“水平/垂直”理解成两种互斥的固定格式。它是<strong>并行度</strong>的连续谱：字段直接编码的水平型微指令，其每个字段内部仍是“垂直”的编码。做题时按题干给的字长与控制字段结构判断，而不是按名称。

### 10.4 微程序流程与后继微地址的形成

微程序通常成块存放在 CM 中，块内靠<strong>顺序控制字段</strong>串联。后继微地址有三类来源，全部送入“顺序逻辑”（多路选择器）后择一输出到 CMAR：

1. <strong>CMDR 的下地址字段</strong>：最常用，微指令自己给出下一条的地址，顺序执行与无条件转移都用它。
2. <strong>指令操作码经微地址形成部件</strong>：只在“取指微程序末条 → 执行微程序入口”这一处使用，形成的是<strong>该指令执行微程序的首地址</strong>。
3. <strong>顺序逻辑按标志选择</strong>：条件分支时，用判别测试位/标志（如 A0）在两个候选入口间选择。

顺序执行也可以用计数器方式实现：微地址寄存器加 1 得到下一条地址（$\mu PC+1$），此时不必在每条微指令中都写下地址，但遇到转移仍需地址字段或测试网络干预。

两条关键的地址转移：

- <strong>取指微程序最后一条</strong>：下地址字段为“××××××”，实际值由 OP 经微地址形成部件产生——此时操作码已送入 IR，正是确定“这条指令该执行哪个微程序”的时刻。
- <strong>执行微程序最后一条</strong>：下地址字段固定为<strong>取指微程序入口</strong>（本课程模型中为八进制 00，即全 0），于是自动回到取指，开始下一条机器指令。

<figure class="cpd-figure not-content" id="lhw-w6-fig-20">
  <svg viewBox="0 0 720 340" role="img" aria-label="取指微程序与执行微程序之间的地址转移" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-12" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="18" class="fig-title" font-size="13">取指微程序与执行微程序之间的地址转移</text>
    <text x="30" y="46" font-size="11" class="fig-muted">取指微程序（公共）</text>
    <line x1="105" y1="90" x2="105" y2="100" class="fig-line" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="105" y1="140" x2="105" y2="150" class="fig-line" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="180" y1="172" x2="208" y2="172" class="fig-line" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="362" y1="172" x2="386" y2="172" class="fig-line-accent"/>
    <line x1="386" y1="56" x2="386" y2="176" class="fig-line-accent"/>
    <line x1="386" y1="56" x2="398" y2="56" class="fig-line-accent" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="386" y1="96" x2="398" y2="96" class="fig-line-accent" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="386" y1="136" x2="398" y2="136" class="fig-line-accent" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="386" y1="176" x2="398" y2="176" class="fig-line-accent" marker-end="url(#lhw-w6-arr-12)"/>
    <line x1="580" y1="56" x2="600" y2="56" class="fig-line"/>
    <line x1="580" y1="96" x2="600" y2="96" class="fig-line"/>
    <line x1="580" y1="136" x2="600" y2="136" class="fig-line"/>
    <line x1="580" y1="176" x2="600" y2="176" class="fig-line"/>
    <path d="M600,56 L600,272 L15,272 L15,72 L26,72" fill="none" stroke="var(--sl-color-green)" marker-end="url(#lhw-w6-arr-12)"/>
    <rect x="30" y="54" width="150" height="36" rx="3" class="fig-box"/>
    <text x="105" y="77" text-anchor="middle" font-size="11" class="fig-label">00：PC→MAR，1→R</text>
    <rect x="30" y="104" width="150" height="36" rx="3" class="fig-box"/>
    <text x="105" y="127" text-anchor="middle" font-size="11" class="fig-label">01：M→MDR，PC+1</text>
    <rect x="30" y="154" width="150" height="36" rx="3" class="fig-box"/>
    <text x="105" y="170" text-anchor="middle" font-size="11" class="fig-label">02：MDR→IR</text>
    <text x="105" y="184" text-anchor="middle" font-size="10" class="fig-red">下地址 ××××××</text>
    <rect x="212" y="146" width="150" height="52" rx="3" class="fig-box-blue"/>
    <text x="287" y="167" text-anchor="middle" font-size="11" class="fig-blue">微地址形成部件</text>
    <text x="287" y="184" text-anchor="middle" font-size="11" class="fig-blue">按 OP 定入口</text>
    <text x="400" y="34" font-size="11" class="fig-muted">执行微程序（各指令不同）</text>
    <rect x="400" y="40" width="180" height="32" rx="3" class="fig-box"/>
    <text x="490" y="61" text-anchor="middle" font-size="11" class="fig-label">LDA 取数微程序</text>
    <rect x="400" y="80" width="180" height="32" rx="3" class="fig-box"/>
    <text x="490" y="101" text-anchor="middle" font-size="11" class="fig-label">ADD 加法微程序</text>
    <rect x="400" y="120" width="180" height="32" rx="3" class="fig-box"/>
    <text x="490" y="141" text-anchor="middle" font-size="11" class="fig-label">STA 存数微程序</text>
    <rect x="400" y="160" width="180" height="32" rx="3" class="fig-box"/>
    <text x="490" y="181" text-anchor="middle" font-size="11" class="fig-label">CLA / 取反 等</text>
    <text x="250" y="290" font-size="11" class="fig-green">执行微程序末条：下地址 = 000000，回到取指入口</text>
    <text x="607" y="250" font-size="11" class="fig-green">顺序控制字段</text>
  </svg>
  <figcaption>图 10-4　取指微程序与执行微程序之间的地址转移</figcaption>
</figure>

> <strong>★考点</strong>：看到“取指微程序最后一条微指令的下地址”就答<strong>由指令操作码经微地址形成部件产生</strong>；看到“执行微程序最后一条”就答<strong>指向取指微程序入口</strong>。

> <strong>注意</strong>：微程序的执行顺序由<strong>微指令中的地址</strong>决定，不是由 CM 的物理相邻单元决定。微指令可以存放在任意单元，靠下地址字段跳转。

### 10.5 微程序设计举例：十条指令的微程序

设计微程序的前提与组合逻辑设计完全一样：先写出每条机器指令的微操作及其节拍安排。节拍决定微指令的条数与先后次序，微操作决定控制字段里哪些位为 1（仍采用非总线结构）。

<strong>（1）取指微程序的写法</strong>

取指需要三个节拍：T0（PC→MAR，1→R）、T1（M→MDR，PC+1）、T2（MDR→IR，同时 OP→微地址形成部件）。若把“把下地址送 CMAR”也单独占一个节拍，则取指微程序 = 3 条节拍微指令 + 若干条“下地址→CMAR”微指令，最后一条的下地址由 OP 形成，指向执行微程序入口。

<strong>（2）统计与初次估算</strong>

10 条指令的全部微操作去重后共 <strong>20 种</strong>，全部微程序合计 <strong>38 条微指令</strong>，其中 <strong>19 条</strong> 专门用于形成后继地址（1 条把 OP 送微地址形成部件，18 条把当前微指令的下地址送 CMAR）。

直接编码时：

$$
\text{word length}=20\ (\text{control})+6\ (\text{next addr})=26\ \text{bits}
$$

下地址 6 位是因为 38 条微指令需要寻址，$2^5=32<38\le 64=2^6$。

<strong>（3）简化：省掉“送 CMAR”这一拍</strong>

若让每条微指令的下地址字段<strong>直接</strong>经顺序逻辑（多路选择器）送 CM 的地址线，就不需要“下地址→CMAR”的专用节拍，则那 18 条微指令连同“OP→微地址形成”那 1 条一起省去：

- 微指令总数：38 − 19 = <strong>19 条</strong> → 下地址 5 位即可（$2^5=32\ge 19$）；
- 微操作种类：20 − 2 = <strong>18 种</strong> → 控制字段 18 位即可。

考虑指令集扩展留余量：控制字段扩到 <strong>24 位</strong>（多留 6 个控制信号），下地址扩到 <strong>6 位</strong>（每加 1 位，CM 寻址空间翻倍），最终

$$
\text{microinstr length}=24+6=30\ \text{bits}
$$

<figure class="cpd-figure not-content" id="lhw-w6-fig-21">
  <svg viewBox="0 0 700 250" role="img" aria-label="微指令字长的两次估算" font-family="sans-serif" font-size="12">
    <text x="16" y="18" class="fig-title" font-size="13">微指令字长的两次估算</text>
    <text x="16" y="72" font-size="11" class="fig-label">初版</text>
    <rect x="110" y="52" width="380" height="32" class="fig-box"/>
    <rect x="490" y="52" width="120" height="32" class="fig-box-subtle"/>
    <text x="300" y="73" text-anchor="middle" font-size="11" class="fig-label">操作控制字段 20 位（20 种微操作）</text>
    <text x="550" y="73" text-anchor="middle" font-size="10" class="fig-muted">下地址 6 位</text>
    <text x="620" y="73" font-size="11" class="fig-label">共 26 位</text>
    <text x="110" y="102" font-size="11" class="fig-muted">38 条微指令 → 需 6 位地址；直接编码每位一个微命令</text>
    <text x="16" y="160" font-size="11" class="fig-label">简化后</text>
    <rect x="110" y="140" width="456" height="32" class="fig-box-blue"/>
    <rect x="566" y="140" width="114" height="32" class="fig-box-subtle"/>
    <text x="338" y="161" text-anchor="middle" font-size="11" class="fig-blue">操作控制字段 24 位（18 种微操作 + 6 位扩展余量）</text>
    <text x="623" y="161" text-anchor="middle" font-size="10" class="fig-muted">下地址 6 位</text>
    <text x="110" y="190" font-size="11" class="fig-muted">19 条微指令只需 5 位，留 1 位作扩展；总长 30 位</text>
    <text x="110" y="214" font-size="11" class="fig-blue">下地址位数由 CM 容量决定，控制字段位数由微命令种数决定</text>
  </svg>
  <figcaption>图 10-5　微指令字长的两次估算（初版 26 位 vs 简化后 30 位）</figcaption>
</figure>

<figure class="cpd-figure not-content" id="lhw-w6-fig-22">
  <svg viewBox="0 0 700 220" role="img" aria-label="硬布线控制器与微程序控制器对比" font-family="sans-serif" font-size="12">
    <defs>
      <marker id="lhw-w6-arr-13" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" markerUnits="userSpaceOnUse" orient="auto">
        <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="20" class="fig-title" font-size="13">硬布线控制器（组合逻辑）vs 微程序控制器（存储逻辑）</text>
    <rect x="20" y="34" width="315" height="170" rx="4" class="fig-box-dashed"/>
    <text x="177" y="54" text-anchor="middle" font-size="12" font-weight="bold" class="fig-orange">硬布线控制器（组合逻辑）</text>
    <line x1="115" y1="86" x2="146" y2="86" class="fig-line" marker-end="url(#lhw-w6-arr-13)"/>
    <line x1="255" y1="86" x2="280" y2="86" class="fig-line" marker-end="url(#lhw-w6-arr-13)"/>
    <rect x="35" y="68" width="80" height="36" rx="3" class="fig-box"/>
    <text x="75" y="90" text-anchor="middle" font-size="11" class="fig-label">OP / 节拍</text>
    <rect x="150" y="68" width="105" height="36" rx="3" class="fig-box-orange"/>
    <text x="202" y="90" text-anchor="middle" font-size="11" class="fig-orange">门电路阵列</text>
    <text x="298" y="90" text-anchor="middle" font-size="11" class="fig-label">微命令</text>
    <text x="35" y="132" font-size="11" class="fig-label">· 速度快（门延迟级）</text>
    <text x="35" y="152" font-size="11" class="fig-label">· 电路庞杂，修改/扩展指令极难</text>
    <text x="35" y="172" font-size="11" class="fig-muted">· 多用于 RISC / 高速流水线</text>
    <rect x="365" y="34" width="315" height="170" rx="4" class="fig-box-dashed"/>
    <text x="522" y="54" text-anchor="middle" font-size="12" font-weight="bold" class="fig-blue">微程序控制器（存储逻辑）</text>
    <line x1="460" y1="86" x2="491" y2="86" class="fig-line" marker-end="url(#lhw-w6-arr-13)"/>
    <line x1="600" y1="86" x2="625" y2="86" class="fig-line" marker-end="url(#lhw-w6-arr-13)"/>
    <rect x="380" y="68" width="80" height="36" rx="3" class="fig-box"/>
    <text x="420" y="90" text-anchor="middle" font-size="11" class="fig-label">CMAR</text>
    <rect x="495" y="68" width="105" height="36" rx="3" class="fig-box-blue"/>
    <text x="547" y="90" text-anchor="middle" font-size="11" class="fig-blue">控制存储器 CM</text>
    <text x="643" y="90" text-anchor="middle" font-size="11" class="fig-label">CMDR</text>
    <text x="380" y="132" font-size="11" class="fig-label">· 结构规整，易修改、易扩展指令</text>
    <text x="380" y="152" font-size="11" class="fig-label">· 速度受限于 CM 访存时间</text>
    <text x="380" y="172" font-size="11" class="fig-muted">· 多用于 CISC / 复杂指令译码</text>
  </svg>
  <figcaption>图 10-6　硬布线控制器与微程序控制器架构对比</figcaption>
</figure>

<strong>（4）微指令编码样例（八进制地址）</strong>

| 地址 | 微操作 | 有效控制位 | 下地址（6 位） |
|---|---|---|---|
| 00 | PC→MAR；1→R | 位0；位1 | 000001 |
| 01 | M→MDR；PC+1 | 位2；位3 | 000010 |
| 02 | MDR→IR；OP→微地址形成部件 | 位4 | ×××××× |
| 03 | 0→AC（清零） | — | 000000 |
| 04 | \~AC→AC（取反） | — | 000000 |
| 10 | 地址码→MAR；读 | 位10；位1 | 001001 |
| 11 | M→MDR | 位2 | 001010 |
| 12 | AC+MDR→AC | — | 000000 |
| 16 | 地址码→MAR；读 | 位10；位1 | 001111 |
| 17 | M→MDR | 位2 | 010000 |
| 20 | MDR→AC | — | 000000 |

读法要点：

- 地址是<strong>八进制</strong>。10 表示十进制 8，16 表示十进制 14，17 表示 15，20 表示 16；两位八进制数正好对应 6 位下地址（如八进制 11 = 001001，17 = 001111，20 = 010000）。
- 表中“有效控制位”的“—”表示讲义未标出该微操作对应的位号（如清零、取反），实际编码时必须占用一位。
- 同一节拍内的两个相容微操作在同一条微指令中（如 00 的位0、位1 同时为 1）；取指 02 中“OP→微地址形成部件”因版面未列出对应位，但它是必不可少的第二条控制线。
- 每条执行微程序最后一条的下地址都是 000000，即回取指；取指 02 的下地址是“××××××”，由 OP 决定。

> <strong>★考点</strong>：微指令字长 = 操作控制字段位数 + 下地址字段位数；下地址位数按 $\lceil \log_2(\text{microinstr count})\rceil$ 取，并留扩展余量。

> <strong>注意</strong>：表中的地址与下地址是八进制（下地址列写成 6 位二进制），别当成十进制。“12”的下一条不是十进制直觉里的“13”，而要按八进制递增。

### 10.6 静态/动态微程序设计、毫微程序与串并行控制

<strong>（1）静态与动态微程序设计</strong>

| 类型 | 控制存储器 | 适用 |
|---|---|---|
| 静态微程序设计 | ROM，微程序固化后不改 | 指令集固定，批量生产 |
| 动态微程序设计 | 可写控制存储器（WCS/EPROM 类） | 需修改或扩展指令集 |

动态微程序设计的价值在于<strong>指令仿真</strong>：往 CM 中写入新指令的微程序，就能在一台机器上模拟另一套指令系统，测试新指令对某类应用的性能；若收益明显，下一代处理器可把该指令正式加入指令集。实际产品更多是<strong>增加指令</strong>而非改动原有指令。

<strong>（2）毫微程序设计</strong>

微程序设计用<strong>微程序解释机器指令</strong>；毫微程序设计用<strong>毫微程序解释微指令</strong>，是两级解释：

- 微指令采用<strong>垂直型</strong>（编码压缩，像机器指令的操作码），一条微指令只表示一个较复杂的操作；
- 微指令的地址码部分给出对应<strong>毫微程序</strong>的入口，送 CMAR2，从第二个控制存储器取出毫微指令；
- 毫微指令采用<strong>水平型</strong>，其控制位直接送各部件，把这条微指令要求的复杂操作分解为若干有先后次序的微操作。

关系对照：机器指令 : 微指令 = 微指令 : 毫微指令。所以毫微程序控制器需要两套地址寄存器（CMAR、CMAR2）和两个控制存储器。

<strong>（3）串行与并行微程序控制</strong>

- <strong>串行</strong>：取第 i 条微指令 → 执行 → 再取第 i+1 条。取微指令与执行微指令严格串行，结构简单，但每个节拍都要等待一次 CM 读。
- <strong>并行（流水）</strong>：在执行第 i 条微指令的同时，利用取微指令部件的空闲时间预取第 i+1 条，从而使“取微指令”与“执行微指令”重叠，缩短微程序的总执行时间。

> <strong>注意</strong>：毫微程序只解决“一条微指令内部还要再分解”的问题，它并不减少控制存储器的总容量（多了一个 CM），换来的是微指令可以写得更短、更抽象。
