2 ; Graphics driver for the 640x480x2 mode on the C128 VDC 64k
3 ; (values for this mode based on Fred Bowen's document)
4 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
6 ; 2004-04-04, Greg King
9 ; For any smart monkey that will try to optimize this: PLEASE do tests on
10 ; real VDC, not only VICE.
12 ; Only DONE routine contains C128-mode specific stuff, everything else will
13 ; work in C64-mode of C128 (C64 needs full VDC init then).
15 ; With special initialization and CALC we can get 320x200 double-pixel mode.
17 ; Color translation values for BROWN and GRAY3 are obviously wrong, they
18 ; could be replaced by equiv. of ORANGE and GRAY2 but this would give only
19 ; 14 of 16 colors available.
21 ; Register 25 ($19) is said to require different value for VDC v1, but I
22 ; couldn't find what it should be.
24 .include "zeropage.inc"
26 .include "tgi-kernel.inc"
27 .include "tgi-mode.inc"
28 .include "tgi-error.inc"
33 ; ------------------------------------------------------------------------
36 VDC_ADDR_REG = $D600 ; VDC address
37 VDC_DATA_REG = $D601 ; VDC data
39 VDC_DSP_HI = 12 ; registers used
50 ; ------------------------------------------------------------------------
51 ; Header. Includes jump table and constants.
55 ; First part of the header is a structure that has a magic and defines the
56 ; capabilities of the driver
58 .byte $74, $67, $69 ; "tgi"
59 .byte TGI_API_VERSION ; TGI version number
60 xres: .word 640 ; X resolution
61 yres: .word 480 ; Y resolution
62 .byte 2 ; Number of drawing colors
63 pages: .byte 0 ; Number of screens available
64 .byte 8 ; System font X size
65 .byte 8 ; System font Y size
66 .res 4, $00 ; Reserved for future extensions
68 ; Next comes the jump table. Currently all entries must be valid and may point
69 ; to an RTS for test versions (function not implemented).
91 .addr 0 ; IRQ entry is unused
93 ; ------------------------------------------------------------------------
96 ; Variables mapped to the zero page segment variables. Some of these are
97 ; used for passing parameters to the driver.
105 ADDR = tmp1 ; (2) CALC
106 TEMP = tmp3 ; CALC icmp
109 TEMP4 = sreg+1 ; LINE
111 ; Line routine stuff (must be on zpage)
114 ERR = regsave ; (2) LINE
115 NX = regsave+2 ; (2) LINE
117 XX = ptr3 ; (2) CIRCLE
118 YY = ptr4 ; (2) CIRCLE
119 MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway)
120 XS = regsave ; (2) CIRCLE
121 YS = regsave+2 ; (2) CIRCLE
123 ; Absolute variables used in the code
127 ERROR: .res 1 ; Error code
128 PALETTE: .res 2 ; The current palette
130 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
132 OLDCOLOR: .res 1 ; colors before entering gfx mode
134 ; Line routine stuff (combined with CIRCLE to save space)
151 ; Constants and tables
155 DEFPALETTE: .byte $00, $0f ; White on black
156 PALETTESIZE = * - DEFPALETTE
158 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
160 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
161 .byte %00001111, %00000111, %00000011, %00000001
163 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
164 .byte %11111000, %11111100, %11111110, %11111111
166 ; color translation table (indexed by VIC color)
167 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
168 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
169 ; colors BROWN and GRAY3 are wrong
171 ; VDC initialization table (reg),(val),...,$ff
173 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
175 .byte VDC_HSCROLL, $87
186 SCN80CLR: .byte 27,88,147,27,88,0
190 ; ------------------------------------------------------------------------
191 ; INSTALL routine. Is called after the driver is loaded into memory. May
192 ; initialize anything that has to be done just once. Is probably empty
195 ; Must set an error code: NO
199 ; check for VDC version and update register $19 value
201 ; check for VDC ram size and update number of available screens
203 ldx #VDC_CSET ; determine size of RAM...
207 jsr VDCWriteReg ; turn on 64k
209 jsr settestadr1 ; save original value of test byte
213 lda #$55 ; write $55 here
215 jsr test64k ; read it here and there
216 lda #$aa ; write $aa here
218 jsr test64k ; read it here and there
222 jsr VDCWriteByte ; restore original value of test byte
224 lda ptr1 ; do bytes match?
233 jsr VDCWriteReg ; restore 16/64k flag
234 jmp @endok ; and leave default values for 16k
249 jsr VDCWriteByte ; write $55
251 jsr VDCReadByte ; read here
254 jsr VDCReadByte ; and there
263 ldy #$02 ; test page 2 (here)
266 ldy #$42 ; or page 64+2 (there)
270 ; ------------------------------------------------------------------------
271 ; UNINSTALL routine. Is called before the driver is removed from memory. May
272 ; clean up anything done by INSTALL but is probably empty most of the time.
274 ; Must set an error code: NO
281 ; ------------------------------------------------------------------------
282 ; INIT: Changes an already installed device from text mode to graphics
284 ; Note that INIT/DONE may be called multiple times while the driver
285 ; is loaded, while INSTALL is only called once, so any code that is needed
286 ; to initializes variables and so on must go here. Setting palette and
287 ; clearing the screen is not needed because this is called by the graphics
289 ; The graphics kernel will never call INIT when a graphics mode is already
290 ; active, so there is no need to protect against that.
292 ; Must set an error code: YES
296 lda pages ; is there enough memory?
297 bne @L11 ; Jump if there is one screen
298 lda #TGI_ERR_INV_MODE ; Error
301 ; Initialize variables
306 ; Remeber current color value
311 ; Switch into graphics mode (set view page 0)
314 @L2: ldx InitVDCTab,y
323 ; Done, reset the error code
329 ; ------------------------------------------------------------------------
330 ; DONE: Will be called to switch the graphics device back into text mode.
331 ; The graphics kernel will never call DONE when no graphics mode is active,
332 ; so there is no need to protect against that.
334 ; Must set an error code: NO
338 ; This part is C128-mode specific
339 jsr $e179 ; reload character set and setup VDC
341 lda $d7 ; in 80-columns?
345 jsr $ffd2 ; print \xe,clr,\xe
349 jsr $ffd2 ; print clr
350 @L1: lda #0 ; restore view page
355 jsr VDCWriteReg ; restore color (background)
358 jmp VDCWriteReg ; switch to text screen
360 ; ------------------------------------------------------------------------
361 ; GETERROR: Return the error code in A and clear it.
369 ; ------------------------------------------------------------------------
370 ; CONTROL: Platform/driver specific entry point.
372 ; Must set an error code: YES
376 lda #TGI_ERR_INV_FUNC
380 ; ------------------------------------------------------------------------
381 ; CLEAR: Clears the screen.
383 ; Must set an error code: NO
392 jsr VDCWriteReg ; set fill mode
394 jsr VDCWriteByte ; put 1rst byte (fill value)
403 ; ------------------------------------------------------------------------
404 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
405 ; The page number is already checked to be valid by the graphics kernel.
407 ; Must set an error code: NO (will only be called if page ok)
413 ; ------------------------------------------------------------------------
414 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
415 ; The page number is already checked to be valid by the graphics kernel.
417 ; Must set an error code: NO (will only be called if page ok)
423 ; ------------------------------------------------------------------------
424 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
425 ; to be in a valid range (0..maxcolor-1).
427 ; Must set an error code: NO (will only be called if color ok)
437 ; ------------------------------------------------------------------------
438 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
439 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
442 ; Must set an error code: YES
447 @L1: lda (ptr1),y ; Copy the palette
448 and #$0F ; Make a valid color
453 ; Get the color entries from the palette
455 ldy PALETTE+1 ; Foreground color
461 ldy PALETTE ; Background color
466 lda #TGI_ERR_OK ; Clear error code
470 ; ------------------------------------------------------------------------
471 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
472 ; set the palette should return the default palette here, so there's no
473 ; way for this function to fail.
475 ; Must set an error code: NO
483 ; ------------------------------------------------------------------------
484 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
485 ; drivers should return something reasonable here, even drivers that don't
486 ; support palettes, otherwise the caller has no way to determine the colors
487 ; of the (not changeable) palette.
489 ; Must set an error code: NO (all drivers must have a default palette)
497 ; ------------------------------------------------------------------------
498 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
499 ; color. The coordinates passed to this function are never outside the
500 ; visible screen area, so there is no need for clipping inside this function.
502 ; Must set an error code: NO
517 jsr icmp ; ( x < xres ) ...
526 jsr icmp ; ... && ( y < yres )
531 jsr CALC ; Calculate coordinates
553 ; ------------------------------------------------------------------------
554 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
555 ; coordinates passed to this function are never outside the visible screen
556 ; area, so there is no need for clipping inside this function.
560 jsr CALC ; Calculate coordinates
562 stx TEMP ; preserve X
574 @L1: tya ; Get color value into A
575 ldx #$00 ; Clear high byte
578 ; ------------------------------------------------------------------------
579 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
580 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
582 ; Must set an error code: NO
674 ; for (count=nx;count>0;--count) {
679 @L0166: lda COUNT ; count>0
684 @L0167: jsr SETPIXELCLIP
724 ; if (abs(pb)<abs(ub)) {
740 ; } else { x1 = x1 + ax
779 ; ------------------------------------------------------------------------
780 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
781 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
782 ; Contrary to most other functions, the graphics kernel will sort and clip
783 ; the coordinates before calling the driver, so on entry the following
784 ; conditions are valid:
787 ; (X1 >= 0) && (X1 < XRES)
788 ; (X2 >= 0) && (X2 < XRES)
789 ; (Y1 >= 0) && (Y1 < YRES)
790 ; (Y2 >= 0) && (Y2 < YRES)
792 ; Must set an error code: NO
800 ; Original code for a horizontal line
807 jsr CALC ; get data for LEFT
808 lda BITMASKL,x ; remember left address and bitmask
819 jsr CALC ; get data for RIGHT
823 pla ; recall data for LEFT
826 sta X1 ; put left address into X1
829 cmp #%11111111 ; if left bit <> 0
831 sta TEMP2 ; do left byte only...
846 inc X1 ; ... and proceed
850 ; do right byte (if Y2=0 ++ADDR and skip)
852 cmp #%11111111 ; if right bit <> 7
854 inc ADDR ; right bit = 7 - the next one is the last
859 @L11: lda ADDR ; do right byte only...
874 @L2: ; do the fill in the middle
875 lda ADDR ; calculate offset in full bytes
878 beq @L3 ; if equal - there are no more bytes
881 lda X1 ; setup for the left side
884 lda BITMASK ; get color
885 jsr VDCWriteByte ; put 1st value
888 beq @L3 ; 1 byte already written
890 stx ADDR ; if there are more bytes - fill them...
893 jsr VDCWriteReg ; setup for fill
896 jsr VDCWriteReg ; ... fill them NOW!
903 ; End of horizontal line code
919 ; ------------------------------------------------------------------------
920 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
921 ; radius in tmp1 and the current drawing color.
923 ; Must set an error code: NO
929 jmp SETPIXELCLIP ; Plot as a point
947 stx YS+1 ; XS/YS to remember the center
956 @L12: ; plot points in 8 slices...
971 sta Y1+1 ; (stack)=ys+y, y1=(stack)
973 jsr SETPIXELCLIP ; plot(xs+x,ys+y)
981 sta Y1+1 ; y3 = y1 = ys-y
983 jsr SETPIXELCLIP ; plot(xs+x,ys-y)
995 jsr SETPIXELCLIP ; plot (xs-x,ys+y)
1000 jsr SETPIXELCLIP ; plot (xs-x,ys-y)
1008 sta X1+1 ; x1 = xs+y
1016 sta Y1+1 ; (stack)=ys+x, y1=(stack)
1018 jsr SETPIXELCLIP ; plot(xs+y,ys+x)
1026 sta Y1+1 ; y3 = y1 = ys-x
1028 jsr SETPIXELCLIP ; plot(xs+y,ys-x)
1032 sta Y1 ; y1 = ys+x(stack)
1040 jsr SETPIXELCLIP ; plot (xs-y,ys+x)
1045 jsr SETPIXELCLIP ; plot (xs-y,ys-x)
1095 @L0148: ; if (abs(ou)<abs(og))
1114 @L014E: ; mo = ou; }
1126 ; ------------------------------------------------------------------------
1127 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1128 ; direction is passend in X/Y, the text direction is passed in A.
1130 ; Must set an error code: NO
1140 ; ------------------------------------------------------------------------
1141 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1142 ; current text style. The text to output is given as a zero terminated
1143 ; string with address in ptr3.
1145 ; Must set an error code: NO
1151 ; ------------------------------------------------------------------------
1152 ; Calculate all variables to plot the pixel at X1/Y1.
1153 ;------------------------
1155 ;> ADDR - address of card
1156 ;> X - bit number (X1 & 7)
1176 sta ADDR+1 ; Y*4+Y=Y*5
1186 sta ADDR ; Y*5*16=Y*80
1199 lda ADDR+1 ; ADDR = Y*80+x/8
1207 beq @even ; even line - no offset
1214 sta ADDR+1 ; odd lines are 21360 bytes farther
1221 ; copies of some runtime routines
1239 ; compare a/y to zp,x
1240 sta TEMP ; TEMP/TEMP2 - arg2
1248 tya ; x/a - arg1 (a=high)
1279 @L0: bit VDC_ADDR_REG
1288 @L0: bit VDC_ADDR_REG