]> git.sur5r.net Git - cc65/blob - libsrc/apple2/read.s
Make use of the new common __directerrno and __mappederrno function.
[cc65] / libsrc / apple2 / read.s
1 ;
2 ; Oliver Schmidt, 12.01.2005
3 ;
4 ; int __fastcall__ read (int fd, void* buf, unsigned count);
5 ;
6
7         .constructor    initprompt
8         .export         _read
9         .import         rwprolog, rwcommon
10         .import         RDKEY, COUT
11
12         .include        "zeropage.inc"
13         .include        "errno.inc"
14         .include        "fcntl.inc"
15         .include        "mli.inc"
16         .include        "filedes.inc"
17         .include        "apple2.inc"
18
19         .segment        "INIT"
20
21 initprompt:
22         ; Set prompt <> ']' to let DOS 3.3 know that we're
23         ; not in Applesoft immediate mode and thus keep it
24         ; from scanning our device I/O for DOS commands.
25         lda     #$80            ; Same value used at $D52C
26         sta     PROMPT
27         rts
28
29         .code
30
31 _read:
32         ; Get parameters
33         jsr     rwprolog
34         bcs     errno
35         tax                     ; Save fd
36
37         ; Check for read access
38         lda     fdtab + FD::FLAGS,y
39         and     #O_RDONLY
40         beq     einval
41
42         ; Check for device
43         txa                     ; Restore fd
44         bmi     device
45
46         ; Do read
47         ldy     #READ_CALL
48         jmp     rwcommon
49
50         ; Set counter to zero
51 device: lda     #$00
52         sta     ptr3
53         sta     ptr3+1
54
55         ; Check for zero count
56         lda     ptr2
57         ora     ptr2+1
58         beq     check
59
60         ; Read from device and echo to device
61 next:   jsr     RDKEY
62         jsr     COUT
63
64         ; Clear hi bit and check for '\r'
65         and     #$7F
66         cmp     #$0D
67         bne     :+
68
69         ; Replace with '\n' and set count to zero
70         lda     #$0A
71         ldy     #$00
72         sty     ptr2
73         sty     ptr2+1
74
75         ; Put char into buf
76 :       ldy     #$00
77         sta     (ptr1),y
78
79         ; Increment pointer
80         inc     ptr1
81         bne     :+
82         inc     ptr1+1
83
84         ; Increment counter
85 :       inc     ptr3
86         bne     check
87         inc     ptr3+1
88
89         ; Check for counter less than count
90 check:  lda     ptr3
91         cmp     ptr2
92         bcc     next
93         ldx     ptr3+1
94         cpx     ptr2+1
95         bcc     next
96
97         ; Return success, AX already set
98         rts
99
100         ; Load errno code
101 einval: lda     #EINVAL
102
103         ; Set __errno
104 errno:  jmp     __directerrno