Spike RISC-V ISA Simulator

repository·master·Indexed 25 days ago

https://github.com/riscv-software-src/riscv-isa-sim

A functional RISC-V ISA simulator used for testing RISC-V software, verifying new instructions, and debugging code. It supports interactive debug modes, GDB/OpenOCD integration, and the ability to simulate new instructions by defining functional behavior and opcodes.

Tokens
993
Snippets
5
Records
6
Agent score
35%

What's inside Spike

  1. Debug Spike with GDB and OpenOCD

    master

    To use GDB for debugging, you must use OpenOCD to bridge the connection.

    1. Run Spike with Remote Bitbang: Start Spike listening on a specific port (e.g., 9824) and map a memory range:

      $ spike --rbb-port=9824 -m0x10000:0x20000 rot13
    2. Run OpenOCD: Create a spike.cfg file to configure the remote bitbang adapter and target, then run:

      $ openocd -f spike.cfg
    3. Run GDB: Start your RISC-V GDB session and connect to the OpenOCD server:

      $ riscv64-unknown-elf-gdb rot13
      (gdb) target extended-remote localhost:3333
      (gdb) load
    $ spike --rbb-port=9824 -m0x10000:0x20000 rot13
    $ openocd -f spike.cfg
    $ riscv64-unknown-elf-gdb rot13
  2. Compile and run a simple C program with Spike

    master

    To run a C program, you must have spike, riscv-gnu-toolchain, and riscv-pk installed.

    1. Compile your C code into a RISC-V ELF binary: riscv64-unknown-elf-gcc -o hello hello.c
    2. Run the binary using the proxy kernel (pk) via Spike: spike pk hello
    $ riscv64-unknown-elf-gcc -o hello hello.c
    $ spike pk hello
  3. Simulate a new instruction in Spike

    master

    To add a new instruction to the simulator, follow these steps:

    1. Functional Behavior: Describe the instruction in riscv/insns/<new_instruction_name>.h.
    2. Opcode Definition: Add the opcode and opcode mask to riscv/opcodes.h. Alternatively, add it to the riscv-opcodes package:
      $ cd ../riscv-opcodes
      $ vi opcodes       # add a line for the new instruction
      $ make install
    3. Build Inclusion: Add the instruction to riscv/riscv.mk.in to ensure it is included in the build (otherwise it will be treated as an illegal instruction).
    4. Rebuild: Rebuild the simulator.
  4. Install Spike on OpenBSD

    master

    On OpenBSD, install bash, gmake, and dtc, then use clang for compilation.

    $ pkg_add bash gmake dtc
    $ exec bash
    $ export CC=cc; export CXX=c++
    $ mkdir build
    $ cd build
    $ ../configure --prefix=$RISCV
    $ gmake
    $ [doas] make install
    $ pkg_add bash gmake dtc
    $ exec bash
    $ export CC=cc; export CXX=c++
    $ mkdir build
    $ cd build
    $ ../configure --prefix=$RISCV
    $ gmake
    $ [doas] make install
  5. Use Spike Interactive Debug Mode

    master

    Launch Spike in interactive debug mode using the -d flag:

    $ spike -d pk hello

    Debug Commands

    • Registers:
      • View integer register: : reg <core_id> <reg_name> (e.g., : reg 0 a0)
      • View floating point register (single precision): : fregs <core_id> <reg_name> (e.g., : fregs 0 ft0)
      • View floating point register (double precision): : fregd <core_id> <reg_name>
    • Memory:
      • View physical address (hex): : mem <address> (e.g., : mem 2020)
      • View virtual address: : mem <core_id> <address> (e.g., : mem 0 2020)
    • Execution Control:
      • Advance one instruction: Enter key
      • Execute until equality: : until <type> <args> <value>
        • Stop when PC reaches address: : until pc 0 2020
        • Stop when register equals value: : until reg 0 mie a
        • Stop when memory equals value: : until mem 2020 50a9907311096993
      • Execute while equality is true: : while mem 2020 50a9907311096993
      • Continue execution indefinitely: : r
      • Quit/End simulation: <control>-<c> or : q

    Note: You can enter interactive debug mode at any time during execution by pressing <control>-<c>.

    $ spike -d pk hello
  6. Install Spike RISC-V ISA Simulator

    master

    To build Spike, ensure the $RISCV environment variable is set to your RISC-V tools installation path. On Debian-based systems, you will need device-tree-compiler, libboost-regex-dev, and libboost-system-dev.

    $ apt-get install device-tree-compiler libboost-regex-dev libboost-system-dev
    $ mkdir build
    $ cd build
    $ ../configure --prefix=$RISCV
    $ make
    $ [sudo] make install

    If using yum, substitute the first step with yum install dtc.

    $ apt-get install device-tree-compiler libboost-regex-dev libboost-system-dev
    $ mkdir build
    $ cd build
    $ ../configure --prefix=$RISCV
    $ make
    $ [sudo] make install