diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/boot.asm | 1 | ||||
| -rw-r--r-- | src/fs.asm | 14 | ||||
| -rw-r--r-- | src/pipe.asm | 215 | ||||
| -rw-r--r-- | src/programs.asm | 7 | ||||
| -rw-r--r-- | src/syscall.asm | 20 |
5 files changed, 256 insertions, 1 deletions
diff --git a/src/boot.asm b/src/boot.asm index 7eed4a8..23511c4 100644 --- a/src/boot.asm +++ b/src/boot.asm @@ -55,6 +55,7 @@ KernelInit: ld [wCurProc], a ld a, HIGH(wKernelPCB) ld [wCurProc+1], a + call pipe_init ; mark all pipes free call fs_init ; mount the disk (format on first boot; persists) call term_init ; set up the LCD text terminal call osk_init ; build the on-screen keyboard (window layer) @@ -761,6 +761,13 @@ sys_open:: ; ============================================================================= sys_close:: ld a, b + call is_pipe_fd + jr c, .notpipe + call pipe_close + xor a + ret +.notpipe + ld a, b cp OF_MAX jr nc, .done ld [wFsSlot], a ; save fd @@ -787,6 +794,9 @@ sys_close:: ; ============================================================================= sys_getb:: ld a, b + call is_pipe_fd + jp nc, pipe_read ; A = pipe read fd + ld a, b cp OF_MAX jr nc, .eof call OFPtr @@ -868,6 +878,10 @@ sys_getb:: sys_putb:: ld a, e ld [wFsByte], a + ld [wPipeByte], a + ld a, b + call is_pipe_fd + jp nc, pipe_write ; A = pipe write fd, byte in wPipeByte ld a, b cp OF_MAX jr nc, .done diff --git a/src/pipe.asm b/src/pipe.asm new file mode 100644 index 0000000..f9473a4 --- /dev/null +++ b/src/pipe.asm @@ -0,0 +1,215 @@ +; ============================================================================= +; 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]. keeps BC. +; (idx*64 + pos <= 255, so the whole offset fits in one byte.) +pipe_bufptr: + ld e, a ; E = pos + ld a, c + swap a + add a + add a ; A = idx*64 + add e ; + pos + ld e, a + ld d, 0 + ld hl, wPipeBuf + add hl, de + 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 + call SchedYield ; block: let the writer run + 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 +.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 + call SchedYield ; block: let the reader drain + jr .retry +.space + ld hl, wPipeTail + add hl, bc + ld a, [hl] ; tail + push af + call pipe_bufptr ; A=tail, C=idx -> HL = &buf byte + ld a, [wPipeByte] + 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 diff --git a/src/programs.asm b/src/programs.asm index 40d9034..e659cee 100644 --- a/src/programs.asm +++ b/src/programs.asm @@ -106,6 +106,9 @@ ProgMkdir: SECTION "prog_count", ROMX[$4000], BANK[23] ProgCount: INCBIN "c/count.bin" +SECTION "prog_ptest", ROMX[$4000], BANK[24] +ProgPtest: + INCBIN "c/ptest.bin" ; ----------------------------------------------------------------------------- ; PROG_SH (bank 3) - the shell, now written in C (c/sh.c): parses >/</| and @@ -192,6 +195,8 @@ ProgramTable:: dw ProgMkdir db LOW(BANK(ProgCount)), HIGH(BANK(ProgCount)) dw ProgCount + db LOW(BANK(ProgPtest)), HIGH(BANK(ProgPtest)) + dw ProgPtest ; ----------------------------------------------------------------------------- ; NameTable (ROM0) - command name -> program id, searched by sys_lookup. @@ -242,4 +247,6 @@ NameTable:: db PROG_MKDIR db "count", 0 db PROG_COUNT + db "ptest", 0 + db PROG_PTEST db 0 diff --git a/src/syscall.asm b/src/syscall.asm index cb2c996..c602097 100644 --- a/src/syscall.asm +++ b/src/syscall.asm @@ -62,6 +62,7 @@ SyscallTable: dw sys_mkdir ; 25 MKDIR dw sys_chdir ; 26 CHDIR dw sys_opendir ; 27 OPENDIR + dw sys_pipe ; 28 PIPE ; ----------------------------------------------------------------------------- sys_nosys: @@ -256,7 +257,24 @@ sys_yield: ; - wake our parent if it is blocked in wait() ; - schedule away forever (RAM bank + PCB slot are freed by the reaper) ; ----------------------------------------------------------------------------- -sys_exit: +sys_exit:: + ; release any pipe ends held as stdin/stdout (so EOF/free fire) + ld a, [wCurProc] + add PROC_STDIN + ld l, a + ld a, [wCurProc+1] + adc 0 + ld h, a + ld a, [hl] + call pipe_close + ld a, [wCurProc] + add PROC_STDOUT + ld l, a + ld a, [wCurProc+1] + adc 0 + ld h, a + ld a, [hl] + call pipe_close ; PROC_STATE = PS_ZOMBIE ld a, [wCurProc] ld l, a |
