-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.asm
More file actions
431 lines (370 loc) · 12.2 KB
/
Debug.asm
File metadata and controls
431 lines (370 loc) · 12.2 KB
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
;/------------------------------------------------------------------------------------------\
;| Debug System |
;|------------------------------------------------------------------------------------------|
;| PUBLIC FUNCTIONS: |
;| DEBUG_Display - Switch over to a mode 3 debug screen |
;| DEBUG_ClearScreen - Clear the screen and reset cursor to top left |
;| DEBUG_PrintChar - Prints the character in A |
;| DEBUG_PrintMessage - Prints a debug message on the screen |
;| DEBUG_PrintHexDEHL - print a 32bit number in DE:HL as hex |
;| DEBUG_PrintHexHL - Prints the number in HL as hex |
;| DEBUG_PrintHexA - Prints the number in A as hex |
;| DEBUG_PrintA_0 - Prints the decimal value in A with leading 0's |
;| DEBUG_PrintA - Prints the decimal value in A without leading 0's |
;| DEBUG_PrintL - Prints the decimal value in L |
;| DEBUG_PrintHL - Prints the decimal value in HL |
;| DEBUG_PrintDEHL - Print the number in DE:HL as decimal |
;\------------------------------------------------------------------------------------------/
;--------------------------------------------------------------------------------------------
; Setup the debug display
;--------------------------------------------------------------------------------------------
; INPUT:
; None
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_Display:
; Get the address to return to
ld de, @DD_Return
ld (@DD_Return + 1), sp
; Get current HMPR
in a, (IO_HMPR)
; Now we want to push our code onto stack "POP AF, OUT (IO_HMPR), A, RET"
ld bc, &c9fb
push bc
ld bc, &d3f1
push bc
; Get the current SP
ld hl, 0
add hl, sp
; Now push the return address, the data and the function address
push de
push af
push hl
; Finally ask it do
ld a, 2
jp SYSCALL_JMODE
@DD_Return:
ld sp, 0
; Turn off the screen prompt
ld a, 1
ld (SVAR_SPROMPT), a
; Clear the screen
call DEBUG_ClearScreen
ret
;--------------------------------------------------------------------------------------------
; Clears the screen and then positions the cursor at he top left
;--------------------------------------------------------------------------------------------
; INPUT:
; None
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_ClearScreen:
; Make sure the screen is clear
xor a
call SYSCALL_JCLSBL
; Set the stream
ld a, 2
call SYSCALL_JSETSTRM
; Move the cursor
call DEBUG_PrintMessage
db 22,0,128
ret
;--------------------------------------------------------------------------------------------
; Prints the character in A - Seems pointless, but this could be updated to print in other ways
;--------------------------------------------------------------------------------------------
; INPUT:
; A - The value to print
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintChar:
push af
rst &10
pop af
ret
;--------------------------------------------------------------------------------------------
; Prints a debug message
;--------------------------------------------------------------------------------------------
; INPUT:
; Message - Data following function will be printed until a byte with bit 7 is found
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintMessage:
; Get the return address as thats where the data is
pop hl
@DPM_Loop:
ld a, (hl)
inc hl
rlca
srl a
call DEBUG_PrintChar
jr nc, @DPM_Loop
; Return
jp (hl)
;--------------------------------------------------------------------------------------------
; Print a 32 bit hex number
;--------------------------------------------------------------------------------------------
; INPUT:
; DE:HL - The 32Bit number to print in hex
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintHexDEHL:
push de
; First print DE
push hl
ex de, hl
call DEBUG_PrintHexHL
; Now print HL
pop hl
call DEBUG_PrintHexHL
pop de
ret
;--------------------------------------------------------------------------------------------
; Prints a HL as hex
;--------------------------------------------------------------------------------------------
; INPUT:
; HL - Number to print
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintHexHL:
push hl
; Just print each part
ld a, h
call DEBUG_PrintHexA
ld a, l
call DEBUG_PrintHexA
pop hl
ret
;--------------------------------------------------------------------------------------------
; Prints a A as hex
;--------------------------------------------------------------------------------------------
; INPUT:
; A - Number to print
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintHexA:
push af
; Get the top digit
rrca
rrca
rrca
rrca
; Print it
call _DEBUG_PrintDigit
; Now do the bottom digit
pop af
call _DEBUG_PrintDigit
ret
;--------------------------------------------------------------------------------------------
; Prints a nunmber as decimal with leading 0's
;--------------------------------------------------------------------------------------------
; INPUT:
; A - 8 bit number to print as decimal
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintA_0:
; Setup the values
ld l, a
ld a, "0"
; Call the main print
jp DEBUG_PrintL
;--------------------------------------------------------------------------------------------
; Prints a nunmber as decimal with no leading zeros
;--------------------------------------------------------------------------------------------
; INPUT:
; A - 8 bit number to print as decimal
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintA:
; Setup the values
ld l, a
xor a
; Call the main print
jp DEBUG_PrintL
;--------------------------------------------------------------------------------------------
; Prints a nunmber as decimal
;--------------------------------------------------------------------------------------------
; INPUT:
; L - 8 bit number to print as decimal
; A - 0 for no leading digits, else the character to display (usually 0 or space)
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintL:
; Clear the higher bit of a 32 bit number
exx
ld hl, &0000
exx
ld h, 0
; Call into the main number print
jp _DEBUG_Print8Bit
;--------------------------------------------------------------------------------------------
; Prints a nunmber as decimal
;--------------------------------------------------------------------------------------------
; INPUT:
; HL - 16 bit number to print as decimal
; A - 0 for no leading digits, else the character to display (usually 0 or space)
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintHL:
; Clear the higher bit of a 32 bit number
exx
ld hl, &0000
exx
; Call into the main number print
jp _DEBUG_Print16Bit
;--------------------------------------------------------------------------------------------
; Prints a nunmber as decimal
;--------------------------------------------------------------------------------------------
; INPUT:
; DE:HL - 32 bit number to print as decimal
; A - 0 for no leading digits, else the character to display (usually 0 or space)
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
DEBUG_PrintDEHL:
; Setup for print (largest number is FFFFFFFF which is 4294967295 in decimal - max 10 digits)
push de
exx
pop hl
ld de, &3b9a ; 1,000,000,000 / 65536
exx
ld de, &ca00 ; 1,000,000,000 % 65536
call _DEBUG_CalcDigit
; Now 100,000,000
exx
ld de, &05f5 ; 100,000,000 % 65536
exx
ld de, &e100 ; 100,000,000 / 65536
call _DEBUG_CalcDigit
; 10,000,000
exx
ld de, &0098 ; 10,000,000 % 65536
exx
ld de, &9680 ; 10,000,000 / 65536
call _DEBUG_CalcDigit
; 1,000,000
exx
ld de, &000f ; 1,000,000 % 65536
exx
ld de, &4240 ; 1,000,000 / 65536
call _DEBUG_CalcDigit
; 100,000
exx
ld de, &0001 ; 100,000 % 65536
exx
ld de, &86a0 ; 100,000 / 65536
call _DEBUG_CalcDigit
_DEBUG_Print16Bit:
; 10,000
exx
ld de, &0000 ; 10,000 % 65536
exx
ld de, &2710 ; 10,000 / 65536
call _DEBUG_CalcDigit
; 1,000
exx
ld de, &0000 ; 1,000 % 65536
exx
ld de, &03e8 ; 1,000 / 65536
call _DEBUG_CalcDigit
_DEBUG_Print8Bit:
; 100
exx
ld de, &0000 ; 100 % 65536
exx
ld de, &0064 ; 100 / 65536
call _DEBUG_CalcDigit
; 10
exx
ld de, &0000 ; 10 % 65536
exx
ld de, &000a ; 10 / 65536
call _DEBUG_CalcDigit
; Print the final digit
ld a, l
add "0"
call DEBUG_PrintChar
ret
_DEBUG_CalcDigit:
; Remember the leading character
ex af, af'
; 32bit subtraction
xor a
@DCD_Loop:
sbc hl, de
exx
sbc hl, de
exx
jr c, @DCD_GotCount
inc a
jr @DCD_Loop
@DCD_GotCount:
; Do an increase so we get the quotient back
add hl, de
exx
adc hl, de
exx
; We now have the value in to display
and a
jr nz, @DCD_NotZero
; Now, if this is a leading 0, we want to display the leading character (maybe nothing), after
; we have displayed a non-zero character this will always be "0"
ex af, af'
and a
ret z
jp @DCD_DoPrint
@DCD_NotZero:
; We can just display the digit
add "0"
call @DCD_DoPrint
; As this was a digit, all future 0's will print as "0"
ld a, "0"
ret
@DCD_DoPrint:
; Store HL'
exx
push hl
exx
; Print the character
call DEBUG_PrintChar
; Restore HL'
exx
pop hl
exx
ret
;--------------------------------------------------------------------------------------------
; INTERNAL: Prints the digit in A, will print in hex so can be used for decimal as well
;--------------------------------------------------------------------------------------------
; INPUT:
; A - Number to print
; OUTPUT:
; None
;--------------------------------------------------------------------------------------------
_DEBUG_PrintDigit:
push af
; Mask off the top digits
and &0f
; Is it a lette or digit
cp 10
jr nc, @DPD_Letter
@DPD_Digit:
add "0" - ("A" - 10)
@DPD_Letter:
add "A" - 10
call DEBUG_PrintChar
pop af
ret
;--------------------------------------------------------------------------------------------
; Variables
;--------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------