-
Notifications
You must be signed in to change notification settings - Fork 19
/
developer.go
352 lines (281 loc) · 9.41 KB
/
developer.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
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
// 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 developer
import (
"fmt"
"sync"
"time"
"github.com/jetsetilly/gopher2600/coprocessor"
"github.com/jetsetilly/gopher2600/coprocessor/developer/breakpoints"
"github.com/jetsetilly/gopher2600/coprocessor/developer/callstack"
"github.com/jetsetilly/gopher2600/coprocessor/developer/dwarf"
"github.com/jetsetilly/gopher2600/coprocessor/developer/faults"
"github.com/jetsetilly/gopher2600/coprocessor/developer/yield"
"github.com/jetsetilly/gopher2600/debugger/govern"
"github.com/jetsetilly/gopher2600/hardware/television"
"github.com/jetsetilly/gopher2600/hardware/television/coords"
"github.com/jetsetilly/gopher2600/hardware/television/signal"
"github.com/jetsetilly/gopher2600/logger"
)
// TV is the interface from the developer type to the television implementation.
type TV interface {
GetFrameInfo() television.FrameInfo
GetCoords() coords.TelevisionCoords
GetLastSignal() signal.SignalAttributes
}
// Cartridge defines the interface to the cartridge required by the developer package
type Cartridge interface {
GetCoProcBus() coprocessor.CartCoProcBus
PushFunction(func())
}
// Emulation defines an interface to the emulation for retreiving the emulation state
type Emulation interface {
State() govern.State
}
// Developer implements the CartCoProcDeveloper interface.
type Developer struct {
emulation Emulation
tv TV
cart Cartridge
// information about the source code to the program. can be nil.
// note that source is checked for nil outside the sourceLock. this is
// performance reasons (not need to acquire the lock if source is nil).
// however, this does mean we should be careful if reassigning the source
// field (but that doesn't happen)
source *dwarf.Source
sourceLock sync.Mutex
faults faults.Faults
faultsLock sync.Mutex
yieldState yield.State
yieldStateLock sync.Mutex
callstack callstack.CallStack
callstackLock sync.Mutex
breakpoints breakpoints.Breakpoints
breakpointsLock sync.Mutex
breakNextInstruction bool
breakAddress uint32
// profiler instance. measures cycles counts for executed address
profiler coprocessor.CartCoProcProfiler
// slow down rate of NewFrame()
framesSinceLastUpdate int
// keeps track of the previous breakpoint check. see checkBreakPointByAddr()
prevBreakpointCheck *dwarf.SourceLine
// keeps track of the previous line in profiling scan. see processProfiling()
prevProfileLine *dwarf.SourceLine
}
// NewDeveloper is the preferred method of initialisation for the Developer type.
func NewDeveloper(state Emulation, tv TV) Developer {
return Developer{
emulation: state,
tv: tv,
yieldState: yield.State{
StrobeTicker: time.NewTicker(100 * time.Microsecond),
},
}
}
func (dev *Developer) AttachCartridge(cart Cartridge, romFile string, elfFile string) error {
dev.cart = nil
dev.sourceLock.Lock()
dev.source = nil
dev.sourceLock.Unlock()
dev.faultsLock.Lock()
dev.faults = faults.NewFaults()
dev.faultsLock.Unlock()
dev.callstackLock.Lock()
dev.callstack = callstack.NewCallStack()
dev.callstackLock.Unlock()
dev.breakpointsLock.Lock()
dev.breakpoints = breakpoints.NewBreakpoints()
dev.breakpointsLock.Unlock()
dev.framesSinceLastUpdate = 0
dev.profiler = coprocessor.CartCoProcProfiler{
Entries: make([]coprocessor.CartCoProcProfileEntry, 0, 1000),
}
if cart == nil || cart.GetCoProcBus() == nil {
return nil
}
dev.cart = cart
// we always set the developer for the cartridge even if we have no source.
// some developer functions don't require source code to be useful
dev.cart.GetCoProcBus().GetCoProc().SetDeveloper(dev)
switch dev.emulation.State() {
case govern.EmulatorStart:
case govern.Initialising:
default:
panic("unexpected emulation on cartridge insertion")
}
var err error
t := time.Now()
dev.sourceLock.Lock()
dev.source, err = dwarf.NewSource(romFile, cart, elfFile)
dev.sourceLock.Unlock()
if err != nil {
return fmt.Errorf("developer: %w", err)
} else {
logger.Logf("developer", "DWARF loaded in %s", time.Since(t))
}
return nil
}
// HighAddress implements the coprocessor.CartCoProcDeveloper interface.
func (dev *Developer) HighAddress() uint32 {
if dev.source == nil {
return 0
}
dev.sourceLock.Lock()
defer dev.sourceLock.Unlock()
return uint32(dev.source.HighAddress)
}
// CheckBreakpoint implements the coprocessor.CartCoProcDeveloper interface.
func (dev *Developer) CheckBreakpoint(addr uint32) bool {
if dev.source == nil {
return false
}
if dev.breakNextInstruction && dev.breakAddress != addr {
dev.breakNextInstruction = false
dev.breakAddress = addr
return true
}
dev.sourceLock.Lock()
defer dev.sourceLock.Unlock()
ln := dev.source.LinesByAddress[uint64(addr)]
if ln == dev.prevBreakpointCheck {
return false
}
dev.prevBreakpointCheck = ln
dev.breakpointsLock.Lock()
defer dev.breakpointsLock.Unlock()
if dev.breakpoints.Check(addr) {
dev.breakAddress = addr
return true
}
return false
}
// HasSource returns true if source information has been found.
func (dev *Developer) HasSource() bool {
return dev.source != nil
}
const maxWaitUpdateTime = 60 // in frames
// NewFrame implements the television.FrameTrigger interface.
func (dev *Developer) NewFrame(frameInfo television.FrameInfo) error {
// only update FrameCycles if new frame was caused by a VSYNC or we've
// waited long enough since the last update
dev.framesSinceLastUpdate++
if !frameInfo.VSync || dev.framesSinceLastUpdate > maxWaitUpdateTime {
return nil
}
dev.framesSinceLastUpdate = 0
// do nothing else if no source is available
if dev.source == nil {
return nil
}
dev.sourceLock.Lock()
defer dev.sourceLock.Unlock()
dev.source.NewFrame(dev.emulation.State() == govern.Rewinding)
return nil
}
// EnableStrobe sets the yield strobe to the specified enable state and the
// specified address
func (dev *Developer) EnableStrobe(enable bool, addr uint32) {
dev.yieldStateLock.Lock()
defer dev.yieldStateLock.Unlock()
dev.yieldState.Strobe = enable
dev.yieldState.StrobeAddr = addr
}
// OnYield implements the coprocessor.CartCoProcDeveloper interface.
func (dev *Developer) OnYield(addr uint32, yield coprocessor.CoProcYield) {
dev.yieldStateLock.Lock()
defer dev.yieldStateLock.Unlock()
dev.yieldState.Addr = addr
dev.yieldState.Reason = yield.Type
dev.yieldState.LocalVariables = dev.yieldState.LocalVariables[:0]
// buildLocalsList() depends on the type of yield and whether a strobe is active
buildLocalsList := func(locals []*dwarf.SourceVariableLocal) {
dev.yieldState.LocalVariables = append(dev.yieldState.LocalVariables, locals...)
}
if yield.Type == coprocessor.YieldSyncWithVCS {
if !dev.yieldState.Strobe || addr != dev.yieldState.StrobeAddr {
return
}
select {
case <-dev.yieldState.StrobeTicker.C:
buildLocalsList = func(locals []*dwarf.SourceVariableLocal) {
dev.yieldState.StrobedLocalVariables = dev.yieldState.StrobedLocalVariables[:0]
dev.yieldState.StrobedLocalVariables = append(dev.yieldState.StrobedLocalVariables, locals...)
}
default:
return
}
}
dev.BorrowSource(func(src *dwarf.Source) {
if src == nil {
return
}
ln := src.FindSourceLine(dev.yieldState.Addr)
if ln == nil {
return
}
locals := src.GetLocalVariables(ln, addr)
buildLocalsList(locals)
if yield.Type.Bug() {
ln.Bug = true
}
// update local variables
for _, local := range locals {
local.Update()
}
src.UpdateGlobalVariables()
})
}
// MemoryFault implements the coprocessor.CartCoProcDeveloper interface.
func (dev *Developer) MemoryFault(event string, fault faults.Category, instructionAddr uint32, accessAddr uint32) {
dev.faultsLock.Lock()
defer dev.faultsLock.Unlock()
dev.faults.NewEntry(fault, event, instructionAddr, accessAddr)
}
// SetEmulationState is called by the emulation whenever state changes
func (dev *Developer) SetEmulationState(state govern.State) {
if dev.cart != nil {
switch state {
case govern.Rewinding:
dev.cart.GetCoProcBus().GetCoProc().BreakpointsEnable(false)
default:
dev.cart.GetCoProcBus().GetCoProc().BreakpointsEnable(true)
}
}
dev.BorrowSource(func(src *dwarf.Source) {
dev.yieldStateLock.Lock()
defer dev.yieldStateLock.Unlock()
switch state {
case govern.Paused:
if src == nil {
return
}
ln := src.FindSourceLine(dev.yieldState.Addr)
if ln == nil {
return
}
locals := src.GetLocalVariables(ln, dev.yieldState.Addr)
dev.yieldState.LocalVariables = dev.yieldState.LocalVariables[:0]
dev.yieldState.LocalVariables = append(dev.yieldState.LocalVariables, locals...)
src.UpdateGlobalVariables()
default:
dev.yieldState.LocalVariables = dev.yieldState.LocalVariables[:0]
}
})
}
// BreakOnNextStep forces the coprocess to break after next instruction execution
func (dev *Developer) BreakNextInstruction() {
dev.breakNextInstruction = true
}