-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLogCreatorExtensions.cs
More file actions
305 lines (254 loc) · 10.6 KB
/
LogCreatorExtensions.cs
File metadata and controls
305 lines (254 loc) · 10.6 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
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
using System.Linq;
using System.Text;
using Diz.Core.Interfaces;
using Diz.Core.model;
using Diz.Core.util;
using Diz.Cpu._65816;
using Diz.LogWriter.util;
namespace Diz.LogWriter;
public static class LogCreatorExtensions
{
public static string CreateAssemblyFormattedTextLine(this ILogCreatorDataSource<IData> data, int offset, int count)
{
var rawStr = new StringBuilder();
for (var i = 0; i < count; i++)
{
rawStr.Append((char)(data.GetRomByte(offset + i) ?? 0));
}
return CreateAssemblyFormattedTextLine(rawStr.ToString());
}
public static string CreateAssemblyFormattedTextLine(string rawStr)
{
// important: Asar will not accept null characters printed inside quoted text. so we need to break up text lines.
// also, asar seems to have issues with exclamation points in text
bool IsPrintableAsciiCharacter(char c) =>
c >= 32 && c <= 127 && c != '"' && c != '!';
var outputStr = new StringBuilder("db ");
var inQuotedSection = false;
bool StartQuotedSectionIfNeeded(bool printedSomethingBeforeThis)
{
if (inQuotedSection)
return false;
if (printedSomethingBeforeThis)
outputStr.Append(", ");
outputStr.Append('"');
inQuotedSection = true;
return true;
}
bool EndQuotedSectionIfNeeded()
{
if (!inQuotedSection)
return false;
outputStr.Append('"');
inQuotedSection = false;
return true;
}
var previouslyOutputConstant = false;
var i = 0;
foreach (var c in rawStr)
{
if (IsPrintableAsciiCharacter(c))
{
StartQuotedSectionIfNeeded(i != 0);
// final thing. there are some characters that, yes, are printable, but, we need an extra escape for them.
switch (c)
{
// NOTE: there might be some way to print a literal double quote in the output stream but,
// couldn't figure it out. something about doubling quotes.
// for now, we'll count it as "not printable"
case '\\':
// literal single backslash, we need to escape it when we write it.
outputStr.Append(@"\\"); // literally double backslash.
break;
default:
// otherwise, it's just a normal character
outputStr.Append(c);
break;
}
}
else
{
if (EndQuotedSectionIfNeeded() || previouslyOutputConstant)
outputStr.Append(", ");
outputStr.Append('$');
outputStr.Append(Util.NumberToBaseString(c, Util.NumberBase.Hexadecimal, 2));
previouslyOutputConstant = true;
}
++i;
}
EndQuotedSectionIfNeeded();
return outputStr.ToString();
}
public static int GetLineByteLength(this ILogCreatorDataSource<IData> data, int offset, int romSizeMax,
int countPerLine)
{
var snesApi = data.Data.GetSnesApi();
var flagType = snesApi.GetFlag(offset);
if (flagType == FlagType.Opcode)
return data.GetInstructionLength(offset);
GetLineByteLengthMaxAndStep(flagType, out var max, out var step, countPerLine);
var bankSize = data.GetBankSize();
var myBank = offset / bankSize;
var srcSnesAddress = data.ConvertPCtoSnes(offset);
var srcRegions = srcSnesAddress == -1 ? [] :
data.Data.Regions
.Where(x => srcSnesAddress >= x.StartSnesAddress && srcSnesAddress <= x.EndSnesAddress)
.OrderBy(x => x.Priority)
.ToList();
var min = step;
while (true)
{
if (min >= max)
break;
if (offset + min >= romSizeMax)
break;
if (snesApi.GetFlag(offset + min) != flagType)
break;
var endSnesAddress = data.ConvertPCtoSnes(offset + min);
if (data.Labels.GetLabel(endSnesAddress) != null)
break;
if (data.GetComment(endSnesAddress) != null)
break;
if ((offset + min) / bankSize != myBank)
break;
// check if we crossed a boundary of any "region" defined
// NOTE: doing it this way means that region boundaries that don't fall neatly across our "min" division
// might create correct but non-intuitive situations.
// i.e. a region boundary that starts/ends in the middle of a 16bit value
var dstRegions = endSnesAddress == -1 ? [] :
data.Data.Regions
.Where(x => endSnesAddress >= x.StartSnesAddress && endSnesAddress <= x.EndSnesAddress)
.OrderBy(x => x.Priority)
.ToList();
// warning: compares by reference (good enough for now)
if (!srcRegions.SequenceEqual(dstRegions))
break;
min += step;
}
return min;
}
private static void GetLineByteLengthMaxAndStep(FlagType flagType, out int max, out int step, int dataPerLineSize)
{
max = 1; step = 1;
switch (flagType)
{
case FlagType.Opcode:
break;
case FlagType.Unreached:
case FlagType.Operand:
case FlagType.Data8Bit:
case FlagType.Graphics:
case FlagType.Music:
case FlagType.Empty:
max = dataPerLineSize;
break;
case FlagType.Text:
max = 21;
break;
case FlagType.Data16Bit:
step = 2;
max = dataPerLineSize;
break;
case FlagType.Data24Bit:
step = 3;
max = dataPerLineSize;
break;
case FlagType.Data32Bit:
step = 4;
max = dataPerLineSize;
break;
case FlagType.Pointer16Bit:
step = 2;
max = 2;
break;
case FlagType.Pointer24Bit:
step = 3;
max = 3;
break;
case FlagType.Pointer32Bit:
step = 4;
max = 4;
break;
}
}
public static string GeneratePointerStr(this ISnesApi<IData> data, int offset, int bytes)
{
var ia = -1;
string format = "", param = "";
switch (bytes)
{
case 2:
// here's a tricky Diz-specific thing.
// at this address, we only have the two bytes of the IA to work with (since this is a 16-bit pointer).
// we need to come up with a bank# to use for this.
//
// which one to use?
// 1. almost always: we want to use the SAME bank as where the pointer is sitting. but we don't want to force the user to use that.
var autoDetectedBank = RomUtil.GetBankFromSnesAddress(data.ConvertPCtoSnes(offset));
// except... 2. we'll still allow the user to manually specify the bank by typing into the grid.
// this is useful if the pointers are going to RAM addresses [ex: Chrono Trigger],
// or, for places where we're using another bank in the code [ex: Megaman X text code]
var bankFromUser = data.GetDataBank(offset);
// Use the autodetected bank# unless the user excplicitly typed in a non-zero value.
var bankToUse = bankFromUser != 0 ? bankFromUser : autoDetectedBank;
ia = (bankToUse << 16) | data.GetRomWordUnsafe(offset);
format = "dw {0}";
param = Util.NumberToBaseString(data.GetRomWordUnsafe(offset), Util.NumberBase.Hexadecimal, 4, true);
break;
case 3:
ia = data.GetRomLongUnsafe(offset);
format = "dl {0}";
param = Util.NumberToBaseString(data.GetRomLongUnsafe(offset), Util.NumberBase.Hexadecimal, 6, true);
break;
case 4:
ia = data.GetRomLongUnsafe(offset);
format = "dl {0}" +
$" : db {Util.NumberToBaseString(data.GetRomByteUnsafe(offset + 3), Util.NumberBase.Hexadecimal, 2, true)}";
param = Util.NumberToBaseString(data.GetRomLongUnsafe(offset), Util.NumberBase.Hexadecimal, 6, true);
break;
}
if (data.ConvertSnesToPc(ia) < 0)
return string.Format(format, param);
var labelName = data.Labels.GetLabelName(ia);
// check: filter +/- labels here, like "+", "-", "++", "--", etc
if (labelName != "" && !RomUtil.IsValidPlusMinusLabel(labelName)) {
param = labelName;
}
return string.Format(format, param);
}
public static string GetFormattedBytes(this IReadOnlyByteSource data, int offset, int step, int bytes)
{
var res = step switch
{
1 => "db ",
2 => "dw ",
3 => "dl ",
4 => "dd ",
_ => ""
};
for (var i = 0; i < bytes; i += step)
{
if (i > 0) res += ",";
switch (step)
{
case 1:
res += Util.NumberToBaseString(data.GetRomByteUnsafe(offset + i), Util.NumberBase.Hexadecimal, 2,
true);
break;
case 2:
res += Util.NumberToBaseString(data.GetRomWordUnsafe(offset + i), Util.NumberBase.Hexadecimal, 4,
true);
break;
case 3:
res += Util.NumberToBaseString(data.GetRomLongUnsafe(offset + i), Util.NumberBase.Hexadecimal, 6,
true);
break;
case 4:
res += Util.NumberToBaseString(data.GetRomDoubleWordUnsafe(offset + i), Util.NumberBase.Hexadecimal,
8, true);
break;
}
}
return res;
}
}