. .. ... The Syntiac Pages ... .. .
[Home] [Synthesizers] [Software Proj] [Hardware Proj] [Pinball]
|
[Games] [PerlWeb] [4Tron] [MPForth] [Video Compression]
|
[About] [Language] [Developers] [i386] [6502] [Research] [Ideas] [Download]
4Tron
Programming techniques and rules
This page contains detailed information for developers working in the 4Tron environment.
Keep in mind that the system is work in progress so the specifications will change in time.
Writing good Forth code for running under the 4Tron compiler is like an art. It isn't something
which happens automatically. 4Tron allowes the creation of very small and fast software systems.
The system runs on almost everything which has a microprocessor and allows your software to run unchanged
on all platforms supported. Because most of these systems are low-end a few restrictions are placed on
the 4tron system. Keeping these restrictions in mind and utilising the features offered by the 4Tron
system will enable you to run large application on (very) small systems. The system is fully open
and enables custimisation of the system to a far extend, without damaging compatiblity.
Ofcourse the system must be fully understood before changes can be made to the kernel. Therefore
these pages will try to help explain the ideas behind the implementations and choices made.
The 4TrOn playing rules
- Keep your word definitions short. 3 Lines of code is enough for even to most complex routines.
If you need more space for implementing the word, it is probably time to factor it into a few smaller definitions.
- Factor, factor and factor. Oh did I mention factoring of your code?
The art of Forth is really splitting up your words into small units. BUT! don't factor just to factor!
This sounds like a contradiction but it isn't. Don't factor when you just see two or more times the same
code sequence, it must make sense on a function level. A usefull trick is this: Only factor when you can
give it a distinct logical name. So 'drop1+swap' is not a logical name even if the sequence 'drop 1+ swap'
occures several times in the code.
- Keep the stack shallow. Don't work with more as 3 items on the stack at a time. The 4TrOn core doesn't provide
equivalents of ROLL, PICK, ROT and -ROT, so keeping the stack shallow is very important to write effective 4TrOn code.
- Know all the 4TrOn core primitives and their function. The core is very limited in size, so it's possible to learn
the function of all the words. Most are standard Forth words, but some have a slightly different definition.
Some implementation details may be assumed to be available on all 4TrOn implementations like tailrecursion or
seperated code and dictionary spaces. Use this knowledge to write efficient implementations.
- Try to prevent conditional code. Most decisions can be rewritten using logic operations which use less
processor cycles. The BOOL core word can be used to convert values to boolean bitpatterns (T and Z).
- Only solve the problems you have, don't solve the global world problem. It is always possible to find a
more general approach to a problem. But most of the time your current problem is a subset of the more general
problem so it will not be the most simple solution. Always try to find the most simple solution to a problem.
The general problem is almost never the most simple solution.
- Don't provide hooks in your code for implementing new functionality. Same as statement above, don't solve
the general problem. Don't provide hooks into your code for changes, because the changes you need to do
will probably be different from the one you anticipated. It is impossible to prepare for every change possible.
So DON'T DO that, and just implement the current problem in a simple way.
Type definitions and stack comments
b | Byte wide value | B, B@ B! |
d | Double cell wide value | stack comment |
f | Floating point number | F* F/MOD F- F+ FNEG |
w | Cell (word) wide value | W, |
Missing words in 4TrOn
+! ( n a -- ; add n to cell at address a )
Looks quite usefull, but until now I never used it in actual code.
ROT -ROT 2DUP 2OVER 2SWAP 2ROT
Don't put too much on the data-stack. For example 2ROT deals with 6 stack positions at
a time! ROT and -ROT might come in handy, but effective use of the return-stack will fix most problems. The 2OVER and 2SWAP
routines can be used when working with double numbers, but I would call them DOVER and DSWAP then... For the last one,
do : 2DUP OVER' OVER' ; if you need it.
DEPTH ( -- n )
Can somebody tell me why this word is in the CORE, instead of TOOLS wordset. Anyway try this if you need it
: DEPTH SP@' NEG' SP0' +' 2/' 2/' ;
The Syntiac Pages -- www.syntiac.com
For website questions mail to: webmaster@syntiac.com
"/4tron_developers" is last updated on Mon Nov 3 01:03:08 2003