In The One Instruction Wonder, I introduced my one-instruction CPU, along with a cross assembler that compiles code for it and other CPUs. Even with one instruction -- or maybe especially with one instruction -- assembly language gets tedious. So eventually even hardcore hackers long for a high-level language.
Consequently, I thought about porting a C compiler and may yet do that. But every time I look at C compilers that are "simple" to retarget, my enthusiasm wanes. Even the simplest is a pretty tall order to port. However, my thoughts recently turned to Forth. I've written a few Forth interpreters in my day, and I realized I could probably crank out a Forth cross compiler using the same quick-and-dirty techniques I used to create the cross assembler. In fact, the more I thought about it, the more I realized that awk and Forth were pretty compatible.
Forth is a simple, yet elegant, language organized around words. Essentially, everything is either a number or a word. Numbers are pushed onto a stack and words causes something to happen. There are just a handful of core words and one of these lets you define new words based on existing words. Like awk, Forth considers any group of characters ending in a some sort of whitespace to be a word.
Normal Forth is partly compiled and partly interpreted. There are a lot of words concerned with the difference between compiled and interpreted code, plus words for managing the interpreter environment (for example, dumping the list of known words). Since I was planning a cross compiler, I dispensed with all of that. My Forth would simply convert words into a sequence of subroutine calls. Eventually, you'd hit some core words that were defined in assembly language. In practice, there are some optimizations performed, but in theory it is just that simple. Words that are always interpreted in a conventional Forth are treated special by the compiler.
The Commando Forth compiler I present in this article (the complete source code -- including an archive containing Version 2 of One-Der with a data stack, interrupts, and hardware debugging, and axasm, the cross assembler, and 1forth the forth compiler -- is available here really shows off the flexibility of the One-Der CPU. In that article, the CPU has one hardware stack. However, Forth uses two stacks. Most systems just emulate the second stack in software, but with One-Der it is a simple matter to just instantiate a new copy of the stack functional unit. In the oneder.v file (available online), I simply added one line right under the existing stack instantiation (stackmem):
FMemStack stackmem(xreset, clk0,invclk,phase,dbus,src0,dst0,13'h4,dta,dta,memaddr, mdout, menable, mwrite, callsel); // "Forth" stack FMemStack stackmem9(xreset, clk0,invclk,phase,dbus,src0,dst0,13'h9,dta,dta,memaddr, mdout, menable, mwrite, 1'b0);
The new stack is an exact copy of the regular stack, but it has a different address on the One-Der functional unit bus. The new stack uses address 9 instead of 4. Of course, the new stack has its own pointer and offset, just as an instance of a C++ or Java object would have its own instance variables.
This is the real power to the One-Der's architecture: It is trivial to duplicate or add or even delete new functions. There are always trade offs, of course. In the original One-Der, there are two accumulators, each with its own math logic. That's good for speed, but wastes FPGA resources since only one accumulator can be working at one time (for now, at least). The current version of One-Der (see the online listings) uses a separate arithmetic logic unit (ALU) that both accumulators share. This saves about 550 slices in the FPGA but reduces the maximum achievable speed to some degree.
The compiler ended up requiring just four files. The cross compiler itself is written in awk (available online).
\ Simple hi lo program \!quick \!include monitor.4th \!ignorecase \ Initialized variables -- note unlike some forth's you do need to use @ and ! with these 1000 value high 1 value low variable counter \ Strings \ The format is odd compared to most Forths \ The first line looks like: \ " word end_word \ Then the 2nd and subsequent lines become the string (CRLF between lines) until you read \ a line that has the end_word (which does not become part of the string) " signon " Welcome to Hi Lo by Al Williams. A game written using just one instruction! " 13 10 0 ( just a silly example of end_word ) " prompt endstring! 13 10 Think of a number from 1 to 999... endstring! 13 10 0 " guess " I guess: " 0 " query " Is that <H>igh, <L>ow, or <C>orrect? " 32 0 " win " I win!\r\nTries: " 32 0 " pagain " 13 10 Play again (<N> to quit) " 13 10 0 ( bump counter ) : bump counter @ 1+ dup .disp counter ! ; ( accept user response and adjust high/low ) : doquery query ." key dup char 'c' = if drop 0 then dup char 'h' = if drop high ! bump 1 return then dup char 'l' = if drop low ! bump 1 return then ( assume character is not zero ) ; ( take a turn ) : turn guess ." high @ low @ + 2/ dup . crlf doquery ; : iwin win ." counter @ . crlf ; ( main play loop ) : playloop prompt ." 1 begin while turn repeat iwin ; ( sign on and start game -- comment out setscanner for debugging ) : main signon ." begin 1000 high ! 1 low ! 1 counter ! 1 .disp playloop pagain ." key char 'n' = until monitor ;