]> git.sur5r.net Git - cc65/blob - libsrc/joystick/joy-kernel.s
Code goes into the CODE segment, not RODATA.
[cc65] / libsrc / joystick / joy-kernel.s
1 ;
2 ; Ullrich von Bassewitz, 2002-12-20
3 ;
4 ; Common functions of the joystick API.
5 ;
6
7         .export         joy_clear_ptr
8
9         .importzp       ptr1
10         .interruptor    joy_irq         ; Export as IRQ handler
11
12         .include        "joy-kernel.inc"
13         .include        "joy-error.inc"
14
15
16 ;----------------------------------------------------------------------------
17 ; Variables
18
19
20 .bss
21 _joy_drv:       .res    2               ; Pointer to driver
22
23 _joy_masks:     .res    .sizeof(JOY_HDR::MASKS)
24
25 ; Jump table for the driver functions.
26 .data
27 joy_vectors:
28 joy_install:    jmp     $0000
29 joy_uninstall:  jmp     $0000
30 joy_count:      jmp     $0000
31 joy_read:       jmp     $0000
32 joy_irq:        .byte   $60, $00, $00   ; RTS plus two dummy bytes
33
34 ; Driver header signature
35 .rodata
36 joy_sig:        .byte   $6A, $6F, $79, JOY_API_VERSION  ; "joy", version
37
38
39 .code
40 ;----------------------------------------------------------------------------
41 ; unsigned char __fastcall__ joy_install (void* driver);
42 ; /* Install the driver once it is loaded */
43
44
45 _joy_install:
46         sta     _joy_drv
47         sta     ptr1
48         stx     _joy_drv+1
49         stx     ptr1+1
50
51 ; Check the driver signature
52
53         ldy     #.sizeof(joy_sig)-1
54 @L0:    lda     (ptr1),y
55         cmp     joy_sig,y
56         bne     inv_drv
57         dey
58         bpl     @L0
59
60 ; Copy the mask array
61
62         ldy     #JOY_HDR::MASKS + .sizeof(JOY_HDR::MASKS) - 1
63         ldx     #.sizeof(JOY_HDR::MASKS)-1
64 @L1:    lda     (ptr1),y
65         sta     _joy_masks,x
66         dey
67         dex
68         bpl     @L1
69
70 ; Copy the jump vectors
71
72         ldy     #JOY_HDR::JUMPTAB
73         ldx     #0
74 @L2:    inx                             ; Skip the JMP opcode
75         jsr     copy                    ; Copy one byte
76         jsr     copy                    ; Copy one byte
77         cpy     #(JOY_HDR::JUMPTAB + .sizeof(JOY_HDR::JUMPTAB))
78         bne     @L2
79
80         jsr     joy_install             ; Call driver install routine
81         tay                             ; Test error code
82         bne     @L3                     ; Bail out if install had errors
83
84 ; Install the IRQ vector if the driver needs it. A/X contains the error code
85 ; from joy_install, so don't use it.
86
87         ldy     joy_irq+2               ; Check high byte of IRQ vector
88         beq     @L3                     ; Jump if vector invalid
89         ldy     #$4C                    ; JMP opcode
90         sty     joy_irq                 ; Activate IRQ routine
91 @L3:    rts
92
93 ; Driver signature invalid
94
95 inv_drv:
96         lda     #JOY_ERR_INV_DRIVER
97         ldx     #0
98         rts
99
100 ; Copy one byte from the jump vectors
101
102 copy:   lda     (ptr1),y
103         iny
104 set:    sta     joy_vectors,x
105         inx
106         rts
107
108 ;----------------------------------------------------------------------------
109 ; unsigned char __fastcall__ joy_uninstall (void);
110 ; /* Uninstall the currently loaded driver. Note: This call does not free
111 ;  * allocated memory.
112 ;  */
113
114 _joy_uninstall:
115         lda     #$60                    ; RTS opcode
116         sta     joy_irq                 ; Disable IRQ entry point
117
118         jsr     joy_uninstall           ; Call the driver routine
119
120 joy_clear_ptr:                          ; External entry point
121         lda     #0
122         sta     _joy_drv
123         sta     _joy_drv+1              ; Clear the driver pointer
124
125         tax                             ; Return zero
126         rts
127