]> git.sur5r.net Git - cc65/blob - libsrc/vic20/joy/vic20-ptvjoy.s
Converted unintentionally introduced tabs to spaces.
[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         .macpack module
17
18
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
21
22         module_header   _vic20_ptvjoy_joy
23
24 ; Driver signature
25
26         .byte   $6A, $6F, $79   ; "joy"
27         .byte   JOY_API_VERSION ; Driver API version number
28
29 ; Library reference
30
31         .addr   $0000
32
33 ; Jump table.
34
35         .addr   INSTALL
36         .addr   UNINSTALL
37         .addr   COUNT
38         .addr   READ
39
40 ; ------------------------------------------------------------------------
41 ; Constants
42
43 JOY_COUNT       = 3             ; Number of joysticks we support
44
45
46 .code
47
48 ; ------------------------------------------------------------------------
49 ; INSTALL routine. Is called after the driver is loaded into memory. If
50 ; possible, check if the hardware is present and determine the amount of
51 ; memory available.
52 ; Must return an JOY_ERR_xx code in a/x.
53 ;
54
55 INSTALL:
56         lda     #<JOY_ERR_OK
57         ldx     #>JOY_ERR_OK
58 ;       rts                     ; Run into UNINSTALL instead
59
60 ; ------------------------------------------------------------------------
61 ; UNINSTALL routine. Is called before the driver is removed from memory.
62 ; Can do cleanup or whatever. Must not return anything.
63 ;
64
65 UNINSTALL:
66         rts
67
68
69 ; ------------------------------------------------------------------------
70 ; COUNT: Return the total number of available joysticks in a/x.
71 ;
72
73 COUNT:
74         lda     #<JOY_COUNT
75         ldx     #>JOY_COUNT
76         rts
77
78 ; ------------------------------------------------------------------------
79 ; READ: Read a particular joystick passed in A.
80 ;
81
82 READ:   tax                     ; Joystick number into X
83         bne     joy2
84
85 ; Read joystick 1
86
87 joy1:   lda     #$7F            ; mask for VIA2 JOYBIT: sw3
88         ldx     #$C3            ; mask for VIA1 JOYBITS: sw0,sw1,sw2,sw4
89         sei                     ; necessary?
90
91         ldy     VIA2_DDRB       ; remember the date of DDRB
92         sta     VIA2_DDRB       ; set JOYBITS on this VIA for input
93         lda     VIA2_PB         ; read JOYBIT: sw3
94         sty     VIA2_DDRB       ; restore the state of DDRB
95         asl                     ; Shift sw3 into carry
96
97         ldy     VIA1_DDRA       ; remember the state of DDRA
98         stx     VIA1_DDRA       ; set JOYBITS on this VIA for input
99         lda     VIA1_PA1        ; read JOYBITS: sw0,sw1,sw2,sw4
100         sty     VIA1_DDRA       ; restore the state of DDRA
101
102         cli                     ; necessary?
103         php                     ; Save sw3 in carry
104         lsr                     ; Shift sw0,sw1,sw2,sw4 into bits 1-4
105         tax                     ; Save sw0,sw1,sw2
106         and     #$10            ; Extract sw4 in bit 4
107         sta     tmp1            ; Save sw4 in bit 4
108         txa                     ; Restore sw0,sw1,sw2
109         lsr                     ; Shift sw0,sw1,sw2 into bits 0-2
110         and     #$07            ; Mask bits 0-2
111         plp                     ; Restore sw3 in carry
112         bcc     @L0             ; Is sw3 set?
113         ora     #$08            ; Yes: Add sw3 in bit 3
114 @L0:    ora     tmp1            ; Add sw4 in bit 4
115         eor     #$1F            ; Active states are inverted
116
117         ldx     #0
118         rts
119
120 ; Read joystick 2
121
122 joy2:   lda     #%10000000      ; via port B Data-Direction
123         sta     VIA1_DDRB       ; bit 7: out    bit 6-0: in
124
125         dex
126         bne     joy3
127
128         lda     #$80            ; via port B read/write
129         sta     VIA1_PB         ; (output one at PB7)
130
131         lda     VIA1_PB         ; via port B read/write
132         and     #$1F            ; get bit 4-0 (PB4-PB0)
133         eor     #$1F
134         rts
135
136 ; Read joystick 3
137
138 joy3:   lda     #$00            ; via port B read/write
139         sta     VIA1_PB         ; (output zero at PB7)
140
141         lda     VIA1_PB         ; via port B read/write
142         and     #$0F            ; get bit 3-0 (PB3-PB0)
143         sta     tmp1            ; joy 4 directions
144
145         lda     VIA1_PB         ; via port B read/write
146         and     #%00100000      ; get bit 5 (PB5)
147         lsr
148         ora     tmp1
149         eor     #$1F
150
151         ldx     #0
152         rts