Python Bindings of Spike RISC-V ISA Simulator
Project description
Python Bindings of Spike RISC-V ISA Simulator
LIU Yu <liuy@huimtlab.org>
2025/10/16 (v0.0.5)
Introduction
This project provides Python bindings for the Spike RISC-V ISA Simulator. The Pythonic Spike (or PySpike) opens up Spike's C++ internals (such as RISC-V disassembler, processors, controllers, peripherals, etc.) for interoperation with Python scripts. It enables users to write ISA / RoCC extensions and MMIO device models in Python, and plug them into vanilla Spike for (co-)simulating complex hardware systems. Through integrating Spike more seamlessly into the Python ecosystem, PySpike aims to boost the agility of Python-based hardware verification tools and workflows.
PyPI package: spike
Getting Started
PySpike requires: Python 3.8+.
Install the wheel package with pip.
$ pip install --pre spike
PySpike ships the original command-line tool spike, a.k.a vanilla Spike, within its wheel package. You can confirm its availability using,
$ spike --help
Spike RISC-V ISA Simulator 1.1.1-dev
...
There is also a 100%-compatible command-line wrapper called pyspike, with additional support for Python-based ISA / MMIO / RoCC extensions via --extlib=<name>.
$ pyspike \
--isa=rv32imc_xmyisa --priv=m \
--pc=0x90000000 \
-m0x90000000:0x4000000 \
--extlib=myisa.py \
--extlib=mydev.py \
--device=mydev,0x20000000 \
tests/data/libc-printf_hello.elf
Hello, World!
Quick ISA Extension
An ISA extension implements one or more custom instructions and / or control-state registers (CSRs) for Spike's RISC-V processor models. With PySpike, an ISA extension is a Python class that inherits riscv.isa.ISA. It should implement a minimum of two methods: get_instructions and get_disasms. The former provides functional models of one or more custom instructions, and the latter provides their disassemblers. Other optional methods include get_csrs and reset, for providing custom CSRs and resetting extension states, respectively. Use decorator @isa.register("myisa") to register the extension under the name myisa.
from typing import List
from riscv import isa
from riscv.csrs import csr_t
from riscv.disasm import disasm_insn_t
from riscv.processor import insn_desc_t, processor_t
@isa.register("myisa")
class MyISA(isa.ISA):
def __init__(self): ...
def get_instructions(self, proc: processor_t) -> List[insn_desc_t]: ...
def get_disasms(self, proc: processor_t) -> List[disasm_insn_t]: ...
def get_csrs(self, proc: processor_t) -> List[csr_t]: ...
def reset(self, proc: processor_t) -> None: ...
Quick Device Model
Likewise to the ISA extension, a device model implements a custom memory-mapped input/output (MMIO) peripheral for Spike's simulated system bus. With PySpike, a device model is a Python class that inherits riscv.dev.MMIO. It should implement a minimum of three methods: __init__, load, and store. The former initializes the model, the latter two handle memory read and write operations. Other optional methods include size and tick, for obtaining the size of memory-mapped address space, and shifting device states, respectively. Use decorator @dev.register("mydev") to register the model under the name mydev.
from typing import Optional
from riscv import dev
from riscv.sim import sim_t
@dev.register("mydev")
class MyDEV(dev.MMIO):
def __init__(self, sim: sim_t, args: Optional[str] = None): ...
def load(self, addr: int, size: int) -> bytes: ...
def store(self, addr: int, data: bytes) -> None: ...
def size(self) -> int: ...
def tick(self, rtc_ticks: int) -> None:
Development
Getting Source Code
$ git clone --recurse-submodules https://github.com/huimtlab/pyspike
$ cd pyspike
Setting Up Develop Environment
Install with pip in editable mode. This will setup development dependencies as well.
$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install -e '.[dev]'
Running Tests
Run the built-in test suite with pytest.
(.venv) $ python -m pytest -v
Running Tests with Coverage
To enable coverage report, rebuild the extension module with --cov option, then re-run the test suite also with --cov option. You should see the coverage report by the end of the test output.
Note, if leaving out the first step, the C++ code will not show up in the coverage report.
(.venv) $ python setup.py build_ext --inplace --cov
(.venv) $ python -m pytest -v --cov
Optionally, you can generate HTML coverage report from the lcov data files.
(.venv) $ genhtml -o coverage --substitute "s#^#$PWD/#g" *.lcov
Packaging
(.venv) $ python -m build
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spike-0.0.5.dev18-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: spike-0.0.5.dev18-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 206.5 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c439ef7bc9890776204d697ca54a21d4c8daaae8278dfb7273abcded9eda0cb4
|
|
| MD5 |
5f7e5562c4b67a6805def4853404cc0c
|
|
| BLAKE2b-256 |
42de1d6d3d24b1963d6f64f0d03ffbe0da07986e5fe359142a0b510097d668dd
|
File details
Details for the file spike-0.0.5.dev18-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: spike-0.0.5.dev18-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 206.5 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f53c71927a5ce355224b80cd0b0f54e11efbbf2f98539d6e3b74c7518f44640d
|
|
| MD5 |
67568e8e1145228ceb632f26060f6ef0
|
|
| BLAKE2b-256 |
31854bb68e2428a2700f88086a5e90040513ee78fb39276103dbefe8a941d221
|
File details
Details for the file spike-0.0.5.dev18-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: spike-0.0.5.dev18-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 206.5 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbfd5c36d3520496be79d7c31d1cc7d236d977d55487f77de11d8bad2c8d0865
|
|
| MD5 |
d0d3dc68479f35c2daa233110b525f37
|
|
| BLAKE2b-256 |
9e9cfe463530833df647e3a41fb1a4ab54fd97255f434e1128919c66e70707a0
|
File details
Details for the file spike-0.0.5.dev18-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: spike-0.0.5.dev18-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 206.5 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4165a299a02290bc5a473cc660e5f420028434b6bf2b54362cd32ea8cf83a75
|
|
| MD5 |
00c00d3b2cf4cdd56492c89f5e434c27
|
|
| BLAKE2b-256 |
787a9bc7a506c675fc9fa902cd8c80569ce6ac860bc49f5a7612c63e7329b66a
|
File details
Details for the file spike-0.0.5.dev18-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: spike-0.0.5.dev18-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 206.5 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c15aee287e12a9eb5e1efed701bd2e5ee8c96f81132e47038a0fd0ef341958b
|
|
| MD5 |
1bafc3ca71b28840a2238a775ae261eb
|
|
| BLAKE2b-256 |
508a67fecf3302f0fea393478bce55e420b0a89e94c8087c3abf3b8db09d594a
|