How openxiangshan/xiangshan works
<explanation>
1. Identify the project type and purpose
- XiangShan is an open‑source, high‑performance RISC‑V processor microarchitecture implemented in Chisel/Scala.
- It generates a full SoC wrapper (Top) with an out‑of‑order core, memory hierarchy (L1–L3), and AXI4‑based peripheral interface.
- It integrates simulation frameworks (difftest, Verilator emu), agile‑development scripts, and device models.
2. Analyze the file structure
- Top‑level directories:
• src/main/scala: all Chisel design code
– top: Chisel entry point, configuration parser, Generator
– system: SoC wrapper around the core and memory bus
– xiangshan: core microarchitecture (frontend, backend, cache, mem, transforms)
– device: virtual AXI4 peripherals (RAM, flash, UART, PLIC, CLINT, keyboard, VGA)
– utils: helper traits, bus codecs, arbiter, Bundles, Verilog record generators
• huancun, coupledL2: stand‑alone L2/L3 cache submodules
• fudian: floating‑point unit submodule
• difftest: cosimulation framework connecting to NEMU
• ready‑to‑run, debug, scripts: simulation images, debug harnesses, performance/test scripts
• Makefile, build.sbt/build.sc, mill‑version: build, test, generate verilog
3. Readme insights
- The master branch implements the Kunminghu microarchitecture:
– Frontend: multiple branch predictors, predecode, IFU, ICache
– Backend: decode → rename → dispatch → execution units (integer, vector, CSR, FPU) → ROB → commit
– Memory subsystem: L1 DCache, MMU/TLB, L2/L3 (huancun), prefetchers, store buffers, SB‐buffer
– SoC‑level AXI4 interconnect to on‑chip devices
- A high‑level SVG of the Kunminghu pipeline is provided in images/xs‑arch‑kunminghu.svg.
4. Main components & interactions to show
a. SoC wrapper (Top)
- Clock/reset, configuration loader (YAML parser)
- AXI4 interconnect fabric
- Attached AXI4 devices: RAM, flash, PLIC, CLINT, UART, keyboard, VGA
b. Core pipeline
- Fetch & branch prediction hierarchy → ICache
- Instruction queue → decode
- Rename & register file → dispatch queues
- Execution clusters: ALU, branch, multiplier, divider, CSR, FPU, vector units
- Out‑of‑order scheduler, issue window, ROB → retirement
c. Memory hierarchy
- L1 DCache → Walk TLB → L2/L3 submodule (huancun or coupledL2) → main memory
- Store buffer & SBuffer for memory disambiguation
- Prefetchers at L1 and L2 levels
d. Control & side channels
- CSR pipeline (CSR module, trap/interrupt handling)
- Difftest co‑simulation interface
- Debug module: performance counter, SC‑stat, trace buffer
5. Architectural patterns & design principles
- Modular Chisel‑based RTL: separate packages for frontend/backend/cache/device.
- OOO pipeline with decoupled handshake nodes (DataDontCareNode, DebugIdentityNode).
- AXI4 bus for memory and peripherals; YAML‑driven configuration.
- FIRRTL transforms (in xiangshan/transforms) for naming and print control.
- Agile scripts automate generation, simulation, performance collection.
6. Diagram‐drawing guidelines
- Use a layered layout:
1) Top‑level SoC & bus on the left
2) Core pipeline in the center, broken into stages
3) Memory hierarchy on the right
4) Peripherals at the bottom
- Color coding:
• Pipeline stages (e.g., fetch/decode/rename/dispatch/execute/commit) in one color
• Memory (caches, TLB, prefetch) in another
• Devices and interconnect in a third
• Simulation/test harness in grey
- Shapes:
• Rectangles for major modules (e.g., Frontend, DCache, ROB)
• Rounded rectangles or clouds for external systems (RAM, flash, NEMU)
• Diamond or parallelogram for queues or buffers
- Arrows:
• Solid arrows for data/instruction flow (fetch → decode → dispatch → execute → commit)
• Dashed arrows for control or configuration (flush, interrupts, CSR access)
• Bidirectional arrows for AXI4 handshakes
7. Tailoring to XiangShan
- Highlight the Chisel‐to‐Verilog generation flow: Generator → make verilog → build/XSTop.v
- Emphasize the YAML‐based config (Default.yml) plugged into ArgParser and Configs.scala
- Show how huancun/fudian/difftest integrate as submodules
- Call out the debug and perf scripts directory to indicate external tooling
8. Essentials to include in the final diagram
- Clear labels on each block matching package names (e.g., xiangshan.frontend.IFU, L1Cache)
- Directional arrows with annotated bus widths and protocols (e.g., “AXI4 read/write”)
- Color legend showing component categories (Pipeline, Memory, Devices, Tools)
- A note or inset illustrating the software toolchain (mill, sbt, Makefile targets, Verilator)
By following this structure, you will accurately capture XiangShan’s microarchitecture, its SoC context, and the supporting tools in a single comprehensive system design diagram.
</explanation>