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
277 @L2: sta VBASE+$1F00,y
283 ; ------------------------------------------------------------------------
284 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
285 ; The page number is already checked to be valid by the graphics kernel.
287 ; Must set an error code: NO (will only be called if page ok)
293 ; ------------------------------------------------------------------------
294 ; SETDRAWPAGE: Set the drawable 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 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
305 ; to be in a valid range (0..maxcolor-1).
307 ; Must set an error code: NO (will only be called if color ok)
317 ; ------------------------------------------------------------------------
318 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
319 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
322 ; Must set an error code: YES
327 @L1: lda (ptr1),y ; Copy the palette
328 and #$0F ; Make a valid color
333 ; Get the color entries from the palette
335 lda PALETTE+1 ; Foreground color
340 ora PALETTE ; Background color
343 ; Initialize the color map with the new color settings (it is below the
348 lda $01 ; Get ROM config
350 and #%11111100 ; Clear bit 0 and 1
352 txa ; Load color code
353 @L2: sta CBASE+$0000,y
363 ; Done, reset the error code
369 ; ------------------------------------------------------------------------
370 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
371 ; set the palette should return the default palette here, so there's no
372 ; way for this function to fail.
374 ; Must set an error code: NO
382 ; ------------------------------------------------------------------------
383 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
384 ; drivers should return something reasonable here, even drivers that don't
385 ; support palettes, otherwise the caller has no way to determine the colors
386 ; of the (not changeable) palette.
388 ; Must set an error code: NO (all drivers must have a default palette)
396 ; ------------------------------------------------------------------------
397 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
398 ; color. The coordinates passed to this function are never outside the
399 ; visible screen area, so there is no need for clipping inside this function.
401 ; Must set an error code: NO
405 jsr CALC ; Calculate coordinates
407 sei ; Get underneath ROM
425 ; ------------------------------------------------------------------------
426 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
427 ; coordinates passed to this function are never outside the visible screen
428 ; area, so there is no need for clipping inside this function.
432 jsr CALC ; Calculate coordinates
434 sei ; Get underneath ROM
450 tya ; Get color value into A
451 ldx #$00 ; Clear high byte
454 ; ------------------------------------------------------------------------
455 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
456 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
458 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
459 ; Format is LINE x2,y2,x1,y1
461 ; Must set an error code: NO
466 @CHECK: lda X2 ;Make sure x1<x2
473 lda Y2 ;If not, swap P1 and P2
501 bpl @DYPOS ;Is y2>=y1?
502 lda Y1 ;Otherwise dy=y1-y2
508 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
512 jsr CALC ; Set up .X, .Y, and POINT
517 sei ; Get underneath ROM
522 cpx DX ;Who's bigger: dy or dx?
523 bcc STEPINX ;If dx, then...
530 ; To simplify my life, just use PLOT to plot points.
533 ; Added special plotting routine -- cool!
535 ; X is now counter, Y is y-coordinate
537 ; On entry, X=DY=number of loop iterations, and Y=
541 sta OLDCHUNK ;So plotting routine will work right
547 bne @CONT ;If dy=0 it's just a point
549 @CONT: lsr ;Init counter to dy/2
561 iny ;Advance Y coordinate
563 bcc @CONT ;No prob if Y=0..7
565 @CONT: lda TEMP ;Restore A
569 YCONT: dex ;X is counter
571 YCONT2: lda (POINT),y ;Plot endpoint
584 bne YCONT ;If we pass a column boundary...
585 ror CHUNK ;then reset CHUNK to $80
587 lda POINT ;And add 8 to POINT
598 ; Big steps in X direction
600 ; On entry, X=DY=number of loop iterations, and Y=
605 .byte $00 ;Temporary counter
613 ror ;Need bit for initialization
614 sta Y1 ;High byte of counter
616 bne @CONT ;Could be $100
623 beq XFIXC ;If we pass a column boundary...
625 bcc XFIXY ;Time to step in Y?
628 dec COUNTHI ;High bits set?
631 lsr CHUNK ;Advance to last point
632 jsr LINEPLOT ;Plot the last chunk
638 ; CHUNK has passed a column, so plot and increment pointer
639 ; and fix up CHUNK, OLDCHUNK.
655 ; Check to make sure there isn't a high bit, plot chunk,
656 ; and update Y-coordinate.
658 XFIXY: dec Y1 ;Maybe high bit set
666 jsr LINEPLOT ;Plot chunk
681 ; Subroutine to plot chunks/points (to save a little
682 ; room, gray hair, etc.)
684 LINEPLOT: ; Plot the line chunk
695 ; Subroutine to fix up pointer when Y decreases through
696 ; zero or increases through 7.
698 FIXY: cpy #255 ;Y=255 or Y=8
701 @INCPTR: ;Add 320 to pointer
702 ldy #0 ;Y increased through 7
711 @DECPTR: ;Okay, subtract 320 then
712 ldy #7 ;Y decreased through 0
722 ; ------------------------------------------------------------------------
723 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
724 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
725 ; Contrary to most other functions, the graphics kernel will sort and clip
726 ; the coordinates before calling the driver, so on entry the following
727 ; conditions are valid:
730 ; (X1 >= 0) && (X1 < XRES)
731 ; (X2 >= 0) && (X2 < XRES)
732 ; (Y1 >= 0) && (Y1 < YRES)
733 ; (Y2 >= 0) && (Y2 < YRES)
735 ; Must set an error code: NO
738 ; Note: This function needs optimization. It's just a cheap translation of
739 ; the original C wrapper and could be written much smaller (besides that,
740 ; calling LINE is not a good idea either).
798 ; ------------------------------------------------------------------------
799 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
800 ; direction is passend in X/Y, the text direction is passed in A.
802 ; Must set an error code: NO
812 ; ------------------------------------------------------------------------
813 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
814 ; current text style. The text to output is given as a zero terminated
815 ; string with address in ptr3.
817 ; Must set an error code: NO
822 ; Calculate a pointer to the representation of the character in the
825 ldx #((>(CHARROM + $0800)) >> 3)
829 ldx #((>(CHARROM + $0000)) >> 3)
845 ; ------------------------------------------------------------------------
846 ; Calculate all variables to plot the pixel at X1/Y1.
853 lsr ; Neg is possible
871 adc #>VBASE ; +bitmap base
878 adc POINT ; +(X AND #$F8)