Subversion Repositories Spectranet

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 226 winston
;The MIT License
2
;
3
;Copyright (c) 2009 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 380 winston
.include        "defs.inc"
23
.include        "spectranet.inc"
24
.text
25 226 winston
; Buffer management routines.
26
; Note - the memory we want to use should be paged into area A.
27
 
28
 
29
;---------------------------------------------------------------------------
30
; Dealing with buffers
31
; Buffers start from 0x1100, with a 256 byte buffer for each possible
32
; stream.
33
; At 0x1000 there is buffer information which is formatted as follows:
34
; Byte 0 - Write buffer number
35
; Byte 1 - Read buffer number
36
; byte 2 - Write buffer pointer
37
; byte 3 - read buffer pointer
38
; Byte 4 - Current descriptor (a socket handle, perhaps)
39 243 winston
; Byte 5-7 - Data specific to the kind of stream:
40
; Byte 5 - Flag bits (bit 0 = socket or file)
41 226 winston
 
42
;--------------------------------------------------------------------------
43
; F_findfreebuf - Returns the number of the first free buffer found.
44
; Carry set if no buffer available.
45 380 winston
.globl F_findfreebuf
46
F_findfreebuf:
47 226 winston
        push hl
48
        ld b, 0x1F              ; number of buffers available
49 380 winston
.findloop1:
50 226 winston
        ld hl, 0x1000           ; address to start from in metadata page
51 380 winston
.next1:
52 226 winston
        ld a, (hl)
53
        cp b
54 380 winston
        jr z, .inuse1
55 226 winston
        inc l                   ; check second buffer
56
        ld a, (hl)
57
        cp b
58 380 winston
        jr z, .inuse1
59 226 winston
        ld a, 7                 ; advance by 7 bytes
60
        add a, l
61 380 winston
        jr z, .free1            ; this buffer number wasn't found, it's free
62 226 winston
        ld l, a
63 380 winston
        jr .next1
64
.inuse1:
65
        djnz .findloop1         ; try the next buffer number
66 226 winston
        pop hl
67
        scf                     ; we ran out of attempts
68
        ret
69 380 winston
.free1:
70 226 winston
        pop hl
71
        ld a, b                 ; get result into A
72
        and a                   ; clear possible carry (from the add op)
73
        ret
74
 
75
;---------------------------------------------------------------------------
76
; F_feedbuffer
77
; Adds the byte in A to the buffer for the stream in L
78 380 winston
.globl F_feedbuffer
79
F_feedbuffer:
80 226 winston
        ex af, af'              ; save A
81 308 winston
        call F_findmetadata     ; IX = pointer to metadata
82 226 winston
 
83 308 winston
        bit BIT_RDONLY, (ix+STRM_FLAGS) ; if read only do nothing
84 243 winston
        ret nz
85 308 winston
        ld d, (ix+STRM_WRITEBUF); D = buffer number (happens to be the MSB)
86
        ld e, (ix+STRM_WRITEPTR); DE = current buffer pointer
87 226 winston
        ex af, af'              ; get byte back
88
        cp 0x0d                 ; Spectrum ENTER?
89 380 winston
        jr nz, .cont2
90 226 winston
 
91
        ld (de), a              ; Convert it to 0x0d 0x0a
92
        inc e
93
        ld a, 0x0a
94
        ld (de), a
95
        jr F_flushbuffer
96 380 winston
.cont2:
97 226 winston
        ld (de), a              ; write the value into the buffer
98
        ld a, 0xFE              ; it would be easier to flush one byte later
99
        cp e                    ; but we must leave enough room for CRLF
100
        jr z, F_flushbuffer     ; If the buffer is full flush it
101
 
102
        inc e                   ; increment the buffer pointer
103 308 winston
        ld (ix+STRM_WRITEPTR), e ; save the buffer pointer
104 226 winston
        ret
105
 
106
;---------------------------------------------------------------------------
107
; F_flushbuffer
108
; Flushes the given buffer
109
;       DE = current buffer pointer
110 308 winston
;       IX = current buffer info pointer
111 380 winston
.globl F_flushbuffer
112
F_flushbuffer:
113 226 winston
        ; TODO
114
        ; This needs to work with all kinds of streams, not just SOCK_STREAM
115 308 winston
        ld (ix+STRM_WRITEPTR), 0 ; reset buffer pointer
116
        ld a, (ix+STRM_FD)      ; get the file descriptor
117 226 winston
 
118
        ld b, 0                 ; set BC to the size of the buffer to send
119
        ld c, e                 ; inc BC to make the proper length
120
        inc bc
121
        ld e, 0                 ; set DE to the start of the buffer
122
 
123
        ; DE now = buffer start
124
        ; BC now = length
125
        ; A now = file descriptor
126 308 winston
        bit 0, (ix+STRM_FLAGS)  ; Check "is a file" flag
127 380 winston
        jr nz, .filewrite3
128 226 winston
        call SEND               ; Send the data
129 380 winston
        jr c, .borked3
130 226 winston
        ret
131
 
132 380 winston
.borked3:
133 226 winston
        ld a, 2                 ; todo - proper error handling
134
        out (254), a
135
        ret
136
 
137 380 winston
.filewrite3:
138 243 winston
        ; TODO: File I/O does not yet handle writing a buffer in page A
139
        ; because that's where the VFS mods put their data. So copy the
140
        ; data first. However, VFS modules ought to be able to cope with
141
        ; this.
142
        ex de, hl
143
        ld de, INTERPWKSPC      ; where to copy
144
        push bc
145
        ldir                    ; copy the buffer
146
        pop bc                  ; restore the length
147
        ld hl, INTERPWKSPC      ; start of new buffer
148
        call WRITE
149 380 winston
        jr c, .borked3
150 243 winston
        ret
151