Skip to content

theonekeyg/librvdec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

librvdec - Library for decoding RISC-V ISA.

Here is the manual that was used for implementing the ISA standard. The current implementation does not recognize and emit assembler pseudo-instructions.

Currently only RV32I, RV64I, RV32M and RV64M instruction sets are implemented.

Installation

The library provides a way of selecting which instruction sets should be supported for decoding. Simply comment unwanted instruction sets in config.h, or just use all instruction sets, which is the default.

Clone the repo.

$ git clone https://github.com/theonekeyg/librvdec.git

Invoke cmake.

$ mkdir build && cd build && cmake ..

Compile and install the library.

$ make && make install

Tests are not built by default. To compile and run testsuite:

$ cd build/test && make && make test

Usage

To decode an instruction, simply use

int riscv_decode(struct riscv_insn *, uint32_t)

It will check if the passed instruction belongs to one of the supported instruction sets. If it does, it parses the instruction, sets the appropriate riscv_insn fields and returns it's kind, otherwise it just returns RVINSN_ILLEGAL.

Example usage:

#include <assert.h>
#include <rvdec/decode.h>
#include <rvdec/instruction.h>
#include <rvdec/register.h>

int main() {
  struct riscv_insn ins;

  if (riscv_decode(&ins, /* addi a5,s0,-200 */ 0xf3840793) != RVINSN_ILLEGAL) {
    assert(ins.kind == RVINSN_ADDI);
    assert(ins.i.imm == -200);
    assert(ins.i.rd == RVREG_a5);
    assert(ins.i.rs1 == RVREG_s0);
  } else {
    printf("ILLEGAL INSTRUCTION\n");
    return 1;
  }

  return 0;
}

Forking

The library was designed with a goal to make adding/modifying instruction sets (and instruction types) as easy as possible. Which also makes it easy to fork the library to add specific RISC-V extensions, or reuse the design for other ISAs.

I can write a more complete guideline for such integrations, so if you're interested in forking the library for your needs, feel free to open the issue on github, so I'll know there is demand for it.

About

Library for decoding RISC-V ISA

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published