]> cloudbase.mooo.com Git - avrcpm.git/blame_incremental - avr/utils.asm
SVN --> GIT
[avrcpm.git] / avr / utils.asm
... / ...
CommitLineData
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: utils.asm 93 2014-01-03 16:32:32Z rapid $
21;
22
23
24 .cseg
25
26
27;Print a unsigned lonng value to the uart
28; r15:r14:temp2:temp = value
29
30print_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
41ultoa1: ldi z_flags, 32 ;yh = r15:temp % 10
42 clr yh ;r15:temp /= 10
43ultoa2: 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
52ultoa3: 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'
65ultoa5: cpi yl,3 ; at least 3 digits (ms)
66 brge ultoa6
67 push temp
68 inc yl
69 rjmp ultoa5
70
71ultoa6: 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
87printhexw:
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
95printhex:
96 swap temp
97 rcall printhexn
98 swap temp
99 ;fall thru
100
101;Prints the lower nibble
102printhexn:
103 push temp
104 andi temp,0xf
105 cpi temp,0xA
106 brlo printhexn_isno
107 subi temp,-7
108printhexn_isno:
109 subi temp,-'0'
110 rcall uartputc
111 pop temp
112 ret
113
114
115; Prints a single space
116
117printspace:
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
127printstr:
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
140printstr_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
151printstr_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
171strcmp_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
179strcmp_pex:
180 ret
181
182#endif
183
184#if 0
185
186strcmp_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
200strncmp_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
209strncmp_peq:
210 sub temp,temp
211strncmp_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
223memcmp_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
230memcmpd_nomatch:
231 ret
232
233; --------------- Debugging stuff ---------------
234
235
236.if SRAM_FILL
237
238stackusage_print:
239 ldiw z,ramtop
240 ldi temp, low(RAMEND+1)
241 ldi temp2,high(RAMEND+1)
242 ldi temp3,SRAMFILL_VAL
243stack_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
251stack_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.if MEMDUMP_DEBUG
269
270;-----------------------------------------------------------------------
271; Prints 16 bytes RAM, pointed to by Z in hex.
272
273dbg_hexdump_line: ;Address in z
274 push temp2
275 push temp
276; printnewline
277 movw temp,z ;Print address
278 rcall printhexw
279 printstring ":"
280 ldi temp2,16 ;16 byte per line
281dbg_hdl1:
282 cpi temp2,8
283 brne PC+2
284 rcall printspace
285
286 rcall printspace
287 ld temp,z+
288 rcall printhex
289 dec temp2
290 brne dbg_hdl1
291 sbiw z,16
292
293 rcall printspace
294 rcall printspace
295 ldi temp2,16
296dbg_hdl2:
297 ld temp,z+
298 cpi temp,' '
299 brlo dbg_hdlpd
300 cpi temp,0x7F
301 brlo dbg_hdlp
302dbg_hdlpd:
303 ldi temp,'.'
304dbg_hdlp:
305 rcall uartputc
306 dec temp2
307 brne dbg_hdl2
308 sbiw z,16
309 rcall printspace
310 pop temp
311 pop temp2
312 printnewline
313 ret
314
315
316; Prints temp2 bytes RAM, pointed to by Z in hex.
317
318dbg_hexdump: ;Address in z
319 push temp
320 push temp2
321; printnewline
322 movw temp,z ;Print address
323 rcall printhexw
324 printstring ":"
325 pop temp2
326 push temp2
327dbg_hd1:
328 rcall printspace
329 ld temp,z+
330 rcall printhex
331 dec temp2
332 brne dbg_hd1
333 pop temp2
334 sub zl,temp2
335 sbc zh,_0
336 rcall printspace
337 rcall printspace
338dbg_hd2:
339 ld temp,z+
340 cpi temp,' '
341 brlo dbg_hdpd
342 cpi temp,0x7F
343 brlo dbg_hdp
344dbg_hdpd:
345 ldi temp,'.'
346dbg_hdp:
347 rcall uartputc
348 dec temp2
349 brne dbg_hd2
350 pop temp
351 printnewline
352 ret
353
354.endif
355
356;-----------------------------------------------------------------------
357; Print a line with the 8080/Z80 registers
358
359printregs:
360 mov temp,z_flags
361 rcall printflags
362 printstring " A ="
363 mov temp,z_a
364 rcall printhex
365 printstring " BC ="
366#if 1
367 movw temp,z_c
368#else
369 ldd temp2,y+oz_b
370 ldd temp,y+oz_c
371#endif
372 rcall printhexw
373 printstring " DE ="
374#if 1
375 movw temp,z_e
376#else
377 ldd temp2,y+oz_d
378 ldd temp,y+oz_e
379#endif
380 rcall printhexw
381 printstring " HL ="
382#if 1
383 movw temp,z_l
384#else
385 ldd temp,y+oz_l
386 ldd temp2,y+oz_h
387#endif
388 rcall printhexw
389 printstring " SP="
390 movw temp, z_spl
391 rcall printhexw
392 printstring " PC="
393 movw temp, z_pcl
394 rcall printhexw
395 printstring " "
396 movw xl,z_pcl
397 lcall dram_read_pp
398 rcall printhex
399 printstring " "
400 lcall dram_read_pp
401 rcall printhex
402 printstring " "
403 lcall dram_read
404 rcall printhex
405 printstring " "
406
407#if EM_Z80
408 ldd temp,y+oz_f2
409 rcall printflags
410 printstring " a'="
411 ldd temp,y+oz_a2
412 rcall printhex
413 printstring " bc'="
414 ldd temp2,y+oz_b2
415 ldd temp,y+oz_c2
416 rcall printhexw
417 printstring " de'="
418 ldd temp2,y+oz_d2
419 ldd temp,y+oz_e2
420 rcall printhexw
421 printstring " hl'="
422 ldd temp2,y+oz_h2
423 ldd temp,y+oz_l2
424 rcall printhexw
425 printstring " IX="
426 ldd temp2,y+oz_xh
427 ldd temp,y+oz_xl
428 rcall printhexw
429 printstring " IY="
430 ldd temp2,y+oz_yh
431 ldd temp,y+oz_yl
432 rcall printhexw
433 printstring " I="
434 ldd temp,y+oz_i
435 rcall printhex
436
437 printstring " "
438#endif
439 ret
440
441
442#if EM_Z80
443zflags_to_ch:
444 .db "SZ H VNC",0,0
445#else
446zflags_to_ch:
447 .db "SZ H PNC",0,0
448#endif
449
450printflags:
451 push temp2
452 mov temp2,temp
453 printnewline
454 push zl
455 push zh
456 ldiw z,zflags_to_ch*2
457pr_zfl_next:
458 lpm temp,z+
459 tst temp
460 breq pr_zfl_end
461 cpi temp,' ' ; Test if no flag
462 breq pr_zfl_noflag
463 sbrs temp2,7 ;
464 ldi temp,' ' ; Flag not set
465 rcall uartputc
466pr_zfl_noflag:
467 rol temp2
468 rjmp pr_zfl_next
469pr_zfl_end:
470 pop zh
471 pop zl
472 pop temp2
473 ret
474
475; vim:set ts=8 noet nowrap