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