Shop OBEX P1 Docs P2 Docs Learn Events
Multitasking token threaded Forth — Parallax Forums

Multitasking token threaded Forth

Peter JakackiPeter Jakacki Posts: 10,193
edited 2010-03-02 06:54 in Propeller 1
As an embedded system designer I frequently write a version of Forth for the chips that I work with. Number one for the reason that I can investigate the chip and system interactively and make sense of the datasheet and user guides (or lack thereof) and secondly to have the ability to write code that is both fast and compact and easy to debug. I wrote a complete Forth kernel, SD FAST16 file-system, software VGA generator etc in 32K on an ARM7 in 32-bit ARM code rather than the more compact 16-bit THUMB. I am planning to do the same for the Propeller but in a slightly different fashion of course.

At first glance I wasn't sure how I would tackle the job or if I would bother as I was quite happy coming to grips with SPIN. But lately I have come to a bit of a wall with code size and speed and lack of debugging capability. Now I can see how I can implement a stand-alone Forth system that can run without a PC babysitting it. With the addition of SD memory, VGA, keyboard, mouse, and a Forth O/S I can completely develop and debug on the Propeller and break free from the PC (not quite yet though).

It will probably be a few more weeks before I release any code plus I'll be busy moving house but I'm throwing this in to stir the pot and see who might be interested and perhaps more importantly who might like to contribute to this project. I'll leave it at that for now and include the header documentation block from my Propeller Forth (plus some screen interaction from my ARM system) that I have started to write.

**************************************************************************************
                     8X32A Assembly language Forth Kernel
**************************************************************************************

Forth is both a high-level stack based language and an Operating System.
It is interactive like Basic yet fast, compact, and extensible.
This language provides a high-level interface that gets very close to bare metal and also ideal for debugging.
A cog can run the 32-bit Forth kernel as one task and read 8-bit instructions tokens from main memory.

Either the token points to a code function in the cog or another high-level function
is called in main memory with additional bytes indicating the address if necessary.

IP points to the next 8-bit instruction in main memory
The 8-bit instruction addresses kernel routines aligned on 8-byte boundary in the 512 word cog ram

The inner interpreter:-

:NEXT                   rdbyte  token,IP                'read next instruction
                        add     IP,#1
                        shl     token,#1                'translate to address 512 longs
                        jmp     token


No token lookup table is employed but rather it is assumed that the token points to
one of 256 code locations on a 2-longs boundary. This may waste a long word between some functions
but it simplifies the translation. Anyway, the waste can be used as a general register.
Obviously not all 256 tokens will be used but maybe 64 as each definition takes around 4 assembly instructions.

A second optional method uses a jump table in the cog memory so that any further "opcodes" can be utilized
to call common high-level functions in main memory.
 
The data and return stack is held in local cog memory.

Heads (names and links) can consume a lot of memory and are not required for run-time operation
and so are delegated to serial memory, either I2C,SPI,or SD.

Text buffers are based in main memory.

Source code files are held and edited in serial memory as well.

Such a Forth system allows the whole Propeller development process to be completely PC free as
the Propeller can handle the screen, keyboard, mouse, and file system.

Many of the 8-bit Forth instructions are only short code snippets and I expect the performance to
be around 2 MIPS per cog. Each cog runs a single task but it is possible to create further tasks on each
cog by loading the multitasking Forth interpreter into the cog and setting the IP (instruction pointer).

Optimizations may include reading a long from main rather than tokens if this can speed things up a bit. 
Further enhancements may include automatic overlay into main memory from virtual memory in SD.

Sample disassembly showing memory usage for a high-level file function vs a more basic prtbyte function

SEE FLOAD-                                    ....  ( adr cnt|0 -- )
--------------------------------------------------
x0000_1402: LFA    FFE2
x0000_1404: ATR    F7
x0000_1405: NFA    46 4C 4F 41 44 2D 00
--------------------------------------------------

m0000_27C2: CFA            003E
m0000_27C4: 54             DUP
m0000_27C5: 21             0=
m0000_27C6: F3 14         (IF) >0000_27DB

m0000_27C8: 5B             DROP
m0000_27C9: 6E 13F0        DIR@
m0000_27CC: 6E 099F        +START
m0000_27CF: 6E 10CE        X@
m0000_27D2: 6E 13F9        DIR@
m0000_27D5: 6E 0990        +CNT
m0000_27D8: 6E 10D7        X@
m0000_27DB: 6E 142A        FILE@
m0000_27DE: 55             ?DUP
m0000_27DF: 63 06          (IF) >0000_27E6

m0000_27E1: 5A             ROT
m0000_27E2: 5A             ROT
m0000_27E3: 6E 0E72        XLOAD
m0000_27E6: 7F             EXIT
 ok
: .BYTE        BASE C@ >R HEX FF AND 2 U.N R> BASE C! ;  ok
DECIMAL 6789 .BYTE 85 ok
HEX 1234 8 SHR .BYTE 12 ok
SEE .BYTE                                     ....  ( byte -- ) Print as hex byte
--------------------------------------------------
x0000_1BCA: LFA    FFE4
x0000_1BCC: ATR    FF
x0000_1BCD: NFA    2E 42 59 54 45 00 00
--------------------------------------------------
m0000_0
m0000_0BD8: 16             BASE
m0000_0BD9: 3A             C@
m0000_0BDA: 4C             >R
m0000_0BDB: 35             HEX
m0000_0BDC: 12             FF
m0000_0BDD: 2A             AND
m0000_0BDE: 02             2
m0000_0BDF: C6             U.N
m0000_0BE0: 4D             R>
m0000_0BE1: 16             BASE
m0000_0BE2: 39             C!
m0000_0BE3: 7F             EXIT
 ok


Comments

  • rokickirokicki Posts: 1,000
    edited 2006-09-28 17:04
    Wow, this looks cool! Keep it coming!
  • Ym2413aYm2413a Posts: 630
    edited 2006-09-28 17:50
    Peter I just checked out the ARM based system on your website. I have to say. It's very impressive!
  • acantostegaacantostega Posts: 105
    edited 2006-09-30 05:13
    This is seriously cool. Please keep working on it!
  • LawsonLawson Posts: 870
    edited 2006-10-01 18:02
    sounds like this would be rather usefull for all those people who use Macs or Linx and have no access to a PC. How many Cogs do you expect the debug portions of this Forth OS to consume?
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2006-10-02 03:03
    Forth itself only requires the one cog but other cogs may be started-up to run more tasks. Debugging can be performed at different levels from the simple interactive exercising of a routine to single step debug/trace of a task from the main control task. A typical system probably would be:

    COG 0 Forth
    COG 1 Keyboard, Mouse, SD Card, I2C
    COG 2 hires VGA
    COG 3 hires VGA
    COG 4 RS-232 Serial I/O
    COG 5 Audio I/O
    COG 6 Spare
    COG 7 Spare

    The keyboard and mouse do not have to be run in real-time but can be polled by the cog when other services such as SD card access is not required.
    Obviously some cogs could be released if hires VGA was not required or even RS-232. It is tempting to place some of these more common functions onto another micro such as an LPC2101 seeing they are only around a couple of dollars apiece. But I want to see what I can do we just the one chip.

    *Peter*
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-10-02 04:23
    I don't know how well this would work, but I think Forth might make it somewhat plausible. It would be neat to build something like the RISC OS for the Propeller.
  • hinvhinv Posts: 1,255
    edited 2007-07-23 03:44
    Hi Peter,

    Has there been any work on this?

    Thanks,
    Doug
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2007-07-23 04:13
    Why yes, but I'm in the middle of a mountain of new hardware as well. The Forth OS is designed to be fast and super-compact in that it executes byte tokens from main memory. As 32K total is not a lot to play with once all the drivers are loaded I am making this Forth a virtual-memory model that reads in from SD memory automatically without impacting on performance overly. There are all the headers and help strings that I would not store inline in order to maximize the 32K for program. Once it is all done the system should be completely stand-alone in that Forth source code can be edited and compiled on the Propeller with the Propeller.

    The TCP/IP stack will rely on a Forth implementation as well, plus I have lots of goodies that can run under Forth, including the SD card and file handlers, audio, I2C, RS485 networking, communications etc.

    I am trying to stick with the Propeller IDE to generate the kernel but I have to work with the way Spin operates in order that contributed objects can be added to the kernel easily. Once the kernel is compiled with the objects it can be considered stand-alone in that it does not need the PC in order to edit and compile Forth code. So I haven't done as much on the kernel as I would like but all I need is a week or two of burning candles to have it up and running.

    *Peter*
  • deSilvadeSilva Posts: 2,967
    edited 2007-07-23 04:27
    An already advanved implementaion of FORTH is underway by a friend of mine, following similiar principles as outlined by Peter. He uses 16-bit words which simplifies matters considerably - at the cost of HUB memory of course. He also uses a "Multi COG" approach with three to four COGs running NEXT in parallel - the other COGs are enrolled for OS-type activities.

    We have estimated that the speed-up wrt to equivalent SPIN programs will be at least 2 in a slightly optimized version. I personally think that 4 will be the limit due to the same overhead issues that any interpreter on the Propeller is bitten by.

    However there are many more benefits other than speed, as already expressed in above postings.

    I welcome this competition!
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-07-23 04:34
    Theres also Cliff Biffle's implementation http://www.cliff.biffle.org/software/propeller/forth/
    Though he hasn't posted since November of last year so I wouldn't expect any updates.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2007-07-23 04:52
    Definitely a work-not-in-progress. The biggest problem I had with Cliff's Forth was the flakey serial coms which is the only means to compile with, He was going to update the coms but that has never happened. Anyway, I had put my Forth on hold at the time because Cliff had released his version which I believe could seriously turn new comers off Forth forever (only because it's 1/8 baked). Cliff seems like he could write a nice Forth if he put some more time into it and I wanted to save myself some time too by working in with him. I've written quite a few implementations of Forth myself and the Propeller and Spin environment present some unique problems to work around to make it all viable/reliable/compilable but it's all do'able.

    *Peter*
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2007-07-23 16:15
    Peter,
    Do keep us posted on progress ...I like this ..

    Peter Said:
    "The TCP/IP stack will rely on a Forth implementation as well, plus I have lots of goodies that can run under Forth, including the SD card and file handlers, audio, I2C, RS485 networking, communications etc. "

    Regards,
    QuattroRS4

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-07-23 19:47
    Paul Baker (Parallax) said...
    Theres also Cliff Biffle's implementation http://www.cliff.biffle.org/software/propeller/forth/
    Though he hasn't posted since November of last year so I wouldn't expect any updates.
    There two problems with his forth. 1) no documentation, not even his target system 2) no support

    Maybe a third, Cliff Biffle's nose, which is out of joint.

    I look forward to alternatives, even as I muddle through making my own.
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-07-23 20:05
    I browsed "words" in Peter's PropForth. I am impressed. The interactivity is what I miss a bit in Spin. It is a nice language, but if I could test things a little more directly, I would be very pleased. The PropForth speed advantage over Spin is also interesting. I wish you lots of fun and success with your Forth system, Peter. And I certainly look forward to have it under my nails.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-07-23 21:10
    I had a strong suspicion Cliff was going to burn himself out, and less than a couple months later he disappeared. We thought about dropping in on him at Google while we were in the area at ESC, but we were so busy we didn't have any time.

    I forgot he "binary-ized" his Forth, which is counter to what we were trying to accomplish to begin with·(had he released the·code others could use it as a stepping stone), I think Peter will be much more successful in his approach.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 7/23/2007 9:17:02 PM GMT
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2007-07-23 23:11
    Skogsgurra said...
    I browsed "words" in Peter's PropForth. I am impressed. The interactivity is what I miss a bit in Spin. It is a nice language, but if I could test things a little more directly, I would be very pleased. The PropForth speed advantage over Spin is also interesting. I wish you lots of fun and success with your Forth system, Peter. And I certainly look forward to have it under my nails.

    Just to set the record straight, I have not yet released a "PropForth", that was Cliff's, although I did write some fundamental & necessary extension words such as "words" and "dump" by analyzing the output of his Forth. I wonder why he didn't include them in his release as he must have had them written for debugging his own code.

    Now if you tried one of my non-public ARM versions I think you would be very impressed, but I'd rather work with Propellers, they are so much more interesting and configurable without all the hundreds of pages and errata that are typical of conventional one-chippers.

    I also look forward to the interactive development and debugging that comes with using Forth. I guess I am so spoilt with using Forth that when I use a conventional compiler etc it feels just like when you try and use windows without a mouse, so so awkward. My usually development cycle includes a lot of "what ifs" that are very easy to test out with one liners or even just tickling the right bits from the "command line" (hey, it's much more than a command line).

    *Peter*
  • bmentinkbmentink Posts: 107
    edited 2010-03-02 01:23
    Peter .... any update on your Forth?
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-03-02 02:06
    Hi Peter,
    I am interested in the OS and interpreter side of things. I do not know Forth, but hey, it's just another language.
    I will be in Qld later this week, so I'll give you a buz and we can catch up for a Qld Propfest.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2010-03-02 02:13
    Bernie, I recently made mention of Forth in a recent thread and I had to dig through my email list to find it (web searches are hopeless).
    http://forums.parallax.com/showthread.php?p=882046

    I would really love to run a Forth (interactive and fast!) on the Prop but as I had mentioned, it feels like a kludge. The ARM is so much better in this respect for the reasons that it has plenty of memory, optimal instructions, and it's fast. What to do? There are issues with interfacing to standard objects, there are issues with memory and cog usage, and speed. Yet I remember Forth running very nicely in an 8K ROM on a 2MHz 6502 so surely we should be able to do it (well) for the Prop, even on one cog. Maybe.

    Your bump makes me think that maybe I should just try for a solid functional Forth first, one that can work well on serial coms at least and then possibly one that uses VGA and SD as a stand-alone. In fact, any kind of Flash or EEPROM memory would be advantageous anyway just to hold the Forth headers. That would be a good start. As to how to interface to other objects I don't know yet. But boy, I sure could use that interactivity and extensibility that has always proved to be so conducive to development and debugging and exploring hardware, I really miss that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    *Peter*
  • mwalimumwalimu Posts: 44
    edited 2010-03-02 06:00
    Please keep at it. I like using FORTH for exactly the reasons you mentioned, it gives me a way to play with the hardware interactively. The two FORTHs that are out there are nice, but not improving.
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-03-02 06:38
    Sorry Peter - hadn't realised it was an old thread an just bumped.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
    · Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
    My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2010-03-02 06:54
    Hi Ray,

    I missed your post before but we will catch up though and see if we can plan a Propfest! However, Forth is not just another computer language. If you have never used a "proper" version then you won't understand, even if you read all about it. When we catch up I will demonstrate my ARM version, I am very sure you will be impressed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    *Peter*
Sign In or Register to comment.