-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assembler.cs
231 lines (190 loc) · 6.24 KB
/
Assembler.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
namespace NetLMC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
public static class Assembler
{
private static readonly Regex lineExpression = new(@"^(\S*)\t\s*([A-z]{1,3})\s*([\S]*)");
public static ushort[] Assemble(FileInfo file) => Assemble(file, out _);
public static ushort[] Assemble(FileInfo file, out AssemblerState state)
{
if (!file.Exists) { throw new FileNotFoundException(file.FullName); }
state = new AssemblerState();
List<AssemblerLine> lines = new(ParseFile(file, state));
ushort[] res = new ushort[100];
int i = 0;
foreach (var line in lines)
{
res[i++] = AssembleLine(line, state);
}
return res;
}
public static string Disassemble(uint code)
{
string op = LookupOp(code, out var arg);
if (op == null) { return $"??? {code}"; }
op = op.PadRight(3, ' ');
if (arg.HasValue) { return $"{op} {arg.Value}"; }
return op;
}
public static string LookupOp(uint code, out uint? arg)
{
if (code == (int)InputOp.IN || code == (int)InputOp.OUT)
{
arg = null;
return ((InputOp)code).ToString();
}
else if (code == 0)
{
arg = null;
return InputOp.HLT.ToString();
}
else
{
arg = code % 100;
string v = Enum.GetName((InputOp)code - (int)arg.Value);
if (v != null)
{
return v;
}
arg = code;
return null;
}
}
public static string LookupTag(uint addr, AssemblerState state)
{
return state.tags.Where(kvp => kvp.Value == addr).Select(kvp => kvp.Key).FirstOrDefault();
}
public static string Disassemble(ushort code, uint addr, AssemblerState debugState)
{
//string dasm = Disassemble(code);
string op = LookupOp(code, out var arg) ?? "???";
string tag1 = LookupTag(addr, debugState) ?? string.Empty;
if (!arg.HasValue)
return $"[{addr:00}] {tag1,10} {op}";
string tag2 = LookupTag(arg.Value, debugState) ?? string.Empty;
return $"[{addr:00}] {tag1,10} {op} {tag2} ({arg:00})";
}
internal struct AssemblerLine
{
public int fileLine;
public string tag;
public InputOp opcode;
public string arg;
public AssemblerLine(int fileLine, string text)
{
this.fileLine = fileLine;
var match = lineExpression.Match(text);
if (!match.Success)
{
throw new InvalidDataException($"Could not parse line instruction `{text}`");
}
if (!Enum.TryParse<InputOp>(match.Groups[2].Value, out this.opcode))
{
throw new InvalidDataException($"Unknown opcode {match.Groups[2].Value} on line {fileLine}");
}
this.tag = match.Groups[1].Value;
this.arg = match.Groups[3].Value;
if (string.IsNullOrWhiteSpace(this.tag)) { this.tag = null; }
if (string.IsNullOrWhiteSpace(this.arg)) { this.arg = null; }
}
}
public class AssemblerState
{
public int totalSize;
public Dictionary<string, int> tags;
public AssemblerState()
{
totalSize = 0;
tags = new Dictionary<string, int>();
}
}
internal static IEnumerable<AssemblerLine> ParseFile(FileInfo file, AssemblerState state)
{
int lineNo = 0;
foreach (var txt in File.ReadLines(file.FullName))
{
lineNo++;
string toParse = txt;
int comment = txt.IndexOf('#');
if (comment != -1)
{
toParse = toParse[..comment];
}
if (string.IsNullOrWhiteSpace(toParse)) { continue; }
toParse = toParse.Trim();
if (state.totalSize >= 100)
{
Console.WriteLine("Warning: Program Truncated, over 100 boxes");
yield break;
}
AssemblerLine line;
try
{
line = new AssemblerLine(lineNo, txt);
}
catch (Exception e)
{
throw new InvalidDataException($"Parsing failed on line {lineNo}:\n{txt}\nException: {e}");
}
if (line.tag != null)
{
if (state.tags.ContainsKey(line.tag))
{
throw new InvalidDataException($"Duplicate tag {line.tag} on line {lineNo}");
}
state.tags.Add(line.tag, state.totalSize);
}
state.totalSize++;
yield return line;
}
}
internal static ushort AssembleLine(AssemblerLine line, AssemblerState state)
{
int op = (int)line.opcode;
bool requiresArg = line.opcode switch
{
InputOp.HLT => line.arg != null, // if an arg is supplied, we assume DAT
InputOp.IN => false,
InputOp.OUT => false,
_ => true,
};
if (requiresArg)
{
if (line.arg == null) { throw new InvalidDataException($"Line {line.fileLine}, op {line.opcode} requires an argument but none is provided."); }
if (line.opcode == InputOp.DAT)
{
if (ushort.TryParse(line.arg, out ushort val))
{
if (val > 999) { throw new InvalidDataException($"DAT on line {line.fileLine} is out of range ({val} > 999)"); }
return val;
}
}
else if (state.tags.TryGetValue(line.arg, out int tagLine))
{
op += tagLine;
}
else
{
throw new InvalidDataException($"Tag {line.arg} used on line {line.fileLine} not defined.");
}
}
return (ushort)op;
}
public enum InputOp
{
HLT = 0,
DAT = 0,
ADD = 100,
SUB = 200,
STO = 300,
LDA = 500,
BR = 600,
BRZ = 700,
BRP = 800,
IN = 901,
OUT = 902,
}
}