-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.rs
More file actions
297 lines (249 loc) · 8.12 KB
/
state.rs
File metadata and controls
297 lines (249 loc) · 8.12 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::io::Write;
use crate::text::Serializable;
/// Keeps track of the RAM's current state.
#[derive(Debug, Clone, Copy)]
pub struct State {
/// `Program Counter`: the offset of the next instruction to be executed
pc: usize,
/// Registers (128-bit * 1024 = 16KiB)
registers: [u128; 1024],
/// Highest register used (read from or written to)
highest_register: usize,
/// Machine running? (Is END reached?)
running: bool,
/// Steps up to now
steps: usize,
}
/// Methods for the State struct.
impl State {
/// Creates an empty (new) state.
pub const fn initial() -> State {
State {
pc: 0,
registers: [0; 1024],
highest_register: 0,
running: false,
steps: 0,
}
}
/// Returns the contents of the accumulator (r0).
pub fn get_acc(&self) -> u128 {
self.registers[0]
}
/// Sets the contents of the accumulator (r0).
pub fn set_acc(&mut self, value: u128) {
self.registers[0] = value;
}
/// Returns the contents of the given register.
pub fn get_reg(&mut self, index: usize) -> u128 {
if self.highest_register < index {
self.highest_register = index;
}
self.registers[index]
}
/// Sets a register to the given value.
pub fn set_reg(&mut self, index: usize, value: u128) {
if self.highest_register < index {
self.highest_register = index;
}
self.registers[index] = value;
}
/// Sets the program counter to the given value.
pub fn set_pc(&mut self, value: usize) {
self.pc = value;
}
/// Returns the program counter.
pub fn get_pc(&self) -> usize {
self.pc
}
/// Increments the program counter by 1.
pub fn inc_pc(&mut self) {
self.pc += 1;
}
/// Returns the highest register used.
pub fn get_highest_register(&self) -> usize {
self.highest_register
}
/// Set running to true when the machine starts
pub fn start(&mut self) {
self.running = true;
}
/// Set running to false when the END instruction is reached
pub fn stop(&mut self) {
self.running = false;
}
/// Check if the machine is running
pub fn is_running(&self) -> bool {
self.running
}
/// Increment the number of steps
pub fn inc_steps(&mut self) {
self.steps += 1;
}
/// Get the number of steps
pub fn get_steps(&self) -> usize {
self.steps
}
/// Prints registers up to the highest register used
pub fn print_registers<T: Write>(&self, output: &mut T) -> Result<(), String> {
for i in 0..self.highest_register+1 {
match output.write(
format!("r{:}: {}", i, self.registers[i]).as_bytes()
) {
Ok(_) => {},
Err(u) => return Err(
format!("Could not write to buffer: {}", u.to_string())
)
};
if i != self.highest_register {
match output.write(b", ") {
Ok(_) => {},
Err(u) => return Err(
format!("Could not write to buffer: {}", u.to_string())
)
};
}
}
Ok(())
}
/// Resets the machine's state to the initial one
pub fn reset(&mut self) {
self.pc = 0;
self.registers = [0; 1024];
self.highest_register = 0;
self.running = false;
self.steps = 0;
}
/// Overwrites the machine's state
pub fn overwrite(&mut self, new: &State) {
self.reset();
self.pc = new.pc;
self.highest_register = new.highest_register;
self.running = new.running;
self.steps = new.steps;
for i in 0..self.highest_register+1 {
self.registers[i] = new.registers[i];
}
}
}
/// Implement Serialization funcs for state objects
impl Serializable for State {
fn to_string(&self) -> String {
let mut res = String::new();
res.push_str(
// The state we get has already has its step/pc incremented internally
format!("Step {:2} -- PC: {:2}, ", self.steps-1, self.pc).as_str()
);
for rn in 0..self.highest_register+1 {
res.push_str(
format!("r{}: {}", rn, self.registers[rn]).as_str()
);
if rn != self.highest_register {
res.push_str(", ");
}
}
res
}
fn dump(&self) {
print!("Step {:2} -- PC: {:2}, ", self.steps-1, self.pc);
for rn in 0..self.highest_register+1 {
print!("r{}: {}, ", rn, self.registers[rn])
}
print!("\x08\x08\x20\x20");
}
}
impl State {
const SEPARATOR: char = '<';
pub fn to_wasm_comm_str(&self) -> String {
let mut res = String::new();
res.push(match self.running {
true => 'r',
false => 's'
});
res.push(Self::SEPARATOR);
res.push_str(&self.steps.to_string());
res.push(Self::SEPARATOR);
res.push_str(&self.pc.to_string());
res.push(Self::SEPARATOR);
res.push_str(&self.highest_register.to_string());
res.push(Self::SEPARATOR);
for i in 0..self.highest_register+1 {
res.push_str(&self.registers[i].to_string());
if i < self.highest_register {
res.push(Self::SEPARATOR);
}
}
res
}
pub fn from_wasm_comm_str(istr: &str) -> Result<State, String> {
let mut res = State::initial();
let mut tokens = istr.split(Self::SEPARATOR);
let mut next_token = tokens.next();
if next_token.is_none() {
return Err("Expected a running/stopped at pos 0".to_string());
} else {
match next_token.unwrap() {
"r" => res.running = true,
"s" => res.running = false,
_ => return Err(
format!("r? {}", next_token.unwrap())
)
}
}
next_token = tokens.next();
if next_token.is_none() {
return Err("Expected steps at pos 1".to_string());
} else {
match next_token.unwrap().parse::<usize>() {
Ok(steps) => res.steps = steps,
Err(pie) => {
return Err(
format!("step number {} -- {}", next_token.unwrap(), pie.to_string())
);
}
}
}
next_token = tokens.next();
if next_token.is_none() {
return Err("Expected a PC at pos 2".to_string());
} else {
match next_token.unwrap().parse::<usize>() {
Ok(pc) => res.set_pc(pc),
Err(pie) => {
return Err(
format!("pc {}: {}", next_token.unwrap(), pie.to_string())
);
}
}
}
next_token = tokens.next();
if next_token.is_none() {
return Err("Expected Highest Register at pos 3".to_string());
} else {
match next_token.unwrap().parse::<usize>() {
Ok(highest) => res.highest_register = highest,
Err(pie) => {
return Err(
format!("highest register {} - {}", next_token.unwrap(), pie.to_string())
);
}
}
}
for i in 0..res.highest_register+1 {
next_token = tokens.next();
if next_token.is_none() {
return Err("Expected more register values".to_string());
} else {
match next_token.unwrap().parse::<u128>() {
Ok(rv) => res.registers[i] = rv,
Err(pie) => {
return Err(
format!("register value {}: {} -- {}", i, next_token.unwrap(), pie.to_string())
);
}
}
}
}
Ok(res)
}
}