A simple Forth development board

forth

Forth is a very interesting programming language. It’s very flexible and is extremely efficient on low powered hardware, but unfortunately not very popular simply due to the fact that it’s not very popular. There were a few Forth-based microcomputers built in the 1980s, but these were largely unsuccessful.

[Leon] is a Forth aficionado and came up with his own Forth development board in the hopes of Forth making a comeback. It’s a very small and cheap board – only about $12 in parts – but it’s still extremely powerful and a fun platform for investigating Forth.

Compared to other programming languages found in 80s microcomputers, Forth is just weird. It’s a stack-based language, so instead of adding two numbers like 3 + 4, Forth uses postfix notation (or Reverse Polish Notation) so the same statement is expressed as 3 4 +. It’s a much more efficient way for computers to handle data, and some claim it’s more efficient for humans as well.

[Leon] created his own board able to be programmed in Forth, shown above, that uses an ATMega328 microcontroller. He’s using AmForth to put Forth on his system, but also extended the base AmForth install with his own floating point version. making this version of Forth at least as powerful as any 80s microcomputer or ATMega development board is today.

[Leon] put together a great demo of the capabilities of Forth and his dev board. You can check that out below.

Comments

  1. Chris says:

    Cool. The FIgnition is also a nice Forth small DIY computer.
    https://sites.google.com/site/libby8dev/fignition

  2. Angus says:

    > not very popular simply due to the fact that it’s not very popular.

    Probably also not popular because stack manipulation doesn’t match how most of us tend to intuitively think about computation.

    • John Bokma says:

      I think you have to replace ‘intuitively’ with ‘culturally’.

      • Angus says:

        I disagree. Humans intuitively work with information by associating it with other information (e.g by naming it, like we name variables in mathematics or in C or Python etc). Nobody naturally thinks by pushing data onto a stack and doing DUP and ROT and SWAP in their head.

        • Kit Scuzz says:

          To be fair to John here, I think you have to admit that there’s really not a lot of naturality to things like parenthesis and the order of operations. You had to memorize “PEMDAS” for parenthesis, exponents, multiplication, division, addition, and subtraction.

          RPN is actually a nice way to resolve that issue. The order of operations doesn’t need to exist, the way you write the equation sucessfully shows what order you *meant* to apply the operations in:
          3 4 + 3 *

          Now instead of thinking about this as a stack, just imagine thinking about it as if you were reading it on a page. You stop when you see the plus and take everything you’ve read so far and add it together, and then you replace what you saw:

          3 4 + 3 *
          Take 3 and 4, add them together: 7
          7 3 *
          Then you repeat, replace the 7 and 3 with the product: 21
          21
          And you have your answer.

          If you worked like this in school, “showing your work” would have you successively performing operations and replacing them with the result like this! This is also very natural compared to how we read in English: left-to-right and you understand the whole operation*.

          If I tried to write this with in-fix notation, we would see (3 + 4) * 3
          But understanding why that’s different than 3 + 4 * 3
          Requires you to know more than just what the operations do and how to read left-to-right.
          ————————————–
          * The whole left-to-right thing only works if you write out the equation in what’s called the “chain calculation” method. You can write hella confusing RPN stuff once you know it’s a stack
          3 3 4 + *
          Is the same equation as above. The only real difference in reading here is that “replacement” happens to the two operations immediately preceeding the operation. You don’t have to refer to it as a stack at any point.

        • t-bone says:

          I’ve been in many classes where others were learning their first language. NOTHING comes intuitively in that first exposure to computers.

          • Kit Scuzz says:

            I think he was saying that RPN is unintuitive to people learning math, even ignoring computers.

            I argue that RPN is no more unintuitive then PEMDAS

          • Cardcomm says:

            My favorite definition of the word “Intuition” is “The act of complex pattern matching performed at a subconscious level”.

            Given that definition, I believe ‘intuitively’ was the correct word. I’d argue that people of MOST cultures can quickly evaluate simple mathematical expressions using subconscious pattern matching. In fact, probably the ONLY people that would find RPN easier would be those with NO previous exposure to math. Even then I’m not sure they would find it easier…

            BTW, my first exposure to RPN was with one of the first “cheap” LED handheld calculators. It required the entry of all calculations in RPN. My family got it when I was about 8, so I was at about 3-4 grade math level. I was the ONLY person in the family that could use it. It was ANYTHING but intuitive. Frustrating as crap, but NOT intuitive…

            Of course, one could argue that I’d already been culturally “polluted” by having taken 4th grade math. The point is that it’ll be hard to find the correct “culture” to embrace stack based coding :)

    • Kit Scuzz says:

      Any language in which you can redefine “1” to be something different and the language doesn’t bat an eye is a language which is dangerous. Check it out! (using gforth)

      : 1 2 ; ok
      1 1 + ok
      . 4 ok

      That’s right! 1 + 1 = 4

      The issue here is typing. There’s no differentiation between the number 1 and the named object one. There’s no separation of name spaces. People say things like “C is powerful but dangerous,” but I had a professor who once told me: “Programming in C is like walking through a workshop filled with bandsaws with no guards on the blades. Programming in forth is like turning off the lights on the guy walking around in that workshop” (and this was a professor who *loved* forth)

      There are a lot of reasons why forth fell out of favor. I could ramble on and on about them. I think a lot of them simply have to do with the fact that we can provide a more functional experience with better syntactical sugar, better type checking, and that computers have enough horse power in general to just run more straightforward code. We can afford the cost of multi-pass compilation and strict type-checking. In uncompiled languages (ruby, python, etc), we can provide better syntaxes which make for more readable code.

      Last but not least: http://globalnerdy.com/wordpress/wp-content/uploads/2007/09/forth_on_the_atari.jpg

      • Sven says:

        Why doesn’t books on C or Python look like that?

      • Tom Hargrave says:

        I agree and I used to program on FORTH. The other part you did not mention is because you are adding commands to the library FORTH becomes a very personal language. If you develop an application in FORTH, then handed that app of to me to work on, the end result would be a disaster even with excellent documentation!!!!

        But FORTH is a neat language and probably one of the first if not the first true object oriented language. Now you’ve done it, I’m going to have to drag out my old FORTH books. Wonder if anyone wrote a FORTH compiler for PIC?

      • lnmaurer says:

        “Forth zealots take [it's power to weight ratio] as one of many signs that Forth is the One-True-Langauge. I disagree; modern desktops and laptops are so powerful that a great power to weight ratio is unnecessary. Generally I’d rather use a language with dynamic typing, automatic memory management, many libraries available, etc. [B]  However, microcontrollers don’t have cycles to burn, making Forth an appealing choice.”
        -TFA

    • Remy Dyer says:

      Actually your mind does have a stack, it’s just that it behaves more like a conveyor belt. (did you see that cool “conveyor machine” CPU design?) in that it’s always moving, and only ~ 7 deep. The 8th thing gets dropped.

      A stack machine is normally implemented in memory so that it tries to never drop anything, much like we always want our machines to be as correct and as deterministic as possible.

      Our minds aren’t though. Follow a train of thought through 7 steps or so, and you’ll forget what you were thinking about originally.

      If you just read a couple of numbers, you’ll probably easily remember the last couple, without even trying. But more than 5 or so and you will need to put them together into a larger, more complex “single” object, and remember that instead. Our minds may be good for only 7 “stack locations / conveyor addresses”, but there isn’t really a limit on how many bits of info can be referenced by each one. Much like passing by reference really.

      • Kit Scuzz says:

        I watched all the videos related to Mill CPU architecture and I remain skeptical of the whole belt idea, but no one responded to my comments :'(
        http://hackaday.com/2013/08/02/the-mill-cpu-architecture/#comment-1035542

        Ignoring that, saying that you have a stack but it behaves like a conveyor belt is the same as saying that you have a queue, and a queue is not a stack. In fact, it takes two stacks to implement a queue!
        http://stackoverflow.com/questions/69192/how-to-implement-a-queue-using-two-stacks

        And skipping past that point, I think what Angus was trying to say was that people tend to think in steps, i.e. do step 1 and save the result in A, then do step 2 and save the result in B, then do step 3 and use A and B. Because people do reasonably well with references like “here we are using the result in A” but they do poorly with “I put the result in the same place every time I get a result, and I can’t randomly pick a result, I have to move all my other results out of the way!”

        I argued above that I think its no less intuitive, but most people are not terribly used to it so they don’t structure our problems in a way that lends themselves to stack-based applications.

  3. Mark says:

    My first programmable device, a Texas Instruments TI-57, also used Reverse Polish Notation. It doesn’t take that long to get used to

  4. lwatcdr says:

    Forth is for people that find Assembly too straight forward.

    • gmcurrie says:

      remember battling with LISP – ‘Recursion’ was the ‘way to go thing’- so,…umm’, yep, ‘Let’s use it For *Everything*’… (Without a syntax-checking editor))))))))))))))))))))

    • tekkieneet says:

      Prolog uses recursion for everything, backtracks and no variable assignments!!
      After doing a course in that, data structure course in CS was easy.

      Yoda speaks RPN.

      • tekkieneet says:

        ^prolog does not let you reassign a new value into same variable.

      • Johan G says:

        “Forth learn you will.” ;-)

        Having read in on the basics of Forth and playing a little bit with it in the past, I realise that it is not a language for general use, but a language for very specific tasks.

        In fact I tend not to see it as a programming language per see, but more as a tiny operating system with the inner interpreter being the kernel and the outer interpreter being the interface to the kernel. I think that when properly used it will quickly become a domain specific “language” (DSL) aimed at the task at hand.

        On systems with extremely tight memory exotic techniques such as Huffman threading could be used (though some of those techniques was probably patented originally).

        From what I have read the task at hand would often be very specific, like firmware in an appliance or in a sensor. Some of the processors specifically developed to run Forth was extremely suited to real-time critical uses due to extremely low interrupt latency (4 processor cycles on late 1980’s hardware).

        As for RPN, learning it was a hassle, but using an infix (algebraic) calculator for any problems involving more than two numbers today makes me cringe. :-P

  5. Kris Lee says:

    When you are going to do Forth then by not go with the following chip instead? http://www.greenarraychips.com/ It will give you 144 Forth cores.

  6. geonomad says:

    In 1984, MacFORTH was the only language available for the Macintosh that didn’t require using a LI$A for development.

    I recall writing several applications on the early Mac in FORTH and even sold one to Disney and some language extensions to the MacFORTH developers.

    But really, I’d rather code in APL.

  7. David says:

    What is it about this board that makes it a Forth board? Why not just use an Arduino?

    • lnmaurer says:

      “If you just want to play around with AmForth, a pre-made board like an arduino will work fine. However, if you want to integrate AmForth into your own circuits, understanding a simple, bare-bones design is useful. To that end, I’ve made a simple board based around a ATMEGA328-AU microcontroller — running at 20MHz — and a FT232R USB to serial IC.”
      -TFA

      It car be tricky to get AmForth setup on your own design, so I wanted to provide a simple worked example. (Precompiled binaries are available for arduinos.

  8. Eric Parker says:

    I don’t wanna be that guy, however; “at least as powerful as any 80s microcomputer or ATMega development board”.

    Looks like an ATMega development board to me. It just happens to run FORTH due to some custom firmware.

    That aside, neat project.

  9. Luke Weston says:

    The custom board shown just appears to be an AVR plus an FT232RL, so I can’t understand why you’d bother to design and fab a custom PCB instead of using some ordinary, cheap, off-the-shelf Arduino or Arduino-like PCB and running custom firmware on it.

  10. marcus says:

    sit in front of my pc/linus, then run a FORTH code on a AVR board?
    why not running the FORTH on my pc/linux? what’s the point…?

    • Alex says:

      With a microcontroller you can do realtime hardware control, which is not possible with your desktop computer. Also its peripherals (A2D, PWM, IO ports, etc) are likely more straightforward to use than the ones in your computer that you must access through your operating system.

    • lnmaurer says:

      Yes, if you want to program Forth on your computer, then this is a silly thing to do.

      That’s not how it’s meant to be used; the video just showed how it can be interactively programed.

      AmForth is meant to program microcontrollers for the things that microcontrollers usually do — controlling other, simple electronic devices. For that, it’s quite useful.

  11. George Witherspoon says:

    FORTH IF HONK THEN

  12. Trui says:

    There’s nothing you can do in Forth that you couldn’t do better in C. The only exception would be CPUs that can’t run C, but they can be easily avoided.

    • polossatik says:

      cpu’s that cannot run C? I assume cpu for which there is no C compilor?

      • Trui says:

        Or maybe there is a compiler, but the net result is still very poor because the architecture isn’t suitable for C. Take the 6502 for instance.

        But most modern CPUs are designed to be C-friendly, so there’s no problem.

        At the same time, most CPUs are not Forth friendly.

    • t-bone says:

      Hook up a terminal to a “C board” and then get back to us. You’re looking at the space of problems that C solves. In that realm, yes, C is better.

      • Trui says:

        I’ve hooked up many terminals to boards running C code. No problem.

        • t-bone says:

          Does what it’s designed to do, right? Awesome. Now try to change the program.

          • Trui says:

            Simple. Just edit the program, compile it, and upload it to the device.

            That sure beats editing the code within the limited Forth environment with its lack of a good editor, version control, and numerous other productivity tools.

          • t-bone says:

            That sound you hear is the point sailing over your head.

          • lnmaurer says:

            Trul,

            “Simple. Just edit the program, compile it, and upload it to the device.”

            That’s exactly what it’s meant to avoid. From TFA:

            “IMHO, the killer feature of AmForth is that it runs on the microcontroller and can be used interactively; you develop directly on the device! Your big computer serves only as a serial terminal that sends text to and receives text from the microcontroller.

            Contrast this with using C: you write the code on your big computer, compile the code on your big computer, upload the code to the microcontroller, and try running the code. If it works, that’s great. If not, you have to go through the whole process again, possibly using debugging tools.

            Since AmForth can be used interactively, you just try the code, and you know right away if it works or not. Combine this with the preferred Forth coding style of breaking everything into small subroutines (that are easily tested), and you have a powerful programming tool.”

    • mstone says:

      Show me a fully functional C interpreter that fits in 4k of memory.

      • Trui says:

        That doesn’t make sense. You’d have to compare solving one specific problem in Forth vs C, for instance writing a Forth interpreter in Forth vs writing the same thing in C. Or compare writing a fully functional C interpreter in Forth.

    • M Simon says:

      I have competed commercially against “C” with Forth. My small team (3 people) ran rings around the 30 person “C” team. We could do in one month what they couldn’t do in six. On top of that the Military Inspector who looked at our code said it was the most readable code he had seen in a few years. The inspector came in knowing nothing about Forth.

      I LOVE using Forth when competing against “C”.

      “C” because of its stack thrash encourages a lot of bad programming practices. Not to mention having to write everything twice.

  13. Reg says:

    If you want to play w/ forth try mecrisp

    http://mecrisp.sourceforge.net/

    on an MSP430 or Stelaris LaunchPad.

    An important thing about forth is it was created for robotics, not general programming. Chuck Moore invented forth to solve the problems he faced automating the Kitt Peak radio telescope. He also used it to meet the original deadline for automating the Riyadh airport after the original contractor was hopelessly behind schedule. He’s also well known for reinventing forth every time he implements a project.

    http://www.forth.com/resources/evolution/evolve_2.html

  14. mstone says:

    Forth (and LISP, which was mentioned above) fall in the “not-C” class of computer languages. People used to C don’t like them at first glance because they don’t have a C-style syntax like they’re used to. They feel the same about Haskell and ML, and audibly wonder why Perl can’t be more like C.

    The trouble with MFPLs (My Favorite Programming Languages) is that everyone considers theirs to be the most powerful. They know the features their language offers, and can see when other languages don’t offer the same features (“function pointers aren’t first-class objects? how lame”), but they don’t understand the utility offered by other languages with features their MFPL doesn’t offer.

    C-style languages go about 80% of the way up the list of support for first-class programming objects. They’re built for machines with a Harvard memory architecture, with separation between program memory and data memory, so they don’t treat ‘the code’ as a first-class programming object.

    The taste of what C-style programmers are missing lives in the short-circuit logical operators — `exp1 && exp2;`, where exp2 doesn’t even get executed if exp1 proves to be false. That’s a hardwired special cases of a more general and powerful programming technique: the ability to decide if/when code will even be interpreted. If you use C++, you also get exception handling, and a taste of type-based programming from signature-dependent function overloading.. but you have to define all your signature rules ahead of time (templates, anyone?).

    C-style languages provide a thick syntactic abstraction between “what the user writes” and “what the computer executes”. They look like the notation we were taught in math class (whose utility is arguable when you look at all the order-of-execution bugs out there (and the depth a paren nesting required to prevent them)) but keep a lot of programming power under the language’s lid in the process. It also requires specialized and notoriously complicated software to turn “what the user writes” into something a computer can execute.

    Forth and Lisp provide a thin layer of syntactic abstraction from the interpreter. “What the user writes” can be executed directly by simple code. You get to build/modify the abstract syntax tree at execution time. They provide built-in support about 90% of the way up the list of first-class programming objects, make about half the rest easily accessible, and provide fundamental support for what’s left.

  15. coder says:

    Apart from dabbling with Jones Forth a few years ago purely out of nostalgia, it’s 20+ years since I did any Forth. At that time I was using a Harris RTX2001 (stack based design optimised for Forth). Comparing Forth to C or any other conventional language is missing the point IMHO. It’s more like having the kernel for an OS that does what *you* want for *your* specific application, no more, no less, and no external dependency on costly (at that time) compilers, debuggers, etc, etc… just you and Forth… heaven :-) The world moved on, communication is key, which translates into code easily readable & adaptable by average programmers who longer understand computers at a hardware level.

    Nice project! Keep the flame lit!

  16. coder says:

    Meant to add: when coding in Forth it’s amazing how quickly you arrive at a DSL, sometimes without consciously meaning to. I suppose that’s true of many languages depending on your approach. It just seemed to be a naturally flow with Forth.

  17. Great to see this new Forth Board. Did some pretty serious Forth hardware and commercial software in the 1980s, and still have a soft spot for the language.

    ps. My Forth book can still be picked up second hand: http://www.amazon.co.uk/The-Complete-FORTH-A-WINFIELD/dp/0905104226

  18. WS says:

    I knew Chuck, he’s a great guy. I went to school with his son Eric, brother of Forth. I still have one of his custom boards running the Novix NC4000.

    One day, I went to visit Eric. His dad was developing some software. He was typing on a home-made keyboard that was made from 7 limit switches attached to his leg with Velcro. He was typing 7-bit ASCII using his fingers and left-right part of his palm. Not only was his typing very fast on that thing, he has also developed a custom predictive dictionary system that made him that much faster! Chuck was the most intelligent person I’ve ever met. I miss you Eric!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s