]> git.sur5r.net Git - cc65/blob - libsrc/apple2/write.s
Package additional linker configs for the apple2enh
[cc65] / libsrc / apple2 / write.s
1 ;
2 ; Oliver Schmidt, 12.01.2005
3 ;
4 ; int __fastcall__ write (int fd, const void* buf, unsigned count);
5 ;
6
7         .export         _write
8         .import         rwprolog, rwcommon, rwepilog
9         .import         errnoexit, oserrexit
10         .import         COUT
11
12         .include        "zeropage.inc"
13         .include        "errno.inc"
14         .include        "fcntl.inc"
15         .include        "mli.inc"
16         .include        "filedes.inc"
17
18 _write:
19         ; Get parameters
20         jsr     rwprolog
21         bcs     errno
22         tax                     ; Save fd
23
24         ; Check for write access
25         lda     fdtab + FD::FLAGS,y
26         and     #O_WRONLY
27         beq     einval
28
29         ; Check for device
30         txa                     ; Restore fd
31         bmi     device
32
33         ; Check for append flag
34         lda     fdtab + FD::FLAGS,y
35         and     #O_APPEND
36         beq     write
37
38         ; Set fd
39         stx     mliparam + MLI::EOF::REF_NUM
40
41         ; Get file size
42         lda     #GET_EOF_CALL
43         ldx     #EOF_COUNT
44         jsr     callmli
45         bcs     oserr
46
47         ; REF_NUM already set
48         .assert MLI::MARK::REF_NUM = MLI::EOF::REF_NUM, error
49
50         ; POSITION already set
51         .assert MLI::MARK::POSITION = MLI::EOF::EOF, error
52
53         ; Set file pointer
54         lda     #SET_MARK_CALL
55         ldx     #MARK_COUNT
56         jsr     callmli
57         bcs     oserr
58
59         ; Do write
60 write:  lda     fdtab + FD::REF_NUM,y
61         ldy     #WRITE_CALL
62         jmp     rwcommon
63
64         ; Save count for epilog
65 device: ldx     ptr2
66         lda     ptr2+1
67         stx     mliparam + MLI::RW::TRANS_COUNT
68         sta     mliparam + MLI::RW::TRANS_COUNT+1
69
70         ; Check for zero count
71         ora     ptr2
72         beq     done
73
74         ; Get char from buf
75         ldy     #$00
76 next:   lda     (ptr1),y
77
78         ; Replace '\n' with '\r'
79         cmp     #$0A
80         bne     :+
81         lda     #$0D
82
83         ; Set hi bit and write to device
84 :       ora     #$80
85         .ifndef __APPLE2ENH__
86         cmp     #$E0            ; Test for lowercase
87         bcc     output
88         and     #$DF            ; Convert to uppercase
89         .endif
90 output: jsr     COUT            ; Preserves X and Y
91
92         ; Increment pointer
93         iny
94         bne     :+
95         inc     ptr1+1
96
97         ; Decrement count
98 :       dex
99         bne     next
100         dec     ptr2+1
101         bpl     next
102
103         ; Return success
104 done:   jmp     rwepilog
105
106         ; Load errno code
107 einval: lda     #EINVAL
108
109         ; Return errno
110 errno:  jmp     errnoexit
111
112         ; Return oserror
113 oserr:  jmp     oserrexit
114