Skip to content

Commit 665ded9

Browse files
committed
feat(encoding) Implemented encoding protocol for many instructions.
1 parent 5ae3023 commit 665ded9

16 files changed

+289
-276
lines changed

encoder_table.svg

+40-43
Loading

examples/counter.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
extern crate arrseq_lightning;
22

3-
use arrseq_lightning::instruction::{Instruction, LargeImmediate, register, SegmentCode};
4-
use arrseq_lightning::instruction::load_immediate::{Immediate, Segment};
3+
use arrseq_lightning::instruction::Instruction;
54

65
fn main() {
7-
let c_max = 10u16;
8-
9-
let program = [
10-
Instruction::LoadImmediate {
11-
destination: register::Code::new(0),
12-
segment: Segment::Segment0(Immediate::new(c_max as u32))
13-
} // hlt
14-
15-
// pseudo code:
16-
//
17-
// ```
18-
// let count = c_max;
19-
// loop {
20-
// count -= 1;
21-
// if count == 0 { return 0 }
22-
// }
23-
// ```
24-
];
6+
let ins = Instruction::decode(0b00000000_00000000_00000000_00000100);
7+
dbg!(ins);
258
}

src/core.rs

-17
This file was deleted.

src/core/arithmetic.rs

-8
This file was deleted.

src/instruction.rs

+66-95
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,79 @@
1+
use proc_bitfield::{bitfield, ConvRaw};
12
use crate::num::MaskedU8;
23

3-
pub mod address;
4-
pub mod vector;
5-
pub mod branch;
6-
pub mod encoding;
7-
pub mod register;
8-
pub mod arithmetic;
94
pub mod load_immediate;
5+
pub mod memory;
6+
pub mod mnemonic;
7+
pub mod vector;
108

11-
pub type SegmentCode = MaskedU8<0x3>;
12-
pub type ScaleCode = MaskedU8<0x03>;
13-
14-
pub const STACK_SOURCE_MASK: (u8, u32) = (0, 0b0000000000000000000000011111);
15-
pub const UNSTACK_DEST_MASK: (u8, u32) = (0, 0b0000000000000000000000011111);
16-
17-
pub const LOAD_IMMEDIATE_DESTINATION_MASK: (u8, u32) = (0, 0b0000000000000000000000011111);
18-
pub const LOAD_IMMEDIATE_SEGMENT_MASK : (u8, u32) = (5, 0b0000000000000000000001100000);
19-
pub const LOAD_IMMEDIATE_OFFSET_MASK : (u8, u32) = (7, 0b1111111111111111111110000000);
20-
21-
pub const BUILD_VECTOR_DESTINATION_MASK: (u8, u32) = (0, 0b0000000000000000000000011111);
22-
pub const BUILD_VECTOR_COMPONENT_0_MASK: (u8, u32) = (5, 0b0000000000000000001111100000);
23-
pub const BUILD_VECTOR_COMPONENT_1_MASK: (u8, u32) = (10, 0b0000000000000111110000000000);
24-
pub const BUILD_VECTOR_COMPONENT_2_MASK: (u8, u32) = (15, 0b0000000011111000000000000000);
25-
pub const BUILD_VECTOR_COMPONENT_3_MASK: (u8, u32) = (20, 0b0001111100000000000000000000);
26-
27-
pub const UNBUILD_VECTOR_SOURCE_MASK : (u8, u32) = (0, 0b0000000000000000000000011111);
28-
pub const UNBUILD_VECTOR_DESTINATION_0_MASK: (u8, u32) = (5, 0b0000000000000000001111100000);
29-
pub const UNBUILD_VECTOR_DESTINATION_1_MASK: (u8, u32) = (10, 0b0000000000000111110000000000);
30-
pub const UNBUILD_VECTOR_DESTINATION_2_MASK: (u8, u32) = (15, 0b0000000011111000000000000000);
31-
pub const UNBUILD_VECTOR_DESTINATION_3_MASK: (u8, u32) = (20, 0b0001111100000000000000000000);
9+
bitfield! {
10+
/// A bitfield showcasing how to specify bit ranges.
11+
#[derive(Clone, Copy, PartialEq, Eq)]
12+
struct Format(pub u32): Debug, FromRaw, IntoRaw { operation: u8 @ 0..=4 }
13+
}
3214

33-
pub const COPY_REGISTER_DESTINATION_FILE_MASK: (u8, u32) = (0, 0b0000000000000000000000000011);
34-
pub const COPY_REGISTER_DESTINATION_MASK : (u8, u32) = (0, 0b0000000000000000000001111100);
35-
pub const COPY_REGISTER_SOURCE_FILE_MASK : (u8, u32) = (0, 0b0000000000000000000110000000);
36-
pub const COPY_REGISTER_SOURCE_MASK : (u8, u32) = (0, 0b0000000000000011111000000000);
15+
#[derive(Debug, Clone, Copy, PartialEq, ConvRaw)]
16+
pub enum Scale { X8, X16, X32, X64 }
3717

3818
#[derive(Debug, Clone, Copy, PartialEq)]
19+
#[repr(u8)]
3920
pub enum Instruction {
40-
None,
41-
Wait,
42-
43-
End,
44-
EndInterrupt,
45-
Interrupt,
46-
47-
Stack { source: register::Code },
48-
Unstack { destination: register::Code },
49-
50-
LoadImmediate {
51-
destination: register::Code,
52-
segment: load_immediate::Segment
53-
},
54-
BuildVector {
55-
destination: register::Code,
56-
components: [register::Code; vector::SIZE]
57-
},
58-
UnBuildVector {
59-
source: register::Code,
60-
destinations: [register::Code; vector::SIZE]
61-
},
62-
63-
CopyRegister {
64-
destination: register::Code,
65-
destination_file: register::FileName,
66-
source: register::Code,
67-
source_file: register::FileName
68-
},
21+
Mnemonic(mnemonic::Operation),
22+
23+
LoadImmediate(load_immediate::Operation),
24+
BuildVector(vector::BuildVectorOperation),
25+
UnBuildVector(vector::UnBuildVectorOperation),
6926

70-
Arithmetic {
71-
operation: arithmetic::Operation,
72-
vector: bool,
73-
atomic: bool,
74-
destination: register::Code,
75-
sources: [register::Code; 2]
76-
},
77-
Address {
78-
operation: address::Operation,
79-
data: address::Meta,
80-
offset: address::LargeOffset
81-
},
82-
AddressWithBase {
83-
operation: address::OperationWithBase,
84-
data: address::Meta,
85-
base: register::Code,
86-
offset: address::SmallOffset
87-
},
88-
Branch {
89-
operation: branch::Operation,
90-
data: branch::Meta,
91-
offset: branch::LargeOffset
92-
},
93-
BranchWithBase {
94-
operation: branch::OperationWithBase,
95-
data: branch::Meta,
96-
base: register::Code,
97-
offset: branch::SmallOffset
98-
},
27+
ReadMemory(memory::ReadOperation),
28+
WriteMemory(memory::WriteOperation),
29+
Stack(memory::StackOperation),
30+
UnStack(memory::UnStackOperation),
31+
Lock(memory::LockOperation),
32+
Branch(memory::BranchOperation),
9933

100-
Timer,
34+
Copy,
10135

102-
Lock,
103-
UnLock,
36+
Unary,
37+
Negate,
38+
SignExtend,
39+
Binary,
40+
RegroupingBinary,
41+
RegroupingQuaternary
42+
}
43+
44+
impl Instruction {
45+
pub const MNEMONIC_CODE : u8 = 0;
46+
pub const LOAD_IMMEDIATE_CODE: u8 = 1;
47+
pub const BUILD_VECTOR_CODE : u8 = 2;
48+
pub const UNBUILD_VECTOR_CODE: u8 = 3;
49+
pub const READ_MEMORY_CODE : u8 = 4;
50+
pub const WRITE_MEMORY_CODE : u8 = 5;
51+
pub const STACK_CODE : u8 = 6;
52+
pub const UNSTACK_CODE : u8 = 7;
53+
pub const LOCK_CODE : u8 = 8;
54+
pub const BRANCH_CODE : u8 = 9;
10455

105-
Free2,
106-
Free3,
107-
Free4
56+
pub fn decode(encoded: u32) -> Self {
57+
let operation = Format::from(encoded);
58+
match operation.operation() {
59+
Self::MNEMONIC_CODE => Self::Mnemonic(mnemonic::Format::from(encoded).operation()),
60+
Self::LOAD_IMMEDIATE_CODE => Self::LoadImmediate(load_immediate::Operation::from(encoded)),
61+
Self::BUILD_VECTOR_CODE => Self::BuildVector(vector::BuildVectorOperation::from(encoded)),
62+
Self::UNBUILD_VECTOR_CODE => Self::UnBuildVector(vector::UnBuildVectorOperation::from(encoded)),
63+
Self::READ_MEMORY_CODE => Self::ReadMemory(memory::ReadOperation::from(encoded)),
64+
Self::WRITE_MEMORY_CODE => Self::WriteMemory(memory::WriteOperation::from(encoded)),
65+
Self::STACK_CODE => Self::Stack(memory::StackOperation::from(encoded)),
66+
Self::UNSTACK_CODE => Self::UnStack(memory::UnStackOperation::from(encoded)),
67+
Self::LOCK_CODE => Self::Lock(memory::LockOperation::from(encoded)),
68+
Self::BRANCH_CODE => Self::Branch(memory::BranchOperation::from(encoded)),
69+
70+
_ => unimplemented!()
71+
}
72+
}
73+
}
74+
75+
impl Default for Instruction {
76+
fn default() -> Self {
77+
Self::Mnemonic(mnemonic::Operation::default())
78+
}
10879
}

src/instruction/address.rs

-24
This file was deleted.

src/instruction/arithmetic.rs

-7
This file was deleted.

src/instruction/branch.rs

-28
This file was deleted.

src/instruction/encoding.rs

Whitespace-only changes.

src/instruction/load_immediate.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::num::MaskedU32;
1+
use proc_bitfield::bitfield;
22

3-
pub type Immediate = MaskedU32<0x1FFFFF>;
4-
5-
#[derive(Debug, Clone, Copy, PartialEq)]
6-
pub enum Segment {
7-
Segment0(Immediate),
8-
Segment1(Immediate),
9-
Segment2(Immediate),
10-
Segment3(bool)
3+
bitfield! {
4+
#[derive(Clone, Copy, PartialEq, Eq)]
5+
pub struct Operation(pub u32): Debug, FromRaw, IntoRaw {
6+
pub segment: u8 @ 5..=6,
7+
pub value: u16 @ 11..=26,
8+
pub base: u8 @ 27..=31
9+
}
1110
}

0 commit comments

Comments
 (0)