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