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