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 .addr $0000 ; Library reference
27 .word 320 ; X resolution
28 .word 200 ; Y resolution
29 .byte 2 ; Number of drawing colors
30 .byte 1 ; Number of screens available
31 .byte 8 ; System font X size
32 .byte 8 ; System font Y size
33 .word $00D4 ; Aspect ratio (based on 4/3 display)
34 .byte 0 ; TGI driver flags
36 ; Next comes the jump table. With the exception of IRQ, all entries must be
37 ; valid and may point to an RTS for test versions (function not implemented).
58 .addr 0 ; IRQ entry is unused
60 ; ------------------------------------------------------------------------
63 ; Variables mapped to the zero page segment variables. Some of these are
64 ; used for passing parameters to the driver.
72 ROW := tmp2 ; Bitmap row...
73 COL := tmp3 ; ...and column, both set by PLOT
77 INRANGE := regsave+2 ; PLOT variable, $00 = coordinates in range
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 := $D000 ; Color memory base address
121 VBASE := $E000 ; Video memory base address
126 ; ------------------------------------------------------------------------
127 ; INSTALL routine. Is called after the driver is loaded into memory. May
128 ; initialize anything that has to be done just once. Is probably empty
131 ; Must set an error code: NO
138 ; ------------------------------------------------------------------------
139 ; UNINSTALL routine. Is called before the driver is removed from memory. May
140 ; clean up anything done by INSTALL but is probably empty most of the time.
142 ; Must set an error code: NO
149 ; ------------------------------------------------------------------------
150 ; INIT: Changes an already installed device from text mode to graphics
152 ; Note that INIT/DONE may be called multiple times while the driver
153 ; is loaded, while INSTALL is only called once, so any code that is needed
154 ; to initializes variables and so on must go here. Setting palette and
155 ; clearing the screen is not needed because this is called by the graphics
157 ; The graphics kernel will never call INIT when a graphics mode is already
158 ; active, so there is no need to protect against that.
160 ; Must set an error code: YES
165 ; Initialize variables
170 ; Switch into graphics mode
172 lda $DD02 ; Set the data direction regs
176 and #$FC ; Switch to bank 3
181 lda #$48 ; Set color map to $D000, screen to $E000
184 lda $D011 ; And turn on bitmap
188 ; Done, reset the error code
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: lda $DD02 ; Set the data direction regs
209 lda OLDD018 ; Screen mem --> $0400
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
245 @L1: sta VBASE+$0000,y
281 ; ------------------------------------------------------------------------
282 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
283 ; The page number is already checked to be valid by the graphics kernel.
285 ; Must set an error code: NO (will only be called if page ok)
291 ; ------------------------------------------------------------------------
292 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
293 ; The page number is already checked to be valid by the graphics kernel.
295 ; Must set an error code: NO (will only be called if page ok)
301 ; ------------------------------------------------------------------------
302 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
303 ; to be in a valid range (0..maxcolor-1).
305 ; Must set an error code: NO (will only be called if color ok)
315 ; ------------------------------------------------------------------------
316 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
317 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
320 ; Must set an error code: YES
325 @L1: lda (ptr1),y ; Copy the palette
326 and #$0F ; Make a valid color
331 ; Get the color entries from the palette
333 lda PALETTE+1 ; Foreground color
338 ora PALETTE ; Background color
341 ; Initialize the color map with the new color settings (it is below the
346 lda $01 ; Get ROM config
348 and #%11111100 ; Clear bit 0 and 1
350 txa ; Load color code
351 @L2: sta CBASE+$0000,y
361 ; Done, reset the error code
367 ; ------------------------------------------------------------------------
368 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
369 ; set the palette should return the default palette here, so there's no
370 ; way for this function to fail.
372 ; Must set an error code: NO
380 ; ------------------------------------------------------------------------
381 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
382 ; drivers should return something reasonable here, even drivers that don't
383 ; support palettes, otherwise the caller has no way to determine the colors
384 ; of the (not changeable) palette.
386 ; Must set an error code: NO (all drivers must have a default palette)
394 ; ------------------------------------------------------------------------
395 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
396 ; color. The coordinates passed to this function are never outside the
397 ; visible screen area, so there is no need for clipping inside this function.
399 ; Must set an error code: NO
403 jsr CALC ; Calculate coordinates
405 sei ; Get underneath ROM
423 ; ------------------------------------------------------------------------
424 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
425 ; coordinates passed to this function are never outside the visible screen
426 ; area, so there is no need for clipping inside this function.
430 jsr CALC ; Calculate coordinates
432 sei ; Get underneath ROM
448 tya ; Get color value into A
449 ldx #$00 ; Clear high byte
452 ; ------------------------------------------------------------------------
453 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
454 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
456 ; To deal with off-screen coordinates, the current row
457 ; and column (40x25) is kept track of. These are set
458 ; negative when the point is off the screen, and made
459 ; positive when the point is within the visible screen.
461 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
462 ; Format is LINE x2,y2,x1,y1
464 ; Must set an error code: NO
469 @CHECK: lda X2 ;Make sure x1<x2
476 lda Y2 ;If not, swap P1 and P2
504 bpl @DYPOS ;Is y2>=y1?
505 lda Y1 ;Otherwise dy=y1-y2
511 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
515 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
520 sei ; Get underneath ROM
525 cpx DX ;Who's bigger: dy or dx?
526 bcc STEPINX ;If dx, then...
533 ; To simplify my life, just use PLOT to plot points.
536 ; Added special plotting routine -- cool!
538 ; X is now counter, Y is y-coordinate
540 ; On entry, X=DY=number of loop iterations, and Y=
544 sta OLDCHUNK ;So plotting routine will work right
550 bne @CONT ;If dy=0 it's just a point
552 @CONT: lsr ;Init counter to dy/2
558 lda INRANGE ;Range check
561 lda (POINT),y ;Otherwise plot
568 iny ;Advance Y coordinate
570 bcc @CONT ;No prob if Y=0..7
572 @CONT: lda TEMP ;Restore A
576 YCONT: dex ;X is counter
578 YCONT2: lda (POINT),y ;Plot endpoint
591 bne YCONT ;If we pass a column boundary...
592 ror CHUNK ;then reset CHUNK to $80
595 bmi @C1 ;Skip if column is negative
596 cmp #39 ;End if move past end of screen
598 @C1: lda POINT ;And add 8 to POINT
603 @CONT: inc COL ;Increment column
608 lda #00 ;Passed into col 0
616 ; Big steps in X direction
618 ; On entry, X=DY=number of loop iterations, and Y=
623 .byte $00 ;Temporary counter
631 ror ;Need bit for initialization
632 sta Y1 ;High byte of counter
634 bne @CONT ;Could be $100
641 beq XFIXC ;If we pass a column boundary...
643 bcc XFIXY ;Time to step in Y?
646 dec COUNTHI ;High bits set?
649 XDONE: lsr CHUNK ;Advance to last point
650 jsr LINEPLOT ;Plot the last chunk
656 ; CHUNK has passed a column, so plot and increment pointer
657 ; and fix up CHUNK, OLDCHUNK.
665 bmi @C1 ;Skip if column is negative
666 cmp #39 ;End if move past end of screen
684 ; Check to make sure there isn't a high bit, plot chunk,
685 ; and update Y-coordinate.
687 XFIXY: dec Y1 ;Maybe high bit set
695 jsr LINEPLOT ;Plot chunk
710 ; Subroutine to plot chunks/points (to save a little
711 ; room, gray hair, etc.)
713 LINEPLOT: ; Plot the line chunk
717 lda (POINT),Y ; Otherwise plot
727 ; Subroutine to fix up pointer when Y decreases through
728 ; zero or increases through 7.
730 FIXY: cpy #255 ;Y=255 or Y=8
732 @INCPTR: ;Add 320 to pointer
733 ldy #0 ;Y increased through 7
735 bmi @C1 ;If negative, then don't update
737 bcs @TOAST ;If at bottom of screen then quit
750 @DECPTR: ;Okay, subtract 320 then
751 ldy #7 ;Y decreased through 0
770 @TOAST: pla ;Remove old return address
772 jmp EXIT ;Restore interrupts, etc.
774 ; ------------------------------------------------------------------------
775 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
776 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
777 ; Contrary to most other functions, the graphics kernel will sort and clip
778 ; the coordinates before calling the driver, so on entry the following
779 ; conditions are valid:
782 ; (X1 >= 0) && (X1 < XRES)
783 ; (X2 >= 0) && (X2 < XRES)
784 ; (Y1 >= 0) && (Y1 < YRES)
785 ; (Y2 >= 0) && (Y2 < YRES)
787 ; Must set an error code: NO
790 ; Note: This function needs optimization. It's just a cheap translation of
791 ; the original C wrapper and could be written much smaller (besides that,
792 ; calling LINE is not a good idea either).
850 ; ------------------------------------------------------------------------
851 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
852 ; direction is passend in X/Y, the text direction is passed in A.
854 ; Must set an error code: NO
864 ; ------------------------------------------------------------------------
865 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
866 ; current text style. The text to output is given as a zero terminated
867 ; string with address in ptr3.
869 ; Must set an error code: NO
874 ; Calculate a pointer to the representation of the character in the
877 ldx #((>(CHARROM + $0800)) >> 3)
881 ldx #((>(CHARROM + $0000)) >> 3)
897 ; ------------------------------------------------------------------------
898 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
899 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
900 ; the coordinates are valid, INRANGE is zero and the carry clear.
907 lsr ; Neg is possible
925 adc #>VBASE ; +bitmap base
942 adc POINT ; +(X AND #$F8)