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