Skip to content

Commit

Permalink
Merge pull request #3 from Alignof/develop
Browse files Browse the repository at this point in the history
Ver 0.1.1
  • Loading branch information
Alignof committed Oct 13, 2023
2 parents 70a9e87 + a628a22 commit 0f1866a
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 108 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Release

on:
push:
branches:
- master
paths:
- Cargo.toml
tags:
- "v*.*.*"

jobs:
tag:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: salsify/action-detect-and-tag-new-version@v2
id: detect_tag
with:
create-tag: true
version-command: cargo read-manifest | jq -r .version
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: softprops/action-gh-release@v1
- run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "raki"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Norimasa Takana <alignof@outlook.com>"]
description = "RISC-V instruction decoder written in Rust."
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
14 changes: 8 additions & 6 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn only_rv64(opcode: OpcodeKind, isa: Isa) -> Result<OpcodeKind, DecodingError>
/// // try to decode illegal instruction.
/// let illegal_inst: u32 = 0b0000_0000_0000_0000_0000_0000_0000_0000;
/// if let Err(error) = illegal_inst.decode(Isa::Rv64) {
/// assert!(matches!(error, DecodingError::IllegalOpcode));
/// assert!(matches!(error, DecodingError::InvalidOpcode));
/// }
///
/// // try to decode rv64 instruction on rv32 environment.
Expand All @@ -41,15 +41,17 @@ pub enum DecodingError {
/// Compressed instructions are expected, but it is 32bit length.
Not32BitInst,
/// It has unexpected Funct3 value.
IllegalFunct3,
InvalidFunct3,
/// It has unexpected Funct5 value.
IllegalFunct5,
InvalidFunct5,
/// It has unexpected Funct6 value.
IllegalFunct6,
InvalidFunct6,
/// It has unexpected Funct7 value.
IllegalFunct7,
InvalidFunct7,
/// Has an opcode that cannot be decoded.
IllegalOpcode,
InvalidOpcode,
/// Illegal instruction (e.g. all zero value instruction)
IllegalInstruction,
/// This instruction is only for Rv64 but appeared at Rv32.
OnlyRv64Inst,
}
Expand Down
4 changes: 4 additions & 0 deletions src/decode/inst_16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use crate::Isa;

impl Decode for u16 {
fn decode(&self, isa: Isa) -> Result<Instruction, DecodingError> {
if *self == 0 {
return Err(DecodingError::IllegalInstruction);
}

let new_opc = self.parse_opcode(isa)?;
let new_rd: Option<usize> = self.parse_rd(&new_opc)?;
let new_rs1: Option<usize> = self.parse_rs1(&new_opc)?;
Expand Down
18 changes: 9 additions & 9 deletions src/decode/inst_16/c_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn quadrant0(_inst: u16, opmap: u8, isa: Isa) -> Result<OpcodeKind, DecodingErro
0b011 => only_rv64(OpcodeKind::C_LD, isa),
0b110 => Ok(OpcodeKind::C_SW),
0b111 => only_rv64(OpcodeKind::C_SD, isa),
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand Down Expand Up @@ -43,21 +43,21 @@ fn quadrant1(inst: u16, opmap: u8, isa: Isa) -> Result<OpcodeKind, DecodingError
0b01 => Ok(OpcodeKind::C_XOR),
0b10 => Ok(OpcodeKind::C_OR),
0b11 => Ok(OpcodeKind::C_AND),
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
},
0b1 => match lo_flag {
0b00 => only_rv64(OpcodeKind::C_SUBW, isa),
0b01 => only_rv64(OpcodeKind::C_ADDW, isa),
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
},
_ => unreachable!(),
},
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
},
0b101 => Ok(OpcodeKind::C_J),
0b110 => Ok(OpcodeKind::C_BEQZ),
0b111 => Ok(OpcodeKind::C_BNEZ),
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand All @@ -82,11 +82,11 @@ fn quadrant2(inst: u16, opmap: u8, isa: Isa) -> Result<OpcodeKind, DecodingError
_ => Ok(OpcodeKind::C_ADD),
},
},
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
},
0b110 => Ok(OpcodeKind::C_SWSP),
0b111 => only_rv64(OpcodeKind::C_SDSP, isa),
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand All @@ -95,14 +95,14 @@ pub fn parse_opcode(inst: u16, isa: Isa) -> Result<OpcodeKind, DecodingError> {
let quadrant: u8 = inst.slice(1, 0) as u8;

if inst == 0b0000_0000_0000_0000 {
return Err(DecodingError::IllegalOpcode);
return Err(DecodingError::InvalidOpcode);
}

match quadrant {
0b00 => quadrant0(inst, opmap, isa),
0b01 => quadrant1(inst, opmap, isa),
0b10 => quadrant2(inst, opmap, isa),
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/decode/inst_32/a_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
0b10100 => Ok(OpcodeKind::AMOMAX_W),
0b11000 => Ok(OpcodeKind::AMOMINU_W),
0b11100 => Ok(OpcodeKind::AMOMAXU_W),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
0b011 => match funct7 {
0b00010 => only_rv64(OpcodeKind::LR_D, isa),
Expand All @@ -35,11 +35,11 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
0b10100 => only_rv64(OpcodeKind::AMOMAX_D, isa),
0b11000 => only_rv64(OpcodeKind::AMOMINU_D, isa),
0b11100 => only_rv64(OpcodeKind::AMOMAXU_D, isa),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand Down
40 changes: 20 additions & 20 deletions src/decode/inst_32/base_i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
0b101 => Ok(OpcodeKind::BGE),
0b110 => Ok(OpcodeKind::BLTU),
0b111 => Ok(OpcodeKind::BGEU),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b000_0011 => match funct3 {
0b000 => Ok(OpcodeKind::LB),
Expand All @@ -31,25 +31,25 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
0b100 => Ok(OpcodeKind::LBU),
0b101 => Ok(OpcodeKind::LHU),
0b110 => only_rv64(OpcodeKind::LWU, isa),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b010_0011 => match funct3 {
0b000 => Ok(OpcodeKind::SB),
0b001 => Ok(OpcodeKind::SH),
0b010 => Ok(OpcodeKind::SW),
0b011 => only_rv64(OpcodeKind::SD, isa),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b001_0011 => match funct3 {
0b000 => Ok(OpcodeKind::ADDI),
0b001 => match isa {
Isa::Rv32 => match funct7 {
0b000_0000 => Ok(OpcodeKind::SLLI),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
Isa::Rv64 => match funct6 {
0b00_0000 => Ok(OpcodeKind::SLLI),
_ => Err(DecodingError::IllegalFunct6),
_ => Err(DecodingError::InvalidFunct6),
},
},
0b010 => Ok(OpcodeKind::SLTI),
Expand All @@ -59,23 +59,23 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
Isa::Rv32 => match funct7 {
0b000_0000 => Ok(OpcodeKind::SRLI),
0b010_0000 => Ok(OpcodeKind::SRAI),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
Isa::Rv64 => match funct6 {
0b00_0000 => Ok(OpcodeKind::SRLI),
0b01_0000 => Ok(OpcodeKind::SRAI),
_ => Err(DecodingError::IllegalFunct6),
_ => Err(DecodingError::InvalidFunct6),
},
},
0b110 => Ok(OpcodeKind::ORI),
0b111 => Ok(OpcodeKind::ANDI),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b011_0011 => match funct3 {
0b000 => match funct7 {
0b000_0000 => Ok(OpcodeKind::ADD),
0b010_0000 => Ok(OpcodeKind::SUB),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
0b001 => Ok(OpcodeKind::SLL),
0b010 => Ok(OpcodeKind::SLT),
Expand All @@ -84,49 +84,49 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
0b101 => match funct7 {
0b000_0000 => Ok(OpcodeKind::SRL),
0b010_0000 => Ok(OpcodeKind::SRA),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
0b110 => Ok(OpcodeKind::OR),
0b111 => Ok(OpcodeKind::AND),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b000_1111 => Ok(OpcodeKind::FENCE),
0b111_0011 => match funct3 {
0b000 => match funct7 {
0b000_0000 => match funct5 {
0b00000 => Ok(OpcodeKind::ECALL),
0b00001 => Ok(OpcodeKind::EBREAK),
_ => Err(DecodingError::IllegalFunct5),
_ => Err(DecodingError::InvalidFunct5),
},
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b001_1011 => match funct3 {
0b000 => only_rv64(OpcodeKind::ADDIW, isa),
0b001 => only_rv64(OpcodeKind::SLLIW, isa),
0b101 => match funct7 {
0b000_0000 => only_rv64(OpcodeKind::SRLIW, isa),
0b010_0000 => only_rv64(OpcodeKind::SRAIW, isa),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b011_1011 => match funct3 {
0b000 => match funct7 {
0b000_0000 => only_rv64(OpcodeKind::ADDW, isa),
0b010_0000 => only_rv64(OpcodeKind::SUBW, isa),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
0b001 => only_rv64(OpcodeKind::SLLW, isa),
0b101 => match funct7 {
0b000_0000 => only_rv64(OpcodeKind::SRLW, isa),
0b010_0000 => only_rv64(OpcodeKind::SRAW, isa),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/decode/inst_32/m_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ pub fn parse_opcode(inst: u32, isa: Isa) -> Result<OpcodeKind, DecodingError> {
0b101 => Ok(OpcodeKind::DIVU),
0b110 => Ok(OpcodeKind::REM),
0b111 => Ok(OpcodeKind::REMU),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
0b011_1011 => match funct3 {
0b000 => only_rv64(OpcodeKind::MULW, isa),
0b100 => only_rv64(OpcodeKind::DIVW, isa),
0b101 => only_rv64(OpcodeKind::DIVUW, isa),
0b110 => only_rv64(OpcodeKind::REMW, isa),
0b111 => only_rv64(OpcodeKind::REMUW, isa),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/decode/inst_32/priv_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn parse_opcode(inst: u32) -> Result<OpcodeKind, DecodingError> {
0b0001_0000_0101_0000_0000_0000_0111_0011 => Ok(OpcodeKind::WFI),
_ => match funct7 {
0b000_1001 => Ok(OpcodeKind::SFENCE_VMA),
_ => Err(DecodingError::IllegalFunct7),
_ => Err(DecodingError::InvalidFunct7),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/decode/inst_32/zicsr_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub fn parse_opcode(inst: u32) -> Result<OpcodeKind, DecodingError> {
0b101 => Ok(OpcodeKind::CSRRWI),
0b110 => Ok(OpcodeKind::CSRRSI),
0b111 => Ok(OpcodeKind::CSRRCI),
_ => Err(DecodingError::IllegalFunct3),
_ => Err(DecodingError::InvalidFunct3),
},
_ => Err(DecodingError::IllegalOpcode),
_ => Err(DecodingError::InvalidOpcode),
}
}

Expand Down
Loading

0 comments on commit 0f1866a

Please sign in to comment.