Shop OBEX P1 Docs P2 Docs Learn Events
Tachyon 5 - Running commands stored on SD card — Parallax Forums

Tachyon 5 - Running commands stored on SD card

bob_g4bbybob_g4bby Posts: 401
edited 2021-08-21 19:48 in Forth
Tachyon 5 has the ability to switch the input stream from terminal to SD card file (or other places). I had a go at this recently, to see how it performed. I first wrote a 'BATCHFILE' word to make it easier to switch. After ON BATCHFILE is entered and the user enters a word to execute that isn't in the dictionary, then tachyon will make a search of the root directory of the SD card for a file of the same name. If the file is found, the contents of the file are read in, as if they were being typed at the terminal. If OFF BATCHFILE is entered, tachyon goes back to only searching the dictionary. Here's the BATCHFILE word:-
--- Tachyon 5v7 - Extension to dictionary search to include running files on SD card

--- Enable or disable SD card search for unknown dictionary words
pub BATCHFILE	( on/off -- )	
IF
	' FRUN undef W!
ELSE
	0 undef W!
THEN ;

--- 	Usage:
--- ON BATCHFILE    If a word is not in the dictionary, a search
--- is made on SD card for a file of the same name
--- OFF BATCHFILE   Tachyon will only search the dictionary for words,
--- the SD card search is turned off
So to test this, I made a modified version of that useful WWORDS function and stored it in SD card in a file named WW (no extension type). This was so I could just type WW in tachyon to run the command
ON QUIET

--- Tachyon 5v7 - Experimental WWORDS debug command that loads from SD card,
--- then is forgotten again after executing
--- Because this is forgotten afterwards, we can afford to
--- fit some user information


pre WW
	[CON
	BEGIN KEY 0= UNTIL
	ERSCN HOME
	." Enter the first letter of the words or RETURN for all the words"
	SPACE WKEY
	DUP $0D =
	IF
		DROP 0
	ELSE
		DUP EMIT
	THEN

	CR CR ." Key to data: Each entry consists of: "
	." Name field address "
	." | Code field address " 
	." | Key field "
	." | Word name"
	CR ." Key to colours: " 
	BOLD red PEN ." Module Marker " 
	PLAIN green PEN ." Public words "
	red PEN ." Private words "
	BOLD green PEN ." Preemptive words "
	PLAIN cyan PEN ." Public data "
	yellow PEN ." Private data" white PEN CR

	@NAMES 0 SWAP
	BEGIN
	  PLAIN DUP C@
	  KEY $1B <> AND
	  2 REG W-- 2 REG W@ 0<> AND
	WHILE ( match wcnt nfa )
	  3RD OVER 1+ C@ =
	  4TH 0= OR
	  IF
	  	OVER 3 AND 0= 
		IF
			CR
		THEN
	  	SWAP 1+ SWAP
	  	lsword
		16 OVER C@ $1F AND - SPACES
	  THEN
	  +NFA
	REPEAT
	CR ." Number of words was " OVER . CR CR
	3DROP CR 2 REG W~
	CON]	
;
OFF QUIET 
WW
0 XTAB FORGET WW ERLINE

N.B. After the last line in this file is a CR character, else the last line won't execute.
Just to summarise the file:-
1. The output stream is turned off with ON QUIET, so compile happens quicker and we don't need to see that anyway
2. The word WW is compiled into hub ram
3. The output stream is turned back on, so we can see WW output
4. The WW word is executed
5. Inside WW, the input stream is switched back to terminal, but the file source is remembered with [CON . This is done because this version of WW requires user input from the terminal
6. I found I had to flush the terminal input stream with BEGIN KEY 0= UNTIL
7. Use is made of ANSI commands to remove unwanted prompts from tachyon
8. ANSI commands are used to explain the meaning of the colour codes displayed
9. At the end of WW, the input stream is switched back to the file by CON]
10. After WW has finished, the last line of the file removes WW from hub ram and does a bit more terminal output tidy up - not perfect but it makes the point

So any number of debug words could be written as similar files on the SD card. If all of them were hub ram resident, they would severely reduce space available for an application under construction. With this technique, only one of the debug words is present in hub ram at any one time. There is a delay of perhaps 1/3 second before WW shows it's user prompt, so for all practical purposes there's no disadvantage in having commands on SD card. Keeping the file size small helps of course.
Sign In or Register to comment.