Transcript
Page 1: WHYP A Programming Language for the FC16 Forth Core

WHYP

A Programming Language for the

FC16 Forth Core

Page 2: 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)

Page 3: WHYP A Programming Language for the FC16 Forth Core

WHYP is developed from scratch in the new book:

Design of Embedded Systems Using 68HC12/11 MicrocontrollersbyRichard E. HaskellPrentice Hall, 2000

Page 4: WHYP A Programming Language for the FC16 Forth Core

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

Page 5: WHYP A Programming Language for the FC16 Forth Core

Chuck Moorereading Haskell’sWHYP book

Page 6: WHYP A Programming Language for the FC16 Forth Core

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

Page 7: WHYP A Programming Language for the FC16 Forth Core

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)

Page 8: WHYP A Programming Language for the FC16 Forth Core

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)

Page 9: WHYP A Programming Language for the FC16 Forth Core

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

Page 10: WHYP A Programming Language for the FC16 Forth Core

WHYP Arithmetic Operators

7 9 + .8 5 - .4 7 * .8 3 / .8 3 /MOD . .

Page 11: WHYP A Programming Language for the FC16 Forth Core

WHYP Colon Definitions

: squared ( n -- n**2)DUP * ;

: cubed ( n -- n**3)DUP \ n n squared \ n n**2* ; \ n**3

Page 12: WHYP A Programming Language for the FC16 Forth Core

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 )

Page 13: WHYP A Programming Language for the FC16 Forth Core

WHYP on the FC16

Page 14: WHYP A Programming Language for the FC16 Forth Core

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);

Page 15: WHYP A Programming Language for the FC16 Forth Core

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)

Page 16: WHYP A Programming Language for the FC16 Forth Core

Stack Manipulation Words

Colon Definitions

: 2DUP ( a b -- a b a b )OVER (0004) \ a b aOVER (0004) ; \ a b a b

Page 17: WHYP A Programming Language for the FC16 Forth Core

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) ;

Page 18: WHYP A Programming Language for the FC16 Forth Core

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

Page 19: WHYP A Programming Language for the FC16 Forth Core

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) ;

Page 20: WHYP A Programming Language for the FC16 Forth Core

Arithmetic Words

Plus w3 = w1 + w2: + ( w1 w2 -- w3 ) PLUS (0010) ;

minus w3 = w1 - w2: - ( w1 w2 -- w3 ) MINUS (0011) ;

Page 21: WHYP A Programming Language for the FC16 Forth Core

Arithmetic Words

One-plus w2 = w1 + 1: 1+ ( w1 – w2 ) PLUS1 (0012) ;

One-minus w2 = w1 - 1: 1- ( w1 – w2 ) MINUS1 (0013) ;

Page 22: WHYP A Programming Language for the FC16 Forth Core

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) ;

Page 23: WHYP A Programming Language for the FC16 Forth Core

TRUE = X“FFFF”: TRUE (-- w ) ONES (0020) ;

Logical Words

FALSE = X“0000”: FALSE (-- w ) ZEROS (0021) ;

Page 24: WHYP A Programming Language for the FC16 Forth Core

Branching and Looping in WHYP

IF…ELSE…THEN

FOR…NEXT

BEGIN…AGAIN

BEGIN…UNTIL

BEGIN…WHILE…REPEAT

Page 25: WHYP A Programming Language for the FC16 Forth Core

IF…ELSE…THEN

<cond> IF <true statements>

ELSE<false statements>

THEN

<cond> is either TRUE (-1) or FALSE (0)

Page 26: WHYP A Programming Language for the FC16 Forth Core

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”)

Page 27: WHYP A Programming Language for the FC16 Forth Core

\ 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 ;

Page 28: WHYP A Programming Language for the FC16 Forth Core

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

Page 29: WHYP A Programming Language for the FC16 Forth Core

BEGIN…AGAIN

BEGIN <WHYP statements> AGAIN

Page 30: WHYP A Programming Language for the FC16 Forth Core

BEGIN…UNTIL

BEGIN <WHYP statements> <flag> UNTIL

<flag> is either TRUE or FALSEusually from some WHYP conditional word

Page 31: WHYP A Programming Language for the FC16 Forth Core

BEGIN…WHILE…REPEAT

BEGIN <words> <flag>

WHILE <words>

REPEAT

Page 32: WHYP A Programming Language for the FC16 Forth Core

\ 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

Page 33: WHYP A Programming Language for the FC16 Forth Core

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 ;

Page 34: WHYP A Programming Language for the FC16 Forth Core

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);

Page 35: WHYP A Programming Language for the FC16 Forth Core

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) ;

Page 36: WHYP A Programming Language for the FC16 Forth Core

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

Page 37: WHYP A Programming Language for the FC16 Forth Core

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);

Page 38: WHYP A Programming Language for the FC16 Forth Core

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

Page 39: WHYP A Programming Language for the FC16 Forth Core

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) ;

Page 40: WHYP A Programming Language for the FC16 Forth Core

if ( f -- ) JZ (0102)----

else JMP (0101)------

then

Branching Words

Page 41: WHYP A Programming Language for the FC16 Forth Core

begin----

while ( f -- ) JZ (0102)------

repeat JMP (0101)

Branching Words

Page 42: WHYP A Programming Language for the FC16 Forth Core

begin------------

until ( f -- ) JZ (0102)

Branching Words

Page 43: WHYP A Programming Language for the FC16 Forth Core

begin------------

again JMP (0101)

Branching Words

Page 44: WHYP A Programming Language for the FC16 Forth Core

for (cnt -- ) >R------------

next DRJNE

---

Looping Words


Recommended