]> git.sur5r.net Git - cc65/blob - libsrc/cbm/write.s
Moved the 'disk' files from 'geos-cbm' to 'geos-common' which are believed to work...
[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         .importzp       sp, ptr1, ptr2, ptr3
13
14         .include        "cbm.inc"
15         .include        "errno.inc"
16         .include        "fcntl.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     invalidfd       ; Invalid handle
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     invalidfd
60
61 ; Valid lfn. Make it the output file
62
63         jsr     CKOUT
64         bcc     @L2
65 @error: jmp     __mappederrno   ; Store into __oserror, map to errno, return -1
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 ; Clear _oserror and return the number of chars written
95
96         lda     #0
97         sta     __oserror
98         lda     ptr3
99         ldx     ptr3+1
100         rts
101
102 ; Error entry: Device not present
103
104 devnotpresent:
105         lda     #ENODEV
106         jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
107
108 ; Error entry: The given file descriptor is not valid or not open
109
110 invalidfd:
111         lda     #EBADF
112         jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
113
114 .endproc
115
116
117
118