]> git.sur5r.net Git - cc65/blob - libsrc/apple2/joy/a2.stdjoy.s
Merge pull request #249 from polluks/master
[cc65] / libsrc / apple2 / joy / a2.stdjoy.s
1 ;
2 ; Standard joystick driver for the Apple ][. May be used multiple times
3 ; when statically linked to the application.
4 ;
5 ; Ullrich von Bassewitz, 2003-05-02
6 ; Oliver Schmidt, 2008-02-25
7 ; Using the readjoy code from Stefan Haubenthal
8 ;
9
10         .include        "zeropage.inc"
11
12         .include        "joy-kernel.inc"
13         .include        "joy-error.inc"
14         .include        "apple2.inc"
15
16         .macpack        module
17
18 ; ------------------------------------------------------------------------
19
20 ; Constants
21
22 THRESHOLD =     20      ; Deviation from center triggering movement
23
24 ; ------------------------------------------------------------------------
25
26 ; ROM entry points
27
28 PREAD   :=      $FB1E   ; Read paddle in X, return AD conv. value in Y
29
30 ; ------------------------------------------------------------------------
31
32 ; Header. Includes jump table.
33
34         .ifdef  __APPLE2ENH__
35         module_header   _a2e_stdjoy_joy
36         .else
37         module_header   _a2_stdjoy_joy
38         .endif
39
40 ; Driver signature
41
42         .byte   $6A, $6F, $79   ; "joy"
43         .byte   JOY_API_VERSION ; Driver API version number
44
45 ; Library reference
46
47         .addr   $0000
48
49 ; Button state masks (8 values)
50
51         .byte   $10
52         .byte   $20
53         .byte   $04
54         .byte   $08
55         .byte   $40
56         .byte   $80
57         .byte   $00             ; Future expansion
58         .byte   $00             ; Future expansion
59
60 ; Jump table
61
62         .addr   INSTALL
63         .addr   UNINSTALL
64         .addr   COUNT
65         .addr   READJOY
66         .addr   0               ; IRQ not used
67
68 ; ------------------------------------------------------------------------
69
70         .code
71
72 ; INSTALL routine. Is called after the driver is loaded into memory. If
73 ; possible, check if the hardware is present and determine the amount of
74 ; memory available.
75 ; Must return an JOY_ERR_xx code in a/x.
76 INSTALL:
77         lda     #<JOY_ERR_OK
78         ldx     #>JOY_ERR_OK
79         ; Fall through
80
81 ; UNINSTALL routine. Is called before the driver is removed from memory.
82 ; Can do cleanup or whatever. Must not return anything.
83 UNINSTALL:
84         rts
85
86 ; COUNT: Return the total number of available joysticks in a/x.
87 COUNT:
88         lda     #$02            ; Number of joysticks we support
89         ldx     #$00
90         rts
91
92 ; READ: Read a particular joystick passed in A.
93 READJOY:
94         bit     $C082           ; Switch in ROM
95         and     #$01            ; Restrict joystick number
96
97         ; Read horizontal paddle
98         asl                     ; Joystick number -> paddle number
99         tax                     ; Set paddle number (0, 2)
100         jsr     PREAD           ; Read paddle value
101         lda     #$00            ; 0 0 0 0 0 0 0 0
102         cpy     #127 - THRESHOLD
103         ror                     ; !LEFT 0 0 0 0 0 0 0
104         cpy     #127 + THRESHOLD
105         ror                     ; RIGHT !LEFT 0 0 0 0 0 0
106
107         ; Read vertical paddle
108         pha
109         inx                     ; Set paddle number (1, 3)
110         jsr     PREAD           ; Read paddle value
111         pla
112         cpy     #127 - THRESHOLD
113         ror                     ; !UP RIGHT !LEFT 0 0 0 0 0
114         cpy     #127 + THRESHOLD
115         ror                     ; DOWN !UP RIGHT !LEFT 0 0 0 0
116
117         ; Read primary button
118         tay
119         lda     BUTN0-1,x       ; Check button (1, 3)
120         asl
121         tya
122         ror                     ; FIRE DOWN !UP RIGHT !LEFT 0 0 0
123
124         ; Read secondary button
125         tay
126         inx
127         txa
128         and     #$03            ; IIgs has fourth button at TAPEIN
129         tax
130         lda     BUTN0-1,x       ; Check button (2, 0)
131         asl
132         tya
133         ror                     ; FIRE2 FIRE DOWN !UP RIGHT !LEFT 0 0
134
135         ; Finalize
136         eor     #%00010100      ; FIRE2 FIRE DOWN UP RIGHT LEFT 0 0
137         ldx     #$00
138         bit     $C080           ; Switch in LC bank 2 for R/O
139         rts