-
Notifications
You must be signed in to change notification settings - Fork 20
/
symbols.go
254 lines (204 loc) · 6.63 KB
/
symbols.go
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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package symbols
import (
"fmt"
"sort"
"sync"
"github.com/jetsetilly/gopher2600/hardware/memory/memorymap"
)
// Symbols contains the all currently defined symbols.
type Symbols struct {
// the master table is made up of three sub-tables
label []*table
read *table
write *table
crit sync.Mutex
}
// newSymbols is the preferred method of initialisation for the Symbols type. In
// many instances however, ReadSymbolsFile() might be more appropriate.
func (sym *Symbols) initialise(numBanks int) {
sym.crit.Lock()
defer sym.crit.Unlock()
sym.label = make([]*table, numBanks)
for i := range sym.label {
sym.label[i] = newTable()
}
sym.read = newTable()
sym.write = newTable()
sym.canonise(nil)
}
// should be called in critical section
func (sym *Symbols) resort() {
for _, l := range sym.label {
sort.Sort(l)
}
sort.Sort(sym.read)
sort.Sort(sym.write)
}
// LabelWidth returns the maximum number of characters required by a label in
// the label table.
func (sym *Symbols) LabelWidth() int {
sym.crit.Lock()
defer sym.crit.Unlock()
max := 0
for _, l := range sym.label {
if l.maxWidth > max {
max = l.maxWidth
}
}
return max
}
// SymbolWidth returns the maximum number of characters required by a symbol in
// the read/write table.
func (sym *Symbols) SymbolWidth() int {
sym.crit.Lock()
defer sym.crit.Unlock()
if sym.read.maxWidth > sym.write.maxWidth {
return sym.read.maxWidth
}
return sym.write.maxWidth
}
// Get symbol from label table.
//
// The problem with this function is that it can't handle getting labels if a
// JMP for example, triggers a bankswtich at the same time. We can see this in
// E7 type cartridges. For example the second instruction of HeMan JMPs from
// bank 7 to bank 5. Short of having a copy of the label in every bank, or more
// entwined knowledge of how cartridge mappers work, there's not a lot we can
// do about this.
func (sym *Symbols) GetLabel(bank int, addr uint16) (Entry, bool) {
sym.crit.Lock()
defer sym.crit.Unlock()
if bank >= len(sym.label) {
return Entry{}, false
}
addr, _ = memorymap.MapAddress(addr, true)
if e, ok := sym.label[bank].byAddr[addr]; ok {
return e, ok
}
return Entry{}, false
}
// Getsymbol from read/write table.
//
// The read argument selects the table: true -> read table, false -> write table.
func (sym *Symbols) GetSymbol(addr uint16, read bool) (Entry, bool) {
sym.crit.Lock()
defer sym.crit.Unlock()
// we first try to get the symbol with a mapped address. if the resulting
// symbol is of SourceSystem then the result is fine, otherwise we try
// again with the unmapped address
ma, _ := memorymap.MapAddress(addr, read)
if read {
if e, ok := sym.read.get(ma); !ok || e.Source == SourceSystem {
return e, ok
}
return sym.read.get(addr)
}
if e, ok := sym.write.get(ma); !ok || e.Source == SourceSystem {
return e, ok
}
return sym.write.get(addr)
}
// SymbolSource identifies the source of the symbol.
type SymbolSource string
// List of valid SymbolSource values.
const (
SourceDASM SymbolSource = "DASM"
SourceAuto SymbolSource = "Auto"
SourceSystem SymbolSource = "System"
SourceCartridge SymbolSource = "Cartridge"
SourceCustom SymbolSource = "Custom"
)
// Add symbol to label table. Symbol will be modified so that it is unique in
// the label table.
func (sym *Symbols) AddLabel(source SymbolSource, bank int, addr uint16, symbol string) bool {
sym.crit.Lock()
defer sym.crit.Unlock()
if bank >= len(sym.label) {
return false
}
addr, _ = memorymap.MapAddress(addr, true)
return sym.label[bank].add(source, addr, symbol)
}
// Add symbol to label table using a symbols created from the address information.
func (sym *Symbols) AddLabelAuto(bank int, addr uint16) bool {
return sym.AddLabel(SourceAuto, bank, addr, fmt.Sprintf("L%04X", addr))
}
// Remove label from label table. Symbol will be modified so that it is unique
// in the label table.
func (sym *Symbols) RemoveLabel(source SymbolSource, bank int, addr uint16) bool {
sym.crit.Lock()
defer sym.crit.Unlock()
addr, _ = memorymap.MapAddress(addr, true)
if sym.label[bank].byAddr[addr].Source != source {
return false
}
return sym.label[bank].remove(addr)
}
// Update symbol in label table. Returns success.
func (sym *Symbols) UpdateLabel(source SymbolSource, bank int, addr uint16, oldLabel string, newLabel string) bool {
sym.crit.Lock()
defer sym.crit.Unlock()
if bank >= len(sym.label) {
return false
}
addr, _ = memorymap.MapAddress(addr, true)
return sym.label[bank].update(source, addr, oldLabel, newLabel)
}
// AddSymbol to read/write table. Symbol will be modified so that it is unique
// in the selected table.
//
// The read argument selects the table: true -> read table, false -> write table.
func (sym *Symbols) AddSymbol(source SymbolSource, addr uint16, symbol string, read bool) bool {
sym.crit.Lock()
defer sym.crit.Unlock()
if source == SourceSystem {
addr, _ = memorymap.MapAddress(addr, true)
}
if read {
return sym.read.add(source, addr, symbol)
}
return sym.write.add(source, addr, symbol)
}
// RemoveSymbol from read/write table.
//
// The read argument selects the table: true -> read table, false -> write table.
func (sym *Symbols) RemoveSymbol(source SymbolSource, addr uint16, read bool) bool {
sym.crit.Lock()
defer sym.crit.Unlock()
if source == SourceSystem {
addr, _ = memorymap.MapAddress(addr, true)
}
if read {
return sym.read.remove(addr)
}
return sym.write.remove(addr)
}
// UpdateSymbol in read/write table. Symbol will be modified so that it is
// unique in the selected table
//
// The read argument selects the table: true -> read table, false -> write table.
func (sym *Symbols) UpdateSymbol(source SymbolSource, addr uint16, oldLabel string, newLabel string, read bool) bool {
sym.crit.Lock()
defer sym.crit.Unlock()
if source == SourceSystem {
addr, _ = memorymap.MapAddress(addr, true)
}
if read {
return sym.read.update(source, addr, oldLabel, newLabel)
}
return sym.write.update(source, addr, oldLabel, newLabel)
}