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-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 API version number
59 .addr $0000 ; Library reference
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 $0100 ; Aspect ratio (based on 4/3 display)
67 .byte 0 ; TGI driver flags
69 ; Next comes the jump table. Currently all entries must be valid and may point
70 ; to an RTS for test versions (function not implemented).
91 .addr 0 ; IRQ entry is unused
93 ; ------------------------------------------------------------------------
96 ; Variables mapped to the zero page segment variables. Some of these are
97 ; used for passing parameters to the driver.
106 TEMP2 = tmp4 ; HORLINE
107 TEMP3 = sreg ; HORLINE
109 ; Absolute variables used in the code
113 ERROR: .res 1 ; Error code
114 PALETTE: .res 2 ; The current palette
116 BITMASK: .res 1 ; $00 = clear, $FF = set pixels
118 OLDCOLOR: .res 1 ; colors before entering gfx mode
125 ; Constants and tables
129 DEFPALETTE: .byte $00, $0f ; White on black
130 PALETTESIZE = * - DEFPALETTE
132 BITTAB: .byte $80,$40,$20,$10,$08,$04,$02,$01
134 BITMASKL: .byte %11111111, %01111111, %00111111, %00011111
135 .byte %00001111, %00000111, %00000011, %00000001
137 BITMASKR: .byte %10000000, %11000000, %11100000, %11110000
138 .byte %11111000, %11111100, %11111110, %11111111
140 ; color translation table (indexed by VIC color)
141 COLTRANS: .byte $00, $0f, $08, $06, $0a, $04, $02, $0c
142 .byte $0d, $0b, $09, $01, $0e, $05, $03, $07
143 ; colors BROWN and GRAY3 are wrong
145 ; VDC initialization table (reg),(val),...,$ff
147 .byte VDC_DSP_HI, 0 ; viewpage 0 as default
149 .byte VDC_HSCROLL, $87
160 SCN80CLR: .byte 27,88,147,27,88,0
164 ; ------------------------------------------------------------------------
165 ; INSTALL routine. Is called after the driver is loaded into memory. May
166 ; initialize anything that has to be done just once. Is probably empty
169 ; Must set an error code: NO
173 ; check for VDC version and update register $19 value
175 ; check for VDC ram size and update number of available screens
177 ldx #VDC_CSET ; determine size of RAM...
181 jsr VDCWriteReg ; turn on 64k
183 jsr settestadr1 ; save original value of test byte
187 lda #$55 ; write $55 here
189 jsr test64k ; read it here and there
190 lda #$aa ; write $aa here
192 jsr test64k ; read it here and there
196 jsr VDCWriteByte ; restore original value of test byte
198 lda ptr1 ; do bytes match?
207 jsr VDCWriteReg ; restore 16/64k flag
208 jmp @endok ; and leave default values for 16k
223 jsr VDCWriteByte ; write $55
225 jsr VDCReadByte ; read here
228 jsr VDCReadByte ; and there
237 ldy #$02 ; test page 2 (here)
240 ldy #$42 ; or page 64+2 (there)
244 ; ------------------------------------------------------------------------
245 ; UNINSTALL routine. Is called before the driver is removed from memory. May
246 ; clean up anything done by INSTALL but is probably empty most of the time.
248 ; Must set an error code: NO
255 ; ------------------------------------------------------------------------
256 ; INIT: Changes an already installed device from text mode to graphics
258 ; Note that INIT/DONE may be called multiple times while the driver
259 ; is loaded, while INSTALL is only called once, so any code that is needed
260 ; to initializes variables and so on must go here. Setting palette and
261 ; clearing the screen is not needed because this is called by the graphics
263 ; The graphics kernel will never call INIT when a graphics mode is already
264 ; active, so there is no need to protect against that.
266 ; Must set an error code: YES
270 lda pages ; is there enough memory?
271 bne @L1 ; Jump if there is one screen
272 lda #TGI_ERR_INV_MODE ; Error
275 ; Initialize variables
280 ; Remeber current color value
285 ; Switch into graphics mode (set view page 0)
288 @L2: ldx InitVDCTab,y
297 ; Done, reset the error code
303 ; ------------------------------------------------------------------------
304 ; DONE: Will be called to switch the graphics device back into text mode.
305 ; The graphics kernel will never call DONE when no graphics mode is active,
306 ; so there is no need to protect against that.
308 ; Must set an error code: NO
312 ; This part is C128-mode specific
313 jsr $e179 ; reload character set and setup VDC
315 lda $d7 ; in 80-columns?
319 jsr $ffd2 ; print \xe,clr,\xe
323 jsr $ffd2 ; print clr
324 @L1: lda #0 ; restore view page
329 jsr VDCWriteReg ; restore color (background)
332 jmp VDCWriteReg ; switch to text screen
334 ; ------------------------------------------------------------------------
335 ; GETERROR: Return the error code in A and clear it.
343 ; ------------------------------------------------------------------------
344 ; CONTROL: Platform/driver specific entry point.
346 ; Must set an error code: YES
350 lda #TGI_ERR_INV_FUNC
354 ; ------------------------------------------------------------------------
355 ; CLEAR: Clears the screen.
357 ; Must set an error code: NO
366 jsr VDCWriteReg ; set fill mode
368 jsr VDCWriteByte ; put 1rst byte (fill value)
377 ; ------------------------------------------------------------------------
378 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
379 ; The page number is already checked to be valid by the graphics kernel.
381 ; Must set an error code: NO (will only be called if page ok)
387 ; ------------------------------------------------------------------------
388 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
389 ; The page number is already checked to be valid by the graphics kernel.
391 ; Must set an error code: NO (will only be called if page ok)
397 ; ------------------------------------------------------------------------
398 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
399 ; to be in a valid range (0..maxcolor-1).
401 ; Must set an error code: NO (will only be called if color ok)
411 ; ------------------------------------------------------------------------
412 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
413 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
416 ; Must set an error code: YES
421 @L1: lda (ptr1),y ; Copy the palette
422 and #$0F ; Make a valid color
427 ; Get the color entries from the palette
429 ldy PALETTE+1 ; Foreground color
435 ldy PALETTE ; Background color
440 lda #TGI_ERR_OK ; Clear error code
444 ; ------------------------------------------------------------------------
445 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
446 ; set the palette should return the default palette here, so there's no
447 ; way for this function to fail.
449 ; Must set an error code: NO
457 ; ------------------------------------------------------------------------
458 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
459 ; drivers should return something reasonable here, even drivers that don't
460 ; support palettes, otherwise the caller has no way to determine the colors
461 ; of the (not changeable) palette.
463 ; Must set an error code: NO (all drivers must have a default palette)
471 ; ------------------------------------------------------------------------
472 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
473 ; color. The coordinates passed to this function are never outside the
474 ; visible screen area, so there is no need for clipping inside this function.
476 ; Must set an error code: NO
480 jsr CALC ; Calculate coordinates
502 ; ------------------------------------------------------------------------
503 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
504 ; coordinates passed to this function are never outside the visible screen
505 ; area, so there is no need for clipping inside this function.
509 jsr CALC ; Calculate coordinates
511 stx TEMP ; preserve X
523 @L1: tya ; Get color value into A
524 ldx #$00 ; Clear high byte
527 ; ------------------------------------------------------------------------
528 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
529 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
530 ; Contrary to most other functions, the graphics kernel will sort and clip
531 ; the coordinates before calling the driver, so on entry the following
532 ; conditions are valid:
535 ; (X1 >= 0) && (X1 < XRES)
536 ; (X2 >= 0) && (X2 < XRES)
537 ; (Y1 >= 0) && (Y1 < YRES)
538 ; (Y2 >= 0) && (Y2 < YRES)
540 ; Must set an error code: NO
548 ; Original code for a horizontal line
555 jsr CALC ; get data for LEFT
556 lda BITMASKL,x ; remember left address and bitmask
567 jsr CALC ; get data for RIGHT
571 pla ; recall data for LEFT
574 sta X1 ; put left address into X1
577 cmp #%11111111 ; if left bit <> 0
579 sta TEMP2 ; do left byte only...
594 inc X1 ; ... and proceed
598 ; do right byte (if Y2=0 ++ADDR and skip)
600 cmp #%11111111 ; if right bit <> 7
602 inc ADDR ; right bit = 7 - the next one is the last
607 @L11: lda ADDR ; do right byte only...
622 @L2: ; do the fill in the middle
623 lda ADDR ; calculate offset in full bytes
626 beq @L3 ; if equal - there are no more bytes
629 lda X1 ; setup for the left side
632 lda BITMASK ; get color
633 jsr VDCWriteByte ; put 1st value
636 beq @L3 ; 1 byte already written
638 stx ADDR ; if there are more bytes - fill them...
641 jsr VDCWriteReg ; setup for fill
644 jsr VDCWriteReg ; ... fill them NOW!
651 ; End of horizontal line code
667 ; ------------------------------------------------------------------------
668 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
669 ; direction is passend in X/Y, the text direction is passed in A.
671 ; Must set an error code: NO
681 ; ------------------------------------------------------------------------
682 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
683 ; current text style. The text to output is given as a zero terminated
684 ; string with address in ptr3.
686 ; Must set an error code: NO
692 ; ------------------------------------------------------------------------
693 ; Calculate all variables to plot the pixel at X1/Y1.
694 ;------------------------
696 ;> ADDR - address of card
697 ;> X - bit number (X1 & 7)
717 sta ADDR+1 ; Y*4+Y=Y*5
727 sta ADDR ; Y*5*16=Y*80
740 lda ADDR+1 ; ADDR = Y*80+x/8
748 beq @even ; even line - no offset
755 sta ADDR+1 ; odd lines are 21360 bytes farther
777 @L0: bit VDC_ADDR_REG
786 @L0: bit VDC_ADDR_REG
791 ; ------------------------------------------------------------------------
793 .include "../../tgi/tgidrv_line.inc"