FORTH

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
Post Reply
Nuxi
Posts: 2
Joined: 07 Jul 2018, 05:01

FORTH

Post by Nuxi »

I'll admit I've been holding off spending a lot of time with the gigatron until a decent keyboard solution presented itself. Now that that's close, while basic is lovely and period and all, has anyone considered a Forth? It's relatively easy to cobble together, and fast.
monsonite
Posts: 101
Joined: 17 May 2018, 07:17

Re: FORTH

Post by monsonite »

Hi Nuxi,

Forth is 50 this year - recently some original 1968 assembly listings for the IBM 1130 came to light, with Charles Moore experimenting with an early Forth. From the 50 year old listings, you see how Chuck builds up his Forth from the ground up using a few primitives, which exploit every feature of the machine architecture.

In the light of this new interest in Forth - I have experimented with a much reduced form of Forth, which requires no dictionary, and the interpreter fits into under 1k bytes of assembly language.

I call it SIMPL - serial interpreted minimal programming language. Having implemented it on ATmega, ARM and MSP430 - I think that it could be ported to the Gigatron, and run as an alternative 16-bit interpreter to vCPU - but still be compatible with the vCPU ROM coded routines. It would in effect treat each vCPU as a primitive instruction on the 16-bit virtual machine - and allow a stack based, Forthlike interactive language to be written from these primitives.

What it does bring is the interactive nature of Forth. Code typed at the keyboard can be executed immediately, or "compiled" into user definitions, which can be strung together to perform simple actions. Flashing LEDs, toggling ports and playing musical notes - by keyboard interaction are just a few of the things I have used it for.

At it's heart, is a character interpreter running in a loop. The interpeter is pointed at an area of RAM containing the user's ascii text input.

Using single ASCII characters, every symbol is treated by the interpreter as a call to an assembly language routine to perform some action.

For example the plus symbol +, when read by the interpreter, calls a block of code that adds the top two entries of the stack and leaves the sum on the stack.

For convenience, lower case characters and punctuation symbols are used to provide a call to "primitive" assembly routines. There are 59 symbols available for these primitives. They provide highly mnemonic access to the assembly routines running in the 16-bit machine from ROM.

Uppercase characters are used to define new words - created from combinations of primitives. This is analogous to the colon definition - as used in Forth. Clearly the user is restricted to just 26 of these user definable words - and this is the main limitation of this implementation. On encountering a capital letter, the interpreter is forced to jump to a fixed location in memory - where the code for the user routine is located. This effectively removes the additional complexity of a dictionary search - found in proper Forths.

The interpeter tests the text input buffer for number characters - and on finding a number or a string of numbers, converts that into a 16 bit integer that it puts on the stack.

I have used the space character as a means of separating numbers. When the space character is encountered it signals the interpreter that the next number should be placed on the arithmetic stack


Marcel's ROM consists of a series of assembly routines that can be called in this manner. It would be a simple task to link his ROM routines with an interpreter - to create a kind of pseudo-Forth.

But what is it good for? When bringing up a microcontroller for the first time you want to exercise ports, access memory and communicate with a serial terminal.

My cut down Forth, allows toggling of I/O ports, flashing LEDs, generating square wave signals (musical tones) and terminal communications - all in an interactive manner.

I think that it could be ported to the Gigatron fairly easily, having prototyped it already on an MSP430 in under 1024 bytes of assembly language - which is about 300 assembly instructions on the 16 bit MSP430.

The Gigatron would need as a minimum a serial terminal interface. At the moment it can shift 8 bit characters onto the databus. Adding an 8 bit output shift register would allow for a synchronous serial interface - which an Arduino could translate into a regular serial terminal interface.

Some examples - entered from a serial terminal:

h - set a port pin high
l - set a port pin low
hlhlhlhl - toggle a port pin to generate a square wave
1000 - put 1000 on the stack
1000 2000+p - add 1000 and 2000 and print them
1000m - a 1000mS delay
600u - a 600uS delay
h600ul600u - set port high for 600uS then low for 600uS

Now we introduce loops

10{kp} k is the loop variable that gets decremented each time around the loop, p prints the top of the stack - which is k

This prints the numbers 10 down to zero.

Now we can make musical tones

1000{h600ul600u} - 1000 cycles of 600uS high and 600uS low -> 1000 cycles of 833Hz tone

Like Forth, we can define new words using a colon definition - capital letters are used for htese new words.

:A1000{h600ul600u}; Define a word "A" that plays 1000 cycles of 833Hz

Every time A is entered - you get a short tone. All 26 capital letters can be assigned in this way.

It's definitely not Forth, but it shares a lot of similar ideas in a much smaller package. I can supply more details for those that are interested.

regards


Ken
Nuxi
Posts: 2
Joined: 07 Jul 2018, 05:01

Re: FORTH

Post by Nuxi »

Sounds a little like Mouse, an early micro language. There was a book on it that I learned the concept of a parser from. Like yours it was rpn and all the instructions were single characters, which made the parser super simple and good for teaching, just a case statement processing a character stream. There were macros too, which were basically simple subroutines rather than your more forthlike compilable routines.

Looks like there's still at least one nostalgic fan:

http://mouse.davidgsimpson.com

I'd love to see yours though. Machines like this need interactive environments other than basic!
User avatar
marcelk
Posts: 488
Joined: 13 May 2018, 08:26

Re: FORTH

Post by marcelk »

I never knew the tradition of microlanguages goes back so much earlier. SIMPL, Mouse look cool... Then there was DC which even predates Unix...

GCL has many elements borrowed from FALSE. I just replaced the stack with zero-page variables, and allowed for longer names because the compilation was going to happen in Python anyway. My original plan was to make a simplified online interactive version with single-character variable names and such. But now that TinyBASIC has come along, I have no real high priority for an online version of GCL. Maybe later.

FORTH, with dictionary, would be great. Next mountain top. And a LISP...? These shouldn't be hard at all.

BTW, we can get some limited data-out over the "input" port using modulation of the vSync signal. Preview here: https://www.youtube.com/watch?v=tAAXzB0iqBw
Post Reply