]> git.sur5r.net Git - cc65/blob - libsrc/c64/c64-hitjoy.s
Added two more joystick drivers from groepaz
[cc65] / libsrc / c64 / 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   $00             ; Driver API version number
25
26 ; Button state masks (8 values)
27
28         .byte   $01                     ; JOY_UP
29         .byte   $02                     ; JOY_DOWN
30         .byte   $04                     ; JOY_LEFT
31         .byte   $08                     ; JOY_RIGHT
32         .byte   $10                     ; JOY_FIRE
33         .byte   $00                     ; Future expansion
34         .byte   $00                     ; Future expansion
35         .byte   $00                     ; Future expansion
36
37 ; Jump table.
38
39         .word   INSTALL
40         .word   DEINSTALL
41         .word   COUNT
42         .word   READ
43
44 ; ------------------------------------------------------------------------
45 ; Constants
46
47 JOY_COUNT       = 4             ; Number of joysticks we support
48
49 ; ------------------------------------------------------------------------
50 ; Data. Written in the IRQ, read by the READ routine
51
52 .bss
53
54 temp3:  .byte 0
55 temp4:  .byte 0
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         sei
68         lda $0314
69         sta irqjmp+1
70         lda $0315
71         sta irqjmp+2
72         lda #<pollirq
73         sta $0314
74         lda #>pollirq
75         sta $0315
76         cli
77
78         lda     #<JOY_ERR_OK
79         ldx     #>JOY_ERR_OK
80
81         rts
82
83 ; ------------------------------------------------------------------------
84 ; DEINSTALL routine. Is called before the driver is removed from memory.
85 ; Can do cleanup or whatever. Must not return anything.
86 ;
87
88 DEINSTALL:
89         sei
90         lda irqjmp+1
91         sta $0314
92         lda irqjmp+2
93         sta $0315
94         cli
95         rts
96
97 ; ------------------------------------------------------------------------
98 ; we must use an irq here since we need timers
99 ; which otherwhise would conflict with system-irq
100 pollirq:
101          ; cia 2 setup
102
103          ldy #$00  ; port b direction
104          sty $dd03 ; => input
105
106 ;         ldy #$00
107          sty $dd05 ; cia2 timer a highbyte
108          sty $dc05 ; cia1 timer a highbyte
109          iny
110 ;         lda #$01
111          sty $dd04 ; cia2 timer a lowbyte
112          sty $dc04 ; cia1 timer a lowbyte
113
114          lda #%00010001
115          sta $dd0e ; control register a
116                    ; timer: start
117                    ;        continous
118                    ;        forced load
119                    ; serial port: input
120
121          ; cia 1 setup
122
123 ;         lda #%00000001
124 ;         sty $dc0d ; irq ctrl reg
125
126          lda #%01010001
127          sta $dc0e ; control register a
128                    ; timer: start
129                    ;        continous
130                    ;        forced load
131                    ; serial port: output
132
133
134          ; read directions 3
135
136          lda $dd01 ;read cia 2 port b
137          and #$0f
138          sta temp3
139
140          ; read button 3
141
142          lda $dd02      ;cia 2 port a
143          and #%11111011 ;data direction
144          sta $dd02      ;=> bit 2 input
145
146          lda $dd00      ;read cia 2 p.A
147          and #%00000100 ;check bit 2
148          asl a
149          asl a
150          ora temp3
151          sta temp3
152
153          ; read directions 4
154
155          lda $dd01 ;read cia 2 port b
156          lsr a
157          lsr a
158          lsr a
159          lsr a
160          sta temp4
161
162          ; read button 4
163
164          ldx #$ff ;serial data register
165          stx $dc0c;=> writing $ff causes
166                   ;cia to output some
167                   ;count signals at cnt1
168
169          ldx $dd0c ;read cia 2 serial in
170          beq fire  ;button press if zero
171
172          lda temp4
173          ora #%00010000
174          sta temp4
175
176 fire:
177
178          ; Default Value: $40/64 on PAL
179          ;                $42/66 on NTSC
180          lda #$41
181          sta $dc05
182          ; Default Value: $25/37 on PAL
183          ;                $95/149 on NTSC
184          lda #0
185          sta $dc04
186
187 irqjmp:  jmp $dead
188
189 ; ------------------------------------------------------------------------
190 ; COUNT: Return the total number of available joysticks in a/x.
191 ;
192
193 COUNT:
194         lda     #<JOY_COUNT
195         ldx     #>JOY_COUNT
196         rts
197
198 ; ------------------------------------------------------------------------
199 ; READ: Read a particular joystick passed in A.
200 ;
201
202 READ:
203         tax            ; Joystick number into X
204         bne joy2
205
206          ; Read joystick 1
207 joy1:
208         lda #$7F
209         sei
210         sta CIA1_PRA
211         lda CIA1_PRB
212         cli
213         and #$1F
214         eor #$1F
215         rts
216
217         ; Read joystick 2
218 joy2:
219         dex
220         bne joy3
221
222         ; ldx   #0
223         lda     #$E0
224         ldy     #$FF
225         sei
226         sta     CIA1_DDRA
227         lda     CIA1_PRA
228         sty     CIA1_DDRA
229         cli
230         and     #$1F
231         eor     #$1F
232         rts
233
234         ; Read joystick 3
235
236 joy3:
237         dex
238         bne joy4
239
240         lda temp3
241         eor     #$1F
242         ldx #0
243         rts
244
245         ; Read joystick 4
246
247 joy4:
248         lda temp4
249         eor     #$1F
250         ldx #0
251         rts
252