\ Define data space that will be used by the serial cog create bufserstack 80 allot \ Data stack space create bufserreturn 80 allot \ Return stack space create bufserbuffer 256 allot \ Serial buffer create bufserrdindex 0 , \ Read index create bufserwrindex 0 , \ Write index \ This word runs in a separate cog and writes input characters into the buffer : bufsercog bufserbuffer begin key over bufserwrindex @ + c! bufserwrindex dup @ 1 + 255 and swap ! again ; \ This word replaces the old KEY word with one that reads from the buffer : key bufserrdindex @ begin dup bufserwrindex @ - until bufserbuffer + c@ bufserrdindex dup @ 1 + 255 and swap ! ; \ This is the cog configuration structure used by the serial cog create bufserconfig \ Forth cog config structure ' bufsercog >body , \ Get execution token for TOGGLE bufserstack , \ Initial value of stack ptr bufserstack , \ Empty value for stack ptr bufserreturn , \ Initial value of return ptr bufserreturn , \ Empty value for return ptr \ This word starts a cog running the BUFSER word : startbufsercog forth @ bufserconfig cognew ; \ This word replaces the old ACCEPT with one that uses the new KEY word : accept ( addr size -- num ) >r dup begin r> dup 1 < \ Check if any space left if drop swap - exit then >r key \ Get character from input dup 13 = over 10 = or \ Check for a new line if cr drop swap - r> drop exit then dup 8 = \ Check for a backspace if drop over over - \ Check if buffer empty if 1 - r> 1 + >r \ Erase character 8 emit bl emit 8 emit then else dup emit over c! 1 + \ Store character r> 1 - >r then again ; \ This word will replace the old REFILL word with one that uses the new ACCEPT : _refill tib 200 accept #tib ! 0 >in ! ; \ This word starts the serial cog and sets REFILL to the new one : newser startbufsercog ['] _refill is refill ;