From cfb0a8e176379ade0302ccf2f5f0ac487dfdbea2 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Wed, 19 Oct 2022 17:23:41 +0200 Subject: [PATCH] decode (recquire tests) --- src/decode.rs | 121 ++++++++++++++++++++++++++++++++------------------ src/main.rs | 2 +- 2 files changed, 78 insertions(+), 45 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index ed21275..80ac041 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,57 +1,90 @@ pub struct Instruction { - value : u64, + pub value : u64, - opcode : u8, - rs1 : u8, - rs2 : u8, - rs3 : u8, - rd : u8, - funct7 : u8, - funct7_smaller : u8, - funct3 : u8, - shamt : u8, + pub opcode : u8, + pub rs1 : u8, + pub rs2 : u8, + pub rs3 : u8, + pub rd : u8, + pub funct7 : u8, + pub funct7_smaller : u8, + pub funct3 : u8, + pub shamt : u8, - imm12_I : u16, - imm12_S : u16, + pub imm12_I : u16, + pub imm12_S : u16, - imm12_I_signed : i16, - imm12_S_signed : i16, - imm13 : i16, - imm13_signed : i16, + pub imm12_I_signed : i16, + pub imm12_S_signed : i16, + pub imm13 : i16, + pub imm13_signed : i16, - imm31_12 : u32, - imm21_1 : u32, + pub imm31_12 : u32, + pub imm21_1 : u32, - imm31_12_signed : i32, - imm21_1_signed : i32, + pub imm31_12_signed : i32, + pub imm21_1_signed : i32, } pub fn decode(val : u64) -> Instruction { + + let value = val; + + let opcode = (val & 0x7f) as u8; + let rs1 = ((val >> 15) & 0x1f) as u8; + let rs2 = ((val >> 20) & 0x1f) as u8; + let rs3 = ((val >> 27) & 0x1f) as u8; + let rd = ((val >> 7) & 0x1f) as u8; + let funct7 = ((val >> 25) & 0x7f) as u8; + let funct7_smaller = (funct7 & 0x3e) as u8; + + let funct3 = ((val >> 12) & 0x7) as u8; + let imm12_I = ((val >> 20) & 0xfff) as u16; + let imm12_S = (((val >> 20) & 0xfe0) + ((val >> 7) & 0x1f)) as u16; + + let imm12_I_signed = if imm12_I >= 2048 { imm12_I - 4096 } else { imm12_I } as i16; + let imm12_S_signed = if imm12_S >= 2048 { imm12_S - 4096 } else { imm12_S } as i16; + + let imm13 = (((val >> 19) & 0x1000) + ((val >> 20) & 0x7e0) + + ((val >> 7) & 0x1e) + ((val << 4) & 0x800)) as i16; + let imm13_signed = if imm13 >= 4096 { imm13 - 8192 } else { imm13 } as i16; + + let imm31_12 = (val & 0xfffff000) as u32; + let imm31_12_signed = imm31_12 as i32; + + let imm21_1 = ((val & 0xff000) + ((val >> 9) & 0x800) + + ((val >> 20) & 0x7fe) + ((val >> 11) & 0x100000)) as u32; + let imm21_1_signed = if imm21_1 >= 1048576 { imm21_1 - 2097152 } else { imm21_1 } as i32; + + let shamt = ((val >> 20) & 0x3f) as u8; + Instruction { - value : val, + value : value, - opcode : 0, - rs1 : 0, - rs2 : 0, - rs3 : 0, - rd : 0, - funct7 : 0, - funct7_smaller : 0, - funct3 : 0, - shamt : 0, - - imm12_I : 0, - imm12_S : 0, - - imm12_I_signed : 0, - imm12_S_signed : 0, - imm13 : 0, - imm13_signed : 0, - - imm31_12 : 0, - imm21_1 : 0, - - imm31_12_signed : 0, - imm21_1_signed : 0, + opcode : opcode, + rs1 : rs1, + rs2 : rs2, + rs3 : rs3, + rd : rd, + funct7 : funct7, + funct7_smaller : funct7_smaller, + + funct3 : funct3, + imm12_I : imm12_I, + imm12_S : imm12_S, + + imm12_I_signed : imm12_I_signed, + imm12_S_signed : imm12_S_signed, + + imm13 : imm13, + imm13_signed : imm13_signed, + + imm31_12 : imm31_12, + imm31_12_signed : imm31_12_signed, + + imm21_1 : imm21_1, + imm21_1_signed : imm21_1_signed, + + shamt : shamt } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9c521f7..16cda16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,5 +2,5 @@ mod decode; fn main() { let instr = decode::decode(98); - println!("Hello, world!"); + println!("Hello, world! opcode : {}", instr.opcode); }