]> git.sur5r.net Git - cc65/blob - libsrc/mouse/mouse-kernel.s
fc8d9490ebfabc2fc9fc892b92874c971d4cbaa9
[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
39 ; Driver header signature
40 .rodata
41 mouse_sig:      .byte   $6d, $6f, $75, MOUSE_API_VERSION    ; "mou", version
42
43
44 .code
45 ;----------------------------------------------------------------------------
46 ; unsigned char __fastcall__ mouse_install (const struct mouse_callbacks* c,
47 ;                                           void* driver);
48 ; /* Install an already loaded driver. Returns an error code. */
49
50 _mouse_install:
51         sta     _mouse_drv
52         sta     ptr1
53         stx     _mouse_drv+1
54         stx     ptr1+1
55
56 ; Check the driver signature
57
58         ldy     #.sizeof(mouse_sig)-1
59 @L0:    lda     (ptr1),y
60         cmp     mouse_sig,y
61         bne     inv_drv
62         dey
63         bpl     @L0
64
65 ; Reset flags
66
67         lda     #1
68         sta     _mouse_hidden
69
70 ; Copy the jump vectors
71
72         ldy     #MOUSE_HDR::JUMPTAB
73         ldx     #0
74 @L1:    inx                             ; Skip the JMP opcode
75         jsr     copyjv                  ; Copy one byte
76         jsr     copyjv                  ; Copy one byte
77         cpy     #(MOUSE_HDR::JUMPTAB + .sizeof(MOUSE_HDR::JUMPTAB))
78         bne     @L1
79
80 ; Copy the callback vectors into the driver space
81
82         jsr     popsreg
83         ldy     #(MOUSE_HDR::CALLBACKS + .sizeof(MOUSE_HDR::CALLBACKS) - 1)
84         sty     tmp2
85         ldy     #.sizeof(MOUSE_CALLBACKS)-1
86         sty     tmp1
87
88 @L2:    jsr     copycb
89         ldy     tmp1
90         jsr     copycb
91         dec     tmp2                    ; Skip opcode byte
92         ldy     tmp1
93         bpl     @L2
94
95 ; Call driver install routine and check for errors
96
97         jsr     mouse_install
98         tay                             ; Test error code
99         bne     @L3                     ; Bail out if install had errors
100
101 ; Install the IRQ vector if the driver needs it. A/X contains the error code
102 ; from mouse_install, so don't use it.
103
104         ldy     mouse_irq+2             ; Check high byte of IRQ vector
105         beq     @L3                     ; Jump if vector invalid
106         ldy     #$4C                    ; Jump opcode
107         sty     mouse_irq               ; Activate IRQ routine
108 @L3:    rts
109
110 ; Driver signature invalid. One word is still on the stack
111
112 inv_drv:
113         lda     #MOUSE_ERR_INV_DRIVER
114         ldx     #0
115         jmp     incsp2
116
117 ; Copy one byte from the jump vectors
118
119 copyjv: lda     (ptr1),y
120         sta     mouse_vectors,x
121         iny
122         inx
123         rts
124
125 ; Copy one byte from the callback vectors
126
127 copycb: lda     (sreg),y
128         dec     tmp1
129         ldy     tmp2
130         sta     (ptr1),y
131         dec     tmp2
132         rts
133
134 ;----------------------------------------------------------------------------
135 ; unsigned char __fastcall__ mouse_uninstall (void);
136 ; /* Uninstall the currently loaded driver. Returns an error code. */
137
138 _mouse_uninstall:
139         lda     #$60                    ; RTS opcode
140         sta     mouse_irq               ; Disable IRQ entry point
141
142         jsr     mouse_uninstall         ; Call driver routine
143
144 mouse_clear_ptr:                        ; External entry point
145         lda     #0
146         sta     _mouse_drv
147         sta     _mouse_drv+1            ; Clear the driver pointer
148
149         tax
150         rts                             ; Return zero
151