]> git.sur5r.net Git - cc65/blob - libsrc/apple2/read.s
Adjusted C declarations to the changed static driver names.
[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         ; Device succeeds always
51 device: lda     #$00
52         sta     __oserror
53
54         ; Set counter to zero
55         sta     ptr3
56         sta     ptr3+1
57
58         ; Check for zero count
59         lda     ptr2
60         ora     ptr2+1
61         beq     check
62
63         ; Read from device and echo to device
64 next:   jsr     RDKEY
65         jsr     COUT
66
67         ; Clear hi bit and check for '\r'
68         and     #$7F
69         cmp     #$0D
70         bne     :+
71
72         ; Replace with '\n' and set count to zero
73         lda     #$0A
74         ldy     #$00
75         sty     ptr2
76         sty     ptr2+1
77
78         ; Put char into buf
79 :       ldy     #$00
80         sta     (ptr1),y
81
82         ; Increment pointer
83         inc     ptr1
84         bne     :+
85         inc     ptr1+1
86
87         ; Increment counter
88 :       inc     ptr3
89         bne     check
90         inc     ptr3+1
91
92         ; Check for counter less than count
93 check:  lda     ptr3
94         cmp     ptr2
95         bcc     next
96         ldx     ptr3+1
97         cpx     ptr2+1
98         bcc     next
99
100         ; Return success, AX already set
101         rts
102
103         ; Load errno code
104 einval: lda     #EINVAL
105
106         ; Set __errno
107 errno:  jmp     __directerrno