]> git.sur5r.net Git - cc65/blob - libsrc/atmos/read.s
Added a comment.
[cc65] / libsrc / atmos / read.s
1 ;
2 ; 2014-08-22, Greg King
3 ;
4 ; int read (int fd, void* buf, unsigned count);
5 ;
6 ; This function is a hack!  It lets us get text from the stdin console.
7 ;
8
9         .export         _read
10         .constructor    initstdin
11
12         .import         popax
13         .importzp       ptr1, ptr2, ptr3
14         .forceimport    disable_caps
15
16         .macpack        generic
17         .include        "atmos.inc"
18
19 .proc   _read
20
21         sta     ptr3
22         stx     ptr3+1          ; save count as result
23         eor     #$FF
24         sta     ptr2
25         txa
26         eor     #$FF
27         sta     ptr2+1          ; Remember -count-1
28
29         jsr     popax           ; get buf
30         sta     ptr1
31         stx     ptr1+1
32         jsr     popax           ; get fd and discard
33
34 L1:     inc     ptr2
35         bnz     L2
36         inc     ptr2+1
37         bze     L9              ; no more room in buf
38
39 ; If there are no more characters in BASIC's input buffer, then get a line from
40 ; the console into that buffer.
41
42 L2:     ldx     text_count
43         bpl     L3
44         jsr     GETLINE
45         ldx     #<(0 - 1)
46
47 L3:     inx
48         lda     BASIC_BUF,x
49         bnz     L4              ; (zero-terminated buffer)
50         ldx     #<-1
51         lda     #$0A            ; return newline char. at end of line
52 L4:     stx     text_count
53         ldy     #0
54         sta     (ptr1),y
55         inc     ptr1
56         bnz     L1
57         inc     ptr1+1
58         bnz     L1              ; branch always
59
60 ; No error, return count.
61
62 L9:     lda     ptr3
63         ldx     ptr3+1
64         rts
65
66 .endproc
67
68
69 ;--------------------------------------------------------------------------
70 ; initstdin:  Reset the stdin console.
71
72 .segment        "INIT"
73
74 initstdin:
75         ldx     #<-1
76         stx     text_count
77         rts
78
79
80 ;--------------------------------------------------------------------------
81
82 .bss
83
84 text_count:
85         .res    1
86