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