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