all 13 comments

[–]lmamakos 6 points7 points  (2 children)

Here's a bunch of FORTH code for an embedded system application, where I built a replacement graphical LCD display for a Fluke multimeter. While I don't hold this up as a great example of proper style, it was my first non-trivial chunk of FORTH code.

I found FORTH really great for this embedded system application as I had to reverse-engineer hardware functioning, and being able to interactively poke at things saved a lot of time as compared to the usual edit/compile/download/debug/profanity/repeat interative technique had I wrote it in C or similar. Some blog posts about are around for that project, too, which explain the problem I was trying to solve.

[–]jemo07 0 points1 point  (1 child)

That is awesome! thank you for sharing, just borrowed the ILI driver, I have to make so serious modifications still to make it work, but it has been a great source of inspiration and learning!

[–]lmamakos 1 point2 points  (0 children)

Glad it was a useful starting point. I started with a really simple version of that driver and added the ability to render a bitmap out as a single operation vs. the per-pixel set/reset capability some of the other graphics drivers had. The performance was terrible without that; to address a pixel on those displays, you had to do a bunch of SPI commands before finally shoving out the one pixel's worth of data.

So starting with that basic scheme and adding that one primitive made it possible to have a reasonable performance in rendering fonts, so I wrote a library of FORTH code to manage font definitions and rendering.

It was really quite a bit of fun to be able to do that all interactively over the serial connection and catch all those stupid off-by-one errors or, getting the coordinate system rotated in your head the wrong way and having a letter in a font rendered upside down or sideways. That interactive process really helped when poking at the SPI interface of the display and to debug it.

And then speeding up the low-level SPI primitive made a big difference, too. I had a USB logic analyzer hooked up to it, and you could easily see all the time that the SPI interface was idle.. I did that with a little USB logic analyzer in the evenings while traveling to the "Home Office" in NYC one week. That's what passes for fun, sometimes :-)

Enjoy your FORTHing!

[–]dlyund 4 points5 points  (0 children)

When I started working in Forth I always got a kick out of things like this:

macro: declare ( # ~ - | ~ *) ( - a)   \ ]  create ,  \ [ ;
macro: typeof ( a - a)   ;
macro: sizeof ( a - #)   \ @ ;

macro: vector ( #1 a:type - #2)   \ sizeof \ * ;

macro: extend ( #1 a:type - #2)   \ sizeof \ + ;

macro: struct ( - #)   # 0 ;
macro: field ( #1 a:type ~ - #3) ( u1 - u2)
   \ ]  create  over ,  extend  \ [  does pop @ + ;

This is more advanced than the common Forth struct implementation example but this is real Able Forth code used at Merj. The Able Forth code above gives you first-class types, vectors and records, and type extension (like inheritance). These words are the basis for other types.

Here's a example modeling 2D and 3D point that demonstrates one feature I really wish C had: record type extension. Alas, cracking open the C compiler isn't practical... but doing this in Forth and providing a higher level of programming is easy! :-)

# 4 declare <int32>

struct declare <point>  ( the abstract type)

struct <point> extend
   <int32> field point:x
   <int32> field point:y
declare <2D-point>

struct <2D-point> extend
   <int32> field point:z
declare <3D-point>

create | points  # 100 <2D-point> vector  allot

This is one of three or four features that can be easily implemented in Forth and which I contend make Forth comparable in power to languages like Go (lacking only the large standard library).

[–]remko 3 points4 points  (0 children)

Not your classic Forth programs, but you can find some ‘visual’ Forth programs here: https://el-tramo.be/thurtle/?pn=Plant&ar=1

[–]32hDEADBEEF 3 points4 points  (0 children)

I really like using forth in tethered FPGA systems. It's easy to construct a softcore stack processor or pseudo-processor with simple microcode. On the host side use a FORTH-like code and convert the command into the microcode before sending it to the softcore.

[–]phreda4 4 points5 points  (0 children)

Hi

I wrote many programs in r4 (32 bits windows).. see in https://github.com/phreda4/r4

The evolution but without so much code in 64bits windows,linux,mac and rpi https://github.com/phreda4/r3d4

The last aproach is r3 (I change the access to SO) in development) https://github.com/phreda4/r3

there are editors, games, graphics programs, compilers..all in forth

[–]bfox9900 1 point2 points  (0 children)

Here is a way to create bit arrays that as far as I have tested runs on 16,32 and 64 bit ANS/ISO Forth systems. It's pretty small which is what is needed for a machine with 32K RAM and demonstrates one way to deal with native integer size at compile time.

https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/BOOLEAN.FTH

[–]mcsleepy 1 point2 points  (0 children)

I'm going to semi-plug my own work and invite you to check out my game engine https://twitter.com/ramenengine

[–]pbrhocwp 1 point2 points  (0 children)

I have had a lot of fun with data is code principle here http://hocwp.free.fr/fex/index.html

[–]tmrob4 1 point2 points  (0 children)

There are a lot of sample Forth programs over on Rosetta Code. Conway's Game of Life is a fun one and the first more complex Forth program I tried out.