123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- // Base size is 152 bytes
- #define CR_CRLF 0 // Convert \n to \r\n (+10 bytes)
- #define USE_XPRINTF 1 // Enable xprintf function (+194 bytes)
- #define USE_XSPRINTF 0 // Add xsprintf function (+78 bytes)
- #define USE_XFPRINTF 0 // Add xfprintf function (+54 bytes)
- #define USE_XATOI 0 // Enable xatoi function (+182 bytes)
- #if FLASHEND > 0x1FFFF
- #error xitoa module does not support 256K devices
- #endif
- .nolist
- #include <avr/io.h> // Include device specific definitions.
- .list
- #ifdef SPM_PAGESIZE // Recent devices have "lpm Rd,Z+" and "movw".
- .macro _LPMI reg
- lpm \reg, Z+
- .endm
- .macro _MOVW dh,dl, sh,sl
- movw \dl, \sl
- .endm
- #else // Earlier devices do not have "lpm Rd,Z+" nor "movw".
- .macro _LPMI reg
- lpm
- mov \reg, r0
- adiw ZL, 1
- .endm
- .macro _MOVW dh,dl, sh,sl
- mov \dl, \sl
- mov \dh, \sh
- .endm
- #endif
- .section .bss
- .global xfunc_out
- xfunc_out: .ds.w 1
- .section .text
- .func xputc
- .global xputc
- xputc:
- #if CR_CRLF
- cpi r24, 10
- brne 1f
- ldi r24, 13
- rcall 1f
- ldi r24, 10
- 1:
- #endif
- push ZH
- push ZL
- lds ZL, xfunc_out+0
- lds ZH, xfunc_out+1
- sbiw ZL, 0
- breq 2f
- icall
- 2: pop ZL
- pop ZH
- ret
- .endfunc
- .func xputs
- .global xputs
- xputs:
- _MOVW ZH,ZL, r25,r24
- 1: _LPMI r24
- cpi r24, 0
- breq 2f
- rcall xputc
- rjmp 1b
- 2: ret
- .endfunc
- .func xitoa
- .global xitoa
- xitoa:
-
- clr r31
- ldi r30, ' '
- ldi r19, ' '
- sbrs r20, 7
- rjmp 0f
- neg r20
- sbrs r25, 7
- rjmp 0f
- ldi r30, '-'
- com r22
- com r23
- com r24
- com r25
- adc r22, r1
- adc r23, r1
- adc r24, r1
- adc r25, r1
- 0: sbrs r18, 7
- rjmp 1f
- neg r18
- ldi r19, '0'
-
- 1: ldi r21, 32
- clr r26
- 2: lsl r22
- rol r23
- rol r24
- rol r25
- rol r26
- cp r26, r20
- brcs 3f
- sub r26, r20
- inc r22
- 3: dec r21
- brne 2b
- cpi r26, 10
- brcs 4f
- subi r26, -7
- 4: subi r26, -'0'
- push r26
- inc r31
- cp r22, r1
- cpc r23, r1
- cpc r24, r1
- cpc r25, r1
- brne 1b
- cpi r30, '-'
- brne 5f
- push r30
- inc r31
- 5: cp r31, r18
- brcc 6f
- push r19
- inc r31
- rjmp 5b
- 6: pop r24
- rcall xputc
- dec r31
- brne 6b
- ret
- .endfunc
- #if USE_XPRINTF
- .func xvprintf
- xvprintf:
- ld ZL, Y+
- ld ZH, Y+
- 0: _LPMI r24
- cpi r24, 0
- breq 90f
- cpi r24, '%'
- breq 20f
- 1: rcall xputc
- rjmp 0b
- 90: ret
- 20: ldi r18, 0
- clt
- _LPMI r21
- cpi r21, '%'
- breq 1b
- cpi r21, '0'
- brne 23f
- set
- 22: _LPMI r21
- 23: cpi r21, '9'+1
- brcc 24f
- subi r21, '0'
- brcs 90b
- lsl r18
- mov r0, r18
- lsl r18
- lsl r18
- add r18, r0
- add r18, r21
- rjmp 22b
- 24: brtc 25f
- neg r18
- 25: ld r24, Y+
- ld r25, Y+
- cpi r21, 'c'
- breq 1b
- cpi r21, 's'
- breq 50f
- cpi r21, 'S'
- breq 60f
- _MOVW r23,r22,r25,r24
- clr r24
- clr r25
- clt
- cpi r21, 'l'
- brne 26f
- ld r24, Y+
- ld r25, Y+
- set
- _LPMI r21
- 26: cpi r21, 'd'
- brne 27f
- ldi r20, -10
- brts 40f
- sbrs r23, 7
- rjmp 40f
- ldi r24, -1
- ldi r25, -1
- rjmp 40f
- 27: cpi r21, 'u'
- ldi r20, 10
- breq 40f
- cpi r21, 'X'
- ldi r20, 16
- breq 40f
- cpi r21, 'b'
- ldi r20, 2
- breq 40f
- ret
- 40: push ZH
- push ZL
- rcall xitoa
- 42: pop ZL
- pop ZH
- rjmp 0b
- 50: push ZH
- push ZL
- _MOVW ZH,ZL, r25,r24
- 51: ld r24, Z+
- cpi r24, 0
- breq 42b
- rcall xputc
- rjmp 51b
- 60: push ZH
- push ZL
- rcall xputs
- rjmp 42b
- .endfunc
- .func __xprintf
- .global __xprintf
- __xprintf:
- push YH
- push YL
- in YL, _SFR_IO_ADDR(SPL)
- #ifdef SPH
- in YH, _SFR_IO_ADDR(SPH)
- #else
- clr YH
- #endif
- adiw YL, 5
- rcall xvprintf
- pop YL
- pop YH
- ret
- .endfunc
- #if USE_XSPRINTF
- .func __xsprintf
- putram:
- _MOVW ZH,ZL, r15,r14
- st Z+, r24
- _MOVW r15,r14, ZH,ZL
- ret
- .global __xsprintf
- __xsprintf:
- push YH
- push YL
- in YL, _SFR_IO_ADDR(SPL)
- #ifdef SPH
- in YH, _SFR_IO_ADDR(SPH)
- #else
- clr YH
- #endif
- adiw YL, 5
- lds ZL, xfunc_out+0
- lds ZH, xfunc_out+1
- push ZL
- push ZH
- ldi ZL, lo8(pm(putram))
- ldi ZH, hi8(pm(putram))
- sts xfunc_out+0, ZL
- sts xfunc_out+1, ZH
- push r15
- push r14
- ld r14, Y+
- ld r15, Y+
- rcall xvprintf
- _MOVW ZH,ZL, r15,r14
- st Z, r1
- pop r14
- pop r15
- pop ZH
- pop ZL
- sts xfunc_out+0, ZL
- sts xfunc_out+1, ZH
- pop YL
- pop YH
- ret
- .endfunc
- #endif
- #if USE_XFPRINTF
- .func __xfprintf
- .global __xfprintf
- __xfprintf:
- push YH
- push YL
- in YL, _SFR_IO_ADDR(SPL)
- #ifdef SPH
- in YH, _SFR_IO_ADDR(SPH)
- #else
- clr YH
- #endif
- adiw YL, 5
- lds ZL, xfunc_out+0
- lds ZH, xfunc_out+1
- push ZL
- push ZH
- ld ZL, Y+
- ld ZH, Y+
- sts xfunc_out+0, ZL
- sts xfunc_out+1, ZH
- rcall xvprintf
- pop ZH
- pop ZL
- sts xfunc_out+0, ZL
- sts xfunc_out+1, ZH
- pop YL
- pop YH
- ret
- .endfunc
- #endif
- #endif
- #if USE_XATOI
- .func xatoi
- .global xatoi
- xatoi:
- _MOVW r1, r0, r23, r22
- _MOVW XH, XL, r25, r24
- ld ZL, X+
- ld ZH, X+
- clr r18
- clr r19
- clr r20
- clr r21
- clt
- ldi r25, 10
- rjmp 41f
- 40: adiw ZL, 1
- 41: ld r22, Z
- cpi r22, ' '
- breq 40b
- brcs 70f
- cpi r22, '-'
- brne 42f
- set
- rjmp 40b
- 42: cpi r22, '9'+1
- brcc 70f
- cpi r22, '0'
- brcs 70f
- brne 51f
- ldi r25, 8
- adiw ZL, 1
- ld r22, Z
- cpi r22, ' '+1
- brcs 80f
- cpi r22, 'b'
- brne 43f
- ldi r25, 2
- rjmp 50f
- 43: cpi r22, 'x'
- brne 51f
- ldi r25, 16
- 50: adiw ZL, 1
- ld r22, Z
- 51: cpi r22, ' '+1
- brcs 80f
- cpi r22, 'a'
- brcs 52f
- subi r22, 0x20
- 52: subi r22, '0'
- brcs 70f
- cpi r22, 10
- brcs 53f
- subi r22, 7
- cpi r22, 10
- brcs 70f
- 53: cp r22, r25
- brcc 70f
- 60: ldi r24, 33
- sub r23, r23
- 61: brcc 62f
- add r23, r25
- 62: lsr r23
- ror r21
- ror r20
- ror r19
- ror r18
- dec r24
- brne 61b
- add r18, r22
- adc r19, r24
- adc r20, r24
- adc r21, r24
- rjmp 50b
- 70: ldi r24, 0
- rjmp 81f
- 80: ldi r24, 1
- 81: brtc 82f
- clr r22
- com r18
- com r19
- com r20
- com r21
- adc r18, r22
- adc r19, r22
- adc r20, r22
- adc r21, r22
- 82: st -X, ZH
- st -X, ZL
- _MOVW XH, XL, r1, r0
- st X+, r18
- st X+, r19
- st X+, r20
- st X+, r21
- clr r1
- ret
- .endfunc
- #endif
|