2 ; Graphics driver for the 320x200x2 mode on the C128.
4 ; Based on Stephen L. Judd's GRLIB code.
6 ; 2017-01-13, Greg King
7 ; 2018-03-13, Sven Klose
8 ; 2018-07-22, Scott Hutter
11 .include "zeropage.inc"
13 .include "tgi-kernel.inc"
14 .include "tgi-error.inc"
20 ; ------------------------------------------------------------------------
21 ; Header. Includes jump table and constants.
23 module_header _c128_hi_tgi
25 ; First part of the header is a structure that has a magic and defines the
26 ; capabilities of the driver
28 .byte $74, $67, $69 ; "tgi"
29 .byte TGI_API_VERSION ; TGI API version number
30 .addr $0000 ; Library reference
31 .word 320 ; X resolution
32 .word 200 ; Y resolution
33 .byte 2 ; Number of drawing colors
34 .byte 1 ; Number of screens available
35 .byte 8 ; System font X size
36 .byte 8 ; System font Y size
37 .word $00D4 ; Aspect ratio (based on 4/3 display)
38 .byte 0 ; TGI driver flags
40 ; Next comes the jump table. With the exception of IRQ, all entries must be
41 ; valid and may point to an RTS for test versions (function not implemented).
63 ; ------------------------------------------------------------------------
66 ; Variables mapped to the zero page segment variables. Some of these are
67 ; used for passing parameters to the driver.
79 CHUNK := X2 ; Used in the line routine
80 OLDCHUNK := X2+1 ; Dito
82 ; Absolute variables used in the code
86 ERROR: .res 1 ; Error code
87 PALETTE: .res 2 ; The current palette
89 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
92 OLDD018: .res 1 ; Old register value
109 ; Constants and tables
113 DEFPALETTE: .byte $00, $01 ; White on black
114 PALETTESIZE = * - DEFPALETTE
116 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
117 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
119 CHARROM := $D000 ; Character rom base address
120 CBASE := $5C00 ; Color memory base address
121 VBASE := $6000 ; Video memory base address
122 ; BASE + $4000 for each bank above VIC bank 0
123 ; $2000 = VIC bank 0 (base)
131 ; ------------------------------------------------------------------------
132 ; INSTALL routine. Is called after the driver is loaded into memory. May
133 ; initialize anything that has to be done just once. Is probably empty
136 ; Must set an error code: NO
143 ; ------------------------------------------------------------------------
144 ; UNINSTALL routine. Is called before the driver is removed from memory. May
145 ; clean up anything done by INSTALL but is probably empty most of the time.
147 ; Must set an error code: NO
154 ; ------------------------------------------------------------------------
155 ; INIT: Changes an already installed device from text mode to graphics
157 ; Note that INIT/DONE may be called multiple times while the driver
158 ; is loaded, while INSTALL is only called once, so any code that is needed
159 ; to initializes variables and so on must go here. Setting palette and
160 ; clearing the screen is not needed because this is called by the graphics
162 ; The graphics kernel will never call INIT when a graphics mode is already
163 ; active, so there is no need to protect against that.
165 ; Must set an error code: YES
169 ; Initialize variables
173 ; Switch into graphics mode
176 ; bank 0=3 ($0-$3FFF) (default)
177 ; bank 1=2 ($4000-$7FFF) *
178 ; bank 2=1 ($8000-$BFFF)
179 ; bank 3=0 ($C000-$FFFF)
182 ora #$02 ; bank number 1 ($4000)
185 ; Switch to bitmap mode
190 DONE1: lda #TGI_ERR_OK
194 ; ------------------------------------------------------------------------
195 ; DONE: Will be called to switch the graphics device back into text mode.
196 ; The graphics kernel will never call DONE when no graphics mode is active,
197 ; so there is no need to protect against that.
199 ; Must set an error code: NO
202 DONE: ; select video bank
205 ora #$03 ; bank 0=3 ($0-$3FFF), 1=2 ($4000-$7FFF), 2=1 ($8000-$BFFF), 3=0 ($C000-$FFFF)
208 lda $0A2D ; change screen ram location
213 LDA #$00 ; switch back to text mode
217 ; ------------------------------------------------------------------------
218 ; GETERROR: Return the error code in A and clear it.
226 ; ------------------------------------------------------------------------
227 ; CONTROL: Platform/driver specific entry point.
229 ; Must set an error code: YES
233 lda #TGI_ERR_INV_FUNC
237 ; ------------------------------------------------------------------------
238 ; CLEAR: Clears the screen.
240 ; Must set an error code: NO
246 @L1: sta VBASE+$0000,y
277 sta VBASE+$1E40,y ; preserve vectors
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 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
458 ; Format is LINE x2,y2,x1,y1
460 ; Must set an error code: NO
465 @CHECK: lda X2 ;Make sure x1<x2
472 lda Y2 ;If not, swap P1 and P2
500 bpl @DYPOS ;Is y2>=y1?
501 lda Y1 ;Otherwise dy=y1-y2
507 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
511 jsr CALC ; Set up .X, .Y, and POINT
516 sei ; Get underneath ROM
521 cpx DX ;Who's bigger: dy or dx?
522 bcc STEPINX ;If dx, then...
529 ; To simplify my life, just use PLOT to plot points.
532 ; Added special plotting routine -- cool!
534 ; X is now counter, Y is y-coordinate
536 ; On entry, X=DY=number of loop iterations, and Y=
540 sta OLDCHUNK ;So plotting routine will work right
546 beq YCONT2 ;If dy=0, it's just a point
547 @CONT: lsr ;Init counter to dy/2
559 iny ;Advance Y coordinate
561 bcc @CONT ;No prob if Y=0..7
563 @CONT: lda TEMP ;Restore A
567 YCONT: dex ;X is counter
569 YCONT2: lda (POINT),y ;Plot endpoint
582 bne YCONT ;If we pass a column boundary...
583 ror CHUNK ;then reset CHUNK to $80
585 lda POINT ;And add 8 to POINT
596 ; Big steps in X direction
598 ; On entry, X=DY=number of loop iterations, and Y=
603 .byte $00 ;Temporary counter
611 ror ;Need bit for initialization
612 sta Y1 ;High byte of counter
614 bne @CONT ;Could be $100
621 beq XFIXC ;If we pass a column boundary...
623 bcc XFIXY ;Time to step in Y?
626 dec COUNTHI ;High bits set?
629 lsr CHUNK ;Advance to last point
630 jsr LINEPLOT ;Plot the last chunk
636 ; CHUNK has passed a column, so plot and increment pointer
637 ; and fix up CHUNK, OLDCHUNK.
653 ; Check to make sure there isn't a high bit, plot chunk,
654 ; and update Y-coordinate.
656 XFIXY: dec Y1 ;Maybe high bit set
664 jsr LINEPLOT ;Plot chunk
679 ; Subroutine to plot chunks/points (to save a little
680 ; room, gray hair, etc.)
682 LINEPLOT: ; Plot the line chunk
693 ; Subroutine to fix up pointer when Y decreases through
694 ; zero or increases through 7.
696 FIXY: cpy #255 ;Y=255 or Y=8
699 @INCPTR: ;Add 320 to pointer
700 ldy #0 ;Y increased through 7
709 @DECPTR: ;Okay, subtract 320 then
710 ldy #7 ;Y decreased through 0
720 ; ------------------------------------------------------------------------
721 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
722 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
723 ; Contrary to most other functions, the graphics kernel will sort and clip
724 ; the coordinates before calling the driver, so on entry the following
725 ; conditions are valid:
728 ; (X1 >= 0) && (X1 < XRES)
729 ; (X2 >= 0) && (X2 < XRES)
730 ; (Y1 >= 0) && (Y1 < YRES)
731 ; (Y2 >= 0) && (Y2 < YRES)
733 ; Must set an error code: NO
736 ; Note: This function needs optimization. It's just a cheap translation of
737 ; the original C wrapper and could be written much smaller (besides that,
738 ; calling LINE is not a good idea either).
796 ; ------------------------------------------------------------------------
797 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
798 ; direction is passend in X/Y, the text direction is passed in A.
800 ; Must set an error code: NO
810 ; ------------------------------------------------------------------------
811 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
812 ; current text style. The text to output is given as a zero terminated
813 ; string with address in ptr3.
815 ; Must set an error code: NO
820 ; Calculate a pointer to the representation of the character in the
823 ldx #((>(CHARROM + $0800)) >> 3)
827 ldx #((>(CHARROM + $0000)) >> 3)
843 ; ------------------------------------------------------------------------
844 ; Calculate all variables to plot the pixel at X1/Y1.
851 lsr ; Neg is possible
869 adc #>VBASE ; +bitmap base
876 adc POINT ; +(X AND #$F8)