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 .word $100 ; Aspect ratio
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).
90 .addr 0 ; IRQ entry is unused
92 ; ------------------------------------------------------------------------
95 ; Variables mapped to the zero page segment variables. Some of these are
96 ; 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 ; Absolute variables used in the code
119 ERROR: .res 1 ; Error code
120 PALETTE: .res 2 ; The current palette
122 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
124 OLDCOLOR: .res 1 ; colors before entering gfx mode
126 ; Line routine stuff (combined with CIRCLE to save space)
140 ; Constants and tables
144 DEFPALETTE: .byte $00, $0f ; White on black
145 PALETTESIZE = * - DEFPALETTE
147 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
149 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
150 .byte %00001111, %00000111, %00000011, %00000001
152 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
153 .byte %11111000, %11111100, %11111110, %11111111
155 ; color translation table (indexed by VIC color)
156 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
157 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
158 ; colors BROWN and GRAY3 are wrong
160 ; VDC initialization table (reg),(val),...,$ff
162 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
164 .byte VDC_HSCROLL, $87
175 SCN80CLR: .byte 27,88,147,27,88,0
179 ; ------------------------------------------------------------------------
180 ; INSTALL routine. Is called after the driver is loaded into memory. May
181 ; initialize anything that has to be done just once. Is probably empty
184 ; Must set an error code: NO
188 ; check for VDC version and update register $19 value
190 ; check for VDC ram size and update number of available screens
192 ldx #VDC_CSET ; determine size of RAM...
196 jsr VDCWriteReg ; turn on 64k
198 jsr settestadr1 ; save original value of test byte
202 lda #$55 ; write $55 here
204 jsr test64k ; read it here and there
205 lda #$aa ; write $aa here
207 jsr test64k ; read it here and there
211 jsr VDCWriteByte ; restore original value of test byte
213 lda ptr1 ; do bytes match?
222 jsr VDCWriteReg ; restore 16/64k flag
223 jmp @endok ; and leave default values for 16k
238 jsr VDCWriteByte ; write $55
240 jsr VDCReadByte ; read here
243 jsr VDCReadByte ; and there
252 ldy #$02 ; test page 2 (here)
255 ldy #$42 ; or page 64+2 (there)
259 ; ------------------------------------------------------------------------
260 ; UNINSTALL routine. Is called before the driver is removed from memory. May
261 ; clean up anything done by INSTALL but is probably empty most of the time.
263 ; Must set an error code: NO
270 ; ------------------------------------------------------------------------
271 ; INIT: Changes an already installed device from text mode to graphics
273 ; Note that INIT/DONE may be called multiple times while the driver
274 ; is loaded, while INSTALL is only called once, so any code that is needed
275 ; to initializes variables and so on must go here. Setting palette and
276 ; clearing the screen is not needed because this is called by the graphics
278 ; The graphics kernel will never call INIT when a graphics mode is already
279 ; active, so there is no need to protect against that.
281 ; Must set an error code: YES
285 lda pages ; is there enough memory?
286 bne @L11 ; Jump if there is one screen
287 lda #TGI_ERR_INV_MODE ; Error
290 ; Initialize variables
295 ; Remeber current color value
300 ; Switch into graphics mode (set view page 0)
303 @L2: ldx InitVDCTab,y
312 ; Done, reset the error code
318 ; ------------------------------------------------------------------------
319 ; DONE: Will be called to switch the graphics device back into text mode.
320 ; The graphics kernel will never call DONE when no graphics mode is active,
321 ; so there is no need to protect against that.
323 ; Must set an error code: NO
327 ; This part is C128-mode specific
328 jsr $e179 ; reload character set and setup VDC
330 lda $d7 ; in 80-columns?
334 jsr $ffd2 ; print \xe,clr,\xe
338 jsr $ffd2 ; print clr
339 @L1: lda #0 ; restore view page
344 jsr VDCWriteReg ; restore color (background)
347 jmp VDCWriteReg ; switch to text screen
349 ; ------------------------------------------------------------------------
350 ; GETERROR: Return the error code in A and clear it.
358 ; ------------------------------------------------------------------------
359 ; CONTROL: Platform/driver specific entry point.
361 ; Must set an error code: YES
365 lda #TGI_ERR_INV_FUNC
369 ; ------------------------------------------------------------------------
370 ; CLEAR: Clears the screen.
372 ; Must set an error code: NO
381 jsr VDCWriteReg ; set fill mode
383 jsr VDCWriteByte ; put 1rst byte (fill value)
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)
402 ; ------------------------------------------------------------------------
403 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
404 ; The page number is already checked to be valid by the graphics kernel.
406 ; Must set an error code: NO (will only be called if page ok)
412 ; ------------------------------------------------------------------------
413 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
414 ; to be in a valid range (0..maxcolor-1).
416 ; Must set an error code: NO (will only be called if color ok)
426 ; ------------------------------------------------------------------------
427 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
428 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
431 ; Must set an error code: YES
436 @L1: lda (ptr1),y ; Copy the palette
437 and #$0F ; Make a valid color
442 ; Get the color entries from the palette
444 ldy PALETTE+1 ; Foreground color
450 ldy PALETTE ; Background color
455 lda #TGI_ERR_OK ; Clear error code
459 ; ------------------------------------------------------------------------
460 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
461 ; set the palette should return the default palette here, so there's no
462 ; way for this function to fail.
464 ; Must set an error code: NO
472 ; ------------------------------------------------------------------------
473 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
474 ; drivers should return something reasonable here, even drivers that don't
475 ; support palettes, otherwise the caller has no way to determine the colors
476 ; of the (not changeable) palette.
478 ; Must set an error code: NO (all drivers must have a default palette)
486 ; ------------------------------------------------------------------------
487 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
488 ; color. The coordinates passed to this function are never outside the
489 ; visible screen area, so there is no need for clipping inside this function.
491 ; Must set an error code: NO
495 jsr CALC ; Calculate coordinates
517 ; ------------------------------------------------------------------------
518 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
519 ; coordinates passed to this function are never outside the visible screen
520 ; area, so there is no need for clipping inside this function.
524 jsr CALC ; Calculate coordinates
526 stx TEMP ; preserve X
538 @L1: tya ; Get color value into A
539 ldx #$00 ; Clear high byte
542 ; ------------------------------------------------------------------------
543 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
544 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
546 ; Must set an error code: NO
638 ; for (count=nx;count>0;--count) {
643 @L0166: lda COUNT ; count>0
688 ; if (abs(pb)<abs(ub)) {
704 ; } else { x1 = x1 + ax
743 ; ------------------------------------------------------------------------
744 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
745 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
746 ; Contrary to most other functions, the graphics kernel will sort and clip
747 ; the coordinates before calling the driver, so on entry the following
748 ; conditions are valid:
751 ; (X1 >= 0) && (X1 < XRES)
752 ; (X2 >= 0) && (X2 < XRES)
753 ; (Y1 >= 0) && (Y1 < YRES)
754 ; (Y2 >= 0) && (Y2 < YRES)
756 ; Must set an error code: NO
764 ; Original code for a horizontal line
771 jsr CALC ; get data for LEFT
772 lda BITMASKL,x ; remember left address and bitmask
783 jsr CALC ; get data for RIGHT
787 pla ; recall data for LEFT
790 sta X1 ; put left address into X1
793 cmp #%11111111 ; if left bit <> 0
795 sta TEMP2 ; do left byte only...
810 inc X1 ; ... and proceed
814 ; do right byte (if Y2=0 ++ADDR and skip)
816 cmp #%11111111 ; if right bit <> 7
818 inc ADDR ; right bit = 7 - the next one is the last
823 @L11: lda ADDR ; do right byte only...
838 @L2: ; do the fill in the middle
839 lda ADDR ; calculate offset in full bytes
842 beq @L3 ; if equal - there are no more bytes
845 lda X1 ; setup for the left side
848 lda BITMASK ; get color
849 jsr VDCWriteByte ; put 1st value
852 beq @L3 ; 1 byte already written
854 stx ADDR ; if there are more bytes - fill them...
857 jsr VDCWriteReg ; setup for fill
860 jsr VDCWriteReg ; ... fill them NOW!
867 ; End of horizontal line code
883 ; ------------------------------------------------------------------------
884 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
885 ; direction is passend in X/Y, the text direction is passed in A.
887 ; Must set an error code: NO
897 ; ------------------------------------------------------------------------
898 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
899 ; current text style. The text to output is given as a zero terminated
900 ; string with address in ptr3.
902 ; Must set an error code: NO
908 ; ------------------------------------------------------------------------
909 ; Calculate all variables to plot the pixel at X1/Y1.
910 ;------------------------
912 ;> ADDR - address of card
913 ;> X - bit number (X1 & 7)
933 sta ADDR+1 ; Y*4+Y=Y*5
943 sta ADDR ; Y*5*16=Y*80
956 lda ADDR+1 ; ADDR = Y*80+x/8
964 beq @even ; even line - no offset
971 sta ADDR+1 ; odd lines are 21360 bytes farther
978 ; copies of some runtime routines
996 ; compare a/y to zp,x
997 sta TEMP ; TEMP/TEMP2 - arg2
1005 tya ; x/a - arg1 (a=high)
1036 @L0: bit VDC_ADDR_REG
1045 @L0: bit VDC_ADDR_REG