2 ; Graphics driver for the 640x200x2 mode on the C128 VDC
3 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
7 ; For any smart monkey that will try to optimize this: PLEASE do tests on
8 ; real VDC, not only VICE.
10 ; Only DONE routine contains C128-mode specific stuff, everything else will
11 ; work in C64-mode of C128 (C64 needs full VDC init then).
13 ; With special initialization and CALC we can get 320x200 double-pixel mode.
15 ; Color translation values for BROWN and GRAY3 are obviously wrong, they
16 ; could be replaced by equiv. of ORANGE and GRAY2 but this would give only
17 ; 14 of 16 colors available.
19 ; Register 25 ($19) is said to require different value for VDC v1, but I
20 ; couldn't find what it should be.
22 .include "zeropage.inc"
24 .include "tgi-kernel.inc"
25 .include "tgi-mode.inc"
26 .include "tgi-error.inc"
31 ; ------------------------------------------------------------------------
34 VDC_ADDR_REG = $D600 ; VDC address
35 VDC_DATA_REG = $D601 ; VDC data
37 VDC_DSP_HI = 12 ; registers used
48 ; ------------------------------------------------------------------------
49 ; Header. Includes jump table and constants.
53 ; First part of the header is a structure that has a magic and defines the
54 ; capabilities of the driver
56 .byte $74, $67, $69 ; "tgi"
57 .byte TGI_API_VERSION ; TGI API version number
58 xres: .word 640 ; X resolution
59 yres: .word 200 ; Y resolution
60 .byte 2 ; Number of drawing colors
61 pages: .byte 1 ; Number of screens available
62 .byte 8 ; System font X size
63 .byte 8 ; System font Y size
64 .res 4, $00 ; Reserved for future extensions
66 ; Next comes the jump table. Currently all entries must be valid and may point
67 ; to an RTS for test versions (function not implemented).
90 ; ------------------------------------------------------------------------
93 ; Variables mapped to the zero page segment variables. Some of these are
94 ; used for passing parameters to the driver.
102 ADDR = tmp1 ; (2) CALC
103 TEMP = tmp3 ; CALC icmp
106 TEMP4 = sreg+1 ; LINE
108 ; Line routine stuff (must be on zpage)
111 ERR = regsave ; (2) LINE
112 NX = regsave+2 ; (2) LINE
114 XX = ptr3 ; (2) CIRCLE
115 YY = ptr4 ; (2) CIRCLE
116 MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway)
117 XS = regsave ; (2) CIRCLE
118 YS = regsave+2 ; (2) CIRCLE
120 ; Absolute variables used in the code
124 SCRBASE: .res 1 ; High byte of screen base
126 ERROR: .res 1 ; Error code
127 PALETTE: .res 2 ; The current palette
129 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
131 OLDCOLOR: .res 1 ; colors before entering gfx mode
133 ; Line routine stuff (combined with CIRCLE to save space)
149 ; Constants and tables
153 DEFPALETTE: .byte $00, $0f ; White on black
154 PALETTESIZE = * - DEFPALETTE
156 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
158 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
159 .byte %00001111, %00000111, %00000011, %00000001
161 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
162 .byte %11111000, %11111100, %11111110, %11111111
164 ; color translation table (indexed by VIC color)
165 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
166 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
167 ; colors BROWN and GRAY3 are wrong
169 ; VDC initialization table (reg),(val),...,$ff
171 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
173 .byte VDC_HSCROLL, $87
176 SCN80CLR: .byte 27,88,147,27,88,0
180 ; ------------------------------------------------------------------------
181 ; INSTALL routine. Is called after the driver is loaded into memory. May
182 ; initialize anything that has to be done just once. Is probably empty
185 ; Must set an error code: NO
189 ; check for VDC version and update register $19 value
191 ; check for VDC ram size and update number of available screens
193 ldx #VDC_CSET ; determine size of RAM...
197 jsr VDCWriteReg ; turn on 64k
199 jsr settestadr1 ; save original value of test byte
203 lda #$55 ; write $55 here
205 jsr test64k ; read it here and there
206 lda #$aa ; write $aa here
208 jsr test64k ; read it here and there
212 jsr VDCWriteByte ; restore original value of test byte
214 lda ptr1 ; do bytes match?
223 jsr VDCWriteReg ; restore 16/64k flag
224 jmp @endok ; and leave default values for 16k
231 sta SCRBASE ; draw page 0 as default
241 jsr VDCWriteByte ; write $55
243 jsr VDCReadByte ; read here
246 jsr VDCReadByte ; and there
255 ldy #$02 ; test page 2 (here)
258 ldy #$42 ; or page 64+2 (there)
262 ; ------------------------------------------------------------------------
263 ; UNINSTALL routine. Is called before the driver is removed from memory. May
264 ; clean up anything done by INSTALL but is probably empty most of the time.
266 ; Must set an error code: NO
273 ; ------------------------------------------------------------------------
274 ; INIT: Changes an already installed device from text mode to graphics
276 ; Note that INIT/DONE may be called multiple times while the driver
277 ; is loaded, while INSTALL is only called once, so any code that is needed
278 ; to initializes variables and so on must go here. Setting palette and
279 ; clearing the screen is not needed because this is called by the graphics
281 ; The graphics kernel will never call INIT when a graphics mode is already
282 ; active, so there is no need to protect against that.
284 ; Must set an error code: YES
289 ; Initialize variables
294 ; Remeber current color value
299 ; Switch into graphics mode (set view page 0)
302 @L2: ldx InitVDCTab,y
311 ; Done, reset the error code
317 ; ------------------------------------------------------------------------
318 ; DONE: Will be called to switch the graphics device back into text mode.
319 ; The graphics kernel will never call DONE when no graphics mode is active,
320 ; so there is no need to protect against that.
322 ; Must set an error code: NO
326 ; This part is C128-mode specific
327 jsr $e179 ; reload character set and setup VDC
329 lda $d7 ; in 80-columns?
333 jsr $ffd2 ; print \xe,clr,\xe
337 jsr $ffd2 ; print clr
338 @L1: lda #0 ; restore view page
343 jsr VDCWriteReg ; restore color (background)
346 jmp VDCWriteReg ; switch to text screen
348 ; ------------------------------------------------------------------------
349 ; GETERROR: Return the error code in A and clear it.
357 ; ------------------------------------------------------------------------
358 ; CONTROL: Platform/driver specific entry point.
360 ; Must set an error code: YES
364 lda #TGI_ERR_INV_FUNC
368 ; ------------------------------------------------------------------------
369 ; CLEAR: Clears the screen.
371 ; Must set an error code: NO
380 jsr VDCWriteReg ; set fill mode
382 jsr VDCWriteByte ; put 1rst byte (fill value)
390 jmp VDCWriteReg ; 1+62*256+127=16000=(640*256)/8
392 ; ------------------------------------------------------------------------
393 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
394 ; The page number is already checked to be valid by the graphics kernel.
396 ; Must set an error code: NO (will only be called if page ok)
407 ; ------------------------------------------------------------------------
408 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
409 ; The page number is already checked to be valid by the graphics kernel.
411 ; Must set an error code: NO (will only be called if page ok)
422 ; ------------------------------------------------------------------------
423 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
424 ; to be in a valid range (0..maxcolor-1).
426 ; Must set an error code: NO (will only be called if color ok)
436 ; ------------------------------------------------------------------------
437 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
438 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
441 ; Must set an error code: YES
446 @L1: lda (ptr1),y ; Copy the palette
447 and #$0F ; Make a valid color
452 ; Get the color entries from the palette
454 ldy PALETTE+1 ; Foreground color
460 ldy PALETTE ; Background color
464 jsr VDCWriteReg ; Clear error code
469 ; ------------------------------------------------------------------------
470 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
471 ; set the palette should return the default palette here, so there's no
472 ; way for this function to fail.
474 ; Must set an error code: NO
482 ; ------------------------------------------------------------------------
483 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
484 ; drivers should return something reasonable here, even drivers that don't
485 ; support palettes, otherwise the caller has no way to determine the colors
486 ; of the (not changeable) palette.
488 ; Must set an error code: NO (all drivers must have a default palette)
496 ; ------------------------------------------------------------------------
497 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
498 ; color. The coordinates passed to this function are never outside the
499 ; visible screen area, so there is no need for clipping inside this function.
501 ; Must set an error code: NO
516 jsr icmp ; if (xres<x1)
517 bcs @cont ; !(xres<x1)
526 jsr icmp ; if (yres<y1)
530 jsr CALC ; Calculate coordinates
552 ; ------------------------------------------------------------------------
553 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
554 ; coordinates passed to this function are never outside the visible screen
555 ; area, so there is no need for clipping inside this function.
559 jsr CALC ; Calculate coordinates
561 stx TEMP ; preserve X
573 @L1: tya ; Get color value into A
574 ldx #$00 ; Clear high byte
577 ; ------------------------------------------------------------------------
578 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
579 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
581 ; Must set an error code: NO
671 ; for (count=nx;count>0;--count) {
676 @L0166: lda COUNT ; count>0
681 @L0167: jsr SETPIXELCLIP
721 ; if (abs(pb)<abs(ub)) {
737 ; } else { x1 = x1 + ay
776 ; ------------------------------------------------------------------------
777 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
778 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
779 ; Contrary to most other functions, the graphics kernel will sort and clip
780 ; the coordinates before calling the driver, so on entry the following
781 ; conditions are valid:
784 ; (X1 >= 0) && (X1 < XRES)
785 ; (X2 >= 0) && (X2 < XRES)
786 ; (Y1 >= 0) && (Y1 < YRES)
787 ; (Y2 >= 0) && (Y2 < YRES)
789 ; Must set an error code: NO
797 ; Original code for a horizontal line
804 jsr CALC ; get data for LEFT
805 lda BITMASKL,x ; remember left address and bitmask
816 jsr CALC ; get data for RIGHT
820 pla ; recall data for LEFT
823 sta X1 ; put left address into X1
826 cmp #%11111111 ; if left bit <> 0
828 sta TEMP2 ; do left byte only...
843 inc X1 ; ... and proceed
847 ; do right byte (if Y2=0 ++ADDR and skip)
849 cmp #%11111111 ; if right bit <> 7
851 inc ADDR ; right bit = 7 - the next one is the last
856 @L11: lda ADDR ; do right byte only...
871 @L2: ; do the fill in the middle
872 lda ADDR ; calculate offset in full bytes
875 beq @L3 ; if equal - there are no more bytes
878 lda X1 ; setup for the left side
881 lda BITMASK ; get color
882 jsr VDCWriteByte ; put 1st value
885 beq @L3 ; 1 byte already written
887 stx ADDR ; if there are more bytes - fill them...
890 jsr VDCWriteReg ; setup for fill
893 jsr VDCWriteReg ; ... fill them NOW!
900 ; End of horizontal line code
915 ; ------------------------------------------------------------------------
916 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
917 ; radius in tmp1 and the current drawing color.
919 ; Must set an error code: NO
925 jmp SETPIXELCLIP ; Plot as a point
943 stx YS+1 ; XS/YS to remember the center
952 @L12: ; plot points in 8 slices...
967 sta Y1+1 ; (stack)=ys+y, y1=(stack)
969 jsr SETPIXELCLIP ; plot(xs+x,ys+y)
977 sta Y1+1 ; y3 = y1 = ys-y
979 jsr SETPIXELCLIP ; plot(xs+x,ys-y)
991 jsr SETPIXELCLIP ; plot (xs-x,ys+y)
996 jsr SETPIXELCLIP ; plot (xs-x,ys-y)
1004 sta X1+1 ; x1 = xs+y
1012 sta Y1+1 ; (stack)=ys+x, y1=(stack)
1014 jsr SETPIXELCLIP ; plot(xs+y,ys+x)
1022 sta Y1+1 ; y3 = y1 = ys-x
1024 jsr SETPIXELCLIP ; plot(xs+y,ys-x)
1028 sta Y1 ; y1 = ys+x(stack)
1036 jsr SETPIXELCLIP ; plot (xs-y,ys+x)
1041 jsr SETPIXELCLIP ; plot (xs-y,ys-x)
1091 @L0148: ; if (abs(ou)<abs(og))
1110 @L014E: ; mo = ou; }
1122 ; ------------------------------------------------------------------------
1123 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1124 ; direction is passend in X/Y, the text direction is passed in A.
1126 ; Must set an error code: NO
1136 ; ------------------------------------------------------------------------
1137 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1138 ; current text style. The text to output is given as a zero terminated
1139 ; string with address in ptr3.
1141 ; Must set an error code: NO
1147 ; ------------------------------------------------------------------------
1148 ; Calculate all variables to plot the pixel at X1/Y1.
1149 ;------------------------
1151 ;> ADDR - address of card
1152 ;> X - bit number (X1 & 7)
1166 sta ADDR+1 ; Y*4+Y=Y*5
1176 sta ADDR ; Y*5*16=Y*80
1189 lda ADDR+1 ; ADDR = Y*80+x/8
1201 ; copies of some runtime routines
1221 ; compare a/y to zp,x
1222 sta TEMP ; TEMP/TEMP2 - arg2
1230 tya ; x/a - arg1 (a=high)
1261 @L0: bit VDC_ADDR_REG
1270 @L0: bit VDC_ADDR_REG