]> git.sur5r.net Git - cc65/blob - libsrc/tgi/tgi_load.s
Added SER_ prefix. Whitespace cleanup
[cc65] / libsrc / tgi / tgi_load.s
1 ;
2 ; Ullrich von Bassewitz, 2012-07-22
3 ;
4 ; void __fastcall__ tgi_load_driver (const char* name);
5 ; /* Load and install the given driver. */
6
7
8         .include        "tgi-kernel.inc"
9         .include        "tgi-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   _tgi_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     _tgi_drv
44         bne     @L0
45         ldy     _tgi_drv+1
46         beq     @L1
47 @L0:    lda     #TGI_ERR_INSTALLED
48         bne     @L3
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     @L2
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         beq     @L5
87 @L2:    lda     #TGI_ERR_CANNOT_LOAD
88
89 ; Set an error and exit
90
91 @L3:    sta     _tgi_error
92 @L4:    rts
93
94 ; Check the driver signature, install the driver.
95 ; tgi_install (ctrl.module);
96
97 @L5:    lda     ctrl + MOD_CTRL::MODULE
98         ldx     ctrl + MOD_CTRL::MODULE+1
99         jsr     _tgi_install
100
101 ; If tgi_install was successful, we're done
102
103         lda     _tgi_error
104         beq     @L4
105
106 ; The driver didn't install correctly. Remove it from memory. The error code
107 ; will be retained.
108
109         lda     _tgi_drv
110         ldx     _tgi_drv+1
111         jsr     _mod_free               ; Free the driver memory
112         jmp     tgi_clear_ptr           ; Clear tgi_drv and return
113
114 .endproc
115
116
117