]> git.sur5r.net Git - cc65/blob - libsrc/vic20/joy/vic20-ptvjoy.s
Replaced whole bunch for Makefiles with a single generic Makefile.
[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 "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                     ; JOY_FIRE2 unavailable
34         .byte   $00                     ; Future expansion
35         .byte   $00                     ; Future expansion
36
37 ; Jump table.
38
39         .addr   INSTALL
40         .addr   UNINSTALL
41         .addr   COUNT
42         .addr   READ
43         .addr   0                       ; IRQ entry unused
44
45 ; ------------------------------------------------------------------------
46 ; Constants
47
48 VIA1_PRB        := VIA1         ; User port register
49 JOY_COUNT       = 3             ; Number of joysticks we support
50
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 ;       rts                     ; Run into UNINSTALL instead
65
66 ; ------------------------------------------------------------------------
67 ; UNINSTALL routine. Is called before the driver is removed from memory.
68 ; Can do cleanup or whatever. Must not return anything.
69 ;
70
71 UNINSTALL:
72         rts
73
74
75 ; ------------------------------------------------------------------------
76 ; COUNT: Return the total number of available joysticks in a/x.
77 ;
78
79 COUNT:
80         lda     #<JOY_COUNT
81         ldx     #>JOY_COUNT
82         rts
83
84 ; ------------------------------------------------------------------------
85 ; READ: Read a particular joystick passed in A.
86 ;
87
88 READ:   tax                     ; Joystick number into X
89         bne     joy2
90
91 ; Read joystick 1
92
93 joy1:   lda     #$7F            ; mask for VIA2 JOYBIT: sw3
94         ldx     #$C3            ; mask for VIA1 JOYBITS: sw0,sw1,sw2,sw4
95         sei                     ; necessary?
96
97         ldy     VIA2_DDRB       ; remember the date of DDRB
98         sta     VIA2_DDRB       ; set JOYBITS on this VIA for input
99         lda     VIA2_JOY        ; read JOYBIT: sw3
100         sty     VIA2_DDRB       ; restore the state of DDRB
101         asl                     ; Shift sw3 into carry
102
103         ldy     VIA1_DDRA       ; remember the state of DDRA
104         stx     VIA1_DDRA       ; set JOYBITS on this VIA for input
105         lda     VIA1_JOY        ; read JOYBITS: sw0,sw1,sw2,sw4
106         sty     VIA1_DDRA       ; restore the state of DDRA
107
108         cli                     ; necessary?
109         ror                     ; Shift sw3 into bit 7
110         and     #$9E            ; Mask relevant bits
111         eor     #$9E            ; Active states are inverted
112
113         rts
114
115 ; Read joystick 2
116
117 joy2:   lda     #%10000000      ; via port B Data-Direction
118         sta     VIA1_DDRB       ; bit 7: out    bit 6-0: in
119
120         dex
121         bne     joy3
122
123         lda     #$80            ; via port B read/write
124         sta     VIA1_PRB        ; (output one at PB7)
125
126         lda     VIA1_PRB        ; via port B read/write
127         and     #$1f            ; get bit 4-0 (PB4-PB0)
128         eor     #$1f
129         rts
130
131 ; Read joystick 3
132
133 joy3:   lda     #$00            ; via port B read/write
134         sta     VIA1_PRB        ; (output zero at PB7)
135
136         lda     VIA1_PRB        ; via port B read/write
137         and     #$0f            ; get bit 3-0 (PB3-PB0)
138         sta     tmp1            ; joy 4 directions
139
140         lda     VIA1_PRB        ; via port B read/write
141         and     #%00100000      ; get bit 5 (PB5)
142         lsr
143         ora     tmp1
144         eor     #$1f
145
146         ldx     #0
147         rts
148