]> git.sur5r.net Git - cc65/blob - libsrc/mouse/mouse-kernel.s
8844ba506a5367c77163743fe7d489145f4900e8
[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
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 ; Reset flags
68
69         lda     #1
70         sta     _mouse_hidden
71
72 ; Copy the jump vectors
73
74         ldy     #MOUSE_HDR::JUMPTAB
75         ldx     #0
76 @L1:    inx                             ; Skip the JMP opcode
77         jsr     copyjv                  ; Copy one byte
78         jsr     copyjv                  ; Copy one byte
79         cpy     #(MOUSE_HDR::JUMPTAB + .sizeof(MOUSE_HDR::JUMPTAB))
80         bne     @L1
81
82 ; Copy the flags byte. It is located directly behind the jump vectors, so Y
83 ; is already correct when we come here. To save code, we use copyjv - crude
84 ; but effective.
85
86         jsr     copyjv
87
88 ; Copy the callback vectors into the driver space
89
90         jsr     popsreg
91         ldy     #(MOUSE_HDR::CALLBACKS + .sizeof(MOUSE_HDR::CALLBACKS) - 1)
92         sty     tmp2
93         ldy     #.sizeof(MOUSE_CALLBACKS)-1
94         sty     tmp1
95
96 @L2:    jsr     copycb
97         ldy     tmp1
98         jsr     copycb
99         dec     tmp2                    ; Skip opcode byte
100         ldy     tmp1
101         bpl     @L2
102
103 ; Install the IRQ vector if the driver needs it
104
105         bit     mouse_flags             ; Test MOUSE_FLAG_EARLY_IRQ
106         bvc     @L3                     ; Jump if no interrupts at this time
107         jsr     install_irq             ; Activate IRQ routine
108
109 ; Call driver install routine and check for errors
110
111 @L3:    jsr     mouse_install
112         tay                             ; Test error code
113         bne     uninstall_irq           ; Jump on error
114
115 ; No errors on INSTALL. If the driver needs late IRQs, enable them now. Be
116 ; careful not to use A/X since these registers contain the error code from
117 ; INSTALL.
118
119         bit     mouse_flags             ; Test MOUSE_FLAG_LATE_IRQ
120         bpl     Exit                    ; Jump if vector not needed
121 install_irq:
122         ldy     #$4C                    ; Jump opcode
123         sty     mouse_irq               ; Activate IRQ routine
124 Exit:   rts
125
126 ; Uninstall IRQ vector if install routine had errors. A/X may contain the
127 ; error code from mouse_install, so don't use it.
128
129 uninstall_irq:
130         ldy     #$60                    ; RTS opcode
131         sty     mouse_irq               ; Disable IRQ entry point
132         rts
133
134 ; Driver signature invalid. One word is still on the stack
135
136 inv_drv:
137         lda     #MOUSE_ERR_INV_DRIVER
138         ldx     #0
139         jmp     incsp2
140
141 ; Copy one byte from the jump vectors
142
143 copyjv: lda     (ptr1),y
144         sta     mouse_vectors,x
145         iny
146         inx
147         rts
148
149 ; Copy one byte from the callback vectors
150
151 copycb: lda     (sreg),y
152         dec     tmp1
153         ldy     tmp2
154         sta     (ptr1),y
155         dec     tmp2
156         rts
157
158 ;----------------------------------------------------------------------------
159 ; unsigned char mouse_uninstall (void);
160 ; /* Uninstall the currently loaded driver. Returns an error code. */
161
162 _mouse_uninstall:
163
164 ; Depending on the late/early IRQ flag, we will disable IRQs before or after
165 ; calling the driver mouse_uninstall routine.
166
167         bit     mouse_flags             ; Test MOUSE_FLAG_LATE_IRQ
168         bpl     @L1                     ; Don't disable interrupts now
169         jsr     uninstall_irq           ; Disable driver interrupts
170 @L1:    jsr     mouse_uninstall         ; Call driver routine
171
172 ; We don't check the flag a second time here, since disabling IRQs twice,
173 ; or disabling them if they weren't enabled will do no harm, and the missing
174 ; check will save a few bytes.
175
176         jsr     uninstall_irq           ; Disable driver interrupts
177
178 _mouse_clear_ptr:                       ; External entry point
179         lda     #0
180         sta     _mouse_drv
181         sta     _mouse_drv+1            ; Clear the driver pointer
182
183         tax
184         rts                             ; Return zero