Skip to content

Commit 8c861e0

Browse files
committed
feat(operation) Added format entries for operations universally.
1 parent a8547f3 commit 8c861e0

File tree

6 files changed

+179
-47
lines changed

6 files changed

+179
-47
lines changed

examples/counter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ fn main() {
2525
// }
2626
// ```
2727
];
28+
29+
dbg!(Instruction::decode(0b1111111111111111_00_0000001));
2830
}

src/core.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub mod arithmetic;
2+
3+
use crate::instruction::RegisterCode;
4+
5+
#[derive(Debug, Clone, Copy, PartialEq)]
6+
pub struct State {
7+
pub instruction_pointer: u64,
8+
// TODO: Operand modifiers state
9+
pub general: [u64; RegisterCode::MASK as usize],
10+
// pub general_vectors: []
11+
}
12+
13+
#[derive(Debug, Clone, Copy, PartialEq)]
14+
pub struct Core {
15+
/// The emulation context
16+
pub state: State
17+
}

src/core/arithmetic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::core::Core;
2+
use crate::instruction::RegisterCode;
3+
4+
impl Core {
5+
pub fn add(&mut self, destination: RegisterCode, sources: [RegisterCode; 2]) {
6+
todo!()
7+
}
8+
}

src/instruction.rs

+47-24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod operation;
22
pub mod address;
33
pub mod vector;
44
pub mod flag;
5+
pub mod encoding;
56

67
use crate::instruction::address::Address;
78
use crate::instruction::flag::Flag;
@@ -13,6 +14,27 @@ pub type RegisterCode = MaskedU8<0xF>;
1314
pub type BranchHintCode = MaskedU8<0x3>;
1415
pub type OperandCode = MaskedU8<0x3>;
1516

17+
#[derive(Debug, Clone, Copy, PartialEq)]
18+
pub enum Format {
19+
WaitForInterrupt,
20+
LoadImmediate,
21+
LoadVectorComponents,
22+
ExtractVectorComponents,
23+
FlagVectorComponents,
24+
MapVector,
25+
Branch,
26+
27+
DualSource,
28+
Destination,
29+
DestinationSource,
30+
DestinationDualSource,
31+
DestinationTripleSource,
32+
DualDestinationDualSource,
33+
Memory,
34+
SourceMemory,
35+
DestinationMemory
36+
}
37+
1638
#[derive(Debug, Clone, Copy, PartialEq)]
1739
pub enum Instruction {
1840
WaitForInterrupt,
@@ -21,6 +43,31 @@ pub enum Instruction {
2143
segment: SegmentCode,
2244
immediate: u16
2345
},
46+
LoadVectorComponents {
47+
destination: RegisterCode,
48+
/// Having [None] means that the component corresponding to the index should be 0.
49+
components: [Option<vector::ComponentCode>; vector::SIZE]
50+
},
51+
ExtractVectorComponents {
52+
vector: RegisterCode,
53+
/// Having [None] means that the component corresponding to the index should not be extracted into a register.
54+
components: [Option<RegisterCode>; vector::SIZE]
55+
},
56+
FlagVectorComponents {
57+
flags: [VectorComponentFlags; vector::SIZE],
58+
temporary: bool
59+
},
60+
/// Only supports 2 operands due to the size constrain of an instruction.
61+
MapVector {
62+
mappings: [VectorComponentMapping; 2],
63+
temporary: bool
64+
},
65+
Branch {
66+
condition: Flag,
67+
hint: Option<bool>,
68+
address: Address
69+
},
70+
2471
DualSource {
2572
operation: operation::DualSource,
2673
sources: [RegisterCode; 2]
@@ -49,25 +96,6 @@ pub enum Instruction {
4996
destinations: [RegisterCode; 2],
5097
sources: [RegisterCode; 2]
5198
},
52-
LoadVectorComponents {
53-
destination: RegisterCode,
54-
/// Having [None] means that the component corresponding to the index should be 0.
55-
components: [Option<vector::ComponentCode>; vector::SIZE]
56-
},
57-
ExtractVectorComponents {
58-
vector: RegisterCode,
59-
/// Having [None] means that the component corresponding to the index should not be extracted into a register.
60-
components: [Option<RegisterCode>; vector::SIZE]
61-
},
62-
FlagVectorComponents {
63-
flags: [VectorComponentFlags; vector::SIZE],
64-
temporary: bool
65-
},
66-
/// Only supports 2 operands due to the size constrain of an instruction.
67-
MapVector {
68-
mappings: [VectorComponentMapping; 2],
69-
temporary: bool
70-
},
7199
Memory {
72100
operation: operation::Memory,
73101
address: Address
@@ -81,10 +109,5 @@ pub enum Instruction {
81109
operation: operation::DestinationMemory,
82110
destination: Address,
83111
source: RegisterCode
84-
},
85-
Branch {
86-
condition: Flag,
87-
hint: Option<bool>,
88-
address: Address
89112
}
90113
}

src/instruction/encoding.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::instruction::Instruction;
2+
3+
pub const OPERATION_MASK: u32 = 0x0000007F;
4+
5+
impl Instruction {
6+
pub fn decode(encoded: u32) {
7+
let operation = encoded & OPERATION_MASK;
8+
dbg!(operation);
9+
}
10+
}

src/instruction/operation.rs

+95-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
use crate::instruction::Format;
2+
use crate::num::MaskedU8;
3+
4+
pub type Code = MaskedU8<0x7F>;
5+
6+
#[derive(Debug, Clone, Copy, PartialEq)]
7+
pub enum DualSource {
8+
Compare
9+
}
10+
11+
impl DualSource {
12+
pub const MAPPINGS: [(Code, DualSource); 1] = [(Code::new(0), Self::Compare)];
13+
}
14+
115
#[derive(Debug, Clone, Copy, PartialEq)]
216
pub enum Destination {
317
Unstack
418
}
519

620
impl Destination {
7-
pub const MAPPINGS: [Destination; 1] = [Self::Unstack];
21+
pub const MAPPINGS: [(Code, Destination); 1] = [(Code::new(1), Self::Unstack)];
822
}
923

1024
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -13,7 +27,7 @@ pub enum DestinationSource {
1327
}
1428

1529
impl DestinationSource {
16-
pub const MAPPINGS: [DestinationSource; 1] = [Self::CopyRegisterToRegister];
30+
pub const MAPPINGS: [(Code, DestinationSource); 1] = [(Code::new(2), Self::CopyRegisterToRegister)];
1731
}
1832

1933
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -40,38 +54,40 @@ pub enum DestinationDualSource {
4054
}
4155

4256
impl DestinationDualSource {
43-
pub const MAPPINGS: [DestinationDualSource; 16] = [
44-
Self::Add, Self::Subtract, Self::Multiply, Self::Divide,
45-
Self::AddFloat, Self::SubtractFloat, Self::MultiplyFloat, Self::DivideFloat,
46-
Self::AddVector, Self::SubtractVector, Self::MultiplyVector, Self::DivideVector,
47-
Self::AddFloatVector, Self::SubtractFloatVector, Self::MultiplyFloatVector, Self::DivideFloatVector,
57+
pub const MAPPINGS: [(Code, DestinationDualSource); 16] = [
58+
(Code::new(3), Self::Add), (Code::new(4), Self::Subtract), (Code::new(5), Self::Multiply), (Code::new(6), Self::Divide),
59+
(Code::new(7), Self::AddFloat), (Code::new(8), Self::SubtractFloat), (Code::new(9), Self::MultiplyFloat), (Code::new(10), Self::DivideFloat),
60+
(Code::new(11), Self::AddVector), (Code::new(12), Self::SubtractVector), (Code::new(13), Self::MultiplyVector), (Code::new(14), Self::DivideVector),
61+
(Code::new(15), Self::AddFloatVector), (Code::new(16), Self::SubtractFloatVector), (Code::new(17), Self::MultiplyFloatVector), (Code::new(18), Self::DivideFloatVector),
4862
];
4963
}
5064

5165
#[derive(Debug, Clone, Copy, PartialEq)]
5266
pub enum DestinationTripleSource {
5367
MultiplyAndAdd,
5468
AddAndMultiply
69+
70+
// todo: Consider neccessity and operation space
5571
}
5672

5773
impl DestinationTripleSource {
58-
pub const MAPPINGS: [DestinationTripleSource; 2] = [
59-
Self::MultiplyAndAdd, Self::AddAndMultiply,
74+
pub const MAPPINGS: [(Code, DestinationTripleSource); 2] = [
75+
(Code::new(19), Self::MultiplyAndAdd), (Code::new(20), Self::AddAndMultiply),
6076
];
6177
}
6278

6379
#[derive(Debug, Clone, Copy, PartialEq)]
6480
pub enum DualDestinationDualSource {
6581
DivideWithRemainder,
66-
DivideFloatingWithRemainder,
82+
DivideFloatWithRemainder,
6783
DivideVectorWithRemainder,
6884
DivideFloatVectorWithRemainder
6985
}
7086

7187
impl DualDestinationDualSource {
72-
pub const MAPPINGS: [DualDestinationDualSource; 4] = [
73-
Self::DivideWithRemainder, Self::DivideFloatingWithRemainder,
74-
Self::DivideVectorWithRemainder, Self::DivideFloatVectorWithRemainder,
88+
pub const MAPPINGS: [(Code, DualDestinationDualSource); 4] = [
89+
(Code::new(21), Self::DivideWithRemainder), (Code::new(22), Self::DivideFloatingWithRemainder),
90+
(Code::new(23), Self::DivideVectorWithRemainder), (Code::new(24), Self::DivideFloatVectorWithRemainder),
7591
];
7692
}
7793

@@ -83,8 +99,8 @@ pub enum Memory {
8399
}
84100

85101
impl Memory {
86-
pub const MAPPINGS: [Memory; 3] = [
87-
Self::Call, Self::ReleaseMemory, Self::Branch,
102+
pub const MAPPINGS: [(Code, Memory); 3] = [
103+
(Code::new(25), Self::Call), (Code::new(26), Self::ReleaseMemory), (Code::new(27), Self::Branch),
88104
];
89105
}
90106

@@ -102,11 +118,11 @@ pub enum SourceMemory {
102118
}
103119

104120
impl SourceMemory {
105-
pub const MAPPINGS: [SourceMemory; 8] = [
106-
Self::CopyMemoryByteToRegister, Self::CopyMemoryWordToRegister,
107-
Self::CopyMemoryDwordToRegister, Self::CopyMemoryQwordToRegister,
108-
Self::AcquireMemoryByte, Self::AcquireMemoryWord,
109-
Self::AcquireMemoryDword, Self::AcquireMemoryQword,
121+
pub const MAPPINGS: [(Code, SourceMemory); 8] = [
122+
(Code::new(28), Self::CopyMemoryByteToRegister), (Code::new(29), Self::CopyMemoryWordToRegister),
123+
(Code::new(30), Self::CopyMemoryDwordToRegister), (Code::new(31), Self::CopyMemoryQwordToRegister),
124+
(Code::new(32), Self::AcquireMemoryByte), (Code::new(33), Self::AcquireMemoryWord),
125+
(Code::new(34), Self::AcquireMemoryDword), (Code::new(35), Self::AcquireMemoryQword),
110126
];
111127
}
112128

@@ -119,8 +135,64 @@ pub enum DestinationMemory {
119135
}
120136

121137
impl DestinationMemory {
122-
pub const MAPPINGS: [DestinationMemory; 4] = [
123-
Self::CopyRegisterByteToMemory, Self::CopyRegisterWordToMemory,
124-
Self::CopyRegisterDwordToMemory, Self::CopyRegisterQwordToMemory,
138+
pub const MAPPINGS: [(Code, DestinationMemory); 4] = [
139+
(Code::new(36), Self::CopyRegisterByteToMemory), (Code::new(37), Self::CopyRegisterWordToMemory),
140+
(Code::new(38), Self::CopyRegisterDwordToMemory), (Code::new(39), Self::CopyRegisterQwordToMemory),
125141
];
126142
}
143+
144+
#[derive(Debug, Clone, Copy, PartialEq)]
145+
pub struct Entry<'a> {
146+
pub format: Format,
147+
pub name: &'a str
148+
}
149+
150+
pub const MAPPINGS: [Entry; 47] = [
151+
Entry { format: Format::WaitForInterrupt, name: "wait_for_interrupt" },
152+
Entry { format: Format::LoadImmediate, name: "load_immediate" },
153+
Entry { format: Format::LoadVectorComponents, name: "load_vector_components" },
154+
Entry { format: Format::ExtractVectorComponents, name: "extract_vector_components" },
155+
Entry { format: Format::FlagVectorComponents, name: "flag_vector_components" },
156+
Entry { format: Format::MapVector, name: "map_vector" },
157+
Entry { format: Format::Branch, name: "branch" },
158+
Entry { format: Format::DualSource, name: "compare" },
159+
Entry { format: Format::Destination, name: "unstack" },
160+
Entry { format: Format::DestinationSource, name: "copy_register_to_register" },
161+
Entry { format: Format::DestinationDualSource, name: "add" },
162+
Entry { format: Format::DestinationDualSource, name: "subtract" },
163+
Entry { format: Format::DestinationDualSource, name: "multiply" },
164+
Entry { format: Format::DestinationDualSource, name: "divide" },
165+
Entry { format: Format::DestinationDualSource, name: "add_float" },
166+
Entry { format: Format::DestinationDualSource, name: "subtract_float" },
167+
Entry { format: Format::DestinationDualSource, name: "multiply_float" },
168+
Entry { format: Format::DestinationDualSource, name: "divide_float" },
169+
Entry { format: Format::DestinationDualSource, name: "add_vector" },
170+
Entry { format: Format::DestinationDualSource, name: "subtract_vector" },
171+
Entry { format: Format::DestinationDualSource, name: "multiply_vector" },
172+
Entry { format: Format::DestinationDualSource, name: "divide_vector" },
173+
Entry { format: Format::DestinationDualSource, name: "add_float_vector" },
174+
Entry { format: Format::DestinationDualSource, name: "subtract_float_vector" },
175+
Entry { format: Format::DestinationDualSource, name: "multiply_float_vector" },
176+
Entry { format: Format::DestinationDualSource, name: "divide_float_vector" },
177+
Entry { format: Format::DestinationTripleSource, name: "multiply_and_add" },
178+
Entry { format: Format::DestinationTripleSource, name: "add_and_multiply" },
179+
Entry { format: Format::DualDestinationDualSource, name: "divide_with_remainder" },
180+
Entry { format: Format::DualDestinationDualSource, name: "divide_float_with_remainder" },
181+
Entry { format: Format::DualDestinationDualSource, name: "divide_vector_with_remainder" },
182+
Entry { format: Format::DualDestinationDualSource, name: "divide_float_vector_with_remainder" },
183+
Entry { format: Format::Memory, name: "call" },
184+
Entry { format: Format::Memory, name: "release_memory" },
185+
Entry { format: Format::Memory, name: "branch" },
186+
Entry { format: Format::SourceMemory, name: "copy_memory_byte_to_register" },
187+
Entry { format: Format::SourceMemory, name: "copy_memory_word_to_register" },
188+
Entry { format: Format::SourceMemory, name: "copy_memory_dword_to_register" },
189+
Entry { format: Format::SourceMemory, name: "copy_memory_qword_to_register" },
190+
Entry { format: Format::SourceMemory, name: "aquire_memory_byte" },
191+
Entry { format: Format::SourceMemory, name: "aquire_memory_word" },
192+
Entry { format: Format::SourceMemory, name: "aquire_memory_dword" },
193+
Entry { format: Format::SourceMemory, name: "aquire_memory_qword" },
194+
Entry { format: Format::DestinationMemory, name: "copy_register_byte_to_memory" },
195+
Entry { format: Format::DestinationMemory, name: "copy_register_word_to_memory" },
196+
Entry { format: Format::DestinationMemory, name: "copy_register_dword_to_memory" },
197+
Entry { format: Format::DestinationMemory, name: "copy_register_qword_to_memory" }
198+
];

0 commit comments

Comments
 (0)