]> git.sur5r.net Git - cc65/blob - libsrc/cbm510/cbm510-stdjoy.s
Two fixes from Stefan Haubenthal
[cc65] / libsrc / cbm510 / cbm510-stdjoy.s
1 ;
2 ; Standard joystick driver for the Commodore 510 (aka P500). May be used
3 ; multiple times when linked to the statically application.
4 ;
5 ; Ullrich von Bassewitz, 2003-02-16
6 ;
7
8         .include        "zeropage.inc"
9         .include        "extzp.inc"
10
11         .include        "joy-kernel.inc"
12         .include        "joy-error.inc"
13         .include        "cbm510.inc"
14         .include        "extzp.inc"
15                                    
16         .macpack        generic
17
18
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
21
22 .segment        "JUMPTABLE"
23
24 ; Driver signature
25
26         .byte   $6A, $6F, $79           ; "joy"
27         .byte   JOY_API_VERSION         ; Driver API version number
28
29 ; Button state masks (8 values)
30
31         .byte   $01                     ; JOY_UP
32         .byte   $02                     ; JOY_DOWN
33         .byte   $04                     ; JOY_LEFT
34         .byte   $08                     ; JOY_RIGHT
35         .byte   $10                     ; JOY_FIRE
36         .byte   $00                     ; Future expansion
37         .byte   $00                     ; Future expansion
38         .byte   $00                     ; Future expansion
39
40 ; Jump table.
41
42         .word   INSTALL
43         .word   UNINSTALL
44         .word   COUNT
45         .word   READ
46
47 ; ------------------------------------------------------------------------
48 ; Constants
49
50 JOY_COUNT       = 2             ; Number of joysticks we support
51
52
53 ; ------------------------------------------------------------------------
54 ; Data.
55
56
57 .code
58
59 ; ------------------------------------------------------------------------
60 ; INSTALL routine. Is called after the driver is loaded into memory. If
61 ; possible, check if the hardware is present and determine the amount of
62 ; memory available.
63 ; Must return an JOY_ERR_xx code in a/x.
64 ;
65
66 INSTALL:
67         lda     #<JOY_ERR_OK
68         ldx     #>JOY_ERR_OK
69 ;       rts                     ; Run into UNINSTALL instead
70
71 ; ------------------------------------------------------------------------
72 ; UNINSTALL routine. Is called before the driver is removed from memory.
73 ; Can do cleanup or whatever. Must not return anything.
74 ;
75
76 UNINSTALL:
77         rts
78
79
80 ; ------------------------------------------------------------------------
81 ; COUNT: Return the total number of available joysticks in a/x.
82 ;
83
84 COUNT:
85         lda     #<JOY_COUNT
86         ldx     #>JOY_COUNT
87         rts
88
89 ; ------------------------------------------------------------------------
90 ; READ: Read a particular joystick passed in A.
91 ;
92
93 READ:   ldx     #$0F            ; Switch to the system bank
94         stx     IndReg
95         tax                     ; Save joystick number
96
97 ; Get the direction bits
98
99         ldy     #CIA::PRB
100         lda     (cia2),y        ; Read joystick inputs
101         sta     tmp1
102
103 ; Get the fire bits
104
105         ldy     #CIA::PRA
106         lda     (cia2),y
107
108 ; Make the result value
109
110         cpx     #$00            ; Joystick 0?
111         bne     @L1             ; Jump if no
112
113 ; Joystick 1, fire is in bit 6, direction in bit 0-3
114
115         asl     a
116         jmp     @L2
117
118 ; Joystick 2, fire is in bit 7, direction in bit 5-7
119
120 @L1:    ldx     #$00            ; High byte of return value
121         lsr     tmp1
122         lsr     tmp1
123         lsr     tmp1
124         lsr     tmp1
125
126 ; Mask the relavant bits, get the fire bit
127
128 @L2:    asl     a               ; Fire bit into carry
129         lda     tmp1
130         and     #$0F
131         bcc     @L3
132         ora     #$10
133 @L3:    eor     #$1F            ; All bits are inverted
134
135 ; Switch back to the execution bank and return the joystick mask in a/x
136
137         ldy     ExecReg
138         sty     IndReg
139         rts
140