]> git.sur5r.net Git - cc65/blob - libsrc/cbm/cbm_read.s
Merge pull request #129 from greg-king5/cbmdir
[cc65] / libsrc / cbm / cbm_read.s
1 ;
2 ; Ullrich von Bassewitz, 22.06.2002
3 ;
4 ; Original C code by Marc 'BlackJack' Rintsch, 19.03.2001
5 ;
6 ; int __fastcall__ cbm_read (unsigned char lfn, void* buffer, unsigned int size)
7 ; /* Reads up to "size" bytes from a file to "buffer".
8 ; ** Returns the number of actually read bytes, 0 if there are no bytes left
9 ; ** (EOF) or -1 in case of an error. _oserror contains an errorcode then (see
10 ; ** table below).
11 ; */
12 ; {
13 ;     static unsigned int bytesread;
14 ;     static unsigned char tmp;
15 ;
16 ;     /* if we can't change to the inputchannel #lfn then return an error */
17 ;     if (_oserror = cbm_k_chkin(lfn)) return -1;
18 ;
19 ;     bytesread = 0;
20 ;
21 ;     while (bytesread<size && !cbm_k_readst()) {
22 ;         tmp = cbm_k_basin();
23 ;
24 ;         /* the kernal routine BASIN sets ST to EOF if the end of file
25 ;         ** is reached the first time, then we have store tmp.
26 ;         ** every subsequent call returns EOF and READ ERROR in ST, then
27 ;         ** we have to exit the loop here immediatly.
28 ;         */
29 ;         if (cbm_k_readst() & 0xBF) break;
30 ;
31 ;         ((unsigned char*)buffer)[bytesread++] = tmp;
32 ;     }
33 ;
34 ;     cbm_k_clrch();
35 ;     return bytesread;
36 ; }
37 ;
38
39         .include        "cbm.inc"
40
41         .export         _cbm_read
42         .import         CHKIN, READST, BASIN, CLRCH
43         .importzp       ptr1, ptr2, ptr3, tmp1
44         .import         popax, popa
45         .import         __oserror
46
47
48 _cbm_read:
49         eor     #$FF
50         sta     ptr1
51         txa
52         eor     #$FF
53         sta     ptr1+1          ; Save -size-1
54
55         jsr     popax
56         sta     ptr2
57         stx     ptr2+1          ; Save buffer
58
59         jsr     popa
60         tax
61         jsr     CHKIN
62         bcs     @E1             ; Branch on error
63
64 ; bytesread = 0;
65
66         lda     #$00
67         sta     ptr3
68         sta     ptr3+1
69         beq     @L3             ; Branch always
70
71 ; Loop
72
73 @L1:    jsr     READST
74         cmp     #0              ; Status ok?
75         bne     @L4
76
77         jsr     BASIN           ; Read next char from file
78         sta     tmp1            ; Save it for later
79
80         jsr     READST
81         and     #$BF
82         bne     @L4
83
84         lda     tmp1
85         ldy     #0
86         sta     (ptr2),y        ; Save read byte
87
88         inc     ptr2
89         bne     @L2
90         inc     ptr2+1          ; ++buffer;
91
92 @L2:    inc     ptr3
93         bne     @L3
94         inc     ptr3+1          ; ++bytesread;
95
96 @L3:    inc     ptr1
97         bne     @L1
98         inc     ptr1+1
99         bne     @L1
100
101 @L4:    jsr     CLRCH
102
103         lda     ptr3
104         ldx     ptr3+1          ; return bytesread;
105
106         rts
107
108 ; CHKIN failed
109
110 @E1:    sta     __oserror
111         lda     #$FF
112         tax
113         rts                     ; return -1
114