]> cloudbase.mooo.com Git - avrcpm.git/blame - avr/utils.asm
SVN --> GIT
[avrcpm.git] / avr / utils.asm
CommitLineData
5985ce1c
L
1; Print and Debug functions
2;
e8384f88 3; Copyright (C) 2010-2013 Leo C.
5985ce1c
L
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;
12a27f27 20; $Id: utils.asm 93 2014-01-03 16:32:32Z rapid $
5985ce1c
L
21;
22
23
24 .cseg
25
26
27;Print a unsigned lonng value to the uart
825ecc9d 28; r15:r14:temp2:temp = value
5985ce1c
L
29
30print_ultoa:
31 push yh
32 push yl
33 push z_flags
825ecc9d
L
34 push r15
35 push r14
5985ce1c
L
36 push temp2
37 push temp
4bd49b80 38
5985ce1c
L
39 clr yl ;yl = stack level
40
825ecc9d
L
41ultoa1: ldi z_flags, 32 ;yh = r15:temp % 10
42 clr yh ;r15:temp /= 10
4bd49b80
L
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
5985ce1c 51 inc temp
4bd49b80 52ultoa3: dec z_flags
5985ce1c
L
53 brne ultoa2
54 cpi yh, 10 ;yh is a numeral digit '0'-'9'
55 subi yh, -'0'
56 push yh ;Stack it
4bd49b80 57 inc yl
825ecc9d 58 cp temp,_0 ;Repeat until r15:temp gets zero
5985ce1c 59 cpc temp2,_0
825ecc9d
L
60 cpc r14,_0
61 cpc r15,_0
4bd49b80
L
62 brne ultoa1
63
5985ce1c
L
64 ldi temp, '0'
65ultoa5: cpi yl,3 ; at least 3 digits (ms)
66 brge ultoa6
4bd49b80 67 push temp
5985ce1c
L
68 inc yl
69 rjmp ultoa5
70
71ultoa6: pop temp ;Flush stacked digits
72 rcall uartputc
4bd49b80
L
73 dec yl
74 brne ultoa6
5985ce1c
L
75
76 pop temp
77 pop temp2
825ecc9d
L
78 pop r14
79 pop r15
5985ce1c
L
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
4bd49b80 98 swap temp
5985ce1c
L
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
dd7aea8c
L
114
115; Prints a single space
116
e8384f88 117printspace:
dd7aea8c 118 push temp
e8384f88 119 ldi temp,' '
dd7aea8c 120 rcall uartputc
dd7aea8c 121 pop temp
dd7aea8c 122 ret
e8384f88 123
98979541 124;-----------------------------------------------------------------------
4bd49b80 125;Prints the zero-terminated string following the call statement.
5985ce1c
L
126
127printstr:
5482d75f
L
128 push zh ;SP+5
129 push zl ; 4
130 push yh ; 3
131 push yl ; 2
132 push temp ; 1
5985ce1c
L
133 in yh,sph
134 in yl,spl
5482d75f
L
135 ldd zl,y+7 ;SP+7 == "return adr." == String adr.
136 ldd zh,y+6 ;SP+6
5985ce1c 137
5482d75f 138 lsl zl ;word to byte conv.
5985ce1c
L
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:
5482d75f
L
152 adiw zl,1 ;rounding up
153 lsr zh ;byte to word conv.
5985ce1c
L
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
4bd49b80 164
623dd899
L
165; ------------------------ String functions -------------------------
166;
167
168#if 0
169; String compare (z, y), one z-string in flash.
170
171strcmp_p:
98979541 172 ld temp,y+
623dd899
L
173 lpm _tmp0, z+
174 sub temp,_tmp0
175 brne strcmp_pex
176 tst _tmp0
177 brne strcmp_p
98979541 178 sub temp,temp
623dd899
L
179strcmp_pex:
180 ret
98979541
L
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
623dd899
L
195#endif
196
98979541 197;-----------------------------------------------------------------------
623dd899
L
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
98979541
L
214;-----------------------------------------------------------------------
215; Memory compare: DRAM - AVR-RAM
4bd49b80 216; DRAM-Addr. in x, AVRRAM-Addr. in y
98979541
L
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
623dd899 232
5985ce1c 233; --------------- Debugging stuff ---------------
98979541
L
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
12a27f27 268.if MEMDUMP_DEBUG
98979541 269
e8384f88
L
270;-----------------------------------------------------------------------
271; Prints 16 bytes RAM, pointed to by Z in hex.
272
e8384f88
L
273dbg_hexdump_line: ;Address in z
274 push temp2
275 push temp
12a27f27 276; printnewline
e8384f88
L
277 movw temp,z ;Print address
278 rcall printhexw
12a27f27 279 printstring ":"
e8384f88 280 ldi temp2,16 ;16 byte per line
e8384f88
L
281dbg_hdl1:
282 cpi temp2,8
283 brne PC+2
284 rcall printspace
4bd49b80 285
e8384f88
L
286 rcall printspace
287 ld temp,z+
288 rcall printhex
289 dec temp2
290 brne dbg_hdl1
291 sbiw z,16
4bd49b80 292
e8384f88
L
293 rcall printspace
294 rcall printspace
295 ldi temp2,16
296dbg_hdl2:
297 ld temp,z+
298 cpi temp,' '
12a27f27 299 brlo dbg_hdlpd
e8384f88 300 cpi temp,0x7F
12a27f27
L
301 brlo dbg_hdlp
302dbg_hdlpd:
e8384f88 303 ldi temp,'.'
12a27f27 304dbg_hdlp:
e8384f88
L
305 rcall uartputc
306 dec temp2
307 brne dbg_hdl2
308 sbiw z,16
309 rcall printspace
310 pop temp
311 pop temp2
12a27f27 312 printnewline
e8384f88 313 ret
12a27f27
L
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
4bd49b80 355
98979541 356;-----------------------------------------------------------------------
80e1fa71 357; Print a line with the 8080/Z80 registers
5985ce1c 358
5985ce1c 359printregs:
80e1fa71
L
360 mov temp,z_flags
361 rcall printflags
5985ce1c
L
362 printstring " A ="
363 mov temp,z_a
4bd49b80 364 rcall printhex
5985ce1c 365 printstring " BC ="
825ecc9d
L
366#if 1
367 movw temp,z_c
368#else
e7a0f403
L
369 ldd temp2,y+oz_b
370 ldd temp,y+oz_c
825ecc9d 371#endif
5985ce1c
L
372 rcall printhexw
373 printstring " DE ="
825ecc9d
L
374#if 1
375 movw temp,z_e
376#else
e7a0f403
L
377 ldd temp2,y+oz_d
378 ldd temp,y+oz_e
825ecc9d 379#endif
5985ce1c
L
380 rcall printhexw
381 printstring " HL ="
825ecc9d
L
382#if 1
383 movw temp,z_l
384#else
e7a0f403 385 ldd temp,y+oz_l
825ecc9d
L
386 ldd temp2,y+oz_h
387#endif
5985ce1c 388 rcall printhexw
80e1fa71 389 printstring " SP="
5985ce1c
L
390 movw temp, z_spl
391 rcall printhexw
80e1fa71 392 printstring " PC="
5985ce1c
L
393 movw temp, z_pcl
394 rcall printhexw
395 printstring " "
396 movw xl,z_pcl
096f3a91 397 lcall dram_read_pp
5985ce1c
L
398 rcall printhex
399 printstring " "
096f3a91 400 lcall dram_read_pp
5985ce1c
L
401 rcall printhex
402 printstring " "
096f3a91 403 lcall dram_read
5985ce1c
L
404 rcall printhex
405 printstring " "
80e1fa71
L
406
407#if EM_Z80
e7a0f403 408 ldd temp,y+oz_f2
80e1fa71 409 rcall printflags
096f3a91 410 printstring " a'="
e7a0f403 411 ldd temp,y+oz_a2
4bd49b80 412 rcall printhex
096f3a91 413 printstring " bc'="
e7a0f403
L
414 ldd temp2,y+oz_b2
415 ldd temp,y+oz_c2
80e1fa71 416 rcall printhexw
096f3a91 417 printstring " de'="
e7a0f403
L
418 ldd temp2,y+oz_d2
419 ldd temp,y+oz_e2
80e1fa71 420 rcall printhexw
096f3a91 421 printstring " hl'="
e7a0f403
L
422 ldd temp2,y+oz_h2
423 ldd temp,y+oz_l2
80e1fa71
L
424 rcall printhexw
425 printstring " IX="
e7a0f403
L
426 ldd temp2,y+oz_xh
427 ldd temp,y+oz_xl
80e1fa71
L
428 rcall printhexw
429 printstring " IY="
e7a0f403
L
430 ldd temp2,y+oz_yh
431 ldd temp,y+oz_yl
80e1fa71
L
432 rcall printhexw
433 printstring " I="
e7a0f403 434 ldd temp,y+oz_i
4bd49b80 435 rcall printhex
80e1fa71
L
436
437 printstring " "
438#endif
439 ret
440
441
442#if EM_Z80
443zflags_to_ch:
444 .db "SZ H VNC",0,0
4bd49b80 445#else
80e1fa71
L
446zflags_to_ch:
447 .db "SZ H PNC",0,0
448#endif
4bd49b80 449
80e1fa71
L
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
4bd49b80 463 sbrs temp2,7 ;
80e1fa71
L
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
4bd49b80 471 pop zl
80e1fa71 472 pop temp2
5985ce1c 473 ret
5985ce1c
L
474
475; vim:set ts=8 noet nowrap