]> git.sur5r.net Git - cc65/blob - libsrc/cbm/cbm_read.s
Fixed a bug
[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 immidiatly. */
28 ;         if (cbm_k_readst() & 0xBF) break;
29 ;
30 ;         ((unsigned char*)buffer)[bytesread++] = tmp;
31 ;     }
32 ;
33 ;     cbm_k_clrch();
34 ;     return bytesread;
35 ; }
36 ;
37
38         .include        "cbm.inc"
39
40         .export         _cbm_read
41         .importzp       ptr1, ptr2, ptr3, tmp1
42         .import         popax, popa
43         .import         __oserror
44
45 _cbm_read:
46         eor     #$FF
47         sta     ptr1
48         txa
49         eor     #$FF
50         sta     ptr1+1          ; Save -size-1
51
52         jsr     popax
53         sta     ptr2
54         stx     ptr2+1          ; Save buffer
55
56         jsr     popa
57         tax
58         jsr     CHKIN
59         bcs     @E1             ; Branch on error
60
61 ; bytesread = 0;
62
63         lda     #$00
64         sta     ptr3
65         sta     ptr3+1
66         beq     @L3             ; Branch always
67
68 ; Loop
69
70 @L1:    jsr     READST
71         cmp     #0              ; Status ok?
72         bne     @L4
73
74         jsr     BASIN           ; Read next char from file
75         sta     tmp1            ; Save it for later
76
77         jsr     READST
78         and     #$BF
79         bne     @L4
80
81         lda     tmp1
82         ldy     #0
83         sta     (ptr2),y        ; Save read byte
84
85         inc     ptr2
86         bne     @L2
87         inc     ptr2+1          ; ++buffer;
88
89 @L2:    inc     ptr3
90         bne     @L3
91         inc     ptr3+1          ; ++bytesread;
92
93 @L3:    inc     ptr1
94         bne     @L1
95         inc     ptr1+1
96         bne     @L1
97
98 @L4:    jsr     CLRCH
99
100         lda     ptr3
101         ldx     ptr3+1          ; return bytesread;
102
103         rts
104
105 ; CHKIN failed
106
107 @E1:    sta     __oserror
108         lda     #$FF
109         tax
110         rts                     ; return -1
111