-
Notifications
You must be signed in to change notification settings - Fork 4
/
BankLabels.cs
145 lines (115 loc) · 4.57 KB
/
BankLabels.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
using System;
using System.Collections.Generic;
using System.Text;
using Romulus.Plugin;
using System.IO;
namespace snarfblasm
{
class AddressLabels : IAddressLabels
{
BankLabels ramLabels = new BankLabels(-1);
BankLabelList bankLabels = new BankLabelList();
public IBankLabels Ram {
get { return ramLabels; }
}
public IBankLabelList Banks {
get { return bankLabels; }
}
}
class BankLabelList : IBankLabelList
{
List<BankLabels> banks = new List<BankLabels>();
const int bankIndexLimit = 255;
internal IList<BankLabels> GetBanks() {
return banks;
}
public IBankLabels this[int bankIndex] {
get {
if (bankIndex < 0 || bankIndex > bankIndexLimit)
throw new ArgumentException("Bank index is out of range.");
for (int iBank = 0; iBank < banks.Count; iBank++) {
if (banks[iBank].BankIndex == bankIndex) {
return banks[iBank];
}
}
BankLabels newBank = new BankLabels(bankIndex);
// Add the new bank object (in order)
for (int iBank = 0; iBank < banks.Count; iBank++) {
// Insert before first bank that has higher index
if (banks[iBank].BankIndex > bankIndex) {
banks.Insert(iBank, newBank);
return newBank;
}
}
// If there is no bank with higher index, add the new bank to the end
banks.Add(newBank);
return newBank;
}
}
}
class BankLabels : IBankLabels
{
internal struct addressData
{
public string label;
public string comment;
public int size;
}
internal Dictionary<ushort, addressData> Labels = new Dictionary<ushort, addressData>();
internal Dictionary<ushort, addressData> GetLabels() {
return Labels;
}
public BankLabels(int bankIndex) {
BankIndex = bankIndex;
}
public int BankIndex { get; private set; }
public void AddComment(ushort address, string comment) {
AddLabelData(address, null, 0, comment);
}
public void AddLabel(ushort address, string label) {
AddLabelData(address, label, 0, null);
}
public void AddLabel(ushort address, string label, string comment) {
AddLabelData(address, label, 0, comment);
}
public void AddArrayLabel(ushort address, string label, int byteCount) {
AddLabelData(address, label, byteCount, null);
}
public void AddArrayLabel(ushort address, string label, int byteCount, string comment) {
AddLabelData(address, label, byteCount, comment);
}
private void AddLabelData(ushort address, string label, int byteCount, string comment) {
addressData data;
// Get data for specified address, if present, and remove it from the dictionary so we can re-add updated data.
if (Labels.TryGetValue(address, out data)) {
Labels.Remove(address);
} else {
data = default(addressData);
}
if (label != null)
data.label = label;
if (comment != null)
data.comment = comment;
if (byteCount > 0)
data.size = byteCount;
Labels.Add(address, data);
}
/// <summary>
/// Creates .NL files for the FCEUX debugger
/// </summary>
/// <param name="bankLabels"></param>
public byte[] BuildNlFile() {
MemoryStream outputStream = new MemoryStream();
StreamWriter output = new StreamWriter(outputStream);
var labels = GetLabels();
foreach (var entry in labels) {
var entryData = entry.Value;
string countString = (entryData.size > 0) ? ("/" + entryData.size.ToString("X")) : (string.Empty);
string nlEntry = "$" + entry.Key.ToString("X4") + countString + "#" + entryData.label + "#" + entryData.comment;
output.WriteLine(nlEntry);
}
output.Flush();
return outputStream.ToArray();
}
}
}