]> git.sur5r.net Git - cc65/blobdiff - 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
index dc7bf348caf3cac63892feda9788fe65ee1033db..46bcfe407c825a22dd50857898646cd8b8dcbf1b 100644 (file)
@@ -7,11 +7,11 @@
         .export         _write
         .constructor    initstdout
 
-        .import         incsp6
-        .import         __errno, __oserror
-        .importzp       sp, ptr1, ptr2
+        .import         SETLFS, OPEN, CKOUT, BSOUT, CLRCH
+        .import         rwcommon
+        .import         __oserror
+        .importzp       sp, ptr1, ptr2, ptr3
 
-        .include        "errno.inc"
         .include        "fcntl.inc"
         .include        "cbm.inc"
         .include        "filedes.inc"
@@ -20,6 +20,8 @@
 ;--------------------------------------------------------------------------
 ; initstdout: Open the stdout and stderr file descriptors for the screen.
 
+.segment        "INIT"
+
 .proc   initstdout
 
         lda     #LFN_WRITE
 
 ;--------------------------------------------------------------------------
 ; _write
-
+                      
+.code
 
 .proc   _write
 
-        ldy     #4
-        lda     (sp),y          ; Get fd
-
-; Check if we have a valid handle
-
-        cmp     #MAX_FDS        ; Is it valid?
-        bcs     invalidfd       ; Jump if no
+        jsr     rwcommon        ; Pop params, check handle
+        bcs     errout          ; Invalid handle, errno already set
 
 ; Check if the LFN is valid and the file is open for writing
 
 
         jsr     CKOUT
         bcs     error
+        bcc     @L2
 
-; Calculate -count-1 for easier looping
-
-        ldy     #0
-        lda     (sp),y
-        eor     #$FF
-        sta     ptr1
-        iny
-        lda     (sp),y
-        eor     #$FF
-        sta     ptr1+1
-
-; Get the pointer to the data buffer
-
-        iny
-        lda     (sp),y
-        sta     ptr2
-        iny
-        lda     (sp),y
-        sta     ptr2+1
-        jmp     deccount
-
-; Read the IEEE488 status
+; Output the next character from the buffer
 
-loop:   jsr     READST
-        cmp     #0
-        bne     error5
-
-        ldy     #0
+@L0:    ldy     #0
         lda     (ptr2),y
         inc     ptr2
         bne     @L1
         inc     ptr2+1          ; A = *buf++;
-
 @L1:    jsr     BSOUT
+        bcs     error           ; Bail out on errors
+
+; Count characters written
+
+        inc     ptr3
+        bne     @L2
+        inc     ptr3+1
 
 ; Decrement count
 
-deccount:
-        inc     ptr1
-        bne     loop
+@L2:    inc     ptr1
+        bne     @L0
         inc     ptr1+1
-        bne     loop
+        bne     @L0
 
 ; Wrote all chars, close the output channel
 
@@ -114,21 +93,9 @@ deccount:
 
 ; Return the number of chars written
 
-        ldy     #1
-        lda     (sp),y
-        tax
-        dey
-        lda     (sp),y
-        jmp     incsp6
-
-; Error entry, file descriptor is invalid
-
-invalidfd:
-        lda     #EINVAL
-        sta     __errno
-        lda     #0
-        sta     __errno+1
-        beq     errout
+        lda     ptr3
+        ldx     ptr3+1
+        rts
 
 ; Error entry, file is not open
 
@@ -142,7 +109,7 @@ error5: lda     #5              ; Device not present
 error:  sta     __oserror
 errout: lda     #$FF
         tax                     ; Return -1
-        jmp     incsp6
+        rts
 
 .endproc