diff --git a/ueforth/Makefile b/ueforth/Makefile index 51e789a..016f952 100644 --- a/ueforth/Makefile +++ b/ueforth/Makefile @@ -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 >$@ diff --git a/ueforth/common/streams.fs b/ueforth/common/streams.fs new file mode 100644 index 0000000..5b74c47 --- /dev/null +++ b/ueforth/common/streams.fs @@ -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 ;