2002-11-16 23:45:15 +00:00
|
|
|
;
|
|
|
|
; Ullrich von Bassewitz, 16.11.2002
|
|
|
|
;
|
|
|
|
; int write (int fd, const void* buf, unsigned count);
|
|
|
|
;
|
|
|
|
|
|
|
|
.export _write
|
|
|
|
.constructor initstdout
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
.import rwcommon
|
|
|
|
.importzp sp, ptr1, ptr2, ptr3
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.include "cbm.inc"
|
2010-06-26 11:28:44 +00:00
|
|
|
.include "errno.inc"
|
|
|
|
.include "fcntl.inc"
|
2002-11-16 23:45:15 +00:00
|
|
|
.include "filedes.inc"
|
|
|
|
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
; initstdout: Open the stdout and stderr file descriptors for the screen.
|
|
|
|
|
2016-03-06 20:26:22 +00:00
|
|
|
.segment "ONCE"
|
2005-02-26 09:28:46 +00:00
|
|
|
|
2002-11-16 23:45:15 +00:00
|
|
|
.proc initstdout
|
|
|
|
|
|
|
|
lda #STDOUT_FILENO + LFN_OFFS
|
|
|
|
jsr @L1
|
|
|
|
lda #STDERR_FILENO + LFN_OFFS
|
|
|
|
@L1: ldx #CBMDEV_SCREEN
|
|
|
|
ldy #$FF
|
|
|
|
jsr SETLFS
|
|
|
|
jmp OPEN ; Will always succeed
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
; _write
|
2010-06-26 11:28:44 +00:00
|
|
|
|
2005-02-26 09:28:46 +00:00
|
|
|
.code
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.proc _write
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
jsr rwcommon ; Pop params, check handle
|
2010-06-26 11:28:44 +00:00
|
|
|
bcs invalidfd ; Invalid handle
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Check if the LFN is valid and the file is open for writing
|
|
|
|
|
|
|
|
adc #LFN_OFFS ; Carry is already clear
|
|
|
|
tax
|
|
|
|
lda fdtab-LFN_OFFS,x; Get flags for this handle
|
|
|
|
and #LFN_WRITE ; File open for writing?
|
2010-06-26 11:28:44 +00:00
|
|
|
beq invalidfd
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Valid lfn. Make it the output file
|
|
|
|
|
|
|
|
jsr CKOUT
|
2002-11-17 22:45:55 +00:00
|
|
|
bcc @L2
|
2010-06-26 11:28:44 +00:00
|
|
|
@error: jmp __mappederrno ; Store into __oserror, map to errno, return -1
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
; Output the next character from the buffer
|
|
|
|
|
2002-11-18 18:22:26 +00:00
|
|
|
@L0: ldy #0
|
2018-05-21 14:21:45 +00:00
|
|
|
lda (ptr1),y
|
|
|
|
inc ptr1
|
2002-11-16 23:45:15 +00:00
|
|
|
bne @L1
|
2018-05-21 14:21:45 +00:00
|
|
|
inc ptr1+1 ; A = *buf++;
|
2002-11-16 23:45:15 +00:00
|
|
|
@L1: jsr BSOUT
|
2012-04-25 20:00:09 +00:00
|
|
|
|
|
|
|
; Check the status
|
|
|
|
|
|
|
|
pha
|
|
|
|
jsr READST
|
|
|
|
lsr a ; Bit zero is write timeout
|
|
|
|
bne devnotpresent2
|
|
|
|
pla
|
|
|
|
bcs @L3
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
; Count characters written
|
|
|
|
|
|
|
|
inc ptr3
|
|
|
|
bne @L2
|
|
|
|
inc ptr3+1
|
|
|
|
|
2002-11-16 23:45:15 +00:00
|
|
|
; Decrement count
|
|
|
|
|
2020-11-01 21:59:07 +00:00
|
|
|
@L2: dec ptr2
|
2002-11-17 22:45:55 +00:00
|
|
|
bne @L0
|
2020-11-01 21:59:07 +00:00
|
|
|
dec ptr2+1
|
2002-11-17 22:45:55 +00:00
|
|
|
bne @L0
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-04-25 20:00:09 +00:00
|
|
|
; Wrote all chars or disk full. Close the output channel
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-04-25 20:00:09 +00:00
|
|
|
@L3: jsr CLRCH
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
; Clear _oserror and return the number of chars written
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
lda #0
|
|
|
|
sta __oserror
|
2002-11-17 22:45:55 +00:00
|
|
|
lda ptr3
|
|
|
|
ldx ptr3+1
|
|
|
|
rts
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
; Error entry: Device not present
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2012-04-25 20:00:09 +00:00
|
|
|
devnotpresent2:
|
|
|
|
pla
|
2010-06-26 11:28:44 +00:00
|
|
|
devnotpresent:
|
|
|
|
lda #ENODEV
|
2022-01-30 02:15:28 +00:00
|
|
|
jmp __directerrno ; Sets _errno, clears _oserror, returns -1
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
; Error entry: The given file descriptor is not valid or not open
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
invalidfd:
|
|
|
|
lda #EBADF
|
|
|
|
jmp __directerrno ; Sets _errno, clears _oserror, returns -1
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.endproc
|