2 ; Graphics driver for the 320x200x2 mode on the C64.
4 ; Based on Stephen L. Judds GRLIB code
7 .include "zeropage.inc"
9 .include "tgi-kernel.inc"
10 .include "tgi-error.inc"
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table and constants.
21 ; First part of the header is a structure that has a magic and defines the
22 ; capabilities of the driver
24 .byte $74, $67, $69 ; "tgi"
25 .byte TGI_API_VERSION ; TGI API version number
26 .word 320 ; X resolution
27 .word 200 ; Y resolution
28 .byte 2 ; Number of drawing colors
29 .byte 1 ; Number of screens available
30 .byte 8 ; System font X size
31 .byte 8 ; System font Y size
32 .word $00D4 ; Aspect ratio (based on 4/3 display)
33 .byte 0 ; TGI driver flags
35 ; Next comes the jump table. With the exception of IRQ, all entries must be
36 ; valid and may point to an RTS for test versions (function not implemented).
57 .addr 0 ; IRQ entry is unused
59 ; ------------------------------------------------------------------------
62 ; Variables mapped to the zero page segment variables. Some of these are
63 ; used for passing parameters to the driver.
71 ROW := tmp2 ; Bitmap row...
72 COL := tmp3 ; ...and column, both set by PLOT
76 INRANGE := regsave+2 ; PLOT variable, $00 = coordinates in range
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
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 ; To deal with off-screen coordinates, the current row
456 ; and column (40x25) is kept track of. These are set
457 ; negative when the point is off the screen, and made
458 ; positive when the point is within the visible screen.
460 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
461 ; Format is LINE x2,y2,x1,y1
463 ; Must set an error code: NO
468 @CHECK: lda X2 ;Make sure x1<x2
475 lda Y2 ;If not, swap P1 and P2
503 bpl @DYPOS ;Is y2>=y1?
504 lda Y1 ;Otherwise dy=y1-y2
510 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
514 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
519 sei ; Get underneath ROM
524 cpx DX ;Who's bigger: dy or dx?
525 bcc STEPINX ;If dx, then...
532 ; To simplify my life, just use PLOT to plot points.
535 ; Added special plotting routine -- cool!
537 ; X is now counter, Y is y-coordinate
539 ; On entry, X=DY=number of loop iterations, and Y=
543 sta OLDCHUNK ;So plotting routine will work right
549 bne @CONT ;If dy=0 it's just a point
551 @CONT: lsr ;Init counter to dy/2
557 lda INRANGE ;Range check
560 lda (POINT),y ;Otherwise plot
567 iny ;Advance Y coordinate
569 bcc @CONT ;No prob if Y=0..7
571 @CONT: lda TEMP ;Restore A
575 YCONT: dex ;X is counter
577 YCONT2: lda (POINT),y ;Plot endpoint
590 bne YCONT ;If we pass a column boundary...
591 ror CHUNK ;then reset CHUNK to $80
594 bmi @C1 ;Skip if column is negative
595 cmp #39 ;End if move past end of screen
597 @C1: lda POINT ;And add 8 to POINT
602 @CONT: inc COL ;Increment column
607 lda #00 ;Passed into col 0
615 ; Big steps in X direction
617 ; On entry, X=DY=number of loop iterations, and Y=
622 .byte $00 ;Temporary counter
630 ror ;Need bit for initialization
631 sta Y1 ;High byte of counter
633 bne @CONT ;Could be $100
640 beq XFIXC ;If we pass a column boundary...
642 bcc XFIXY ;Time to step in Y?
645 dec COUNTHI ;High bits set?
648 XDONE: lsr CHUNK ;Advance to last point
649 jsr LINEPLOT ;Plot the last chunk
655 ; CHUNK has passed a column, so plot and increment pointer
656 ; and fix up CHUNK, OLDCHUNK.
664 bmi @C1 ;Skip if column is negative
665 cmp #39 ;End if move past end of screen
683 ; Check to make sure there isn't a high bit, plot chunk,
684 ; and update Y-coordinate.
686 XFIXY: dec Y1 ;Maybe high bit set
694 jsr LINEPLOT ;Plot chunk
709 ; Subroutine to plot chunks/points (to save a little
710 ; room, gray hair, etc.)
712 LINEPLOT: ; Plot the line chunk
716 lda (POINT),Y ; Otherwise plot
726 ; Subroutine to fix up pointer when Y decreases through
727 ; zero or increases through 7.
729 FIXY: cpy #255 ;Y=255 or Y=8
731 @INCPTR: ;Add 320 to pointer
732 ldy #0 ;Y increased through 7
734 bmi @C1 ;If negative, then don't update
736 bcs @TOAST ;If at bottom of screen then quit
749 @DECPTR: ;Okay, subtract 320 then
750 ldy #7 ;Y decreased through 0
769 @TOAST: pla ;Remove old return address
771 jmp EXIT ;Restore interrupts, etc.
773 ; ------------------------------------------------------------------------
774 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
775 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
776 ; Contrary to most other functions, the graphics kernel will sort and clip
777 ; the coordinates before calling the driver, so on entry the following
778 ; conditions are valid:
781 ; (X1 >= 0) && (X1 < XRES)
782 ; (X2 >= 0) && (X2 < XRES)
783 ; (Y1 >= 0) && (Y1 < YRES)
784 ; (Y2 >= 0) && (Y2 < YRES)
786 ; Must set an error code: NO
789 ; Note: This function needs optimization. It's just a cheap translation of
790 ; the original C wrapper and could be written much smaller (besides that,
791 ; calling LINE is not a good idea either).
849 ; ------------------------------------------------------------------------
850 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
851 ; direction is passend in X/Y, the text direction is passed in A.
853 ; Must set an error code: NO
863 ; ------------------------------------------------------------------------
864 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
865 ; current text style. The text to output is given as a zero terminated
866 ; string with address in ptr3.
868 ; Must set an error code: NO
873 ; Calculate a pointer to the representation of the character in the
876 ldx #((>(CHARROM + $0800)) >> 3)
880 ldx #((>(CHARROM + $0000)) >> 3)
896 ; ------------------------------------------------------------------------
897 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
898 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
899 ; the coordinates are valid, INRANGE is zero and the carry clear.
906 lsr ; Neg is possible
924 adc #>VBASE ; +bitmap base
941 adc POINT ; +(X AND #$F8)