Skip to main content

Resources for working with RISC-V in Python

Project description

riscvlib

Resources for working with RISC-V assembly in Python. Supporting instruction creation, pseudo instruction translation and register lookup for extenions:

  • M Multiply and Divide
  • F Single precision foating point (see limitations)
  • B (Zba, Zbb, Zbc, Zbs) bit manipulation
  • Zifencei ebreak and ecall
  • Zicsr Control register instructions

Installation

The package can be installed using pip:

 $ pip install riscvlib  

Examples

Instruction, Static creation from a string

>> from riscvlib.instruction import Instruction  

>> i = Instruction.from_line("add x1, x2, x3")  
>> print(i) 
add x1, x2, x3  
>> i.to_bytes() 
b'\xb3\x001\x00'  
>> i  
RInstruction 'add' ['x1', 'x2', 'x3']

Specific Instruction Type

>> from riscvlib.instruction import RInstruction, IInstruction

>> r_instr = RInstruction("add", "x1", "x2", "a2")
>> print(r_instr)
add x1, x2, a2

>> i_instr = IInstruction("andi", "a0", "a1", -13)
>> print(i_instr)
andi a0, a1, -13
>> i_instr.to_bytes()  # note: bytes are 'little endian' ordered
b'\x13\xf55\xff'

Pseudo Instruction Translation

>> from riscvlib.instruction import translate_pseudo_instruction

>> out_list = translate_pseudo_instruction("mv", "a0", "x30")
>> print(out_list)
['addi a0, x30, 0']

>> out_list = translate_pseudo_instruction("la", "a0", "400")
>> print(out_list)
['lui a0, %hi(400)', 'addi a0, a0, %lo(400)']

Instruction Data

>> from riscvlib.risvdata import INSTRUCTION_MAP

>> INSTRUCTION_MAP['mul']
('MUL', '0110011', '000', '0000001', 'R', 'm', '32/64')
# (name, opcode, func3, func7, itype, extension, rv32/rv64)

Limitations

  • Currently supports RV32IMFB (RISC-V 32 bit I,M,F and B(Zba,Zbb,Zbc,Zbs) extensions)
  • F extension does not support "R4" type instructions i.e fmadd.s
  • F extension Rounding Modes hardcoded to 'Nearest'(000)

Future

  • Additional F extension support
  • A, C and D extensions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

riscvlib-0.6.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

riscvlib-0.6.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file riscvlib-0.6.0.tar.gz.

File metadata

  • Download URL: riscvlib-0.6.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for riscvlib-0.6.0.tar.gz
Algorithm Hash digest
SHA256 5180639fee0de839d361bfe9f3e9e7001fc1e813de1da5c29706dcf1a9d59971
MD5 15a3298b4960aecc38820e04c7d7defa
BLAKE2b-256 b3595606d1fd72d2291b7de31feeeb938aaec311fa72f88e2eed6098aaa34d76

See more details on using hashes here.

Provenance

The following attestation bundles were made for riscvlib-0.6.0.tar.gz:

Publisher: python-publish.yml on Mike451/riscvlib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file riscvlib-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: riscvlib-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for riscvlib-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2faca95f79e5394c746a56c62bdf5d50c0ba50fa466ecc36458f2a965b8aa768
MD5 2c8dd41c4aab3be2a015f4c3539c6238
BLAKE2b-256 aabb06b9896cf37a98ad30ed84a0ea802997a8a3fd86a4aa2841d33c32d9c763

See more details on using hashes here.

Provenance

The following attestation bundles were made for riscvlib-0.6.0-py3-none-any.whl:

Publisher: python-publish.yml on Mike451/riscvlib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page