2 ; 2002-11-16, Ullrich von Bassewitz
3 ; 2013-12-18, 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
55 and #LFN_READ ; File open for writing?
58 ; Check the EOF flag. If it is set, don't read anything
60 lda fdtab-LFN_OFFS,x; Get flags for this handle
63 ; Remember the device number.
65 ldy unittab-LFN_OFFS,x
68 ; Valid lfn. Make it the input file
71 bcc @L3 ; Branch if ok
72 jmp __mappederrno ; Store into __oserror, map to errno, return -1
77 sta tmp1 ; Save the input byte
79 bne @L0_1 ; Not keyboard/screen-editor
80 cmp #$0D ; Is it a Carriage Return?
82 jsr BSOUT ; Yes, echo it (because editor didn't)
84 @L0_1: jsr READST ; Read the IEEE status
86 and #%10111111 ; Check anything but the EOI bit
87 bne devnotpresent ; Assume device not present
89 ; Store the byte just read
96 inc ptr2+1 ; *buf++ = A;
98 ; Increment the byte count
104 ; Get the status again and check the EOI bit
107 and #%01000000 ; Check for EOI
108 bne @L4 ; Jump if end of file reached
110 ; Decrement the count
116 beq done ; Branch always
118 ; Set the EOI flag and bail out
120 @L4: ldx tmp2 ; Get the handle
125 ; Read done, close the input channel
129 ; Clear _oserror and return the number of chars read
137 ; Error entry: Device not present
141 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
143 ; Error entry: The given file descriptor is not valid or not open
147 jmp __directerrno ; Sets _errno, clears _oserror, returns -1
152 ;--------------------------------------------------------------------------