]> git.sur5r.net Git - cc65/blob - libsrc/atmos/read.s
Remove trailings spaces from CBM-related asm files
[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, popptr1
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     popptr1         ; get buf
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
67 ;--------------------------------------------------------------------------
68 ; initstdin:  Reset the stdin console.
69
70 .segment        "ONCE"
71
72 initstdin:
73         ldx     #<-1
74         stx     text_count
75         rts
76
77
78 ;--------------------------------------------------------------------------
79
80 .segment        "INIT"
81
82 text_count:
83         .res    1