]> git.sur5r.net Git - cc65/blob - libsrc/atari/getfd.s
small optimization; fix line buffered read of length 0
[cc65] / libsrc / atari / getfd.s
1 ;
2 ; Christian Groessler, Oct-2000
3 ;
4 ; allocates a new fd in the indirection table
5 ; the fdtable itself is defined here
6 ;
7
8         .include "atari.inc"
9         .include "fd.inc"
10         .importzp tmp1
11
12         .export fdt_to_fdi,getfd
13         .export fd_table,fd_index
14         .export ___fd_table,___fd_index ; for test(debug purposes only
15
16         .data
17
18 ___fd_index:
19 fd_index:       ; fd number is index into this table, entry's value specifies the fd_table entry
20         .res    MAX_FD_INDEX,$ff
21
22 ___fd_table:
23 fd_table:       ; each entry represents an open iocb
24         .byte   0,0,'E',0       ; system console, app starts with opened iocb #0 for E:
25         .byte   0,$ff,0,0
26         .byte   0,$ff,0,0
27         .byte   0,$ff,0,0
28         .byte   0,$ff,0,0
29         .byte   0,$ff,0,0
30         .byte   0,$ff,0,0
31         .byte   0,$ff,0,0
32
33         .code
34
35 ; fdt_to_fdi
36 ; returns a fd_index entry pointing to the given ft_table entry
37 ; get fd_table entry in A
38 ; return C = 0/1 for OK/error
39 ; return fd_index entry in A if OK
40 ; registers destroyed
41 .proc   fdt_to_fdi
42
43         tay
44         lda     #$ff
45         tax
46         inx
47 loop:   cmp     fd_index,x
48         beq     found
49         inx
50         cpx     #MAX_FD_INDEX
51         bcc     loop
52         rts
53
54 found:  tya
55         sta     fd_index,x
56         txa
57         clc
58         rts
59
60 .endproc
61
62 ; getfd
63 ; get a new fd pointing to a ft_table entry
64 ; usage counter of ft_table entry incremented
65 ; A - fd_table entry
66 ; return C = 0/1 for OK/error
67 ; returns fd in A if OK
68 ; registers destroyed, tmp1 destroyed
69 .proc   getfd
70
71         sta     tmp1            ; save fd_table entry
72         jsr     fdt_to_fdi
73         bcs     error
74
75         pha
76         lda     tmp1
77         asl     a
78         asl     a                       ; also clears C
79         tax
80         inc     fd_table+ft_usa,x       ; increment usage counter
81         pla
82 error:  rts
83
84 .endproc