]> git.sur5r.net Git - cc65/blobdiff - libsrc/cbm/read.s
Removed .import for std kernal entries. Added .include "cbm.inc"
[cc65] / libsrc / cbm / read.s
index 5a2157b998bb8e636fc82aef84509e53780bac1d..ee01596aa6ada0881eabfb0905d4cc7eeb76e252 100644 (file)
@@ -1,33 +1,32 @@
 ;
-; Ullrich von Bassewitz, 16.11.2002
+; 2002-11-16, Ullrich von Bassewitz
+; 2013-12-23, Greg King
 ;
 ; int read (int fd, void* buf, unsigned count);
 ;
 
         .export         _read
         .constructor    initstdin
-           
+
         .import         rwcommon
         .import         popax
-        .import         __errno, __oserror
         .importzp       ptr1, ptr2, ptr3, tmp1, tmp2, tmp3
 
+        .include        "cbm.inc"
         .include        "errno.inc"
         .include        "fcntl.inc"
-        .include        "cbm.inc"
         .include        "filedes.inc"
 
 
 ;--------------------------------------------------------------------------
 ; initstdin: Open the stdin file descriptors for the keyboard
 
+.segment        "ONCE"
+
 .proc   initstdin
 
-        lda     #LFN_READ
-        sta     fdtab+STDIN_FILENO
         lda     #STDIN_FILENO + LFN_OFFS
         ldx     #CBMDEV_KBD
-        stx     unittab+STDIN_FILENO
         ldy     #$FF
         jsr     SETLFS
         jmp     OPEN            ; Will always succeed
 ;--------------------------------------------------------------------------
 ; _read
 
+.code
 
 .proc   _read
 
         jsr     rwcommon        ; Pop params, check handle
-        bcs     invalidfd       ; Branch if handle not ok
+        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
+        tay
         and     #LFN_READ       ; File open for writing?
-        beq     notopen
+        beq     invalidfd
 
 ; Check the EOF flag. If it is set, don't read anything
 
-        lda     fdtab-LFN_OFFS,x; Get flags for this handle
+        tya                     ; Get flags again
         bmi     eof
 
-; Valid lfn. Make it the input file
+; Remember the device number.
 
-        jsr     CHKIN
-        bcs     error
+        ldy     unittab-LFN_OFFS,x
+        sty     unit
 
-; Go looping...
+; Valid lfn. Make it the input file
 
-        bcc     @L3             ; Branch always
+        jsr     CHKIN
+        bcc     @L3             ; Branch if ok
+        jmp     __mappederrno   ; Store into __oserror, map to errno, return -1
 
 ; Read the next byte
 
 @L0:    jsr     BASIN
         sta     tmp1            ; Save the input byte
+        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)
 
-        jsr     READST          ; Read the IEEE status
+@L0_1:  jsr     READST          ; Read the IEEE status
         sta     tmp3            ; Save it
         and     #%10111111      ; Check anything but the EOI bit
-        bne     error5          ; Assume device not present
+        bne     devnotpresent   ; Assume device not present
 
 ; Store the byte just read
 
         ldy     #0
         lda     tmp1
-        sta     (ptr2),y
-        inc     ptr2
+        sta     (ptr1),y
+        inc     ptr1
         bne     @L1
-        inc     ptr2+1          ; *buf++ = A;
+        inc     ptr1+1          ; *buf++ = A;
 
 ; Increment the byte count
 
 
 ; Decrement the count
 
-@L3:    inc     ptr1
+@L3:    inc     ptr2
         bne     @L0
-        inc     ptr1+1
+        inc     ptr2+1
         bne     @L0
         beq     done            ; Branch always
 
 
 done:   jsr     CLRCH
 
-; Return the number of chars read
+; Clear _oserror and return the number of chars read
 
-eof:    lda     ptr3
+eof:    lda     #0
+        sta     __oserror
+        lda     ptr3
         ldx     ptr3+1
         rts
 
-; Error entry, file descriptor is invalid
+; Error entry: Device not present
+
+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     #EINVAL
-        sta     __errno
-        lda     #0
-        sta     __errno+1
-        beq     errout
-
-; Error entry, file is not open
-
-notopen:
-        lda     #3              ; File not open
-        bne     error
-                    
-; Error entry, status not ok
-
-error5: lda     #5              ; Device not present
-error:  sta     __oserror
-errout: lda     #$FF
-        tax                     ; Return -1
-        rts
+        lda     #EBADF
+        jmp     __directerrno   ; Sets _errno, clears _oserror, returns -1
 
 .endproc
 
 
+;--------------------------------------------------------------------------
 
+.bss
 
+unit:   .res    1