2 ; Graphics driver for the 320x200x2 mode on the C128.
4 ; Based on Stephen L. Judd's GRLIB code.
6 ; 2018-03-13, Sven Klose
7 ; 2018-07-22, Scott Hutter
8 ; 2018-07-28, Greg King
11 .include "zeropage.inc"
13 .include "tgi-kernel.inc"
14 .include "tgi-error.inc"
21 ; ------------------------------------------------------------------------
22 ; Header. Includes jump table and constants.
24 module_header _c128_hi_tgi
26 ; First part of the header is a structure that has a magic and defines the
27 ; capabilities of the driver
29 .byte $74, $67, $69 ; "tgi"
30 .byte TGI_API_VERSION ; TGI API version number
31 .addr $0000 ; Library reference
32 .word 320 ; X resolution
33 .word 200 ; Y resolution
34 .byte 2 ; Number of drawing colors
35 .byte 1 ; Number of screens available
36 .byte 8 ; System font X size
37 .byte 8 ; System font Y size
38 .word $00D4 ; Aspect ratio (based on 4/3 display)
39 .byte 0 ; TGI driver flags
41 ; Next comes the jump table. With the exception of IRQ, all entries must be
42 ; valid and may point to an RTS for test versions (function not implemented).
64 ; ------------------------------------------------------------------------
67 ; Variables mapped to the zero page segment variables. Some of these are
68 ; used for passing parameters to the driver.
80 CHUNK := X2 ; Used in the line routine
81 OLDCHUNK := X2+1 ; Ditto
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
107 ; Constants and tables
111 DEFPALETTE: .byte $00, $01 ; White on black
112 PALETTESIZE = * - DEFPALETTE
114 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
115 BITCHUNK: .byte $FF,$7F,$3F,$1F,$0F,$07,$03,$01
117 CHARROM := $D000 ; Character ROM base address
119 VBASE := $C000 ; Video memory base address
120 CBASE := $E000 ; Color memory base address
124 ; ------------------------------------------------------------------------
125 ; INSTALL routine. Is called after the driver is loaded into memory. May
126 ; initialize anything that has to be done just once. Is probably empty
129 ; Must set an error code: NO
136 ; ------------------------------------------------------------------------
137 ; UNINSTALL routine. Is called before the driver is removed from memory. May
138 ; clean up anything done by INSTALL but is probably empty most of the time.
140 ; Must set an error code: NO
147 ; ------------------------------------------------------------------------
148 ; INIT: Changes an already installed device from text mode to graphics
150 ; Note that INIT/DONE may be called multiple times while the driver
151 ; is loaded, while INSTALL is only called once, so any code that is needed
152 ; to initializes variables and so on must go here. Setting palette and
153 ; clearing the screen is not needed because this is called by the graphics
155 ; The graphics kernel will never call INIT when a graphics mode is already
156 ; active, so there is no need to protect against that.
158 ; Must set an error code: YES
163 ; Initialize variables.
165 ldx #$FF ; Foreground color
168 ; Switch into graphics mode.
170 ; Select a video bank:
171 ; bank 0 = $03 ($0000-$3FFF) (default)
172 ; bank 1 = $02 ($4000-$7FFF)
173 ; bank 2 = $01 ($8000-$BFFF)
174 ; bank 3 = $00 ($C000-$FFFF) (TGI)
180 lda #$80 ; color-map at $E000, bitmap at $C000
183 ; Make the VIC-IIe read RAM instead of the font ROM.
188 ; Switch to bitmap mode.
197 ; ------------------------------------------------------------------------
198 ; DONE: Will be called to switch the graphics device back into text mode.
199 ; The graphics kernel will never call DONE when no graphics mode is active,
200 ; so there is no need to protect against that.
202 ; Must set an error code: NO
207 ; Select the text video bank.
213 ; Make the VIC-IIe read the font ROM instead of RAM.
218 ;lda #%00000000 ; Switch back to text mode
221 ; Restore a value that's needed by BASIC's GRAPHIC 1 statement.
223 lda #$78 ; color-map at $1C00, bitmap at $2000
227 ; ------------------------------------------------------------------------
228 ; GETERROR: Return the error code in A and clear it.
236 ; ------------------------------------------------------------------------
237 ; CONTROL: Platform/driver specific entry point.
239 ; Must set an error code: YES
243 lda #TGI_ERR_INV_FUNC
247 ; ------------------------------------------------------------------------
248 ; CLEAR: Clears the screen.
250 ; Must set an error code: NO
259 @L1: sta VBASE+$0000,y
298 ; ------------------------------------------------------------------------
299 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
300 ; The page number is already checked to be valid by the graphics kernel.
302 ; Must set an error code: NO (will only be called if page ok)
308 ; ------------------------------------------------------------------------
309 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
310 ; The page number is already checked to be valid by the graphics kernel.
312 ; Must set an error code: NO (will only be called if page ok)
318 ; ------------------------------------------------------------------------
319 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
320 ; to be in a valid range (0..maxcolor-1).
322 ; Must set an error code: NO (will only be called if color ok)
332 ; ------------------------------------------------------------------------
333 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
334 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
337 ; Must set an error code: YES
342 @L1: lda (ptr1),y ; Copy the palette
343 and #$0F ; Make a valid color
348 ; Get the color entries from the palette
350 lda PALETTE+1 ; Foreground color
355 ora PALETTE ; Background color
357 ; Initialize the color map with the new color settings (it is below the
364 @L2: sta CBASE+$0000,y
374 ; Done, reset the error code
380 ; ------------------------------------------------------------------------
381 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
382 ; set the palette should return the default palette here, so there's no
383 ; way for this function to fail.
385 ; Must set an error code: NO
393 ; ------------------------------------------------------------------------
394 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
395 ; drivers should return something reasonable here, even drivers that don't
396 ; support palettes, otherwise the caller has no way to determine the colors
397 ; of the (not changeable) palette.
399 ; Must set an error code: NO (all drivers must have a default palette)
407 ; ------------------------------------------------------------------------
408 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
409 ; color. The coordinates passed to this function are never outside the
410 ; visible screen area, so there is no need for clipping inside this function.
412 ; Must set an error code: NO
416 jsr CALC ; Calculate coordinates
418 lda #MMU_CFG_RAM0 ; Work behind ROMs
434 ; ------------------------------------------------------------------------
435 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
436 ; coordinates passed to this function are never outside the visible screen
437 ; area, so there is no need for clipping inside this function.
441 jsr CALC ; Calculate coordinates
443 lda #MMU_CFG_RAM0 ; Work behind ROMs
450 lda #$01 ; Foreground color
452 @L1: ldy #MMU_CFG_CC65
455 ldx #$00 ; Clear high byte
458 ; ------------------------------------------------------------------------
459 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
460 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
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, and POINT
521 lda #MMU_CFG_RAM0 ; Work behind ROMs
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 beq YCONT2 ;If dy=0, it's just a point
552 @CONT: lsr ;Init counter to dy/2
564 iny ;Advance Y coordinate
566 bcc @CONT ;No prob if Y=0..7
568 @CONT: lda TEMP ;Restore A
572 YCONT: dex ;X is counter
574 YCONT2: lda (POINT),y ;Plot endpoint
587 bne YCONT ;If we pass a column boundary...
588 ror CHUNK ;then reset CHUNK to $80
590 lda POINT ;And add 8 to POINT
601 ; Big steps in X direction
603 ; On entry, X=DY=number of loop iterations, and Y=
608 .byte $00 ;Temporary counter
616 ror ;Need bit for initialization
617 sta Y1 ;High byte of counter
619 bne @CONT ;Could be $100
626 beq XFIXC ;If we pass a column boundary...
628 bcc XFIXY ;Time to step in Y?
631 dec COUNTHI ;High bits set?
634 lsr CHUNK ;Advance to last point
635 jsr LINEPLOT ;Plot the last chunk
641 ; CHUNK has passed a column, so plot and increment pointer
642 ; and fix up CHUNK, OLDCHUNK.
658 ; Check to make sure there isn't a high bit, plot chunk,
659 ; and update Y-coordinate.
661 XFIXY: dec Y1 ;Maybe high bit set
669 jsr LINEPLOT ;Plot chunk
684 ; Subroutine to plot chunks/points (to save a little
685 ; room, gray hair, etc.)
687 LINEPLOT: ; Plot the line chunk
698 ; Subroutine to fix up pointer when Y decreases through
699 ; zero or increases through 7.
701 FIXY: cpy #255 ;Y=255 or Y=8
704 @INCPTR: ;Add 320 to pointer
705 ldy #0 ;Y increased through 7
714 @DECPTR: ;Okay, subtract 320 then
715 ldy #7 ;Y decreased through 0
725 ; ------------------------------------------------------------------------
726 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
727 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
728 ; Contrary to most other functions, the graphics kernel will sort and clip
729 ; the coordinates before calling the driver, so on entry the following
730 ; conditions are valid:
733 ; (X1 >= 0) && (X1 < XRES)
734 ; (X2 >= 0) && (X2 < XRES)
735 ; (Y1 >= 0) && (Y1 < YRES)
736 ; (Y2 >= 0) && (Y2 < YRES)
738 ; Must set an error code: NO
741 ; Note: This function needs optimization. It's just a cheap translation of
742 ; the original C wrapper and could be written much smaller (besides that,
743 ; calling LINE is not a good idea either).
792 ; ------------------------------------------------------------------------
793 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
794 ; direction is passend in X/Y, the text direction is passed in A.
796 ; Must set an error code: NO
806 ; ------------------------------------------------------------------------
807 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
808 ; current text style. The text to output is given as a zero terminated
809 ; string with address in ptr3.
811 ; Must set an error code: NO
816 ; Calculate a pointer to the representation of the character in the
819 ldx #((>(CHARROM + $0800)) >> 3)
823 ldx #((>(CHARROM + $0000)) >> 3)
839 ; ------------------------------------------------------------------------
840 ; Calculate all variables to plot the pixel at X1/Y1.
847 lsr ; Neg is possible
865 adc #>VBASE ; +bitmap base
872 adc POINT ; +(X AND #$F8)