]> git.sur5r.net Git - cc65/blob - libsrc/osic1p/osiscreen.inc
added optimization for indexed 16-bit array load of form (array[i & 0x7f])
[cc65] / libsrc / osic1p / osiscreen.inc
1 ;
2 ; Macro definitions for screen layout modules
3 ;
4
5         .include        "extzp.inc"
6         
7 .linecont +
8
9 ;
10 ; Internal function for screensize()
11 ;
12 .macro  osi_screensize ScrWidth, ScrHeight
13                                 ; Macro implementation of internal screensize
14                                 ; function for given width and height in
15                                 ; characters
16                                 
17         .export         screensize
18
19 .proc   screensize
20         ldx     #ScrWidth
21         ldy     #ScrHeight
22         rts
23 .endproc
24 .endmacro
25
26 ;
27 ; void clrscr (void);
28 ;
29 .macro  osi_clrscr ScrBase, ScrRamSize
30
31         .export         _clrscr
32
33 .proc   _clrscr
34         lda     #<ScrBase       ; Fill whole video RAM with blanks by calling
35         ldx     #>ScrBase       ; memset appropriately
36         jsr     pushax
37         
38         lda     #' '
39         ldx     #$00
40         jsr     pushax
41         
42         lda     #<ScrRamSize
43         ldx     #>ScrRamSize
44         jsr     _memset
45
46         lda     #$00            ; Cursor in upper left corner
47         sta     CURS_X
48         sta     CURS_Y
49         
50         jmp     plot            ; Set the cursor position
51 .endproc
52
53 .endmacro
54
55 ;
56 ; cputc/cputcxy for Challenger 1P
57 ; Based on PET/CBM implementation
58 ;
59
60 .macro  osi_cputfuncs ScrBase, ScrFirstChar, ScrWidth, ScrHeight, \
61                          ScrollDist, ScrLo, ScrHi
62
63                                 ; Number of characters to move for scrolling
64                                 ; by one line
65 ScrollLength    = (ScrHeight - 1) * ScrollDist
66
67 ;
68 ; void cputcxy (unsigned char x, unsigned char y, char c);
69 ; void cputc (char c);
70 ;
71         .export         _cputcxy, _cputc, cputdirect, putchar
72         .export         newline, plot
73
74 _cputcxy:
75         pha                     ; Save C
76         jsr     gotoxy          ; Set cursor, drop x and y
77         pla                     ; Restore C
78
79 ; Plot a character - also used as internal function
80
81 _cputc: cmp     #$0A            ; CR?
82         bne     L1
83         lda     #0
84         sta     CURS_X
85         beq     plot            ; Recalculate pointers
86
87 L1:     cmp     #$0D            ; LF?
88         beq     newline         ; Recalculate pointers
89
90 cputdirect:
91         jsr     putchar         ; Write the character to the screen
92
93 ; Advance cursor position, register Y contains horizontal position after
94 ; putchar
95
96         cpy     #(ScrWidth - 1) ; Check whether line is full
97         bne     L3
98         jsr     newline         ; New line
99         ldy     #$FF            ; + cr
100 L3:     iny
101         sty     CURS_X
102         rts
103
104 newline:
105         inc     CURS_Y
106         lda     CURS_Y
107         cmp     #ScrHeight      ; Screen height
108         bne     plot
109         dec     CURS_Y          ; Bottom of screen reached, scroll
110
111                                 ; Scroll destination address
112         lda     #<(ScrBase + ScrFirstChar)
113         ldx     #>(ScrBase + ScrFirstChar)
114         jsr     pushax
115         
116                                 ; Scroll source address
117         lda     #<(ScrBase + ScrFirstChar + ScrollDist)
118         ldx     #>(ScrBase + ScrFirstChar + ScrollDist)
119         jsr     pushax
120         
121                                 ; Number of characters to move
122         lda     #<ScrollLength
123         ldx     #>ScrollLength
124         jsr     _memmove
125
126                                 ; Address of first character in last line
127                                 ; of screen
128         lda     #<(ScrBase + ScrFirstChar + ScrollLength)
129         sta     ptr1
130         lda     #>(ScrBase + ScrFirstChar + ScrollLength)
131         sta     ptr1+1
132         
133         ldy     #ScrWidth       ; Fill last line with blanks
134         lda     #' '
135 clrln:  sta     (ptr1),y
136         dey
137         bpl     clrln
138
139 plot:   ldy     CURS_Y
140         lda     ScrLo,y
141         sta     SCREEN_PTR
142         lda     ScrHi,y
143         sta     SCREEN_PTR+1
144         rts
145
146 ; Write one character to the screen without doing anything else, return X
147 ; position in register Y
148
149 putchar:
150         ldy     CURS_X
151         sta     (SCREEN_PTR),y  ; Set char
152         rts
153         
154 .endmacro
155
156 .macro osi_screen_funcs ScrBase, ScrRamSize, ScrFirstChar, \
157                         ScrWidth, ScrHeight, ScrollDist
158
159         .import         gotoxy
160         .import         _memmove, _memset, pushax
161         .importzp       ptr1
162
163 .rodata
164
165 ; Screen address tables - offset to real screen
166 ScrTabLo:
167         .repeat ScrHeight, I
168         .byte <(ScrBase + ScrFirstChar + I * ScrollDist)
169         .endrep
170         
171 ScrTabHi:
172         .repeat ScrHeight, I
173         .byte >(ScrBase + ScrFirstChar + I * ScrollDist)
174         .endrep
175
176 .code
177
178 osi_cputfuncs   ScrBase, ScrFirstChar, ScrWidth, ScrHeight, \
179                          ScrollDist, ScrTabLo, ScrTabHi
180 osi_screensize  ScrWidth, ScrHeight
181 osi_clrscr      ScrBase, ScrRamSize
182
183 .endmacro