;
 ; Ullrich von Bassewitz, 05.08.1998
+; Christian Krueger, 11-Mar-2017, spend two bytes for one cycle, improved 65SC02 optimization
 ;
 ; CC65 runtime: add ints
 ;
 ; called a lot!
 
         .export         tosadda0, tosaddax
-        .importzp       sp
+        .importzp       sp, tmp1
 
         .macpack        cpu
 
 tosadda0:
         ldx     #0
 tosaddax:
-        clc
-.if (.cpu .bitand CPU_ISET_65SC02)
-        adc     (sp)            ; 65SC02 version - saves 2 cycles
-        ldy     #1
-.else
-        ldy     #0
-        adc     (sp),y          ; lo byte
-        iny
-.endif
-        pha                     ; save it
-        txa
-        adc     (sp),y          ; hi byte
-        tax
-        clc
-        lda     sp
-        adc     #2
-        sta     sp
-        bcc     L1
-        inc     sp+1
-L1:     pla                     ; Restore low byte
-        rts
+        clc                     ; (2)
+
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+
+        adc     (sp)            ; (7)
+        tay                     ; (9)
+        inc     sp              ; (14)
+        bne     hiadd           ; (17)
+        inc     sp+1            ; (-1+5)
+hiadd:  txa                     ; (19)
+        adc     (sp)            ; (24)
+        tax                     ; (26)
+        inc     sp              ; (31)
+        bne     done            ; (34)
+        inc     sp+1            ; (-1+5)
+done:   tya                     ; (36)
 
+.else        
+
+        ldy     #0              ; (4)
+        adc     (sp),y          ; (9) lo byte
+        iny                     ; (11)
+        sta     tmp1            ; (14) save it
+        txa                     ; (16) 
+        adc     (sp),y          ; (21) hi byte
+        tax                     ; (23)
+        clc                     ; (25)
+        lda     sp              ; (28)
+        adc     #2              ; (30)
+        sta     sp              ; (33)
+        bcc     L1              ; (36)
+        inc     sp+1            ; (-1+5)
+L1:     lda     tmp1            ; (39) restore low byte
+
+.endif
+        rts                     ; (6502: 45 cycles, 26 bytes <-> 65SC02: 42 cycles, 22 bytes )
 
 ;
 ; Ullrich von Bassewitz, 23.11.2002
+; Christian Krueger, 11-Mar-2017, saved 5 bytes
 ;
 ; CC65 runtime: Convert char in ax into a long
 ;
 
 ; Convert A from char to long in EAX
 
-aulong: ldx     #0
-        stx     sreg
-        stx     sreg+1
-        rts
+along:  ldx     #$ff
+        cmp     #$80            ; Positive?
+        bcs     store           ; no, apply $FF
 
-along:  cmp     #$80            ; Positive?
-        bcc     aulong          ; Yes, handle like unsigned type
-        ldx     #$ff
-        stx     sreg
+aulong: ldx     #0
+store:  stx     sreg
         stx     sreg+1
         rts
-
-
+        
\ No newline at end of file
 
 ;
 ; Ullrich von Bassewitz, 07.04.2000
+; Christian Krueger, 12-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: += operator
 ;
         .export         laddeq1, laddeqa, laddeq
         .importzp       sreg, ptr1, tmp1
 
+        .macpack        cpu
 
 laddeq1:
         lda     #$01
         stx     sreg+1
 
 laddeq: sty     ptr1+1                  ; Store high byte of address
-        ldy     #$00                    ; Address low byte
         clc
 
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        adc     (ptr1)
+        sta     (ptr1)
+        ldy     #$01                    ; Address byte 1
+.else
+        ldy     #$00                    ; Address low byte
         adc     (ptr1),y
         sta     (ptr1),y
+        iny                             ; Address byte 1
+.endif
         pha                             ; Save byte 0 of result for later
 
-        iny                             ; Address byte 1
         txa
         adc     (ptr1),y                ; Load byte 1
         sta     (ptr1),y
 
 ;
 ; Ullrich von Bassewitz, 06.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: and on longs
 ;
         .import         addysp1
         .importzp       sp, sreg, tmp1
 
+        .macpack        cpu
                                    
 tosand0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif  
 
 tosandeax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        and     (sp)            ; byte 0
+        ldy     #1
+.else
         ldy     #0
         and     (sp),y          ; byte 0
-        sta     tmp1
         iny
+.endif        
+        sta     tmp1
         txa
         and     (sp),y          ; byte 1
         tax
 
 ;  
 ; Ullrich von Bassewitz, 06.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: function epilogue
 ;
         .import         addysp
         .importzp       sp
 
+        .macpack        cpu
+
 leave00:
         lda     #0
 leave0: ldx     #0
         ldx     #0              ; return < 256
 leavey:
         jsr     addysp          ; drop stack frame
+
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+
+leave:  tay                     ; save A a sec
+        lda     (sp)            ; that's the pushed arg size
+        sec                     ; Count the byte, the count's stored in
+        adc     sp
+        sta     sp
+        bcc     L1
+        inc     sp+1
+L1:     tya                     ; Get return value back
+
+.else
+
 leave:  pha                     ; save A a sec
         ldy     #0
         lda     (sp),y          ; that's the pushed arg size
         bcc     L1
         inc     sp+1
 L1:     pla                     ; Get return value back
+
+.endif
         rts
 
 
 ;
 ; Ullrich von Bassewitz, 07.08.1998
-;
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ; CC65 runtime: modulo operation for long signed ints
 ;
 
         .import         poplsargs, udiv32, negeax
         .importzp       sreg, ptr1, ptr2, tmp1, tmp3, tmp4
 
+        .macpack        cpu
+
 tosmod0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif
 
 tosmodeax:                         
         jsr     poplsargs       ; Get arguments from stack, adjust sign
 
 ;
 ; Ullrich von Bassewitz, 13.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: multiplication for long (unsigned) ints
 ;
         .import         addysp1
         .importzp       sp, sreg, tmp1, tmp2, tmp3, tmp4, ptr1, ptr3, ptr4
 
+        .macpack        cpu
+
 tosmul0ax:
 tosumul0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00    
         sty     sreg
         sty     sreg+1
+.endif        
 
 tosmuleax:
 tosumuleax:
 mul32:  sta     ptr1
         stx     ptr1+1          ; op2 now in ptr1/sreg
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        lda     (sp)
+        ldy     #1
+.else        
         ldy     #0
         lda     (sp),y
-        sta     ptr3
         iny
+.endif
+        sta     ptr3
         lda     (sp),y
         sta     ptr3+1
         iny
 
 ;
 ; Ullrich von Bassewitz, 06.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: or on longs
 ;
         .import         addysp1
         .importzp       sp, sreg, tmp1
                                   
+        .macpack        cpu
 
 tosor0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
-        sty     sreg+1 
+        sty     sreg+1
+.endif  
 
 tosoreax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        ora     (sp)
+        ldy     #1
+.else
         ldy     #0
         ora     (sp),y          ; byte 0
-        sta     tmp1
         iny
+.endif        
+        sta     tmp1
         txa
         ora     (sp),y          ; byte 1
         tax
 
 ;
 ; Ullrich von Bassewitz, 29.12.1999
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: long pop
 ;
         .import         incsp4
         .importzp       sp, sreg
 
+        .macpack        cpu
 
 popeax: ldy     #3
         lda     (sp),y
         dey
         lda     (sp),y
         tax
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        lda     (sp)
+.else        
         dey
         lda     (sp),y
+.endif
         jmp     incsp4
 
 
 
 ;
 ; Ullrich von Bassewitz, 06.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: long push
 ;
         .import         decsp4
         .importzp       sp, sreg
 
+        .macpack        cpu
+
 pushl0:
         lda     #0
         tax
 push0ax:
-        ldy     #0
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
+        ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif
 pusheax:
         pha                     ; decsp will destroy A (but not X)
         jsr     decsp4
         dey
         txa
         sta     (sp),y
-        dey
         pla
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        sta     (sp)
+.else        
+        dey
         sta     (sp),y
+.endif        
         rts
 
 
 ;
 ; Ullrich von Bassewitz, 05.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: long sub reversed
 ;
         .import         addysp1
         .importzp       sp, sreg, tmp1
 
+        .macpack        cpu
+
 tosrsub0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif
 
-tosrsubeax:                         
-        ldy     #0
+tosrsubeax:
         sec
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        sbc     (sp)
+        ldy     #1
+.else                         
+        ldy     #0
         sbc     (sp),y          ; byte 0
+        iny
+.endif
         sta     tmp1            ; use as temp storage
         txa
-        iny
         sbc     (sp),y          ; byte 1
         tax
         iny
 
 ;
 ; Ullrich von Bassewitz, 08.08.1998
+; Christian Krueger, 11-Mar-2017, optimization
 ;
 ; CC65 runtime: save ax into temp storage/restore ax from temp storage
 ;
 saveeax:
         sta     regsave
         stx     regsave+1
-        lda     sreg
-        sta     regsave+2
-        lda     sreg+1
-        sta     regsave+3
-        lda     regsave
+        ldy     sreg
+        sty     regsave+2
+        ldy     sreg+1
+        sty     regsave+3
         rts
 
 resteax:
 
 ;
 ; Ullrich von Bassewitz, 05.08.1998
-;
+; Christian Krueger, 11-Mar-2017, ímproved  65SC02 optimization
 ; CC65 runtime: long sub
 ;
 
         .macpack        cpu
 
 tossub0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif  
 
 tossubeax:
         sec
         eor     #$FF
-.if (.cpu .bitand CPU_ISET_65SC02)
+.if (.cpu .bitand ::CPU_ISET_65SC02)
         adc     (sp)            ; 65SC02 version - saves 2 cycles
         ldy     #1
 .else
 
 ;                                 
 ; Ullrich von Bassewitz, 07.04.2000
+; Christian Krueger, 12-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: -= operator
 ;
         .export         lsubeq1, lsubeqa, lsubeq
         .importzp       sreg, ptr1
 
+        .macpack        cpu
 
 lsubeq1:
         lda     #$01
         stx     sreg+1
 
 lsubeq: sty     ptr1+1                  ; Store high byte of address
-        ldy     #$00                    ; Address low byte
+    
         sec
-
         eor     #$FF
+ .if (.cpu .bitand ::CPU_ISET_65SC02)
+        adc     (ptr1)                  ; Subtract byte 0
+        sta     (ptr1)
+        ldy     #$01                    ; Address byte 1               
+ .else
+        ldy     #$00                    ; Address low byte
         adc     (ptr1),y                ; Subtract byte 0
         sta     (ptr1),y
+        iny                             ; Address byte 1       
+ .endif  
         pha                             ; Save byte 0 of result for later
-
-        iny                             ; Address byte 1
         txa
         eor     #$FF
         adc     (ptr1),y                ; Subtract byte 1
 
 ;
 ; Ullrich von Bassewitz, 17.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: division for long unsigned ints
 ;
         .import         addysp1
         .importzp       sp, sreg, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
 
+        .macpack        cpu
+
 tosudiv0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif
 
 tosudiveax:                         
         jsr     getlop          ; Get the paramameters
         lda     sreg+1
         sta     ptr4+1
 
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        lda     (sp)
+        ldy     #1
+.else
         ldy     #0              ; Put left operand in place
         lda     (sp),y
-        sta     ptr1
         iny
+.endif
+        sta     ptr1
         lda     (sp),y
         sta     ptr1+1
         iny
 
 ;
 ; Ullrich von Bassewitz, 27.09.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: modulo operation for long unsigned ints
 ;
         .import         getlop, udiv32
         .importzp       sreg, tmp3, tmp4, ptr2
 
+        .macpack        cpu
+
 tosumod0ax:                          
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif
 
 tosumodeax:
         jsr     getlop          ; Get the paramameters
 
 ;
 ; Ullrich von Bassewitz, 06.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: xor on longs
 ;
         .import         addysp1
         .importzp       sp, sreg, tmp1
 
+        .macpack        cpu
+
 tosxor0ax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        stz     sreg
+        stz     sreg+1
+.else
         ldy     #$00
         sty     sreg
         sty     sreg+1
+.endif
 
-tosxoreax:                         
+tosxoreax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        eor     (sp)            ; byte 0
+        ldy     #1
+.else                         
         ldy     #0
         eor     (sp),y          ; byte 0
-        sta     tmp1
         iny
+.endif
+        sta     tmp1
         txa
         eor     (sp),y          ; byte 1
         tax
 
 ;
 ; Ullrich von Bassewitz, 05.10.1998
+; Christian Krueger, 11-Mar-2017, optimization
 ;
 ; CC65 runtime: Make boolean according to flags
 ;
 
 
 boolne: bne     ret1
-        ldx     #$00
+ret0:   ldx     #$00
         txa
         rts
 
 
-booleq: beq     ret1
-        ldx     #$00
-        txa
+booleq: bne     ret0
+ret1:   ldx     #$00
+        lda     #$01
         rts
 
 
 
 
 boolugt:
-        beq     L1
+        beq     ret0
 booluge:
-        bcs     ret1
-L1:     ldx     #$00
+        ldx     #$00
         txa
+        rol     a
         rts
-
-
-ret1:   ldx     #$00
-        lda     #$01
-        rts
-
-
-
 
 ;
 ; Ullrich von Bassewitz, 05.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: or on ints
 ;
         .import         addysp1
         .importzp       sp, tmp1
 
+        .macpack        cpu
+
 tosora0:
         ldx     #$00
 tosorax:
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        ora     (sp)
+        ldy     #1
+.else
         ldy     #0
         ora     (sp),y
-        sta     tmp1
         iny
+.endif        
+        sta     tmp1
         txa
         ora     (sp),y
         tax
 
 ;
 ; Ullrich von Bassewitz, 05.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: sub ints reversed
 ;
         .import         addysp1
         .importzp       sp, tmp1
 
+        .macpack        cpu
+
 ;
 ; AX = AX - TOS
 ;
 tosrsuba0:
         ldx     #0
 tosrsubax:
-        ldy     #0
         sec
+.if (.cpu .bitand CPU_ISET_65SC02)
+        sbc     (sp)
+        ldy     #1
+.else
+        ldy     #0
         sbc     (sp),y          ; lo byte
+        iny
+.endif
         sta     tmp1            ; save lo byte
         txa
-        iny
         sbc     (sp),y          ; hi byte
         tax
         lda     tmp1
 
 ;
 ; Ullrich von Bassewitz, 26.10.2000
+; Christian Krueger, 12-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: Store a/x indirect into address at top of stack with index
 ;
         .import         incsp2
         .importzp       sp, tmp1, ptr1
 
+        .macpack        cpu
+
 .proc   staxspidx
 
         sty     tmp1            ; Save Y
         ldy     #1
         lda     (sp),y
         sta     ptr1+1
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        lda     (sp)
+.else
         dey
         lda     (sp),y
+.endif
         sta     ptr1            ; Address now in ptr1
         ldy     tmp1            ; Restore Y
         iny                     ; Address high byte
 
 ;
 ; Ullrich von Bassewitz, 06.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: swap ax with TOS
 ;
         .export         swapstk
         .importzp       sp, ptr4
 
+        .macpack        cpu
+
 swapstk:
         sta     ptr4
         stx     ptr4+1
         tax
         lda     ptr4+1
         sta     (sp),y
+.if (.cpu .bitand ::CPU_ISET_65SC02)
+        lda     (sp)
+        tay
+        lda     ptr4
+        sta     (sp)
+        tya    
+.else        
         dey
         lda     (sp),y
         pha
         lda     ptr4
         sta     (sp),y
         pla
+.endif        
         rts                     ; whew!
-
 
 ;
 ; Ullrich von Bassewitz, 05.08.1998
+; Christian Krueger, 11-Mar-2017, added 65SC02 optimization
 ;
 ; CC65 runtime: xor on ints
 ;
         .import         addysp1
         .importzp       sp, tmp1
 
+        .macpack        cpu
+
 tosxora0:
         ldx     #$00
 tosxorax:
+.if (.cpu .bitand CPU_ISET_65SC02)
+        eor     (sp)
+        ldy     #1
+.else
         ldy     #0
         eor     (sp),y
-        sta     tmp1
         iny
+.endif
+        sta     tmp1
         txa
         eor     (sp),y
         tax