-
Notifications
You must be signed in to change notification settings - Fork 2
/
unitformdebug.pas
385 lines (321 loc) · 10.2 KB
/
unitformdebug.pas
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
unit UnitFormDebug;
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, UnitSpectrum, UnitDisassembler, Z80Processor,
UnitFrameGridMemory, CommonFunctionsLCL, UnitDebugCpuState, UnitConfigs,
UnitDebugger, UnitFrameBreakpoints, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls, fpjson;
type
TFormDebug = class (TForm, TSpectrum.IFormDebug)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
LabelRomBanks: TLabel;
Label5: TLabel;
LabelRamBanks: TLabel;
LabelTicksInCurrentFrame: TLabel;
LabelFramesPassed: TLabel;
Panel1: TPanel;
Panel10: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
Panel8: TPanel;
Panel9: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
strict private
const
cSectionDebugger: RawByteString = 'debugger';
strict private
FSpectrum: TSpectrum;
FrameGridMemory: TFrameGridMemory;
FDebugCPUState: TDebugCpuState;
FormBreakpoints: TCustomForm;
ScrDisplayCtrl: TFlagsCtrl;
procedure FormFirstShow(Sender: TObject);
function GetDebugger: TDebugger;
procedure AfterShow(Data: PtrInt);
procedure LabelBreakpointsOnClick(Sender: TObject);
procedure FormBreakpointsBeforeDestruction(Sender: TObject);
procedure LoadFromJson();
procedure SaveToJson();
procedure SetOnDebuggerRunStop(AValue: TNotifyEvent);
protected
procedure DoClose(var CloseAction: TCloseAction); override;
public
procedure SetSpectrum(ASpectrum: TSpectrum);
procedure OnStep(out DoContinue: Boolean);
procedure AfterStep;
procedure SetActive(const AActive: Boolean);
function GetActive(): Boolean;
property OnDebuggerRunStop: TNotifyEvent write SetOnDebuggerRunStop;
end;
implementation
{$R *.lfm}
{ TFormDebug }
procedure TFormDebug.FormCreate(Sender: TObject);
var
Lab: TCustomLabel;
begin
FSpectrum := nil;
FormBreakpoints := nil;
Panel3.Enabled := True;
Panel1.Caption := '';
Panel2.Caption := '';
Panel3.Caption := '';
Panel3.BevelOuter := bvNone;
Panel5.Caption := '';
Panel5.BevelOuter := bvNone;
Panel4.Caption := '';
Panel4.BevelOuter := bvNone;
Panel4.BorderStyle := bsSingle;
Panel4.AutoSize := True;
Panel6.Caption := '';
Panel6.BevelOuter := bvNone;
Panel7.Caption := '';
Panel7.BevelOuter := bvNone;
Panel7.BorderStyle := bsSingle;
Panel7.AutoSize := True;
Panel8.Caption := '';
Panel8.BevelOuter := bvNone;
Panel9.Caption := '';
Panel9.BevelOuter := bvNone;
Panel9.BorderStyle := bsSingle;
Panel9.AutoSize := True;
Panel10.Caption := '';
Panel10.BevelOuter := bvNone;
Panel2.Anchors := [];
Panel3.Anchors := [];
FDebugCPUState := TDebugCpuState.Create(Self);
FDebugCPUState.Anchors := [];
FDebugCPUState.AnchorParallel(akTop, 0, Panel1);
FDebugCPUState.AnchorParallel(akLeft, 0, Panel1);
Panel3.AnchorParallel(akLeft, 3, Panel1);
Panel3.AnchorToNeighbour(akTop, 0, FDebugCPUState);
Panel3.AnchorParallel(akRight, 4, Panel1);
Panel3.BorderSpacing.Bottom := 2;
Panel2.AnchorParallel(akLeft, 0, Panel1);
Panel2.AnchorToNeighbour(akTop, 0, Panel3);
Panel2.AnchorParallel(akBottom, 0, Panel1);
Panel2.AnchorParallel(akRight, 0, Panel1);
FrameGridMemory := TFrameGridMemory.Create(Self);
FrameGridMemory.AnchorParallel(akLeft, 0, Panel2);
FrameGridMemory.AnchorParallel(akTop, 0, Panel2);
FrameGridMemory.AnchorParallel(akRight, 0, Panel2);
FrameGridMemory.AnchorParallel(akBottom, 0, Panel2);
Panel4.Anchors := [];
Panel4.AnchorParallel(akLeft, 0, Panel3);
Panel4.AnchorParallel(akBottom, 0, Panel3);
Panel7.Anchors := [];
Panel7.AnchorToNeighbour(akLeft, 3, Panel4);
Panel7.AnchorParallel(akBottom, 0, Panel3);
Panel9.Anchors := [];
Panel9.AnchorToNeighbour(akLeft, 3, Panel7);
Panel9.AnchorParallel(akBottom, 0, Panel3);
ScrDisplayCtrl := TFlagsCtrl.Create(Panel3, ['Shadow screen'], True, False);
ScrDisplayCtrl.Anchors := [];
ScrDisplayCtrl.AnchorToNeighbour(akLeft, 3, Panel9);
ScrDisplayCtrl.AnchorParallel(akBottom, 0, Panel3);
Panel4.Color := ScrDisplayCtrl.Color;
Panel7.Color := ScrDisplayCtrl.Color;
Panel9.Color := ScrDisplayCtrl.Color;
FDebugCPUState.Parent := Panel1;
FrameGridMemory.Parent := Panel2;
ScrDisplayCtrl.Parent := Panel3;
Panel3.AutoSize := True;
Lab := TCommonFunctionsLCL.CreateLinkLabel(Panel10, 'Breakpoints');
Lab.OnClick := @LabelBreakpointsOnClick;
Lab.Anchors := [];
Lab.AnchorParallel(akLeft, 0, Panel10);
Lab.AnchorParallel(akTop, 0, Panel10);
Lab.Parent := Panel10;
Panel10.Anchors := [];
Panel10.AnchorParallel(akBottom, 0, Panel3);
Panel10.AnchorToNeighbour(akLeft, 2, ScrDisplayCtrl);
Panel10.AutoSize := True;
TCommonFunctionsLCL.GrowFormHeight(Self);
Self.Constraints.MinHeight := Self.Height;
LabelFramesPassed.Caption := ' ';
LabelTicksInCurrentFrame.Caption := ' ';
LabelRomBanks.Caption := ' ';
LabelRamBanks.Caption := ' ';
ScrDisplayCtrl.SetValues(0);
Panel4.Hint := 'Frames passed since Spectrum was switched on'
+ LineEnding + '(application start or last hard reset)';
Panel4.ShowHint := True;
Panel7.Hint := 'Clock ticks passed since last interrupt';
Panel7.ShowHint := True;
Panel9.Hint := 'Currently mounted rom and ram banks'
+ LineEnding + '(interesting on 128K models only)';
Panel9.ShowHint := True;
ScrDisplayCtrl.Hint := 'Is "shadow" screen (bank 7) mounted'
+ LineEnding + '(interesting on 128K models only)';
ScrDisplayCtrl.ShowHint := True;
FrameGridMemory.Grid.OnEditBreakPoints := @LabelBreakpointsOnClick;
AfterShow(-1);
AddHandlerFirstShow(@FormFirstShow);
end;
procedure TFormDebug.FormDestroy(Sender: TObject);
begin
RemoveAllHandlersOfObject(Self);
Application.RemoveAsyncCalls(Self);
SetSpectrum(nil);
if Assigned(FrameGridMemory) then begin
FrameGridMemory.OnRunStop := nil;
FrameGridMemory.Grid.OnEditBreakPoints := nil;
FreeAndNil(FrameGridMemory);
end;
end;
procedure TFormDebug.LoadFromJson;
var
JObj: TJSONObject;
begin
JObj := TConfJSON.GetJSONObject(cSectionDebugger);
if Assigned(JObj) then begin
FrameGridMemory.LoadFromJSON(JObj);
end;
end;
procedure TFormDebug.SaveToJson;
var
JObj: TJSONObject;
begin
if Assigned(FrameGridMemory) then
if FrameGridMemory.SaveToJSON(JObj) then
if Assigned(JObj) then
try
TConfJSON.RemoveSection(cSectionDebugger);
if TConfJSON.AddToConf(cSectionDebugger, JObj) then
JObj := nil;
finally
JObj.Free;
end;
end;
procedure TFormDebug.SetOnDebuggerRunStop(AValue: TNotifyEvent);
begin
FrameGridMemory.OnRunStop := AValue;
end;
procedure TFormDebug.FormFirstShow(Sender: TObject);
begin
RemoveHandlerFirstShow(@FormFirstShow);
AfterShow(2);
end;
function TFormDebug.GetDebugger: TDebugger;
begin
Result := FrameGridMemory.Debugger;
end;
procedure TFormDebug.AfterShow(Data: PtrInt);
begin
TCommonFunctionsLCL.AdjustFormPos(Self);
AutoSize := False;
if Data > 0 then begin
AutoSize := True;
Application.QueueAsyncCall(@AfterShow, Data - 1);
end else if Data = 0 then begin
Constraints.MinHeight := 0;
Panel10.BringToFront;
end;
end;
procedure TFormDebug.LabelBreakpointsOnClick(Sender: TObject);
begin
if FormBreakpoints = nil then begin
FormBreakpoints := TFrameBreakpoints.ShowFormBreakpoints(Self, GetDebugger, True, FrameGridMemory.Grid.Row - FrameGridMemory.Grid.FixedRows);
FormBreakpoints.AddHandlerOnBeforeDestruction(@FormBreakpointsBeforeDestruction);
end else
FormBreakpoints.BringToFront;
end;
procedure TFormDebug.FormBreakpointsBeforeDestruction(Sender: TObject);
begin
if Assigned(FormBreakpoints) then begin
FormBreakpoints.RemoveAllHandlersOfObject(Self);
FormBreakpoints := nil;
end;
end;
procedure TFormDebug.DoClose(var CloseAction: TCloseAction);
var
D: TDebugger;
begin
Application.RemoveAllHandlersOfObject(Self);
Application.RemoveAsyncCalls(Self);
D := GetDebugger;
if Assigned(D) then
D.RemoveOnBreakPointChangeHandlerOfObject(Self);
if Assigned(FSpectrum) then
FSpectrum.DettachFormDebug;
CloseAction := caFree;
inherited DoClose(CloseAction);
end;
procedure TFormDebug.SetSpectrum(ASpectrum: TSpectrum);
var
D: TDebugger;
begin
if ASpectrum <> FSpectrum then begin
D := nil;
if Assigned(FSpectrum) then
FSpectrum.DettachFormDebug;
if ASpectrum = nil then begin
SaveToJson();
end else if Assigned(ASpectrum.Memory) then begin
if ASpectrum.GetDebugger is TDebugger then begin
D := TDebugger(ASpectrum.GetDebugger);
end;
end;
FSpectrum := ASpectrum;
FrameGridMemory.Debugger := D;
if Assigned(D) then
LoadFromJson();
AfterStep;
end;
end;
procedure TFormDebug.OnStep(out DoContinue: Boolean);
begin
DoContinue := False;
if Assigned(FrameGridMemory) then begin
FrameGridMemory.OnStep(DoContinue);
end;
end;
procedure TFormDebug.AfterStep;
var
B: Byte;
begin
if Assigned(FrameGridMemory) then
if Assigned(FSpectrum) then begin
FrameGridMemory.Sp := FSpectrum.GetProcessor.RegSP;
FrameGridMemory.Pc := FSpectrum.GetProcessor.RegPC;
FDebugCPUState.SetAll(FSpectrum.GetProcessor);
LabelFramesPassed.Caption := FSpectrum.GetFrameCount.ToString;
LabelTicksInCurrentFrame.Caption := FSpectrum.GetProcessor.TStatesInCurrentFrame.ToString;
if Panel9.Enabled xor (FSpectrum.Is128KModel) then begin
Panel9.Enabled := FSpectrum.Is128KModel;
ScrDisplayCtrl.Enabled := FSpectrum.Is128KModel;
end;
LabelRomBanks.Caption := FSpectrum.Memory.ActiveRomPageNo.ToString;
LabelRamBanks.Caption := FSpectrum.Memory.ActiveRamPageNo.ToString;
if FSpectrum.Memory.ShadowScreenDisplay then
B := 1
else
B := 0;
ScrDisplayCtrl.SetValues(B);
FrameGridMemory.AfterStep;
end;
end;
procedure TFormDebug.SetActive(const AActive: Boolean);
begin
if Panel3.Enabled xor AActive then begin
Panel3.Enabled := AActive;
FrameGridMemory.IsActive := AActive;
AfterStep;
end;
end;
function TFormDebug.GetActive(): Boolean;
begin
Result := Panel3.Enabled;
end;
end.