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 ; So the code is not really based on any algorithms done by somebody.
8 ; I have looked at other routines in the cc65 libs to see what kind of
9 ; entry points we need. And I have looked at the old cc65 libs by
10 ; Bastian Schick to see how things worked in the past.
12 ; This code is written by Karri Kaksonen, 2004 for the cc65 compiler.
15 .include "zeropage.inc"
18 .include "tgi-kernel.inc"
19 .include "tgi-mode.inc"
20 .include "tgi-error.inc"
26 ; ------------------------------------------------------------------------
27 ; Header. Includes jump table and constants.
31 ; First part of the header is a structure that has a magic and defines the
32 ; capabilities of the driver
34 .byte $74, $67, $69 ; "tgi"
35 .byte TGI_API_VERSION ; TGI API version number
36 .word 160 ; X resolution
37 .word 102 ; Y resolution
38 .byte 16 ; Number of drawing colors
39 .byte 2 ; Number of screens available
40 .byte 8 ; System font X size
41 .byte 8 ; System font Y size
42 .res 4, $00 ; Reserved for future extensions
44 ; Next comes the jump table. Currently all entries must be valid and may point
45 ; to an RTS for test versions (function not implemented). A future version may
46 ; allow for emulation: In this case the vector will be zero. Emulation means
47 ; that the graphics kernel will emulate the function by using lower level
48 ; primitives - for example ploting a line by using calls to SETPIXEL.
73 ; ------------------------------------------------------------------------
76 ; Variables mapped to the zero page segment variables. Some of these are
77 ; used for passing parameters to the driver.
89 ; Absolute variables used in the code
93 ERROR: .res 1 ; Error code
95 DRAWINDEX: .res 1 ; Pen to use for drawing
105 BGINDEX: .res 1 ; Pen to use for text background
107 ; Double buffer IRQ stuff
111 text_bitmap: .res 8*(1+20+1)+1
112 ; 8 rows with (one offset-byte plus 20 character bytes plus one fill-byte) plus one 0-offset-byte
114 ; Constants and tables
118 DEFPALETTE: .byte >$000
151 PALETTESIZE = * - DEFPALETTE
155 ; ------------------------------------------------------------------------
156 ; INSTALL routine. Is called after the driver is loaded into memory. May
157 ; initialize anything that has to be done just once. Is probably empty
160 ; Must set an error code: NO
173 ; ------------------------------------------------------------------------
174 ; UNINSTALL routine. Is called before the driver is removed from memory. May
175 ; clean up anything done by INSTALL but is probably empty most of the time.
177 ; Must set an error code: NO
184 ; ------------------------------------------------------------------------
185 ; INIT: Changes an already installed device from text mode to graphics
187 ; Note that INIT/DONE may be called multiple times while the driver
188 ; is loaded, while INSTALL is only called once, so any code that is needed
189 ; to initializes variables and so on must go here. Setting palette and
190 ; clearing the screen is not needed because this is called by the graphics
192 ; The graphics kernel will never call INIT when a graphics mode is already
193 ; active, so there is no need to protect against that.
195 ; Must set an error code: YES
199 ; Done, reset the error code
204 ; ------------------------------------------------------------------------
205 ; DONE: Will be called to switch the graphics device back into text mode.
206 ; The graphics kernel will never call DONE when no graphics mode is active,
207 ; so there is no need to protect against that.
209 ; Must set an error code: NO
215 ; ------------------------------------------------------------------------
216 ; GETERROR: Return the error code in A and clear it.
224 ; ------------------------------------------------------------------------
225 ; CONTROL: Platform/driver specific entry point.
227 ; Must set an error code: YES
229 ; The TGI lacks a way to draw sprites. As that functionality is vital to
230 ; Lynx games we borrow this CONTROL function to implement the still
231 ; missing tgi_draw_sprite funtion. To use this in your C-program
232 ; do a #define tgi_draw_sprite(spr) tgi_ioctl(0, spr)
234 ; To do a flip-screen call tgi_ioctl(1, 0)
236 ; To set the background index for text outputs call tgi_ioctl(2, bgindex)
238 ; To set the frame rate for the display hardware call tgi_ioctl(3, rate)
240 ; To check if the drawing engine is busy with the previous swap you can
241 ; call tgi_ioctl(4, 0). It returns 0 if idle and 1 if busy
243 ; To update displays you can call tgi_ioctl(4, 1) it will wait for the
244 ; next VBL interrupt and swap draw and view buffers.
247 pha ; Almost all control routines succeed
255 lda ptr1 ; Swap request
267 cmp #75 ; Set framerate
276 rate50: lda #$bd ; 50 Hz
279 rate60: lda #$9e ; 60 Hz
282 rate75: lda #$7e ; 75 Hz
291 bne ControlFlipScreen
293 lda ptr1 ; Set text background color
299 bne ControlDrawSprite
301 lda __sprsys ; Flip screen
326 lda ptr1 ; Get the sprite address
329 draw_sprite: ; Draw it in render buffer
348 ; ------------------------------------------------------------------------
349 ; CLEAR: Clears the screen.
351 ; Must set an error code: NO
356 .byte 3,%10000100,%00000000, $0 ; A pixel bitmap
358 .byte %00000001 ; A pixel sprite
369 CLEAR: lda #<cls_sprite
373 ; ------------------------------------------------------------------------
374 ; SETVIEWPAGE: Set the visible page. Called with the new page in A (0..n).
375 ; The page number is already checked to be valid by the graphics kernel.
377 ; Must set an error code: NO (will only be called if page ok)
379 ; It is a good idea to call this function during the vertical blanking
380 ; period. If you call it in the middle of the screen update then half of
381 ; the drawn frame will be from the old buffer and the other half is
382 ; from the new buffer. This is usually noticed by the user.
386 beq @L1 ; page == maxpages-1
394 sty VIEWPAGEL ; Save viewpage for getpixel
397 lda __viddma ; Process flipped displays
412 ; ------------------------------------------------------------------------
413 ; SETDRAWPAGE: Set the drawable page. Called with the new page in A (0..n).
414 ; The page number is already checked to be valid by the graphics kernel.
416 ; Must set an error code: NO (will only be called if page ok)
421 beq @L1 ; page == maxpages-1
433 ; ------------------------------------------------------------------------
434 ; IRQ: VBL interrupt handler
437 TIMER0_INTERRUPT = $01
438 TIMER1_INTERRUPT = $02
439 TIMER2_INTERRUPT = $04
440 TIMER3_INTERRUPT = $08
441 TIMER4_INTERRUPT = $10
442 TIMER5_INTERRUPT = $20
443 TIMER6_INTERRUPT = $40
444 TIMER7_INTERRUPT = $80
446 HBL_INTERRUPT = TIMER0_INTERRUPT
447 VBL_INTERRUPT = TIMER2_INTERRUPT
448 SERIAL_INTERRUPT = TIMER4_INTERRUPT
451 lda INTSET ; Poll all pending interrupts
453 beq IRQEND ; Exit if not a VBL interrupt
469 ; ------------------------------------------------------------------------
470 ; SETCOLOR: Set the drawing color (in A). The new color is already checked
471 ; to be in a valid range (0..maxcolor-1).
473 ; Must set an error code: NO (will only be called if color ok)
480 ; ------------------------------------------------------------------------
481 ; SETPALETTE: Set the palette (not available with all drivers/hardware).
482 ; A pointer to the palette is passed in ptr1. Must set an error if palettes
485 ; Must set an error code: YES
491 sta GCOLMAP,y ; $FDA0
495 ; Done, reset the error code
501 ; ------------------------------------------------------------------------
502 ; GETPALETTE: Return the current palette in A/X. Even drivers that cannot
503 ; set the palette should return the default palette here, so there's no
504 ; way for this function to fail.
506 ; Must set an error code: NO
510 lda #<GCOLMAP ; $FDA0
514 ; ------------------------------------------------------------------------
515 ; GETDEFPALETTE: Return the default palette for the driver in A/X. All
516 ; drivers should return something reasonable here, even drivers that don't
517 ; support palettes, otherwise the caller has no way to determine the colors
518 ; of the (not changeable) palette.
520 ; Must set an error code: NO (all drivers must have a default palette)
528 ; ------------------------------------------------------------------------
529 ; SETPIXEL: Draw one pixel at X1/Y1 = ptr1/ptr2 with the current drawing
530 ; color. The coordinates passed to this function are never outside the
531 ; visible screen area, so there is no need for clipping inside this function.
533 ; Must set an error code: NO
538 .byte %00000001 ; A pixel sprite
560 ; ------------------------------------------------------------------------
561 ; GETPIXEL: Read the color value of a pixel and return it in A/X. The
562 ; coordinates passed to this function are never outside the visible screen
563 ; area, so there is no need for clipping inside this function.
568 sta MATHD ; Hardware multiply
605 ; ------------------------------------------------------------------------
606 ; LINE: Draw a line from X1/Y1 to X2/Y2, where X1/Y1 = ptr1/ptr2 and
607 ; X2/Y2 = ptr3/ptr4 using the current drawing color.
609 ; Must set an error code: NO
614 .byte 0 ; Will be replaced by the code
662 lda #%00000000 ; Not flipped
680 lda #%00010000 ; Vertical flip
695 sta MATHP ; hardware divide
708 bmi @L3 ; wait for math done (bit 7 of sprsys)
728 ; ------------------------------------------------------------------------
729 ; BAR: Draw a filled rectangle with the corners X1/Y1, X2/Y2, where
730 ; X1/Y1 = ptr1/ptr2 and X2/Y2 = ptr3/ptr4 using the current drawing color.
731 ; Contrary to most other functions, the graphics kernel will sort and clip
732 ; the coordinates before calling the driver, so on entry the following
733 ; conditions are valid:
736 ; (X1 >= 0) && (X1 < XRES)
737 ; (X2 >= 0) && (X2 < XRES)
738 ; (Y1 >= 0) && (Y1 < YRES)
739 ; (Y2 >= 0) && (Y2 < YRES)
741 ; Must set an error code: NO
746 .byte %00000001 ; A pixel sprite
777 ; ------------------------------------------------------------------------
778 ; CIRCLE: Draw a circle around the center X1/Y1 (= ptr1/ptr2) with the
779 ; radius in tmp1 and the current drawing color.
781 ; Must set an error code: NO
783 ; There is no sensible way of drawing a circle on a Lynx. As I would
784 ; have to use line elements to do the circle I rather do it in C than
785 ; create it here in the driver.
787 ; To do a circle please add this to your C program
791 ; 3196, // 11.25 degrees
792 ; 6270, // 22.5 degrees
793 ; 9102, // 33.75 degrees
794 ; 11585, // 45 degrees
795 ; 13623, // 56.25 degrees
796 ; 15137, // 67.5 degrees
797 ; 16069, // 78.75 degrees
798 ; 16384 // 90 degrees
815 ;void tgi_Circle(int x0, int y0, unsigned char r)
818 ; int x1, y1, x2, y2;
820 ; x1 = ((long)sin(0) * r + 8192) / 16384 + x0;
821 ; y1 = ((long)sin(8) * r + 8192) / 16384 + y0;
822 ; for (i = 1; i <= 32; i++) {
823 ; x2 = ((long)sin(i) * r + 8192) / 16384 + x0;
824 ; y2 = ((long)sin(i+8) * r + 8192) / 16384 + y0;
825 ; tgi_line(x1, y1, x2, y2);
831 ;#define tgi_circle tgi_Circle
836 ; ------------------------------------------------------------------------
837 ; TEXTSTYLE: Set the style used when calling OUTTEXT. Text scaling in X and Y
838 ; direction is passend in X/Y, the text direction is passed in A.
840 ; Must set an error code: NO
849 ; ------------------------------------------------------------------------
850 ; OUTTEXT: Output text at X/Y = ptr1/ptr2 using the current color and the
851 ; current text style. The text to output is given as a zero terminated
852 ; string with address in ptr3.
854 ; Must set an error code: NO
858 lda TEXTMAGX ; Scale sprite
863 stz text_sprite ; Set normal sprite
867 sta text_sprite ; Set opaque sprite
869 lda DRAWINDEX ; Set color
877 lda X1 ; Set start position
886 ldy #-1 ; Calculate string length
897 rts ; Zero length string
899 iny ; Prepare text_bitmap
903 ldy #8-1 ; 8 pixel lines per character
940 ; and now copy the 8 bytes of that char
946 ; draw char from top to bottom, reading char-data from offset 8-1 to offset 0
949 lda (FONTOFF),y ; *chptr
950 sta text_bitmap,x ;textbuf[y*(1+len+1)+1+x]
986 ; 96 characters from ASCII 32 to 127
987 ; 8 pixels wide, 8 pixels high
988 ; bit value 0 = foreground, bit value 1 = background / transparent
991 .byte $FF, $FF, $FF, $FF, $FF, $FF, $FF, $FF ;32
992 .byte $FF, $E7, $FF, $FF, $E7, $E7, $E7, $E7 ;33
993 .byte $FF, $FF, $FF, $FF, $FF, $99, $99, $99 ;34
994 .byte $FF, $99, $99, $00, $99, $00, $99, $99 ;35
995 .byte $FF, $E7, $83, $F9, $C3, $9F, $C1, $E7 ;36
996 .byte $FF, $B9, $99, $CF, $E7, $F3, $99, $9D ;37
997 .byte $FF, $C0, $99, $98, $C7, $C3, $99, $C3 ;38
998 .byte $FF, $FF, $FF, $FF, $FF, $E7, $F3, $F9 ;39
999 .byte $FF, $F3, $E7, $CF, $CF, $CF, $E7, $F3 ;40
1000 .byte $FF, $CF, $E7, $F3, $F3, $F3, $E7, $CF ;41
1001 .byte $FF, $FF, $99, $C3, $00, $C3, $99, $FF ;42
1002 .byte $FF, $FF, $E7, $E7, $81, $E7, $E7, $FF ;43
1003 .byte $CF, $E7, $E7, $FF, $FF, $FF, $FF, $FF ;44
1004 .byte $FF, $FF, $FF, $FF, $81, $FF, $FF, $FF ;45
1005 .byte $FF, $E7, $E7, $FF, $FF, $FF, $FF, $FF ;46
1006 .byte $FF, $9F, $CF, $E7, $F3, $F9, $FC, $FF ;47
1007 .byte $FF, $C3, $99, $99, $89, $91, $99, $C3 ;48
1008 .byte $FF, $81, $E7, $E7, $E7, $C7, $E7, $E7 ;49
1009 .byte $FF, $81, $9F, $CF, $F3, $F9, $99, $C3 ;50
1010 .byte $FF, $C3, $99, $F9, $E3, $F9, $99, $C3 ;51
1011 .byte $FF, $F9, $F9, $80, $99, $E1, $F1, $F9 ;52
1012 .byte $FF, $C3, $99, $F9, $F9, $83, $9F, $81 ;53
1013 .byte $FF, $C3, $99, $99, $83, $9F, $99, $C3 ;54
1014 .byte $FF, $E7, $E7, $E7, $E7, $F3, $99, $81 ;55
1015 .byte $FF, $C3, $99, $99, $C3, $99, $99, $C3 ;56
1016 .byte $FF, $C3, $99, $F9, $C1, $99, $99, $C3 ;57
1017 .byte $FF, $FF, $E7, $FF, $FF, $E7, $FF, $FF ;58
1018 .byte $CF, $E7, $E7, $FF, $FF, $E7, $FF, $FF ;59
1019 .byte $FF, $F1, $E7, $CF, $9F, $CF, $E7, $F1 ;60
1020 .byte $FF, $FF, $FF, $81, $FF, $81, $FF, $FF ;61
1021 .byte $FF, $8F, $E7, $F3, $F9, $F3, $E7, $8F ;62
1022 .byte $FF, $E7, $FF, $E7, $F3, $F9, $99, $C3 ;63
1025 .byte $FF, $C3, $9D, $9F, $91, $91, $99, $C3 ;0
1026 .byte $FF, $99, $99, $99, $81, $99, $C3, $E7 ;1
1027 .byte $FF, $83, $99, $99, $83, $99, $99, $83 ;2
1028 .byte $FF, $C3, $99, $9F, $9F, $9F, $99, $C3 ;3
1029 .byte $FF, $87, $93, $99, $99, $99, $93, $87 ;4
1030 .byte $FF, $81, $9F, $9F, $87, $9F, $9F, $81 ;5
1031 .byte $FF, $9F, $9F, $9F, $87, $9F, $9F, $81 ;6
1032 .byte $FF, $C3, $99, $99, $91, $9F, $99, $C3 ;7
1033 .byte $FF, $99, $99, $99, $81, $99, $99, $99 ;8
1034 .byte $FF, $C3, $E7, $E7, $E7, $E7, $E7, $C3 ;9
1035 .byte $FF, $C7, $93, $F3, $F3, $F3, $F3, $E1 ;10
1036 .byte $FF, $99, $93, $87, $8F, $87, $93, $99 ;11
1037 .byte $FF, $81, $9F, $9F, $9F, $9F, $9F, $9F ;12
1038 .byte $FF, $9C, $9C, $9C, $94, $80, $88, $9C ;13
1039 .byte $FF, $99, $99, $91, $81, $81, $89, $99 ;14
1040 .byte $FF, $C3, $99, $99, $99, $99, $99, $C3 ;15
1041 .byte $FF, $9F, $9F, $9F, $83, $99, $99, $83 ;16
1042 .byte $FF, $F1, $C3, $99, $99, $99, $99, $C3 ;17
1043 .byte $FF, $99, $93, $87, $83, $99, $99, $83 ;18
1044 .byte $FF, $C3, $99, $F9, $C3, $9F, $99, $C3 ;19
1045 .byte $FF, $E7, $E7, $E7, $E7, $E7, $E7, $81 ;20
1046 .byte $FF, $C3, $99, $99, $99, $99, $99, $99 ;21
1047 .byte $FF, $E7, $C3, $99, $99, $99, $99, $99 ;22
1048 .byte $FF, $9C, $88, $80, $94, $9C, $9C, $9C ;23
1049 .byte $FF, $99, $99, $C3, $E7, $C3, $99, $99 ;24
1050 .byte $FF, $E7, $E7, $E7, $C3, $99, $99, $99 ;25
1051 .byte $FF, $81, $9F, $CF, $E7, $F3, $F9, $81 ;26
1052 .byte $FF, $C3, $CF, $CF, $CF, $CF, $CF, $C3 ;27
1053 .byte $FF, $03, $9D, $CF, $83, $CF, $ED, $F3 ;28
1054 .byte $FF, $C3, $F3, $F3, $F3, $F3, $F3, $C3 ;29
1055 .byte $E7, $E7, $E7, $E7, $81, $C3, $E7, $FF ;30
1056 .byte $FF, $EF, $CF, $80, $80, $CF, $EF, $FF ;31
1060 .byte $FF, $C3, $9D, $9F, $91, $91, $99, $C3 ;224
1061 .byte $FF, $C1, $99, $C1, $F9, $C3, $FF, $FF ;225
1062 .byte $FF, $83, $99, $99, $83, $9F, $9F, $FF ;226
1063 .byte $FF, $C3, $9F, $9F, $9F, $C3, $FF, $FF ;227
1064 .byte $FF, $C1, $99, $99, $C1, $F9, $F9, $FF ;228
1065 .byte $FF, $C3, $9F, $81, $99, $C3, $FF, $FF ;229
1066 .byte $FF, $E7, $E7, $E7, $C1, $E7, $F1, $FF ;230
1067 .byte $83, $F9, $C1, $99, $99, $C1, $FF, $FF ;231
1068 .byte $FF, $99, $99, $99, $83, $9F, $9F, $FF ;232
1069 .byte $FF, $C3, $E7, $E7, $C7, $FF, $E7, $FF ;233
1070 .byte $C3, $F9, $F9, $F9, $F9, $FF, $F9, $FF ;234
1071 .byte $FF, $99, $93, $87, $93, $9F, $9F, $FF ;235
1072 .byte $FF, $C3, $E7, $E7, $E7, $E7, $C7, $FF ;236
1073 .byte $FF, $9C, $94, $80, $80, $99, $FF, $FF ;237
1074 .byte $FF, $99, $99, $99, $99, $83, $FF, $FF ;238
1075 .byte $FF, $C3, $99, $99, $99, $C3, $FF, $FF ;239
1076 .byte $9F, $9F, $83, $99, $99, $83, $FF, $FF ;240
1077 .byte $F9, $F9, $C1, $99, $99, $C1, $FF, $FF ;241
1078 .byte $FF, $9F, $9F, $9F, $99, $83, $FF, $FF ;242
1079 .byte $FF, $83, $F9, $C3, $9F, $C1, $FF, $FF ;243
1080 .byte $FF, $F1, $E7, $E7, $E7, $81, $E7, $FF ;244
1081 .byte $FF, $C1, $99, $99, $99, $99, $FF, $FF ;245
1082 .byte $FF, $E7, $C3, $99, $99, $99, $FF, $FF ;246
1083 .byte $FF, $C9, $C1, $80, $94, $9C, $FF, $FF ;247
1084 .byte $FF, $99, $C3, $E7, $C3, $99, $FF, $FF ;248
1085 .byte $87, $F3, $C1, $99, $99, $99, $FF, $FF ;249
1086 .byte $FF, $81, $CF, $E7, $F3, $81, $FF, $FF ;250
1087 .byte $FF, $C3, $CF, $CF, $CF, $CF, $CF, $C3 ;251
1088 .byte $FF, $03, $9D, $CF, $83, $CF, $ED, $F3 ;252
1089 .byte $FF, $C3, $F3, $F3, $F3, $F3, $F3, $C3 ;253
1090 .byte $E7, $E7, $E7, $E7, $81, $C3, $E7, $FF ;254
1091 .byte $FF, $EF, $CF, $80, $80, $CF, $EF, $FF ;255