Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ver 1.1.0 #28

Merged
merged 15 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "raki"
version = "1.0.1"
version = "1.1.0"
edition = "2021"
authors = ["Norimasa Takana <[email protected]>"]
repository = "https://github.com/Alignof/raki"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn main() {
- [x] M
- [x] A
- [ ] D
- [ ] G
- [ ] Q
- [x] C
- [ ] B
Expand All @@ -39,6 +38,8 @@ fn main() {
- [ ] H
- [x] Zicsr
- [x] Zifencei
- [x] Zicntr
- [x] Zicfiss
- [ ] Priv (Now only supports `mret`, `sret`, `wfi`, `sfence.vma`)

## License
Expand Down
13 changes: 12 additions & 1 deletion src/decode/inst_16.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[allow(non_snake_case)]
mod c_extension;
mod zicfiss_extension;

use super::{Decode, DecodeUtil, DecodingError};
use crate::instruction::{InstFormat, Instruction, OpcodeKind};
Expand Down Expand Up @@ -33,34 +34,41 @@ impl Decode for u16 {

match extension {
Ok(Extensions::C) => Ok(OpcodeKind::C(c_extension::parse_opcode(self, isa)?)),
Ok(Extensions::Zicfiss) => Ok(OpcodeKind::Zicfiss(zicfiss_extension::parse_opcode(
self, isa,
)?)),
_ => Err(DecodingError::Not16BitInst),
}
}

fn parse_rd(self, opkind: &OpcodeKind) -> Result<Option<usize>, DecodingError> {
match opkind {
OpcodeKind::C(opc) => Ok(c_extension::parse_rd(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_rd(self, opc)),
_ => Err(DecodingError::Not16BitInst),
}
}

fn parse_rs1(self, opkind: &OpcodeKind) -> Result<Option<usize>, DecodingError> {
match opkind {
OpcodeKind::C(opc) => Ok(c_extension::parse_rs1(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_rs1(self, opc)),
_ => Err(DecodingError::Not16BitInst),
}
}

fn parse_rs2(self, opkind: &OpcodeKind) -> Result<Option<usize>, DecodingError> {
match opkind {
OpcodeKind::C(opc) => Ok(c_extension::parse_rs2(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_rs2(self, opc)),
_ => Err(DecodingError::Not16BitInst),
}
}

fn parse_imm(self, opkind: &OpcodeKind, _isa: Isa) -> Result<Option<i32>, DecodingError> {
match opkind {
OpcodeKind::C(opc) => Ok(c_extension::parse_imm(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_imm(self, opc)),
_ => Err(DecodingError::Not16BitInst),
}
}
Expand All @@ -73,7 +81,10 @@ impl DecodeUtil for u16 {
}

fn parse_extension(self) -> Result<Extensions, DecodingError> {
Ok(Extensions::C)
match self {
0b0110_0000_1000_0001 | 0b0110_0010_1000_0001 => Ok(Extensions::Zicfiss),
_ => Ok(Extensions::C),
}
}

fn set(self, mask: &[u32]) -> u32 {
Expand Down
77 changes: 77 additions & 0 deletions src/decode/inst_16/zicfiss_extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use super::super::DecodingError;
use crate::instruction::zicfiss_extension::ZicfissOpcode;
use crate::Isa;

pub fn parse_opcode(inst: u16, _isa: Isa) -> Result<ZicfissOpcode, DecodingError> {
match inst {
0b0110_0000_1000_0001 => Ok(ZicfissOpcode::C_SSPUSH),
0b0110_0010_1000_0001 => Ok(ZicfissOpcode::C_SSPOPCHK),
_ => Err(DecodingError::InvalidOpcode),
}
}

#[allow(clippy::unnecessary_wraps)]
pub fn parse_rd(_inst: u16, opkind: &ZicfissOpcode) -> Option<usize> {
match opkind {
ZicfissOpcode::C_SSPUSH => Some(1),
ZicfissOpcode::C_SSPOPCHK => Some(5),
_ => unreachable!(),
}
}

#[allow(clippy::unnecessary_wraps)]
pub fn parse_rs1(_inst: u16, _opkind: &ZicfissOpcode) -> Option<usize> {
None
}

#[allow(clippy::unnecessary_wraps)]
pub fn parse_rs2(_inst: u16, _opkind: &ZicfissOpcode) -> Option<usize> {
None
}

#[allow(clippy::cast_possible_wrap, clippy::unnecessary_wraps)]
pub fn parse_imm(_inst: u16, _opkind: &ZicfissOpcode) -> Option<i32> {
None
}

#[cfg(test)]
#[allow(unused_variables)]
mod test_zicfiss {
#[test]
#[allow(overflowing_literals)]
fn zicfiss_16() {
use super::*;
use crate::{Decode, Isa, OpcodeKind};
let test_16 = |inst_16: u16,
op: OpcodeKind,
rd: Option<usize>,
rs1: Option<usize>,
rs2: Option<usize>,
imm: Option<i32>| {
let op_16 = inst_16.parse_opcode(Isa::Rv64).unwrap();
assert!(matches!(&op_16, op));
assert_eq!(inst_16.parse_rd(&op_16).unwrap(), rd);
assert_eq!(inst_16.parse_rs1(&op_16).unwrap(), rs1);
assert_eq!(inst_16.parse_rs2(&op_16).unwrap(), rs2);
assert_eq!(inst_16.parse_imm(&op_16, Isa::Rv64).unwrap(), imm);
};

test_16(
0x6081,
OpcodeKind::Zicfiss(ZicfissOpcode::C_SSPUSH),
Some(1),
None,
None,
None,
);

test_16(
0x6281,
OpcodeKind::Zicfiss(ZicfissOpcode::C_SSPOPCHK),
Some(5),
None,
None,
None,
);
}
}
17 changes: 16 additions & 1 deletion src/decode/inst_32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod a_extension;
mod base_i;
mod m_extension;
mod priv_extension;
mod zicfiss_extension;
mod zicntr_extension;
mod zicsr_extension;
mod zifencei_extension;
Expand Down Expand Up @@ -41,6 +42,9 @@ impl Decode for u32 {
self,
)?)),
Ok(Extensions::Zicsr) => Ok(OpcodeKind::Zicsr(zicsr_extension::parse_opcode(self)?)),
Ok(Extensions::Zicfiss) => {
Ok(OpcodeKind::Zicfiss(zicfiss_extension::parse_opcode(self)?))
}
Ok(Extensions::Zicntr) => Ok(OpcodeKind::Zicntr(zicntr_extension::parse_opcode(self)?)),
Ok(Extensions::Priv) => Ok(OpcodeKind::Priv(priv_extension::parse_opcode(self)?)),
Ok(Extensions::C) => Err(DecodingError::Not32BitInst),
Expand All @@ -55,6 +59,7 @@ impl Decode for u32 {
OpcodeKind::A(opc) => Ok(a_extension::parse_rd(self, opc)),
OpcodeKind::Zifencei(opc) => Ok(zifencei_extension::parse_rd(self, opc)),
OpcodeKind::Zicsr(opc) => Ok(zicsr_extension::parse_rd(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_rd(self, opc)),
OpcodeKind::Zicntr(opc) => Ok(zicntr_extension::parse_rd(self, opc)),
OpcodeKind::Priv(opc) => Ok(priv_extension::parse_rd(self, opc)),
OpcodeKind::C(_) => Err(DecodingError::Not32BitInst),
Expand All @@ -68,6 +73,7 @@ impl Decode for u32 {
OpcodeKind::A(opc) => Ok(a_extension::parse_rs1(self, opc)),
OpcodeKind::Zifencei(opc) => Ok(zifencei_extension::parse_rs1(self, opc)),
OpcodeKind::Zicsr(opc) => Ok(zicsr_extension::parse_rs1(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_rs1(self, opc)),
OpcodeKind::Zicntr(opc) => Ok(zicntr_extension::parse_rs1(self, opc)),
OpcodeKind::Priv(opc) => Ok(priv_extension::parse_rs1(self, opc)),
OpcodeKind::C(_) => Err(DecodingError::Not32BitInst),
Expand All @@ -81,6 +87,7 @@ impl Decode for u32 {
OpcodeKind::A(opc) => Ok(a_extension::parse_rs2(self, opc)),
OpcodeKind::Zifencei(opc) => Ok(zifencei_extension::parse_rs2(self, opc)),
OpcodeKind::Zicsr(opc) => Ok(zicsr_extension::parse_rs2(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_rs2(self, opc)),
OpcodeKind::Zicntr(opc) => Ok(zicntr_extension::parse_rs2(self, opc)),
OpcodeKind::Priv(opc) => Ok(priv_extension::parse_rs2(self, opc)),
OpcodeKind::C(_) => Err(DecodingError::Not32BitInst),
Expand All @@ -94,6 +101,7 @@ impl Decode for u32 {
OpcodeKind::A(opc) => Ok(a_extension::parse_imm(self, opc)),
OpcodeKind::Zifencei(opc) => Ok(zifencei_extension::parse_imm(self, opc)),
OpcodeKind::Zicsr(opc) => Ok(zicsr_extension::parse_imm(self, opc)),
OpcodeKind::Zicfiss(opc) => Ok(zicfiss_extension::parse_imm(self, opc)),
OpcodeKind::Zicntr(opc) => Ok(zicntr_extension::parse_imm(self, opc)),
OpcodeKind::Priv(opc) => Ok(priv_extension::parse_imm(self, opc)),
OpcodeKind::C(_) => Err(DecodingError::Not32BitInst),
Expand All @@ -110,12 +118,18 @@ impl DecodeUtil for u32 {
fn parse_extension(self) -> Result<Extensions, DecodingError> {
let opmap: u8 = u8::try_from(self.slice(6, 0)).unwrap();
let funct3: u8 = u8::try_from(self.slice(14, 12)).unwrap();
let funct5: u8 = u8::try_from(self.slice(31, 27)).unwrap();
let funct7: u8 = u8::try_from(self.slice(31, 25)).unwrap();
let csr: u16 = u16::try_from(self.slice(31, 20)).unwrap();

match opmap {
0b000_1111 => Ok(Extensions::Zifencei),
0b010_1111 => Ok(Extensions::A),
0b010_1111 => match funct5 {
0b00000 | 0b00001 | 0b00010 | 0b00011 | 0b00100 | 0b01000 | 0b01100 | 0b10000
| 0b10100 | 0b11000 | 0b11100 => Ok(Extensions::A),
0b01001 => Ok(Extensions::Zicfiss),
_ => Err(DecodingError::UnknownExtension),
},
0b011_0011 => match funct7 {
0b000_0001 => Ok(Extensions::M),
_ => Ok(Extensions::BaseI),
Expand All @@ -135,6 +149,7 @@ impl DecodeUtil for u32 {
0xc00..=0xc02 | 0xc80..=0xc82 => Ok(Extensions::Zicntr),
_ => Ok(Extensions::Zicsr),
},
0b100 => Ok(Extensions::Zicfiss),
_ => Ok(Extensions::Zicsr),
},
_ => Ok(Extensions::BaseI),
Expand Down
Loading