]> git.sur5r.net Git - cc65/blob - libsrc/nes/joy/nes-stdjoy.s
nes: Fix reading the second controller
[cc65] / libsrc / nes / joy / nes-stdjoy.s
1 ;
2 ; Standard joypad driver for the NES. May be used multiple times when
3 ; linked to the statically application.
4 ;
5 ; Ullrich von Bassewitz, 2003-05-02
6 ; Stefan Haubenthal, 2004-10-05
7 ;
8
9         .include        "zeropage.inc"
10
11         .include        "joy-kernel.inc"
12         .include        "joy-error.inc"
13         .include        "nes.inc"
14
15         .macpack        module
16
17
18 ; ------------------------------------------------------------------------
19 ; Header. Includes jump table
20
21         module_header   _nes_stdjoy_joy
22
23 ; Driver signature
24
25         .byte   $6A, $6F, $79           ; "joy"
26         .byte   JOY_API_VERSION         ; Driver API version number
27
28 ; Library reference
29
30         .addr   $0000
31
32 ; Button state masks (8 values)
33
34         .byte   $10                     ; JOY_UP
35         .byte   $20                     ; JOY_DOWN
36         .byte   $40                     ; JOY_LEFT
37         .byte   $80                     ; JOY_RIGHT
38         .byte   $01                     ; JOY_FIRE      (A)
39         .byte   $02                     ; JOY_FIRE2     (B)
40         .byte   $04                     ;               (Select)
41         .byte   $08                     ;               (Start)
42
43 ; Jump table.
44
45         .addr   INSTALL
46         .addr   UNINSTALL
47         .addr   COUNT
48         .addr   READJOY
49         .addr   0                       ; IRQ entry unused
50
51 ; ------------------------------------------------------------------------
52 ; Constants
53
54 JOY_COUNT       = 2             ; Number of joysticks we support
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     #0
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     #0
87         rts
88
89 ; ------------------------------------------------------------------------
90 ; READ: Read a particular joystick passed in A.
91 ;
92
93 READJOY:
94         and     #$01            ; Fix joystick number
95         tay                     ; Joystick number (0,1) into Y
96
97         lda     #1
98         sta     APU_PAD1
99         lda     #0
100         sta     APU_PAD1
101
102 ; Read joystick
103
104         ldx     #8
105 @Loop:  lda     APU_PAD1,y
106         ror     a
107         ror     tmp1
108         dex
109         bne     @Loop
110
111         lda     tmp1
112 ;       ldx     #$00            ; X implicitly fixed
113         rts