aboutsummaryrefslogtreecommitdiffstats
path: root/src/pipe.asm
blob: 1a77c5e0d5d814e8d2acf2706e9ebccf2218612c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
; =============================================================================
; pipe.asm - anonymous pipes: bounded in-kernel FIFOs with blocking ends.
;
; A pipe fd is $F0 + idx*2 (+1 for the write end); $FF stays "console". getb/
; putb/close dispatch pipe fds here. read blocks (yields) while empty and there
; is still a writer, then returns EOF once all writers are gone; write blocks
; while full and there is a reader, and if the last reader is gone the writer is
; killed (SIGPIPE). refcounts (writers/readers) are set to 1 by pipe() - the one
; producer and one consumer of a shell pipeline - and dropped on close/exit.
; =============================================================================
INCLUDE "include/gbos.inc"

SECTION "pipe_bss", WRAM0
wPipeW::    DS PIPE_MAX                 ; writers refcount (0 = end/free)
wPipeR::    DS PIPE_MAX                 ; readers refcount
wPipeHead:: DS PIPE_MAX                 ; read index
wPipeTail:: DS PIPE_MAX                 ; write index
wPipeCnt::  DS PIPE_MAX                 ; bytes buffered
wPipeBuf::  DS PIPE_MAX * PIPE_BUFSZ    ; ring buffers
wPipeByte:: DS 1                        ; scratch

SECTION "pipe", ROM0

; pipe_init - mark every pipe free (writers=readers=0). Called at boot.
pipe_init::
    ld hl, wPipeW
    ld b, PIPE_MAX * 2                  ; wPipeW followed by wPipeR
    xor a
.z
    ld [hl+], a
    dec b
    jr nz, .z
    ret

; sys_pipe - allocate a pipe -> A = read fd (write = read+1), or $FF.
sys_pipe::
    jp pipe_alloc

; is_pipe_fd - A = fd. CF set (carry) if it is NOT a pipe fd. Preserves A.
is_pipe_fd::
    cp PIPE_FD_BASE
    ret c                               ; < base -> not a pipe (CF set)
    cp PIPE_FD_BASE + PIPE_MAX*2
    ccf                                 ; >= top -> CF set (not pipe); else CF clear
    ret

; pipe_alloc - -> A = read fd, or $FF if none free. Empties + writers=readers=1.
pipe_alloc::
    ld c, 0
.scan
    ld hl, wPipeW
    ld b, 0
    add hl, bc
    ld a, [hl]
    or a
    jr nz, .next
    ld hl, wPipeR
    add hl, bc
    ld a, [hl]
    or a
    jr z, .found
.next
    inc c
    ld a, c
    cp PIPE_MAX
    jr c, .scan
    ld a, $FF
    ret
.found
    ld b, 0
    ld hl, wPipeW
    add hl, bc
    ld [hl], 1
    ld hl, wPipeR
    add hl, bc
    ld [hl], 1
    ld hl, wPipeHead
    add hl, bc
    ld [hl], 0
    ld hl, wPipeTail
    add hl, bc
    ld [hl], 0
    ld hl, wPipeCnt
    add hl, bc
    ld [hl], 0
    ld a, c
    add a                               ; idx*2
    add PIPE_FD_BASE
    ret

; pipe_close - A = fd. If a pipe fd, drop the appropriate refcount.
pipe_close::
    call is_pipe_fd
    ret c
    sub PIPE_FD_BASE
    ld c, a
    srl c                               ; C = idx
    and 1                               ; A = 1 if write end
    ld hl, wPipeR
    jr z, .have
    ld hl, wPipeW
.have
    ld b, 0
    add hl, bc
    ld a, [hl]
    or a
    ret z
    dec [hl]
    ret

; pipe_bufptr - C = idx, A = pos -> HL = &wPipeBuf[idx*PIPE_BUFSZ + pos].
; (idx*64 + pos <= 255, so the whole offset fits in one byte.) Preserves B,C,D.
pipe_bufptr:
    ld e, a                             ; E = pos
    ld a, c
    swap a
    add a
    add a                               ; A = idx*64
    add e                               ; + pos (0..255)
    ld e, a
    ld hl, wPipeBuf
    ld a, l
    add e
    ld l, a
    ld a, h
    adc 0
    ld h, a
    ret

; pipe_read - A = read fd -> A = byte, CF set on EOF. Blocks while empty w/ writer.
pipe_read::
    sub PIPE_FD_BASE
    srl a
    ld c, a                             ; C = idx
.retry
    ld hl, wPipeCnt
    ld b, 0
    add hl, bc
    ld a, [hl]
    or a
    jr nz, .data
    ld hl, wPipeW                       ; empty: any writers left?
    add hl, bc
    ld a, [hl]
    or a
    jr z, .eof                          ; none -> EOF
    push bc                             ; block: let the writer run (keep idx)
    call SchedYield
    pop bc
    jr .retry
.data
    ld hl, wPipeHead
    add hl, bc
    ld a, [hl]                          ; head
    push af
    call pipe_bufptr                    ; A=head, C=idx -> HL = &buf byte
    ld a, [hl]
    ld [wPipeByte], a
    pop af                              ; head
    inc a
    cp PIPE_BUFSZ
    jr c, .hok
    xor a
.hok
    ld hl, wPipeHead
    add hl, bc
    ld [hl], a
    ld hl, wPipeCnt
    add hl, bc
    dec [hl]
    ld a, [wPipeByte]
    and a                               ; CF = 0
    ret
.eof
    scf
    ret

; pipe_write - A = write fd, byte in wPipeByte. Blocks while full w/ reader;
; SIGPIPE (exit) if no reader.
pipe_write::
    sub PIPE_FD_BASE
    srl a
    ld c, a                             ; C = idx
    ld a, [wPipeByte]
    ld d, a                             ; D = byte (must survive yields; wPipeByte
                                        ;   is shared and gets clobbered by others)
.retry
    ld hl, wPipeCnt
    ld b, 0
    add hl, bc
    ld a, [hl]
    cp PIPE_BUFSZ
    jr c, .space
    ld hl, wPipeR                       ; full: any readers left?
    add hl, bc
    ld a, [hl]
    or a
    jr z, .epipe                        ; none -> SIGPIPE
    push bc                             ; block: let the reader drain (keep idx+byte)
    push de
    call SchedYield
    pop de
    pop bc
    jr .retry
.space
    ld hl, wPipeTail
    add hl, bc
    ld a, [hl]                          ; tail
    push af
    call pipe_bufptr                    ; A=tail, C=idx -> HL (preserves D)
    ld a, d                             ; the byte
    ld [hl], a
    pop af                              ; tail
    inc a
    cp PIPE_BUFSZ
    jr c, .tok
    xor a
.tok
    ld hl, wPipeTail
    add hl, bc
    ld [hl], a
    ld hl, wPipeCnt
    add hl, bc
    inc [hl]
    ret
.epipe
    ld b, 141                           ; 128 + SIGPIPE(13)
    jp sys_exit