2002-11-16 23:45:15 +00:00
|
|
|
;
|
2013-12-23 16:48:58 +00:00
|
|
|
; 2002-11-16, Ullrich von Bassewitz
|
2013-12-23 17:20:28 +00:00
|
|
|
; 2013-12-23, Greg King
|
2002-11-16 23:45:15 +00:00
|
|
|
;
|
|
|
|
; int read (int fd, void* buf, unsigned count);
|
|
|
|
;
|
|
|
|
|
|
|
|
.export _read
|
|
|
|
.constructor initstdin
|
2002-11-19 23:02:47 +00:00
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
.import rwcommon
|
2002-11-16 23:45:15 +00:00
|
|
|
.import popax
|
2002-11-17 22:45:55 +00:00
|
|
|
.importzp ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
|
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"
|
|
|
|
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
; initstdin: Open the stdin file descriptors for the keyboard
|
2010-06-26 11:28:44 +00:00
|
|
|
|
2016-03-06 20:26:22 +00:00
|
|
|
.segment "ONCE"
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.proc initstdin
|
|
|
|
|
|
|
|
lda #STDIN_FILENO + LFN_OFFS
|
|
|
|
ldx #CBMDEV_KBD
|
|
|
|
ldy #$FF
|
|
|
|
jsr SETLFS
|
|
|
|
jmp OPEN ; Will always succeed
|
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
; _read
|
|
|
|
|
2005-02-26 09:28:46 +00:00
|
|
|
.code
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
.proc _read
|
|
|
|
|
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
|
2013-12-23 17:20:28 +00:00
|
|
|
tay
|
2002-11-16 23:45:15 +00:00
|
|
|
and #LFN_READ ; File open for writing?
|
2010-06-26 11:28:44 +00:00
|
|
|
beq invalidfd
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
; Check the EOF flag. If it is set, don't read anything
|
|
|
|
|
2013-12-23 17:20:28 +00:00
|
|
|
tya ; Get flags again
|
2002-11-17 22:45:55 +00:00
|
|
|
bmi eof
|
|
|
|
|
2013-12-23 16:48:58 +00:00
|
|
|
; Remember the device number.
|
|
|
|
|
|
|
|
ldy unittab-LFN_OFFS,x
|
|
|
|
sty unit
|
|
|
|
|
2002-11-16 23:45:15 +00:00
|
|
|
; Valid lfn. Make it the input file
|
|
|
|
|
|
|
|
jsr CHKIN
|
2010-06-26 11:28:44 +00:00
|
|
|
bcc @L3 ; Branch if ok
|
|
|
|
jmp __mappederrno ; Store into __oserror, map to errno, return -1
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Read the next byte
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
@L0: jsr BASIN
|
2002-11-16 23:45:15 +00:00
|
|
|
sta tmp1 ; Save the input byte
|
2013-12-23 16:48:58 +00:00
|
|
|
ldx unit
|
|
|
|
bne @L0_1 ; Not keyboard/screen-editor
|
|
|
|
cmp #$0D ; Is it a Carriage Return?
|
|
|
|
bne @L0_1
|
|
|
|
jsr BSOUT ; Yes, echo it (because editor didn't)
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2013-12-23 16:48:58 +00:00
|
|
|
@L0_1: jsr READST ; Read the IEEE status
|
2002-11-17 22:45:55 +00:00
|
|
|
sta tmp3 ; Save it
|
2002-11-16 23:45:15 +00:00
|
|
|
and #%10111111 ; Check anything but the EOI bit
|
2010-06-26 11:28:44 +00:00
|
|
|
bne devnotpresent ; Assume device not present
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Store the byte just read
|
|
|
|
|
|
|
|
ldy #0
|
|
|
|
lda tmp1
|
2018-05-21 14:21:45 +00:00
|
|
|
sta (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 ; *buf++ = A;
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Increment the byte count
|
|
|
|
|
|
|
|
@L1: inc ptr3
|
|
|
|
bne @L2
|
|
|
|
inc ptr3+1
|
|
|
|
|
|
|
|
; Get the status again and check the EOI bit
|
|
|
|
|
2002-11-17 22:45:55 +00:00
|
|
|
@L2: lda tmp3
|
2002-11-16 23:45:15 +00:00
|
|
|
and #%01000000 ; Check for EOI
|
2002-11-17 22:45:55 +00:00
|
|
|
bne @L4 ; Jump if end of file reached
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Decrement the count
|
|
|
|
|
2018-05-21 14:21:45 +00:00
|
|
|
@L3: inc ptr2
|
2002-11-17 22:45:55 +00:00
|
|
|
bne @L0
|
2018-05-21 14:21:45 +00:00
|
|
|
inc ptr2+1
|
2002-11-17 22:45:55 +00:00
|
|
|
bne @L0
|
|
|
|
beq done ; Branch always
|
|
|
|
|
|
|
|
; Set the EOI flag and bail out
|
|
|
|
|
|
|
|
@L4: ldx tmp2 ; Get the handle
|
|
|
|
lda #LFN_EOF
|
|
|
|
ora fdtab,x
|
|
|
|
sta fdtab,x
|
2002-11-16 23:45:15 +00:00
|
|
|
|
|
|
|
; Read done, close the input channel
|
|
|
|
|
|
|
|
done: jsr CLRCH
|
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
; Clear _oserror and return the number of chars read
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
eof: lda #0
|
|
|
|
sta __oserror
|
|
|
|
lda ptr3
|
2002-11-16 23:45:15 +00:00
|
|
|
ldx ptr3+1
|
|
|
|
rts
|
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
; Error entry: Device not present
|
2002-11-16 23:45:15 +00:00
|
|
|
|
2010-06-26 11:28:44 +00:00
|
|
|
devnotpresent:
|
|
|
|
lda #ENODEV
|
|
|
|
jmp __directerrno ; Sets _errno, clears _oserror, returns -1
|
2002-11-19 23:02:47 +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
|
|
|
|
|
|
|
|
|
2013-12-23 16:48:58 +00:00
|
|
|
;--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
unit: .res 1
|