Mu Forth


Tips for programming AVR in muforth.

Read the source for the muforth system. It is here:

Look at an example that actually runs. This is the first ever code we wrote for the muforth-cybords collaboration. The page will offers a hex download if the last save got a clean assembly.


Imagine what muforth for cybords could be like if we wrote words that manipulated code fragments.

Let me enumerate some of the features I've assumed in my fantasy version.

  • Native instruction words, MOV, COM and CP, consume registers and leaves code fragment references on the stack. A code fragment looks enough like a register that short sequences can be assembled without lots of intervening words.

  • Native port pin words, pb0, pb1 and pb2, produce resource fragment references.

  • Bracket words, { and }, mark the stack and aggregate contained fragment references into a single compound fragment.

  • Macros like hi!, lo!, on! and off! create code fragments that manipulate port pins. hi?, lo?, on?, and off? create fragments that sense state.

  • The temp macro allocates a temp register and creates a fragment that loads it with an immediate constant, 128 in our example. Temp lifetime is that of the smallest fragment that encloses all references when generated; visibility is a the whim of the forth programmer.

  • The repeat macro wraps one fragment with an infinite loop. The if-else macro wraps two clauses and a condition register setting fragment with appropriate skip and branch fragements.

  • The wait macro expects a cycle count, which can be computed from a desired period, 60 msec and knowledge about the clock speed. The cycles word reports the expected duration of a fragment thus, 50 wait cycles answers 50, further, { do-it dup cycles 100 usec swap - wait } will pad do-it to take 100 usec.

  • The setup macros, setup-ddrb and setup-bynase, factor setup code out of code fragments. If not factored, the setup will be performed at each use.

  • The clock macro programs the clock-divide fuse and established the assumed cycle time for the wait macro.

  • The reset macro programs the code that will execute on cpu reset.


Mu-wiki and Muforth are now visible from the public site.

Cybords search will find mu-wiki pages. Try searching for byin or byout.


Cybord use of Muforth is still evolving. I am begining to see layers forming.

  • mu-wiki -- Easy modification and distribution of Cybords. Should have some trace or dump capability eventually. A client-side downloader-programmer falls into this level, as does the server site git support.

  • mu-bynase -- Library of macros that emit and assemble most of the code fragements that one would use to write Cybords. One should rarely have to go past the macros and write a specific AVR instruction. (This layer could use a better name. The current name comes from the include file bynase.asm.)

  • mu-codosome -- This is the infrastructure one would use to write macros in muforth. It sets the discipline for using the compile-time stack and manages the accumulations of fragments as a Cybord compiles. FirstLoop is the HelloWorld for mu-codosome.

  • mu-target -- This is the forth cross-compiler technology that makes the upper layers possible. It is itself divided into AVR specific, general compiling, and elemental forth layers.

 

Last edited April 8, 2008
Return to WelcomeVisitors