Skip to content

Instantly share code, notes, and snippets.

@mohamed
Last active January 26, 2024 20:48
Show Gist options
  • Save mohamed/a6e406e086e6c9cc0ded222d23bcb0a6 to your computer and use it in GitHub Desktop.
Save mohamed/a6e406e086e6c9cc0ded222d23bcb0a6 to your computer and use it in GitHub Desktop.
Setting up RISC-V toolchain and simulator

Setting up RISC-V toolchain and simulator

This guide explains the steps needed to build the compiler and simulator toolchain for RISC-V ISA. It assumes RV32GC instruction extension.

Tool links:

Building Steps

  1. We will install the tools that we build manually into a folder called riscv-tools. We will point to this folder from now on with the environmnent variable $RISCV. The repos will be cloned into another directory called repos

    $ setenv RISCV=/path/to/install/riscv/tools
    $ mkdir repos; cd repos
    
  2. Install the compiler prerequisites:

    # For OpenSUSE
    $ sudo zypper install autoconf automake python3 libmpc3 mpfr-devel gmp-devel gawk  bison flex texinfo patchutils gcc gcc-c++ zlib-devel libexpat-devel ninja
    # For Ubuntu
    $ sudo apt-get install autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev
    
  3. Clone and build the GCC compiler:

    $ git clone https://github.com/riscv/riscv-gnu-toolchain --recursive
    $ cd riscv-gnu-toolchain
    $ mkdir build
    $ cd build
    $ ../configure --prefix=$RISCV --with-arch=rv32gc --enable-multilib
    $ make -j 4
    $ cd ..
    
  4. Clone and build the LLVM compiler:

    $ git clone https://github.com/llvm/llvm-project.git riscv-llvm
    $ cd riscv-llvm
    $ mkdir build
    $ cd build
    $ cmake -G Ninja -DCMAKE_BUILD_TYPE="Release" \
       -DCMAKE_INSTALL_PREFIX="$RISCV" \
       -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" \
       -DLLVM_ENABLE_RUNTIMES="libc;libcxx;libcxxabi" \
       -DLLVM_USE_SPLIT_DWARF=True \
       -DLLVM_BUILD_TESTS=False \
       -DLLVM_DEFAULT_TARGET_TRIPLE="riscv32-unknown-elf" \
       -DLLVM_TARGETS_TO_BUILD="RISCV" \
       -DLLVM_INSTALL_UTILS=ON \
       ../llvm
    $ cmake --build . --target all
    $ cmake --build . --target install
    $ cd ..
    
  5. Clone and build riscv-pk:

    $ git clone https://github.com/riscv-software-src/riscv-pk
    $ cd riscv-pk
    $ mkdir build
    $ cd build
    $ export PATH=$RISCV/bin:$PATH
    $ ../configure --prefix=$RISCV --host=riscv32-unknown-elf --with-arch=rv32gc
    $ make
    $ make install
    $ cd ..
    
  6. Clone and build the simulator:

    $ git clone https://github.com/riscv-software-src/riscv-isa-sim
    $ cd riscv-isa-sim
    $ mkdir build
    $ cd build
    $ ../configure --prefix=$RISCV
    $ make
    $ make install
    $ cd ..
    

Testing the simulator

   $ cat hello.c
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char * argv[]) {
      printf("Hello World from RISC-V!\n");
      return EXIT_SUCCESS;
    }
   $ riscv32-unknown-elf-gcc hello.c
   $ $RISCV/bin/spike --isa=RV32GC_zicntr $RISCV/riscv32-unknown-elf/bin/pk -s a.out
    bbl loader
    Hello World from RISC-V!
    1200 ticks
    56746 cycles
    56748 instructions
    0.99 CPI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment