]> git.sur5r.net Git - cc65/blob - libsrc/serial/ser_load.s
Atari: Make a good "default device" on AtariDOS (2.0 and 2.5) and MyDOS.
[cc65] / libsrc / serial / ser_load.s
1 ;
2 ; Ullrich von Bassewitz, 2006-06-05
3 ;
4 ; unsigned char __fastcall__ ser_load_driver (const char* name)
5 ; /* Load a serial driver and return an error code */
6
7
8         .include        "ser-kernel.inc"
9         .include        "ser-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   _ser_load_driver
39
40 ; Check if we do already have a driver loaded. This is an error. Do not
41 ; touch A/X because they contain the file name.
42
43         ldy     _ser_drv
44         bne     @L0
45         ldy     _ser_drv+1
46         beq     @L1
47 @L0:    lda     #SER_ERR_INSTALLED
48         bne     @L4
49
50 ; Push the name onto the C stack and open the file. The parameter will get
51 ; removed by open().
52 ; ctrl.callerdata = open (name, O_RDONLY);
53
54 @L1:    jsr     pushax
55         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 ser_install().
90 ; Res = ser_install (ctrl.module);
91
92         lda     ctrl + MOD_CTRL::MODULE
93         ldx     ctrl + MOD_CTRL::MODULE+1
94         jsr     _ser_install
95
96 ; If ser_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     _ser_drv
106         ldx     _ser_drv+1
107         jsr     _mod_free               ; Free the driver memory
108         jsr     _ser_clear_ptr          ; Clear ser_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     #<SER_ERR_CANNOT_LOAD
116 @L4:    ldx     #0
117         rts
118
119 .endproc
120
121