]> git.sur5r.net Git - cc65/blob - libsrc/joystick/joy_load.s
Changed most "backticks" (grave accents) into apostrophes.
[cc65] / libsrc / joystick / joy_load.s
1 ;
2 ; Ullrich von Bassewitz, 2006-06-05
3 ;
4 ; unsigned char __fastcall__ joy_load_driver (const char* driver);
5 ; /* Load and install a joystick driver. Return an error code. */
6
7
8         .include        "joy-kernel.inc"
9         .include        "joy-error.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   _joy_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     _joy_drv
47         ora     _joy_drv+1
48         beq     @L1
49         jsr     _joy_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 joy_install().
90 ; Res = joy_install (ctrl.module);  
91
92         lda     ctrl + MOD_CTRL::MODULE
93         ldx     ctrl + MOD_CTRL::MODULE+1
94         jsr     _joy_install
95
96 ; If joy_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     _joy_drv
106         ldx     _joy_drv+1
107         jsr     _mod_free               ; Free the driver memory
108         jsr     _joy_clear_ptr          ; Clear joy_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. Return an error code.
114
115 @L3:    lda     #<JOY_ERR_CANNOT_LOAD
116         ldx     #>JOY_ERR_CANNOT_LOAD
117         rts
118
119 .endproc
120
121