r/Forth • • Apr 17 '26

voxels in r3forth

51 Upvotes

Working on a voxel graphics system in r3forth (open source on GitHub), it's still missing some elements but some things are already showing up. The system works well on Windows and Linux.


r/Forth • • Sep 27 '20

sectorforth: A 16-bit x86 Forth that fits in a boot sector

Thumbnail
github.com
50 Upvotes

r/Forth • • May 08 '26

working in a mini 3d voxel engine in r3forth.

47 Upvotes

Donwload and try in https://github.com/phreda4/r3


r/Forth • • Nov 09 '25

My new Forth

Thumbnail
gallery
47 Upvotes

I was discussing Forth implementations on a Forth discord and we discussed how to implement a Forth in C or C++. I noodled on it a bit and came up with a way to implement one in C++.

The idea I came up with is an array of (cells) function pointers (to words written as C++ functions). Some of these pointers have a cell following as argument - typically an address like to a variable's or constant's memory or to a WORD's PFA (the word's CFA precedes it, right?)

So what you're looking at above is SDL2 1920x1200 window and the screen/window/console drivers written in C++. The Forth consists of 2 headers (.hpp) and 2 source (.cpp) files. One is for Dictionary operations, the other is all of the Forth.

The SDL window is managed by C++. The console driver renders arbitrary fonts and point sizes into the console window. EMIT renders characters into the console window using SDL functions. The console driver supports many ANSI escape sequences, though not all. Sufficient to set foreground/background color, clear to EOL, clear screen, etc.

The background/wallpaper is one I found on the Internet. I like the look of a grey theme.

This Forth has no concept of NEXT. The CPU stack is used for calling and returning from C++ functions and nothing else. There is a separate return and data stack used by Forth words. I'm using no assembly, inline or otherwise, so accessing things like the CPU stack register has to be done with C++ trickery only.

Keeping the CPU stack in a good state is done via C++ try/catch/throw. I use try/catch around the query/interpret loop and ABORT simply throws an Exception that is caught. When caught, the CPU stack is exactly where we want it to be. Also, it turns out that you can throw within signal handlers, so I installed a SIGSEGV handler that throws an exception and if you do 1 0 ! it prints "SEG FAULT" in the console window and prints ok...

The magic is in DOCOLON:

PRIMITIVE DOCOLON() {
  // ClearLocalVariables();
  auto initial_rsp = RSP;
  rpush((cell_t)IP);
  IP = (cell_t*)W;
  auto rbp_save = RBP;
  do {
    FPTR cfa = (FPTR)*IP++;
    W = (cell_t)*IP;
    (cfa)();
  } while (IP && initial_rsp != RSP);
  IP++;
  RBP = rbp_save;
}

The do/while loop iterates through the array and calls the C++ function. Words written via : .... ; have DOCOLON as the address in the array IP points to with the DFA of the words within : and ;

More magic is done in EXIT:

// Updating RSP (to return from the Forth word stream) causes the do/while in DOCOLON to exit.
PRIMITIVE EXIT() {
  cell_t ndx = local_variables.size();
  RSP += ndx;
  IP = (cell_t*)*RSP++;
  ClearLocalVariables();
}

There's some additional logic in there to deal with local variables. RBP is a C-like BP pointer that points to local variables. Normally I would push that BP on the return stack, make space for locals, then set BP to current RSP (return stack pointer). Then local accesses are relative to BP. On EXIT, space has to be reclaimed from the stack and the old BP popped.

The only gotcha in this scheme is that when calling EXECUTE from the INTERPRET loop (interactively). There is no IP pointing to some code and no return address at that point. Thus I fetch W as the next cell from the IP stream or set it in INTERPRET so word like DOCONST can know where the constant's memory location is.

I stayed away from std:: namespace functions except in the GUI and in a few cases in the compiler (vector to create a stack of local variable names and offsets, etc.).

The dictionary is traditional Forth style. One big block of memory that has a HERE and LATEST and grows up as words are added. The predefined PRIMITIVE words do not take up space in the dictionary, just their dictionary headers do (not the code).

The second image shows that I'm using about 9K of dictionary space in total as of now.

To give more of a flavor of the C++ functions:

PRIMITIVE STATE() { dpush((cell_t)&state); }
PRIMITIVE LBRAC() { state = FALSE; }
PRIMITIVE RBRAC() { state = TRUE; }


// ( n addr -- , store cell at address )
PRIMITIVE STORE() {
  cell_t* addr = (cell_t*)dpop();
  *addr = dpop();
}
PRIMITIVE PLUSSTORE() {
  cell_t* addr = (cell_t*)dpop();
  *addr += dpop();
}
PRIMITIVE ONEPLUS() { TOS += 1; }
PRIMITIVE TWOPLUS() { TOS += 2; }

TOS is kept in a variable - hopefully C++ compiler will optimize that to use a register. Either way, it makes the code elegant enough.

One more thing to mention is that all the USER type variables are defined with __thread attribute (I #define TLD __thread). This causes, on x64, the code to use FS or GS register relative addressing and every pthread gets its own FS and GS. That is, I should be able to run multiple threads of Forth (in separate windows) using pthreads. I haven't tested using pthreads for a 2nd thread running Forth (I do have another thread doing SDL logic).

// We keep TOS in a separate variable so we don't have a ton of stack access
// for eery word.
TLD cell_t TOS;
// Data stack
TLD cell_t DSTACK[STACK_SIZE]; // memory for the stack
TLD cell_t* DSTACK_TOP; // address of top of stack
TLD cell_t* DSP; // data stack pointer
// "Return" stack
// We use the CPU stack for call/return to C++ functions and this
// stack is used for maintaining Forth IP while executing DOCOLON words.
TLD cell_t RSTACK[STACK_SIZE]; // memory for the stack
TLD cell_t* RSTACK_TOP; // address of top of stack
TLD cell_t* RSP; // return stack pointer
TLD cell_t* RBP; // base pointer for local variables

// Instruction pointer
TLD cell_t* IP;
TLD cell_t W;

Repo is at https://gitlab.com:mschwartz/nforth

The repo was created on Oct 25 and today is Nov 9. So about 2 weeks old.

I'm planning to rename this to "inspiration" from nforth. TBD later though.


r/Forth • • Apr 06 '25

VIDEO Update on my STC Forth implementation

Thumbnail
gallery
47 Upvotes

I am currently calling it bforth for lack of a better name.

The idea of this Forth is to boot from permanent storage on an old laptop I have and to work entirely baremetal.

As you can see, I have implemented a desktop and windows system entirely in Forth. You can also see that bforth has time sliced multitasking. You can see in each window a console with a forth prompt and commands can run and print to windows simultaneously (multitasking).

Windows can have a console attached and not necessarily be a Forth command prompt. For example, it can be a block editor or file editor.

Windows can have no console attached and be used to render graphics - I will upload images later once this bit is working fully. These windows without consoles can select from a set of event messages they will receive - key up/down, mouse move, window resized, window moved, window close box hit, etc.

I have implemented a signals system so a task can wait for a signal to be sent (waiting tasks are on a waiting task list and otherwise not processed, or "blocked"). This might be used by a timer to wake up the task periodically.

On top of signals, I have implemented a message passing system. This is styled after Amiga OS's MessagePorts and Messages. You create a Message object, fill in fields like requested operation, data, etc., and send it to another Task's port. That task wakes up and gets the messages, performs the desired operation, and replies to the message. This is ideally suited to implement devices, like disk block access.

I haven't tried it on real hardware yet. I have to implement an NVME driver in assembly for the boot loader to be able to load the Forth image from disk. But it works excellently in QEMU on my m1 MacBook Pro. That's X64 emulation - and it's fast.

I have a long ways to go. The generated code is not optimized. I can see by disassembly of words things like DUP followed by DROP which can be optimized out entirely. It's still plenty fast, but the idea is to make it better and better over time.

I just made the repo public. Have a look. I'm willing to take on collaborators (experienced ForthWrights only).

https://gitlab.com/mschwartz/bforth


r/Forth • • Dec 22 '21

The smallest LISP now beats smallest FORTH at 436 bytes

Thumbnail justine.lol
47 Upvotes

r/Forth • • Jul 16 '24

milliForth: A FORTH in 340 bytes — the smallest "real" programming language currently made.

Thumbnail
github.com
46 Upvotes

r/Forth • • May 30 '22

Frustration: escaping a Turing tarpit with Forth

Thumbnail
gitlab.cs.washington.edu
46 Upvotes

r/Forth • • Jul 18 '26

Pre-announcing SUPERSHOW, my upcoming Forth game engine.

Post image
45 Upvotes

Greetings-

I wrote a game engine called SUPERSHOW entirely in VFXForth and it is nearing 1.0. I designed it for making pixel art games. These are some screenshots of several test programs showing off what it can do.

Some things to know:

  • I created several experimental game-oriented Forths over the years - including GC-Forth (GameCube), Glypher, Tengoku, and Ramen.

  • This is the evolution of the latest iteration of the project. I've previously referred to this iteration as VFXLand5.

  • It is Windows-only.

  • It includes a dialect I created to make coding in Forth easier (called TUFF).

  • No separate scripting language - everything's in Forth. Math is fixed-point and a sophisticated OOP system called NIBS is the basis for everything.

  • Other features: An asset system, a parallel graphics window that lets you use the VFX Forth IDE while the game is running, a namespacing system, Forth-style multitasking support, and runtime validations (WIP).

  • You can break out of the pixel game orientation if that is not your thing. (Like the 3D example on the top-left.)

  • It has its own simplified graphics API but it runs on OpenGL, which you're free to do what you want with.

  • No dependencies except VFXForth (you compile the engine yourself; a no-step process) - to release games commercially all you need is their $20/month subscription.

  • Several big features (such as joystick support) will be added post-1.0.

  • It'll be freely available as source code, and there'll be a paid option to get official tools and other stuff.

It'll be 2-3 months before official release. I still need to tie up loose ends, write documentation, and build a proper example game.

Fire away with any questions!


r/Forth • • Mar 07 '26

my homebrew 16-bit Forth system — tanuki OS

45 Upvotes

Update on my OS since I last made a post here a month ago.

It is a "baremetal" subroutine threaded code 16 bit Forth for the x86 series.

This has been an incredibly educational and productive experience as someone quite new to programming and learning how computers work.

If my emulator was any good, it should be backwards compatible up to the original IBM XT PCs. However, the OS is reliant on certain BIOS behaviours, which isn't consistent across all machines, so your milage may vary. Through my personal experience, it does seem to work properly on some 90s and 00s era PC hardware.

The OS supports either a floppy or a USB boot. In the image above, the OS is running directly off a USB on my Thinkpad T480.

On its own, it comes with a line-based text editor, block memory to load in programs during runtime, and an 8086 assembler that you can inline inside Forth words like shown in the example above.

I desire to add more features and continue to expand my little toy, but due to time constraints I plan to retire this project.

A fork of this OS is "Borbular", which is packaged with a primitive graphics library for drawing tiles, texts, numbers, etc. to the screen in graphics mode.

The overarching goal of the OS was to write as simple version of a Forth as reasonable. I wish there were more "educational" Forths out there, since I believe the available information can often be arcane to the uninitiated. At least it was for me. I hope my Forth can be refined to that level where other people will find it useful to learn from. :) But it is a lot of work to make things accessible for others.

Overall it was a very fun experience! I learnt a lot about Forth and low level interaction with hardware. I wish the language could gain some popularity. It certainly has that elegance and true simplicity that seems to be rare in modern programming cultures.


r/Forth • • Nov 03 '21

The smallest LISP now ties the smallest FORTH at 512b

Thumbnail justine.lol
45 Upvotes

r/Forth • • Jul 21 '25

MykesForth update

Thumbnail
gallery
44 Upvotes

It's been a few weeks since I last posted a status update on MykesForth.

MykesForth now boots from a FAT32 volume. The boot sector loads MykesForth from the FAT32 volume as well. All the file I/O words in MykesForth now support FAT32. FAT32 so far is read-only. I haven't bothered yet to implement write support.

I have themed the desktop with this grey theme.

There is now a Widget system. You can see on the window title bars and screen title bar the MacOS looking buttons for close, minimize (not implemented) and depth (send window to back). The Widget system is a framework for implementing much more interesting UI components like menus and buttons and text input boxes and sliders and so on.

There are now desktop Icons that you can click on to launch "applications." An application is a word that spawns a task that runs and opens/manages a window. There are several as you can see.

I also implemented a Dock at the bottom. Those Icons can be clicked on to launch programs/apps as well.

The system supports multiple screens and each one can either act as a workspace/desktop (with icons/dock, etc.) or can be specific to an application. For example, a spreadsheet (one of these days!) application might open a screen and use that for all its tool windows and the main editing window. The screens can be depth arranged similar to how the Windows can be. I have it on my list to do Amiga style drag down the front screen to see partial screen(s) behind it :)

My most recent addition has been to support PCF (X11 binary) fonts. The fonts demo on the first screenshot shows a variety of styles and sizes of fonts.

MykesForth does not currently support development on Linux. It's a todo item to implement the build system on the Linux side. The code will assemble fine, just making the FAT32 volume/file is currently using very MacOS specific commands (hdiutil).

I am building this on my M1 Max MBP. To build everything takes 7 seconds, most of that is copying files to the FAT32 volume (fonts are big files!). Images are in TGA format and those are big files, too. All that said, the whole system uses about 50MB of the 256MB disk volume/file.

At the top of the screen it shows memory usage, memory free, and how big the dictionary is. Currently, everything you see is about 750K of dictionary and uses about 65MB of RAM. Forth is so compact in comparison to Linux/C which uses hundreds of MB for a minimal desktop, and 2+ MB just for the gcc exe.

Project is at https://gitlab.com/mschwartz/mykesforth/


r/Forth • • Oct 16 '24

Slightly heretic Forth dialect for graphics prototyping running bare metal on RPI Zero 1.3

Thumbnail
gallery
44 Upvotes

Showing my Forth with a graphics sample, bare metal although U-Boot is used for the board setup.

Compiler is around 350 lines of ARM assembly plus a bit more for the primitives so more like 500, it doesn't have much reflection features and differ on some primitive so may looks quite heretic :) mainly made for quick graphics prototyping akin to p5js, right now support quotation, static array, strings and variables, has a small parser, only has two type of loop, a simple for going down to 0 and a do with step and direction, both use next as termination word, there is no index word instead the loops push their index so it may either be dropped or used as a var, parentheses are ignored but i use them as group for readability.

Mostly a Forth beginner coming from a mainly imperative background so i use a lot of vars but i like some of Forth concept.

It was built upon this first minimal version : https://github.com/grz0zrg/ARM-ForthLite


r/Forth • • Oct 26 '21

Current progress on "Cosmic Conquest (1982)": It sort of runs in GForth!

Post image
44 Upvotes

r/Forth • • May 07 '26

An interesting potential niche for Forth: programming on the move with a chorded keyboard.

Post image
43 Upvotes

This has already mostly been explored in the form of things like M5CardForth and BANDIT but I wanted to add on a potential major reduction of form factor onto that.

If you've never heard of it, a chorded keyboard is a keyboard where combinations of keys are used instead of individual labeled keys. Despite having no labels, it takes just a few hours to memorize the combinations, and despite having fewer keys, the lack of finger travel means accuracy and max typing speeds are much higher. Here's the homemade one pictured, which I believe would have 60 combinations. They can easily be handheld or worn, and used one-handed without looking. If you replaced one or all of the buttons with joysticks like a CharaChorder or added a second chorder for your other hand you could get even crazier.

Chorded keyboards with fewer buttons can be limited in the number of keyboard characters that can be used without introducing double taps or finger travel, but Forth uses very few characters, and the number of characters can be cut down further through use of words. 2 2 + vs 2 2 ADD is a very small change compared to 2 + 2 vs add(2, 2) in other languages. I believe the standard set of characters required for Forth would just barely fit in 60, but would go well over if using Python and maybe over if using BASIC.

I think optimally it would be combined with a pair of glasses with display like ElevenLabs G1 or Brilliant Labs Frame so you don't have to hold something up to your face to see what you're typing, but a little screen is good enough for grinding out a few lines of Forth while taking a shit.

Bubbly may be a good option for trying this out.


r/Forth • • Sep 02 '22

FORTH on the HP 71B Pocket Computer

Thumbnail
youtube.com
43 Upvotes

r/Forth • • Sep 14 '21

So excited to have this!! 😄

Post image
43 Upvotes

r/Forth • • Jul 06 '26

Inspiration Forth Update

Thumbnail
gallery
42 Upvotes

https://gitlab.com/mschwartz/inspiration

I made some YouTube videos so you can see Inspiration in action.

https://youtube.com/playlist?list=PLRYxtMZb7Qy2wuArGURPCkXkxwK7tnT48&si=ZgRRLVrfLLnhLcj4

It's been about 6 weeks since my last update, and there is a lot of new code and programs implemented.

I am working on a music program and it's coming along nicely. The logic to align the notes on the staff wasn't easy!

After reading a thread about how to implement cards in r/cplusplus, I got inspired to implement a Blackjack game, with casino rules. You can split hands, double bet, 5 card charlies, buy insurance if dealer shows an Ace, etc. The game implements a shoe which is made of 4 decks.

Here's the trick with cards. A card is a random number between 0 and 51. The suit is card mod 13 and the rank is card / 13. I use a 52 byte array to keep track of what cards in a deck have been dealt. Shoe logic draws a card randomly from one of the 4 decks.

The cards, decks, hands, shoe, etc., are general purpose so I can later make a Klondike solitaire game.

I also finished the Evade2 game. It is a first person space shooter with music and sound effects. It is a game I made for Modus Create several years ago, so it was a port. It only took a few days. The music and art and logic in C++ was already done, so I just translated from C++ to Forth in the editor.

I got sidetracked again from the Music program. This time guys on the Forth discord channel were talking about 6502. Turns out I made games for the 2600, C64, and other 6502 based systems. I also made the 6502 Artist Workstation for Electronic Arts in the mid 1980s, which included an assembler and debugger.

In about 20 man hours, I made a dasm (written by my friend Matt Dillon!) 6502 assembler workalike, a 6502 disassembler, and a 6502 debugger/emulator. You can see these in the screen shots.

I never use claude or codex or any other LLM to generate any code. All of Inspiration originated with me and was coded by me. I did use a random number generator I found in a Usenet chain by the author of GForth. The repo was started in October 2025 and has hundreds of commits and merges. Proper PRs! You can view the issue boards at the URL to see how I track TODO and done work items.

The artwork (cards, icons, window decorations, etc.) are images I found on the Internet and are royalty free and free to use. I am not an artist, or I would have made the images myself.

Inspiration is a multithreaded (pthreads) Forth implementation that has a graphical desktop, windows, icons, and so on. I was inspired to make a Forth where you can type in the terminal at the Ok prompt and have graphics rendered. All threads share the one dictionary. All programs have access to all those words.

The threads allow multiple "applications" to be running at the same time, as you would with any desktop environment. Every pixel in these images are rendered by Inspiration.

A trick I found is that I can use C++ try/catch around EXECUTE and anything that throws a C++ exception is caught. I tested on a dozen or so operating systems including FreeBSD, MacOS, Linux distros, on X64, ARM, and even Raspberry Pi. What I found is that in a signal handler (e.g. SEGFAULT), I can throw an exception and it is caught by the try/catch around EXECUTE. So at the OK prompt or in any word, I can do something incredibly stupid like:

OK> 100 0 !
OK> 100 EXECUTE

And I catch the SEGFAULT or SIGBUS errors and print an error message and ABORT. Inspiration should not crash as I installed signal handlers for all the signals.

The rendering engine is based on SDL2. SDL2 gets me fonts with antialiased text, bitmaps for my code to manipulate images at the pixel level, and GPU acceleration where I can take advantage of it.

I envision a Forth with native graphics capabilities. I didn't see the point in making another Forth that runs in the terminal window. There are so many good ones already. What makes Inspiration different is you can do this:

Ok> 10 10 100 100 $ ffffff draw-line \ no set up, white line in your console

You can see the graphics capabilities in the screenshots.

Why am I making this? I want a project I can work on for years to come. I am not close to running out of programs to implement and enhance. The music program alone is one that I may end up working on and enhancing for years.

My Forth coding style relies heavily on structures and local variables. Here is a sample of the logic for the deck of cards.

STRUCT| _Deck
WORD| Deck.number // deck number (in shoe)
WORD| Deck.remaining
52 BYTES| Deck.dealt
|STRUCT

: Deck.Shuffle { deck -- , shuffle the deck }
52 0 do
0 deck s& Deck.dealt i + c!
loop
52 deck s! Deck.remaining
;


r/Forth • • Aug 27 '24

Assembled the original My4th board and it's really great!

Post image
42 Upvotes

r/Forth • • Apr 26 '22

Don't Thread on Me

Post image
42 Upvotes

r/Forth • • Jul 03 '21

I built a tiny Forth from scratch - inside a bootloader-based Arduino UNO's 2K of RAM

Thumbnail
thanassis.space
42 Upvotes

r/Forth • • May 07 '26

More Inspiration, in progress

41 Upvotes

I've been working on some game demos. As you can see in the video, I have a good start on a game engine in Forth, with physics and sprite engine and state machine for controls/enemy intelligence.

The video shows a huge universe with 3 sprites, 2 positioned far off screen. The arrow keys apply thrust in the direction. If no key is pressed, friction is applied to bring the speeds to a stop.

The starfield is rendered at 1/2 world velocity for a parallax effect.

I started on this yesterday and this is how far I got by this morning. I wrote only Forth code for this. Downloaded the planet sprites from a free clip art site.

Zero AI assist used in any of Inspiration development.

I can't add photos and video for some reason. I can start another thread if people want to see more.

Or you can look for screenshots in the repo:

https://gitlab.com/mschwartz/inspiration


r/Forth • • Nov 17 '25

VIDEO Windows update break ColorForth, Chuck thinks about giving up on it after asking AI Copilot for help

Thumbnail
youtube.com
41 Upvotes

r/Forth • • Sep 12 '25

I wrote a small block editor for zeptoforth (picocalc)

Thumbnail
gallery
41 Upvotes

A small block editor I wrote for the picocalc, since in my experience the built-in one does not work well for it.

It can fit within 10 lines of a traditional forth block.

block import variable b 1024 allot variable y 64 allot
: b! b swap block! ; : b@ find-block b 1024 move ;
: >b 1- 64 * b + ; : run 10 b! 10 load ; : .l >b 64 type ;
: .n dup 10 < if space then . ; : .> cr ." > " accept ;
: d >b 64 0 fill ; : dd b 1024 0 fill ; : new-block dd b! ;
: l cr .l cr ; : ll cr 17 1 do i .n i .l cr loop ;
: w >b dup 64 .> dup >r + 64 r> - 0 fill ;
: ww b dup 1024 .> dup >r + 1024 r> - 0 fill ;
: c >b y 64 move ; : p >b y swap 64 move ; : cp swap c p ;
: a >b 64 begin over c@ 0<> while 1- >r 1+ r> repeat .> drop ;

basic commands include:

  • b@ ( n -- ) fetch block into the buffer

  • b! ( n -- ) store block from the buffer

  • d ( n -- ) delete line

  • dd ( -- ) delete all lines

  • l ( n -- ) print line

  • ll ( -- ) print all lines

  • w ( n -- ) write to nth line

  • ww ( -- ) write to buffer (multi-line)

  • c ( n -- ) copy from nth line

  • p ( p -- ) paste to nth line

  • cp ( src dest -- ) copy and paste

  • a ( n -- ) append to end of nth line

  • run ( -- ) interpret contents in the buffer

  • new-block ( n -- ) create new block with id "n"

words provided by zeptoforth:

  • load ( id -- ) interpret each line of block with id "n"

  • list ( id -- ) print contents of block with id "n"

  • block? ( id -- f ) returns whether the block with id "n" exists

  • copy-block ( src-id dest-id -- )

  • delete-block ( id -- )

  • delete-blocks ( id count -- )

etc...

I do not have much forth experience, so feel free to grill me into a better programmer.