.constructor initstdout
.import rwcommon
- .import __errno, __oserror
.importzp sp, ptr1, ptr2, ptr3
+ .include "cbm.inc"
.include "errno.inc"
.include "fcntl.inc"
- .include "cbm.inc"
.include "filedes.inc"
;--------------------------------------------------------------------------
; initstdout: Open the stdout and stderr file descriptors for the screen.
+.segment "ONCE"
+
.proc initstdout
- lda #LFN_WRITE
- sta fdtab+STDOUT_FILENO
- sta fdtab+STDERR_FILENO
- lda #CBMDEV_SCREEN
- sta unittab+STDOUT_FILENO
- sta unittab+STDERR_FILENO
lda #STDOUT_FILENO + LFN_OFFS
jsr @L1
lda #STDERR_FILENO + LFN_OFFS
;--------------------------------------------------------------------------
; _write
+.code
.proc _write
jsr rwcommon ; Pop params, check handle
- bcs invalidfd ; Branch if handle not ok
+ bcs invalidfd ; Invalid handle
; Check if the LFN is valid and the file is open for writing
tax
lda fdtab-LFN_OFFS,x; Get flags for this handle
and #LFN_WRITE ; File open for writing?
- beq notopen
+ beq invalidfd
; Valid lfn. Make it the output file
jsr CKOUT
- bcs error
bcc @L2
-
-; Read the IEEE488 status
-
-@L0: jsr READST
- cmp #0
- bne error5
+@error: jmp __mappederrno ; Store into __oserror, map to errno, return -1
; Output the next character from the buffer
- ldy #0
- lda (ptr2),y
- inc ptr2
+@L0: ldy #0
+ lda (ptr1),y
+ inc ptr1
bne @L1
- inc ptr2+1 ; A = *buf++;
+ inc ptr1+1 ; A = *buf++;
@L1: jsr BSOUT
+; Check the status
+
+ pha
+ jsr READST
+ lsr a ; Bit zero is write timeout
+ bne devnotpresent2
+ pla
+ bcs @L3
+
; Count characters written
inc ptr3
; Decrement count
-@L2: inc ptr1
+@L2: inc ptr2
bne @L0
- inc ptr1+1
+ inc ptr2+1
bne @L0
-; Wrote all chars, close the output channel
+; Wrote all chars or disk full. Close the output channel
- jsr CLRCH
+@L3: jsr CLRCH
-; Return the number of chars written
+; Clear _oserror and return the number of chars written
+ lda #0
+ sta __oserror
lda ptr3
ldx ptr3+1
rts
-; Error entry, file descriptor is invalid
-
-invalidfd:
- lda #EINVAL
- sta __errno
- lda #0
- sta __errno+1
- beq errout
-
-; Error entry, file is not open
+; Error entry: Device not present
-notopen:
- lda #3 ; File not open
- bne error
+devnotpresent2:
+ pla
+devnotpresent:
+ lda #ENODEV
+ jmp __directerrno ; Sets _errno, clears _oserror, returns -1
-; Error entry, status not ok
+; Error entry: The given file descriptor is not valid or not open
-error5: lda #5 ; Device not present
-error: sta __oserror
-errout: lda #$FF
- tax ; Return -1
- rts
+invalidfd:
+ lda #EBADF
+ jmp __directerrno ; Sets _errno, clears _oserror, returns -1
.endproc
-
-
-
-