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