-
Notifications
You must be signed in to change notification settings - Fork 2
/
Z80Node.cs
390 lines (322 loc) · 12.1 KB
/
Z80Node.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ZXNTCount
{
public enum FlagIndex
{
C = 0,
N = 1,
PV = 2,
H = 3,
Z = 4,
S = 5
};
public enum FlagType
{
Unknown = 0,
Unaffected,
AffectedAsDefined,
DetectsParity,
DetectsOverflow,
Set,
Reset,
Exceptional
}
class Z80Node : IComparable<Z80Node>
{
private string[] FlagStrings =
{
"Unknown",
"Unaffected",
"Affected As Defined",
"Detects Parity",
"Detects Overflow",
"Set",
"Reset",
"Exceptional"
};
public int Index = 0;
public int Grade = 0;
public string OpCode;
public string Name;
public int Size;
public int[] Time;
public string Description;
public FlagType[] Flags;
public bool IsUndocumented;
public bool IsNext;
public Regex Regex;
public Z80Node()
{
}
public Z80Node(string opCode, string name, int size, int[] time, string description, FlagType[] flags, bool isUndocumented, bool isNext)
{
OpCode = opCode;
Name = name;
Size = size;
Time = time;
Description = description;
Flags = flags;
IsUndocumented = isUndocumented;
IsNext = isNext;
Initialize();
}
public void Initialize()
{
Regex = new Regex(RegexString, RegexOptions.Compiled | RegexOptions.IgnoreCase);
Grade = GetGrade();
}
public string RegexString
{
get
{
bool hasSpace = Name.Contains(" ");
string retString = "";
retString += Name;
retString = retString.Replace("(", @"\(");
retString = retString.Replace(")", @"\)");
retString = retString.Replace("**", "##");
retString = retString.Replace("*", "#");
retString = retString.Replace("+#", @"(?<displacement>|[+-]#)");
retString = retString.Replace("##", "(?<twoasterisks>.*)");
retString = retString.Replace("#", "(?<oneasterisk>.*)");
retString = retString.Replace(" ", @"[ \t]+)");
retString = retString.Replace(",", @",[ \t]*");
if (!hasSpace)
retString += ")";
retString = retString.Insert(0, "^(?<mnemonic>");
retString += "$";
return retString;
}
}
public void ProcessValue(ref StringBuilder stringBuilder, Group displacementGroup, Group asteriskGroup, string asteriskString)
{
for (int i = asteriskGroup.Captures.Count - 1; i >= 0; i--)
{
Capture asteriskCapture = asteriskGroup.Captures[i];
string asteriskValue = asteriskCapture.Value;
int startIndex = StringTools.IndexOf(stringBuilder, asteriskString);
foreach (Capture displacementCapture in displacementGroup.Captures)
{
string displacementValue = displacementGroup.Value;
if (displacementValue.Length == 0)
continue;
if (asteriskValue.Equals(displacementValue.Substring(1)))
{
asteriskValue = displacementValue;
if (asteriskValue.StartsWith("+"))
asteriskValue = asteriskValue.Substring(1);
break;
}
}
stringBuilder.Remove(startIndex, asteriskString.Length);
stringBuilder.Insert(startIndex, asteriskValue);
}
}
public bool GetInfo(string instruction, out string mnemonic, out string operands, out string description)
{
mnemonic = String.Empty;
operands = String.Empty;
description = Description;
Match match = Regex.Match(instruction);
if (match.Success)
{
StringBuilder stringBuilder = new StringBuilder(description);
Group mnemonicGroup = match.Groups["mnemonic"];
Group displacementGroup = match.Groups["displacement"];
Group twoAsterisksGroup = match.Groups["twoasterisks"];
Group oneAsteriskGroup = match.Groups["oneasterisk"];
ProcessValue(ref stringBuilder, displacementGroup, twoAsterisksGroup, "**");
ProcessValue(ref stringBuilder, displacementGroup, oneAsteriskGroup, "*");
stringBuilder.Replace("*", "0");
mnemonic = mnemonicGroup.Value.Trim();
//operands = instruction.Substring(mnemonicGroup.Value.Length).Replace(", ", ",");
operands = instruction.Substring(mnemonicGroup.Value.Length);
description = stringBuilder.ToString();
return true;
}
return false;
}
public int GetGrade()
{
int grade = !Name.Contains("(") ? 1 : 0; // no parentheses 1, parentheses 0
grade += Name.Contains("+*") ? 2 : 0; // ixy displacement is +2
grade += Name.Contains("(*") ? 4 : 0; // (**) addressing and (*) in/out is +4
grade += Name.Contains(",*") ? 8 : 0; // immediate arg2 is +8
grade += Name.Contains(" *") ? 16 : 0; // immediate arg1 is +16
return grade;
}
public string FlagString
{
get
{
string retString = "";
char[] flagArray = { ' ', '-', '+', 'P', 'V', '1', '0', '*' };
for (int i = 0; i < Flags.Length; i++)
retString += flagArray[(int)Flags[i]];
return retString;
}
}
public string FlagArrayString
{
get
{
string retString = "new FlagType[] { ";
for (int i = 0; i < Flags.Length; i++)
{
retString += String.Format("FlagType.{0}", Flags[i]);
if (i < Flags.Length - 1)
retString += ", ";
}
retString += " }";
return retString;
}
}
public string FlagTextString
{
get
{
string[] retArray = new string[6];
retArray[0] = String.Format("C: {0}", FlagStrings[(int)C]);
retArray[1] = String.Format("N: {0}", FlagStrings[(int)N]);
retArray[2] = String.Format("P/V: {0}", FlagStrings[(int)PV]);
retArray[3] = String.Format("H: {0}", FlagStrings[(int)H]);
retArray[4] = String.Format("Z: {0}", FlagStrings[(int)Z]);
retArray[5] = String.Format("S: {0}", FlagStrings[(int)S]);
return String.Join(Environment.NewLine, retArray);
}
}
public string TimeArrayString
{
get
{
string retString = "new int[] { ";
for (int i = 0; i < Time.Length; i++)
{
retString += Time[i].ToString();
if (i < Time.Length - 1)
retString += ", ";
}
retString += " }";
return retString;
}
}
public string TimeString
{
get { return Time.Length == 1 ? Time[0].ToString() : String.Format("{0}/{1}", MinTime, MaxTime); }
}
public int MaxTime
{
get { return Time.Length == 1 ? Time[0] : Math.Max(Time[0], Time[1]); }
}
public int MinTime
{
get { return Time.Length == 1 ? Time[0] : Math.Min(Time[0], Time[1]); }
}
public string ToolTip
{
get
{
List<string> retList = new List<string>();
retList.Add(String.Format("Opcode: {0}", OpCode));
retList.Add(String.Format("Name: {0}", Name));
retList.Add(String.Format("Size (bytes): {0}", Size));
retList.Add(String.Format("Time (clock cycles): {0}", TimeString));
retList.Add(FlagTextString);
retList.Add(StringTools.WordWrap(Description, 30));
return String.Join(Environment.NewLine, retList.ToArray());
}
}
public void ReadIni(IniFile inifile, string section)
{
OpCode = inifile.Read(section, "OpCode");
Name = inifile.Read(section, "Name");
Size = inifile.Read<int>(section, "Size");
Time = inifile.ReadArray<int>(section, "Time");
Description = inifile.Read(section, "Description");
Flags = inifile.ReadArray<FlagType>(section, "Flags");
IsUndocumented = inifile.Read<bool>(section, "IsUndocumented");
IsNext = inifile.Read<bool>(section, "IsNext");
Initialize();
}
public void WriteIni(IniFile inifile, string section)
{
inifile.Write(section, "OpCode", OpCode);
inifile.Write(section, "Name", Name);
inifile.Write<int>(section, "Size", Size);
inifile.WriteArray<int>(section, "Time", Time);
inifile.Write(section, "Description", Description);
inifile.WriteArray<FlagType>(section, "Flags", Flags);
inifile.Write<bool>(section, "IsUndocumented", IsUndocumented);
inifile.Write<bool>(section, "IsNext", IsNext);
}
public override string ToString()
{
return String.Format("new Z80Node(\"{0}\", \"{1}\", {2}, {3}, \"{4}\", {5}, {6}, {7}),", OpCode, Name, Size, TimeArrayString, Description, FlagArrayString, IsUndocumented.ToString().ToLower(), IsNext.ToString().ToLower());
}
public override bool Equals(object obj)
{
Z80Node other = (Z80Node)obj;
if (!this.OpCode.Equals(other.OpCode))
return false;
if (!this.Name.Equals(other.Name))
return false;
if (!this.Size.Equals(other.Size))
return false;
if (!this.Time.SequenceEqual(other.Time))
return false;
if (!this.Description.Equals(other.Description))
return false;
if (!this.Flags.SequenceEqual(other.Flags))
return false;
if (!this.IsUndocumented.Equals(other.IsUndocumented))
return false;
if (!this.IsNext.Equals(other.IsNext))
return false;
return true;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public int CompareTo(Z80Node other)
{
// [grade asc, length desc, lexicographic asc, index asc]
if (this.Grade != other.Grade)
return this.Grade.CompareTo(other.Grade);
if (this.Name.Length != other.Name.Length)
return -this.Name.Length.CompareTo(other.Name.Length);
if (!this.Name.Equals(other.Name))
return this.Name.CompareTo(other.Name);
return this.Index.CompareTo(other.Index);
}
public FlagType C
{
get { return Flags[(int)FlagIndex.C]; }
}
public FlagType N
{
get { return Flags[(int)FlagIndex.N]; }
}
public FlagType PV
{
get { return Flags[(int)FlagIndex.PV]; }
}
public FlagType H
{
get { return Flags[(int)FlagIndex.H]; }
}
public FlagType Z
{
get { return Flags[(int)FlagIndex.Z]; }
}
public FlagType S
{
get { return Flags[(int)FlagIndex.S]; }
}
}
}