]> git.sur5r.net Git - cc65/blob - libsrc/cbm/write.s
64b3c580ebd432a47f2e9672ade69870eccff618
[cc65] / libsrc / cbm / write.s
1 ;
2 ; Ullrich von Bassewitz, 16.11.2002
3 ;
4 ; int write (int fd, const void* buf, unsigned count);
5 ;
6
7         .export         _write
8         .constructor    initstdout
9
10         .import         SETLFS, OPEN, CKOUT, BSOUT, CLRCH
11         .import         rwcommon
12         .import         __errno, __oserror
13         .importzp       sp, ptr1, ptr2, ptr3
14
15         .include        "errno.inc"
16         .include        "fcntl.inc"
17         .include        "cbm.inc"
18         .include        "filedes.inc"
19
20
21 ;--------------------------------------------------------------------------
22 ; initstdout: Open the stdout and stderr file descriptors for the screen.
23
24 .proc   initstdout
25
26         lda     #LFN_WRITE
27         sta     fdtab+STDOUT_FILENO
28         sta     fdtab+STDERR_FILENO
29         lda     #CBMDEV_SCREEN
30         sta     unittab+STDOUT_FILENO
31         sta     unittab+STDERR_FILENO
32         lda     #STDOUT_FILENO + LFN_OFFS
33         jsr     @L1
34         lda     #STDERR_FILENO + LFN_OFFS
35 @L1:    ldx     #CBMDEV_SCREEN
36         ldy     #$FF
37         jsr     SETLFS
38         jmp     OPEN            ; Will always succeed
39
40 .endproc
41
42 ;--------------------------------------------------------------------------
43 ; _write
44
45
46 .proc   _write
47
48         jsr     rwcommon        ; Pop params, check handle
49         bcs     invalidfd       ; Branch if handle not ok
50
51 ; Check if the LFN is valid and the file is open for writing
52
53         adc     #LFN_OFFS       ; Carry is already clear
54         tax
55         lda     fdtab-LFN_OFFS,x; Get flags for this handle
56         and     #LFN_WRITE      ; File open for writing?
57         beq     notopen
58
59 ; Valid lfn. Make it the output file
60
61         jsr     CKOUT
62         bcs     error
63         bcc     @L2
64
65 ; Output the next character from the buffer
66
67 @L0:    ldy     #0
68         lda     (ptr2),y
69         inc     ptr2
70         bne     @L1
71         inc     ptr2+1          ; A = *buf++;
72 @L1:    jsr     BSOUT
73         bcs     error           ; Bail out on errors
74
75 ; Count characters written
76
77         inc     ptr3
78         bne     @L2
79         inc     ptr3+1
80
81 ; Decrement count
82
83 @L2:    inc     ptr1
84         bne     @L0
85         inc     ptr1+1
86         bne     @L0
87
88 ; Wrote all chars, close the output channel
89
90         jsr     CLRCH
91
92 ; Return the number of chars written
93
94         lda     ptr3
95         ldx     ptr3+1
96         rts
97
98 ; Error entry, file descriptor is invalid
99
100 invalidfd:
101         lda     #EINVAL
102         sta     __errno
103         lda     #0
104         sta     __errno+1
105         beq     errout
106
107 ; Error entry, file is not open
108
109 notopen:
110         lda     #3              ; File not open
111         bne     error
112
113 ; Error entry, status not ok
114
115 error5: lda     #5              ; Device not present
116 error:  sta     __oserror
117 errout: lda     #$FF
118         tax                     ; Return -1
119         rts
120
121 .endproc
122
123
124
125