Mecrisp on other platforms Jul 6, 2016

We’ve already seen that Mecrisp Forth is also available for the Nandland Go Board FPGA board.

But Mecrisp Forth can in fact be used on several other platforms.

For one, there’s the MSP430 version (which like the Go Board, is a 16-bit version, not 32-bit as on ARM). Here’s a build using the MSP430G2553 chip, with 16K flash and 512 bytes of RAM:

This one communicates at 9600 baud by default, and has about 5 KB flash free for user code. The neat bit is that it all ends up in an easy-to-use DIP chip, which could readily transfer to your own projects once flashed with working code.

Ken Boak has blogged about a build using the MSP430FR2433 chip with 15.5K FRAM (that’s “Ferro RAM”, which retains its contents on power-down) and 4K RAM.

But the Mecrisp options don’t stop there. There is also a build for 32-bit ARM which runs on Linux. Here’s a demo, running on my home automation server:

$ ssh mohse
jcw@mohse:~$ uname -a
Linux mohse 3.8.13.30 #1 SMP PREEMPT [...] armv7l armv7l armv7l GNU/Linux
jcw@mohse:~$ mecrisp
Mecrisp-Stellaris RA 2.2.7 for Linux by Matthias Koch
ok.

As of Mecrisp 2.2.7, this version runs really well (an important cache bug was fixed by Matthias recently). In fact, it fully supports the way flash and RAM memory areas are implemented on embedded µC’s, including “flash erasure” and the cornerstone word. The one major difference of course, is that we’re now running under the Linux OS, and have no direct access to hardware. Instead, there’s a syscall word, which can be used to implement system calls to Linux.

One use which I intend to explore, is as test environment with a TDD approach. The speed of this setup should be a huge time-saver - both for uploading new code and for running it.

The Folie terminal front-end utility now also supports remote execution using SSH:

$ folie -x ssh mohse bin/mecrisp
Connected to: (ssh mohse bin/mecrisp)
Mecrisp-Stellaris RA 2.2.7 for Linux by Matthias Koch
ok.
: a 0 do loop ;  ok.
1000000000 a  ok.
^D
$

This is a quad-core Odroid C1 running at 2 GHz. That 1,000,000,000x loop takes under 1 second, so the loop cycle time in Forth is under 1 ns.

Now watch this:

$ ssh debbix uname -a
Linux debbix 3.16.0-4-amd64 #1 SMP Debian [...] GNU/Linux
$ folie -x ssh debbix bin/mecrisp
Connected to: (ssh debbix bin/mecrisp)
Mecrisp-Stellaris RA 2.2.7 for Linux by Matthias Koch
ok.
: a 0 do loop ;  ok.
1000000000 a  ok.
^D
$

Marginally slower, roughly 1.5 sec for the same loop. But there is something very unusual going on here: this is a virtual machine running on my MacBook Pro laptop. How can an Intel 64-bit x86 chip run ARM binaries?

$ ssh debbix
jcw@debbix:~$ file bin/mecrisp
bin/mecrisp: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), \
                                        statically linked, not stripped
jcw@debbix:~$ ls /usr/bin/qemu-
qemu-aarch64-static       qemu-mips64-static        qemu-s390x-static
qemu-alpha-static         qemu-mipsel-static        qemu-sh4eb-static
qemu-armeb-static         qemu-mipsn32el-static     qemu-sh4-static
qemu-arm-static           qemu-mipsn32-static       qemu-sparc32plus-static
qemu-cris-static          qemu-mips-static          qemu-sparc64-static
qemu-i386-static          qemu-or32-static          qemu-sparc-static
qemu-m68k-static          qemu-ppc64abi32-static    qemu-unicore32-static
qemu-microblazeel-static  qemu-ppc64le-static       qemu-x86_64-static
qemu-microblaze-static    qemu-ppc64-static         
qemu-mips64el-static      qemu-ppc-static           
jcw@debbix:~$ mecrisp
Mecrisp-Stellaris RA 2.2.7 for Linux by Matthias Koch
ok.

It’s all done by Qemu, an open source emulator for dozens of different CPU architectures (see the list above). And by installing qemu-user-static, something very clever happens: when a binary executable is launched which does not match the host architure, Qemu is transparantly started to take over. It emulates the processor, and passes all the Linux system calls through to the host. The effect is truly magical: if you have all the necessary shared libraries installed (using the “multiarch” mechanism), any binary will work just as if it was running on that specific CPU!

So there you have it: Mecrisp Forth runs in lots of places, which is perfect to try it out and play with it - whether embedded, with serial uploads (and freezes), or under Linux, with instant uploads (and harmless premature quits).

And if you want to look into a (large!) implementation of Forth, check out gforth - it’s available as standard package for just about every Linux distribution, as well as Windows and Mac OSX.

The development “feeling” you get from running code interactively, the ability to peek and poke at actual hardware, and the concise way in which an application can be implemented are unique to Forth, and yet it fits on some of the smallest µC’s:

(from Leo Brodie’s “Thinking Forth” book)

Some more resources for getting into Forth:

Forth is not mainstream / hype of the day, but oodles of fun - and amazingly effective!

Weblog © Jean-Claude Wippler. Generated by Hugo.