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
use std::collections::HashMap;
use crate::{
mem::fast::Ram16k,
vm::{instruction::VmInstruction, segment::Segment},
Signal16,
};
pub struct VmEmulator {
instructions: Vec<VmInstruction>,
instruction_index: usize,
pub ram: Ram16k,
symbol_table: HashMap<String, usize>,
}
impl Default for VmEmulator {
fn default() -> Self {
let mut ram = Ram16k::default();
ram.data[0] = 256.into();
Self {
instructions: Default::default(),
instruction_index: 0,
ram,
symbol_table: Default::default(),
}
}
}
impl VmEmulator {
pub fn load(&mut self, instructions: Vec<VmInstruction>) {
self.instructions = instructions;
for (num, instruction) in self.instructions.iter().enumerate() {
match instruction {
VmInstruction::Label(label) | VmInstruction::Function(label, ..) => {
self.symbol_table.insert(label.clone(), num);
}
_ => (),
}
}
}
pub fn get_segment_address(&self, segment: Segment) -> i16 {
match segment {
Segment::Pointer | Segment::Static | Segment::Temp => {
let segment_pointer = segment.get_base_address();
segment_pointer as i16
}
_ => {
let segment_pointer = self.ram.data[segment.get_base_address()];
i16::from(segment_pointer)
}
}
}
pub fn set(&mut self, segment: Segment, value: Signal16) {
self.ram.data[segment.get_base_address()] = value;
}
pub fn push(&mut self, value: Signal16) {
let stack_pointer = &mut self.ram.data[Segment::Stack.get_base_address()];
let stack_address: usize = stack_pointer.into();
*stack_pointer += 1;
self.ram.data[stack_address] = value;
}
pub fn push_from(&mut self, segment: Segment, offset: Signal16) {
let value = match segment {
Segment::Constant => offset,
Segment::Pointer | Segment::Temp | Segment::Static => {
let offset: usize = offset.into();
let segment_address: usize = segment.get_base_address() + offset;
self.ram.data[segment_address]
}
_ => {
let segment_pointer = self.ram.data[segment.get_base_address()];
let segment_address: usize = (segment_pointer + offset).into();
self.ram.data[segment_address]
}
};
self.push(value);
}
pub fn pop(&mut self) -> Signal16 {
let stack_pointer = &mut self.ram.data[Segment::Stack.get_base_address()];
*stack_pointer -= 1;
let stack_address: usize = stack_pointer.into();
self.ram.data[stack_address]
}
pub fn pop_into(&mut self, segment: Segment, offset: Signal16) {
let element = self.pop();
let segment_address = self.get_segment_address(segment) + i16::from(offset);
self.ram.data[segment_address as usize] = element;
}
pub fn step(&mut self) {
if self.instruction_index >= self.instructions.len() {
return;
}
let instruction = self.instructions[self.instruction_index].clone();
match instruction {
VmInstruction::Push(segment, offset) => self.push_from(segment, offset.into()),
VmInstruction::Pop(segment, offset) => self.pop_into(segment, offset.into()),
VmInstruction::Add => {
let value = self.pop() + self.pop();
self.push(value);
}
VmInstruction::Sub => {
let b = self.pop();
let a = self.pop();
let value = a - b;
self.push(value);
}
VmInstruction::And => {
let value = self.pop() & self.pop();
self.push(value);
}
VmInstruction::Or => {
let value = self.pop() | self.pop();
self.push(value);
}
VmInstruction::Neg => {
let value = -self.pop();
self.push(value);
}
VmInstruction::Not => {
let value = !self.pop();
self.push(value);
}
VmInstruction::Eq => {
let value = self.pop() == self.pop();
self.push(value.into());
}
VmInstruction::Lt => {
let value = self.pop() > self.pop();
self.push(value.into());
}
VmInstruction::Gt => {
let value = self.pop() < self.pop();
self.push(value.into());
}
VmInstruction::Label(_) => (), VmInstruction::Goto(label) => {
self.instruction_index = *self.symbol_table.get(&label).unwrap();
}
VmInstruction::IfGoto(label) => {
let value = self.pop();
if value != Signal16::FALSE {
self.instruction_index = *self.symbol_table.get(&label).unwrap();
}
}
VmInstruction::Function(_function, local_count) => {
for _ in 0..local_count {
self.push(0.into());
}
}
VmInstruction::Call(function, arg_count) => {
let return_address = self.instruction_index as i16;
self.push(return_address.into());
let caller_local_pointer = self.ram.data[Segment::Local.get_base_address()];
self.push(caller_local_pointer);
let caller_arg_pointer = self.ram.data[Segment::Argument.get_base_address()];
self.push(caller_arg_pointer);
let caller_this_pointer = self.ram.data[Segment::This.get_base_address()];
self.push(caller_this_pointer);
let caller_that_pointer = self.ram.data[Segment::That.get_base_address()];
self.push(caller_that_pointer);
let stack_pointer = self.ram[Segment::Stack.get_base_address()];
self.ram[Segment::Argument.get_base_address()] =
stack_pointer - 5 - arg_count as i16;
self.ram[Segment::Local.get_base_address()] = stack_pointer;
let function_address = self.symbol_table.get(&function).unwrap();
self.instruction_index = *function_address - 1;
}
VmInstruction::Return(return_size_in_words) => {
let lcl = self.ram[Segment::Local.get_base_address()] as usize;
let return_address = self.ram[lcl - 5] as usize;
self.instruction_index = return_address;
let current_arg_address = self.ram[Segment::Argument.get_base_address()];
self.ram[Segment::Local.get_base_address()] = self.ram[lcl - 4];
self.ram[Segment::Argument.get_base_address()] = self.ram[lcl - 3];
self.ram[Segment::This.get_base_address()] = self.ram[lcl - 2];
self.ram[Segment::That.get_base_address()] = self.ram[lcl - 1];
for i in 0..return_size_in_words {
let offset = return_size_in_words - 1 - i;
let signal = self.pop();
self.ram.data[current_arg_address as usize + offset as usize] = signal;
}
self.ram[Segment::Stack.get_base_address()] =
current_arg_address + return_size_in_words as i16;
}
};
self.instruction_index += 1;
}
}