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