]> git.sur5r.net Git - cc65/blob - libsrc/em/em_load.s
Code review changes and build fix.
[cc65] / libsrc / em / em_load.s
1 ;
2 ; Ullrich von Bassewitz, 2012-07-22
3 ;
4 ; unsigned char __fastcall__ em_load_driver (const char* name)
5 ; /* Load an extended memory driver and return an error code */
6
7
8         .include        "em-kernel.inc"
9         .include        "em-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   _em_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     _em_drv
44         bne     @L0
45         ldy     _em_drv+1
46         beq     @L1
47 @L0:    lda     #EM_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.
89 ; Res = em_install (ctrl.module);
90
91         lda     ctrl + MOD_CTRL::MODULE
92         ldx     ctrl + MOD_CTRL::MODULE+1
93         jsr     _em_install
94
95 ; If em_install was successful, we're done
96
97         tax
98         beq     @L2
99
100 ; The driver didn't install correctly. Remove it from memory and return the
101 ; error code.
102
103         pha                             ; Save the error code
104         lda     _em_drv
105         ldx     _em_drv+1
106         jsr     _mod_free               ; Free the driver memory
107         jsr     em_clear_ptr            ; Clear em_drv
108         pla                             ; Restore the error code
109         ldx     #0                      ; We must return an int
110 @L2:    rts                             ; Done
111
112 ; Open or mod_load failed. Return an error code.
113
114 @L3:    lda     #<EM_ERR_CANNOT_LOAD
115 @L4:    ldx     #0
116         rts
117
118 .endproc
119
120