]> git.sur5r.net Git - cc65/blob - libsrc/c64/c64-hitjoy.s
Use get_tv.inc
[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   JOY_API_VERSION ; 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          sty $dd05 ; cia2 timer a highbyte
107          sty $dc05 ; cia1 timer a highbyte
108          iny
109          sty $dd04 ; cia2 timer a lowbyte
110          sty $dc04 ; cia1 timer a lowbyte
111
112          lda #%00010001
113          sta $dd0e ; control register a
114                    ; timer: start
115                    ;        continous
116                    ;        forced load
117                    ; serial port: input
118
119          ; cia 1 setup
120          lda #%01010001
121          sta $dc0e ; control register a
122                    ; timer: start
123                    ;        continous
124                    ;        forced load
125                    ; serial port: output
126
127
128          ; read directions 3
129          lda $dd01 ;read cia 2 port b
130          and #$0f
131          sta temp3
132
133          ; read button 3
134          lda $dd02      ;cia 2 port a
135          and #%11111011 ;data direction
136          sta $dd02      ;=> bit 2 input
137
138          lda $dd00      ;read cia 2 p.A
139          and #%00000100 ;check bit 2
140          asl a
141          asl a
142          ora temp3
143          sta temp3
144
145          ; read directions 4
146          lda $dd01 ;read cia 2 port b
147          lsr a
148          lsr a
149          lsr a
150          lsr a
151          sta temp4
152
153          ; read button 4
154          ldx #$ff ;serial data register
155          stx $dc0c;=> writing $ff causes
156                   ;cia to output some
157                   ;count signals at cnt1
158
159          ldx $dd0c ;read cia 2 serial in
160          beq fire  ;button press if zero
161
162          lda temp4
163          ora #%00010000
164          sta temp4
165
166 fire:
167
168          ; Default Value: $40/64 on PAL
169          ;                $42/66 on NTSC
170          lda #$41
171          sta $dc05
172          ; Default Value: $25/37 on PAL
173          ;                $95/149 on NTSC
174          lda #0
175          sta $dc04
176
177 irqjmp:  jmp $dead
178
179 ; ------------------------------------------------------------------------
180 ; COUNT: Return the total number of available joysticks in a/x.
181 ;
182
183 COUNT:
184         lda     #<JOY_COUNT
185         ldx     #>JOY_COUNT
186         rts
187
188 ; ------------------------------------------------------------------------
189 ; READ: Read a particular joystick passed in A.
190 ;
191
192 READ:
193         tax            ; Joystick number into X
194         bne joy2
195
196          ; Read joystick 1
197 joy1:
198         lda #$7F
199         sei
200         sta CIA1_PRA
201         lda CIA1_PRB
202         cli
203         and #$1F
204         eor #$1F
205         rts
206
207         ; Read joystick 2
208 joy2:
209         dex
210         bne joy3
211
212         ; ldx   #0
213         lda     #$E0
214         ldy     #$FF
215         sei
216         sta     CIA1_DDRA
217         lda     CIA1_PRA
218         sty     CIA1_DDRA
219         cli
220         and     #$1F
221         eor     #$1F
222         rts
223
224         ; Read joystick 3
225
226 joy3:
227         dex
228         bne joy4
229
230         lda temp3
231         eor     #$1F
232         ldx #0
233         rts
234
235         ; Read joystick 4
236
237 joy4:
238         lda temp4
239         eor     #$1F
240         ldx #0
241         rts
242