2 ; PTV-3 Player joystick driver for the VIC20
4 ; Stefan Haubenthal, 2005-05-25
5 ; Groepaz/Hitmen, 2002-12-23
6 ; obviously based on Ullrichs driver :)
7 ; Using code from Steve Schmidtke
10 .include "zeropage.inc"
12 .include "joy-kernel.inc"
13 .include "joy-error.inc"
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
22 module_header _vic20_ptvjoy_joy
26 .byte $6A, $6F, $79 ; "joy"
27 .byte JOY_API_VERSION ; Driver API version number
40 ; ------------------------------------------------------------------------
43 VIA1_PRB := VIA1 ; User port register
44 JOY_COUNT = 3 ; Number of joysticks we support
49 ; ------------------------------------------------------------------------
50 ; INSTALL routine. Is called after the driver is loaded into memory. If
51 ; possible, check if the hardware is present and determine the amount of
53 ; Must return an JOY_ERR_xx code in a/x.
59 ; rts ; Run into UNINSTALL instead
61 ; ------------------------------------------------------------------------
62 ; UNINSTALL routine. Is called before the driver is removed from memory.
63 ; Can do cleanup or whatever. Must not return anything.
70 ; ------------------------------------------------------------------------
71 ; COUNT: Return the total number of available joysticks in a/x.
79 ; ------------------------------------------------------------------------
80 ; READ: Read a particular joystick passed in A.
83 READ: tax ; Joystick number into X
88 joy1: lda #$7F ; mask for VIA2 JOYBIT: sw3
89 ldx #$C3 ; mask for VIA1 JOYBITS: sw0,sw1,sw2,sw4
92 ldy VIA2_DDRB ; remember the date of DDRB
93 sta VIA2_DDRB ; set JOYBITS on this VIA for input
94 lda VIA2_JOY ; read JOYBIT: sw3
95 sty VIA2_DDRB ; restore the state of DDRB
96 asl ; Shift sw3 into carry
98 ldy VIA1_DDRA ; remember the state of DDRA
99 stx VIA1_DDRA ; set JOYBITS on this VIA for input
100 lda VIA1_JOY ; read JOYBITS: sw0,sw1,sw2,sw4
101 sty VIA1_DDRA ; restore the state of DDRA
104 php ; Save sw3 in carry
105 lsr ; Shift sw0,sw1,sw2,sw4 into bits 1-4
106 tax ; Save sw0,sw1,sw2
107 and #$10 ; Extract sw4 in bit 4
108 sta tmp1 ; Save sw4 in bit 4
109 txa ; Restore sw0,sw1,sw2
110 lsr ; Shift sw0,sw1,sw2 into bits 0-2
111 and #$07 ; Mask bits 0-2
112 plp ; Restore sw3 in carry
113 bcc @L0 ; Is sw3 set?
114 ora #$08 ; Yes: Add sw3 in bit 3
115 @L0: ora tmp1 ; Add sw4 in bit 4
116 eor #$1F ; Active states are inverted
123 joy2: lda #%10000000 ; via port B Data-Direction
124 sta VIA1_DDRB ; bit 7: out bit 6-0: in
129 lda #$80 ; via port B read/write
130 sta VIA1_PRB ; (output one at PB7)
132 lda VIA1_PRB ; via port B read/write
133 and #$1F ; get bit 4-0 (PB4-PB0)
139 joy3: lda #$00 ; via port B read/write
140 sta VIA1_PRB ; (output zero at PB7)
142 lda VIA1_PRB ; via port B read/write
143 and #$0F ; get bit 3-0 (PB3-PB0)
144 sta tmp1 ; joy 4 directions
146 lda VIA1_PRB ; via port B read/write
147 and #%00100000 ; get bit 5 (PB5)