# RISC-V Instruction Sets

## Summary


The design of RISC-V instruction sets is modular. Rather than take the approach of a large and complex monolith, a modular design enables flexible implementations that suit specific applications. 

RISC-V defines base user-level integer instruction sets. Additional capability to these are specified as optional extensions, thus giving implementations flexibility to pick and choose what they want for their applications. The specifications of the base ISA has been frozen since 2014. Some of the extensions are also frozen while many others are being defined.

## Discussion

### Could you give an overview of RISC-V instruction set?

RISC-V comprises of a base user-level 32-bit integer instruction set. Called **RV32I**, it includes 47 instructions, which can be grouped into six types: 

    + R-type: register-register
    + I-type: short immediates and loads
    + S-type: stores
    + B-type: conditional branches, a variation of S-type
    + U-type: long immediates
    + J-type: unconditional jumps, a variation of U-typeRV32I has `x0` register hardwired to constant 0, plus `x1-x31` general purpose registers. All registers are 32 bits wide but in RV64I they become 64 bits wide. RV32I is a **load-store architecture**. This means that only load and store instructions access memory; arithmetic operations use only the registers. User space is 32-bit byte addressable and little endian. 

Correspondingly, **RV64I** is for 64-bit address space and **RV128I** is for 128-bit address space. The need for RV128I is debatable and its specification is evolving. We also have **RV32E** for embedded systems. RV32E has only 16 32-bit registers and makes the counters of RV32I optional. 


### What RISC-V extensions have been defined?

RISC-V defines a number of extensions, all of which are optional. Some of them are frozen and these are noted below:

    + M: Integer multiplication and division.
    + A: Atomic.
    + F: Single-precision floating point compliant with IEEE 754-2008.
    + D: Double-precision floating point compliant with IEEE 754-2008.
    + Q: Quad-precision floating point compliant with IEEE 754-2008.
    + C: Compressed instructions (16-bit instructions) to yield about 25-30% reduced code size. "RVC" refers to compressed instruction set.Among the evolving or future extensions are L (decimal float), B (bit manipulation), J (dynamically translated languages), T (transactional memory), P (packed SIMD), V (vector operations), N (user-level interrupts), and H (hypervisor support). 

When multiple extensions are supported, that ISA variant can be described by concatenating the letters; such as, RV64IMAFD. To represent the standard general purpose ISA, "G" is defined as a short form for "IMAFD". 

RV32I uses one-eighth of the encoding space. This means there's plenty of room for custom extensions. 


### What are pseudo-instructions?

To ease the job of an assembly language programmer or a compiler writer, some base instructions can be represented by what are called **pseudo-instructions**. For example, a no operation is `addi x0, x0, 0` for which `nop` is the pseudo-instruction. Likewise, branch if zero is `beq rs, x0, offset` for which `beqz rs, offset` is the pseudo-instruction. 


### What are privileged instructions?

Application code usually runs in user mode or **U-mode**. RV32I and RV32G are user mode ISAs. Two more modes are available:

    + **Machine mode (M-mode)**: For running trusted code. This is the most privileged mode in RISC-V and has complete access to memory, I/O and anything else to boot and configure the system. It's most important feature is to handle synchronous exceptions and interrupts. The simplest RISC-V microcontrollers need to support only M-mode.
    + **Supervisor mode (S-mode)**: For supporting operating system needs of say Linux, FreeBSD or Windows. This is more privileged than U-mode but less privileged than M-mode. Where OS needs to process exceptions/interrupts, *exception delegation* is used to pass control to S-mode selectively. S-mode also provides a virtual memory system.

### Could you describe some technical considerations in the design of RISC-V instructions?

Design of RISC-V ISA considered cost, simplicity, performance, implementation-independence, room for growth, program size, and ease of use. 

RV32I includes generously 32 integer registers, making it easier for compilers to use them more often than memory. By keeping instructions simple, RISC-V instructions typically require only one clock cycle and deliver predictable performance. For dynamic linking, it adopts PC-relative branches. 

Instructions offer three register operands, avoiding the extra move required by ISAs with only two register operands. These are also in the same positions so that access can begin before decoding the instruction. 

The design was also informed by mistakes of other ISAs. For example, initial Alpha ISA did not have byte or half-word load/store. The shift operation in ARM can be seen as an overdesign. Delayed branches of MIPS and SPARC affected their ISAs. ARM Thumb and MIP16 added 16-bit instructions in hindsight. 


### Are RISC-V instructions without precedents?

When we consider the 122 instructions of RV32G, only 6 of them are without precedents. 98 instructions appear in at least three prior ISAs. 18 instructions appear in one or two prior ISAs. This study included 18 prior RISC ISAs, including the CDC 6600 dating back to 1964. 

It's been commented that one cannot design a flawless ISA, nor an ISA with flaws doomed to fail. 

## Milestones

1981

RISC-I is defined by David A. Patterson at UC Berkeley as an alternative to increasingly complex instructions of the computers of the day. This is followed by RISC-II (1983), SOAR (1985) and SPUR (1986) projects. 

2010

Researchers at the UC Berkeley conceive RISC-V as the fifth generation of their RISC design from the 1980s. 

May  
2011

Version 1.0 of the RISC-V base user-level ISA is published as volume 1. This version is not frozen. Volume 2 is for supervisor-level ISA. 

May  
2014

Version 2.0 of the user-level ISA is published. This is a frozen version. 

May  
2017

Version 2.2 of the user-level ISA is published. This does not modify the ISA base plus extensions IMAFDQ version 2.0. Extension C is frozen in this version.