Added streams.

This commit is contained in:
Brad Nelson
2021-01-08 16:27:36 -08:00
parent 6cd7a2e14f
commit af0960c971
2 changed files with 22 additions and 2 deletions

View File

@ -72,7 +72,7 @@ $(GEN):
POSIX_BOOT = common/boot.fs common/terminal.fs \
posix/posix.fs posix/posix_highlevel.fs \
common/filetools.fs posix/posix_desktop.fs \
common/tasks.fs
common/tasks.fs common/streams.fs
$(GEN)/posix_boot.h: common/source_to_string.js $(POSIX_BOOT) | $(GEN)
echo "ok" | cat $(POSIX_BOOT) - | $< boot >$@
@ -82,7 +82,7 @@ $(GEN)/windows_boot.h: common/source_to_string.js $(WINDOWS_BOOT) | $(GEN)
ARDUINO_BOOT = common/boot.fs arduino/arduino.fs \
posix/posix_highlevel.fs common/filetools.fs \
common/tasks.fs arduino/arduino_server.fs \
common/tasks.fs common/streams.fs arduino/arduino_server.fs \
arduino/autoboot.fs
$(GEN)/arduino_boot.h: common/source_to_string.js $(ARDUINO_BOOT) | $(GEN)
echo "ok" | cat $(ARDUINO_BOOT) - | $< boot >$@

20
ueforth/common/streams.fs Normal file
View File

@ -0,0 +1,20 @@
( Byte Stream / Ring Buffer )
: stream ( n "name" ) create 1+ dup , 0 , 0 , allot align ;
: >write ( st -- wr ) cell+ ; : >read ( st -- rd ) 2 cells + ;
: >offset ( n st -- a ) 3 cells + + ;
: stream# ( sz -- n ) >r r@ >write @ r@ >read @ - r> @ mod ;
: full? ( st -- f ) dup stream# swap @ 1- = ;
: empty? ( st -- f ) stream# 0= ;
: wait-write ( st -- ) begin dup full? while pause repeat drop ;
: wait-read ( st -- ) begin dup empty? while pause repeat drop ;
: ch>stream ( ch st -- )
dup wait-write
>r r@ >write @ r@ >offset c!
r@ >write @ 1+ r@ @ mod r> >write ! ;
: stream>ch ( st -- ch )
dup wait-read
>r r@ >read @ r@ >offset c@
r@ >read @ 1+ r@ @ mod r> >read ! ;
: >stream ( a n st -- )
swap 0 do over c@ over ch>stream swap 1+ swap loop 2drop ;