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 $00 ; TGI 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.
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
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
97 INRANGE: .res 1 ; $00 = coordinates in range
103 ; Circle routine stuff
106 BROW: .res 1 ; Bottom row
107 TROW: .res 1 ; Top row
108 LCOL: .res 1 ; Left column
109 RCOL: .res 1 ; Right column
120 ; Constants and tables
124 DEFPALETTE: .byte $00, $01 ; White on black
125 PALETTESIZE = * - DEFPALETTE
127 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
128 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
130 VBASE = $E000 ; Video memory base address
131 CBASE = $D000 ; Color memory base address
136 ; ------------------------------------------------------------------------
137 ; INSTALL routine. Is called after the driver is loaded into memory. May
138 ; initialize anything that has to be done just once. Is probably empty
141 ; Must set an error code: NO
148 ; ------------------------------------------------------------------------
149 ; DEINSTALL routine. Is called before the driver is removed from memory. May
150 ; clean up anything done by INSTALL but is probably empty most of the time.
152 ; Must set an error code: NO
159 ; ------------------------------------------------------------------------
160 ; INIT: Changes an already installed device from text mode to graphics
161 ; mode. The number of the graphics mode is passed to the function in A.
162 ; Note that INIT/DONE may be called multiple times while the driver
163 ; is loaded, while INSTALL is only called once, so any code that is needed
164 ; to initializes variables and so on must go here. Setting palette and
165 ; clearing the screen is not needed because this is called by the graphics
167 ; The graphics kernel will never call INIT when a graphics mode is already
168 ; active, so there is no need to protect against that.
170 ; Must set an error code: YES
173 INIT: cmp #TGI_MODE_320_200_2 ; Correct mode?
174 beq @L1 ; Jump if yes
175 lda #TGI_ERR_INV_MODE ; ## Error
178 ; Initialize variables
183 ; Switch into graphics mode
185 lda $DD02 ; Set the data direction regs
189 and #$FC ; Switch to bank 3
194 lda #$48 ; Set color map to $D000, screen to $E000
197 lda $D011 ; And turn on bitmap
201 ; Done, reset the error code
207 ; ------------------------------------------------------------------------
208 ; DONE: Will be called to switch the graphics device back into text mode.
209 ; The graphics kernel will never call DONE when no graphics mode is active,
210 ; so there is no need to protect against that.
212 ; Must set an error code: YES
215 DONE: lda $DD02 ; Set the data direction regs
222 lda OLDD018 ; Screen mem --> $0400
230 ; ------------------------------------------------------------------------
231 ; GETERROR: Return the error code in A and clear it.
239 ; ------------------------------------------------------------------------
240 ; CONTROL: Platform/driver specific entry point.
242 ; Must set an error code: YES
246 lda #TGI_ERR_INV_FUNC
250 ; ------------------------------------------------------------------------
251 ; CLEAR: Clears the screen.
253 ; Must set an error code: NO
258 @L1: sta VBASE+$0000,y
294 ; ------------------------------------------------------------------------
295 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
296 ; The page number is already checked to be valid by the graphics kernel.
298 ; Must set an error code: NO (will only be called if page ok)
304 ; ------------------------------------------------------------------------
305 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
306 ; The page number is already checked to be valid by the graphics kernel.
308 ; Must set an error code: NO (will only be called if page ok)
314 ; ------------------------------------------------------------------------
315 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
316 ; to be in a valid range (0..maxcolor-1).
318 ; Must set an error code: NO (will only be called if color ok)
328 ; ------------------------------------------------------------------------
329 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
330 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
333 ; Must set an error code: YES
338 @L1: lda (ptr1),y ; Copy the palette
339 and #$0F ; Make a valid color
344 ; Get the color entries from the palette
346 lda PALETTE+1 ; Foreground color
351 ora PALETTE ; Background color
354 ; Initialize the color map with the new color settings (it is below the
359 lda $01 ; Get ROM config
361 and #%11111100 ; Clear bit 0 and 1
363 txa ; Load color code
364 @L2: sta CBASE+$0000,y
378 ; ------------------------------------------------------------------------
379 ; GETPALETTE: Return the current palette in A/X. Must return NULL and set an
380 ; error if palettes are not supported.
382 ; Must set an error code: YES
390 ; ------------------------------------------------------------------------
391 ; GETDEFPALETTE: Return the default palette for the driver in A/X. Must
392 ; return NULL and set an error of palettes are not supported.
394 ; Must set an error code: YES
402 ; ------------------------------------------------------------------------
403 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
404 ; color. The coordinates passed to this function are never outside the
405 ; visible screen area, so there is no need for clipping inside this function.
407 ; Must set an error code: NO
411 jsr CALC ; Calculate coordinates
413 sei ; Get underneath ROM
431 ; ------------------------------------------------------------------------
432 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
433 ; coordinates passed to this function are never outside the visible screen
434 ; area, so there is no need for clipping inside this function.
438 jsr CALC ; Calculate coordinates
440 sei ; Get underneath ROM
456 tya ; Get color value into A
457 ldx #$00 ; Clear high byte
460 ; ------------------------------------------------------------------------
461 ; HORLINE: Draw a horizontal line from X1/Y to X2/Y, where X1 = ptr1,
462 ; Y = ptr2 and X2 = ptr3, using the current drawing color.
464 ; This is a special line drawing entry used when the line is know to be
465 ; horizontal, for example by the BAR emulation routine. If the driver does
466 ; not have special code for horizontal lines, it may just copy Y to Y2 and
467 ; proceed with the generic line drawing code.
469 ; Note: Line coordinates will always be sorted (Y1 <= X2) and clipped.
471 ; Must set an error code: NO
481 ; ------------------------------------------------------------------------
482 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
483 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
485 ; To deal with off-screen coordinates, the current row
486 ; and column (40x25) is kept track of. These are set
487 ; negative when the point is off the screen, and made
488 ; positive when the point is within the visible screen.
490 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
491 ; Format is LINE x2,y2,x1,y1
493 ; Must set an error code: NO
498 @CHECK: lda X2 ;Make sure x1<x2
505 lda Y2 ;If not, swap P1 and P2
533 bpl @DYPOS ;Is y2>=y1?
534 lda Y1 ;Otherwise dy=y1-y2
540 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
544 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
549 sei ; Get underneath ROM
554 cpx DX ;Who's bigger: dy or dx?
555 bcc STEPINX ;If dx, then...
562 ; To simplify my life, just use PLOT to plot points.
565 ; Added special plotting routine -- cool!
567 ; X is now counter, Y is y-coordinate
569 ; On entry, X=DY=number of loop iterations, and Y=
573 sta OLDCHUNK ;So plotting routine will work right
579 bne @CONT ;If dy=0 it's just a point
581 @CONT: lsr ;Init counter to dy/2
587 lda INRANGE ;Range check
590 lda (POINT),y ;Otherwise plot
597 iny ;Advance Y coordinate
599 bcc @CONT ;No prob if Y=0..7
601 @CONT: lda TEMP ;Restore A
605 YCONT: dex ;X is counter
607 YCONT2: lda (POINT),y ;Plot endpoint
620 bne YCONT ;If we pass a column boundary...
621 ror CHUNK ;then reset CHUNK to $80
624 bmi @C1 ;Skip if column is negative
625 cmp #39 ;End if move past end of screen
627 @C1: lda POINT ;And add 8 to POINT
632 @CONT: inc COL ;Increment column
637 lda #00 ;Passed into col 0
645 ; Big steps in X direction
647 ; On entry, X=DY=number of loop iterations, and Y=
652 .byte $00 ;Temporary counter
660 ror ;Need bit for initialization
661 sta Y1 ;High byte of counter
663 bne @CONT ;Could be $100
670 beq XFIXC ;If we pass a column boundary...
672 bcc XFIXY ;Time to step in Y?
675 dec COUNTHI ;High bits set?
678 XDONE: lsr CHUNK ;Advance to last point
679 jsr LINEPLOT ;Plot the last chunk
685 ; CHUNK has passed a column, so plot and increment pointer
686 ; and fix up CHUNK, OLDCHUNK.
694 bmi @C1 ;Skip if column is negative
695 cmp #39 ;End if move past end of screen
713 ; Check to make sure there isn't a high bit, plot chunk,
714 ; and update Y-coordinate.
716 XFIXY: dec Y1 ;Maybe high bit set
724 jsr LINEPLOT ;Plot chunk
739 ; Subroutine to plot chunks/points (to save a little
740 ; room, gray hair, etc.)
742 LINEPLOT: ; Plot the line chunk
746 lda (POINT),Y ; Otherwise plot
756 ; Subroutine to fix up pointer when Y decreases through
757 ; zero or increases through 7.
759 FIXY: cpy #255 ;Y=255 or Y=8
761 @INCPTR: ;Add 320 to pointer
762 ldy #0 ;Y increased through 7
764 bmi @C1 ;If negative, then don't update
766 bcs @TOAST ;If at bottom of screen then quit
781 @DECPTR: ;Okay, subtract 320 then
782 ldy #7 ;Y decreased through 0
800 @TOAST: pla ;Remove old return address
802 jmp EXIT ;Restore interrupts, etc.
804 ; ------------------------------------------------------------------------
805 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
806 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
807 ; Contrary to most other functions, the graphics kernel will sort and clip
808 ; the coordinates before calling the driver, so on entry the following
809 ; conditions are valid:
812 ; (X1 >= 0) && (X1 < XRES)
813 ; (X2 >= 0) && (X2 < XRES)
814 ; (Y1 >= 0) && (Y1 < YRES)
815 ; (Y2 >= 0) && (Y2 < YRES)
817 ; Must set an error code: NO
822 ; ------------------------------------------------------------------------
823 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
824 ; radius in tmp1 and the current drawing color.
826 ; Must set an error code: NO
832 jmp SETPIXEL ; Plot as a point
839 @L2: jsr CALC ; Compute XC, YC+R
849 sta CHUNK1 ; Forwards chunk
853 sta CHUNK2 ; Backwards chunk
856 sta TEMP2 ; TEMP2 = forwards high pointer
857 sta X2 ; X2 = backwards high pointer
875 jsr CALC ; Compute new coords
878 sta X1 ; X1 will be the backwards
879 lda POINT+1 ; low-pointer
880 sta X1+1 ; POINT will be forwards
884 sei ; Get underneath ROM
895 @LOOP: inc CURX ; x=x+1
897 lsr CHUNK1 ; Right chunk
899 jsr UPCHUNK1 ; Update if we move past a column
907 adc CURY ;if a<0 then a=a+y; y=y-1
919 dec Y2 ;Decrement y-offest for upper
929 cpy CURY ;if y<=x then punt
930 bcc @LOOP ;Now draw the other half
932 ; Draw the other half of the circle by exactly reversing
936 lsr OLDCH1 ;Only plot a bit at a time
943 jsr PCHUNK1 ;Plot points
962 sbc CURX ;if a<0 then x=x+1; a=a+x
966 jsr UPCH1 ;Upchunk, but no plot
967 @CONT3: lsr OLDCH1 ;Only the bits...
968 asl CHUNK2 ;Fix chunks
975 CIRCEXIT: ;Restore interrupts
981 ; Decrement lower pointers
988 lda X2 ;If we pass through zero, then
990 sbc #<320 ;subtract 320
1007 EXIT2: pla ;Grab return address
1009 jmp CIRCEXIT ;Restore interrupts, etc.
1011 ; Increment upper pointers
1039 ; UPCHUNK1 -- Update right-moving chunk pointers
1040 ; Due to passing through a column
1045 UPCH1: lda #$FF ;Alternative entry point
1065 ; UPCHUNK2 -- Update left-moving chunk pointers
1089 ; Plot right-moving chunk pairs for circle routine
1093 lda RCOL ;Make sure we're in range
1096 lda CHUNK1 ;Otherwise plot
1099 lda TROW ;Check for underflow
1108 @SKIP: lda BROW ;If CY+Y >= 200...
1120 ; Plot left-moving chunk pairs for circle routine
1124 lda LCOL ;Range check in X
1127 lda CHUNK2 ;Otherwise plot
1130 lda TROW ;Check for underflow
1139 @SKIP: lda BROW ;If CY+Y >= 200...
1150 ; ------------------------------------------------------------------------
1151 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1152 ; direction is passend in X/Y, the text direction is passed in A.
1154 ; Must set an error code: NO
1164 ; ------------------------------------------------------------------------
1165 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1166 ; current text style. The text to output is given as a zero terminated
1167 ; string with address in ptr3.
1169 ; Must set an error code: NO
1175 ; ------------------------------------------------------------------------
1176 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
1177 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
1178 ; the coordinates are valid, INRANGE is zero and the carry clear.
1185 lsr ; Neg is possible
1203 adc #>VBASE ; +bitmap base
1220 adc POINT ; +(X AND #$F8)