rob perrin ron hathaway

Post on 13-Jan-2016

44 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Rob Perrin Ron Hathaway. Forth History. 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." - PowerPoint PPT Presentation

TRANSCRIPT

Rob Perrin

Ron Hathaway

Forth History 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."

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:

Dictionary with words Push down stacks Interpreter Assembler Postfix computation Interactive Absolutely no data typing

"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 pbForth?

Forth in it's popularity and commercialization has spawned dozens of Forth variants. pbForth 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 pbForth (14KB) is loaded 14KB is left in RAM for pbForth programs to run compared with 6KB in the case of the standard firmware.

Forth Words and the Dictionary: The Dictionary in pbForth is a maintained list of

all the currently defined Words.

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: The definition of a new Word starts with

a : and ends with a ;

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.

: double (n1 -- n2) 2 * ; (n2 is 2 times n1

Forth Words Continued:

To see the construct behind a Forth Word you would use the SEE command.

SEE double : double (n1 -- n2) 2 * ; (n2 is 2 times n1 ok

Forth Interpreter Model:

Data Stack: The Data Stack holds the arguments being passed

between Words. This stack replaces the parameter lists used by conventional languages.

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 .s 1 2 3 ok

Return Stack: 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:

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 for formulation of expressions.

Line Orientation & Comments:

It is not line oriented, with the exception of some forms of comments. ( comment only within parentheses ) ( comment till end of line \ again comment till end of line

White Space:

White Space is ignored : double (n1 -- n2) 2 * ; (n2 is 2 times n1

: double (n1 -- n2) (treated the same as above 2 * ;

A notable exception is that with the command SEE the Word definition will be returned in the format it was entered.

Data Types: Scalar

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: Aggregates

2.4e 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 forthArray 32 CELLS ALLOT• Use @ and ! to manipulate the array

Forth has no support for data typing either at compile time or at runtime.

Scope: General Forth has locals and globals

\ Exchange values of two variables \ Without locals

• : vswap1 ( addr1 addr2 -- ) 2DUP 2>R @ SWAP @ R> ! R> ! ;

\ And With locals• : vswap2 ( addr1 addr2 -- ) LOCALS| v1 v2 | v1 @ v2 @

v1 ! v2 ! ;

Lifetime: Words maintain their meaning even when sub-words are

changed. : 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: DUP (n1 -- n1 n1)

2 DUP .s 2 2 ok

?DUP (same as above, but only if n1 is not 0

OVER (n1 n2 -- n1 n2 n1) 1 2 OVER .s 1 2 1 ok

Stack Manipulation: SWAP (n1 n2 -- n2 n1)

1 2 SWAP .s 2 1 ok

DROP (n1 -- ) 1 2 DROP .s 1 ok

Stack Manipulation: NIP (n1 n2 -- n2)

1 2 NIP .s 2 ok

TUCK (n1 n2 n3 -- n1 n3 n2 n3) 1 2 3 TUCK .s 1 3 2 3 ok

Stack Manipulation: ROT (n1 n2 n3 n4 -- n2 n3 n4 n1)

1 2 3 4 ROT .s 2 3 4 1 ok

-ROT(n1 n2 n3 n4 -- n4 n1 n2 n3) 1 2 3 4 -ROT .s 4 1 2 3 ok

Stack Manipulation: By putting a two, 2, in front on these Words the

action will affect two members of the stack.

2DUP (n1 n2 -- n1 n2 n1 n2) 1 2 2DUP .s 1 2 1 2 ok

Notable, no documentation of being able to use larger numbers to manipulate more elements.

Display Options: There are several ways to format output, if no

option is given the value will be returned as a signed integer. DECIMAL (print as decimal HEX (print as hex CR (force carriage return u (as unsigned integer c (as ASCII character d (as signed double-cell integer f (as floating-point

Math, Postfix Evaluation: + (n1 n2 -- n3) - (n1 n2 -- n3) * (n1 n2 -- n3) / (n1 n2 -- n3) (stores only the quotient */ (n1 n2 n3 -- n4) (equivalent to [n1*n2]/n3 MOD (n1 n2 -- n3 n4) (remainder & quotient */MOD (n1 n2 n3 -- n4 n5)

2 3 + 4 * (equivalent to [2+3]*4

Conditionals: = (n1 n2 -- f) < (n1 n2 -- f) > (n1 n2 -- f) 0= (n1 -- f) 0< (n1 -- f) AND (n1 n2 -- n3) (bitwise AND on n1 & n2 OR (n1 n2 -- n3) (bitwise OR on n1 & n2 XOR (n1 n2 -- n3) (bitwise XOR on n1 & n2 INVERT (n1 -- n2) (bitwise inverse of n1

Loops: .. IF .. ELSE .. THEN

: ifLoop ( n1 -- n2 ) 0 = IF 4 ELSE ( ELSE is optional 6 THEN ; ( may also use ENDIF

Loops: .. DO .. LOOP

: doLoop ( n1 n2 -- ) (for loop equivalent DO (sets loop index i to n2 i .s LOOP (increment i up by 1, if i > n1 exit, else repeat ;

Notable, i is a keyword as are j and k for further nested loops

Loops: BEGIN .. UNTIL

: finiteLoop ( n1 -- ) BEGIN DUP . 1 - DUP 0 < (loop till this condition is met UNTIL DROP ; (then go on

Loops: BEGIN .. AGAIN

: infiniteLoop ( n1 -- n1 ) BEGIN DUP . AGAIN ; (loop back to the BEGIN

Loops: BEGIN .. WHILE .. REPEAT

: multiLevelLoop BEGIN DUP DUP . 0 = if WHILE (if flag true, do lower block, . (else start over at BEGIN REPEAT;

Direct Memory Access: CREATE , ALLOT CELLS CELL+ VARIABLE CONSTANT VALUE TO

Multi-Tasking: Older version of pbForth where multi-tasking,

but current version is not

The RCX: The software engineers at LEGO provided the

RCX with a bunch of helper routines. The pbForth system uses the obvious ones to control the motors, read sensors, and drive the LCD. There are also math routines for multiply and divide built into the RCX, and this version of pbForth makes extensive use of them. The result is blazingly fast fixed-point math.

The RCX:

RCX and Power Controls RCX_INIT (Before any other command can be

(issued the RCX must be initialized.

RCX_SHUTDOWN RCX_POWER (return value of

POWER_GET POWER_OFF (shutdown POWER_GET (what is the voltage and is

it on

The RCX:

Motor Control MOTOR_SET ( power mode idx -- )

• power has a range of 0 to 7• idx indicates which output 0 = A, 1 = B, 2 = C• mode

• 1 Forward• 2 Backward• 3 Stop• 4 Float

7 2 0 MOTOR_SET (Have motor C run forward at full power

The RCX:

Button Controls BUTTON_INIT (Button system must be

initialized (before any buttons can be

used. RCX_BUTTON (return the value of BUTTON_GET

BUTTON_GET• 1 RUN button pressed• 2 PRGM button pressed• 3 VIEW button pressed

The RCX: Sensor Controls

SENSOR_INIT (Sensor system must be initialized (before any buttons can be used.

SENSOR_TYPE (type idx -- )• Type

• 1 Touch (must be set to active• 2 Temperature (must be set to passive• 3 Light (must be set to passive• 4 Rotation (must be set to active

• idx• 0 Sensor 1• 1 Sensor 2• 2 Sensor 3

The RCX: Sensor Controls

SENSOR_ACTIVE ( idx -- )

SENSOR_PASSIVE (idx -- ) SENSOR_MODE (mode idx -- )

• mode• 0 Raw mode

• 32 Boolean mode

• 64 Edge detection - every transition counts

• 96 Pulse detection - only negative transitions count

• 128 Percent of scale

• 160 Degrees Celsius

• 192 Degrees Fahrenheit

• 224 Angle detection

The RCX:

Sensor Controls SENSOR_READ ( idx -- ) (get data from this sensor

SENSOR_RAW ( idx -- raw )

SENSOR_VALUE ( idx -- val )

SENSOR_BOOL ( idx -- bool )

SENSOR_CLEAR ( idx -- ) (clears stored value, leaves (mode and type intact

The RCX:

Timer Controls High Resolution

• There are ten, with 10ms declinating counters that stop at zero.

• timer_SET ( idx value -- )• timer_GET ( idx -- value )

Low Resolution• There are four, with 100ms incrementing counters that roll

over at 65535.• TIMER_SET ( idx value -- )• TIMER_GET ( idx -- value )

The RCX: Sample Code :square ( -- ) BEGIN 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: http://www.cs.rit.edu/~ats/plcr-2002-1/reports/pbforth/

Dan Lovette & Fuyuko Takegawa http://www.cs.rit.edu/~ats/plcr-2003-1/reports/pbforth/index.html

Josh King & Joe Spears http://www.forth.com/resources/evolution/index.html

Forth, Inc explaination of Forth http://dec.bournemouth.ac.uk/forth/forth.html

Forth: An underview http://www.taygeta.com/forth/dpans.html

American National Standard for Information Systems, Programming Languages, Forth http://dec.bournemouth.ac.uk/forth/rfc/index.html

The Rochester Forth Conference, Held at UofR http://www.hempeldesigngroup.com/lego/

Hempel Design Group, LEGO? Models and Robotics http://www.forthfreak.net/wiki/

ForthFreak http://hem.passagen.se/tiletech/forth.htm

Crash Course In Forth http://www.circuitcellar.com/DesignForum/features/9805022/TNtext.htm

Lost at C? Forth May Be the Answer

top related