2 ; Graphics driver for the 320x200x2 mode on the C64.
4 ; Based on Stephen L. Judd's GRLIB code.
6 ; 2017-01-13, Greg King
7 ; 2018-03-13, Sven Klose
10 .include "zeropage.inc"
12 .include "tgi-kernel.inc"
13 .include "tgi-error.inc"
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table and constants.
22 module_header _c64_hi_tgi
24 ; First part of the header is a structure that has a magic and defines the
25 ; capabilities of the driver
27 .byte $74, $67, $69 ; "tgi"
28 .byte TGI_API_VERSION ; TGI API version number
29 .addr $0000 ; Library reference
30 .word 320 ; X resolution
31 .word 200 ; Y resolution
32 .byte 2 ; Number of drawing colors
33 .byte 1 ; Number of screens available
34 .byte 8 ; System font X size
35 .byte 8 ; System font Y size
36 .word $00D4 ; Aspect ratio (based on 4/3 display)
37 .byte 0 ; TGI driver flags
39 ; Next comes the jump table. With the exception of IRQ, all entries must be
40 ; valid and may point to an RTS for test versions (function not implemented).
62 ; ------------------------------------------------------------------------
65 ; Variables mapped to the zero page segment variables. Some of these are
66 ; used for passing parameters to the driver.
78 CHUNK := X2 ; Used in the line routine
79 OLDCHUNK := X2+1 ; Dito
81 ; Absolute variables used in the code
85 ERROR: .res 1 ; Error code
86 PALETTE: .res 2 ; The current palette
88 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
91 OLDD018: .res 1 ; Old register value
108 ; Constants and tables
112 DEFPALETTE: .byte $00, $01 ; White on black
113 PALETTESIZE = * - DEFPALETTE
115 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
116 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
118 CHARROM := $D000 ; Character rom base address
119 CBASE := $D000 ; Color memory base address
120 VBASE := $E000 ; Video memory base address
125 ; ------------------------------------------------------------------------
126 ; INSTALL routine. Is called after the driver is loaded into memory. May
127 ; initialize anything that has to be done just once. Is probably empty
130 ; Must set an error code: NO
137 ; ------------------------------------------------------------------------
138 ; UNINSTALL routine. Is called before the driver is removed from memory. May
139 ; clean up anything done by INSTALL but is probably empty most of the time.
141 ; Must set an error code: NO
148 ; ------------------------------------------------------------------------
149 ; INIT: Changes an already installed device from text mode to graphics
151 ; Note that INIT/DONE may be called multiple times while the driver
152 ; is loaded, while INSTALL is only called once, so any code that is needed
153 ; to initializes variables and so on must go here. Setting palette and
154 ; clearing the screen is not needed because this is called by the graphics
156 ; The graphics kernel will never call INIT when a graphics mode is already
157 ; active, so there is no need to protect against that.
159 ; Must set an error code: YES
164 ; Initialize variables
169 ; Switch into graphics mode
171 lda $DD02 ; Set the data direction regs
175 and #$FC ; Switch to bank 3
180 lda #$48 ; Set color map to $D000, screen to $E000
183 lda $D011 ; And turn on bitmap
187 ; Done, reset the error code
193 ; ------------------------------------------------------------------------
194 ; DONE: Will be called to switch the graphics device back into text mode.
195 ; The graphics kernel will never call DONE when no graphics mode is active,
196 ; so there is no need to protect against that.
198 ; Must set an error code: NO
201 DONE: lda $DD02 ; Set the data direction regs
208 lda OLDD018 ; Screen mem --> $0400
216 ; ------------------------------------------------------------------------
217 ; GETERROR: Return the error code in A and clear it.
225 ; ------------------------------------------------------------------------
226 ; CONTROL: Platform/driver specific entry point.
228 ; Must set an error code: YES
232 lda #TGI_ERR_INV_FUNC
236 ; ------------------------------------------------------------------------
237 ; CLEAR: Clears the screen.
239 ; Must set an error code: NO
244 @L1: sta VBASE+$0000,y
275 sta VBASE+$1E40,y ; preserve vectors
280 ; ------------------------------------------------------------------------
281 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
282 ; The page number is already checked to be valid by the graphics kernel.
284 ; Must set an error code: NO (will only be called if page ok)
290 ; ------------------------------------------------------------------------
291 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
292 ; The page number is already checked to be valid by the graphics kernel.
294 ; Must set an error code: NO (will only be called if page ok)
300 ; ------------------------------------------------------------------------
301 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
302 ; to be in a valid range (0..maxcolor-1).
304 ; Must set an error code: NO (will only be called if color ok)
314 ; ------------------------------------------------------------------------
315 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
316 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
319 ; Must set an error code: YES
324 @L1: lda (ptr1),y ; Copy the palette
325 and #$0F ; Make a valid color
330 ; Get the color entries from the palette
332 lda PALETTE+1 ; Foreground color
337 ora PALETTE ; Background color
340 ; Initialize the color map with the new color settings (it is below the
345 lda $01 ; Get ROM config
347 and #%11111100 ; Clear bit 0 and 1
349 txa ; Load color code
350 @L2: sta CBASE+$0000,y
360 ; Done, reset the error code
366 ; ------------------------------------------------------------------------
367 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
368 ; set the palette should return the default palette here, so there's no
369 ; way for this function to fail.
371 ; Must set an error code: NO
379 ; ------------------------------------------------------------------------
380 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
381 ; drivers should return something reasonable here, even drivers that don't
382 ; support palettes, otherwise the caller has no way to determine the colors
383 ; of the (not changeable) palette.
385 ; Must set an error code: NO (all drivers must have a default palette)
393 ; ------------------------------------------------------------------------
394 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
395 ; color. The coordinates passed to this function are never outside the
396 ; visible screen area, so there is no need for clipping inside this function.
398 ; Must set an error code: NO
402 jsr CALC ; Calculate coordinates
404 sei ; Get underneath ROM
422 ; ------------------------------------------------------------------------
423 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
424 ; coordinates passed to this function are never outside the visible screen
425 ; area, so there is no need for clipping inside this function.
429 jsr CALC ; Calculate coordinates
431 sei ; Get underneath ROM
447 tya ; Get color value into A
448 ldx #$00 ; Clear high byte
451 ; ------------------------------------------------------------------------
452 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
453 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
455 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
456 ; Format is LINE x2,y2,x1,y1
458 ; Must set an error code: NO
463 @CHECK: lda X2 ;Make sure x1<x2
470 lda Y2 ;If not, swap P1 and P2
498 bpl @DYPOS ;Is y2>=y1?
499 lda Y1 ;Otherwise dy=y1-y2
505 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
509 jsr CALC ; Set up .X, .Y, and POINT
514 sei ; Get underneath ROM
519 cpx DX ;Who's bigger: dy or dx?
520 bcc STEPINX ;If dx, then...
527 ; To simplify my life, just use PLOT to plot points.
530 ; Added special plotting routine -- cool!
532 ; X is now counter, Y is y-coordinate
534 ; On entry, X=DY=number of loop iterations, and Y=
538 sta OLDCHUNK ;So plotting routine will work right
544 beq YCONT2 ;If dy=0, it's just a point
545 @CONT: lsr ;Init counter to dy/2
557 iny ;Advance Y coordinate
559 bcc @CONT ;No prob if Y=0..7
561 @CONT: lda TEMP ;Restore A
565 YCONT: dex ;X is counter
567 YCONT2: lda (POINT),y ;Plot endpoint
580 bne YCONT ;If we pass a column boundary...
581 ror CHUNK ;then reset CHUNK to $80
583 lda POINT ;And add 8 to POINT
594 ; Big steps in X direction
596 ; On entry, X=DY=number of loop iterations, and Y=
601 .byte $00 ;Temporary counter
609 ror ;Need bit for initialization
610 sta Y1 ;High byte of counter
612 bne @CONT ;Could be $100
619 beq XFIXC ;If we pass a column boundary...
621 bcc XFIXY ;Time to step in Y?
624 dec COUNTHI ;High bits set?
627 lsr CHUNK ;Advance to last point
628 jsr LINEPLOT ;Plot the last chunk
634 ; CHUNK has passed a column, so plot and increment pointer
635 ; and fix up CHUNK, OLDCHUNK.
651 ; Check to make sure there isn't a high bit, plot chunk,
652 ; and update Y-coordinate.
654 XFIXY: dec Y1 ;Maybe high bit set
662 jsr LINEPLOT ;Plot chunk
677 ; Subroutine to plot chunks/points (to save a little
678 ; room, gray hair, etc.)
680 LINEPLOT: ; Plot the line chunk
691 ; Subroutine to fix up pointer when Y decreases through
692 ; zero or increases through 7.
694 FIXY: cpy #255 ;Y=255 or Y=8
697 @INCPTR: ;Add 320 to pointer
698 ldy #0 ;Y increased through 7
707 @DECPTR: ;Okay, subtract 320 then
708 ldy #7 ;Y decreased through 0
718 ; ------------------------------------------------------------------------
719 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
720 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
721 ; Contrary to most other functions, the graphics kernel will sort and clip
722 ; the coordinates before calling the driver, so on entry the following
723 ; conditions are valid:
726 ; (X1 >= 0) && (X1 < XRES)
727 ; (X2 >= 0) && (X2 < XRES)
728 ; (Y1 >= 0) && (Y1 < YRES)
729 ; (Y2 >= 0) && (Y2 < YRES)
731 ; Must set an error code: NO
734 ; Note: This function needs optimization. It's just a cheap translation of
735 ; the original C wrapper and could be written much smaller (besides that,
736 ; calling LINE is not a good idea either).
794 ; ------------------------------------------------------------------------
795 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
796 ; direction is passend in X/Y, the text direction is passed in A.
798 ; Must set an error code: NO
808 ; ------------------------------------------------------------------------
809 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
810 ; current text style. The text to output is given as a zero terminated
811 ; string with address in ptr3.
813 ; Must set an error code: NO
818 ; Calculate a pointer to the representation of the character in the
821 ldx #((>(CHARROM + $0800)) >> 3)
825 ldx #((>(CHARROM + $0000)) >> 3)
841 ; ------------------------------------------------------------------------
842 ; Calculate all variables to plot the pixel at X1/Y1.
849 lsr ; Neg is possible
867 adc #>VBASE ; +bitmap base
874 adc POINT ; +(X AND #$F8)