Forth interpreter/compiler for Megadrive

Announce (tech) demos or games releases

Moderator: Mask of Destiny

Post Reply
ehaliewicz
Interested
Posts: 43
Joined: Tue Dec 24, 2013 1:00 am

Forth interpreter/compiler for Megadrive

Post by ehaliewicz » Tue Jun 10, 2014 12:46 am

I've been working on a small forth system for the Megadrive part-time for about a month now, it's very rough around the edges and buggy, but someone suggested that I upload it. It might be fun to mess around with.

Image

Source: https://github.com/ehaliewicz/megaforth
Binary: https://github.com/ehaliewicz/megaforth ... /forth.bin

Controls (6-button isn't required but definitely helps):
A - Set character at cursor to A
B - Delete character in front of cursor
C - Insert space at cursor
X - show possible words based on prefix starting where the cursor is pointing (shows one suggestion per button press)
Y - enable on-screen keyboard
Z - hold to allow left-right movement in keyboard mode.
Mode - Starts or completes word definitions by inserting ";" or ":" based on state. This is done on a line-by-line basis, it doesn't keep track of the state of the interpreter. Don't use with multi-line definitions.
Start - send line to interpreter

Features:
- Basic forth dictionary (stack manipulation, arithmetic, memory manipulation, basic text i/o)
- Subroutine-threaded compiler (compiles words as lists of JSRs to other words)
- C interop via CCALL0/1/2/3 words, binary includes SGDK libraries. Push args in left-to-right order with the function's address last. C function addresses have to be hardcoded, it has no way of discovering them at runtime.
- Supports compiling included forth text at start up.


Currently known issues:
- Probably several word-related bugs. Expect division related words to fail.
- "." (general number printing) is buggy for larger numbers.
- Weird issue with deleting and inserting near the beginning of input line.
- Prefix suggestions overwrite rather than insert text
- Clearing keyboard on hardware doesn't work

Future Ideas (in order of likeliness to implement):
- Fix bugs
- Implement more compiler words (for IF/ELSE, variables, etc.)
- Implement optimizer (for inlining short words, compiling BSRs rather than JSRs, tail-call optimization, etc)
- Some way of calling arbitrary C routines at runtime? We'd need to keep track of a symbol table, I guess.
- Task switching for lightweight cooperative multitasking
- Some way to develop on PCs and send code over serial to compile
- Cool logo on startup
- Open to other suggestions



Feel free to mention bugs here or on github. The code is probably terrible at the moment, but if you want to modify it and submit pull requests, go ahead.



Update:

I figure a lot of people don't know Forth, so I added documentation for all predefined words. I think it just about doubled the size of the ROM, however.
Last edited by ehaliewicz on Thu Jun 12, 2014 5:08 pm, edited 5 times in total.

mic_
Very interested
Posts: 265
Joined: Tue Aug 12, 2008 12:26 pm
Location: Sweden
Contact:

Post by mic_ » Tue Jun 10, 2014 7:18 am

That's pretty cool. I also did some Forth stuff for the Megadrive a long time a ago. But I used cross-compilation rather than interpretation (I had written a Forth compiler that could generate M68k assembly, which I then assembled and linked with the GNU binutils).

ehaliewicz
Interested
Posts: 43
Joined: Tue Dec 24, 2013 1:00 am

Post by ehaliewicz » Tue Jun 10, 2014 8:21 am

mic_ wrote:That's pretty cool. I also did some Forth stuff for the Megadrive a long time a ago. But I used cross-compilation rather than interpretation (I had written a Forth compiler that could generate M68k assembly, which I then assembled and linked with the GNU binutils).
A cross-compiler is probably more practical (especially if it supports interactively sending code to the hardware), but not as fun as a native interpreter in my opinion.

haroldoop
Very interested
Posts: 113
Joined: Sun Apr 29, 2007 10:04 pm
Location: Belo Horizonte, MG, Brazil

Post by haroldoop » Wed Jun 11, 2014 1:29 am

Pretty cool idea, but it would require at least keyboard support to be "not painful" to use. :D

ehaliewicz
Interested
Posts: 43
Joined: Tue Dec 24, 2013 1:00 am

Post by ehaliewicz » Wed Jun 11, 2014 3:09 am

haroldoop wrote:Pretty cool idea, but it would require at least keyboard support to be "not painful" to use. :D
True, but I don't have a megadrive keyboard to use :lol:

Anyway, the interactive mode, while cool, is probably best used for testing, afterwards you should be able to type in the same code on your pc and compile it into the binary.

Post Reply