A Programming Language for the FC16 Forth Core WHYP A Programming Language for the FC16 Forth Core
WHYP Pronounced “whip” “Words to Help You Program” Subroutine-threaded Forth for Embedded Systems 68HC11 (16-bit) 68332 (32-bit) 68HC12 (16-bit)
WHYP is developed from scratch in the new book: Design of Embedded Systems Using 68HC12/11 Microcontrollers by Richard E. Haskell Prentice Hall, 2000
FORTH is a programming language that --- was invented by Charles Moore in the early 70’s is extensible keeps all definitions in a dictionary is extremely compact is recursive can be programmed in RAM, PROM, or ROM is structured uses a stack and postfix notation
Chuck Moore reading Haskell’s WHYP book
FORTH is a programming languages that --- is extremely modular is interactive is easy to debug allow easy machine access is fast is transportable can be understood in its entirety is unlike any other language
Introducing WHYP (Forth) Everything in WHYP is a word WHYP words must be separated by a space WHYP words are stored in a dictionary WHYP words may be either interpreted or compiled When in the interpret mode, a WHYP word is executed When in the compile mode, a WHYP word is stored in the dictionary
Introducing WHYP (Forth) If you type a WHYP word and press <enter>, the word will be executed (interpret mode) If you type a number (e.g. 6) and press <enter>, the number will be pushed on the data stack. WHYP uses the stack to pass parameters from one word to the next. You can define new words in WHYP by stringing together previously defined words.
The Structure of WHYP PC Target 68HC12 C++ Program Kernel: Dictionary: --- --- Serial Line LOOP BSR INWDY --- --- JSR 0,Y --- --- send address BRA LOOP --- --- F82C display F82C --- --- F82C ----- --- --- ----- ----- ----- Screen ----- C:\>whyp RTS ok display
WHYP Arithmetic Operators 7 9 + . 8 5 - . 4 7 * . 8 3 / . 8 3 /MOD . .
WHYP Colon Definitions : squared ( n -- n**2) DUP * ; : cubed ( n -- n**3) DUP \ n n squared \ n n**2 * ; \ n**3
WHYP Stack Manipulation Words DUP ( n -- n n ) SWAP ( a b -- b a ) DROP ( a -- ) OVER ( a b -- a b a ) TUCK ( a b -- b a b ) ROT ( a b c -- b c a ) -ROT ( a b c -- c a b ) NIP ( a b -- b ) 2DUP ( a b -- a b a b ) 2SWAP ( a b c d -- c d a b ) 2DROP ( a b -- ) 2OVER ( a b c d -- a b c d a b )
WHYP on the FC16
Stack Manipulation Words FC16 Primitives : DUP ( w -- w w ) DUP (0001); : DROP ( w -- ) DROP (0003) ; : SWAP ( a b -- b a) SWAP (0002) : NIP ( a b -- b ) NIP (0007);
Stack Manipulation Words FC16 Primitives : ROT ( a b c -- b c a) ROT (0005) ; : -ROT ( a b c -- c a b) MROT (0006) ; : OVER ( a b -- a b a) OVER (0004) ; : TUCK ( a b -- b a b) TUCK (0008)
Stack Manipulation Words Colon Definitions : 2DUP ( a b -- a b a b ) OVER (0004) \ a b a OVER (0004) ; \ a b a b
Memory Access Words Fetch Store \ Fetch word at address T in RAM and \ load it into T : @ ( a -- w ) fetch (0034) ; Store \ Store the word in N at the address T. \ Pop both T and N. : ! ( w a -- ) store (010E) ;
RAM Module
Return Stack Words To-R R-From R-Fetch R-From-Drop : >R ( w -- ) \ pop T & push to R TOR (0030) ; R-From : R> ( -- w ) \ pop R & push to T RFROM (0031) ; R-Fetch : R@ ( -- w ) \ copy R & push to T RFETCH (0032) ; R-From-Drop : R>DROP ( -- ) \ pop R & drop it RFROMDROP (0033) ;
Arithmetic Words Plus w3 = w1 + w2 minus w3 = w1 - w2
Arithmetic Words One-plus w2 = w1 + 1 One-minus w2 = w1 - 1
Logical Words Bitwise AND Bitwise OR Bitwise XOR One’s complement : AND ( w1 w2 -- w3 ) ANDD (0015) ; Bitwise OR : OR ( w1 w2 -- w3 ) ORR (0016) ; Bitwise XOR : XOR ( w1 w2 -- w3 ) XORR (0017) ; One’s complement : INVERT ( w1 -- w2 ) INVERT (0014) ;
Logical Words TRUE = X“FFFF” FALSE = X“0000” : TRUE (-- w ) ONES (0020) ; FALSE = X“0000” : FALSE (-- w ) ZEROS (0021) ;
Branching and Looping in WHYP IF…ELSE…THEN FOR…NEXT BEGIN…AGAIN BEGIN…UNTIL BEGIN…WHILE…REPEAT
IF…ELSE…THEN <cond> IF <true statements> ELSE <false statements> THEN <cond> is either TRUE (-1) or FALSE (0)
WHYP Conditional Words < ( n1 n2 -- f ) (“less-than”) > ( n1 n2 -- f ) (“greater-than”) = ( n1 n2 -- f ) (“equals”) <> ( n1 n2 -- f ) (“not-equals”) <= ( n1 n2 -- f ) (“less-than or equal”) >= ( n1 n2 -- f ) (“greater-than or equal”) 0< ( n -- f) (“zero-less”) 0> ( n -- f) (“zero-greater”) 0= ( n -- f) (“zero-equal”) U< ( u1 u2 -- f ) (“U-less-than”) U> ( u1 u2 -- f ) (“U-greater-than”) U<= ( u1 u2 -- f ) (“U-less-than or equal”) U>= ( u1 u2 -- f ) (“U-greater-than or equal”)
FOR…NEXT Loop n FOR <WHYP statements> NEXT >R drjne <WHYP statements> Decrement top of return stack and branch back to <WHYP statements> if not equal to zero. Therefore, <WHYP statements> are executed n times.
BEGIN…AGAIN BEGIN <WHYP statements> AGAIN
BEGIN…UNTIL BEGIN <WHYP statements> <flag> UNTIL <flag> is either TRUE or FALSE usually from some WHYP conditional word
BEGIN…WHILE…REPEAT BEGIN <words> <flag> WHILE
x = 1; i = 2; WHILE (i <= n) { x = x * i i = i + 1 } factorial = x
FC16 Relational Operator Words Zero-equals -- true if T = 0 (NOT) : 0= ( n -- f ) zeroequal (0022) ; Zero-less-than -- true if T < 0 : 0< ( n -- f ) zeroless (0023) ; Zero-greater-than -- true if T > 0 : 0> ( n -- f ) DUP 0= \ n f1 SWAP 0< \ f1 f2 OR NOT ;
Relational Operator Words U-greater-than -- true if u1 > u2 : U> ( u1 u2 -- f ) ugt (0024); when ugt if N > T then ones; else zeros; endif; U-less-than -- true if u1 < u2 : U< ( n1 n2 -- f ) ult (0025); equal-to -- true if n1 = n2 : = ( n1 n2 -- f ) eq (0026) ; Implement these instructions directly In Funit
Relational Operator Words U-greater-than-or equal -- true if u1 >= u2 : U>= ( u1 u2 -- f ) ugte (0027) ; U-less-than-or equal -- true if u1 <= u2 : U<= ( u1 u2 -- f ) ulte (0028) ; not-equal-to -- true if n1 /= n2 : <> ( n1 n2 -- f ) neq (0029) ;
Relational Operator Words greater-than -- true if n1 > n2 : > ( n1 n2 -- f ) gt (002A) ; less-than -- true if n1 < n2 : < ( n1 n2 -- f ) lt (002B) ; use IEEE.std_logic_arith.all; variable avs, bvs: signed (width-1 downto 0); In Funit16
use IEEE.std_logic_arith.all; variable avs, bvs: signed (width-1 downto 0); variable y_tmp: STD_LOGIC_VECTOR (width-1 downto 0); for i in 0 to width-1 loop true(i) := '1'; false(i) := '0'; av(i) := a(i); bv(i) := b(i); avs(i) := a(i); bvs(i) := b(i); end loop;
In Funit16 when “101010" => if (avs > bvs) then y <= true; else y <= false; end if; when “101011" => if (avs < bvs) then y <= true;
Relational Operator Words greater-than-or-equal -- true if n1 >= n2 : >= ( n1 n2 -- f ) gte (002C) ; less-than-or-equal -- true if n1 <= n2 : <= ( n1 n2 -- f ) lte (002D) ;
Branching Words if ( f -- ) JZ (0102) -- else JMP (0101) then
Branching Words begin -- while ( f -- ) JZ (0102) repeat JMP (0101)
Branching Words begin -- until ( f -- ) JZ (0102)
Branching Words begin -- again JMP (0101)
Looping Words for (cnt -- ) >R --- next DRJNE