135

FORTH on the Atari Learning by Using

  • Upload
    glia

  • View
    19

  • Download
    0

Embed Size (px)

DESCRIPTION

FORTH is a programming language. It is easy to learn. Programs, written in FORTH are very short, compared to other high level languages. This book show the novice and the experienced programmer how to use this language on the ATARI . It has short examples and use sound and grafics for demonstration.

Citation preview

  • 5/19/2018 FORTH on the Atari Learning by Using

    1/132

  • 5/19/2018 FORTH on the Atari Learning by Using

    2/132

    This

    book

    is

    an

    independent

    production

    of Ing. W.

    HOFACKER

    GMBH International. It is published

    as

    a service to all ATARI per

    sonal

    computer

    users

    worldwide

    .

    All

    rights reserved. No part

    of

    this

    book

    may

    be

    reproduced

    by

    any means

    without

    the express

    written

    permission

    of

    the publisher.

    Example programs are for personal use only. Every reasonable

    effort has been made to ensure accuracy throughout this

    book

    but neither the

    author

    or publisher can assume responsibil ity for

    any errors or omissions. No lia

    bility is

    assumed

    for

    any direct or

    indirect damages resulting from the use

    of

    information contained

    herein.

    First

    Edition

    First Printing

    February 1983 in the Federal Republic

    of

    Germany

    Copyright

    1982

    by Winfri

    ed Hofacker

    Order-

    No. 17

    IS N

    3 88963

    -

    170

    -3

    Reference was made to

    AT

    A R I throughout t hi s book.

    ATAR

    is a

    trad

    emark of

    ATAR I Inc a

    di

    vision

    of

    Warner Communica

    ti

    ons Company.

    Publ i

    sher

    Ing . W. HOFAC KER Gm bH . T

    ege

    rnseerstr . 18 .

    0-8

    150 Ho lzk

    ir

    ch

    en

    . W

    .

    Germany

    US Distributor:

    E LCOMP Pu b li sh ing. Inc .. 53 Redrock Lane. Pomona

    CA

    9 1766

  • 5/19/2018 FORTH on the Atari Learning by Using

    3/132

    T BLE

    OF

    CONTENTS

    1 What is FORTH . . . .

    . . .

    . . . . . .

    . . 1

    2. Basic elements of FORTH . .

    . .

    . . . . . . . 4

    2-1 The stack . . . 4

    2-2 Words in FORTH . . .

    . . .

    .

    .

    . .

    . . .

    6

    3

    Using words . . . .

    . .

    . . . . . . . .

    . . . . . 9

    3-1 Definition of new words . . . . . . . .

    . .

    . . . . . . . . 9

    3-2 Changing the stack .

    . . . .

    10

    3 3 Fundamental operations in arithmetic . . . 12

    3 4 Input output . . . . . . .

    . .

    13

    3-5

    Some simple programs

    . .

    .

    15

    3-6

    The

    screen

    . .

    . .

    . . .

    . . . .

    . . . . . . . .

    . 19

    3-7 Constants and variables . . . . . . . . 21

    3-8

    Comparison . . . . . . .

    . . . . . .

    . . . . .

    24

    4 Control structures 25

    4-1 DO .... LOOP . . . . . . . .

    . .

    . . . . . . 25

    4-2 The return stack

    . .

    .

    . . .

    29

    4 3 IF .ELSE ....THEN ENDIF) 30

    4-4 BEGIN UNTIL . . . . . . . 32

    4-5 BEGIN .WHILE ... REPEAT . 33

    4-6

    The

    case statement . . . . . . . . . . . . . . . . . . . . . . . . . . 34

    5

    Sample programs . . 35

    5-1

    Some grafics

    . . .

    .

    .

    .

    . .

    .

    35

    5-2 Pattern

    . .

    . .

    . . .

    . . . . .

    . . .

    . . . . . . . . . . . . . . . . . . . .

    41

    5 3 Sound and color . . . . . . . . . . . . 42

    5-4 Hexdump . . . . . . . . . . . . . . . . . 46

    5-5 Largest common divisor . . .

    . .

    .

    48

    5-6 F ibboconacci numbers . . . . . . . . .

    . . . . . . . . 50

    5 7 Prime numbers . . . . . . . . . . . . 52

    5-8

    More sound and grafics . .

    .

    . .

    .

    53

    5-9 Using the game

    port for

    control app lications

    . . .

    . . . 55

  • 5/19/2018 FORTH on the Atari Learning by Using

    4/132

    6. Text and strings 64

    6 1

    INPUT/OUTPUT

    of text

    .

    64

    6 2

    Formatting the

    output

    . . 70

    7.

    The vocabulary .

    . .

    .

    .

    74

    7 1 Different vocabularies .

    . .

    . . . . . 74

    7 2 Play organ

    or

    piano

    with

    the

    ATAR

    I 76

    7 3 The construction of a FORTH

    word

    80

    7 4

    Changing the

    top of

    the

    dictionary

    .

    . 84

    7 5 The virtual memory . . 88

    7 6 Definition

    words . . . . .

    91

    8. Applications .

    . .

    .

    95

    8 1

    Mailing list .

    . .

    . . . . . 95

    8 2 Serial output via game port three . . . . . . . . . . . . . .

    107

    9.

    Appendix

    . 114

    A FORTH-compiler/interpreter is available from the

    following

    vendors:

    1 ELCOMP

    PUBLISHING

    INC. Pomona CA 91766

    Phone 714) 6 2 3 ~ 3 4

    2.

    Quality

    Software

    6660

    Resede

    Blvd. Suite

    105,

    Reseda

    CA 91335, Phone 213) 344-6599

    Books and source code

    information are

    available

    from:

    1. FORTH

    Interest Group

    P.O.Box 1105

    San

    Carlos

    CA

    94070

    2. Mountain View Press

    P.O.Box

    4656

    Mountain View CA

    94040

    3. FORTH Inc.

    2309

    Pacific Coast

    Hwy.

    Hermosa Beach CA 90254

  • 5/19/2018 FORTH on the Atari Learning by Using

    5/132

    PREFACE

    ATARI FORTH PROGR MMING

    Learning by using

    FORTH

    i s

    a

    new,

    exc i t i ng

    programming

    language. I t i s easy to l ea rn and programs,

    wri t t en

    in

    FORTH are very sh o r t

    compared

    to o ther high l eve l languages.

    The

    aim of t h i s book i s to show

    the

    novice

    and the exper ienced

    programmer

    how to

    use

    t h i s

    language

    on

    the

    ATARI

    The

    examples

    are shor t and

    use

    sound and

    gr a f i c s

    for

    demonst ra t ion .

    Use

    these examples

    to

    lea rn

    FORTH

    Two

    a p p l i c a t i o n s

    a

    mai l ing

    l st and a

    s e r i a l i n t e r f ace for a p r i n t e r are

    inc luded . These two app l i ca t i ons show the

    wide

    v a r i e t y

    FORTH

    can

    be

    used

    fo r .

    With

    FORTH

    an app l i ca t ion

    can

    and

    debugged

    f a s t e r

    than in

    programming language.

    be

    wri t t en

    any other

    I

    hope t h i s

    book

    wi l l add new

    f r i ends to

    the

    community

    of

    FORTH

    programmers.

    Thank s

    to Rick Schwarz, who helped me

    in

    proofreading .

    E.Floege l

  • 5/19/2018 FORTH on the Atari Learning by Using

    6/132

  • 5/19/2018 FORTH on the Atari Learning by Using

    7/132

    VVhatisFORTH

    1

    WH T

    IS

    FORTH?

    FORTH i s l i ke BASIC, FORTR N or PASCAL a

    programming

    language

    I t

    i s , however

    qu i t e

    d i f f e r en t

    from these l anguages . I t was

    invented

    by CH RLES MOORE about t en years

    ago and

    i t s

    f i r s t use

    was to

    con t ro l

    t e lescopes

    on

    t he

    Ki t t

    Peak

    observa to ry .

    Today

    FORTH i s widely used in

    con t ro l

    app l i ca t ions . One example

    i s

    the con t ro l

    of movie

    cameras in the

    movie

    BATTLESHIP

    GALACTICA Using the concept of v i r t u a l

    memory, FORTH a l so i s used for da ta base

    managment

    FORTH

    can

    not be compared

    with other

    programming

    languages While you

    are

    learn ing to program in FORTH t would be

    be t t e r to fo rge t a l l you have l ea rned

    about

    othe

    r

    languages .

    What makes FORTH so unique ?

    One of the bas ic elements of FORTH i s the

    s tack . Everybody knows what a

    s tack i s .

    Nearly every desk

    has a s tack of paper .

    Something i s

    l a i d upon

    t h i s s tack and may

    be

    removed

    l a t e r

    In

    terms of a computer

    t h i s i s a

    LIFO

    Last I n , F i r s t

    O u t

    memory

    The

    l a s t

    th ing

    put

    onto

    the s tack

    i s

    removed

    f i r s t .

  • 5/19/2018 FORTH on the Atari Learning by Using

    8/132

    FORTH uses the s tack in

    two d i f f e r en t

    ways.

    F i r s t t he re i s

    a

    parameter

    s tack

    for da ta

    and

    ca l cu l a t i ng . Second t he re i s a s tack

    for

    words ca l l ed

    the d ic t ionary or

    vocabulary .

    In

    FORTH

    words

    are

    the

    elements for programming.

    Severa l of

    these

    words can be combined to form a new

    word

    performing a

    ce r t a in

    t a sk . A

    bas ic

    d ic t ionary

    of

    predef ined

    words i s

    conta ined with in FORTH.

    These

    words

    are

    used by

    the

    programmer to

    c rea t e

    new words.

    This

    new

    words

    are

    placed

    on

    top

    of

    the

    d ic t iona ry .

    Running a program in FORTH

    means

    ca l l i ng

    a sequence of words.

    For example

    you have

    wr i t t en a FORTH

    program to

    con t ro l

    s tepper

    motors .

    START

    may be a

    word

    for

    s t a r t i n g the s tepper

    motor .

    FASTER

    SLOWER

    may be

    words

    for

    changing the

    speed

    while

    STOP could

    be the

    word

    for s toping it

    Defining your own

    words

    makes FORTH

    very

    f l ex i b l e . Words can be

    rearanged

    to

    perform new t a sks .

    All t h i s makes FORTH a

    dynamic language. On

    the

    o ther hand a l l

    t h i s

    freedom

    makes

    the

    programmer

    respons ib le for

    the

    cor rec tness

    of a

    program. There are only a few er ro r

    messages

    and warnings .

    Though FORTH i s used sometimes

    with other

    opera t ing systems it could be

    seen

    as i t s

    own opera t ing

    sytem. I t i s an

    i n t e rp re t e r

    ca l l i ng and execut ing one word. I t i s

    a l so

    a compi ler

    with i t s

    capab i l i t y to

    compile

    new words in

    the

    d ic t i na ry .

    I t

    uses a

    t ex t

    e d i t o r

    and

    of t en

    an

    assembler .

    In t h i s bookle t we wi l l l ea rn FORTH by

    using t h i s

    language

    on

    an

    ATARI

    400/800.

    We wi l l use

    graph ics

    sound and j oys t i ck s .

    2

  • 5/19/2018 FORTH on the Atari Learning by Using

    9/132

    All programs

    however,

    which don t use

    specif ic hardware on the

    T RI

    can be

    easi ly

    adapted

    to

    other computers.

  • 5/19/2018 FORTH on the Atari Learning by Using

    10/132

    asic

    l m nts

    of

    ORTH

    2 .

    BASIC

    L M NTS

    OF

    FORTH.

    Let us

    f i r s t

    make some remarks concerning

    the

    bas ic

    elements

    of

    FORTH.

    2-1

    TH

    STACK.

    As

    mentioned

    above

    the

    s tack

    i s

    a LIFO

    memory. In Fig

    2-1 a

    s tack

    i s represented

    in

    two ways. Fig 2 l a shows

    the

    normal

    rep resen ta t ion of a s tack . In t h i s book

    we

    wi l l use the s tack

    as

    it

    i s

    shown

    in Fig 2-

    lb .

    TOS

    Fig .2-1a

    TOS

    - .--. .---- --.----r---r------.

    - - L _ - - - . l . _ - - J i . . . - _ - - - _ ~ _ - - _ . . . J

    Fig.

    2- 1 b

    Fig 2-1

    Two

    r ep resen ta t ions of

    the s tack .

    4

  • 5/19/2018 FORTH on the Atari Learning by Using

    11/132

    The top of s tack TOS )

    i s

    always the

    r ightmost element . One

    memory

    c e l l of

    the

    s tack

    i s a

    16

    b i t c e l l .

    For documentat ion

    and unders tanding

    of

    FORTH

    words,

    t

    IS

    necessary

    to show

    how

    the s tack i s

    af fec ted by using a word.

    We

    wi l l use the

    fol lowing

    abbrev ia t ions :

    a 16-b i t address

    n s igned 16 b i t

    number

    u unsigned

    16 b i t

    number

    d s igned 32 b i t number

    b 8

    b i t

    byte

    c 7 b i t ASCII cha rac t e r

    f boolean number, f l ag

    These

    abbrev ia t ions are

    fo l lowing manner:

    used

    in

    WORD STACK

    BEFOR -

    ST CK

    AFTER )

    Examples:

    LOOK an)

    the

    The word LOOK, whatever

    t

    does , reques ts

    an address and a number on top

    of

    s tack

    before execut ing .

    Both

    i tems

    are

    removed

    from

    s tack a f t e r

    execut ion .

    FOUND - f )

    FOUND

    doesn t

    need any parameter before

    execut ion and l eaves a boolean number f

    on

    the s tack

    a f t e r

    execut ion .

    COMP a a c - nf)

    The word COMP

    needs

    charac te r on

    top

    execut ion a

    number

    l e f t

    on

    s t ack . In

    top

    of

    s tack

    i s

    two

    addresses and a

    of

    the s tack . After

    and a boolean f l ag i s

    t h i s r ep re sen ta t i on , the

    always

    the

    r igh tmost

    5

  • 5/19/2018 FORTH on the Atari Learning by Using

    12/132

    cha rac t e r . To d i f f e r between severa l

    addresses or

    numbers t he re

    can be

    added

    a

    hyphen

    or

    a

    number to the charac te r l i ke

    a

    a or n n l n2.

    2-2 WORDS IN

    FORTH.

    A

    word in

    FORTH may be

    any a rb i t r a ry

    s t r i n g of

    charac te r s , excluding

    th ree

    spec ia l cha rac te r s .

    The

    excluded

    charac te r s

    are :

    the

    space

    backspace

    and

    the re turn

    charac te r .

    The space

    charac te r

    i s

    the only

    de l imi te r

    between

    FORTH words.

    The backspace charac ter i s

    used

    for

    cor rec t ing

    typing e r ro r s ,

    while

    the re turn

    charac te r

    i s

    used to ind ica te t h a t the

    input to

    the

    computer i s f in i shed .

    Examples

    of words:

    FIRST

    lTIMES

    NAME

    @VALUE

    THR EE i s

    not

    recogniced as

    because t he re

    i s

    a space

    in it

    represen t

    two words THR and

    EE.

    one

    word

    I t would

    Some words in the

    predef ined

    d ic t ionary

    cons i s t of only one

    charac te r . The

    dot

    i s for example the pr in t ing

    s ta tement .

    The @ s ign fe tches the conten t of a 16 b i t

    memory c e l l . These l e t t e r s

    can be used

    to

    i nd ica te

    a

    spec ia l

    funct ion

    of

    a

    word.

    In

    the

    example above

    NAME

    wi l l

    p r i n t

    a

    name

    while

    @VALUE wi l l ge t

    some value from

    memory.

    This

    naming convent ion makes it

    eas i e r to

    read

    FORTH programs.

    2-3 THE

    REVERSE POLISH NOTATION.

    For

    ca l cu l a t i ons , FORTH

    uses the reverse

    Pol i sh , or pos t f ix nota t ion .

    The

    opera tor

  • 5/19/2018 FORTH on the Atari Learning by Using

    13/132

    for the ca l cu la t ion i s entered

    here

    a f t e r

    ente r ing

    the numbers. I f you

    type in

    36

    f i r s t a

    t h ree

    i s put on s t ack then a

    s ix .

    At t h i s

    t ime,

    the

    ORTH

    i n t e r p r e t e r

    recognices

    the word + This word takes

    the

    two

    top numbers

    on

    s t ack adds them

    toge ther and

    places the

    r e s u l t 9 , on top

    of

    the

    s t ack . I f you now

    en te r

    the

    command a 9

    i s

    disp layed

    on

    the

    screen.

    Try

    it.

    The word +

    i s

    def ined

    in

    the fo l lowing

    manner:

    + nnl -

    n2)

    n2=n+nl

    Exerc ises :

    3*4-7

    3*5+3)/6-4

    5

    A

    2-4

    A

    2

    3+5)*2

    20/ 2*5)

    Using

    the reverse

    Pol ish

    n o t a t i o n

    t he re

    i s no

    need to use bracke t s . Let

    us look a t

    the

    fol lowing example:

    3+5)* 4-2)-7

    A BASIC

    i n t e r p r e t e r

    who

    has

    to

    decipher

    such an express ion

    s t a r t s with the opening

    bracke t then

    gets

    a number, an

    opera to r

    once

    more a

    number

    and then the

    c los ing

    bracke t . At t h i s moment it can do the

    f i r s t

    ca l cu l a t i on .

    The next s t ep ind ica t e s

    a requirement for

    mul t ip l i ca t i on . This can

    be done a f t e r

    reveal ing the second

    express ion .

    7

  • 5/19/2018 FORTH on the Atari Learning by Using

    14/132

    Using

    RPN,

    t

    i s eas i e r for a

    computer

    program to ca l cu la t e t h i s express ion . In

    RP

    we en te r

    3 5 4 2 -

    7 -

    You see t he re i s no need for

    bracke t s .

    Fig

    2-2

    shows

    the

    eva lua t ion

    on s t ack .

    ST CK

    TOS

    3

    INPUT

    3

    3

    8

    8

    4

    .

    8

    6

    5

    8

    4

    2

    2

    16

    7

    9

    5

    +

    4

    2

    7

    Fig

    2-2

    Calcu la t ing an

    ar i thmet ic

    express ion

    using RPN.

    To ge t acquain ted

    with t h i s reverse

    no ta t ion the re

    are

    some

    exe rc i

    se s .

    them i n to the compute r

    and

    use

    command

    to

    ge t

    the r e su l t s .

    Exerc ises :

    8

    3*4-7

    (3*5+3)/6-4

    5 2-4 2

    (3+5)

    *2

    20/(2*5)

    Pol i sh

    Type

    the

  • 5/19/2018 FORTH on the Atari Learning by Using

    15/132

    3 sing words

    3.

    USING WORDS

    You can f ind a d ic t ionary

    of

    a l l common

    words

    in appendix A. Here we wi l l

    discuss

    the

    most

    used words

    for

    wri t ing programs

    in

    FORTH.

    3-1 DEFINITION OF

    NEW

    WORDS

    The

    def in i t ion

    of a new

    word

    s t a r t s

    with

    the s ign , followed by a space and the

    name of the new

    word

    .

    Then

    a l l words used

    by t h i s new word are l i s t ed . The

    def in i t i on ends with t h e ; s ign .

    : XXX

    Eegin

    of a colon

    def in i t ion XXX

    End

    of

    a colon

    def in i t ion .

    Unti l now

    we have learned only two

    words.

    The

    word

    and the word. We

    want to

    combine these two to a new

    one,

    ca l l ed .

    plus pr in t . I t adds two numbers and

    disp lays the

    r e su l t .

    The de f in i t i on i s :

    :

    .

    Now

    we

    can

    enter

    3 6

    . RET

    an

    d

    get

    the

    r e su l t 9

    on

    the sc r een .

    9

  • 5/19/2018 FORTH on the Atari Learning by Using

    16/132

    To show

    t h a t

    the

    opera tors

    rea l FORTH words we change the

    these

    two words

    by

    + and - are

    meaning of

    + -

    After h i t t i n g re turn

    we ge t

    a warning

    ISN T UNIQUE OK

    This ind ica tes

    t ha t

    the

    word

    al ready def ined . The

    i n t e r p r e t e r

    the

    new def ined

    +

    When

    we

    now

    RET, we ge t the

    r e su l t

    3.

    + has

    been

    wi l l

    use

    type

    3 6

    Let us fo rge t t h i s new word. By typing

    FORGET

    RET

    t h i s def in i t i on and

    made

    l a t e r

    on

    d ic t ionary .

    a l so a l l

    def in i t i ons

    are

    removed

    from

    the

    3-2 CH NGING THE STACK.

    The s tack can be changed in t h ree ways. I t

    can

    be

    enlarged,

    reduced or

    rearanged. All

    t h i s can be done

    with the fol lowing

    words:

    10

    DUP n -

    nn) Dupl ica te

    top of s t ack .

    DROP n)

    Throwaway the

    top

    of

    s tack .

    SW P n n - n n ) Reverse the two top

    elements .

    OVER n n - n n n ) Copy of the second

    element

    on top .

    ROT nln2n3-n2n3nl)

    Rotate the top

    th ree

    elements

    counte r

    clock

    wise

    .

  • 5/19/2018 FORTH on the Atari Learning by Using

    17/132

    Fig 3-1 shows how these words a f f ec t the

    s t ack

    2

    4

    2

    4

    5

    2

    4

    3

    2

    4

    3

    2 4

    2 4

    3

    5

    3

    3

    5

    3

    5

    3

    3

    5

    3

    5

    3

    DUP

    ROT

    SW P

    DROP

    OVER

    Fig

    3-1

    Changing

    the

    s t ack

    Now l e t us do some examples and exe rc i s e s

    Example

    1:

    The s tack

    con ta ins

    the

    numbers 3 2 1

    with

    the

    1 on

    top

    of the

    s t ack

    What words must

    be

    en te red to ob ta in the

    sequence

    3 2 2

    I?

    F i r s t we

    en te r

    OVER and ge t 3 2 1 2 .

    Second we en te r SWAP to ob ta in the r e su l t

    3 2 2

    1

    Example 2:

    On

    the

    s tack

    we

    have

    3 2 1 with

    the

    1

    on

    top

    of the

    s t ack

    Which

    words must be

    entered to

    ge t the sequence 2 3 3

    ?

    The

    answer i s shown below.

    DROP

    SWAP

    DUP

    Exerc ise :

    and ge t

    S t a r t

    a

    b

    c

    d

    e

    3 2 1

    3

    2

    2 3

    2 3 3

    with

    3 2

    1 on

    3 2

    2 3

    23

    23

    32 2

    the

    s tack

  • 5/19/2018 FORTH on the Atari Learning by Using

    18/132

    The w o r d . S dot s , p r in t

    stack)

    shown

    in

    Fig

    3-2

    i s very

    use fu l l . I t

    i s

    def ined in

    most

    of the FORTH vers ions and

    pr in t s

    the

    conten ts

    of

    the s tack without des t roying

    it

    I f your FORTH vers ion

    does n t know

    t h i s word j u s t type it in .

    S

    SP@ ;

    DEEP SO @

    S

    - 2 / 1 -

    .S CR DEEP S 2 -

    SO

    @ 2 -

    DO

    I @

    -2

    +LOOP

    ELSE

    .

    EMPTY THEN

    Fig

    3-2

    Non-des t ruct ive

    s tack pr in t .

    3-3

    FUNDAMENTAL

    OPERATIONS IN ARITHMETIC.

    The word for the fundamental opera t ions of

    ar i thmet i c

    are def ined as fo l lows:

    +

    nnl-n2)

    n2=n+nl

    nnl-n2)

    n2=n-nl

    nnl-n2) n2=n*nl

    /

    nnl-n2) n2=n/nl

    This ar i thmet i c i s done with 16 b i t s igned

    f ixed numbers.

    Finding the

    remainder and

    the

    quot ien t

    of

    a

    def in i t i on

    you

    can use

    two more words.

    MO nnl-n2) n2 i s the

    remainder

    of n /n l .

    /MOD nn1-n2n3) n2

    i s

    the remainder

    and

    n3

    i s

    the

    quo

    t i en t

    of

    n /n l .

    12

  • 5/19/2018 FORTH on the Atari Learning by Using

    19/132

    Example:

    We want to c a l c u l a t e

    the value

    of t he

    te rm

    XA

    + X*y +

    Z. The va lues for X,

    Y

    and

    Z

    are

    s to red

    on

    the

    s tack wi th

    Z

    on

    top

    of

    the

    s t a c k

    Fig

    3-3 shows t he sequence of

    words

    to c a l c u l a t e t h i s va lue

    x

    Y

    x

    Z

    - -

    Z

    Y

    Z

    Y

    x

    z

    x

    x

    Z

    x

    Z

    Z

    Y

    x

    x

    Y

    X+Y

    X X+YI

    RG

    SWAP

    R T

    OUP

    R T

    +

    Fig

    3-3

    Calcu la t ing

    the

    te rm

    XA2+X*Y+Z.

    We can

    def ine a

    word

    .VALUE

    which needs

    t h ree numbers

    on

    the

    s tack

    and c a l c u l a t e s

    the

    value

    of t h i s express ion

    VALUE nnln2)

    SWAP

    ROT

    DUP ROT + * +

    .

    ,

    Exerc i ses :

    for

    Try to

    ge t the

    word sequences

    a) X

    A

    2+X*Y-Z

    b)

    x

    A

    2+x*Y+Z

    A

    2

    c) X

    A

    2-X*Y-Z

    A

    2

    with

    X Y Z

    on

    the

    s t ack

    and

    ge t the

    r e s u l t s

    3-4 INPUT OUTPUT .

    I n s e r t

    numbers

    One of

    the output

    i n s t r u c t i o n s we

    have

    a l read

    y

    used was th e

    .

    word.

    I t

    de f i n i t i on

    i s :

    n)

    Pr in t the top of s t ack

    13

  • 5/19/2018 FORTH on the Atari Learning by Using

    20/132

    Other output i n s t ruc t ions are

    :

    Example :

    Pr in t

    message.

    The

    message ends

    with

    . HELLO

    RET HELLO OK

    EMIT

    c)

    Pr in t s

    ASCII

    value

    c .

    Example:

    69 EMIT RET EOK

    R

    SPACE

    SPACES

    .R

    Example:

    Pr in t s

    one ca r r i age

    re turn

    .

    Pr in t s

    one

    space .

    n) Pr

    i n t s n spaces .

    nnl) Pr in t n, r i g h t j u s t i -

    f i ed

    in

    f i e l d . Fie ld -

    with i s n l .

    F i r s t we def ine a word P as

    P (

    n) 4

    R R ;

    Now we en te r :

    R 1 P 10 P 100 P

    and ge t the r e su l t

    14

    1

    10

    100

  • 5/19/2018 FORTH on the Atari Learning by Using

    21/132

    The fo l lowing

    words are

    for

    da ta input :

    KEY

    ( - c ) Reads the keyboard and

    places the

    ASCII

    value

    on

    the

    s t ack .

    ?TERMINAL - f ) True

    i f

    the

    break key

    i s pressed .

    But on

    the ATARI:

    ?TERMINAL -n

    n=l ,2 ,4

    i f

    one

    of

    the

    yel low

    keys was

    pressed .

    n=O

    i f

    no key was

    pressed .

    START

    key

    n=l

    SELECT key n=2

    OPTION

    key

    n=4

    There are some

    more input

    and

    output

    words,

    which wi l l be

    discussed

    l a t e r on in an

    ex

    ample.

    3-5 SOME SIMPLE PROGRAMS

    Unti l

    now we have l ea rned

    only

    a

    few

    FORTH

    words, however

    they

    are

    s u f f i c i e n t

    to

    wri te some simple programs.

    3-5a A LANGUAGE TRANSLATOR.

    The

    concept of the

    d ic t ionary and

    of

    de f in

    ing new words can be

    used

    for a language

    t r ans l a to r .

    As an example we t r a n s l a t e

    some words in to the german

    language.

    I . ICH ;

    AM

    .

    BI N

    ;

    HERE

    .

    DA

    ;

    5

  • 5/19/2018 FORTH on the Atari Learning by Using

    22/132

    I f we

    type

    I M HERE

    we

    ge t

    the

    t r an s l a t i o n

    ICH

    BIN

    DA

    This

    i s

    only a very simple example.

    But

    you

    can

    use

    t

    to remeber

    spec ia l

    words in

    a fore ign language chemical formulas or

    to

    make

    a

    l st

    of

    your

    favour i t e

    rad io

    s t a t i o n s

    and t h e i r

    f requencies . For

    example:

    KBIG

    : KIQQ

    104 FM

    100 FM ;

    3-5b WEIGHTWATCHER

    Some

    peoples

    want

    to ca l cu l a t e

    the

    amount

    of ca l o r i e s

    they had

    for breakfas t

    or

    dinner .

    The

    input

    of

    da ta

    should

    be

    done

    in the fo l lowing way:

    BEER 1 GLAS

    COKE

    2 GLASSES

    BREAD 1 SLICE and

    so

    on.

    The program

    i s

    shown in Fig .3 4 . F i r s t

    t he re

    i s

    a

    comment

    ind ica t ing

    which

    un i t s

    of

    measuremen

    t

    are

    used for

    the d i f f e r en t

    types of foods. Regard t h i s only as an

    example.

    Change

    them as you l i ke . The next

    screen

    ( we

    wi l l discuss t h i s express ion

    in the next subchapter ) , conta ins the

    def in i t i ons

    for

    the un i t s of measurement

    while

    the

    fol lowing

    screen conta ins the

    def in i t i ons for the ca lo r i e s .

    16

  • 5/19/2018 FORTH on the Atari Learning by Using

    23/132

    Fig 3-4 Weightwatcher .

    SCR

    125

    o

    1

    2

    3

    4

    5

    6

    7

    8

    9

    ( AFB WEIGHTWATCHER ef

    ( UNIT GLAS : BEER, APPLEJUICE

    COCA-COLA, CHAMPAGNE

    UNIT SLICE

    BREAD

    UNIT PIECE

    CAKE, MUFFINS

    UNIT OZ : BEEF , HAM CHIPS,

    NUTS

    , CHEESE

    10

    11

    12

    13

    14

    15

    UNIT CUP :

    RICE,

    PASTA

    UNIT TBSP [ TABLESPOON

    BUTTER ,

    UNIT

    PKG

    :

    SEAFOOD

    ,

    SCR

    126

    o

    AFB

    WEIGHTWATCHER

    ntd

    ef

    1 MEASUREMENT UNITS )

    2 GLAS DROP

    + ;

    3 GLASSES

    + ;

    4

    SLICE DROP +

    5 SLICES

    +

    6 PIECE DROP

    +

    7

    PIECES

    +

    8

    OZ

    DROP + ;

    9 OZES + ;

    10

    OF

    ;

    11 START 0 ;

    12 TBSP DROP

    + ; :

    TBSPS

    +

    13 PKG

    DROP + ;

    PKGS

    +

    14

    CUP

    DROP + ; : CUPS +

    15

    17

  • 5/19/2018 FORTH on the Atari Learning by Using

    24/132

    SCR 127

    o AFB WEIGHTWATCHER cntd ef

    1

    CALORIES

    2

    BEER

    255

    ; :

    COCA COLA 88

    3

    BREAD 100

    ;

    4

    BUTTER 100

    ; :

    BROWNIES 224

    5 RICE

    200

    ; :

    GOUDA 108

    ;

    6 MUFFINS 118

    7

    LASAGNE

    241 RAVIOLI 210

    8 BURITO 47 ; CNCHIPS 166

    9

    PEANUTS

    179

    PTCHIPS

    156

    10

    11

    12 AMOUNT DUP

    13

    14

    15

    START OK

    COCA COLA

    1

    GLAS O

    BREAD

    2

    SLICES OK

    MUFFINS 2 PIECES O

    GOUDA

    2

    OZ.ES OK

    BUTTER

    1 TBSP

    OK

    AMOUNT 840

    OK

    CNCHIPS 2

    OZES

    O

    AMOUNT 1172 OK

    LASAGNE 2 CUPS

    O

    AMOUNT 1654 OK

    Fig

    3-4

    Weightwatcher .

    I f you en te r the word BEER , the number 255

    i s

    put

    on

    the

    s tack

    . When you now

    enter

    2

    GLASSES ,

    f i r s t

    the

    number

    2

    i s

    put

    onto

    the s tack

    then

    the both top

    numbers

    ( 255

    2 ) are mul t ip l ied and the r e su l t i s

    added

    to

    the

    previous amount .

    To s t a r t t h i s ca lcu la t ion

    of

    ca lo r i e s you

    have to enter the

    word

    START, which puts a

    o on

    top

    of the

    s tack .

    The

    word

    AMOUNT

    dupl ica tes

    the

    top

    of

    s tack

    and

    pr in t s

    t

    on

    the screen .

    8

  • 5/19/2018 FORTH on the Atari Learning by Using

    25/132

    3-6 THE SCREEN.

    The

    wri t ing

    of a FORTH program

    can

    be done

    in

    two

    ways.

    You

    can

    en te r

    t he

    FORTH

    words

    d i r ec t i n to the d ic t ionary or

    use

    a t ex t

    ed i to r

    and wr i t e the words i n to a sc reen .

    Fiq . 3-5

    shows

    an example of a sc reen .

    FORTH vers ions running on an ATARI use a

    screen

    wi th

    16 l i n e s and 32

    charac te r s

    each.

    SCR

    148

    o

    (SCRN PRINT

    10/14/82

    ef)

    1 0

    VARIABLE ROW

    0

    VARIABLE COLN

    2

    ?FIN -f)

    COLN @ 24 = ;

    3

    lROW

    40 0 DO

    ROW @

    C 32

    +

    4

    DUP

    128

    > IF

    32 -

    THEN

    5 OUTCHR 1 ROW

    +

    LOOP

    6

    7 .SCRN

    88

    @

    ROW

    0 COLN

    8

    BEGIN lROW CRR

    1

    COLN +

    ?FIN

    9

    UNTIL;

    10

    11

    12

    13

    14

    15 -->

    Fig 3-5 Textsc reen .

    This

    uses

    512

    bytes

    of

    memory.

    Thus,

    when

    using a disk dr ive , you

    can

    s t o r e

    164

    sc reens .

    In

    t h i s bookle t we

    d o n t

    descr ibe

    the ed i t o r . I t has numerous va r i a t i ons

    in

    the

    var ious FORTH ver s ions .

    Please

    r e f e r

    to the

    i n s t r uc t i on

    manual

    of your FORTH.

    Once

    you

    LO D

    it.

    have

    LO D

    wri t t en

    compiles

    a sc reen ,

    you

    can

    the

    words

    of

    a

    19

  • 5/19/2018 FORTH on the Atari Learning by Using

    26/132

    screen

    i n to the d ic t iona ry .

    You

    LIST a sc reen . LIST disp lays the

    of a

    disk screen

    on the TV sc reen .

    The d e f i n i t i o n s are :

    may a l so

    conten t

    LO D ( n )

    LIST ( n )

    Compile

    disk

    screen n

    i n to the d ic t iona ry .

    L i s t disk screen n on

    the TV screen .

    There

    are

    some

    convent ions

    in the wr i t ing

    of a

    sc reen . The

    f i r s t

    l i n e

    should

    be

    a

    comment,

    not ing the t ask ,

    the da te and

    the

    programmer .

    For example :

    WEIGHTW TCHER

    10/16/82

    ef)

    The comment in

    FORTH

    s t a r t s

    with

    the

    opening bracke t and

    ends with

    the c los ing

    bracke t . The i s a FORTH word and must

    the re fo re

    be fol lowed

    by

    one space .

    Beginning of a comment.

    You can use the word INDEX to d isp lay a l l

    f i r s t l i n e s of a

    disk screen

    on the TV

    sc reen .

    INDEX nn ) Display

    the

    f i r s t

    l i nes

    from

    screen n to

    screen

    n .

    In

    wri t ing a screen , every new d e f i n i t i o n

    of a

    word

    should s t a r t in a new l i ne .

    This

    makes t eas i e r to

    read

    a FORTH

    program.

    After wri t ing you

    save

    the screen

    on

    disk

    with

    the word

    FLUSH.

    FLUSH

    Saves

    screen

    on

    disk

    .

    20

  • 5/19/2018 FORTH on the Atari Learning by Using

    27/132

    3-7 CONSTANTS AND

    VARIABLES.

    Most

    of the

    da ta

    used for

    ca l cu l a t i on

    i s

    s tored on the s t ack . Sometimes t i s

    necessary

    to

    use

    a

    cons tan t

    or

    a

    var i ab l e .

    The d e f i n i t i o n s of a cons t an t and a

    va r i ab l e are:

    CONSTANT NAME n) Crea tes a cons tan t

    NAME with the

    value

    n.

    VARIABLE

    NAME

    n) Crea tes a

    va r i ab le

    NAME and the

    i n i t i a l value n.

    There i s a

    d i f fe rence in

    ca l l i ng a

    cons tan t

    or

    a var i ab l e . Cal l ing a cons tan t

    py

    name,

    the

    value

    of

    the

    cons tan t

    i s

    place on the s tack . Cal l ing

    a

    va r i ab le by

    name,

    the

    address

    of t h i s va r i ab le

    i s

    placed on the s tack

    . In

    order to ge t the

    value

    of

    a

    var i ab l e you have to

    f e tch

    t

    .

    This i s done

    by the word @ For

    changing

    the value of

    a

    var i ab l e the word

    (

    s to re

    )

    i s

    used.

    @

    a-n)

    na)

    Fetches the

    conten t

    s tored a t address a .

    S to res

    n

    a t address a .

    Both

    i n s t r u c t i o n s fe tch and

    s t o r e

    use 16

    b i t

    numbers.

    For

    s to r ing

    and

    fe tch ing

    a

    s ing le by te the fo l lowing words are used.

    C

    a-b)

    C

    ba)

    Fetches a s ing le byte

    from

    address

    a .

    Sto r es a

    s ing le byte

    a t

    address

    a .

    21

  • 5/19/2018 FORTH on the Atari Learning by Using

    28/132

    Try t h i s :

    o

    755

    C

    4

    755

    C

    3

    755

    C

    The

    cursor

    w i l l disappear .

    The

    l e t t e r s

    are upside

    down.

    Rese ts

    to

    the

    normal

    mode.

    Later

    we

    w i l l use

    these

    words to cont ro l

    with the co lo r and sound r eg i s t e r s of the

    ATARI.

    You

    can increment the value of

    a var i ab le

    by

    o VARIABLE V

    V

    @

    1

    +

    V

    or you

    can

    use the word + ( p l u s - s to r e .

    +

    na)

    Add n

    to

    the conten t

    a t address a .

    Another word ?

    fe tches

    the conten t of a

    va r i ab l e

    and

    p r

    i n t s

    .

    ?

    (

    a)

    Pr in t s

    the conten t

    of

    address a .

    The FORTH system i t s e l f uses severa l

    cons tan t s and va r i ab l e s

    . We

    w i l l use the

    va r i ab l e

    BASE in

    the

    fo l lowing

    example.

    The conten t of

    t h i s

    va r i ab l e dete rmines

    the number base in which ca l cu la t ions

    a re

    made.

    I f

    t he

    value

    of

    BASE

    i s

    10,

    a l l

    ca l cu la t ions are made in decimal . Changing

    t h i s value to 16

    ,

    the ca l cu la t ions are

    made in hexadecimal . Y

    ou

    may use however,

    any

    other value for

    ca l cu l a t i ng in any

    other number

    base

    sys t em. Two words are

    def ined to se t a spec i f i c

    number base .

    DECIMAL

    (

    Set

    number

    base

    dec imal .

  • 5/19/2018 FORTH on the Atari Learning by Using

    29/132

    HEX

    Set number base

    hexadecimal.

    n

    example:

    We def ine :

    BIN 2 B SE ;

    TRI 3 B SE ;

    .BIN ( n BIN

    .TRI ( n TRI

    DECIM L ;

    DECIM L ;

    The word

    BIN

    s e t s

    the

    number

    base to two

    and the word

    TRI

    to t h ree . The t r i n a r y

    system uses

    only

    the numbers O l a n d 2

    for

    represen t ing a number.

    The

    words .BIN and

    .TRI

    take

    a decimal value from the s t ack ,

    conver ts t to the b inary or

    t r i n a ry num-

    ber ,

    p r in t s

    t

    and

    swi tches

    back

    in to

    the

    decimal

    number base . Let

    us

    conver t

    some

    numbers.

    120

    .BIN

    1111000

    O

    140 .TRI

    12012

    O

    The word

    .

    B SE fe tches

    the conten t

    of

    BASE,

    dup l ica tes

    t

    and

    p r i n t s

    t in

    decimal .

    The the value

    i s

    res tored in

    BASE.

    : .BASE

    B SE

    @

    DUP DECIM L B SE

    , .

    ,

    I f

    you

    only type B SE

    @ .

    you

    wi l l

    always

    get the r e su l t 10, regard less in which

    number base

    you

    are .

    A cons tan t should s tay a cons tan t . But you

    can change the value of a cons tan t

    with

    the

    ( t i c k word. The br ings the

    address

    of

    a def in i t i on

    on

    the

    s tack .

    , N ME ( - a

    Find

    the address

    of

    N ME

    in vocabulary .

    23

  • 5/19/2018 FORTH on the Atari Learning by Using

    30/132

    Example:

    10

    CONSTANT C OK

    C

    10 OK

    12

    I

    C

    OK

    C

    12 OK

    3-8 COMPARISON.

    The

    comparison

    t akes p lace with

    the

    two

    top

    numbers

    on

    the

    s t ack .

    They

    are

    rep laced by the r e su l t

    of

    the

    comparison.

    The r e su l t

    i s a one

    i f the

    comparison

    was

    t r ue or

    the r e su l t

    i s a zero , i f t h i s

    comparison was not t rue .

    This

    boolean

    f l ag

    i s

    used

    by the

    con t ro l

    words,

    descr ibed

    in

    the next chap te r

    , to con t ro l

    the f lo

    w of a program.

    The

    words used

    for

    comparison

    are :

    (

    nn f)

    f= l ,

    i f n

    grea te r

    than

    n

    =

    (

    nn

    - f )

    f= l ,

    i f n

    equal

    to

    n

    0

    -n

    Retr ieve

    number

    from

    re turn

    s t ack .

    R

    -n

    Copy

    the

    top

    of

    the

    re turn

    s tack

    to

    the

    parameter

    s t ack .

    Same

    as

    1

    Use these words only

    within

    a colon def

    i n i t i on

    and outs ide

    of loops .

    Example:

    We

    def ine

    a

    word

    2SWAP, which exchanges

    the

    two top

    numbers on the s tack with

    the

    th i rd and the four th

    number.

    :

    SWAP nnln2n3-n2n3nnl

    >R ROT ROT R> ROT ROT ;

    The

    eva lua t

    i on of th i s word

    on

    the s tack

    i s shown

    i n F i g

    4-3

    .

    29

  • 5/19/2018 FORTH on the Atari Learning by Using

    36/132

    4

    3

    4

    3

    2

    2

    4

    2

    3

    2

    1

    2

    3

    2

    4

    3

    1

    4

    1

    2

    4

    3

    1

    4

    3

    ) R

    ROT

    ROT

    R

    ROT

    ROT

    Fig

    4-3

    Evalua t ion of

    2SWAP

    4-3

    IF

    ELSE THEN

    ENDIF).

    This

    i s

    the f i r s t word t o con t ro l the

    flow

    of a program.

    I t

    i s used in the form IF

    THEN

    or

    IF ELSE

    THEN. Some

    vers ions of FORTH use

    ENDIF

    ins tead of THEN.

    The de f i n i t i ons :

    IF f)

    THEN

    IF f)

    ELSE

    THEN

    Example:

    The words between IF

    and

    THEN

    ENDIF)

    are

    executed

    i f

    f

    i s

    non

    zero .

    The words between IF

    and ELSE are

    executed

    i f f i s non zero .

    Otherwise the words

    between

    ELSE and

    THEN

    ENDIF)

    are

    executed.

    We

    def ine a word COMP which compares

    the

    two top numbers

    on

    the

    s tack . The

    message

    EQUAL i s pr in t ed i f both numbers are

    equa l . I f no t the message SMALLER

    or

    BIGGER

    i s

    pr in ted .

    The

    de f in i t i on

    of

    the

    word COMP i s shown in

    Fig

    4-4.

    30

  • 5/19/2018 FORTH on the Atari Learning by Using

    37/132

    F i r s t

    2DUP ( n n - n n n n )

    OVER OVER ;

    DROP

    ( nn ' )

    DROP DROP

    NOTEQUAL

    (

    f)

    IF . BIGGER

    ELSE

    .

    SMALLER

    THEN;

    COMP

    ( nn ' )

    DUP

    = IF

    .

    EQUAL

    DROP ELSE

    R

    draws

    a l i ne from

    l e f t to

    we

    def ine

    the

    mode 7

    and

    The

    next

    word

    r igh t .

    SCR

    140

    o

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    AFB GRAPHICS 10/20

    ef

    START

    7 GR. 2 0 0 SETCOLOR ;

    R>L

    ( )

    9

    9

    DO

    I

    10

    2

    PLOT

    LOOP ;

    U>D ( ) 9 10 DO

    8

    I 3

    PLOT

    LOOP ;

    L >R ) 8 8

    DO

    I 8 2

    PLOT -1

    LOOP ;

    D>U ( ) 9 7 8 DO 9 I 3

    PLOT

    -1

    LOOP

    ;

    RECT START R>L U>D L>R

    D>U

    ;

    Fig 5-2 Lines .

    The

    next

    word

    U>D

    draws a l i n e down

    the

    screen

    s t a r t ing a t

    the

    ending

    poin t of

    the previous l i ne . In

    the

    same

    manner

    the

    words

    R>L and D>U are def ined . They always

    s t a r t

    a t

    the end of the former l i ne . The

    l a s t word

    RECT combines

    these words to

    draw

    a

    rec tangle .

    Here

    you can see one of the advantages of

    FORTH.

    Once

    you have def ined a word, you

    can use

    t

    with in

    other words.

    With FORTH

    t

    i s

    also easy to change a program. You

    may def in a new word

    with

    another sequence

    of

    predef ined

    words.

    Try t in

    t h i s

    example.

    Rearrange

    the

    words

    in

    such

    a

    37

  • 5/19/2018 FORTH on the Atari Learning by Using

    44/132

    manner,

    so

    t ha t

    bot tom

    l i n e are

    two s ide l i ne s .

    the top l i n e and the

    drawn

    f i r s t and then the

    In

    t h i s

    example,

    l i ne

    one

    of

    the

    t ex t

    screen #140

    i s

    l e f t blank.

    In

    developing

    t h i s

    sc reen

    the

    words FORGET START

    were

    i n se r t ed . These

    words

    were i n se r t ed a f t e r

    the f i r s t compi la t ion of the sc reen . This

    preven t s

    the

    s tack of

    the

    vocabulary from

    becoming

    too l a rge

    thus

    r ec iev in ig

    the

    message word NOT UNIQUE

    In the

    next program,

    we wi l l use the

    joys t i ck

    to draw l i ne s .

    Suppose

    the red

    but ton

    i s in

    the upper

    l e f t co rne r you

    ge t the e igh t values shown in

    Fig

    5-3 for

    the

    e igh t d i r ec t ions

    of the

    j oys t i ck by

    reading memory

    c e l l

    632.

    4

    4

    3

    Fig

    5-3

    Joys t i ck Cont ro l l e r

    Movement.

    Plug

    in

    a

    j oys t i ck in

    game

    por t one, then

    type

    632 C

    You

    wi l l ge t the values shown in

    Fig

    5-3 .

    The de f in i t i on of the words i s shown in

    t ex t screen

    116.

    We

    use two

    var i ab le s

    X

    an

    Y. The

    word

    X

    increments

    the

    value

    of

    X

    by

    one.

    The word Y i s def ined

    as

    : Y -1 Y

    +

    ;

    38

  • 5/19/2018 FORTH on the Atari Learning by Using

    45/132

    because Y counts

    pos i t i ve

    downward on

    the

    screen.

    The

    word STICK expects a

    value

    on

    top

    of

    the s tack

    which

    i s

    equal

    to

    the conten t

    of

    game por t one. Then t

    decides

    which

    var i ab le has to be incremented or decre

    mented.

    SCR 116

    o

    (AFB

    JOYSTICK 10/18 ef)

    1 0

    VARIABLE X

    0

    VARIABLE Y

    2

    X

    X 1 ; : -X 1 X 1

    3

    Y

    -1 Y 1 ; : -Y 1 Y 1 ;

    4 STICK ( n)

    5 DUP 14 = IF

    Y

    ELSE

    6 DUP 13

    = IF

    -Y ELSE

    7

    DUP

    7

    = IF

    X ELSE

    8 DUP

    11 =

    IF -X ELSE

    9

    DUP

    6

    =

    IF

    X Y

    ELSE

    10

    DUP 5

    =

    IF

    X

    -Y ELSE

    11 DUP 9 = IF -X -Y ELSE

    12 DUP 10

    =

    IF -X Y THEN

    13 THEN THEN THEN

    THEN

    THEN

    14 THEN THEN ;

    15

    SCR 118

    o AFB GRAFICS

    10/18 ef)

    1 START 2 0 0 SETCOLOR 7 GR.

    2

    10

    Y 1

    10

    X 1 ;

    3 NOT ( n-n ) 1 XOR ;

    4 PL ( n n ) X @ Y @ 2 PLOT;

    5

    NPL

    ( nn ) X @ Y @

    0

    PLOT;

    6 ?STICK 632 C DUP 15

    =

    NOT

    7 IF STICK PL THEN DROP

    8 PJOY START PL BEGIN

    9 ?STICK ?TERMINAL UNTIL

    10 0

    GR. ;

    11

    Fig 5-4

    Cont ro l l ing the

    j oys t i ck .

    39

  • 5/19/2018 FORTH on the Atari Learning by Using

    46/132

    The program cont inues in screen 118. The

    word START se t s

    the

    gra f i c mode,

    the

    background colors

    and

    the

    s t a r t i ng

    values

    for

    X and Y. The word NOT i s

    used

    to

    change

    the

    r e su l t

    of

    a

    comparison.

    I f

    a

    comparison

    i s f u l f i l l e d a one i s on the

    s tack .

    NOT changes th i s value to zero . I f

    a

    zero

    i s on the

    s t ack

    NOT

    changes t

    to

    one. With t h i s word the Exclusive

    OR

    funct ion i s used. This funct ion i s shown

    in

    Fig

    5-5 for one b i t . FORTH

    app l ie s t h i s

    funct ion

    b i t

    by

    b i t

    on

    the

    two

    top

    numbers

    of

    the s tack .

    Bi t l

    o

    o

    Bit2

    o

    o

    XOR

    1

    o

    o

    Fig 5-5 Exclusive Or

    The other

    l og ica l funct ions

    AND

    and

    OR are

    a l so

    implemented.The

    def in i t i ons are :

    AND

    nnl-n2 log ica l AND

    b i twise

    OR

    nnl-n2 log ica l OR

    b i twise

    XOR

    nnl-n2

    log ica l

    XOR

    bi twise

    Now we cont inue with the program. The word

    PL p lo t s a poin t a t X

    and

    Y in co lor two.

    The word ?STICK

    determines

    i f the j oys t i ck

    i s moved in one of the e igh t d i r ec t ions .

    Then a poin t i s p lo t t ed . The word PJOY

    combines a l l these

    words

    to

    p l o t l i n e s on

    the TV screen .

    The word NPL p lo t s the po in t in the color

    of the background.

    This

    erases a po in t on

    the TV screen .

    We

    can i n se r t t h i s word in

    ?STICK to

    move

    a poin t ac ross the TV

    screen .

    40

    ?STICK

    632

    C

    DUP

    15

    =

    NOT

    IF NPL STICK PL THEN DROP

  • 5/19/2018 FORTH on the Atari Learning by Using

    47/132

    5-2 PATTERN.

    The

    program in

    Fig

    5-6

    c rea t e s

    a random

    pa t t e rn . I t uses a

    random

    number genera to r .

    RND

    expects

    a

    number

    n

    on

    the

    s t ack .

    A

    random

    number

    between 0 and n- l i s

    genera ted .

    As a s t a r t i ng value for the

    random numbers, we use the conten t of

    memory

    loca t ion 53770.

    In

    t h i s memory

    loca t ion the re

    are c rea ted

    random numbers

    ins ide the ATARI.

    SCR

    117

    o AFB

    RANDOM

    PATTERN ef)

    1 (RANDOM

    GENERATOR

    )

    2

    0 VARIABLE RND

    53770

    @ RND

    3

    RANDOM RND

    @

    31421

    *

    6972

    +

    4 DUP RND ;

    5 RND (

    n-n )

    RANDOM U* SWAP

    6 DROP;

    7 RNDP 7 GR. 2 0 0 SETCOLOR

    8 BEGIN 160

    RND

    80

    RND

    9 16 RND PLOT

    10

    ?TERMINAL UNTIL ;

    11

    12

    13

    14

    15

    Fig

    5-6

    Random

    Pa t t e rn .

    The

    word

    RNDP se t s

    the

    gra f i c mode 7 and

    the background co lo r .

    In

    a loop,

    terminated by one of the yel low keys,

    t

    c rea tes random

    numbers

    for the X Y

    coord ina tes

    and

    the

    co lor .

    The r e s u l t i s

    displayed

    on

    the

    TV

    screen

    .

    41

  • 5/19/2018 FORTH on the Atari Learning by Using

    48/132

    5-3

    SOUND

    AND COLOR

    The word

    SOUND

    uses four

    parameters

    on the

    s t ack .

    SOUND nnln2n3)

    n (

    0-15

    )

    i s the

    volume, n l

    a

    d i s to r t i on ,

    n2 the

    frequency and

    n4

    the channel .

    For

    crea t ing

    severa l

    sounds

    and

    noises ,

    we

    use the random

    number

    genera tor and

    a

    wait

    loop. The

    word WAIT reques t s

    one number

    on the s tack . The higher

    the

    number, the

    longer

    the

    delay. The

    t ex t

    screens

    81

    to

    85

    in

    Fig 5-7 conta in

    some

    sound

    words,

    such as the word THUNDER in screen 81.

    Using

    random

    pi t ch

    and

    volume,

    a

    sound

    l ike

    a

    swarm

    of

    f l i e s

    i s generated .

    ENG

    uses

    in te rmi t t en t sound

    for

    noise tha t

    sounds

    l i ke

    an

    engine.

    Fig 5-7 Sound.

    42

    SCR

    081

    o

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    AFB SOUND

    WAIT

    0

    DO

    C (

    n-n

    I

    SWAP /

    10/20 ef

    LOOP

    ;

    11 RND

    5 + 10

    OFF 0 0 0 0 SOUND

    o 0 0 1

    SOUND

    ;

    T

    100

    5

    DO

    I C

    8

    I 0

    SOUND

    I C

    8

    I 20 + 1

    SOUND

    DUP WAIT

    5

    RND +LOOP OFF

    ;

    THUNDER

    300 T

    -->

  • 5/19/2018 FORTH on the Atari Learning by Using

    49/132

    SCR

    082

    o

    AFB SOUND cntd

    ef

    1 FLY)

    2

    PI

    ( n) 7

    RND

    250

    +

    3 V ( n) 4

    RND

    6 +

    4 F V

    14

    PI 0 SOUND

    5

    FLY

    BEGIN F 500

    WAIT

    6 ?TERMINAL UNTIL

    OFF

    7

    ENGINE

    8

    ENG

    BEGIN 10 10

    250

    0

    SOUND

    9

    1500

    WAIT

    OFF

    ?TERMINAL

    10 UNTIL

    11

    12

    13

    14

    15 -->

    SCR

    083

    o

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    AFB

    SOUND

    cntd

    ef

    INCR ( n) 10 / 1

    DO

    I 10 60 0 SOUND

    LOOP ;

    DECR

    ( n) 2 10 / DUP 1

    SWAP DO DUP

    10 60 0

    SOUND

    -1

    +LOOP

    DROP ;

    T

    100

    a

    DO

    I INCR 2

    +LOOP

    TT

    0

    100 DO

    I DECR -2

    +LOOP

    CC

    0 100 DO I INCR I DECR

    -2 +LOOP

    -->

    43

  • 5/19/2018 FORTH on the Atari Learning by Using

    50/132

    SCR 084

    o

    AFB

    SOUND cntd ef)

    1 15 VARIABLE VI

    15

    VARIABLE

    V2

    2 15 VARIABLE V3

    3 ST 15 VI 1 15 V2 1 15 V3 1 ;

    4 : SS VI @ 8

    20

    0 SOUND

    5 V2 @ 8 40 2 SOUND

    6 V3 @ 8 70 2 SOUND

    7

    8 DEC

    -1

    VI 1

    -1

    V2 1

    9 -1

    V3

    1

    ;

    10

    EXPL ST BEGIN SS DEC

    11 1000 WAIT V3 @ 0< UNTIL

    12

    13

    14

    15

    SCR 085

    a

    AFB SOUND

    cntd

    ef)

    1 SI 15 a DO I 10 60 I 2 -

    2 a SOUND 100 WAIT

    LOOP

    ;

    3

    4

    SIREN

    BEGIN

    SI

    OFF

    50

    WAIT

    5 ?TERMINAL UNTIL ;

    6

    7

    8

    DWN

    200 100 DO 8 10 I

    a SOUND

    9 100

    WAIT LOOP ;

    10 UP 100 200

    DO

    8 10 I 0 SOUND

    11 100 WAIT -1 +LOOP ;

    12

    EUSI

    10

    0

    DO

    UP

    DWN

    LOOP

    13

    OFF;

    14

    15

    The word CC in sc reen 83 s imula tes a

    dropped co in These examples are from the

    book

    ATARI BASIC

    Learning

    by

    Using

    by

    Tomas E. Rowley .

    44

  • 5/19/2018 FORTH on the Atari Learning by Using

    51/132

    Some more

    sound words are

    in the next

    screens

    EXPL s imula tes an

    explosion

    by

    changing

    the

    volume and

    the

    pi tch

    on

    three

    channels . In the

    l a s t

    screen

    we have

    the

    word

    SIREN

    for s imulat ing

    an

    american

    pol ice s i r en and the word EUSI for an

    european pol ice

    s i r en

    The

    bes t

    way

    to crea te

    sound

    e f fec t s i s

    the methode of t r i a l

    an e r ro r

    Define new

    words and combine them in

    d i f f e ren t

    ways.

    SCR

    # 086

    0 ( AFB

    COLOR

    10/22

    ef

    1

    CF 712

    C@

    710

    C@

    712

    C

    2

    709

    C@

    710 C 709

    C

    3

    4 CCF 100 0

    DO

    CF

    100

    WAIT

    5

    LOOP 0

    GR.

    ;

    6

    7

    BG

    254 0 DO I 712 C

    500

    WAIT

    8 2 +LOOP ;

    9

    10

    FG 254

    0

    DO

    I

    710 C

    500 ~ v I T

    11 2

    +LOOP

    12

    SCR

    # 087

    o

    AFB

    COLOR cntd ef

    1 DI

    16

    0

    DO

    I 709

    C

    100 WAIT

    2

    LOOP;

    3 AR 0

    14 DO

    I

    709 C

    100 WAIT

    4 -1

    +LOOP

    ;

    5

    6 CURS (

    nnl

    85 84 C

    7 CLR 125 EMIT ;

    8 DIS CLR

    10

    5 CURS

    9 222 710

    C

    . HELLO

    10

    1000 WAIT DI KEY AR ;

    11

    12

    Fig

    5-8

    Color.

    45

  • 5/19/2018 FORTH on the Atari Learning by Using

    52/132

    In

    the

    t ex t screens 86 and 87 ( Fig

    5-8

    ,

    the

    s to re

    and

    fe tch

    words

    are

    used

    to

    change

    the

    con ten t s of

    the

    co lo r r eg i s t e r s .

    The word CF ro t a t e s the conten ts of the

    color

    r eg i s t e r s .

    This

    causes

    a

    quick

    change

    of

    foreground and

    background co lors .

    Use t h i s word in a new d e f in i t i o n of

    THUNDER

    BG

    changes the background

    color

    and

    FG changes the foreground color

    In

    the

    next screen, the

    t ex t

    HELLO

    disappears

    and,

    a f t e r

    press ing

    a key,

    t

    reappears .

    The

    word

    DIS

    uses

    two

    other

    words CLR and CURS.

    The word

    CURS reques ts

    two

    numbers on the

    s tack . N

    i s the

    hor izonta l row and

    n l

    the v e r t i c a l column.

    By sending out an end of

    f i l e

    charac te r (

    155 EMIT ) the cursor i s placed a t the

    spec i f i ed

    pos i t i on .

    The word CLR

    c l ea r s

    the TV

    sc reen

    and pos i t ions

    the

    cursor

    in

    the

    upper

    l e f t

    corner .

    5-4

    H E X U M ~

    Most

    of the FORTH vers ions have def ined

    the

    word DUMP

    as

    t h a t

    which dumps

    the

    conten t

    of

    memory

    loca t Ions

    on

    the

    screen .

    The program

    in

    Fig

    5-9

    (

    screens

    88

    and

    89

    ) i s a s imi la r program. The word

    DDUMP

    reques ts

    two

    numbers on the s t ack . N i s

    the

    s t a r t i n g

    address

    and nl

    the

    number

    of

    l i nes ,

    e igh t

    bytes each.

    SCR

    088

    0

    ( AFB

    HEXDUMP

    10/22

    ef

    1

    HEX

    2

    LNE ( n DUP DUP 8 SWAP

    3

    DO

    I

    C

    3

    .R LOOP

    .

    4 NR

    CR CR

    5 SPACES 8 0

    5

    DO

    I 3

    .R LOOP CR

    6

    DOT

    DROP 2E ;

    46

  • 5/19/2018 FORTH on the Atari Learning by Using

    53/132

    7

    8 ATARI (

    n-n

    DUP

    20

    IF

    DOT THEN

    13

    THEN THEN

    THEN THEN

    THEN

    ;

    14

    15 -->

    SCR

    089

    o AFB

    HEXDUMP cntd

    ef

    1 ASCII ( a-a DUP DUP 8

    2 SWAP DO I

    C

    ATARI

    EMIT

    3 2 SPACES LOOP ;

    4

    5

    6

    7

    8 HDUMP ( n-n ) CR

    DUP

    SPACE

    9 LNE

    CR

    7 SPACES ASCII 8

    ;

    10

    DDUMP (

    an

    HEX

    11

    NR

    0

    DO

    HDUMP

    LOOP DROP

    12

    DECIMAL

    13

    DECIMAL

    14

    15

    Fig

    5-9 Hexdump.

    An example of

    the

    pr in tou t i s shown in Fig

    5-10.

    47

  • 5/19/2018 FORTH on the Atari Learning by Using

    54/132

    HEX

    3008

    9 DDUMP

    0 1

    2 3

    4

    5 6

    7

    3008

    86

    4B

    45

    59

    4C

    49

    D4 EA

    f

    K

    E Y L

    I T

    j

    3010

    2F D9

    C 60

    D F3 2F 7C

    y

    s

    I

    3018

    C 60

    D El

    8 lC

    C 3D

    a

    =

    3020 C

    FA

    8 F3 2F

    70 D 9C

    z

    s

    P

    3028

    B

    9C

    B

    91

    C

    9

    F 59

    q

    Y

    3030 8 4

    0

    22

    B

    FA 8 7A

    z z

    3038

    8 E4 FF F B

    31 0 87

    d

    1 g

    3040

    43 4F

    4E 54 52

    4F

    CC 8

    C

    0

    N

    T R

    0

    L

    3048

    30

    SF 11

    13

    20 86

    C

    46

    0 f

    F OK

    Fig 5-10

    Hexdump

    Pr in tou t .

    The word ATARI changes

    a l l unwanted

    charac te r s

    for example

    the

    byte 9B which

    erases the screen

    to

    do ts .

    This word i s

    used with in the

    word

    ASCII, which

    p r in t s

    the

    ASCII

    charac te r s .

    LNE pr in t s one l i n e

    of hex

    bytes

    while NR does

    the

    numbering

    on top of the pr in tou t .

    Next

    we

    look

    a t some mathematical

    examples.

    5-5 LARGEST

    COMMON

    DIVISOR

    We

    use the

    a lgor i thm

    of

    ca l cu l a t e

    the

    l a rges t

    common

    two numbers A

    and B. F i r s t the

    48

    EUCLID

    to

    d iv i so r

    of

    remainder

  • 5/19/2018 FORTH on the Atari Learning by Using

    55/132

    of

    lB

    i s

    determined by

    A B MOD

    I f

    the

    remainder

    R

    i s zero

    B

    i s

    the l a rge s t

    common

    d iv i so r . I f

    R i s not zero A i s s e t

    to

    Band

    B

    i s s e t

    to

    R

    and

    the

    modulo

    d iv i s ion

    i s repea ted un t i l

    R i s zero . The

    def in i t i on of

    LCD

    i s shown in

    Fig

    5-11.

    0

    AFB

    MATH

    EXAMPLES

    ef

    1

    2

    LCD

    BEGIN SWAP OVER

    MOD

    DUP

    3

    0= UNTIL

    DROP

    ;

    4

    27

    21

    LCD

    3

    OK

    Fig

    5-11

    Larges t

    Common

    Divisor .

    STACK

    27

    27

    2

    2 27

    2

    2

    6

    2

    6

    2

    6

    6

    2

    6

    6

    3

    6 3

    6

    3

    3

    6

    3

    3

    0

    3

    0

    3

    TOS

    27

    2

    2

    27

    2

    6

    6

    0

    6

    2

    6

    3

    3

    0

    3

    6

    3

    0

    0

    1

    0

    3

    INPUT

    27

    2

    BEGIN

    SW P

    OVER

    MOD

    DUP

    0=

    UNTIL

    SW P

    OVER

    MOD

    DUP

    0=

    UNTIL

    SWAP

    OVER

    MOD

    DUP

    0=

    UNTIL

    DROP

    Fig

    5-12

    Larges t

    Common

    Divisor

    Evalua t ion on the s tack

    49

  • 5/19/2018 FORTH on the Atari Learning by Using

    56/132

    For a b e t t e r unders tanding of t h i s

    d e f in i t i o n

    the

    evalua t ion on

    the

    s tack

    for the

    given

    example i s

    shown

    in Fig 5-12.

    5-6

    FIBBOCONACCI

    NUMBERS

    Fibboconacci numbers are a

    se r i e s

    of

    numbers. The

    next

    element of t h i s se r i e s

    i s always

    the

    sum of the

    two predecessors .

    The se r i e s

    s t a r t s

    with

    zero

    and

    one.

    The

    word

    FIB

    in Fig 5-13

    c rea t e s these

    numbers.

    This i s

    an example for

    the BEGIN

    WHILE

    REPEAT loop. FIB

    expects

    on the

    s tack one

    number

    which determines the

    end

    of the

    s e r i e s .

    I f the ca lcu la t ed

    element i s

    l a rge r than t h i s

    number,

    the ca l cu la t ion

    s tops .

    SCR 099

    a

    1

    2

    3

    4

    5

    6

    AFB

    MATH

    EXAMPLES

    cndt

    ef

    FIB (

    n

    CR

    a

    1 BEGIN DUP

    >R

    ROT

    DUP

    R>

    > WHILE

    ROT ROT

    DUP

    ROT +

    DUP

    REPEAT DROP DROP DROP

    100 FIB

    1 2 3 5 8 13 21 34 55 89 144 OK

    Fig

    5-13 FIBBOCONACCI Numbers. (

    The

    evalua t ion on

    the

    s tack for

    the

    f i r s t

    t h ree loops i s

    shown

    in

    Fig 5-14.

    50

  • 5/19/2018 FORTH on the Atari Learning by Using

    57/132

    20

    20

    20

    2

    1

    2 1

    2 1

    1

    2 1

    1

    20

    1

    20

    20

    1 2

    2

    1

    1

    2

    1

    1

    20

    1

    20

    2

    1

    2

    1

    1

    2

    1

    1

    2

    1 1

    2

    1 1

    1

    20

    1

    1

    2 1

    1

    20

    1 1

    1

    2

    1

    1

    1

    1 20

    1 1 2

    20

    1 1

    2

    20

    1

    1

    1

    20

    1

    1

    1

    20

    1

    20

    1

    20

    1 1

    20

    1 1

    1

    20 1

    1

    1

    2

    1

    2

    2

    1

    2

    2

    2

    1 2

    20

    1 2

    20

    1 2 2

    Fig 5 14 FIBBOCON CCI

    Numbers.

    Evaluation

    on

    the

    stack.

    2

    FIB

    o

    BEGIN

    UP

    R

    R T

    UP

    R

    WHILE

    R T

    R T

    UP

    R T

    +

    UP

    REPE T

    UP

    R

    R T

    UP

    R

    >

    WHILE

    R T

    R T

    UP

    R T

    +

    UP

    REPE T

    UP

    5

  • 5/19/2018 FORTH on the Atari Learning by Using

    58/132

    5-7 PRIME NUMBERS.

    In the

    next

    example, we ca lcu la t e the

    prime numbers between two

    l imi t s .

    The

    algor i thm

    used

    i s very

    s imple .

    The word

    PTEST t e s t s

    within

    a

    loop,

    i a

    number

    i s

    d iv i s ib l e by

    the loop

    index.

    The loop

    s t a r t s with two

    and ends

    a t ha l f

    of

    the

    number. In t h i s program the

    predef ined

    word LEAVE i s

    used.

    This word t e rmina tes

    the execut ion

    of

    a loop.

    The

    program

    cont inues

    a f t e r

    the

    next LOOP

    word.

    LEAVE

    Terminates a

    loop

    I f TEST f inds a remainder equal

    to zero ,

    a

    zero i s

    placed

    on the

    s tack

    - and the loop

    i s

    l e f t .

    The

    program then

    cont inues

    with

    the

    word

    DUP.

    With

    a

    zero

    on

    the s tack

    the

    word . PRIM i s

    not

    executed

    and

    the top of

    s tack

    i s discarded . To format the output

    a

    var iab le

    ROW i s used.

    The program

    i s

    shown

    in

    Fig 5-15.

    52

    SCR

    100

    o

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    AFB

    MATH

    EXAMPLES cnd t

    ef)

    4 VARIABLE

    ROW

    TEST ( n-f) MO 0= ,

    .PRIM ( n-n) DUP 4

    .R ROW @

    DUP 0= IF

    CR DROP

    4 ELSE

    1 - THEN #ROW ;

    PTEST

    n ) DUP

    2 / 2

    DO

    DUP

    I TEST

    IF

    0

    LEAVE

    THEN

    LOOP

    DUP IF .PRIM ELSE DROP

    THEN

    DROP ;

    PRIM (

    nn' ) CR

    4

    ROW

    DO I PTEST LOOP CR

  • 5/19/2018 FORTH on the Atari Learning by Using

    59/132

    200 1

    PRIM

    1 3

    5

    7

    11

    13 17 19 23

    29

    31

    37 41 43

    47

    53 59

    61

    67

    71

    73

    79 83

    89

    97

    101

    103 107 109

    113

    127

    131

    137 139

    149

    151

    157

    163

    167 173

    179

    181 191

    193

    197

    199

    OK

    Fig

    5-15

    Prime Numbers.

    5-8 HORE

    SOUND

    AND

    GRAFICS.

    I found some new sounds

    in the

    September

    i ssue

    of

    COMPUTE

    magazine.

    I

    t r ns l t ed

    these examples from BASIC to FORTH. These

    new sound

    words re shown

    in Fig 5-16.

    There

    i s

    another thunder

    which

    you can

    combine with

    the

    old thunder and r in to

    cre t e stormy weather .

    SCR 094

    0

    MORE

    SOUND 10/26

    ef

    1

    B

    (

    -n

    255

    RND#

    50

    +

    2

    X

    ( -n

    200 RND#

    ;

    3

    Tl

    (

    )

    B 1 DO

    15

    8

    I 0

    SOUND

    4

    LOOP ;

    5 T2

    ( )

    X 1 DO LOOP ;

    6

    TH

    )

    2 0

    DO

    Tl OFF

    7

    T2 OFF

    LOOP

    ;

    8

    STO

    TH

    TH THUNDER

    TH ;

    9

    Rl

    n)

    o 0 2 SOUND ;

    10

    RAIN

    150

    0 DO I 10 / Rl

    11

    100 WAIT 2 +LOOP BEGIN

    12

    ?TERMINAL UNTIL ;

    13

    14

    15

    53

  • 5/19/2018 FORTH on the Atari Learning by Using

    60/132

    o MORE SOUND

    cntd

    ef)

    1 :

    HB

    10 1

    DO

    15 3

    12

    0 SOUND

    2

    1000

    WAIT OFF 6000

    WAIT

    LOOP

    3

    4

    -1 VARIABLE XX

    5 LOI 40

    150

    DO I 10 / 0 15 0

    6 SOUND XX @ LOOP

    7 LO SO lDO LOI XX @

    -7

    >

    IF

    8

    -1 XX 1 THEN LOOP

    9 LS

    10

    10

    40

    1 SOUND

    10

    8 10

    10

    2 SOUND

    11

    10

    10 90

    3

    SOUND

    12

    STE

    2

    0 DO LS

    4000

    WAIT

    XSND

    13 1000 WAIT LOOP

    14 STEAM -1 XX 1 LO STE LO

    15

    XSND

    Fig 5-16

    More

    sound.

    The

    program

    in

    Fig

    5-17

    produces

    the

    gra f i c

    shown in p ic tu re 5-18. The l i nes

    are

    drawn in gra f i c mode

    e igh t

    Draw t h i s

    p ic tu re as

    t

    i s and then a second t ime

    with the background co lo r I t appears and

    d isappear s

    o

    MORE

    GRAFICS 10/26

    ef)

    1 0

    VARIABLE X

    0

    VARIABLE Y

    2

    CG

    8 GR 0 0 2 SETCOLOR

    3 SG 260 X

    10

    Y

    4

    PG

    160 80 1 PLOT

    5 -X -10 X

    1

    6

    X 10 X 1

    7 -Y -10 Y 1

    8

    Y

    10

    Y

    1

    9 LG X @ Y @ 1 DRAWTO

    10

    LL

    20

    0 DO PG LG

    -X

    LOOP

    11 DD

    15 0

    DO PG

    LG Y

    LOOP

    12 RR 20 0 DO PG LG X LOOP

    13 UU

    15 0 DO PG LG -Y LOOP

    14 PIC

    CG SG LL DD RR UU

    15

    Fig 5-17 Rayshaped pa t t e rn

    54

  • 5/19/2018 FORTH on the Atari Learning by Using

    61/132

    Fig

    5 18

    Pic tu re of the

    rayshaped

    pa t t e rn .

    5 9

    USING THE

    G ME PORTS FOR CONTROL

    APPLICATIONS .

    The ATARI uses

    two

    6520 Per iphera l

    In te r face Adapters P IA

    )

    for the

    game

    por t s . These

    por t s can

    be

    used

    to

    t ransmi t

    or

    to recieve data . The

    6520

    provides two

    b i -d i r ec t i ona l

    por t s

    , A and i two cont ro l

    r eg i s t e r s

    and

    four

    in te r rup t

    l i ne s .

    In

    our

    examples we only use the por t s and the

    con t ro l r eg i s t e r s

    . The four r eg i s t e r s have

    the

    fo l lowing

    addresses :

    PORT

    =

    D300

    PORTB

    =

    D301

    P CTL

    =

    D302

    and PBCTL

    =

    D303

    55

  • 5/19/2018 FORTH on the Atari Learning by Using

    62/132

    The data d i rec t ion for the

    two

    por t s i s

    se t by two

    data

    di rec t ion r eg i s t e r s DDRA

    and DDRB. Both,

    the

    por ts and

    the

    data d i r -

    ec t ion

    r eg i s t e r s

    have the same

    addresses .

    Bit two of

    the

    cont ro l r eg i s t e r determines

    which

    one of the

    two r eg i s t e r s

    i s

    accessed.

    I f

    b i t two of

    the

    cont ro l r eg i s t e r PACTL

    i s

    one,

    PORTA ac t s as a

    por t .

    I f b i t two

    i s zero,

    PORTA

    ac t s as a data

    d i r ec t ion

    r eg i s t e r .

    A l i ne

    of

    a

    por t

    i s

    s e t

    to

    an

    output

    i f

    the

    corresponding

    b i t

    in the

    data d i rec t ion

    reg i s t e r

    i s

    se t to

    one.

    Respect iv ly a zero marks a l i ne as inpu t .

    The

    word

    INIT in Fig 5-19 se t s the four

    lower b i t s

    of

    por t A as outpu ts .

    These

    four l i nes

    are avai lable

    a t

    game por t

    1 .

    SCR

    #

    104

    o

    AFB

    PORT CONTROL 10/16

    ef)

    1 HEX D300 CONSTANT

    PORTA

    2

    D30l

    CONSTANT PORTB

    3 D302 CONSTANT PACTL

    4 D303 CONSTANT PBCTL

    5

    INIT

    30

    PACTL

    C

    6 OF PORTA C

    7

    34

    PACTL C

    8 00 PORTA C

    9

    10 DECIMAL

    11

    12

    PAl (

    b)

    PORTA

    C ;

    13 WAIT ( n) 0 DO

    LOOP

    14

    15

    Fig 5-19

    I n i t i a l i s a t i on

    of the por t s .

    We

    use

    the

    c i r cu i t

    shown

    in

    Fig

    5-20

    to

    demonstra te the output of data .

    56

  • 5/19/2018 FORTH on the Atari Learning by Using

    63/132

    0 0

    6

    89

    GAME

    CONNECTOR

    I

    I

    INSIDE ATARI

    22 I

    ~

    001

    l

    I

    I

    I

    1

    =

    PAD

    2

    =

    PA

    3

    =

    PA2

    4 = PA3

    7 =GND

    8 =

    vee

    REPEAT 4 TIMES

    NPN

    TRANSISTOR

    VCC

    = 5V

    Fig 5 20

    Schematic of

    the c i r c u i t

    Fig

    5 21

    The exper imenter board .

    57

  • 5/19/2018 FORTH on the Atari Learning by Using

    64/132

    Fig

    5-21

    The

    experimenter

    board.

    Screen 105 in Fig 5- 22 conta ins th ree

    programs.

    RL s imula tes

    a

    running l i gh t

    The

    four

    LED s

    turn on and

    of f

    one a f t e r the

    other

    The

    sequence

    s t a r t s

    with

    one

    on

    the

    s t ack

    This tu rns

    on

    LE one.

    The

    second LE i s

    turned on by mult iplying the top of the

    s tack

    with

    two. With an addi t ional

    mul t ip l i ca t ion the th i rd LE i s switched

    on.

    The l a s t

    LE

    i s turned on with the

    number

    eight

    on

    the s t ack I f

    the number

    on

    top

    of

    the

    s tack

    i s

    equal to 16 the

    sequence r e s t a r t s

    with

    one.

    58

  • 5/19/2018 FORTH on the Atari Learning by Using

    65/132

    SCR

    105

    o FB

    PORT

    CONTROL cn td ef

    1 RL 1 BEGIN DUP P L 2

    DUP

    2

    16

    =

    IF

    DROP

    1

    THEN

    2000

    WAIT

    3

    ?TERMINAL UNTIL

    4

    5

    LB

    0 BEGIN DUP

    P L

    2 1 +

    6 DUP 31 = IF DROP a THEN

    7

    1000

    WAIT ?TERMINAL UNTIL

    8

    9

    OINIT

    INIT

    0 DUP

    P L

    10 NEW 1 SW P DUP 1

    =

    IF

    DROP

    11 ELSE 1 DO 2 LOOP THEN

    12

    ON

    ( n) NEW

    OR

    DUP

    P L

    13 OFF (

    n)

    NEW

    XOR

    DUP

    14

    P L

    15

    Fig

    5-22

    Running

    l i g h t ,

    l i g h t b a r

    and

    ON

    OFF

    LB s imu la te s a l i g h t b a r . The

    LED s

    are

    tu rned of f u n t i l

    a l l

    four LED s

    are

    The

    sequence s t a r t s with one.

    mul t ip ly ing with

    two

    and -

    adding one

    we

    t h r ee .

    This

    value

    tu rns

    on

    LED

    one

    and

    not

    on.

    By

    ge t

    two.

    n add i t i ona l m u l t i p l i c a t i o n

    and

    add i t i on

    l eaves t he value seven on top of the s t ack .

    This

    t u r n s

    on

    LED

    one, two and t h r ee .

    After

    a l l

    LED s a re tu rned on a zero on

    top of the s tack

    tu rns

    a l l

    o f f .

    Both

    programs

    use

    t he

    WAIT

    loop .

    The

    words def ined in sc reen 105, l i n e 9

    th ru 14, can be used to tu rn on or

    of f a

    p a r t i cu l a r LED.

    1 ON t u rn s on

    LED one,

    3

    ON

    t u r n s on

    LED

    t h r ee

    and

    1 OFF

    t u r n s

    o f f

    LED

    one.

    59

  • 5/19/2018 FORTH on the Atari Learning by Using

    66/132

    The word OINIT i n i t i a l i s e s p o r t A, p laces

    a zero

    on t op of

    the

    s tack and t u rns

    of f

    a l l

    LED s.

    The

    word

    NEW

    de te rmines which

    LED has

    to be t u rned

    on.

    The

    s t a t e

    of

    the

    four

    LED s

    i s

    s to red

    in

    the

    four

    lower

    b i t s

    of

    a byte

    on

    top of the s t ack . A

    one

    markes the corresponding LED as turned on,

    a

    zero

    marks

    t as turned

    of f . Using

    the

    l og ica l OR

    func t ion the new

    LED

    i s added.

    In the

    same

    way a LED i s turned of f using

    the l og ica l XOR

    func t ion .

    The app l i ca t i on

    in

    Fig

    5-23

    s imula t es a

    dice .

    Seven LED s are grouped

    to form

    the

    spo t s of a dice see Fig 5-25 . The word

    DINIT

    i n i t i a l i s e s por t A.

    Seven

    l i ne s

    are

    used as outpu t , one l i n e as an inpu t .

    A

    pushbut ton i s connected to

    t h i s

    l i n e see

    Fig 5-24) The word PA

    f e t ches

    the

    con ten t

    of

    por t

    A

    and

    masks

    out

    the

    seven

    low

    order

    b i t s . 128 AND). With

    128 XOR

    b i t e igh t i s inver ted . The number 128 i s

    read i f t he but ton

    i s

    pressed and a

    zero

    i s

    read

    i f t i s not pressed . The next

    words ID th ru 6D

    r e l a t e

    the

    decimal

    numbers

    1 t h ru 6

    to

    the s ix

    spo t s of

    the

    dice .

    These

    words

    use

    a

    binary

    p a t t e rn .

    For

    i n s t ance

    0001000

    tu rns

    on

    the LED in

    the middle . This i s equal to a

    throw

    of

    one.

    OD

    i s used as a dummy

    d e f i n i t i o n

    in

    the CASE: s ta tement CDICE.

    60

  • 5/19/2018 FORTH on the Atari Learning by Using

    67/132

    SCR #

    106

    o

    (AFB

    PORI CONI ROL cntd

    ef)

    1

    HEX

    2 DINIT

    30

    PACl L

    C

    3

    7F PORI A

    C

    4

    34 PACl L

    C

    5

    00

    PORI A

    C

    6

    DECIMAL

    7

    PA

    (

    -b

    roRI'A C 128

    ND

    8 128

    XOR

    ; 2

    BASE

    9 ID

    0001000

    PAL

    ;

    10

    2D

    1000001 PAL

    11

    3D

    0101010

    PAL

    ;

    12

    4D

    1010101 PAL ;

    13 5D 1011101

    PAL

    14 6D 1110111

    PAl

    DECIMAL

    15 OD

    ; -

    SCR

    #

    107

    o (AFB roRI CONI ROL cntd

    ef)

    1 CASE:

    CDICE

    OD

    ID 2D 3D

    2

    4D 5D

    6D

    ;

    3

    BU ITON

    1 BffiIN

    DROP

    PA@ DUP

    4

    UNTIL

    500 WAIT

    5 BffiIN

    WHITLE PA

    6 REPEAT;

    7 DICE 1 BffiIN

    1 + DUP

    7

    =

    8

    IF

    DROP

    1

    THEN DUP

    CDICE

    9 PA UNTIL

    500

    WAIT

    1

    10

    BffiIN

    WHILE

    PA

    REPEAT

    11 DROP;

    12

    DODI

    BffiIN BUTTON DICE

    13 1000

    WAIT

    ?TERMlNAL

    UNTIL

    ;

    14

    15

    Fig

    5-23

    S imu l a t i ng

    a

    d i c e

    61

  • 5/19/2018 FORTH on the Atari Learning by Using

    68/132

    The word

    BUTTON

    wai t s in the BEGIN . UNTIL

    loop un t i l

    the but ton

    i s

    pressed.

    Then

    a delay loop

    fol lows to

    suppress the

    bouncing

    of

    the key. The BEGIN

    WHILE

    UNTIL

    loop

    i s

    l e f t

    on

    re leas ing

    the but ton .

    The

    word

    DICE

    s t a r t s

    the count . Press ing

    the

    but ton

    s tops it and the thrown number

    i s shown

    on

    the disp lay .

    62

    . . ; t l ) C D I '

  • 5/19/2018 FORTH on the Atari Learning by Using

    69/132

    Fig

    5 25

    The

    dice

    63

  • 5/19/2018 FORTH on the Atari Learning by Using

    70/132

    Text and strings

    6.

    TEXT ND STRINGS.

    In

    the

    s tandard vers ion

    of FORTH, the re

    are no s t r ings or s t r i ng funct ions

    implemented.

    Yet

    t

    i s

    very

    easy

    to

    en ter

    t ex t or to pr in t out

    t ex t .

    6-1 INPUT/OUTPUT OF TEXT.

    The word EXPECT expects

    the input of

    cha rac te r s .

    The def in i t ion

    i s :

    EXPECT

    a n ) Expects n charac te r s

    For

    the

    specia l

    address

    the

    top

    of t ex t a t address

    a .

    Also

    te rminated by CR.

    input of

    t ex t you can

    use a

    area in memory

    ca l l ed

    PAD. The

    of

    P D

    i s

    always

    68 bytes

    beyond

    of

    the vocabulary s t ack .

    P D

    - a )

    Address of

    P D

    i s put

    on

    the s tack .

    Example:

    P D 15 EXPECT RET THIS

    IS

    FORTH RET

    6

  • 5/19/2018 FORTH on the Atari Learning by Using

    71/132

    The t ex t THIS IS FORTH i s s tored in

    To

    get the

    momentary

    address

    of PAD

    PAD and t ry to ge t the t ex t out onto

    screen with DUMP

    or

    DDUMP You

    can

    use the

    predef ined

    word

    TYPE.

    PAD.

    type

    the

    also

    TYPE a n )

    Pr in t s

    n charac te r s

    of

    t ex t , s t a r t i ng a t

    address a .

    I f we

    have s to red

    t ex t as in

    the example

    above

    and

    made

    no

    new

    de f in i t i ons ,

    which

    would a l t e r the address of the top of the

    vocabulary s tack , we

    can

    get the t ex t from

    PAD with:

    PAD

    15 TYPE

    RET

    THIS IS FORTH

    We can

    make

    a pr in tou t

    of

    only a par t of

    the

    t ex t .

    We

    are

    able

    to

    change

    the

    s t a r t -

    ing address and the number of cha rac te r s .

    PAD 2 + 2

    TYPE

    p r i n t s

    IS.

    We

    use

    a new word, TEXT, to read t ex t

    i n to

    PAD.

    In

    some vers ions TEXT

    i s

    a predef ined

    word.

    The def in i t ion i s :

    TEXT c

    PAD 72 32 FILL WORD

    HERE

    COUNT

    PAD SWAP CMOVE ;

    F i r s t ,

    we

    must descr ibe

    severa l

    new words

    used in t h i s def in i t ion . The word FILL

    f i l l s

    memory

    ce l l s

    withe

    the

    byte

    b .

    FILL

    a n b )

    F i l l

    n bytes , s t a r t i ng

    a t

    address

    a , with the

    byte

    b .

    The next new

    word

    i s

    WORD

    There are

    two

    d i f f e r en t def in i t ions

    of WORD in FIG FORTH

    and

    in FORTH-79.

    65

  • 5/19/2018 FORTH on the Atari Learning by Using

    72/132

    WORD

    c ) Read t ex t from

    the

    input buf fe r u n t i l

    the

    de l imi te r c

    i s encoun-

    t e r ed . FIG FORTH

    WORD

    c - a n )

    Read t ex t

    from

    the

    input buf fe r ,

    un t i l

    de l imi te r c

    i s encoun-

    t e red . Leave

    the

    address

    a and

    the

    l ength n of

    the

    t ex t

    on

    the

    s t ack .

    FORTH-

    9