]> git.sur5r.net Git - cc65/blob - libsrc/cbm510/joy/cbm510-std.s
Merge pull request #283 from polluks/sp65
[cc65] / libsrc / cbm510 / joy / cbm510-std.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
15         .macpack        generic
16         .macpack        module
17
18
19 ; ------------------------------------------------------------------------
20 ; Header. Includes jump table
21
22         module_header   _cbm510_std_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 JOY_COUNT       = 2             ; Number of joysticks we support
56
57
58 ; ------------------------------------------------------------------------
59 ; Data.
60
61
62 .code
63
64 ; ------------------------------------------------------------------------
65 ; INSTALL routine. Is called after the driver is loaded into memory. If
66 ; possible, check if the hardware is present and determine the amount of
67 ; memory available.
68 ; Must return an JOY_ERR_xx code in a/x.
69 ;
70
71 INSTALL:
72         lda     #<JOY_ERR_OK
73         ldx     #>JOY_ERR_OK
74 ;       rts                     ; Run into UNINSTALL instead
75
76 ; ------------------------------------------------------------------------
77 ; UNINSTALL routine. Is called before the driver is removed from memory.
78 ; Can do cleanup or whatever. Must not return anything.
79 ;
80
81 UNINSTALL:
82         rts
83
84
85 ; ------------------------------------------------------------------------
86 ; COUNT: Return the total number of available joysticks in a/x.
87 ;
88
89 COUNT:
90         lda     #<JOY_COUNT
91         ldx     #>JOY_COUNT
92         rts
93
94 ; ------------------------------------------------------------------------
95 ; READ: Read a particular joystick passed in A.
96 ;
97
98 READ:   ldx     #$0F            ; Switch to the system bank
99         stx     IndReg
100         tax                     ; Save joystick number
101
102 ; Get the direction bits
103
104         ldy     #CIA::PRB
105         lda     (cia2),y        ; Read joystick inputs
106         sta     tmp1
107
108 ; Get the fire bits
109
110         ldy     #CIA::PRA
111         lda     (cia2),y
112
113 ; Make the result value
114
115         cpx     #$00            ; Joystick 0?
116         bne     @L1             ; Jump if no
117
118 ; Joystick 1, fire is in bit 6, direction in bit 0-3
119
120         asl     a
121         jmp     @L2
122
123 ; Joystick 2, fire is in bit 7, direction in bit 5-7
124
125 @L1:    ldx     #$00            ; High byte of return value
126         lsr     tmp1
127         lsr     tmp1
128         lsr     tmp1
129         lsr     tmp1
130
131 ; Mask the relavant bits, get the fire bit
132
133 @L2:    asl     a               ; Fire bit into carry
134         lda     tmp1
135         and     #$0F
136         bcc     @L3
137         ora     #$10
138 @L3:    eor     #$1F            ; All bits are inverted
139
140 ; Switch back to the execution bank and return the joystick mask in a/x
141
142         ldy     ExecReg
143         sty     IndReg
144         rts
145