Skip to content

Commit 7497d94

Browse files
committed
feat(vld) Added decode support.
1 parent 51cf4df commit 7497d94

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

examples/counter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ fn main() {
2626
// ```
2727
];
2828

29-
dbg!(Instruction::decode(0b1111111111111111_00_1111_0000001));
29+
dbg!(Instruction::decode(0b1000_1000_1000_1000_0001_1000_0000010));
3030
}

src/instruction/encoding.rs

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1-
use crate::instruction::{Format, Instruction, operation, RegisterCode, SegmentCode};
1+
use crate::instruction::{Format, Instruction, operation, RegisterCode, SegmentCode, vector};
2+
use crate::num::MaskedU32;
23

34
pub const OPERATION_MASK: u32 = 0x0000007F;
45

5-
pub const LOAD_IMMEDIATE_DESTINATION: u32 = 0b00000000_00000000_00000000_00001111;
6-
pub const LOAD_IMMEDIATE_SEGMENT : (u32, u32) = (4, 0b00000000_00000000_00000000_00110000);
7-
pub const LOAD_IMMEDIATE_IMMEDIATE : (u32, u32) = (6, 0b00000000_00111111_11111111_11000000);
6+
pub const LOAD_IMMEDIATE_DESTINATION : u32 = 0b0_00000000_00000000_00001111;
7+
pub const LOAD_IMMEDIATE_SEGMENT : (u32, u32) = (4, 0b0_00000000_00000000_00110000);
8+
pub const LOAD_IMMEDIATE_IMMEDIATE : (u32, u32) = (6, 0b0_00111111_11111111_11000000);
9+
pub const LOAD_VECTOR_DESTINATION : u32 = 0b0_00000000_00000000_00001111;
10+
pub const LOAD_VECTOR_COMPONENT_ENABLE_0: (u32, u32) = (4, 0b0_00000000_00000000_00010000);
11+
pub const LOAD_VECTOR_COMPONENT_ENABLE_1: (u32, u32) = (5, 0b0_00000000_00000000_00100000);
12+
pub const LOAD_VECTOR_COMPONENT_ENABLE_2: (u32, u32) = (6, 0b0_00000000_00000000_01000000);
13+
pub const LOAD_VECTOR_COMPONENT_ENABLE_3: (u32, u32) = (7, 0b0_00000000_00000000_10000000);
14+
pub const LOAD_VECTOR_COMPONENT_0 : (u32, u32) = (8, 0b0_00000000_00001111_00000000);
15+
pub const LOAD_VECTOR_COMPONENT_1 : (u32, u32) = (12, 0b0_00000000_11110000_00000000);
16+
pub const LOAD_VECTOR_COMPONENT_2 : (u32, u32) = (16, 0b0_00001111_00000000_00000000);
17+
pub const LOAD_VECTOR_COMPONENT_3 : (u32, u32) = (20, 0b0_11110000_00000000_00000000);
18+
19+
pub type EncodedOperands = MaskedU32<0x1FFFFFF>;
820

921
impl Instruction {
10-
const fn decode_load_immediate(operands: u32) -> (RegisterCode, SegmentCode, u16) {
22+
const fn decode_load_immediate(operands: EncodedOperands) -> (RegisterCode, SegmentCode, u16) {
23+
let operands = operands.get();
1124
let destination = RegisterCode::new((operands & LOAD_IMMEDIATE_DESTINATION) as u8);
1225
let segment = SegmentCode::new(((operands & LOAD_IMMEDIATE_SEGMENT.1) >> LOAD_IMMEDIATE_SEGMENT.0) as u8);
1326
let immediate = ((operands & LOAD_IMMEDIATE_IMMEDIATE.1) >> LOAD_IMMEDIATE_IMMEDIATE.0) as u16;
1427
(destination, segment, immediate)
1528
}
16-
29+
30+
fn decode_load_vector(operands: EncodedOperands) -> (RegisterCode, [Option<vector::ComponentCode>; vector::SIZE]) {
31+
let operands = operands.get();
32+
let destination = RegisterCode::new((operands & LOAD_VECTOR_DESTINATION) as u8);
33+
34+
let enable_0 = ((operands & LOAD_VECTOR_COMPONENT_ENABLE_0.1) >> LOAD_VECTOR_COMPONENT_ENABLE_0.0) > 0;
35+
let enable_1 = ((operands & LOAD_VECTOR_COMPONENT_ENABLE_1.1) >> LOAD_VECTOR_COMPONENT_ENABLE_1.0) > 0;
36+
let enable_2 = ((operands & LOAD_VECTOR_COMPONENT_ENABLE_2.1) >> LOAD_VECTOR_COMPONENT_ENABLE_2.0) > 0;
37+
let enable_3 = ((operands & LOAD_VECTOR_COMPONENT_ENABLE_3.1) >> LOAD_VECTOR_COMPONENT_ENABLE_3.0) > 0;
38+
39+
let component_0 = vector::ComponentCode::new(((operands & LOAD_VECTOR_COMPONENT_0.1) >> LOAD_VECTOR_COMPONENT_0.0) as u8);
40+
let component_1 = vector::ComponentCode::new(((operands & LOAD_VECTOR_COMPONENT_1.1) >> LOAD_VECTOR_COMPONENT_1.0) as u8);
41+
let component_2 = vector::ComponentCode::new(((operands & LOAD_VECTOR_COMPONENT_2.1) >> LOAD_VECTOR_COMPONENT_2.0) as u8);
42+
let component_3 = vector::ComponentCode::new(((operands & LOAD_VECTOR_COMPONENT_3.1) >> LOAD_VECTOR_COMPONENT_3.0) as u8);
43+
44+
(destination, [
45+
enable_0.then_some(component_0),
46+
enable_1.then_some(component_1),
47+
enable_2.then_some(component_2),
48+
enable_3.then_some(component_3)
49+
])
50+
}
51+
1752
pub fn decode(encoded: u32) -> Self {
1853
let operation = encoded & OPERATION_MASK;
1954

@@ -22,15 +57,18 @@ impl Instruction {
2257
.unwrap_or(&operation::MAPPINGS[0])
2358
.format;
2459

25-
let operands = (encoded & !OPERATION_MASK) >> 7;
60+
let operands = EncodedOperands::new((encoded & !OPERATION_MASK) >> 7);
2661

2762
match format {
2863
Format::WaitForInterrupt => Self::WaitForInterrupt,
2964
Format::LoadImmediate => {
3065
let (destination, segment, immediate) = Self::decode_load_immediate(operands);
3166
Self::LoadImmediate { destination, segment, immediate }
3267
},
33-
Format::LoadVectorComponents => todo!(),
68+
Format::LoadVectorComponents => {
69+
let (destination, components) = Self::decode_load_vector(operands);
70+
Self::LoadVectorComponents {destination, components}
71+
},
3472
Format::ExtractVectorComponents => todo!(),
3573
Format::FlagVectorComponents => todo!(),
3674
Format::MapVector => todo!(),

0 commit comments

Comments
 (0)