mirror of
https://github.com/cc65/cc65.git
synced 2024-12-24 11:31:31 +00:00
419eb700b5
The way we want to use the INITBSS segment - and especially the fact that it won't have the type bss on all ROM based targets - means that the name INITBSS is misleading. After all INIT is the best name from my perspective as it serves several purposes and therefore needs a rather generic name. Unfortunately this means that the current INIT segment needs to be renamed too. Looking for a short (ideally 4 letter) name I came up with ONCE as it contains all code (and data) accessed only once during initialization.
119 lines
2.7 KiB
ArmAsm
119 lines
2.7 KiB
ArmAsm
;
|
|
; Ullrich von Bassewitz, 16.11.2002
|
|
;
|
|
; int write (int fd, const void* buf, unsigned count);
|
|
;
|
|
|
|
.export _write
|
|
.constructor initstdout
|
|
|
|
.import SETLFS, OPEN, CKOUT, BSOUT, READST, CLRCH
|
|
.import rwcommon
|
|
.importzp sp, ptr1, ptr2, ptr3
|
|
|
|
.include "cbm.inc"
|
|
.include "errno.inc"
|
|
.include "fcntl.inc"
|
|
.include "filedes.inc"
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
; initstdout: Open the stdout and stderr file descriptors for the screen.
|
|
|
|
.segment "ONCE"
|
|
|
|
.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
|
|
|
|
.code
|
|
|
|
.proc _write
|
|
|
|
jsr rwcommon ; Pop params, check handle
|
|
bcs invalidfd ; Invalid handle
|
|
|
|
; 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?
|
|
beq invalidfd
|
|
|
|
; Valid lfn. Make it the output file
|
|
|
|
jsr CKOUT
|
|
bcc @L2
|
|
@error: jmp __mappederrno ; Store into __oserror, map to errno, return -1
|
|
|
|
; Output the next character from the buffer
|
|
|
|
@L0: ldy #0
|
|
lda (ptr2),y
|
|
inc ptr2
|
|
bne @L1
|
|
inc ptr2+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
|
|
bne @L2
|
|
inc ptr3+1
|
|
|
|
; Decrement count
|
|
|
|
@L2: inc ptr1
|
|
bne @L0
|
|
inc ptr1+1
|
|
bne @L0
|
|
|
|
; Wrote all chars or disk full. Close the output channel
|
|
|
|
@L3: jsr CLRCH
|
|
|
|
; Clear _oserror and return the number of chars written
|
|
|
|
lda #0
|
|
sta __oserror
|
|
lda ptr3
|
|
ldx ptr3+1
|
|
rts
|
|
|
|
; Error entry: Device not present
|
|
|
|
devnotpresent2:
|
|
pla
|
|
devnotpresent:
|
|
lda #ENODEV
|
|
jmp __directerrno ; Sets _errno, clears _oserror, returns -1
|
|
|
|
; Error entry: The given file descriptor is not valid or not open
|
|
|
|
invalidfd:
|
|
lda #EBADF
|
|
jmp __directerrno ; Sets _errno, clears _oserror, returns -1
|
|
|
|
.endproc
|