2 ; Graphics driver for the 640x200x2 mode on the C128 VDC
3 ; Maciej 'YTM/Elysium' Witkowiak <ytm@elysium.pl>
7 ; On the real machine there are some additional pixels appearing (probably bad reads)
8 ; VICE emulator does not show them. Needs further investigation.
10 ; Only DONE routine contains C128-mode specific stuff, everything else will work in
11 ; C64-mode of C128 (C64 needs full VDC init then).
13 ; X-axis resolution can be changed by manipulating xres header field so
14 ; any resolution 8..708 is possible only with gfx-mode initialization and CALC changed.
16 ; With special initialization and CALC we can get 320x200 double-pixel mode.
18 ; Color translation values for BROWN and GRAY3 are obviously wrong, they could
19 ; be replaced by equiv. of ORANGE and GRAY2 but this would give only 14 of 16 colors available.
21 ; Register 25 ($19) is said to require different value for VDC v1, but I couldn't find what
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 $00 ; TGI version number
60 xres: .word 640 ; X resolution
61 yres: .word 200 ; Y resolution
62 .byte 2 ; Number of drawing colors
63 pages: .byte 1 ; 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). A future version may
70 ; allow for emulation: In this case the vector will be zero. Emulation means
71 ; that the graphics kernel will emulate the function by using lower level
72 ; primitives - for example ploting a line by using calls to SETPIXEL.
96 ; ------------------------------------------------------------------------
99 ; Variables mapped to the zero page segment variables. Some of these are
100 ; used for passing parameters to the driver.
108 ADDR = tmp1 ; (2) CALC
109 TEMP = tmp3 ; CALC icmp
112 TEMP4 = sreg+1 ; LINE
114 ; Line routine stuff (must be on zpage)
117 ERR = regsave ; (2) LINE
118 NX = regsave+2 ; (2) LINE
120 XX = ptr3 ; (2) CIRCLE
121 YY = ptr4 ; (2) CIRCLE
122 MaxO = sreg ; (overwritten by TEMP3+TEMP4, but restored from OG/OU anyway)
123 XS = regsave ; (2) CIRCLE
124 YS = regsave+2 ; (2) CIRCLE
126 ; Absolute variables used in the code
130 SCRBASE: .res 1 ; High byte of screen base
132 ERROR: .res 1 ; Error code
133 PALETTE: .res 2 ; The current palette
135 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
137 OLDCOLOR: .res 1 ; colors before entering gfx mode
139 ; Line routine stuff (combined with CIRCLE to save space)
155 ; Constants and tables
159 DEFPALETTE: .byte $00, $0f ; White on black
160 PALETTESIZE = * - DEFPALETTE
162 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
164 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
165 .byte %00001111, %00000111, %00000011, %00000001
167 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
168 .byte %11111000, %11111100, %11111110, %11111111
170 ; color translation table (indexed by VIC color)
171 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
172 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
173 ; colors BROWN and GRAY3 are wrong
175 ; VDC initialization table (reg),(val),...,$ff
177 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
179 .byte VDC_HSCROLL, $87
182 SCN80CLR: .byte 27,88,147,27,88,0
186 ; ------------------------------------------------------------------------
187 ; INSTALL routine. Is called after the driver is loaded into memory. May
188 ; initialize anything that has to be done just once. Is probably empty
191 ; Must set an error code: NO
195 ; check for VDC version and update register $19 value
197 ; check for VDC ram size and update number of available screens
199 ldx #VDC_CSET ; determine size of RAM...
203 jsr VDCWriteReg ; turn on 64k
205 jsr settestadr1 ; save original value of test byte
209 lda #$55 ; write $55 here
211 jsr test64k ; read it here and there
212 lda #$aa ; write $aa here
214 jsr test64k ; read it here and there
218 jsr VDCWriteByte ; restore original value of test byte
220 lda ptr1 ; do bytes match?
229 jsr VDCWriteReg ; restore 16/64k flag
230 jmp @endok ; and leave default values for 16k
237 sta SCRBASE ; draw page 0 as default
247 jsr VDCWriteByte ; write $55
249 jsr VDCReadByte ; read here
252 jsr VDCReadByte ; and there
261 ldy #$02 ; test page 2 (here)
264 ldy #$42 ; or page 64+2 (there)
268 ; ------------------------------------------------------------------------
269 ; DEINSTALL routine. Is called before the driver is removed from memory. May
270 ; clean up anything done by INSTALL but is probably empty most of the time.
272 ; Must set an error code: NO
279 ; ------------------------------------------------------------------------
280 ; INIT: Changes an already installed device from text mode to graphics
281 ; mode. The number of the graphics mode is passed to the function in A.
282 ; Note that INIT/DONE may be called multiple times while the driver
283 ; is loaded, while INSTALL is only called once, so any code that is needed
284 ; to initializes variables and so on must go here. Setting palette and
285 ; clearing the screen is not needed because this is called by the graphics
287 ; The graphics kernel will never call INIT when a graphics mode is already
288 ; active, so there is no need to protect against that.
290 ; Must set an error code: YES
293 INIT: cmp #TGI_MODE_640_200_2 ; Correct mode?
294 beq @L1 ; Jump if yes
295 lda #TGI_ERR_INV_MODE ; ## Error
298 ; Initialize variables
303 ; Switch into graphics mode (set view page 0)
306 @L2: ldx InitVDCTab,y
315 ; Remeber current color value
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: YES
335 ; This part is C128-mode specific
336 jsr $ce0c ; reload character set
337 lda $d7 ; in 80-columns?
341 jsr $ffd2 ; print \xe,clr,\xe
345 jsr $ffd2 ; print clr
346 @L1: lda #0 ; restore view page
351 jsr VDCWriteReg ; restore color (background)
354 jmp VDCWriteReg ; switch to text screen
356 ; ------------------------------------------------------------------------
357 ; GETERROR: Return the error code in A and clear it.
365 ; ------------------------------------------------------------------------
366 ; CONTROL: Platform/driver specific entry point.
368 ; Must set an error code: YES
372 lda #TGI_ERR_INV_FUNC
376 ; ------------------------------------------------------------------------
377 ; CLEAR: Clears the screen.
379 ; Must set an error code: NO
388 jsr VDCWriteReg ; set fill mode
390 jsr VDCWriteByte ; put 1rst byte (fill value)
398 jmp VDCWriteReg ; 1+62*256+127=16000=(640*256)/8
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)
415 ; ------------------------------------------------------------------------
416 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
417 ; The page number is already checked to be valid by the graphics kernel.
419 ; Must set an error code: NO (will only be called if page ok)
430 ; ------------------------------------------------------------------------
431 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
432 ; to be in a valid range (0..maxcolor-1).
434 ; Must set an error code: NO (will only be called if color ok)
444 ; ------------------------------------------------------------------------
445 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
446 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
449 ; Must set an error code: YES
454 @L1: lda (ptr1),y ; Copy the palette
455 and #$0F ; Make a valid color
460 ; Get the color entries from the palette
462 ldy PALETTE+1 ; Foreground color
468 ldy PALETTE ; Background color
474 ; ------------------------------------------------------------------------
475 ; GETPALETTE: Return the current palette in A/X. Must return NULL and set an
476 ; error if palettes are not supported.
478 ; Must set an error code: YES
486 ; ------------------------------------------------------------------------
487 ; GETDEFPALETTE: Return the default palette for the driver in A/X. Must
488 ; return NULL and set an error of palettes are not supported.
490 ; Must set an error code: YES
498 ; ------------------------------------------------------------------------
499 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
500 ; color. The coordinates passed to this function are never outside the
501 ; visible screen area, so there is no need for clipping inside this function.
503 ; Must set an error code: NO
518 jsr icmp ; if (xres<x1)
519 bcs @cont ; !(xres<x1)
528 jsr icmp ; if (yres<y1)
532 jsr CALC ; Calculate coordinates
554 ; ------------------------------------------------------------------------
555 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
556 ; coordinates passed to this function are never outside the visible screen
557 ; area, so there is no need for clipping inside this function.
561 jsr CALC ; Calculate coordinates
563 stx TEMP ; preserve X
575 @L1: tya ; Get color value into A
576 ldx #$00 ; Clear high byte
579 ; ------------------------------------------------------------------------
580 ; HORLINE: Draw a horizontal line from X1/Y to X2/Y, where X1 = ptr1,
581 ; Y = ptr2 and X2 = ptr3, using the current drawing color.
583 ; This is a special line drawing entry used when the line is know to be
584 ; horizontal, for example by the BAR emulation routine. If the driver does
585 ; not have special code for horizontal lines, it may just copy Y to Y2 and
586 ; proceed with the generic line drawing code.
588 ; Note: Line coordinates will always be sorted (Y1 <= X2) and clipped.
590 ; Must set an error code: NO
598 jsr CALC ; get data for LEFT
599 lda BITMASKL,x ; remember left address and bitmask
610 jsr CALC ; get data for RIGHT
614 pla ; recall data for LEFT
617 sta X1 ; put left address into X1
620 cmp #%11111111 ; if left bit <> 0
622 sta TEMP2 ; do left byte only...
637 inc X1 ; ... and proceed
641 ; do right byte (if Y2=0 ++ADDR and skip)
643 cmp #%11111111 ; if right bit <> 7
645 inc ADDR ; right bit = 7 - the next one is the last
650 @L11: lda ADDR ; do right byte only...
665 @L2: ; do the fill in the middle
666 lda ADDR ; calculate offset in full bytes
669 beq @L3 ; if equal - there are no more bytes
672 lda X1 ; setup for the left side
675 lda BITMASK ; get color
676 jsr VDCWriteByte ; put 1st value
679 beq @L3 ; 1 byte already written
681 stx ADDR ; if there are more bytes - fill them...
684 jsr VDCWriteReg ; setup for fill
687 jsr VDCWriteReg ; ... fill them NOW!
695 ; ------------------------------------------------------------------------
696 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
697 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
699 ; Must set an error code: NO
719 @L0137: ; if (y2>y1) {
726 jmp HORLINE ; x1/x2 are sorted, y1==y2 - do faster horizontal line draw
769 ; dx = dy = 0; ax = ay = 1 }
774 ; else { dx = dy = 1; ax = ay = 0 }
784 ; for (count=nx;count>0;--count) {
789 @L0166: lda COUNT ; count>0
794 @L0167: jsr SETPIXELCLIP
828 ; if (abs(pb)<abs(ub)) {
844 ; } else { x1 = x1 + ay
877 ; ------------------------------------------------------------------------
878 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
879 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
880 ; Contrary to most other functions, the graphics kernel will sort and clip
881 ; the coordinates before calling the driver, so on entry the following
882 ; conditions are valid:
885 ; (X1 >= 0) && (X1 < XRES)
886 ; (X2 >= 0) && (X2 < XRES)
887 ; (Y1 >= 0) && (Y1 < YRES)
888 ; (Y2 >= 0) && (Y2 < YRES)
890 ; Must set an error code: NO
909 ; ------------------------------------------------------------------------
910 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
911 ; radius in tmp1 and the current drawing color.
913 ; Must set an error code: NO
919 jmp SETPIXELCLIP ; Plot as a point
937 stx YS+1 ; XS/YS to remember the center
946 @L12: ; plot points in 8 slices...
961 sta Y1+1 ; (stack)=ys+y, y1=(stack)
963 jsr SETPIXELCLIP ; plot(xs+x,ys+y)
971 sta Y1+1 ; y3 = y1 = ys-y
973 jsr SETPIXELCLIP ; plot(xs+x,ys-y)
985 jsr SETPIXELCLIP ; plot (xs-x,ys+y)
990 jsr SETPIXELCLIP ; plot (xs-x,ys-y)
1006 sta Y1+1 ; (stack)=ys+x, y1=(stack)
1008 jsr SETPIXELCLIP ; plot(xs+y,ys+x)
1016 sta Y1+1 ; y3 = y1 = ys-x
1018 jsr SETPIXELCLIP ; plot(xs+y,ys-x)
1022 sta Y1 ; y1 = ys+x(stack)
1030 jsr SETPIXELCLIP ; plot (xs-y,ys+x)
1035 jsr SETPIXELCLIP ; plot (xs-y,ys-x)
1085 @L0148: ; if (abs(ou)<abs(og))
1104 @L014E: ; mo = ou; }
1116 ; ------------------------------------------------------------------------
1117 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
1118 ; direction is passend in X/Y, the text direction is passed in A.
1120 ; Must set an error code: NO
1130 ; ------------------------------------------------------------------------
1131 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
1132 ; current text style. The text to output is given as a zero terminated
1133 ; string with address in ptr3.
1135 ; Must set an error code: NO
1141 ; ------------------------------------------------------------------------
1142 ; Calculate all variables to plot the pixel at X1/Y1.
1143 ;------------------------
1145 ;> ADDR - address of card
1146 ;> X - bit number (X1 & 7)
1160 sta ADDR+1 ; Y*4+Y=Y*5
1170 sta ADDR ; Y*5*16=Y*80
1183 lda ADDR+1 ; ADDR = Y*80+x/8
1195 ; copies of some runtime routines
1215 ; compare a/y to zp,x
1216 sta TEMP ; TEMP/TEMP2 - arg2
1224 tya ; x/a - arg1 (a=high)
1245 @L0: bit VDC_ADDR_REG
1250 @L1: bit VDC_ADDR_REG
1260 @L0: bit VDC_ADDR_REG
1269 @L0: bit VDC_ADDR_REG