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
9 .include "zeropage.inc"
11 .include "tgi-kernel.inc"
12 .include "tgi-error.inc"
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table and constants.
21 module_header _c64_hi_tgi
23 ; First part of the header is a structure that has a magic and defines the
24 ; capabilities of the driver
26 .byte $74, $67, $69 ; "tgi"
27 .byte TGI_API_VERSION ; TGI API version number
28 .addr $0000 ; Library reference
29 .word 320 ; X resolution
30 .word 200 ; Y resolution
31 .byte 2 ; Number of drawing colors
32 .byte 1 ; Number of screens available
33 .byte 8 ; System font X size
34 .byte 8 ; System font Y size
35 .word $00D4 ; Aspect ratio (based on 4/3 display)
36 .byte 0 ; TGI driver flags
38 ; Next comes the jump table. With the exception of IRQ, all entries must be
39 ; valid and may point to an RTS for test versions (function not implemented).
61 ; ------------------------------------------------------------------------
64 ; Variables mapped to the zero page segment variables. Some of these are
65 ; used for passing parameters to the driver.
73 ROW := tmp2 ; Bitmap row...
74 COL := tmp3 ; ...and column, both set by PLOT
78 INRANGE := regsave+2 ; PLOT variable, $00 = coordinates in range
80 CHUNK := X2 ; Used in the line routine
81 OLDCHUNK := X2+1 ; Dito
83 ; Absolute variables used in the code
87 ERROR: .res 1 ; Error code
88 PALETTE: .res 2 ; The current palette
90 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
93 OLDD018: .res 1 ; Old register value
110 ; Constants and tables
114 DEFPALETTE: .byte $00, $01 ; White on black
115 PALETTESIZE = * - DEFPALETTE
117 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
118 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
120 CHARROM := $D000 ; Character rom base address
121 CBASE := $D000 ; Color memory base address
122 VBASE := $E000 ; Video memory base address
127 ; ------------------------------------------------------------------------
128 ; INSTALL routine. Is called after the driver is loaded into memory. May
129 ; initialize anything that has to be done just once. Is probably empty
132 ; Must set an error code: NO
139 ; ------------------------------------------------------------------------
140 ; UNINSTALL routine. Is called before the driver is removed from memory. May
141 ; clean up anything done by INSTALL but is probably empty most of the time.
143 ; Must set an error code: NO
150 ; ------------------------------------------------------------------------
151 ; INIT: Changes an already installed device from text mode to graphics
153 ; Note that INIT/DONE may be called multiple times while the driver
154 ; is loaded, while INSTALL is only called once, so any code that is needed
155 ; to initializes variables and so on must go here. Setting palette and
156 ; clearing the screen is not needed because this is called by the graphics
158 ; The graphics kernel will never call INIT when a graphics mode is already
159 ; active, so there is no need to protect against that.
161 ; Must set an error code: YES
166 ; Initialize variables
171 ; Switch into graphics mode
173 lda $DD02 ; Set the data direction regs
177 and #$FC ; Switch to bank 3
182 lda #$48 ; Set color map to $D000, screen to $E000
185 lda $D011 ; And turn on bitmap
189 ; Done, reset the error code
195 ; ------------------------------------------------------------------------
196 ; DONE: Will be called to switch the graphics device back into text mode.
197 ; The graphics kernel will never call DONE when no graphics mode is active,
198 ; so there is no need to protect against that.
200 ; Must set an error code: NO
203 DONE: lda $DD02 ; Set the data direction regs
210 lda OLDD018 ; Screen mem --> $0400
218 ; ------------------------------------------------------------------------
219 ; GETERROR: Return the error code in A and clear it.
227 ; ------------------------------------------------------------------------
228 ; CONTROL: Platform/driver specific entry point.
230 ; Must set an error code: YES
234 lda #TGI_ERR_INV_FUNC
238 ; ------------------------------------------------------------------------
239 ; CLEAR: Clears the screen.
241 ; Must set an error code: NO
246 @L1: sta VBASE+$0000,y
282 ; ------------------------------------------------------------------------
283 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
284 ; The page number is already checked to be valid by the graphics kernel.
286 ; Must set an error code: NO (will only be called if page ok)
292 ; ------------------------------------------------------------------------
293 ; SETDRAWPAGE: Set the drawable 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 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
304 ; to be in a valid range (0..maxcolor-1).
306 ; Must set an error code: NO (will only be called if color ok)
316 ; ------------------------------------------------------------------------
317 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
318 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
321 ; Must set an error code: YES
326 @L1: lda (ptr1),y ; Copy the palette
327 and #$0F ; Make a valid color
332 ; Get the color entries from the palette
334 lda PALETTE+1 ; Foreground color
339 ora PALETTE ; Background color
342 ; Initialize the color map with the new color settings (it is below the
347 lda $01 ; Get ROM config
349 and #%11111100 ; Clear bit 0 and 1
351 txa ; Load color code
352 @L2: sta CBASE+$0000,y
362 ; Done, reset the error code
368 ; ------------------------------------------------------------------------
369 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
370 ; set the palette should return the default palette here, so there's no
371 ; way for this function to fail.
373 ; Must set an error code: NO
381 ; ------------------------------------------------------------------------
382 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
383 ; drivers should return something reasonable here, even drivers that don't
384 ; support palettes, otherwise the caller has no way to determine the colors
385 ; of the (not changeable) palette.
387 ; Must set an error code: NO (all drivers must have a default palette)
395 ; ------------------------------------------------------------------------
396 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
397 ; color. The coordinates passed to this function are never outside the
398 ; visible screen area, so there is no need for clipping inside this function.
400 ; Must set an error code: NO
404 jsr CALC ; Calculate coordinates
406 sei ; Get underneath ROM
424 ; ------------------------------------------------------------------------
425 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
426 ; coordinates passed to this function are never outside the visible screen
427 ; area, so there is no need for clipping inside this function.
431 jsr CALC ; Calculate coordinates
433 sei ; Get underneath ROM
449 tya ; Get color value into A
450 ldx #$00 ; Clear high byte
453 ; ------------------------------------------------------------------------
454 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
455 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
457 ; To deal with off-screen coordinates, the current row
458 ; and column (40x25) is kept track of. These are set
459 ; negative when the point is off the screen, and made
460 ; positive when the point is within the visible screen.
462 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
463 ; Format is LINE x2,y2,x1,y1
465 ; Must set an error code: NO
470 @CHECK: lda X2 ;Make sure x1<x2
477 lda Y2 ;If not, swap P1 and P2
505 bpl @DYPOS ;Is y2>=y1?
506 lda Y1 ;Otherwise dy=y1-y2
512 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
516 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
521 sei ; Get underneath ROM
526 cpx DX ;Who's bigger: dy or dx?
527 bcc STEPINX ;If dx, then...
534 ; To simplify my life, just use PLOT to plot points.
537 ; Added special plotting routine -- cool!
539 ; X is now counter, Y is y-coordinate
541 ; On entry, X=DY=number of loop iterations, and Y=
545 sta OLDCHUNK ;So plotting routine will work right
551 bne @CONT ;If dy=0 it's just a point
553 @CONT: lsr ;Init counter to dy/2
559 lda INRANGE ;Range check
562 lda (POINT),y ;Otherwise plot
569 iny ;Advance Y coordinate
571 bcc @CONT ;No prob if Y=0..7
573 @CONT: lda TEMP ;Restore A
577 YCONT: dex ;X is counter
579 YCONT2: lda (POINT),y ;Plot endpoint
592 bne YCONT ;If we pass a column boundary...
593 ror CHUNK ;then reset CHUNK to $80
596 bmi @C1 ;Skip if column is negative
597 cmp #39 ;End if move past end of screen
599 @C1: lda POINT ;And add 8 to POINT
604 @CONT: inc COL ;Increment column
609 lda #00 ;Passed into col 0
617 ; Big steps in X direction
619 ; On entry, X=DY=number of loop iterations, and Y=
624 .byte $00 ;Temporary counter
632 ror ;Need bit for initialization
633 sta Y1 ;High byte of counter
635 bne @CONT ;Could be $100
642 beq XFIXC ;If we pass a column boundary...
644 bcc XFIXY ;Time to step in Y?
647 dec COUNTHI ;High bits set?
650 XDONE: lsr CHUNK ;Advance to last point
651 jsr LINEPLOT ;Plot the last chunk
657 ; CHUNK has passed a column, so plot and increment pointer
658 ; and fix up CHUNK, OLDCHUNK.
666 bmi @C1 ;Skip if column is negative
667 cmp #39 ;End if move past end of screen
685 ; Check to make sure there isn't a high bit, plot chunk,
686 ; and update Y-coordinate.
688 XFIXY: dec Y1 ;Maybe high bit set
696 jsr LINEPLOT ;Plot chunk
711 ; Subroutine to plot chunks/points (to save a little
712 ; room, gray hair, etc.)
714 LINEPLOT: ; Plot the line chunk
718 lda (POINT),Y ; Otherwise plot
728 ; Subroutine to fix up pointer when Y decreases through
729 ; zero or increases through 7.
731 FIXY: cpy #255 ;Y=255 or Y=8
733 @INCPTR: ;Add 320 to pointer
734 ldy #0 ;Y increased through 7
736 bmi @C1 ;If negative, then don't update
738 bcs @TOAST ;If at bottom of screen then quit
751 @DECPTR: ;Okay, subtract 320 then
752 ldy #7 ;Y decreased through 0
771 @TOAST: pla ;Remove old return address
773 jmp EXIT ;Restore interrupts, etc.
775 ; ------------------------------------------------------------------------
776 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
777 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
778 ; Contrary to most other functions, the graphics kernel will sort and clip
779 ; the coordinates before calling the driver, so on entry the following
780 ; conditions are valid:
783 ; (X1 >= 0) && (X1 < XRES)
784 ; (X2 >= 0) && (X2 < XRES)
785 ; (Y1 >= 0) && (Y1 < YRES)
786 ; (Y2 >= 0) && (Y2 < YRES)
788 ; Must set an error code: NO
791 ; Note: This function needs optimization. It's just a cheap translation of
792 ; the original C wrapper and could be written much smaller (besides that,
793 ; calling LINE is not a good idea either).
851 ; ------------------------------------------------------------------------
852 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
853 ; direction is passend in X/Y, the text direction is passed in A.
855 ; Must set an error code: NO
865 ; ------------------------------------------------------------------------
866 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
867 ; current text style. The text to output is given as a zero terminated
868 ; string with address in ptr3.
870 ; Must set an error code: NO
875 ; Calculate a pointer to the representation of the character in the
878 ldx #((>(CHARROM + $0800)) >> 3)
882 ldx #((>(CHARROM + $0000)) >> 3)
898 ; ------------------------------------------------------------------------
899 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
900 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
901 ; the coordinates are valid, INRANGE is zero and the carry clear.
908 lsr ; Neg is possible
926 adc #>VBASE ; +bitmap base
943 adc POINT ; +(X AND #$F8)