---
# System prepended metadata

title: RISCV 編譯
tags: [RISCV]

---

  ```bash
git clone https://github.com/riscv/riscv-gnu-toolchain

sudo apt-get install autoconf automake python3 libmpc-dev libmpfr-dev libgmp-dev gawk bison flex texinfo patchutils gcc g++ zlib1g-dev libexpat1-dev
sudo apt-get install device-tree-compiler

```
  
如果不行的話請用下面的指令:
sudo apt install python-is-python3
sudo apt-get install texinfo
sudo apt-get install bison
sudo apt-get install flex
sudo apt-get install zlib1g-dev
sudo apt-get install libgmp-dev libmpfr-dev
sudo apt-get install libexpat1-dev

Install GCC and Make:
  ```bash
sudo apt update
sudo apt install build-essential
  ```
 Install Ninja: Ninja is a small build system used to compile and link code. To install it, run:
  ```bash
sudo apt install ninja-build


gcc --version
make --version
ninja --version
  ```
  Configure, build, and install the GNU toolchain and qemu
```bash
cd riscv-gnu-toolchain/
./configure --prefix=/opt/riscv
sudo make
sudo apt install qemu-system-misc
./configure --prefix=/opt/riscv --with-arch=rv32gc --with-abi=ilp32d
sudo make linux
 ```
 
 ```bash
sudo apt install gcc-riscv64-unknown-elf
./configure --prefix=/opt/riscv --with-arch=rv32i --with-abi=ilp32
sudo make newlib
sudo add-apt-repository universe
sudo apt-get update


```
TEST:
```bash
which qemu-system-riscv32
qemu-system-riscv32 --version
qemu-system-riscv32 -machine help
```
Testing GCC
To test GCC, run the following commands:
```
sudo apt-get install python3-pyelftools
sudo make -C build-spike install
```
```bash
./configure --prefix=$RISCV --disable-linux --with-arch=rv32ima 
sudo make newlib
sudo make report-newlib SIM=gdb  

./configure --prefix=$RISCV
sudo make linux
sudo make report-linux SIM=qemu 

./configure --prefix=$RISCV --with-sim=spike
sudo make linux
sudo make report               
```
執行RISCV32的指令:
```bash
# 編譯 C 檔案，生成 ELF 可執行檔
# 將 ELF 檔案轉換為二進制檔
# 將二進制檔轉換為十六進制格式
riscv64-unknown-elf-gcc -march=rv32imac_zicsr  -mabi=ilp32 -nostartfiles -nostdlib -o test.elf test.c

riscv64-unknown-elf-objcopy -O binary example.elf example.bin

hexdump -e '"%08x\n"' example.bin > example.hex
```

# 與FPGA進行連接:

查看USB裝置:
```bash
sudo apt install usbutils
lsusb
dmesg | grep usb
```

```bash
sudo apt update
sudo apt install openocd
sudo apt install linux-tools-common 
```
以管理員權限在Power Shell分享USB位置
![image](https://hackmd.io/_uploads/S13QmU61Jx.png)
```bash
usbipd list
usbipd bind --busid 3-4
usbipd attach --wsl --busid 3-4

```
```bash
riscv32-unknown-elf-gcc -r exampleC.c -o exampleC.o
riscv32-unknown-elf-ld -o exampleC.elf -T c_prog.ld -m elf32lriscv -nostdlib --no-relax
```
針對您的設備位置和識別 ID，您可以更新 `udev` 規則文件如下：

1. 使用以下命令來編輯 `udev` 規則文件：
   ```bash
   sudo vi /etc/udev/rules.d/99-openocd.rules
   ```

2. 添加以下內容，並將 `idVendor` 和 `idProduct` 更新為您的設備 ID：
   ```plaintext
   # For FT232H USB-UART/FIFO IC
   SUBSYSTEM=="usb", ATTR{idVendor}=="0403", ATTR{idProduct}=="6014", MODE="664", GROUP="plugdev"
   SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", MODE="664", GROUP="plugdev"
   ```

3. 如果您使用 `vim` 編輯器並希望以 `sudo` 權限編輯文件，可以按以下步驟操作：

1. **打開文件**：
   使用 `sudo` 來確保您有權限編輯系統文件。例如，編輯 `99-openocd.rules` 文件：
   ```bash
   sudo vim /etc/udev/rules.d/99-openocd.rules
   ```

2. **編輯文件**：
   - 進入文件後，按 `i` 鍵進入插入模式進行編輯。
   - 編輯完成後，按 `Esc` 鍵退出插入模式。

3. **保存並退出**：
   - 在命令模式下（按 `Esc` 退出插入模式），輸入 `:wq` 保存並退出文件。
   - 如果編輯過程中發生錯誤，想要放棄更改並退出，可以輸入 `:q!`。

這樣，您就可以使用 `vim` 編輯器以 `sudo` 權限修改文件。

4. 完成後，可以重新插入設備或執行以下命令來加載新規則：
   ```bash
   sudo udevadm control --reload-rules && sudo udevadm trigger
   ```

這樣可以確保您的 FT232H 設備被 `plugdev` 組所管理，便於後續使用。
# OpenOCD:
![image](https://hackmd.io/_uploads/S1qpX8akJe.png)

```bash
sudo apt-get install openocd
openocd --version

```

```bash
 make dasm PROGRAM=demo_gpio NANO_PFLOAT=0
 find / -name riscv64-unknown-elf-gcc 2>/dev/null
make software PROGRAM=demo_gpio     RISCV_PATH=/opt/riscv     RISCV_PREFIX=riscv64-unknown-elf-     CC=riscv64-unknown-elf-gcc     AR=riscv64-unknown-elf-ar     RISCV_ARCH=rv32imac_zicsr     CFLAGS_EXTRA="-nostartfiles -nostdlib"     VERBOSE=1
```

# **riscv-none-embed-gcc 下載:**
To download and set up the **xPack GNU RISC-V Embedded GCC v10.2.0-1.1** on Linux, follow these steps:

### 1. **Download the File**
1. Open your terminal.
2. Use `wget` to download the appropriate asset from the release page:
   ```bash
   wget https://github.com/xpack-dev-tools/riscv-none-embed-gcc-xpack/releases/download/v10.2.0-1.1/xpack-riscv-none-embed-gcc-10.2.0-1.1-linux-x64.tar.gz
   ```

### 2. **Extract the File**
3. Extract the downloaded `.tar.gz` file:
   ```bash
   tar -xzf xpack-riscv-none-embed-gcc-10.2.0-1.1-linux-x64.tar.gz
   ```

4. Navigate to the extracted folder:
   ```bash
   cd xpack-riscv-none-embed-gcc-10.2.0-1.1
   ```

### 3. **Add to PATH**
5. Add the toolchain's `bin` directory to your `PATH` environment variable for easy access:
   ```bash
   echo 'export PATH=$PWD/bin:$PATH' >> ~/.bashrc
   source ~/.bashrc
   ```

### 4. **Verify Installation**
6. Confirm that the toolchain is installed and accessible:
   ```bash
   riscv-none-embed-gcc --version
   ```
   You should see the version output, confirming that the toolchain is ready to use.

### Notes:
- If you encounter any issues, ensure that you have the necessary permissions and that the `tar` utility is installed on your system.
- Use `sudo` if extracting or modifying files in restricted directories.
