forked from kieranhj/archie-verse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.asm
More file actions
405 lines (329 loc) · 7.53 KB
/
debug.asm
File metadata and controls
405 lines (329 loc) · 7.53 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
; ============================================================================
; Debug helpers.
; TODO: Could do log to open file (on HostFS) if useful.
; ============================================================================
.if _DEBUG
.equ Debug_TempLen, 16
.equ Debug_MaxVars, 8
.equ Debug_MaxKeys, 32
.equ Debug_MaxGlyphs, 96
.equ Debug_Colour, 0xf
debug_num_keys:
.long 0
debug_pressed_mask:
.long 0
debug_prev_mask:
.long 0
; Plot a string to the screen at the current cursor position.
; R0=ptr to null terminated string.
.if Screen_Mode==9
.equ debug_plot_string, debug_plot_string_mode9
.else
.equ debug_plot_string, debug_plot_string_slow
.endif
; TODO: debug font for MODE!=9
; R0=value to plot as %04x
; Trashes R1-R2.
debug_plot_hex4:
adr r1, debug_temp_string
mov r2, #Debug_TempLen
swi OS_ConvertHex4
adr r0, debug_temp_string
b debug_plot_string
.if 0
; R0=fp value.
debug_write_fp:
stmfd sp!, {r1, r2}
adr r1, debug_temp_string
mov r2, #Debug_TempLen
swi OS_ConvertHex8
adr r0, debug_temp_string
swi OS_WriteO
mov r0, #32
swi OS_WriteC
ldmfd sp!, {r1, r2}
mov pc, lr
; R0=vector ptr.
debug_write_vector:
stmfd sp!, {r0, r3, lr}
mov r3, r0
ldr r0, [r3, #0]
bl debug_write_fp
ldr r0, [r3, #4]
bl debug_write_fp
ldr r0, [r3, #8]
bl debug_write_fp
ldmfd sp!, {r0, r3, pc}
.endif
; R0=address of a variable to add to the debug display.
; Trashes: R1-R3
debug_register_var:
adr r1, debug_var_stack
mov r2, #0
.1:
ldr r3, [r1, r2, lsl #2]
cmp r3, r0
moveq pc, lr ; already registered.
cmp r3, #0
streq r0, [r1, r2, lsl #2]
moveq pc, lr
add r2, r2, #1
cmp r2, #Debug_MaxVars
blt .1
adr r0, error_out_of_vars
swi OS_GenerateError
mov pc, lr
; R12=screen addr.
debug_plot_vars:
ldrb r0, debug_show_info
cmp r0, #0
moveq pc, lr
str lr, [sp, #-4]!
SET_BORDER 0xffffff ; white = debug
bl debug_cursor_home
adr r10, debug_var_stack
mov r9, #0
.1:
ldr r1, [r10, r9, lsl #2]
cmp r1, #0
beq .2
ldr r0, [r1]
bl debug_plot_hex4
bl debug_cursor_right
add r9, r9, #1
cmp r9, #Debug_MaxVars
blt .1
.2:
SET_BORDER 0x000000
ldr pc, [sp], #4
; R0=key code to register.
; R1=addr of function to call.
; R2=param to call the function with.
; Trashes: R3-R4.
debug_register_key:
adr r4, debug_key_stack
ldr r3, debug_num_keys
cmp r3, #Debug_MaxKeys
adrge r0, error_out_of_keys
swige OS_GenerateError
add r4, r4, r3, lsl #3
add r4, r4, r3, lsl #2
stmia r4, {r0-r2}
add r3, r3, #1
str r3, debug_num_keys
mov pc, lr
; R1=0 key up or 1 key down
; R2=internal key number (RMKey_*)
debug_handle_keypress:
stmfd sp!, {r3-r6}
adr r4, debug_key_stack
ldr r3, debug_num_keys
add r4, r4, r3, lsl #3
add r4, r4, r3, lsl #2
mov r5, #1
mov r5, r5, lsl r3 ; key bit.
ldr r3, debug_pressed_mask
.1:
; Any bits left?
movs r5, r5, lsr #1
beq .2 ; no more bits.
; Check key code.
ldr r6, [r4, #-12]!
cmp r6, r2
bne .1
; Key matches so mask bit in/out.
cmp r1, #0
biceq r3, r3, r5
orrne r3, r3, r5
b .1
.2:
str r3, debug_pressed_mask
ldmfd sp!, {r3-r6}
mov pc, lr
debug_do_key_callbacks:
str lr, [sp, #-4]!
ldr r0, debug_pressed_mask
ldr r2, debug_prev_mask
mvn r2, r2 ; ~old
and r2, r0, r2 ; new & ~old ; diff bits
str r0, debug_prev_mask
and r4, r2, r0 ; diff bits & key down bits
adr r3, debug_key_stack
ldr r0, debug_num_keys
add r3, r3, r0, lsl #3
add r3, r3, r0, lsl #2
mov r5, #1
mov r5, r5, lsl r0 ; key bit.
.1:
; Any bits left?
movs r5, r5, lsr #1
beq .2 ; no more bits.
; Key down?
sub r3, r3, #12
tst r4, r5
beq .1
; Make key callback.
ldr r0, [r3, #4] ; func.
ldr r1, [r3, #8] ; data.
adr lr, .1
mov pc, r0
.2:
ldr pc, [sp], #4
; R1=byte addr.
debug_toggle_byte:
ldrb r0, [r1]
eor r0, r0, #1
strb r0, [r1]
mov pc, lr
debug_set_byte_true:
mov r0, #1
strb r0, [r1]
mov pc, lr
debug_temp_string:
.skip Debug_TempLen
debug_var_stack:
.skip 4*Debug_MaxVars
error_out_of_vars:
.long 0
.byte "Out of debug vars!"
.p2align 2
.long 0
debug_key_stack:
.skip 12*Debug_MaxKeys
error_out_of_keys:
.long 0
.byte "Out of debug keys!"
.p2align 2
.long 0
; Font is 8 bytes per glyph, 1bpp.
debug_font_p:
.long debug_font_no_adr
debug_font_mode9_p:
.long debug_font_mode9_no_adr
debug_init:
; Reset debug keys & vars.
mov r0, #0
str r0, debug_num_keys
adr r1, debug_var_stack
mov r2, #Debug_MaxVars
.3:
str r0, [r1], #4
subs r2, r2, #1
bne .3
; Explode font to MODE 9 for fast plotting.
ldr r10, debug_font_p ; src
ldr r11, debug_font_mode9_p ; dst
mov r9, #Debug_MaxGlyphs
.1:
ldr r0, [r10], #4 ; src word = 4x8-bit rows.
mov r2, #8 ; glyph height.
.2:
mov r1, #0 ; dst word.
.rept 8
movs r0, r0, lsr #1
orrcs r1, r1, #Debug_Colour ; or 0xf for mask.
mov r1, r1, lsl #4
.endr
str r1, [r11], #4
cmp r2, #5
ldreq r0, [r10], #4
subs r2, r2, #1
bne .2
subs r9, r9, #1
bne .1
mov pc, lr
debug_cursor_x:
.byte 0
debug_cursor_y:
.byte 0
.p2align 2
; R0=ptr to string
; R12=screen addr
; Trashes: R1-R2, R8-R11.
debug_plot_string_mode9:
stmfd sp!, {r1-r11, lr}
bl debug_calc_scr_ptr
ldr r9, debug_font_mode9_p
mov r8, r0
adr lr, .1
.1:
ldrb r0, [r8], #1
cmp r0, #0
ldmeqfd sp!, {r1-r11, pc} ; exit
cmp r0, #ASCII_Space
blt .2 ; vdu code.
subs r0, r0, #ASCII_Space
cmp r0, #Debug_MaxGlyphs
bge .10 ; ascii>127
; Blit glyph.
add r10, r9, r0, lsl #5 ; 32 bytes per glyph.
ldmia r10, {r0-r7}
str r0, [r11], #Screen_Stride
str r1, [r11], #Screen_Stride
str r2, [r11], #Screen_Stride
str r3, [r11], #Screen_Stride
str r4, [r11], #Screen_Stride
str r5, [r11], #Screen_Stride
str r6, [r11], #Screen_Stride
str r7, [r11], #Screen_Stride
.10:
; Update cursor.
b debug_cursor_right
; Handle VDU codes.
.2:
cmp r0, #VDU_SetPos ; set cursor
bne .3
ldrb r1, [r8], #1
ldrb r2, [r8], #1
b debug_set_cursor
.3:
cmp r0, #VDU_TextColour ; set colour
bne .4
ldrb r9, [r8], #1
; TODO: Support debug text colour at runtime.
;strb r9, debug_colour ; not supported!
b .1
.4:
cmp r0, #VDU_Home ; home cursor
b debug_cursor_home
debug_cursor_home:
.if Screen_Mode==9
mov r1, #0
mov r2, #0
b debug_set_cursor
.else
swi OS_WriteI+VDU_Home
mov pc, lr
.endif
debug_cursor_right:
.if Screen_Mode==9
ldrb r1, debug_cursor_x
ldrb r2, debug_cursor_y
add r1, r1, #1
cmp r1, #40
movge r1, #0
addge r2, r2, #1
cmp r2, #32
movge r2, #0
; FALL THROUGH!
.else
swi OS_WriteI+ASCII_Space
mov pc, lr
.endif
; R1=x, R2=y
debug_set_cursor:
strb r1, debug_cursor_x
strb r2, debug_cursor_y
; FALL THROUGH!
; R12=screen addr.
debug_calc_scr_ptr:
ldrb r1, debug_cursor_x
ldrb r2, debug_cursor_y
add r11, r12, r2, lsl #7
add r11, r11, r2, lsl #5 ; y*160
add r11, r11, r1, lsl #2 ; x*4
mov pc, lr
debug_plot_string_slow:
swi OS_WriteO
mov pc, lr
.endif