]> git.sur5r.net Git - cc65/blob - libsrc/creativision/joy/creativision-stdjoy.s
Merge branch 'master' into popptr1
[cc65] / libsrc / creativision / joy / creativision-stdjoy.s
1 ;
2 ; Standard joystick driver for the Creativision.
3 ;
4 ; Christian Groessler, 2017-03-08
5 ;
6
7                 .include        "zeropage.inc"
8
9                 .include        "joy-kernel.inc"
10                 .include        "joy-error.inc"
11                 .include        "creativision.inc"
12
13                 .macpack        module
14
15
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
18
19                module_header   _creativisionstd_joy
20
21 ; Driver signature
22
23                 .byte   $6A, $6F, $79           ; "joy"
24                 .byte   JOY_API_VERSION         ; Driver API version number
25
26 ; Library reference
27
28                 .addr   $0000
29
30 ; Jump table.
31
32                 .addr   INSTALL
33                 .addr   UNINSTALL
34                 .addr   COUNT
35                 .addr   READJOY
36
37 ; ------------------------------------------------------------------------
38 ; Constants
39
40 JOY_COUNT       =       2                       ; Number of joysticks we support
41
42 ; Symbolic names for joystick masks (similar names like the defines in joystick.h, but not related to them)
43
44 JOY_UP          =       $10
45 JOY_DOWN        =       $04
46 JOY_LEFT        =       $20
47 JOY_RIGHT       =       $08
48
49 ; ------------------------------------------------------------------------
50 ; Code
51
52                 .code
53
54 ; ------------------------------------------------------------------------
55 ; INSTALL routine. Is called after the driver is loaded into memory. If
56 ; possible, check if the hardware is present and determine the amount of
57 ; memory available.
58 ; Must return an JOY_ERR_xx code in a/x.
59 ;
60
61 INSTALL:        lda     #JOY_ERR_OK
62                 ldx     #0
63 ;               rts                             ; Fall through
64
65 ; ------------------------------------------------------------------------
66 ; UNINSTALL routine. Is called before the driver is removed from memory.
67 ; Can do cleanup or whatever. Must not return anything.
68 ;
69
70 UNINSTALL:      rts
71
72
73 ; ------------------------------------------------------------------------
74 ; COUNT: Return the total number of available joysticks in a/x.
75 ;
76
77 COUNT:          lda     #<JOY_COUNT
78                 ldx     #>JOY_COUNT
79                 rts
80
81 ; ------------------------------------------------------------------------
82 ; READ: Read a particular joystick passed in A.
83 ;
84
85 READJOY:        and     #1                      ; fix joystick number
86                 bne     READJOY_1               ; read right joystick
87
88 ; Read left joystick
89
90                 ldx     ZP_JOY0_DIR
91                 lda     ZP_JOY0_BUTTONS
92                 jmp     convert                 ; convert joystick state to cc65 values
93
94 ; Read right joystick
95
96 READJOY_1:      ldx     ZP_JOY1_DIR
97                 lda     ZP_JOY1_BUTTONS
98                 lsr     a
99                 lsr     a
100                 ;jmp    convert                 ; convert joystick state to cc65 values
101                                                 ; fall thru...
102
103 ; ------------------------------------------------------------------------
104 ; convert: make runtime lib compatible values
105 ;       inputs:
106 ;               A - buttons
107 ;               X - direction
108 ;
109
110 convert:
111
112 ; ------
113 ; buttons:
114 ; Port values are for the left hand joystick (right hand joystick
115 ; values were shifted to the right to be identical).
116 ; Why are there two bits indicating a pressed trigger?
117 ; According to the "Second book of programs for the Dick Smith Wizard"
118 ; (pg. 88ff), the left hand button gives the value of
119 ; %00010001 and the right hand button gives %00100010
120 ; Why two bits? Can there be cases that just one of those bits is set?
121 ; Until these questions have been answered, we only use the lower two
122 ; bits and ignore the upper ones...
123
124                 and     #%00000011              ; button status came in in A, strip high bits
125                 sta     retval                  ; initialize 'retval' with button status
126
127 ; ------
128 ; direction:
129 ; CV has a 16-direction joystick
130 ;
131 ; port values: (compass points)
132 ; N      -  $49 - %01001001
133 ; NNE    -  $48 - %01001000
134 ; NE     -  $47 - %01000111
135 ; ENE    -  $46 - %01000110
136 ; E      -  $45 - %01000101
137 ; ESE    -  $44 - %01000100
138 ; SE     -  $43 - %01000011
139 ; SSE    -  $42 - %01000010
140 ; S      -  $41 - %01000001
141 ; SSW    -  $40 - %01000000
142 ; SW     -  $4F - %01001111
143 ; WSW    -  $4E - %01001110
144 ; W      -  $4D - %01001101
145 ; WNW    -  $4C - %01001100
146 ; NW     -  $4B - %01001011
147 ; NNW    -  $4A - %01001010
148 ; center -  $00 - %00000000
149 ;
150 ; mapping to cc65 definitions (4-direction joystick with 8 possible directions thru combinations)
151 ; N, E, S, W            ->      JOY_UP, JOY_RIGHT, JOY_DOWN, JOY_LEFT
152 ; NE, SE, SW, NW        ->      (JOY_UP | JOY_RIGHT), (JOY_DOWN | JOY_RIGHT), (JOY_DOWN | JOY_LEFT), (JOY_UP | JOY_LEFT)
153 ; NNE, ENE, ESE, SSE, SSW, WSW, WNW, NNW:
154 ;  toggle between straight and diagonal direction for every call, e.g.
155 ;  NNE:
156 ;    call to READJOY:   return JOY_UP | JOY_RIGHT
157 ;    call to READJOY:   return JOY_UP
158 ;    call to READJOY:   return JOY_UP | JOY_RIGHT
159 ;    call to READJOY:   return JOY_UP
160 ;    call to READJOY:   return JOY_UP | JOY_RIGHT
161 ;    etc...
162
163                 txa                             ; move direction status into A
164                 beq     done                    ; center position (no bits are set), nothing to do
165
166                 and     #$0F                    ; get rid of the "$40" bit
167                 bit     bit0                    ; is it a "three letter" direction (NNE, ENE, etc.)?
168                 beq     special                 ; yes (bit #0 is zero)
169
170                 lsr     a                       ; create index into table
171                 tax
172                 lda     dirtable,x
173 done:           ora     retval                  ; include "button" bits
174                 ldx     #0
175                 rts
176
177 ; NNE, ENE, ESE, SSE, SSW, WSW, WNW, NNW
178
179 special:        lsr     a
180                 tax
181
182                 lda     toggler                 ; toggle the toggler
183                 eor     #$01
184                 sta     toggler
185                 bne     spec_1                  ; toggler is 1, use spectable_1 entry
186
187                 lda     spectable_0,x           ; toggler is 0, use spectable_0 entry
188                 bne     done                    ; jump always
189
190 spec_1:         lda     spectable_1,x
191                 bne     done                    ; jump always
192
193 ; ------------------------------------------------------------------------
194 ;
195                 .rodata
196
197                 ; a mapping table of "port values" to "cc65 values"
198                 ; port value had been shifted one bit to the right (range 0..7)
199 dirtable:       .byte   JOY_DOWN                ; S
200                 .byte   JOY_DOWN | JOY_RIGHT    ; SE
201                 .byte   JOY_RIGHT               ; E
202                 .byte   JOY_UP   | JOY_RIGHT    ; NE
203                 .byte   JOY_UP                  ; N
204                 .byte   JOY_UP   | JOY_LEFT     ; NW
205                 .byte   JOY_LEFT                ; W
206                 .byte   JOY_DOWN | JOY_LEFT     ; SW
207
208                 ; two "special" mapping tables for three-letter directions (NNE, etc.)
209 spectable_0:    .byte   JOY_DOWN                ; SSW
210                 .byte   JOY_DOWN                ; SSE
211                 .byte   JOY_RIGHT               ; ESE
212                 .byte   JOY_RIGHT               ; ENE
213                 .byte   JOY_RIGHT               ; NNE
214                 .byte   JOY_UP                  ; NNW
215                 .byte   JOY_LEFT                ; WNW
216                 .byte   JOY_LEFT                ; WSW
217
218 spectable_1:    .byte   JOY_DOWN | JOY_LEFT     ; SSW
219                 .byte   JOY_DOWN | JOY_RIGHT    ; SSE
220                 .byte   JOY_DOWN | JOY_RIGHT    ; ESE
221                 .byte   JOY_UP   | JOY_RIGHT    ; ENE
222                 .byte   JOY_UP   | JOY_RIGHT    ; NNE
223                 .byte   JOY_UP   | JOY_LEFT     ; NNW
224                 .byte   JOY_UP   | JOY_LEFT     ; WNW
225                 .byte   JOY_DOWN | JOY_LEFT     ; WSW
226
227 ; ------------------------------------------------------------------------
228 ;
229 bit0:           .byte   $01
230
231 ; ------------------------------------------------------------------------
232 ;
233                 .bss
234 toggler:        .res    1
235 retval:         .res    1
236
237                 .end