use five32_instruction_set::layout; use five32_instruction_set::fields::*; #[test] fn test_parser() { // // Super useful reference for all of this: // https://danielmangum.com/posts/risc-v-bytes-intro-instruction-formats/ // { let instr_raw: Word = 0b00000000_10110101_00000101_00111011; let instr = layout::R::from(instr_raw); assert_eq!(instr.opcode, Opcode(0b0111011)); assert_eq!(instr.func3, Func3(0)); assert_eq!(instr.func7, Func7(0)); assert_eq!(instr.rd, Rd(0b01010)); assert_eq!(instr.rs1, Rs1(0b01010)); assert_eq!(instr.rs2, Rs2(0b01011)); } { let instr_raw: Word = 0b11111111_00000001_00000001_00010011; let instr = layout::I::from(instr_raw); assert_eq!(instr.opcode, Opcode(0b010011)); assert_eq!(instr.func3, Func3(0)); assert_eq!(instr.immediate.0 as i32, -16); assert_eq!(instr.rd, Rd(0b00010)); assert_eq!(instr.rs1, Rs1(0b00010)); } { let instr_raw: Word = 0b00000000_00010001_00110100_00100011; let instr = layout::S::from(instr_raw); assert_eq!(instr.opcode, Opcode(0b0100011)); assert_eq!(instr.func3, Func3(0b011)); assert_eq!(instr.immediate, ImmediateS(0b000000001000)); assert_eq!(instr.rs1, Rs1(0b00010)); assert_eq!(instr.rs2, Rs2(0b00001)); } { let instr_raw: Word = 0b00000000_10100101_11000110_01100011; let instr = layout::B::from(instr_raw); assert_eq!(instr.opcode, Opcode(0b1100011)); assert_eq!(instr.func3, Func3(0b100)); assert_eq!(instr.immediate.0 as i32, 12); assert_eq!(instr.rs1, Rs1(0b01011)); assert_eq!(instr.rs2, Rs2(0b01010)); } { let instr_raw: Word = 0b00000000_00000010_00010101_00110111; let instr = layout::U::from(instr_raw); assert_eq!(instr.opcode, Opcode(0b0110111)); assert_eq!(instr.rd, Rd(0b01010)); assert_eq!(instr.immediate, ImmediateU(0b00000000000000100001000000000000)); } }