-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunitframegridmemory.pas
490 lines (424 loc) · 13 KB
/
unitframegridmemory.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
unit UnitFrameGridMemory;
// Copyright 2022-2025 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, UnitGridSpectrumMemory, UnitDisassembler, UnitDebugger,
Forms, Controls, ExtCtrls, StdCtrls, Buttons, fpjson;
type
TFrameGridMemory = class(TFrame)
BitBtn1: TBitBtn;
ButtonRunStop: TButton;
ButtonJumpToPC: TButton;
ButtonJumpToSP: TButton;
ButtonJumpGo: TButton;
ButtonStep: TButton;
CheckBoxDisplayRelativeJumpOffsetAsAbsolute: TCheckBox;
CheckBoxFollowPC: TCheckBox;
ComboBoxNumBase: TComboBox;
ComboBoxHexFormat: TComboBox;
EditAddr: TEdit;
ImageList1: TImageList;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
PanelOptions: TPanel;
PanelNumBase: TPanel;
PanelHexFormat: TPanel;
procedure BitBtn1Click(Sender: TObject);
procedure ButtonJumpGoClick(Sender: TObject);
procedure ButtonJumpToPCClick(Sender: TObject);
procedure ButtonJumpToSPClick(Sender: TObject);
procedure ButtonStepClick(Sender: TObject);
private
const
cSectionHexFormat: RawByteString = 'hex_format';
cSectionNumBase: RawByteString = 'num_base';
cSectionDisplayRelativeJumpOffsetAsAbsolute: RawByteString =
'display_relative_jump_offset_as_absolute';
cCheckBoxFollowPCHint: RawByteString =
'When stepping through debugger, the focus follows the programme counter'
;
cPanelNumBaseHint: RawByteString =
'Numeric base in assembly instructions — hex, dec or bin.'
+ LineEnding + 'Note:'
+ LineEnding + ' This setting does not affect relative offset (signed 8-bit) numbers, such as d in ...(IX+d),'
+ LineEnding + ' as well as in JR d (the later unless "show relative jumps as absolute" option is chosen).'
+ LineEnding + ' These are always displayed as signed decimal.'
;
cPanelHexFormatHint: RawByteString =
'Hex number format in assembly instructions:'
+ LineEnding + 'You can choose among prefix%s or suffix%s.'
+ LineEnding + 'This makes difference only when hex num. format is chosen.'
;
cCheckBoxDisplayRelativeJumpOffsetAsAbsoluteHint: RawByteString =
'Relative offsets in JR and DJNZ instructions are displayed'
+ LineEnding + 'as absolute addresses of the destination.'
;
cJumpToAddrHint: RawByteString = 'Jump to address'
+ LineEnding + 'To input address in hexadecimal, use $ as prefix.'
;
cJumpToPC: RawByteString = 'Jump to programme counter';
cJumpToSP: RawByteString = 'Jump to stack pointer';
cStartHint: RawByteString = 'Continue running';
cStopHint: RawByteString = 'Stop running';
cStepHint: RawByteString = 'Step to next instruction';
private
FGrid: TSpectrumMemoryGrid;
FOneStep: Boolean;
EditAddrPrevValue: String;
function GetActive: Boolean;
function GetDisassembler: TDisassembler;
function GetDebugger: TDebugger;
function GetPc: Word;
function GetSp: Word;
procedure SetActive(AValue: Boolean);
procedure SetDebugger(const AValue: TDebugger);
procedure SetOnRunStop(AValue: TNotifyEvent);
procedure SetPc(const AValue: Word);
procedure SetSp(const AValue: Word);
procedure CheckBoxFollowPcOnChange(Sender: TObject);
procedure DisassemblerDisplayChange(Sender: TObject);
procedure UpdateValuesFromDisassembler;
function ValidateEditAddr: Integer;
procedure ValidateEditAddrAndJumpTo();
procedure EditAddrEditingDone(Sender: TObject);
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure OnStep(out DoContinue: Boolean);
procedure AfterStep;
function SaveToJSON(out JSONObj: TJSONObject): Boolean;
procedure LoadFromJSON(const JSONObj: TJSONObject);
property Pc: Word read GetPc write SetPc;
property Sp: Word read GetSp write SetSp;
property Debugger: TDebugger read GetDebugger write SetDebugger;
property Grid: TSpectrumMemoryGrid read FGrid;
property IsActive: Boolean read GetActive write SetActive;
property OnRunStop: TNotifyEvent write SetOnRunStop;
end;
implementation
{$R *.lfm}
{ TFrameGridMemory }
procedure TFrameGridMemory.ButtonJumpToPCClick(Sender: TObject);
begin
FGrid.JumpTo(FGrid.Pc);
end;
procedure TFrameGridMemory.ButtonJumpGoClick(Sender: TObject);
begin
ValidateEditAddrAndJumpTo();
end;
procedure TFrameGridMemory.BitBtn1Click(Sender: TObject);
begin
PanelOptions.Visible := not PanelOptions.Visible;
if PanelOptions.Visible then
BitBtn1.ImageIndex := 1
else
BitBtn1.ImageIndex := 0;
end;
procedure TFrameGridMemory.ButtonJumpToSPClick(Sender: TObject);
begin
FGrid.JumpTo(FGrid.Sp);
end;
procedure TFrameGridMemory.ButtonStepClick(Sender: TObject);
begin
FOneStep := True;
end;
function TFrameGridMemory.GetDisassembler: TDisassembler;
var
D: TDebugger;
begin
D := GetDebugger;
if Assigned(D) then
Result := D.Disassembler
else
Result := nil;
end;
function TFrameGridMemory.GetActive: Boolean;
begin
Result := FGrid.Enabled;
end;
function TFrameGridMemory.GetDebugger: TDebugger;
begin
if Assigned(FGrid) then
Result := FGrid.Debugger
else
Result := nil;
end;
function TFrameGridMemory.GetPc: Word;
begin
Result := FGrid.Pc;
end;
function TFrameGridMemory.GetSp: Word;
begin
Result := FGrid.Sp;
end;
procedure TFrameGridMemory.SetActive(AValue: Boolean);
begin
if AValue xor GetActive then begin
PanelOptions.Enabled := AValue;
Panel1.Enabled := AValue;
if AValue then begin
ButtonRunStop.Caption := 'Run';
ButtonRunStop.Hint := cStartHint;
end else begin
ButtonRunStop.Caption := 'Stop';
ButtonRunStop.Hint := cStopHint;
end;
FGrid.Enabled := AValue;
end;
end;
procedure TFrameGridMemory.SetDebugger(const AValue: TDebugger);
begin
if Assigned(FGrid) then begin
FGrid.Debugger := AValue;
UpdateValuesFromDisassembler;
end;
end;
procedure TFrameGridMemory.SetOnRunStop(AValue: TNotifyEvent);
begin
ButtonRunStop.OnClick := AValue;
end;
procedure TFrameGridMemory.SetPc(const AValue: Word);
begin
FGrid.Pc := AValue;
end;
procedure TFrameGridMemory.SetSp(const AValue: Word);
begin
FGrid.Sp := AValue;
end;
procedure TFrameGridMemory.CheckBoxFollowPcOnChange(Sender: TObject);
begin
FGrid.FollowPc := CheckBoxFollowPC.Checked;
end;
procedure TFrameGridMemory.DisassemblerDisplayChange(Sender: TObject);
var
D: TDisassembler;
begin
D := GetDisassembler;
if Assigned(D) then begin
D.NumDisplay := TDisassembler.TNumDisplay(ComboBoxNumBase.ItemIndex);
D.HexFormatIndex := ComboBoxHexFormat.ItemIndex;
D.DisplayRelativeJumpOffsetAsAbsolute :=
CheckBoxDisplayRelativeJumpOffsetAsAbsolute.Checked;
FGrid.Invalidate;
end;
end;
procedure TFrameGridMemory.UpdateValuesFromDisassembler;
var
D: TDisassembler;
begin
D := GetDisassembler;
if Assigned(D) then begin
ValidateEditAddr;
ComboBoxNumBase.ItemIndex := Ord(D.NumDisplay);
ComboBoxHexFormat.ItemIndex := D.HexFormatIndex;
CheckBoxDisplayRelativeJumpOffsetAsAbsolute.OnChange := nil;
CheckBoxFollowPC.OnChange := nil;
CheckBoxDisplayRelativeJumpOffsetAsAbsolute.Checked := D.DisplayRelativeJumpOffsetAsAbsolute;
CheckBoxFollowPC.Checked := FGrid.FollowPc;
CheckBoxDisplayRelativeJumpOffsetAsAbsolute.OnChange := @DisassemblerDisplayChange;
CheckBoxFollowPC.OnChange := @CheckBoxFollowPcOnChange;
end;
end;
function TFrameGridMemory.ValidateEditAddr: Integer;
function ValidateAddr(const S: AnsiString): Integer;
var
C: AnsiChar;
begin
C := PAnsiChar(S)^;
if (C in ['%', '&']) // don't allow binary or octal
or (not TryStrToInt(S, Result)) or (Result < 0) // allow decimal or hex
or (Result >= FGrid.RowCount - FGrid.FixedRows)
then
Result := -1;
end;
var
S: AnsiString;
begin
S := Trim(EditAddr.Text);
Result := ValidateAddr(S);
if Result < 0 then begin
if ValidateAddr(EditAddrPrevValue) < 0 then
S := '0'
else
S := EditAddrPrevValue;
end;
EditAddr.Text := S;
end;
procedure TFrameGridMemory.ValidateEditAddrAndJumpTo();
var
N: Integer;
begin
N := ValidateEditAddr;
if N >= 0 then begin
FGrid.JumpTo(N);
EditAddrPrevValue := EditAddr.Text;
end;
end;
procedure TFrameGridMemory.EditAddrEditingDone(Sender: TObject);
begin
if EditAddr.Focused then
ValidateEditAddrAndJumpTo();
end;
constructor TFrameGridMemory.Create(TheOwner: TComponent);
var
Nd: TDisassembler.TNumDisplay;
I: Integer;
S, S1, S2: RawByteString;
begin
inherited Create(TheOwner);
SetOnRunStop(nil);
FOneStep := False;
EditAddrPrevValue := #0;
EditAddr.Text := EditAddrPrevValue;
Panel7.BevelOuter := bvNone;
Panel6.BevelOuter := bvNone;
Panel1.Caption := '';
Panel1.BevelOuter := bvNone;
Panel2.Caption := '';
Panel2.BevelOuter := bvNone;
PanelOptions.Caption := '';
PanelOptions.BevelOuter := bvNone;
FGrid := TSpectrumMemoryGrid.Create(nil);
FGrid.AnchorParallel(akTop, 0, Panel2);
FGrid.AnchorParallel(akLeft, 0, Panel2);
FGrid.AnchorParallel(akRight, 0, Panel2);
FGrid.AnchorParallel(akBottom, 0, Panel2);
Panel1.AutoSize := True;
CheckBoxFollowPC.Checked := FGrid.FollowPc;
CheckBoxFollowPC.OnChange := @CheckBoxFollowPcOnChange;
ComboBoxNumBase.Items.Clear;
for Nd := Low(TDisassembler.TNumDisplay) to High(TDisassembler.TNumDisplay) do begin
WriteStr(S, Nd);
S := LowerCase(Copy(S, 3));
ComboBoxNumBase.Items.Add(S);
end;
ComboBoxHexFormat.Items.Clear;
S1 := '';
for I := Low(TDisassembler.AllowedHexPrefixes) to High(TDisassembler.AllowedHexPrefixes) do begin
S := Lowercase(Trim(TDisassembler.AllowedHexPrefixes[I]));
ComboBoxHexFormat.Items.Add(S + '...');
if I > 0 then begin
if I = 1 then
S1 := 'es' + S1;
S1 := S1 + ',';
end;
S1 := S1 + ' "' + S + '"';
end;
S2 := '';
for I := Low(TDisassembler.AllowedHexSuffixes) to High(TDisassembler.AllowedHexSuffixes) do begin
S := Lowercase(Trim(TDisassembler.AllowedHexSuffixes[I]));
ComboBoxHexFormat.Items.Add('...' + S);
if I > 0 then begin
if I = 1 then
S2 := 'es' + S2;
S2 := S2 + ',';
end;
S2 := S2 + ' "' + S + '"';
end;
PanelNumBase.Hint := cPanelNumBaseHint;
PanelHexFormat.Hint := Format(cPanelHexFormatHint, [S1, S2]);
CheckBoxDisplayRelativeJumpOffsetAsAbsolute.Hint :=
cCheckBoxDisplayRelativeJumpOffsetAsAbsoluteHint;
CheckBoxFollowPC.Hint := cCheckBoxFollowPCHint;
ButtonStep.Hint := cStepHint;
ButtonJumpGo.Hint := cJumpToAddrHint;
EditAddr.Hint := cJumpToAddrHint;
ButtonJumpToPC.Hint := cJumpToPC;
ButtonJumpToSP.Hint := cJumpToSP;
Panel5.ShowHint := True;
ComboBoxNumBase.OnChange := @DisassemblerDisplayChange;
ComboBoxHexFormat.OnChange := @DisassemblerDisplayChange;
BorderStyle := bsNone;
FGrid.Enabled := False;
SetActive(True);
FGrid.Parent := Panel2;
ButtonRunStop.Constraints.MinWidth := ButtonRunStop.Width;
EditAddr.OnEditingDone := @EditAddrEditingDone;
end;
destructor TFrameGridMemory.Destroy;
begin
FGrid.Free;
inherited Destroy;
end;
procedure TFrameGridMemory.OnStep(out DoContinue: Boolean);
begin
DoContinue := FOneStep;
FOneStep := False;
end;
procedure TFrameGridMemory.AfterStep;
begin
if Assigned(FGrid) then begin
FGrid.Invalidate;
end;
end;
function TFrameGridMemory.SaveToJSON(out JSONObj: TJSONObject): Boolean;
var
N: Integer;
S: RawByteString;
D: TDisassembler;
JObj: TJSONObject;
begin
Result := False;
JSONObj := nil;
JObj := TJSONObject.Create;
try
D := GetDisassembler;
if Assigned(D) then begin
WriteStr(S, D.NumDisplay);
S := LowerCase(Copy(S, 3));
JObj.Add(cSectionNumBase, S);
JObj.Add(cSectionHexFormat, D.Hexfix);
if D.DisplayRelativeJumpOffsetAsAbsolute then
N := 1
else
N := 0;
JObj.Add(cSectionDisplayRelativeJumpOffsetAsAbsolute, N);
end;
if JObj.Count > 0 then begin
JSONObj := JObj;
JObj := nil;
Result := True;
end;
finally
JObj.Free;
end;
end;
procedure TFrameGridMemory.LoadFromJSON(const JSONObj: TJSONObject);
var
S, S1: RawByteString;
Nd: TDisassembler.TNumDisplay;
D: TDisassembler;
begin
if Assigned(JSONObj) then begin
D := GetDisassembler;
if Assigned(D) then begin
S := '';
S := JSONObj.Get(cSectionNumBase, S);
for Nd := Low(TDisassembler.TNumDisplay) to High(TDisassembler.TNumDisplay) do begin
WriteStr(S1, Nd);
if CompareText(Copy(S1, 3), S) = 0 then begin
D.NumDisplay := Nd;
Break;
end;
end;
S := '';
S := JSONObj.Get(cSectionHexFormat, S);
D.HexFormatIndex := TDisassembler.DecodeHexFormatIndex(S);
D.DisplayRelativeJumpOffsetAsAbsolute := JSONObj.Get(cSectionDisplayRelativeJumpOffsetAsAbsolute, Integer(0)) <> 0;
UpdateValuesFromDisassembler;
end;
end;
end;
end.