1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::bytecode::BitwiseMethod;
use crate::bytecode::BitwiseMethod::*;
pub struct Memory {
pub data: Vec<bool>
}
impl Memory {
pub fn new() -> Self {
Self {
data: vec![]
}
}
pub fn load(&mut self, vec: &mut Vec<bool>) {
self.data = vec.to_owned();
}
pub fn append(&mut self, vec: &mut Vec<bool>) {
self.data.append(vec);
}
pub fn dump(&mut self) -> Vec<bool> {
self.data.clone()
}
pub fn set(&mut self, a: usize, condition: bool) {
if condition {
self.data[a] = true;
}
}
pub fn unset(&mut self, a: usize) {
self.data[a] = false;
}
pub fn grow(&mut self, amount: usize) {
self.data.append(&mut vec![false; amount]);
}
pub fn shrink(&mut self, amount: usize) -> Result<(), &'static str> {
let length = self.data.len() as isize;
if length - amount as isize >= 0 {
self.data.truncate(length as usize - amount);
Ok(())
} else {
Err("Cannot shrink to a buffer size lower than zero")
}
}
pub fn compute(&mut self, method: BitwiseMethod) -> Result<(), String> {
match method {
AND { a, b, out, len } => {
let out = out as usize;
let a = a as usize;
let b = b as usize;
for offset in 0 .. len {
self.data[out + offset] = self.data[a + offset] & self.data[b + offset];
}
},
OR { a, b, out, len } => {
let out = out as usize;
let a = a as usize;
let b = b as usize;
for offset in 0 .. len {
self.data[out + offset] = self.data[a + offset] | self.data[b + offset];
}
},
XOR { a, b, out, len } => {
let out = out as usize;
let a = a as usize;
let b = b as usize;
for offset in 0 .. len {
self.data[out + offset] = self.data[a + offset] ^ self.data[b + offset];
}
},
NOT { a, out, len } => {
let out = out as usize;
let a = a as usize;
for offset in 0 .. len {
self.data[out + offset] = !self.data[a + offset];
}
},
LSH { a, out, amount, len } => {
let mut bits: Vec<bool> = self.data[a as usize .. a as usize + len].to_vec();
for _ in 0 .. amount {
bits.remove(0);
}
for _ in 0 .. amount {
bits.push(false);
}
for (i, bit) in bits.iter().enumerate() {
if ((out as usize + i) as usize) < len {
self.data[out as usize + i] = *bit;
} else {
break;
}
}
},
RSH { a, out, amount, len } => {
let mut bits: Vec<bool> = self.data[a as usize .. a as usize + len].to_vec();
let sign_bit = bits[0];
for _ in 0 .. amount {
bits.pop();
}
for _ in 0 .. amount {
bits.insert(0, sign_bit);
}
for (i, bit) in bits.iter().enumerate() {
if ((out as usize + i) as usize) < len {
self.data[out as usize + i] = *bit;
} else {
break;
}
}
},
GROW { amount } => self.grow(amount as usize),
SHRINK { amount } => self.shrink(amount as usize)?,
SET { address } => self.set(address as usize, true),
UNSET { address } => self.unset(address as usize),
}
Ok(())
}
pub fn is_set(&self, address: Address) -> bool {
self.data[address]
}
pub fn is_unset(&self, address: Address) -> bool {
!self.data[address]
}
}
pub type Address = usize;