Skip to content

AgentZero2002/ComputerArchitecture

Repository files navigation

THU Computer Architecture Sample Code Repository

清华大学计算机组成原理课程 —— 样本代码仓库

C++ Standard

简介

本仓库基于 Patterson & Hennessy《计算机组成与设计 (RISC-V Edition)》体系构建,提供 48 个完整可运行 C++ 实现,涵盖 12 章、4 大知识领域。每个文件对应一个核心概念的模拟器/演示器。适合:

  • 清华计算机组成原理课程的学习者
  • 408/826 计算机考研复习
  • CPU/缓存/流水线等硬件概念的实践理解
  • 体系结构方向的面试准备

仓库结构 (4 部 12 章)

ca/
├── Hello.cpp                              # 仓库入口
│
╔══ Part I: 基础篇 (01-03) — 性能/数据/运算 ═════════════════════════════╗
║                                                                        ║
├── 01_introduction/                       # 第1章 计算机导论
│   ├── performance_metrics.cpp            #   性能度量 (CPI/MIPS/CPU时间)
│   ├── amdahl_law.cpp                     #   Amdahl定律 & 加速比分析
│   └── moores_law.cpp                     #   摩尔定律 & 工艺演进
│
├── 02_data_representation/                # 第2章 数据表示
│   ├── integer_representation.cpp         #   补码/溢出/符号扩展
│   ├── ieee754.cpp                        #   IEEE 754 浮点数编码
│   ├── endianness.cpp                     #   大端/小端 & 字节序转换
│   └── character_encoding.cpp             #   ASCII/Unicode/UTF-8
│
├── 03_arithmetic/                         # 第3章 算术运算
│   ├── alu.cpp                            #   32-bit ALU 完整实现
│   ├── adder_designs.cpp                  #   RCA/CLA 加法器对比
│   ├── multiplier.cpp                     #   Shift-Add/Booth/Wallace Tree
│   ├── divider.cpp                        #   Restoring/Non-Restoring 除法
│   └── floating_point_ops.cpp             #   浮点加减乘除 & 舍入
║                                                                        ║
╚══════════════════════════════════════════════════════════════════════════╝

╔══ Part II: 处理器设计 (04-07) — ISA/单周期/流水线/冒险 ═════════════════╗
║                                                                        ║
├── 04_isa/                                # 第4章 指令集架构
│   ├── riscv_isa.cpp                      #   RISC-V ISA 概述 & 设计哲学
│   ├── instruction_decode.cpp             #   指令解码器 (6种格式)
│   ├── assembler.cpp                      #   RISC-V 汇编器 (text→binary)
│   └── disassembler.cpp                   #   RISC-V 反汇编器 (binary→text)
│
├── 05_single_cycle/                       # 第5章 单周期CPU
│   ├── datapath.cpp                       #   数据通路 (RegFile/Mem/PC)
│   ├── control_unit.cpp                   #   控制单元 (opcode→控制信号)
│   └── single_cycle_cpu.cpp               #   完整单周期CPU模拟器
│
├── 06_pipeline/                           # 第6章 流水线
│   ├── pipeline_registers.cpp             #   流水线寄存器 (IF/ID/EX/MEM/WB)
│   ├── five_stage_pipeline.cpp            #   5级流水线时空图
│   ├── pipeline_timing.cpp                #   周期级时序追踪 & RAW检测
│   └── pipeline_simulator.cpp             #   流水线性能模拟器
│
├── 07_hazards/                            # 第7章 冒险处理
│   ├── data_hazards.cpp                   #   数据冒险 (RAW/WAW/WAR)
│   ├── forwarding_unit.cpp                #   前递单元 (EX/MEM & MEM/WB)
│   ├── control_hazards.cpp                #   控制冒险 & 分支惩罚
│   └── hazard_simulator.cpp              #   综合冒险模拟 (前递+停顿+冲刷)
║                                                                        ║
╚══════════════════════════════════════════════════════════════════════════╝

╔══ Part III: 存储与I/O (08-10) — 缓存/虚拟内存/总线 ═════════════════════╗
║                                                                        ║
├── 08_cache/                              # 第8章 缓存
│   ├── cache_organization.cpp             #   直接映射/组相联/全相联
│   ├── cache_replacement.cpp              #   LRU/FIFO/Random/OPT 替换
│   ├── cache_write_policy.cpp             #   Write-Through vs Write-Back
│   ├── cache_simulator.cpp                #   缓存模拟器 (3C缺失模型)
│   └── cache_performance.cpp              #   AMAT & CPI影响 & 多级缓存
│
├── 09_virtual_memory/                     # 第9章 虚拟内存
│   ├── page_table.cpp                     #   两级页表 & 多级页表
│   ├── tlb_simulator.cpp                  #   TLB 模拟器
│   ├── address_translation.cpp            #   地址翻译全程 (VA→TLB→PT→PA)
│   └── page_replacement.cpp               #   页面替换 (FIFO/LRU/Clock/OPT)
│
├── 10_io_bus/                             # 第10章 I/O与总线
│   ├── mmio_dma.cpp                       #   MMIO & DMA 传输
│   ├── interrupt_handler.cpp              #   中断处理 & 异常向量表
│   └── bus_arbitration.cpp                #   总线仲裁 (优先级/Round-Robin)
║                                                                        ║
╚══════════════════════════════════════════════════════════════════════════╝

╔══ Part IV: 高级架构 (11-12) — 分支预测/超标量/一致性/SIMD ═════════════╗
║                                                                        ║
├── 11_branch_prediction/                  # 第11章 分支预测
│   ├── static_predictor.cpp               #   静态预测 (BTFN/Profiling)
│   ├── dynamic_predictor.cpp              #   动态预测 (1-bit/2-bit BHT)
│   ├── correlating_predictor.cpp          #   关联预测 (GShare/Tournament)
│   └── branch_simulator.cpp               #   综合模拟 (BTB+BHT+RAS)
│
├── 12_advanced_arch/                      # 第12章 高级架构
│   ├── superscalar.cpp                    #   超标量多发射 & IPC分析
│   ├── tomasulo.cpp                       #   Tomasulo算法 & 乱序执行
│   ├── cache_coherence.cpp                #   MESI缓存一致性协议
│   └── simd_vector.cpp                    #   SIMD & 向量处理 (SSE/AVX/NEON)
║                                                                        ║
╚══════════════════════════════════════════════════════════════════════════╝

快速开始

每个 .cpp 文件均可独立编译运行:

g++ -std=c++17 01_introduction/performance_metrics.cpp -o perf
./perf

各章核心内容速查

Part I — 基础篇

章节 核心内容 关键概念
计算机导论 性能度量、Amdahl/Gustafson定律、摩尔定律 CPI, MIPS, Speedup
数据表示 补码、IEEE 754、字节序、字符编码 Two's complement, NaN, Endianness
算术运算 ALU、加法器(CLA)、乘法器(Booth)、除法器 Carry Lookahead, Wallace Tree

Part II — 处理器设计

章节 核心内容 关键概念
指令集架构 RISC-V ISA、指令格式、汇编/反汇编 R/I/S/B/U/J, 6 formats
单周期CPU 数据通路、控制单元、完整CPU模拟器 Datapath, Control Signals
流水线 5级流水线、时序图、性能模型 IF/ID/EX/MEM/WB, Speedup
冒险处理 数据冒险、前递单元、控制冒险 Forwarding, Load-Use Stall

Part III — 存储与I/O

章节 核心内容 关键概念
缓存 组织方式/替换/写策略/模拟器/性能 AMAT, 3C Misses, Write-Back
虚拟内存 页表/TLB/地址翻译/页面替换 Page Walk, TLB Miss, Clock
I/O与总线 MMIO/DMA/中断/总线仲裁 BusRdX, Interrupt Vector

Part IV — 高级架构

章节 核心内容 关键概念
分支预测 静态/动态/GShare/Tournament/BTB+RAS BHT, TAGE, Perceptron
高级架构 超标量/Tomasulo/MESI/SIMD ILP, Register Renaming, MESI

参考资料

  • 教材: Patterson & Hennessy, Computer Organization and Design: RISC-V Edition
  • 进阶: Hennessy & Patterson, Computer Architecture: A Quantitative Approach
  • RISC-V: RISC-V ISA Manual Vol.1

构建说明

本仓库由以下 AI 协作完成:

  • 代码架构与实现: Claude (Anthropic) — 生成全部 C++ 代码,设计目录结构与测试用例
  • 推理引擎: DeepSeek V4 Pro (1M 上下文) — 提供底层推理能力支持

所有代码经人工审查确认,AI 工具仅作为生产力辅助。

许可

MIT License

About

清华计算机组成原理 | 12章48文件 | CPU模拟器/流水线/缓存/分支预测/Tomasulo乱序/MESI一致性 | 基于 Patterson & Hennessy COD RISC-V Edition

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages