2 ; 2002-11-16, Ullrich von Bassewitz
3 ; 2013-12-23, Greg King
5 ; int read (int fd, void* buf, unsigned count);
11 .import SETLFS, OPEN, CHKIN, BASIN, CLRCH, BSOUT, READST
14 .importzp ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
19 .include "filedes.inc"
22 ;--------------------------------------------------------------------------
23 ; initstdin: Open the stdin file descriptors for the keyboard
30 sta fdtab+STDIN_FILENO
31 lda #STDIN_FILENO + LFN_OFFS
33 stx unittab+STDIN_FILENO
36 jmp OPEN ; Will always succeed
40 ;--------------------------------------------------------------------------
47 jsr rwcommon ; Pop params, check handle
48 bcs invalidfd ; Invalid handle
50 ; Check if the LFN is valid and the file is open for writing
52 adc #LFN_OFFS ; Carry is already clear
54 lda fdtab-LFN_OFFS,x; Get flags for this handle
56 and #LFN_READ ; File open for writing?
59 ; Check the EOF flag. If it is set, don't read anything
64 ; Remember the device number.
66 ldy unittab-LFN_OFFS,x
69 ; Valid lfn. Make it the input file
72 bcc @L3 ; Branch if ok
73 jmp __mappederrno ; Store into __oserror, map to errno, return -1
78 sta tmp1 ; Save the input byte
80 bne @L0_1 ; Not keyboard/screen-editor
81 cmp #$0D ; Is it a Carriage Return?
83 jsr BSOUT ; Yes, echo it (because editor didn't)
85 @L0_1: jsr READST ; Read the IEEE status
87 and #%10111111 ; Check anything but the EOI bit
88 bne devnotpresent ; Assume device not present
90 ; Store the byte just read
97 inc ptr2+1 ; *buf++ = A;
99 ; Increment the byte count
105 ; Get the status again and check the EOI bit
108 and #%01000000 ; Check for EOI
109 bne @L4 ; Jump if end of file reached
111 ; Decrement the count
117 beq done ; Branch always
119 ; Set the EOI flag and bail out
121 @L4: ldx tmp2 ; Get the handle
126 ; Read done, close the input channel
130 ; Clear _oserror and return the number of chars read
138 ; Error entry: Device not present
142 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
144 ; Error entry: The given file descriptor is not valid or not open
148 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
153 ;--------------------------------------------------------------------------