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 MicrocontrollersbyRichard E. HaskellPrentice 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 Moorereading Haskell’sWHYP book
is extremely modularis interactiveis easy to debugallow easy machine accessis fastis transportablecan be understood in its entiretyis unlike any other language
FORTH is a programming languages that ---
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.
Introducing WHYP (Forth)
The Structure of WHYP
Serial Line
send address
PC Target 68HC12
Kernel: LOOP BSR INWDY JSR 0,Y BRA LOOP
F82C
F82C ----- ----- ----- ----- ----- RTS
Dictionary: --- --- --- --- --- --- --- --- display F82C --- --- --- ---
C++ Program
ScreenC:\>whyp 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 WordsDUP ( 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 WordsFC16 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 WordsFC16 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 aOVER (0004) ; \ a b a b
Memory Access Words
Fetch\ 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
FC16
clk clrT
N
E1
SB
P
M
we
oe
cclk
E2
mclk bn
ProgramROM
P
M RAM
clk
clrwe
T
digload
clr
N
Return Stack WordsTo-R
: >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: + ( w1 w2 -- w3 ) PLUS (0010) ;
minus w3 = w1 - w2: - ( w1 w2 -- w3 ) MINUS (0011) ;
Arithmetic Words
One-plus w2 = w1 + 1: 1+ ( w1 – w2 ) PLUS1 (0012) ;
One-minus w2 = w1 - 1: 1- ( w1 – w2 ) MINUS1 (0013) ;
Logical Words
Bitwise AND: 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) ;
TRUE = X“FFFF”: TRUE (-- w ) ONES (0020) ;
Logical Words
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”)
\ Convert hex to ASCIIHEX
: hex2asc ( n -- asc ) 0F AND \ mask upper nibble DUP 9 > \ if n > 9 IF 37 + \ add $37 ELSE 30 + \ else add $30 THEN ;
>R
Decrement top of return stack and branch back to <WHYP statements> if not equal to zero.Therefore, <WHYP statements> are executedn times.
FOR…NEXT Loop
n FOR <WHYP statements> NEXT
drjne <WHYP statements>
BEGIN…AGAIN
BEGIN <WHYP statements> AGAIN
BEGIN…UNTIL
BEGIN <WHYP statements> <flag> UNTIL
<flag> is either TRUE or FALSEusually from some WHYP conditional word
BEGIN…WHILE…REPEAT
BEGIN <words> <flag>
WHILE <words>
REPEAT
\ Example of BEGIN...WHILE...REPEAT
: factorial ( n -- n! ) 1 2 ROT \ x i n BEGIN \ x i n 2DUP <= \ x i n f WHILE \ x i n -ROT TUCK \ n i x i * SWAP \ n x' i 1+ ROT \ x' i' n REPEAT \ x i n 2DROP ; \ x
x = 1; i = 2;WHILE (i <= n) { x = x * i i = i + 1 }factorial = x
Zero-equals -- true if T = 0 (NOT): 0= ( n -- f )
zeroequal (0022) ;
FC16 Relational Operator Words
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 f1SWAP 0< \ f1 f2
OR NOT ;
U-less-than -- true if u1 < u2: U< ( n1 n2 -- f )
ult (0025);
Relational Operator Words
equal-to -- true if n1 = n2: = ( n1 n2 -- f )
eq (0026) ;
if N > T then ones;else zeros;endif;
when ugt
Implement theseinstructions directlyIn Funit
U-greater-than -- true if u1 > u2: U> ( u1 u2 -- f )
ugt (0024);
U-greater-than-or equal -- true if u1 >= u2: U>= ( u1 u2 -- f )
ugte (0027) ;
Relational Operator Words
not-equal-to -- true if n1 /= n2: <> ( n1 n2 -- f )
neq (0029) ;
U-less-than-or equal -- true if u1 <= u2: U<= ( u1 u2 -- f )
ulte (0028) ;
greater-than -- true if n1 > n2: > ( n1 n2 -- f )
gt (002A) ;
Relational Operator Words
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);
for i in 0 to width-1 looptrue(i) := '1';false(i) := '0';av(i) := a(i);bv(i) := b(i);avs(i) := a(i);bvs(i) := b(i);
end loop;
variable y_tmp: STD_LOGIC_VECTOR (width-1 downto 0);
when “101010" => if (avs > bvs) then y <= true;
else y <= false; end if;
when “101011" =>
if (avs < bvs) then y <= true;
else y <= false; end if;
In Funit16
greater-than-or-equal -- true if n1 >= n2: >= ( n1 n2 -- f )
gte (002C) ;
Relational Operator Words
less-than-or-equal -- true if n1 <= n2: <= ( n1 n2 -- f )
lte (002D) ;
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)
Branching Words
for (cnt -- ) >R------------
next DRJNE
---
Looping Words