]> git.sur5r.net Git - cc65/blob - libsrc/c64/joy/c64-hitjoy.s
Merge branch 'master' of https://github.com/jedeoric/cc65
[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         .macpack module
16
17
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
20
21         module_header   _c64_hitjoy_joy
22
23 ; Driver signature
24
25         .byte   $6A, $6F, $79   ; "joy"
26         .byte   JOY_API_VERSION ; Driver API version number
27
28 ; Library reference
29
30         .addr   $0000
31
32 ; Jump table.
33
34         .addr   INSTALL
35         .addr   UNINSTALL
36         .addr   COUNT
37         .addr   READ
38         .addr   IRQ
39
40 ; ------------------------------------------------------------------------
41 ; Constants
42
43 JOY_COUNT       = 4             ; Number of joysticks we support
44
45 ; ------------------------------------------------------------------------
46 ; Data. Written in the IRQ, read by the READ routine
47
48 .bss
49
50 temp3:  .byte 0
51 temp4:  .byte 0
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:
63         lda     #<JOY_ERR_OK
64         ldx     #>JOY_ERR_OK
65
66 ;       rts             ; Run into UNINSTALL instead
67
68 ; ------------------------------------------------------------------------
69 ; UNINSTALL routine. Is called before the driver is removed from memory.
70 ; Can do cleanup or whatever. Must not return anything.
71 ;
72
73 UNINSTALL:
74         rts
75
76 ; ------------------------------------------------------------------------
77 ; IRQ entry point. Is called from the C layer as a subroutine in the
78 ; interrupt. The routine MUST return carry set if the interrupt has been
79 ; 'handled' - which means that the interrupt source is gone. Otherwise it
80 ; MUST return carry clear.
81
82 IRQ:    ; cia 2 setup
83
84         ldy     #$00            ; port b direction
85         sty     $dd03           ; => input
86
87         sty     $dd05           ; cia2 timer a highbyte
88         sty     $dc05           ; cia1 timer a highbyte
89         iny
90         sty     $dd04           ; cia2 timer a lowbyte
91         sty     $dc04           ; cia1 timer a lowbyte
92
93         lda     #%00010001
94         sta     $dd0e           ; control register a
95                                 ; timer: start
96                                 ;        continous
97                                 ;        forced load
98                                 ; serial port: input
99
100         ; cia 1 setup
101         lda     #%01010001
102         sta     $dc0e           ; control register a
103                                 ; timer: start
104                                 ;        continous
105                                 ;        forced load
106                                 ; serial port: output
107
108         ; read directions 3
109         lda     $dd01           ;read cia 2 port b
110         and     #$0f
111         sta     temp3
112
113         ; read button 3
114         lda     $dd02           ;cia 2 port a
115         and     #%11111011      ;data direction
116         sta     $dd02           ;=> bit 2 input
117
118         lda     $dd00           ;read cia 2 p.A
119         and     #%00000100      ;check bit 2
120         asl     a
121         asl     a
122         ora     temp3
123         sta     temp3
124
125         ; read directions 4
126         lda     $dd01           ;read cia 2 port b
127         lsr     a
128         lsr     a
129         lsr     a
130         lsr     a
131         sta     temp4
132
133         ; read button 4
134         ldx     #$ff            ;serial data register
135         stx     $dc0c           ;=> writing $ff causes
136                                 ;cia to output some
137                                 ;count signals at cnt1
138
139         ldx     $dd0c           ;read cia 2 serial in
140         beq     fire            ;button press if zero
141
142         lda     temp4
143         ora     #%00010000
144         sta     temp4
145
146 fire:
147         ; Default Value: $40/64 on PAL
148         ;                    $42/66 on NTSC
149         lda     #$41
150         sta     $dc05
151         ; Default Value: $25/37 on PAL
152         ;                    $95/149 on NTSC
153         lda     #0
154         sta     $dc04
155
156         ; We do never "handle" the interrupt, we use it just as a timer.
157         clc
158         rts
159
160 ; ------------------------------------------------------------------------
161 ; COUNT: Return the total number of available joysticks in a/x.
162 ;
163
164 COUNT:  lda     #<JOY_COUNT
165         ldx     #>JOY_COUNT
166         rts
167
168 ; ------------------------------------------------------------------------
169 ; READ: Read a particular joystick passed in A.
170 ;
171
172 READ:   tax            ; Joystick number into X
173         bne joy2
174
175 ; Read joystick 1
176
177 joy1:   lda #$7F
178         sei
179         sta CIA1_PRA
180         lda CIA1_PRB
181         cli
182         and #$1F
183         eor #$1F
184         rts
185
186 ; Read joystick 2
187
188 joy2:   dex
189         bne joy3
190
191         ; ldx   #0
192         lda     #$E0
193         ldy     #$FF
194         sei
195         sta     CIA1_DDRA
196         lda     CIA1_PRA
197         sty     CIA1_DDRA
198         cli
199         and     #$1F
200         eor     #$1F
201         rts
202
203         ; Read joystick 3
204
205 joy3:   dex
206         bne     joy4
207
208         lda     temp3
209         eor     #$1F
210         rts
211
212         ; Read joystick 4
213
214 joy4:   lda     temp4
215         eor     #$1F
216         ldx     #0
217         rts
218