]> git.sur5r.net Git - cc65/blob - libsrc/cbm/cbm_read.s
atari5200: testcode/lib/atari5200/hello.c: adapt to changed COLOR_xxx defines
[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         .importzp       ptr1, ptr2, ptr3, tmp1
43         .import         popax, popa
44         .import         __oserror
45
46
47 _cbm_read:
48         eor     #$FF
49         sta     ptr1
50         txa
51         eor     #$FF
52         sta     ptr1+1          ; Save -size-1
53
54         jsr     popax
55         sta     ptr2
56         stx     ptr2+1          ; Save buffer
57
58         jsr     popa
59         tax
60         jsr     CHKIN
61         bcs     @E1             ; Branch on error
62
63 ; bytesread = 0;
64
65         lda     #$00
66         sta     ptr3
67         sta     ptr3+1
68         beq     @L3             ; Branch always
69
70 ; Loop
71
72 @L1:    jsr     READST
73         cmp     #0              ; Status ok?
74         bne     @L4
75
76         jsr     BASIN           ; Read next char from file
77         sta     tmp1            ; Save it for later
78
79         jsr     READST
80         and     #$BF
81         bne     @L4
82
83         lda     tmp1
84         ldy     #0
85         sta     (ptr2),y        ; Save read byte
86
87         inc     ptr2
88         bne     @L2
89         inc     ptr2+1          ; ++buffer;
90
91 @L2:    inc     ptr3
92         bne     @L3
93         inc     ptr3+1          ; ++bytesread;
94
95 @L3:    inc     ptr1
96         bne     @L1
97         inc     ptr1+1
98         bne     @L1
99
100 @L4:    jsr     CLRCH
101
102         lda     ptr3
103         ldx     ptr3+1          ; return bytesread;
104
105         rts
106
107 ; CHKIN failed
108
109 @E1:    sta     __oserror
110         lda     #$FF
111         tax
112         rts                     ; return -1
113