2 ; Graphics driver for the 160x102x16 mode on the Lynx.
4 ; All the drawing functions are simply done by sprites as the sprite
5 ; engine is the only way to do fast graphics on a Lynx.
7 ; This code is written by Karri Kaksonen, 2004 for the cc65 compiler.
10 .include "zeropage.inc"
13 .include "tgi-kernel.inc"
14 .include "tgi-error.inc"
20 ; ------------------------------------------------------------------------
21 ; Header. Includes jump table and constants.
25 ; First part of the header is a structure that has a magic and defines the
26 ; capabilities of the driver
28 .byte $74, $67, $69 ; "tgi"
29 .byte TGI_API_VERSION ; TGI API version number
30 .word 160 ; X resolution
31 .word 102 ; Y resolution
32 .byte 16 ; Number of drawing colors
33 .byte 2 ; Number of screens available
34 .byte 8 ; System font X size
35 .byte 8 ; System font Y size
36 .word $0100 ; Aspect ratio (square pixel LCD)
37 .byte TGI_BM_FONT_FINESCALE ; TGI driver flags
39 ; Next comes the jump table. Currently all entries must be valid and may point
40 ; to an RTS for test versions (function not implemented). A future version may
41 ; allow for emulation: In this case the vector will be zero. Emulation means
42 ; that the graphics kernel will emulate the function by using lower level
43 ; primitives - for example ploting a line by using calls to SETPIXEL.
67 ; ------------------------------------------------------------------------
70 ; Variables mapped to the zero page segment variables. Some of these are
71 ; used for passing parameters to the driver.
83 ; Absolute variables used in the code
87 ERROR: .res 1 ; Error code
89 DRAWINDEX: .res 1 ; Pen to use for drawing
99 BGINDEX: .res 1 ; Pen to use for text background
101 ; Double buffer IRQ stuff
105 text_bitmap: .res 8*(1+20+1)+1
106 ; 8 rows with (one offset-byte plus 20 character bytes plus one fill-byte) plus one 0-offset-byte
108 ; Constants and tables
112 DEFPALETTE: .byte >$000
145 PALETTESIZE = * - DEFPALETTE
149 ; ------------------------------------------------------------------------
150 ; INSTALL routine. Is called after the driver is loaded into memory. May
151 ; initialize anything that has to be done just once. Is probably empty
154 ; Must set an error code: NO
167 ; ------------------------------------------------------------------------
168 ; UNINSTALL routine. Is called before the driver is removed from memory. May
169 ; clean up anything done by INSTALL but is probably empty most of the time.
171 ; Must set an error code: NO
178 ; ------------------------------------------------------------------------
179 ; INIT: Changes an already installed device from text mode to graphics
181 ; Note that INIT/DONE may be called multiple times while the driver
182 ; is loaded, while INSTALL is only called once, so any code that is needed
183 ; to initializes variables and so on must go here. Setting palette and
184 ; clearing the screen is not needed because this is called by the graphics
186 ; The graphics kernel will never call INIT when a graphics mode is already
187 ; active, so there is no need to protect against that.
189 ; Must set an error code: YES
193 ; Enable interrupts for VBL
196 ; Set up collision buffer to $A058
201 ; Put collision index before sprite data
206 ; Done, reset the error code
211 ; ------------------------------------------------------------------------
212 ; DONE: Will be called to switch the graphics device back into text mode.
213 ; The graphics kernel will never call DONE when no graphics mode is active,
214 ; so there is no need to protect against that.
216 ; Must set an error code: NO
222 ; ------------------------------------------------------------------------
223 ; GETERROR: Return the error code in A and clear it.
231 ; ------------------------------------------------------------------------
232 ; CONTROL: Platform/driver specific entry point.
234 ; Must set an error code: YES
236 ; The TGI lacks a way to draw sprites. As that functionality is vital to
237 ; Lynx games we borrow this CONTROL function to implement the still
238 ; missing tgi_draw_sprite funtion. To use this in your C-program
239 ; do a #define tgi_draw_sprite(spr) tgi_ioctl(0, spr)
241 ; To do a flip-screen call tgi_ioctl(1, 0)
243 ; To set the background index for text outputs call tgi_ioctl(2, bgindex)
245 ; To set the frame rate for the display hardware call tgi_ioctl(3, rate)
247 ; To check if the drawing engine is busy with the previous swap you can
248 ; call tgi_ioctl(4, 0). It returns 0 if idle and 1 if busy
250 ; To update displays you can call tgi_ioctl(4, 1) it will wait for the
251 ; next VBL interrupt and swap draw and view buffers.
253 ; Activate or deactivate collision detection by calling tgi_ioctl(5, 0/1).
256 pha ; Almost all control routines succeed
278 lda ptr1 ; Swap request
290 cmp #75 ; Set framerate
299 rate50: lda #$bd ; 50 Hz
302 rate60: lda #$9e ; 60 Hz
305 rate75: lda #$7e ; 75 Hz
314 bne ControlFlipScreen
316 lda ptr1 ; Set text background color
322 bne ControlDrawSprite
324 lda __sprsys ; Flip screen
349 lda ptr1 ; Get the sprite address
352 draw_sprite: ; Draw it in render buffer
371 ; ------------------------------------------------------------------------
372 ; CLEAR: Clears the screen.
374 ; Must set an error code: NO
379 .byte 3,%10000100,%00000000, $0 ; A pixel bitmap
381 .byte %00000001 ; A pixel sprite
392 CLEAR: lda #<cls_sprite
396 ; ------------------------------------------------------------------------
397 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
398 ; The page number is already checked to be valid by the graphics kernel.
400 ; Must set an error code: NO (will only be called if page ok)
402 ; It is a good idea to call this function during the vertical blanking
403 ; period. If you call it in the middle of the screen update then half of
404 ; the drawn frame will be from the old buffer and the other half is
405 ; from the new buffer. This is usually noticed by the user.
409 beq @L1 ; page == maxpages-1
417 sty VIEWPAGEL ; Save viewpage for getpixel
420 lda __viddma ; Process flipped displays
435 ; ------------------------------------------------------------------------
436 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
437 ; The page number is already checked to be valid by the graphics kernel.
439 ; Must set an error code: NO (will only be called if page ok)
444 beq @L1 ; page == maxpages-1
456 ; ------------------------------------------------------------------------
457 ; IRQ: VBL interrupt handler
461 lda INTSET ; Poll all pending interrupts
463 beq IRQEND ; Exit if not a VBL interrupt
479 ; ------------------------------------------------------------------------
480 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
481 ; to be in a valid range (0..maxcolor-1).
483 ; Must set an error code: NO (will only be called if color ok)
490 ; ------------------------------------------------------------------------
491 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
492 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
495 ; Must set an error code: YES
501 sta GCOLMAP,y ; $FDA0
505 ; Done, reset the error code
511 ; ------------------------------------------------------------------------
512 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
513 ; set the palette should return the default palette here, so there's no
514 ; way for this function to fail.
516 ; Must set an error code: NO
520 lda #<GCOLMAP ; $FDA0
524 ; ------------------------------------------------------------------------
525 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
526 ; drivers should return something reasonable here, even drivers that don't
527 ; support palettes, otherwise the caller has no way to determine the colors
528 ; of the (not changeable) palette.
530 ; Must set an error code: NO (all drivers must have a default palette)
538 ; ------------------------------------------------------------------------
539 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
540 ; color. The coordinates passed to this function are never outside the
541 ; visible screen area, so there is no need for clipping inside this function.
543 ; Must set an error code: NO
548 .byte %00000001 ; A pixel sprite
570 ; ------------------------------------------------------------------------
571 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
572 ; coordinates passed to this function are never outside the visible screen
573 ; area, so there is no need for clipping inside this function.
578 sta MATHD ; Hardware multiply
615 ; ------------------------------------------------------------------------
616 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
617 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
619 ; Must set an error code: NO
624 .byte 0 ; Will be replaced by the code
672 lda #%00000000 ; Not flipped
690 lda #%00010000 ; Vertical flip
705 sta MATHP ; hardware divide
718 bmi @L3 ; wait for math done (bit 7 of sprsys)
738 ; ------------------------------------------------------------------------
739 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
740 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
741 ; Contrary to most other functions, the graphics kernel will sort and clip
742 ; the coordinates before calling the driver, so on entry the following
743 ; conditions are valid:
746 ; (X1 >= 0) && (X1 < XRES)
747 ; (X2 >= 0) && (X2 < XRES)
748 ; (Y1 >= 0) && (Y1 < YRES)
749 ; (Y2 >= 0) && (Y2 < YRES)
751 ; Must set an error code: NO
756 .byte %00000001 ; A pixel sprite
787 ; ------------------------------------------------------------------------
788 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
789 ; direction is passend in X/Y, the text direction is passed in A.
791 ; Must set an error code: NO
800 ; ------------------------------------------------------------------------
801 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
802 ; current text style. The text to output is given as a zero terminated
803 ; string with address in ptr3.
805 ; Must set an error code: NO
809 lda TEXTMAGX ; Scale sprite
814 stz text_sprite ; Set normal sprite
818 sta text_sprite ; Set opaque sprite
820 lda DRAWINDEX ; Set color
828 lda X1 ; Set start position
837 ldy #<-1 ; Calculate string length
848 rts ; Zero length string
850 iny ; Prepare text_bitmap
854 ldy #8-1 ; 8 pixel lines per character
891 ; and now copy the 8 bytes of that char
897 ; draw char from top to bottom, reading char-data from offset 8-1 to offset 0
900 lda (FONTOFF),y ; *chptr
901 sta text_bitmap,x ;textbuf[y*(1+len+1)+1+x]
937 ; 96 characters from ASCII 32 to 127
938 ; 8 pixels wide, 8 pixels high
939 ; bit value 0 = foreground, bit value 1 = background / transparent
942 .byte $FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF ;32
943 .byte $FF, $E7, $FF, $FF, $E7, $E7, $E7, $E7 ;33
944 .byte $FF, $FF, $FF, $FF, $FF, $99, $99, $99 ;34
945 .byte $FF, $99, $99, $00, $99, $00, $99, $99 ;35
946 .byte $FF, $E7, $83, $F9, $C3, $9F, $C1, $E7 ;36
947 .byte $FF, $B9, $99, $CF, $E7, $F3, $99, $9D ;37
948 .byte $FF, $C0, $99, $98, $C7, $C3, $99, $C3 ;38
949 .byte $FF, $FF, $FF, $FF, $FF, $E7, $F3, $F9 ;39
950 .byte $FF, $F3, $E7, $CF, $CF, $CF, $E7, $F3 ;40
951 .byte $FF, $CF, $E7, $F3, $F3, $F3, $E7, $CF ;41
952 .byte $FF, $FF, $99, $C3, $00, $C3, $99, $FF ;42
953 .byte $FF, $FF, $E7, $E7, $81, $E7, $E7, $FF ;43
954 .byte $CF, $E7, $E7, $FF, $FF, $FF, $FF, $FF ;44
955 .byte $FF, $FF, $FF, $FF, $81, $FF, $FF, $FF ;45
956 .byte $FF, $E7, $E7, $FF, $FF, $FF, $FF, $FF ;46
957 .byte $FF, $9F, $CF, $E7, $F3, $F9, $FC, $FF ;47
958 .byte $FF, $C3, $99, $99, $89, $91, $99, $C3 ;48
959 .byte $FF, $81, $E7, $E7, $E7, $C7, $E7, $E7 ;49
960 .byte $FF, $81, $9F, $CF, $F3, $F9, $99, $C3 ;50
961 .byte $FF, $C3, $99, $F9, $E3, $F9, $99, $C3 ;51
962 .byte $FF, $F9, $F9, $80, $99, $E1, $F1, $F9 ;52
963 .byte $FF, $C3, $99, $F9, $F9, $83, $9F, $81 ;53
964 .byte $FF, $C3, $99, $99, $83, $9F, $99, $C3 ;54
965 .byte $FF, $E7, $E7, $E7, $E7, $F3, $99, $81 ;55
966 .byte $FF, $C3, $99, $99, $C3, $99, $99, $C3 ;56
967 .byte $FF, $C3, $99, $F9, $C1, $99, $99, $C3 ;57
968 .byte $FF, $FF, $E7, $FF, $FF, $E7, $FF, $FF ;58
969 .byte $CF, $E7, $E7, $FF, $FF, $E7, $FF, $FF ;59
970 .byte $FF, $F1, $E7, $CF, $9F, $CF, $E7, $F1 ;60
971 .byte $FF, $FF, $FF, $81, $FF, $81, $FF, $FF ;61
972 .byte $FF, $8F, $E7, $F3, $F9, $F3, $E7, $8F ;62
973 .byte $FF, $E7, $FF, $E7, $F3, $F9, $99, $C3 ;63
976 .byte $FF, $C3, $9D, $9F, $91, $91, $99, $C3 ;0
977 .byte $FF, $99, $99, $99, $81, $99, $C3, $E7 ;1
978 .byte $FF, $83, $99, $99, $83, $99, $99, $83 ;2
979 .byte $FF, $C3, $99, $9F, $9F, $9F, $99, $C3 ;3
980 .byte $FF, $87, $93, $99, $99, $99, $93, $87 ;4
981 .byte $FF, $81, $9F, $9F, $87, $9F, $9F, $81 ;5
982 .byte $FF, $9F, $9F, $9F, $87, $9F, $9F, $81 ;6
983 .byte $FF, $C3, $99, $99, $91, $9F, $99, $C3 ;7
984 .byte $FF, $99, $99, $99, $81, $99, $99, $99 ;8
985 .byte $FF, $C3, $E7, $E7, $E7, $E7, $E7, $C3 ;9
986 .byte $FF, $C7, $93, $F3, $F3, $F3, $F3, $E1 ;10
987 .byte $FF, $99, $93, $87, $8F, $87, $93, $99 ;11
988 .byte $FF, $81, $9F, $9F, $9F, $9F, $9F, $9F ;12
989 .byte $FF, $9C, $9C, $9C, $94, $80, $88, $9C ;13
990 .byte $FF, $99, $99, $91, $81, $81, $89, $99 ;14
991 .byte $FF, $C3, $99, $99, $99, $99, $99, $C3 ;15
992 .byte $FF, $9F, $9F, $9F, $83, $99, $99, $83 ;16
993 .byte $FF, $F1, $C3, $99, $99, $99, $99, $C3 ;17
994 .byte $FF, $99, $93, $87, $83, $99, $99, $83 ;18
995 .byte $FF, $C3, $99, $F9, $C3, $9F, $99, $C3 ;19
996 .byte $FF, $E7, $E7, $E7, $E7, $E7, $E7, $81 ;20
997 .byte $FF, $C3, $99, $99, $99, $99, $99, $99 ;21
998 .byte $FF, $E7, $C3, $99, $99, $99, $99, $99 ;22
999 .byte $FF, $9C, $88, $80, $94, $9C, $9C, $9C ;23
1000 .byte $FF, $99, $99, $C3, $E7, $C3, $99, $99 ;24
1001 .byte $FF, $E7, $E7, $E7, $C3, $99, $99, $99 ;25
1002 .byte $FF, $81, $9F, $CF, $E7, $F3, $F9, $81 ;26
1003 .byte $FF, $C3, $CF, $CF, $CF, $CF, $CF, $C3 ;27
1004 .byte $FF, $03, $9D, $CF, $83, $CF, $ED, $F3 ;28
1005 .byte $FF, $C3, $F3, $F3, $F3, $F3, $F3, $C3 ;29
1006 .byte $E7, $E7, $E7, $E7, $81, $C3, $E7, $FF ;30
1007 .byte $FF, $EF, $CF, $80, $80, $CF, $EF, $FF ;31
1011 .byte $FF, $C3, $9D, $9F, $91, $91, $99, $C3 ;224
1012 .byte $FF, $C1, $99, $C1, $F9, $C3, $FF, $FF ;225
1013 .byte $FF, $83, $99, $99, $83, $9F, $9F, $FF ;226
1014 .byte $FF, $C3, $9F, $9F, $9F, $C3, $FF, $FF ;227
1015 .byte $FF, $C1, $99, $99, $C1, $F9, $F9, $FF ;228
1016 .byte $FF, $C3, $9F, $81, $99, $C3, $FF, $FF ;229
1017 .byte $FF, $E7, $E7, $E7, $C1, $E7, $F1, $FF ;230
1018 .byte $83, $F9, $C1, $99, $99, $C1, $FF, $FF ;231
1019 .byte $FF, $99, $99, $99, $83, $9F, $9F, $FF ;232
1020 .byte $FF, $C3, $E7, $E7, $C7, $FF, $E7, $FF ;233
1021 .byte $C3, $F9, $F9, $F9, $F9, $FF, $F9, $FF ;234
1022 .byte $FF, $99, $93, $87, $93, $9F, $9F, $FF ;235
1023 .byte $FF, $C3, $E7, $E7, $E7, $E7, $C7, $FF ;236
1024 .byte $FF, $9C, $94, $80, $80, $99, $FF, $FF ;237
1025 .byte $FF, $99, $99, $99, $99, $83, $FF, $FF ;238
1026 .byte $FF, $C3, $99, $99, $99, $C3, $FF, $FF ;239
1027 .byte $9F, $9F, $83, $99, $99, $83, $FF, $FF ;240
1028 .byte $F9, $F9, $C1, $99, $99, $C1, $FF, $FF ;241
1029 .byte $FF, $9F, $9F, $9F, $99, $83, $FF, $FF ;242
1030 .byte $FF, $83, $F9, $C3, $9F, $C1, $FF, $FF ;243
1031 .byte $FF, $F1, $E7, $E7, $E7, $81, $E7, $FF ;244
1032 .byte $FF, $C1, $99, $99, $99, $99, $FF, $FF ;245
1033 .byte $FF, $E7, $C3, $99, $99, $99, $FF, $FF ;246
1034 .byte $FF, $C9, $C1, $80, $94, $9C, $FF, $FF ;247
1035 .byte $FF, $99, $C3, $E7, $C3, $99, $FF, $FF ;248
1036 .byte $87, $F3, $C1, $99, $99, $99, $FF, $FF ;249
1037 .byte $FF, $81, $CF, $E7, $F3, $81, $FF, $FF ;250
1038 .byte $FF, $C3, $CF, $CF, $CF, $CF, $CF, $C3 ;251
1039 .byte $FF, $03, $9D, $CF, $83, $CF, $ED, $F3 ;252
1040 .byte $FF, $C3, $F3, $F3, $F3, $F3, $F3, $C3 ;253
1041 .byte $E7, $E7, $E7, $E7, $81, $C3, $E7, $FF ;254
1042 .byte $FF, $EF, $CF, $80, $80, $CF, $EF, $FF ;255