]> git.sur5r.net Git - cc65/blob - libsrc/pce/joy/pce-stdjoy.s
ab25134ddfe4eb3fa6ce3738e35f2d408906a9f5
[cc65] / libsrc / pce / joy / pce-stdjoy.s
1
2 ;
3 ; Standard joystick driver for the PCEngine
4 ;
5
6         .include        "joy-kernel.inc"
7         .include        "joy-error.inc"
8         .include        "pce.inc"
9
10         .macpack        module
11
12
13 ; ------------------------------------------------------------------------
14 ; Header. Includes jump table
15
16         module_header   _pce_stdjoy_joy
17
18 ; Driver signature
19
20         .byte   $6A, $6F, $79           ; "joy"
21         .byte   JOY_API_VERSION         ; Driver API version number
22
23 ; Library reference
24
25         .addr   $0000
26
27 ; Jump table.
28
29         .addr   INSTALL
30         .addr   UNINSTALL
31         .addr   COUNT
32         .addr   READJOY
33         .addr   0                       ; IRQ entry unused
34
35 ; ------------------------------------------------------------------------
36 ; Constants
37
38 JOY_COUNT       = 4             ; Number of joysticks we support
39
40
41 .code
42
43 ; ------------------------------------------------------------------------
44 ; INSTALL routine. Is called after the driver is loaded into memory. If
45 ; possible, check if the hardware is present and determine the amount of
46 ; memory available.
47 ; Must return an JOY_ERR_xx code in a/x.
48 ;
49
50 INSTALL:
51         lda     #<JOY_ERR_OK
52         ldx     #>JOY_ERR_OK
53
54 ;        rts                     ; Run into UNINSTALL instead
55
56 ; ------------------------------------------------------------------------
57 ; DEINSTALL routine. Is called before the driver is removed from memory.
58 ; Can do cleanup or whatever. Must not return anything.
59 ;
60
61 UNINSTALL:
62         rts
63
64
65 ; ------------------------------------------------------------------------
66 ; COUNT: Return the total number of available joysticks in a/x.
67 ;
68 ;unsigned char __fastcall__ joy_count (void);
69
70 COUNT:
71         lda     #<JOY_COUNT
72         ldx     #>JOY_COUNT
73         rts
74
75 ; ------------------------------------------------------------------------
76 ; READ: Read a particular joystick passed in A.
77 ;
78 ;unsigned char __fastcall__ joy_read (unsigned char joystick);
79
80 READJOY:
81         pha
82         jsr     read_joy
83         pla
84         tax                     ; Joystick number into X
85
86         ; return value from buffer
87
88 joy1:
89         lda     padbuffer,x
90         ldx     #0
91         rts
92
93 read_joy:
94         ; reset multitap counter
95         lda     #$01
96         sta     JOY_CTRL
97         pha
98         pla
99         nop
100         nop
101
102         lda     #$03
103         sta     JOY_CTRL
104         pha
105         pla
106         nop
107         nop
108
109         cly
110 nextpad:
111         lda     #$01
112         sta     JOY_CTRL           ; sel = 1
113         pha
114         pla
115         nop                     ; some delay is required
116         nop
117
118         lda     JOY_CTRL
119         asl     a
120         asl     a
121         asl     a
122         asl     a
123         sta     padbuffer, y     ; store new value
124
125         stz     JOY_CTRL
126         pha
127         pla
128
129         nop                     ; some delay is required
130         nop
131
132         lda     JOY_CTRL
133         and     #$0F
134         ora     padbuffer, y     ; second half of new value
135
136         eor     #$FF
137         sta     padbuffer, y     ; store new value
138
139         iny
140         cpy     #$05
141         bcc     nextpad
142         rts
143
144 .bss
145
146 padbuffer:
147         .res    4
148