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-mode.inc"
11 .include "tgi-error.inc"
17 ; ------------------------------------------------------------------------
18 ; Header. Includes jump table and constants.
22 ; First part of the header is a structure that has a magic and defines the
23 ; capabilities of the driver
25 .byte $74, $67, $69 ; "tgi"
26 .byte TGI_API_VERSION ; TGI API version number
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 $100 ; Aspect ratio
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.
70 ROW := tmp2 ; Bitmap row...
71 COL := tmp3 ; ...and column, both set by PLOT
75 INRANGE := regsave+2 ; PLOT variable, $00 = coordinates in range
77 CHUNK := X2 ; Used in the line routine
78 OLDCHUNK := X2+1 ; Dito
80 ; Absolute variables used in the code
84 ERROR: .res 1 ; Error code
85 PALETTE: .res 2 ; The current palette
87 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
90 OLDD018: .res 1 ; Old register value
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 VBASE = $E000 ; Video memory base address
118 CBASE = $D000 ; Color memory base address
123 ; ------------------------------------------------------------------------
124 ; INSTALL routine. Is called after the driver is loaded into memory. May
125 ; initialize anything that has to be done just once. Is probably empty
128 ; Must set an error code: NO
135 ; ------------------------------------------------------------------------
136 ; UNINSTALL routine. Is called before the driver is removed from memory. May
137 ; clean up anything done by INSTALL but is probably empty most of the time.
139 ; Must set an error code: NO
146 ; ------------------------------------------------------------------------
147 ; INIT: Changes an already installed device from text mode to graphics
149 ; Note that INIT/DONE may be called multiple times while the driver
150 ; is loaded, while INSTALL is only called once, so any code that is needed
151 ; to initializes variables and so on must go here. Setting palette and
152 ; clearing the screen is not needed because this is called by the graphics
154 ; The graphics kernel will never call INIT when a graphics mode is already
155 ; active, so there is no need to protect against that.
157 ; Must set an error code: YES
162 ; Initialize variables
167 ; Switch into graphics mode
169 lda $DD02 ; Set the data direction regs
173 and #$FC ; Switch to bank 3
178 lda #$48 ; Set color map to $D000, screen to $E000
181 lda $D011 ; And turn on bitmap
185 ; Done, reset the error code
191 ; ------------------------------------------------------------------------
192 ; DONE: Will be called to switch the graphics device back into text mode.
193 ; The graphics kernel will never call DONE when no graphics mode is active,
194 ; so there is no need to protect against that.
196 ; Must set an error code: NO
199 DONE: lda $DD02 ; Set the data direction regs
206 lda OLDD018 ; Screen mem --> $0400
214 ; ------------------------------------------------------------------------
215 ; GETERROR: Return the error code in A and clear it.
223 ; ------------------------------------------------------------------------
224 ; CONTROL: Platform/driver specific entry point.
226 ; Must set an error code: YES
230 lda #TGI_ERR_INV_FUNC
234 ; ------------------------------------------------------------------------
235 ; CLEAR: Clears the screen.
237 ; Must set an error code: NO
242 @L1: sta VBASE+$0000,y
278 ; ------------------------------------------------------------------------
279 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
280 ; The page number is already checked to be valid by the graphics kernel.
282 ; Must set an error code: NO (will only be called if page ok)
288 ; ------------------------------------------------------------------------
289 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
290 ; The page number is already checked to be valid by the graphics kernel.
292 ; Must set an error code: NO (will only be called if page ok)
298 ; ------------------------------------------------------------------------
299 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
300 ; to be in a valid range (0..maxcolor-1).
302 ; Must set an error code: NO (will only be called if color ok)
312 ; ------------------------------------------------------------------------
313 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
314 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
317 ; Must set an error code: YES
322 @L1: lda (ptr1),y ; Copy the palette
323 and #$0F ; Make a valid color
328 ; Get the color entries from the palette
330 lda PALETTE+1 ; Foreground color
335 ora PALETTE ; Background color
338 ; Initialize the color map with the new color settings (it is below the
343 lda $01 ; Get ROM config
345 and #%11111100 ; Clear bit 0 and 1
347 txa ; Load color code
348 @L2: sta CBASE+$0000,y
358 ; Done, reset the error code
364 ; ------------------------------------------------------------------------
365 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
366 ; set the palette should return the default palette here, so there's no
367 ; way for this function to fail.
369 ; Must set an error code: NO
377 ; ------------------------------------------------------------------------
378 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
379 ; drivers should return something reasonable here, even drivers that don't
380 ; support palettes, otherwise the caller has no way to determine the colors
381 ; of the (not changeable) palette.
383 ; Must set an error code: NO (all drivers must have a default palette)
391 ; ------------------------------------------------------------------------
392 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
393 ; color. The coordinates passed to this function are never outside the
394 ; visible screen area, so there is no need for clipping inside this function.
396 ; Must set an error code: NO
400 jsr CALC ; Calculate coordinates
402 sei ; Get underneath ROM
420 ; ------------------------------------------------------------------------
421 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
422 ; coordinates passed to this function are never outside the visible screen
423 ; area, so there is no need for clipping inside this function.
427 jsr CALC ; Calculate coordinates
429 sei ; Get underneath ROM
445 tya ; Get color value into A
446 ldx #$00 ; Clear high byte
449 ; ------------------------------------------------------------------------
450 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
451 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
453 ; To deal with off-screen coordinates, the current row
454 ; and column (40x25) is kept track of. These are set
455 ; negative when the point is off the screen, and made
456 ; positive when the point is within the visible screen.
458 ; X1,X2 etc. are set up above (x2=LINNUM in particular)
459 ; Format is LINE x2,y2,x1,y1
461 ; Must set an error code: NO
466 @CHECK: lda X2 ;Make sure x1<x2
473 lda Y2 ;If not, swap P1 and P2
501 bpl @DYPOS ;Is y2>=y1?
502 lda Y1 ;Otherwise dy=y1-y2
508 @DYPOS: sty DY ; 8-bit DY -- FIX ME?
512 jsr CALC ; Set up .X,.Y,POINT, and INRANGE
517 sei ; Get underneath ROM
522 cpx DX ;Who's bigger: dy or dx?
523 bcc STEPINX ;If dx, then...
530 ; To simplify my life, just use PLOT to plot points.
533 ; Added special plotting routine -- cool!
535 ; X is now counter, Y is y-coordinate
537 ; On entry, X=DY=number of loop iterations, and Y=
541 sta OLDCHUNK ;So plotting routine will work right
547 bne @CONT ;If dy=0 it's just a point
549 @CONT: lsr ;Init counter to dy/2
555 lda INRANGE ;Range check
558 lda (POINT),y ;Otherwise plot
565 iny ;Advance Y coordinate
567 bcc @CONT ;No prob if Y=0..7
569 @CONT: lda TEMP ;Restore A
573 YCONT: dex ;X is counter
575 YCONT2: lda (POINT),y ;Plot endpoint
588 bne YCONT ;If we pass a column boundary...
589 ror CHUNK ;then reset CHUNK to $80
592 bmi @C1 ;Skip if column is negative
593 cmp #39 ;End if move past end of screen
595 @C1: lda POINT ;And add 8 to POINT
600 @CONT: inc COL ;Increment column
605 lda #00 ;Passed into col 0
613 ; Big steps in X direction
615 ; On entry, X=DY=number of loop iterations, and Y=
620 .byte $00 ;Temporary counter
628 ror ;Need bit for initialization
629 sta Y1 ;High byte of counter
631 bne @CONT ;Could be $100
638 beq XFIXC ;If we pass a column boundary...
640 bcc XFIXY ;Time to step in Y?
643 dec COUNTHI ;High bits set?
646 XDONE: lsr CHUNK ;Advance to last point
647 jsr LINEPLOT ;Plot the last chunk
653 ; CHUNK has passed a column, so plot and increment pointer
654 ; and fix up CHUNK, OLDCHUNK.
662 bmi @C1 ;Skip if column is negative
663 cmp #39 ;End if move past end of screen
681 ; Check to make sure there isn't a high bit, plot chunk,
682 ; and update Y-coordinate.
684 XFIXY: dec Y1 ;Maybe high bit set
692 jsr LINEPLOT ;Plot chunk
707 ; Subroutine to plot chunks/points (to save a little
708 ; room, gray hair, etc.)
710 LINEPLOT: ; Plot the line chunk
714 lda (POINT),Y ; Otherwise plot
724 ; Subroutine to fix up pointer when Y decreases through
725 ; zero or increases through 7.
727 FIXY: cpy #255 ;Y=255 or Y=8
729 @INCPTR: ;Add 320 to pointer
730 ldy #0 ;Y increased through 7
732 bmi @C1 ;If negative, then don't update
734 bcs @TOAST ;If at bottom of screen then quit
747 @DECPTR: ;Okay, subtract 320 then
748 ldy #7 ;Y decreased through 0
767 @TOAST: pla ;Remove old return address
769 jmp EXIT ;Restore interrupts, etc.
771 ; ------------------------------------------------------------------------
772 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
773 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
774 ; Contrary to most other functions, the graphics kernel will sort and clip
775 ; the coordinates before calling the driver, so on entry the following
776 ; conditions are valid:
779 ; (X1 >= 0) && (X1 < XRES)
780 ; (X2 >= 0) && (X2 < XRES)
781 ; (Y1 >= 0) && (Y1 < YRES)
782 ; (Y2 >= 0) && (Y2 < YRES)
784 ; Must set an error code: NO
787 ; Note: This function needs optimization. It's just a cheap translation of
788 ; the original C wrapper and could be written much smaller (besides that,
789 ; calling LINE is not a good idea either).
847 ; ------------------------------------------------------------------------
848 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
849 ; direction is passend in X/Y, the text direction is passed in A.
851 ; Must set an error code: NO
861 ; ------------------------------------------------------------------------
862 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
863 ; current text style. The text to output is given as a zero terminated
864 ; string with address in ptr3.
866 ; Must set an error code: NO
872 ; ------------------------------------------------------------------------
873 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
874 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
875 ; the coordinates are valid, INRANGE is zero and the carry clear.
882 lsr ; Neg is possible
900 adc #>VBASE ; +bitmap base
917 adc POINT ; +(X AND #$F8)