2 ; Graphics driver for the 640x200x2 mode on the C128 VDC
3 ; Maciej 'YTM/Elysium' Witkowiak <ytm@friko.onet.pl>
8 ; Only DONE routine contains C128-mode specific stuff, everything else will work in
11 ; All Y-axis calculations are 16-bit, so this driver will support 640x400 (interlaced)
12 ; mode only with gfx-mode initialization changed.
13 ; (is the above true? I can't remember if interlace skips lines or takes them from two
14 ; different places (same offset, different base))
16 ; X-axis resolution can be changed by manipulating xres header field so
17 ; any resolution 8..708 is possible only with gfx-mode initialization and CALC changed.
19 ; With special initialization and CALC we can get 320x200 double-pixel mode.
21 ; Color translation values for BROWN and GRAY3 are obviously wrong, they could
22 ; be replaced by equiv. of ORANGE and GRAY2 but this would give only 14 of 16 colors available.
24 ; Register 25 ($19) is said to require different value for VDC v1, but I couldn't find what
27 .include "zeropage.inc"
29 .include "tgi-kernel.inc"
30 .include "tgi-mode.inc"
31 .include "tgi-error.inc"
36 ; ------------------------------------------------------------------------
39 VDC_ADDR_REG = $D600 ; VDC address
40 VDC_DATA_REG = $D601 ; VDC data
42 VDC_DSP_HI = 12 ; registers used
53 ; ------------------------------------------------------------------------
54 ; Header. Includes jump table and constants.
58 ; First part of the header is a structure that has a magic and defines the
59 ; capabilities of the driver
61 .byte $74, $67, $69 ; "tgi"
62 .byte $00 ; TGI version number
63 xres: .word 640 ; X resolution
64 yres: .word 200 ; Y resolution
65 .byte 2 ; Number of drawing colors
66 pages: .byte 1 ; Number of screens available
67 .byte 8 ; System font X size
68 .byte 8 ; System font Y size
69 .res 4, $00 ; Reserved for future extensions
71 ; Next comes the jump table. Currently all entries must be valid and may point
72 ; to an RTS for test versions (function not implemented). A future version may
73 ; allow for emulation: In this case the vector will be zero. Emulation means
74 ; that the graphics kernel will emulate the function by using lower level
75 ; primitives - for example ploting a line by using calls to SETPIXEL.
99 ; ------------------------------------------------------------------------
102 ; Variables mapped to the zero page segment variables. Some of these are
103 ; used for passing parameters to the driver.
111 ADDR = tmp1 ; (2) CALC
112 TEMP = tmp3 ; CALC icmp
115 TEMP4 = sreg+1 ; LINE
117 ; Line routine stuff (must be on zpage)
120 ERR = regsave ; (2) LINE
121 NX = regsave+2 ; (2) LINE
123 XX = ptr3 ; (2) CIRCLE
124 YY = ptr4 ; (2) CIRCLE
125 MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway)
126 XS = regsave ; (2) CIRCLE
127 YS = regsave+2 ; (2) CIRCLE
129 ; Absolute variables used in the code
133 SCRBASE: .res 1 ; High byte of screen base
135 ERROR: .res 1 ; Error code
136 PALETTE: .res 2 ; The current palette
138 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
140 OLDCOLOR: .res 1 ; colors before entering gfx mode
142 ; Line routine stuff (combined with CIRCLE to save space)
158 ; Constants and tables
162 DEFPALETTE: .byte $00, $0f ; White on black
163 PALETTESIZE = * - DEFPALETTE
165 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
167 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
168 .byte %00001111, %00000111, %00000011, %00000001
170 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
171 .byte %11111000, %11111100, %11111110, %11111111
173 ; color translation table (indexed by VIC color)
174 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
175 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
176 ; colors BROWN and GRAY3 are wrong
178 ; VDC initialization table (reg),(val),...,$ff
180 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
182 .byte VDC_HSCROLL, $87
185 SCN80CLR: .byte 27,88,147,27,88,0
189 ; ------------------------------------------------------------------------
190 ; INSTALL routine. Is called after the driver is loaded into memory. May
191 ; initialize anything that has to be done just once. Is probably empty
194 ; Must set an error code: NO
198 ; check for VDC version and update register $19 value
200 ; check for VDC ram size and update number of available screens
202 ldx #VDC_CSET ; determine size of RAM...
206 jsr VDCWriteReg ; turn on 64k
208 jsr settestadr1 ; save original value of test byte
212 lda #$55 ; write $55 here
214 jsr test64k ; read it here and there
215 lda #$aa ; write $aa here
217 jsr test64k ; read it here and there
221 jsr VDCWriteByte ; restore original value of test byte
223 lda ptr1 ; do bytes match?
232 jsr VDCWriteReg ; restore 16/64k flag
233 jmp @endok ; and leave default values for 16k
240 sta SCRBASE ; draw page 0 as default
250 jsr VDCWriteByte ; write $55
252 jsr VDCReadByte ; read here
255 jsr VDCReadByte ; and there
264 ldy #$02 ; test page 2 (here)
267 ldy #$42 ; or page 64+2 (there)
271 ; ------------------------------------------------------------------------
272 ; DEINSTALL routine. Is called before the driver is removed from memory. May
273 ; clean up anything done by INSTALL but is probably empty most of the time.
275 ; Must set an error code: NO
282 ; ------------------------------------------------------------------------
283 ; INIT: Changes an already installed device from text mode to graphics
284 ; mode. The number of the graphics mode is passed to the function in A.
285 ; Note that INIT/DONE may be called multiple times while the driver
286 ; is loaded, while INSTALL is only called once, so any code that is needed
287 ; to initializes variables and so on must go here. Setting palette and
288 ; clearing the screen is not needed because this is called by the graphics
290 ; The graphics kernel will never call INIT when a graphics mode is already
291 ; active, so there is no need to protect against that.
293 ; Must set an error code: YES
296 INIT: cmp #TGI_MODE_640_200_2 ; Correct mode?
297 beq @L1 ; Jump if yes
298 lda #TGI_ERR_INV_MODE ; ## Error
301 ; Initialize variables
306 ; Switch into graphics mode (set view page 0)
309 @L2: ldx InitVDCTab,y
318 ; Remeber current color value
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: YES
338 ; This part is C128-mode specific
339 jsr $ce0c ; reload character set
340 lda $d7 ; in 80-columns?
344 jsr $ffd2 ; print \xe,clr,\xe
348 jsr $ffd2 ; print clr
349 @L1: lda #0 ; restore view page
354 jsr VDCWriteReg ; restore color (background)
357 jmp VDCWriteReg ; switch to text screen
359 ; ------------------------------------------------------------------------
360 ; GETERROR: Return the error code in A and clear it.
368 ; ------------------------------------------------------------------------
369 ; CONTROL: Platform/driver specific entry point.
371 ; Must set an error code: YES
375 lda #TGI_ERR_INV_FUNC
379 ; ------------------------------------------------------------------------
380 ; CLEAR: Clears the screen.
382 ; Must set an error code: NO
391 jsr VDCWriteReg ; set fill mode
393 jsr VDCWriteByte ; put 1rst byte (fill value)
401 jmp VDCWriteReg ; 1+62*256+127=16000=(640*256)/8
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)
418 ; ------------------------------------------------------------------------
419 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
420 ; The page number is already checked to be valid by the graphics kernel.
422 ; Must set an error code: NO (will only be called if page ok)
433 ; ------------------------------------------------------------------------
434 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
435 ; to be in a valid range (0..maxcolor-1).
437 ; Must set an error code: NO (will only be called if color ok)
447 ; ------------------------------------------------------------------------
448 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
449 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
452 ; Must set an error code: YES
457 @L1: lda (ptr1),y ; Copy the palette
458 and #$0F ; Make a valid color
463 ; Get the color entries from the palette
465 ldy PALETTE+1 ; Foreground color
471 ldy PALETTE ; Background color
477 ; ------------------------------------------------------------------------
478 ; GETPALETTE: Return the current palette in A/X. Must return NULL and set an
479 ; error if palettes are not supported.
481 ; Must set an error code: YES
489 ; ------------------------------------------------------------------------
490 ; GETDEFPALETTE: Return the default palette for the driver in A/X. Must
491 ; return NULL and set an error of palettes are not supported.
493 ; Must set an error code: YES
501 ; ------------------------------------------------------------------------
502 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
503 ; color. The coordinates passed to this function are never outside the
504 ; visible screen area, so there is no need for clipping inside this function.
506 ; Must set an error code: NO
521 jsr icmp ; if (xres<x1)
522 bcs @cont ; !(xres<x1)
531 jsr icmp ; if (yres<y1)
535 jsr CALC ; Calculate coordinates
557 ; ------------------------------------------------------------------------
558 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
559 ; coordinates passed to this function are never outside the visible screen
560 ; area, so there is no need for clipping inside this function.
564 jsr CALC ; Calculate coordinates
566 stx TEMP ; preserve X
578 @L1: tya ; Get color value into A
579 ldx #$00 ; Clear high byte
582 ; ------------------------------------------------------------------------
583 ; HORLINE: Draw a horizontal line from X1/Y to X2/Y, where X1 = ptr1,
584 ; Y = ptr2 and X2 = ptr3, using the current drawing color.
586 ; This is a special line drawing entry used when the line is know to be
587 ; horizontal, for example by the BAR emulation routine. If the driver does
588 ; not have special code for horizontal lines, it may just copy Y to Y2 and
589 ; proceed with the generic line drawing code.
591 ; Note: Line coordinates will always be sorted (Y1 <= X2) and clipped.
593 ; Must set an error code: NO
601 jsr CALC ; get data for LEFT
602 lda BITMASKL,x ; remember left address and bitmask
613 jsr CALC ; get data for RIGHT
617 pla ; recall data for LEFT
620 sta X1 ; put left address into X1
623 cmp #%11111111 ; if left bit <> 0
625 sta TEMP2 ; do left byte only...
640 inc X1 ; ... and proceed
644 ; do right byte (if Y2=0 ++ADDR and skip)
646 cmp #%11111111 ; if right bit <> 7
648 inc ADDR ; right bit = 7 - the next one is the last
653 @L11: lda ADDR ; do right byte only...
668 @L2: ; do the fill in the middle
669 lda ADDR ; calculate offset in full bytes
672 beq @L3 ; if equal - there are no more bytes
675 lda X1 ; setup for the left side
678 lda BITMASK ; get color
679 jsr VDCWriteByte ; put 1st value
682 beq @L3 ; 1 byte already written
684 stx ADDR ; if there are more bytes - fill them...
687 jsr VDCWriteReg ; setup for fill
690 jsr VDCWriteReg ; ... fill them NOW!
698 ; ------------------------------------------------------------------------
699 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
700 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
702 ; Must set an error code: NO
722 @L0137: ; if (y2>y1) {
729 jmp HORLINE ; x1/x2 are sorted, y1==y2 - do faster horizontal line draw
772 ; dx = dy = 0; ax = ay = 1 }
777 ; else { dx = dy = 1; ax = ay = 0 }
787 ; for (count=nx;count>0;--count) {
792 @L0166: lda COUNT ; count>0
797 @L0167: jsr SETPIXELCLIP
831 ; if (abs(pb)<abs(ub)) {
847 ; } else { x1 = x1 + ay
880 ; ------------------------------------------------------------------------
881 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
882 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
883 ; Contrary to most other functions, the graphics kernel will sort and clip
884 ; the coordinates before calling the driver, so on entry the following
885 ; conditions are valid:
888 ; (X1 >= 0) && (X1 < XRES)
889 ; (X2 >= 0) && (X2 < XRES)
890 ; (Y1 >= 0) && (Y1 < YRES)
891 ; (Y2 >= 0) && (Y2 < YRES)
893 ; Must set an error code: NO
912 ; ------------------------------------------------------------------------
913 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
914 ; radius in tmp1 and the current drawing color.
916 ; Must set an error code: NO
922 jmp SETPIXELCLIP ; Plot as a point
940 stx YS+1 ; XS/YS to remember the center
949 @L12: ; plot points in 8 slices...
964 sta Y1+1 ; (stack)=ys+y, y1=(stack)
966 jsr SETPIXELCLIP ; plot(xs+x,ys+y)
974 sta Y1+1 ; y3 = y1 = ys-y
976 jsr SETPIXELCLIP ; plot(xs+x,ys-y)
988 jsr SETPIXELCLIP ; plot (xs-x,ys+y)
993 jsr SETPIXELCLIP ; plot (xs-x,ys-y)
1001 sta X1+1 ; x1 = xs+y
1009 sta Y1+1 ; (stack)=ys+x, y1=(stack)
1011 jsr SETPIXELCLIP ; plot(xs+y,ys+x)
1019 sta Y1+1 ; y3 = y1 = ys-x
1021 jsr SETPIXELCLIP ; plot(xs+y,ys-x)
1025 sta Y1 ; y1 = ys+x(stack)
1033 jsr SETPIXELCLIP ; plot (xs-y,ys+x)
1038 jsr SETPIXELCLIP ; plot (xs-y,ys-x)
1088 @L0148: ; if (abs(ou)<abs(og))
1107 @L014E: ; mo = ou; }
1119 ; ------------------------------------------------------------------------
1120 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1121 ; direction is passend in X/Y, the text direction is passed in A.
1123 ; Must set an error code: NO
1133 ; ------------------------------------------------------------------------
1134 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1135 ; current text style. The text to output is given as a zero terminated
1136 ; string with address in ptr3.
1138 ; Must set an error code: NO
1144 ; ------------------------------------------------------------------------
1145 ; Calculate all variables to plot the pixel at X1/Y1. If the point is out
1146 ; of range, a carry is returned and INRANGE is set to a value !0 zero. If
1147 ; the coordinates are valid, INRANGE is zero and the carry clear.
1149 ;------------------------
1151 ;> ADDR - address of card, INRANGE
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)
1251 @L0: bit VDC_ADDR_REG
1264 @L0: bit VDC_ADDR_REG
1273 @L0: bit VDC_ADDR_REG