]> git.sur5r.net Git - cc65/blob - libsrc/pce/joy/pce-stdjoy.s
Merge remote-tracking branch 'upstream/master' into pcenginetarget
[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
9         .macpack        module
10
11
12 ; ------------------------------------------------------------------------
13 ; Header. Includes jump table
14
15         module_header   _pce_stdjoy_joy
16
17 ; Driver signature
18
19         .byte   $6A, $6F, $79           ; "joy"
20         .byte   JOY_API_VERSION         ; Driver API version number
21
22 ; Library reference
23
24         .addr   $0000
25
26 ; Button state masks (8 values)
27
28         .byte   $10                     ; JOY_UP
29         .byte   $40                     ; JOY_DOWN
30         .byte   $80                     ; JOY_LEFT
31         .byte   $20                     ; JOY_RIGHT
32         .byte   $01                     ; JOY_FIRE_A
33         .byte   $02                     ; JOY_FIRE_B
34         .byte   $04                     ; JOY_SELECT
35         .byte   $08                     ; JOY_RUN
36
37 ; Jump table.
38
39         .addr   INSTALL
40         .addr   UNINSTALL
41         .addr   COUNT
42         .addr   READJOY
43         .addr   0                       ; IRQ entry unused
44
45 ; ------------------------------------------------------------------------
46 ; Constants
47
48 JOY_COUNT       = 4             ; Number of joysticks we support
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
64 ;               rts                     ; Run into DEINSTALL instead
65
66 ; ------------------------------------------------------------------------
67 ; DEINSTALL routine. Is called before the driver is removed from memory.
68 ; Can do cleanup or whatever. Must not return anything.
69 ;
70
71 UNINSTALL:
72                 rts
73
74
75 ; ------------------------------------------------------------------------
76 ; COUNT: Return the total number of available joysticks in a/x.
77 ;
78 ;unsigned char __fastcall__ joy_count (void);
79
80 COUNT:
81                 lda     #<JOY_COUNT
82                 ldx     #>JOY_COUNT
83                 rts
84
85 ; ------------------------------------------------------------------------
86 ; READ: Read a particular joystick passed in A.
87 ;
88 ;unsigned char __fastcall__ joy_read (unsigned char joystick);
89
90 READJOY:
91                 pha
92                 jsr read_joy
93                 pla
94                 tax             ; Joystick number into X
95
96                 ; return value from buffer
97
98 joy1:
99                 lda padbuffer,x
100                 ldx #0
101                 rts
102
103 read_joy:
104                 ; reset multitap counter
105                 lda     #$01
106                 sta     $1000
107                 pha
108                 pla
109                 nop
110                 nop
111
112                 lda     #$03
113                 sta     $1000
114                 pha
115                 pla
116                 nop
117                 nop
118
119                 cly
120 nextpad:
121                 lda     #$01
122                 sta     $1000   ; sel = 1
123                 pha
124                 pla
125                 nop
126                 nop
127
128                 lda     $1000
129                 asl     a
130                 asl     a
131                 asl     a
132                 asl     a
133                 sta     padbuffer, y     ; store new value
134
135                 stz     $1000
136                 pha
137                 pla
138                 nop
139                 nop
140
141                 lda     $1000
142                 and     #$0F
143                 ora     padbuffer, y     ; second half of new value
144
145                 eor     #$FF
146                 sta     padbuffer, y     ; store new value
147
148                 iny
149                 cpy     #$05
150                 bcc     nextpad
151                 rts
152
153 .bss
154
155 padbuffer:
156                 .res 4
157