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>
8 ; For any smart monkey that will try to optimize this: PLEASE do tests on
9 ; real VDC, not only VICE.
11 ; Only DONE routine contains C128-mode specific stuff, everything else will
12 ; work in C64-mode of C128 (C64 needs full VDC init then).
14 ; With special initialization and CALC we can get 320x200 double-pixel mode.
16 ; Color translation values for BROWN and GRAY3 are obviously wrong, they
17 ; could be replaced by equiv. of ORANGE and GRAY2 but this would give only
18 ; 14 of 16 colors available.
20 ; Register 25 ($19) is said to require different value for VDC v1, but I
21 ; couldn't find what it should be.
23 .include "zeropage.inc"
25 .include "tgi-kernel.inc"
26 .include "tgi-mode.inc"
27 .include "tgi-error.inc"
32 ; ------------------------------------------------------------------------
35 VDC_ADDR_REG = $D600 ; VDC address
36 VDC_DATA_REG = $D601 ; VDC data
38 VDC_DSP_HI = 12 ; registers used
49 ; ------------------------------------------------------------------------
50 ; Header. Includes jump table and constants.
54 ; First part of the header is a structure that has a magic and defines the
55 ; capabilities of the driver
57 .byte $74, $67, $69 ; "tgi"
58 .byte TGI_API_VERSION ; TGI version number
59 xres: .word 640 ; X resolution
60 yres: .word 480 ; Y resolution
61 .byte 2 ; Number of drawing colors
62 pages: .byte 0 ; Number of screens available
63 .byte 8 ; System font X size
64 .byte 8 ; System font Y size
65 .res 4, $00 ; Reserved for future extensions
67 ; Next comes the jump table. Currently all entries must be valid and may point
68 ; to an RTS for test versions (function not implemented).
91 ; ------------------------------------------------------------------------
94 ; Variables mapped to the zero page segment variables. Some of these are
95 ; used for passing parameters to the driver.
103 ADDR = tmp1 ; (2) CALC
104 TEMP = tmp3 ; CALC icmp
107 TEMP4 = sreg+1 ; LINE
109 ; Line routine stuff (must be on zpage)
112 ERR = regsave ; (2) LINE
113 NX = regsave+2 ; (2) LINE
115 XX = ptr3 ; (2) CIRCLE
116 YY = ptr4 ; (2) CIRCLE
117 MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway)
118 XS = regsave ; (2) CIRCLE
119 YS = regsave+2 ; (2) CIRCLE
121 ; Absolute variables used in the code
125 ERROR: .res 1 ; Error code
126 PALETTE: .res 2 ; The current palette
128 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
130 OLDCOLOR: .res 1 ; colors before entering gfx mode
132 ; Line routine stuff (combined with CIRCLE to save space)
148 ; Constants and tables
152 DEFPALETTE: .byte $00, $0f ; White on black
153 PALETTESIZE = * - DEFPALETTE
155 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
157 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
158 .byte %00001111, %00000111, %00000011, %00000001
160 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
161 .byte %11111000, %11111100, %11111110, %11111111
163 ; color translation table (indexed by VIC color)
164 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
165 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
166 ; colors BROWN and GRAY3 are wrong
168 ; VDC initialization table (reg),(val),...,$ff
170 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
172 .byte VDC_HSCROLL, $87
183 SCN80CLR: .byte 27,88,147,27,88,0
187 ; ------------------------------------------------------------------------
188 ; INSTALL routine. Is called after the driver is loaded into memory. May
189 ; initialize anything that has to be done just once. Is probably empty
192 ; Must set an error code: NO
196 ; check for VDC version and update register $19 value
198 ; check for VDC ram size and update number of available screens
200 ldx #VDC_CSET ; determine size of RAM...
204 jsr VDCWriteReg ; turn on 64k
206 jsr settestadr1 ; save original value of test byte
210 lda #$55 ; write $55 here
212 jsr test64k ; read it here and there
213 lda #$aa ; write $aa here
215 jsr test64k ; read it here and there
219 jsr VDCWriteByte ; restore original value of test byte
221 lda ptr1 ; do bytes match?
230 jsr VDCWriteReg ; restore 16/64k flag
231 jmp @endok ; and leave default values for 16k
246 jsr VDCWriteByte ; write $55
248 jsr VDCReadByte ; read here
251 jsr VDCReadByte ; and there
260 ldy #$02 ; test page 2 (here)
263 ldy #$42 ; or page 64+2 (there)
267 ; ------------------------------------------------------------------------
268 ; UNINSTALL routine. Is called before the driver is removed from memory. May
269 ; clean up anything done by INSTALL but is probably empty most of the time.
271 ; Must set an error code: NO
278 ; ------------------------------------------------------------------------
279 ; INIT: Changes an already installed device from text mode to graphics
281 ; Note that INIT/DONE may be called multiple times while the driver
282 ; is loaded, while INSTALL is only called once, so any code that is needed
283 ; to initializes variables and so on must go here. Setting palette and
284 ; clearing the screen is not needed because this is called by the graphics
286 ; The graphics kernel will never call INIT when a graphics mode is already
287 ; active, so there is no need to protect against that.
289 ; Must set an error code: YES
293 lda pages ; is there enough memory?
294 bne @L11 ; Jump if there is one screen
295 lda #TGI_ERR_INV_MODE ; ## Error
298 ; Initialize variables
303 ; Remeber current color value
308 ; Switch into graphics mode (set view page 0)
311 @L2: ldx InitVDCTab,y
320 ; Done, reset the error code
326 ; ------------------------------------------------------------------------
327 ; DONE: Will be called to switch the graphics device back into text mode.
328 ; The graphics kernel will never call DONE when no graphics mode is active,
329 ; so there is no need to protect against that.
331 ; Must set an error code: NO
335 ; This part is C128-mode specific
336 jsr $e179 ; reload character set and setup VDC
338 lda $d7 ; in 80-columns?
342 jsr $ffd2 ; print \xe,clr,\xe
346 jsr $ffd2 ; print clr
347 @L1: lda #0 ; restore view page
352 jsr VDCWriteReg ; restore color (background)
355 jmp VDCWriteReg ; switch to text screen
357 ; ------------------------------------------------------------------------
358 ; GETERROR: Return the error code in A and clear it.
366 ; ------------------------------------------------------------------------
367 ; CONTROL: Platform/driver specific entry point.
369 ; Must set an error code: YES
373 lda #TGI_ERR_INV_FUNC
377 ; ------------------------------------------------------------------------
378 ; CLEAR: Clears the screen.
380 ; Must set an error code: NO
389 jsr VDCWriteReg ; set fill mode
391 jsr VDCWriteByte ; put 1rst byte (fill value)
400 ; ------------------------------------------------------------------------
401 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
402 ; The page number is already checked to be valid by the graphics kernel.
404 ; Must set an error code: NO (will only be called if page ok)
410 ; ------------------------------------------------------------------------
411 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
412 ; The page number is already checked to be valid by the graphics kernel.
414 ; Must set an error code: NO (will only be called if page ok)
420 ; ------------------------------------------------------------------------
421 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
422 ; to be in a valid range (0..maxcolor-1).
424 ; Must set an error code: NO (will only be called if color ok)
434 ; ------------------------------------------------------------------------
435 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
436 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
439 ; Must set an error code: YES
444 @L1: lda (ptr1),y ; Copy the palette
445 and #$0F ; Make a valid color
450 ; Get the color entries from the palette
452 ldy PALETTE+1 ; Foreground color
458 ldy PALETTE ; Background color
463 lda #TGI_ERR_OK ; Clear error code
467 ; ------------------------------------------------------------------------
468 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
469 ; set the palette should return the default palette here, so there's no
470 ; way for this function to fail.
472 ; Must set an error code: NO
480 ; ------------------------------------------------------------------------
481 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
482 ; drivers should return something reasonable here, even drivers that don't
483 ; support palettes, otherwise the caller has no way to determine the colors
484 ; of the (not changeable) palette.
486 ; Must set an error code: NO (all drivers must have a default palette)
494 ; ------------------------------------------------------------------------
495 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
496 ; color. The coordinates passed to this function are never outside the
497 ; visible screen area, so there is no need for clipping inside this function.
499 ; Must set an error code: NO
514 jsr icmp ; if (xres<x1)
515 bcs @cont ; !(xres<x1)
524 jsr icmp ; if (yres<y1)
528 jsr CALC ; Calculate coordinates
550 ; ------------------------------------------------------------------------
551 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
552 ; coordinates passed to this function are never outside the visible screen
553 ; area, so there is no need for clipping inside this function.
557 jsr CALC ; Calculate coordinates
559 stx TEMP ; preserve X
571 @L1: tya ; Get color value into A
572 ldx #$00 ; Clear high byte
575 ; ------------------------------------------------------------------------
576 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
577 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
579 ; Must set an error code: NO
669 ; for (count=nx;count>0;--count) {
674 @L0166: lda COUNT ; count>0
679 @L0167: jsr SETPIXELCLIP
719 ; if (abs(pb)<abs(ub)) {
735 ; } else { x1 = x1 + ay
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
795 ; Original code for a horizontal line
802 jsr CALC ; get data for LEFT
803 lda BITMASKL,x ; remember left address and bitmask
814 jsr CALC ; get data for RIGHT
818 pla ; recall data for LEFT
821 sta X1 ; put left address into X1
824 cmp #%11111111 ; if left bit <> 0
826 sta TEMP2 ; do left byte only...
841 inc X1 ; ... and proceed
845 ; do right byte (if Y2=0 ++ADDR and skip)
847 cmp #%11111111 ; if right bit <> 7
849 inc ADDR ; right bit = 7 - the next one is the last
854 @L11: lda ADDR ; do right byte only...
869 @L2: ; do the fill in the middle
870 lda ADDR ; calculate offset in full bytes
873 beq @L3 ; if equal - there are no more bytes
876 lda X1 ; setup for the left side
879 lda BITMASK ; get color
880 jsr VDCWriteByte ; put 1st value
883 beq @L3 ; 1 byte already written
885 stx ADDR ; if there are more bytes - fill them...
888 jsr VDCWriteReg ; setup for fill
891 jsr VDCWriteReg ; ... fill them NOW!
898 ; End of horizontal line code
914 ; ------------------------------------------------------------------------
915 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
916 ; radius in tmp1 and the current drawing color.
918 ; Must set an error code: NO
924 jmp SETPIXELCLIP ; Plot as a point
942 stx YS+1 ; XS/YS to remember the center
951 @L12: ; plot points in 8 slices...
966 sta Y1+1 ; (stack)=ys+y, y1=(stack)
968 jsr SETPIXELCLIP ; plot(xs+x,ys+y)
976 sta Y1+1 ; y3 = y1 = ys-y
978 jsr SETPIXELCLIP ; plot(xs+x,ys-y)
990 jsr SETPIXELCLIP ; plot (xs-x,ys+y)
995 jsr SETPIXELCLIP ; plot (xs-x,ys-y)
1003 sta X1+1 ; x1 = xs+y
1011 sta Y1+1 ; (stack)=ys+x, y1=(stack)
1013 jsr SETPIXELCLIP ; plot(xs+y,ys+x)
1021 sta Y1+1 ; y3 = y1 = ys-x
1023 jsr SETPIXELCLIP ; plot(xs+y,ys-x)
1027 sta Y1 ; y1 = ys+x(stack)
1035 jsr SETPIXELCLIP ; plot (xs-y,ys+x)
1040 jsr SETPIXELCLIP ; plot (xs-y,ys-x)
1090 @L0148: ; if (abs(ou)<abs(og))
1109 @L014E: ; mo = ou; }
1121 ; ------------------------------------------------------------------------
1122 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1123 ; direction is passend in X/Y, the text direction is passed in A.
1125 ; Must set an error code: NO
1135 ; ------------------------------------------------------------------------
1136 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1137 ; current text style. The text to output is given as a zero terminated
1138 ; string with address in ptr3.
1140 ; Must set an error code: NO
1146 ; ------------------------------------------------------------------------
1147 ; Calculate all variables to plot the pixel at X1/Y1.
1148 ;------------------------
1150 ;> ADDR - address of card
1151 ;> X - bit number (X1 & 7)
1171 sta ADDR+1 ; Y*4+Y=Y*5
1181 sta ADDR ; Y*5*16=Y*80
1194 lda ADDR+1 ; ADDR = Y*80+x/8
1202 beq @even ; even line - no offset
1209 sta ADDR+1 ; odd lines are 21360 bytes farther
1216 ; copies of some runtime routines
1236 ; compare a/y to zp,x
1237 sta TEMP ; TEMP/TEMP2 - arg2
1245 tya ; x/a - arg1 (a=high)
1276 @L0: bit VDC_ADDR_REG
1285 @L0: bit VDC_ADDR_REG