---
# System prepended metadata

title: 'Class 1 : Gem5 With Risc-V Env Setting'
tags: [Gem5 練習]

---

# Class 1 : Gem5 With Risc-V Env Setting 
## 第一階段：基礎建設與首次模擬

* 目標： 在你的系統上成功安裝 gem5，並運行一個最基礎的 RISC-V 模擬。

這一步的重點是「讓它動起來」

> Ubuntu 22.04 LTS (sever version on UTM using Mac)
### 第 1 步：環境設定 (安裝依賴項)


```
# 適用於 Ubuntu 20.04/22.04
sudo apt-get update
sudo apt-get install build-essential git m4 scons zlib1g zlib1g-dev \
    libprotobuf-dev protobuf-compiler libprotoc-dev libgoogle-perftools-dev \
    python3-dev python-is-python3 libboost-all-dev pkg-config
```

>* build-essential, git, m4: 基本的編譯工具。
>* scons: gem5 使用的編譯系統，類似於 make。
>* python3-dev: gem5 的設定檔是用 Python 寫的。
>* libprotobuf-dev: 用於處理資料交換格式。

### 第 2 步：下載 gem5 原始碼

安裝完依賴項後，我們需要從官方的 Git 倉庫下載 gem5 的原始碼。建立一個你喜歡的工作目錄，然後執行：
```
# 建議在你的家目錄 (~) 下建立一個工作區
mkdir ~/gem5
cd ~/gem5

# 1. 下載 gem5 原始碼
git clone https://gem5.googlesource.com/public/gem5

# 2. 下載 gem5 模擬資源
git clone https://gem5.googlesource.com/public/gem5-resources
```

### 第 3 步 : 編譯 & python 依賴
```
cd gem5  # 進入你剛下載的 gem5 目錄

# 使用 scons 編譯 RISC-V 的優化版本
# -j 後面的數字是你想使用的 CPU 核心數，可以大幅加快編譯速度
# 例如，如果你的電腦有 8 個核心，可以用 -j8 或 -j9
scons build/RISCV/gem5.opt -j$(nproc)
```
會遇到
>You're missing the pre-commit/commit-msg hooks. These hook help to ensure your
code follows gem5's style rules on git commit and your commit messages follow
our commit message requirements. This script will now install these hooks in
your .git/hooks/ directory.
Press enter to continue, or ctrl-c to abort:
 
>Cannot find 'pre-commit'. Please ensure all Python requirements are 
installed. This can be done via 'pip install -r requirements.txt'.
It is strongly recommended you install the pre-commit hooks before working with gem5. Do you want to continue compilation (y/n)?


#### 先裝python venv
```
sudo apt install python3.12-venv
```
#### 開啟 venv
```
 source gem5-env/bin/activate
 
 # 安裝
 pip install -r requirements.txt
 
```
#### 退出 venv
```
deactivate
```

#### 重新編譯
```
scons build/RISCV/gem5.opt -j$(nproc)
```

> 如果遇到
```ㄏ
Summary of Warnings
Warning: Header file <png.h> not found.
         This host has no libpng library.
         Disabling support for PNG framebuffers.
Warning: Couldn't find HDF5 C++ libraries. Disabling HDF5 support.
```
> 可以不用理他，但要解他就安裝
```
sudo apt-get install libpng-dev
sudo apt-get install libhdf5-dev
```
### 第 4 步 : 確認
先跳出 venv
```
deactivate
```
確認
```
ls build/RISCV/gem5.opt
```
如果你能看到這個檔案，恭喜你！你已經成功打造了專屬於你的 RISC-V 模擬器。

### 第 5 步 : Reference 
參考資料：

>gem5 官方建置文件 (Building gem5): https://www.gem5.org/documentation/learning_gem5/part1/building


>gem5-resources GitHub (README): 
>https://github.com/gem5/gem5-resources/blob/stable/README.md



---
# 9/9 
## 安裝
### 1
```
sudo apt update
sudo apt install -y build-essential git python3 scons swig m4 libprotobuf-dev protobuf-compiler libhdf5-dev zlib1g-dev libtcmalloc-minimal4
```
### 2
```
git clone https://gem5.googlesource.com/public/gem5
cd gem5
```
### 3
```
export GEM5_CHECKOUT_PATH=$(pwd)
export SCONSFLAGS="-j$(nproc)"
```

### 4
```
scons build/RISCV/gem5.opt
./build/RISCV/gem5.opt configs/learning_gem5/part1/simple-riscv.py

```
### test
```
./build/RISCV/gem5.opt configs/learning_gem5/part1/simple-riscv.py
```
### return
```
a@a:~/gem5$ ./build/RISCV/gem5.opt configs/learning_gem5/part1/simple-riscv.py
gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 25.0.0.1
gem5 compiled Sep  9 2025 08:34:23
gem5 started Sep  9 2025 09:03:07
gem5 executing on a, pid 253331
command line: ./build/RISCV/gem5.opt configs/learning_gem5/part1/simple-riscv.py

Global frequency set at 1000000000000 ticks per second
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/arch/riscv/isa.cc:319: info: RVV enabled, VLEN = 256 bits, ELEN = 64 bits
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
Beginning simulation!
src/sim/syscall_emul.hh:1121: warn: readlink() called on '/proc/self/exe' may yield unexpected results in various settings.
      Returning '/home/a/gem5/tests/test-progs/hello/bin/riscv/linux/hello'
src/sim/mem_state.cc:443: info: Increasing stack size by one page.
Hello world!
Exiting @ tick 487017000 because exiting with last active thread context
```