]> git.sur5r.net Git - cc65/blob - libsrc/atari/fdtable.s
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / atari / fdtable.s
1 ;
2 ; Christian Groessler, May-2000
3 ;
4 ; fd indirection table & helper functions
5 ;
6
7         .include "atari.inc"
8         .export fdtoiocb
9         .export fdtoiocb_down
10         .export fd_table
11
12         .data
13
14 fd_table:
15         .byte   0,$ff,0,0
16         .byte   0,$ff,0,0
17         .byte   0,$ff,0,0
18         .byte   0,$ff,0,0
19         .byte   0,$ff,0,0
20         .byte   0,$ff,0,0
21         .byte   0,$ff,0,0
22         .byte   0,$ff,0,0
23         .byte   0,$ff,0,0
24         .byte   0,$ff,0,0
25         .byte   0,$ff,0,0
26         .byte   0,$ff,0,0
27
28 MAX_FD_VAL      =       (* - fd_table) / 4
29
30 ft_usa  = 0     ; usage counter
31 ft_iocb = 1     ; iocb index (0,$10,$20,etc.), $ff for empty entry
32 ft_dev  = 2     ; device of open iocb
33 ft_flag = 3     ; flags
34
35         .code
36
37 ; gets fd in ax, decrements usage counter
38 ; return iocb index in X
39 ; return N bit set for invalid fd
40 ; return Z bit set if last user
41 ; all registers destroyed
42 .proc   fdtoiocb_down
43
44         cpx     #0
45         bne     inval
46         cmp     #MAX_FD_VAL
47         bcs     inval
48         asl     a                       ; create index into fd table
49         asl     a
50         tax
51         lda     #$ff
52         cmp     fd_table+ft_iocb,x      ; entry in use?
53         beq     inval                   ; no, return error
54         lda     fd_table+ft_usa,x       ; get usage counter
55         beq     ok_notlast              ; 0?
56         sec
57         sbc     #1                      ; decr usage counter
58         sta     fd_table+ft_usa,x
59 retiocb:php
60         txa
61         tay
62         lda     fd_table+ft_iocb,x      ; get iocb
63         tax
64         plp
65         bne     cont
66         php
67         lda     #$ff
68         sta     fd_table+ft_iocb,y      ; clear table entry
69         plp
70 cont:   rts
71
72 ok_notlast:
73         lda     #1                      ; clears Z
74         jmp     retiocb
75
76 .endproc
77
78 inval:  ldx     #$ff                    ; sets N
79         rts
80
81
82 ; gets fd in ax
83 ; return iocb index in X
84 ; return N bit set for invalid fd
85 ; all registers destroyed
86 .proc   fdtoiocb
87
88         cpx     #0
89         bne     inval
90         cmp     #MAX_FD_VAL
91         bcs     inval
92         asl     a                       ; create index into fd table
93         asl     a
94         tax
95         lda     #$ff
96         cmp     fd_table+ft_iocb,x      ; entry in use?
97         beq     inval                   ; no, return error
98         lda     fd_table+ft_usa,x       ; get usage counter
99         beq     inval                   ; 0? should not happen
100         lda     fd_table+ft_iocb,x      ; get iocb
101         rts
102
103 .endproc