]> git.sur5r.net Git - cc65/blob - libsrc/c64/joy/c64-hitjoy.s
Fixed _textcolor definition.
[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
39 ; ------------------------------------------------------------------------
40 ; Constants
41
42 JOY_COUNT       = 4             ; Number of joysticks we support
43
44 ; ------------------------------------------------------------------------
45 ; Data. Written in the IRQ, read by the READ routine
46
47 .bss
48
49 temp3:  .byte 0
50 temp4:  .byte 0
51
52 .code
53
54 ; ------------------------------------------------------------------------
55 ; INSTALL routine. Is called after the driver is loaded into memory. If
56 ; possible, check if the hardware is present and determine the amount of
57 ; memory available.
58 ; Must return an JOY_ERR_xx code in a/x.
59 ;
60
61 INSTALL:
62         lda     #<JOY_ERR_OK
63         ldx     #>JOY_ERR_OK
64
65 ;       rts             ; Run into UNINSTALL instead
66
67 ; ------------------------------------------------------------------------
68 ; UNINSTALL routine. Is called before the driver is removed from memory.
69 ; Can do cleanup or whatever. Must not return anything.
70 ;
71
72 UNINSTALL:
73         rts
74
75 ; ------------------------------------------------------------------------
76 ; COUNT: Return the total number of available joysticks in a/x.
77 ;
78
79 COUNT:  lda     #<JOY_COUNT
80         ldx     #>JOY_COUNT
81         rts
82
83 ; ------------------------------------------------------------------------
84 ; READ: Read a particular joystick passed in A.
85 ;
86
87 readadapter:
88
89         sei
90
91         ; cia 2 setup
92         ldy     #$00            ; port b direction
93         sty     $dd03           ; => input
94
95         sty     $dd05           ; cia2 timer a highbyte
96         sty     $dc05           ; cia1 timer a highbyte
97         iny
98         sty     $dd04           ; cia2 timer a lowbyte
99         sty     $dc04           ; cia1 timer a lowbyte
100
101         lda     #%00010001
102         sta     $dd0e           ; control register a
103                                 ; timer: start
104                                 ;        continous
105                                 ;        forced load
106                                 ; serial port: input
107
108         ; cia 1 setup
109         lda     #%01010001
110         sta     $dc0e           ; control register a
111                                 ; timer: start
112                                 ;        continous
113                                 ;        forced load
114                                 ; serial port: output
115
116         ; read directions 3
117         lda     $dd01           ;read cia 2 port b
118         and     #$0f
119         sta     temp3
120
121         ; read button 3
122         lda     $dd02           ;cia 2 port a
123         and     #%11111011      ;data direction
124         sta     $dd02           ;=> bit 2 input
125
126         lda     $dd00           ;read cia 2 p.A
127         and     #%00000100      ;check bit 2
128         asl     a
129         asl     a
130         ora     temp3
131         sta     temp3
132
133         ; read directions 4
134         lda     $dd01           ;read cia 2 port b
135         lsr     a
136         lsr     a
137         lsr     a
138         lsr     a
139         sta     temp4
140
141         ; read button 4
142         ldx     #$ff            ;serial data register
143         stx     $dc0c           ;=> writing $ff causes
144                                 ;cia to output some
145                                 ;count signals at cnt1
146
147         ldx     $dd0c           ;read cia 2 serial in
148         beq     fire            ;button press if zero
149
150         lda     temp4
151         ora     #%00010000
152         sta     temp4
153
154 fire:
155         ; FIXME: to be really 100% correct this should restore the correct timer
156         ;        values for the respective machine (PAL: $4025, NTSC: $4295)
157         ;        however, this should hardly be a problem in a real world program
158
159         lda     #$41
160         sta     $dc05
161         lda     #0
162         sta     $dc04
163
164         cli
165         rts
166
167 READ:
168         pha
169         jsr readadapter
170         pla
171
172         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