]> git.sur5r.net Git - cc65/blob - libsrc/mouse/mouse_load.s
Fixed LinuxDoc Tools issues in some verbatim blocks in the Atari document.
[cc65] / libsrc / mouse / mouse_load.s
1 ;                         
2 ; Ullrich von Bassewitz, 2006-06-05
3 ;
4 ; unsigned char __fastcall__ mouse_load_driver (const struct mouse_callbacks* c,
5 ;                                               const char* name)
6 ; /* Load a mouse driver and return an error code */
7
8
9         .include        "mouse-kernel.inc"
10         .include        "modload.inc"
11         .include        "fcntl.inc"
12
13         .import         pushax
14         .import         pusha0
15         .import         incsp2
16         .import         _open
17         .import         _read
18         .import         _close
19
20
21
22 ;----------------------------------------------------------------------------
23 ; Variables
24
25 .data
26
27 ctrl:   .addr   _read
28         .res    2                       ; CALLERDATA
29         .res    2                       ; MODULE
30         .res    2                       ; MODULE_SIZE
31         .res    2                       ; MODULE_ID
32
33 ;----------------------------------------------------------------------------
34 ; Code
35
36 .code
37
38 .proc   _mouse_load_driver
39
40 ; Save name on the C stack. We will need it later as parameter passed to open()
41
42         jsr     pushax
43
44 ; Check if we do already have a driver loaded. If so, remove it.
45
46         lda     _mouse_drv
47         ora     _mouse_drv+1
48         beq     @L1
49         jsr     _mouse_uninstall
50
51 ; Open the file. The name parameter is already on stack and will get removed
52 ; by open().
53 ; ctrl.callerdata = open (name, O_RDONLY);
54
55 @L1:    lda     #<O_RDONLY
56         jsr     pusha0
57         ldy     #4                      ; Argument size
58         jsr     _open
59         sta     ctrl + MOD_CTRL::CALLERDATA
60         stx     ctrl + MOD_CTRL::CALLERDATA+1
61
62 ; if (ctrl.callerdata >= 0) {
63
64         txa
65         bmi     @L3
66
67 ; /* Load the module */
68 ; Res = mod_load (&ctrl);
69
70         lda     #<ctrl
71         ldx     #>ctrl
72         jsr     _mod_load
73         pha
74
75 ; /* Close the input file */
76 ; close (ctrl.callerdata);
77
78         lda     ctrl + MOD_CTRL::CALLERDATA
79         ldx     ctrl + MOD_CTRL::CALLERDATA+1
80         jsr     _close
81
82 ; /* Check the return code */
83 ; if (Res == MLOAD_OK) {
84
85         pla
86         bne     @L3
87
88 ; Check the driver signature, install the driver. c is already on stack and
89 ; will get removed by mouse_install().
90 ; Res = mouse_install (c, ctrl.module);
91
92         lda     ctrl + MOD_CTRL::MODULE
93         ldx     ctrl + MOD_CTRL::MODULE+1
94         jsr     _mouse_install
95
96 ; If mouse_install was successful, we're done
97
98         tax
99         beq     @L2
100
101 ; The driver didn't install correctly. Remove it from memory and return the
102 ; error code.
103
104         pha                             ; Save the error code
105         lda     _mouse_drv
106         ldx     _mouse_drv+1
107         jsr     _mod_free               ; Free the driver memory
108         jsr     _mouse_clear_ptr        ; Clear mouse_drv
109         pla                             ; Restore the error code
110         ldx     #0                      ; We must return an int
111 @L2:    rts                             ; Done
112
113 ; Open or mod_load failed. Remove excess arguments from stack and return an
114 ; error code.
115
116 @L3:    jsr     incsp2
117         lda     #<MOUSE_ERR_CANNOT_LOAD
118         ldx     #>MOUSE_ERR_CANNOT_LOAD
119         rts
120
121 .endproc
122
123