2 ; Graphics driver for the 320x200x2 mode on the C64.
4 ; Based on Stephen L. Judds GRLIB code
7 .include "zeropage.inc"
9 .include "tgi-kernel.inc"
10 .include "tgi-mode.inc"
11 .include "tgi-error.inc"
17 ; ------------------------------------------------------------------------
18 ; Header. Includes jump table and constants.
22 ; First part of the header is a structure that has a magic and defines the
23 ; capabilities of the driver
25 .byte $74, $67, $69 ; "tgi"
26 .byte TGI_API_VERSION ; TGI API version number
27 .word 320 ; X resolution
28 .word 200 ; Y resolution
29 .byte 2 ; Number of drawing colors
30 .byte 1 ; Number of screens available
31 .byte 8 ; System font X size
32 .byte 8 ; System font Y size
33 .res 4, $00 ; Reserved for future extensions
35 ; Next comes the jump table. Currently all entries must be valid and may point
36 ; to an RTS for test versions (function not implemented). A future version may
37 ; allow for emulation: In this case the vector will be zero. Emulation means
38 ; that the graphics kernel will emulate the function by using lower level
39 ; primitives - for example ploting a line by using calls to SETPIXEL.
61 .addr 0 ; IRQ entry is unused
63 ; ------------------------------------------------------------------------
66 ; Variables mapped to the zero page segment variables. Some of these are
67 ; used for passing parameters to the driver.
75 ROW := tmp2 ; Bitmap row...
76 COL := tmp3 ; ...and column, both set by PLOT
80 INRANGE := regsave+2 ; PLOT variable, $00 = coordinates in range
82 CHUNK := X2 ; Used in the line routine
83 OLDCHUNK := X2+1 ; Dito
85 ; Absolute variables used in the code
89 ERROR: .res 1 ; Error code
90 PALETTE: .res 2 ; The current palette
92 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
95 OLDD018: .res 1 ; Old register value
101 ; Circle routine stuff, overlaid by BAR variables
106 BROW: .res 1 ; Bottom row
107 TROW: .res 1 ; Top row
109 LCOL: .res 1 ; Left column
110 RCOL: .res 1 ; Right column
122 ; Constants and tables
126 DEFPALETTE: .byte $00, $01 ; White on black
127 PALETTESIZE = * - DEFPALETTE
129 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
130 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
132 VBASE = $E000 ; Video memory base address
133 CBASE = $D000 ; Color memory base address
138 ; ------------------------------------------------------------------------
139 ; INSTALL routine. Is called after the driver is loaded into memory. May
140 ; initialize anything that has to be done just once. Is probably empty
143 ; Must set an error code: NO
150 ; ------------------------------------------------------------------------
151 ; UNINSTALL routine. Is called before the driver is removed from memory. May
152 ; clean up anything done by INSTALL but is probably empty most of the time.
154 ; Must set an error code: NO
161 ; ------------------------------------------------------------------------
162 ; INIT: Changes an already installed device from text mode to graphics
164 ; Note that INIT/DONE may be called multiple times while the driver
165 ; is loaded, while INSTALL is only called once, so any code that is needed
166 ; to initializes variables and so on must go here. Setting palette and
167 ; clearing the screen is not needed because this is called by the graphics
169 ; The graphics kernel will never call INIT when a graphics mode is already
170 ; active, so there is no need to protect against that.
172 ; Must set an error code: YES
177 ; Initialize variables
182 ; Switch into graphics mode
184 lda $DD02 ; Set the data direction regs
188 and #$FC ; Switch to bank 3
193 lda #$48 ; Set color map to $D000, screen to $E000
196 lda $D011 ; And turn on bitmap
200 ; Done, reset the error code
206 ; ------------------------------------------------------------------------
207 ; DONE: Will be called to switch the graphics device back into text mode.
208 ; The graphics kernel will never call DONE when no graphics mode is active,
209 ; so there is no need to protect against that.
211 ; Must set an error code: NO
214 DONE: lda $DD02 ; Set the data direction regs
221 lda OLDD018 ; Screen mem --> $0400
229 ; ------------------------------------------------------------------------
230 ; GETERROR: Return the error code in A and clear it.
238 ; ------------------------------------------------------------------------
239 ; CONTROL: Platform/driver specific entry point.
241 ; Must set an error code: YES
245 lda #TGI_ERR_INV_FUNC
249 ; ------------------------------------------------------------------------
250 ; CLEAR: Clears the screen.
252 ; Must set an error code: NO
257 @L1: sta VBASE+$0000,y
293 ; ------------------------------------------------------------------------
294 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
295 ; The page number is already checked to be valid by the graphics kernel.
297 ; Must set an error code: NO (will only be called if page ok)
303 ; ------------------------------------------------------------------------
304 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
305 ; The page number is already checked to be valid by the graphics kernel.
307 ; Must set an error code: NO (will only be called if page ok)
313 ; ------------------------------------------------------------------------
314 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
315 ; to be in a valid range (0..maxcolor-1).
317 ; Must set an error code: NO (will only be called if color ok)
327 ; ------------------------------------------------------------------------
328 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
329 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
332 ; Must set an error code: YES
337 @L1: lda (ptr1),y ; Copy the palette
338 and #$0F ; Make a valid color
343 ; Get the color entries from the palette
345 lda PALETTE+1 ; Foreground color
350 ora PALETTE ; Background color
353 ; Initialize the color map with the new color settings (it is below the
358 lda $01 ; Get ROM config
360 and #%11111100 ; Clear bit 0 and 1
362 txa ; Load color code
363 @L2: sta CBASE+$0000,y
373 ; Done, reset the error code
379 ; ------------------------------------------------------------------------
380 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
381 ; set the palette should return the default palette here, so there's no
382 ; way for this function to fail.
384 ; Must set an error code: NO
392 ; ------------------------------------------------------------------------
393 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
394 ; drivers should return something reasonable here, even drivers that don't
395 ; support palettes, otherwise the caller has no way to determine the colors
396 ; of the (not changeable) palette.
398 ; Must set an error code: NO (all drivers must have a default palette)
406 ; ------------------------------------------------------------------------
407 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
408 ; color. The coordinates passed to this function are never outside the
409 ; visible screen area, so there is no need for clipping inside this function.
411 ; Must set an error code: NO
415 jsr CALC ; Calculate coordinates
417 sei ; Get underneath ROM
435 ; ------------------------------------------------------------------------
436 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
437 ; coordinates passed to this function are never outside the visible screen
438 ; area, so there is no need for clipping inside this function.
442 jsr CALC ; Calculate coordinates
444 sei ; Get underneath ROM
460 tya ; Get color value into A
461 ldx #$00 ; Clear high byte
464 ; ------------------------------------------------------------------------
465 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
466 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
468 ; To deal with off-screen coordinates, the current row
469 ; and column (40x25) is kept track of. These are set
470 ; negative when the point is off the screen, and made
471 ; positive when the point is within the visible screen.
473 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
474 ; Format is LINE x2,y2,x1,y1
476 ; Must set an error code: NO
481 @CHECK: lda X2 ;Make sure x1<x2
488 lda Y2 ;If not, swap P1 and P2
516 bpl @DYPOS ;Is y2>=y1?
517 lda Y1 ;Otherwise dy=y1-y2
523 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
527 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
532 sei ; Get underneath ROM
537 cpx DX ;Who's bigger: dy or dx?
538 bcc STEPINX ;If dx, then...
545 ; To simplify my life, just use PLOT to plot points.
548 ; Added special plotting routine -- cool!
550 ; X is now counter, Y is y-coordinate
552 ; On entry, X=DY=number of loop iterations, and Y=
556 sta OLDCHUNK ;So plotting routine will work right
562 bne @CONT ;If dy=0 it's just a point
564 @CONT: lsr ;Init counter to dy/2
570 lda INRANGE ;Range check
573 lda (POINT),y ;Otherwise plot
580 iny ;Advance Y coordinate
582 bcc @CONT ;No prob if Y=0..7
584 @CONT: lda TEMP ;Restore A
588 YCONT: dex ;X is counter
590 YCONT2: lda (POINT),y ;Plot endpoint
603 bne YCONT ;If we pass a column boundary...
604 ror CHUNK ;then reset CHUNK to $80
607 bmi @C1 ;Skip if column is negative
608 cmp #39 ;End if move past end of screen
610 @C1: lda POINT ;And add 8 to POINT
615 @CONT: inc COL ;Increment column
620 lda #00 ;Passed into col 0
628 ; Big steps in X direction
630 ; On entry, X=DY=number of loop iterations, and Y=
635 .byte $00 ;Temporary counter
643 ror ;Need bit for initialization
644 sta Y1 ;High byte of counter
646 bne @CONT ;Could be $100
653 beq XFIXC ;If we pass a column boundary...
655 bcc XFIXY ;Time to step in Y?
658 dec COUNTHI ;High bits set?
661 XDONE: lsr CHUNK ;Advance to last point
662 jsr LINEPLOT ;Plot the last chunk
668 ; CHUNK has passed a column, so plot and increment pointer
669 ; and fix up CHUNK, OLDCHUNK.
677 bmi @C1 ;Skip if column is negative
678 cmp #39 ;End if move past end of screen
696 ; Check to make sure there isn't a high bit, plot chunk,
697 ; and update Y-coordinate.
699 XFIXY: dec Y1 ;Maybe high bit set
707 jsr LINEPLOT ;Plot chunk
722 ; Subroutine to plot chunks/points (to save a little
723 ; room, gray hair, etc.)
725 LINEPLOT: ; Plot the line chunk
729 lda (POINT),Y ; Otherwise plot
739 ; Subroutine to fix up pointer when Y decreases through
740 ; zero or increases through 7.
742 FIXY: cpy #255 ;Y=255 or Y=8
744 @INCPTR: ;Add 320 to pointer
745 ldy #0 ;Y increased through 7
747 bmi @C1 ;If negative, then don't update
749 bcs @TOAST ;If at bottom of screen then quit
762 @DECPTR: ;Okay, subtract 320 then
763 ldy #7 ;Y decreased through 0
782 @TOAST: pla ;Remove old return address
784 jmp EXIT ;Restore interrupts, etc.
786 ; ------------------------------------------------------------------------
787 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
788 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
789 ; Contrary to most other functions, the graphics kernel will sort and clip
790 ; the coordinates before calling the driver, so on entry the following
791 ; conditions are valid:
794 ; (X1 >= 0) && (X1 < XRES)
795 ; (X2 >= 0) && (X2 < XRES)
796 ; (Y1 >= 0) && (Y1 < YRES)
797 ; (Y2 >= 0) && (Y2 < YRES)
799 ; Must set an error code: NO
802 ; Note: This function needs optimization. It's just a cheap translation of
803 ; the original C wrapper and could be written much smaller (besides that,
804 ; calling LINE is not a good idea either).
862 ; ------------------------------------------------------------------------
863 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
864 ; radius in tmp1 and the current drawing color.
866 ; Must set an error code: NO
872 jmp SETPIXEL ; Plot as a point
879 @L2: jsr CALC ; Compute XC, YC+R
889 sta CHUNK1 ; Forwards chunk
893 sta CHUNK2 ; Backwards chunk
896 sta TEMP2 ; TEMP2 = forwards high pointer
897 sta X2 ; X2 = backwards high pointer
915 jsr CALC ; Compute new coords
918 sta X1 ; X1 will be the backwards
919 lda POINT+1 ; low-pointer
920 sta X1+1 ; POINT will be forwards
924 sei ; Get underneath ROM
935 @LOOP: inc CURX ; x=x+1
937 lsr CHUNK1 ; Right chunk
939 jsr UPCHUNK1 ; Update if we move past a column
947 adc CURY ;if a<0 then a=a+y; y=y-1
959 dec Y2 ;Decrement y-offest for upper
969 cpy CURY ;if y<=x then punt
970 bcc @LOOP ;Now draw the other half
972 ; Draw the other half of the circle by exactly reversing
976 lsr OLDCH1 ;Only plot a bit at a time
983 jsr PCHUNK1 ;Plot points
1002 sbc CURX ;if a<0 then x=x+1; a=a+x
1006 jsr UPCH1 ;Upchunk, but no plot
1007 @CONT3: lsr OLDCH1 ;Only the bits...
1008 asl CHUNK2 ;Fix chunks
1015 CIRCEXIT: ;Restore interrupts
1021 ; Decrement lower pointers
1028 lda X2 ;If we pass through zero, then
1030 sbc #<320 ;subtract 320
1047 EXIT2: pla ;Grab return address
1049 jmp CIRCEXIT ;Restore interrupts, etc.
1051 ; Increment upper pointers
1079 ; UPCHUNK1 -- Update right-moving chunk pointers
1080 ; Due to passing through a column
1085 UPCH1: lda #$FF ;Alternative entry point
1105 ; UPCHUNK2 -- Update left-moving chunk pointers
1129 ; Plot right-moving chunk pairs for circle routine
1133 lda RCOL ;Make sure we're in range
1136 lda CHUNK1 ;Otherwise plot
1139 lda TROW ;Check for underflow
1148 @SKIP: lda BROW ;If CY+Y >= 200...
1160 ; Plot left-moving chunk pairs for circle routine
1164 lda LCOL ;Range check in X
1167 lda CHUNK2 ;Otherwise plot
1170 lda TROW ;Check for underflow
1179 @SKIP: lda BROW ;If CY+Y >= 200...
1190 ; ------------------------------------------------------------------------
1191 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1192 ; direction is passend in X/Y, the text direction is passed in A.
1194 ; Must set an error code: NO
1204 ; ------------------------------------------------------------------------
1205 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1206 ; current text style. The text to output is given as a zero terminated
1207 ; string with address in ptr3.
1209 ; Must set an error code: NO
1215 ; ------------------------------------------------------------------------
1216 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
1217 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
1218 ; the coordinates are valid, INRANGE is zero and the carry clear.
1225 lsr ; Neg is possible
1243 adc #>VBASE ; +bitmap base
1260 adc POINT ; +(X AND #$F8)