Rob Perrin Ron Hathaway Forth History n Forth
Rob Perrin Ron Hathaway
Forth History n Forth started in the 50's as Charles Moore person work tool in response to his frustration with existing software tools, which he viewed as a sort of "tower of Babel. " n By 1971, Charles Moore began calling the interpreter Fourth. This was due to the fact that they were working on machines that were considered third-generation. He felt his interpreter was so advanced, that it was a fourth-generation language. He had intended to call it Fourth, however, the system he was working on at the time only had space for five character identifiers. Therefore, the "u", was dropped and thus FORTH was born.
Elements of Forth: n n n n Dictionary with words Push down stacks Interpreter Assembler Postfix computation Interactive Absolutely no data typing n "If I want to add 1 to the letter A, it's none of the compiler's business to tell me I can't. "
What do this have to do with pb. Forth? n n Forth in it's popularity and commercialization has spawned dozens of Forth variants. pb. Forth is a version designed by Ralph Hempel to work specifically with the LEGO Mindstorms RCX brick as volatile firmware sitting between non-volatile firmware and end user programs. After pb. Forth (14 KB) is loaded 14 KB is left in RAM for pb. Forth programs to run compared with 6 KB in the case of the standard firmware.
Forth Words and the Dictionary: n The Dictionary in pb. Forth is a maintained list of all the currently defined Words. n Words can either be made of a collection of other Words or a set of direct assembly instructions to the hardware. Other versions of Forth may implement multiple Dictionaries.
Forth Words Continued: n The definition of a new Word starts with a and ends with a ; n The first white space delineated string after the colon is the new Word. All subsequent strings are Words defining the action of the new Word. n : double (n 1 -- n 2) 2 * ; (n 2 is 2 times n 1 :
Forth Words Continued: n To see the construct behind a Forth Word you would use the SEE command. n n n SEE double : double (n 1 -- n 2) 2 * ; (n 2 is 2 times n 1 ok
Forth Interpreter Model:
Data Stack: n The Data Stack holds the arguments being passed between Words. This stack replaces the parameter lists used by conventional languages. n n n 1 2 3 (pushes the successive numbers on the stack. . . (pop successive values off the stack To take a look at the stack use the. s command n n n . s 123 ok
Return Stack: n The Return Stack is used to hold return addresses for nested definitions, although other kinds of system data are occasionally held there temporarily. The user could access this stack but it is not recommended.
Lexical Input Format: n Forth code is just a bunch of Words with spaces between them. There are only minimal rules for how Words are to be used in context or formulation of expressions.
Line Orientation & Comments: n It is not line oriented, with the exception of some forms of comments. ( comment only within parentheses ) n ( comment till end of line n again comment till end of line n
White Space: n White Space is ignored n : double (n 1 -- n 2) 2 * ; (n 2 is 2 times n 1 n : double (n 1 -- n 2) (treated the same as above 2 * ; n n A notable exception is that with the command SEE the Word definition will be returned in the format it was entered.
Data Types: n Scalar n n n 1 2 3 integer • 16 bit, 32 bit, signed, unsigned 0 3 -1 flag • Non-zero is true, zero is false • Forth flags are cell-size bit patterns like Forth integers and Forth integers can be used as Forth flags. Memory Addresses • @ • ! (Retrieves the cell value stored at the address (Stores the value x into the cell at the address
Data Types: n Aggregates n n n 2. 4 e float • Stored as two integers consecutively in memory • To represent floating point numbers they must have an e at the end. “Forth” string • . ” (prints the up to, not including next double quote Arrays • CREATE forth. Array 32 CELLS ALLOT • Use @ and ! to manipulate the array n Forth has no support for data typing either at compile time or at runtime.
Scope: n General Forth has locals and globals n n Exchange values of two variables Without locals • : vswap 1 ( addr 1 addr 2 -- ) 2 DUP 2>R @ SWAP @ R> ! ; n And With locals • : vswap 2 ( addr 1 addr 2 -- ) LOCALS| v 1 v 2 | v 1 @ v 2 @ v 1 ! v 2 ! ;
Lifetime: n Words maintain their meaning even when sub-words are changed. n n n : HELLO. " Hello World" ; HELLO Hello World ok : SAY-HI HELLO ; SAY-HI Hello World ok : HELLO. " Greetings" ; HELLO Greetings ok SAY-HI (Will keep its original value, despite the change of HELLO Hello World ok
Stack Manipulation: n DUP n n n (n 1 -- n 1) 2 DUP. s 22 ok n ? DUP (same as above, but only if n 1 is not 0 n OVER (n 1 n 2 -- n 1 n 2 n 1) n n n 1 2 OVER. s 121 ok
Stack Manipulation: n SWAP n n 1 2 SWAP. s 21 ok DROP n n n (n 1 n 2 -- n 2 n 1) (n 1 -- ) 1 2 DROP. s 1 ok
Stack Manipulation: n NIP n n 1 2 NIP. s 2 ok TUCK n n n (n 1 n 2 -- n 2) (n 1 n 2 n 3 -- n 1 n 3 n 2 n 3) 1 2 3 TUCK. s 1323 ok
Stack Manipulation: n ROT n n 1 2 3 4 ROT. s 2341 ok -ROT n n n (n 1 n 2 n 3 n 4 -- n 2 n 3 n 4 n 1) (n 1 n 2 n 3 n 4 -- n 4 n 1 n 2 n 3) 1 2 3 4 -ROT. s 4123 ok
Stack Manipulation: n By putting a two, 2, in front on these Words the action will affect two members of the stack. n 2 DUP n n (n 1 n 2 -- n 1 n 2) 1 2 2 DUP. s 1212 ok Notable, no documentation of being able to use larger numbers to manipulate more elements.
Display Options: n There are several ways to format output, if no option is given the value will be returned as a signed integer. n n n n DECIMAL HEX CR u c d f (print as decimal (print as hex (force carriage return (as unsigned integer (as ASCII character (as signed double-cell integer (as floating-point
Math, Postfix Evaluation: n + (n 1 n 2 -- n 3) * (n 1 n 2 -- n 3) / (n 1 n 2 -- n 3) (stores only the quotient */ (n 1 n 2 n 3 -- n 4) (equivalent to [n 1*n 2]/n 3 MOD (n 1 n 2 -- n 3 n 4) (remainder & quotient */MOD (n 1 n 2 n 3 -- n 4 n 5) n 23+4* n n n (equivalent to [2+3]*4
Conditionals: n n n n n = (n 1 n 2 -- f) < (n 1 n 2 -- f) > (n 1 n 2 -- f) 0= (n 1 -- f) 0< (n 1 -- f) AND (n 1 n 2 -- n 3) (bitwise AND on n 1 & n 2 OR (n 1 n 2 -- n 3) (bitwise OR on n 1 & n 2 XOR (n 1 n 2 -- n 3) (bitwise XOR on n 1 & n 2 INVERT (n 1 -- n 2) (bitwise inverse of n 1
Loops: n . . IF. . ELSE. . THEN n n n : if. Loop ( n 1 -- n 2 ) 0 = IF 4 ELSE ( ELSE is optional 6 THEN ; ( may also use ENDIF
Loops: n . . DO. . LOOP n n n n : do. Loop ( n 1 n 2 -- ) (for loop equivalent DO (sets loop index i to n 2 i. s LOOP (increment i up by 1, if i > n 1 exit, else repeat ; Notable, i is a keyword as are j and k for further nested loops
Loops: n BEGIN. . UNTIL n n n : finite. Loop ( n 1 -- ) BEGIN DUP. 1 - DUP 0< UNTIL DROP ; (loop till this condition is met (then go on
Loops: n BEGIN. . AGAIN n n n : infinite. Loop ( n 1 -- n 1 ) BEGIN DUP. AGAIN ; (loop back to the BEGIN
Loops: n BEGIN. . WHILE. . REPEAT n n n n : multi. Level. Loop BEGIN DUP. 0 = if WHILE (if flag true, do lower block, . (else start over at BEGIN REPEAT;
Direct Memory Access: n n n n n CREATE , ALLOT CELLS CELL+ VARIABLE CONSTANT VALUE TO
Multi-Tasking: n Older version of pb. Forth where multi-tasking, but current version is not
The RCX: n The software engineers at LEGO provided the RCX with a bunch of helper routines. The pb. Forth system uses the obvious ones to control the motors, read sensors, and drive the LCD. There also math routines for multiply and divide built into the RCX, and this version of pb. Forth makes extensive use of them. The result is blazingly fast fixed-point math.
The RCX: n RCX and Power Controls n RCX_INIT (issued the RCX must be initialized. n n n (Before any other command can be RCX_SHUTDOWN RCX_POWER_OFF POWER_GET (return value of POWER_GET (shutdown (what is the voltage and is it on
The RCX: n Motor Control n MOTOR_SET ( power mode idx -- ) • power has a range of 0 to 7 • idx indicates which output 0 = A, 1 = B, 2 = C • mode • • n 1 2 3 4 Forward Backward Stop Float 7 2 0 MOTOR_SET (Have motor C run forward at full power
The RCX: n Button Controls n BUTTON_INIT (Button system must be initialized (before any buttons can be used. RCX_BUTTON_GET (return the value of BUTTON_GET n n n • 1 • 2 • 3 RUN button pressed PRGM button pressed VIEW button pressed
The RCX: n Sensor Controls n SENSOR_INIT (Sensor system must be initialized (before any buttons can be used. SENSOR_TYPE (type idx -- ) n n • Type • • 1 2 3 4 Touch Temperature Light Rotation • idx • 0 Sensor 1 • 1 Sensor 2 • 2 Sensor 3 (must be set to active (must be set to passive (must be set to active
The RCX: n Sensor Controls n n n SENSOR_ACTIVE ( idx -- ) SENSOR_PASSIVE (idx -- ) SENSOR_MODE (mode idx -- ) • mode • • 0 32 64 96 128 160 192 224 Raw mode Boolean mode Edge detection - every transition counts Pulse detection - only negative transitions count Percent of scale Degrees Celsius Degrees Fahrenheit Angle detection
The RCX: n Sensor Controls n n n SENSOR_READ SENSOR_RAW SENSOR_VALUE SENSOR_BOOL SENSOR_CLEAR ( idx -- ) (get data from this sensor ( idx -- raw ) ( idx -- val ) ( idx -- bool ) ( idx -- ) (clears stored value, leaves (mode and type intact
The RCX: n Timer Controls n High Resolution • There are ten, with 10 ms declinating counters that stop at zero. • timer_SET ( idx value -- ) • timer_GET ( idx -- value ) n Low Resolution • There are four, with 100 ms incrementing counters that roll over at 65535. • TIMER_SET ( idx value -- ) • TIMER_GET ( idx -- value )
The RCX: n n n n Sample Code : square ( -- ) BEGIN 7 1 0 MOTER_SET (both motors forward 7 1 2 MOTER_SET timer_SET ( 0 4 ) timer_GET = 0 IF UNTIL BEGIN 7 2 2 MOTER_SET (set motor to reverse timer_SET ( 0 4 ) timer_GET = 0 IF UNTIL AGAIN ;
Sources: n n n n n http: //www. cs. rit. edu/~ats/plcr-2002 -1/reports/pbforth/ n Dan Lovette & Fuyuko Takegawa http: //www. cs. rit. edu/~ats/plcr-2003 -1/reports/pbforth/index. html n Josh King & Joe Spears http: //www. forth. com/resources/evolution/index. html n Forth, Inc explaination of Forth http: //dec. bournemouth. ac. uk/forth. html n Forth: An underview http: //www. taygeta. com/forth/dpans. html n American National Standard for Information Systems, Programming Languages, Forth http: //dec. bournemouth. ac. uk/forth/rfc/index. html n The Rochester Forth Conference, Held at Uof. R http: //www. hempeldesigngroup. com/lego/ n Hempel Design Group, LEGO? Models and Robotics http: //www. forthfreak. net/wiki/ n Forth. Freak http: //hem. passagen. se/tiletech/forth. htm n Crash Course In Forth http: //www. circuitcellar. com/Design. Forum/features/9805022/TNtext. htm n Lost at C? Forth May Be the Answer
- Slides: 42