-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDebugger.Macros.AS.asm
657 lines (564 loc) · 17 KB
/
Debugger.Macros.AS.asm
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
; ---------------------------------------------------------------
; WARNING! This disables automatic padding in order to combine DC.B's correctly
; Make sure your code doesn't rely on padding (enabled by default)!
; ---------------------------------------------------------------
padding off
supmode on ; bypass warnings on privileged instructions
; ---------------------------------------------------------------
; Creates assertions for debugging
; ---------------------------------------------------------------
; EXAMPLES:
; assert.b d0, eq, #1 ; d0 must be $01, or else crash
; assert.w d5, pl ; d5 must be positive
; assert.l a1, hi, a0 ; assert a1 > a0, or else crash
; assert.b (MemFlag).w, ne ; MemFlag must be set (non-zero)
; assert.l a0, eq, #Obj_Player, MyObjectsDebugger
;
; NOTICE:
; All "assert" saves and restores CCR so it's fully safe
; to use in-between any instructions.
; Use "_assert" instead if you deliberatly want to disbale
; this behavior and safe a few cycles.
; ---------------------------------------------------------------
assert: macro src, cond, dest, consoleprogram
#ifndef MD-SHELL
; Assertions only work in DEBUG builds
ifdef __DEBUG__
#endif
move.w sr, -(sp)
_assert.ATTRIBUTE src, cond, dest, consoleprogram
move.w (sp)+, sr
#ifndef MD-SHELL
endif
#endif
endm
; Same as "assert", but doesn't save/restore CCR (can be used to save a few cycles)
_assert: macro src, cond, dest, consoleprogram
#ifndef MD-SHELL
; Assertions only work in DEBUG builds
ifdef __DEBUG__
#endif
if "dest"<>""
cmp.ATTRIBUTE dest, src
else
tst.ATTRIBUTE src
endif
switch lowstring("cond")
case "eq"
beq .skip
case "ne"
bne .skip
case "cs"
bcs .skip
case "cc"
bcc .skip
case "pl"
bpl .skip
case "mi"
bmi .skip
case "hi"
bhi .skip
case "hs"
bhs .skip
case "ls"
bls .skip
case "lo"
blo .skip
case "gt"
bgt .skip
case "ge"
bge .skip
case "le"
ble .skip
case "lt"
blt .skip
case "vs"
bvs .skip
case "vc"
bvc .skip
elsecase
!error "Unknown condition cond"
endcase
if "dest"<>""
RaiseError "Assertion failed:%<endl>%<pal2>> assert.ATTRIBUTE %<pal0>src,%<pal2>cond%<pal0>,dest%<endl>%<pal1>Got: %<.ATTRIBUTE src>", consoleprogram
else
RaiseError "Assertion failed:%<endl>%<pal2>> assert.ATTRIBUTE %<pal0>src,%<pal2>cond%%<endl>%<pal1>Got: %<.ATTRIBUTE src>", consoleprogram
endif
.skip:
#ifndef MD-SHELL
endif
#endif
endm
; ---------------------------------------------------------------
; Raises an error with the given message
; ---------------------------------------------------------------
; EXAMPLES:
; RaiseError "Something is wrong"
; RaiseError "Your D0 value is BAD: %<.w d0>"
; RaiseError "Module crashed! Extra info:", YourMod_Debugger
; ---------------------------------------------------------------
RaiseError: macro string, consoleprogram, opts
pea *(pc)
move.w sr, -(sp)
__FSTRING_GenerateArgumentsCode string
jsr MDDBG__ErrorHandler
__FSTRING_GenerateDecodedString string
if ("consoleprogram"<>"") ; if console program offset is specified ...
.__align_flag: set ((((*)&1)!1)*_eh_align_offset)
if "opts"<>""
dc.b opts+_eh_enter_console|.__align_flag ; add flag "_eh_align_offset" if the next byte is at odd offset ...
else
dc.b _eh_enter_console|.__align_flag ; ''
endif
!align 2 ; ... to tell Error handler to skip this byte, so it'll jump to ...
if DEBUGGER__EXTENSIONS__ENABLE
jsr consoleprogram ; ... an aligned "jsr" instruction that calls console program itself
jmp MDDBG__ErrorHandler_PagesController
else
jmp consoleprogram ; ... an aligned "jmp" instruction that calls console program itself
endif
else
if DEBUGGER__EXTENSIONS__ENABLE
.__align_flag: set ((((*)&1)!1)*_eh_align_offset)
if "opts"<>""
dc.b opts+_eh_return|.__align_flag ; add flag "_eh_align_offset" if the next byte is at odd offset ...
else
dc.b _eh_return|.__align_flag ; add flag "_eh_align_offset" if the next byte is at odd offset ...
endif
!align 2 ; ... to tell Error handler to skip this byte, so it'll jump to ...
jmp MDDBG__ErrorHandler_PagesController
else
dc.b opts+0 ; otherwise, just specify \opts for error handler, +0 will generate dc.b 0 ...
!align 2 ; ... in case \opts argument is empty or skipped
endif
endif
!align 2
endm
; ---------------------------------------------------------------
; Console interface
; ---------------------------------------------------------------
; EXAMPLES:
#ifndef MD-SHELL
; Console.Run YourConsoleProgram
#endif
; Console.Write "Hello "
; Console.WriteLine "...world!"
; Console.WriteLine "Your data is %<.b d0>"
; Console.WriteLine "%<pal0>Your code pointer: %<.l a0 sym>"
; Console.SetXY #1, #4
; Console.SetXY d0, d1
; Console.Sleep #60 ; sleep for 1 second
; Console.Pause
;
; NOTICE:
; All "Console.*" calls save and restore CCR so they are fully
; safe to use in-between any instructions.
; Use "_Console.*" instead if you deliberatly want to disbale
; this behavior and safe a few cycles.
; ---------------------------------------------------------------
Console: macro argument1, argument2
switch lowstring("ATTRIBUTE")
; "Console.Run" doesn't have to save/restore CCR, because it's a no-return
case "run"
_Console.ATTRIBUTE argument1, argument2
; Other Console calls do save/restore CCR
elsecase
move.w sr, -(sp)
_Console.ATTRIBUTE argument1, argument2
move.w (sp)+, sr
endcase
endm
; Same as "Console", but doesn't save/restore CCR (can be used to save a few cycles)
_Console: macro argument1, argument2
switch lowstring("ATTRIBUTE")
case "write"
__FSTRING_GenerateArgumentsCode argument1
; If we have any arguments in string, use formatted string function ...
if (.__sp>0)
movem.l a0-a2/d7, -(sp)
lea 4*4(sp), a2
lea .__data(pc), a1
jsr MDDBG__Console_Write_Formatted
movem.l (sp)+, a0-a2/d7
if (.__sp>8)
lea .__sp(sp), sp
elseif (.__sp>0)
addq.w #.__sp, sp
endif
; ... Otherwise, use direct write as an optimization
else
move.l a0, -(sp)
lea .__data(pc), a0
jsr MDDBG__Console_Write
move.l (sp)+, a0
endif
bra.w .__leave
.__data:
__FSTRING_GenerateDecodedString argument1
!align 2
.__leave:
case "writeline"
__FSTRING_GenerateArgumentsCode argument1
; If we have any arguments in string, use formatted string function ...
if (.__sp>0)
movem.l a0-a2/d7, -(sp)
lea 4*4(sp), a2
lea .__data(pc), a1
jsr MDDBG__Console_WriteLine_Formatted
movem.l (sp)+, a0-a2/d7
if (.__sp>8)
lea .__sp(sp), sp
elseif (.__sp>0)
addq.w #.__sp, sp
endif
; ... Otherwise, use direct write as an optimization
else
move.l a0, -(sp)
lea .__data(pc), a0
jsr MDDBG__Console_WriteLine
move.l (sp)+, a0
endif
bra.w .__leave
.__data:
__FSTRING_GenerateDecodedString argument1
!align 2
.__leave:
#ifndef MD-SHELL
case "run"
jsr MDDBG__ErrorHandler_ConsoleOnly
jsr argument1
bra.s *
#endif
case "clear"
jsr MDDBG__ErrorHandler_ClearConsole
case "pause"
jsr MDDBG__ErrorHandler_PauseConsole
case "sleep"
move.w d0, -(sp)
move.l a0, -(sp)
move.w argument1, d0
subq.w #1, d0
bcs.s .__sleep_done
.__sleep_loop:
jsr MDDBG__VSync
dbf d0, .__sleep_loop
.__sleep_done:
move.l (sp)+, a0
move.w (sp)+, d0
case "setxy"
movem.l d0-d1, -(sp)
move.w argument2, -(sp)
move.w argument1, -(sp)
jsr MDDBG__Console_SetPosAsXY_Stack
addq.w #4, sp
movem.l (sp)+, d0-d1
case "breakline"
jsr MDDBG__Console_StartNewLine
elsecase
!error "ATTRIBUTE isn't a member of Console"
endcase
endm
; ---------------------------------------------------------------
; KDebug integration interface
; ---------------------------------------------------------------
; EXAMPLES:
; KDebug.WriteLine "Look in your debug console!"
; KDebug.WriteLine "Your D0 is %<.w d0>"
; KDebug.BreakPoint
; KDebug.StartTimer
; KDebug.EndTimer
;
; NOTICE:
; All "KDebug.*" calls save and restore CCR so they are fully
; safe to use in-between any instructions.
; Use "_KDebug.*" instead if you deliberatly want to disbale
; this behavior and safe a few cycles.
; ---------------------------------------------------------------
KDebug: macro argument1
#ifndef MD-SHELL
ifdef __DEBUG__ ; KDebug interface is only available in DEBUG builds
#endif
move.w sr, -(sp)
_KDebug.ATTRIBUTE argument1
move.w (sp)+, sr
#ifndef MD-SHELL
endif
#endif
endm
; Same as "KDebug", but doesn't save/restore CCR (can be used to save a few cycles)
_KDebug macro argument1
#ifndef MD-SHELL
ifdef __DEBUG__ ; KDebug interface is only available in DEBUG builds
#endif
switch lowstring("ATTRIBUTE")
case "write"
__FSTRING_GenerateArgumentsCode argument1
; If we have any arguments in string, use formatted string function ...
if (.__sp>0)
movem.l a0-a2/d7, -(sp)
lea 4*4(sp), a2
lea .__data(pc), a1
jsr MDDBG__KDebug_Write_Formatted
movem.l (sp)+, a0-a2/d7
if (.__sp>8)
lea .__sp(sp), sp
elseif (.__sp>0)
addq.w #.__sp, sp
endif
; ... Otherwise, use direct write as an optimization
else
move.l a0, -(sp)
lea .__data(pc), a0
jsr MDDBG__KDebug_Write
move.l (sp)+, a0
endif
bra.w .__leave
.__data:
__FSTRING_GenerateDecodedString argument1
!align 2
.__leave:
case "writeline"
__FSTRING_GenerateArgumentsCode argument1
; If we have any arguments in string, use formatted string function ...
if (.__sp>0)
movem.l a0-a2/d7, -(sp)
lea 4*4(sp), a2
lea .__data(pc), a1
jsr MDDBG__KDebug_WriteLine_Formatted
movem.l (sp)+, a0-a2/d7
if (.__sp>8)
lea .__sp(sp), sp
elseif (.__sp>0)
addq.w #.__sp, sp
endif
; ... Otherwise, use direct write as an optimization
else
move.l a0, -(sp)
lea .__data(pc), a0
jsr MDDBG__KDebug_WriteLine
move.l (sp)+, a0
endif
bra.w .__leave
.__data:
__FSTRING_GenerateDecodedString argument1
!align 2
.__leave:
case "breakline"
jsr MDDBG__KDebug_FlushLine
case "starttimer"
move.w #$9FC0, ($C00004).l
case "endtimer"
move.w #$9F00, ($C00004).l
case "breakpoint"
move.w #$9D00, ($C00004).l
elsecase
!error "ATTRIBUTE isn't a member of KDebug"
endcase
#ifndef MD-SHELL
endif
#endif
endm
; ---------------------------------------------------------------
__ErrorMessage: macro string, opts
__FSTRING_GenerateArgumentsCode string
jsr MDDBG__ErrorHandler
__FSTRING_GenerateDecodedString string
if DEBUGGER__EXTENSIONS__ENABLE
.__align_flag: set (((*)&1)!1)*_eh_align_offset
dc.b (opts)+_eh_return|.__align_flag ; add flag "_eh_align_offset" if the next byte is at odd offset ...
!align 2 ; ... to tell Error handler to skip this byte, so it'll jump to ...
jmp MDDBG__ErrorHandler_PagesController ; ... extensions controller
else
dc.b (opts)+0
!align 2
endif
endm
; ---------------------------------------------------------------
; WARNING: Since AS cannot compile instructions out of strings
; we have to do lots of switch-case bullshit down here..
__FSTRING_PushArgument: macro OPERAND,DEST
.__operand: set OPERAND
.__dval: set 0
; If OPERAND starts with "#", simulate "#immediate" mode by splitting OPERAND string
if (substr(OPERAND, 0, 1)="#")
.__dval: set VAL(substr(OPERAND, 1, 0))
.__operand: set "#"
; If OPERAND ends with ".w", simulate "XXX.w" mode
elseif (substr(OPERAND, strlen(OPERAND)-2, 2)=".w")
.__dval: set VAL(substr(OPERAND, 0, strlen(OPERAND)-2))
.__operand: set "x.w"
; If OPERAND ends with ".l", simulate "XXX.l" mode
elseif (substr(OPERAND, strlen(OPERAND)-2, 2)=".l")
.__dval: set VAL(substr(OPERAND, 0, strlen(OPERAND)-2))
.__operand: set "x.l"
; If OPERAND ends with "(pc)", simulate "d16(pc)" mode by splitting OPERAND string
elseif (strlen(OPERAND)>4)&&(substr(OPERAND, strlen(OPERAND)-4, 4)="(pc)")
.__dval: set VAL(substr(OPERAND, 0, strlen(OPERAND)-4))
.__operand: set substr(OPERAND, strlen(OPERAND)-4, 0)
; If OPERAND ends with "(an)", simulate "d16(an)" mode by splitting OPERAND string
elseif (strlen(OPERAND)>4)&&(substr(OPERAND, strlen(OPERAND)-4, 2)="(a")&&(substr(OPERAND, strlen(OPERAND)-1, 1)=")")
.__dval: set VAL(substr(OPERAND, 0, strlen(OPERAND)-4))
.__operand: set substr(OPERAND, strlen(OPERAND)-4, 0)
endif
switch lowstring(.__operand)
case "d0"
move.ATTRIBUTE d0,DEST
case "d1"
move.ATTRIBUTE d1,DEST
case "d2"
move.ATTRIBUTE d2,DEST
case "d3"
move.ATTRIBUTE d3,DEST
case "d4"
move.ATTRIBUTE d4,DEST
case "d5"
move.ATTRIBUTE d5,DEST
case "d6"
move.ATTRIBUTE d6,DEST
case "d7"
move.ATTRIBUTE d7,DEST
case "a0"
move.ATTRIBUTE a0,DEST
case "a1"
move.ATTRIBUTE a1,DEST
case "a2"
move.ATTRIBUTE a2,DEST
case "a3"
move.ATTRIBUTE a3,DEST
case "a4"
move.ATTRIBUTE a4,DEST
case "a5"
move.ATTRIBUTE a5,DEST
case "a6"
move.ATTRIBUTE a6,DEST
case "(a0)"
move.ATTRIBUTE .__dval(a0),DEST
case "(a1)"
move.ATTRIBUTE .__dval(a1),DEST
case "(a2)"
move.ATTRIBUTE .__dval(a2),DEST
case "(a3)"
move.ATTRIBUTE .__dval(a3),DEST
case "(a4)"
move.ATTRIBUTE .__dval(a4),DEST
case "(a5)"
move.ATTRIBUTE .__dval(a5),DEST
case "(a6)"
move.ATTRIBUTE .__dval(a6),DEST
case "x.w"
move.ATTRIBUTE (.__dval).w,DEST
case "x.l"
move.ATTRIBUTE (.__dval).l,DEST
case "(pc)"
move.ATTRIBUTE .__dval(pc),DEST
case "#"
move.ATTRIBUTE #.__dval,DEST
elsecase
.__evaluated_operand: set VAL(OPERAND)
move.ATTRIBUTE .__evaluated_operand,DEST
endcase
endm
; ---------------------------------------------------------------
; WARNING! Incomplete!
__FSTRING_GenerateArgumentsCode: macro string
.__pos: set strstr(string,"%<") ; token position
.__sp: set 0 ; stack displacement
.__str: set string
; Parse string itself
while (.__pos>=0)
; Find the last occurance "%<" in the string
while ( strstr(substr(.__str,.__pos+2,0),"%<")>=0 )
.__pos: set strstr(substr(.__str,.__pos+2,0),"%<")+.__pos+2
endm
.__substr: set substr(.__str,.__pos,0)
; Retrive expression in brackets following % char
.__endpos: set strstr(.__substr,">")
if (.__endpos<0) ; Fix bizzare AS bug as stsstr() fails to check the last character of string
.__endpos: set strlen(.__substr)-1
endif
.__midpos: set strstr(substr(.__substr,5,0)," ")
if ((.__midpos<0)||(.__midpos+5>.__endpos))
.__midpos: set .__endpos
else
.__midpos: set .__midpos+5
endif
.__type: set substr(.__substr,2,2) ; .type
; Expression is an effective address (e.g. %(.w d0 hex) )
if ((strlen(.__type)==2)&&(substr(.__type,0,1)=="."))
.__operand: set substr(.__substr,5,.__midpos-5) ; ea
.__param: set substr(.__substr,.__midpos+1,.__endpos-.__midpos-1) ; param
if (.__type==".b")
subq.w #2, sp
__FSTRING_PushArgument.b .__operand,1(sp)
.__sp: set .__sp+2
elseif (.__type==".w")
__FSTRING_PushArgument.w .__operand,-(sp)
.__sp: set .__sp+2
elseif (.__type==".l")
__FSTRING_PushArgument.l .__operand,-(sp)
.__sp: set .__sp+4
else
error "Unrecognized type in string operand: \{.__type}"
endif
endif
; Cut string
if (.__pos>0)
.__str: set substr(.__str, 0, .__pos)
.__pos: set strstr(.__str,"%<")
else
.__pos: set -1
endif
endm
endm
; ---------------------------------------------------------------
__FSTRING_GenerateDecodedString: macro string
.__lpos: set 0 ; start position
.__pos: set strstr(string, "%<")
while (.__pos>=0)
; Write part of string before % token
if (.__pos-.__lpos>0)
dc.b substr(string, .__lpos, .__pos-.__lpos)
endif
; Retrive expression in brakets following % char
.__endpos: set strstr(substr(string,.__pos+1,0),">")+.__pos+1
if (.__endpos<=.__pos) ; Fix bizzare AS bug as stsstr() fails to check the last character of string
.__endpos: set strlen(string)-1
endif
.__midpos: set strstr(substr(string,.__pos+5,0)," ")+.__pos+5
if ((.__midpos<.__pos+5)||(.__midpos>.__endpos))
.__midpos: set .__endpos
endif
.__type: set substr(string,.__pos+1+1,2) ; .type
; Expression is an effective address (e.g. %<.w d0 hex> )
if ((strlen(.__type)==2)&&(substr(.__type,0,1)=="."))
.__param: set substr(string,.__midpos+1,.__endpos-.__midpos-1) ; param
; Validate format setting ("param")
if (strlen(.__param)<1)
.__param: set "hex" ; if param is ommited, set it to "hex"
elseif (.__param=="signed")
.__param: set "hex+signed" ; if param is "signed", correct it to "hex+signed"
endif
if (val(.__param) < $80)
!error "Illegal operand format setting: \{.__param}. Expected hex, dec, bin, sym, str or their derivatives."
endif
if (.__type==".b")
dc.b val(.__param)
elseif (.__type==".w")
dc.b val(.__param)|1
else
dc.b val(.__param)|3
endif
; Expression is an inline constant (e.g. %<endl> )
else
dc.b val(substr(string,.__pos+1+1,.__endpos-.__pos-2))
endif
.__lpos: set .__endpos+1
if (strstr(substr(string,.__pos+1,0),"%<")>=0)
.__pos: set strstr(substr(string,.__pos+1,0), "%<")+.__pos+1
else
.__pos: set -1
endif
endm
; Write part of string before the end
dc.b substr(string, .__lpos, 0), 0
endm