]> git.sur5r.net Git - cc65/blob - libsrc/c64/soft80_cputc.s
some more rework, second attempt on fixing the color issues. added ifdefs to disable...
[cc65] / libsrc / c64 / soft80_cputc.s
1 ;
2 ; void cputcxy (unsigned char x, unsigned char y, char c);
3 ; void cputc (char c);
4 ;
5
6         .export         soft80_cputcxy, soft80_cputc
7         .export         soft80_cputdirect, soft80_putchar
8         .export         soft80_newline, soft80_plot
9
10         .import         popa, _gotoxy
11         .import         xsize
12         .import         soft80_kplot
13         .import         __bgcolor
14
15         .importzp       tmp4,tmp3
16
17         .macpack        longbranch
18
19         .include        "c64.inc"
20         .include        "soft80.inc"
21
22 .if SOFT80COLORVOODOO = 1
23         .export         soft80_putcolor
24 .endif
25
26 soft80_cputcxy:
27         pha                     ; Save C
28         jsr     popa            ; Get Y
29         jsr     _gotoxy         ; Set cursor, drop x
30         pla                     ; Restore C
31
32 ; Plot a character - also used as internal function
33
34 soft80_cputc:
35         cmp     #$0A            ; CR?
36         bne     L1
37
38         lda     #0
39         sta     CURS_X
40
41         ; Set cursor position, calculate RAM pointers
42 soft80_plot:
43         ldx     CURS_Y
44         ldy     CURS_X
45         clc
46         jmp     soft80_kplot    ; Set the new cursor
47
48 L1:     cmp     #$0D            ; LF?
49         beq     soft80_newline  ; Recalculate pointers
50
51         ; Printable char of some sort
52
53         tay
54         bpl     L10
55
56         ; extra check for petscii codes 160-191, these have been moved to
57         ; 0-31 in the charset
58         and     #%11100000
59         cmp     #%10100000
60         bne     @sk
61
62         tya
63         and     #%00011111
64         bpl     L10             ; branch always
65 @sk:
66         tya
67         clc
68         adc     #$20
69         and     #$7F
70 L10:
71
72 soft80_cputdirect:
73         jsr     soft80_putchar  ; Write the character to the screen
74
75 ; Advance cursor position
76
77 advance:
78         iny                     ; contains CURS_X
79         cpy     #charsperline
80         beq     L3
81
82         sty     CURS_X
83         tya
84         and     #$01
85         bne     @L5
86
87         lda     SCREEN_PTR
88         clc
89         adc     #8
90         sta     SCREEN_PTR
91         bcc     @L4
92         inc     SCREEN_PTR+1
93 @L4:
94         inc     CRAM_PTR
95         bne     @L5
96         inc     CRAM_PTR+1
97 @L5:
98         rts
99 L3:
100         inc     CURS_Y          ; new line
101         ldy     #0              ; + cr
102         sty     CURS_X
103         jmp     soft80_plot
104
105 soft80_newline:
106
107         lda     SCREEN_PTR
108         clc
109         adc     #<(40*8)
110         sta     SCREEN_PTR
111
112         lda     SCREEN_PTR+1
113         adc     #>(40*8)
114         sta     SCREEN_PTR+1
115
116         lda     CRAM_PTR
117         clc
118         adc     #40
119         sta     CRAM_PTR
120         bcc     L5
121         inc     CRAM_PTR+1
122 L5:
123         inc     CURS_Y
124         rts
125
126 ; Write one character to the screen without doing anything else
127 ; in:         A:  character
128 ; returns:    Y:  cursor X position
129 ; this function is going to be used a lot so we unroll it a bit for speed
130
131 .if SOFT80FASTSPACE = 1
132 ; output space
133 ; in: y must be $00
134 _space:
135
136         lda     RVS
137         jne     _spaceinvers
138
139 .if SOFT80COLORVOODOO = 1
140         jsr     remcolor
141 .endif
142         ;ldy     #$00            ; is still $00
143
144         lda     CURS_X
145         and     #$01
146         bne     @l1
147
148         .repeat 8,line
149         lda     (SCREEN_PTR),y
150         ora     #$f0
151         sta     (SCREEN_PTR),y
152         .if (line < 7)
153         iny
154         .endif
155         .endrepeat
156         jmp     _back
157 @l1:
158         .repeat 8,line
159         lda     (SCREEN_PTR),y
160         ora     #$0f
161         sta     (SCREEN_PTR),y
162         .if line < 7
163         iny
164         .endif
165         .endrepeat
166 @l2:
167         jmp     _back
168
169 ; output inverted space
170 ; in: y must be $00
171 _spaceinvers:
172
173 .if SOFT80COLORVOODOO = 1
174         jsr     soft80_putcolor
175 .else
176         lda     CHARCOLOR
177         sta     (CRAM_PTR),y    ; vram
178 .endif
179
180         lda     CURS_X
181         and     #$01
182         bne     @l1
183
184         .repeat 8,line
185         lda     (SCREEN_PTR),y
186         and     #$0f
187         sta     (SCREEN_PTR),y
188         .if line < 7
189         iny
190         .endif
191         .endrepeat
192         jmp     _back
193 @l1:
194         .repeat 8,line
195         lda     (SCREEN_PTR),y
196         and     #$f0
197         sta     (SCREEN_PTR),y
198         .if line < 7
199         iny
200         .endif
201         .endrepeat
202
203         jmp     _back
204 .endif
205
206 ; output a character
207
208 soft80_putchar:
209         sta     tmp3            ; remember charcode
210
211         sei
212         ldx     $01
213         stx     tmp4
214         ldx     #$34
215
216         stx     $01             ; will stay $34 for space
217         ldy     #$00            ; will be $00 from now on
218
219 .if SOFT80FASTSPACE = 1
220         cmp     #' '            ; space is a special (optimized) case
221         jeq     _space
222 .endif
223
224 .if SOFT80COLORVOODOO = 1
225         jsr     soft80_putcolor
226 .else
227         lda     CHARCOLOR
228         sta     (CRAM_PTR),y    ; vram
229 .endif
230         ; output character
231
232         ldx     tmp3            ; get charcode
233
234         lda     RVS
235         jne     _invers
236
237         lda     CURS_X
238         and     #$01
239         bne     @l1
240
241         .repeat 8,line
242         lda     (SCREEN_PTR),y
243         and     #$0f
244         ora     soft80_hi_charset+(line*$80),x
245         sta     (SCREEN_PTR),y
246         .if line < 7
247         iny
248         .endif
249         .endrepeat
250         jmp     @l2
251 @l1:
252
253         .repeat 8,line
254         lda     (SCREEN_PTR),y
255         and     #$f0
256         ora     soft80_lo_charset+(line*$80),x
257         sta     (SCREEN_PTR),y
258         .if line < 7
259         iny
260         .endif
261         .endrepeat
262
263 @l2:
264
265 _back:
266         lda     tmp4
267         sta     $01
268         cli
269
270         ldy     CURS_X
271         rts
272
273 ; output inverted character
274 _invers:
275
276         lda     CURS_X
277         and     #$01
278         bne     @l1
279
280         .repeat 8,line
281         lda     (SCREEN_PTR),y
282         ora     #$f0
283         eor     soft80_hi_charset+(line*$80),x
284         sta     (SCREEN_PTR),y
285         .if line < 7
286         iny
287         .endif
288         .endrepeat
289         jmp     _back
290 @l1:
291         .repeat 8,line
292         lda     (SCREEN_PTR),y
293         ora     #$0f
294         eor     soft80_lo_charset+(line*$80),x
295         sta     (SCREEN_PTR),y
296         .if line < 7
297         iny
298         .endif
299         .endrepeat
300         jmp     _back
301
302 ;-------------------------------------------------------------------------------
303
304 .if SOFT80COLORVOODOO = 1
305 ;--- start color vodoo
306
307 ; remove color from cell, called before putting a "space" character to the bitmap
308 ; y unmodified
309 remcolor:
310
311         ;ldy     #$00            ; is still $00
312
313         ; if the textcolor in vram is equal to the background color, then
314         ; no (visible) character is in the current cell and we can exit
315         ; immediately.
316         lda     (CRAM_PTR),y    ; vram (textcolor)
317         and     #$0f
318         cmp     __bgcolor
319         jeq     @l2b            ; yes, vram==bgcolor
320
321         ; now check if the textcolor in color ram is equal the background color,
322         ; if yes then there is only one (visible) character in the current cell
323         inc     $01
324         lda     (CRAM_PTR),y    ; colram (2nd textcolor)
325         stx     $01             ;$34
326         and     #$0f
327         cmp     __bgcolor
328         beq     @l2s            ; yes, colram==bgcolor
329
330         ; two characters in the current cell, of which one will get removed
331
332         ; vram = colram
333         ;inc     $01
334         ;lda     (CRAM_PTR),y    ; colram
335         ;stx     $01             ;$34
336         ;and     #$0f
337
338         ; move 2nd text color to vram
339         sta     tmp3
340         lda     (CRAM_PTR),y    ; vram
341         and     #$f0
342         ora     tmp3
343         sta     (CRAM_PTR),y    ; vram
344
345         ; colram = bgcolor
346         lda     __bgcolor
347         inc     $01
348         sta     (CRAM_PTR),y    ; colram
349         stx     $01 ;$34
350
351         jmp     @l2b
352
353 @l2s:
354         ; colram is bgcolor
355         ; => only one char in cell used
356
357         jsr     soft80_checkchar
358         bcc     @l2b            ; space at current position
359
360         ; vram (textcolor) = bgcolor
361         lda     (CRAM_PTR),y    ; vram
362         and     #$f0
363         ora     __bgcolor
364         sta     (CRAM_PTR),y    ; vram
365 @l2b:
366         rts
367
368 ; put color to cell
369 ; in: $01 is $34 (RAM under I/O) when entering
370 ;     y must be $00
371 ; out: y = $00
372 soft80_putcolor:
373
374         ;ldy     #$00            ; is still $00
375
376         lda     (CRAM_PTR),y    ; vram
377         and     #$0f
378         cmp     __bgcolor
379         beq     @l2s            ; vram==bgcolor => first char in cell
380
381         ; vram!=bgcolor => second char in cell
382
383         inc     $01             ;$35
384         lda     (CRAM_PTR),y    ; colram
385         stx     $01             ;$34
386         and     #$0f
387         cmp     __bgcolor
388         bne     @l2s            ; colram!=bgcolor
389
390         ; colram==bgcolor => second char in cell or overwrite 1st char
391
392         jsr     soft80_checkchar
393         bcs     @l2a            ; char at current position => overwrite 1st
394
395         ; colram=vram
396         lda     (CRAM_PTR),y    ; vram
397         inc     $01
398         sta     (CRAM_PTR),y    ; colram
399         stx     $01 ;$34
400
401         ;jmp     @l2a
402
403 @l2s:
404         ; colram!=bgcolor => alread 2 chars in cell
405 @l2a:
406
407         ; Set color
408         lda     CHARCOLOR
409         sta     (CRAM_PTR),y    ; vram
410
411         rts
412
413         ;.export soft80_checkchar
414
415 ; test if there is a space or a character at current position
416 ; in:  y must be $00
417 ; out: CLC: space        SEC: character
418 ;      y = $00
419 soft80_checkchar:
420
421         ;ldy     #$00            ; is still $00
422
423         lda     CURS_X
424         and     #$01
425         jne     @l1a
426
427         .repeat 8,line
428         lda     (SCREEN_PTR),y
429         and     #$f0
430         cmp     #$f0
431         bne     @l2b
432         .if (line < 7)
433         iny
434         .endif
435         .endrepeat
436
437         ldy     #$00
438         clc
439         rts
440 @l2b:
441         ldy     #$00
442         sec
443         rts
444 @l1a:
445         .repeat 8,line
446         lda     (SCREEN_PTR),y
447         and     #$0f
448         cmp     #$0f
449         bne     @l2bb
450         .if line < 7
451         iny
452         .endif
453         .endrepeat
454         ldy     #$00
455         clc
456         rts
457 @l2bb:
458         ldy     #$00
459         sec
460         rts
461
462 ;--- end color vodoo
463 .endif