]> git.sur5r.net Git - cc65/blob - libsrc/vic20/joy/vic20-stdjoy.s
67299cc1a8e34bd272968159b3170c747104fc79
[cc65] / libsrc / vic20 / joy / vic20-stdjoy.s
1 ;
2 ; Standard joystick driver for the VIC20. May be used multiple times when linked
3 ; to the statically application.
4 ;
5 ; Ullrich von Bassewitz, 2002-12-20
6 ; Using code from Steve Schmidtke
7 ;
8
9         .include        "zeropage.inc"
10
11         .include        "joy-kernel.inc"
12         .include        "joy-error.inc"
13         .include        "vic20.inc"
14
15         .macpack        generic
16         .macpack        module
17
18
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
21
22         module_header   _vic20_stdjoy_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         .addr   0                       ; IRQ entry unused
40
41 ; ------------------------------------------------------------------------
42 ; Constants
43
44 JOY_COUNT       = 1             ; Number of joysticks we support
45
46
47 ; ------------------------------------------------------------------------
48 ; Data.
49
50
51 .code
52
53 ; ------------------------------------------------------------------------
54 ; INSTALL routine. Is called after the driver is loaded into memory. If
55 ; possible, check if the hardware is present and determine the amount of
56 ; memory available.
57 ; Must return an JOY_ERR_xx code in a/x.
58 ;
59
60 INSTALL:
61         lda     #<JOY_ERR_OK
62         ldx     #>JOY_ERR_OK
63 ;       rts                     ; Run into UNINSTALL instead
64
65 ; ------------------------------------------------------------------------
66 ; UNINSTALL routine. Is called before the driver is removed from memory.
67 ; Can do cleanup or whatever. Must not return anything.
68 ;
69
70 UNINSTALL:
71         rts
72
73
74 ; ------------------------------------------------------------------------
75 ; COUNT: Return the total number of available joysticks in a/x.
76 ;
77
78 COUNT:
79         lda     #<JOY_COUNT
80         ldx     #>JOY_COUNT
81         rts
82
83 ; ------------------------------------------------------------------------
84 ; READ: Read a particular joystick passed in A.
85 ; The current implemenation will ignore the joystick number because we do only
86 ; have one joystick
87
88 READ:   lda     #$7F            ; mask for VIA2 JOYBIT: sw3
89         ldx     #$C3            ; mask for VIA1 JOYBITS: sw0,sw1,sw2,sw4
90         sei                     ; necessary?
91
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
97
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
102
103         cli                     ; necessary?
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
117
118         ldx     #0
119         rts