]> git.sur5r.net Git - cc65/blob - libsrc/atmos/read.s
Merge pull request #114 from greg-king5/phantom
[cc65] / libsrc / atmos / read.s
1 ;
2 ; 2013-12-24, 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
15         .macpack        generic
16         .include        "atmos.inc"
17
18 .proc   _read
19
20         sta     ptr3
21         stx     ptr3+1          ; save count as result
22         eor     #$FF
23         sta     ptr2
24         txa
25         eor     #$FF
26         sta     ptr2+1          ; Remember -count-1
27
28         jsr     popax           ; get buf
29         sta     ptr1
30         stx     ptr1+1
31         jsr     popax           ; get fd and discard
32
33 L1:     inc     ptr2
34         bnz     L2
35         inc     ptr2+1
36         bze     L9              ; no more room in buf
37
38 ; If there are no more characters in BASIC's input buffer, then get a line from
39 ; the console into that buffer.
40
41 L2:     ldx     text_count
42         bpl     L3
43         jsr     GETLINE
44         ldx     #<(0 - 1)
45
46 L3:     inx
47         lda     BASIC_BUF,x
48         bnz     L4              ; (zero-terminated buffer)
49         ldx     #<-1
50         lda     #$0A            ; return newline char. at end of line
51 L4:     stx     text_count
52         ldy     #0
53         sta     (ptr1),y
54         inc     ptr1
55         bnz     L1
56         inc     ptr1+1
57         bnz     L1              ; branch always
58
59 ; No error, return count.
60
61 L9:     lda     ptr3
62         ldx     ptr3+1
63         rts
64
65 .endproc
66
67
68 ;--------------------------------------------------------------------------
69 ; initstdin:  Reset the stdin console.
70
71 .segment        "INIT"
72
73 initstdin:
74         ldx     #<-1
75         stx     text_count
76         rts
77
78
79 ;--------------------------------------------------------------------------
80
81 .bss
82
83 text_count:
84         .res    1
85