]> git.sur5r.net Git - cc65/blob - libsrc/creativision/joy/creativision-stdjoy.s
Merge remote-tracking branch 'upstream/master' into something_to_pull2
[cc65] / libsrc / creativision / joy / creativision-stdjoy.s
1 ;
2 ; Standard joystick driver for the Creativision.
3 ;
4 ; Christian Groessler, 2017-02-06
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 ; Button state masks (8 values)
31
32         .byte   $10                     ; JOY_UP
33         .byte   $04                     ; JOY_DOWN
34         .byte   $20                     ; JOY_LEFT
35         .byte   $08                     ; JOY_RIGHT
36         .byte   $01                     ; JOY_FIRE (button #1)
37         .byte   $02                     ; JOY_FIRE2 (button #2)
38         .byte   $00                     ; Future expansion
39         .byte   $00                     ; Future expansion
40
41 ; Jump table.
42
43         .addr   INSTALL
44         .addr   UNINSTALL
45         .addr   COUNT
46         .addr   READJOY
47         .addr   0                       ; IRQ entry not used
48
49 ; ------------------------------------------------------------------------
50 ; Constants
51
52 JOY_COUNT       = 2             ; Number of joysticks we support
53
54 ; ------------------------------------------------------------------------
55 ; Code
56
57         .code
58
59 ; ------------------------------------------------------------------------
60 ; INSTALL routine. Is called after the driver is loaded into memory. If
61 ; possible, check if the hardware is present and determine the amount of
62 ; memory available.
63 ; Must return an JOY_ERR_xx code in a/x.
64 ;
65
66 INSTALL:
67         lda     #JOY_ERR_OK
68         ldx     #0
69 ;       rts                     ; Fall through
70
71 ; ------------------------------------------------------------------------
72 ; UNINSTALL routine. Is called before the driver is removed from memory.
73 ; Can do cleanup or whatever. Must not return anything.
74 ;
75
76 UNINSTALL:
77         rts
78
79
80 ; ------------------------------------------------------------------------
81 ; COUNT: Return the total number of available joysticks in a/x.
82 ;
83
84 COUNT:
85         lda     #<JOY_COUNT
86         ldx     #>JOY_COUNT
87         rts
88
89 ; ------------------------------------------------------------------------
90 ; READ: Read a particular joystick passed in A.
91 ;
92
93 READJOY:
94         and     #1              ; fix joystick number
95         bne     READJOY_1       ; read right joystick
96
97 ; Read left joystick
98
99         ldx     ZP_JOY0_DIR
100         lda     ZP_JOY0_BUTTONS
101         jmp     convert         ; convert joystick state to sane cc65 values
102
103 ; Read right joystick
104
105 READJOY_1:
106
107         ldx     ZP_JOY1_DIR
108         lda     ZP_JOY1_BUTTONS
109         lsr     a
110         lsr     a
111         ;jmp    convert         ; convert joystick state to sane cc65 values
112                                 ; fall thru...
113
114 ; ------------------------------------------------------------------------
115 ; convert: make runtime lib compatible values
116 ;       A - buttons
117 ;       X - direction
118 ;
119
120 convert:
121         ldy     #0
122         sty     retval          ; initialize return value
123
124 ; ------
125 ; buttons:
126         ; Port values are for the left hand joystick (right hand joystick
127         ; values were shifted to the right to be identical).
128         ; Why are there two bits indicating a pressed trigger?
129         ; According to the "Second book of programs for the Dick Smith Wizard"
130         ; (pg. 88ff), the left hand fire button gives the value of
131         ; %00010001 and the right hand button gives %00100010
132         ; Why two bits? Am I missing something? Can there be cases that just
133         ; one of those bits is set?
134         ; We just test if any of those two bits is not zero...
135
136         tay
137         and     #%00010001
138         beq     cnv_1
139
140         inc     retval           ; left button pressed
141
142 cnv_1:  tya
143         and     #%00100010
144         beq     cnv_2
145
146         lda     #$02
147         ora     retval
148         sta     retval           ; right button pressed
149
150 ; ------
151 ; direction:
152 cnv_2:  txa
153         ; tested with https://sourceforge.net/projects/creativisionemulator
154         ; $49 - %01001001 - up
155         ; $41 - %01000001 - down
156         ; $4D - %01001101 - left
157         ; $45 - %01000101 - right
158         ;
159         ; are these correct? "Second book of programs for the Dick Smith Wizard" pg. 85 says something different
160         ; ignored for now...
161         ; $85 - %10000101 - up + right
162         ; $8D - %10001101 - down + left
163         ; $89 - %10001001 - up + left
164         ; $85 - %10000101 - down + right (emulator bug?)
165
166         bit     testbit         ; bit #0 set?
167         beq     done            ; no, no direction
168
169         and     #%00001100      ; mask out other bits
170         tax
171         lda     #%00000100      ; init bitmask
172 loop:   dex
173         bmi     done2
174         asl     a
175         bne     loop
176
177 done2:  ora     retval
178         rts
179
180 done:   lda     retval
181         rts
182
183 ; ------------------------------------------------------------------------
184 ;
185         .data
186 testbit:.byte   $01
187
188 ; ------------------------------------------------------------------------
189 ;
190         .bss
191 retval: .res    0