]> git.sur5r.net Git - cc65/blob - libsrc/mouse/mouse-kernel.s
Added space after function name.
[cc65] / libsrc / mouse / mouse-kernel.s
1 ;
2 ; Ullrich von Bassewitz, 2003-12-28, 2009-09-26
3 ;
4 ; Common functions of the mouse driver API.
5 ;
6
7         .import         return0, popsreg, incsp2, mouse_libref
8         .importzp       sreg, ptr1, tmp1, tmp2
9         .interruptor    mouse_irq               ; Export as IRQ handler
10
11         .include        "mouse-kernel.inc"
12
13
14
15 ;----------------------------------------------------------------------------
16 ; Variables
17
18
19 .bss
20 _mouse_drv:     .res    2               ; Pointer to driver
21
22 _mouse_hidden:  .res    1               ; Mouse visibility flag
23
24 ; Jump table for the driver functions.
25 .data
26 mouse_vectors:
27 mouse_install:  jmp     return0
28 mouse_uninstall:jmp     return0
29 mouse_hide:     jmp     return0
30 mouse_show:     jmp     return0
31 mouse_setbox:   jmp     return0
32 mouse_getbox:   jmp     return0
33 mouse_move:     jmp     return0
34 mouse_buttons:  jmp     return0
35 mouse_pos:      jmp     return0
36 mouse_info:     jmp     return0
37 mouse_ioctl:    jmp     return0
38 mouse_irq:      .byte   $60, $00, $00   ; RTS plus two dummy bytes
39 mouse_flags:    .byte   $00
40
41 ; Driver header signature
42 .rodata
43 mouse_sig:      .byte   $6d, $6f, $75, MOUSE_API_VERSION    ; "mou", version
44
45
46 .code
47 ;----------------------------------------------------------------------------
48 ; unsigned char __fastcall__ mouse_install (const struct mouse_callbacks* c,
49 ;                                           void* driver);
50 ; /* Install an already loaded driver. Returns an error code. */
51
52 _mouse_install:
53         sta     _mouse_drv
54         sta     ptr1
55         stx     _mouse_drv+1
56         stx     ptr1+1
57
58 ; Check the driver signature
59
60         ldy     #.sizeof(mouse_sig)-1
61 @L0:    lda     (ptr1),y
62         cmp     mouse_sig,y
63         bne     inv_drv
64         dey
65         bpl     @L0
66
67 ; Set the library reference
68
69         ldy     #MOUSE_HDR::LIBREF
70         lda     #<mouse_libref
71         sta     (ptr1),y
72         iny
73         lda     #>mouse_libref
74         sta     (ptr1),y
75
76 ; Reset flags
77
78         lda     #1
79         sta     _mouse_hidden
80
81 ; Copy the jump vectors
82
83         ldy     #MOUSE_HDR::JUMPTAB
84         ldx     #0
85 @L1:    inx                             ; Skip the JMP opcode
86         jsr     copyjv                  ; Copy one byte
87         jsr     copyjv                  ; Copy one byte
88         cpy     #(MOUSE_HDR::JUMPTAB + .sizeof(MOUSE_HDR::JUMPTAB))
89         bne     @L1
90
91 ; Copy the flags byte. It is located directly behind the jump vectors, so Y
92 ; is already correct when we come here. To save code, we use copyjv - crude
93 ; but effective.
94
95         jsr     copyjv
96
97 ; Copy the callback vectors into the driver space
98
99         jsr     popsreg
100         ldy     #(MOUSE_HDR::CALLBACKS + .sizeof(MOUSE_HDR::CALLBACKS) - 1)
101         sty     tmp2
102         ldy     #.sizeof(MOUSE_CALLBACKS)-1
103         sty     tmp1
104
105 @L2:    jsr     copycb
106         ldy     tmp1
107         jsr     copycb
108         dec     tmp2                    ; Skip opcode byte
109         ldy     tmp1
110         bpl     @L2
111
112 ; Install the IRQ vector if the driver needs it
113
114         bit     mouse_flags             ; Test MOUSE_FLAG_EARLY_IRQ
115         bvc     @L3                     ; Jump if no interrupts at this time
116         jsr     install_irq             ; Activate IRQ routine
117
118 ; Call driver install routine and check for errors
119
120 @L3:    jsr     mouse_install
121         tay                             ; Test error code
122         bne     uninstall_irq           ; Jump on error
123
124 ; No errors on INSTALL. If the driver needs late IRQs, enable them now. Be
125 ; careful not to use A/X since these registers contain the error code from
126 ; INSTALL.
127
128         bit     mouse_flags             ; Test MOUSE_FLAG_LATE_IRQ
129         bpl     Exit                    ; Jump if vector not needed
130 install_irq:
131         ldy     #$4C                    ; Jump opcode
132         sty     mouse_irq               ; Activate IRQ routine
133 Exit:   rts
134
135 ; Uninstall IRQ vector if install routine had errors. A/X may contain the
136 ; error code from mouse_install, so don't use it.
137
138 uninstall_irq:
139         ldy     #$60                    ; RTS opcode
140         sty     mouse_irq               ; Disable IRQ entry point
141         rts
142
143 ; Driver signature invalid. One word is still on the stack
144
145 inv_drv:
146         lda     #MOUSE_ERR_INV_DRIVER
147         ldx     #0
148         jmp     incsp2
149
150 ; Copy one byte from the jump vectors
151
152 copyjv: lda     (ptr1),y
153         sta     mouse_vectors,x
154         iny
155         inx
156         rts
157
158 ; Copy one byte from the callback vectors
159
160 copycb: lda     (sreg),y
161         dec     tmp1
162         ldy     tmp2
163         sta     (ptr1),y
164         dec     tmp2
165         rts
166
167 ;----------------------------------------------------------------------------
168 ; unsigned char mouse_uninstall (void);
169 ; /* Uninstall the currently loaded driver. Returns an error code. */
170
171 _mouse_uninstall:
172
173 ; Depending on the late/early IRQ flag, we will disable IRQs before or after
174 ; calling the driver mouse_uninstall routine.
175
176         bit     mouse_flags             ; Test MOUSE_FLAG_LATE_IRQ
177         bpl     @L1                     ; Don't disable interrupts now
178         jsr     uninstall_irq           ; Disable driver interrupts
179 @L1:    jsr     mouse_uninstall         ; Call driver routine
180
181 ; We don't check the flag a second time here, since disabling IRQs twice,
182 ; or disabling them if they weren't enabled will do no harm, and the missing
183 ; check will save a few bytes.
184
185         jsr     uninstall_irq           ; Disable driver interrupts
186
187 _mouse_clear_ptr:                       ; External entry point
188         lda     #0
189         sta     _mouse_drv
190         sta     _mouse_drv+1            ; Clear the driver pointer
191
192         tax
193         rts                             ; Return zero