1 ;---------------------------------------------------------------------------;
2 ; Extended itoa, puts, printf and atoi (C)ChaN, 2011
3 ;---------------------------------------------------------------------------;
5 // Base size is 152 bytes
6 #define CR_CRLF 0 // Convert \n to \r\n (+10 bytes)
7 #define USE_XPRINTF 1 // Enable xprintf function (+194 bytes)
8 #define USE_XSPRINTF 0 // Add xsprintf function (+78 bytes)
9 #define USE_XFPRINTF 0 // Add xfprintf function (+54 bytes)
10 #define USE_XATOI 0 // Enable xatoi function (+182 bytes)
13 #if FLASHEND > 0x1FFFF
14 #error xitoa module does not support 256K devices
18 #include <avr/io.h> // Include device specific definitions.
21 #ifdef SPM_PAGESIZE // Recent devices have "lpm Rd,Z+" and "movw".
25 .macro _MOVW dh,dl, sh,sl
28 #else // Earlier devices do not have "lpm Rd,Z+" nor "movw".
34 .macro _MOVW dh,dl, sh,sl
42 ;---------------------------------------------------------------------------
43 ; Stub function to forward to user output function
45 ;Prototype: void xputc (char chr // a character to be output
50 .global xfunc_out ; xfunc_out must be initialized before using this module.
59 cpi r24, 10 ;LF --> CRLF
68 lds ZL, xfunc_out+0 ;Pointer to the registered output function.
69 lds ZH, xfunc_out+1 ;/
70 sbiw ZL, 0 ;Skip if null
80 ;---------------------------------------------------------------------------
81 ; Direct ROM string output
83 ;Prototype: void xputs (const char *str_p // rom string to be output
89 _MOVW ZH,ZL, r25,r24 ; Z = pointer to rom string
99 ;---------------------------------------------------------------------------
100 ; Extended direct numeral string output (32bit version)
102 ;Prototype: void xitoa (long value, // value to be output
103 ; char radix, // radix
104 ; char width); // minimum width
110 ;r25:r22 = value, r20 = base, r18 = digits
111 clr r31 ;r31 = stack level
112 ldi r30, ' ' ;r30 = sign
113 ldi r19, ' ' ;r19 = filler
114 sbrs r20, 7 ;When base indicates signd format and the value
115 rjmp 0f ;is minus, add a '-'.
128 0: sbrs r18, 7 ;When digits indicates zero filled,
129 rjmp 1f ;filler is '0'.
132 ;----- string conversion loop
133 1: ldi r21, 32 ;r26 = r25:r22 % r20
134 clr r26 ;r25:r22 /= r20
146 cpi r26, 10 ;r26 is a numeral digit '0'-'F'
152 cp r22, r1 ;Repeat until r25:r22 gets zero
158 cpi r30, '-' ;Minus sign if needed
162 5: cp r31, r18 ;Filler
168 6: pop r24 ;Flush stacked digits and exit
178 ;---------------------------------------------------------------------------;
179 ; Formatted string output (16/32bit version)
182 ; void __xprintf (const char *format_p, ...);
183 ; void __xsprintf(char*, const char *format_p, ...);
184 ; void __xfprintf(void(*func)(char), const char *format_p, ...);
191 ld ZL, Y+ ;Z = pointer to format string
194 0: _LPMI r24 ;Get a format char
195 cpi r24, 0 ;End of format string?
197 cpi r24, '%' ;Is format?
199 1: rcall xputc ;Put a normal character
203 20: ldi r18, 0 ;r18: digits
206 cpi r21, '%' ;Is a %?
208 cpi r21, '0' ;Zero filled?
211 22: _LPMI r21 ;Get width
224 24: brtc 25f ;get value (low word)
228 cpi r21, 'c' ;Is type character?
230 cpi r21, 's' ;Is type RAM string?
232 cpi r21, 'S' ;Is type ROM string?
234 _MOVW r23,r22,r25,r24 ;r25:r22 = value
238 cpi r21, 'l' ;Is long int?
240 ld r24, Y+ ;get value (high word)
244 26: cpi r21, 'd' ;Is type signed decimal?
253 27: cpi r21, 'u' ;Is type unsigned decimal?
256 cpi r21, 'X' ;Is type hexdecimal?
259 cpi r21, 'b' ;Is type binary?
263 40: push ZH ;Output the value
270 50: push ZH ;Put a string on the RAM
279 60: push ZH ;Put a string on the ROM
291 in YL, _SFR_IO_ADDR(SPL)
293 in YH, _SFR_IO_ADDR(SPH)
297 adiw YL, 5 ;Y = pointer to arguments
317 in YL, _SFR_IO_ADDR(SPL)
319 in YH, _SFR_IO_ADDR(SPH)
323 adiw YL, 5 ;Y = pointer to arguments
324 lds ZL, xfunc_out+0 ;Save registered output function
325 lds ZH, xfunc_out+1 ;
328 ldi ZL, lo8(pm(putram));Set local output function
329 ldi ZH, hi8(pm(putram));
330 sts xfunc_out+0, ZL ;
331 sts xfunc_out+1, ZH ;/
332 push r15 ;Initialize pointer to string buffer
337 _MOVW ZH,ZL, r15,r14 ;Terminate string
341 pop ZH ;Restore registered output function
343 sts xfunc_out+0, ZL ;
344 sts xfunc_out+1, ZH ;/
358 in YL, _SFR_IO_ADDR(SPL)
360 in YH, _SFR_IO_ADDR(SPH)
364 adiw YL, 5 ;Y = pointer to arguments
365 lds ZL, xfunc_out+0 ;Save registered output function
366 lds ZH, xfunc_out+1 ;
369 ld ZL, Y+ ;Set output function
371 sts xfunc_out+0, ZL ;
372 sts xfunc_out+1, ZH ;/
374 pop ZH ;Restore registered output function
376 sts xfunc_out+0, ZL ;
377 sts xfunc_out+1, ZH ;/
388 ;---------------------------------------------------------------------------
389 ; Extended numeral string input
392 ; char xatoi ( /* 1: Successful, 0: Failed */
393 ; const char **str, /* pointer to pointer to source string */
394 ; long *res /* result */
403 _MOVW r1, r0, r23, r22
404 _MOVW XH, XL, r25, r24
407 clr r18 ;r21:r18 = 0;
413 ldi r25, 10 ;r25 = 10;
416 41: ld r22, Z ;r22 = *Z;
417 cpi r22, ' ' ;if(r22 == ' ') continue
419 brcs 70f ;if(r22 < ' ') error;
420 cpi r22, '-' ;if(r22 == '-') {
424 42: cpi r22, '9'+1 ;if(r22 > '9') error;
426 cpi r22, '0' ;if(r22 < '0') error;
428 brne 51f ;if(r22 > '0') cv_start;
430 adiw ZL, 1 ;r22 = *(++Z);
432 cpi r22, ' '+1 ;if(r22 <= ' ') exit;
434 cpi r22, 'b' ;if(r22 == 'b') {
436 ldi r25, 2 ; cv_start;
438 43: cpi r22, 'x' ;if(r22 != 'x') error;
440 ldi r25, 16 ;r25 = 16;
444 51: cpi r22, ' '+1 ;if(r22 <= ' ') break;
446 cpi r22, 'a' ;if(r22 >= 'a') r22 =- 0x20;
449 52: subi r22, '0' ;if((r22 -= '0') < 0) error;
451 cpi r22, 10 ;if(r22 >= 10) {
453 subi r22, 7 ; if(r22 < 10)
456 53: cp r22, r25 ;if(r22 >= r25) error;
458 60: ldi r24, 33 ;r21:r18 *= r25;
469 add r18, r22 ;r21:r18 += r22;