-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
408 lines (341 loc) · 12.6 KB
/
Program.cs
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using System;
using System.Collections.Generic;
using System.Text;
namespace MutableVM
{
class Program
{
static void Main(string[] args)
{
Machine m = new Machine();
int type;
int register;
int memory;
int constant;
// Simple Fibonacci number calculator
m.setMemoryAtLocation(0, 8000000000001); // A = 1
m.setMemoryAtLocation(1, 8000100000001); // B = 1
m.setMemoryAtLocation(2, 8000200000001); // C = 1
m.setMemoryAtLocation(3, 8000300000144); // D = 144
m.setMemoryAtLocation(4, 2000100000001); // A = A + B
m.setMemoryAtLocation(5, 1000100020001); // C = A;
m.setMemoryAtLocation(6, 1000100040001); // E = C
m.setMemoryAtLocation(7, 3000400030000); // E = E - D
m.setMemoryAtLocation(8, 7000400170002); // Jump to line 17 if E > 0
m.setMemoryAtLocation(9, 8000400000000); // E = 0
m.setMemoryAtLocation(10, 2000000010001); // B = B + A
m.setMemoryAtLocation(11, 1000000020001); // C = B;
m.setMemoryAtLocation(12, 1000100040001); // E = C
m.setMemoryAtLocation(13, 3000400030000); // E = E - D
m.setMemoryAtLocation(14, 7000400170002); // Jump to line 17 if E > 0
m.setMemoryAtLocation(15, 8000400000000); // E = 0
m.setMemoryAtLocation(16, 7000100049999); // Jump to 4
m.setMemoryAtLocation(17, 7000300009999); // goto line 3
Console.WriteLine("MutableVM by overlordnyaldee\n");
Console.WriteLine("Commands: ");
Console.WriteLine("printreg, printmem, quit\n");
Console.WriteLine("Current Memory:");
Console.WriteLine(m.printMemory());
Console.WriteLine(m.printRegisters());
// Internal instruction representation
List<int> CurrentInstruction = new List<int>(4);
// Main execution loop
while (true)
{
// Read input from console, split based on spaces
String[] input = Console.ReadLine().Split(' ');
// Manage commands
if (input[0].Equals("printmem"))
{
if (input.Length > 1)
{
int pos = Convert.ToInt32(input[1]);
Console.WriteLine("\nMemory at position " + pos + ":");
Console.WriteLine(m.printMemory(pos));
continue;
}
else
{
int pos = (int)m.getRegister((int)Machine.RegisterEnum.PC);
Console.WriteLine("\nMemory at position " + pos + ":");
Console.WriteLine(m.printMemory(pos));
continue;
}
}
else if (input[0].Equals("printreg"))
{
Console.WriteLine(m.printRegisters());
continue;
}
else if (input[0].Equals("quit"))
{
break;
}
// Retreive next instruction
Int64 instruction = m.getMemoryAtLocation((int)m.getRegister((int)Machine.RegisterEnum.PC));
Console.WriteLine("instruction: " + instruction.ToString("0000000000000"));
// Attempt to parse raw string into internal instruction format
try
{
CurrentInstruction = parseInstructionFromInteger(instruction);
}
catch (Exception e)
{
Console.WriteLine("instruction error: " + e.ToString());
break;
}
// Rename for readability
type = CurrentInstruction[0];
register = CurrentInstruction[1];
memory = CurrentInstruction[2];
constant = CurrentInstruction[3];
// Primary instruction switch
switch (type)
{
case 0: // Load
if (constant == 0)
{
m.setRegister(register, m.getMemoryAtLocation(memory));
}
else
{
m.setRegister(register, m.getRegister(memory));
}
break;
case 1: // Store
if (constant == 0)
{
m.setMemoryFromRegister(register, memory);
}
else
{
m.setRegister(memory, m.getRegister(register));
}
break;
case 2: // Add
m.setRegister(register, m.getRegister(register) + m.getRegister(memory));
break;
case 3: // Subtract
m.setRegister(register, m.getRegister(register) - m.getRegister(memory));
break;
case 4: // Multiply
m.setRegister(register, m.getRegister(register) * m.getRegister(memory));
break;
case 5: // Divide
m.setRegister(register, m.getRegister(register) / m.getRegister(memory));
break;
case 6: // Remainder
m.setRegister(register, m.getRegister(register) % m.getRegister(memory));
break;
case 7: // Jump
// Check Flags
if (constant == 0)
{
// Jump if zero
if (m.getRegister(register) == 0)
{
m.setRegister((int)Machine.RegisterEnum.PC, memory - 1);
}
}
else if (constant == 1)
{
// Jump if less than zero
if (m.getRegister(register) < 0)
{
m.setRegister((int)Machine.RegisterEnum.PC, memory - 1);
}
}
else if (constant == 2)
{
// Jump if greater than zero
if (m.getRegister(register) > 0)
{
m.setRegister((int)Machine.RegisterEnum.PC, memory - 1);
}
}
else
{
// Jump always
m.setRegister((int)Machine.RegisterEnum.PC, memory - 1);
}
break;
case 8: // Set register to constant
m.setRegister(register, constant);
break;
case 9: // No operation
break;
}
// Increment program counter
m.setRegister((int)Machine.RegisterEnum.PC, m.getRegister((int)Machine.RegisterEnum.PC) + 1);
Console.WriteLine(m.printRegisters());
}
Console.ReadLine();
}
static List<int> parseInstructionFromString(String a)
{
List<int> c = new List<int>(4);
c.Add(Convert.ToInt32(a.Substring(0, 1)));
c.Add(Convert.ToInt32(a.Substring(1, 4)));
c.Add(Convert.ToInt32(a.Substring(5, 4)));
c.Add(Convert.ToInt32(a.Substring(9)));
return c;
}
static List<int> parseInstructionFromInteger(Int64 instruction)
{
String a = instruction.ToString("G");
return parseInstructionFromString(a);
}
}
/// <summary>
/// Machine class that manages the overall machine
/// </summary>
class Machine
{
public enum RegisterEnum {A, B, C, D, E, F, PC};
Register[] registers = new Register[7] { new Register(), new Register(),
new Register(), new Register(), new Register(), new Register(), new Register() };
Memory Memory = new Memory();
public Machine()
{
clearAllRegisters();
clearMemory();
}
public Int64 getRegister(RegisterEnum index)
{
return registers[(int)index].register;
}
public Int64 getRegister(int index)
{
RegisterEnum e = (RegisterEnum)index;
return getRegister(e);
}
public void setRegister(RegisterEnum index, Int64 data)
{
registers[(int)index].register = data;
}
public void setRegister(int index, Int64 data)
{
RegisterEnum e = (RegisterEnum)index;
setRegister(e, data);
}
public String printRegisters()
{
StringBuilder sb = new StringBuilder(12);
sb.Append("PC: ");
sb.Append(registers[(int)RegisterEnum.PC].register);
for (int i = 0; i < registers.Length-1; i++)
{
sb.Append(" " + (char)(i+65) + ": ");
sb.Append(registers[i].register);
}
return sb.ToString();
}
public void clearRegister(RegisterEnum index)
{
registers[(int)index].register = 0;
}
public void clearRegister(int index)
{
RegisterEnum e = (RegisterEnum)index;
clearRegister(e);
}
public void clearAllRegisters()
{
foreach (Register r in registers)
{
r.register = 0;
}
}
public void setMemoryFromRegister(int register, int location)
{
Memory.setMemoryAtLocation(location, getRegister(register));
}
public Int64 getMemoryAtLocation(int location)
{
return Memory.getMemoryAtLocation(location);
}
public void setMemoryAtLocation(int location, Int64 data)
{
Memory.setMemoryAtLocation(location, data);
}
public void clearMemory()
{
Memory.clearMemory();
}
public String printMemory()
{
return printMemory(0);
}
public String printMemory(int location)
{
return printMemory(location, 16);
}
public String printMemory(int location, int length)
{
return Memory.printMemory(location, length);
}
}
/// <summary>
/// Class to internally represent a register
/// </summary>
class Register
{
public Int64 register { get; set; }
public Register()
{
clearRegister();
}
public void clearRegister()
{
register = 0;
}
}
/// <summary>
/// Class that manages the memory of a machine
/// </summary>
class Memory
{
private List<Int64> MemoryData;
public const int MemorySize = 8192;
public Memory()
{
MemoryData = new List<Int64>(MemorySize);
clearMemory();
}
public Int64 getMemoryAtLocation(int location)
{
return MemoryData[location];
}
public void setMemoryAtLocation(int location, Int64 data)
{
MemoryData[location] = data;
}
public void clearMemory()
{
MemoryData.Clear();
for (int i = 0; i < MemorySize; i++)
{
MemoryData.Add((Int64)0);
}
}
public String printMemory()
{
return printMemory(0);
}
public String printMemory(int location)
{
return printMemory(location, 16);
}
public String printMemory(int location, int length)
{
StringBuilder sb = new StringBuilder(length * 32);
String currentMem;
for (int i = location; i < location + length; i++)
{
currentMem = MemoryData[i].ToString("0000000000000");
sb.AppendLine(currentMem);
}
return sb.ToString();
}
}
}