]> git.sur5r.net Git - cc65/blob - libsrc/common/_fdesc.s
a2cf747ce6520ec87864b6ef3ced50401d1df99a
[cc65] / libsrc / common / _fdesc.s
1 ;
2 ; Ullrich von Bassewitz, 17.06.1998
3 ;
4 ; int _fdesc (void);
5 ; /* Find a free descriptor slot */
6
7
8         .export         __fdesc
9         .import         return0
10
11         .include        "_file.inc"
12
13 .proc   __fdesc
14
15         ldy     #0
16         lda     #_FOPEN
17 Loop:   and     __filetab + _FILE_f_flags,y     ; load flags
18         beq     Found                           ; jump if closed
19 .repeat ::_FILE_size
20         iny
21 .endrepeat
22         cpy     #(FOPEN_MAX * _FILE_size)       ; Done?
23         bne     Loop
24
25 ; File table is full
26
27         jmp     return0
28
29 ; Free slot found, get address
30
31 Found:  tya                     ; Offset
32         clc
33         adc     #<__filetab
34         ldx     #>__filetab     ; High byte
35         bcc     @L1             ; Jump if no overflow
36         inx                     ; Bump high byte
37 @L1:    rts
38
39 .endproc
40
41
42