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::*;

/// A generic, very low-level structure for data and code storage. It has no concept of numbers or strings. Instead, it manipulates an array
/// of bits. The basic operations can be used to store numbers or strings, compose methods like addition, multiplication, etc.
/// This makes the module really versatile.
pub struct Memory {
	pub data: Vec<bool>
}

impl Memory {
	/// Create a new memory instance
	pub fn new() -> Self {
		Self {
			data: vec![]
		}
	}

	/// Load a memory snapshot and drop the content from before
	pub fn load(&mut self, vec: &mut Vec<bool>) {
		self.data = vec.to_owned();
	}

	/// Add a vector of bits to the container
	pub fn append(&mut self, vec: &mut Vec<bool>) {
		self.data.append(vec);
	}

	/// Return the contents of the container
	pub fn dump(&mut self) -> Vec<bool> {
		self.data.clone()
	}

	/// Set the given bit to 1
	pub fn set(&mut self, a: usize, condition: bool) {
		if condition {
			self.data[a] = true;
		}
	}

	/// Set the given bit to 0
	pub fn unset(&mut self, a: usize) { 
		self.data[a] = false; 
	}	

	/// Grow the container by (n) bits
	pub fn grow(&mut self, amount: usize) { 
		self.data.append(&mut vec![false; amount]); 
	}

	/// Shrink the container by (n) bits
	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 } => {
				// Create a temporary copy of the source range
		        let mut bits: Vec<bool> = self.data[a as usize .. a as usize + len].to_vec();

		        // Remove the first N elements
		        for _ in 0 .. amount {
		            bits.remove(0);
		        }

		        // Pad the right side with zeroes
		        for _ in 0 .. amount {
		            bits.push(false);
		        }

		        // Write the result to the second range
		        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 } => {
				// Create a temporary copy of the source range
		        let mut bits: Vec<bool> = self.data[a as usize .. a as usize + len].to_vec();

		        let sign_bit = bits[0];
		        
		        // Remove the last N elements
		        for _ in 0 .. amount {
		            bits.pop();
		        }

		        // Pad with the sign bit
		        for _ in 0 .. amount {
		            bits.insert(0, sign_bit);
		        }

		        // Write the result to the second range
		        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;