]> cloudbase.mooo.com Git - avrcpm.git/blob - avr/utils.asm
bd0d8d078a88acc97b193d3d0a2d61a3a3942232
[avrcpm.git] / avr / utils.asm
1 ; Print and Debug functions
2 ;
3 ; Copyright (C) 2010-2013 Leo C.
4 ;
5 ; This file is part of avrcpm.
6 ;
7 ; avrcpm is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
11 ;
12 ; avrcpm is distributed in the hope that it will be useful,
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ; GNU General Public License for more details.
16 ;
17 ; You should have received a copy of the GNU General Public License
18 ; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
19 ;
20 ; $Id$
21 ;
22
23
24 .cseg
25
26
27 ;Print a unsigned lonng value to the uart
28 ; r15:r14:temp2:temp = value
29
30 print_ultoa:
31 push yh
32 push yl
33 push z_flags
34 push r15
35 push r14
36 push temp2
37 push temp
38
39 clr yl ;yl = stack level
40
41 ultoa1: ldi z_flags, 32 ;yh = r15:temp % 10
42 clr yh ;r15:temp /= 10
43 ultoa2: lsl temp
44 rol temp2
45 rol r14
46 rol r15
47 rol yh
48 cpi yh,10
49 brcs ultoa3
50 subi yh,10
51 inc temp
52 ultoa3: dec z_flags
53 brne ultoa2
54 cpi yh, 10 ;yh is a numeral digit '0'-'9'
55 subi yh, -'0'
56 push yh ;Stack it
57 inc yl
58 cp temp,_0 ;Repeat until r15:temp gets zero
59 cpc temp2,_0
60 cpc r14,_0
61 cpc r15,_0
62 brne ultoa1
63
64 ldi temp, '0'
65 ultoa5: cpi yl,3 ; at least 3 digits (ms)
66 brge ultoa6
67 push temp
68 inc yl
69 rjmp ultoa5
70
71 ultoa6: pop temp ;Flush stacked digits
72 rcall uartputc
73 dec yl
74 brne ultoa6
75
76 pop temp
77 pop temp2
78 pop r14
79 pop r15
80 pop z_flags
81 pop yl
82 pop yh
83 ret
84
85
86 ;Prints temp2:temp in hex to the uart
87 printhexw:
88 push temp
89 mov temp,temp2
90 rcall printhex
91 pop temp
92 ;fall thru
93
94 ;Prints temp in hex to the uart
95 printhex:
96 swap temp
97 rcall printhexn
98 swap temp
99 ;fall thru
100
101 ;Prints the lower nibble
102 printhexn:
103 push temp
104 andi temp,0xf
105 cpi temp,0xA
106 brlo printhexn_isno
107 subi temp,-7
108 printhexn_isno:
109 subi temp,-'0'
110 rcall uartputc
111 pop temp
112 ret
113
114
115 ; Prints a single space
116
117 printspace:
118 push temp
119 ldi temp,' '
120 rcall uartputc
121 pop temp
122 ret
123
124 ;-----------------------------------------------------------------------
125 ;Prints the zero-terminated string following the call statement.
126
127 printstr:
128 push zh ;SP+5
129 push zl ; 4
130 push yh ; 3
131 push yl ; 2
132 push temp ; 1
133 in yh,sph
134 in yl,spl
135 ldd zl,y+7 ;SP+7 == "return adr." == String adr.
136 ldd zh,y+6 ;SP+6
137
138 lsl zl ;word to byte conv.
139 rol zh
140 printstr_loop:
141 lpm temp,z+
142 cpi temp,0
143 breq printstr_end
144 rcall uartputc
145 cpi temp,13
146 brne printstr_loop
147 ldi temp,10
148 rcall uartputc
149 rjmp printstr_loop
150
151 printstr_end:
152 adiw zl,1 ;rounding up
153 lsr zh ;byte to word conv.
154 ror zl
155
156 std y+7,zl
157 std y+6,zh
158 pop temp
159 pop yl
160 pop yh
161 pop zl
162 pop zh
163 ret
164
165 ; ------------------------ String functions -------------------------
166 ;
167
168 #if 0
169 ; String compare (z, y), one z-string in flash.
170
171 strcmp_p:
172 ld temp,y+
173 lpm _tmp0, z+
174 sub temp,_tmp0
175 brne strcmp_pex
176 tst _tmp0
177 brne strcmp_p
178 sub temp,temp
179 strcmp_pex:
180 ret
181
182 #endif
183
184 #if 0
185
186 strcmp_p:
187 ld temp,y+
188 lpm _tmp0,z+
189 sub temp,_tmp0
190 cpse _tmp0,_0
191 breq strcmp_p
192 ret
193
194
195 #endif
196
197 ;-----------------------------------------------------------------------
198 ; String compare (x, y, temp2). Max temp2 bytes are compared.
199
200 strncmp_p:
201 subi temp2,1
202 brcs strncmp_peq
203 ld temp,y+
204 lpm _tmp0, z+
205 sub temp,_tmp0
206 brne strncmp_pex
207 tst _tmp0
208 brne strncmp_p
209 strncmp_peq:
210 sub temp,temp
211 strncmp_pex:
212 ret
213
214 ;-----------------------------------------------------------------------
215 ; Memory compare: DRAM - AVR-RAM
216 ; DRAM-Addr. in x, AVRRAM-Addr. in y
217 ; Compare temp3 bytes.
218 ;
219 ; Return Z-Flag == 1 if match
220 ; temp, _tmp0 destroyed
221 ;
222
223 memcmp_d:
224 rcall dram_read_pp
225 ld _tmp0,y+
226 cp temp,_tmp0
227 brne memcmpd_nomatch
228 dec temp3
229 brne memcmp_d
230 memcmpd_nomatch:
231 ret
232
233 ; --------------- Debugging stuff ---------------
234
235
236 .if SRAM_FILL
237
238 stackusage_print:
239 ldiw z,ramtop
240 ldi temp, low(RAMEND+1)
241 ldi temp2,high(RAMEND+1)
242 ldi temp3,SRAMFILL_VAL
243 stack_search_l:
244 ld _tmp0,z+
245 cp temp3,_tmp0
246 brne stack_search_found
247 cp zl,temp
248 cpc zh,temp2
249 brne stack_search_l
250
251 stack_search_found:
252 sbiw z,1
253 sub temp, zl
254 sbc temp2,zh
255 printnewline
256 printstring "Stack used (bytes): "
257 push r15
258 push r14
259 clr r14
260 clr r15
261 rcall print_ultoa
262 pop r14
263 pop r15
264 ret
265 .endif
266
267
268
269 ;-----------------------------------------------------------------------
270 ; Prints 16 bytes RAM, pointed to by Z in hex.
271
272 #if 0
273
274 dbg_hexdump_line: ;Address in z
275 push temp2
276 push temp
277 printnewline
278 movw temp,z ;Print address
279 rcall printhexw
280 ldi temp2,16 ;16 byte per line
281 rcall printspace
282 dbg_hdl1:
283 cpi temp2,8
284 brne PC+2
285 rcall printspace
286
287 rcall printspace
288 ld temp,z+
289 rcall printhex
290 dec temp2
291 brne dbg_hdl1
292 sbiw z,16
293
294 rcall printspace
295 rcall printspace
296 ldi temp2,16
297 dbg_hdl2:
298 ld temp,z+
299 cpi temp,' '
300 brlo dbg_hdpd
301 cpi temp,0x7F
302 brlo dbg_hdp
303 dbg_hdpd:
304 ldi temp,'.'
305 dbg_hdp:
306 rcall uartputc
307 dec temp2
308 brne dbg_hdl2
309 sbiw z,16
310 rcall printspace
311 pop temp
312 pop temp2
313 ret
314 #endif
315
316 ;-----------------------------------------------------------------------
317 ; Print a line with the 8080/Z80 registers
318
319 printregs:
320 mov temp,z_flags
321 rcall printflags
322 printstring " A ="
323 mov temp,z_a
324 rcall printhex
325 printstring " BC ="
326 #if 1
327 movw temp,z_c
328 #else
329 ldd temp2,y+oz_b
330 ldd temp,y+oz_c
331 #endif
332 rcall printhexw
333 printstring " DE ="
334 #if 1
335 movw temp,z_e
336 #else
337 ldd temp2,y+oz_d
338 ldd temp,y+oz_e
339 #endif
340 rcall printhexw
341 printstring " HL ="
342 #if 1
343 movw temp,z_l
344 #else
345 ldd temp,y+oz_l
346 ldd temp2,y+oz_h
347 #endif
348 rcall printhexw
349 printstring " SP="
350 movw temp, z_spl
351 rcall printhexw
352 printstring " PC="
353 movw temp, z_pcl
354 rcall printhexw
355 printstring " "
356 movw xl,z_pcl
357 lcall dram_read_pp
358 rcall printhex
359 printstring " "
360 lcall dram_read_pp
361 rcall printhex
362 printstring " "
363 lcall dram_read
364 rcall printhex
365 printstring " "
366
367 #if EM_Z80
368 ldd temp,y+oz_f2
369 rcall printflags
370 printstring " a'="
371 ldd temp,y+oz_a2
372 rcall printhex
373 printstring " bc'="
374 ldd temp2,y+oz_b2
375 ldd temp,y+oz_c2
376 rcall printhexw
377 printstring " de'="
378 ldd temp2,y+oz_d2
379 ldd temp,y+oz_e2
380 rcall printhexw
381 printstring " hl'="
382 ldd temp2,y+oz_h2
383 ldd temp,y+oz_l2
384 rcall printhexw
385 printstring " IX="
386 ldd temp2,y+oz_xh
387 ldd temp,y+oz_xl
388 rcall printhexw
389 printstring " IY="
390 ldd temp2,y+oz_yh
391 ldd temp,y+oz_yl
392 rcall printhexw
393 printstring " I="
394 ldd temp,y+oz_i
395 rcall printhex
396
397 printstring " "
398 #endif
399 ret
400
401
402 #if EM_Z80
403 zflags_to_ch:
404 .db "SZ H VNC",0,0
405 #else
406 zflags_to_ch:
407 .db "SZ H PNC",0,0
408 #endif
409
410 printflags:
411 push temp2
412 mov temp2,temp
413 printnewline
414 push zl
415 push zh
416 ldiw z,zflags_to_ch*2
417 pr_zfl_next:
418 lpm temp,z+
419 tst temp
420 breq pr_zfl_end
421 cpi temp,' ' ; Test if no flag
422 breq pr_zfl_noflag
423 sbrs temp2,7 ;
424 ldi temp,' ' ; Flag not set
425 rcall uartputc
426 pr_zfl_noflag:
427 rol temp2
428 rjmp pr_zfl_next
429 pr_zfl_end:
430 pop zh
431 pop zl
432 pop temp2
433 ret
434
435 ; vim:set ts=8 noet nowrap