]> git.sur5r.net Git - cc65/blob - libsrc/vic20/joy/vic20-ptvjoy.s
07027c290f9bce12d975ee12065be2a3be18c28b
[cc65] / libsrc / vic20 / joy / vic20-ptvjoy.s
1 ;
2 ; PTV-3 Player joystick driver for the VIC20
3 ;
4 ; Stefan Haubenthal, 2005-05-25
5 ; Groepaz/Hitmen, 2002-12-23
6 ; obviously based on Ullrichs driver :)
7 ; Using code from Steve Schmidtke
8 ;
9
10         .include "zeropage.inc"
11
12         .include "joy-kernel.inc"
13         .include "joy-error.inc"
14         .include "vic20.inc"
15
16 ; ------------------------------------------------------------------------
17 ; Header. Includes jump table
18
19         .segment "HEADER"
20
21 ; Driver signature
22
23         .byte   $6A, $6F, $79   ; "joy"
24         .byte   JOY_API_VERSION ; Driver API version number
25
26 ; Library reference
27
28         .addr   $0000
29
30 ; Button state masks (8 values)
31
32         .byte   $01                     ; JOY_UP
33         .byte   $02                     ; JOY_DOWN
34         .byte   $04                     ; JOY_LEFT
35         .byte   $08                     ; JOY_RIGHT
36         .byte   $10                     ; JOY_FIRE
37         .byte   $00                     ; JOY_FIRE2 unavailable
38         .byte   $00                     ; Future expansion
39         .byte   $00                     ; Future expansion
40
41 ; Jump table.
42
43         .addr   INSTALL
44         .addr   UNINSTALL
45         .addr   COUNT
46         .addr   READ
47         .addr   0                       ; IRQ entry unused
48
49 ; ------------------------------------------------------------------------
50 ; Constants
51
52 VIA1_PRB        := VIA1         ; User port register
53 JOY_COUNT       = 3             ; Number of joysticks we support
54
55
56 .code
57
58 ; ------------------------------------------------------------------------
59 ; INSTALL routine. Is called after the driver is loaded into memory. If
60 ; possible, check if the hardware is present and determine the amount of
61 ; memory available.
62 ; Must return an JOY_ERR_xx code in a/x.
63 ;
64
65 INSTALL:
66         lda     #<JOY_ERR_OK
67         ldx     #>JOY_ERR_OK
68 ;       rts                     ; Run into UNINSTALL instead
69
70 ; ------------------------------------------------------------------------
71 ; UNINSTALL routine. Is called before the driver is removed from memory.
72 ; Can do cleanup or whatever. Must not return anything.
73 ;
74
75 UNINSTALL:
76         rts
77
78
79 ; ------------------------------------------------------------------------
80 ; COUNT: Return the total number of available joysticks in a/x.
81 ;
82
83 COUNT:
84         lda     #<JOY_COUNT
85         ldx     #>JOY_COUNT
86         rts
87
88 ; ------------------------------------------------------------------------
89 ; READ: Read a particular joystick passed in A.
90 ;
91
92 READ:   tax                     ; Joystick number into X
93         bne     joy2
94
95 ; Read joystick 1
96
97 joy1:   lda     #$7F            ; mask for VIA2 JOYBIT: sw3
98         ldx     #$C3            ; mask for VIA1 JOYBITS: sw0,sw1,sw2,sw4
99         sei                     ; necessary?
100
101         ldy     VIA2_DDRB       ; remember the date of DDRB
102         sta     VIA2_DDRB       ; set JOYBITS on this VIA for input
103         lda     VIA2_JOY        ; read JOYBIT: sw3
104         sty     VIA2_DDRB       ; restore the state of DDRB
105         asl                     ; Shift sw3 into carry
106
107         ldy     VIA1_DDRA       ; remember the state of DDRA
108         stx     VIA1_DDRA       ; set JOYBITS on this VIA for input
109         lda     VIA1_JOY        ; read JOYBITS: sw0,sw1,sw2,sw4
110         sty     VIA1_DDRA       ; restore the state of DDRA
111
112         cli                     ; necessary?
113         ror                     ; Shift sw3 into bit 7
114         and     #$9E            ; Mask relevant bits
115         eor     #$9E            ; Active states are inverted
116
117         rts
118
119 ; Read joystick 2
120
121 joy2:   lda     #%10000000      ; via port B Data-Direction
122         sta     VIA1_DDRB       ; bit 7: out    bit 6-0: in
123
124         dex
125         bne     joy3
126
127         lda     #$80            ; via port B read/write
128         sta     VIA1_PRB        ; (output one at PB7)
129
130         lda     VIA1_PRB        ; via port B read/write
131         and     #$1f            ; get bit 4-0 (PB4-PB0)
132         eor     #$1f
133         rts
134
135 ; Read joystick 3
136
137 joy3:   lda     #$00            ; via port B read/write
138         sta     VIA1_PRB        ; (output zero at PB7)
139
140         lda     VIA1_PRB        ; via port B read/write
141         and     #$0f            ; get bit 3-0 (PB3-PB0)
142         sta     tmp1            ; joy 4 directions
143
144         lda     VIA1_PRB        ; via port B read/write
145         and     #%00100000      ; get bit 5 (PB5)
146         lsr
147         ora     tmp1
148         eor     #$1f
149
150         ldx     #0
151         rts
152