]> git.sur5r.net Git - cc65/blob - libsrc/atari/scroll.s
Optimized mul20 & mul40 and extracted to new library.
[cc65] / libsrc / atari / scroll.s
1 ;
2 ; Christian Groessler, June 2004
3 ;
4 ; void __fastcall__ _scroll (signed char numlines);
5 ; numlines > 0  scrolls up
6 ; numlines < 0  scrolls down
7 ;
8
9         .include        "atari.inc"
10         .importzp       tmp1,tmp4,ptr1,ptr2
11         .import         _mul40,_clrscr
12         .export         __scroll
13
14 .proc   __scroll
15
16         cmp     #0
17         beq     jmpfin
18 ;       cmp     #$80
19 ;       bcc     up
20         bpl     up
21
22 ;scroll down
23         eor     #$ff
24         clc
25         adc     #1              ; make positive
26         sta     tmp1
27
28         cmp     #24             ; scroll >= the whole screen?
29         bcc     down_ok
30         jmp     _clrscr
31
32 down_ok:lda     SAVMSC
33         clc
34         adc     #<(40*23)
35         sta     ptr1
36         sta     ptr2
37         lda     SAVMSC+1
38         adc     #>(40*23)
39         sta     ptr1+1          ; point to last line on screen
40         sta     ptr2+1
41
42         lda     tmp1
43         jsr     _mul40
44         sta     tmp4
45         lda     ptr2
46         sec
47         sbc     tmp4
48         sta     ptr2
49         stx     tmp4
50         lda     ptr2+1
51         sbc     tmp4
52         sta     ptr2+1
53
54         lda     #24             ; # of lines on screen
55         sec
56         sbc     tmp1            ; # of lines to move
57         tax
58
59         ;very simple, could be improved
60
61 scrold: ldy     #39             ; # of chars on a line - 1
62 copy_d: lda     (ptr2),y
63         sta     (ptr1),y
64         dey
65         bpl     copy_d
66         lda     ptr1
67         sec
68         sbc     #40
69         sta     ptr1
70         bcs     u1
71         dec     ptr1+1
72 u1:     lda     ptr2
73         sec
74         sbc     #40
75         sta     ptr2
76         bcs     u2
77         dec     ptr2+1
78 u2:     dex
79         bne     scrold
80
81         ; fill new scrolled in lines with space
82
83         ldx     tmp1            ; # of new lines
84 fild:   lda     #0
85         ldy     #39
86 fill_d: sta     (ptr1),y
87         dey
88         bpl     fill_d
89         dex
90 jmpfin: beq     finish
91         lda     ptr1
92         sec
93         sbc     #40
94         sta     ptr1
95         bcs     u3
96         dec     ptr1+1
97 u3:     jmp     fild
98
99 ;scroll up
100 up:     sta     tmp1            ; # of lines to scroll
101         cmp     #24             ; scroll >= the whole screen?
102         bcc     up_ok
103         jmp     _clrscr
104
105         ;multiply by 40 (xsize)
106 up_ok:  jsr     _mul40          ; carry is cleared by _mul40
107         adc     SAVMSC          ; add start of screen mem
108         sta     ptr2
109         txa
110         adc     SAVMSC+1
111         sta     ptr2+1
112         lda     SAVMSC+1
113         sta     ptr1+1
114         lda     SAVMSC
115         sta     ptr1
116         lda     #24             ; # of lines on screen
117         sec
118         sbc     tmp1            ; # of lines to move
119         tax
120
121         ;very simple, could be improved
122
123 scroll: ldy     #39             ; # of chars on a line - 1
124 copy_l: lda     (ptr2),y
125         sta     (ptr1),y
126         dey
127         bpl     copy_l
128         lda     #40
129         clc
130         adc     ptr1
131         sta     ptr1
132         bcc     l1
133         inc     ptr1+1
134 l1:     lda     #40
135         clc
136         adc     ptr2
137         sta     ptr2
138         bcc     l2
139         inc     ptr2+1
140 l2:     dex
141         bne     scroll
142
143         ; fill new scrolled in lines with space
144
145         ldx     tmp1            ; # of new lines
146 fill:   lda     #0
147         ldy     #39
148 fill_l: sta     (ptr1),y
149         dey
150         bpl     fill_l
151         dex
152         beq     finish
153         lda     #40
154         clc
155         adc     ptr1
156         sta     ptr1
157         bcc     l3
158         inc     ptr1+1
159 l3:     jmp     fill
160
161 finish: rts
162
163 .endproc