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