-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDebugUnit.pas
268 lines (238 loc) · 7.37 KB
/
DebugUnit.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
unit DebugUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls,
SerialUnit, DebugDisplayUnit, DebuggerUnit;
type
TDebugForm = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ResetDisplays;
procedure CloseDisplays;
procedure ResizeDisplay;
procedure CloseLogFile;
procedure ChrIn(x: byte);
procedure NewChr(x: byte);
procedure NewLine;
end;
var
DebugForm : TDebugForm;
LogFile : TextFile;
LogFileOpen : boolean;
LogFileSize : integer;
DebuggerForm : array[0..7] of TDebuggerForm;
DebuggerEna : integer; // 8 bitwise enables
DisplayForm : array[0..31] of TDebugDisplayForm;
DisplayStrFlag : boolean;
DisplayStrLen : integer;
LineChrs : array[0..4095] of byte;
SrcRect : TRect;
DstRect : TRect;
TextLeft : integer;
TextTop : integer;
Cols : integer;
Col : integer;
implementation
uses GlobalUnit;
{$R *.dfm}
//////////////////////
// Event Routines //
//////////////////////
procedure TDebugForm.FormCreate(Sender: TObject);
var i: integer;
begin
// Hide the 'minimize' button
i := GetWindowLongA(Self.Handle, GWL_STYLE);
i := SetWindowLongA(Self.Handle, GWL_STYLE, i and not WS_MINIMIZEBOX);
// Set initial size
Left := Screen.Width div 4;
Top := Screen.Height * 3 div 4;
Width := Screen.Width div 2;
Height := Screen.Height * 3 div 16;
// Disable debugger
DebuggerEna := 0;
LastDebugTick := 0;
RequestCOGBRK := 0;
end;
procedure TDebugForm.FormResize(Sender: TObject);
begin
ResizeDisplay;
end;
procedure TDebugForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_ESCAPE then Close;
end;
procedure TDebugForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
CloseDisplays;
CloseLogFile;
DebugActive := False;
end;
////////////////////////
// Display Routines //
////////////////////////
// Reset display variables
procedure TDebugForm.ResetDisplays;
begin
// Close any open displays and reset state
CloseDisplays;
P2ResetDebugSymbols;
DisplayStrFlag := False;
// Set window position and size
if P2.DebugLeft >= 0 then Left := P2.DebugLeft;
if P2.DebugTop >= 0 then Top := P2.DebugTop;
if P2.DebugWidth >= 0 then Width := P2.DebugWidth;
if P2.DebugHeight >= 0 then Height := P2.DebugHeight;
ResizeDisplay;
// Start log file?
if P2.DebugLogSize > 0 then
begin
CloseLogFile;
AssignFile(LogFile, 'DEBUG.log');
ReWrite(LogFile);
LogFileOpen := True;
LogFileSize := 0;
end;
Show;
end;
// Close any open display/debugger windows
procedure TDebugForm.CloseDisplays;
var i: integer;
begin
// Close any open displays
for i := 0 to 31 do if P2.DebugDisplayEna shr i and 1 = 1 then DisplayForm[i].Free;
P2.DebugDisplayEna := 0;
// Close any open debuggers
for i := 0 to 7 do if DebuggerEna shr i and 1 = 1 then DebuggerForm[i].Free;
DebuggerEna := 0;
end;
// Resize display
procedure TDebugForm.ResizeDisplay;
var
ChrWidth, ChrHeight, ColIndent, RowIndent, Rows: integer;
begin
// Set font properties
Canvas.Font.Name := FontName;
Canvas.Font.Size := FontSize;
Canvas.Font.Color := clLime;
Canvas.Font.Style := [];
Canvas.Brush.Color := clBlack;
// Get text metrics
ChrWidth := Canvas.TextWidth('X');
ChrHeight := Canvas.TextHeight('X');
// Compute column and row indents
ColIndent := ChrWidth shr 1;
RowIndent := ChrHeight shr 2;
// Compute columns and rows
Cols := (ClientWidth - ColIndent) div ChrWidth;
Rows := (ClientHeight - RowIndent * 2) div ChrHeight;
MinLimit(Cols, 0);
MinLimit(Rows, 0);
// Clear display
Canvas.FillRect(Rect(0, 0, ClientWidth, ClientHeight));
// Make source and destination rectangles for scrolling
DstRect := Rect(0, RowIndent, ClientWidth, RowIndent + (Rows - 1) * ChrHeight);
SrcRect := Rect(0, RowIndent + ChrHeight, ClientWidth, RowIndent + Rows * ChrHeight);
// Make text output coordinates
TextLeft := ColIndent;
TextTop := DstRect.Bottom;
// Reset column
Col := 0;
end;
procedure TDebugForm.CloseLogFile;
begin
if LogFileOpen then CloseFile(LogFile);
LogFileOpen := False;
end;
// Receive character
procedure TDebugForm.ChrIn(x: byte);
var
i, j: integer;
begin
// start of debugger message?
if (x < 8) then
begin
ReturnRByte;
if DebuggerEna shr x and 1 = 0 then
begin
DebuggerID := x;
DebuggerForm[x] := TDebuggerForm.Create(Application);
DebuggerEna := DebuggerEna or 1 shl x;
end;
LastDebugTick := GetTickCount;
DebuggerForm[x].Breakpoint;
Exit;
end;
// start of display string?
if (x = $60) and not DisplayStrFlag then
begin
DisplayStrLen := 0;
DisplayStrFlag := True;
end
// body of display string?
else if DisplayStrFlag then
begin
if DisplayStrLen < DebugStringLimit then
begin
if x <> 13 then
begin
P2.DebugDisplayStr[DisplayStrLen] := x;
Inc(DisplayStrLen);
end
else
begin
P2.DebugDisplayStr[DisplayStrLen] := 0;
P2ParseDebugString;
DisplayStrFlag := False;
// start new debug display?
if P2.DebugDisplayType[0] = 1 then
begin
DisplayForm[P2.DebugDisplayNew] := TDebugDisplayForm.Create(Application);
SetFocus; // return focus to this form
end
else
// update existing debug display(s)?
if P2.DebugDisplayType[0] = 2 then
begin
for i := 0 to P2.DebugDisplayTargs - 1 do
begin
j := P2.DebugDisplayValue[i];
DisplayForm[j].UpdateDisplay(P2.DebugDisplayTargs);
if P2.DebugDisplayEna shr j and 1 = 0 then DisplayForm[j].Close; // free display if closed by command
end;
end;
end;
end
else if x = 13 then DisplayStrFlag := False;
end;
// Update window
if (x >= $20) and (x <= $7F) then NewChr(x)
else if x = 13 then NewLine
else if x = 9 then repeat NewChr($20) until (Col and 7) = 0;
// Update log file
if LogFileOpen then
begin
Write(LogFile, Chr(x));
Inc(LogFileSize);
if LogFileSize >= P2.DebugLogSize then CloseLogFile;
end;
end;
// Output new character to screen
procedure TDebugForm.NewChr(x: byte);
begin
LineChrs[Col] := x;
Col := Col + 1;
if Col = Cols then NewLine;
end;
// Output new line to screen
procedure TDebugForm.NewLine;
begin
FillChar(LineChrs[Col], Cols - Col + 1, $20); // fill remainder of line + 1 with spaces
LineChrs[Cols + 1] := 0; // ready to print one extra space to overwrite text dithering
Canvas.CopyRect(DstRect, Canvas, SrcRect); // scroll image on canvas
Canvas.TextOut(TextLeft, TextTop, PChar(@LineChrs)); // print new bottom line
Col := 0; // reset column
end;
end.