Subversion Repositories Spectranet

[/] [branches/] [gnubinutils/] [modules/] [streams/] [ctrlchan.asm] - Blame information for rev 380

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 308 winston
;The MIT License
2
;
3
;Copyright (c) 2010 Dylan Smith
4
;
5
;Permission is hereby granted, free of charge, to any person obtaining a copy
6
;of this software and associated documentation files (the "Software"), to deal
7
;in the Software without restriction, including without limitation the rights
8
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
;copies of the Software, and to permit persons to whom the Software is
10
;furnished to do so, subject to the following conditions:
11
;
12
;The above copyright notice and this permission notice shall be included in
13
;all copies or substantial portions of the Software.
14
;
15
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
;THE SOFTWARE.
22
 
23
; Control channel for BASIC streams.
24
; The control channel allows the BASIC programmer to find out the state
25
; of a stream and do things to the stream.
26
 
27
; The control streams have different metadata than a normal file or
28
; socket. There should be only one control stream so it has only one
29
; (very short) buffer in a fixed location. Therefore the stream
30
; information block that normally holds things like buffer offsets instead
31
; has:
32
; byte 0: flags
33
; byte 1: current command
34
; byte 2: state of current cmd
35
; byte 3: stream to act upon
36 380 winston
.include        "zxrom.inc"
37
.include        "zxsysvars.inc"
38
.include        "spectranet.inc"
39
.include        "defs.inc"
40
.text
41 308 winston
;-------------------------------------------------------------------------
42
; Control stream entry point
43 380 winston
.globl F_control
44
F_control:
45 308 winston
        bit 7, l
46
        jr nz, F_read_ctrlstream
47
; F_write_ctrlstream
48
; Sets the function of the control stream. This allows the BASIC
49
; programmer do something like "INPUT#4;"command";a%".
50
; Enter with L=stream
51 380 winston
.globl F_write_ctrlstream
52
F_write_ctrlstream:
53 308 winston
        ex af, af'
54
        call F_findmetadata     ; find our metadata table
55
        ex af, af'
56
        cp 0x21                 ; check it's not a control char of some sort
57
        jp c, F_leave           ; if so ignore it
58
        ld (ix+OFS_CURCMD), a   ; store the putative command
59
        ld (ix+OFS_CURSTATE), 0 ; clear the current state
60
        jp F_leave
61
 
62
;-------------------------------------------------------------------------
63
; F_read_ctrlstream
64 380 winston
.globl F_read_ctrlstream
65
F_read_ctrlstream:
66 308 winston
        call F_findmetadata     ; stream metadata now pointed to by IX
67
 
68
        ld hl, ZX_TV_FLAG       ; the mode is considered unchanged
69
        res 3, (hl)
70 380 winston
.isinkey3:                      ; The control stream only works with
71 308 winston
        ld hl, (ZX_ERR_SP)      ; INPUT#, make INKEY$ a no-op
72
        ld e, (hl)
73
        inc hl
74
        ld d, (hl)
75
        and a
76
        ld hl, 0x107F           ; Compare with ED_ERROR
77
        sbc hl, de
78 380 winston
        jp nz, .wasinkey3
79 308 winston
 
80
        ld a, (ix+OFS_CURCMD)   ; find out the command in use
81
        cp CMD_POLLALL
82
        jp z, J_pollall
83
        and a
84 380 winston
        jr z, .nocmd3
85 308 winston
 
86 380 winston
.badcmd3:
87 308 winston
        ld hl, STR_badcmd       ; Unknown command
88
        jp F_sendbuf
89 380 winston
.nocmd3:
90 308 winston
        ld hl, STR_nocmd
91
        jp F_sendbuf
92 380 winston
.wasinkey3:
93 308 winston
        xor a                   ; Z and C reset - no data
94
        ld l, 1                 ; Don't fix the stack
95
        jp F_leave
96
 
97
;-------------------------------------------------------------------------
98
; J_pollall
99
; Find out which stream (if any) has data waiting for it.
100
; Note that not all sockets may be being used by BASIC, some may be used
101
; by ROM modules or other machine code programs. If a socket that's
102
; ready is not for a BASIC stream it gets ignored.
103 380 winston
J_pollall:
104 308 winston
        ld a, (ix+OFS_CURSTATE) ; check current state
105
        and a                   ; if is zero, do the actual poll
106 380 winston
        jr nz, .retsockstate3   ; return the socket state
107 308 winston
        ld (ix+OFS_CURSTATE), 1 ; set current state flag to "get status"
108
 
109
        ; Check to find out whether there is pending data already
110
        ; in the buffer.
111 380 winston
.checkbufs3:
112 308 winston
        ; start with HL pointing at the first potential FD
113
        ld hl, BUFMETADATA+STRM_FLAGS+BUFDATASZ
114
        ld b, 15                ; maximum number of iterations
115
        ld c, a                 ; store fd
116 380 winston
.findloop_buf3:
117 308 winston
        xor a                   ; check flags for the stream
118
        cp (hl)                 ; - for a socket, should all be zero
119 380 winston
        jr nz, .nextbuf3
120 308 winston
        dec l                   ; point at the read buffer current
121
        dec l                   ; pointer
122
        cp (hl)
123 380 winston
        jr nz, .bufrdy3         ; a buffer still has stuff in it
124 308 winston
        inc l
125
        inc l                   ; restore HL
126 380 winston
.nextbuf3:
127 308 winston
        ld a, BUFDATASZ         ; move to the next channel info
128
        add a, l
129
        ld l, a
130 380 winston
        djnz .findloop_buf3
131 308 winston
 
132
        ; no buffers have pending data, poll the sockets.
133
        call POLLALL            ; poll all open sockets
134 380 winston
        jr z, .none3            ; nothing waiting
135 308 winston
        ld (ix+OFS_CURDATA), c  ; save socket state
136
 
137
        ; start with HL pointing at the first potential FD
138
        ld hl, BUFMETADATA+STRM_FLAGS+BUFDATASZ
139
        ld b, 15                ; maximum number of iterations
140
        ld c, a                 ; store fd
141 380 winston
.findloop3:
142 308 winston
        xor a                   ; check flags for the stream
143
        cp (hl)                 ; - for a socket, should all be zero
144 380 winston
        jr nz, .next3
145 308 winston
        ld a, c                 ; check FD = stream's FD
146
        dec l
147
        cp (hl)
148 380 winston
        jr z, .foundchan3
149 308 winston
        inc l
150 380 winston
.next3:
151 308 winston
        ld a, BUFDATASZ         ; move to the next channel info
152
        add a, l
153
        ld l, a
154 380 winston
        djnz .findloop3
155
        jr .checkbufs3          ; something not ready for us, but check
156 308 winston
                                ; our buffers too.
157
 
158 380 winston
.bufrdy3:
159 308 winston
        set BIT_RECV, (ix+OFS_CURDATA)
160 380 winston
.foundchan3:
161 308 winston
        ld a, 16                ; maximum stream number
162
        sub b                   ; subtract where we got to
163
        ld hl, INTERPWKSPC      ; and convert it to a string
164
        call ITOA8
165
        ld (hl), 0              ; add the null
166
        ld hl, INTERPWKSPC      ; and send it to BASIC's input buffer.
167
        jp F_sendbuf
168
 
169 380 winston
.retsockstate3:
170 308 winston
        ld (ix+OFS_CURSTATE), 0 ; reset state flags
171
        ld a, (ix+OFS_CURDATA)  ; get the socket's state
172
        bit BIT_RECV, a         ; Received data?
173 380 winston
        jr nz, .recv3
174 308 winston
        bit BIT_DISCON, a       ; Disconnected?
175 380 winston
        jr nz, .discon3
176 308 winston
        bit BIT_CONN, a         ; Connected?
177 380 winston
        jr nz, .connected3
178 308 winston
        ld hl, STR_unk
179
        jp F_sendbuf
180 380 winston
.recv3:
181 308 winston
        ld hl, STR_recv
182
        jp F_sendbuf
183 380 winston
.discon3:
184 308 winston
        ld hl, STR_discon
185
        jp F_sendbuf
186 380 winston
.connected3:
187 308 winston
        ld hl, STR_conn
188
        jp F_sendbuf
189 380 winston
.none3:
190 308 winston
        ld hl, STR_zero
191
        jp F_sendbuf
192
 
193
;-------------------------------------------------------------------------
194
; Send the command buffer. Buffer is at (HL)
195 380 winston
.globl F_sendbuf
196
F_sendbuf:
197 308 winston
        ld a, (hl)
198
        and a                   ; end of buffer?
199 380 winston
        jr z, .bufdone4
200 308 winston
        push hl
201
        rst CALLBAS
202
        defw 0x0F85             ; ZX_ADD_CHAR - put a character into
203
        pop hl                  ; INPUT's buffer.
204
        inc hl
205
        jr F_sendbuf
206 380 winston
.bufdone4:
207 308 winston
        ld a, 0x0d              ; put a CR as the last item
208
        ld l, 0                 ; and signal "munge the stack"
209
        scf
210
        jp F_leave              ; done.
211
 
212
; Some pre-defined strings to return to BASIC.
213 380 winston
STR_badcmd:     defb "err Bad command",0
214
STR_nocmd:      defb "err No command",0
215
STR_unk:                defb "unknown",0
216
STR_recv:       defb "recv",0
217
STR_discon:     defb "disconn",0
218
STR_conn:       defb "conn",0
219
STR_zero:       defb "0",0
220 308 winston