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:

  • I Base Instruction Set
  • M Multiply and Divide
  • F Single precision floating point
  • B (Zba, Zbb, Zbc, Zbs) bit manipulation
  • Zifencei ebreak and ecall
  • Zicsr Control Status 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'

>> i = CSRInstruction('csrrw', 'x1', 'mie', 'x5')
>> i.to_bitstring()
"00110000010000101001000011110011"

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)

oppcode, func3, func7 = INSTRUCTION_MAP['fmadd.s'][1:4]

Project Intent

The project has reached the MVP(Minimum Viable Product) state. It does what I want it to do, and I am reasonably sure that no major bugs exist. I built this to use in my own projects after I found Python Risc-v tools somewhat lacking. I will fix bugs as they crop up. I will add features as I need them, or as they are requested.

This is not an industrial strength library and is targeted at hobbyists/educational use. I've made readability, ease of use and simplicity priorities.

No warranties either stated or implied are associated with this project.

Use the code, or just copy the data that I've compiled from multiple sources, often conflicting with each other on the exact details.

Limitations

  • F extension Rounding Modes hardcoded to 'Nearest'(000)

Future

  • 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.7.2.tar.gz (14.5 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.7.2-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for riscvlib-0.7.2.tar.gz
Algorithm Hash digest
SHA256 a6985cdf1305ab72ce47f350f7387c57802d114b4e2870c7f83feccdd2f464f4
MD5 f1bfb7afb1c559e984ad8befc3648fec
BLAKE2b-256 42b4adb242b71f2d55575e9a8680b92624cf6495ad0f5c66c2e30c7420341fb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for riscvlib-0.7.2.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.7.2-py3-none-any.whl.

File metadata

  • Download URL: riscvlib-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 14.4 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.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6d411860a1c7e1d61d9d96f5985e8582d1c7e50c4db64bc4bd65c8b0de378a62
MD5 71687416c3dfb78dc22d53b72499439b
BLAKE2b-256 5cf2f6ff4f10062cdbd8f2d7392c45142ba18c26ff4d680defd2e09edc2e708f

See more details on using hashes here.

Provenance

The following attestation bundles were made for riscvlib-0.7.2-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