2 ; 2002-11-16, Ullrich von Bassewitz
3 ; 2013-12-23, Greg King
5 ; int read (int fd, void* buf, unsigned count);
13 .importzp ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
18 .include "filedes.inc"
21 ;--------------------------------------------------------------------------
22 ; initstdin: Open the stdin file descriptors for the keyboard
28 lda #STDIN_FILENO + LFN_OFFS
32 jmp OPEN ; Will always succeed
36 ;--------------------------------------------------------------------------
43 jsr rwcommon ; Pop params, check handle
44 bcs invalidfd ; Invalid handle
46 ; Check if the LFN is valid and the file is open for writing
48 adc #LFN_OFFS ; Carry is already clear
50 lda fdtab-LFN_OFFS,x; Get flags for this handle
52 and #LFN_READ ; File open for writing?
55 ; Check the EOF flag. If it is set, don't read anything
60 ; Remember the device number.
62 ldy unittab-LFN_OFFS,x
65 ; Valid lfn. Make it the input file
68 bcc @L3 ; Branch if ok
69 jmp __mappederrno ; Store into __oserror, map to errno, return -1
74 sta tmp1 ; Save the input byte
76 bne @L0_1 ; Not keyboard/screen-editor
77 cmp #$0D ; Is it a Carriage Return?
79 jsr BSOUT ; Yes, echo it (because editor didn't)
81 @L0_1: jsr READST ; Read the IEEE status
83 and #%10111111 ; Check anything but the EOI bit
84 bne devnotpresent ; Assume device not present
86 ; Store the byte just read
93 inc ptr1+1 ; *buf++ = A;
95 ; Increment the byte count
101 ; Get the status again and check the EOI bit
104 and #%01000000 ; Check for EOI
105 bne @L4 ; Jump if end of file reached
107 ; Decrement the count
113 beq done ; Branch always
115 ; Set the EOI flag and bail out
117 @L4: ldx tmp2 ; Get the handle
122 ; Read done, close the input channel
126 ; Clear _oserror and return the number of chars read
134 ; Error entry: Device not present
138 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
140 ; Error entry: The given file descriptor is not valid or not open
144 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
149 ;--------------------------------------------------------------------------