gnupic: Optimiziing Forth compiler for mid-range PICs


Previous by date: 7 Mar 2000 02:46:02 -0000 Re: gpasm-0.18.1, Robert Stanton
Next by date: 7 Mar 2000 02:46:02 -0000 Re: Optimiziing Forth compiler for mid-range PICs, Francisco Rodrigo Escobedo Robles
Previous in thread: 7 Mar 2000 02:46:02 -0000 Re: Optimiziing Forth compiler for mid-range PICs, Wojciech Zabolotny
Next in thread: 7 Mar 2000 02:46:02 -0000 Re: Optimiziing Forth compiler for mid-range PICs, Francisco Rodrigo Escobedo Robles

Subject: Re: Optimiziing Forth compiler for mid-range PICs
From: Francisco Rodrigo Escobedo Robles ####@####.####
Date: 7 Mar 2000 02:46:02 -0000
Message-Id: <200003070233.CAA02085@xinef.pepix.net>

I noticed the small typo in the subject of my announce, but in order not to 
break the thread, i will not correct it :)

> > I mailed Scott Dattalo at the address listed in the gnupic.org pages, as I 
> 
> That page is hopelessly outdated. My current e-mail address is the one
> where I'm sending this response.

Thanks, it seems our messages get crossed, as I recently joined the list.

> > Any suggestion (and code patches :) are welcome. Right now, PIC models 
> > supported are 16C84 and 16C63, but adding more mid-range models is easy. 
> > Low-range PIC support is not planned. High-range may be supported in the 
> > future.
> 
> >From what I can tell, the compiler is written Forth as well. I don't know
> Forth at all. Could you perhaps provide some sample pic programs written
> in Forth?

It's my intention to make a sort of tutorial, but I lack the time right now. 
There is a very small example in the docs, and I have just sent a message to 
the list in response to Wojciech Zabolotny in which there is a snippet of code 
showing an interrupt service routine.

Some words about Forth (funny, because 'word' is the term for a callable 
routine, a variable, etc. Everything is a word in Forth):

 - Forth uses a stack for intermediate results, local variables and parameter
passing. This mechanism is not transparent to the user. Every word takes its 
parameters and leaves its results in the stack.

 - @ is used to fetch the value of a memory address. So, if PORTA is defined 
as the address of port A (in fact, it is in the currently supported processors), 
    PORTA @
   effectively leaves in the stack the current value at the port A pins.

 - ! stores the value in the second on stack into the address in the top of 
stack, so if PORTB is defined as the address of port B,
    PORTA @ PORTB !
  effectively copies the current value at the port A pins into port B.

 - there are high level flow control structures, as IF and loops as WHILE, 
UNTIL and infinite loops.

 - a word definition is like a subroutine, but in mary this is extended so a 
routine can have several entry points.

For example, suppose you want to make a toy in which a led bar "moves" in a 
ping-pong fashion (or a "Knight Rider" fashion, if you like). A possible (not 
complete) code could be:

----8<----
( AN EXAMPLE OF A PING-PONG LED BAR )

PICFORTH HEX

INIT-PIC

100 CONSTANT MYDELAY ( ALTER VALUE OF MYDELAY FOR CHANGING SPEED )
VARIABLE COUNTER

0 ORG            ( START OF MEMORY    )
FLABEL MAIN GOTO ( MAIN CODE          )

4 ORG            ( INTERRUPT VECTOR   )
FLABEL ISR GOTO  ( INCONDITIONAL JUMP )


( SUPPORT WORDS )

: DELAY ( N -- ) ( A TIMED DELAY )
    COUNTER !

    BEGIN
        COUNTER @        ( LOOPS UNTIL 0 )
    UNTIL
;


: PINGPONG ( -- ) ( A FULL CYCLE OF LED BAR PING PONG )
    1 CONST, ( INITIAL VALUE, FIRST HALF CYCLE )

    BEGIN
        DUP
    WHILE
        DUP PORTB !
        MYDELAY DELAY
        2*            ( EQUIVALENT TO A LEFT SHIFT )
    REPEAT

    40 CONST, ( INITIAL VALUE, SECOND HALF CYCLE )

    BEGIN
        DUP
    WHILE
        DUP PORTB !
        MYDELAY DELAY
        2/            ( EQUIVALENT TO A RIGHT SHIFT )
    REPEAT
;


( HERE COMES MAIN PROGRAM CODE )

MAIN

BEGIN
    PINGPONG
AGAIN


( INTERRUPT SERVICE ROUTINE. MUST BE CALLED PERIODICALLY )

ISR

COUNTER @ IF             ( IS COUNTER NON-ZERO? )
    -1 CONST, COUNTER +! ( YES, DECREMENT IT    )
THEN

RETFIE
----8<----

Again, too lazy of me for testing it right now (it's too late), the interrupts 
must be properly initialized, but they aren't in this code.

There are a pair of tricks in the example, one being the testing for the end 
of the WHILE loops, the other being the reliance on a periodic interrupt. 
There is a kludge in mary in the for of the (rather ugly) CONST, word, whose 
purpose is to push into the stack a previous unnamed constant. This will 
disappear in the future, but for the time being, we must live with this. 
Remember mary is in alpha state.

Oops, too long a message. Sorry.

Regards.


---
Francisco Rodrigo Escobedo Robles - ####@####.####
El surrealismo vive en Pepix Labs - http://www.pepix.net/
Este mensaje expresa unicamente mi opinion en este momento



Previous by date: 7 Mar 2000 02:46:02 -0000 Re: gpasm-0.18.1, Robert Stanton
Next by date: 7 Mar 2000 02:46:02 -0000 Re: Optimiziing Forth compiler for mid-range PICs, Francisco Rodrigo Escobedo Robles
Previous in thread: 7 Mar 2000 02:46:02 -0000 Re: Optimiziing Forth compiler for mid-range PICs, Wojciech Zabolotny
Next in thread: 7 Mar 2000 02:46:02 -0000 Re: Optimiziing Forth compiler for mid-range PICs, Francisco Rodrigo Escobedo Robles


Powered by ezmlm-browse 0.20.