]> git.sur5r.net Git - cc65/blob - libsrc/c64/joy/c64-hitjoy.s
Added library reference joy_libref to JOY interface.
[cc65] / libsrc / c64 / joy / c64-hitjoy.s
1 ;
2 ; DXS/HIT-4 Player joystick driver for the C64
3 ;
4 ; Groepaz/Hitmen, 2002-12-23
5 ; obviously based on Ullrichs driver :)
6 ;
7
8         .include "zeropage.inc"
9
10         .include "joy-kernel.inc"
11         .include "joy-error.inc"
12         .include "c64.inc"
13
14         .macpack generic
15
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
18
19         .segment "JUMPTABLE"
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   $01                     ; JOY_UP
33         .byte   $02                     ; JOY_DOWN
34         .byte   $04                     ; JOY_LEFT
35         .byte   $08                     ; JOY_RIGHT
36         .byte   $10                     ; JOY_FIRE
37         .byte   $00                     ; JOY_FIRE2 unavailable
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   READ
47         .addr   IRQ
48
49 ; ------------------------------------------------------------------------
50 ; Constants
51
52 JOY_COUNT       = 4             ; Number of joysticks we support
53
54 ; ------------------------------------------------------------------------
55 ; Data. Written in the IRQ, read by the READ routine
56
57 .bss
58
59 temp3:  .byte 0
60 temp4:  .byte 0
61
62 .code
63
64 ; ------------------------------------------------------------------------
65 ; INSTALL routine. Is called after the driver is loaded into memory. If
66 ; possible, check if the hardware is present and determine the amount of
67 ; memory available.
68 ; Must return an JOY_ERR_xx code in a/x.
69 ;
70
71 INSTALL:
72         lda     #<JOY_ERR_OK
73         ldx     #>JOY_ERR_OK
74
75 ;       rts             ; Run into UNINSTALL instead
76
77 ; ------------------------------------------------------------------------
78 ; UNINSTALL routine. Is called before the driver is removed from memory.
79 ; Can do cleanup or whatever. Must not return anything.
80 ;
81
82 UNINSTALL:
83         rts
84
85 ; ------------------------------------------------------------------------
86 ; IRQ entry point. Is called from the C layer as a subroutine in the
87 ; interrupt. The routine MUST return carry set if the interrupt has been
88 ; 'handled' - which means that the interrupt source is gone. Otherwise it
89 ; MUST return carry clear.
90
91 IRQ:    ; cia 2 setup
92
93         ldy     #$00            ; port b direction
94         sty     $dd03           ; => input
95
96         sty     $dd05           ; cia2 timer a highbyte
97         sty     $dc05           ; cia1 timer a highbyte
98         iny
99         sty     $dd04           ; cia2 timer a lowbyte
100         sty     $dc04           ; cia1 timer a lowbyte
101
102         lda     #%00010001
103         sta     $dd0e           ; control register a
104                                 ; timer: start
105                                 ;        continous
106                                 ;        forced load
107                                 ; serial port: input
108
109         ; cia 1 setup
110         lda     #%01010001
111         sta     $dc0e           ; control register a
112                                 ; timer: start
113                                 ;        continous
114                                 ;        forced load
115                                 ; serial port: output
116
117         ; read directions 3
118         lda     $dd01           ;read cia 2 port b
119         and     #$0f
120         sta     temp3
121
122         ; read button 3
123         lda     $dd02           ;cia 2 port a
124         and     #%11111011      ;data direction
125         sta     $dd02           ;=> bit 2 input
126
127         lda     $dd00           ;read cia 2 p.A
128         and     #%00000100      ;check bit 2
129         asl     a
130         asl     a
131         ora     temp3
132         sta     temp3
133
134         ; read directions 4
135         lda     $dd01           ;read cia 2 port b
136         lsr     a
137         lsr     a
138         lsr     a
139         lsr     a
140         sta     temp4
141
142         ; read button 4
143         ldx     #$ff            ;serial data register
144         stx     $dc0c           ;=> writing $ff causes
145                                 ;cia to output some
146                                 ;count signals at cnt1
147
148         ldx     $dd0c           ;read cia 2 serial in
149         beq     fire            ;button press if zero
150
151         lda     temp4
152         ora     #%00010000
153         sta     temp4
154
155 fire:
156         ; Default Value: $40/64 on PAL
157         ;                    $42/66 on NTSC
158         lda     #$41
159         sta     $dc05
160         ; Default Value: $25/37 on PAL
161         ;                    $95/149 on NTSC
162         lda     #0
163         sta     $dc04
164
165         ; We do never "handle" the interrupt, we use it just as a timer.
166         clc
167         rts
168
169 ; ------------------------------------------------------------------------
170 ; COUNT: Return the total number of available joysticks in a/x.
171 ;
172
173 COUNT:  lda     #<JOY_COUNT
174         ldx     #>JOY_COUNT
175         rts
176
177 ; ------------------------------------------------------------------------
178 ; READ: Read a particular joystick passed in A.
179 ;
180
181 READ:   tax            ; Joystick number into X
182         bne joy2
183
184 ; Read joystick 1
185
186 joy1:   lda #$7F
187         sei
188         sta CIA1_PRA
189         lda CIA1_PRB
190         cli
191         and #$1F
192         eor #$1F
193         rts
194
195 ; Read joystick 2
196
197 joy2:   dex
198         bne joy3
199
200         ; ldx   #0
201         lda     #$E0
202         ldy     #$FF
203         sei
204         sta     CIA1_DDRA
205         lda     CIA1_PRA
206         sty     CIA1_DDRA
207         cli
208         and     #$1F
209         eor     #$1F
210         rts
211
212         ; Read joystick 3
213
214 joy3:   dex
215         bne     joy4
216
217         lda     temp3
218         eor     #$1F
219         rts
220
221         ; Read joystick 4
222
223 joy4:   lda     temp4
224         eor     #$1F
225         ldx     #0
226         rts
227