-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputer.cs
More file actions
171 lines (150 loc) · 4.43 KB
/
Computer.cs
File metadata and controls
171 lines (150 loc) · 4.43 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
using System;
namespace NARDIAC
{
public enum Opcode : byte
{
INP = 0,
CLA = 1,
ADD = 2,
TAC = 3,
SFT = 4,
OUT = 5,
STO = 6,
SUB = 7,
JMP = 8,
HRS = 9,
}
public class Computer
{
bool paused = true;
int pc = 0;
Word ir; // Instruction register
Word a = new Word(0, 0, 0); // Accumulator register
byte carry = 0; // Accumulator's carry
Opcode op => (Opcode)ir.Item1;
int xy => ir.Item2 * 10 + ir.Item3;
readonly NARDIAC.Input input;
readonly NARDIAC.Output output;
readonly NARDIAC.Memory memory = new Memory();
public Computer(Input input, Output output)
{
if (input == null) throw new ArgumentNullException(nameof(input));
if (output == null) throw new ArgumentNullException(nameof(output));
this.input = input;
this.output = output;
}
public void Run()
{
paused = false;
while (!paused)
{
Step();
}
}
private void Step()
{
// Fetch
ir = memory[pc];
// Advance
pc++;
// Decode
var instruction = Decode();
// Execute
instruction();
}
private Action Decode()
{
switch (op)
{
case Opcode.INP: // 0XY INP - Read input card into cell XY
return Inp;
case Opcode.CLA: // 1XY CLA - Clear accumulator and add into it the contents of cell XY
return Cla;
case Opcode.ADD: // 2XY ADD - Add contents of cell XY into accumulator.
return Add;
case Opcode.TAC: // 3XY TAC - Test accumulator and jump if negative.
return Tac;
case Opcode.SFT: // 4XY SFT - Shift accumulator X left, then Y right
return Sft;
case Opcode.OUT: // 5XY OUT - Print contents of cell XY on output card
return Out;
case Opcode.STO: // 6XY STO - Store contents of accumulator in cell XY.
return Sto;
case Opcode.SUB: // 2XY SUB - Subtract contents of cell XY from accumulator.
return Sub;
case Opcode.JMP: // 8XY JMP - Jump to XY and save PC to *99
return Jmp;
case Opcode.HRS: // 9XY HRS - Halt and reset program counter to XY. (usually 900)
return Hrs;
default:
throw new NotImplementedException(op.ToString());
}
}
private void Inp()
{
memory[xy] = input.Read();
}
private void Cla()
{
a = memory[xy];
carry = 0;
}
private void Add()
{
int @int = IntFromWord(a) + IntFromWord(memory[xy]);
a = WordFromInt(@int);
if (@int < 0) @int = -@int;
carry = (byte)(@int / 1000);
}
private void Sft() => throw new NotImplementedException("SFT");
private void Tac()
{
if (a.Negative)
{
// do not save old pc to *99
pc = xy;
}
}
private void Out()
{
output.Write(memory[xy]);
}
private void Sto()
{
memory[xy] = a;
}
private void Sub()
{
int @int = IntFromWord(a) - IntFromWord(memory[xy]);
a = WordFromInt(@int);
if (@int < 0) @int = -@int;
carry = (byte)(@int / 1000);
}
private void Jmp()
{
memory[99] = WordFromInt((pc % 100) + 800);
pc = xy;
}
private void Hrs()
{
pc = xy;
paused = true;
}
private Word WordFromInt(int i)
{
bool neg = i < 0;
if (neg) i = -i;
return new Word(
(byte)(i / 100),
(byte)((i % 100) / 10),
(byte)(i % 10),
neg
);
}
private int IntFromWord(Word c)
{
var i = c.Item1 * 100 + c.Item2 * 10 + c.Item3;
return c.Negative ? -i : i;
}
}
}