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.
62 ; ------------------------------------------------------------------------
65 ; Variables mapped to the zero page segment variables. Some of these are
66 ; used for passing parameters to the driver.
74 ROW := tmp2 ; Bitmap row...
75 COL := tmp3 ; ...and column, both set by PLOT
79 INRANGE := regsave+2 ; PLOT variable, $00 = coordinates in range
81 CHUNK := X2 ; Used in the line routine
82 OLDCHUNK := X2+1 ; Dito
84 ; Absolute variables used in the code
88 ERROR: .res 1 ; Error code
89 PALETTE: .res 2 ; The current palette
91 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
94 OLDD018: .res 1 ; Old register value
100 ; Circle routine stuff, overlaid by BAR variables
105 BROW: .res 1 ; Bottom row
106 TROW: .res 1 ; Top row
108 LCOL: .res 1 ; Left column
109 RCOL: .res 1 ; Right column
121 ; Constants and tables
125 DEFPALETTE: .byte $00, $01 ; White on black
126 PALETTESIZE = * - DEFPALETTE
128 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
129 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
131 VBASE = $E000 ; Video memory base address
132 CBASE = $D000 ; Color memory base address
137 ; ------------------------------------------------------------------------
138 ; INSTALL routine. Is called after the driver is loaded into memory. May
139 ; initialize anything that has to be done just once. Is probably empty
142 ; Must set an error code: NO
149 ; ------------------------------------------------------------------------
150 ; UNINSTALL routine. Is called before the driver is removed from memory. May
151 ; clean up anything done by INSTALL but is probably empty most of the time.
153 ; Must set an error code: NO
160 ; ------------------------------------------------------------------------
161 ; INIT: Changes an already installed device from text mode to graphics
163 ; Note that INIT/DONE may be called multiple times while the driver
164 ; is loaded, while INSTALL is only called once, so any code that is needed
165 ; to initializes variables and so on must go here. Setting palette and
166 ; clearing the screen is not needed because this is called by the graphics
168 ; The graphics kernel will never call INIT when a graphics mode is already
169 ; active, so there is no need to protect against that.
171 ; Must set an error code: YES
176 ; Initialize variables
181 ; Switch into graphics mode
183 lda $DD02 ; Set the data direction regs
187 and #$FC ; Switch to bank 3
192 lda #$48 ; Set color map to $D000, screen to $E000
195 lda $D011 ; And turn on bitmap
199 ; Done, reset the error code
205 ; ------------------------------------------------------------------------
206 ; DONE: Will be called to switch the graphics device back into text mode.
207 ; The graphics kernel will never call DONE when no graphics mode is active,
208 ; so there is no need to protect against that.
210 ; Must set an error code: NO
213 DONE: lda $DD02 ; Set the data direction regs
220 lda OLDD018 ; Screen mem --> $0400
228 ; ------------------------------------------------------------------------
229 ; GETERROR: Return the error code in A and clear it.
237 ; ------------------------------------------------------------------------
238 ; CONTROL: Platform/driver specific entry point.
240 ; Must set an error code: YES
244 lda #TGI_ERR_INV_FUNC
248 ; ------------------------------------------------------------------------
249 ; CLEAR: Clears the screen.
251 ; Must set an error code: NO
256 @L1: sta VBASE+$0000,y
292 ; ------------------------------------------------------------------------
293 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
294 ; The page number is already checked to be valid by the graphics kernel.
296 ; Must set an error code: NO (will only be called if page ok)
302 ; ------------------------------------------------------------------------
303 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
304 ; The page number is already checked to be valid by the graphics kernel.
306 ; Must set an error code: NO (will only be called if page ok)
312 ; ------------------------------------------------------------------------
313 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
314 ; to be in a valid range (0..maxcolor-1).
316 ; Must set an error code: NO (will only be called if color ok)
326 ; ------------------------------------------------------------------------
327 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
328 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
331 ; Must set an error code: YES
336 @L1: lda (ptr1),y ; Copy the palette
337 and #$0F ; Make a valid color
342 ; Get the color entries from the palette
344 lda PALETTE+1 ; Foreground color
349 ora PALETTE ; Background color
352 ; Initialize the color map with the new color settings (it is below the
357 lda $01 ; Get ROM config
359 and #%11111100 ; Clear bit 0 and 1
361 txa ; Load color code
362 @L2: sta CBASE+$0000,y
372 ; Done, reset the error code
378 ; ------------------------------------------------------------------------
379 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
380 ; set the palette should return the default palette here, so there's no
381 ; way for this function to fail.
383 ; Must set an error code: NO
391 ; ------------------------------------------------------------------------
392 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
393 ; drivers should return something reasonable here, even drivers that don't
394 ; support palettes, otherwise the caller has no way to determine the colors
395 ; of the (not changeable) palette.
397 ; Must set an error code: NO (all drivers must have a default palette)
405 ; ------------------------------------------------------------------------
406 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
407 ; color. The coordinates passed to this function are never outside the
408 ; visible screen area, so there is no need for clipping inside this function.
410 ; Must set an error code: NO
414 jsr CALC ; Calculate coordinates
416 sei ; Get underneath ROM
434 ; ------------------------------------------------------------------------
435 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
436 ; coordinates passed to this function are never outside the visible screen
437 ; area, so there is no need for clipping inside this function.
441 jsr CALC ; Calculate coordinates
443 sei ; Get underneath ROM
459 tya ; Get color value into A
460 ldx #$00 ; Clear high byte
463 ; ------------------------------------------------------------------------
464 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
465 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
467 ; To deal with off-screen coordinates, the current row
468 ; and column (40x25) is kept track of. These are set
469 ; negative when the point is off the screen, and made
470 ; positive when the point is within the visible screen.
472 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
473 ; Format is LINE x2,y2,x1,y1
475 ; Must set an error code: NO
480 @CHECK: lda X2 ;Make sure x1<x2
487 lda Y2 ;If not, swap P1 and P2
515 bpl @DYPOS ;Is y2>=y1?
516 lda Y1 ;Otherwise dy=y1-y2
522 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
526 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
531 sei ; Get underneath ROM
536 cpx DX ;Who's bigger: dy or dx?
537 bcc STEPINX ;If dx, then...
544 ; To simplify my life, just use PLOT to plot points.
547 ; Added special plotting routine -- cool!
549 ; X is now counter, Y is y-coordinate
551 ; On entry, X=DY=number of loop iterations, and Y=
555 sta OLDCHUNK ;So plotting routine will work right
561 bne @CONT ;If dy=0 it's just a point
563 @CONT: lsr ;Init counter to dy/2
569 lda INRANGE ;Range check
572 lda (POINT),y ;Otherwise plot
579 iny ;Advance Y coordinate
581 bcc @CONT ;No prob if Y=0..7
583 @CONT: lda TEMP ;Restore A
587 YCONT: dex ;X is counter
589 YCONT2: lda (POINT),y ;Plot endpoint
602 bne YCONT ;If we pass a column boundary...
603 ror CHUNK ;then reset CHUNK to $80
606 bmi @C1 ;Skip if column is negative
607 cmp #39 ;End if move past end of screen
609 @C1: lda POINT ;And add 8 to POINT
614 @CONT: inc COL ;Increment column
619 lda #00 ;Passed into col 0
627 ; Big steps in X direction
629 ; On entry, X=DY=number of loop iterations, and Y=
634 .byte $00 ;Temporary counter
642 ror ;Need bit for initialization
643 sta Y1 ;High byte of counter
645 bne @CONT ;Could be $100
652 beq XFIXC ;If we pass a column boundary...
654 bcc XFIXY ;Time to step in Y?
657 dec COUNTHI ;High bits set?
660 XDONE: lsr CHUNK ;Advance to last point
661 jsr LINEPLOT ;Plot the last chunk
667 ; CHUNK has passed a column, so plot and increment pointer
668 ; and fix up CHUNK, OLDCHUNK.
676 bmi @C1 ;Skip if column is negative
677 cmp #39 ;End if move past end of screen
695 ; Check to make sure there isn't a high bit, plot chunk,
696 ; and update Y-coordinate.
698 XFIXY: dec Y1 ;Maybe high bit set
706 jsr LINEPLOT ;Plot chunk
721 ; Subroutine to plot chunks/points (to save a little
722 ; room, gray hair, etc.)
724 LINEPLOT: ; Plot the line chunk
728 lda (POINT),Y ; Otherwise plot
738 ; Subroutine to fix up pointer when Y decreases through
739 ; zero or increases through 7.
741 FIXY: cpy #255 ;Y=255 or Y=8
743 @INCPTR: ;Add 320 to pointer
744 ldy #0 ;Y increased through 7
746 bmi @C1 ;If negative, then don't update
748 bcs @TOAST ;If at bottom of screen then quit
761 @DECPTR: ;Okay, subtract 320 then
762 ldy #7 ;Y decreased through 0
781 @TOAST: pla ;Remove old return address
783 jmp EXIT ;Restore interrupts, etc.
785 ; ------------------------------------------------------------------------
786 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
787 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
788 ; Contrary to most other functions, the graphics kernel will sort and clip
789 ; the coordinates before calling the driver, so on entry the following
790 ; conditions are valid:
793 ; (X1 >= 0) && (X1 < XRES)
794 ; (X2 >= 0) && (X2 < XRES)
795 ; (Y1 >= 0) && (Y1 < YRES)
796 ; (Y2 >= 0) && (Y2 < YRES)
798 ; Must set an error code: NO
801 ; Note: This function needs optimization. It's just a cheap translation of
802 ; the original C wrapper and could be written much smaller (besides that,
803 ; calling LINE is not a good idea either).
861 ; ------------------------------------------------------------------------
862 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
863 ; radius in tmp1 and the current drawing color.
865 ; Must set an error code: NO
871 jmp SETPIXEL ; Plot as a point
878 @L2: jsr CALC ; Compute XC, YC+R
888 sta CHUNK1 ; Forwards chunk
892 sta CHUNK2 ; Backwards chunk
895 sta TEMP2 ; TEMP2 = forwards high pointer
896 sta X2 ; X2 = backwards high pointer
914 jsr CALC ; Compute new coords
917 sta X1 ; X1 will be the backwards
918 lda POINT+1 ; low-pointer
919 sta X1+1 ; POINT will be forwards
923 sei ; Get underneath ROM
934 @LOOP: inc CURX ; x=x+1
936 lsr CHUNK1 ; Right chunk
938 jsr UPCHUNK1 ; Update if we move past a column
946 adc CURY ;if a<0 then a=a+y; y=y-1
958 dec Y2 ;Decrement y-offest for upper
968 cpy CURY ;if y<=x then punt
969 bcc @LOOP ;Now draw the other half
971 ; Draw the other half of the circle by exactly reversing
975 lsr OLDCH1 ;Only plot a bit at a time
982 jsr PCHUNK1 ;Plot points
1001 sbc CURX ;if a<0 then x=x+1; a=a+x
1005 jsr UPCH1 ;Upchunk, but no plot
1006 @CONT3: lsr OLDCH1 ;Only the bits...
1007 asl CHUNK2 ;Fix chunks
1014 CIRCEXIT: ;Restore interrupts
1020 ; Decrement lower pointers
1027 lda X2 ;If we pass through zero, then
1029 sbc #<320 ;subtract 320
1046 EXIT2: pla ;Grab return address
1048 jmp CIRCEXIT ;Restore interrupts, etc.
1050 ; Increment upper pointers
1078 ; UPCHUNK1 -- Update right-moving chunk pointers
1079 ; Due to passing through a column
1084 UPCH1: lda #$FF ;Alternative entry point
1104 ; UPCHUNK2 -- Update left-moving chunk pointers
1128 ; Plot right-moving chunk pairs for circle routine
1132 lda RCOL ;Make sure we're in range
1135 lda CHUNK1 ;Otherwise plot
1138 lda TROW ;Check for underflow
1147 @SKIP: lda BROW ;If CY+Y >= 200...
1159 ; Plot left-moving chunk pairs for circle routine
1163 lda LCOL ;Range check in X
1166 lda CHUNK2 ;Otherwise plot
1169 lda TROW ;Check for underflow
1178 @SKIP: lda BROW ;If CY+Y >= 200...
1189 ; ------------------------------------------------------------------------
1190 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1191 ; direction is passend in X/Y, the text direction is passed in A.
1193 ; Must set an error code: NO
1203 ; ------------------------------------------------------------------------
1204 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1205 ; current text style. The text to output is given as a zero terminated
1206 ; string with address in ptr3.
1208 ; Must set an error code: NO
1214 ; ------------------------------------------------------------------------
1215 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
1216 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
1217 ; the coordinates are valid, INRANGE is zero and the carry clear.
1224 lsr ; Neg is possible
1242 adc #>VBASE ; +bitmap base
1259 adc POINT ; +(X AND #$F8)