]> git.sur5r.net Git - cc65/blob - libsrc/apple2/extra/iobuf-0800.s
c7a9dae2e22a80a2239207c91202178e3e79ab7d
[cc65] / libsrc / apple2 / extra / iobuf-0800.s
1 ;
2 ; Oliver Schmidt, 15.09.2009
3 ;
4 ; ProDOS 8 I/O buffer management for memory between
5 ; location $0800 and the cc65 program start address
6 ;
7
8         .constructor    initiobuf
9         .export         iobuf_alloc, iobuf_free
10         .import         __STARTUP_RUN__
11         .import         incsp2, popptr1
12
13         .include        "zeropage.inc"
14         .include        "errno.inc"
15         .include        "../filedes.inc"
16
17         .segment        "ONCE"
18
19 initiobuf:
20         ; Convert end address highbyte to table index
21         lda     #>__STARTUP_RUN__
22         sec
23         sbc     #>$0800
24         lsr
25         lsr
26
27         ; Mark all remaining table entries as used
28         tax
29         lda     #$FF
30 :       cpx     #MAX_FDS
31         bcc     :+
32         rts
33 :       sta     table,x
34         inx
35         bne     :--             ; Branch always
36
37 ; ------------------------------------------------------------------------
38
39         .code
40
41 iobuf_alloc:
42         ; Get and save "memptr"
43         jsr     incsp2
44         jsr     popptr1
45
46         ; Search table for free entry
47         ldx     #$00
48 :       lda     table,x
49         beq     :+
50         inx
51         cpx     #MAX_FDS
52         bcc     :-
53         lda     #ENOMEM
54         rts
55
56         ; Mark table entry as used
57 :       lda     #$FF
58         sta     table,x
59
60         ; Convert table index to address hibyte
61         txa
62         asl
63         asl
64         clc
65         adc     #>$0800
66
67         ; Store address in "memptr"
68         ldy     #$01
69         sta     (ptr1),y
70         dey
71         tya
72         sta     (ptr1),y
73         rts
74
75 iobuf_free:
76         ; Convert address hibyte to table index
77         txa
78         sec
79         sbc     #>$0800
80         lsr
81         lsr
82
83         ; Mark table entry as free
84         tax
85         lda     #$00
86         sta     table,x
87         rts
88
89 ; ------------------------------------------------------------------------
90
91         .data
92
93 table:  .res    MAX_FDS